Skip to content

Commit

Permalink
address reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDee committed Nov 4, 2024
1 parent 8129081 commit 0b47079
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import logging
from typing import Dict, List, Optional, Sequence, Set
from functools import cached_property
from typing import Dict, List, Optional, Sequence, Set, Tuple

from dbt_semantic_interfaces.protocols.dimension import Dimension
from dbt_semantic_interfaces.protocols.entity import Entity
Expand Down Expand Up @@ -75,10 +76,10 @@ def __init__(self, model: SemanticManifest, custom_granularities: Dict[str, Expa
self._measure_lookup = MeasureLookup(sorted_semantic_models, custom_granularities)
self._dimension_lookup = DimensionLookup(sorted_semantic_models)

@property
def custom_granularity_names(self) -> Sequence[str]:
@cached_property
def custom_granularity_names(self) -> Tuple[str, ...]:
"""Returns all the custom_granularity names."""
return list(self._custom_granularities.keys())
return tuple(self._custom_granularities.keys())

def get_dimension_references(self) -> Sequence[DimensionReference]:
"""Retrieve all dimension references from the collection of semantic models."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
from functools import lru_cache
from typing import Optional, Sequence, Tuple

from dbt_semantic_interfaces.type_enums.date_part import DatePart
Expand Down Expand Up @@ -34,6 +35,7 @@ def __init__(
self.date_part = date_part

@staticmethod
@lru_cache
def from_name(qualified_name: str, custom_granularity_names: Sequence[str]) -> StructuredLinkableSpecName:
"""Construct from a name e.g. listing__ds__month."""
name_parts = qualified_name.split(DUNDER)
Expand All @@ -52,10 +54,13 @@ def from_name(qualified_name: str, custom_granularity_names: Sequence[str]) -> S
for granularity in TimeGranularity:
if name_parts[-1] == granularity.value:
associated_granularity = granularity.value
break

for custom_grain in custom_granularity_names:
if name_parts[-1] == custom_grain:
associated_granularity = custom_grain
if associated_granularity is None:
for custom_grain in custom_granularity_names:
if name_parts[-1] == custom_grain:
associated_granularity = custom_grain
break

# Has a time granularity
if associated_granularity:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Optional, Sequence
from typing import Optional, Sequence, Tuple

from dbt_semantic_interfaces.call_parameter_sets import (
TimeDimensionCallParameterSet,
Expand Down Expand Up @@ -69,7 +69,7 @@ def __init__( # noqa
spec_resolution_lookup: FilterSpecResolutionLookUp,
where_filter_location: WhereFilterLocation,
rendered_spec_tracker: RenderedSpecTracker,
custom_granularity_names: Sequence[str],
custom_granularity_names: Tuple[str, ...],
):
self._column_association_resolver = column_association_resolver
self._resolved_spec_lookup = spec_resolution_lookup
Expand Down
3 changes: 1 addition & 2 deletions metricflow/engine/metricflow_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ def simple_dimensions_for_metrics( # noqa: D102
else None
),
).qualified_name,
entity_links=(),
description="Event time for metrics.",
metadata=None,
type_params=PydanticDimensionTypeParams(
Expand Down Expand Up @@ -658,7 +659,6 @@ def simple_dimensions_for_metrics( # noqa: D102
dimension_reference=linkable_dimension.reference,
),
entity_links=path_key.entity_links,
custom_granularity_names=self._semantic_manifest_lookup.semantic_model_lookup.custom_granularity_names,
)
)
return sorted(dimensions, key=lambda dimension: dimension.qualified_name)
Expand All @@ -676,7 +676,6 @@ def list_dimensions(self) -> List[Dimension]: # noqa: D102
semantic_model=semantic_model, dimension_reference=dimension_reference
),
entity_links=(SemanticModelHelper.resolved_primary_entity(semantic_model),),
custom_granularity_names=self._semantic_manifest_lookup.semantic_model_lookup.custom_granularity_names,
)
)

Expand Down
11 changes: 5 additions & 6 deletions metricflow/engine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,18 @@ class Dimension:
qualified_name: str
description: Optional[str]
type: DimensionType
entity_links: Tuple[EntityReference, ...]
type_params: Optional[DimensionTypeParams]
metadata: Optional[Metadata]
is_partition: bool = False
expr: Optional[str] = None
label: Optional[str] = None
_possible_custom_granularity_names: Sequence[str] = ()

@classmethod
def from_pydantic(
cls,
pydantic_dimension: SemanticManifestDimension,
entity_links: Tuple[EntityReference, ...],
custom_granularity_names: Sequence[str],
) -> Dimension:
"""Build from pydantic Dimension and entity_key."""
qualified_name = DimensionSpec(element_name=pydantic_dimension.name, entity_links=entity_links).qualified_name
Expand All @@ -111,7 +110,7 @@ def from_pydantic(
is_partition=pydantic_dimension.is_partition,
expr=pydantic_dimension.expr,
label=pydantic_dimension.label,
_possible_custom_granularity_names=custom_granularity_names,
entity_links=entity_links,
)

@property
Expand All @@ -125,9 +124,9 @@ def granularity_free_qualified_name(self) -> str:
Dimension set has de-duplicated TimeDimensions such that you never have more than one granularity
in your set for each TimeDimension.
"""
return StructuredLinkableSpecName.from_name(
qualified_name=self.qualified_name, custom_granularity_names=self._possible_custom_granularity_names
).granularity_free_qualified_name
return StructuredLinkableSpecName(
entity_link_names=tuple(e.element_name for e in self.entity_links), element_name=self.name
).qualified_name


@dataclass(frozen=True)
Expand Down

0 comments on commit 0b47079

Please sign in to comment.