Skip to content

Commit a2d483b

Browse files
committed
fix(dialect): don't parse @@ system variables as macros
The macro parser treated the second @ in @@DATEFIRST as the start of a macro variable, so T-SQL configuration functions like @@DATEFIRST and BigQuery's @@query_label became a MacroVar and the model failed to render with "Unsupported expression type MacroVar". @@name is now parsed as a system variable, the same way the dialect parses it. Signed-off-by: neatninja <manan81140@gmail.com>
1 parent 271b855 commit a2d483b

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎sqlmesh/core/dialect.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ def _parse_macro(self: Parser, keyword_macro: str = "") -> t.Optional[exp.Expr]:
218218
if self._prev.text != SQLMESH_MACRO_PREFIX:
219219
return self._parse_parameter()
220220

221+
# @@name is a system variable, e.g. T-SQL's @@DATEFIRST or BigQuery's @@query_label,
222+
# and can't be a macro reference, so parse it like the dialect would
223+
if self._curr and self._curr.text == SQLMESH_MACRO_PREFIX and self._match(TokenType.PARAMETER):
224+
return self.expression(exp.Parameter(this=self._parse_parameter()))
225+
221226
comments = self._prev.comments
222227
index = self._index
223228
field = self._parse_primary() or self._parse_function(functions={}) or self._parse_id_var()

‎tests/core/test_dialect.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,38 @@ def test_macro_parse():
13241324
)
13251325

13261326

1327+
@pytest.mark.parametrize(
1328+
"dialect, sql",
1329+
[
1330+
("tsql", "SELECT @@DATEFIRST AS x"),
1331+
("bigquery", "SELECT @@query_label AS x"),
1332+
("mysql", "SELECT @@session.time_zone AS x"),
1333+
],
1334+
)
1335+
def test_double_at_system_variable_is_not_a_macro(dialect: str, sql: str):
1336+
query = parse_one(sql, read=dialect)
1337+
assert not list(query.find_all(d.MacroVar))
1338+
assert query.sql(dialect) == sql
1339+
1340+
1341+
def test_double_at_system_variable_in_model():
1342+
model = load_sql_based_model(
1343+
parse(
1344+
"""
1345+
MODEL (name db.m, dialect tsql);
1346+
SELECT (DATEPART(WEEKDAY, d) + @@DATEFIRST - 2) % 7 + 1 AS iso_weekday, @x AS x FROM t
1347+
""",
1348+
default_dialect="tsql",
1349+
),
1350+
dialect="tsql",
1351+
variables={"x": 1},
1352+
)
1353+
assert model.render_query_or_raise().sql("tsql") == (
1354+
"SELECT (DATEPART(WEEKDAY, [d]) + @@DATEFIRST - 2) % 7 + 1 AS [iso_weekday], 1 AS [x] "
1355+
"FROM [t] AS [t]"
1356+
)
1357+
1358+
13271359
def test_conditional_statement():
13281360
q = parse_one(
13291361
"""

0 commit comments

Comments
 (0)