Skip to content

Commit 66fda53

Browse files
authored
fix: Drop columns before adding columns when altering table in Spark (#599)
1 parent 85ea3e9 commit 66fda53

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

sqlmesh/core/engine_adapter/base_spark.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ def alter_table(
8282
) -> None:
8383
alter_table = exp.AlterTable(this=exp.to_table(table_name))
8484

85+
if dropped_columns:
86+
drop_columns = exp.Drop(
87+
this=exp.Schema(
88+
expressions=[exp.to_identifier(column_name) for column_name in dropped_columns]
89+
),
90+
kind="COLUMNS",
91+
)
92+
alter_table.set("actions", [drop_columns])
93+
self.execute(alter_table)
94+
8595
if added_columns:
8696
add_columns = exp.Schema(
8797
expressions=[
@@ -95,16 +105,6 @@ def alter_table(
95105
alter_table.set("actions", [add_columns])
96106
self.execute(alter_table)
97107

98-
if dropped_columns:
99-
drop_columns = exp.Drop(
100-
this=exp.Schema(
101-
expressions=[exp.to_identifier(column_name) for column_name in dropped_columns]
102-
),
103-
kind="COLUMNS",
104-
)
105-
alter_table.set("actions", [drop_columns])
106-
self.execute(alter_table)
107-
108108
def _create_table_properties(
109109
self,
110110
storage_format: t.Optional[str] = None,

tests/core/engine_adapter/test_spark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def test_alter_table(mocker: MockerFixture):
5959
cursor_mock.execute.assert_has_calls(
6060
[
6161
# 1st call.
62-
call("""ALTER TABLE `test_table` ADD COLUMNS (`a` INT, `b` STRING)"""),
6362
call("""ALTER TABLE `test_table` DROP COLUMNS (`c`, `d`)"""),
63+
call("""ALTER TABLE `test_table` ADD COLUMNS (`a` INT, `b` STRING)"""),
6464
# 2nd call.
6565
call("""ALTER TABLE `test_table` ADD COLUMNS (`e` DOUBLE)"""),
6666
# 3d call.

0 commit comments

Comments
 (0)