Skip to content

fix: eliminate group by constant empty input#22132

Open
HairstonE wants to merge 3 commits into
apache:mainfrom
HairstonE:fix/11748-eliminate-group-by-constant-empty-input
Open

fix: eliminate group by constant empty input#22132
HairstonE wants to merge 3 commits into
apache:mainfrom
HairstonE:fix/11748-eliminate-group-by-constant-empty-input

Conversation

@HairstonE
Copy link
Copy Markdown

Which issue does this PR close?

Closes #11748

Rationale for this change

EliminateGroupByConstant strips constant expressions from GROUP BY. When it strips all of them, it converts a grouping aggregate into a global aggregate. A grouping aggregate returns 0 rows on empty input, a global aggregate returns 1. GROUP BY false HAVING false should return 0 rows. It was returning 1 row of NULL.

Found the root cause by reading PR #11897.

What changes are included in this PR?

The guard now bails out whenever all GROUP BY expressions are constants, regardless of whether aggregate functions exist. One line change, plus updated tests.

Are these changes tested?

Yes. Sqllogictest reproducer for the original issue, updated unit test and optimizer SLT expectations.

Are there any user-facing changes?

Queries with all-constant GROUP BY may now return fewer (correct) rows.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels May 12, 2026
@kumarUjjawal kumarUjjawal changed the title Fix/11748 eliminate group by constant empty input fix: eliminate group by constant empty input May 13, 2026
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.

We should also add this test SELECT AVG(v1) FROM t1 GROUP BY false;

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.

Also could you add a non-empty positive case alongside the empty-table reproducer?

Copy link
Copy Markdown
Author

@HairstonE HairstonE May 13, 2026

Choose a reason for hiding this comment

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

Yeah I'll add those now. Added those tests.

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 result returned by a group by query (SQLancer-TLP)

2 participants