dongjoon-hyun commented on code in PR #60765:
URL: https://github.com/apache/spark/pull/60765#discussion_r4651054818


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -3115,14 +3115,16 @@ object PushPredicateThroughNonJoin extends 
Rule[LogicalPlan] with PredicateHelpe
   private def pushDownPredicate(
       filter: Filter,
       grandchild: LogicalPlan,
-      keepThrowableUp: Boolean)(insertFilter: Expression => LogicalPlan): 
LogicalPlan = {
+      keepFailableUp: Boolean)(insertFilter: Expression => LogicalPlan): 
LogicalPlan = {
     // Only push down the predicates that is deterministic and all the 
referenced attributes
-    // come from grandchild. With `keepThrowableUp`, a throwable predicate is 
not pushed even when
-    // it is written first, as the operator in between can drop rows it would 
then see.
+    // come from grandchild. With `keepFailableUp`, a predicate that can fail, 
i.e. one that
+    // `ExprUtils.canEvaluateUnconditionally` rejects such as a throwable one 
or an ANSI division,
+    // is not pushed even when it is written first, as the operator in between 
can drop rows it
+    // would then see.
     // TODO: non-deterministic predicates could be pushed through some 
operators that do not change
     // the rows.
     val (pushDown, stayUp) = splitPushableConjuncts(filter.condition) { cond =>
-      cond.deterministic && !(keepThrowableUp && cond.throwable) &&
+      cond.deterministic && (!keepFailableUp || 
ExprUtils.canEvaluateUnconditionally(cond)) &&

Review Comment:
   Thank you. I confirmed both examples: they return their rows on v4.3.0 and 
fail with this PR. As written, the generator runs before the `WHERE` clause, so 
these queries only succeeded because the optimizer moved the condition below 
the generator. I added a migration guide entry next to the one for inferred 
filters. It states the lost pushdown, the queries that now fail, and the 
rewrite that filters in a subquery below the `LATERAL VIEW`. The PR description 
states the same. I'd not push a relaxed `try_eval` copy of the condition below 
the operator in this PR: it evaluates the condition twice, can not reach a data 
source, and is a new mechanism that the other filter pushdown rules do not 
have. Like SPARK-60633, which this PR extends, I did not add a legacy conf.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2994,7 +2994,7 @@ object PushPredicateThroughNonJoin extends 
Rule[LogicalPlan] with PredicateHelpe
 
     case filter @ Filter(_, u: UnaryNode)
         if canPushThrough(u) && u.expressions.forall(_.deterministic) =>
-      pushDownPredicate(filter, u.child, keepThrowableUp = canDropRows(u)) { 
predicate =>
+      pushDownPredicate(filter, u.child, keepFailableUp = canDropRows(u)) { 
predicate =>

Review Comment:
   Thank you. I agree that these generators always output a row for each input 
row. Treating them as such in `canDropRows` would also change which throwable 
conditions SPARK-60633 keeps above them, so I'd keep this PR to the conditions 
that can fail. As you noted, the `ReplicateRows` generate of `EXCEPT ALL` and 
`INTERSECT ALL` has to keep the restriction. I added that case to the PR 
description as Issue 4: it fails with `CAST_INVALID_INPUT` on v3.5.9 to v4.3.0 
and succeeds with this PR.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -3115,14 +3115,16 @@ object PushPredicateThroughNonJoin extends 
Rule[LogicalPlan] with PredicateHelpe
   private def pushDownPredicate(
       filter: Filter,
       grandchild: LogicalPlan,
-      keepThrowableUp: Boolean)(insertFilter: Expression => LogicalPlan): 
LogicalPlan = {
+      keepFailableUp: Boolean)(insertFilter: Expression => LogicalPlan): 
LogicalPlan = {
     // Only push down the predicates that is deterministic and all the 
referenced attributes
-    // come from grandchild. With `keepThrowableUp`, a throwable predicate is 
not pushed even when
-    // it is written first, as the operator in between can drop rows it would 
then see.
+    // come from grandchild. With `keepFailableUp`, a predicate that can fail, 
i.e. one that
+    // `ExprUtils.canEvaluateUnconditionally` rejects such as a throwable one 
or an ANSI division,
+    // is not pushed even when it is written first, as the operator in between 
can drop rows it
+    // would then see.
     // TODO: non-deterministic predicates could be pushed through some 
operators that do not change
     // the rows.
     val (pushDown, stayUp) = splitPushableConjuncts(filter.condition) { cond =>
-      cond.deterministic && !(keepThrowableUp && cond.throwable) &&
+      cond.deterministic && (!keepFailableUp || 
ExprUtils.canEvaluateUnconditionally(cond)) &&

Review Comment:
   Thank you. Confirmed: the dynamic partition pruning filter stayed above the 
`explode` and was removed. `pushDownPredicate` now uses 
`cannotFailOnDroppedRows`, which lets a `DynamicPruningSubquery`, a 
`BloomFilterMightContain` over a bloom filter that reads no column, or a 
`PerPartitionRuntimeFilterSubquery` through when its key can not fail. I added 
a `FilterPushdownSuite` test for the three filters and for a key that can fail. 
Your example query plans `dynamicpruningexpression` on the fact scan again.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2994,7 +2994,7 @@ object PushPredicateThroughNonJoin extends 
Rule[LogicalPlan] with PredicateHelpe
 
     case filter @ Filter(_, u: UnaryNode)
         if canPushThrough(u) && u.expressions.forall(_.deterministic) =>
-      pushDownPredicate(filter, u.child, keepThrowableUp = canDropRows(u)) { 
predicate =>
+      pushDownPredicate(filter, u.child, keepFailableUp = canDropRows(u)) { 
predicate =>

Review Comment:
   Thank you. Your example also fails with `DIVIDE_BY_ZERO` on v4.3.0, and 
`PushDownLeftSemiAntiJoin` does not keep a throwable condition above these 
operators either, so this is a pre-existing issue of another rule. I'll file a 
new JIRA issue for it.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -3115,14 +3115,16 @@ object PushPredicateThroughNonJoin extends 
Rule[LogicalPlan] with PredicateHelpe
   private def pushDownPredicate(
       filter: Filter,
       grandchild: LogicalPlan,
-      keepThrowableUp: Boolean)(insertFilter: Expression => LogicalPlan): 
LogicalPlan = {
+      keepFailableUp: Boolean)(insertFilter: Expression => LogicalPlan): 
LogicalPlan = {
     // Only push down the predicates that is deterministic and all the 
referenced attributes
-    // come from grandchild. With `keepThrowableUp`, a throwable predicate is 
not pushed even when
-    // it is written first, as the operator in between can drop rows it would 
then see.
+    // come from grandchild. With `keepFailableUp`, a predicate that can fail, 
i.e. one that

Review Comment:
   Thank you. The comment of `cannotFailOnDroppedRows` now says that the check 
is conservative, with `upper(s) = 'X'` as an example.
   



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala:
##########
@@ -1001,6 +1001,24 @@ class FilterPushdownSuite extends PlanTest {
     comparePlans(Optimize.execute(outerQuery), outerAnswer)
   }
 
+  test("SPARK-62143: a condition that can fail is not pushed through a 
generate that can drop " +

Review Comment:
   Thank you. I added a `FilterPushdownSuite` test with an ANSI division of 
literals above a `ScriptTransformation`.
   



-- 
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]

Reply via email to