vortex-array 0.70.0

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

use vortex_error::VortexResult;

use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::array::ArrayView;
use crate::arrays::ListView;
use crate::arrays::ListViewArray;
use crate::arrays::listview::ListViewArrayExt;
use crate::builtins::ArrayBuiltins;
use crate::dtype::DType;
use crate::scalar_fn::fns::cast::CastKernel;
use crate::scalar_fn::fns::cast::CastReduce;
use crate::validity::Validity;

fn build_with_validity(
    array: ArrayView<'_, ListView>,
    new_elements: ArrayRef,
    validity: Validity,
) -> ArrayRef {
    // SAFETY: Since `cast` is length-preserving, all of the invariants remain the same.
    unsafe {
        ListViewArray::new_unchecked(
            new_elements,
            array.offsets().clone(),
            array.sizes().clone(),
            validity,
        )
        .with_zero_copy_to_list(array.is_zero_copy_to_list())
    }
    .into_array()
}

impl CastReduce for ListView {
    fn cast(array: ArrayView<'_, ListView>, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
        // Check if we're casting to a `List` type.
        let Some(target_element_type) = dtype.as_list_element_opt() else {
            return Ok(None);
        };
        let Some(validity) = array
            .validity()?
            .trivial_cast_nullability(dtype.nullability(), array.len())?
        else {
            return Ok(None);
        };

        // Cast the elements to the target element type.
        let new_elements = array.elements().cast((**target_element_type).clone())?;
        Ok(Some(build_with_validity(array, new_elements, validity)))
    }
}

impl CastKernel for ListView {
    fn cast(
        array: ArrayView<'_, ListView>,
        dtype: &DType,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<Option<ArrayRef>> {
        let Some(target_element_type) = dtype.as_list_element_opt() else {
            return Ok(None);
        };

        let validity = array
            .validity()?
            .cast_nullability(dtype.nullability(), array.len(), ctx)?;
        let new_elements = array.elements().cast((**target_element_type).clone())?;

        Ok(Some(build_with_validity(array, new_elements, validity)))
    }
}