vortex_array/arrays/varbin/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::{VarBinArray, VarBinVTable};
8use crate::compute::{CastKernel, CastKernelAdapter};
9use crate::vtable::ValidityHelper;
10use crate::{ArrayRef, IntoArray, register_kernel};
11
12impl CastKernel for VarBinVTable {
13    fn cast(&self, array: &VarBinArray, 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        Ok(Some(
25            VarBinArray::try_new(
26                array.offsets().clone(),
27                array.bytes().clone(),
28                new_dtype,
29                new_validity,
30            )?
31            .into_array(),
32        ))
33    }
34}
35
36register_kernel!(CastKernelAdapter(VarBinVTable).lift());
37
38#[cfg(test)]
39mod tests {
40    use rstest::rstest;
41    use vortex_dtype::{DType, Nullability};
42
43    use crate::arrays::VarBinArray;
44    use crate::compute::cast;
45    use crate::compute::conformance::cast::test_cast_conformance;
46
47    #[rstest]
48    #[case(
49        DType::Utf8(Nullability::Nullable),
50        DType::Utf8(Nullability::NonNullable)
51    )]
52    #[case(
53        DType::Binary(Nullability::Nullable),
54        DType::Binary(Nullability::NonNullable)
55    )]
56    #[case(
57        DType::Utf8(Nullability::NonNullable),
58        DType::Utf8(Nullability::Nullable)
59    )]
60    #[case(
61        DType::Binary(Nullability::NonNullable),
62        DType::Binary(Nullability::Nullable)
63    )]
64    fn try_cast_varbin_nullable(#[case] source: DType, #[case] target: DType) {
65        let varbin = VarBinArray::from_iter(vec![Some("a"), Some("b"), Some("c")], source);
66
67        let res = cast(varbin.as_ref(), &target);
68        assert_eq!(res.unwrap().dtype(), &target);
69    }
70
71    #[rstest]
72    #[should_panic]
73    #[case(DType::Utf8(Nullability::Nullable))]
74    #[should_panic]
75    #[case(DType::Binary(Nullability::Nullable))]
76    fn try_cast_varbin_fail(#[case] source: DType) {
77        let non_nullable_source = source.as_nonnullable();
78        let varbin = VarBinArray::from_iter(vec![Some("a"), Some("b"), None], source);
79        cast(varbin.as_ref(), &non_nullable_source).unwrap();
80    }
81
82    #[rstest]
83    #[case(VarBinArray::from_iter(vec![Some("hello"), Some("world"), Some("test")], DType::Utf8(Nullability::NonNullable)))]
84    #[case(VarBinArray::from_iter(vec![Some("hello"), None, Some("world")], DType::Utf8(Nullability::Nullable)))]
85    #[case(VarBinArray::from_iter(vec![Some(b"binary".as_slice()), Some(b"data".as_slice())], DType::Binary(Nullability::NonNullable)))]
86    #[case(VarBinArray::from_iter(vec![Some(b"test".as_slice()), None], DType::Binary(Nullability::Nullable)))]
87    #[case(VarBinArray::from_iter(vec![Some("single")], DType::Utf8(Nullability::NonNullable)))]
88    fn test_cast_varbin_conformance(#[case] array: VarBinArray) {
89        test_cast_conformance(array.as_ref());
90    }
91}