Skip to content

Commit 8263170

Browse files
sinelineGuillem G.
authored andcommitted
feat: Add support for ALTER ICEBERG TABLE in Snowflake by passing table format to the alter_table method.
1 parent e13fc47 commit 8263170

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

sqlmesh/core/engine_adapter/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ def get_alter_operations(
11841184
def alter_table(
11851185
self,
11861186
alter_expressions: t.Union[t.List[exp.Alter], t.List[TableAlterOperation]],
1187+
**kwargs: t.Any,
11871188
) -> None:
11881189
"""
11891190
Performs the alter statements to change the current table into the structure of the target table.

sqlmesh/core/engine_adapter/snowflake.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,31 @@ def clone_table(
689689
**kwargs,
690690
)
691691

692+
def alter_table(
693+
self,
694+
alter_expressions: t.Union[t.List[exp.Alter], t.List["TableAlterOperation"]],
695+
**kwargs: t.Any,
696+
) -> None:
697+
# Snowflake requires ALTER ICEBERG TABLE instead of ALTER TABLE for Iceberg tables
698+
table_format = kwargs.pop("table_format", None)
699+
if table_format and isinstance(table_format, str) and table_format.upper() == "ICEBERG":
700+
from sqlmesh.core.schema_diff import TableAlterOperation
701+
702+
resolved_expressions = []
703+
for x in alter_expressions:
704+
if isinstance(x, TableAlterOperation):
705+
alter_expr = x.expression
706+
else:
707+
alter_expr = x
708+
alter_expr.args["kind"] = f"{table_format.upper()} TABLE"
709+
resolved_expressions.append(alter_expr)
710+
711+
with self.transaction():
712+
for alter_expr in resolved_expressions:
713+
self.execute(alter_expr)
714+
else:
715+
super().alter_table(alter_expressions, **kwargs)
716+
692717
@t.overload
693718
def _columns_to_types(
694719
self,

sqlmesh/core/snapshot/evaluator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ def migrate(
21092109
_check_additive_schema_change(
21102110
snapshot, alter_operations, kwargs["allow_additive_snapshots"]
21112111
)
2112-
self.adapter.alter_table(alter_operations)
2112+
self.adapter.alter_table(alter_operations, table_format=snapshot.model.table_format)
21132113

21142114
# Apply grants after schema migration
21152115
deployability_index = kwargs.get("deployability_index")

0 commit comments

Comments
 (0)