Skip to content

fix: apply a pulled up HAVING in scalar subqueries whose aggregate is NULL for empty input - #26608

Closed
abp002 wants to merge 1 commit into
apache:mainfrom
abp002:scalar-subquery-having-null-agg
Closed

abp002 wants to merge 1 commit into
apache:mainfrom
abp002:scalar-subquery-having-null-agg

Conversation

@abp002

@abp002 abp002 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

A correlated scalar subquery without GROUP BY ignores its HAVING when the aggregate is NULL for an empty input (sum, max, min) and the HAVING is true for an empty input:

SELECT x.a,
       (SELECT sum(y.b) FROM (VALUES (1, 10)) AS y(a, b) WHERE y.a = x.a HAVING sum(y.b) IS NULL) AS v
FROM (VALUES (1), (2)) AS x(a)

For a = 1 this returns 10 instead of NULL.

When the correlated filter is pulled up, such a HAVING is removed from the subquery and applied again in the count bug CASE that build_join adds over the join. But build_join skips that CASE for aggregates that are NULL for an empty input, so the HAVING was never applied.

What changes are included in this PR?

build_join only skips the CASE for those aggregates when there is no pulled up HAVING.

The HAVING reads the aggregates by their unqualified names, so it is now qualified with the subquery alias. Otherwise two such subqueries in the same query, or an outer column with the same name, fail with an ambiguous reference.

What is the testing strategy for this PR?

New sqllogictest cases in subquery.slt with sum, max, HAVING count(*) = 0, a HAVING that keeps some matched rows and two subqueries with the same aggregate. The expected results match SQLite.

Are there any user-facing changes?

No, only the wrong results are fixed.

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

Thank you for your contribution; Sorry I don't think this is the right approach

As I mentioned elsewhere (sorry for the repeated content) I don't think we should be trying to patch these subquery cases to make them work; Instead I think we should make the query error otherwise i worry we just open ourselves up to more wrong results bugs. To make it work and have confidence it will be correct, I think we need a more general approach than patches. See here for details

@alamb
alamb marked this pull request as draft October 28, 2026 20:04
@abp002

abp002 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @alamb, understood. Closing this; #26505 is already on your wrong-results list in #5492, so it can be handled there, either by the new framework or by making the query error.

@abp002 abp002 closed this Oct 30, 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.

Correlated scalar subquery ignores HAVING that is true for empty input

2 participants