raviston commented on PR #60296:
URL: https://github.com/apache/spark/pull/60296#issuecomment-6787609011

   Thanks for the thorough fix. I ran this PR locally and found a regression: 
with ANSI mode on (the default), some subqueries that work on master now fail 
during optimization with `DIVIDE_BY_ZERO`, even though no row divides by zero 
at runtime.
   
   ```sql
   CREATE OR REPLACE TEMP VIEW t1(a1, a2) AS VALUES (1, 10), (2, 20);
   CREATE OR REPLACE TEMP VIEW t2(b1, b2) AS VALUES (1, 100), (2, 200);
   
   -- Q2
   SELECT a1, (SELECT COUNT(*) FROM (SELECT 10 / COALESCE(MAX(b2), 0) d FROM t2 
WHERE t2.b1 = t1.a1)) x
   FROM t1 ORDER BY a1;
   -- Q3
   SELECT a1, (SELECT SUM(d) FROM (SELECT 10 / COALESCE(MAX(b2), 0) d FROM t2 
WHERE t2.b1 = t1.a1)) x
   FROM t1 ORDER BY a1;
   ```
   
   Every row of `t1` has a match, so the division is never by zero:
   
   | | master (e540d564a21) | this PR (e77a0ba7ae1, catalyst classes on the 
same build) |
   |---|---|---|
   | Q2 | `(1, 1), (2, 1)` | `DIVIDE_BY_ZERO` |
   | Q3 | `(1, 0.1), (2, 0.05)` | `DIVIDE_BY_ZERO` |
   | Q3, `scalarSubqueryNestedAggregateCountBugBehavior=true` | | `(1, 0.1), 
(2, 0.05)` |
   | Q3, `spark.sql.ansi.enabled=false` | | `(1, 0.1), (2, 0.05)` |
   
   The exception comes from `RewriteCorrelatedScalarSubquery.tryEvalExpr` <- 
`evalAggExprOnZeroTups` <- `evalAggregateOnZeroTups` <- 
`evalSubqueryOnZeroTups` <- `constructLeftJoins`.
   
   I think the cause is that `collapseAggregatesOverOneRow` turns the top 
`Aggregate` into a `Project`, so `splitSubquery` now returns the lower 
`Aggregate` as the aggregate node, and `evalSubqueryOnZeroTups` folds `10 / 
COALESCE(NULL, 0)` at planning time. On master, the aggregate node is the top 
`COUNT`/`SUM`, and the lower aggregate's expressions are never folded.
   
   The underlying problem already exists on master for the same expression 
directly in the subquery:
   
   ```sql
   SELECT a1, (SELECT 10 / COALESCE(MAX(b2), 0) FROM t2 WHERE t2.b1 = t1.a1) x 
FROM t1;
   -- DIVIDE_BY_ZERO on master too
   ```
   
   But this PR makes more shapes go through that path, so queries that 
currently succeed start failing. The same should apply to anything that throws 
when folded on NULL input under ANSI, e.g. a failing `CAST` or `raise_error`.
   
   One way to fix both would be for `tryEvalExpr` to keep the expression when 
evaluating it throws, the same as it already does for non-foldable expressions 
(SPARK-28441):
   
   ```scala
   } else {
     try {
       Literal.create(expr.eval(), expr.dataType)
     } catch {
       case NonFatal(_) => expr
     }
   }
   ```
   
   Then the expression is only evaluated at runtime, in the 
`If(IsNull(alwaysTrue), ...)` branch, for outer rows without a match, which is 
where the error belongs. If you'd rather keep this PR focused, an alternative 
would be to not collapse when the lower `Aggregate` would become the aggregate 
node and its expressions can't be folded safely, and to fix master's case 
separately. Either way, Q2 and Q3 would be good additions to 
`nested-scalar-subquery-count-bug.sql`.
   


-- 
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