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

   ### What changes were proposed in this pull request?
   
   This is a backport of #60325 to `branch-4.2`. See #60325 for the full 
description.
   
   `RewriteCorrelatedScalarSubquery.tryEvalExpr` now keeps the result on zero 
rows of a correlated subquery as an expression when evaluating it fails, the 
same as for an expression that is not foldable, and tags it with 
`FAILED_TO_EVALUATE` so that `ConstantFolding` does not evaluate it again.
   
   The main code is the same as in `master`. The cherry-pick conflicts only in 
the tests: `master` has other new tests at the end of `SubquerySuite` and 
`RewriteSubquerySuite`, with their imports. So this PR adds only the tests of 
#60325, with the imports they need.
   
   ### Why are the changes needed?
   
   With ANSI mode on (the default since 4.0), a correlated subquery fails while 
the query is optimized when its result on zero rows throws, even if every outer 
row has matching rows:
   
   ```sql
   CREATE OR REPLACE TEMP VIEW o(k) AS VALUES (1), (2);
   CREATE OR REPLACE TEMP VIEW i(k, v) AS VALUES (1, 100), (1, 200), (2, 300);
   
   SELECT k, (SELECT 10 / COALESCE(MAX(v), 0) FROM i WHERE i.k = o.k) FROM o;
   -- [DIVIDE_BY_ZERO] Division by zero. Expected (1, 0.05), (2, 0.0333...)
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, the change of #60325, in 4.2.2. Such a query no longer fails while it 
is optimized. When the result on zero rows is used for an outer row without 
matching rows, the query still fails with the same error, at runtime.
   
   ### How was this patch tested?
   
   The tests of #60325. These pass on `branch-4.2` (JDK 17):
   - `build/sbt 'catalyst/testOnly *RewriteSubquerySuite 
*DecorrelateInnerQuerySuite *PullupCorrelatedPredicatesSuite *OptimizerSuite'` 
(55 tests)
   - `build/sbt 'sql/testOnly org.apache.spark.sql.SubquerySuite'` (103 tests)
   - `build/sbt 'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z 
subquery'` (86 tests, no golden file changes)
   - `catalyst/scalastyle`, `catalyst/Test/scalastyle`, `sql/scalastyle` and 
`sql/Test/scalastyle`
   
   Both new tests fail on `branch-4.2` with `DIVIDE_BY_ZERO` without the change 
in `subquery.scala` and `DecorrelateInnerQuery.scala`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.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