Skip to main content

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