vortex_array/arrays/null/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, vortex_bail};
6use vortex_scalar::{Scalar, ScalarValue};
7
8use crate::arrays::{ConstantArray, NullArray, NullVTable};
9use crate::compute::{CastKernel, CastKernelAdapter};
10use crate::{ArrayRef, IntoArray, register_kernel};
11
12impl CastKernel for NullVTable {
13    fn cast(&self, array: &NullArray, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
14        if !dtype.is_nullable() {
15            vortex_bail!("Cannot cast Null to {}", dtype);
16        }
17        if dtype == &DType::Null {
18            return Ok(Some(array.to_array()));
19        }
20
21        let scalar = Scalar::new(dtype.clone(), ScalarValue::null());
22        Ok(Some(ConstantArray::new(scalar, array.len()).into_array()))
23    }
24}
25
26register_kernel!(CastKernelAdapter(NullVTable).lift());
27
28#[cfg(test)]
29mod tests {
30    use rstest::rstest;
31    use vortex_dtype::{DType, Nullability, PType};
32
33    use crate::arrays::NullArray;
34    use crate::compute::cast;
35    use crate::compute::conformance::cast::test_cast_conformance;
36
37    #[test]
38    fn test_cast_null_to_null() {
39        let null_array = NullArray::new(5);
40        let result = cast(null_array.as_ref(), &DType::Null).unwrap();
41        assert_eq!(result.len(), 5);
42        assert_eq!(result.dtype(), &DType::Null);
43    }
44
45    #[test]
46    fn test_cast_null_to_nullable_succeeds() {
47        let null_array = NullArray::new(5);
48        let result = cast(
49            null_array.as_ref(),
50            &DType::Primitive(PType::I32, Nullability::Nullable),
51        )
52        .unwrap();
53
54        // Should create a ConstantArray of nulls
55        assert_eq!(result.len(), 5);
56        assert_eq!(
57            result.dtype(),
58            &DType::Primitive(PType::I32, Nullability::Nullable)
59        );
60
61        // Verify all values are null
62        for i in 0..5 {
63            assert!(result.scalar_at(i).is_null());
64        }
65    }
66
67    #[test]
68    fn test_cast_null_to_non_nullable_fails() {
69        let null_array = NullArray::new(5);
70        let result = cast(
71            null_array.as_ref(),
72            &DType::Primitive(PType::I32, Nullability::NonNullable),
73        );
74        assert!(result.is_err());
75    }
76
77    #[rstest]
78    #[case(NullArray::new(5))]
79    #[case(NullArray::new(1))]
80    #[case(NullArray::new(100))]
81    #[case(NullArray::new(0))]
82    fn test_cast_null_conformance(#[case] array: NullArray) {
83        test_cast_conformance(array.as_ref());
84    }
85}