diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index d0b7031c3f..777ebc62ae 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -3405,7 +3405,12 @@ def _dataframe_to_data_files( ), ) else: - partitions = _determine_partitions(spec=table_metadata.spec(), schema=table_metadata.schema(), arrow_table=df) + partitions = _determine_partitions( + spec=table_metadata.spec(), + schema=table_metadata.schema(), + arrow_table=df, + downcast_ns_timestamp_to_us=downcast_ns_timestamp_to_us, + ) yield from write_file( io=io, table_metadata=table_metadata, @@ -3429,7 +3434,9 @@ class _TablePartition: arrow_table_partition: pa.Table -def _determine_partitions(spec: PartitionSpec, schema: Schema, arrow_table: pa.Table) -> Iterable[_TablePartition]: +def _determine_partitions( + spec: PartitionSpec, schema: Schema, arrow_table: pa.Table, downcast_ns_timestamp_to_us: bool = False +) -> Iterable[_TablePartition]: """Based on the iceberg table partition spec, filter the arrow table into partitions with their keys. Example: @@ -3463,6 +3470,14 @@ def _determine_partitions(spec: PartitionSpec, schema: Schema, arrow_table: pa.T if full_field_name is None: raise ValueError(f"Could not find column name for field ID: {partition.source_id}") field_array = _get_field_from_arrow_table(arrow_table, full_field_name) + # The writer downcasts ns timestamps after the partition values are computed, so partition on the microseconds + if ( + downcast_ns_timestamp_to_us + and isinstance(source_field.field_type, (TimestampType, TimestamptzType)) + and isinstance(field_array.type, pa.TimestampType) + and field_array.type.unit == "ns" + ): + field_array = field_array.cast(pa.timestamp("us", tz=field_array.type.tz), safe=False) partition_array = partition.transform.pyarrow_transform(source_field.field_type)(field_array) # PyArrow has no group by or equality kernels for extension types such as arrow.uuid, so use their storage type if isinstance(partition_array.type, pa.BaseExtensionType): diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index e1e99be4c7..1d47d7f881 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -115,7 +115,7 @@ from pyiceberg.table.metadata_columns import LAST_UPDATED_SEQUENCE_NUMBER, ROW_ID from pyiceberg.table.name_mapping import create_mapping_from_schema from pyiceberg.table.puffin import MAGIC_BYTES, PuffinFile -from pyiceberg.transforms import BucketTransform, HourTransform, IdentityTransform, Transform, TruncateTransform +from pyiceberg.transforms import BucketTransform, DayTransform, HourTransform, IdentityTransform, Transform, TruncateTransform from pyiceberg.typedef import UTF8, Properties, Record, TableVersion from pyiceberg.types import ( BinaryType, @@ -137,6 +137,7 @@ StructType, TimestampNanoType, TimestampType, + TimestamptzNanoType, TimestamptzType, TimeType, UUIDType, @@ -4154,6 +4155,42 @@ def test__to_requested_schema_timestamps_without_downcast_raises_exception( assert "Unsupported schema projection from timestamp[ns] to timestamp[us]" in str(exc_info.value) +def test_append_partitioned_ns_timestamp_downcast(monkeypatch: pytest.MonkeyPatch, catalog: InMemoryCatalog) -> None: + monkeypatch.setenv("PYICEBERG_DOWNCAST_NS_TIMESTAMP_TO_US_ON_WRITE", "True") + catalog.create_namespace("default") + # Stored as [1, 1, 0, 3600000000, null] us: 1000 and 1500 ns share 1 us, and -1 ns becomes 0 us + values = [1000, 1500, -1, 3600000000000, None] + cases: list[tuple[Transform[Any, Any], PrimitiveType, str | None, dict[Any, int]]] = [ + (IdentityTransform(), TimestampType(), None, {1: 2, 0: 1, 3600000000: 1, None: 1}), + (HourTransform(), TimestampType(), None, {0: 3, 1: 1, None: 1}), + (DayTransform(), TimestamptzType(), "UTC", {0: 4, None: 1}), + ] + for transform, ts_type, tz, expected in cases: + table = catalog.create_table( + f"default.ns_downcast_{transform}", + schema=Schema(NestedField(1, "id", LongType(), required=False), NestedField(2, "ts", ts_type, required=False)), + partition_spec=PartitionSpec(PartitionField(2, 1000, transform, name="ts_part")), + ) + table.append(pa.table({"id": pa.array(range(5), pa.int64()), "ts": pa.array(values, pa.timestamp("ns", tz=tz))})) + + rows = table.scan().to_arrow().sort_by("id") + assert rows.column("id").to_pylist() == [0, 1, 2, 3, 4], transform + assert rows.column("ts").cast(pa.int64()).to_pylist() == [1, 1, 0, 3600000000, None], transform + assert {task.file.partition[0]: task.file.record_count for task in table.scan().plan_files()} == expected, transform + + +def test_determine_partitions_native_ns_timestamp_is_not_downcast() -> None: + # v3 nanosecond sources keep their precision, so day(-1 ns) stays -1 even with the downcast flag on + for source_type, tz in [(TimestampNanoType(), None), (TimestamptzNanoType(), "UTC")]: + schema = Schema(NestedField(1, "id", LongType(), required=False), NestedField(2, "ts", source_type, required=False)) + spec = PartitionSpec(PartitionField(2, 1000, DayTransform(), name="ts_day")) + arrow_table = pa.table({"id": pa.array([0, 1], pa.int64()), "ts": pa.array([-1, None], pa.timestamp("ns", tz=tz))}) + result = _determine_partitions(spec, schema, arrow_table, downcast_ns_timestamp_to_us=True) + assert { + p.partition_key.partition[0]: p.arrow_table_partition.column("ts").cast(pa.int64()).to_pylist() for p in result + } == {-1: [-1], None: [None]}, source_type + + def test__to_requested_schema_null_map_of_structs() -> None: requested_schema = Schema( NestedField(