azecevic000 opened a new pull request, #12542:
URL: https://github.com/apache/arrow-rs/pull/12542

   # Which issue does this PR close?
   
   - Closes https://github.com/apache/arrow-rs/issues/3478
   - Closes https://github.com/apache/arrow-rs/issues/9388
   
   This is the continuation and refinement of PR started by @DevMattG in 
https://github.com/apache/arrow-rs/pull/11048. Also applies the idea by 
@Rich-T-kid from https://github.com/apache/arrow-rs/pull/10383.
   
   # Rationale for this change
   
   `FlightDataEncoder` split each `RecordBatch` by an estimate of its size, 
made before the IPC encoding. There are two main related issues with current 
approach:
   - messages could be much larger than `max_flight_data_size` (#3478): the 
assumption is that rows are approximately equal-sized, which can work badly for 
cases where variable-length data-type fields exist (like `Utf8`);
   - batches could unnecessarily be split into many small messages (#9388): 
`get_buffer_memory_size` is used for estimating data size in the buffer, which 
counts the whole buffers of sliced arrays, which results in overestimation of 
data size, sometimes easily by a few orders of magnitude. The most prominent 
and realistic example is re-encoding of IPC decoded batch, where every `Array` 
share the same buffer.
   
   # What changes are included in this PR?
   
   **1. First split, by an estimate:**
   
   ```text
   framing = 128 + 128 * columns                       IPC header and padding
   room    = limit - framing   if 2 * framing <= limit
           = limit             otherwise
   size    = buffer memory     if that is <= room
           = sum of the slice sizes                    get_slice_memory_size; 
for top-level views,
                                                       the bytes that the views 
use
   pieces  = ceil(size / room)                         with equal row counts
   ```
   
   We can call it the optimistic case. This is a refined idea from what the 
main currently does. It introduces framing estimate, which becomes significant 
for very wide and complex schemas. The refinments compared to the main are:
   - take slice size sum if it helps the batch stay under the limit
   - introduce framing overhead estimation; we can generally stop splitting if 
overhead of a single batch becomes large enough (here the factor of 2 was 
choose)
   
   **2. Encode each piece.** Its top-level `Utf8View` and `BinaryView` columns 
are compacted with `gc()` first, when
   `used bytes < buffer bytes`: the IPC writer writes all data buffers of a 
view array, however it is sliced. A
   message with `size > limit` and more than one row is cut into `n = ceil(size 
/ limit)` pieces with equal row
   counts, which are encoded before they are used. Each piece repeats an 
`overhead` that the message has only once
   (the IPC header, and the data that all slices keep):
   
   ```text
   total     = size of the n pieces = size + (n - 1) * overhead
   max_total = size + (n - 1) * limit / 2
   
   all pieces fit       ->  send the pieces
   total > max_total    ->  send the message as-is
   otherwise            ->  handle each piece in the same way
   
       overhead           > limit / 2
   <=> (n - 1) * overhead > (n - 1) * limit / 2
   <=> total - size       > (n - 1) * limit / 2
   <=> total              > max_total
   ```
   
   **Cost.** When the estimate is right (1st step), `encodes = pieces`, as on 
main, and nothing is split again.
   
   This still doesn't make `FlightDataEncoded::max_flight_data_size` a hard 
limit (it would require splitting a single row in the general case), but it 
should generally improve a lot of cases, sometimes dramatically and 
`max_flight_data_size` should be a much better approximation in all cases I 
could think of.
   
   # Are these changes tested?
   
   Yes, there are a lot of new unit tests, since there are a lot of edge cases 
to cover. Some existing tests are also changed (look at the explanation). These 
tests cover both the functional part (round-trip tests), and heuristic part 
(how close are splitted batches to the `max_flight_data_size`).
   
   # Are there any significant user-facing changes?
   
   No API breaking changes. This is a user-facing change in terms of the number 
of batches and their sizes for `FlightDataEncoder` users.
   
   # AI usage disclosure
   
   AI was used for writing the implementation and tests and parts of the 
description of this PR. The design decisions and review of LLM implementation 
were done by me.
   


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