Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
case (_, Literal(false, BooleanType)) => Literal(false)
// a && a => a
case (l, r) if l fastEquals r => l
// a && (not(a) || b) => a && b
case (l, Or(l1, r)) if (Not(l) == l1) => And(l, r)
case (l, Or(r, l1)) if (Not(l) == l1) => And(l, r)
case (Or(l, l1), r) if (l1 == Not(r)) => And(l, r)
case (Or(l1, l), r) if (l1 == Not(r)) => And(l, r)
// (a || b) && (a || c) => a || (b && c)
case _ =>
// 1. Split left and right to get the disjunctive predicates,
Expand Down Expand Up @@ -540,6 +545,10 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
case LessThan(l, r) => GreaterThanOrEqual(l, r)
// not(l <= r) => l > r
case LessThanOrEqual(l, r) => GreaterThan(l, r)
// not(l || r) => not(l) && not(r)
case Or(l, r) => And(Not(l), Not(r))
// not(l && r) => not(l) or not(r)
case And(l, r) => Or(Not(l), Not(r))
// not(not(e)) => e
case Not(e) => e
case _ => not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ class BooleanSimplificationSuite extends PlanTest with PredicateHelper {
('a === 'b || 'b > 3 && 'a > 3 && 'a < 5))
}

test("a && (!a || b)") {
checkCondition('a && (!'a || 'b ), 'a && 'b)

checkCondition('a && ('b || !'a ), 'a && 'b)

checkCondition((!'a || 'b ) && 'a, 'b && 'a)

checkCondition(('b || !'a ) && 'a, 'b && 'a)
}

test("DeMorgan's law") {
checkCondition(!('a && 'b), !'a || !'b)

checkCondition(!('a || 'b), !'a && !'b)

checkCondition(!(('a && 'b) || ('c && 'd)), (!'a || !'b) && (!'c || !'d))

checkCondition(!(('a || 'b) && ('c || 'd)), (!'a && !'b) || (!'c && !'d))
}

private val caseInsensitiveAnalyzer =
new Analyzer(EmptyCatalog, EmptyFunctionRegistry, new SimpleCatalystConf(false))

Expand Down