forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-26246][SQL] Inferring TimestampType from JSON
## What changes were proposed in this pull request? The `JsonInferSchema` class is extended to support `TimestampType` inferring from string fields in JSON input: - If the `prefersDecimal` option is set to `true`, it tries to infer decimal type from the string field. - If decimal type inference fails or `prefersDecimal` is disabled, `JsonInferSchema` tries to infer `TimestampType`. - If timestamp type inference fails, `StringType` is returned as the inferred type. ## How was this patch tested? Added new test suite - `JsonInferSchemaSuite` to check date and timestamp types inferring from JSON using `JsonInferSchema` directly. A few tests were added `JsonSuite` to check type merging and roundtrip tests. This changes was tested by `JsonSuite`, `JsonExpressionsSuite` and `JsonFunctionsSuite` as well. Closes apache#23201 from MaxGekk/json-infer-time. Lead-authored-by: Maxim Gekk <[email protected]> Co-authored-by: Maxim Gekk <[email protected]> Signed-off-by: Hyukjin Kwon <[email protected]>
- Loading branch information
1 parent
86100df
commit d72571e
Showing
3 changed files
with
171 additions
and
5 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
102 changes: 102 additions & 0 deletions
102
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/json/JsonInferSchemaSuite.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,102 @@ | ||
/* | ||
* 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.json | ||
|
||
import com.fasterxml.jackson.core.JsonFactory | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.catalyst.plans.SQLHelper | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types._ | ||
|
||
class JsonInferSchemaSuite extends SparkFunSuite with SQLHelper { | ||
|
||
def checkType(options: Map[String, String], json: String, dt: DataType): Unit = { | ||
val jsonOptions = new JSONOptions(options, "UTC", "") | ||
val inferSchema = new JsonInferSchema(jsonOptions) | ||
val factory = new JsonFactory() | ||
jsonOptions.setJacksonOptions(factory) | ||
val parser = CreateJacksonParser.string(factory, json) | ||
parser.nextToken() | ||
val expectedType = StructType(Seq(StructField("a", dt, true))) | ||
|
||
assert(inferSchema.inferField(parser) === expectedType) | ||
} | ||
|
||
def checkTimestampType(pattern: String, json: String): Unit = { | ||
checkType(Map("timestampFormat" -> pattern), json, TimestampType) | ||
} | ||
|
||
test("inferring timestamp type") { | ||
Seq(true, false).foreach { legacyParser => | ||
withSQLConf(SQLConf.LEGACY_TIME_PARSER_ENABLED.key -> legacyParser.toString) { | ||
checkTimestampType("yyyy", """{"a": "2018"}""") | ||
checkTimestampType("yyyy=MM", """{"a": "2018=12"}""") | ||
checkTimestampType("yyyy MM dd", """{"a": "2018 12 02"}""") | ||
checkTimestampType( | ||
"yyyy-MM-dd'T'HH:mm:ss.SSS", | ||
"""{"a": "2018-12-02T21:04:00.123"}""") | ||
checkTimestampType( | ||
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX", | ||
"""{"a": "2018-12-02T21:04:00.123567+01:00"}""") | ||
} | ||
} | ||
} | ||
|
||
test("prefer decimals over timestamps") { | ||
Seq(true, false).foreach { legacyParser => | ||
withSQLConf(SQLConf.LEGACY_TIME_PARSER_ENABLED.key -> legacyParser.toString) { | ||
checkType( | ||
options = Map( | ||
"prefersDecimal" -> "true", | ||
"timestampFormat" -> "yyyyMMdd.HHmmssSSS" | ||
), | ||
json = """{"a": "20181202.210400123"}""", | ||
dt = DecimalType(17, 9) | ||
) | ||
} | ||
} | ||
} | ||
|
||
test("skip decimal type inferring") { | ||
Seq(true, false).foreach { legacyParser => | ||
withSQLConf(SQLConf.LEGACY_TIME_PARSER_ENABLED.key -> legacyParser.toString) { | ||
checkType( | ||
options = Map( | ||
"prefersDecimal" -> "false", | ||
"timestampFormat" -> "yyyyMMdd.HHmmssSSS" | ||
), | ||
json = """{"a": "20181202.210400123"}""", | ||
dt = TimestampType | ||
) | ||
} | ||
} | ||
} | ||
|
||
test("fallback to string type") { | ||
Seq(true, false).foreach { legacyParser => | ||
withSQLConf(SQLConf.LEGACY_TIME_PARSER_ENABLED.key -> legacyParser.toString) { | ||
checkType( | ||
options = Map("timestampFormat" -> "yyyy,MM,dd.HHmmssSSS"), | ||
json = """{"a": "20181202.210400123"}""", | ||
dt = StringType | ||
) | ||
} | ||
} | ||
} | ||
} |
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