Skip to content

Commit 0ac4c9c

Browse files
committed
fix: skip WindowTopN rewrite when fetch is 0 to avoid PartitionedTopK 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
1 parent e7e037d commit 0ac4c9c

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)