Skip to content

Commit

Permalink
Cache SemanticModelLookup.get_defined_time_granularity() (#1437)
Browse files Browse the repository at this point in the history
The defined time granularity of a time dimension is frequently called
during query resolution to figure out the time granularity for an
ambiguous group-by item.
  • Loading branch information
plypaul authored and courtneyholcomb committed Oct 8, 2024
1 parent 880d8a7 commit a9ed2ad
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def __init__(self, model: SemanticManifest, custom_granularities: Dict[str, Expa
for semantic_model in sorted(model.semantic_models, key=lambda semantic_model: semantic_model.name):
self._add_semantic_model(semantic_model)

# Cache for defined time granularity.
self._time_dimension_to_defined_time_granularity: Dict[TimeDimensionReference, TimeGranularity] = {}

def get_dimension_references(self) -> Sequence[DimensionReference]:
"""Retrieve all dimension references from the collection of semantic models."""
return tuple(self._dimension_index.keys())
Expand Down Expand Up @@ -378,6 +381,16 @@ def get_agg_time_dimension_specs_for_measure(

def get_defined_time_granularity(self, time_dimension_reference: TimeDimensionReference) -> TimeGranularity:
"""Time granularity from the time dimension's YAML definition. If not set, defaults to DAY."""
result = self._time_dimension_to_defined_time_granularity.get(time_dimension_reference)

if result is not None:
return result

result = self._get_defined_time_granularity(time_dimension_reference)
self._time_dimension_to_defined_time_granularity[time_dimension_reference] = result
return result

def _get_defined_time_granularity(self, time_dimension_reference: TimeDimensionReference) -> TimeGranularity:
time_dimension = self.get_dimension(time_dimension_reference)

defined_time_granularity = DEFAULT_TIME_GRANULARITY
Expand Down

0 comments on commit a9ed2ad

Please sign in to comment.