Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -2056,4 +2056,11 @@ 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")
df.registerTempTable("maptest")
Copy link
Contributor

Choose a reason for hiding this comment

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

use withTempTable to delete the table after test.

checkAnswer(sql("SELECT value['abc'] FROM maptest"), Row("somestring"))
checkAnswer(sql("SELECT value['cba'] FROM maptest"), Row(null))
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you verified if this throws NPE without this fix ? The test runs fine over trunk:

scala> val df = Seq(1 -> Map("abc" -> "somestring", "cba" -> null)).toDF("key", "value")
df: org.apache.spark.sql.DataFrame = [key: int, value: map<string,string>]

scala> df.registerTempTable("maptest")

scala> sqlContext.sql("SELECT value['cba'] FROM maptest").collect()
res28: Array[org.apache.spark.sql.Row] = Array([null])

scala> sqlContext.sql("SELECT value['cba'] FROM maptest").foreach(println)
[null]

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to use RDD instead of Seq to build the DataFrame, or the local optimization will evaluate it directly, without codegen.

Copy link
Contributor

Choose a reason for hiding this comment

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

You need to modify the test case:

scala> sqlContext.sql("SELECT value['cba'] FROM maptest WHERE key = 1").collect()
16/01/31 21:22:13 ERROR Executor: Exception in task 15.0 in stage 2.0 (TID 47)
java.lang.NullPointerException
    at org.apache.spark.sql.catalyst.expressions.UnsafeRowWriters$UTF8StringWriter.getSize(UnsafeRowWriters.java:90)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.apply(Unknown Source)
    at org.apache.spark.sql.execution.TungstenProject$$anonfun$3$$anonfun$apply$3.apply(basicOperators.scala:90)
    at org.apache.spark.sql.execution.TungstenProject$$anonfun$3$$anonfun$apply$3.apply(basicOperators.scala:88)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
    at scala.collection.Iterator$class.foreach(Iterator.scala:727)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:103)....
....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tejasapatil OK, actually I used a udf to generate the map and got your exception, later I changed to this following the guide from @cloud-fan but didn't verify it myself. I'll modify the test case here, Thanks!

}
}