-
Notifications
You must be signed in to change notification settings - Fork 29.3k
SPARK-3711: Optimize where in clause filter queries #2561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
bee98aa
430f5d1
bd84c67
0fc902f
afedbcd
4bf2d19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ package org.apache.spark.sql.catalyst.expressions | |
| import org.apache.spark.sql.catalyst.analysis.UnresolvedException | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.types.BooleanType | ||
|
|
||
| import scala.collection.immutable.HashSet | ||
|
|
||
| object InterpretedPredicate { | ||
| def apply(expression: Expression, inputSchema: Seq[Attribute]): (Row => Boolean) = | ||
|
|
@@ -95,6 +95,21 @@ case class In(value: Expression, list: Seq[Expression]) extends Predicate { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates to `true` if `list` contains `value`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment that this is an optimized version of |
||
| */ | ||
| case class InSet(value: Expression, hset: HashSet[Any], child: Seq[Expression]) | ||
| extends Predicate { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should actually only be indented 2 spaces (and I'd include a blank line after). Only wrapped arguments are indented 4. |
||
| def children = child | ||
|
|
||
| def nullable = true // TODO: Figure out correct nullability semantics of IN. | ||
| override def toString = s"$value IN ${hset.mkString("(", ",", ")")}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd actually say InSet here as the |
||
|
|
||
| override def eval(input: Row): Any = { | ||
| hset.contains(value.eval(input)) | ||
| } | ||
| } | ||
|
|
||
| case class And(left: Expression, right: Expression) extends BinaryPredicate { | ||
| def symbol = "&&" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ import org.apache.spark.sql.catalyst.plans.logical | |
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.types._ | ||
|
|
||
| import scala.collection.immutable.HashSet | ||
| /* Implicit conversions */ | ||
| import scala.collection.JavaConversions._ | ||
|
|
||
|
|
@@ -977,7 +978,16 @@ private[hive] object HiveQl { | |
| case Token("TOK_FUNCTION", Token("TOK_ISNULL", Nil) :: child :: Nil) => | ||
| IsNull(nodeToExpr(child)) | ||
| case Token("TOK_FUNCTION", Token(IN(), Nil) :: value :: list) => | ||
| In(nodeToExpr(value), list.map(nodeToExpr)) | ||
| val valExpr = nodeToExpr(value) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of hardwiring this into the parser, lets create a rule in |
||
| val listMap = list.map(nodeToExpr) | ||
| if (listMap.exists(e => ((!e.isInstanceOf[Literal]) && | ||
| (!e.isInstanceOf[UnaryMinus]))) == false){ | ||
| val hSet = listMap.map(e => e.eval(null)) | ||
| InSet(valExpr, HashSet() ++ hSet, valExpr +: listMap) | ||
| } | ||
| else{ | ||
| In(valExpr, listMap) | ||
| } | ||
| case Token("TOK_FUNCTION", | ||
| Token(BETWEEN(), Nil) :: | ||
| Token("KW_FALSE", Nil) :: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scala import should go first. See here for more details.