raviston commented on code in PR #60325:
URL: https://github.com/apache/spark/pull/60325#discussion_r4541832011
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -715,7 +716,7 @@ object RewriteCorrelatedScalarSubquery extends
Rule[LogicalPlan] with AliasHelpe
/**
* Checks if given expression is foldable. Evaluates it and returns it as
literal, if yes.
- * If not, returns the original expression without evaluation.
+ * If not, or if the evaluation fails, returns the original expression
without evaluation.
Review Comment:
Thanks, changed to "If not, returns the original expression. If the
evaluation fails, returns the original expression without its aliases." The
second part is from the `trimAliases` change in the next comment.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -724,7 +725,16 @@ 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.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, evaluate
it in query runtime
+ // instead, so that the query fails only if such an outer row exists.
ConstantFolding
+ // does the same in conditional branches (SPARK-39106).
+ case NonFatal(_) => expr
Review Comment:
Done, all three:
- The comment now says it keeps the expression "so that it is evaluated
later and the query fails only if the result on zero tuples is used".
- It evaluates `expr.freshCopyIfContainsStatefulExpression()` and tags the
expression that failed with `ConstantFolding.FAILED_TO_EVALUATE`, like
`tryFold`.
- It returns `trimAliases(expr)` when the evaluation fails. The non-foldable
path from SPARK-28441 still returns `expr`, as before.
With the tag, your `(SELECT * FROM i WHERE false)` query still fails with
`DIVIDE_BY_ZERO` while it is optimized, so the description says that.
##########
sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala:
##########
@@ -3401,4 +3401,52 @@ class SubquerySuite extends SharedSparkSession
Row(1, 2))
}
}
+
+ test("SPARK-61444: COUNT bug with a result on zero rows that fails to
evaluate") {
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+ 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")
+ // Every outer row has matching rows, so the result on zero rows is
not used, and the
+ // queries must not fail while the subquery is rewritten.
+ // The top Aggregate, handled in
RewriteCorrelatedScalarSubquery.constructLeftJoins.
+ checkAnswer(
+ sql("SELECT k, (SELECT 10 / coalesce(max(v), 0) FROM i WHERE i.k =
o.k) FROM o"),
+ Row(1, 0.05) :: Row(2, 10.0 / 300) :: Nil)
+ checkAnswer(
+ sql("SELECT k, (SELECT CAST(coalesce(max(CAST(v AS STRING)), 'x') AS
INT) " +
+ "FROM i WHERE i.k = o.k) FROM o"),
+ Row(1, 200) :: Row(2, 300) :: Nil)
+ checkAnswer(
+ sql("SELECT k FROM o WHERE (SELECT 10 / coalesce(max(v), 0) FROM i
WHERE i.k = o.k) " +
+ "> 0.04"),
+ Row(1) :: Nil)
+ // A HAVING clause whose condition fails to evaluate on zero rows.
+ checkAnswer(
+ sql("SELECT k, (SELECT count(*) FROM i WHERE i.k = o.k HAVING 10 /
count(*) > 0) " +
+ "FROM o"),
+ Row(1, 2) :: Row(2, 1) :: Nil)
Review Comment:
Thanks for checking that. The `HAVING` queries now also run with
`spark.sql.optimizer.decorrelateInnerQuery.enabled=false`: the `o` one returns
`(1, 2), (2, 1)`, and the `o2` one fails with `DIVIDE_BY_ZERO` at runtime. With
either of your two replacements, the test now fails with `Expected exception
org.apache.spark.SparkArithmeticException to be thrown, but no exception was
thrown`. The description now says that CASE 3 is reached with that conf.
##########
sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala:
##########
@@ -3401,4 +3401,52 @@ class SubquerySuite extends SharedSparkSession
Row(1, 2))
}
}
+
+ test("SPARK-61444: COUNT bug with a result on zero rows that fails to
evaluate") {
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+ 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")
+ // Every outer row has matching rows, so the result on zero rows is
not used, and the
+ // queries must not fail while the subquery is rewritten.
+ // The top Aggregate, handled in
RewriteCorrelatedScalarSubquery.constructLeftJoins.
+ checkAnswer(
+ sql("SELECT k, (SELECT 10 / coalesce(max(v), 0) FROM i WHERE i.k =
o.k) FROM o"),
+ Row(1, 0.05) :: Row(2, 10.0 / 300) :: Nil)
+ checkAnswer(
+ sql("SELECT k, (SELECT CAST(coalesce(max(CAST(v AS STRING)), 'x') AS
INT) " +
+ "FROM i WHERE i.k = o.k) FROM o"),
+ Row(1, 200) :: Row(2, 300) :: Nil)
+ checkAnswer(
+ sql("SELECT k FROM o WHERE (SELECT 10 / coalesce(max(v), 0) FROM i
WHERE i.k = o.k) " +
+ "> 0.04"),
+ Row(1) :: Nil)
+ // A HAVING clause whose condition fails to evaluate on zero rows.
+ checkAnswer(
+ sql("SELECT k, (SELECT count(*) FROM i WHERE i.k = o.k HAVING 10 /
count(*) > 0) " +
+ "FROM o"),
+ Row(1, 2) :: Row(2, 1) :: Nil)
+ // A lower Aggregate, handled in DecorrelateInnerQuery.
+ checkAnswer(
+ sql("SELECT k, (SELECT max(c) FROM (SELECT 10 / count(*) c FROM i
WHERE i.k = o.k)) " +
+ "FROM o"),
+ Row(1, 5.0) :: Row(2, 10.0) :: Nil)
+
+ // The outer row k = 3 of o2 has no matching rows and uses the result
on zero rows, so
+ // the queries still fail, at runtime.
+ Seq(
+ "SELECT k, (SELECT 10 / coalesce(max(v), 0) FROM i WHERE i.k = o2.k)
FROM o2",
+ "SELECT k, (SELECT count(*) FROM i WHERE i.k = o2.k HAVING 10 /
count(*) > 0) FROM o2",
+ "SELECT k, (SELECT max(c) FROM (SELECT 10 / count(*) c FROM i WHERE
i.k = o2.k)) " +
+ "FROM o2"
+ ).foreach { query =>
+ val e = intercept[SparkArithmeticException] {
+ sql(query).collect()
+ }
+ assert(e.getCondition === "DIVIDE_BY_ZERO")
+ }
Review Comment:
Done. A helper now calls `executedPlan` before `intercept`, and takes the
expected exception class and condition. The `o2` versions of the `WHERE` query
(`DIVIDE_BY_ZERO`) and the `CAST` query (`SparkNumberFormatException`,
`CAST_INVALID_INPUT`) are added, along with `LATERAL` and `IN` ones
(`DIVIDE_BY_ZERO`).
--
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]