Skip to content

Commit

Permalink
/* PR_START p--short-term-perf 12 */ Cache `SemanticModelLookup.get_d…
Browse files Browse the repository at this point in the history
…efined_time_granularity()`.
  • Loading branch information
plypaul committed Oct 1, 2024
1 parent bfc147b commit 0498d82
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 0498d82

Please sign in to comment.