Skip to content
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

[SPARK-13056][SQL] map column would throw NPE if value is null #10964

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ case class GetMapValue(child: Expression, key: Expression)
}
}

if (!found) {
if (!found || map.valueArray().isNullAt(i)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just realized the GetArrayItem also need to add null check for interpreted version eval, can you also fix that please?

null
} else {
map.valueArray().get(i, dataType)
Expand Down Expand Up @@ -307,7 +307,7 @@ case class GetMapValue(child: Expression, key: Expression)
}
}

if ($found) {
if ($found && !$eval1.valueArray().isNullAt($index)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we assign $eval1.valueArray() to a local variable?

${ev.value} = ${ctx.getValue(eval1 + ".valueArray()", dataType, index)};
} else {
${ev.isNull} = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1463,4 +1463,11 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
|FROM (SELECT '{"f1": "value1", "f2": 12}' json, 'hello' as str) test
""".stripMargin), Row("value1", "12", BigDecimal("3.14"), "hello"))
}

test("SPARK-13056: Null in map value causes NPE") {
Seq((1, "abc=somestring,cba")).toDF("key", "value").registerTempTable("mapsrc")
sql("""CREATE TABLE maptest AS SELECT str_to_map(value, ",", "=") as col1 FROM mapsrc""")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The str_to_map stuff makes this test a little hard to read, can we create a DataFrame by
val df = Seq(1 -> Map("a" -> "1", "b" -> null)).toDF("key", "value") and test it by DataFrames APIs directly?

checkAnswer(sql("SELECT col1['abc'] FROM maptest"), Row("somestring"))
checkAnswer(sql("SELECT col1['cba'] FROM maptest"), Row(null))
}
}