vortex_array/arrays/listview/compute/
cast.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_dtype::DType;
5use vortex_error::VortexResult;
6
7use crate::ArrayRef;
8use crate::IntoArray;
9use crate::arrays::ListViewArray;
10use crate::arrays::ListViewVTable;
11use crate::compute::CastKernel;
12use crate::compute::CastKernelAdapter;
13use crate::compute::{self};
14use crate::register_kernel;
15use crate::vtable::ValidityHelper;
16
17impl CastKernel for ListViewVTable {
18    fn cast(&self, array: &ListViewArray, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
19        // Check if we're casting to a `List` type.
20        let Some(target_element_type) = dtype.as_list_element_opt() else {
21            return Ok(None);
22        };
23
24        // Cast the elements to the target element type.
25        let new_elements = compute::cast(array.elements(), target_element_type)?;
26        let validity = array
27            .validity()
28            .clone()
29            .cast_nullability(dtype.nullability(), array.len())?;
30
31        // SAFETY: Since `cast` is length-preserving, all of the invariants remain the same.
32        Ok(Some(
33            unsafe {
34                ListViewArray::new_unchecked(
35                    new_elements,
36                    array.offsets().clone(),
37                    array.sizes().clone(),
38                    validity,
39                )
40                .with_zero_copy_to_list(array.is_zero_copy_to_list())
41            }
42            .into_array(),
43        ))
44    }
45}
46
47register_kernel!(CastKernelAdapter(ListViewVTable).lift());