Skip to content

Commit 7d58178

Browse files
committed
Add DataType class for timestamp with nanoseconds
1 parent 500bf78 commit 7d58178

4 files changed

Lines changed: 96 additions & 0 deletions

File tree

sql/api/src/main/java/org/apache/spark/sql/types/DataTypes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ public class DataTypes {
5858
* Gets the TimestampNTZType object.
5959
*/
6060
public static final DataType TimestampNTZType = TimestampNTZType$.MODULE$;
61+
/**
62+
* Gets the TimestampNsType object.
63+
*/
64+
public static final DataType TimestampNsType = TimestampNsType$.MODULE$;
65+
66+
/**
67+
* Gets the TimestampNsNTZType object.
68+
*/
69+
public static final DataType TimestampNsNTZType = TimestampNsNTZType$.MODULE$;
6170

6271
/**
6372
* Gets the CalendarIntervalType object.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.types
19+
20+
import org.apache.spark.annotation.Unstable
21+
22+
/**
23+
* The timestamp_ns without time zone type represents a local time in nanosecond precision, which is
24+
* independent of time zone. Its valid range is [0001-01-01T00:00:00.000000000,
25+
* 9999-12-31T23:59:59.999999999]. To represent an absolute point in time, use `TimestampNsType`
26+
* instead.
27+
*
28+
* Please use the singleton `DataTypes.TimestampNsNTZType` to refer the type.
29+
*
30+
*/
31+
@Unstable
32+
class TimestampNsNTZType private () extends DatetimeType {
33+
34+
/**
35+
* The default size of a value of the TimestampNsNTZType is 8 bytes.
36+
*/
37+
override def defaultSize: Int = 10
38+
39+
override def typeName: String = "timestamp_ns_ntz"
40+
41+
private[spark] override def asNullable: TimestampNsNTZType = this
42+
}
43+
44+
@Unstable
45+
case object TimestampNsNTZType extends TimestampNsNTZType
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.types
19+
20+
import org.apache.spark.annotation.Unstable
21+
22+
/**
23+
* The timestamp_ns type represents a time instant in nanosecond precision. Valid range is
24+
* [0001-01-01T00:00:00.000000000Z, 9999-12-31T23:59:59.999999999Z] where the left/right-bound is a
25+
* date and time of the proleptic Gregorian calendar in UTC+00:00.
26+
*
27+
* Please use the singleton `DataTypes.TimestampNsType` to refer the type.
28+
*/
29+
30+
@Unstable
31+
class TimestampNsType private () extends DatetimeType {
32+
override def defaultSize: Int = 10
33+
34+
override def typeName: String = "timestamp_ns"
35+
36+
private[spark] override def asNullable: TimestampType = this
37+
}
38+
39+
@UnStable
40+
case object TimestampNsType extends TimestampNsType

sql/catalyst/src/test/scala/org/apache/spark/sql/types/DataTypeSuite.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ class DataTypeSuite extends SparkFunSuite {
365365
checkDefaultSize(DateType, 4)
366366
checkDefaultSize(TimestampType, 8)
367367
checkDefaultSize(TimestampNTZType, 8)
368+
checkDefaultSize(TimestampNsType, 10)
369+
checkDefaultSize(TimestampNsNTZType, 10)
368370
checkDefaultSize(StringType, 20)
369371
checkDefaultSize(CharType(20), 20)
370372
checkDefaultSize(VarcharType(20), 20)

0 commit comments

Comments
 (0)