-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-56876][SQL] Add TimestampNTZNanosType and TimestampLTZNanosType #55952
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
+245
−0
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
59e49ed
Add TimestampNTZNanosType and TimestampLTZNanosType
MaxGekk 86e157d
Fix coding style
MaxGekk 06ffd74
Improve error messages
MaxGekk e87f6ae
Handle precision overflow in nanos timestamp JSON parsing
MaxGekk 39584c5
Merge remote-tracking branch 'origin/master' into nanos-add-types
MaxGekk 14106e7
Drop redundant nanos timestamp entries from otherTypes map
MaxGekk 8b1e2ab
Clarify scaladoc on nanos timestamp types
MaxGekk 4730b9b
Cover malformed JSON forms and DRY the SPARK-56876 parser test
MaxGekk 89616ff
Use Locale.ROOT in DataTypeSuite to satisfy scalastyle
MaxGekk 63f2bc3
Apply scalafmt to TimestampNTZNanosType scaladoc
MaxGekk 4714bd6
Collapse nanos timestamp precision errors into INVALID_TIMESTAMP_PREC…
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 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
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
62 changes: 62 additions & 0 deletions
62
sql/api/src/main/scala/org/apache/spark/sql/types/TimestampLTZNanosType.scala
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,62 @@ | ||
| /* | ||
| * 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.types | ||
|
|
||
| import org.apache.spark.annotation.Unstable | ||
| import org.apache.spark.sql.errors.DataTypeErrors | ||
|
|
||
| /** | ||
| * Timestamp with local time zone with fractional-second precision in the nanosecond-capable range | ||
| * (7 to 9 decimal digits). Represents a time instant analogous to `TimestampType`, but with | ||
| * sub-microsecond precision: valid range is [0001-01-01T00:00:00.000000000Z, | ||
| * 9999-12-31T23:59:59.999999999Z] in the proleptic Gregorian calendar at UTC+00:00. No time zone | ||
| * is stored; the session time zone is used when converting values to and from text. | ||
| * | ||
| * @param precision | ||
| * Number of digits of fractional seconds for this SQL type. The valid values are 7, 8, and 9 | ||
| * where 9 means nanosecond precision. | ||
| * | ||
| * @since 4.2.0 | ||
| */ | ||
| @Unstable | ||
| case class TimestampLTZNanosType(precision: Int) extends DatetimeType { | ||
|
|
||
| if (precision < TimestampLTZNanosType.MIN_PRECISION || | ||
| precision > TimestampLTZNanosType.MAX_PRECISION) { | ||
| throw DataTypeErrors.invalidTimestampPrecisionError(precision.toString, "TIMESTAMP_LTZ") | ||
| } | ||
|
|
||
| /** | ||
| * Default size used by Spark for row-size estimation. Values are represented logically as epoch | ||
| * microseconds (Long, 8 bytes) plus nanoseconds within that micro (Short, 2 bytes). | ||
| */ | ||
| override def defaultSize: Int = 10 | ||
|
|
||
| override def typeName: String = s"timestamp_ltz($precision)" | ||
|
|
||
| private[spark] override def asNullable: TimestampLTZNanosType = this | ||
| } | ||
|
|
||
| object TimestampLTZNanosType { | ||
| val MIN_PRECISION: Int = 7 | ||
| val MAX_PRECISION: Int = 9 | ||
| val NANOS_PRECISION: Int = 9 | ||
| val DEFAULT_PRECISION: Int = NANOS_PRECISION | ||
|
|
||
| def apply(): TimestampLTZNanosType = new TimestampLTZNanosType(DEFAULT_PRECISION) | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
sql/api/src/main/scala/org/apache/spark/sql/types/TimestampNTZNanosType.scala
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,62 @@ | ||
| /* | ||
| * 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.types | ||
|
|
||
| import org.apache.spark.annotation.Unstable | ||
| import org.apache.spark.sql.errors.DataTypeErrors | ||
|
|
||
| /** | ||
| * Timestamp without time zone with fractional-second precision in the nanosecond-capable range (7 | ||
| * to 9 decimal digits). Represents a local date-time analogous to `TimestampNTZType`, but with | ||
| * sub-microsecond precision: valid range is [0001-01-01T00:00:00.000000000, | ||
| * 9999-12-31T23:59:59.999999999] in the proleptic Gregorian calendar. The value is independent of | ||
| * any time zone. To represent an absolute point in time, use `TimestampLTZNanosType` instead. | ||
| * | ||
| * @param precision | ||
| * Number of digits of fractional seconds for this SQL type. The valid values are 7, 8, and 9 | ||
| * where 9 means nanosecond precision. | ||
| * | ||
| * @since 4.2.0 | ||
| */ | ||
| @Unstable | ||
| case class TimestampNTZNanosType(precision: Int) extends DatetimeType { | ||
|
|
||
| if (precision < TimestampNTZNanosType.MIN_PRECISION || | ||
| precision > TimestampNTZNanosType.MAX_PRECISION) { | ||
| throw DataTypeErrors.invalidTimestampPrecisionError(precision.toString, "TIMESTAMP_NTZ") | ||
| } | ||
|
|
||
| /** | ||
| * Default size used by Spark for row-size estimation. Values are represented logically as epoch | ||
| * microseconds (Long, 8 bytes) plus nanoseconds within that micro (Short, 2 bytes). | ||
| */ | ||
| override def defaultSize: Int = 10 | ||
|
|
||
| override def typeName: String = s"timestamp_ntz($precision)" | ||
|
|
||
| private[spark] override def asNullable: TimestampNTZNanosType = this | ||
| } | ||
|
|
||
| object TimestampNTZNanosType { | ||
| val MIN_PRECISION: Int = 7 | ||
| val MAX_PRECISION: Int = 9 | ||
| val NANOS_PRECISION: Int = 9 | ||
| val DEFAULT_PRECISION: Int = NANOS_PRECISION | ||
|
|
||
| def apply(): TimestampNTZNanosType = new TimestampNTZNanosType(DEFAULT_PRECISION) | ||
| } |
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.
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 current timestamp type doesn't include "LTZ" in the name. Why not go with
TimestampNanosTypehere?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.
First of all, because the SPIP https://docs.google.com/document/d/1DeW15QueI4PdRyPm6C6jsTZFmIjbXX2j4h-Ja5W_fsg/edit?usp=sharing defines this class with such name. Probably you might ask why I named it in this way in the SPIP. So, there are a few reasons:
Pairs with TimestampNTZNanosType. Spark already has two SQL timestamp families: with local time zone (TimestampType / TIMESTAMP_LTZ) and without (TimestampNTZType / TIMESTAMP_NTZ). The nanosecond-capable types are the same split. Alone TimestampNanosType reads as “the” nano timestamp type and does not signal which semantics apply.
Matches SQL and typeName. The class backs timestamp_ltz(p). TimestampLTZNanosType lines up with TimestampNTZNanosType and with the SPIP/SQL names; TimestampNanosType would mirror neither timestamp_ntz nor the explicit TIMESTAMP_LTZ(n) surface.
Consistency with how Spark names the NTZ side. TimestampType omits “LTZ” for history (timestamp defaulted to session-local semantics), but TimestampNTZType is explicit because the second variant exists. For new APIs where both variants are first-class, being explicit on both sides avoids the ambiguity that already bites people (TimestampType vs “timestamp with TZ” in docs).
Safer for pattern matches and downstream code. Much of the codebase branches TimestampType vs TimestampNTZType. TimestampLTZNanosType + TimestampNTZNanosType extend that model predictably; TimestampNanosType would be assumed LTZ-by-analogy-to-TimestampType, which is easy to get wrong in reviews and refactors.