gengliangwang commented on code in PR #53041:
URL: https://github.com/apache/spark/pull/53041#discussion_r2525056355
##########
connector/avro/src/test/scala/org/apache/spark/sql/avro/AvroSuite.scala:
##########
@@ -1423,6 +1423,56 @@ abstract class AvroSuite
}
}
+ test("to_avro with reordered fields and nullable target succeeds") {
+ // Test that when Catalyst and Avro field orders differ, null values
+ // are correctly validated against the mapped Avro field's nullability
+ val avroSchema = """{
+ "type": "record",
+ "name": "ReorderedRecord",
+ "fields": [
+ {"name": "a", "type": ["null", "string"]},
+ {"name": "b", "type": "string"}
+ ]
+ }"""
+
+ // Catalyst has fields in order [b, a], Avro has [a, b]
+ // Pass null for 'a' which is nullable in Avro - should succeed
+ val df = Seq(("B", null.asInstanceOf[String])).toDF("b", "a")
+ .select(struct($"b", $"a").as("s"))
+ val result = df.select(functions.to_avro($"s", avroSchema).as("avro"))
+
+ // Should succeed without throwing AVRO_CANNOT_WRITE_NULL_FIELD
+ val collected = result.collect()
+ assert(collected.length == 1)
Review Comment:
let's use `checkAnswer` to verify the result data
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]