Skip to content

Native Top-K fails when the output contains only sort keys #2463

Description

@lyne7-sc

Describe the bug

Native Top-K queries may fail when all output columns are also sort keys.

In this case, the sort keys are stored separately and the data batch contains no columns. The sorted indices are truncated by the Top-K limit, but the zero-column batch still keeps the original input row count.

This row-count mismatch can cause the merge cursor to read past the retained sort-key rows.

To Reproduce

Run a Top-K query whose output contains only the sort column:

spark
  .range(10)
  .repartition(1)
  .select("id")
  .orderBy("id")
  .limit(6)
  .collect()

The query fails in native SortExec with:

unsafe precondition(s) violated:
slice::get_unchecked requires that the index is within the slice

Expected behavior

The query should return the correct result.

Screenshots

Additional context

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