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