adriangb commented on code in PR #12184:
URL: https://github.com/apache/arrow-rs/pull/12184#discussion_r4722409070
##########
arrow-select/src/minify.rs:
##########
@@ -0,0 +1,2608 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! [`minify`]: Copy an array into buffers that hold only the data its rows
reference
+//!
+//! Arrays often keep memory alive that their visible rows do not use:
+//!
+//! * A sliced array shares the buffers of the original, unsliced array.
+//! * A sliced list, map or byte array shares the full child array or value
buffer.
+//! * A view array can reference a small part of large data buffers.
+//! * A dictionary can hold values that no key references.
+//! * A dense union can hold child rows that no row references.
+//!
+//! [`minify`] returns a logically equal array without this unused memory.
+//!
+//! # Reuse policy
+//!
+//! [`minify`] copies a buffer only when its allocation is larger than the
+//! visible data needs (rounded up to 64 bytes). Otherwise it reuses the buffer
+//! without a copy. This makes `minify` cheap on an array that is already
+//! minimal, and calling it twice copies nothing the second time.
+//!
+//! A buffer that other arrays share counts as too large when its allocation
+//! is larger than the visible data. `minify` copies it, so the total memory in
+//! use can grow until the other arrays drop the shared buffer.
+//!
+//! A buffer can also appear more than once inside `array`, for example in
+//! two struct columns over the same array, or in two dictionaries over the
+//! same values. Struct columns over the same rows of the same buffers share
+//! one minified column. In other cases `minify` can copy the buffer once per
+//! reference. If the result then uses more memory than `array` (beyond 64
+//! bytes per allocation), `minify` returns `array` unchanged.
+//!
+//! # Null rows
+//!
+//! `minify` gives every null row of a byte, list, map or list view array an
+//! empty value range, and every null row of a byte view array an empty view.
+//! The child array or value buffers then hold only the data of valid rows.
+//!
+//! An O(rows) check over the offsets and the null bitmap finds out if any null
+//! row has a non-empty range. If none has, `minify` copies or slices the
+//! referenced range in one piece. Otherwise it copies the ranges of each run
of
+//! valid rows.
+//!
+//! # Limitations
+//!
+//! * `minify` does not deduplicate dictionary values or merge equal runs.
+//! * Null rows of a struct or fixed size list array keep their child data.
+//! The child rows stay at the same positions, so `minify` cannot drop them.
+//! * The rows of a sparse union keep the data of all children, also of the
+//! children that their type ids do not select.
+
+use std::collections::HashSet;
+use std::ops::Range;
+use std::sync::Arc;
+
+use arrow_array::cast::AsArray;
+use arrow_array::types::*;
+use arrow_array::*;
+use arrow_buffer::{
+ ArrowNativeType, BooleanBuffer, BooleanBufferBuilder, Buffer, NullBuffer,
OffsetBuffer,
+ ScalarBuffer, bit_util,
+};
+use arrow_data::transform::{Capacities, MutableArrayData};
+use arrow_data::{ArrayData, ByteView, MAX_INLINE_VIEW_LEN};
+use arrow_schema::{
+ ArrowError, DataType, FieldRef, IntervalUnit, TimeUnit, UnionFields,
UnionMode,
+};
+
+use crate::take::take;
+
+/// Returns an array that is logically equal to `array` and whose buffers hold
+/// only the data that its rows reference.
+///
+/// The result has an offset of zero, except when `minify` returns `array`
+/// unchanged because aliased buffers would make the result larger (see the
+/// [module documentation](self)). `minify` applies itself recursively to
+/// child arrays, dictionary values and run-end encoded values.
+///
+/// See the [module documentation](self) for the policy on when `minify`
+/// reuses a buffer instead of copying it.
+///
+/// # Example
+///
+/// ```
+/// # use arrow_array::{Array, Int32Array};
+/// # use arrow_select::minify::minify;
+/// let array = Int32Array::from_iter_values(0..1024);
+/// let sliced = array.slice(10, 4);
+/// // The slice keeps the whole 4 KiB buffer alive
+/// assert_eq!(sliced.get_buffer_memory_size(), 4096);
+///
+/// let minified = minify(&sliced).unwrap();
+/// assert_eq!(minified.as_ref(), &sliced as &dyn Array);
+/// assert_eq!(minified.get_buffer_memory_size(), 64);
+/// ```
+///
+/// # Errors
+///
+/// `minify` returns `InvalidArgumentError` for a time type with an invalid
+/// unit, a dictionary with an invalid key type and a run-end encoded array
+/// with an invalid run end type. It also returns the error of an array
+/// constructor that rejects the minified parts.
Review Comment:
Simplified the doc, it now just says it errors on invalid data types. I
think we should keep `Result`: `take` and `filter` return one too, and it
leaves us room to add real error cases later without breaking the signature.
--
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]