Skip to main content

vortex_array/arrays/listview/compute/
cast.rs

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