vortex_array/arrays/varbinview/compute/
cast.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_dtype::DType;
5use vortex_error::VortexResult;
6
7use crate::arrays::{VarBinViewArray, VarBinViewVTable};
8use crate::compute::{CastKernel, CastKernelAdapter};
9use crate::vtable::ValidityHelper;
10use crate::{ArrayRef, IntoArray, register_kernel};
11
12impl CastKernel for VarBinViewVTable {
13    fn cast(&self, array: &VarBinViewArray, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
14        if !array.dtype().eq_ignore_nullability(dtype) {
15            return Ok(None);
16        }
17
18        let new_nullability = dtype.nullability();
19        let new_validity = array
20            .validity()
21            .clone()
22            .cast_nullability(new_nullability, array.len())?;
23        let new_dtype = array.dtype().with_nullability(new_nullability);
24
25        // SAFETY: casting just changes the DType, does not affect invariants on views/buffers.
26        unsafe {
27            Ok(Some(
28                VarBinViewArray::new_unchecked(
29                    array.views().clone(),
30                    array.buffers().clone(),
31                    new_dtype,
32                    new_validity,
33                )
34                .into_array(),
35            ))
36        }
37    }
38}
39
40register_kernel!(CastKernelAdapter(VarBinViewVTable).lift());
41
42#[cfg(test)]
43mod tests {
44    use rstest::rstest;
45    use vortex_dtype::{DType, Nullability};
46
47    use crate::arrays::VarBinViewArray;
48    use crate::compute::cast;
49    use crate::compute::conformance::cast::test_cast_conformance;
50
51    #[rstest]
52    #[case(
53        DType::Utf8(Nullability::Nullable),
54        DType::Utf8(Nullability::NonNullable)
55    )]
56    #[case(
57        DType::Binary(Nullability::Nullable),
58        DType::Binary(Nullability::NonNullable)
59    )]
60    #[case(
61        DType::Utf8(Nullability::NonNullable),
62        DType::Utf8(Nullability::Nullable)
63    )]
64    #[case(
65        DType::Binary(Nullability::NonNullable),
66        DType::Binary(Nullability::Nullable)
67    )]
68    fn try_cast_varbin_nullable(#[case] source: DType, #[case] target: DType) {
69        let varbin = VarBinViewArray::from_iter(vec![Some("a"), Some("b"), Some("c")], source);
70
71        let res = cast(varbin.as_ref(), &target);
72        assert_eq!(res.unwrap().dtype(), &target);
73    }
74
75    #[rstest]
76    #[should_panic]
77    #[case(DType::Utf8(Nullability::Nullable))]
78    #[should_panic]
79    #[case(DType::Binary(Nullability::Nullable))]
80    fn try_cast_varbin_fail(#[case] source: DType) {
81        let non_nullable_source = source.as_nonnullable();
82        let varbin = VarBinViewArray::from_iter(vec![Some("a"), Some("b"), None], source);
83        cast(varbin.as_ref(), &non_nullable_source).unwrap();
84    }
85
86    #[rstest]
87    #[case(VarBinViewArray::from_iter(vec![Some("hello"), Some("world"), Some("test")], DType::Utf8(Nullability::NonNullable)))]
88    #[case(VarBinViewArray::from_iter(vec![Some("hello"), None, Some("world")], DType::Utf8(Nullability::Nullable)))]
89    #[case(VarBinViewArray::from_iter(vec![Some(b"binary".as_slice()), Some(b"data".as_slice())], DType::Binary(Nullability::NonNullable)))]
90    #[case(VarBinViewArray::from_iter(vec![Some(b"test".as_slice()), None], DType::Binary(Nullability::Nullable)))]
91    #[case(VarBinViewArray::from_iter(vec![Some("single")], DType::Utf8(Nullability::NonNullable)))]
92    #[case(VarBinViewArray::from_iter(vec![Some("very long string that exceeds the inline size to test view functionality with multiple buffers")], DType::Utf8(Nullability::NonNullable)))]
93    fn test_cast_varbinview_conformance(#[case] array: VarBinViewArray) {
94        test_cast_conformance(array.as_ref());
95    }
96}