From 845982062850d0eab16b2eb0c8af157d128fb68d Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Thu, 3 Aug 2023 09:36:41 -0700 Subject: [PATCH] Begin creating metrics from measures with `create_metric = True` This likely won't be the final implementation, but a stepping stone. The reason for that is: THIS DOESN'T ACCOUNT FOR PARTIAL PARSING. Not accounting for partial parsing is problematic. There are 3 specific cases that we need to keep in mind / figure out: 1. What happens when a measure maintains `create_metric = True` and partial parsing happens? With this current workflow, a parsing error is raised because the measure tries to create the metric again as it has no way to know a metric has already been created 2. What happens when a measure changes from `create_metric = True` to `create_metric = False` and partial parsing happens?? With this current workflow, the metric will continue to exist because it doesn't know to check if a metric needs to be removed, or even how to check for that case. 3. What happens when a measure changes from `create_metric = False` to `create_metric = True` and partial parsing? With the current workflow, the metric gets created, which is what we want. --- core/dbt/parser/schema_yaml_readers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/dbt/parser/schema_yaml_readers.py b/core/dbt/parser/schema_yaml_readers.py index 2f2a3eb18e6..780a446cb71 100644 --- a/core/dbt/parser/schema_yaml_readers.py +++ b/core/dbt/parser/schema_yaml_readers.py @@ -491,6 +491,18 @@ def _get_non_additive_dimension( else: return None + def _create_metric(self, measure: UnparsedMeasure) -> None: + unparsed_metric = UnparsedMetric( + name=measure.name, + label=measure.name, + type="simple", + type_params=UnparsedMetricTypeParams(measure=measure.name, expr=measure.name), + description=measure.description or f"Metric created from measure {measure.name}", + ) + + parser = MetricParser(self.schema_parser, yaml=self.yaml) + parser.parse_metric(unparsed=unparsed_metric) + def _get_measures(self, unparsed_measures: List[UnparsedMeasure]) -> List[Measure]: measures: List[Measure] = [] for unparsed in unparsed_measures: @@ -507,6 +519,9 @@ def _get_measures(self, unparsed_measures: List[UnparsedMeasure]) -> List[Measur agg_time_dimension=unparsed.agg_time_dimension, ) ) + if unparsed.create_metric is True: + self._create_metric(unparsed) + return measures def parse_semantic_model(self, unparsed: UnparsedSemanticModel):