Skip to content

Commit cc96074

Browse files
committed
fix(snowflake): do not quote parameter names in ALTER SESSION
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 #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>
1 parent ba3953b commit cc96074

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎sqlmesh/core/dialect.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,27 @@ def _parse_alter_table_alter(self: Parser) -> t.Optional[exp.Expr]:
569569
return alter_column
570570

571571

572+
# Snowflake session parameter names in ALTER SESSION SET / UNSET (e.g. TIMEZONE or QUERY_TAG) are
573+
# keywords, not identifiers, so Snowflake rejects them when quoted: ALTER SESSION SET "TIMEZONE" = 'UTC'
574+
# is a syntax error. sqlglot parses them as identifiers, which means they get normalized and quoted
575+
# along with the rest of the statement, so we turn them into vars instead. Vars are also left out of
576+
# case normalization, so a parameter written as query_tag stays lowercase; Snowflake treats parameter
577+
# names case-insensitively, so this is intentional.
578+
# The parsing bug is in sqlglot itself (https://github.com/tobymao/sqlglot/issues/9336). Remove this
579+
# override once the sqlglot version SQLMesh pins includes the fix.
580+
def _parse_alter_session(self: Snowflake.Parser) -> exp.AlterSession:
581+
alter_session = self.__parse_alter_session() # type: ignore
582+
583+
for item in alter_session.expressions:
584+
name = item.this.this if isinstance(item.this, exp.EQ) else item.this
585+
if isinstance(name, exp.Column) and not name.table:
586+
name = name.this
587+
if isinstance(name, exp.Identifier) and not name.quoted:
588+
name.replace(exp.var(name.name))
589+
590+
return alter_session
591+
592+
572593
def altercolumn_sql(self: Generator, expression: exp.AlterColumn) -> str:
573594
sql = self._altercolumn_sql(expression) # type: ignore
574595

@@ -1437,6 +1458,7 @@ def extend_sqlglot() -> None:
14371458
_override(Parser, _parse_interval_span)
14381459
_override(Parser, _warn_unsupported)
14391460
_override(Snowflake.Parser, _parse_table_parts)
1461+
_override(Snowflake.Parser, _parse_alter_session)
14401462
_override(TSQL.Parser, _parse_alter_table_alter)
14411463
_override(TSQL.Generator, altercolumn_sql)
14421464

‎tests/core/engine_adapter/test_snowflake.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ def test_session(
213213
assert to_sql_calls(adapter) == expected_exception_calls
214214

215215

216+
def test_alter_session_parameters_are_not_quoted(make_mocked_engine_adapter: t.Callable):
217+
adapter = make_mocked_engine_adapter(SnowflakeEngineAdapter)
218+
219+
adapter.execute(parse_one("ALTER SESSION SET TIMEZONE = 'UTC'", read="snowflake"))
220+
adapter.execute(parse_one("ALTER SESSION UNSET TIMEZONE", read="snowflake"))
221+
222+
assert to_sql_calls(adapter) == [
223+
"ALTER SESSION SET TIMEZONE = 'UTC'",
224+
"ALTER SESSION UNSET TIMEZONE",
225+
]
226+
227+
216228
def test_get_table_last_modified_ts_quoted_and_unquoted_catalogs(
217229
snowflake_mocked_engine_adapter: SnowflakeEngineAdapter,
218230
) -> None:

‎tests/core/test_dialect.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,41 @@ def test_tsql_alter_column_nullability():
13951395
)
13961396

13971397

1398+
def test_snowflake_alter_session_parameters_are_not_quoted():
1399+
# Issue #5666: session parameter names are not identifiers, so Snowflake rejects them when
1400+
# quoted, e.g. ALTER SESSION SET "TIMEZONE" = 'UTC' is a syntax error.
1401+
for sql, expected in [
1402+
(
1403+
"ALTER SESSION SET TIMEZONE = 'UTC', query_tag = @tag",
1404+
"ALTER SESSION SET TIMEZONE = 'UTC', query_tag = @tag",
1405+
),
1406+
("ALTER SESSION UNSET TIMEZONE, query_tag", "ALTER SESSION UNSET TIMEZONE, query_tag"),
1407+
# Explicit quoting is left as written
1408+
("ALTER SESSION SET \"TIMEZONE\" = 'UTC'", "ALTER SESSION SET \"TIMEZONE\" = 'UTC'"),
1409+
]:
1410+
assert parse_one(sql, read="snowflake").sql(dialect="snowflake", identify=True) == expected
1411+
1412+
model = load_sql_based_model(
1413+
parse(
1414+
"""
1415+
MODEL (name db.t, kind FULL, dialect snowflake);
1416+
1417+
ALTER SESSION SET TIMEZONE = 'UTC';
1418+
1419+
SELECT 1 AS a;
1420+
1421+
ALTER SESSION UNSET TIMEZONE;
1422+
"""
1423+
)
1424+
)
1425+
assert [s.sql(dialect="snowflake") for s in model.render_pre_statements()] == [
1426+
"ALTER SESSION SET TIMEZONE = 'UTC'"
1427+
]
1428+
assert [s.sql(dialect="snowflake") for s in model.render_post_statements()] == [
1429+
"ALTER SESSION UNSET TIMEZONE"
1430+
]
1431+
1432+
13981433
def test_model_name_cannot_be_string():
13991434
with pytest.raises(ParseError) as parse_error:
14001435
parse(

0 commit comments

Comments
 (0)