Repository navigation
feat(arrow-select): add a target batch size in bytes to BatchCoalescer - #12509
Open
ranflarion wants to merge 15 commits into
Open
ranflarion wants to merge 15 commits into
ranflarion wants to merge 15 commits into
Conversation
… count rows by a bytes-to-rows ratio
… its data buffers
…s hold, and size nested copies by their values
…ts whole as shared, and leave list views shared
…batch for views that fit one whole
…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
…s with fewer rows
…ce its bytes, as the view builder does
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.
Which issue does this PR close?
Rationale for this change
BatchCoalescercuts output batches by rows only, so wide rows give batches of hundreds of MB: 8,192 rows of 10,000Float64columns are 655 MB. A caller cannot bound them from outside, because the builders allocatetarget_batch_sizevalues 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_bytesand atarget_batch_bytesgetter. With a target,push_batchalso completes a batch once the buffered rows reach it.push_batch_with_filterandpush_batch_with_indicesmaterialize 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.ArrayData::get_slice_memory_size(falling back toget_array_memory_sizefor layouts it does not size), with two exceptions that count what an output batch keeps: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.start_arrayalso 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:biggest_coalesce_batch_sizebypass 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.biggest_coalesce_batch_sizebypass, so a small slice of a large allocation is copied.i32::MAXbytes, as the indexed copy does. It wrote each offset asu32into 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_sizecounts 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 incoalesce/byte_view.rsand one incoalesce/primitive.rs: cutting wide rows; coalescing across input batches; a row larger than the target; narrow rows still reachingtarget_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 withbiggest_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.