From e2b616aa04f539cc32fe5320b6e9dda5aec2d954 Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Mon, 29 Apr 2024 12:25:20 -0700 Subject: [PATCH] Use qualified name as ElementPathKey element name to ensure uniqueness There might be multiple linkable metrics available for a given metric_name + entity_links combo. This is because there might be multiple options for entity paths in the metric subquery, which is not accounted for here. Using the qualified name ensures we will have a unique ElementPathKey for each LinkableMetric. --- .../model/semantics/linkable_spec_resolver.py | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/metricflow/model/semantics/linkable_spec_resolver.py b/metricflow/model/semantics/linkable_spec_resolver.py index 61541666b9..3d49ca3ad2 100644 --- a/metricflow/model/semantics/linkable_spec_resolver.py +++ b/metricflow/model/semantics/linkable_spec_resolver.py @@ -120,8 +120,23 @@ def element_name(self) -> str: # noqa: D102 return self.reference.element_name @property - def path_key(self) -> ElementPathKey: # noqa: D102 - return ElementPathKey(element_name=self.element_name, entity_links=self.join_path.entity_links) + def path_key(self) -> ElementPathKey: + """Key that can uniquely identify an element and the joins used to realize the element. + + Use qualified name to guarantee uniqueness. + """ + return ElementPathKey( + element_name=self.as_group_by_metric_spec.qualified_name, entity_links=self.join_path.entity_links + ) + + @property + def as_group_by_metric_spec(self) -> GroupByMetricSpec: + """Convert LinkableMetric to GroupByMetricSpec.""" + return GroupByMetricSpec( + element_name=self.element_name, + entity_links=self.join_path.entity_links, + metric_subquery_entity_links=self.metric_subquery_entity_links, + ) @property def reference(self) -> MetricReference: # noqa: D102 @@ -257,7 +272,9 @@ def intersection_by_path_key(linkable_element_sets: Sequence[LinkableElementSet] for path_key, entities in join_path_to_linkable_entities.items() }, path_key_to_linkable_metrics={ - path_key: tuple(sorted(metrics, key=lambda linkable_metric: linkable_metric.element_name)) + path_key: tuple( + sorted(metrics, key=lambda linkable_metric: linkable_metric.as_group_by_metric_spec.qualified_name) + ) for path_key, metrics in join_path_to_linkable_metrics.items() }, ) @@ -355,11 +372,7 @@ def as_spec_set(self) -> LinkableSpecSet: # noqa: D102 for path_key in self.path_key_to_linkable_entities ), group_by_metric_specs=tuple( - GroupByMetricSpec( - element_name=linkable_metric.element_name, - entity_links=linkable_metric.join_path.entity_links, - metric_subquery_entity_links=linkable_metric.metric_subquery_entity_links, - ) + linkable_metric.as_group_by_metric_spec for linkable_metrics in self.path_key_to_linkable_metrics.values() for linkable_metric in linkable_metrics ),