diff --git a/metricflow-semantics/metricflow_semantics/query/validation_rules/metric_time_requirements.py b/metricflow-semantics/metricflow_semantics/query/validation_rules/metric_time_requirements.py index 5b215562d8..1e6ec6cd40 100644 --- a/metricflow-semantics/metricflow_semantics/query/validation_rules/metric_time_requirements.py +++ b/metricflow-semantics/metricflow_semantics/query/validation_rules/metric_time_requirements.py @@ -1,8 +1,7 @@ from __future__ import annotations from dataclasses import dataclass -from functools import lru_cache -from typing import List, Sequence +from typing import List, Sequence, Tuple from dbt_semantic_interfaces.enum_extension import assert_values_exhausted from dbt_semantic_interfaces.naming.keywords import METRIC_TIME_ELEMENT_NAME @@ -14,6 +13,7 @@ from dbt_semantic_interfaces.type_enums import MetricType from typing_extensions import override +from metricflow_semantics.collection_helpers.lru_cache import LruCache from metricflow_semantics.model.semantic_manifest_lookup import SemanticManifestLookup from metricflow_semantics.query.group_by_item.resolution_path import MetricFlowQueryResolutionPath from metricflow_semantics.query.issues.issues_base import MetricFlowQueryResolutionIssueSet @@ -65,10 +65,23 @@ def __init__( # noqa: D107 custom_granularities=self._manifest_lookup.custom_granularities, ) ) + self._query_items_analysis_cache: LruCache[ + Tuple[ResolverInputForQuery, MetricReference], QueryItemsAnalysis + ] = LruCache(128) - @lru_cache def _get_query_items_analysis( self, query_resolver_input: ResolverInputForQuery, metric_reference: MetricReference + ) -> QueryItemsAnalysis: + cache_key = (query_resolver_input, metric_reference) + result = self._query_items_analysis_cache.get(cache_key) + if result is not None: + return result + result = self._uncached_query_items_analysis(query_resolver_input, metric_reference) + self._query_items_analysis_cache.set(cache_key, result) + return result + + def _uncached_query_items_analysis( + self, query_resolver_input: ResolverInputForQuery, metric_reference: MetricReference ) -> QueryItemsAnalysis: has_agg_time_dimension = False has_metric_time = False