Skip to content

Commit a028330

Browse files
adrian-wangmarmbrus
authored andcommitted
[SPARK-3362][SQL] Fix resolution for casewhen with nulls.
Current implementation will ignore else val type. Author: Daoyuan Wang <daoyuan.wang@intel.com> Closes #2245 from adrian-wang/casewhenbug and squashes the following commits: 3332f6e [Daoyuan Wang] remove wrong comment 83b536c [Daoyuan Wang] a comment to trigger retest d7315b3 [Daoyuan Wang] code improve eed35fc [Daoyuan Wang] bug in casewhen resolve
1 parent 6f7a768 commit a028330

12 files changed

+19
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ case class CaseWhen(branches: Seq[Expression]) extends Expression {
265265
false
266266
} else {
267267
val allCondBooleans = predicates.forall(_.dataType == BooleanType)
268-
val dataTypesEqual = values.map(_.dataType).distinct.size <= 1
268+
// both then and else val should be considered.
269+
val dataTypesEqual = (values ++ elseValue).map(_.dataType).distinct.size <= 1
269270
allCondBooleans && dataTypesEqual
270271
}
271272
}
272273

273-
/** Written in imperative fashion for performance considerations. Same for CaseKeyWhen. */
274+
/** Written in imperative fashion for performance considerations. */
274275
override def eval(input: Row): Any = {
275276
val len = branchesArr.length
276277
var i = 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NULL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NULL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NULL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NULL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NULL

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveTypeCoercionSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class HiveTypeCoercionSuite extends HiveComparisonTest {
3333
}
3434
}
3535

36+
val nullVal = "null"
37+
baseTypes.init.foreach { i =>
38+
createQueryTest(s"case when then $i else $nullVal end ", s"SELECT case when true then $i else $nullVal end FROM src limit 1")
39+
createQueryTest(s"case when then $nullVal else $i end ", s"SELECT case when true then $nullVal else $i end FROM src limit 1")
40+
}
41+
3642
test("[SPARK-2210] boolean cast on boolean value should be removed") {
3743
val q = "select cast(cast(key=0 as boolean) as boolean) from src"
3844
val project = TestHive.sql(q).queryExecution.executedPlan.collect { case e: Project => e }.head

0 commit comments

Comments
 (0)