Description
When formatting a TSQL model using sqlmesh format, boolean values (true) in the MODEL (...) header are transpiled to (1=1) — the T-SQL representation of boolean true. Once corrupted, SQLMesh's parser cannot re-parse (1=1) as a boolean in the MODEL DDL context, breaking the model file.
Steps to Reproduce
- Create a TSQL model with a boolean property in the header, e.g.:
MODEL (
name my_schema.my_model,
kind FULL,
standalone true
);
SELECT 1 AS col
-
Run sqlmesh format on the project.
-
The header is rewritten as:
MODEL (
name my_schema.my_model,
kind FULL,
standalone (1=1)
);
- SQLMesh can no longer parse the model file.
Root Cause
In sqlmesh/core/dialect.py, format_model_expressions renders all expressions — including the MODEL (...) / AUDIT (...) header — using the target dialect:
# sqlmesh/core/dialect.py ~line 781
return ";\n\n".join(
expression.sql(pretty=True, dialect=dialect, **kwargs) for expression in expressions
).strip()
The MODEL (...) header is SQLMesh-specific DDL, not standard SQL. When dialect="tsql" is used, SQLGlot's TSQL generator converts exp.Boolean(this=True) → (1=1) because T-SQL has no native TRUE keyword.
The same issue exists in the early-return path for single-expression files (~line 750):
if len(expressions) == 1 and is_meta_expression(expressions[0]):
return expressions[0].sql(pretty=True, dialect=dialect)
Affected Properties
Any boolean property set to true in a MODEL (...) or AUDIT (...) header, including:
- standalone true
- formatting true
- Any other boolean model/audit property
Suggested Fix
Meta expressions (the MODEL/AUDIT header) should be rendered with dialect=None since they are SQLMesh-specific syntax and must not be transpiled. Only the actual SQL query expressions should use the target dialect.
return ";\n\n".join(
expression.sql(
pretty=True,
dialect=None if is_meta_expression(expression) else dialect,
**kwargs
)
for expression in expressions
).strip()
And similarly for the early-return path:
if len(expressions) == 1 and is_meta_expression(expressions[0]):
return expressions[0].sql(pretty=True, dialect=None)
Environment
- Dialect: tsql
- Command: sqlmesh format
- File: sqlmesh/core/dialect.py — format_model_expressions
Description
When formatting a TSQL model using sqlmesh format, boolean values (true) in the MODEL (...) header are transpiled to (1=1) — the T-SQL representation of boolean true. Once corrupted, SQLMesh's parser cannot re-parse (1=1) as a boolean in the MODEL DDL context, breaking the model file.
Steps to Reproduce
Run sqlmesh format on the project.
The header is rewritten as:
Root Cause
In sqlmesh/core/dialect.py, format_model_expressions renders all expressions — including the MODEL (...) / AUDIT (...) header — using the target dialect:
The MODEL (...) header is SQLMesh-specific DDL, not standard SQL. When dialect="tsql" is used, SQLGlot's TSQL generator converts exp.Boolean(this=True) → (1=1) because T-SQL has no native TRUE keyword.
The same issue exists in the early-return path for single-expression files (~line 750):
Affected Properties
Any boolean property set to true in a MODEL (...) or AUDIT (...) header, including:
Suggested Fix
Meta expressions (the MODEL/AUDIT header) should be rendered with dialect=None since they are SQLMesh-specific syntax and must not be transpiled. Only the actual SQL query expressions should use the target dialect.
And similarly for the early-return path:
Environment