spark-test commented on code in PR #60653:
URL: https://github.com/apache/spark/pull/60653#discussion_r4610644894


##########
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:
   **Blocking (P1):** This skips the whole factoring whenever any common factor 
is throwable, including shapes the base branch already factored safely, so 
shared conjuncts that join planning depends on stay inside the OR. Take `s1 
JOIN s2 ON (s1.k = s2.k AND r AND s1.a = 1) OR (s1.k = s2.k AND r AND s2.b = 
2)` with `r = CASE WHEN s1.v < 0 THEN raise_error('bad v') ELSE TRUE END`. On 
the base branch the condition becomes `s1.k = s2.k AND (s1.a = 1 OR s2.b = 2) 
AND r`, so the join is planned on `s1.k = s2.k`, and for two tables r runs only 
on pairs whose keys match. With this change, as the code reads:
   
   - For two streams, ExtractEquiJoinKeys finds no top-level equality and 
StreamingJoinStrategy throws 'Stream-stream join without equality predicate is 
not supported', so a query that runs today fails to start or to restart.
   - For two tables, the join becomes a nested-loop or cartesian join, and 
PushExtraPredicateThroughJoin, which checks only determinism, pushes `(r AND 
s1.a = 1) OR r` below the join. An s1 row with `v < 0` and no matching key, 
which the written condition never checks, now fails the query with 
USER_RAISED_EXCEPTION.
   
   The trailing form `s1.k = s2.k AND ((s1.a = 1 AND r) OR (s2.b = 2 AND r))` 
gets the pushed filter `(s1.a = 1 AND r) OR r` too, although on the base branch 
r is factored out and kept in the join condition after `s1.a = 1 OR s2.b = 2`. 
The PR description says the factoring is skipped, but not that these queries 
regress.
   
   See **Shared repair plan 1** in the review body.



##########
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:
   **Non-blocking (P2):** `!b.throwable` lets through every operand that can 
raise without declaring it, and under the default ANSI mode those are the most 
common failing operands. In `SELECT id FROM range(3) WHERE (id < 5 OR 10 / (id 
- 1) > 1) AND id >= 5` the written predicate never divides, because `id < 5` 
holds on every row, so it should return no rows. Reading the code, b = `10 / 
(id - 1) > 1` is not throwable, so this case still rewrites the condition to 
`10 / (id - 1) > 1 AND id >= 5`, which raises DIVIDE_BY_ZERO at id = 1, while 
the same query with `raise_error('boom') IS NULL` now succeeds. Line 566 does 
the same for `(id >= 5 AND 10 / (id - 1) > 1) OR id < 5`, and lines 586 and 626 
still factor such an operand out when subexpression elimination is off. 
ExprUtils.canEvaluateUnconditionally is the repository's check for moving an 
expression out of a short-circuited position; its documentation says it 
deliberately does not rely on Expression.throwable, and ConstantPropagation
  uses it for the same failure (SPARK-61033).
   
   See **Shared repair plan 1** in the review body.



##########
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:
   **Nit (P3):** This explains why a throwable common factor is not factored, 
but under the default configuration a filter such as `(id < 5 OR 
raise_error('boom') IS NULL) AND (id > -5 OR raise_error('boom') IS NULL)` 
still evaluates raise_error first, as the code reads. Subexpression elimination 
is on and `spark.sql.subexpressionElimination.skipForShortcutExpr` is off, so 
EquivalentExpressions collects the duplicated raise_error from both operands, 
and FilterExec evaluates it before the first conjunct that references it, ahead 
of `id < 5`. The PR description defers that path, and in code only the 
DataFrameSuite test says so, by turning subexpression elimination off. A TODO 
here and in the disjunction block's step list (lines 616-619) that names the 
deferred subexpression-elimination path, and its follow-up issue if there is 
one, would tell readers that this half of the fix takes effect only where that 
elimination does not hoist the factor.
   
   **Verification:**
   - **Inspection:** Both common-factor step lists in BooleanSimplification 
carry a TODO naming subexpression elimination's hoisting of a duplicated common 
factor out of its guarded positions as the deferred path, and the change 
touches no code.



##########
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:
   **Nit (P3):** With this condition the else branch also handles predicates 
whose common factors include a throwable one, but its comments still describe a 
branch without common factors: line 598 says 'No common factors from 
disjunctive predicates, reduce common factor from conjunction' and line 602 
says 'No common factors, return the original predicate', and the disjunction 
block repeats both at lines 638 and 642. For `(a < 5 OR r) AND (c < 50 OR r)` 
with a throwable r, line 603 returns the predicate under 'No common factors' 
although r is common. Comments that say no common factor is factored out, 
because there is none or because each must stay in place behind its guards, 
would match both cases, for example 'No duplicates, return the original 
predicate' at lines 602 and 642.
   
   **Verification:**
   - **Inspection:** The comments inside both else branches of 
BooleanSimplification's common-factor eliminations no longer say that the 
branch runs only without common factors, and the change touches no code.



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