raviston commented on code in PR #60325:
URL: https://github.com/apache/spark/pull/60325#discussion_r4545327972
##########
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, added in b03e254c49b: `SPARK-61444: keep the result on zero rows of
an Aggregate that fails to evaluate` in `RewriteSubquerySuite` checks that
`evalAggregateOnZeroTups` maps the output of an `Aggregate` over `10 /
coalesce(max(v), 0)` to an expression with no `Alias` and with the tag in ANSI
mode, and to a `null` literal without it. It fails without the tag
(`result.containsTag[Unit](ConstantFolding.FAILED_TO_EVALUATE) was false`) and
without the `trimAliases` (`Some(... AS d#2) was not empty`). I left the fresh
copy out, as it doesn't change the result.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -801,21 +820,17 @@ object RewriteCorrelatedScalarSubquery extends
Rule[LogicalPlan] with AliasHelpe
if (bindings.isEmpty) {
bindings
} else {
- val bindCondition = bindingExpr(condition, bindings)
-
- if (!bindCondition.foldable) {
- // We can't evaluate the condition. Evaluate it in query runtime.
- bindings.map { case (id, expr) =>
- val newExpr = If(bindCondition, expr, Literal.create(null,
expr.dataType))
- (id, newExpr)
- }
- } else {
- // The bound condition can be evaluated.
- bindCondition.eval() match {
- // For filter condition, null is the same as false.
- case null | false => Map.empty
- case true => bindings
- }
+ bindingExpr(condition, bindings) match {
+ // The bound condition was evaluated. For filter condition, null
is the same as false.
+ case Literal(null, _) | Literal(false, BooleanType) => Map.empty
+ case Literal(true, BooleanType) => bindings
+ case bindCondition =>
+ // We can't evaluate the condition, because it is not foldable
or its evaluation
+ // failed. Evaluate it in query runtime.
Review Comment:
Changed to "Keep it in an `If`, see `tryEvalExpr`." in b03e254c49b.
##########
sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala:
##########
@@ -3401,4 +3402,116 @@ class SubquerySuite extends SharedSparkSession
Row(1, 2))
}
}
+
+ test("SPARK-61444: COUNT bug with a result on zero rows that fails to
evaluate") {
+ withTempView("o", "o2", "i") {
+ Seq(1, 2).toDF("k").createOrReplaceTempView("o")
+ Seq(1, 2, 3).toDF("k").createOrReplaceTempView("o2")
+ Seq((1, 100), (1, 200), (2, 300)).toDF("k",
"v").createOrReplaceTempView("i")
+ // The outer row k = 3 of o2 has no matching rows and uses the result on
zero rows. The query
+ // is still optimized and planned, and fails when it runs.
+ def checkFailsAtRuntime[T <: SparkThrowable : ClassTag](
+ query: String,
+ condition: String): Unit = {
+ val df = sql(query)
+ df.queryExecution.executedPlan
+ val e = intercept[T](df.collect())
+ assert(e.getCondition === condition, query)
Review Comment:
Done in b03e254c49b: the helper body is now in `withClue(query)`, and the
clue on the `assert` is removed.
--
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]