bozhang2820 commented on a change in pull request #30224:
URL: https://github.com/apache/spark/pull/30224#discussion_r517296172



##########
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:
       This is required here so we have different save paths for `["null", 
"int", "long"]` and `["int", "long"]`.




----------------------------------------------------------------
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:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to