Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 @@ -20,6 +20,12 @@ package org.apache.spark.sql
import java.sql.{Date, Timestamp}

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.ScalaReflection
import org.apache.spark.sql.catalyst.encoders._
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.catalyst.expressions.UnsafeRow
import org.apache.spark.sql.catalyst.plans.logical.LocalRelation
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.test.SharedSQLContext

case class ReflectData(
Expand Down Expand Up @@ -138,4 +144,23 @@ class ScalaReflectionRelationSuite extends SparkFunSuite with SharedSQLContext {
Map(10 -> 100L, 20 -> 200L, 30 -> null),
Row(null, "abc"))))
}

test("null as array") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this test be done with the encoder tests?

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 unsafeRows = data.map(arrayDataEncoder.toRow(_).copy())
val df = DataFrame(sqlContext, LocalRelation(attributeSeq, unsafeRows))
assert(df.collect() === Seq(
Row(Seq(2, 1, 3), Seq("b", "c", "a")),
Row(Seq[Int](), Seq[String]()),
Row(null, null)))
}
}