Skip to content
Open
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
5 changes: 5 additions & 0 deletions sqlmesh/core/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def _parse_macro(self: Parser, keyword_macro: str = "") -> t.Optional[exp.Expr]:
if self._prev.text != SQLMESH_MACRO_PREFIX:
return self._parse_parameter()

# @@name is a system variable, e.g. T-SQL's @@DATEFIRST or BigQuery's @@query_label,
# and can't be a macro reference, so parse it like the dialect would
if self._curr and self._curr.text == SQLMESH_MACRO_PREFIX and self._match(TokenType.PARAMETER):
return self.expression(exp.Parameter(this=self._parse_parameter()))

comments = self._prev.comments
index = self._index
field = self._parse_primary() or self._parse_function(functions={}) or self._parse_id_var()
Expand Down
32 changes: 32 additions & 0 deletions tests/core/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,38 @@ def test_macro_parse():
)


@pytest.mark.parametrize(
"dialect, sql",
[
("tsql", "SELECT @@DATEFIRST AS x"),
("bigquery", "SELECT @@query_label AS x"),
("mysql", "SELECT @@session.time_zone AS x"),
],
)
def test_double_at_system_variable_is_not_a_macro(dialect: str, sql: str):
query = parse_one(sql, read=dialect)
assert not list(query.find_all(d.MacroVar))
assert query.sql(dialect) == sql


def test_double_at_system_variable_in_model():
model = load_sql_based_model(
parse(
"""
MODEL (name db.m, dialect tsql);
SELECT (DATEPART(WEEKDAY, d) + @@DATEFIRST - 2) % 7 + 1 AS iso_weekday, @x AS x FROM t
""",
default_dialect="tsql",
),
dialect="tsql",
variables={"x": 1},
)
assert model.render_query_or_raise().sql("tsql") == (
"SELECT (DATEPART(WEEKDAY, [d]) + @@DATEFIRST - 2) % 7 + 1 AS [iso_weekday], 1 AS [x] "
"FROM [t] AS [t]"
)


def test_conditional_statement():
q = parse_one(
"""
Expand Down