Skip to content

Commit

Permalink
[SPARK-6201] [SQL] promote string and do widen types for IN
Browse files Browse the repository at this point in the history
huangjs
Acutally spark sql will first go through analysis period, in which we do widen types and promote strings, and then optimization, where constant IN will be converted into INSET.

So it turn out that we only need to fix this for IN.

Author: Daoyuan Wang <[email protected]>

Closes apache#4945 from adrian-wang/inset and squashes the following commits:

71e05cc [Daoyuan Wang] minor fix
581fa1c [Daoyuan Wang] mysql way
f3f7baf [Daoyuan Wang] address comments
5eed4bc [Daoyuan Wang] promote string and do widen types for IN
  • Loading branch information
adrian-wang authored and nemccarthy committed Jun 19, 2015
1 parent 20a4dac commit bbfcc37
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ trait HiveTypeCoercion {
val typeCoercionRules =
PropagateTypes ::
ConvertNaNs ::
InConversion ::
WidenTypes ::
PromoteStrings ::
DecimalPrecision ::
Expand Down Expand Up @@ -287,6 +288,16 @@ trait HiveTypeCoercion {
}
}

/**
* Convert all expressions in in() list to the left operator type
*/
object InConversion extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions {
case i @ In(a, b) if b.exists(_.dataType != a.dataType) =>
i.makeCopy(Array(a, b.map(Cast(_, a.dataType))))
}
}

// scalastyle:off
/**
* Calculates and propagates precision for fixed-precision decimals. Hive has a number of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ object OptimizeIn extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
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)
val hSet = list.map(e => e.eval(null))
InSet(v, HashSet() ++ hSet)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
Row(1, 1) :: Nil)
}

test("SPARK-6201 IN type conversion") {
jsonRDD(sparkContext.parallelize(Seq("{\"a\": \"1\"}}", "{\"a\": \"2\"}}", "{\"a\": \"3\"}}")))
.registerTempTable("d")

checkAnswer(
sql("select * from d where d.a in (1,2)"),
Seq(Row("1"), Row("2")))
}

test("SPARK-3176 Added Parser of SQL ABS()") {
checkAnswer(
sql("SELECT ABS(-1.3)"),
Expand Down

0 comments on commit bbfcc37

Please sign in to comment.