Skip to content

Commit

Permalink
Add NoGroupByMetricPattern
Browse files Browse the repository at this point in the history
GroupByMetrics are allowed in filters, but not in group by items. Use new pattern to distinguish the two.
  • Loading branch information
courtneyholcomb committed May 13, 2024
1 parent 49ff11f commit 4f5244f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from metricflow_semantics.query.suggestion_generator import QueryItemSuggestionGenerator
from metricflow_semantics.specs.patterns.base_time_grain import BaseTimeGrainPattern
from metricflow_semantics.specs.patterns.no_group_by_metric import NoGroupByMetricPattern
from metricflow_semantics.specs.patterns.spec_pattern import SpecPattern
from metricflow_semantics.specs.patterns.typed_patterns import TimeDimensionPattern
from metricflow_semantics.specs.spec_classes import LinkableInstanceSpec
Expand Down Expand Up @@ -87,7 +88,7 @@ def resolve_matching_item_for_querying(
"""
push_down_visitor = _PushDownGroupByItemCandidatesVisitor(
manifest_lookup=self._manifest_lookup,
source_spec_patterns=(spec_pattern,),
source_spec_patterns=(spec_pattern, NoGroupByMetricPattern()),
suggestion_generator=suggestion_generator,
)

Expand Down Expand Up @@ -146,15 +147,12 @@ def resolve_matching_item_for_filters(
suggestion_generator = QueryItemSuggestionGenerator(
input_naming_scheme=ObjectBuilderNamingScheme(),
input_str=input_str,
candidate_filters=QueryItemSuggestionGenerator.GROUP_BY_ITEM_CANDIDATE_FILTERS,
candidate_filters=QueryItemSuggestionGenerator.FILTER_ITEM_CANDIDATE_FILTERS,
)

push_down_visitor = _PushDownGroupByItemCandidatesVisitor(
manifest_lookup=self._manifest_lookup,
source_spec_patterns=(
spec_pattern,
BaseTimeGrainPattern(),
),
source_spec_patterns=(spec_pattern, BaseTimeGrainPattern()),
suggestion_generator=suggestion_generator,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

from typing import List, Sequence

from typing_extensions import override

from metricflow_semantics.specs.patterns.spec_pattern import SpecPattern
from metricflow_semantics.specs.spec_classes import (
InstanceSpec,
LinkableInstanceSpec,
)
from metricflow_semantics.specs.spec_set import group_specs_by_type


class NoGroupByMetricPattern(SpecPattern):
"""Matches to linkable specs, but only if they're not group by metrics.
Group by metrics are allowed in filters but not in the query input group by.
"""

@override
def match(self, candidate_specs: Sequence[InstanceSpec]) -> Sequence[LinkableInstanceSpec]:
specs_to_return: List[LinkableInstanceSpec] = []
spec_set = group_specs_by_type(candidate_specs)
specs_to_return.extend(spec_set.time_dimension_specs)
specs_to_return.extend(spec_set.dimension_specs)
specs_to_return.extend(spec_set.entity_specs)

return specs_to_return

0 comments on commit 4f5244f

Please sign in to comment.