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