[SPARK-58429][SQL] Uncorrelated IN-subquery selected with a global aggregate returns false/NULL instead of true when the input is empty - #57817
Open
MrHappyEnding wants to merge 1 commit into
Conversation
uros-b
approved these changes
Aug 6, 2026
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.
What changes were proposed in this pull request?
This PR rewrites an uncorrelated
INsubquery in the result expressions of a global aggregate above theAggregate.This avoids wrapping the introduced
existsattribute inFirst, which does not preserve the subquery result when the aggregate input is empty.The rewrite only inspects
resultExpressions, so subqueries inside aggregate functions remain below the aggregate and are not repeatedly rewritten.Why are the changes needed?
A global aggregate produces one output row even when its input is empty. Currently,
RewritePredicateSubqueryplaces theExistenceJoinbelow the aggregate and wraps its result inFirst(exists).For example:
Previously, this returned:
The expected result is:
The
INpredicate is uncorrelated and should be evaluated on the single row produced by the global aggregate.Does this PR introduce any user-facing change?
Yes. Uncorrelated
INsubqueries projected alongside a global aggregate over empty input now return the correct result. There is no public API change.How was this patch tested?
Added regression coverage that verifies:
ExistenceJoinis placed above the global aggregate;First(exists)is not introduced for this case;INresults over empty input;RewriteSubquerySuitepassed with 8 tests. The modified source and test files compile successfully, and Scalastyle reports no errors or warnings.Was this patch authored or co-authored using generative AI tooling?
co-authored by Codex (GPT-5)