Skip to content

Commit ea0a0d1

Browse files
committed
fix(format): render macro properties in the header with the model dialect
A macro in property position wraps user-authored arguments, so it carries warehouse SQL the same way `columns` or `audits` do. It took a separate branch in _props_sql and kept rendering generically, which left it on the compounding path: DATETIME2 -> TIMESTAMP -> VARBINARY across two format runs. Signed-off-by: mday-io <mdaytn@gmail.com>
1 parent 9cdbb00 commit ea0a0d1

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

‎sqlmesh/core/dialect.py‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -785,30 +785,42 @@ def _props_sql(self: Generator, expressions: t.List[exp.Expr]) -> str:
785785
size = len(expressions)
786786

787787
for i, prop in enumerate(expressions):
788+
parent = prop.parent
789+
meta_dialect = parent.meta.get(_SQLMESH_META_DIALECT) if parent else None
790+
791+
def render_with_model_dialect(node: exp.Expr, **overrides: t.Any) -> str:
792+
opts: t.Dict[str, t.Any] = {
793+
"dialect": meta_dialect,
794+
"pretty": self.pretty,
795+
"identify": self.identify,
796+
"normalize": self.normalize,
797+
"pad": self.pad,
798+
"indent": self._indent,
799+
"normalize_functions": self.normalize_functions,
800+
"leading_comma": self.leading_comma,
801+
"max_text_width": self.max_text_width,
802+
"comments": self.comments,
803+
}
804+
opts.update(overrides)
805+
return node.sql(**opts)
806+
788807
if isinstance(prop, MacroFunc):
789-
sql = self.indent(self.sql(prop, comment=False))
808+
# A macro in property position wraps user-authored arguments, so it carries
809+
# warehouse SQL the same way `columns` or `audits` do.
810+
sql = self.indent(
811+
render_with_model_dialect(prop, comments=False)
812+
if meta_dialect
813+
else self.sql(prop, comment=False)
814+
)
790815
else:
791816
value = prop.args.get("value")
792-
parent = prop.parent
793-
meta_dialect = parent.meta.get(_SQLMESH_META_DIALECT) if parent else None
794817

795818
if (
796819
meta_dialect
797820
and isinstance(value, exp.Expr)
798821
and _meta_render_policy().get(prop.name.lower())
799822
):
800-
value_sql = value.sql(
801-
dialect=meta_dialect,
802-
pretty=self.pretty,
803-
identify=self.identify,
804-
normalize=self.normalize,
805-
pad=self.pad,
806-
indent=self._indent,
807-
normalize_functions=self.normalize_functions,
808-
leading_comma=self.leading_comma,
809-
max_text_width=self.max_text_width,
810-
comments=self.comments,
811-
)
823+
value_sql = render_with_model_dialect(value)
812824
else:
813825
value_sql = self.sql(prop, "value")
814826

‎tests/core/test_dialect.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ def test_format_model_expressions_meta_render_policy(dialect: str, audit_type: s
427427
"kind SCD_TYPE_2_BY_COLUMN(unique_key id, columns (a, b), time_data_type DATETIME2(6))",
428428
"physical_properties (labels = (('env', 'prod')))",
429429
"allow_partials true, description 'my description'",
430+
"@my_prop(cutoff := CAST('2024-01-01' AS DATETIME2))",
430431
],
431432
)
432433
def test_format_model_expressions_is_idempotent(header: str):

0 commit comments

Comments
 (0)