Skip to content

Commit

Permalink
/* PR_START p--short-term-perf 17 */ Update patterns to use a filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Oct 2, 2024
1 parent cf2796e commit 3f61cc0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing
from contextlib import contextmanager
from dataclasses import dataclass
from typing import Dict, FrozenSet, Iterator, List, Optional, Sequence, Set, Tuple
from typing import Dict, Iterator, List, Optional, Sequence, Tuple

from dbt_semantic_interfaces.enum_extension import assert_values_exhausted
from dbt_semantic_interfaces.references import MetricReference
Expand All @@ -15,7 +15,6 @@
from metricflow_semantics.mf_logging.formatting import indent
from metricflow_semantics.mf_logging.lazy_formattable import LazyFormat
from metricflow_semantics.mf_logging.pretty_print import mf_pformat, mf_pformat_many
from metricflow_semantics.model.linkable_element_property import LinkableElementProperty
from metricflow_semantics.model.semantic_manifest_lookup import SemanticManifestLookup
from metricflow_semantics.model.semantics.element_filter import LinkableElementFilter
from metricflow_semantics.query.group_by_item.candidate_push_down.group_by_item_candidate import GroupByItemCandidateSet
Expand Down Expand Up @@ -148,8 +147,6 @@ def __init__(
suggestion_generator: Optional[QueryItemSuggestionGenerator],
group_by_item_resolver: GroupByItemResolver,
source_spec_patterns: Sequence[SpecPattern] = (),
with_any_property: Optional[FrozenSet[LinkableElementProperty]] = None,
without_any_property: Optional[FrozenSet[LinkableElementProperty]] = None,
filter_location: Optional[WhereFilterLocation] = None,
) -> None:
"""Initializer.
Expand All @@ -168,8 +165,6 @@ def __init__(
self._semantic_manifest_lookup = manifest_lookup
self._source_spec_patterns = tuple(source_spec_patterns)
self._path_from_start_node_tracker = DagTraversalPathTracker()
self._with_any_property = with_any_property
self._without_any_property = without_any_property
self._suggestion_generator = suggestion_generator
self._filter_location = filter_location
self._group_by_item_resolver_for_query = group_by_item_resolver
Expand All @@ -180,17 +175,10 @@ def visit_measure_node(self, node: MeasureGroupByItemSourceNode) -> PushDownResu
with self._path_from_start_node_tracker.track_node_visit(node) as current_traversal_path:
logger.debug(LazyFormat(lambda: f"Handling {node.ui_description}"))

without_any_property: Set[LinkableElementProperty] = set()
if self._without_any_property is not None:
without_any_property.update(self._without_any_property)
for spec_pattern in self._source_spec_patterns:
without_any_property.update(spec_pattern.without_linkable_element_properties)

items_available_for_measure = self._semantic_manifest_lookup.metric_lookup.linkable_elements_for_measure(
measure_reference=node.measure_reference,
element_filter=LinkableElementFilter(
with_any_of=self._with_any_property or LinkableElementProperty.all_properties(),
without_any_of=frozenset(without_any_property),
element_filter=LinkableElementFilter.merge_iterable(
spec_pattern.element_pre_filter for spec_pattern in self._source_spec_patterns
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import logging
from dataclasses import dataclass
from enum import Enum
from typing import Any, FrozenSet, List, Optional, Sequence, Tuple
from typing import Any, List, Optional, Sequence, Tuple

from dbt_semantic_interfaces.references import EntityReference
from dbt_semantic_interfaces.type_enums.date_part import DatePart
from more_itertools import is_sorted
from typing_extensions import override

from metricflow_semantics.model.linkable_element_property import LinkableElementProperty
from metricflow_semantics.model.semantics.element_filter import LinkableElementFilter
from metricflow_semantics.specs.instance_spec import InstanceSpec, LinkableInstanceSpec
from metricflow_semantics.specs.patterns.spec_pattern import SpecPattern
from metricflow_semantics.specs.spec_set import group_specs_by_type
Expand Down Expand Up @@ -152,10 +153,11 @@ def match(self, candidate_specs: Sequence[InstanceSpec]) -> Sequence[LinkableIns

@property
@override
def without_linkable_element_properties(self) -> FrozenSet[LinkableElementProperty]:
def element_pre_filter(self) -> LinkableElementFilter:
if (
self.parameter_set.metric_subquery_entity_links is None
or len(self.parameter_set.metric_subquery_entity_links) == 0
):
return frozenset({LinkableElementProperty.METRIC})
return frozenset()
return LinkableElementFilter(without_any_of=frozenset({LinkableElementProperty.METRIC}))

return LinkableElementFilter()
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import FrozenSet, List, Sequence
from typing import List, Sequence

from typing_extensions import override

from metricflow_semantics.model.linkable_element_property import LinkableElementProperty
from metricflow_semantics.model.semantics.element_filter import LinkableElementFilter
from metricflow_semantics.specs.instance_spec import InstanceSpec, LinkableInstanceSpec
from metricflow_semantics.specs.patterns.spec_pattern import SpecPattern
from metricflow_semantics.specs.spec_set import group_specs_by_type
Expand All @@ -30,5 +31,5 @@ def match(self, candidate_specs: Sequence[InstanceSpec]) -> Sequence[LinkableIns

@property
@override
def without_linkable_element_properties(self) -> FrozenSet[LinkableElementProperty]:
return frozenset({LinkableElementProperty.METRIC})
def element_pre_filter(self) -> LinkableElementFilter:
return LinkableElementFilter(without_any_of=frozenset({LinkableElementProperty.METRIC}))
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, FrozenSet, Sequence
from typing import TYPE_CHECKING, Sequence

from metricflow_semantics.model.linkable_element_property import LinkableElementProperty
from metricflow_semantics.model.semantics.element_filter import LinkableElementFilter

if TYPE_CHECKING:
from metricflow_semantics.specs.instance_spec import InstanceSpec
Expand All @@ -25,6 +25,9 @@ def matches_any(self, candidate_specs: Sequence[InstanceSpec]) -> bool:
return len(self.match(candidate_specs)) > 0

@property
def without_linkable_element_properties(self) -> FrozenSet[LinkableElementProperty]:
"""Returns the set of properties of linkable elements that this won't match."""
return frozenset()
def element_pre_filter(self) -> LinkableElementFilter:
"""Returns a filter for a `LinkableElementSet` that can reduce the number of items to match.
i.e. the filter can produce a superset of the elements that will match.
"""
return LinkableElementFilter()
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import FrozenSet, List, Sequence
from typing import List, Sequence

from dbt_semantic_interfaces.call_parameter_sets import (
DimensionCallParameterSet,
Expand All @@ -13,6 +13,7 @@
from typing_extensions import override

from metricflow_semantics.model.linkable_element_property import LinkableElementProperty
from metricflow_semantics.model.semantics.element_filter import LinkableElementFilter
from metricflow_semantics.naming.linkable_spec_name import StructuredLinkableSpecName
from metricflow_semantics.specs.instance_spec import InstanceSpec, LinkableInstanceSpec
from metricflow_semantics.specs.patterns.entity_link_pattern import (
Expand Down Expand Up @@ -53,8 +54,8 @@ def from_call_parameter_set( # noqa: D102

@property
@override
def without_linkable_element_properties(self) -> FrozenSet[LinkableElementProperty]:
return frozenset({LinkableElementProperty.METRIC})
def element_pre_filter(self) -> LinkableElementFilter:
return LinkableElementFilter(without_any_of=frozenset({LinkableElementProperty.METRIC}))


@dataclass(frozen=True)
Expand Down Expand Up @@ -99,8 +100,8 @@ def from_call_parameter_set(

@property
@override
def without_linkable_element_properties(self) -> FrozenSet[LinkableElementProperty]:
return frozenset({LinkableElementProperty.METRIC})
def element_pre_filter(self) -> LinkableElementFilter:
return LinkableElementFilter(without_any_of=frozenset({LinkableElementProperty.METRIC}))


@dataclass(frozen=True)
Expand Down Expand Up @@ -130,8 +131,8 @@ def from_call_parameter_set(entity_call_parameter_set: EntityCallParameterSet) -

@property
@override
def without_linkable_element_properties(self) -> FrozenSet[LinkableElementProperty]:
return frozenset({LinkableElementProperty.METRIC})
def element_pre_filter(self) -> LinkableElementFilter:
return LinkableElementFilter(without_any_of=frozenset({LinkableElementProperty.METRIC}))


@dataclass(frozen=True)
Expand Down

0 comments on commit 3f61cc0

Please sign in to comment.