use vortex_dtype::DType;
use vortex_error::VortexResult;
use crate::array::ArrayRef;
use crate::arrays::{BoolArray, BoolVTable};
use crate::compute::{CastKernel, CastKernelAdapter};
use crate::register_kernel;
use crate::vtable::ValidityHelper;
impl CastKernel for BoolVTable {
fn cast(&self, array: &BoolArray, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
if !matches!(dtype, DType::Bool(_)) {
return Ok(None);
}
let new_nullability = dtype.nullability();
let new_validity = array
.validity()
.clone()
.cast_nullability(new_nullability, array.len())?;
Ok(Some(
BoolArray::from_bool_buffer(array.boolean_buffer().clone(), new_validity).to_array(),
))
}
}
register_kernel!(CastKernelAdapter(BoolVTable).lift());
#[cfg(test)]
mod tests {
use rstest::rstest;
use vortex_dtype::{DType, Nullability};
use crate::arrays::BoolArray;
use crate::compute::cast;
use crate::compute::conformance::cast::test_cast_conformance;
#[test]
fn try_cast_bool_success() {
let bool = BoolArray::from_iter(vec![Some(true), Some(false), Some(true)]);
let res = cast(bool.as_ref(), &DType::Bool(Nullability::NonNullable));
assert!(res.is_ok());
assert_eq!(res.unwrap().dtype(), &DType::Bool(Nullability::NonNullable));
}
#[test]
#[should_panic]
fn try_cast_bool_fail() {
let bool = BoolArray::from_iter(vec![Some(true), Some(false), None]);
cast(bool.as_ref(), &DType::Bool(Nullability::NonNullable)).unwrap();
}
#[rstest]
#[case(BoolArray::from_iter(vec![true, false, true, true, false]))]
#[case(BoolArray::from_iter(vec![Some(true), Some(false), None, Some(true), None]))]
#[case(BoolArray::from_iter(vec![true]))]
#[case(BoolArray::from_iter(vec![false, false]))]
fn test_cast_bool_conformance(#[case] array: BoolArray) {
test_cast_conformance(array.as_ref());
}
}