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 6 commits
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 @@ -218,7 +218,7 @@ case class GetArrayItem(child: Expression, ordinal: Expression)
protected override def nullSafeEval(value: Any, ordinal: Any): Any = {
val baseValue = value.asInstanceOf[ArrayData]
val index = ordinal.asInstanceOf[Number].intValue()
if (index >= baseValue.numElements() || index < 0) {
if (index >= baseValue.numElements() || index < 0 || baseValue.isNullAt(index)) {
null
} else {
baseValue.get(index, dataType)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just realized it is ok to let it return null here, without the condition above.

Copy link
Contributor

Choose a reason for hiding this comment

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

yea, it's ok, but having it makes the interpreted version and codegen version more consistent, which is good.

Expand Down Expand Up @@ -267,6 +267,7 @@ case class GetMapValue(child: Expression, key: Expression)
val map = value.asInstanceOf[MapData]
val length = map.numElements()
val keys = map.keyArray()
val values = map.valueArray()

var i = 0
var found = false
Expand All @@ -278,10 +279,10 @@ case class GetMapValue(child: Expression, key: Expression)
}
}

if (!found) {
if (!found || values.isNullAt(i)) {
null
} else {
map.valueArray().get(i, dataType)
values.get(i, dataType)
}
}

Expand All @@ -291,10 +292,12 @@ case class GetMapValue(child: Expression, key: Expression)
val keys = ctx.freshName("keys")
val found = ctx.freshName("found")
val key = ctx.freshName("key")
val values = ctx.freshName("values")
nullSafeCodeGen(ctx, ev, (eval1, eval2) => {
s"""
final int $length = $eval1.numElements();
final ArrayData $keys = $eval1.keyArray();
final ArrayData $values = $eval1.valueArray();

int $index = 0;
boolean $found = false;
Expand All @@ -307,10 +310,10 @@ case class GetMapValue(child: Expression, key: Expression)
}
}

if ($found) {
${ev.value} = ${ctx.getValue(eval1 + ".valueArray()", dataType, index)};
} else {
if (!$found || $values.isNullAt($index)) {
${ev.isNull} = true;
} else {
${ev.value} = ${ctx.getValue(values, dataType, index)};
}
"""
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,15 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
)
}

test("SPARK-13056: Null in map value causes NPE") {
val df = Seq(1 -> Map("abc" -> "somestring", "cba" -> null)).toDF("key", "value")
withTempTable("maptest") {
df.registerTempTable("maptest")
checkAnswer(sql("SELECT value['abc'] FROM maptest where key = 1"), Row("somestring"))
Copy link
Contributor

Choose a reason for hiding this comment

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

As I explained before, the problem is local optimization: #10964 (comment) , so adding a filter here do fixes the problem, by breaking the local optimization, and we should add comments to say it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right

checkAnswer(sql("SELECT value['cba'] FROM maptest where key = 1"), Row(null))
}
}

test("hash function") {
val df = Seq(1 -> "a", 2 -> "b").toDF("i", "j")
withTempTable("tbl") {
Expand Down