Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix.
  • Loading branch information
gatorsmile committed Sep 11, 2018
commit b05052e29fd1e6f8c11586c336864e8808bef737
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,18 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
case a And b if !a.nullable && !b.nullable &&
(Not(a).semanticEquals(b) || a.semanticEquals(Not(b))) =>
FalseLiteral
case a And b if Not(a).semanticEquals(b) =>
If(IsNull(a), Literal.create(null, a.dataType), FalseLiteral)
case a And b if a.semanticEquals(Not(b)) =>
If(IsNull(b), Literal.create(null, b.dataType), FalseLiteral)

case a Or b if !a.nullable && !b.nullable &&
(Not(a).semanticEquals(b) || a.semanticEquals(Not(b))) =>
TrueLiteral
case a Or b if Not(a).semanticEquals(b) =>
If(IsNull(a), Literal.create(null, a.dataType), TrueLiteral)
case a Or b if a.semanticEquals(Not(b)) =>
If(IsNull(b), Literal.create(null, b.dataType), TrueLiteral)

case a And b if a.semanticEquals(b) => a
case a Or b if a.semanticEquals(b) => a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.BooleanType

class BooleanSimplificationSuite extends PlanTest with PredicateHelper {

Expand All @@ -48,12 +49,12 @@ class BooleanSimplificationSuite extends PlanTest with PredicateHelper {
testRelation.output, Seq(Row(1, 2, 3, "abc"))
)

val testNotnullableRelation = LocalRelation('a.int.notNull, 'b.int.notNull, 'c.int.notNull,
val testNotNullableRelation = LocalRelation('a.int.notNull, 'b.int.notNull, 'c.int.notNull,
'd.string.notNull, 'e.boolean.notNull, 'f.boolean.notNull, 'g.boolean.notNull,
'h.boolean.notNull)

val testNotnullableRelationWithData = LocalRelation.fromExternalRows(
testNotnullableRelation.output, Seq(Row(1, 2, 3, "abc"))
val testNotNullableRelationWithData = LocalRelation.fromExternalRows(
testNotNullableRelation.output, Seq(Row(1, 2, 3, "abc"))
)

private def checkCondition(input: Expression, expected: LogicalPlan): Unit = {
Expand All @@ -71,7 +72,7 @@ class BooleanSimplificationSuite extends PlanTest with PredicateHelper {

private def checkConditionInNotNullableRelation(
input: Expression, expected: LogicalPlan): Unit = {
val plan = testNotnullableRelationWithData.where(input).analyze
val plan = testNotNullableRelationWithData.where(input).analyze
val actual = Optimize.execute(plan)
comparePlans(actual, expected)
}
Expand Down Expand Up @@ -189,19 +190,23 @@ class BooleanSimplificationSuite extends PlanTest with PredicateHelper {
}

test("Complementation Laws") {
checkConditionInNotNullableRelation('e && !'e, testNotnullableRelation)
checkConditionInNotNullableRelation(!'e && 'e, testNotnullableRelation)
checkConditionInNotNullableRelation('e && !'e, testNotNullableRelation)
checkConditionInNotNullableRelation(!'e && 'e, testNotNullableRelation)

checkConditionInNotNullableRelation('e || !'e, testNotnullableRelationWithData)
checkConditionInNotNullableRelation(!'e || 'e, testNotnullableRelationWithData)
checkConditionInNotNullableRelation('e || !'e, testNotNullableRelationWithData)
checkConditionInNotNullableRelation(!'e || 'e, testNotNullableRelationWithData)
}

test("Complementation Laws - null handling") {
checkCondition('e && !'e, testRelationWithData.where('e && !'e).analyze)
checkCondition(!'e && 'e, testRelationWithData.where(!'e && 'e).analyze)

checkCondition('e || !'e, testRelationWithData.where('e || !'e).analyze)
checkCondition(!'e || 'e, testRelationWithData.where(!'e || 'e).analyze)
checkCondition('e && !'e,
testRelationWithData.where(If('e.isNull, Literal.create(null, BooleanType), false)).analyze)
checkCondition(!'e && 'e,
testRelationWithData.where(If('e.isNull, Literal.create(null, BooleanType), false)).analyze)

checkCondition('e || !'e,
testRelationWithData.where(If('e.isNull, Literal.create(null, BooleanType), true)).analyze)
checkCondition(!'e || 'e,
testRelationWithData.where(If('e.isNull, Literal.create(null, BooleanType), true)).analyze)
}

test("Complementation Laws - negative case") {
Expand Down