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 @@ -407,10 +407,13 @@ object ScalaReflection extends ScalaReflection {
val externalDataType = dataTypeFor(elementType)
val Schema(catalystType, nullable) = silentSchemaFor(elementType)
if (isNativeType(catalystType)) {
NewInstance(
classOf[GenericArrayData],
input :: Nil,
dataType = ArrayType(catalystType, nullable))
expressions.If(
IsNull(input),
expressions.Literal.create(null, ArrayType(catalystType, nullable)),
NewInstance(
classOf[GenericArrayData],
input :: Nil,
dataType = ArrayType(catalystType, nullable)))
Copy link
Contributor

Choose a reason for hiding this comment

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

A simple idea is to set propafateNull = true for NewInstance, and it's also fixed in #10443 which makes false as default value for propagateNull.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok. I think this can be closed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will #10443 be merged soon? I think I have another pr depending this fixing.

} else {
val clsName = getClassNameFromType(elementType)
val newPath = s"""- array element class: "$clsName"""" +: walkedTypePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.google.common.collect.MapMaker

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.Encoders
import org.apache.spark.sql.catalyst.ScalaReflection
import org.apache.spark.sql.catalyst.expressions.AttributeReference
import org.apache.spark.sql.catalyst.util.ArrayData
import org.apache.spark.sql.catalyst.{OptionalData, PrimitiveData}
Expand Down Expand Up @@ -239,6 +240,24 @@ class ExpressionEncoderSuite extends SparkFunSuite {
ExpressionEncoder.tuple(intEnc, ExpressionEncoder.tuple(intEnc, longEnc))
}

test("null as array") {
val data = Seq(
(Array[Int](2, 1, 3), Array("b", "c", "a")),
(Array[Int](), Array[String]()),
(null, null)
)

val schema = ScalaReflection.schemaFor[Tuple2[Array[Int], Array[String]]]
.dataType.asInstanceOf[StructType]
val attributeSeq = schema.toAttributes
val arrayDataEncoder = encoderFor[Tuple2[Array[Int], Array[String]]]
val boundEncoder = arrayDataEncoder.resolve(attributeSeq, outers).bind(attributeSeq)
data.foreach { x =>
val convertedBack = boundEncoder.fromRow(boundEncoder.toRow(x))
assert(convertedBack._1 === x._1 && convertedBack._2 === x._2)
}
}

test("nullable of encoder schema") {
def checkNullable[T: ExpressionEncoder](nullable: Boolean*): Unit = {
assert(implicitly[ExpressionEncoder[T]].schema.map(_.nullable) === nullable.toSeq)
Expand Down