-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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-5938][SPARK-5443][SQL] Improve JsonRDD performance #5801
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7ca70c1
Eliminate arrow pattern, replace with pattern matches
0bbc445
Improve JSON parsing and type inference performance
f636c14
Enable JsonRDD2 by default, add a flag to switch back to JsonRDD
ab6ee87
Add projection pushdown support to JsonRDD/JsonRDD2
842846d
Point the empty schema inference test at JsonRDD2
80dba17
Add comments regarding null handling and empty strings
fa0be47
Remove unused default case in the field parser
f8add6e
Add comments on lack of support for precision and scale DecimalTypes
15c5d1b
JSONRelation's baseRDD need not be lazy
b31917b
Rename `useJsonRDD2` to `useJacksonStreamingAPI`
fa8234f
Wrap long lines
6822712
Split up JsonRDD2 into multiple objects
e06a1dd
Add comments to the `useJacksonStreamingAPI` config flag
a7ebeb2
Increase coverage of inserts into a JSONRelation
26fea31
Recreate the baseRDD each for each scan operation
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
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
171 changes: 171 additions & 0 deletions
171
sql/core/src/main/scala/org/apache/spark/sql/json/InferSchema.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,171 @@ | ||
/* | ||
* 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.json | ||
|
||
import com.fasterxml.jackson.core._ | ||
|
||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.sql.catalyst.analysis.HiveTypeCoercion | ||
import org.apache.spark.sql.json.JacksonUtils.nextUntil | ||
import org.apache.spark.sql.types._ | ||
|
||
private[sql] object InferSchema { | ||
/** | ||
* Infer the type of a collection of json records in three stages: | ||
* 1. Infer the type of each record | ||
* 2. Merge types by choosing the lowest type necessary to cover equal keys | ||
* 3. Replace any remaining null fields with string, the top type | ||
*/ | ||
def apply( | ||
json: RDD[String], | ||
samplingRatio: Double = 1.0, | ||
columnNameOfCorruptRecords: String): StructType = { | ||
require(samplingRatio > 0, s"samplingRatio ($samplingRatio) should be greater than 0") | ||
val schemaData = if (samplingRatio > 0.99) { | ||
json | ||
} else { | ||
json.sample(withReplacement = false, samplingRatio, 1) | ||
} | ||
|
||
// perform schema inference on each row and merge afterwards | ||
schemaData.mapPartitions { iter => | ||
val factory = new JsonFactory() | ||
iter.map { row => | ||
try { | ||
val parser = factory.createParser(row) | ||
parser.nextToken() | ||
inferField(parser) | ||
} catch { | ||
case _: JsonParseException => | ||
StructType(Seq(StructField(columnNameOfCorruptRecords, StringType))) | ||
} | ||
} | ||
}.treeAggregate[DataType](StructType(Seq()))(compatibleRootType, compatibleRootType) match { | ||
case st: StructType => nullTypeToStringType(st) | ||
} | ||
} | ||
|
||
/** | ||
* Infer the type of a json document from the parser's token stream | ||
*/ | ||
private def inferField(parser: JsonParser): DataType = { | ||
import com.fasterxml.jackson.core.JsonToken._ | ||
parser.getCurrentToken match { | ||
case null | VALUE_NULL => NullType | ||
|
||
case FIELD_NAME => | ||
parser.nextToken() | ||
inferField(parser) | ||
|
||
case VALUE_STRING if parser.getTextLength < 1 => | ||
// Zero length strings and nulls have special handling to deal | ||
// with JSON generators that do not distinguish between the two. | ||
// To accurately infer types for empty strings that are really | ||
// meant to represent nulls we assume that the two are isomorphic | ||
// but will defer treating null fields as strings until all the | ||
// record fields' types have been combined. | ||
NullType | ||
|
||
case VALUE_STRING => StringType | ||
case START_OBJECT => | ||
val builder = Seq.newBuilder[StructField] | ||
while (nextUntil(parser, END_OBJECT)) { | ||
builder += StructField(parser.getCurrentName, inferField(parser), nullable = true) | ||
} | ||
|
||
StructType(builder.result().sortBy(_.name)) | ||
|
||
case START_ARRAY => | ||
// If this JSON array is empty, we use NullType as a placeholder. | ||
// If this array is not empty in other JSON objects, we can resolve | ||
// the type as we pass through all JSON objects. | ||
var elementType: DataType = NullType | ||
while (nextUntil(parser, END_ARRAY)) { | ||
elementType = compatibleType(elementType, inferField(parser)) | ||
} | ||
|
||
ArrayType(elementType) | ||
|
||
case VALUE_NUMBER_INT | VALUE_NUMBER_FLOAT => | ||
import JsonParser.NumberType._ | ||
parser.getNumberType match { | ||
// For Integer values, use LongType by default. | ||
case INT | LONG => LongType | ||
// Since we do not have a data type backed by BigInteger, | ||
// when we see a Java BigInteger, we use DecimalType. | ||
case BIG_INTEGER | BIG_DECIMAL => DecimalType.Unlimited | ||
case FLOAT | DOUBLE => DoubleType | ||
} | ||
|
||
case VALUE_TRUE | VALUE_FALSE => BooleanType | ||
} | ||
} | ||
|
||
private def nullTypeToStringType(struct: StructType): StructType = { | ||
val fields = struct.fields.map { | ||
case StructField(fieldName, dataType, nullable, _) => | ||
val newType = dataType match { | ||
case NullType => StringType | ||
case ArrayType(NullType, containsNull) => ArrayType(StringType, containsNull) | ||
case ArrayType(struct: StructType, containsNull) => | ||
ArrayType(nullTypeToStringType(struct), containsNull) | ||
case struct: StructType =>nullTypeToStringType(struct) | ||
case other: DataType => other | ||
} | ||
|
||
StructField(fieldName, newType, nullable) | ||
} | ||
|
||
StructType(fields) | ||
} | ||
|
||
/** | ||
* Remove top-level ArrayType wrappers and merge the remaining schemas | ||
*/ | ||
private def compatibleRootType: (DataType, DataType) => DataType = { | ||
case (ArrayType(ty1, _), ty2) => compatibleRootType(ty1, ty2) | ||
case (ty1, ArrayType(ty2, _)) => compatibleRootType(ty1, ty2) | ||
case (ty1, ty2) => compatibleType(ty1, ty2) | ||
} | ||
|
||
/** | ||
* Returns the most general data type for two given data types. | ||
*/ | ||
private[json] def compatibleType(t1: DataType, t2: DataType): DataType = { | ||
HiveTypeCoercion.findTightestCommonType(t1, t2).getOrElse { | ||
// t1 or t2 is a StructType, ArrayType, or an unexpected type. | ||
(t1, t2) match { | ||
case (other: DataType, NullType) => other | ||
case (NullType, other: DataType) => other | ||
case (StructType(fields1), StructType(fields2)) => | ||
val newFields = (fields1 ++ fields2).groupBy(field => field.name).map { | ||
case (name, fieldTypes) => | ||
val dataType = fieldTypes.view.map(_.dataType).reduce(compatibleType) | ||
StructField(name, dataType, nullable = true) | ||
} | ||
StructType(newFields.toSeq.sortBy(_.name)) | ||
|
||
case (ArrayType(elementType1, containsNull1), ArrayType(elementType2, containsNull2)) => | ||
ArrayType(compatibleType(elementType1, elementType2), containsNull1 || containsNull2) | ||
|
||
// strings and every string is a Json object. | ||
case (_, _) => StringType | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Can you add comment to explain that it is a temporary flag and we will remove the old code path in 1.5?