Skip to content

Commit

Permalink
[SPARK-10197] [SQL] Add null check in wrapperFor (inside HiveInspecto…
Browse files Browse the repository at this point in the history
…rs).

https://issues.apache.org/jira/browse/SPARK-10197

Author: Yin Huai <[email protected]>

Closes #8407 from yhuai/ORCSPARK-10197.

(cherry picked from commit 0e6368f)
Signed-off-by: Cheng Lian <[email protected]>
  • Loading branch information
yhuai authored and liancheng committed Aug 25, 2015
1 parent a0f22cf commit 73f1dd1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,36 @@ private[hive] trait HiveInspectors {
protected def wrapperFor(oi: ObjectInspector, dataType: DataType): Any => Any = oi match {
case _: JavaHiveVarcharObjectInspector =>
(o: Any) =>
val s = o.asInstanceOf[UTF8String].toString
new HiveVarchar(s, s.size)
if (o != null) {
val s = o.asInstanceOf[UTF8String].toString
new HiveVarchar(s, s.size)
} else {
null
}

case _: JavaHiveDecimalObjectInspector =>
(o: Any) => HiveDecimal.create(o.asInstanceOf[Decimal].toJavaBigDecimal)
(o: Any) =>
if (o != null) {
HiveDecimal.create(o.asInstanceOf[Decimal].toJavaBigDecimal)
} else {
null
}

case _: JavaDateObjectInspector =>
(o: Any) => DateTimeUtils.toJavaDate(o.asInstanceOf[Int])
(o: Any) =>
if (o != null) {
DateTimeUtils.toJavaDate(o.asInstanceOf[Int])
} else {
null
}

case _: JavaTimestampObjectInspector =>
(o: Any) => DateTimeUtils.toJavaTimestamp(o.asInstanceOf[Long])
(o: Any) =>
if (o != null) {
DateTimeUtils.toJavaTimestamp(o.asInstanceOf[Long])
} else {
null
}

case soi: StandardStructObjectInspector =>
val schema = dataType.asInstanceOf[StructType]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ abstract class OrcSuite extends QueryTest with BeforeAndAfterAll {
sql("SELECT * FROM normal_orc_as_source"),
(6 to 10).map(i => Row(i, s"part-$i")))
}

test("write null values") {
sql("DROP TABLE IF EXISTS orcNullValues")

val df = sql(
"""
|SELECT
| CAST(null as TINYINT),
| CAST(null as SMALLINT),
| CAST(null as INT),
| CAST(null as BIGINT),
| CAST(null as FLOAT),
| CAST(null as DOUBLE),
| CAST(null as DECIMAL(7,2)),
| CAST(null as TIMESTAMP),
| CAST(null as DATE),
| CAST(null as STRING),
| CAST(null as VARCHAR(10))
|FROM orc_temp_table limit 1
""".stripMargin)

df.write.format("orc").saveAsTable("orcNullValues")

checkAnswer(
sql("SELECT * FROM orcNullValues"),
Row.fromSeq(Seq.fill(11)(null)))

sql("DROP TABLE IF EXISTS orcNullValues")
}
}

class OrcSourceSuite extends OrcSuite {
Expand Down

0 comments on commit 73f1dd1

Please sign in to comment.