vortex-array 0.54.0

Vortex in memory columnar data format
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use num_traits::Zero;
use vortex_dtype::{Nullability, match_each_integer_ptype};
use vortex_error::VortexResult;
use vortex_scalar::Scalar;

use crate::arrays::{ListViewArray, ListViewRebuildMode, ListViewVTable};
use crate::compute::{self, TakeKernel, TakeKernelAdapter};
use crate::vtable::ValidityHelper;
use crate::{Array, ArrayRef, IntoArray, register_kernel};

/// The threshold for triggering a rebuild of the [`ListViewArray`].
///
/// By default, we will not touch the underlying `elements` array of the [`ListViewArray`] since it
/// can be potentially expensive to reorganize the array based on what views we have into it.
///
/// However, we also do not want to carry around a large amount of garbage data. Below this
/// threshold of the density of the selection mask, we will rebuild the [`ListViewArray`], removing
/// any garbage data.
#[allow(unused)]
const REBUILD_DENSITY_THRESHOLD: f64 = 0.1;

/// [`ListViewArray`] take implementation.
///
/// This implementation is deliberately simple and read-optimized. We just take the `offsets` and
/// `sizes` at the requested indices and reuse the original `elements` array. This works because
/// `ListView` (unlike `List`) allows non-contiguous and out-of-order lists.
///
/// We don't slice the `elements` array because it would require computing min/max offsets and
/// adjusting all offsets accordingly, which is not really worth the small potential memory we would
/// be able to get back.
///
/// The trade-off is that we may keep unreferenced elements in memory, but this is acceptable since
/// we're optimizing for read performance and the data isn't being copied.
impl TakeKernel for ListViewVTable {
    fn take(&self, array: &ListViewArray, indices: &dyn Array) -> VortexResult<ArrayRef> {
        let elements = array.elements();
        let offsets = array.offsets();
        let sizes = array.sizes();

        // Compute the new validity by combining the array's validity with the indices' validity.
        let new_validity = array.validity().take(indices)?;

        // Take the offsets and sizes arrays at the requested indices.
        // Take can reorder offsets, create gaps, and may introduce overlaps if the `indices`
        // contain duplicates.
        let nullable_new_offsets = compute::take(offsets.as_ref(), indices)?;
        let nullable_new_sizes = compute::take(sizes.as_ref(), indices)?;

        // Since `take` returns nullable arrays, we simply cast it back to non-nullable (filled with
        // zeros to represent null lists).
        let new_offsets = match_each_integer_ptype!(nullable_new_offsets.dtype().as_ptype(), |O| {
            compute::fill_null(
                &nullable_new_offsets,
                &Scalar::primitive(O::zero(), Nullability::NonNullable),
            )?
        });
        let new_sizes = match_each_integer_ptype!(nullable_new_sizes.dtype().as_ptype(), |S| {
            compute::fill_null(
                &nullable_new_sizes,
                &Scalar::primitive(S::zero(), Nullability::NonNullable),
            )?
        });

        // SAFETY: Take operation maintains all `ListViewArray` invariants:
        // - `new_offsets` and `new_sizes` are derived from existing valid child arrays.
        // - `new_offsets` and `new_sizes` are non-nullable.
        // - `new_offsets` and `new_sizes` have the same length (both taken with the same
        //   `indices`).
        // - Validity correctly reflects the combination of array and indices validity.
        let new_array = unsafe {
            ListViewArray::new_unchecked(elements.clone(), new_offsets, new_sizes, new_validity)
        };

        // TODO(connor)[ListView]: Ideally, we would only rebuild after all `take`s and `filter`
        // compute functions have run, at the "top" of the operator tree. However, we cannot do this
        // right now, so we will just rebuild every time (similar to `ListArray`).

        Ok(new_array
            .rebuild(ListViewRebuildMode::MakeZeroCopyToList)
            .into_array())
    }
}

register_kernel!(TakeKernelAdapter(ListViewVTable).lift());