forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-49954][SQL] Codegen Support for SchemaOfJson (by Invoke & Runt…
…imeReplaceable)
- Loading branch information
1 parent
c3176a7
commit 320fb0a
Showing
5 changed files
with
74 additions
and
25 deletions.
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
53 changes: 53 additions & 0 deletions
53
...c/main/scala/org/apache/spark/sql/catalyst/expressions/json/JsonExpressionEvalUtils.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,53 @@ | ||
/* | ||
* 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.catalyst.expressions.json | ||
|
||
import com.fasterxml.jackson.core.JsonFactory | ||
|
||
import org.apache.spark.sql.catalyst.json.{CreateJacksonParser, JsonInferSchema, JSONOptions} | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types.{ArrayType, DataType, StructType} | ||
import org.apache.spark.unsafe.types.UTF8String | ||
import org.apache.spark.util.Utils | ||
|
||
object JsonExpressionEvalUtils { | ||
|
||
def schemaOfJson( | ||
jsonFactory: JsonFactory, | ||
jsonOptions: JSONOptions, | ||
jsonInferSchema: JsonInferSchema, | ||
json: UTF8String): UTF8String = { | ||
val dt = Utils.tryWithResource(CreateJacksonParser.utf8String(jsonFactory, json)) { parser => | ||
parser.nextToken() | ||
// To match with schema inference from JSON datasource. | ||
jsonInferSchema.inferField(parser) match { | ||
case st: StructType => | ||
jsonInferSchema.canonicalizeType(st, jsonOptions).getOrElse(StructType(Nil)) | ||
case at: ArrayType if at.elementType.isInstanceOf[StructType] => | ||
jsonInferSchema | ||
.canonicalizeType(at.elementType, jsonOptions) | ||
.map(ArrayType(_, containsNull = at.containsNull)) | ||
.getOrElse(ArrayType(StructType(Nil), containsNull = at.containsNull)) | ||
case other: DataType => | ||
jsonInferSchema.canonicalizeType(other, jsonOptions).getOrElse( | ||
SQLConf.get.defaultStringType) | ||
} | ||
} | ||
|
||
UTF8String.fromString(dt.sql) | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...ect/common/src/test/resources/query-tests/explain-results/function_schema_of_json.explain
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Project [schema_of_json([{"col":01}]) AS schema_of_json([{"col":01}])#0] | ||
Project [static_invoke(JsonExpressionEvalUtils.schemaOfJson(com.fasterxml.jackson.core.JsonFactory, org.apache.spark.sql.catalyst.json.JSONOptions, org.apache.spark.sql.catalyst.json.JsonInferSchema, [{"col":01}])) AS schema_of_json([{"col":01}])#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |
2 changes: 1 addition & 1 deletion
2
...c/test/resources/query-tests/explain-results/function_schema_of_json_with_options.explain
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Project [schema_of_json([{"col":01}], (allowNumericLeadingZeros,true)) AS schema_of_json([{"col":01}])#0] | ||
Project [static_invoke(JsonExpressionEvalUtils.schemaOfJson(com.fasterxml.jackson.core.JsonFactory, org.apache.spark.sql.catalyst.json.JSONOptions, org.apache.spark.sql.catalyst.json.JsonInferSchema, [{"col":01}])) AS schema_of_json([{"col":01}])#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |