Skip to content

Commit 82e9fb0

Browse files
Fix(databricks): Add column type only along materialized view comments (#5617)
1 parent 8edb95c commit 82e9fb0

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

sqlmesh/core/engine_adapter/base.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ def _build_schema_exp(
811811
column_descriptions: t.Optional[t.Dict[str, str]] = None,
812812
expressions: t.Optional[t.List[exp.PrimaryKey]] = None,
813813
is_view: bool = False,
814+
materialized: bool = False,
814815
) -> exp.Schema:
815816
"""
816817
Build a schema expression for a table, columns, column comments, and additional schema properties.
@@ -823,6 +824,7 @@ def _build_schema_exp(
823824
target_columns_to_types=target_columns_to_types,
824825
column_descriptions=column_descriptions,
825826
is_view=is_view,
827+
materialized=materialized,
826828
)
827829
+ expressions,
828830
)
@@ -832,6 +834,7 @@ def _build_column_defs(
832834
target_columns_to_types: t.Dict[str, exp.DataType],
833835
column_descriptions: t.Optional[t.Dict[str, str]] = None,
834836
is_view: bool = False,
837+
materialized: bool = False,
835838
) -> t.List[exp.ColumnDef]:
836839
engine_supports_schema_comments = (
837840
self.COMMENT_CREATION_VIEW.supports_schema_def
@@ -1260,7 +1263,11 @@ def create_view(
12601263
schema: t.Union[exp.Table, exp.Schema] = exp.to_table(view_name)
12611264
if target_columns_to_types:
12621265
schema = self._build_schema_exp(
1263-
exp.to_table(view_name), target_columns_to_types, column_descriptions, is_view=True
1266+
exp.to_table(view_name),
1267+
target_columns_to_types,
1268+
column_descriptions,
1269+
is_view=True,
1270+
materialized=materialized,
12641271
)
12651272

12661273
properties = create_kwargs.pop("properties", None)

sqlmesh/core/engine_adapter/databricks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,14 @@ def _build_column_defs(
400400
target_columns_to_types: t.Dict[str, exp.DataType],
401401
column_descriptions: t.Optional[t.Dict[str, str]] = None,
402402
is_view: bool = False,
403+
materialized: bool = False,
403404
) -> t.List[exp.ColumnDef]:
404405
# Databricks requires column types to be specified when adding column comments
405406
# in CREATE MATERIALIZED VIEW statements. Override is_view to False to force
406407
# column types to be included when comments are present.
407-
if is_view and column_descriptions:
408+
if is_view and materialized and column_descriptions:
408409
is_view = False
409410

410-
return super()._build_column_defs(target_columns_to_types, column_descriptions, is_view)
411+
return super()._build_column_defs(
412+
target_columns_to_types, column_descriptions, is_view, materialized
413+
)

sqlmesh/core/engine_adapter/trino.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def _build_schema_exp(
284284
column_descriptions: t.Optional[t.Dict[str, str]] = None,
285285
expressions: t.Optional[t.List[exp.PrimaryKey]] = None,
286286
is_view: bool = False,
287+
materialized: bool = False,
287288
) -> exp.Schema:
288289
if "delta_lake" in self.get_catalog_type_from_table(table):
289290
target_columns_to_types = self._to_delta_ts(target_columns_to_types)

tests/core/engine_adapter/test_databricks.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,36 @@ def test_materialized_view_with_column_comments(
406406
]
407407

408408

409+
def test_regular_view_with_column_comments(
410+
mocker: MockFixture, make_mocked_engine_adapter: t.Callable
411+
):
412+
mocker.patch(
413+
"sqlmesh.core.engine_adapter.databricks.DatabricksEngineAdapter.set_current_catalog"
414+
)
415+
adapter = make_mocked_engine_adapter(DatabricksEngineAdapter, default_catalog="test_catalog")
416+
mocker.patch.object(adapter, "get_current_catalog", return_value="test_catalog")
417+
418+
adapter.create_view(
419+
"test_view",
420+
parse_one("SELECT a, b FROM source_table"),
421+
target_columns_to_types={
422+
"a": exp.DataType.build("INT"),
423+
"b": exp.DataType.build("STRING"),
424+
},
425+
materialized=False,
426+
column_descriptions={
427+
"a": "column a description",
428+
"b": "column b description",
429+
},
430+
)
431+
432+
sql_calls = to_sql_calls(adapter)
433+
# Regular views should NOT include column types even when column comments are present
434+
assert sql_calls == [
435+
"CREATE OR REPLACE VIEW `test_view` (`a` COMMENT 'column a description', `b` COMMENT 'column b description') AS SELECT `a`, `b` FROM `source_table`",
436+
]
437+
438+
409439
def test_create_table_clustered_by(mocker: MockFixture, make_mocked_engine_adapter: t.Callable):
410440
mocker.patch(
411441
"sqlmesh.core.engine_adapter.databricks.DatabricksEngineAdapter.set_current_catalog"

0 commit comments

Comments
 (0)