From 3d620782935333629bfa1fd29304928be796ad05 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Fri, 20 Nov 2026 23:12:04 +0530 Subject: [PATCH 1/5] Core: Honor identity sort orders for Arrow table writes 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. --- pyiceberg/io/pyarrow.py | 82 +++++++++++++++++++++----- tests/io/test_pyarrow.py | 124 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 191 insertions(+), 15 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index 8aff1f7e73..3a52c7474f 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -160,6 +160,7 @@ from pyiceberg.table.metadata import TableMetadata from pyiceberg.table.metadata_columns import LAST_UPDATED_SEQUENCE_NUMBER, ROW_ID from pyiceberg.table.name_mapping import NameMapping, apply_name_mapping +from pyiceberg.table.sorting import NullOrder, SortDirection from pyiceberg.transforms import IdentityTransform, TruncateTransform from pyiceberg.typedef import EMPTY_DICT, Properties, Record, TableVersion from pyiceberg.types import ( @@ -3165,10 +3166,7 @@ def write_data_file(task: WriteTask) -> DataFile: file_format=file_format, partition=task.partition_key.partition if task.partition_key else Record(), file_size_in_bytes=len(fo), - # After this has been fixed: - # https://github.com/apache/iceberg-python/issues/271 - # sort_order_id=task.sort_order_id, - sort_order_id=None, + sort_order_id=task.sort_order_id, # Just copy these from the table for now spec_id=table_metadata.default_spec_id, equality_ids=None, @@ -3427,6 +3425,11 @@ def _dataframe_to_data_files( "Materialise the reader as a pa.Table first, or follow " "https://github.com/apache/iceberg-python/issues/2152 for partitioned streaming support." ) + if not table_metadata.sort_order().is_unsorted: + warnings.warn( + "Sort order is not applied to streaming RecordBatchReader writes; data files are marked unsorted", + stacklevel=2, + ) yield from write_file( io=io, table_metadata=table_metadata, @@ -3438,31 +3441,82 @@ def _dataframe_to_data_files( return if table_metadata.spec().is_unpartitioned(): + sorted_df, sort_order_id = _sort_table_for_write(table_metadata, df) yield from write_file( io=io, table_metadata=table_metadata, tasks=( - WriteTask(write_uuid=write_uuid, task_id=next(counter), record_batches=batches, schema=task_schema) - for batches in bin_pack_arrow_table(df, target_file_size) + WriteTask( + write_uuid=write_uuid, + task_id=next(counter), + record_batches=batches, + schema=task_schema, + sort_order_id=sort_order_id, + ) + for batches in bin_pack_arrow_table(sorted_df, target_file_size) ), ) else: partitions = _determine_partitions(spec=table_metadata.spec(), schema=table_metadata.schema(), arrow_table=df) - yield from write_file( - io=io, - table_metadata=table_metadata, - tasks=( + tasks: list[WriteTask] = [] + for partition in partitions: + sorted_partition, sort_order_id = _sort_table_for_write(table_metadata, partition.arrow_table_partition) + tasks.extend( 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, ) - for partition in partitions - for batches in bin_pack_arrow_table(partition.arrow_table_partition, target_file_size) - ), - ) + for batches in bin_pack_arrow_table(sorted_partition, target_file_size) + ) + yield from write_file(io=io, table_metadata=table_metadata, tasks=tasks) + + +def _sort_table_for_write(table_metadata: TableMetadata, table: pa.Table) -> tuple[pa.Table, int | None]: + """Sort the table for the table's sort order and return its file sort-order id. + + Returns the table unchanged and ``None`` when the sort order cannot be applied, so the + written files are marked unsorted. + """ + sort_order = table_metadata.sort_order() + if sort_order.is_unsorted: + return table, None + + schema = table_metadata.schema() + sort_keys: list[tuple[str, str]] = [] + null_orders: set[NullOrder] = set() + for sort_field in sort_order.fields: + if not isinstance(sort_field.transform, IdentityTransform): + warnings.warn( + f"Sort transform {sort_field.transform} is not supported for writes; data files are marked unsorted", + stacklevel=2, + ) + return table, None + column_name = schema.find_column_name(sort_field.source_id) + if column_name is None or column_name not in table.column_names: + warnings.warn( + f"Sort field with source id {sort_field.source_id} refers to a nested or missing column; " + "data files are marked unsorted", + stacklevel=2, + ) + return table, None + sort_keys.append((column_name, "descending" if sort_field.direction == SortDirection.DESC else "ascending")) + null_orders.add(sort_field.null_order) + + if len(null_orders) != 1: + 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" + if int(pa.__version__.split(".")[0]) >= 25: + # Per-key null placement replaced the SortOptions-level argument in Arrow 25. + indices = pc.sort_indices(table, sort_keys=cast(Any, [(*key, null_placement) for key in sort_keys])) + else: + indices = pc.sort_indices(table, sort_keys=cast(Any, sort_keys), null_placement=cast(Any, null_placement)) + return table.take(indices), sort_order.order_id @dataclass(frozen=True) diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 92d533651c..1ecf321dbe 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -93,10 +93,12 @@ _ConvertToArrowSchema, _ConvertViewTypes, _create_arrow_read_format, + _dataframe_to_data_files, _determine_partitions, _primitive_to_physical, _read_all_delete_files, _read_deletes, + _sort_table_for_write, _task_to_record_batches, _to_requested_schema, bin_pack_arrow_table, @@ -119,10 +121,11 @@ PROPERTY_REFERENCED_DATA_FILE, deletion_vectors_from_puffin_file, ) -from pyiceberg.table.metadata import TableMetadataV2, TableMetadataV3 +from pyiceberg.table.metadata import TableMetadataV2, TableMetadataV3, new_table_metadata 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.table.sorting import NullOrder, SortDirection, SortField, SortOrder from pyiceberg.transforms import BucketTransform, HourTransform, IdentityTransform, Transform, TruncateTransform, YearTransform from pyiceberg.typedef import UTF8, Properties, Record, TableVersion from pyiceberg.types import ( @@ -3147,6 +3150,125 @@ def test_bin_pack_arrow_table_target_size_smaller_than_row(arrow_table_with_null assert sum(batch.num_rows for bin_ in bin_packed for batch in bin_) == arrow_table_with_null.num_rows +def test_sort_table_for_write_uses_identity_sort_order() -> None: + schema = Schema( + NestedField(1, "id", LongType(), required=False), + NestedField(2, "value", StringType(), required=False), + ) + cases = [ + (SortDirection.ASC, NullOrder.NULLS_LAST, [1, 2, None]), + (SortDirection.ASC, NullOrder.NULLS_FIRST, [None, 1, 2]), + (SortDirection.DESC, NullOrder.NULLS_LAST, [2, 1, None]), + (SortDirection.DESC, NullOrder.NULLS_FIRST, [None, 2, 1]), + ] + + for direction, null_order, expected in cases: + metadata = new_table_metadata( + schema=schema, + partition_spec=PartitionSpec(), + sort_order=SortOrder(SortField(1, IdentityTransform(), direction, null_order)), + location="file:///tmp/sorted", + ) + table = pa.table({"id": [2, None, 1], "value": ["b", "null", "a"]}) + + sorted_table, sort_order_id = _sort_table_for_write(metadata, table) + + assert sorted_table["id"].to_pylist() == expected + assert sort_order_id == metadata.default_sort_order_id + + +def test_sort_table_for_write_leaves_unsupported_sort_orders_unsorted() -> None: + schema = Schema( + NestedField(1, "id", LongType(), required=False), + NestedField(2, "value", StringType(), required=False), + ) + table = pa.table({"id": [2, 1], "value": ["b", "a"]}) + cases = [ + ( + SortOrder(SortField(1, TruncateTransform(10))), + "Sort transform truncate\\[10\\] is not supported", + ), + ( + SortOrder( + SortField(1, IdentityTransform(), null_order=NullOrder.NULLS_FIRST), + SortField(2, IdentityTransform(), null_order=NullOrder.NULLS_LAST), + ), + "Mixed null ordering", + ), + ] + + for sort_order, message in cases: + metadata = new_table_metadata( + schema=schema, + partition_spec=PartitionSpec(), + sort_order=sort_order, + location="file:///tmp/unsorted", + ) + + with pytest.warns(UserWarning, match=message): + sorted_table, sort_order_id = _sort_table_for_write(metadata, table) + + assert sorted_table is table + assert sort_order_id is None + + metadata = new_table_metadata( + schema=schema, + partition_spec=PartitionSpec(), + sort_order=SortOrder(SortField(2, IdentityTransform())), + location="file:///tmp/unsorted-missing-column", + ) + table_without_sort_column = pa.table({"id": [2, 1]}) + + with pytest.warns(UserWarning, match="nested or missing column"): + sorted_table, sort_order_id = _sort_table_for_write(metadata, table_without_sort_column) + + assert sorted_table is table_without_sort_column + assert sort_order_id is None + + +def test_write_sorted_data_files_per_partition(tmp_path: Path) -> None: + schema = Schema( + NestedField(1, "id", LongType(), required=False), + NestedField(2, "category", StringType(), required=False), + ) + metadata = new_table_metadata( + schema=schema, + partition_spec=PartitionSpec(PartitionField(source_id=2, field_id=1000, transform=IdentityTransform(), name="category")), + sort_order=SortOrder(SortField(1, IdentityTransform(), SortDirection.ASC, NullOrder.NULLS_LAST)), + location=f"file://{tmp_path}/sorted-partitioned", + ) + table = pa.table({"id": [3, None, 1, 2, None, 4], "category": ["a", "b", "b", "a", "a", "b"]}) + io = PyArrowFileIO() + + data_files = list(_dataframe_to_data_files(table_metadata=metadata, df=table, io=io)) + + assert len(data_files) == 2 + for data_file in data_files: + assert data_file.sort_order_id == metadata.default_sort_order_id + with io.new_input(data_file.file_path).open() as f: + read_table = pq.read_table(f) + assert len(read_table["category"].unique()) == 1 + ids = read_table["id"].to_pylist() + assert ids == sorted(ids, key=lambda value: (value is None, value)) + + +def test_streaming_write_ignores_sort_order(tmp_path: Path) -> None: + schema = Schema(NestedField(1, "id", LongType(), required=False)) + metadata = new_table_metadata( + schema=schema, + partition_spec=PartitionSpec(), + sort_order=SortOrder(SortField(1, IdentityTransform())), + location=f"file://{tmp_path}/sorted-streaming", + ) + table = pa.table({"id": [2, 1]}) + + with pytest.warns(UserWarning, match="streaming RecordBatchReader writes"): + data_files = list(_dataframe_to_data_files(table_metadata=metadata, df=table.to_reader(), io=PyArrowFileIO())) + + assert data_files + assert all(data_file.sort_order_id is None for data_file in data_files) + + def test_bin_pack_record_batches_single_bin(arrow_table_with_null: pa.Table) -> None: batches = arrow_table_with_null.to_batches() bins = list(bin_pack_record_batches(iter(batches), target_file_size=arrow_table_with_null.nbytes * 10)) From 5015b11c0cf0acf55ffbc3e5e6bb73495ac74073 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Sat, 21 Nov 2026 03:03:18 +0530 Subject: [PATCH 2/5] Pass the partitioned write tasks as an iterator write_file expects an Iterator, and mypy rejects a list on Python 3.11. --- pyiceberg/io/pyarrow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index 3a52c7474f..9e785bfbfb 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -3472,7 +3472,7 @@ def _dataframe_to_data_files( ) for batches in bin_pack_arrow_table(sorted_partition, target_file_size) ) - yield from write_file(io=io, table_metadata=table_metadata, tasks=tasks) + yield from write_file(io=io, table_metadata=table_metadata, tasks=iter(tasks)) def _sort_table_for_write(table_metadata: TableMetadata, table: pa.Table) -> tuple[pa.Table, int | None]: From 520564d9772f682702710dd18296dbd961da5968 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Fri, 27 Nov 2026 11:11:28 +0530 Subject: [PATCH 3/5] Core: Stream partitioned write tasks lazily --- pyiceberg/io/pyarrow.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index 9e785bfbfb..a00d8fdf20 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -3458,21 +3458,21 @@ def _dataframe_to_data_files( ) else: partitions = _determine_partitions(spec=table_metadata.spec(), schema=table_metadata.schema(), arrow_table=df) - tasks: list[WriteTask] = [] - for partition in partitions: - sorted_partition, sort_order_id = _sort_table_for_write(table_metadata, partition.arrow_table_partition) - tasks.extend( - 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, - ) - for batches in bin_pack_arrow_table(sorted_partition, target_file_size) - ) - yield from write_file(io=io, table_metadata=table_metadata, tasks=iter(tasks)) + + 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()) def _sort_table_for_write(table_metadata: TableMetadata, table: pa.Table) -> tuple[pa.Table, int | None]: From e8d1f49cc576b912039259f8eba0fab4bda05378 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Fri, 27 Nov 2026 11:13:02 +0530 Subject: [PATCH 4/5] Core: Type Arrow sort keys with null placement --- pyiceberg/io/pyarrow.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index a00d8fdf20..ac12cb715b 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -3486,7 +3486,7 @@ def _sort_table_for_write(table_metadata: TableMetadata, table: pa.Table) -> tup return table, None schema = table_metadata.schema() - sort_keys: list[tuple[str, str]] = [] + sort_key_pairs: list[tuple[str, str]] = [] null_orders: set[NullOrder] = set() for sort_field in sort_order.fields: if not isinstance(sort_field.transform, IdentityTransform): @@ -3503,7 +3503,7 @@ def _sort_table_for_write(table_metadata: TableMetadata, table: pa.Table) -> tup stacklevel=2, ) return table, None - sort_keys.append((column_name, "descending" if sort_field.direction == SortDirection.DESC else "ascending")) + sort_key_pairs.append((column_name, "descending" if sort_field.direction == SortDirection.DESC else "ascending")) null_orders.add(sort_field.null_order) if len(null_orders) != 1: @@ -3511,11 +3511,12 @@ def _sort_table_for_write(table_metadata: TableMetadata, table: pa.Table) -> tup return table, None null_placement = "at_start" if NullOrder.NULLS_FIRST in null_orders else "at_end" + sort_keys: list[tuple[str, str, str]] = [(name, order, null_placement) for name, order in sort_key_pairs] if int(pa.__version__.split(".")[0]) >= 25: # Per-key null placement replaced the SortOptions-level argument in Arrow 25. - indices = pc.sort_indices(table, sort_keys=cast(Any, [(*key, null_placement) for key in sort_keys])) + indices = pc.sort_indices(table, sort_keys=cast(Any, sort_keys)) else: - indices = pc.sort_indices(table, sort_keys=cast(Any, sort_keys), null_placement=cast(Any, null_placement)) + indices = pc.sort_indices(table, sort_keys=cast(Any, sort_key_pairs), null_placement=cast(Any, null_placement)) return table.take(indices), sort_order.order_id From 799a1ae8217b54b83f4e02efdfc4aab737000367 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Sat, 28 Nov 2026 00:58:57 +0530 Subject: [PATCH 5/5] Core: Skip sorting float columns when Arrow misorders NaN Arrow places NaN next to the nulls, which only matches the Iceberg float ordering when nulls sort low. Fall back to unsorted files with a warning for ascending NULLS FIRST and descending NULLS LAST float columns. --- pyiceberg/io/pyarrow.py | 10 ++++++++++ tests/io/test_pyarrow.py | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index ac12cb715b..62dd519b65 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -3503,6 +3503,16 @@ def _sort_table_for_write(table_metadata: TableMetadata, table: pa.Table) -> tup stacklevel=2, ) return table, None + nan_sorted_with_nulls = ( + sort_field.direction == SortDirection.ASC and sort_field.null_order == NullOrder.NULLS_FIRST + ) or (sort_field.direction == SortDirection.DESC and sort_field.null_order == NullOrder.NULLS_LAST) + if isinstance(schema.find_field(sort_field.source_id).field_type, (FloatType, DoubleType)) and nan_sorted_with_nulls: + warnings.warn( + f"Sort field {column_name} is a float, and Arrow places NaN next to the nulls for " + f"{sort_field.direction} with {sort_field.null_order}; data files are marked unsorted", + stacklevel=2, + ) + return table, None sort_key_pairs.append((column_name, "descending" if sort_field.direction == SortDirection.DESC else "ascending")) null_orders.add(sort_field.null_order) diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 1ecf321dbe..b718bae603 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -3177,6 +3177,48 @@ def test_sort_table_for_write_uses_identity_sort_order() -> None: assert sort_order_id == metadata.default_sort_order_id +def test_sort_table_for_write_handles_nan_with_null_placement() -> None: + schema = Schema(NestedField(1, "value", DoubleType(), required=False)) + table = pa.table({"value": [1.0, math.nan, None, -1.0, math.inf]}) + cases = [ + (SortDirection.ASC, NullOrder.NULLS_LAST, [-1.0, 1.0, math.inf, "nan", None]), + (SortDirection.DESC, NullOrder.NULLS_FIRST, [None, "nan", math.inf, 1.0, -1.0]), + ] + + for direction, null_order, expected in cases: + metadata = new_table_metadata( + schema=schema, + partition_spec=PartitionSpec(), + sort_order=SortOrder(SortField(1, IdentityTransform(), direction, null_order)), + location="file:///tmp/sorted-nan", + ) + + sorted_table, sort_order_id = _sort_table_for_write(metadata, table) + + values = [ + "nan" if isinstance(value, float) and math.isnan(value) else value for value in sorted_table["value"].to_pylist() + ] + assert values == expected + assert sort_order_id == metadata.default_sort_order_id + + for direction, null_order in [ + (SortDirection.ASC, NullOrder.NULLS_FIRST), + (SortDirection.DESC, NullOrder.NULLS_LAST), + ]: + metadata = new_table_metadata( + schema=schema, + partition_spec=PartitionSpec(), + sort_order=SortOrder(SortField(1, IdentityTransform(), direction, null_order)), + location="file:///tmp/unsorted-nan", + ) + + with pytest.warns(UserWarning, match="Arrow places NaN next to the nulls"): + sorted_table, sort_order_id = _sort_table_for_write(metadata, table) + + assert sorted_table is table + assert sort_order_id is None + + def test_sort_table_for_write_leaves_unsupported_sort_orders_unsorted() -> None: schema = Schema( NestedField(1, "id", LongType(), required=False),