Fokko commented on code in PR #4449:
URL: https://github.com/apache/iceberg-python/pull/4449#discussion_r4786415378
##########
pyiceberg/io/pyarrow.py:
##########
@@ -3456,13 +3450,84 @@ 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
+ 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,
+ )
Review Comment:
Previously we didn't raise warnings, and I don't think we want to introduce
them because of this change.
--
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]