Skip to content

Core: Honor identity sort orders for Arrow table writes - #4449

Open
Anurag-S1ngh wants to merge 4 commits into
apache:mainfrom
Anurag-S1ngh:feat/3848-honor-identity-sort-order
Open

Anurag-S1ngh wants to merge 4 commits into
apache:mainfrom
Anurag-S1ngh:feat/3848-honor-identity-sort-order

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

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 RecordBatchReader writes) keep writing unsorted files and emit a warning.

Are these changes tested?

Yes. New tests in tests/io/test_pyarrow.py cover the four direction and null-placement combinations, the unsupported cases, sorted files per partition, and the streaming warning. tests/table and 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.

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
Fokko self-requested a review November 25, 2026 07:58

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_file starts, 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 hand write_file a 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 Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted as an inline suggestion on the partitioned write path.

Comment thread pyiceberg/io/pyarrow.py Resolved
@Anurag-S1ngh

Copy link
Copy Markdown
Contributor Author

Addressed both nits in 774e900f and 5978093e. Partitioned write tasks now stream through a generator, and the Arrow sort-key typing matches the 3-tuple used on Arrow 25. make lint and make test pass locally.

Comment thread pyiceberg/io/pyarrow.py
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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

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.

2 participants