Skip to content

Commit 92dc29d

Browse files
fix(snowflake): parse a clustering key only when a column is dropped
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent cc294f5 commit 92dc29d

2 files changed

Lines changed: 79 additions & 15 deletions

File tree

‎sqlmesh/core/engine_adapter/snowflake.py‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -704,23 +704,18 @@ def _apply_clustering_operation(
704704
) -> None:
705705
"""Apply clustering DDL in the order Snowflake requires.
706706
707-
Snowflake rejects DROP COLUMN while the column is still named in the clustering
708-
key, including when the name sits inside an expression such as TO_DATE("TS").
709-
It also rejects CLUSTER BY on a column that has not yet been added. The
710-
required sequence when the old key names a column being dropped is:
711-
712-
1. DROP CLUSTERING KEY, before any drop of a column the old key still names.
713-
2. Column changes (drops and adds), in their existing schema-diff order.
714-
3. CLUSTER BY, after all column changes, when the target table is clustered.
715-
716-
A type change that schema diff renders as DROP plus ADD uses the same sequence
717-
even when the key text does not change. When no dropped column belongs to the
718-
current key, an unchanged key emits nothing and a changed key stays at the end.
719-
Column names from DESCRIBE are already stored as Snowflake resolved them, so
720-
they are compared as returned.
707+
DROP COLUMN fails while the column is still in the clustering key, including
708+
inside an expression. CLUSTER BY fails on a column that does not exist yet. A
709+
type change that becomes DROP plus ADD still needs the key dropped first even
710+
when the key text is unchanged. An unchanged key with no matching column drop
711+
emits nothing. DESCRIBE names are compared as stored.
721712
"""
722713
clustering_key = current_table_info.clustering_key if current_table_info else None
723-
if clustering_key:
714+
operations_include_drop_column = any(
715+
isinstance(op, TableAlterDropColumnOperation) for op in operations
716+
)
717+
718+
if clustering_key and operations_include_drop_column:
724719
referenced = self._clustering_key_column_names(clustering_key)
725720
drop_index = next(
726721
(

‎tests/core/engine_adapter/test_snowflake.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from pytest_mock.plugin import MockerFixture
77
from sqlglot import exp, parse_one
8+
from sqlglot.errors import ParseError
89
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
910

1011
import sqlmesh.core.dialect as d
@@ -1804,3 +1805,71 @@ def test_clustering_key_issue_5813_clone_inherited_column_drop(
18041805
]
18051806
for kept in ('"TENANT_ID"', '"USER_ID"', '"SESSION_START_TS"'):
18061807
assert not any(f"DROP COLUMN {kept}" in sql for sql in sql_calls)
1808+
1809+
1810+
def test_clustering_key_unparseable_without_column_drop_skips_parse(
1811+
make_mocked_engine_adapter: t.Callable, mocker: MockerFixture
1812+
) -> None:
1813+
"""A key string that cannot be parsed is ignored unless a column is dropped.
1814+
1815+
LINEAR( is not valid Snowflake SQL. With no column drop, the current key is not
1816+
parsed: an unchanged key emits nothing, and a removed or replaced key is appended.
1817+
"""
1818+
unchanged = _make_snowflake_clustering_adapter(
1819+
make_mocked_engine_adapter,
1820+
mocker,
1821+
current_columns={"ID": "INT"},
1822+
target_columns={"ID": "INT"},
1823+
current_clustering_key="LINEAR(",
1824+
target_clustering_key="LINEAR(",
1825+
)
1826+
assert (
1827+
unchanged.get_alter_operations("catalog.db.current_table", "catalog.db.target_table") == []
1828+
)
1829+
1830+
removed = _make_snowflake_clustering_adapter(
1831+
make_mocked_engine_adapter,
1832+
mocker,
1833+
current_columns={"ID": "INT"},
1834+
target_columns={"ID": "INT"},
1835+
current_clustering_key="LINEAR(",
1836+
target_clustering_key=None,
1837+
)
1838+
removed_operations = removed.get_alter_operations(
1839+
"catalog.db.current_table", "catalog.db.target_table"
1840+
)
1841+
removed.alter_table(removed_operations)
1842+
assert [type(op) for op in removed_operations] == [TableAlterDropClusterKeyOperation]
1843+
assert to_sql_calls(removed) == [_cluster_alter("DROP CLUSTERING KEY")]
1844+
1845+
replaced = _make_snowflake_clustering_adapter(
1846+
make_mocked_engine_adapter,
1847+
mocker,
1848+
current_columns={"ID": "INT"},
1849+
target_columns={"ID": "INT"},
1850+
current_clustering_key="LINEAR(",
1851+
target_clustering_key='LINEAR("ID")',
1852+
)
1853+
replaced_operations = replaced.get_alter_operations(
1854+
"catalog.db.current_table", "catalog.db.target_table"
1855+
)
1856+
replaced.alter_table(replaced_operations)
1857+
assert [type(op) for op in replaced_operations] == [TableAlterChangeClusterKeyOperation]
1858+
assert to_sql_calls(replaced) == [_cluster_alter('CLUSTER BY ("ID")')]
1859+
1860+
1861+
def test_clustering_key_unparseable_column_drop_still_raises(
1862+
make_mocked_engine_adapter: t.Callable, mocker: MockerFixture
1863+
) -> None:
1864+
"""Dropping a column still parses the current key, so a bad key still fails."""
1865+
adapter = _make_snowflake_clustering_adapter(
1866+
make_mocked_engine_adapter,
1867+
mocker,
1868+
current_columns={"ID": "INT", "EXTRA": "INT"},
1869+
target_columns={"ID": "INT"},
1870+
current_clustering_key="LINEAR(",
1871+
target_clustering_key=None,
1872+
)
1873+
1874+
with pytest.raises(ParseError):
1875+
adapter.get_alter_operations("catalog.db.current_table", "catalog.db.target_table")

0 commit comments

Comments
 (0)