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.validity().clone().cast_nullability(new_nullability)?;
20        let new_dtype = array.dtype().with_nullability(new_nullability);
21
22        // SAFETY: casting just changes the DType, does not affect invariants on views/buffers.
23        unsafe {
24            Ok(Some(
25                VarBinViewArray::new_unchecked(
26                    array.views().clone(),
27                    array.buffers().clone(),
28                    new_dtype,
29                    new_validity,
30                )
31                .into_array(),
32            ))
33        }
34    }
35}
36
37register_kernel!(CastKernelAdapter(VarBinViewVTable).lift());
38
39#[cfg(test)]
40mod tests {
41    use rstest::rstest;
42    use vortex_dtype::{DType, Nullability};
43
44    use crate::arrays::VarBinViewArray;
45    use crate::compute::cast;
46    use crate::compute::conformance::cast::test_cast_conformance;
47
48    #[rstest]
49    #[case(
50        DType::Utf8(Nullability::Nullable),
51        DType::Utf8(Nullability::NonNullable)
52    )]
53    #[case(
54        DType::Binary(Nullability::Nullable),
55        DType::Binary(Nullability::NonNullable)
56    )]
57    #[case(
58        DType::Utf8(Nullability::NonNullable),
59        DType::Utf8(Nullability::Nullable)
60    )]
61    #[case(
62        DType::Binary(Nullability::NonNullable),
63        DType::Binary(Nullability::Nullable)
64    )]
65    fn try_cast_varbin_nullable(#[case] source: DType, #[case] target: DType) {
66        let varbin = VarBinViewArray::from_iter(vec![Some("a"), Some("b"), Some("c")], source);
67
68        let res = cast(varbin.as_ref(), &target);
69        assert_eq!(res.unwrap().dtype(), &target);
70    }
71
72    #[rstest]
73    #[should_panic]
74    #[case(DType::Utf8(Nullability::Nullable))]
75    #[should_panic]
76    #[case(DType::Binary(Nullability::Nullable))]
77    fn try_cast_varbin_fail(#[case] source: DType) {
78        let non_nullable_source = source.as_nonnullable();
79        let varbin = VarBinViewArray::from_iter(vec![Some("a"), Some("b"), None], source);
80        cast(varbin.as_ref(), &non_nullable_source).unwrap();
81    }
82
83    #[rstest]
84    #[case(VarBinViewArray::from_iter(vec![Some("hello"), Some("world"), Some("test")], DType::Utf8(Nullability::NonNullable)))]
85    #[case(VarBinViewArray::from_iter(vec![Some("hello"), None, Some("world")], DType::Utf8(Nullability::Nullable)))]
86    #[case(VarBinViewArray::from_iter(vec![Some(b"binary".as_slice()), Some(b"data".as_slice())], DType::Binary(Nullability::NonNullable)))]
87    #[case(VarBinViewArray::from_iter(vec![Some(b"test".as_slice()), None], DType::Binary(Nullability::Nullable)))]
88    #[case(VarBinViewArray::from_iter(vec![Some("single")], DType::Utf8(Nullability::NonNullable)))]
89    #[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)))]
90    fn test_cast_varbinview_conformance(#[case] array: VarBinViewArray) {
91        test_cast_conformance(array.as_ref());
92    }
93}