Skip to content

Commit

Permalink
[SPARK-11908][SQL] Add NullType support to RowEncoder
Browse files Browse the repository at this point in the history
JIRA: https://issues.apache.org/jira/browse/SPARK-11908

We should add NullType support to RowEncoder.

Author: Liang-Chi Hsieh <viirya@appier.com>

Closes #9891 from viirya/rowencoder-nulltype.
  • Loading branch information
viirya authored and marmbrus committed Nov 22, 2015
1 parent ff442bb commit 426004a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object RowEncoder {
private def extractorsFor(
inputObject: Expression,
inputType: DataType): Expression = inputType match {
case BooleanType | ByteType | ShortType | IntegerType | LongType |
case NullType | BooleanType | ByteType | ShortType | IntegerType | LongType |
FloatType | DoubleType | BinaryType => inputObject

case udt: UserDefinedType[_] =>
Expand Down Expand Up @@ -143,6 +143,7 @@ object RowEncoder {
case _: MapType => ObjectType(classOf[scala.collection.Map[_, _]])
case _: StructType => ObjectType(classOf[Row])
case udt: UserDefinedType[_] => ObjectType(udt.userClass)
case _: NullType => ObjectType(classOf[java.lang.Object])
}

private def constructorFor(schema: StructType): Expression = {
Expand All @@ -158,7 +159,7 @@ object RowEncoder {
}

private def constructorFor(input: Expression): Expression = input.dataType match {
case BooleanType | ByteType | ShortType | IntegerType | LongType |
case NullType | BooleanType | ByteType | ShortType | IntegerType | LongType |
FloatType | DoubleType | BinaryType => input

case udt: UserDefinedType[_] =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ case class MapObjects(
private lazy val completeFunction = function(loopAttribute)

private def itemAccessorMethod(dataType: DataType): String => String = dataType match {
case NullType =>
val nullTypeClassName = NullType.getClass.getName + ".MODULE$"
(i: String) => s".get($i, $nullTypeClassName)"
case IntegerType => (i: String) => s".getInt($i)"
case LongType => (i: String) => s".getLong($i)"
case FloatType => (i: String) => s".getFloat($i)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ class RowEncoderSuite extends SparkFunSuite {
private val structOfString = new StructType().add("str", StringType)
private val structOfUDT = new StructType().add("udt", new ExamplePointUDT, false)
private val arrayOfString = ArrayType(StringType)
private val arrayOfNull = ArrayType(NullType)
private val mapOfString = MapType(StringType, StringType)
private val arrayOfUDT = ArrayType(new ExamplePointUDT, false)

encodeDecodeTest(
new StructType()
.add("null", NullType)
.add("boolean", BooleanType)
.add("byte", ByteType)
.add("short", ShortType)
Expand All @@ -101,6 +103,7 @@ class RowEncoderSuite extends SparkFunSuite {

encodeDecodeTest(
new StructType()
.add("arrayOfNull", arrayOfNull)
.add("arrayOfString", arrayOfString)
.add("arrayOfArrayOfString", ArrayType(arrayOfString))
.add("arrayOfArrayOfInt", ArrayType(ArrayType(IntegerType)))
Expand Down

0 comments on commit 426004a

Please sign in to comment.