From 402c4c615382f8c4b40c5ddbbc974cca48a6b819 Mon Sep 17 00:00:00 2001 From: Alice Jia Qi Liu Date: Tue, 6 Aug 2024 17:10:31 -0700 Subject: [PATCH] Support using the measure label when using create_metric option --- .changes/unreleased/Fixes-20240806-172110.yaml | 6 ++++++ core/dbt/parser/schema_yaml_readers.py | 2 +- tests/functional/semantic_models/fixtures.py | 6 ++++++ .../semantic_models/test_semantic_model_parsing.py | 6 ++++++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Fixes-20240806-172110.yaml diff --git a/.changes/unreleased/Fixes-20240806-172110.yaml b/.changes/unreleased/Fixes-20240806-172110.yaml new file mode 100644 index 00000000000..716bf861639 --- /dev/null +++ b/.changes/unreleased/Fixes-20240806-172110.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Propagate measure label when using create_metrics +time: 2024-08-06T17:21:10.265494-07:00 +custom: + Author: aliceliu + Issue: "10536" diff --git a/core/dbt/parser/schema_yaml_readers.py b/core/dbt/parser/schema_yaml_readers.py index 20e0a44bc08..e63d39953c8 100644 --- a/core/dbt/parser/schema_yaml_readers.py +++ b/core/dbt/parser/schema_yaml_readers.py @@ -612,7 +612,7 @@ def _create_metric( ) -> None: unparsed_metric = UnparsedMetric( name=measure.name, - label=measure.name, + label=measure.label or measure.name, type="simple", type_params=UnparsedMetricTypeParams(measure=measure.name, expr=measure.name), description=measure.description or f"Metric created from measure {measure.name}", diff --git a/tests/functional/semantic_models/fixtures.py b/tests/functional/semantic_models/fixtures.py index 46b7fd86d06..7788067e91d 100644 --- a/tests/functional/semantic_models/fixtures.py +++ b/tests/functional/semantic_models/fixtures.py @@ -240,6 +240,12 @@ agg: sum agg_time_dimension: ds create_metric: true + - name: txn_revenue_with_label + label: "Transaction Revenue with label" + expr: revenue + agg: sum + agg_time_dimension: ds + create_metric: true - name: sum_of_things expr: 2 agg: sum diff --git a/tests/functional/semantic_models/test_semantic_model_parsing.py b/tests/functional/semantic_models/test_semantic_model_parsing.py index e89c41f9d8d..a9616fbe869 100644 --- a/tests/functional/semantic_models/test_semantic_model_parsing.py +++ b/tests/functional/semantic_models/test_semantic_model_parsing.py @@ -107,6 +107,7 @@ def test_semantic_model_deleted_partial_parsing(self, project): def test_semantic_model_flipping_create_metric_partial_parsing(self, project): generated_metric = "metric.test.txn_revenue" + generated_metric_with_label = "metric.test.txn_revenue_with_label" # First, use the default schema.yml to define our semantic model, and # run the dbt parse command write_file(schema_yml, project.project_root, "models", "schema.yml") @@ -117,6 +118,11 @@ def test_semantic_model_flipping_create_metric_partial_parsing(self, project): # Verify the metric created by `create_metric: true` exists metric = result.result.metrics[generated_metric] assert metric.name == "txn_revenue" + assert metric.label == "txn_revenue" + + metric_with_label = result.result.metrics[generated_metric_with_label] + assert metric_with_label.name == "txn_revenue_with_label" + assert metric_with_label.label == "Transaction Revenue with label" # --- Next, modify the default schema.yml to have no `create_metric: true` --- no_create_metric_schema_yml = schema_yml.replace(