Skip to content

Commit

Permalink
Merge pull request #1144 from dbt-labs/remove-granularity-free-time-d…
Browse files Browse the repository at this point in the history
…imension-element-path-keys
  • Loading branch information
tlento authored Apr 23, 2024
2 parents d4a475f + 8164aaf commit 599fe42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
29 changes: 16 additions & 13 deletions metricflow/model/semantics/metric_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from dbt_semantic_interfaces.enum_extension import assert_values_exhausted
from dbt_semantic_interfaces.protocols.metric import Metric, MetricInputMeasure, MetricType
from dbt_semantic_interfaces.protocols.semantic_manifest import SemanticManifest
from dbt_semantic_interfaces.references import MeasureReference, MetricReference, TimeDimensionReference
from dbt_semantic_interfaces.references import MeasureReference, MetricReference

from metricflow.errors.errors import DuplicateMetricError, MetricNotFoundError, NonExistentMeasureError
from metricflow.model.semantics.linkable_element import ElementPathKey, LinkableElementProperty
from metricflow.model.semantics.linkable_element import LinkableElementProperty
from metricflow.model.semantics.linkable_element_set import LinkableElementSet
from metricflow.model.semantics.linkable_spec_resolver import (
ValidLinkableSpecResolver,
Expand Down Expand Up @@ -144,31 +144,34 @@ def contains_cumulative_or_time_offset_metric(self, metric_references: Sequence[
return True
return False

def _get_agg_time_dimension_path_keys_for_metric(
def _get_agg_time_dimension_specs_for_metric(
self, metric_reference: MetricReference
) -> Sequence[ElementPathKey]:
) -> Sequence[TimeDimensionSpec]:
"""Retrieves the aggregate time dimensions associated with the metric's measures."""
metric = self.get_metric(metric_reference)
path_keys = set()
specs: Set[TimeDimensionSpec] = set()
for input_measure in metric.input_measures:
path_key = self._semantic_model_lookup.get_agg_time_dimension_path_key_for_measure(
time_dimension_specs = self._semantic_model_lookup.get_agg_time_dimension_specs_for_measure(
measure_reference=input_measure.measure_reference
)
path_keys.add(path_key)
return list(path_keys)
specs.update(time_dimension_specs)
return list(specs)

def get_valid_agg_time_dimensions_for_metric(
self, metric_reference: MetricReference
) -> Sequence[TimeDimensionSpec]:
"""Get the agg time dimension specs that can be used in place of metric time for this metric, if applicable."""
agg_time_dimension_element_path_keys = self._get_agg_time_dimension_path_keys_for_metric(metric_reference)
if len(agg_time_dimension_element_path_keys) != 1:
agg_time_dimension_specs = self._get_agg_time_dimension_specs_for_metric(metric_reference)
distinct_agg_time_dimension_identifiers = set(
[(spec.reference, spec.entity_links) for spec in agg_time_dimension_specs]
)
if len(distinct_agg_time_dimension_identifiers) != 1:
# If the metric's input measures have different agg_time_dimensions, user must use metric_time.
return []

path_key = agg_time_dimension_element_path_keys[0]
agg_time_dimension_reference, agg_time_dimension_entity_links = distinct_agg_time_dimension_identifiers.pop()
valid_agg_time_dimension_specs = TimeDimensionSpec.generate_possible_specs_for_time_dimension(
time_dimension_reference=TimeDimensionReference(element_name=path_key.element_name),
entity_links=path_key.entity_links,
time_dimension_reference=agg_time_dimension_reference,
entity_links=agg_time_dimension_entity_links,
)
return valid_agg_time_dimension_specs
25 changes: 11 additions & 14 deletions metricflow/model/semantics/semantic_model_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from metricflow.errors.errors import InvalidSemanticModelError
from metricflow.mf_logging.pretty_print import mf_pformat
from metricflow.model.semantics.element_group import ElementGrouper
from metricflow.model.semantics.linkable_element import ElementPathKey
from metricflow.model.spec_converters import MeasureConverter
from metricflow.specs.specs import (
DimensionSpec,
Expand Down Expand Up @@ -350,22 +349,20 @@ def get_element_spec_for_name(self, element_name: str) -> LinkableInstanceSpec:
else:
raise ValueError(f"Unable to find linkable element {element_name} in manifest")

def get_agg_time_dimension_path_key_for_measure(self, measure_reference: MeasureReference) -> ElementPathKey:
"""Get the agg time dimension associated with the measure."""
agg_time_dimension = self.get_agg_time_dimension_for_measure(measure_reference)

# A measure's agg_time_dimension is required to be in the same semantic model as the measure,
# so we can assume the same semantic model for both measure and dimension.
semantic_model = self.get_semantic_model_for_measure(measure_reference)
entity_link = self.get_primary_entity_else_error(semantic_model)
return ElementPathKey(element_name=agg_time_dimension.element_name, entity_links=(entity_link,))

def get_agg_time_dimension_specs_for_measure(
self, measure_reference: MeasureReference
) -> Sequence[TimeDimensionSpec]:
"""Get the agg time dimension specs that can be used in place of metric time for this measure."""
path_key = self.get_agg_time_dimension_path_key_for_measure(measure_reference)
agg_time_dimension = self.get_agg_time_dimension_for_measure(measure_reference)
# A measure's agg_time_dimension is required to be in the same semantic model as the measure,
# so we can assume the same semantic model for both measure and dimension.
semantic_model = self.get_semantic_model_for_measure(measure_reference)
entity_link = self.resolved_primary_entity(semantic_model)
assert entity_link is not None, (
f"Expected semantic model {semantic_model} to have a primary entity since it has a "
"measure requiring an agg_time_dimension, but found none.",
)
return TimeDimensionSpec.generate_possible_specs_for_time_dimension(
time_dimension_reference=TimeDimensionReference(element_name=path_key.element_name),
entity_links=path_key.entity_links,
time_dimension_reference=agg_time_dimension,
entity_links=(entity_link,),
)

0 comments on commit 599fe42

Please sign in to comment.