Skip to content

Commit 2d3406e

Browse files
committed
added comment for spark-4937
1 parent ee1c1f3 commit 2d3406e

File tree

1 file changed

+14
-2
lines changed
  • sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer

1 file changed

+14
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,19 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
311311
// a && a => a
312312
case (l, r) if l fastEquals r => l
313313
case (_, _) =>
314+
/* Do optimize for predicates using formula (a || b) && (a || c) => a || (b && c)
315+
* 1. Split left and right to get the disjunctive predicates, i.e. lhsSet = (a, b), rhsSet = (a, c)
316+
* 2. Find the common predict between lhsSet and rhsSet, i.e. common = (a)
317+
* 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = (b), rdiff = (c)
318+
* 4. Apply the formula, get the optimized predict: common || (ldiff && rdiff)
319+
*/
314320
val lhsSet = splitDisjunctivePredicates(left).toSet
315321
val rhsSet = splitDisjunctivePredicates(right).toSet
316322
val common = lhsSet.intersect(rhsSet)
317323
val ldiff = lhsSet.diff(common)
318324
val rdiff = rhsSet.diff(common)
319325
if (ldiff.size == 0 || rdiff.size == 0) {
320-
// a && (a || b)
326+
// a && (a || b) => a
321327
common.reduce(Or)
322328
} else {
323329
// (a || b || c || ...) && (a || b || d || ...) && (a || b || e || ...) ... =>
@@ -339,13 +345,19 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
339345
// a || a => a
340346
case (l, r) if l fastEquals r => l
341347
case (_, _) =>
348+
/* Do optimize for predicates using formula (a && b) || (a && c) => a && (b || c)
349+
* 1. Split left and right to get the conjunctive predicates, i.e. lhsSet = (a, b), rhsSet = (a, c)
350+
* 2. Find the common predict between lhsSet and rhsSet, i.e. common = (a)
351+
* 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = (b), rdiff = (c)
352+
* 4. Apply the formula, get the optimized predict: common && (ldiff || rdiff)
353+
*/
342354
val lhsSet = splitConjunctivePredicates(left).toSet
343355
val rhsSet = splitConjunctivePredicates(right).toSet
344356
val common = lhsSet.intersect(rhsSet)
345357
val ldiff = lhsSet.diff(common)
346358
val rdiff = rhsSet.diff(common)
347359
if ( ldiff.size == 0 || rdiff.size == 0) {
348-
// a || (b && a)
360+
// a || (b && a) => a
349361
common.reduce(And)
350362
} else {
351363
// (a && b && c && ...) || (a && b && d && ...) || (a && b && e && ...) ... =>

0 commit comments

Comments
 (0)