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

   ### What changes were proposed in this pull request?
   
   `RewriteCorrelatedScalarSubquery.tryEvalExpr` now keeps the expression when 
evaluating it fails, the same as it already does for an expression that is not 
foldable (SPARK-28441). The expression is then evaluated at runtime, and only 
for outer rows without matching rows.
   
   The `Filter` case of `evalSubqueryOnZeroTups` (a `HAVING` clause) now checks 
whether the bound condition was evaluated to a `Literal`, instead of checking 
`foldable` and calling `eval()` again, so a condition that failed to evaluate 
is also evaluated at runtime.
   
   ### Why are the changes needed?
   
   With ANSI mode on (the default), a correlated scalar 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...)
   ```
   
   With `spark.sql.ansi.enabled=false`, the same query returns the expected 
rows.
   
   To handle the COUNT bug, `evalSubqueryOnZeroTups` (and 
`DecorrelateInnerQuery`, through `evalAggregateOnZeroTups`) computes the result 
of the subquery on zero rows while the query is optimized, and puts it into 
`If(IsNull(alwaysTrue), <result on zero rows>, <subquery result>)`, which uses 
it only for outer rows without matching rows. `tryEvalExpr` called `eval()` on 
every foldable expression without catching its errors, so an error that can 
only happen for such an outer row failed the whole query, even when no such row 
exists.
   
   `ConstantFolding` keeps an expression that fails to evaluate in a 
conditional branch for the same reason (SPARK-39106). The result on zero rows 
is always used in such a branch: `If` in CASE 2 and `CaseWhen` in CASE 3 of 
`constructLeftJoins`, and `If` in `DecorrelateInnerQuery`.
   
   The same happens with other errors and in the other places that use the 
result on zero rows:
   
   | Query, every outer row has matching rows | Before | After |
   |---|---|---|
   | `SELECT k, (SELECT 10 / COALESCE(MAX(v), 0) FROM i WHERE i.k = o.k) FROM 
o` | `DIVIDE_BY_ZERO` | `(1, 0.05), (2, 0.0333...)` |
   | `SELECT k, (SELECT CAST(COALESCE(MAX(CAST(v AS STRING)), 'x') AS INT) FROM 
i WHERE i.k = o.k) FROM o` | `CAST_INVALID_INPUT` | `(1, 200), (2, 300)` |
   | `SELECT k FROM o WHERE (SELECT 10 / COALESCE(MAX(v), 0) FROM i WHERE i.k = 
o.k) > 0.04` | `DIVIDE_BY_ZERO` | `(1)` |
   | `SELECT k, (SELECT COUNT(*) FROM i WHERE i.k = o.k HAVING 10 / COUNT(*) > 
0) FROM o` | `DIVIDE_BY_ZERO` | `(1, 2), (2, 1)` |
   | `SELECT k, (SELECT MAX(c) FROM (SELECT 10 / COUNT(*) c FROM i WHERE i.k = 
o.k)) FROM o` (`DecorrelateInnerQuery`) | `DIVIDE_BY_ZERO` | `(1, 5.0), (2, 
10.0)` |
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A correlated scalar subquery whose result on zero rows fails to 
evaluate, e.g. with `DIVIDE_BY_ZERO` or `CAST_INVALID_INPUT` in ANSI mode, no 
longer fails the query while it is optimized. The query returns its rows when 
every outer row has matching rows, and fails with the same error at runtime 
when an outer row without matching rows exists. Results are unchanged when the 
evaluation succeeds or ANSI mode is off.
   
   The code is the same in branch-3.5 and later. ANSI mode is on by default 
since 4.0.
   
   ### How was this patch tested?
   
   - New test `SPARK-61444: COUNT bug with a result on zero rows that fails to 
evaluate` in `SubquerySuite`, with ANSI mode on:
     - The five queries above, where every outer row has matching rows, return 
their rows. They cover the top `Aggregate` in `constructLeftJoins` (in `SELECT` 
and `WHERE`), a `HAVING` condition, a `CAST` error and a lower `Aggregate` in 
`DecorrelateInnerQuery`.
     - With an outer row without matching rows, the `DIVIDE_BY_ZERO` queries 
still fail with `DIVIDE_BY_ZERO`.
     - Without the change, the test fails with `DIVIDE_BY_ZERO`.
   - `build/sbt 'catalyst/testOnly *RewriteSubquerySuite 
*DecorrelateInnerQuerySuite *PullupCorrelatedPredicatesSuite'` (55 tests), 
`build/sbt 'sql/testOnly org.apache.spark.sql.SubquerySuite'` (121 tests), and 
`build/sbt 'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z 
subquery'` (86 tests, no golden file changes) pass.
   - `catalyst/scalastyle` and `sql/Test/scalastyle` pass.
   
   ### 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