Skip to content

fix: stop aggregate_statistics walk at the first aggregate below a Final - #27307

Open
CuteChuanChuan wants to merge 2 commits into
apache:mainfrom
CuteChuanChuan:issue-27220/aggregate-statistics-filter
Open

CuteChuanChuan wants to merge 2 commits into
apache:mainfrom
CuteChuanChuan:issue-27220/aggregate-statistics-filter

Conversation

@CuteChuanChuan

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

As stated in #27220, count(*) FILTER (WHERE ...) over an aggregate subquery fails to plan with an internal error from aggregate_statistics.

What changes are included in this PR?

As pointed out in the issue, take_optimizable now stops at the first AggregateExec below a Final and uses it only if it is usable (raw input, no GROUP BY, no filter).

What is the testing strategy for this PR?

Added tests provided in issue into aggregate.slt.

Are there any user-facing changes?

No.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Nov 10, 2026

@HairstonE HairstonE 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.

@CuteChuanChuan Thanks for putting this in.
I reviewed the updated conditional and the test cases, ran the tests with and without the change, had claude conduct its own review, and ran the CI test jobs locally on the PR.

This looks good apart from one missing test. There should be a test that covers

SELECT count(1) FILTER (WHERE c > 0) FROM (SELECT count(1) AS c FROM t);

On main right now it incorrectly returns 5 instead of 1, with no error. This change fixes that, but the tests should reflect it. Something like this in the new block:

query I
SELECT count(1) FILTER (WHERE c > 0) FROM (SELECT count(1) AS c FROM t);
----
1

@CuteChuanChuan

Copy link
Copy Markdown
Contributor Author

@CuteChuanChuan Thanks for putting this in. I reviewed the updated conditional and the test cases, ran the tests with and without the change, had claude conduct its own review, and ran the CI test jobs locally on the PR.

This looks good apart from one missing test. There should be a test that covers

SELECT count(1) FILTER (WHERE c > 0) FROM (SELECT count(1) AS c FROM t);

On main right now it incorrectly returns 5 instead of 1, with no error. This change fixes that, but the tests should reflect it. Something like this in the new block:

query I
SELECT count(1) FILTER (WHERE c > 0) FROM (SELECT count(1) AS c FROM t);
----
1

@HairstonE Thanks for pointing this out! Good catch!
Added the count(1) case in 8ac0d1e. It returns 5 on main with no error and 1 with this change.

@CuteChuanChuan

Copy link
Copy Markdown
Contributor Author

@jayzhan211 could you PTAL when you have a chance? Thanks 🙏!

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.

count(*) FILTER (WHERE ...) over an aggregate subquery fails with an internal error from aggregate_statistics

2 participants