sarutak commented on code in PR #60325:
URL: https://github.com/apache/spark/pull/60325#discussion_r4542950051
##########
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:
Nit: "Evaluate it in query runtime." has the same issue as the comment in
`tryEvalExpr` had: when the evaluation failed and the subquery is known to be
empty, it is evaluated while the query is optimized. Something like "Keep it in
an `If`, see `tryEvalExpr`." would match.
##########
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:
Nit: none of the tests I ran depends on the tag, the `trimAliases` or the
fresh copy: with each of them removed separately, `SPARK-61444` and the other
`SubquerySuite` tests I ran still pass, since the results are the same. A small
catalyst test could pin the first two, e.g. that `evalAggregateOnZeroTups` on
an `Aggregate` over `10 / coalesce(max(v), 0)` with ANSI on maps the
aggregate's output to an expression with no `Alias` that has the tag.
##########
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:
Nit: when `intercept` fails, e.g. "Expected exception ... to be thrown, but
no exception was thrown", the message does not say which query it was. Wrapping
the body in `withClue(query) { ... }` would show it, and the clue on the
`assert` would no longer be needed.
--
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]