Skip to content

fix(snowflake): do not quote parameter names in ALTER SESSION - #6122

Open
DG47 wants to merge 1 commit into
SQLMesh:mainfrom
DG47:fix/snowflake-alter-session-quoting
Open

DG47 wants to merge 1 commit into
SQLMesh:mainfrom
DG47:fix/snowflake-alter-session-quoting

Conversation

@DG47

@DG47 DG47 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #5666.

Snowflake session parameters (TIMEZONE, QUERY_TAG, ...) are keywords, not identifiers, so ALTER SESSION SET "TIMEZONE" = 'UTC' is a syntax error. sqlglot parses the parameter names in ALTER SESSION SET/UNSET as identifiers (Column(Identifier) / Identifier), so a model pre/post statement such as

ALTER SESSION SET TIMEZONE = 'UTC';

was quoted twice: once by the renderer's normalize-and-quote step and again by EngineAdapter._to_sql. It reached Snowflake as ALTER SESSION SET "TIMEZONE" = 'UTC'.

This adds a small parser override in sqlmesh/core/dialect.py, following the same pattern as the existing T-SQL ALTER COLUMN override. After sqlglot parses ALTER SESSION, unquoted parameter names become exp.Var, so they are neither normalized nor quoted. Values are left alone (literals, macros such as @tag, etc.), and so are parameter names the user quoted explicitly.

The SQL produced for the unrendered statement doesn't change (TIMEZONE = 'UTC' both before and after), so model data and metadata hashes stay the same and existing snapshots aren't affected. I checked this on a sample model.

An earlier attempt (#5670) turned quoting off for every Alter/Set statement in the Snowflake adapter and also special-cased the renderer. Its author closed it. This change only touches ALTER SESSION parameter names and lives at the parse level, so the renderer and the engine adapter both pick it up without engine-specific code in either.

Test Plan

  • tests/core/test_dialect.py::test_snowflake_alter_session_parameters_are_not_quoted: parse/generate with identify=True for SET (with a macro value), UNSET, and an explicitly quoted name, plus rendered model pre/post statements.
  • tests/core/engine_adapter/test_snowflake.py::test_alter_session_parameters_are_not_quoted: SQL executed by the mocked Snowflake adapter.
  • Both tests fail on main (ALTER SESSION SET "TIMEZONE" = 'UTC') and pass with the fix.
  • pytest -n 8 -m "fast and not web and not github and not dbt and not jupyter and not cicdonly and not isolated": 2436 passed. 6 failures in tests/core/test_connection_config.py (pyodbc) are unrelated and come from a missing local unixODBC library; they fail the same way on main.
  • pytest -m "dbt and fast" tests/dbt: 88 passed.
  • SKIP=prettier,eslint pre-commit run --all-files (ruff, ruff-format, mypy, migrations): passed. The prettier/eslint hooks were skipped because no frontend files changed.

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) (I ran the core fast and dbt fast subsets listed above, not the whole target)
  • My commits are signed off (git commit -s) per the DCO

Summary by CodeRabbit

  • Bug Fixes
    • Snowflake ALTER SESSION SET and UNSET statements now retain unquoted parameter names when generated or executed, including in model pre- and post-statements.
    • Explicitly quoted parameter names remain quoted, and non-identifier expressions are unchanged. This keeps generated statements consistent with the SQL form provided and avoids unintended quoting of session parameter names.

@Vamsi-klu Vamsi-klu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, doing this at parse time is a lot cleaner than the adapter + renderer route from #5670. I double checked the hash claim: a Snowflake model with ALTER SESSION SET TIMEZONE = 'UTC', query_tag = 'x' as a pre statement has the same data and metadata hash on main and on this branch, only the rendered SQL changes. One scoping nit inline, looks good otherwise.

Comment thread sqlmesh/core/dialect.py Outdated
_override(Parser, _parse_interval_span)
_override(Parser, _warn_unsupported)
_override(Snowflake.Parser, _parse_table_parts)
_override(Parser, _parse_alter_session)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On sqlglot 30.8 only the Snowflake parser sends ALTER SESSION to _parse_alter_session, Oracle and Databricks still end up as Command. So it's Snowflake only in practice, but overriding the base Parser makes it read as global. Could this be _override(Snowflake.Parser, _parse_alter_session) like the _parse_table_parts one above? Keeps the scope obvious and won't quietly pick up other dialects if sqlglot routes them here later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, done in 637af55: the override is now registered on Snowflake.Parser next to _parse_table_parts, and the function is annotated accordingly. I checked that Databricks and Oracle still come out as Command with the original text untouched.

Comment thread sqlmesh/core/dialect.py
if isinstance(name, exp.Column) and not name.table:
name = name.this
if isinstance(name, exp.Identifier) and not name.quoted:
name.replace(exp.var(name.name))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, no change needed: since these are Vars now they don't get normalized, so query_tag stays lowercase where main uppercased it. Snowflake doesn't care about case for parameter names so it's harmless, but maybe worth a line in the comment above so nobody "fixes" it later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a sentence to the comment in the same commit explaining that vars skip case normalization, that query_tag therefore stays lowercase, and that Snowflake treats parameter names case-insensitively so this is intentional.

@DG47

DG47 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

@cmgoffena13 could you take a look at this one? It fixes #5666: Snowflake session parameters in ALTER SESSION SET/UNSET were quoted as identifiers (ALTER SESSION SET "TIMEZONE" = 'UTC'), which Snowflake rejects. @Vamsi-klu's two review points from 10-18 are addressed in 637af55: the override is registered on Snowflake.Parser only, and there's a comment on the lowercase parameter names.

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Snowflake parser converts unquoted ALTER SESSION SET and UNSET parameter names to vars. Tests cover SQL rendering through the parser, engine adapter, and model-loading paths. Explicitly quoted names remain quoted.

Changes: Snowflake ALTER SESSION parameter handling

Layer / File(s) Summary
Parse unquoted session parameters
sqlmesh/core/dialect.py
The Snowflake parser converts unquoted identifier parameter names in SET and UNSET statements to vars. The parser override is registered on Snowflake.Parser.
Verify SQL rendering paths
tests/core/test_dialect.py, tests/core/engine_adapter/test_snowflake.py
Tests check rendering with identifier quoting enabled, preserve explicitly quoted names, and verify engine adapter and model pre-/post-statement output.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to cc960

Snowflake ALTER SESSION SET and UNSET statements will no longer quote parameter names such as TIMEZONE, which fixes the invalid SQL. The change is narrowly scoped, tested and does not alter existing hashes, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(snowflake): do not quote parameter names in ALTER SESSION" matches the main change. The PR adds a Snowflake parser override so unquoted ALTER SESSION SET/UNSET parameter names parse as …
Description check Passed The description covers the required template sections. The Description section explains the bug, the cause (sqlglot parses parameter names as identifiers), and the fix (a parser override in sqlmesh/co…
Linked Issues check Passed Issue [#5666] requires that SQLMesh stop quoting Snowflake session parameter names in ALTER SESSION SET, because ALTER SESSION SET "TIMEZONE" = 'UTC' is invalid Snowflake syntax. The PR adds `_par…
Out of Scope Changes check Passed The whole-PR change set touches three files, about +69 lines: sqlmesh/core/dialect.py (+22, the Snowflake ALTER SESSION parser override and its registration), tests/core/test_dialect.py (+35, pa…
Full details: Title check

Explanation

The title "fix(snowflake): do not quote parameter names in ALTER SESSION" matches the main change. The PR adds a Snowflake parser override so unquoted ALTER SESSION SET/UNSET parameter names parse as exp.Var and are not quoted. The title names the affected engine, the statement, and the fix. It is specific and concise, and it follows the conventional commit style.

Full details: Description check

Explanation

The description covers the required template sections. The Description section explains the bug, the cause (sqlglot parses parameter names as identifiers), and the fix (a parser override in sqlmesh/core/dialect.py). The Test Plan lists the new tests, their expected failure on main, and the commands run. The Checklist is present. The author marks the 'All existing tests pass (make fast-test)' item unchecked and explains that only the core fast and dbt fast subsets ran. This gap is disclosed, so it is not a missing section. The description also links issue #5666 and states the effect on snapshots.

Full details: Linked Issues check

Explanation

Issue [#5666] requires that SQLMesh stop quoting Snowflake session parameter names in ALTER SESSION SET, because ALTER SESSION SET "TIMEZONE" = 'UTC' is invalid Snowflake syntax. The PR adds _parse_alter_session to sqlmesh/core/dialect.py, registered on Snowflake.Parser. Per the current-review summary, it converts unquoted parameter names in SET assignments and UNSET items from identifiers to exp.Var, so the normalize-and-quote step no longer quotes them. Values, macro values, and explicitly quoted names are left unchanged. Tests in tests/core/test_dialect.py and tests/core/engine_adapter/test_snowflake.py cover SET, UNSET, explicit quoting, and rendered pre/post statements. The PR description reports that these tests fail on main and pass with the change. Limitation: the whole-PR diff and a full read of dialect.py at the reviewed head could not be read (diff blob unavailable; file read truncated before the new function). This verdict relies on the change summary, the PR description, and the prior assessment, and it assumes the code at head matches them. The issue has no non-coding requirements.

Full details: Out of Scope Changes check

Explanation

The whole-PR change set touches three files, about +69 lines: sqlmesh/core/dialect.py (+22, the Snowflake ALTER SESSION parser override and its registration), tests/core/test_dialect.py (+35, parser and rendering tests), and tests/core/engine_adapter/test_snowflake.py (+12, adapter SQL test). The current-review summary reports no changes to public declarations. Each change supports the override for issue [#5666]. The rebase and the added override comment, as described in the PR, do not add unrelated behavior. Limitation: the diff could not be read directly at the reviewed head, so this verdict relies on the summary and PR description.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2c427099-0662-4402-8703-a4ccf4102a20
Commits

Reviewing files that changed from the base of the PR and between ad2c834 and cc96074.

Files selected for processing: 2

  • sqlmesh/core/dialect.py
  • tests/core/engine_adapter/test_snowflake.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@DG47
DG47 requested a review from Vamsi-klu October 27, 2026 18:11
@DG47

DG47 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Checking back on the fix for #5666: unquoted parameter names in ALTER SESSION SET/UNSET now parse as exp.Var via an override in sqlmesh/core/dialect.py, so Snowflake no longer receives ALTER SESSION SET "TIMEZONE" = 'UTC'. @cmgoffena13 @mday-io, could one of you review it?

@mday-io

mday-io commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Sorry for the wait on this one. I checked that the new tests fail on main and pass here, and that model data and metadata hashes don't change, so existing projects won't rebuild.

The bug reproduces in sqlglot alone, so I filed it upstream as tobymao/sqlglot#9336. We're pinned to sqlglot 30.8.x, so your override is the fix for now. Could you add a line to its comment that links that issue and says to remove the override once our sqlglot pin includes the fix?

637af55 is also missing its DCO sign-off. This should fix it:

git fetch upstream
git rebase --signoff upstream/main
git push --force-with-lease

Once that's pushed I'll start CI.

Session parameters such as TIMEZONE or QUERY_TAG are keywords, not
identifiers, so Snowflake rejects them when quoted. sqlglot parses them
as identifiers, which means model pre/post statements like
ALTER SESSION SET TIMEZONE = 'UTC' were rendered and executed as
ALTER SESSION SET "TIMEZONE" = 'UTC', a syntax error.

Parse unquoted parameter names in ALTER SESSION SET/UNSET as vars so
they are neither normalized nor quoted.

Fixes SQLMesh#5666

The override is scoped to Snowflake and links tobymao/sqlglot#9336; remove it once
the pinned sqlglot includes that fix.

Signed-off-by: Dhruv Gupta <todhruvg@gmail.com>
@DG47
DG47 force-pushed the fix/snowflake-alter-session-quoting branch from ad2c834 to cc96074 Compare November 24, 2026 15:50
@DG47

DG47 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@mday-io thanks for checking the hashes and for filing tobymao/sqlglot#9336. Both done in cc96074:

  • The comment above _parse_alter_session now links sqlglot#9336 and says to remove the override once SQLMesh's sqlglot pin includes the fix.
  • The branch is one signed-off commit on current main. git rebase --signoff conflicted while replaying the earlier merges from main, so I squashed the PR's three files onto main instead. The diff is the same as before plus the comment.

pytest tests/core/test_dialect.py tests/core/engine_adapter/test_snowflake.py: 273 passed, 2 skipped. ruff check and ruff format --check are clean.

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.

quoting parameters in ALTER SESSION SET for Snowflake is illegal syntax.

3 participants