Skip to content

Commit f6078cd

Browse files
committed
fix(format): keep MODEL/AUDIT header dialect-agnostic (#5773)
format_model_expressions rendered every parsed expression with the target dialect, including the SQLMesh-specific MODEL/AUDIT/METRIC header. That header is SQLMesh DDL, not standard SQL, so transpiling it corrupts boolean properties: under tsql (which has no TRUE keyword) a property like `allow_partials true` was rewritten to `allow_partials (1 = 1)`. Render meta expressions (is_meta_expression) with dialect=None so the header stays as written, while the actual query/statement body still transpiles to the target dialect. Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
1 parent 991a327 commit f6078cd

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

sqlmesh/core/dialect.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff 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

tests/core/test_dialect.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,33 @@ 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+
233260
x = format_model_expressions(
234261
parse(
235262
"""

0 commit comments

Comments
 (0)