Skip to content

Commit ecbebcb

Browse files
committed
add timestamp_tz_ns support to the pyspark module
1 parent aa511e3 commit ecbebcb

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

duckdb/experimental/spark/sql/type_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
TimeNTZType,
2828
TimestampMillisecondNTZType,
2929
TimestampNanosecondNTZType,
30+
TimestampNanosecondType,
3031
TimestampNTZType,
3132
TimestampSecondNTZType,
3233
TimestampType,
@@ -62,9 +63,10 @@
6263
"time with time zone": TimeType,
6364
"timestamp": TimestampNTZType,
6465
"timestamp with time zone": TimestampType,
65-
"timestamp_ms": TimestampNanosecondNTZType,
66-
"timestamp_ns": TimestampMillisecondNTZType,
66+
"timestamp_ms": TimestampMillisecondNTZType,
67+
"timestamp_ns": TimestampNanosecondNTZType,
6768
"timestamp_s": TimestampSecondNTZType,
69+
"timestamptz_ns": TimestampNanosecondType,
6870
"interval": DayTimeIntervalType,
6971
"list": ArrayType,
7072
"struct": StructType,

duckdb/experimental/spark/sql/types.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"TimestampMillisecondNTZType",
5050
"TimestampNTZType",
5151
"TimestampNanosecondNTZType",
52+
"TimestampNanosecondType",
5253
"TimestampSecondNTZType",
5354
"TimestampType",
5455
"UUIDType",
@@ -239,6 +240,26 @@ def fromInternal(self, ts: int) -> datetime.datetime: # noqa: D102
239240
return datetime.datetime.fromtimestamp(ts // 1000000).replace(microsecond=ts % 1000000)
240241

241242

243+
class TimestampNanosecondType(AtomicType, metaclass=DataTypeSingleton):
244+
"""Timestamp (datetime.datetime) data type with timezone information with nanosecond precision."""
245+
246+
def __init__(self) -> None: # noqa: D107
247+
super().__init__(DuckDBPyType("TIMESTAMPTZ_NS"))
248+
249+
def needConversion(self) -> bool: # noqa: D102
250+
return True
251+
252+
@classmethod
253+
def typeName(cls) -> str: # noqa: D102
254+
return "timestamptz_ns"
255+
256+
def toInternal(self, dt: datetime.datetime) -> int: # noqa: D102
257+
raise ContributionsAcceptedError
258+
259+
def fromInternal(self, ts: int) -> datetime.datetime: # noqa: D102
260+
raise ContributionsAcceptedError
261+
262+
242263
class TimestampNTZType(AtomicType, metaclass=DataTypeSingleton):
243264
"""Timestamp (datetime.datetime) data type without timezone information with microsecond precision."""
244265

tests/fast/spark/test_spark_types.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
TimeNTZType,
3333
TimestampMillisecondNTZType,
3434
TimestampNanosecondNTZType,
35+
TimestampNanosecondType,
3536
TimestampNTZType,
3637
TimestampSecondNTZType,
3738
TimestampType,
@@ -86,10 +87,11 @@ def test_all_types_schema(self, spark):
8687
StructField("time", TimeNTZType(), True),
8788
StructField("timestamp", TimestampNTZType(), True),
8889
StructField("timestamp_s", TimestampSecondNTZType(), True),
89-
StructField("timestamp_ms", TimestampNanosecondNTZType(), True),
90-
StructField("timestamp_ns", TimestampMillisecondNTZType(), True),
90+
StructField("timestamp_ms", TimestampMillisecondNTZType(), True),
91+
StructField("timestamp_ns", TimestampNanosecondNTZType(), True),
9192
StructField("time_tz", TimeType(), True),
9293
StructField("timestamp_tz", TimestampType(), True),
94+
StructField("timestamp_tz_ns", TimestampNanosecondType(), True),
9395
StructField("float", FloatType(), True),
9496
StructField("double", DoubleType(), True),
9597
StructField("dec_4_1", DecimalType(4, 1), True),

0 commit comments

Comments
 (0)