Skip to content

Commit

Permalink
Avoids converting predicate sets to lists
Browse files Browse the repository at this point in the history
  • Loading branch information
liancheng committed Jan 18, 2015
1 parent e833ca4 commit 1bf3258
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
} else {
// (a || b || c || ...) && (a || b || d || ...) =>
// ((c || ...) && (d || ...)) || a || b
(And(ldiff.reduce(Or), rdiff.reduce(Or)) :: common.toList).reduce(Or)
(common + And(ldiff.reduce(Or), rdiff.reduce(Or))).reduce(Or)
}
}
} // end of And(left, right)
Expand Down Expand Up @@ -373,7 +373,7 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
} else {
// (a && b && c && ...) || (a && b && d && ...) =>
// ((c && ...) || (d && ...)) && a && b
(Or(ldiff.reduce(And), rdiff.reduce(And)) :: common.toList).reduce(And)
(common + Or(ldiff.reduce(And), rdiff.reduce(And))).reduce(And)
}
}
} // end of Or(left, right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.catalyst.optimizer

import org.apache.spark.sql.catalyst.analysis.EliminateAnalysisOperators
import org.apache.spark.sql.catalyst.expressions.{Literal, Expression}
import org.apache.spark.sql.catalyst.expressions.{Or, And, Literal, Expression}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.rules._
Expand All @@ -40,11 +40,22 @@ class BooleanSimplificationSuite extends PlanTest {

val testRelation = LocalRelation('a.int, 'b.int, 'c.int, 'd.string)

def checkCondition(originCondition: Expression, optimizedCondition: Expression): Unit = {
val originQuery = testRelation.where(originCondition).analyze
val optimized = Optimize(originQuery)
val expected = testRelation.where(optimizedCondition).analyze
comparePlans(optimized, expected)
def compareConditions(e1: Expression, e2: Expression): Boolean = (e1, e2) match {
case (And(l1, l2), And(r1, r2)) =>
compareConditions(l1, r1) && compareConditions(l2, r2) ||
compareConditions(l1, r2) && compareConditions(l2, r1)

case (Or(l1, l2), Or(r1, r2)) =>
compareConditions(l1, r1) && compareConditions(l2, r2) ||
compareConditions(l1, r2) && compareConditions(l2, r1)

case (l, r) => l == r
}

def checkCondition(input: Expression, expected: Expression): Unit = {
val plan = testRelation.where(input).analyze
val actual = Optimize(plan).expressions.head
compareConditions(actual, expected)
}

test("a && a => a") {
Expand Down

0 comments on commit 1bf3258

Please sign in to comment.