Anurag-S1ngh commented on code in PR #4449:
URL: https://github.com/apache/iceberg-python/pull/4449#discussion_r4729644394


##########
pyiceberg/io/pyarrow.py:
##########
@@ -3333,13 +3327,74 @@ def _dataframe_to_data_files(
                     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_df, 
target_file_size)
             ),
         )
+    else:
+        partitions = _determine_partitions(spec=table_metadata.spec(), 
schema=table_metadata.schema(), arrow_table=df)
+
+        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]:
+    """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_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):
+            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_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:
+        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"

Review Comment:
   Added the fallback in `_sort_table_for_write`. Float and double columns now 
write unsorted files with a warning for ASC NULLS FIRST and DESC NULLS LAST, 
and the test covers all four combinations with NaN, inf and null. Your ordering 
also reproduces on pyarrow 25. Rebased on main, so the import conflict is gone.



-- 
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]

Reply via email to