Repository navigation
fix: preserve can_repartition in WindowAggExec::replace_children (#26626) - #26675
Open
XiaoMingFa wants to merge 3 commits into
Open
XiaoMingFa wants to merge 3 commits into
XiaoMingFa wants to merge 3 commits into
Conversation
…che#26626) ## Which issue does this PR close? - Closes apache#26626. ## Rationale for this change When `datafusion.execution.target_partitions = 1`, the physical planner constructs `WindowAggExec` with `can_repartition = false` so that the window stage requires `SinglePartition` distribution and inserts a `CoalescePartitionsExec`. During physical optimization, replacing children via `replace_children` under `ChildrenPropertiesMode::Recompute` rebuilt `WindowAggExec` with hardcoded `can_repartition = true` instead of propagating `self.can_repartition`. This flipped the distribution requirement from `SinglePartition` to `KeyPartitioned`, stripping the required coalesce and triggering a `SanityCheckPlan` distribution error. `BoundedWindowAggExec` already correctly propagates `self.can_repartition` during `replace_children`. This change aligns `WindowAggExec` with `BoundedWindowAggExec`. ## What changes are included in this PR? - Pass `self.can_repartition` instead of `true` in `WindowAggExec::replace_children` when `ChildrenPropertiesMode::Recompute` is used. - Added unit test `test_window_agg_replace_children_preserves_can_repartition` in `window_agg_exec.rs` verifying preservation for both `false` and `true`. - Added sqllogictest regression test in `window.slt` verifying query execution with `target_partitions = 1`. ## Are these changes tested? Yes: - Local unit test `test_window_agg_replace_children_preserves_can_repartition` passed. - Integration test added to `window.slt`.
harsh-ande
approved these changes
Oct 29, 2026
harsh-ande
left a comment
Contributor
There was a problem hiding this comment.
Thanks @XiaoMingFa! I confirmed the bug on main: the new window.slt test case fails with SanityCheckPlan at target_partitions = 1. The fix makes WindowAggExec::replace_children keep self.can_repartition, similar to how BoundedWindowAggExec already does.
Also added another NIT comment.
Comment on lines
+706
to
+734
| // can_repartition = false should be preserved after replace_children with Recompute | ||
| let window = Arc::new(WindowAggExec::try_new( | ||
| vec![window_expr.clone()], | ||
| Arc::clone(&input), | ||
| false, | ||
| )?); | ||
| assert!(!window.can_repartition); | ||
|
|
||
| let new_child: Arc<dyn ExecutionPlan> = | ||
| Arc::new(TestMemoryExec::try_new(&[], Arc::clone(&schema), None)?); | ||
| let replaced = window.clone().replace_children( | ||
| vec![new_child], | ||
| ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute), | ||
| )?; | ||
| let replaced_window = replaced.downcast_ref::<WindowAggExec>().unwrap(); | ||
| assert_eq!(replaced_window.can_repartition, false); | ||
|
|
||
| // can_repartition = true should also be preserved | ||
| let window_true = | ||
| Arc::new(WindowAggExec::try_new(vec![window_expr], input, true)?); | ||
| assert!(window_true.can_repartition); | ||
| let new_child2: Arc<dyn ExecutionPlan> = | ||
| Arc::new(TestMemoryExec::try_new(&[], Arc::clone(&schema), None)?); | ||
| let replaced_true = window_true.clone().replace_children( | ||
| vec![new_child2], | ||
| ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute), | ||
| )?; | ||
| let replaced_window_true = replaced_true.downcast_ref::<WindowAggExec>().unwrap(); | ||
| assert_eq!(replaced_window_true.can_repartition, true); |
Contributor
There was a problem hiding this comment.
NIT: These 2 blocks of code differ only in the flag. So a for can_repartition in [false, true] loop would enhance readability and shorten the test.
Author
There was a problem hiding this comment.
Updated to iterate over [false, true] as suggested! Much cleaner and eliminates the duplicate block. Thanks @harsh-ande!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
target_partitions = 1(WindowAggExeclosescan_repartitionwhen its children are replaced) #26626.Rationale for this change
When
datafusion.execution.target_partitions = 1, the physical planner constructsWindowAggExecwithcan_repartition = falseso that the window stage requiresSinglePartitiondistribution and inserts aCoalescePartitionsExec.During physical optimization, replacing children via
replace_childrenunderChildrenPropertiesMode::RecomputerebuiltWindowAggExecwith hardcodedcan_repartition = trueinstead of propagatingself.can_repartition. This flipped the distribution requirement fromSinglePartitiontoKeyPartitioned, stripping the required coalesce and triggering aSanityCheckPlandistribution error.BoundedWindowAggExecalready correctly propagatesself.can_repartitionduringreplace_children. This change alignsWindowAggExecwithBoundedWindowAggExec.What changes are included in this PR?
self.can_repartitioninstead oftrueinWindowAggExec::replace_childrenwhenChildrenPropertiesMode::Recomputeis used.test_window_agg_replace_children_preserves_can_repartitioninwindow_agg_exec.rsverifying preservation for bothfalseandtrue.window.sltverifying query execution withtarget_partitions = 1.Are these changes tested?
Yes:
test_window_agg_replace_children_preserves_can_repartitionpasses.datafusion/sqllogictest/test_files/window.sltpasses.Are there any user-facing changes?
No.