Skip to content

Commit 98998d4

Browse files
authored
Fix: Include column types in Databricks materialized views with comments (#5578)
1 parent 5c51f1a commit 98998d4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

sqlmesh/core/engine_adapter/databricks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,17 @@ def _build_table_properties_exp(
394394
expressions.append(clustered_by_exp)
395395
properties = exp.Properties(expressions=expressions)
396396
return properties
397+
398+
def _build_column_defs(
399+
self,
400+
target_columns_to_types: t.Dict[str, exp.DataType],
401+
column_descriptions: t.Optional[t.Dict[str, str]] = None,
402+
is_view: bool = False,
403+
) -> t.List[exp.ColumnDef]:
404+
# Databricks requires column types to be specified when adding column comments
405+
# in CREATE MATERIALIZED VIEW statements. Override is_view to False to force
406+
# column types to be included when comments are present.
407+
if is_view and column_descriptions:
408+
is_view = False
409+
410+
return super()._build_column_defs(target_columns_to_types, column_descriptions, is_view)

tests/core/engine_adapter/test_databricks.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,36 @@ def test_materialized_view_properties(mocker: MockFixture, make_mocked_engine_ad
376376
]
377377

378378

379+
def test_materialized_view_with_column_comments(
380+
mocker: MockFixture, make_mocked_engine_adapter: t.Callable
381+
):
382+
mocker.patch(
383+
"sqlmesh.core.engine_adapter.databricks.DatabricksEngineAdapter.set_current_catalog"
384+
)
385+
adapter = make_mocked_engine_adapter(DatabricksEngineAdapter, default_catalog="test_catalog")
386+
mocker.patch.object(adapter, "get_current_catalog", return_value="test_catalog")
387+
388+
adapter.create_view(
389+
"test_view",
390+
parse_one("SELECT a, b FROM source_table"),
391+
target_columns_to_types={
392+
"a": exp.DataType.build("INT"),
393+
"b": exp.DataType.build("STRING"),
394+
},
395+
materialized=True,
396+
column_descriptions={
397+
"a": "column a description",
398+
"b": "column b description",
399+
},
400+
)
401+
402+
sql_calls = to_sql_calls(adapter)
403+
# Databricks requires column types when column comments are present in materialized views
404+
assert sql_calls == [
405+
"CREATE OR REPLACE MATERIALIZED VIEW `test_view` (`a` INT COMMENT 'column a description', `b` STRING COMMENT 'column b description') AS SELECT `a`, `b` FROM `source_table`",
406+
]
407+
408+
379409
def test_create_table_clustered_by(mocker: MockFixture, make_mocked_engine_adapter: t.Callable):
380410
mocker.patch(
381411
"sqlmesh.core.engine_adapter.databricks.DatabricksEngineAdapter.set_current_catalog"

0 commit comments

Comments
 (0)