-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-13764][SQL] Parse modes in JSON data source #11756
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4c46f4b
Parse modes in JSON data source
HyukjinKwon 3675fae
Separate tests for parse modes
HyukjinKwon 87c3251
Do not infer _corrupt_record when the mode is DROPMALFORMED
HyukjinKwon 4440a55
Update comments for parse modes.
HyukjinKwon 32ae8b2
Add the missing description
HyukjinKwon de8d291
Update comments and add some more tests.
HyukjinKwon a563534
Correct missing ":" for comments and add missing schema comparison tests
HyukjinKwon 29a8f68
Extra space in comments
HyukjinKwon 551593a
Add extra ":" for scala one as well
HyukjinKwon bfc0405
Resolve conflicts
HyukjinKwon 59e7214
Change Logging to internal.Logging
HyukjinKwon 3ff900e
Reorder imports
HyukjinKwon dec3d81
Separate tests for each mode
HyukjinKwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ParseModes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.execution.datasources | ||
|
||
private[datasources] object ParseModes { | ||
val PERMISSIVE_MODE = "PERMISSIVE" | ||
val DROP_MALFORMED_MODE = "DROPMALFORMED" | ||
val FAIL_FAST_MODE = "FAILFAST" | ||
|
||
val DEFAULT = PERMISSIVE_MODE | ||
|
||
def isValidMode(mode: String): Boolean = { | ||
mode.toUpperCase match { | ||
case PERMISSIVE_MODE | DROP_MALFORMED_MODE | FAIL_FAST_MODE => true | ||
case _ => false | ||
} | ||
} | ||
|
||
def isDropMalformedMode(mode: String): Boolean = mode.toUpperCase == DROP_MALFORMED_MODE | ||
def isFailFastMode(mode: String): Boolean = mode.toUpperCase == FAIL_FAST_MODE | ||
def isPermissiveMode(mode: String): Boolean = if (isValidMode(mode)) { | ||
mode.toUpperCase == PERMISSIVE_MODE | ||
} else { | ||
true // We default to permissive is the mode string is not valid | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,8 @@ package org.apache.spark.sql.execution.datasources.json | |
|
||
import com.fasterxml.jackson.core.{JsonFactory, JsonParser} | ||
|
||
import org.apache.spark.sql.execution.datasources.CompressionCodecs | ||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.execution.datasources.{CompressionCodecs, ParseModes} | ||
|
||
/** | ||
* Options for the JSON data source. | ||
|
@@ -28,7 +29,7 @@ import org.apache.spark.sql.execution.datasources.CompressionCodecs | |
*/ | ||
private[sql] class JSONOptions( | ||
@transient private val parameters: Map[String, String]) | ||
extends Serializable { | ||
extends Logging with Serializable { | ||
|
||
val samplingRatio = | ||
parameters.get("samplingRatio").map(_.toDouble).getOrElse(1.0) | ||
|
@@ -49,6 +50,16 @@ private[sql] class JSONOptions( | |
val allowBackslashEscapingAnyCharacter = | ||
parameters.get("allowBackslashEscapingAnyCharacter").map(_.toBoolean).getOrElse(false) | ||
val compressionCodec = parameters.get("compression").map(CompressionCodecs.getCodecClassName) | ||
private val parseMode = parameters.getOrElse("mode", "PERMISSIVE") | ||
|
||
// Parse mode flags | ||
if (!ParseModes.isValidMode(parseMode)) { | ||
logWarning(s"$parseMode is not a valid parse mode. Using ${ParseModes.DEFAULT}.") | ||
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. #11756 (comment), That warning should be dealt with here. |
||
} | ||
|
||
val failFast = ParseModes.isFailFastMode(parseMode) | ||
val dropMalformed = ParseModes.isDropMalformedMode(parseMode) | ||
val permissive = ParseModes.isPermissiveMode(parseMode) | ||
|
||
/** Sets config options on a Jackson [[JsonFactory]]. */ | ||
def setJacksonOptions(factory: JsonFactory): Unit = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ import org.apache.hadoop.fs.{Path, PathFilter} | |
import org.apache.hadoop.io.SequenceFile.CompressionType | ||
import org.apache.hadoop.io.compress.GzipCodec | ||
|
||
import org.apache.spark.SparkException | ||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.sql._ | ||
import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
|
@@ -963,7 +964,56 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData { | |
) | ||
} | ||
|
||
test("Corrupt records") { | ||
test("Corrupt records: FAILFAST mode") { | ||
val schema = StructType( | ||
StructField("a", StringType, true) :: Nil) | ||
// `FAILFAST` mode should throw an exception for corrupt records. | ||
val exceptionOne = intercept[SparkException] { | ||
sqlContext.read | ||
.option("mode", "FAILFAST") | ||
.json(corruptRecords) | ||
.collect() | ||
} | ||
assert(exceptionOne.getMessage.contains("Malformed line in FAILFAST mode: {")) | ||
|
||
val exceptionTwo = intercept[SparkException] { | ||
sqlContext.read | ||
.option("mode", "FAILFAST") | ||
.schema(schema) | ||
.json(corruptRecords) | ||
.collect() | ||
} | ||
assert(exceptionTwo.getMessage.contains("Malformed line in FAILFAST mode: {")) | ||
} | ||
|
||
test("Corrupt records: DROPMALFORMED mode") { | ||
val schemaOne = StructType( | ||
StructField("a", StringType, true) :: | ||
StructField("b", StringType, true) :: | ||
StructField("c", StringType, true) :: Nil) | ||
val schemaTwo = StructType( | ||
StructField("a", StringType, true) :: Nil) | ||
// `DROPMALFORMED` mode should skip corrupt records | ||
val jsonDFOne = sqlContext.read | ||
.option("mode", "DROPMALFORMED") | ||
.json(corruptRecords) | ||
checkAnswer( | ||
jsonDFOne, | ||
Row("str_a_4", "str_b_4", "str_c_4") :: Nil | ||
) | ||
assert(jsonDFOne.schema === schemaOne) | ||
|
||
val jsonDFTwo = sqlContext.read | ||
.option("mode", "DROPMALFORMED") | ||
.schema(schemaTwo) | ||
.json(corruptRecords) | ||
checkAnswer( | ||
jsonDFTwo, | ||
Row("str_a_4") :: Nil) | ||
assert(jsonDFTwo.schema === schemaTwo) | ||
} | ||
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. Can we add more test cases with user specified schema? |
||
|
||
test("Corrupt records: PERMISSIVE mode") { | ||
// Test if we can query corrupt records. | ||
withSQLConf(SQLConf.COLUMN_NAME_OF_CORRUPT_RECORD.key -> "_unparsed") { | ||
withTempTable("jsonTable") { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should we log a warning for this case?