Skip to content

feat(arrow-select): add compacted_size, row_sizes and split_by_size next to minify - #12190

Draft
adriangb wants to merge 10 commits into
apache:mainfrom
adriangb:minify-sizes
Draft

adriangb wants to merge 10 commits into
apache:mainfrom
adriangb:minify-sizes

Conversation

@adriangb

@adriangb adriangb commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Stacked on #12184. GitHub cannot stack a PR on another PR from a fork, so this PR targets main. Please review only the 4 commits after the 6 minify commits:

  1. feat: add compacted_size, row_sizes and split_by_size with their tests
  2. bench: compare compacted_size and row_sizes with minify
  3. fix: review follow-ups. Null view rows pay no bytes, sums saturate, and the docs state the limits of a split
  4. perf: row_sizes and compacted_size size only the used span of dictionary values and dense union child rows

Rationale for this change

Callers often need the size of an array after minify without paying for the copy. Examples are splitting a large batch into pieces under a byte limit before writing it, and knowing when an output builder has collected enough rows. get_slice_memory_size cannot answer this. It counts list children, list view children, dense union children, run-end encoded values, dictionary values and view data buffers in full.

What changes are included in this PR?

Three functions in arrow_select::minify:

  • compacted_size(&dyn Array) -> Result<usize, ArrowError> returns the exact buffer bytes that minify produces, without copying. It counts unpadded buffer lengths, not the size of an IPC message.
  • row_sizes(&dyn Array, &mut [u64]) -> Result<(), ArrowError> adds the compacted size of each row into the slice. A caller can sum all columns of a batch into one slice.
  • split_by_size(&[u64], max_size) -> Vec<Range<usize>> splits rows greedily into ranges under max_size. A row larger than the limit gets its own range.

minify and the size functions share the helpers that decide which rows and bytes are used, so they cannot disagree.

row_sizes charges shared data (dictionary values, run-end encoded runs, shared dense union child rows, overlapping list views, shared view bytes) to every row that references it. It can overestimate for long runs or for large dictionary values that many rows reference, and then split_by_size can return one row per range. It can also underestimate by a small fixed amount per range: each offset buffer holds one offset more than its rows, and each bitmap can need one byte more than its charge. So max_size is a target, not a hard cap. The doc comments of row_sizes and split_by_size state the bound. Null rows of byte and byte view arrays pay no value bytes, because minify empties them.

Are these changes tested?

Yes. Unit tests check exact values. A seeded random test over every supported type checks that compacted_size(a) equals the buffer bytes of minify(a), and that the sum of row_sizes over a range stays within the documented bound of compacted_size of that slice. Fuzzing, longer property runs and mutation testing were run outside the repository, and their findings are in the unit tests.

Are there any significant user-facing changes?

New public API only: compacted_size, row_sizes and split_by_size in arrow_select::minify.

AI usage disclosure

Claude Code wrote the implementation, tests and this description under my direction.

🤖 Generated with Claude Code

@adriangb

Copy link
Copy Markdown
Contributor Author

Benchmark results for arrow/benches/minify_kernels.rs, on a GCP c4a-standard-16 VM. Each case slices 8192 rows from a parent of 100k rows unless noted.

Results
Case minify compacted_size row_sizes
i32 564 ns 6 ns 1.60 µs
boolean 189 ns 6 ns 1.39 µs
fixed size binary 1.83 µs 6 ns 1.60 µs
fixed size list 2.05 µs 30 ns 29 µs
utf8, no nulls 29.9 µs 8 ns 1.92 µs
utf8, null ranges 54.1 µs 12.4 µs 7.4 µs
utf8view 25.9 µs 1.67 µs 5.2 µs
utf8view, sparse take 33.2 µs 1.67 µs 5.2 µs
list 252 µs 45.7 µs 79.0 µs
list view 134 µs 32.2 µs 31.3 µs
dense union 66.9 µs 62.0 µs 106.6 µs
dictionary, few referenced 27.8 µs 8.7 µs 31.9 µs
struct<i64, utf8> 30.3 µs 1.77 µs 13.3 µs
run-end encoded 2.72 µs 108 ns 5.5 µs
list, already minimal 8.50 µs 9.37 µs 68.6 µs
utf8, already minimal 11.7 µs 12.4 µs 7.4 µs
utf8, 4M rows with null ranges 178.6 ms 6.32 ms 5.03 ms

@boring-cyborg boring-cyborg Bot added arrow-core Core crates related to representing arrow data arrow-kernels Crates that transform arrow data (e.g. compute, cast) labels Nov 11, 2026
adriangb and others added 3 commits November 11, 2026 17:21
Sliced arrays and view arrays can keep memory alive that their rows do not
reference. Callers such as spilling and IPC writers each work around this on
their own.

`minify` returns a logically equal array with offset zero whose buffers hold
only referenced data. This commit adds the reuse policy and the null buffer
handling, and supports the flat types: primitive, temporal, decimal,
interval, boolean, null, fixed size binary, byte and byte view arrays. Null
rows of byte arrays get empty ranges. `view_data_len` and
`keeps_view_buffers` hold the view array decision, so that later code can
share it.

The dispatch matches every `DataType` variant without a wildcard, so a new
variant in arrow-schema fails to compile until `minify` handles it. Nested,
union, dictionary and run-end encoded types return `NotYetImplemented`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oded types in `minify`

Sliced lists and maps share the full child array. Dictionaries can hold
values that no key references, and dense unions can hold child rows that no
row references. `minify` needs to recurse into these types to drop that
memory.

This commit adds List, LargeList, Map, FixedSizeList, Struct, ListView,
LargeListView, sparse and dense Union, Dictionary and RunEndEncoded. Null
rows of lists and maps get empty ranges. These types share one gather path,
`gather_ranges`. It gathers dense unions, list views and run-end encoded
arrays by their parts, so child rows that several rows share stay shared.
`list_view_child` and `dense_union_rows` hold the child row selection of
list views and dense unions, so that later code can share it.

`minify`, `gathers_parts` and `gather_ranges` match every `DataType`
variant without a wildcard. The dispatch passes the list field and the
union fields to the helpers, so they need no `unreachable!` arm. Invalid
dictionary key, run end and time unit types return `InvalidArgumentError`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `minify` kernel has no benchmarks, so performance changes cannot be
measured. This adds `minify_kernels` with slices of 8192 rows from parents
of 100k rows for each main type, an already minimal input, null-heavy
strings with non-empty null ranges, and one large 4M-row case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…iased buffers

Review found inputs where `minify` kept or created memory that the rows do not need.
Repeated views counted shared bytes once per view, so `minify` kept a 1 MB buffer for 100 distinct bytes or made `gc` write the bytes once per view.
Null view rows kept their bytes.
The slack of 64 bytes per data buffer kept thousands of unreferenced small buffers.
A buffer aliased twice inside one array was copied twice, so memory grew.
The dense union lookup table was as long as the child.

View arrays now merge the distinct ranges that valid views reference.
`minify` keeps a data buffer only if the views reference all of its bytes and it is not oversized.
It copies the other ranges once into new buffers and gives null rows an empty view.
Equal struct columns share one minified column.
If the result still uses more memory than the input, `minify` returns the input unchanged.
The dense union table spans only the visible offsets.
The module doc lists the parents that keep child data of null rows.
The `allocated` test helper counts each allocation once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
adriangb and others added 6 commits November 13, 2026 19:14
…in `minify`

`gather_run` copied a run value once per gathered range. A list whose null rows keep their child ranges, or a dense union over a run-end encoded child, then gathered many ranges of one run. A 183 KB input reached a 1 GB peak, and the fallback returned the input unchanged although nothing was aliased.

A range that starts in the run where the previous range ended now extends that run.
Dense union child rows are numbered in ascending order, so gathered ranges never go back and coalesce.
The offsets keep the logical rows unchanged, and a child that is used in full stays shared.
`view_data` adds view offsets and lengths as `usize`, because a data buffer can be longer than `u32::MAX`.
Dictionary garbage collection costs O(keys + span of used keys) and not O(values).

New tests pin the run fix, the view range math, the fallback threshold, the null comparisons of `same_data`, the paths for all-valid null buffers, `push_range`, `gathers_parts` and the exact byte capacity of a gather.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The cases cover already compact arrays, small slices of large utf8, view, dictionary and dense union parents, reversed views and arrays with one data buffer per row. `gc`, `take` and `garbage_collect_dictionary` run as baselines that do comparable work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ize` next to `minify`

DataFusion needs to know how large an array would be after `minify`
without the copy. It splits large batches into pieces under a byte limit
for spilling, and it predicts the decoded size of a spill. Operators that
build output with `take` or `interleave` also need per-row sizes to know
when their output reaches a byte limit.

`compacted_size` returns the buffer bytes of `minify` exactly. It walks
the array like `minify` and calls the same selection helpers. It models
`minify` of gathered rows with a list of row ranges, so nested arrays,
dictionaries, dense unions and runs follow the same path as in `minify`.

`row_sizes` adds the compacted bytes of each row to a slice, so a caller
can sum the columns of a batch. Shared data (dictionary values, runs,
dense union child rows, overlapping list views and shared view bytes)
counts once for each row that references it. The doc comment states the
error bound against `compacted_size` of a slice. `split_by_size` cuts
rows into ranges under a byte limit.

A seeded random test generates arrays of every supported type with random
nulls, null ranges, slices and nesting. It checks `compacted_size`
against `minify` and the row size bound on random slices. Set
`MINIFY_SIZES_ITERATIONS` and `MINIFY_SIZES_SEED` for a longer run.

Both size walks match every `DataType` variant without a wildcard, like
`minify`, so a new variant fails to compile until they handle it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Callers choose between `compacted_size` and `minify` by cost, so the
benchmark runs `minify`, `compacted_size` and `row_sizes` on the same
inputs. It adds slices of boolean, fixed size binary, fixed size list,
view and struct arrays. The existing `minify` benchmarks keep their names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`minify` gives a null view row an empty view, but `row_sizes` charged its bytes.
Now a null view row pays only its view.

`split_by_size` and the `row_sizes` sums overflowed on large sizes.
They now saturate, and `split_by_size` starts a new range when the sum would overflow.

The docs now say that a range of `split_by_size` can compact to more than `max_size`, that shared data can make every range one row long, and that `compacted_size` is unpadded buffer bytes and not an IPC size.
Tests pin the view sizes over many small buffers and over shared bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cted_size`

`row_sizes` sized every dictionary value and every dense union child row, so a 100 row slice of a dictionary with 10M values took 28 ms and 80 MB.
`add_dictionary` and the dense union arm now size only the span between the smallest and largest used key or offset.
`dictionary_size` builds one mask over the span of the used keys, as `minify` does.
Tests pin the sizes of small slices of large parents, and the sizes of many gathered ranges in one run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriangb
adriangb force-pushed the minify-sizes branch 2 times, most recently from 7e93f2e to ea3c990 Compare November 14, 2026 01:24

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-core Core crates related to representing arrow data arrow-kernels Crates that transform arrow data (e.g. compute, cast)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant