spark-test commented on code in PR #60772:
URL: https://github.com/apache/spark/pull/60772#discussion_r4651715810
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2871,10 +2871,13 @@ object PushPredicateThroughNonJoin extends
Rule[LogicalPlan] with PredicateHelpe
// split orders conditions by cost, and cost is no way to decide how
many rows a
// non-deterministic expression sees. Throwable conditions, or
conditions over throwable
// aliases, stay there too: ordering by cost could evaluate them (or
compute the aliases
- // in a lower projection) ahead of the conditions written before them.
+ // in a lower projection) ahead of the conditions written before them.
So do all the
+ // conditions written after the first of these: each reads an
expensive alias, e.g. a UDF
Review Comment:
**Non-blocking (P2):** The new code comment scopes the rule to conditions
that read an expensive alias, but a condition that can fail and reads none
takes the cheap path instead: `splitPushable` (Optimizer.scala:3158-3164),
called from this case at Optimizer.scala:2858-2861, pushes below the projection
every cheap conjunct written after the first one that stays above unless it is
marked throwable, even when that one is non-deterministic or throwable, and
`Cast`, ANSI arithmetic and `ScalaUDF` keep `Expression.throwable`'s default
(Expression.scala:169). By the review's reading of the code, the PR's
DataFrameSuite query without the `n` term,
```scala
.select($"e", notX($"e") as "f")
.where((rand(10) * 10 > 100 || $"f") && $"e".cast("int") > 0)
```
still pushes the cast below the projection and fails with CAST_INVALID_INPUT
on `'x'`, the row the first conjunct drops, with whole-stage codegen on or off
and with the split on or off, as on the base branch; with
`spark.sql.optimizer.avoidDoubleFilterEval` off the whole condition is pushed
in written order and the query returns 9 rows. Writing the second conjunct as
`parse($"e") > 0` fails the same way with FAILED_EXECUTE_UDF. `splitMovable`
(predicates.scala:307-320), which `CombineFilters` and the join rules use and
which the PR says it matches, keeps such a conjunct behind a non-deterministic
or throwable one, while `splitPushable`'s docstring (Optimizer.scala:3148-3157)
still says moving a non-throwable conjunct below a throwable one is fine. No
test covers a condition that can fail, reads no expensive alias and follows a
non-deterministic or throwable one.
See **Shared repair plan 1** in the review body.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2871,10 +2871,13 @@ object PushPredicateThroughNonJoin extends
Rule[LogicalPlan] with PredicateHelpe
// split orders conditions by cost, and cost is no way to decide how
many rows a
// non-deterministic expression sees. Throwable conditions, or
conditions over throwable
// aliases, stay there too: ordering by cost could evaluate them (or
compute the aliases
- // in a lower projection) ahead of the conditions written before them.
+ // in a lower projection) ahead of the conditions written before them.
So do all the
+ // conditions written after the first of these: each reads an
expensive alias, e.g. a UDF
+ // or a regex whose pattern comes from the data, which can fail, so a
lower layer must
+ // not evaluate it on the rows that condition filters out.
val (splitChild, splitAliases, stayUp) =
if (expensiveWithUsed.nonEmpty &&
SQLConf.get.splitProjectionForExpensiveFilters) {
- val (splittable, notSplittable) = expensiveWithUsed.partition {
+ val (splittable, notSplittable) = expensiveWithUsed.span {
Review Comment:
**Blocking (P1):** `span` keeps the conditions written after the first
non-deterministic or throwable condition, the barrier, out of the split only on
the pass that performs the split. The split turns every alias it computes in a
lower layer into a plain attribute of the top projection
(Optimizer.scala:2901), and `PushDownPredicates` runs again in the fixed-point
Operator Optimization batch. On that next pass a later condition that reads
only those attributes uses no expensive alias, so `isCheap` holds
(Optimizer.scala:2852), and `splitPushable` (Optimizer.scala:3158-3164), which
holds back only throwable conjuncts once one has stayed above, pushes it below
the projection, ahead of the barrier, which is still above it; `CombineFilters`
then merges it after the earlier condition.
This needs a condition written before the barrier whose aliases, or aliases
sharing their expensive work, the split computes below; a barrier that still
reads an alias left in the top projection; and a later condition that can fail
without being marked throwable, such as an ANSI cast or arithmetic, or a UDF
applied in the condition. The review's reading of the code, replayed in a model
of the rule but not run in Spark, gives two consequences:
- The PR's own DataFrameSuite query with `$"n" > 0 &&` prepended still fails
with CAST_INVALID_INPUT, as it does on the base branch: `n > 0` is split off,
and on the next pass `cast(e as int) + n > 0` is pushed under the barrier.
- Some queries that succeed on the base branch now fail. Under ANSI mode,
over the PR's test data, with `notX` as in the PR's test and `len1`, `len2` two
UDFs returning `e.length`:
```scala
.select($"e", notX($"e") as "f", len1($"e") as "a", len2($"e") as "b")
.where($"a" + $"b" > 0 && (rand(10) * 10 > 100 || $"f") &&
$"f".cast("string") =!= "" && $"e".cast("int") + $"a" + $"b" > 0)
```
On the base branch the split places the cheaper group over `f` first and
leaves `a + b > 0` and the cast in the top filter after the barrier, so the
query returns 9 rows, as it does with
`spark.sql.optimizer.splitProjectionForExpensiveFilters` off. With this PR only
`a + b > 0` reaches the split, which places it because `f` is still to compute;
the next pass then pushes the cast under the barrier, where it casts `'x'`,
with whole-stage codegen on and off.
The new comment (Optimizer.scala:2874-2877) and docstring
(Optimizer.scala:3214-3216) hold only for the pass that performs the split, and
no test has a condition before the barrier that reads the same aliases as a
later condition that can fail.
See **Shared repair plan 1** in the review body.
--
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]