dongjoon-hyun commented on a change in pull request #24722: [External/Avro] Fix
for avro deserialization on union types with multiple non-null types
URL: https://github.com/apache/spark/pull/24722#discussion_r287896437
##########
File path:
external/avro/src/test/scala/org/apache/spark/sql/avro/AvroSuite.scala
##########
@@ -247,6 +247,32 @@ class AvroSuite extends QueryTest with SharedSQLContext
with SQLTestUtils {
}
}
+ test("Union type: More than one non-null type") {
+ withTempDir { dir =>
+ val complexNullUnionType = Schema.createUnion(
+ List(Schema.create(Type.INT), Schema.create(Type.NULL),
Schema.create(Type.STRING)).asJava)
+ val fields = Seq(
+ new Field("field1", complexNullUnionType, "doc",
null.asInstanceOf[AnyVal])).asJava
+ val schema = Schema.createRecord("name", "docs", "namespace", false)
+ schema.setFields(fields)
+ val datumWriter = new GenericDatumWriter[GenericRecord](schema)
+ val dataFileWriter = new DataFileWriter[GenericRecord](datumWriter)
+ dataFileWriter.create(schema, new File(s"$dir.avro"))
+ val avroRec = new GenericData.Record(schema)
+ avroRec.put("field1", 42)
+ dataFileWriter.append(avroRec)
+ val avroRec2 = new GenericData.Record(schema)
+ avroRec2.put("field1", "Alice")
+ dataFileWriter.append(avroRec2)
+ dataFileWriter.flush()
+ dataFileWriter.close()
+
+ val df = spark.read.format("avro").load(s"$dir.avro")
+ assertResult(42)(df.selectExpr("field1.member0").take(1)(0).get(0))
+
assertResult("Alice")(df.selectExpr("field1.member1").take(2).drop(1)(0).get(0))
Review comment:
Could you replace the above two lines like the following?
```scala
- assertResult(42)(df.selectExpr("field1.member0").take(1)(0).get(0))
-
assertResult("Alice")(df.selectExpr("field1.member1").take(2).drop(1)(0).get(0))
+ assert(df.schema === StructType.fromDDL("field1 struct<member0: int,
member1: string>"))
+ assert(df.collect().toSet == Set(Row(Row(42, null)), Row(Row(null,
"Alice"))))
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]