Skip to content

Commit 70383b1

Browse files
ueshinmarmbrus
authored andcommitted
[SPARK-2036] [SQL] CaseConversionExpression should check if the evaluated value is null.
`CaseConversionExpression` should check if the evaluated value is `null`. Author: Takuya UESHIN <ueshin@happy-camper.st> Closes #982 from ueshin/issues/SPARK-2036 and squashes the following commits: 61e1c54 [Takuya UESHIN] Add check if the evaluated value is null. (cherry picked from commit e4c11ee) Signed-off-by: Michael Armbrust <michael@databricks.com>
1 parent 8100cbd commit 70383b1

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ trait CaseConversionExpression {
8181
def dataType: DataType = StringType
8282

8383
override def eval(input: Row): Any = {
84-
val converted = child.eval(input)
85-
convert(converted.toString)
84+
val evaluated = child.eval(input)
85+
if (evaluated == null) {
86+
null
87+
} else {
88+
convert(evaluated.toString)
89+
}
8690
}
8791
}
8892

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ class SQLQuerySuite extends QueryTest {
322322
(2, "B"),
323323
(3, "C"),
324324
(4, "D")))
325+
326+
checkAnswer(
327+
sql("SELECT n, UPPER(s) FROM nullStrings"),
328+
Seq(
329+
(1, "ABC"),
330+
(2, "ABC"),
331+
(3, null)))
325332
}
326333

327334
test("system function lower()") {
@@ -334,6 +341,13 @@ class SQLQuerySuite extends QueryTest {
334341
(4, "d"),
335342
(5, "e"),
336343
(6, "f")))
344+
345+
checkAnswer(
346+
sql("SELECT n, LOWER(s) FROM nullStrings"),
347+
Seq(
348+
(1, "abc"),
349+
(2, "abc"),
350+
(3, null)))
337351
}
338352

339353
}

sql/core/src/test/scala/org/apache/spark/sql/TestData.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,12 @@ object TestData {
106106
NullInts(null) :: Nil
107107
)
108108
nullInts.registerAsTable("nullInts")
109+
110+
case class NullStrings(n: Int, s: String)
111+
val nullStrings =
112+
TestSQLContext.sparkContext.parallelize(
113+
NullStrings(1, "abc") ::
114+
NullStrings(2, "ABC") ::
115+
NullStrings(3, null) :: Nil)
116+
nullStrings.registerAsTable("nullStrings")
109117
}

0 commit comments

Comments
 (0)