From 2a04ab3deaa989738ef77b9e70dd00bba6ae4d1e Mon Sep 17 00:00:00 2001 From: Michael Armbrust Date: Thu, 13 Nov 2014 16:59:46 -0800 Subject: [PATCH] Simplify implementation of InSet. --- .../sql/catalyst/expressions/predicates.scala | 4 ++-- .../spark/sql/catalyst/optimizer/Optimizer.scala | 2 +- .../expressions/ExpressionEvaluationSuite.scala | 14 +++++++------- .../sql/catalyst/optimizer/OptimizeInSuite.scala | 3 +-- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala index 1e22b2d03c672..94b6fb084d38a 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala @@ -99,10 +99,10 @@ case class In(value: Expression, list: Seq[Expression]) extends Predicate { * Optimized version of In clause, when all filter values of In clause are * static. */ -case class InSet(value: Expression, hset: HashSet[Any], child: Seq[Expression]) +case class InSet(value: Expression, hset: Set[Any]) extends Predicate { - def children = child + def children = value :: Nil def nullable = true // TODO: Figure out correct nullability semantics of IN. override def toString = s"$value INSET ${hset.mkString("(", ",", ")")}" diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala index a4aa322fc52d8..f164a6c68a0de 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala @@ -289,7 +289,7 @@ object OptimizeIn extends Rule[LogicalPlan] { case q: LogicalPlan => q transformExpressionsDown { case In(v, list) if !list.exists(!_.isInstanceOf[Literal]) => val hSet = list.map(e => e.eval(null)) - InSet(v, HashSet() ++ hSet, v +: list) + InSet(v, HashSet() ++ hSet) } } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala index 918996f11da2c..2f57be94a80fb 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala @@ -158,13 +158,13 @@ class ExpressionEvaluationSuite extends FunSuite { val nl = Literal(null) val s = Seq(one, two) val nullS = Seq(one, two, null) - checkEvaluation(InSet(one, hS, one +: s), true) - checkEvaluation(InSet(two, hS, two +: s), true) - checkEvaluation(InSet(two, nS, two +: nullS), true) - checkEvaluation(InSet(nl, nS, nl +: nullS), true) - checkEvaluation(InSet(three, hS, three +: s), false) - checkEvaluation(InSet(three, nS, three +: nullS), false) - checkEvaluation(InSet(one, hS, one +: s) && InSet(two, hS, two +: s), true) + checkEvaluation(InSet(one, hS), true) + checkEvaluation(InSet(two, hS), true) + checkEvaluation(InSet(two, nS), true) + checkEvaluation(InSet(nl, nS), true) + checkEvaluation(InSet(three, hS), false) + checkEvaluation(InSet(three, nS), false) + checkEvaluation(InSet(one, hS) && InSet(two, hS), true) } test("MaxOf") { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeInSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeInSuite.scala index 97a78ec971c39..017b180c574b4 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeInSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeInSuite.scala @@ -52,8 +52,7 @@ class OptimizeInSuite extends PlanTest { val optimized = Optimize(originalQuery.analyze) val correctAnswer = testRelation - .where(InSet(UnresolvedAttribute("a"), HashSet[Any]()+1+2, - UnresolvedAttribute("a") +: Seq(Literal(1),Literal(2)))) + .where(InSet(UnresolvedAttribute("a"), HashSet[Any]()+1+2)) .analyze comparePlans(optimized, correctAnswer)