File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -783,7 +783,10 @@ def format_model_expressions(
783783 A string representing the formatted model.
784784 """
785785 if len (expressions ) == 1 and is_meta_expression (expressions [0 ]):
786- return expressions [0 ].sql (pretty = True , dialect = dialect )
786+ # Meta expressions (MODEL/AUDIT/METRIC) are SQLMesh DDL, not standard SQL,
787+ # so they must never be transpiled to the target dialect (e.g. tsql would
788+ # rewrite a boolean property like `allow_partials TRUE` to `(1 = 1)`).
789+ return expressions [0 ].sql (pretty = True , dialect = None )
787790
788791 if rewrite_casts :
789792
@@ -815,7 +818,14 @@ def cast_to_colon(node: exp.Expr) -> exp.Expr:
815818 expressions = new_expressions
816819
817820 return ";\n \n " .join (
818- expression .sql (pretty = True , dialect = dialect , ** kwargs ) for expression in expressions
821+ # Meta expressions (MODEL/AUDIT/METRIC) are SQLMesh DDL and must stay
822+ # dialect-agnostic; only the actual query/statement expressions transpile.
823+ expression .sql (
824+ pretty = True ,
825+ dialect = None if is_meta_expression (expression ) else dialect ,
826+ ** kwargs ,
827+ )
828+ for expression in expressions
819829 ).strip ()
820830
821831
Original file line number Diff line number Diff line change @@ -230,6 +230,60 @@ def test_format_model_expressions():
230230 1::Int32 AS bar"""
231231 )
232232
233+ x = format_model_expressions (
234+ parse (
235+ """
236+ MODEL(name a.b, kind FULL, dialect tsql, allow_partials true);
237+ SELECT TRUE AS col, CAST(x AS INT) AS y FROM t
238+ """
239+ ),
240+ dialect = "tsql" ,
241+ )
242+ # The MODEL header is SQLMesh DDL and must not be transpiled: a boolean property
243+ # such as `allow_partials true` must stay `TRUE`, not become tsql's `(1 = 1)`.
244+ # The query body must still transpile to the target dialect.
245+ assert (
246+ x
247+ == """MODEL (
248+ name a.b,
249+ kind FULL,
250+ dialect tsql,
251+ allow_partials TRUE
252+ );
253+
254+ SELECT
255+ 1 AS col,
256+ x::INTEGER AS y
257+ FROM t"""
258+ )
259+
260+ x = format_model_expressions (
261+ parse (
262+ """
263+ AUDIT(name my_audit, dialect tsql, blocking false);
264+ SELECT TRUE AS col, CAST(x AS INT) AS y FROM t WHERE x > 0
265+ """
266+ ),
267+ dialect = "tsql" ,
268+ )
269+ # AUDIT headers are SQLMesh DDL too: a `false` boolean property must stay
270+ # `FALSE`, not become tsql's `(1 = 0)`, while the query body still transpiles.
271+ assert (
272+ x
273+ == """AUDIT (
274+ name my_audit,
275+ dialect tsql,
276+ blocking FALSE
277+ );
278+
279+ SELECT
280+ 1 AS col,
281+ x::INTEGER AS y
282+ FROM t
283+ WHERE
284+ x > 0"""
285+ )
286+
233287 x = format_model_expressions (
234288 parse (
235289 """
You can’t perform that action at this time.
0 commit comments