Skip to content

Downcast nanosecond timestamps before partitioning - #4479

Open
rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/ns-timestamp-hour-partition
Open

rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/ns-timestamp-hour-partition

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

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:

# 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.

Partition values were computed on timestamp[ns] input before the writer
downcast it, so identity partitions duplicated or lost rows, hour partitions
failed, and negative year/month/day partitions disagreed with the stored
values. Cast each nanosecond partition source to timestamp[us] before its
transform when downcast-ns-timestamp-to-us-on-write is set.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@ebyhr, this fixes hour and identity partitions on timestamp[ns] when downcast-ns-timestamp-to-us-on-write is on, by computing partition values on the microseconds that get written. Could you take a look when you have a chance? Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant