-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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-26246][SQL] Inferring TimestampType from JSON #23201
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2a26e2c
Added a test for timestamp inferring
MaxGekk bd47207
Infer date and timestamp types
MaxGekk 9dbdf0a
Test for date type
MaxGekk 9376832
Added a test to check that inferring of the date type is prioritised …
MaxGekk 05bbfea
Infer date type before timestamp type
MaxGekk 53778f9
Merge remote-tracking branch 'origin/master' into json-infer-time
MaxGekk 63ebf42
Fix merges
MaxGekk f92ff86
Merge remote-tracking branch 'origin/master' into json-infer-time
MaxGekk e6fc432
Inferring timestamp only
MaxGekk b27d081
Test for inferring timestamps and decimals
MaxGekk c59e3e8
`type` -> dt
MaxGekk e7471a7
GMT -> UTC
MaxGekk 82816ed
Test for fallback to string type
MaxGekk b0d1374
Fix task is not serializable
MaxGekk 5782de5
Added test for schema inferring
MaxGekk 63a6568
Roundtrip test for timestamp inferring
MaxGekk e67a2a1
Merge branch 'json-infer-time' of github.com:MaxGekk/spark-1 into jso…
MaxGekk 11daee3
Testing for legacy and new timestamp parser
MaxGekk 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
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
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.
Yea, I think this was a mistake. Previously if
prefersDecimal
wasfalse
(by default), it won't try decimal casting. Now looks we're trying decimal try always. @bersprockets, can you open a PR to fix it? I think we can just make itlazy
.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.
We shouldn't. The
opt
method calls body by name:def opt[U >: T](body: => U): Option[U]
It should not try infer ifoptions.prefersDecimal
is false.... or
prefersDecimal
becametrue
by default?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.
@HyukjinKwon What's the problem here. Could you give more context (JIRA, PR), please.
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.
Here, SPARK-26711. You're cc'ed there as well :).
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.
The problem here is decimal conversion looks always being tried even when
prefersDecimal
is false. Previously, it checkedprefersDecimal
first so decimal try wasn't made. This looks causing performance regression.I mean
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.
and making it lazy will save us by short circuiting. I wanted to open a PR right away but wanted to let him open since this is what I investigated at SPARK-26711.
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.
re: #23201 (comment)
That's being called by name within
opt
I believe.