vortex_array/arrays/bool/compute/
cast.rs1use vortex_dtype::DType;
2use vortex_error::{VortexResult, vortex_bail};
3
4use crate::array::{Array, ArrayRef};
5use crate::arrays::{BoolArray, BoolEncoding};
6use crate::compute::{CastKernel, CastKernelAdapter};
7use crate::register_kernel;
8
9impl CastKernel for BoolEncoding {
10 fn cast(&self, array: &BoolArray, dtype: &DType) -> VortexResult<ArrayRef> {
11 if !matches!(dtype, DType::Bool(_)) {
12 vortex_bail!("Cannot cast {} to {}", array.dtype(), dtype);
13 }
14
15 let new_nullability = dtype.nullability();
16 let new_validity = array.validity().clone().cast_nullability(new_nullability)?;
17 Ok(BoolArray::new(array.boolean_buffer().clone(), new_validity).into_array())
18 }
19}
20
21register_kernel!(CastKernelAdapter(BoolEncoding).lift());
22
23#[cfg(test)]
24mod tests {
25 use vortex_dtype::{DType, Nullability};
26
27 use crate::arrays::BoolArray;
28 use crate::compute::cast;
29
30 #[test]
31 fn try_cast_bool_success() {
32 let bool = BoolArray::from_iter(vec![Some(true), Some(false), Some(true)]);
33
34 let res = cast(&bool, &DType::Bool(Nullability::NonNullable));
35 assert!(res.is_ok());
36 assert_eq!(res.unwrap().dtype(), &DType::Bool(Nullability::NonNullable));
37 }
38
39 #[test]
40 #[should_panic]
41 fn try_cast_bool_fail() {
42 let bool = BoolArray::from_iter(vec![Some(true), Some(false), None]);
43 cast(&bool, &DType::Bool(Nullability::NonNullable)).unwrap();
44 }
45}