-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: keep a CoalescePartitionsExec required by a SinglePartition child #23948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -609,11 +609,40 @@ fn adjust_window_sort_removal( | |
| /// the plan, some of the remaining `RepartitionExec`s might become unnecessary. | ||
| /// Removes such `RepartitionExec`s from the plan as well. | ||
| fn remove_bottleneck_in_subplan( | ||
| requirements: PlanWithCorrespondingCoalescePartitions, | ||
| ) -> Result<PlanWithCorrespondingCoalescePartitions> { | ||
| // The root is the node `parallelize_sorts` is rewriting (a `SortExec`, | ||
| // `SortPreservingMergeExec` or `CoalescePartitionsExec`). Its own distribution | ||
| // requirement does not constrain the removal, because the caller drops the node and | ||
| // rebuilds the cascade around the result. | ||
| remove_bottleneck_in_subplan_impl(requirements, true) | ||
| } | ||
|
|
||
| fn remove_bottleneck_in_subplan_impl( | ||
| mut requirements: PlanWithCorrespondingCoalescePartitions, | ||
| is_root: bool, | ||
| ) -> Result<PlanWithCorrespondingCoalescePartitions> { | ||
| let plan = &requirements.plan; | ||
| // Below the root, a `CoalescePartitionsExec` feeding a child that requires | ||
| // `Distribution::SinglePartition` is not an avoidable bottleneck: it is what satisfies | ||
| // that requirement. Removing it leaves the parent with a multi-partition input it cannot | ||
| // accept, and nothing re-runs distribution enforcement afterwards, so the plan reaches | ||
| // `SanityCheckPlan` invalid. The traversal reaches such a node because | ||
| // `update_coalesce_ctx_children` marks a node as connected when *any* child qualifies: | ||
| // a `CollectLeft` `HashJoinExec` whose probe side is connected is descended into even | ||
| // though its build side must stay single-partition. | ||
| let removable = |idx: usize| { | ||
| is_root | ||
| || !matches!( | ||
| plan.input_distribution_requirements() | ||
| .child_distribution(idx), | ||
| Some(Distribution::SinglePartition) | ||
| ) | ||
|
Comment on lines
+634
to
+640
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question (non-blocking): should this also protect |
||
| }; | ||
|
Comment on lines
+634
to
+641
|
||
| let remove_from_first_child = | ||
| is_coalesce_partitions(&requirements.children[0].plan) && removable(0); | ||
|
Comment on lines
+634
to
+643
|
||
| let children = &mut requirements.children; | ||
| if is_coalesce_partitions(&children[0].plan) { | ||
| if remove_from_first_child { | ||
| // We can safely use the 0th index since we have a `CoalescePartitionsExec`. | ||
| let mut new_child_node = children[0].children.swap_remove(0); | ||
| while new_child_node.plan.output_partitioning() == plan.output_partitioning() | ||
|
|
@@ -627,9 +656,10 @@ fn remove_bottleneck_in_subplan( | |
| requirements.children = requirements | ||
| .children | ||
| .into_iter() | ||
| .map(|node| { | ||
| if node.data { | ||
| remove_bottleneck_in_subplan(node) | ||
| .enumerate() | ||
| .map(|(idx, node)| { | ||
| if node.data && removable(idx) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth a short comment that this is deliberately conservative: skipping the descent entirely also skips legitimate cleanups below the protected child (e.g. a redundant second coalesce underneath the load-bearing one). Correctness-first is the right call for a fix — just flagging that the gate could later be narrowed to "descend, but protect only the top-level coalesce" if anyone cares about the missed optimization. |
||
| remove_bottleneck_in_subplan_impl(node, false) | ||
| } else { | ||
| Ok(node) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I verified this locally and I'm afraid these two unit tests don't actually cover the regression: with the fix commit reverted (and
cargo clean -p datafusion-physical-optimizerto force a rebuild), both tests still pass. I then instrumented the pre-fixchildren[0]-coalesce-removal branch with aneprintln!probe — it never fires for these mock plans, so they never reach the buggy path at all. The snapshots are identical pre/post fix.(The sqllogictest is fine — on the reverted code it fails with exactly the reported
does not satisfy distribution requirements: SinglePartitionerror, and passes with the fix.)My guess at the cause: by the time
parallelize_sortsruns, the distribution phase has already restructured the probe side, so the coalesce link that makes the traversal descend into the join in the real reproducer isn't there in this mock shape. Could you either make these construct the pre-phase-3a plan shape directly (so the removal branch demonstrably fires — aneprintlnprobe onmainis a quick way to confirm), or drop them and let the sqllogictest carry the regression? As-is they'd stay green if the bug were reintroduced.