orxh opened a new issue, #12530:
URL: https://github.com/apache/arrow-rs/issues/12530
### Is your feature request related to a problem or challenge?
After a sparse `take`, `BatchCoalescer` can compact the data buffers of a
top-level `Utf8View` column. With the same input values and row selection,
`List<Utf8View>` and `Struct<Utf8View>` each keep the full source data buffers
alive.
During hash repartitioning, each partition can keep the full source data
buffers alive even if it receives only a small fraction of the input rows.
Memory accounting can also count the same shared buffers once per partition.
The following reproduces on `arrow` 60.2.0 and `main` at `0f1fd354bb`:
```rust
use std::sync::Arc;
use arrow::array::{
Array, ArrayRef, AsArray, ListArray, RecordBatch, StringViewArray,
StructArray, UInt32Array,
};
use arrow::buffer::OffsetBuffer;
use arrow::compute::{BatchCoalescer, take_record_batch};
use arrow::datatypes::{DataType, Field};
fn data_bytes(array: &dyn Array) -> usize {
array
.as_string_view()
.data_buffers()
.iter()
.map(|b| b.len())
.sum()
}
fn coalesce_sparse_take(column: ArrayRef) -> ArrayRef {
let batch = RecordBatch::try_from_iter([("c", column)]).unwrap();
// Keep 10% of rows, exceeding the coalescer's 2x compaction threshold.
let indices = UInt32Array::from_iter_values((0..1000).step_by(10));
let mut coalescer = BatchCoalescer::new(batch.schema(), 1024);
coalescer
.push_batch(take_record_batch(&batch, &indices).unwrap())
.unwrap();
coalescer.finish_buffered_batch().unwrap();
coalescer.next_completed_batch().unwrap().column(0).clone()
}
fn main() {
// Exceed the 12-byte inline limit so strings are stored in data buffers.
let strings = StringViewArray::from_iter_values((0..1000).map(|i|
format!("{i:0>100}")));
let input_bytes = data_bytes(&strings);
let top_level = coalesce_sparse_take(Arc::new(strings.clone()));
let field = Arc::new(Field::new_list_field(DataType::Utf8View, false));
let list = ListArray::new(
field,
OffsetBuffer::from_lengths([1; 1000]),
Arc::new(strings.clone()),
None,
);
let list = coalesce_sparse_take(Arc::new(list));
let field = Arc::new(Field::new("s", DataType::Utf8View, false));
let struct_array = StructArray::from(vec![(field, Arc::new(strings) as
ArrayRef)]);
let struct_array = coalesce_sparse_take(Arc::new(struct_array));
println!("input: {} bytes", input_bytes);
println!("bytes kept alive by the 100-row output, after the input is
dropped:");
println!("Utf8View: {} bytes", data_bytes(&top_level));
println!(
"List<Utf8View>: {} bytes",
data_bytes(list.as_list::<i32>().values())
);
println!(
"Struct<Utf8View>: {} bytes",
data_bytes(struct_array.as_struct().column(0))
);
}
```
Output:
```
input: 100000 bytes
bytes kept alive by the 100-row output, after the input is dropped:
Utf8View: 10000 bytes
List<Utf8View>: 100000 bytes
Struct<Utf8View>: 100000 bytes
```
The top-level result retains only the roughly 10,000 bytes selected by the
`take`. The list and struct results should do the same, but instead retain all
100,000 input bytes.
### Describe the solution you'd like
Extend `BatchCoalescer`’s existing compaction heuristic to `Utf8View` and
`BinaryView` arrays nested at any depth within `List`, `LargeList`,
`FixedSizeList`, `Map`, and `Struct`.
For each nested view array with non-inline values (longer than 12 bytes),
compact its data buffers if their total capacity exceeds twice
`total_buffer_bytes_used()`, the number of bytes used by those values. At or
below that threshold, reuse the existing data buffers.
Nested view compaction should happen automatically through existing
`BatchCoalescer` calls.
### Describe alternatives you've considered
1. **Compact finished columns (preferred).** In `finish_buffered_batch`, a
crate-private helper would walk each assembled column that contains nested
views and apply the 2× threshold to its view arrays. The coalescer would keep
the source buffers alive until it finishes assembling the output column.
2. **Use the proposed `minify` kernel.** Build on #7186 and #12184. The
current proposal compacts view arrays whenever they have unreferenced bytes.
The coalescer would need a way to apply its 2× threshold when using the kernel.
3. **Compact while building columns.** Give nested columns child builders so
`InProgressByteViewArray` can compact view buffers as rows are added. This
could release source buffers sooner and would require changes to how the
coalescer builds nested columns.
### Additional context
- #11635 (and draft PR #11655) tracks the analogous top-level issue on the
sparse `push_batch_with_filter` path.
- #7186 (with open PR #12184) proposes a general `minify` kernel with
recursive view compaction, but does not integrate it with `BatchCoalescer`.
- Part of #7761.
### Willingness to contribute
I would like to contribute to this feature, but request guidance from the
community
--
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]