fix: skip WindowTopN rewrite when fetch is 0 to avoid PartitionedTopK panic - #24405
Merged
Conversation
… panic A predicate like `rn < 1` (or the flipped `1 > rn`) makes the WindowTopN rule compute a fetch of 0. Since ROW_NUMBER/RANK are always >= 1, no row can satisfy such a predicate and the correct result is empty. The rule passed fetch = 0 to `PartitionedTopKExec::try_new`, whose `assert!(k > 0)` panicked. Bail out of the rewrite when the computed limit is 0 and let the regular FilterExec produce the (empty) result instead. Closes apache#24404 Co-authored-by: Claude Code
viirya
force-pushed
the
fix-window-topn-k0-panic
branch
from
August 16, 2026 00:04
72de4dc to
0ac4c9c
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #24405 +/- ##
==========================================
- Coverage 81.18% 81.18% -0.01%
==========================================
Files 1110 1110
Lines 388906 388909 +3
Branches 388906 388909 +3
==========================================
Hits 315733 315733
- Misses 54576 54578 +2
- Partials 18597 18598 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jayzhan211
approved these changes
Aug 16, 2026
Member
Author
|
Thanks @jayzhan211 for review! |
imtherealnaska
pushed a commit
to imtherealnaska/datafusion
that referenced
this pull request
Aug 16, 2026
… 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.
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?
Rationale for this change
With
datafusion.optimizer.enable_window_topn = true, a query filtering a partitionedROW_NUMBER()/RANK()withrn < 1(or the flipped1 > rn) panics withPartitionedTopK requires k > 0.ROW_NUMBER/RANKare always>= 1, so such a predicate matches no rows and the correct result is empty — it must not panic. TheWindowTopNrule'sextract_window_limitmapsrn < Kto a fetch ofK - 1; forK = 1that fetch is0, which is passed toPartitionedTopKExec::try_new, whoseassert!(k > 0)panics. With the optimization disabled the same query correctly returns no rows.What changes are included in this PR?
WindowTopN::try_transform, bail out of the rewrite when the computed limit is0, letting the regularFilterExecproduce the (empty) result.window_topn.sltforrn < 1,1 > rn, andrn <= 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_topntests continue to pass.Are there any user-facing changes?
No API changes.
rn < 1/1 > rnnow returns an empty result instead of panicking whenenable_window_topnis on, matching the behavior when it is off.