Skip to main content

vortex_array/arrays/null/compute/
cast.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5use vortex_error::vortex_bail;
6
7use crate::ArrayRef;
8use crate::IntoArray;
9use crate::array::ArrayView;
10use crate::arrays::ConstantArray;
11use crate::arrays::Null;
12use crate::dtype::DType;
13use crate::scalar::Scalar;
14use crate::scalar_fn::builtin::cast::CastReduce;
15
16impl CastReduce for Null {
17    fn cast(array: ArrayView<'_, Null>, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
18        if !dtype.is_nullable() {
19            vortex_bail!("Cannot cast Null to {}", dtype);
20        }
21        if dtype == &DType::Null {
22            return Ok(Some(array.array().clone()));
23        }
24
25        let scalar = Scalar::null(dtype.clone());
26        Ok(Some(ConstantArray::new(scalar, array.len()).into_array()))
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use std::sync::LazyLock;
33
34    use rstest::rstest;
35    use vortex_session::VortexSession;
36
37    use crate::IntoArray;
38    use crate::VortexSessionExecute;
39    use crate::arrays::NullArray;
40    use crate::builtins::ArrayBuiltins;
41    use crate::compute::conformance::cast::test_cast_conformance;
42    use crate::dtype::DType;
43    use crate::dtype::Nullability;
44    use crate::dtype::PType;
45    use crate::optimizer::ArrayOptimizer;
46
47    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
48
49    #[test]
50    fn test_cast_null_to_null() {
51        let null_array = NullArray::new(5);
52        let result = null_array.into_array().cast(DType::Null).unwrap();
53        assert_eq!(result.len(), 5);
54        assert_eq!(result.dtype(), &DType::Null);
55    }
56
57    #[test]
58    fn test_cast_null_to_nullable_succeeds() {
59        let null_array = NullArray::new(5);
60        let result = null_array
61            .into_array()
62            .cast(DType::Primitive(PType::I32, Nullability::Nullable))
63            .unwrap();
64
65        // Should create a ConstantArray of nulls
66        assert_eq!(result.len(), 5);
67        assert_eq!(
68            result.dtype(),
69            &DType::Primitive(PType::I32, Nullability::Nullable)
70        );
71
72        // Verify all values are null
73        for i in 0..5 {
74            assert!(
75                result
76                    .execute_scalar(i, &mut SESSION.create_execution_ctx())
77                    .unwrap()
78                    .is_null()
79            );
80        }
81    }
82
83    #[test]
84    fn test_cast_null_to_non_nullable_fails() {
85        let null_array = NullArray::new(5);
86        // The cast is rejected when it is optimized.
87        let result = null_array
88            .into_array()
89            .cast(DType::Primitive(PType::I32, Nullability::NonNullable))
90            .and_then(|cast| cast.optimize_ctx(&SESSION));
91        assert!(result.is_err());
92    }
93
94    #[rstest]
95    #[case(NullArray::new(5))]
96    #[case(NullArray::new(1))]
97    #[case(NullArray::new(100))]
98    #[case(NullArray::new(0))]
99    fn test_cast_null_conformance(#[case] array: NullArray) {
100        test_cast_conformance(&array.into_array(), &mut SESSION.create_execution_ctx());
101    }
102}