Skip to content

Commit

Permalink
[SPARK-16174][SQL] Improve OptimizeIn optimizer to remove literal r…
Browse files Browse the repository at this point in the history
…epetitions

## What changes were proposed in this pull request?

This PR improves `OptimizeIn` optimizer to remove the literal repetitions from SQL `IN` predicates. This optimizer prevents user mistakes and also can optimize some queries like [TPCDS-36](https://github.com/apache/spark/blob/master/sql/core/src/test/resources/tpcds/q36.sql#L19).

**Before**
```scala
scala> sql("select state from (select explode(array('CA','TN')) state) where state in ('TN','TN','TN','TN','TN','TN','TN')").explain
== Physical Plan ==
*Filter state#6 IN (TN,TN,TN,TN,TN,TN,TN)
+- Generate explode([CA,TN]), false, false, [state#6]
   +- Scan OneRowRelation[]
```

**After**
```scala
scala> sql("select state from (select explode(array('CA','TN')) state) where state in ('TN','TN','TN','TN','TN','TN','TN')").explain
== Physical Plan ==
*Filter state#6 IN (TN)
+- Generate explode([CA,TN]), false, false, [state#6]
   +- Scan OneRowRelation[]
```

## How was this patch tested?

Pass the Jenkins tests (including a new testcase).

Author: Dongjoon Hyun <[email protected]>

Closes apache#13876 from dongjoon-hyun/SPARK-16174.
  • Loading branch information
dongjoon-hyun authored and cloud-fan committed Jul 7, 2016
1 parent 6343f66 commit a04cab8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ case class In(value: Expression, list: Seq[Expression]) extends Predicate
}

override def children: Seq[Expression] = value +: list
lazy val inSetConvertible = list.forall(_.isInstanceOf[Literal])

override def nullable: Boolean = children.exists(_.nullable)
override def foldable: Boolean = children.forall(_.foldable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,16 +820,24 @@ object ConstantFolding extends Rule[LogicalPlan] {
}

/**
* Replaces [[In (value, seq[Literal])]] with optimized version[[InSet (value, HashSet[Literal])]]
* which is much faster
* Optimize IN predicates:
* 1. Removes literal repetitions.
* 2. Replaces [[In (value, seq[Literal])]] with optimized version
* [[InSet (value, HashSet[Literal])]] which is much faster.
*/
case class OptimizeIn(conf: CatalystConf) extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case q: LogicalPlan => q transformExpressionsDown {
case In(v, list) if !list.exists(!_.isInstanceOf[Literal]) &&
list.size > conf.optimizerInSetConversionThreshold =>
val hSet = list.map(e => e.eval(EmptyRow))
InSet(v, HashSet() ++ hSet)
case expr @ In(v, list) if expr.inSetConvertible =>
val newList = ExpressionSet(list).toSeq
if (newList.size > conf.optimizerInSetConversionThreshold) {
val hSet = newList.map(e => e.eval(EmptyRow))
InSet(v, HashSet() ++ hSet)
} else if (newList.size < list.size) {
expr.copy(list = newList)
} else { // newList.length == list.length
expr
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ class OptimizeInSuite extends PlanTest {

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

test("OptimizedIn test: Remove deterministic repetitions") {
val originalQuery =
testRelation
.where(In(UnresolvedAttribute("a"),
Seq(Literal(1), Literal(1), Literal(2), Literal(2), Literal(1), Literal(2))))
.where(In(UnresolvedAttribute("b"),
Seq(UnresolvedAttribute("a"), UnresolvedAttribute("a"),
Round(UnresolvedAttribute("a"), 0), Round(UnresolvedAttribute("a"), 0),
Rand(0), Rand(0))))
.analyze

val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer =
testRelation
.where(In(UnresolvedAttribute("a"), Seq(Literal(1), Literal(2))))
.where(In(UnresolvedAttribute("b"),
Seq(UnresolvedAttribute("a"), UnresolvedAttribute("a"),
Round(UnresolvedAttribute("a"), 0), Round(UnresolvedAttribute("a"), 0),
Rand(0), Rand(0))))
.analyze

comparePlans(optimized, correctAnswer)
}

test("OptimizedIn test: In clause not optimized to InSet when less than 10 items") {
val originalQuery =
testRelation
Expand Down

0 comments on commit a04cab8

Please sign in to comment.