Github user marmbrus commented on a diff in the pull request:
https://github.com/apache/spark/pull/10391#discussion_r48314060
--- Diff:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/encoders/ExpressionEncoderSuite.scala
---
@@ -239,6 +240,30 @@ class ExpressionEncoderSuite extends SparkFunSuite {
ExpressionEncoder.tuple(intEnc, ExpressionEncoder.tuple(intEnc,
longEnc))
}
+ test("Option in Array") {
+ val data = (Seq(Some(1), Some(2), None), "Option in array")
+
+ val schema = ScalaReflection.schemaFor[Tuple2[Seq[Option[Int]],
String]]
+ .dataType.asInstanceOf[StructType]
+ val attributeSeq = schema.toAttributes
+ val dataEncoder = encoderFor[Tuple2[Seq[Option[Int]], String]]
+ val boundEncoder = dataEncoder.resolve(attributeSeq,
outers).bind(attributeSeq)
+ assert(boundEncoder.fromRow(boundEncoder.toRow(data)) === (
+ Seq(Some(1), Some(2), null), "Option in array"))
+ }
+
+ test("Option in Map") {
+ val data = (Map(1 -> Some(10L), 2 -> Some(20L), 3 -> None), "Option in
map")
+
+ val schema = ScalaReflection.schemaFor[Tuple2[Map[Int, Option[Long]],
String]]
+ .dataType.asInstanceOf[StructType]
+ val attributeSeq = schema.toAttributes
+ val dataEncoder = encoderFor[Tuple2[Map[Int, Option[Long]], String]]
+ val boundEncoder = dataEncoder.resolve(attributeSeq,
outers).bind(attributeSeq)
+ assert(boundEncoder.fromRow(boundEncoder.toRow(data)) === (
+ Map(1 -> Some(10L), 2 -> Some(20L), 3 -> null), "Option in map"))
+ }
--- End diff --
The original implementation in `ScalaReflection` only had access to the
schema as a DataType when it was going to produce external `Row` objects. This
schema is erased in that it can't differentiate `Option` from `null`. This is
not true for encoders since we know the full type the user is expecting from
the provided `TypeTag`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]