vortex-array 0.94.0

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

use std::hash::Hasher;
use std::sync::Arc;

use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure_eq;
use vortex_error::vortex_err;
use vortex_error::vortex_panic;
use vortex_session::registry::CachedId;

use crate::ArrayEq;
use crate::ArrayHash;
use crate::ArrayRef;
use crate::EqMode;
use crate::ExecutionCtx;
use crate::ExecutionResult;
use crate::IntoArray;
use crate::array::Array;
use crate::array::ArrayId;
use crate::array::ArrayParts;
use crate::array::ArrayView;
use crate::array::VTable;
use crate::array::with_empty_buffers;
use crate::arrays::Primitive;
use crate::arrays::list::ListArraySlotsExt;
use crate::arrays::list::ListData;
use crate::arrays::list::ListSlots;
use crate::arrays::listview::list_view_from_list;
use crate::buffer::BufferHandle;
use crate::builders::ArrayBuilder;
use crate::dtype::DType;
use crate::dtype::PType;
use crate::match_each_list_builder;
use crate::require_child;
mod operations;
mod plugin;
mod validity;
/// A [`List`]-encoded Vortex array.
pub type ListArray = Array<List>;

#[derive(Clone, prost::Message)]
pub struct ListMetadata {
    #[prost(uint64, tag = "1")]
    elements_len: u64,
    #[prost(int32, tag = "2")]
    offset_ptype: i32,
}

impl ListMetadata {
    fn get_offset_ptype(&self) -> VortexResult<PType> {
        PType::try_from(self.offset_ptype).map_err(|_| {
            vortex_err!(
                "ListMetadata offset_ptype must be a known PType, got {}",
                self.offset_ptype
            )
        })
    }
}

impl ArrayHash for ListData {
    fn array_hash<H: Hasher>(&self, _state: &mut H, _accuracy: EqMode) {}
}

impl ArrayEq for ListData {
    fn array_eq(&self, _other: &Self, _accuracy: EqMode) -> bool {
        true
    }
}

impl VTable for List {
    type TypedArrayData = ListData;

    type OperationsVTable = Self;
    type ValidityVTable = Self;
    fn id(&self) -> ArrayId {
        static ID: CachedId = CachedId::new("vortex.list");
        *ID
    }

    fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
        0
    }

    fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
        vortex_panic!("ListArray buffer index {idx} out of bounds")
    }

    fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
        vortex_panic!("ListArray buffer_name index {idx} out of bounds")
    }

    fn with_buffers(
        &self,
        array: ArrayView<'_, Self>,
        buffers: &[BufferHandle],
    ) -> VortexResult<ArrayParts<Self>> {
        with_empty_buffers(self, array, buffers)
    }

    fn validate(
        &self,
        _data: &ListData,
        dtype: &DType,
        len: usize,
        slots: &[Option<ArrayRef>],
    ) -> VortexResult<()> {
        vortex_ensure_eq!(slots.len(), ListSlots::COUNT);
        let elements = slots[ListSlots::ELEMENTS]
            .as_ref()
            .vortex_expect("ListArray elements slot");
        let offsets = slots[ListSlots::OFFSETS]
            .as_ref()
            .vortex_expect("ListArray offsets slot");
        vortex_ensure_eq!(
            offsets.len().saturating_sub(1),
            len,
            "ListArray length does not match outer length",
        );

        let actual_dtype = DType::List(Arc::new(elements.dtype().clone()), dtype.nullability());
        vortex_ensure_eq!(
            &actual_dtype,
            dtype,
            "ListArray dtype does not match outer dtype",
        );

        Ok(())
    }

    fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
        ListSlots::NAMES[idx].to_string()
    }

    fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
        let array = require_child!(array, array.offsets(), ListSlots::OFFSETS => Primitive);

        Ok(ExecutionResult::done(
            list_view_from_list(array, ctx)?.into_array(),
        ))
    }

    // The complexity comes from the expansion of `match_each_list_builder!`.
    #[expect(clippy::cognitive_complexity)]
    fn append_to_builder(
        array: ArrayView<'_, Self>,
        builder: &mut dyn ArrayBuilder,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<()> {
        match match_each_list_builder!(&mut *builder, |b| b.append_list_array(array, ctx)) {
            Some(result) => result,
            None => vortex_bail!(
                "cannot append a List array of dtype {} to a {} builder",
                array.dtype(),
                builder.dtype()
            ),
        }
    }
}

#[derive(Clone, Debug)]
pub struct List;

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::LazyLock;

    use prost::Message;
    use vortex_error::VortexResult;
    use vortex_session::VortexSession;

    use super::List;
    use super::ListMetadata;
    use crate::ArrayRef;
    use crate::array::ArrayDeserialization;
    use crate::array::ArrayPlugin;
    use crate::array::VTable;
    use crate::dtype::DType;
    use crate::dtype::Nullability;
    use crate::dtype::PType;

    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);

    #[test]
    fn test_deserialize_rejects_unknown_offset_ptype() -> VortexResult<()> {
        let dtype = DType::List(
            Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
            Nullability::NonNullable,
        );
        let metadata = ListMetadata {
            offset_ptype: 11,
            ..Default::default()
        }
        .encode_to_vec();
        let children: Vec<ArrayRef> = Vec::new();
        let result = ArrayPlugin::deserialize(
            &List,
            ArrayDeserialization::new(VTable::id(&List), &dtype, 0, &metadata, &[], &children),
            &SESSION,
        );
        assert!(result.is_err_and(|err| {
            err.to_string()
                .contains("ListMetadata offset_ptype must be a known PType, got 11")
        }));

        let known = ListMetadata {
            offset_ptype: PType::U32 as i32,
            ..Default::default()
        };
        assert_eq!(known.get_offset_ptype()?, PType::U32);

        Ok(())
    }
}