MaxGekk commented on code in PR #59985:
URL: https://github.com/apache/spark/pull/59985#discussion_r4507921031
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -173,8 +174,59 @@ trait GeneratePredicateHelper extends PredicateHelper {
* Whether evaluating `e` on a row can be observed other than through its
value: it can raise
* an error, is nondeterministic or may have side effects, such as a UDF.
Such an expression
* has to be evaluated on the rows, and in the order, that `And.eval`
evaluates it on.
+ *
+ * On top of `ExprUtils.canEvaluateUnconditionally`, a total expression over
such expressions
+ * is known not to raise: the string functions of `isTotal`, a hash, the
Bloom filter predicate
+ * of a runtime filter, and the arithmetic of numbers in the legacy
evaluation mode, which
+ * returns NULL instead of raising. A column that an operator of the same
stage computes
+ * lazily, such as a projection alias or a result of an aggregate, is judged
by the expression
+ * that computes it, see `producedBy`, since a check on the column evaluates
that expression.
+ */
+ protected def mayRaise(e: Expression): Boolean = {
+ val resolved = e.transform { case a: Attribute => producedBy(a) }
Review Comment:
Fixed in 94228e373fa. The NULL-conjunct decisions keep master's
classification by the conjunct's own expression, and the config does not change
them. Your query is a test and returns no row, with the config on and off.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -474,7 +540,9 @@ case class FilterExec(condition: Expression, child:
SparkPlan)
// got synthetic coverage) emit after all otherPreds. With the
NULL-conjunct
// flag, once an otherPred that can be NULL is emitted, the
notNullPreds that
// precede a later otherPred in the condition are emitted before it
first, when it
- // or one of them can raise (`precedingNotNullChecks`), and a complex
+ // or one of them can raise; and with or without the flag, the ones
written in the
+ // query that cannot raise, inferred ones excluded, are emitted
before an otherPred
+ // that can raise (`precedingNotNullChecks`, SPARK-60329), and a
complex
Review Comment:
Done in 94228e373fa. The SPARK-60329 clause is a sentence of its own after
the NULL-conjunct one.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -173,8 +174,59 @@ trait GeneratePredicateHelper extends PredicateHelper {
* Whether evaluating `e` on a row can be observed other than through its
value: it can raise
* an error, is nondeterministic or may have side effects, such as a UDF.
Such an expression
* has to be evaluated on the rows, and in the order, that `And.eval`
evaluates it on.
+ *
+ * On top of `ExprUtils.canEvaluateUnconditionally`, a total expression over
such expressions
+ * is known not to raise: the string functions of `isTotal`, a hash, the
Bloom filter predicate
+ * of a runtime filter, and the arithmetic of numbers in the legacy
evaluation mode, which
+ * returns NULL instead of raising. A column that an operator of the same
stage computes
Review Comment:
The list is gone in 94228e373fa. `mayRaise` is
`ExprUtils.canEvaluateUnconditionally`, which on master now covers
`StartsWith`, `EndsWith`, `Contains`, the casts that cannot fail and the date
fields, with the reasons in its Scaladoc, and does not accept `Concat`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1808,6 +1808,21 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val LEGACY_FILTER_LAZY_NOT_NULL_CHECK_PLACEMENT =
+ buildConf("spark.sql.legacy.filterLazyNotNullCheckPlacement")
+ .internal()
+ .doc("When true, whole stage codegen of a filter condition, including
the FILTER clause " +
+ "of an aggregate function, evaluates an IS NOT NULL check just before
the first " +
+ "top-level conjunct that references its column, or after all
conjuncts, even when a " +
+ "conjunct before that position can raise an error, so the error is
raised on a row " +
Review Comment:
Rewritten in 94228e373fa for the new config. It describes both values, the
placement of inferred predicates, the nondeterministic and UDF conjuncts, and
the NULL-conjunct handling that applies with `false`.
##########
docs/sql-migration-guide.md:
##########
@@ -28,6 +28,7 @@ license: |
## Upgrading from Spark SQL 4.3 to 4.4
+- Since Spark 4.4, with whole-stage codegen enabled, a filter condition,
including the `FILTER` clause of an aggregate function, evaluates an `IS NOT
NULL` check written in the query that cannot raise an error, such as a check on
a column, on a struct field or on a string function of them, before a later
conjunct that can raise an error, is nondeterministic or calls a user-defined
function, in the order of the condition, as Spark already did with whole-stage
codegen disabled. Previously, the check was evaluated just before the first
conjunct that references the column, or after all conjuncts, so a query such as
`SELECT a FROM t WHERE x IS NOT NULL AND y + 1 > 0` could fail with
`ARITHMETIC_OVERFLOW` in ANSI mode on a row where `x` is `NULL` and `y` is
`2147483647`, which is dropped with whole-stage codegen disabled. As a
consequence, a nondeterministic conjunct such as `rand(seed)` after such a
check now runs only on the rows the check keeps, so it can return different
values for th
e same seed and data, and a side-effecting user-defined function is no longer
called on the rows the check rejects. An `IS NOT NULL` check that the optimizer
infers from another conjunct, a check on an expression that can raise an error
itself, such as `cast(s AS INT) IS NOT NULL`, and a check on a column that the
same stage computes by such an expression, such as an aggregate result in a
`HAVING` clause, keep the previous placement, so a later conjunct can still
raise an error on a row that such a check rejects; and a check written after a
conjunct that references its column is still evaluated before that conjunct. To
restore the previous placement, set
`spark.sql.legacy.filterLazyNotNullCheckPlacement` to `true`.
Review Comment:
The note is gone in 94228e373fa, since the config is off by default.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala:
##########
@@ -3156,6 +3157,119 @@ class WholeStageCodegenSuite extends SharedSparkSession
}
}
+ test("SPARK-60329: a filter checks a column IS NOT NULL before a conjunct
that can raise") {
Review Comment:
Added in 94228e373fa. A check before `rand(42) < 0.5` returns the same rows
with codegen on and off. A check before a counting UDF calls it only on the 50
of 100 rows the check keeps.
--
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]