diff --git a/pyiceberg/transforms.py b/pyiceberg/transforms.py index 45c8ffd196..4a77e0dd87 100644 --- a/pyiceberg/transforms.py +++ b/pyiceberg/transforms.py @@ -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. diff --git a/pyiceberg/utils/datetime.py b/pyiceberg/utils/datetime.py index 70ffb1f303..338fc19ec3 100644 --- a/pyiceberg/utils/datetime.py +++ b/pyiceberg/utils/datetime.py @@ -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 diff --git a/tests/table/test_partitioning.py b/tests/table/test_partitioning.py index 1f24cfccd8..b5eabe7512 100644 --- a/tests/table/test_partitioning.py +++ b/tests/table/test_partitioning.py @@ -45,7 +45,9 @@ PrimitiveType, StringType, StructType, + TimestampNanoType, TimestampType, + TimestamptzNanoType, TimestamptzType, TimeType, UnknownType, @@ -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), diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 6ab7b52c4c..0575d15122 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -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"), diff --git a/tests/utils/test_datetime.py b/tests/utils/test_datetime.py index 6cbbf9c1c4..5347a4e695 100644 --- a/tests/utils/test_datetime.py +++ b/tests/utils/test_datetime.py @@ -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, ) @@ -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"