Skip to content

Commit

Permalink
Dedupe qualified name for GroupByMetricSpecs (#1171)
Browse files Browse the repository at this point in the history
Typically, a spec's qualified name is `<entity_links>__<element_name>`.
In the case of `GroupByMetricSpecs`, this name isn't guaranteed to be
unique. That's because the `metric_subquery_entity_links` are not
accounted for. This will also be a problem in SQL generation, since you
could end up with a query that references two different group by metrics
by the same column name. To avoid that, here I added some deduping logic
for the qualified name.
1. If `entity_links` matches `metric_subquery_entity_links`, use the
standard qualified name.
2. If they don't match, include both, e.g.,
`<entity_links>__<metric_subquery_entity_links>__<element_name>`. You
can still tell where one path ends and the next begins because they will
both end with the same entity link, which is the last link before the
element name.
  • Loading branch information
courtneyholcomb authored May 3, 2024
1 parent 02c3fdd commit 62db0e8
Show file tree
Hide file tree
Showing 125 changed files with 924 additions and 923 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from metricflow_semantics.model.semantic_manifest_lookup import SemanticManifestLookup
from metricflow_semantics.naming.linkable_spec_name import DUNDER, StructuredLinkableSpecName
from metricflow_semantics.naming.linkable_spec_name import DUNDER
from metricflow_semantics.specs.column_assoc import (
ColumnAssociation,
ColumnAssociationResolver,
Expand Down Expand Up @@ -59,23 +59,13 @@ def visit_measure_spec(self, measure_spec: MeasureSpec) -> ColumnAssociation: #

def visit_dimension_spec(self, dimension_spec: DimensionSpec) -> ColumnAssociation: # noqa: D102
return ColumnAssociation(
column_name=StructuredLinkableSpecName(
entity_link_names=tuple(x.element_name for x in dimension_spec.entity_links),
element_name=dimension_spec.element_name,
).qualified_name,
column_name=dimension_spec.qualified_name,
single_column_correlation_key=SingleColumnCorrelationKey(),
)

def visit_time_dimension_spec(self, time_dimension_spec: TimeDimensionSpec) -> ColumnAssociation: # noqa: D102
column_name = StructuredLinkableSpecName(
entity_link_names=tuple(x.element_name for x in time_dimension_spec.entity_links),
element_name=time_dimension_spec.element_name,
time_granularity=time_dimension_spec.time_granularity,
date_part=time_dimension_spec.date_part,
).qualified_name

return ColumnAssociation(
column_name=column_name
column_name=time_dimension_spec.qualified_name
+ (
f"{DUNDER}{time_dimension_spec.aggregation_state.value.lower()}"
if time_dimension_spec.aggregation_state
Expand All @@ -86,19 +76,13 @@ def visit_time_dimension_spec(self, time_dimension_spec: TimeDimensionSpec) -> C

def visit_entity_spec(self, entity_spec: EntitySpec) -> ColumnAssociation: # noqa: D102
return ColumnAssociation(
column_name=StructuredLinkableSpecName(
entity_link_names=tuple(x.element_name for x in entity_spec.entity_links),
element_name=entity_spec.element_name,
).qualified_name,
column_name=entity_spec.qualified_name,
single_column_correlation_key=SingleColumnCorrelationKey(),
)

def visit_group_by_metric_spec(self, group_by_metric_spec: GroupByMetricSpec) -> ColumnAssociation: # noqa: D102
return ColumnAssociation(
column_name=StructuredLinkableSpecName(
entity_link_names=tuple(x.element_name for x in group_by_metric_spec.entity_links),
element_name=group_by_metric_spec.element_name,
).qualified_name,
column_name=group_by_metric_spec.qualified_name,
single_column_correlation_key=SingleColumnCorrelationKey(),
)

Expand Down
17 changes: 17 additions & 0 deletions metricflow-semantics/metricflow_semantics/specs/spec_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,23 @@ def metric_subquery_entity_spec(self) -> EntitySpec:
entity_links=self.metric_subquery_entity_links[:-1],
)

@property
def qualified_name(self) -> str:
"""Element name prefixed with entity links.
If same entity links are used in inner & outer query, use standard qualified name (country__bookings).
Else, specify both sets of entity links (listing__country__user__country__bookings).
"""
if self.entity_links == self.metric_subquery_entity_links:
entity_links = self.entity_links
else:
entity_links = self.entity_links + self.metric_subquery_entity_links

return StructuredLinkableSpecName(
entity_link_names=tuple(entity_link.element_name for entity_link in entity_links),
element_name=self.element_name,
).qualified_name

def __eq__(self, other: Any) -> bool: # type: ignore[misc] # noqa: D105
if not isinstance(other, GroupByMetricSpec):
return False
Expand Down
Loading

0 comments on commit 62db0e8

Please sign in to comment.