11from __future__ import annotations
22
33import contextlib
4+ import itertools
45import logging
56import typing as t
67
@@ -699,22 +700,26 @@ def _apply_clustering_operation(
699700 self ,
700701 operations : t .List [TableAlterOperation ],
701702 clustering_operation : TableAlterClusterByOperation ,
702- current_table_info : t .Any ,
703+ current_table_info : t .Optional [ DataObject ] ,
703704 ) -> None :
704705 """Apply clustering DDL in the order Snowflake requires.
705706
706707 Snowflake rejects DROP COLUMN while the column is still named in the clustering
707- key, and also rejects CLUSTER BY on a column that has not yet been added. The
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
708710 required sequence when the old key names a column being dropped is:
709711
710- 1. DROP CLUSTERING KEY — before any drop of a column the old key still names.
711- 2. Column changes (drops and adds) — in their existing schema-diff order.
712- 3. CLUSTER BY — after all column changes, if the target table is clustered.
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.
713715
714- When no column being dropped belongs to the current key the historical order is
715- kept: column changes first, then the single clustering change at the end.
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.
716721 """
717- clustering_key = getattr ( current_table_info , " clustering_key" , None )
722+ clustering_key = current_table_info . clustering_key if current_table_info else None
718723 if clustering_key :
719724 referenced = self ._clustering_key_column_names (clustering_key )
720725 drop_index = next (
@@ -723,10 +728,7 @@ def _apply_clustering_operation(
723728 for i , op in enumerate (operations )
724729 if isinstance (op , TableAlterDropColumnOperation )
725730 and op .column_parts
726- and normalize_identifiers (
727- exp .to_identifier (op .column_parts [0 ].name ), dialect = self .dialect
728- ).name
729- in referenced
731+ and op .column_parts [0 ].name in referenced
730732 ),
731733 None ,
732734 )
@@ -738,7 +740,8 @@ def _apply_clustering_operation(
738740 if isinstance (clustering_operation , TableAlterChangeClusterKeyOperation ):
739741 operations .append (clustering_operation )
740742 return
741- # No key column is being dropped: append at end (historical order).
743+ if self ._clustering_key_is_unchanged (clustering_operation , current_table_info ):
744+ return
742745 operations .append (clustering_operation )
743746
744747 def _clustering_key_column_names (self , clustering_key : str ) -> t .Set [str ]:
@@ -747,9 +750,11 @@ def _clustering_key_column_names(self, clustering_key: str) -> t.Set[str]:
747750 clustering_key = clustering_key ,
748751 dialect = self .dialect ,
749752 ).cluster_key_expressions
753+ cluster_columns = itertools .chain .from_iterable (
754+ expression .find_all (exp .Column ) for expression in expressions
755+ )
750756 return {
751- normalize_identifiers (expression , dialect = self .dialect ).name
752- for expression in expressions
757+ normalize_identifiers (column , dialect = self .dialect ).name for column in cluster_columns
753758 }
754759
755760 def alter_table (
0 commit comments