jayzhan211 opened a new pull request, #27772:
URL: https://github.com/apache/datafusion/pull/27772
## Which issue does this PR close?
- Part of #26481.
## Rationale for this change
A rule list that runs `WindowTopN` after `EnsureRequirements` can return
rows in the wrong order. For
```sql
SELECT pk, rk, rn, id FROM (
SELECT pk, v, id,
rank() OVER (PARTITION BY pk ORDER BY v) AS rk,
row_number() OVER (PARTITION BY pk ORDER BY v) AS rn
FROM t
) WHERE rn <= 1 ORDER BY pk
```
`EnsureRequirements` sorts the window's input by `pk ASC NULLS LAST` and
drops the sort for `ORDER BY pk`, since the window's output already has that
order. `WindowTopN` then puts a `PartitionedTopKExec` under the window, which
emits `pk` with `NULLS FIRST`, so the row with a NULL `pk` comes first. When an
order-preserving repartition and a merge sit above the window, the same rewrite
fails planning instead (`window_topn.slt:1580` under a local harness that moves
the rule).
With this PR, `WindowTopN` keeps the plan as it was when its rewrite breaks
a requirement that held, as every rule after `EnsureRequirements` must.
## What changes are included in this PR?
- `WindowTopN::optimize` in `window_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 order `WindowTopN` runs before
`EnsureRequirements`, 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 `WindowTopN` rewrite in
that plan. I chose it because the operator that relies on the order can be
several nodes above the window (in `window_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_on` in
`core/tests/physical_optimizer/window_topn.rs`: a session with `WindowTopN`
after `EnsureRequirements` runs the query above and returns `pk` 1, 2, NULL. On
main it returns the NULL row first.
- The sqllogictest suite (with TPC-H) under a local harness, main 73af333429
vs this PR: default order 0 → 0 failures and no plan changes; `WindowTopN`
after `EnsureRequirements` 1 → 0 `SanityCheckPlan` errors.
## Are there any user-facing changes?
No public API change, and no plan change in the default rule order.
For rule lists that run `WindowTopN` after `EnsureRequirements`: a plan
where the rewrite would break an order keeps its window instead of using
`PartitionedTopKExec`, and returns rows in `ORDER BY` order.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]