MrHappyEnding opened a new pull request, #57817:
URL: https://github.com/apache/spark/pull/57817

   ### 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:
   
   ```sql
   SELECT count(*), 1 IN (SELECT id FROM range(1, 2))
   FROM range(0);
   ```
   
   Previously, this returned:
   
   ```text
   0, false
   ```
   
   The expected result is:
   
   ```text
   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)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to