Skip to content

Commit

Permalink
Improve branch combining logic for AggregateMeasuresNode
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Aug 21, 2024
1 parent 4756bef commit 1dff572
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions metricflow/dataflow/optimizer/source_scan/cm_branch_combiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,27 @@ def visit_aggregate_measures_node( # noqa: D102

assert len(combined_parent_nodes) == 1
combined_parent_node = combined_parent_nodes[0]
assert combined_parent_node is not None

combined_metric_input_measure_specs = tuple(
dict.fromkeys(
self._current_left_node.metric_input_measure_specs + current_right_node.metric_input_measure_specs
).keys()
)

# Avoid combining branches if the AggregateMeasuresNode specifies a metric with an alias to avoid
# collisions e.g. two metrics use the same alias for two different measures. This is not always the case,
# so this could be improved later.
seen_aliases = set()
for spec in combined_metric_input_measure_specs:
# Avoid combining branches if the AggregateMeasuresNode specifies a metric with an alias to avoid
# collisions e.g. two metrics use the same alias for two different measures. This is not always the case,
# so this could be improved later.
if spec.alias is not None:
self._log_combine_failure(
left_node=self._current_left_node,
right_node=current_right_node,
combine_failure_reason=f"Metric input measure spec {spec} has an alias",
)
return ComputeMetricsBranchCombinerResult()
if spec.alias in seen_aliases:
self._log_combine_failure(
left_node=self._current_left_node,
right_node=current_right_node,
combine_failure_reason=f"Found multiple metric input measure specs with alias '{spec.alias}'",
)
return ComputeMetricsBranchCombinerResult()
seen_aliases.add(spec.alias)

combined_node = AggregateMeasuresNode.create(
parent_node=combined_parent_node,
Expand Down

0 comments on commit 1dff572

Please sign in to comment.