ranflarion opened a new pull request, #28025:
URL: https://github.com/apache/datafusion/pull/28025

   ## Which issue does this PR close?
   
   - Closes #28024.
   
   ## Rationale for this change
   
   The sort-merge join reserves each buffered batch by 
`get_array_memory_size()`, which charges an allocation once per buffer that 
views it. A batch decoded from uncompressed Arrow IPC, from an Arrow file or an 
uncompressed spill file, keeps all its columns in one allocation, so the join 
reserves a batch of N columns about N times and spills or fails where it fits. 
In the issue's reproducer, a join over an Arrow file of 4.3 MB batches reserves 
563 MB for two of them on main and fails in a 64 MB pool with spilling 
disabled. With this PR it peaks at 8.7 MB in every configuration and does not 
spill.
   
   ## What changes are included in this PR?
   
   `BufferedBatch::try_new` counts the key arrays and then the batch with one 
`RecordBatchMemoryCounter`, so each allocation counts once and a key column 
that is one of the batch's own columns is no longer counted twice.
   
   `restore_spilled_batches` charges a batch it reads back from the join's 
spill file by the buffers of that copy, measured with 
`get_record_batch_memory_size`, on top of the key arrays that stayed in memory, 
instead of the estimate taken before the spill. The copy shares no allocation 
with the original, so columns that viewed one buffer come back with one each, 
and the new estimate would undercount them.
   
   The count stays per batch. `RecordBatchMemoryTracker` across the buffered 
batches would also catch batches that are slices of one parent, but the join 
reserves, spills and frees each batch on its own. `BitwiseSortMergeJoinStream` 
(semi, anti, mark) sizes its key-group slices with `get_array_memory_size()` 
too and is not changed here.
   
   ## What is the testing strategy for this PR?
   
   New tests in `joins/sort_merge_join/tests.rs`:
   
   - `buffered_columns_sharing_one_allocation_are_reserved_once` buffers a 
batch whose three columns slice one allocation, in a pool with room for it 
twice. It fails on main, where the join spills.
   - `restored_batch_is_reserved_by_its_own_buffers` spills a batch whose eight 
columns view one buffer and checks `peak_mem_used` covers the copy read back. 
It fails if the restore charges the new estimate.
   
   `spill_join_arrays_memory_accounting`, `spill_read_back_memory_accounting` 
and `spill_read_back_single_source` compute the expected estimate the new way. 
`overallocation_multi_batch_spill`, `spill_with_filter_multi_batch` and 
`materializing_spill_pending_stream` lower their pool from 500 to 100 bytes, 
since their batches now fit in 500. In `explain_analyze.slt` the filtered inner 
join's `peak_mem_used` goes from 432 B to 80 B.
   
   ## Are there any user-facing changes?
   
   No API changes. Sort-merge joins reserve less for their buffered batches, so 
they report a lower `peak_mem_used` and spill less.
   


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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to