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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -609,15 +613,17 @@ object BooleanSimplification extends Rule[LogicalPlan] 
with PredicateHelper {
       //    i.e.  lhs = (a && b), rhs = (a && c)
       // 2. Find the common predict between lhsSet and rhsSet, i.e. common = 
(a)
       // 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = (b), 
rdiff = (c)
-      // 4. If common is non-empty, apply the formula to get the optimized 
predicate:
-      //    common && (ldiff || rdiff)
-      // 5. Else if common is empty, split left and right to get the 
conjunctive predicates.
+      // 4. If common is non-empty and not throwable, apply the formula to get 
the optimized
+      //    predicate: common && (ldiff || rdiff)
+      //    The formula evaluates common first, also on the rows where the 
operands before it in
+      //    left skip it. Thus, common can't be throwable.
+      // 5. Else, split left and right to get the conjunctive predicates.
       // for example lhs = (a || b), rhs = (a || c) => all = (a, b, a, c), 
distinct = (a, b, c)
       // optimized predicate: (a || b || c)
       val lhs = splitConjunctivePredicates(left)
       val rhs = splitConjunctivePredicates(right)
       val common = lhs.filter(e => rhs.exists(e.semanticEquals))
-      if (common.nonEmpty) {
+      if (common.nonEmpty && !common.exists(_.throwable)) {

Review Comment:
   Thank you. You're right: on master this condition keeps `s1.k = s2.k` as a 
join key, and this PR lost it. I applied your rule: a common factor that can 
fail is still extracted when, in either operand, only the common factors 
extracted before it come before it. So `(k AND r AND a) OR (k AND r AND b)` 
becomes `k AND r AND (a OR b)` again, and the stream-stream join plans as on 
master.
   
   For the trailing form `k AND ((a AND r) OR (b AND r))`, 
`PushExtraPredicateThroughJoin` now keeps a throwable predicate in the join 
condition, as `PushPredicateThroughJoin` does, so it no longer pushes `(a AND 
r) OR r` below the join. `DataFrameSuite` runs both joins, and 
`FilterPushdownSuite` and `BooleanSimplificationSuite` check the plans.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -552,7 +552,8 @@ object BooleanSimplification extends Rule[LogicalPlan] with 
PredicateHelper {
     // (NULL And (FALSE Or NULL)) = NULL, but (NULL And FALSE) = FALSE. Thus, 
a can't be nullable.
     case a And (b Or c) if !a.nullable && Not(a).semanticEquals(c) => And(a, b)
     // ((NULL Or FALSE) And NULL) = NULL, but (FALSE And NULL) = FALSE. Thus, 
c can't be nullable.
-    case (a Or b) And c if !c.nullable && a.semanticEquals(Not(c)) => And(b, c)
+    // And(b, c) evaluates b also on the rows where a is true. Thus, b can't 
be throwable.
+    case (a Or b) And c if !c.nullable && !b.throwable && 
a.semanticEquals(Not(c)) => And(b, c)

Review Comment:
   Thank you. I switched the four rewrites to 
`ExprUtils.canEvaluateUnconditionally`, so an ANSI division is kept behind its 
guard too. `PushExtraPredicateThroughJoin` still checks `throwable`, as 
`PushPredicateThroughJoin` does. The PR title and description are updated.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -571,15 +573,17 @@ object BooleanSimplification extends Rule[LogicalPlan] 
with PredicateHelper {
       //    i.e. lhs = (a || b), rhs = (a || c)
       // 2. Find the common predict between lhsSet and rhsSet, i.e. common = 
(a)
       // 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = (b), 
rdiff = (c)
-      // 4. If common is non-empty, apply the formula to get the optimized 
predicate:
-      //    common || (ldiff && rdiff)
-      // 5. Else if common is empty, split left and right to get the 
conjunctive predicates.
+      // 4. If common is non-empty and not throwable, apply the formula to get 
the optimized
+      //    predicate: common || (ldiff && rdiff)
+      //    The formula evaluates common first, also on the rows where the 
operands before it in

Review Comment:
   Thank you. Subexpression elimination is a separate issue: it also hoists 
operands that this rule never moves, such as the divisions in `id = 0 OR (10 / 
id > 1 AND 10 / id < 100)`. So I'd rather fix it separately than add a TODO 
here. The test turns it off for the two common factor queries, with a comment 
on why.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -571,15 +573,17 @@ object BooleanSimplification extends Rule[LogicalPlan] 
with PredicateHelper {
       //    i.e. lhs = (a || b), rhs = (a || c)
       // 2. Find the common predict between lhsSet and rhsSet, i.e. common = 
(a)
       // 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = (b), 
rdiff = (c)
-      // 4. If common is non-empty, apply the formula to get the optimized 
predicate:
-      //    common || (ldiff && rdiff)
-      // 5. Else if common is empty, split left and right to get the 
conjunctive predicates.
+      // 4. If common is non-empty and not throwable, apply the formula to get 
the optimized
+      //    predicate: common || (ldiff && rdiff)
+      //    The formula evaluates common first, also on the rows where the 
operands before it in
+      //    left skip it. Thus, common can't be throwable.
+      // 5. Else, split left and right to get the conjunctive predicates.
       //    for example lhs = (a && b), rhs = (a && c) => all = (a, b, a, c), 
distinct = (a, b, c)
       //    optimized predicate: (a && b && c)
       val lhs = splitDisjunctivePredicates(left)
       val rhs = splitDisjunctivePredicates(right)
       val common = lhs.filter(e => rhs.exists(e.semanticEquals))
-      if (common.nonEmpty) {
+      if (common.nonEmpty && !common.exists(_.throwable)) {

Review Comment:
   Thank you. Updated to "No common factor to extract ..." and "No duplicates, 
return the original predicate".
   



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