-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19641][SQL] JSON schema inference in DROPMALFORMED mode produces incorrect schema for non-array/object JSONs #17492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1041,7 +1041,6 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData { | |
| spark.read | ||
| .option("mode", "FAILFAST") | ||
| .json(corruptRecords) | ||
| .collect() | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I intended to remove this line to make sure it happens in schrma inference. I think this is not related with the change here. |
||
| } | ||
| assert(exceptionOne.getMessage.contains("JsonParseException")) | ||
|
|
||
|
|
@@ -1082,6 +1081,18 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData { | |
| assert(jsonDFTwo.schema === schemaTwo) | ||
| } | ||
|
|
||
| test("SPARK-19641: Additional corrupt records: DROPMALFORMED mode") { | ||
| val schema = new StructType().add("dummy", StringType) | ||
| // `DROPMALFORMED` mode should skip corrupt records | ||
| val jsonDF = spark.read | ||
| .option("mode", "DROPMALFORMED") | ||
| .json(additionalCorruptRecords) | ||
| checkAnswer( | ||
| jsonDF, | ||
| Row("test")) | ||
| assert(jsonDF.schema === schema) | ||
| } | ||
|
|
||
| test("Corrupt records: PERMISSIVE mode, without designated column for malformed records") { | ||
| val schema = StructType( | ||
| StructField("a", StringType, true) :: | ||
|
|
@@ -1882,6 +1893,24 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData { | |
| } | ||
| } | ||
|
|
||
| test("SPARK-19641: Handle multi-line corrupt documents (DROPMALFORMED)") { | ||
| withTempPath { dir => | ||
| val path = dir.getCanonicalPath | ||
| val corruptRecordCount = additionalCorruptRecords.count().toInt | ||
| assert(corruptRecordCount === 5) | ||
|
|
||
| additionalCorruptRecords | ||
| .toDF("value") | ||
| // this is the minimum partition count that avoids hash collisions | ||
| .repartition(corruptRecordCount * 4, F.hash($"value")) | ||
| .write | ||
| .text(path) | ||
|
|
||
| val jsonDF = spark.read.option("wholeFile", true).option("mode", "DROPMALFORMED").json(path) | ||
| checkAnswer(jsonDF, Seq(Row("test"))) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-18352: Handle multi-line corrupt documents (FAILFAST)") { | ||
| withTempPath { dir => | ||
| val path = dir.getCanonicalPath | ||
|
|
@@ -1903,9 +1932,8 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData { | |
| .option("wholeFile", true) | ||
| .option("mode", "FAILFAST") | ||
| .json(path) | ||
| .collect() | ||
| } | ||
| assert(exceptionOne.getMessage.contains("Failed to parse a value")) | ||
| assert(exceptionOne.getMessage.contains("Failed to infer a common schema")) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Up to my knowledge, this test case throws an exception when actually parsing the data before. Now, I intended to check the exception in schema inference as it looks parsing data case is covered below. |
||
|
|
||
| val exceptionTwo = intercept[SparkException] { | ||
| spark.read | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When will we hit this branch? Seems never?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks possible to run this line. I added a test in 1936L in
JsonSuite. In more details, if the json is a valid one but not a object of an areay of object, it will infer notStructTypeper record. If one of the types is not a struct type and failfast mode is enabled, we will hit this line.I just checked as below: