|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import contextlib |
| 4 | +import itertools |
4 | 5 | import logging |
5 | 6 | import typing as t |
6 | 7 |
|
|
16 | 17 | ClusteredByMixin, |
17 | 18 | RowDiffMixin, |
18 | 19 | GrantsFromInfoSchemaMixin, |
| 20 | + TableAlterChangeClusterKeyOperation, |
| 21 | + TableAlterClusterByOperation, |
| 22 | + TableAlterDropClusterKeyOperation, |
19 | 23 | ) |
| 24 | +from sqlmesh.core.schema_diff import TableAlterDropColumnOperation, TableAlterOperation |
20 | 25 | from sqlmesh.core.engine_adapter.shared import ( |
21 | 26 | CatalogSupport, |
22 | 27 | DataObject, |
23 | 28 | DataObjectType, |
24 | 29 | SourceQuery, |
25 | 30 | set_catalog, |
26 | 31 | ) |
27 | | -from sqlmesh.core.schema_diff import TableAlterOperation |
28 | 32 | from sqlmesh.utils import optional_import, get_source_columns_to_types |
29 | 33 | from sqlmesh.utils.errors import SQLMeshError |
30 | 34 | from sqlmesh.utils.pandas import columns_to_types_from_dtypes |
@@ -692,6 +696,62 @@ def clone_table( |
692 | 696 | **kwargs, |
693 | 697 | ) |
694 | 698 |
|
| 699 | + def _apply_clustering_operation( |
| 700 | + self, |
| 701 | + operations: t.List[TableAlterOperation], |
| 702 | + clustering_operation: TableAlterClusterByOperation, |
| 703 | + current_table_info: t.Optional[DataObject], |
| 704 | + ) -> None: |
| 705 | + """Apply clustering DDL in the order Snowflake requires. |
| 706 | +
|
| 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. |
| 712 | + """ |
| 713 | + clustering_key = current_table_info.clustering_key if current_table_info else None |
| 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: |
| 719 | + referenced = self._clustering_key_column_names(clustering_key) |
| 720 | + drop_index = next( |
| 721 | + ( |
| 722 | + i |
| 723 | + for i, op in enumerate(operations) |
| 724 | + if isinstance(op, TableAlterDropColumnOperation) |
| 725 | + and op.column_parts |
| 726 | + and op.column_parts[0].name in referenced |
| 727 | + ), |
| 728 | + None, |
| 729 | + ) |
| 730 | + if drop_index is not None: |
| 731 | + drop_key_op = TableAlterDropClusterKeyOperation( |
| 732 | + target_table=clustering_operation.target_table |
| 733 | + ) |
| 734 | + operations.insert(drop_index, drop_key_op) |
| 735 | + if isinstance(clustering_operation, TableAlterChangeClusterKeyOperation): |
| 736 | + operations.append(clustering_operation) |
| 737 | + return |
| 738 | + if self._clustering_key_is_unchanged(clustering_operation, current_table_info): |
| 739 | + return |
| 740 | + operations.append(clustering_operation) |
| 741 | + |
| 742 | + def _clustering_key_column_names(self, clustering_key: str) -> t.Set[str]: |
| 743 | + expressions = TableAlterChangeClusterKeyOperation( |
| 744 | + target_table=exp.to_table("_"), |
| 745 | + clustering_key=clustering_key, |
| 746 | + dialect=self.dialect, |
| 747 | + ).cluster_key_expressions |
| 748 | + cluster_columns = itertools.chain.from_iterable( |
| 749 | + expression.find_all(exp.Column) for expression in expressions |
| 750 | + ) |
| 751 | + return { |
| 752 | + normalize_identifiers(column, dialect=self.dialect).name for column in cluster_columns |
| 753 | + } |
| 754 | + |
695 | 755 | def alter_table( |
696 | 756 | self, |
697 | 757 | alter_expressions: t.Union[t.List[exp.Alter], t.List[TableAlterOperation]], |
|
0 commit comments