PCTEJA commented on code in PR #4505:
URL: https://github.com/apache/iceberg-python/pull/4505#discussion_r4760120837
##########
pyiceberg/io/pyarrow.py:
##########
@@ -3233,14 +3245,43 @@ def bin_pack_record_batches(batches:
Iterable[pa.RecordBatch], target_file_size:
``ParquetWriter`` with ``OutputStream.tell()`` (#2998).
"""
buffer: list[pa.RecordBatch] = []
- buffer_bytes = 0
+ buffer_bytes = 0.0
for batch in batches:
- buffer.append(batch)
- buffer_bytes += batch.nbytes
- if buffer_bytes >= target_file_size:
- yield buffer
- buffer = []
- buffer_bytes = 0
+ if batch.num_rows == 0:
+ buffer.append(batch)
+ continue
+ offset = 0
+ while offset < batch.num_rows:
+ remaining_rows = batch.num_rows - offset
+ space = target_file_size - buffer_bytes
+ fit = remaining_rows if batch.nbytes == 0 else int(space *
batch.num_rows / batch.nbytes)
+ if fit >= remaining_rows:
+ buffer.append(batch.slice(offset))
+ buffer_bytes += batch.nbytes * remaining_rows / batch.num_rows
+ offset = batch.num_rows
+ if buffer_bytes >= target_file_size:
+ yield buffer
+ buffer = []
+ buffer_bytes = 0.0
+ elif fit > 0:
+ buffer.append(batch.slice(offset, fit))
+ buffer_bytes += batch.nbytes * fit / batch.num_rows
+ offset += fit
+ yield buffer
+ buffer = []
+ buffer_bytes = 0.0
+ elif buffer:
+ yield buffer
+ buffer = []
+ buffer_bytes = 0.0
+ else:
+ buffer.append(batch.slice(offset, 1))
+ buffer_bytes += batch.nbytes / batch.num_rows
+ offset += 1
+ if buffer_bytes >= target_file_size:
+ yield buffer
+ buffer = []
+ buffer_bytes = 0.0
Review Comment:
```python
rows = min(batch.num_rows - offset, int(max(target_file_size - buffer_bytes,
0) * batch.num_rows / batch_bytes))
```
```python
if batch_bytes == 0:
buffer.append(batch)
if buffer_bytes >= target_file_size:
yield buffer
buffer, buffer_bytes = [], 0.0
continue
```
060c41d, bins identical to the old implementation on a few thousand random
and edge inputs.
One small guard for target zero or below
clamped the remaining space at zero and kept the flush after zero-byte
batches. Nothing changes for positive targets.
--
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]