@@ -311,13 +311,20 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
311
311
// a && a => a
312
312
case (l, r) if l fastEquals r => l
313
313
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,
316
+ * i.e. lhsSet = (a, b), rhsSet = (a, c)
317
+ * 2. Find the common predict between lhsSet and rhsSet, i.e. common = (a)
318
+ * 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = (b), rdiff = (c)
319
+ * 4. Apply the formula, get the optimized predict: common || (ldiff && rdiff)
320
+ */
314
321
val lhsSet = splitDisjunctivePredicates(left).toSet
315
322
val rhsSet = splitDisjunctivePredicates(right).toSet
316
323
val common = lhsSet.intersect(rhsSet)
317
324
val ldiff = lhsSet.diff(common)
318
325
val rdiff = rhsSet.diff(common)
319
326
if (ldiff.size == 0 || rdiff.size == 0 ) {
320
- // a && (a || b)
327
+ // a && (a || b) => a
321
328
common.reduce(Or )
322
329
} else {
323
330
// (a || b || c || ...) && (a || b || d || ...) && (a || b || e || ...) ... =>
@@ -339,13 +346,20 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
339
346
// a || a => a
340
347
case (l, r) if l fastEquals r => l
341
348
case (_, _) =>
349
+ /* Do optimize for predicates using formula (a && b) || (a && c) => a && (b || c)
350
+ * 1. Split left and right to get the conjunctive predicates,
351
+ * i.e. lhsSet = (a, b), rhsSet = (a, c)
352
+ * 2. Find the common predict between lhsSet and rhsSet, i.e. common = (a)
353
+ * 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = (b), rdiff = (c)
354
+ * 4. Apply the formula, get the optimized predict: common && (ldiff || rdiff)
355
+ */
342
356
val lhsSet = splitConjunctivePredicates(left).toSet
343
357
val rhsSet = splitConjunctivePredicates(right).toSet
344
358
val common = lhsSet.intersect(rhsSet)
345
359
val ldiff = lhsSet.diff(common)
346
360
val rdiff = rhsSet.diff(common)
347
361
if ( ldiff.size == 0 || rdiff.size == 0 ) {
348
- // a || (b && a)
362
+ // a || (b && a) => a
349
363
common.reduce(And )
350
364
} else {
351
365
// (a && b && c && ...) || (a && b && d && ...) || (a && b && e && ...) ... =>
0 commit comments