Skip to content

fix: preserve sort column identity in TopK aggregation - #26712

Open
xgbtxy wants to merge 1 commit into
apache:mainfrom
xgbtxy:fix/topk-computed-projection
Open

xgbtxy wants to merge 1 commit into
apache:mainfrom
xgbtxy:fix/topk-computed-projection

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Oct 24, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Closes #26691.

Rationale for this change

A projection can change an aggregate value while keeping its output name. For example, ordering by -max(v) in descending order must retain the groups with the smallest original maxima. TopK aggregation currently follows the reused name and can discard those groups before the projection runs.

What changes are included in this PR?

Track the sort column through the corresponding projection output index. Continue through direct column references, including renamed or reordered outputs, and stop at a computed expression. Treat materialized CTEs as a boundary because their producer and output continuation can use different column positions.

The regression tests cover the reported MAX query, a computed projection over DISTINCT, a direct sort column with a computed sibling that should retain TopK aggregation, and an outer sort over a materialized CTE with reordered output columns.

What is the testing strategy for this PR?

The new tests fail with two result mismatches on the unchanged upstream source. With the production change applied, the same complete aggregates_topk.slt file passes. The positive EXPLAIN assertion retains AggregateExec lim=[2].

Focused validation: https://github.com/xgbtxy/contribution-checks/actions/runs/40091933143

On upstream base 92f170cb7ea5ad87bdff8fde89ac7a1541f3b0fa, formatting, all-target/all-feature Clippy, extended workspace tests, core/CLI tests, and the complete repository lint suite have passed.

Required checks: https://github.com/xgbtxy/contribution-checks/actions/runs/40091939116

Materialized CTE control: https://github.com/xgbtxy/contribution-checks/actions/runs/40091936040

Fork CI for the submitted commit: https://github.com/xgbtxy/datafusion/actions/runs/40095216860 (28 jobs passed). The fork coverage upload lacked a token; the native tests completed successfully.

Are there any user-facing changes?

Queries that order computed aggregate or DISTINCT outputs with reused column names return the correct groups. There are no public API changes.

Developed with AI assistance. The original five-row reproducer was reported by @jayzhan211 in #26691.

Copilot AI balanced review requested due to automatic review settings October 24, 2026 13:44

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TopK aggregation keeps the wrong groups when a projection computes a new column with the aggregate's name

2 participants