Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions datafusion/physical-optimizer/src/window_topn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ impl WindowTopN {
// Step 2: Extract limit from predicate (rn <= K, rn < K, etc.)
let (col_idx, limit_n) = extract_window_limit(filter.predicate())?;

// A predicate such as `rn < 1` (or the flipped `1 > rn`) yields a fetch of
// 0. `ROW_NUMBER`/`RANK` are always >= 1, so no row can satisfy it and the
// correct result is empty. `PartitionedTopKExec` requires `k > 0` and would
// panic on `k = 0`, so bail out here and let the regular `FilterExec` produce
// the (empty) result instead of rewriting.
if limit_n == 0 {
return None;
}

// Step 3: Walk through optional ProjectionExec and RepartitionExec to find BoundedWindowAggExec
let child = filter.input();
let (window_exec, intermediates) = find_window_below(child)?;
Expand Down
23 changes: 23 additions & 0 deletions datafusion/sqllogictest/test_files/window_topn.slt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ SELECT id, pk, val FROM (
8 3 100
9 3 50

# Test 3b: rn < 1 has fetch = 0. ROW_NUMBER is always >= 1 so no row qualifies and
# the result must be empty. The rewrite must not build a PartitionedTopKExec with k = 0
# (which panics); it falls back to the regular filter. Regression for the k = 0 panic.
query III rowsort
SELECT id, pk, val FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY pk ORDER BY val) as rn FROM window_topn_t
) WHERE rn < 1;
----

# Test 3c: flipped form `1 > rn` (fetch = 0) — same empty result, no panic.
query III rowsort
SELECT id, pk, val FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY pk ORDER BY val) as rn FROM window_topn_t
) WHERE 1 > rn;
----

# Test 3d: rn <= 0 (fetch = 0) — empty, no panic.
query III rowsort
SELECT id, pk, val FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY pk ORDER BY val) as rn FROM window_topn_t
) WHERE rn <= 0;
----

# Test 4: Without PARTITION BY — should NOT optimize
query II rowsort
SELECT id, val FROM (
Expand Down