Repository navigation
Commit b551ba0
authored
fix: skip WindowTopN rewrite when fetch is 0 to avoid PartitionedTopK panic (apache#24405)
## Which issue does this PR close?
- Closes apache#24404.
## Rationale for this change
With `datafusion.optimizer.enable_window_topn = true`, a query filtering
a partitioned `ROW_NUMBER()`/`RANK()` with `rn < 1` (or the flipped `1 >
rn`) panics with `PartitionedTopK requires k > 0`.
`ROW_NUMBER`/`RANK` are always `>= 1`, so such a predicate matches no
rows and the correct result is empty — it must not panic. The
`WindowTopN` rule's `extract_window_limit` maps `rn < K` to a fetch of
`K - 1`; for `K = 1` that fetch is `0`, which is passed to
`PartitionedTopKExec::try_new`, whose `assert!(k > 0)` panics. With the
optimization disabled the same query correctly returns no rows.
## What changes are included in this PR?
- In `WindowTopN::try_transform`, bail out of the rewrite when the
computed limit is `0`, letting the regular `FilterExec` produce the
(empty) result.
- Add regression cases to `window_topn.slt` for `rn < 1`, `1 > rn`, and
`rn <= 0` (all must return empty without panicking).
## Are these changes tested?
Yes — the new sqllogictest cases fail (panic) without the fix and pass
with it. Existing `window_topn` tests continue to pass.
## Are there any user-facing changes?
No API changes. `rn < 1` / `1 > rn` now returns an empty result instead
of panicking when `enable_window_topn` is on, matching the behavior when
it is off.1 parent c1b39bd commit b551ba0
2 files changed
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
144 | 153 | | |
145 | 154 | | |
146 | 155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
87 | 110 | | |
88 | 111 | | |
89 | 112 | | |
| |||
0 commit comments