Repository navigation
Conversation
bin_pack_record_batches never split an incoming batch, so a streamed input that exports a single large batch (for example a Polars DataFrame through the PyCapsule interface) was written as one data file regardless of write.target-file-size-bytes. Slice a batch that would push the buffer past the target size so streamed writes honor the target file size like Table input does.
Contributor
Author
Fokko
reviewed
Nov 30, 2026
Fokko
left a comment
Contributor
There was a problem hiding this comment.
Thanks for picking this up! I re-ran the repro from #4499 and the plain Polars case is fixed: with 500k rows and write.target-file-size-bytes=1048576, tbl.append(polars_df) now writes 17 files, the same as tbl.append(pa.table(polars_df)).
One problem is blocking: dictionary-encoded columns, such as Polars Categorical, are now written one row per file. Details and a repro are in the inline comment on _rows_fitting. The other inline comments are smaller.
Two things that can't go inline:
- The description says the new tests fail on the previous implementation.
test_bin_pack_record_batches_exact_target_flushes_without_reading_aheadpasses on the old code (see the inline comment), so that sentence should be updated. Closes #4499sits inside an HTML comment, so GitHub doesn't link the issue and won't close it when the PR merges.
Contributor
Author
|
I will look into all the comments @Fokko . Thank you for the detailed review and your valuable time. |
Sizing slices by slice(...).nbytes counts a batch's dictionary in every slice, so a dictionary-encoded batch whose dictionary exceeds the target degenerated to one row per bin. Slice by the batch's average row size instead, like bin_pack_arrow_table, and account bins in estimated bytes. Bins are now kept at or below the target for every batch shape; the previous num_rows == 1 exception let single-row batches push a bin over the target. A single row larger than the target is still kept whole.
Contributor
Author
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4499
Rationale for this change
Since #3447,
tbl.append()andtbl.overwrite()accept any Arrow PyCapsule stream and stream it into unpartitioned tables throughbin_pack_record_batches. That function never split a batch, so a dataframe that exports a single chunk, such as a Polars DataFrame built in one go, ended up in one data file regardless ofwrite.target-file-size-bytes. Converting withpa.table(df)first goes throughbin_pack_arrow_table, which does split by rows.bin_pack_record_batchesnow slices an incoming batch that would push the buffer past the target size, usingRecordBatch.slice(zero copy), so streamed input honors the target file size the same waypa.Tableinput does. Rows are sliced by the batch's average row size, as inbin_pack_arrow_table, and bins are kept at or below the target; a single row larger than the target is kept whole in its own bin. Sizing slices byRecordBatch.slice(...).nbytesinstead, as the first version of this PR did, counts fixed per-batch buffers such as a dictionary in every slice, which degenerates to one row per bin when the dictionary alone exceeds the target. The docs sentence inmkdocs/docs/api.mdthat said a streamed batch is never split has been removed, as suggested in #4499.Are these changes tested?
Yes. Tests in
tests/io/test_pyarrow.py:test_bin_pack_record_batches_splits_oversized_batch: a single 10,000-row batch is split into bins at or below the target, and the rebuilt table equals the input.test_bin_pack_record_batches_splits_batch_after_partial_buffer: a large batch arriving after a small one is split across bins at or below the target, with all rows preserved.test_bin_pack_record_batches_dictionary_column: a dictionary-encoded batch whose dictionary alone exceeds the target is packed into a handful of bins instead of one row per bin.test_bin_pack_record_batches_exact_target_flushes_without_reading_ahead: a batch that exactly fills the target is yielded without consuming the next input batch.test_bin_pack_record_batches_microbatchednow asserts that bins stay at or below the target instead of crossing it.The split tests and the updated microbatched test fail on the previous implementation, and the dictionary and microbatched tests also fail on the first version of this PR. The exact-target test passes on the previous implementation; it guards the lazy, single-pass behaviour rather than a regression. The existing
bin_packand append tests intests/io/test_pyarrow.pyalso pass.Are there any user-facing changes?
Yes. Streamed writes to unpartitioned tables now produce multiple data files sized by
write.target-file-size-bytesinstead of a single file when the input arrives as one large batch. No API changes.