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