Skip to content

fix(dialect): don't parse @@ system variables as macros - #6117

Open
manan28025 wants to merge 1 commit into
SQLMesh:mainfrom
manan28025:tsql-double-at-variables
Open

manan28025 wants to merge 1 commit into
SQLMesh:mainfrom
manan28025:tsql-double-at-variables

Conversation

@manan28025

Copy link
Copy Markdown
Contributor

Description

Fixes #5601.

In _parse_macro, the second @ of @@DATEFIRST was parsed as the start of a macro variable, so system variables like T-SQL's @@DATEFIRST / @@ROWCOUNT and BigQuery's @@query_label became a MacroVar. The model then failed to render with Unsupported expression type MacroVar.

@@name can't be a macro reference, so it's now parsed as a system variable (Parameter(Parameter(...))), the same way sqlglot parses it for these dialects. A single @ is still a macro.

Test Plan

  • Added test_double_at_system_variable_is_not_a_macro (tsql, bigquery, mysql) and test_double_at_system_variable_in_model, which renders a T-SQL model using @@DATEFIRST next to a regular @x macro. The tsql, bigquery and model tests fail on main. MySQL already worked, since it tokenizes @@session... differently.
  • Ran the fast and slow tests in tests/core. The only failures are MSSQL/Fabric connection tests that need pyodbc / mssql-python, which I don't have installed. They fail the same way on main.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test) (ran the tests/core fast and slow suites, see above)
  • My commits are signed off (git commit -s) per the DCO

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>
@mday-io

mday-io commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Sorry for the slow review. I checked that the new tests fail on main and pass here. This also stops sqlmesh format from rewriting @@DATEFIRST to @DATEFIRST.

One change before we merge. The new branch fires whenever the next token is @, so a few macro forms change meaning. @ @x with a space becomes a system variable, and on Postgres SELECT @ @x renders $$x. @@{x}, @@EACH(...) and @@my_macro(1) now raise a ParseError. Could the check only fire for @@name written with no spaces and no ( after the name? Something like:

def _at_system_variable(self: Parser) -> bool:
    at, name = self._curr, self._next
    if not at or not name or at.text != SQLMESH_MACRO_PREFIX or name.token_type != TokenType.VAR:
        return False
    if at.start != self._prev.end + 1 or name.start != at.end + 1:
        return False
    after = seq_get(self._tokens, self._index + 2)
    return not after or after.token_type != TokenType.L_PAREN

With that, every macro form I tried parses the same as on main, and your tests still pass. Could you add cases for @ @x, @@{x} and @@EACH(...) to test_double_at_system_variable_is_not_a_macro, asserting they parse the same as before? Once that's pushed I'll start CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

don't treat identifiers starting with @@ as macro

3 participants