Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -346,16 +346,20 @@ private[hive] trait HiveInspectors {
case soi: StandardStructObjectInspector =>
val wrappers = soi.getAllStructFieldRefs.map(ref => wrapperFor(ref.getFieldObjectInspector))
(o: Any) => {
val struct = soi.create()
(soi.getAllStructFieldRefs, wrappers, o.asInstanceOf[Row]).zipped.foreach {
(field, wrapper, data) => soi.setStructFieldData(struct, field, wrapper(data))
if (o != null) {
val struct = soi.create()
(soi.getAllStructFieldRefs, wrappers, o.asInstanceOf[Row]).zipped.foreach {
(field, wrapper, data) => soi.setStructFieldData(struct, field, wrapper(data))
}
struct
} else {
null
}
struct
}

case loi: ListObjectInspector =>
val wrapper = wrapperFor(loi.getListElementObjectInspector)
(o: Any) => seqAsJavaList(o.asInstanceOf[Seq[_]].map(wrapper))
(o: Any) => if (o != null) seqAsJavaList(o.asInstanceOf[Seq[_]].map(wrapper)) else null

case moi: MapObjectInspector =>
// The Predef.Map is scala.collection.immutable.Map.
Expand All @@ -364,9 +368,15 @@ private[hive] trait HiveInspectors {

val keyWrapper = wrapperFor(moi.getMapKeyObjectInspector)
val valueWrapper = wrapperFor(moi.getMapValueObjectInspector)
(o: Any) => mapAsJavaMap(o.asInstanceOf[Map[_, _]].map { case (key, value) =>
keyWrapper(key) -> valueWrapper(value)
})
(o: Any) => {
if (o != null) {
mapAsJavaMap(o.asInstanceOf[Map[_, _]].map { case (key, value) =>
keyWrapper(key) -> valueWrapper(value)
})
} else {
null
}
}

case _ =>
identity[Any]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.spark.sql.QueryTest

import org.apache.spark.sql.Row
import org.apache.spark.sql.hive.test.TestHive._
import org.apache.spark.util.Utils
import org.apache.spark.sql.types._

case class Nested1(f1: Nested2)
case class Nested2(f2: Nested3)
Expand Down Expand Up @@ -214,4 +214,39 @@ class SQLQuerySuite extends QueryTest {
Seq.empty[Row])
}
}

test("SPARK-5284 Insert into Hive throws NPE when a inner complex type field has a null value") {
val schema = StructType(
StructField("s",
StructType(
StructField("innerStruct", StructType(StructField("s1", StringType, true) :: Nil)) ::
StructField("innerArray", ArrayType(IntegerType), true) ::
StructField("innerMap", MapType(StringType, IntegerType)) :: Nil), true) :: Nil)
val row = Row(Row(null, null, null))

val rowRdd = sparkContext.parallelize(row :: Nil)

applySchema(rowRdd, schema).registerTempTable("testTable")

sql(
"""CREATE TABLE nullValuesInInnerComplexTypes
| (s struct<innerStruct: struct<s1:string>,
| innerArray:array<int>,
| innerMap: map<string, int>>)
""".stripMargin).collect

sql(
"""
|INSERT OVERWRITE TABLE nullValuesInInnerComplexTypes
|SELECT * FROM testTable
""".stripMargin)

checkAnswer(
sql("SELECT * FROM nullValuesInInnerComplexTypes"),
Seq(Seq(Seq(null, null, null)))
)

sql("DROP TABLE nullValuesInInnerComplexTypes")
dropTempTable("testTable")
}
}