cloud-fan commented on a change in pull request #33212:
URL: https://github.com/apache/spark/pull/33212#discussion_r669634117
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/json/JsonSuite.scala
##########
@@ -2924,6 +2924,55 @@ abstract class JsonSuite
}
}
}
+
+ test("SPARK-35912: nullability with different parse mode -- struct") {
+ // JSON field is missing.
+ val missingFieldInput = """{"c1": 1}"""
+ // JSON filed is null.
+ val nullValueInput = """{"c1": 1, "c2": null}"""
+
+ val load = (mode: String, schema: StructType, inputJson: String) => {
+ val json = spark.createDataset(
+ spark.sparkContext.parallelize(inputJson :: Nil))(Encoders.STRING)
+ spark.read
+ .option("mode", mode)
+ .schema(schema)
+ .json(json)
+ }
+
+ Seq(true, false).foreach { nullable =>
+ val schema = StructType(Seq(
+ StructField("c1", IntegerType, nullable = true),
+ StructField("c2", IntegerType, nullable = nullable)))
+
+ Seq(missingFieldInput, nullValueInput).foreach { jsonString =>
+ if (nullable) {
+ checkAnswer(load("DROPMALFORMED", schema, jsonString), Row(1, null)
:: Nil)
+ checkAnswer(load("FAILFAST", schema, jsonString), Row(1, null) ::
Nil)
+ checkAnswer(load("PERMISSIVE", schema, jsonString), Row(1, null) ::
Nil)
+ } else {
+ checkAnswer(load("DROPMALFORMED", schema, jsonString), Seq.empty)
+
+ val exceptionMsg1 = intercept[SparkException] {
+ load("FAILFAST", schema, jsonString).collect
+ }.getMessage
+ val expectedMsg1 = if (jsonString == missingFieldInput) {
+ "field c2 is not nullable but it's missing in one record."
+ } else {
+ s"field c2 is not nullable but the parsed value is null."
+ }
+ assert(exceptionMsg1.contains(expectedMsg1))
+
+ val exceptionMsg2 = intercept[SparkException] {
+ load("PERMISSIVE", schema, jsonString).collect
+ }
+ val expectedMsg2 =
+ "Field c2 is not nullable but PERMISSIVE mode only works with
nullable fields."
Review comment:
Can we add a migration guide for it? This is a breaking change as
`PERMISSIVE` is the default mode.
Also cc @HyukjinKwon for this breaking change.
--
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]