From 19cc61e8583e4a84ed2d27d7ce5a5e76d59b8895 Mon Sep 17 00:00:00 2001 From: Shubham Dhal Date: Tue, 16 Jun 2026 18:33:41 +0530 Subject: [PATCH] fix: apply tblproperties to metric_view models at create time The metric_view create branch applied databricks_tags but not tblproperties, so tblproperties configured on a model were dropped on the first build and only applied on a later alter/replace run. Apply them on the create branch, mirroring replace_with_metric_view. Closes #1527 --- CHANGELOG.md | 1 + .../macros/materializations/metric_view.sql | 4 +++ .../adapter/metric_views/fixtures.py | 20 +++++++++++++++ .../test_metric_view_materialization.py | 25 +++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b3f486c6..96bcc2191 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Add catalogs.yml v2 support (requires `use_catalogs_v2: true` in dbt-core) ([1440](https://github.com/databricks/dbt-databricks/pull/1440)) ### Fixes +- Apply `tblproperties` to `metric_view` models at create time, not only on a later alter/replace run ([#1530](https://github.com/databricks/dbt-databricks/pull/1530) closes [#1527](https://github.com/databricks/dbt-databricks/issues/1527)) - Raise a `DbtRuntimeError` when a Python model job run terminates with a non-success `result_state` (e.g. `FAILED`/`TIMEDOUT`) instead of returning silently ([#1477](https://github.com/databricks/dbt-databricks/pull/1477)) ### Under the Hood diff --git a/dbt/include/databricks/macros/materializations/metric_view.sql b/dbt/include/databricks/macros/materializations/metric_view.sql index b3884dce1..1c6c80d40 100644 --- a/dbt/include/databricks/macros/materializations/metric_view.sql +++ b/dbt/include/databricks/macros/materializations/metric_view.sql @@ -31,6 +31,10 @@ {{ get_create_metric_view_as_sql(target_relation, sql) }} {%- endcall %} {{ apply_tags(target_relation, tags) }} + {% set tblproperties = config.get('tblproperties') %} + {% if tblproperties %} + {{ apply_tblproperties(target_relation, tblproperties) }} + {% endif %} {% endif %} {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke(existing_relation, full_refresh_mode=True)) %} diff --git a/tests/functional/adapter/metric_views/fixtures.py b/tests/functional/adapter/metric_views/fixtures.py index 89b330de0..a048e36c5 100644 --- a/tests/functional/adapter/metric_views/fixtures.py +++ b/tests/functional/adapter/metric_views/fixtures.py @@ -76,3 +76,23 @@ - name: order_count expr: count(1) """ + +metric_view_with_tblproperties = """ +{{ + config( + materialized='metric_view', + tblproperties={ + 'quality': 'gold' + } + ) +}} + +version: 1.1 +source: "{{ ref('source_orders') }}" +dimensions: + - name: status + expr: status +measures: + - name: order_count + expr: count(1) +""" diff --git a/tests/functional/adapter/metric_views/test_metric_view_materialization.py b/tests/functional/adapter/metric_views/test_metric_view_materialization.py index a0c4752da..7900c467c 100644 --- a/tests/functional/adapter/metric_views/test_metric_view_materialization.py +++ b/tests/functional/adapter/metric_views/test_metric_view_materialization.py @@ -6,6 +6,7 @@ metric_view_bare_ref, metric_view_with_config, metric_view_with_filter, + metric_view_with_tblproperties, source_table, ) @@ -240,3 +241,27 @@ def test_bare_ref_metric_view_creates_and_queries(self, project): fetch="all", ) assert query_result and query_result[0][0] == 3 + + +@pytest.mark.skip_profile("databricks_cluster") +class TestMetricViewCreateTblProperties: + """tblproperties configured on a metric view are set on the freshly created view.""" + + @pytest.fixture(scope="class") + def models(self): + return { + "source_orders.sql": source_table, + "tblprops_metrics.sql": metric_view_with_tblproperties, + } + + def test_tblproperties_applied_on_create(self, project): + results = run_dbt(["run"]) + assert len(results) == 2 + assert all(result.status == "success" for result in results) + + rows = project.run_sql( + f"show tblproperties {project.database}.{project.test_schema}.tblprops_metrics", + fetch="all", + ) + tblprops = {row[0]: row[1] for row in rows} + assert tblprops.get("quality") == "gold"