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::arrays::ListViewArray;
9use crate::arrays::ListViewVTable;
10use crate::builtins::ArrayBuiltins;
11use crate::dtype::DType;
12use crate::scalar_fn::fns::cast::CastReduce;
13use crate::vtable::ValidityHelper;
14
15impl CastReduce for ListViewVTable {
16    fn cast(array: &ListViewArray, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
17        // Check if we're casting to a `List` type.
18        let Some(target_element_type) = dtype.as_list_element_opt() else {
19            return Ok(None);
20        };
21
22        // Cast the elements to the target element type.
23        let new_elements = array.elements().cast((**target_element_type).clone())?;
24        let validity = array
25            .validity()
26            .clone()
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}