/
DayTimeIntervalType.scala
110 lines (94 loc) · 3.92 KB
/
DayTimeIntervalType.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* 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
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.types.DayTimeIntervalType.fieldToString
/**
* 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.
*
* @param startField The leftmost field which the type comprises of. Valid values:
* 0 (DAY), 1 (HOUR), 2 (MINUTE), 3 (SECOND).
* @param endField The rightmost field which the type comprises of. Valid values:
* 0 (DAY), 1 (HOUR), 2 (MINUTE), 3 (SECOND).
*
* @since 3.2.0
*/
@Unstable
case class DayTimeIntervalType(startField: Byte, endField: Byte) 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
override val typeName: String = {
val startFieldName = fieldToString(startField)
val endFieldName = fieldToString(endField)
if (startFieldName == endFieldName) {
s"interval $startFieldName"
} else if (startField < endField) {
s"interval $startFieldName to $endFieldName"
} else {
throw QueryCompilationErrors.invalidDayTimeIntervalType(startFieldName, endFieldName)
}
}
}
/**
* Extra factory methods and pattern matchers for DayTimeIntervalType.
*
* @since 3.2.0
*/
@Unstable
case object DayTimeIntervalType extends AbstractDataType {
val DAY: Byte = 0
val HOUR: Byte = 1
val MINUTE: Byte = 2
val SECOND: Byte = 3
val dayTimeFields = Seq(DAY, HOUR, MINUTE, SECOND)
def fieldToString(field: Byte): String = field match {
case DAY => "day"
case HOUR => "hour"
case MINUTE => "minute"
case SECOND => "second"
case invalid => throw QueryCompilationErrors.invalidDayTimeField(invalid)
}
val DEFAULT = DayTimeIntervalType(DAY, SECOND)
def apply(): DayTimeIntervalType = DEFAULT
override private[sql] def defaultConcreteType: DataType = DEFAULT
override private[sql] def acceptsType(other: DataType): Boolean = {
other.isInstanceOf[DayTimeIntervalType]
}
override private[sql] def simpleString: String = defaultConcreteType.simpleString
}