Skip to content

feat(arrow-select): add a target batch size in bytes to BatchCoalescer - #12509

Open
ranflarion wants to merge 15 commits into
apache:mainfrom
ranflarion:coalesce-target-batch-bytes
Open

ranflarion wants to merge 15 commits into
apache:mainfrom
ranflarion:coalesce-target-batch-bytes

Conversation

@ranflarion

@ranflarion ranflarion commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

BatchCoalescer cuts output batches by rows only, so wide rows give batches of hundreds of MB: 8,192 rows of 10,000 Float64 columns are 655 MB. A caller cannot bound them from outside, because the builders allocate target_batch_size values per column for every batch, so a batch finished early still holds full-size buffers.

What changes are included in this PR?

  • BatchCoalescer::with_target_batch_bytes and a target_batch_bytes getter. With a target, push_batch also completes a batch once the buffered rows reach it.
  • With a target, push_batch_with_filter and push_batch_with_indices materialize the selection with the filter and take kernels and push it as a batch, so that the selected rows are measured and not their source. Indices are gathered no more at once than the current batch has room for by its row limit, as the direct indexed copy does. If an array cannot hold the rows of a gather, the batch being built is completed, and each batch from there on is made of the most rows, doubling from one, that a single gather can hold. The batch is completed between two such gathers, so that none gets rows that were not gathered together. The direct filter and indexed copies are unchanged without a target.
  • Each row counts as an equal share of the batch it is pushed in, kept as a ratio of bytes to rows, so rows of less than a byte, such as booleans, are not rounded up to one. Columns are measured with ArrayData::get_slice_memory_size (falling back to get_array_memory_size for layouts it does not size), with two exceptions that count what an output batch keeps:
    • a view column counts its data buffers if the batch fits whole and shares them, and otherwise the value of every view, since each is copied, one for each view of a value that several share. A batch whose views share values, and which the current output batch has no room for whole, starts a new output batch if it fits that one whole;
    • a dictionary of strings counts its keys by row, and its values once for each output batch that takes rows of it, however many pushes share them.
  • InProgressArray::start_array, called before the first rows of each output array with the number of rows the batch is expected to hold, which the primitive, boolean, byte, fixed-size binary and view builders allocate for, validity included. With a target, a finished batch is also shrunk to fit, so that buffers allocated for rows that did not come, or grown past the rows that did, are not kept.
  • Under a byte target, start_array also asks for an array that owns its buffers, so that a batch cut to a few rows does not keep a whole input batch alive:
    • the view builder copies the strings of a source it takes only a part of into a buffer of exactly their size. Without a target it shares the source's buffers, or fills blocks that grow to 1 MiB. It still shares the buffers of a dense source it takes whole;
    • the list builder and the generic one copy a batch that is a single slice of an input, which they otherwise return as it is, unless the slice is the whole input and its buffers hold at most twice its bytes, the rule the view builder applies to its data buffers. So a caller that pushes whole batches that each complete an output batch, as a coalescer without the biggest_coalesce_batch_size bypass does, gets them back without a copy of their list and struct columns. The copy grows from empty, so a child is sized by its own values, not by the rows of its parent. A column with a list view in it is left as it is, because a copy holds the values of every list, even those that the lists share.
  • A batch whose buffers hold more than the target does not take the biggest_coalesce_batch_size bypass, so a small slice of a large allocation is copied.
  • Rows buffered before a target is set were not measured, so they are completed as a batch before more rows are added.
  • The function that copies the strings of contiguous views starts a new buffer at i32::MAX bytes, as the indexed copy does. It wrote each offset as u32 into a single buffer, so more than 4 GiB of strings copied at once wrapped the offsets past it. The compaction of a sparse source, which is not new, goes through the same function.

Limits. Rows of a batch that differ a lot in size can take an output batch past the target, and so can a part of a long run of a run-end encoded column, since the rows of a run count as equal shares of its value and a batch that takes some of them keeps the whole value. get_slice_memory_size counts the whole child of a sliced list, the whole data buffers of a view nested in another type, and the values of a dictionary that is not of strings, so those give smaller batches than the target allows. The values of a dictionary, columns with a list view in them, and views and dictionaries nested in another type stay shared with the input, so a batch cut from them can keep more than the target alive. An input taken whole stays shared when its buffers hold at most twice its bytes, so it can keep up to twice its share of the target. As without a target, a batch fails to complete if a column cannot hold its rows together, for example when they have more distinct values than the keys of a dictionary can index. A target changes which rows are completed together, so it can cause such a failure, or avoid one; fitting a batch to its key and offset types would be a change for both cases and is not part of this PR.

Are these changes tested?

Yes, 35 tests in arrow-select/src/coalesce.rs, two in coalesce/byte_view.rs and one in coalesce/primitive.rs: cutting wide rows; coalescing across input batches; a row larger than the target; narrow rows still reaching target_batch_size; a stream whose row width changes, including a batch cut before the rows it was allocated for; boolean rows; validity, pending and finished; a batch with the wrong number of columns; the interaction with biggest_coalesce_batch_size, including a slice of a larger buffer; filtered and indexed pushes, including selections of strings and views much wider than the rest of their batch, and indices whose rows an array can only hold a batch, or a row, at a time; setting the target with rows buffered, with and without columns, and unsetting it; that views, lists and structs split from a batch come out in buffers of their own, while a batch of them taken whole stays shared unless its buffers hold more than twice its bytes, including views of one value that fit whole, in the current batch or the next; dictionary values counted once a batch; nested copies of empty lists and of a run array sized by their values; list views left shared; the builders' allocation for the rows and strings they are given; and the string copy splitting at a small buffer limit.

No benchmark is included. Without a target the added work is one call per column when an output batch starts. I have not measured that, nor the cost with a target of sizing each pushed batch, of the copies that replace shared buffers, of shrinking finished batches, and of materializing selections where the direct copies would have run.

Are there any significant user-facing changes?

Two new public methods on BatchCoalescer. Without a target the output batches are the same as before, except that more than 2 GiB of strings compacted from a sparse view source at once now go to more than one buffer.

AI usage disclosure

AI tools were used to write the code, tests and this description. I own this change.

@boring-cyborg boring-cyborg Bot added the arrow-kernels Crates that transform arrow data (e.g. compute, cast) label Nov 29, 2026
…s hold, and size nested copies by their values
…ts whole as shared, and leave list views shared
…rget, and count dictionary values once a batch
…or the expected rows, and check columns before sizing them
…hat a batch whose rows a column cannot hold fails to complete

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

arrow-kernels Crates that transform arrow data (e.g. compute, cast)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BatchCoalescer: add a target batch size in bytes

1 participant