Skip to content

[AURON #2463] Fix native Top-K with only sort keys - #2464

Merged
slfan1989 merged 1 commit into
apache:masterfrom
lyne7-sc:fix/sort-only-id-topk
Aug 15, 2026
Merged

[AURON #2463] Fix native Top-K with only sort keys#2464
slfan1989 merged 1 commit into
apache:masterfrom
lyne7-sc:fix/sort-only-id-topk

Conversation

@lyne7-sc

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #2463

Rationale for this change

When all output columns are sort keys, the native sorter stores the sort keys separately and creates a zero-column data batch.

For Top-K queries, the sorted indices are limited before this batch is created, but the batch currently retains the original input row count. This mismatch can cause the merge cursor to read past the retained sort-key rows.

What changes are included in this PR?

  • Use the number of retained sorted indices as the row count of the zero-column batch.

Are there any user-facing changes?

Bug fix only.

How was this patch tested?

Added a regression test to AuronExecSuite.

Was this patch authored or co-authored using generative AI tooling?

  • Yes
  • No

If yes, include: Generated-by: GPT-5

ASF guidance: https://www.apache.org/legal/generative-tooling.html

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a native Top-K failure in SortExec when all output columns are also sort keys, by ensuring the zero-column RecordBatch created for the output uses the retained (post-limit) row count so downstream merge cursors don’t read past available sort-key rows.

Changes:

  • Adjust zero-column batch row count to sorted_indices.len() after Top-K limiting in the native external sorter.
  • Add a Spark regression test covering ORDER BY ... LIMIT when projecting only the sort column.
  • Add a DataFusion-native regression test for Top-K with a single (sort) column.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
spark-extension-shims-spark/src/test/scala/org/apache/auron/exec/AuronExecSuite.scala Adds a Spark-side regression test ensuring native TakeOrdered works when projecting only the sort column.
native-engine/datafusion-ext-plans/src/sort_exec.rs Fixes zero-column batch row count for Top-K and adds a native regression test for the scenario.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1574 to +1584
let expected = r#"+----+
| id |
+----+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+----+"#;
assert_batches_eq!(expected.lines().collect::<Vec<_>>(), &batches);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt compacts the suggested Vec<&str> form into a few long lines, making the expected table less readable. The raw string has no leading spaces, so I prefer to keep the current format.

Comment on lines 675 to 679
let sorted_batch = if !self.prune_sort_keys_from_batch.is_all_pruned() {
take_batch(batch, sorted_indices)?
} else {
create_zero_column_batch(batch.num_rows())
create_zero_column_batch(sorted_indices.len())
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The observation about over-allocation is correct, but this allocation behavior predates this pr and remains unchanged by it. Changing the reservation strategy is independent of this correctness fix and requires separate validation, so it is better suited to a followup pr.

@slfan1989 slfan1989 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. Using the retained sorted-index count keeps the zero-column batch aligned with the separately stored sort keys, and the Rust and Spark regression tests cover the failing Top-K path well. LGTM.

@slfan1989
slfan1989 merged commit 38f6396 into apache:master Aug 15, 2026
123 checks passed
@slfan1989

Copy link
Copy Markdown
Contributor

@lyne7-sc Thanks for the contribution! Merged into master.

@lyne7-sc

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews! @slfan1989 @SteNicholas

@lyne7-sc
lyne7-sc deleted the fix/sort-only-id-topk branch August 15, 2026 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants