raviston commented on code in PR #60325:
URL: https://github.com/apache/spark/pull/60325#discussion_r4572554578
##########
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:
Thanks for finding this. I reproduced both rows of your table, and fixed it
as you suggested in 3ce7341da66: it now tags `trimAliases(expr).clone()`, and
the comment says why.
New checks:
- `SubquerySuite`: your `LATERAL (SELECT count(*) c, 10 / 0 d ...)` query
over `o2` and over an empty outer table, which must both fail with
`DIVIDE_BY_ZERO` while the query is optimized (`executedPlan` throws). Without
the `clone()`, the `o2` one fails at runtime and the empty one returns no rows,
so the test fails. On master, both already fail while the query is optimized.
- `RewriteSubquerySuite`: the catalyst test also runs with a constant `10 /
0`, and checks that the expression in the `Aggregate` has no tag, only the
result does. Without the `clone()`, it fails with `Aggregate [(cast(10 as
double) / cast(0 as double)) AS d#4]`.
--
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]