Skip to content
Snippets Groups Projects
Commit c632bdc0 authored by Dongjoon Hyun's avatar Dongjoon Hyun Committed by Michael Armbrust
Browse files

[SPARK-14029][SQL] Improve BooleanSimplification optimization by implementing...

[SPARK-14029][SQL] Improve BooleanSimplification optimization by implementing `Not` canonicalization.

## What changes were proposed in this pull request?

Currently, **BooleanSimplification** optimization can handle the following cases.
* a && (!a || b ) ==> a && b
* a && (b || !a ) ==> a && b

However, it can not handle the followings cases since those equations fail at the comparisons between their canonicalized forms.
* a < 1 && (!(a < 1) || b)     ==> (a < 1) && b
* a <= 1 && (!(a <= 1) || b) ==> (a <= 1) && b
* a > 1 && (!(a > 1) || b)     ==> (a > 1) && b
* a >= 1 && (!(a >= 1) || b) ==> (a >= 1) && b

This PR implements the above cases and also the followings, too.
* a < 1 && ((a >= 1) || b )   ==> (a < 1) && b
* a <= 1 && ((a > 1) || b )   ==> (a <= 1) && b
* a > 1 && ((a <= 1) || b)  ==> (a > 1) && b
* a >= 1 && ((a < 1) || b)  ==> (a >= 1) && b

## How was this patch tested?

Pass the Jenkins tests including new test cases in BooleanSimplicationSuite.

Author: Dongjoon Hyun <dongjoon@apache.org>

Closes #11851 from dongjoon-hyun/SPARK-14029.
parent 0ce01635
No related branches found
No related tags found
No related merge requests found
......@@ -71,6 +71,15 @@ object Canonicalize extends {
case GreaterThanOrEqual(l, r) if l.hashCode() > r.hashCode() => LessThanOrEqual(r, l)
case LessThanOrEqual(l, r) if l.hashCode() > r.hashCode() => GreaterThanOrEqual(r, l)
case Not(GreaterThan(l, r)) if l.hashCode() > r.hashCode() => GreaterThan(r, l)
case Not(GreaterThan(l, r)) => LessThanOrEqual(l, r)
case Not(LessThan(l, r)) if l.hashCode() > r.hashCode() => LessThan(r, l)
case Not(LessThan(l, r)) => GreaterThanOrEqual(l, r)
case Not(GreaterThanOrEqual(l, r)) if l.hashCode() > r.hashCode() => GreaterThanOrEqual(r, l)
case Not(GreaterThanOrEqual(l, r)) => LessThan(l, r)
case Not(LessThanOrEqual(l, r)) if l.hashCode() > r.hashCode() => LessThanOrEqual(r, l)
case Not(LessThanOrEqual(l, r)) => GreaterThan(l, r)
case _ => e
}
}
......@@ -74,6 +74,12 @@ class ExpressionSetSuite extends SparkFunSuite {
setTest(1, aUpper > bUpper, bUpper < aUpper)
setTest(1, aUpper >= bUpper, bUpper <= aUpper)
// `Not` canonicalization
setTest(1, Not(aUpper > 1), aUpper <= 1, Not(Literal(1) < aUpper), Literal(1) >= aUpper)
setTest(1, Not(aUpper < 1), aUpper >= 1, Not(Literal(1) > aUpper), Literal(1) <= aUpper)
setTest(1, Not(aUpper >= 1), aUpper < 1, Not(Literal(1) <= aUpper), Literal(1) > aUpper)
setTest(1, Not(aUpper <= 1), aUpper > 1, Not(Literal(1) >= aUpper), Literal(1) < aUpper)
test("add to / remove from set") {
val initialSet = ExpressionSet(aUpper + 1 :: Nil)
......
......@@ -99,6 +99,34 @@ class BooleanSimplificationSuite extends PlanTest with PredicateHelper {
checkCondition(('b || !'a ) && 'a, 'b && 'a)
}
test("a < 1 && (!(a < 1) || b)") {
checkCondition('a < 1 && (!('a < 1) || 'b), ('a < 1) && 'b)
checkCondition('a < 1 && ('b || !('a < 1)), ('a < 1) && 'b)
checkCondition('a <= 1 && (!('a <= 1) || 'b), ('a <= 1) && 'b)
checkCondition('a <= 1 && ('b || !('a <= 1)), ('a <= 1) && 'b)
checkCondition('a > 1 && (!('a > 1) || 'b), ('a > 1) && 'b)
checkCondition('a > 1 && ('b || !('a > 1)), ('a > 1) && 'b)
checkCondition('a >= 1 && (!('a >= 1) || 'b), ('a >= 1) && 'b)
checkCondition('a >= 1 && ('b || !('a >= 1)), ('a >= 1) && 'b)
}
test("a < 1 && ((a >= 1) || b)") {
checkCondition('a < 1 && ('a >= 1 || 'b ), ('a < 1) && 'b)
checkCondition('a < 1 && ('b || 'a >= 1), ('a < 1) && 'b)
checkCondition('a <= 1 && ('a > 1 || 'b ), ('a <= 1) && 'b)
checkCondition('a <= 1 && ('b || 'a > 1), ('a <= 1) && 'b)
checkCondition('a > 1 && (('a <= 1) || 'b), ('a > 1) && 'b)
checkCondition('a > 1 && ('b || ('a <= 1)), ('a > 1) && 'b)
checkCondition('a >= 1 && (('a < 1) || 'b), ('a >= 1) && 'b)
checkCondition('a >= 1 && ('b || ('a < 1)), ('a >= 1) && 'b)
}
test("DeMorgan's law") {
checkCondition(!('a && 'b), !'a || !'b)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment