Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pyiceberg/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,16 @@ def _(_type: IcebergType, value: int) -> str:
return datetime.to_human_timestamptz(value)


@_int_to_human_string.register(TimestampNanoType)
def _(_type: IcebergType, value: int) -> str:
return datetime.to_human_timestamp_ns(value)


@_int_to_human_string.register(TimestamptzNanoType)
def _(_type: IcebergType, value: int) -> str:
return datetime.to_human_timestamptz_ns(value)


class UnknownTransform(Transform[S, T]):
"""A transform that represents when an unknown transform is provided.

Expand Down
14 changes: 14 additions & 0 deletions pyiceberg/utils/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ def to_human_timestamp(timestamp_micros: int) -> str:
return (EPOCH_TIMESTAMP + timedelta(microseconds=timestamp_micros)).isoformat()


def to_human_timestamp_ns(timestamp_nanos: int) -> str:
"""Convert a TimestampNanoType value to human string, matching Java's ISO_LOCAL_DATE_TIME."""
seconds, nanos = divmod(timestamp_nanos, 1_000_000_000)
human = (EPOCH_TIMESTAMP + timedelta(seconds=seconds)).isoformat()
if nanos:
human += "." + f"{nanos:09d}".rstrip("0")
return human


def to_human_timestamptz_ns(timestamp_nanos: int) -> str:
"""Convert a TimestamptzNanoType value to human string, matching Java's ISO_LOCAL_DATE_TIME with offset."""
return to_human_timestamp_ns(timestamp_nanos) + "+00:00"


def micros_to_hours(micros: int) -> int:
"""Convert a timestamp in microseconds to hours from 1970-01-01T00:00."""
return micros // 3_600_000_000
Expand Down
18 changes: 18 additions & 0 deletions tests/table/test_partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
PrimitiveType,
StringType,
StructType,
TimestampNanoType,
TimestampType,
TimestamptzNanoType,
TimestamptzType,
TimeType,
UnknownType,
Expand Down Expand Up @@ -180,6 +182,22 @@ def test_partition_spec_to_path_asterisk_and_tilde() -> None:
assert spec.partition_to_path(Record("a*b~c-d_e.f"), schema) == "my*str%7Efield=a*b%7Ec-d_e.f"


def test_partition_spec_to_path_timestamp() -> None:
for field_type, expected_path in [
(TimestampType(), "ts=2025-02-23T20%3A21%3A44.375612"),
(TimestamptzType(), "ts=2025-02-23T20%3A21%3A44.375612%2B00%3A00"),
(TimestampNanoType(), "ts=2025-02-23T20%3A21%3A44.375612001"),
(TimestamptzNanoType(), "ts=2025-02-23T20%3A21%3A44.375612001%2B00%3A00"),
]:
schema = Schema(NestedField(field_id=1, name="ts", field_type=field_type, required=False))
spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="ts"))

# Exercise nanosecond precision that cannot be represented in microseconds.
value = 1740342104375612001 if isinstance(field_type, (TimestampNanoType, TimestamptzNanoType)) else 1740342104375612

assert spec.partition_to_path(Record(value), schema) == expected_path


def test_partition_spec_to_path_dropped_source_id() -> None:
schema = Schema(
NestedField(field_id=1, name="str", field_type=StringType(), required=False),
Expand Down
2 changes: 2 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ def test_satisfies_order_of_method(transform: TimeTransform[Any], other_transfor
(TimeType(), 36775038194, "10:12:55.038194"),
(TimestamptzType(), 1512151975038194, "2017-12-01T18:12:55.038194+00:00"),
(TimestampType(), 1512151975038194, "2017-12-01T18:12:55.038194"),
(TimestampNanoType(), 1740342104375612001, "2025-02-23T20:21:44.375612001"),
(TimestamptzNanoType(), 1740342104375612001, "2025-02-23T20:21:44.375612001+00:00"),
(LongType(), -1234567890000, "-1234567890000"),
(StringType(), "a/b/c=d", "a/b/c=d"),
(DecimalType(9, 2), Decimal("-1.50"), "-1.50"),
Expand Down
30 changes: 30 additions & 0 deletions tests/utils/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
to_human_month,
to_human_time,
to_human_timestamp,
to_human_timestamp_ns,
to_human_timestamptz,
to_human_timestamptz_ns,
to_human_year,
)

Expand Down Expand Up @@ -290,3 +292,31 @@ def test_to_human_timestamptz() -> None:
(1_740_342_104_375_612, "2025-02-23T20:21:44.375612+00:00"),
]:
assert to_human_timestamptz(micros) == expected


# Matches Java DateTimeUtil.nanosToIsoTimestamp / nanosToIsoTimestamptz, which use
# ISO_LOCAL_DATE_TIME: trailing fraction zeros are trimmed and whole seconds have no fraction.
NANOS_HUMAN_STRINGS = [
(0, "1970-01-01T00:00:00"),
(1, "1970-01-01T00:00:00.000000001"),
(999, "1970-01-01T00:00:00.000000999"),
(1_000, "1970-01-01T00:00:00.000001"),
(100_000_000, "1970-01-01T00:00:00.1"),
(1_000_000_000, "1970-01-01T00:00:01"),
(-1, "1969-12-31T23:59:59.999999999"),
(-1_000_000_000, "1969-12-31T23:59:59"),
(-1_500_000_000, "1969-12-31T23:59:58.5"),
(1_740_342_104_375_612_001, "2025-02-23T20:21:44.375612001"),
(1_740_342_104_375_612_000, "2025-02-23T20:21:44.375612"),
(1_740_342_104_000_000_001, "2025-02-23T20:21:44.000000001"),
]


def test_to_human_timestamp_ns() -> None:
for nanos, human_str in NANOS_HUMAN_STRINGS:
assert to_human_timestamp_ns(nanos) == human_str


def test_to_human_timestamptz_ns() -> None:
for nanos, human_str in NANOS_HUMAN_STRINGS:
assert to_human_timestamptz_ns(nanos) == human_str + "+00:00"
Loading