Repository navigation
fix: WindowTopN keeps the plan when its rewrite breaks a requirement that held - #27772
jayzhan211 wants to merge 2 commits into
Conversation
|
I reproduced an To reproduce, keep the data, SQL, and expected rows in the regression test in this PR and change where the helper inserts - let enforcement = position(&rules, EnsureRequirements::new().name());
- rules.insert(enforcement + 1, window_topn);
+ let removal = rules
+ .iter()
+ .rposition(|rule| rule.name() == "OutputRequirements")
+ .unwrap();
+ rules.insert(removal + 1, window_topn);cargo test -p datafusion window_topn_after_enforcement_keeps_order_relied_onI ran the same SQL in a standalone harness. The PR returned the expected order at the original rule position. At the later position, both the base and the PR head returned Could we extend this fix and its regression test to cover that later rule position? AI assisted with source review, test design, and drafting. |
Thanks for the repro. I'd like to keep this PR to positions before Your case reproduces on this branch: with |
|
A separate PR works for me. Thanks for reproducing it. |
Which issue does this PR close?
Rationale for this change
A rule list that runs
WindowTopNafterEnsureRequirementscan return rows in the wrong order. ForEnsureRequirementssorts the window's input bypk ASC NULLS LASTand drops the sort forORDER BY pk, since the window's output already has that order.WindowTopNthen puts aPartitionedTopKExecunder the window, which emitspkwithNULLS FIRST, so the row with a NULLpkcomes first. When an order-preserving repartition and a merge sit above the window, the same rewrite fails planning instead (window_topn.slt:1580under a local harness that moves the rule).With this PR,
WindowTopNkeeps the plan as it was when its rewrite breaks a requirement that held, as every rule afterEnsureRequirementsmust.This covers positions up to the point where
OutputRequirementsremoves its marker. After that, nothing in the plan declares theORDER BY, so aWindowTopNplaced there still returns rows out of order, as on main.What changes are included in this PR?
WindowTopN::optimizeinwindow_topn.rs: if the plan's order and distribution requirements held before the rewrite and don't after it, the rule returns the plan unchanged. In the default orderWindowTopNruns beforeEnsureRequirements, when the window's sort isn't there yet, so its rewrites stay as they are.The decision I'd most like reviewed: the check is per plan, not per window, so one rewrite that breaks a requirement drops every
WindowTopNrewrite in that plan. I chose it because the operator that relies on the order can be several nodes above the window (inwindow_topn.slt:1580, a merge above a projection and a repartition), which a check at the window can't see. The two plan walks only run when the rule rewrote something.What is the testing strategy for this PR?
window_topn_after_enforcement_keeps_order_relied_onincore/tests/physical_optimizer/window_topn.rs: a session withWindowTopNafterEnsureRequirementsruns the query above and returnspk1, 2, NULL. On main it returns the NULL row first.WindowTopNafterEnsureRequirements1 → 0SanityCheckPlanerrors.Are there any user-facing changes?
No public API change, and no plan change in the default rule order.
For rule lists that run
WindowTopNafterEnsureRequirementsand beforeOutputRequirementsremoves its marker: a plan where the rewrite would break an order keeps its window instead of usingPartitionedTopKExec, and returns rows inORDER BYorder.