@@ -314,7 +314,7 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
314
314
// a && a => a
315
315
case (l, r) if l fastEquals r => l
316
316
// (a || b) && (a || c) => a || (b && c)
317
- case (_, _) =>
317
+ case _ =>
318
318
// 1. Split left and right to get the disjunctive predicates,
319
319
// i.e. lhsSet = (a, b), rhsSet = (a, c)
320
320
// 2. Find the common predict between lhsSet and rhsSet, i.e. common = (a)
@@ -323,19 +323,20 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
323
323
val lhsSet = splitDisjunctivePredicates(left).toSet
324
324
val rhsSet = splitDisjunctivePredicates(right).toSet
325
325
val common = lhsSet.intersect(rhsSet)
326
- val ldiff = lhsSet.diff(common)
327
- val rdiff = rhsSet.diff(common)
328
- if (ldiff.size == 0 || rdiff.size == 0 ) {
329
- // a && (a || b) => a
330
- common.reduce(Or )
326
+ if (common.isEmpty) {
327
+ // No common factors, return the original predicate
328
+ and
331
329
} else {
332
- // (a || b || c || ...) && (a || b || d || ...) && (a || b || e || ...) ... =>
333
- // (a || b) || ((c || ...) && (f || ...) && (e || ...) && ...)
334
- (ldiff.reduceOption(Or ) ++ rdiff.reduceOption(Or ))
335
- .reduceOption(And )
336
- .map(_ :: common.toList)
337
- .getOrElse(common.toList)
338
- .reduce(Or )
330
+ val ldiff = lhsSet.diff(common)
331
+ val rdiff = rhsSet.diff(common)
332
+ if (ldiff.isEmpty || rdiff.isEmpty) {
333
+ // (a || b || c || ...) && (a || b) => (a || b)
334
+ common.reduce(Or )
335
+ } else {
336
+ // (a || b || c || ...) && (a || b || d || ...) =>
337
+ // ((c || ...) && (d || ...)) || a || b
338
+ (And (ldiff.reduce(Or ), rdiff.reduce(Or )) :: common.toList).reduce(Or )
339
+ }
339
340
}
340
341
} // end of And(left, right)
341
342
@@ -351,7 +352,7 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
351
352
// a || a => a
352
353
case (l, r) if l fastEquals r => l
353
354
// (a && b) || (a && c) => a && (b || c)
354
- case (_, _) =>
355
+ case _ =>
355
356
// 1. Split left and right to get the conjunctive predicates,
356
357
// i.e. lhsSet = (a, b), rhsSet = (a, c)
357
358
// 2. Find the common predict between lhsSet and rhsSet, i.e. common = (a)
@@ -360,19 +361,20 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
360
361
val lhsSet = splitConjunctivePredicates(left).toSet
361
362
val rhsSet = splitConjunctivePredicates(right).toSet
362
363
val common = lhsSet.intersect(rhsSet)
363
- val ldiff = lhsSet.diff(common)
364
- val rdiff = rhsSet.diff(common)
365
- if ( ldiff.size == 0 || rdiff.size == 0 ) {
366
- // a || (b && a) => a
367
- common.reduce(And )
364
+ if (common.isEmpty) {
365
+ // No common factors, return the original predicate
366
+ or
368
367
} else {
369
- // (a && b && c && ...) || (a && b && d && ...) || (a && b && e && ...) ... =>
370
- // a && b && ((c && ...) || (d && ...) || (e && ...) || ...)
371
- (ldiff.reduceOption(And ) ++ rdiff.reduceOption(And ))
372
- .reduceOption(Or )
373
- .map(_ :: common.toList)
374
- .getOrElse(common.toList)
375
- .reduce(And )
368
+ val ldiff = lhsSet.diff(common)
369
+ val rdiff = rhsSet.diff(common)
370
+ if (ldiff.isEmpty || rdiff.isEmpty) {
371
+ // (a && b) || (a && b && c && ...) => a && b
372
+ common.reduce(And )
373
+ } else {
374
+ // (a && b && c && ...) || (a && b && d && ...) =>
375
+ // ((c && ...) || (d && ...)) && a && b
376
+ (Or (ldiff.reduce(And ), rdiff.reduce(And )) :: common.toList).reduce(And )
377
+ }
376
378
}
377
379
} // end of Or(left, right)
378
380
0 commit comments