ajoakim opened a new issue, #12393:
URL: https://github.com/apache/arrow-rs/issues/12393
**Describe the bug**
When writing `Decimal128` (and other types that go through
`FixedLenByteArray`) with `ArrowWriter`, column-chunk statistics keep the whole
per-write value buffer alive until the column chunk is closed.
In `parquet/src/arrow/arrow_writer/mod.rs`, `chunk_array_slice` builds one
contiguous `Vec<u8>` for all values of a write. `chunk_contiguous_vec` then
turns it into a `Bytes` and hands out each value as
`arena.split_to(chunk_size)`, so every `FixedLenByteArray` is a zero-copy slice
of that shared buffer.
In `parquet/src/column/writer/mod.rs`, `update_min` / `update_max` /
`update_stat` store the new min/max with `val.clone()`. For `ByteArray` /
`FixedLenByteArray` that clone shares the underlying `Bytes`, so the chunk's
`min_column_value` / `max_column_value` pin the entire buffer of the write that
produced them (for a 65,536-value batch of Decimal128 that is ~1 MiB per
column), not just 16 bytes.
The effect grows with the number of open column chunks: with many partition
files open at once (one writer per partition, each with an unflushed row
group), peak heap grows by roughly one write buffer per decimal column per open
file.
**To Reproduce**
1. Create an `ArrowWriter` with statistics enabled (the default) for a
schema with a `Decimal128(38, 2)` column.
2. Write a large `RecordBatch` (e.g. 65,536 rows) and keep the writer open
without flushing the row group.
3. Drop the batch. Heap usage stays at roughly the size of the batch's
16-byte-per-value encoding buffer until the row group is flushed, because the
chunk min/max `FixedLenByteArray` still references it.
4. Setting statistics to `None` for that column removes the extra retention.
We measured this with glibc `mallinfo2` in a pull pipeline keeping ~480
partition writers open: disabling statistics only for the decimal amount column
cut the extra peak heap per open file from about +1.6 MiB to about +0.2 MiB.
**Expected behavior**
Statistics should hold an owned copy of only the min/max value bytes, so the
per-write buffer can be freed after the values are encoded.
**Additional context**
A minimal fix we are using locally (only the stats path copies, and only
when the min/max actually changes):
```rust
// data_type.rs
impl ByteArray {
pub(crate) fn unshare(&mut self) {
if let Some(d) = &self.data {
self.data = Some(Bytes::copy_from_slice(d));
}
}
}
// column/writer/mod.rs: use in update_min / update_max / update_stat
instead of val.clone()
fn owned_stat<T: ParquetValueType>(val: &T) -> T {
let mut out = val.clone();
let any = out.as_mut_any();
if let Some(b) = any.downcast_mut::<ByteArray>() {
b.unshare();
} else if let Some(f) = any.downcast_mut::<FixedLenByteArray>() {
f.unshare();
}
out
}
```
Observed on parquet 60.0.0; the same code is present in 60.1.0. Happy to
open a PR if this approach is acceptable.
--
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]