bozhang2820 commented on a change in pull request #30224:
URL: https://github.com/apache/spark/pull/30224#discussion_r517429166
##########
File path:
external/avro/src/test/scala/org/apache/spark/sql/avro/AvroSuite.scala
##########
@@ -1015,6 +1015,65 @@ abstract class AvroSuite
}
}
+ test("support user provided nullable avro schema " +
+ "for non-nullable catalyst schema without any null record") {
+ withTempPath { tempDir =>
+ val catalystSchema =
+ StructType(Seq(
+ StructField("Age", IntegerType, nullable = false),
+ StructField("Name", StringType, nullable = false)))
+
+ val avroSchema = """
+ |{
+ | "type" : "record",
+ | "name" : "test_schema",
+ | "fields" : [
+ | {"name": "Age", "type": ["null", "int"]},
+ | {"name": "Name", "type": ["null", "string"]}
+ | ]
+ |}
+ """.stripMargin
+
+ val df = spark.createDataFrame(
+ spark.sparkContext.parallelize(Seq(Row(2, "Aurora"))), catalystSchema)
+
+ df.write.format("avro").option("avroSchema",
avroSchema).save(tempDir.getPath)
+ checkAvroSchemaEquals(avroSchema,
getAvroSchemaStringFromFiles(tempDir.getPath))
+ }
+ }
+
+ test("unsupported nullable avro type") {
+ withTempPath { tempDir =>
+ val catalystSchema =
+ StructType(Seq(
+ StructField("Age", IntegerType, nullable = false),
+ StructField("Name", StringType, nullable = false)))
+
+ for (unsupportedAvroType <- Seq("""["null", "int", "long"]""",
"""["int", "long"]""")) {
+ val avroSchema = s"""
+ |{
+ | "type" : "record",
+ | "name" : "test_schema",
+ | "fields" : [
+ | {"name": "Age", "type": $unsupportedAvroType},
+ | {"name": "Name", "type": ["null", "string"]}
+ | ]
+ |}
+ """.stripMargin
+
+ val df = spark.createDataFrame(
+ spark.sparkContext.parallelize(Seq(Row(2, "Aurora"))),
catalystSchema)
+
+ val tempSavePath = s"$tempDir/save/${UUID.randomUUID()}"
Review comment:
I was following the pattern in the other tests in the same suite. Will
change to limit the scope of `withTempPath` in the two newly added tests.
----------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]