-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19228][SQL] Introduce tryParseDate method to process csv date,… #20140
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -90,6 +90,7 @@ private[csv] object CSVInferSchema { | |
| // DecimalTypes have different precisions and scales, so we try to find the common type. | ||
| findTightestCommonType(typeSoFar, tryParseDecimal(field, options)).getOrElse(StringType) | ||
| case DoubleType => tryParseDouble(field, options) | ||
| case DateType => tryParseDate(field, options) | ||
| case TimestampType => tryParseTimestamp(field, options) | ||
| case BooleanType => tryParseBoolean(field, options) | ||
| case StringType => StringType | ||
|
|
@@ -140,14 +141,23 @@ private[csv] object CSVInferSchema { | |
| private def tryParseDouble(field: String, options: CSVOptions): DataType = { | ||
| if ((allCatch opt field.toDouble).isDefined || isInfOrNan(field, options)) { | ||
| DoubleType | ||
| } else { | ||
| tryParseDate(field, options) | ||
| } | ||
| } | ||
|
|
||
| private def tryParseDate(field: String, options: CSVOptions): DataType = { | ||
| // This case infers a custom `dateFormat` is set. | ||
| if ((allCatch opt options.dateFormatter.parse(field)).isDefined) { | ||
| DateType | ||
| } else { | ||
| tryParseTimestamp(field, options) | ||
| } | ||
| } | ||
|
|
||
| private def tryParseTimestamp(field: String, options: CSVOptions): DataType = { | ||
| // This case infers a custom `dataFormat` is set. | ||
| if ((allCatch opt options.timestampFormat.parse(field)).isDefined) { | ||
| // This case infers a custom `timestampFormat` is set. | ||
| if ((allCatch opt options.timestampFormatter.parse(field)).isDefined) { | ||
| TimestampType | ||
| } else if ((allCatch opt DateTimeUtils.stringToTime(field)).isDefined) { | ||
| // We keep this for backwards compatibility. | ||
|
|
@@ -216,6 +226,8 @@ private[csv] object CSVInferSchema { | |
| } else { | ||
| Some(DecimalType(range + scale, scale)) | ||
| } | ||
| // By design 'TimestampType' (8 bytes) is larger than 'DateType' (4 bytes). | ||
| case (t1: DateType, t2: TimestampType) => Some(TimestampType) | ||
|
Member
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. I think we should do the opposite case too |
||
|
|
||
| case _ => None | ||
| } | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.spark.sql.execution.datasources.csv | ||
|
|
||
| import java.nio.charset.StandardCharsets | ||
| import java.time.format.{DateTimeFormatter, ResolverStyle} | ||
| import java.util.{Locale, TimeZone} | ||
|
|
||
| import com.univocity.parsers.csv.{CsvParserSettings, CsvWriterSettings, UnescapedQuoteHandling} | ||
|
|
@@ -150,6 +151,16 @@ class CSVOptions( | |
|
|
||
| val isCommentSet = this.comment != '\u0000' | ||
|
|
||
| lazy val dateFormatter: DateTimeFormatter = { | ||
|
Member
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.
|
||
| DateTimeFormatter.ofPattern(dateFormat.getPattern) | ||
| .withLocale(Locale.US).withZone(timeZone.toZoneId).withResolverStyle(ResolverStyle.SMART) | ||
| } | ||
|
|
||
| lazy val timestampFormatter: DateTimeFormatter = { | ||
| DateTimeFormatter.ofPattern(timestampFormat.getPattern) | ||
| .withLocale(Locale.US).withZone(timeZone.toZoneId).withResolverStyle(ResolverStyle.SMART) | ||
| } | ||
|
|
||
| def asWriterSettings: CsvWriterSettings = { | ||
| val writerSettings = new CsvWriterSettings() | ||
| val format = writerSettings.getFormat | ||
|
|
||
4 changes: 4 additions & 0 deletions
4
sql/core/src/test/resources/test-data/dates-and-timestamps.csv
This file contains hidden or 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,4 @@ | ||
| timestamp,date | ||
| 26/08/2015 22:31:46.913,27/09/2015 | ||
| 27/10/2014 22:33:31.601,26/12/2016 | ||
| 28/01/2016 22:33:52.888,28/01/2017 |
This file contains hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.
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 replace it to
timestampFormatterin CSV parsing logic too and document it in the migration guide? (e.g., date format is now inferred correctly and also things you mentioned in #20140 (comment))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.
Probably, adding a configuration to control this behaviour looks preferred in this case.