Skip to content

[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
apache:masterfrom
MrHappyEnding:SPARK-58429
Open

[SPARK-58429][SQL] Uncorrelated IN-subquery selected with a global aggregate returns false/NULL instead of true when the input is empty#57817
MrHappyEnding wants to merge 1 commit into
apache:masterfrom
MrHappyEnding:SPARK-58429

Conversation

@MrHappyEnding

Copy link
Copy Markdown

What changes were proposed in this pull request?

This PR rewrites an uncorrelated IN subquery in the result expressions of a global aggregate above the Aggregate.

This avoids wrapping the introduced exists attribute in First, 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, RewritePredicateSubquery places the ExistenceJoin below the aggregate and wraps its result in First(exists).

For example:

SELECT count(*), 1 IN (SELECT id FROM range(1, 2))
FROM range(0);

Previously, this returned:

0, false

The expected result is:

0, true

The IN predicate 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 IN subqueries 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:

  • the ExistenceJoin is placed above the global aggregate;
  • First(exists) is not introduced for this case;
  • subqueries inside aggregate functions remain below the aggregate;
  • true and false IN results over empty input;
  • empty subquery behavior;
  • existing behavior over non-empty input.

RewriteSubquerySuite passed 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants