Skip to content

Fix incorrect SortExec removal before AggregateExec (option 2)#20247

Merged
akurmustafa merged 3 commits intoapache:mainfrom
alamb:alamb/fix_monotonic_sort_bug_option2
Feb 16, 2026
Merged

Fix incorrect SortExec removal before AggregateExec (option 2)#20247
akurmustafa merged 3 commits intoapache:mainfrom
alamb:alamb/fix_monotonic_sort_bug_option2

Conversation

@alamb
Copy link
Contributor

@alamb alamb commented Feb 9, 2026

Which issue does this PR close?

This is an alternatative to

Rationale for this change

Wrong answers bug was exposed by #19287 in 52. See #20244 and backstory here

What changes are included in this PR?

Fix the bug by properly implemnting

Are these changes tested?

Yes, a new test is added

Are there any user-facing changes?

A bug is fixed

@github-actions github-actions bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Feb 9, 2026
@alamb alamb changed the title Fix group by sorting bug (option 2) Fix incorrect SortExec removal before AggregateExec (option 2) Feb 10, 2026
}
}
} else if let Some(aggregate_exec) = plan.as_any().downcast_ref::<AggregateExec>() {
handle_aggregate_pushdown(aggregate_exec, parent_required)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment explains why this is needed: #19287 (comment)

Basically the generic version is not correct for AggregateExec, but this was masked due to some limitations that were lifted in #19287

@alamb alamb marked this pull request as ready for review February 10, 2026 20:52
@alamb
Copy link
Contributor Author

alamb commented Feb 12, 2026

@ozankabak @mustafasrepo / @akurmustafa or @berkaysynnada I wonder if you might have time to review this PR (it is a bug fix for a long existing bug)

Copy link
Contributor

@akurmustafa akurmustafa left a comment

Choose a reason for hiding this comment

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

Thank you, @alamb for this PR. I added a new test case and resolved the merge conflicts. This PR looks good to me.

EXPLAIN SELECT x, SUM(v)
FROM agg_expr_parquet
GROUP BY x
ORDER BY x + 1 DESC;
Copy link
Contributor

Choose a reason for hiding this comment

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

It is not important but, maybe we can change this test such that desired ordering is ORDER BY x + 1 ASC. Because input ordering is already x ASC and in theory this version can be eliminated, not the current version in test as far as I can tell.

Copy link
Contributor

Choose a reason for hiding this comment

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

No longer applicable, I added a new testcase with ORDER BY 2*x ASC.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you -- sorry for the delay -- I was out last week

Copy link
Contributor

@akurmustafa akurmustafa left a comment

Choose a reason for hiding this comment

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

I added an additional testcase and resolved the merge conflict. Thank you @alamb for this PR.

2 150
1 60

# Test 3.12: Aggregate ORDER BY non-column expression (unsatisfied) keeps SortExec
Copy link
Contributor

Choose a reason for hiding this comment

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

I added this test case to showcase in theory final SortExec can be eliminated however currently cannot. I tried to use ORDER BY x +1 ASC. However, in that case there was no SortExec at the end. Hence I added this example.

@akurmustafa akurmustafa added this pull request to the merge queue Feb 16, 2026
Merged via the queue into apache:main with commit 132b043 Feb 16, 2026
32 checks passed
kosiew added a commit to kosiew/datafusion that referenced this pull request Feb 23, 2026
kosiew added a commit to kosiew/datafusion that referenced this pull request Feb 23, 2026
@alamb
Copy link
Contributor Author

alamb commented Feb 23, 2026

Thank you very much @akurmustafa for your help with this issue

@alamb alamb deleted the alamb/fix_monotonic_sort_bug_option2 branch February 23, 2026 19:52
alamb added a commit to alamb/datafusion that referenced this pull request Feb 23, 2026
…pache#20247)

- Fixes apache#20244

This is an alternatative to
- apache#20245

Wrong answers bug was exposed by
apache#19287 in 52. See
apache#20244 and backstory here
- apache#19287 (comment)

Fix the bug by properly implemnting

Yes, a new test is added

A bug is fixed

---------

Co-authored-by: Mustafa Akur <[email protected]>
alamb added a commit to alamb/datafusion that referenced this pull request Feb 23, 2026
…pache#20247)

- Fixes apache#20244

This is an alternatative to
- apache#20245

Wrong answers bug was exposed by
apache#19287 in 52. See
apache#20244 and backstory here
- apache#19287 (comment)

Fix the bug by properly implemnting

Yes, a new test is added

A bug is fixed

---------

Co-authored-by: Mustafa Akur <[email protected]>
@alamb
Copy link
Contributor Author

alamb commented Feb 23, 2026

alamb added a commit that referenced this pull request Feb 25, 2026
…20247) (#20507)

## Which issue does this PR close?
- part of #20287
- Fixes #20287 on branch-52
## Rationale for this change
See issues

## What changes are included in this PR?

- backports #20247

## Are these changes tested?

By CI
## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

---------

Co-authored-by: Mustafa Akur <[email protected]>
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.

Incorrect answers due to an incorrectly removed Sort before AggregateExec

3 participants