sarutak commented on code in PR #60325:
URL: https://github.com/apache/spark/pull/60325#discussion_r4566632060


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -724,14 +726,27 @@ object RewriteCorrelatedScalarSubquery extends 
Rule[LogicalPlan] with AliasHelpe
       // Needs to evaluate them on query runtime.
       expr
     } else {
-      Literal.create(expr.eval(), expr.dataType)
+      try {
+        Literal.create(expr.freshCopyIfContainsStatefulExpression().eval(), 
expr.dataType)
+      } catch {
+        // SPARK-61444: The result on zero tuples is only used for the outer 
rows without
+        // matching rows, in a conditional branch such as 
If(IsNull(alwaysTrue), ...). If its
+        // evaluation fails, e.g. with DIVIDE_BY_ZERO in ANSI mode, keep it, 
so that it is
+        // evaluated later and the query fails only if the result on zero 
tuples is used.
+        // Like ConstantFolding in conditional branches (SPARK-39106), tag it 
so that
+        // ConstantFolding does not evaluate it again.
+        case NonFatal(_) =>
+          val trimmed = trimAliases(expr)
+          trimmed.setTagValue(ConstantFolding.FAILED_TO_EVALUATE, ())
+          trimmed

Review Comment:
   This one comes from the tag I suggested earlier, sorry. When `expr` has no 
attribute and no aggregate, e.g. a constant `10 / 0`, neither the `transform` 
in `evalAggExprOnZeroTups` nor `trimAliases` makes a copy, so the tag goes onto 
the node in the plan itself. `DecorrelateInnerQuery` then also keeps that node 
in the output of the `Aggregate` (through `a.newInstance()`), outside the 
conditional branch, where `ConstantFolding` now skips it too. With ANSI on:
   
   | Query | master | this PR | this PR with `trimAliases(expr).clone()` |
   |---|---|---|---|
   | `SELECT o2.k, t.c, t.d FROM o2, LATERAL (SELECT count(*) c, 10 / 0 d FROM 
i WHERE i.k = o2.k) t` | `DIVIDE_BY_ZERO` while the query is optimized | 
`DIVIDE_BY_ZERO` at runtime | `DIVIDE_BY_ZERO` while the query is optimized |
   | the same with an empty outer table | `DIVIDE_BY_ZERO` while the query is 
optimized | no rows, no error | `DIVIDE_BY_ZERO` while the query is optimized |
   
   In these, the failing expression is also a column of the subquery itself 
(`d`), not only part of the result on zero rows, and in the second one the 
query now succeeds where it failed before. Tagging a copy, e.g. `val trimmed = 
trimAliases(expr).clone()`, keeps the old behavior for both, and 
`RewriteSubquerySuite` (including the new test), `DecorrelateInnerQuerySuite`, 
`PullupCorrelatedPredicatesSuite` and `SubquerySuite` still pass with it. Could 
the test also have the second query, which should then fail with 
`DIVIDE_BY_ZERO` while it is optimized?
   



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