Skip to content

TSQL dialect formatting corrupts boolean true in MODEL header to (1=1) #5773

@fresioAS

Description

@fresioAS

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

  1. 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
  1. Run sqlmesh format on the project.

  2. The header is rewritten as:

MODEL (
  name my_schema.my_model,
  kind FULL,
  standalone (1=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.pyformat_model_expressions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions