vortex_array/arrays/struct_/compute/
cast.rs

1use itertools::Itertools;
2use vortex_dtype::DType;
3use vortex_error::{VortexExpect, VortexResult, vortex_bail};
4
5use crate::arrays::{StructArray, StructEncoding};
6use crate::compute::{CastKernel, CastKernelAdapter, cast};
7use crate::{Array, ArrayRef, register_kernel};
8
9impl CastKernel for StructEncoding {
10    fn cast(&self, array: &StructArray, dtype: &DType) -> VortexResult<ArrayRef> {
11        let Some(target_sdtype) = dtype.as_struct() else {
12            vortex_bail!("cannot cast {} to {}", array.dtype(), dtype);
13        };
14
15        let source_sdtype = array
16            .dtype()
17            .as_struct()
18            .vortex_expect("struct array must have struct dtype");
19
20        if target_sdtype.names() != source_sdtype.names() {
21            vortex_bail!("cannot cast {} to {}", array.dtype(), dtype);
22        }
23
24        let validity = array
25            .validity()
26            .clone()
27            .cast_nullability(dtype.nullability())?;
28
29        StructArray::try_new(
30            target_sdtype.names().clone(),
31            array
32                .fields()
33                .iter()
34                .zip_eq(target_sdtype.fields())
35                .map(|(field, dtype)| cast(field, &dtype))
36                .try_collect()?,
37            array.len(),
38            validity,
39        )
40        .map(|a| a.into_array())
41    }
42}
43
44register_kernel!(CastKernelAdapter(StructEncoding).lift());