Skip to content

fix(arrow-flight): Improve FlightDataEncoded::max_flight_data_size semantics - #12542

Open
azecevic000 wants to merge 2 commits into
apache:mainfrom
azecevic000:flight-limit-small
Open

azecevic000 wants to merge 2 commits into
apache:mainfrom
azecevic000:flight-limit-small

Conversation

@azecevic000

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

This is the continuation and refinement of PR started by @DevMattG in #11048. Also applies the idea by @Rich-T-kid from #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:

What changes are included in this PR?

1. First split, by an estimate:

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

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.

DevMattG and others added 2 commits December 3, 2026 15:38
… size

`split_batch_for_grpc_response` only estimates a RecordBatch's encoded
size from `get_buffer_memory_size()`, which ignores IPC framing/padding
overhead and can badly undercount for variable-length data (dictionaries,
strings). This let `FlightDataEncoder` silently emit `FlightData` messages
larger than `max_flight_data_size` (see apache#3478).

`FlightDataEncoder::encode_batch` now delegates to a new
`encode_piece_adaptive`, which encodes each heuristic-split piece, queues
any dictionaries unconditionally (they're keyed by id and correct to send
regardless of how the batch body ends up sliced), and if the *actual*
encoded size still exceeds the limit, re-splits, recursively if necessary.
Continue apache#11048, which encodes each piece and splits a message that is
larger than the limit again. Make the first split fit, and keep the
splits from sending data again with every row:

- Estimate the first split from the slices of the arrays
  (`ArrayData::get_slice_memory_size`, the idea of apache#10383), with room
  for the IPC framing of the message and of each column. Decoded and
  sliced batches are then no longer split into many small messages.
- Compact the top-level view columns of a piece with `gc()` when that
  makes them smaller: the IPC writer writes all data buffers of a view
  array, however it is sliced.
- Encode the pieces of a message over the limit before they are used.
  If they do not all fit and the overhead of each piece, for example a
  large IPC header or the child of a list view array, is more than half
  of the limit, send the message as-is.
- Encode the pieces of a split message without reserving the scratch
  buffer of the IPC writer: every body would start with at least the
  capacity of the split message, which is over the limit.

Document the limit and its exceptions.
@boring-cyborg boring-cyborg Bot added the arrow-flight arrow-flight crate label Dec 3, 2026
@azecevic000

Copy link
Copy Markdown
Contributor Author

Thank you for the patience, it should be ready for review.

CC @Rich-T-kid

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-flight arrow-flight crate

Projects

None yet

2 participants