Skip to content

Commit c7b003c

Browse files
committed
fix(db2): do not embed inline COMMENT= in CTAS SQL
Db2 rejects COMMENT= as a table property in CREATE TABLE ... AS ... WITH DATA statements (SQL0104N). The CTAS path in _create_table was passing table_description into _build_create_table_exp which unconditionally injects a SchemaCommentProperty. Fix: pass table_description=None to _build_create_table_exp on the CTAS path. The description is still applied correctly via a separate COMMENT ON TABLE command (COMMENT_CREATION_TABLE = COMMENT_COMMAND_ONLY already handles this at line 462). Fixes: test_ctas_source_columns[db2] CI failure. Adds: test_ctas_with_table_description unit test to prevent regression.
1 parent 0b04c14 commit c7b003c

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

sqlmesh/core/engine_adapter/db2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,17 @@ def _create_table(
432432
else:
433433
self.drop_view(table, ignore_if_not_exists=True)
434434

435+
# Do NOT pass table_description here: Db2 does not support inline
436+
# COMMENT= in CREATE TABLE AS ... WITH DATA syntax (SQL0104N).
437+
# The description is applied via a separate COMMENT ON TABLE command
438+
# below (when COMMENT_CREATION_TABLE.is_comment_command_only).
435439
create_exp = self._build_create_table_exp(
436440
table_name_or_schema=table_name_or_schema,
437441
expression=expression,
438442
exists=False,
439443
replace=False,
440444
target_columns_to_types=target_columns_to_types,
441-
table_description=table_description,
445+
table_description=None,
442446
table_kind=table_kind,
443447
**kwargs,
444448
)

tests/core/engine_adapter/test_db2.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,38 @@ def test_ctas_with_data(adapter: Db2EngineAdapter, mocker: MockerFixture):
221221
assert "_subquery" not in sql_calls[0]
222222

223223

224+
def test_ctas_with_table_description(adapter: Db2EngineAdapter, mocker: MockerFixture):
225+
"""CTAS with table_description must not embed COMMENT= in the CREATE TABLE SQL.
226+
227+
Db2 rejects inline COMMENT= in CTAS (SQL0104N). The description must be
228+
applied via a separate COMMENT ON TABLE statement after the table is created.
229+
"""
230+
mocker.patch.object(adapter, "table_exists", return_value=False)
231+
mocker.patch.object(adapter, "drop_view")
232+
233+
adapter.ctas(
234+
table_name="test_schema.test_table",
235+
query_or_df=parse_one("SELECT id FROM source_table"),
236+
exists=False,
237+
table_description="test table description",
238+
column_descriptions={"id": "test id column description"},
239+
)
240+
241+
sql_calls = to_sql_calls(adapter)
242+
# First call: the CTAS itself — must contain WITH DATA and no inline COMMENT=
243+
assert "CREATE TABLE" in sql_calls[0]
244+
assert "WITH DATA" in sql_calls[0]
245+
assert "COMMENT=" not in sql_calls[0].replace(" ", "")
246+
# Second call: separate COMMENT ON TABLE
247+
assert any("COMMENT ON TABLE" in c for c in sql_calls), (
248+
"Expected a separate COMMENT ON TABLE statement"
249+
)
250+
# Third call: separate COMMENT ON COLUMN
251+
assert any("COMMENT ON COLUMN" in c for c in sql_calls), (
252+
"Expected a separate COMMENT ON COLUMN statement"
253+
)
254+
255+
224256
# ---------------------------------------------------------------------------
225257
# drop_view — guards via SYSCAT.VIEWS (no DROP VIEW IF EXISTS in Db2)
226258
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)