ranflarion opened a new issue, #12508:
URL: https://github.com/apache/arrow-rs/issues/12508

   ### Is your feature request related to a problem or challenge?
   
   `BatchCoalescer` completes an output batch at `target_batch_size` rows and 
has no notion of bytes. With wide rows that makes very large batches: 8,192 
rows of 10,000 `Float64` columns are 655 MB, and 8,192 rows that each hold a 
100 KB list are about 800 MB. Operators downstream then have to buffer, sort or 
spill such a batch whole. DataFusion's sort reserves roughly twice a batch's 
size before buffering it, so one of these batches fails a memory-limited query 
that has plenty of room for the same rows in smaller batches.
   
   A caller cannot fix this from outside. Lowering `target_batch_size` 
penalizes narrow rows, and the row width is not known before the data arrives 
and can change during a stream. Calling `finish_buffered_batch` early does cut 
a batch, but the primitive and view builders reserve `target_batch_size` values 
for every output batch and hand that capacity to the array, so a batch cut at 
100 rows still holds buffers allocated for 8,192.
   
   ### Describe the solution you'd like
   
   An optional byte target next to the row target:
   
   ```rust
   let coalescer = BatchCoalescer::new(schema, 8192)
       .with_target_batch_bytes(Some(32 * 1024 * 1024));
   ```
   
   With a target, an output batch is also completed once the rows buffered for 
it reach that many bytes. Each row counts as an equal share of the batch it 
arrived in (`ArrayData::get_slice_memory_size` divided by the row count), which 
costs one pass over the columns per pushed batch and nothing per row. A batch 
larger than the target is split instead of taking the 
`biggest_coalesce_batch_size` bypass, a single row larger than the target is a 
batch of its own, and each output batch's buffers are allocated for the rows it 
is expected to hold. Without a target nothing changes.
   
   ### Describe alternatives you've considered
   
   - A wrapper around `BatchCoalescer` that measures input batches and calls 
`finish_buffered_batch`. It cannot avoid the full-size allocation described 
above, and it cannot split a filtered push without materializing it first.
   - Exact per-row sizes instead of the batch average. That is precise for rows 
of uneven size, but needs a pass over the offsets of every variable-width 
column, nested ones included. The average keeps the cost per batch and can be 
refined later.
   - A re-chunking operator in the query engine (apache/datafusion#23390 
proposed one for data sources). It bounds what one operator emits, but every 
operator that coalesces after it builds large batches again.
   
   ### Additional context
   
   DataFusion's filter, hash join, sort-merge join and nested loop join build 
their output through `BatchCoalescer`, so a target here gives each of them a 
byte bound with one builder call.
   
   ### Willingness to contribute
   
   I plan to contribute to this feature independently
   


-- 
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]

Reply via email to