Skip to content

Commit

Permalink
Use MeasureReferences to ensure deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jun 28, 2024
1 parent 3005c7d commit 7e4de5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
19 changes: 7 additions & 12 deletions dbt_semantic_interfaces/implementations/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -242,26 +237,26 @@ 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)
)
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)

Expand Down
6 changes: 3 additions & 3 deletions dbt_semantic_interfaces/validations/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7e4de5b

Please sign in to comment.