Skip to content

Commit b551ba0

Browse files
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

File tree

‎datafusion/physical-optimizer/src/window_topn.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ impl WindowTopN {
141141
// Step 2: Extract limit from predicate (rn <= K, rn < K, etc.)
142142
let (col_idx, limit_n) = extract_window_limit(filter.predicate())?;
143143

144+
// A predicate such as `rn < 1` (or the flipped `1 > rn`) yields a fetch of
145+
// 0. `ROW_NUMBER`/`RANK` are always >= 1, so no row can satisfy it and the
146+
// correct result is empty. `PartitionedTopKExec` requires `k > 0` and would
147+
// panic on `k = 0`, so bail out here and let the regular `FilterExec` produce
148+
// the (empty) result instead of rewriting.
149+
if limit_n == 0 {
150+
return None;
151+
}
152+
144153
// Step 3: Walk through optional ProjectionExec and RepartitionExec to find BoundedWindowAggExec
145154
let child = filter.input();
146155
let (window_exec, intermediates) = find_window_below(child)?;

‎datafusion/sqllogictest/test_files/window_topn.slt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ SELECT id, pk, val FROM (
8484
8 3 100
8585
9 3 50
8686

87+
# Test 3b: rn < 1 has fetch = 0. ROW_NUMBER is always >= 1 so no row qualifies and
88+
# the result must be empty. The rewrite must not build a PartitionedTopKExec with k = 0
89+
# (which panics); it falls back to the regular filter. Regression for the k = 0 panic.
90+
query III rowsort
91+
SELECT id, pk, val FROM (
92+
SELECT *, ROW_NUMBER() OVER (PARTITION BY pk ORDER BY val) as rn FROM window_topn_t
93+
) WHERE rn < 1;
94+
----
95+
96+
# Test 3c: flipped form `1 > rn` (fetch = 0) — same empty result, no panic.
97+
query III rowsort
98+
SELECT id, pk, val FROM (
99+
SELECT *, ROW_NUMBER() OVER (PARTITION BY pk ORDER BY val) as rn FROM window_topn_t
100+
) WHERE 1 > rn;
101+
----
102+
103+
# Test 3d: rn <= 0 (fetch = 0) — empty, no panic.
104+
query III rowsort
105+
SELECT id, pk, val FROM (
106+
SELECT *, ROW_NUMBER() OVER (PARTITION BY pk ORDER BY val) as rn FROM window_topn_t
107+
) WHERE rn <= 0;
108+
----
109+
87110
# Test 4: Without PARTITION BY — should NOT optimize
88111
query II rowsort
89112
SELECT id, val FROM (

0 commit comments

Comments
 (0)