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_dtype::DType;
5use vortex_error::VortexResult;
6
7use crate::ArrayRef;
8use crate::IntoArray;
9use crate::arrays::ListViewArray;
10use crate::arrays::ListViewVTable;
11use crate::builtins::ArrayBuiltins;
12use crate::compute::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
24            .elements()
25            .cast((**target_element_type).clone())?
26            .to_canonical()?
27            .into_array();
28        let validity = array
29            .validity()
30            .clone()
31            .cast_nullability(dtype.nullability(), array.len())?;
32
33        // SAFETY: Since `cast` is length-preserving, all of the invariants remain the same.
34        Ok(Some(
35            unsafe {
36                ListViewArray::new_unchecked(
37                    new_elements,
38                    array.offsets().clone(),
39                    array.sizes().clone(),
40                    validity,
41                )
42                .with_zero_copy_to_list(array.is_zero_copy_to_list())
43            }
44            .into_array(),
45        ))
46    }
47}