Skip to content

enable_window_topn panics on WHERE rn < 1 (PartitionedTopK requires k > 0) #24404

Description

@viirya

Describe the bug

With datafusion.optimizer.enable_window_topn = true, a query whose filter is rn < 1 (or the flipped 1 > rn) over a partitioned ROW_NUMBER()/RANK() panics:

thread '...' panicked at datafusion/physical-plan/src/topk/mod.rs:1294:
PartitionedTopK requires k > 0

ROW_NUMBER()/RANK() are always >= 1, so rn < 1 matches no rows and the correct result is an empty relation — it should not panic. With the optimization disabled the same query correctly returns no rows.

To Reproduce

SET datafusion.optimizer.enable_window_topn = true;

SELECT * FROM (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY pk ORDER BY val) AS rn
  FROM t
) WHERE rn < 1;          -- panics; also `WHERE 1 > rn`

Root cause

The WindowTopN rule's extract_window_limit maps rn < K to a fetch of K - 1 (and the flipped K > rn likewise). For K = 1 the fetch is 0, which is passed to PartitionedTopKExec::try_new, whose assert!(k > 0) panics.

Expected behavior

rn < 1 / 1 > rn should return an empty result without panicking (matching the enable_window_topn = false behavior).

Additional context

The fix is to skip the rewrite when the computed fetch is 0 and let the regular FilterExec produce the empty result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions