-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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-27793][SQL] Add ANSI SQL day-time and year-month interval types #31614
Closed
Closed
Changes from all commits
Commits
Show all changes
5 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 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
70 changes: 70 additions & 0 deletions
70
sql/catalyst/src/main/scala/org/apache/spark/sql/types/DayTimeIntervalType.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,70 @@ | ||
/* | ||
* 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 scala.math.Ordering | ||
import scala.reflect.runtime.universe.typeTag | ||
|
||
import org.apache.spark.annotation.Unstable | ||
|
||
/** | ||
* The type represents day-time intervals of the SQL standard. A day-time interval is made up | ||
* of a contiguous subset of the following fields: | ||
* - SECOND, seconds within minutes and possibly fractions of a second [0..59.999999], | ||
* - MINUTE, minutes within hours [0..59], | ||
* - HOUR, hours within days [0..23], | ||
* - DAY, days in the range [0..106751991]. | ||
* | ||
* `DayTimeIntervalType` represents positive as well as negative day-time intervals. | ||
* | ||
* Please use the singleton `DataTypes.DayTimeIntervalType` to refer the type. | ||
* | ||
* @since 3.2.0 | ||
*/ | ||
@Unstable | ||
class DayTimeIntervalType private() extends AtomicType { | ||
/** | ||
* Internally, values of day-time intervals are stored in `Long` values as amount of time in terms | ||
* of microseconds that are calculated by the formula: | ||
* -/+ (24*60*60 * DAY + 60*60 * HOUR + 60 * MINUTE + SECOND) * 1000000 | ||
*/ | ||
private[sql] type InternalType = Long | ||
|
||
@transient private[sql] lazy val tag = typeTag[InternalType] | ||
|
||
private[sql] val ordering = implicitly[Ordering[InternalType]] | ||
|
||
/** | ||
* The day-time interval type has constant precision. A value of the type always occupies 8 bytes. | ||
* The DAY field is constrained by the upper bound 106751991 to fit to `Long`. | ||
*/ | ||
override def defaultSize: Int = 8 | ||
|
||
private[spark] override def asNullable: DayTimeIntervalType = this | ||
} | ||
|
||
/** | ||
* The companion case object and its class is separated so the companion object also subclasses | ||
* the DayTimeIntervalType class. Otherwise, the companion object would be of type | ||
* "DayTimeIntervalType$" in byte code. Defined with a private constructor so the companion object | ||
* is the only possible instantiation. | ||
* | ||
* @since 3.2.0 | ||
*/ | ||
@Unstable | ||
case object DayTimeIntervalType extends DayTimeIntervalType |
68 changes: 68 additions & 0 deletions
68
sql/catalyst/src/main/scala/org/apache/spark/sql/types/YearMonthIntervalType.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,68 @@ | ||
/* | ||
* 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 scala.math.Ordering | ||
import scala.reflect.runtime.universe.typeTag | ||
|
||
import org.apache.spark.annotation.Unstable | ||
|
||
/** | ||
* The type represents year-month intervals of the SQL standard. A year-month interval is made up | ||
* of a contiguous subset of the following fields: | ||
* - MONTH, months within years [0..11], | ||
* - YEAR, years in the range [0..178956970]. | ||
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. ditto 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. The same >>> 2**31/12.0
178956970.66666666 |
||
* | ||
* `YearMonthIntervalType` represents positive as well as negative year-month intervals. | ||
* | ||
* Please use the singleton `DataTypes.YearMonthIntervalType` to refer the type. | ||
* | ||
* @since 3.2.0 | ||
*/ | ||
@Unstable | ||
class YearMonthIntervalType private() extends AtomicType { | ||
/** | ||
* Internally, values of year-month intervals are stored in `Int` values as amount of months | ||
* that are calculated by the formula: | ||
* -/+ (12 * YEAR + MONTH) | ||
*/ | ||
private[sql] type InternalType = Int | ||
|
||
@transient private[sql] lazy val tag = typeTag[InternalType] | ||
|
||
private[sql] val ordering = implicitly[Ordering[InternalType]] | ||
|
||
/** | ||
* Year-month interval values always occupy 4 bytes. | ||
* The YEAR field is constrained by the upper bound 178956970 to fit to `Int`. | ||
*/ | ||
override def defaultSize: Int = 4 | ||
|
||
private[spark] override def asNullable: YearMonthIntervalType = this | ||
} | ||
|
||
/** | ||
* The companion case object and its class is separated so the companion object also subclasses | ||
* the YearMonthIntervalType class. Otherwise, the companion object would be of type | ||
* "YearMonthIntervalType$" in byte code. Defined with a private constructor so the companion object | ||
* is the only possible instantiation. | ||
* | ||
* @since 3.2.0 | ||
*/ | ||
@Unstable | ||
case object YearMonthIntervalType extends YearMonthIntervalType |
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.
how do you get the upper limit?
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.
can this be negative?
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.
From the formula below:
even more conservative:
Interval value, yes. The field, no, according to SQL:2016:

So, individual interval fields are non-negative but interval itself can have a sign, and be negative.