Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions sqlmesh/core/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,10 @@ def format_model_expressions(
A string representing the formatted model.
"""
if len(expressions) == 1 and is_meta_expression(expressions[0]):
return expressions[0].sql(pretty=True, dialect=dialect)
# Meta expressions (MODEL/AUDIT/METRIC) are SQLMesh DDL, not standard SQL,
# so they must never be transpiled to the target dialect (e.g. tsql would
# rewrite a boolean property like `allow_partials TRUE` to `(1 = 1)`).
return expressions[0].sql(pretty=True, dialect=None)

if rewrite_casts:

Expand Down Expand Up @@ -815,7 +818,14 @@ def cast_to_colon(node: exp.Expr) -> exp.Expr:
expressions = new_expressions

return ";\n\n".join(
expression.sql(pretty=True, dialect=dialect, **kwargs) for expression in expressions
# Meta expressions (MODEL/AUDIT/METRIC) are SQLMesh DDL and must stay
# dialect-agnostic; only the actual query/statement expressions transpile.
expression.sql(
pretty=True,
dialect=None if is_meta_expression(expression) else dialect,
**kwargs,
)
for expression in expressions
).strip()


Expand Down
54 changes: 54 additions & 0 deletions tests/core/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,60 @@ def test_format_model_expressions():
1::Int32 AS bar"""
)

x = format_model_expressions(
parse(
"""
MODEL(name a.b, kind FULL, dialect tsql, allow_partials true);
SELECT TRUE AS col, CAST(x AS INT) AS y FROM t
"""
),
dialect="tsql",
)
# The MODEL header is SQLMesh DDL and must not be transpiled: a boolean property
# such as `allow_partials true` must stay `TRUE`, not become tsql's `(1 = 1)`.
# The query body must still transpile to the target dialect.
assert (
x
== """MODEL (
name a.b,
kind FULL,
dialect tsql,
allow_partials TRUE
);

SELECT
1 AS col,
x::INTEGER AS y
FROM t"""
)

x = format_model_expressions(
parse(
"""
AUDIT(name my_audit, dialect tsql, blocking false);
SELECT TRUE AS col, CAST(x AS INT) AS y FROM t WHERE x > 0
"""
),
dialect="tsql",
)
# AUDIT headers are SQLMesh DDL too: a `false` boolean property must stay
# `FALSE`, not become tsql's `(1 = 0)`, while the query body still transpiles.
assert (
x
== """AUDIT (
name my_audit,
dialect tsql,
blocking FALSE
);

SELECT
1 AS col,
x::INTEGER AS y
FROM t
WHERE
x > 0"""
)

x = format_model_expressions(
parse(
"""
Expand Down
Loading