Skip to content
Snippets Groups Projects
Commit 7dbc162f authored by zhaorongsheng's avatar zhaorongsheng Committed by Xiao Li
Browse files

[SPARK-20017][SQL] change the nullability of function 'StringToMap' from 'false' to 'true'

## What changes were proposed in this pull request?

Change the nullability of function `StringToMap` from `false` to `true`.

Author: zhaorongsheng <334362872@qq.com>

Closes #17350 from zhaorongsheng/bug-fix_strToMap_NPE.
parent ae4b91d1
No related branches found
No related tags found
No related merge requests found
...@@ -390,6 +390,8 @@ case class CreateNamedStructUnsafe(children: Seq[Expression]) extends CreateName ...@@ -390,6 +390,8 @@ case class CreateNamedStructUnsafe(children: Seq[Expression]) extends CreateName
Examples: Examples:
> SELECT _FUNC_('a:1,b:2,c:3', ',', ':'); > SELECT _FUNC_('a:1,b:2,c:3', ',', ':');
map("a":"1","b":"2","c":"3") map("a":"1","b":"2","c":"3")
> SELECT _FUNC_('a');
map("a":null)
""") """)
// scalastyle:on line.size.limit // scalastyle:on line.size.limit
case class StringToMap(text: Expression, pairDelim: Expression, keyValueDelim: Expression) case class StringToMap(text: Expression, pairDelim: Expression, keyValueDelim: Expression)
...@@ -407,7 +409,7 @@ case class StringToMap(text: Expression, pairDelim: Expression, keyValueDelim: E ...@@ -407,7 +409,7 @@ case class StringToMap(text: Expression, pairDelim: Expression, keyValueDelim: E
override def inputTypes: Seq[AbstractDataType] = Seq(StringType, StringType, StringType) override def inputTypes: Seq[AbstractDataType] = Seq(StringType, StringType, StringType)
override def dataType: DataType = MapType(StringType, StringType, valueContainsNull = false) override def dataType: DataType = MapType(StringType, StringType)
override def checkInputDataTypes(): TypeCheckResult = { override def checkInputDataTypes(): TypeCheckResult = {
if (Seq(pairDelim, keyValueDelim).exists(! _.foldable)) { if (Seq(pairDelim, keyValueDelim).exists(! _.foldable)) {
......
...@@ -251,6 +251,9 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper { ...@@ -251,6 +251,9 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper {
} }
test("StringToMap") { test("StringToMap") {
val expectedDataType = MapType(StringType, StringType, valueContainsNull = true)
assert(new StringToMap("").dataType === expectedDataType)
val s0 = Literal("a:1,b:2,c:3") val s0 = Literal("a:1,b:2,c:3")
val m0 = Map("a" -> "1", "b" -> "2", "c" -> "3") val m0 = Map("a" -> "1", "b" -> "2", "c" -> "3")
checkEvaluation(new StringToMap(s0), m0) checkEvaluation(new StringToMap(s0), m0)
...@@ -271,6 +274,10 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper { ...@@ -271,6 +274,10 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper {
val m4 = Map("a" -> "1", "b" -> "2", "c" -> "3") val m4 = Map("a" -> "1", "b" -> "2", "c" -> "3")
checkEvaluation(new StringToMap(s4, Literal("_")), m4) checkEvaluation(new StringToMap(s4, Literal("_")), m4)
val s5 = Literal("a")
val m5 = Map("a" -> null)
checkEvaluation(new StringToMap(s5), m5)
// arguments checking // arguments checking
assert(new StringToMap(Literal("a:1,b:2,c:3")).checkInputDataTypes().isSuccess) assert(new StringToMap(Literal("a:1,b:2,c:3")).checkInputDataTypes().isSuccess)
assert(new StringToMap(Literal(null)).checkInputDataTypes().isFailure) assert(new StringToMap(Literal(null)).checkInputDataTypes().isFailure)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment