From 7e4de5b624cc6be09e4073a86a984ef581786fd7 Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Fri, 28 Jun 2024 07:17:15 -0700 Subject: [PATCH] Use MeasureReferences to ensure deduplication --- .../implementations/metric.py | 19 +++++++------------ .../validations/metrics.py | 6 +++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/dbt_semantic_interfaces/implementations/metric.py b/dbt_semantic_interfaces/implementations/metric.py index 8c8da8a8..4a6313dd 100644 --- a/dbt_semantic_interfaces/implementations/metric.py +++ b/dbt_semantic_interfaces/implementations/metric.py @@ -16,12 +16,7 @@ PydanticWhereFilterIntersection, ) from dbt_semantic_interfaces.implementations.metadata import PydanticMetadata -from dbt_semantic_interfaces.protocols import ( - Metric, - MetricConfig, - MetricInputMeasure, - ProtocolHint, -) +from dbt_semantic_interfaces.protocols import Metric, MetricConfig, ProtocolHint from dbt_semantic_interfaces.references import MeasureReference, MetricReference from dbt_semantic_interfaces.type_enums import ( ConversionCalculationType, @@ -242,17 +237,17 @@ def input_metrics(self) -> Sequence[PydanticMetricInput]: @staticmethod def all_input_measures_for_metric( metric: Metric, metric_index: Dict[MetricReference, Metric] - ) -> Set[MetricInputMeasure]: + ) -> Set[MeasureReference]: """Gets all input measures for the metric, including those defined on input metrics (recursively).""" - measures = set() + measures: Set[MeasureReference] = set() if metric.type is MetricType.SIMPLE or metric.type is MetricType.CUMULATIVE: assert ( metric.type_params.measure is not None ), f"Metric {metric.name} should have a measure defined, but it does not." - measures.add(metric.type_params.measure) + measures.add(metric.type_params.measure.measure_reference) elif metric.type is MetricType.DERIVED or metric.type is MetricType.RATIO: for input_metric in metric.input_metrics: - nested_metric = metric_index.get(MetricReference(input_metric.name)) + nested_metric = metric_index.get(input_metric.as_reference) assert nested_metric, f"Could not find metric {input_metric.name} in semantic manifest." measures.update( PydanticMetric.all_input_measures_for_metric(metric=nested_metric, metric_index=metric_index) @@ -260,8 +255,8 @@ def all_input_measures_for_metric( elif metric.type is MetricType.CONVERSION: conversion_type_params = metric.type_params.conversion_type_params assert conversion_type_params, "Conversion metric should have conversion_type_params." - measures.add(conversion_type_params.base_measure) - measures.add(conversion_type_params.conversion_measure) + measures.add(conversion_type_params.base_measure.measure_reference) + measures.add(conversion_type_params.conversion_measure.measure_reference) else: assert_values_exhausted(metric.type) diff --git a/dbt_semantic_interfaces/validations/metrics.py b/dbt_semantic_interfaces/validations/metrics.py index fc07f4b4..bced1a64 100644 --- a/dbt_semantic_interfaces/validations/metrics.py +++ b/dbt_semantic_interfaces/validations/metrics.py @@ -592,9 +592,9 @@ def _min_queryable_granularity_for_metric( Defaults to DAY in the """ min_queryable_granularity: Optional[TimeGranularity] = None - for input_measure in PydanticMetric.all_input_measures_for_metric(metric=metric, metric_index=metric_index): - agg_time_dimension = measure_to_agg_time_dimension.get(input_measure.measure_reference) - assert agg_time_dimension, f"Measure '{input_measure.name}' not found in semantic manifest." + for measure_reference in PydanticMetric.all_input_measures_for_metric(metric=metric, metric_index=metric_index): + agg_time_dimension = measure_to_agg_time_dimension.get(measure_reference) + assert agg_time_dimension, f"Measure '{measure_reference.element_name}' not found in semantic manifest." if not agg_time_dimension.type_params: continue defined_time_granularity = agg_time_dimension.type_params.time_granularity