rahulsmahadev opened a new pull request, #4479:
URL: https://github.com/apache/iceberg-python/pull/4479
# Rationale for this change
Bug: with `downcast-ns-timestamp-to-us-on-write` on (non-default; set here
with `PYICEBERG_DOWNCAST_NS_TIMESTAMP_TO_US_ON_WRITE=true`), an hour partition
on a `timestamp[ns]` column fails. The partition values are computed on the
nanosecond input, and the writer only downcasts afterwards. The hour transform
has no nanosecond kernel:
```python
# table partitioned by hour(event_ts)
tbl.append(pa.table({"id": [0, 1], "event_ts": pa.array([1000000001500,
3600000000000], pa.timestamp("ns"))}))
# ValueError: FeatureUnsupported => Unsupported data type for hour
transform: Timestamp(Nanosecond, None)
```
For an identity partition on the same column, main duplicates the aligned
row when it shares a microsecond with an unaligned one (1000 and 1500 ns give
IDs `[0, 0]`), and raises `ZeroDivisionError` for an unaligned value on its own.
Fix: when the option is on, cast each nanosecond partition source to
microseconds with the writer's safe=False cast before applying its transform.
The writer still downcasts the data, so partition values match stored values.
Refs #1619, the same conversion-order problem for the day transform, closed as
stale.
Java's v1/v2 partition transforms receive microseconds: Spark supplies them
directly, and the generic Kafka writer uses InternalRecordWrapper to convert
timestamps first. This PR applies PyIceberg's existing downcast before
partitioning.
## Are these changes tested?
Yes. Four tests in `tests/io/test_pyarrow.py`, next to the existing downcast
tests: `test_append_hour_partition_with_ns_timestamp_downcast` (top-level and
struct-child hour partitions),
`test_append_identity_partition_with_ns_timestamp_downcast` (1000 and 1500 ns
share one partition, IDs [0, 1]),
`test_append_year_month_day_partition_negative_ns_timestamp_downcast` (-1 ns
and a null row, for year, month, day and a UTC day; partitions 0 and null), and
`test_determine_partitions_native_ns_timestamp_is_not_downcast` (v3 nanosecond
sources keep -1 ns). The first three fail on main with the `FeatureUnsupported`
error above, IDs [0, 0], and partition -1, and pass with this change. The
fourth fails if the native-type check is removed.
## Are there any user-facing changes?
With the option on, hour and identity partitions accept timestamp[ns]. Year,
month and day partitions now follow the stored microseconds at negative
boundaries (-1 ns becomes 0 us, so partition -1 becomes 0). Existing files
retain their old partition values.
Please add the `changelog` label.
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]