Skip to content

Core: Split oversized record batches when streaming writes - #4505

Open
PCTEJA wants to merge 1 commit into
apache:mainfrom
PCTEJA:split-oversized-stream-batches-4499
Open

PCTEJA wants to merge 1 commit into
apache:mainfrom
PCTEJA:split-oversized-stream-batches-4499

Conversation

@PCTEJA

@PCTEJA PCTEJA commented Nov 27, 2026

Copy link
Copy Markdown

Rationale for this change

Since #3447, tbl.append() and tbl.overwrite() accept any Arrow PyCapsule stream and stream it into unpartitioned tables through bin_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 of write.target-file-size-bytes. Converting with pa.table(df) first goes through bin_pack_arrow_table, which does split by rows.

bin_pack_record_batches now slices an incoming batch that would push the buffer past the target size, using RecordBatch.slice (zero copy), so streamed input honors the target file size the same way pa.Table input does. The number of rows that fit is found with a binary search over the slice byte size, which also handles variable-width columns. A batch that cannot be sliced to fit, such as a single row larger than the target, is kept whole, and a buffer that reaches the target exactly is flushed before the next input batch is read. The docs note in mkdocs/docs/api.md that said a streamed batch is never split has been updated.

Are these changes tested?

Yes. Three regression tests were added to 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, with all rows preserved.
  • 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.

The new tests fail on the previous implementation and pass with this change. The existing bin_pack and append tests in tests/io/test_pyarrow.py also pass.

Are there any user-facing changes?

Yes. Streamed writes to unpartitioned tables now produce multiple data files sized by write.target-file-size-bytes instead of a single file when the input arrives as one large batch. No API changes.

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.
@PCTEJA

PCTEJA commented Nov 27, 2026

Copy link
Copy Markdown
Author

@Fokko, this implements the proposal in #4499.

Could you please take a look when you are free?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant