From 6e07b8db13ddff6787c2d4deabd93fb992d8fcbf Mon Sep 17 00:00:00 2001 From: Andreas Fred-Ojala Date: Fri, 31 Oct 2025 14:55:30 +0100 Subject: [PATCH 1/3] Fix: Include column types in Databricks materialized views with comments Databricks requires column types to be specified when adding column comments in CREATE MATERIALIZED VIEW statements. Without the column types, the comments are silently ignored. This change adds a Databricks-specific override of _build_column_defs that forces column types to be included when creating views with column descriptions. --- sqlmesh/core/engine_adapter/databricks.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sqlmesh/core/engine_adapter/databricks.py b/sqlmesh/core/engine_adapter/databricks.py index 7521124684..569c75f887 100644 --- a/sqlmesh/core/engine_adapter/databricks.py +++ b/sqlmesh/core/engine_adapter/databricks.py @@ -394,3 +394,19 @@ def _build_table_properties_exp( expressions.append(clustered_by_exp) properties = exp.Properties(expressions=expressions) return properties + + def _build_column_defs( + self, + target_columns_to_types: t.Dict[str, exp.DataType], + column_descriptions: t.Optional[t.Dict[str, str]] = None, + is_view: bool = False, + ) -> t.List[exp.ColumnDef]: + # Databricks requires column types to be specified when adding column comments + # in CREATE MATERIALIZED VIEW statements. Override is_view to False to force + # column types to be included when comments are present. + if is_view and column_descriptions: + engine_supports_schema_comments = self.COMMENT_CREATION_VIEW.supports_schema_def + if engine_supports_schema_comments and self.comments_enabled: + is_view = False + + return super()._build_column_defs(target_columns_to_types, column_descriptions, is_view) From c1ec990d871659c6a5673f2db501f783a1e6602e Mon Sep 17 00:00:00 2001 From: Andreas Fred-Ojala Date: Wed, 5 Nov 2025 08:58:29 +0100 Subject: [PATCH 2/3] Test: Add test for Databricks materialized views with column comments Verifies that column types are included in CREATE MATERIALIZED VIEW statements when column comments are present, as required by Databricks. --- tests/core/engine_adapter/test_databricks.py | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/core/engine_adapter/test_databricks.py b/tests/core/engine_adapter/test_databricks.py index e4512f11c9..ca36b9856f 100644 --- a/tests/core/engine_adapter/test_databricks.py +++ b/tests/core/engine_adapter/test_databricks.py @@ -376,6 +376,36 @@ def test_materialized_view_properties(mocker: MockFixture, make_mocked_engine_ad ] +def test_materialized_view_with_column_comments( + mocker: MockFixture, make_mocked_engine_adapter: t.Callable +): + mocker.patch( + "sqlmesh.core.engine_adapter.databricks.DatabricksEngineAdapter.set_current_catalog" + ) + adapter = make_mocked_engine_adapter(DatabricksEngineAdapter, default_catalog="test_catalog") + mocker.patch.object(adapter, "get_current_catalog", return_value="test_catalog") + + adapter.create_view( + "test_view", + parse_one("SELECT a, b FROM source_table"), + target_columns_to_types={ + "a": exp.DataType.build("INT"), + "b": exp.DataType.build("STRING"), + }, + materialized=True, + column_descriptions={ + "a": "column a description", + "b": "column b description", + }, + ) + + sql_calls = to_sql_calls(adapter) + # Databricks requires column types when column comments are present in materialized views + assert sql_calls == [ + "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`", + ] + + def test_create_table_clustered_by(mocker: MockFixture, make_mocked_engine_adapter: t.Callable): mocker.patch( "sqlmesh.core.engine_adapter.databricks.DatabricksEngineAdapter.set_current_catalog" From 4cfcfc81cd390242c48788b628bb95fbcad57668 Mon Sep 17 00:00:00 2001 From: Andreas Fred-Ojala Date: Mon, 10 Nov 2025 10:06:18 +0100 Subject: [PATCH 3/3] Removes unnecessary nested statement --- sqlmesh/core/engine_adapter/databricks.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sqlmesh/core/engine_adapter/databricks.py b/sqlmesh/core/engine_adapter/databricks.py index 569c75f887..5c12c5f6cb 100644 --- a/sqlmesh/core/engine_adapter/databricks.py +++ b/sqlmesh/core/engine_adapter/databricks.py @@ -405,8 +405,6 @@ def _build_column_defs( # in CREATE MATERIALIZED VIEW statements. Override is_view to False to force # column types to be included when comments are present. if is_view and column_descriptions: - engine_supports_schema_comments = self.COMMENT_CREATION_VIEW.supports_schema_def - if engine_supports_schema_comments and self.comments_enabled: - is_view = False + is_view = False return super()._build_column_defs(target_columns_to_types, column_descriptions, is_view)