Repository navigation
Core: Honor identity sort orders for Arrow table writes - #4449
Anurag-S1ngh wants to merge 4 commits into
Conversation
Sort materialized Arrow writes that use only identity sort fields with a consistent null order, and record the table's sort order id on the written data files. Each partition is sorted independently. Sort orders that cannot be applied, such as non-identity transforms, nested or missing sort columns, mixed null ordering, and streaming readers, leave the files marked unsorted and emit a warning.
write_file expects an Iterator, and mypy rejects a list on Python 3.11.
Fokko
left a comment
There was a problem hiding this comment.
Thanks for the PR @Anurag-S1ngh 🙌 Nice to see the long-standing sort_order_id=None write path finally go away, and the fallback warnings cover the unsupported cases well.
I checked that Arrow 25 keeps accepting the 3-tuple (name, order, null_placement) sort keys, so the version branch works, and CI looks green.
One nit (non-blocking):
- pyiceberg/io/pyarrow.py around line 3338: the partitioned path collects all write tasks in a list before
write_filestarts, so every partition's record batches are held in memory at once. The previous code streamed task creation (and your second commit message hints at the same goal). You could handwrite_filea generator again:
def _tasks() -> Iterator[WriteTask]:
for partition in partitions:
sorted_partition, sort_order_id = _sort_table_for_write(table_metadata, partition.arrow_table_partition)
for batches in bin_pack_arrow_table(sorted_partition, target_file_size):
yield WriteTask(
write_uuid=write_uuid,
task_id=next(counter),
record_batches=batches,
partition_key=partition.partition_key,
schema=task_schema,
sort_order_id=sort_order_id,
)
yield from write_file(io=io, table_metadata=table_metadata, tasks=_tasks())
Nit: the sort_keys: list[tuple[str, str]] annotation now holds 3-tuples on Arrow 25.
Otherwise LGTM.
Fokko
left a comment
There was a problem hiding this comment.
Posted as an inline suggestion on the partitioned write path.
|
Addressed both nits in |
| warnings.warn("Mixed null ordering is not supported for writes; data files are marked unsorted", stacklevel=2) | ||
| return table, None | ||
|
|
||
| null_placement = "at_start" if NullOrder.NULLS_FIRST in null_orders else "at_end" |
There was a problem hiding this comment.
Arrow keeps NaN next to the nulls, so it follows null_placement instead of the Iceberg float ordering (-NaN < -Inf < ... < Inf < NaN). Sorting [1.0, nan, None, -1.0, inf] on pyarrow 26:
| order | Arrow | spec |
|---|---|---|
| ASC, NULLS_LAST | [-1, 1, inf, nan, None] |
same |
| ASC, NULLS_FIRST | [None, nan, -1, 1, inf] |
[None, -1, 1, inf, nan] |
| DESC, NULLS_LAST | [inf, 1, -1, nan, None] |
[nan, inf, 1, -1, None] |
| DESC, NULLS_FIRST | [None, nan, inf, 1, -1] |
same |
For ASC NULLS_FIRST and DESC NULLS_LAST on a float/double column, the files get a sort_order_id they don't satisfy. Could we fall back to unsorted with a warning for those combinations, and add a NaN case to the tests?
Rationale for this change
Arrow table writes ignored the table sort order and always wrote
sort_order_id=None, so manifests disagreed with the table metadata and readers could not prune on sort order.Sort materialized Arrow tables when every sort field uses an identity transform, the column exists at the top level, and all fields agree on null placement. Unpartitioned tables are sorted before bin packing and each partition is sorted on its own. The table sort order id is written on each data file.
Unsupported cases (non-identity transforms, nested or missing columns, mixed null placement, streaming
RecordBatchReaderwrites) keep writing unsorted files and emit a warning.Are these changes tested?
Yes. New tests in
tests/io/test_pyarrow.pycover the four direction and null-placement combinations, the unsupported cases, sorted files per partition, and the streaming warning.tests/tableand the local write tests pass.Are there any user-facing changes?
Yes. Tables with an identity sort order now write physically sorted data files that carry the sort order id. Previously written files are unchanged.