Skip to content

Commit

Permalink
Support using the measure label when using create_metric option (#10536)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliceliu authored Aug 7, 2024
1 parent 56bfbea commit 7f57dd5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240806-172110.yaml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion core/dbt/parser/schema_yaml_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/semantic_models/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions tests/functional/semantic_models/test_semantic_model_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ def test_semantic_model_parsing(self, project):
semantic_model.node_relation.relation_name
== f'"dbt"."{project.test_schema}"."fct_revenue"'
)
assert len(semantic_model.measures) == 6
# manifest should have one metric (that was created from a measure)
assert len(manifest.metrics) == 2
assert len(semantic_model.measures) == 7
# manifest should have two metrics created from measures
assert len(manifest.metrics) == 3
metric = manifest.metrics["metric.test.txn_revenue"]
assert metric.name == "txn_revenue"
metric_with_label = manifest.metrics["metric.test.txn_revenue_with_label"]
assert metric_with_label.name == "txn_revenue_with_label"
assert metric_with_label.label == "Transaction Revenue with label"

def test_semantic_model_error(self, project):
# Next, modify the default schema.yml to remove the semantic model.
Expand Down Expand Up @@ -107,6 +110,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")
Expand All @@ -117,6 +121,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(
Expand Down

0 comments on commit 7f57dd5

Please sign in to comment.