Skip to content

Commit

Permalink
Handle both entity paths in filter spec pattern logic
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Apr 29, 2024
1 parent 72d233a commit ed3559c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions metricflow/specs/patterns/entity_link_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ParameterSetField(Enum):
ENTITY_LINKS = "entity_links"
TIME_GRANULARITY = "time_granularity"
DATE_PART = "date_part"
METRIC_SUBQUERY_ENTITY_LINKS = "metric_subquery_entity_links"

def __lt__(self, other: Any) -> bool: # type: ignore[misc]
"""Allow for ordering so that a sequence of these can be consistently represented for test snapshots."""
Expand All @@ -50,6 +51,7 @@ class EntityLinkPatternParameterSet:
# Properties of time dimensions to match.
time_granularity: Optional[TimeGranularity] = None
date_part: Optional[DatePart] = None
metric_subquery_entity_links: Optional[Tuple[EntityReference, ...]] = None

@staticmethod
def from_parameters( # noqa: D102
Expand All @@ -58,13 +60,15 @@ def from_parameters( # noqa: D102
entity_links: Optional[Sequence[EntityReference]] = None,
time_granularity: Optional[TimeGranularity] = None,
date_part: Optional[DatePart] = None,
metric_subquery_entity_links: Optional[Tuple[EntityReference, ...]] = None,
) -> EntityLinkPatternParameterSet:
return EntityLinkPatternParameterSet(
fields_to_compare=tuple(sorted(fields_to_compare)),
element_name=element_name,
entity_links=tuple(entity_links) if entity_links is not None else None,
time_granularity=time_granularity,
date_part=date_part,
metric_subquery_entity_links=metric_subquery_entity_links,
)

def __post_init__(self) -> None:
Expand Down
10 changes: 7 additions & 3 deletions metricflow/specs/patterns/typed_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,23 @@ def from_call_parameter_set( # noqa: D102
"Currently only one group by item is allowed for Metric filters. "
"This should have been caught by validations."
)
group_by = metric_call_parameter_set.group_by[0]
structured_name = StructuredLinkableSpecName.from_name(group_by.element_name)
entity_links = tuple(
structured_name = StructuredLinkableSpecName.from_name(metric_call_parameter_set.group_by[0].element_name)
metric_subquery_entity_links = tuple(
EntityReference(entity_name)
for entity_name in (structured_name.entity_link_names + (structured_name.element_name,))
)
# Temp: we don't have a parameter to specify the join path from the outer query to the metric subquery,
# so just use the last entity. Will need to add another param for that later.
entity_links = metric_subquery_entity_links[-1:]
return GroupByMetricPattern(
parameter_set=EntityLinkPatternParameterSet.from_parameters(
fields_to_compare=(
ParameterSetField.ELEMENT_NAME,
ParameterSetField.ENTITY_LINKS,
ParameterSetField.METRIC_SUBQUERY_ENTITY_LINKS,
),
element_name=metric_call_parameter_set.metric_reference.element_name,
entity_links=entity_links,
metric_subquery_entity_links=metric_subquery_entity_links,
)
)

0 comments on commit ed3559c

Please sign in to comment.