Repository navigation
Conversation
… NULL for empty input
abp002
marked this pull request as ready for review
October 24, 2026 20:55
alamb
requested changes
Oct 28, 2026
alamb
left a comment
Contributor
There was a problem hiding this comment.
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
marked this pull request as draft
October 28, 2026 20:04
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
HAVINGthat is true for empty input #26505.Rationale for this change
A correlated scalar subquery without
GROUP BYignores itsHAVINGwhen the aggregate is NULL for an empty input (sum,max,min) and theHAVINGis true for an empty input:For
a = 1this returns10instead ofNULL.When the correlated filter is pulled up, such a
HAVINGis removed from the subquery and applied again in the count bugCASEthatbuild_joinadds over the join. Butbuild_joinskips thatCASEfor aggregates that are NULL for an empty input, so theHAVINGwas never applied.What changes are included in this PR?
build_joinonly skips theCASEfor those aggregates when there is no pulled upHAVING.The
HAVINGreads 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
sqllogictestcases insubquery.sltwithsum,max,HAVING count(*) = 0, aHAVINGthat 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.