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::fns::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
46    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
47
48    #[test]
49    fn test_cast_null_to_null() {
50        let null_array = NullArray::new(5);
51        let result = null_array.into_array().cast(DType::Null).unwrap();
52        assert_eq!(result.len(), 5);
53        assert_eq!(result.dtype(), &DType::Null);
54    }
55
56    #[test]
57    fn test_cast_null_to_nullable_succeeds() {
58        let null_array = NullArray::new(5);
59        let result = null_array
60            .into_array()
61            .cast(DType::Primitive(PType::I32, Nullability::Nullable))
62            .unwrap();
63
64        // Should create a ConstantArray of nulls
65        assert_eq!(result.len(), 5);
66        assert_eq!(
67            result.dtype(),
68            &DType::Primitive(PType::I32, Nullability::Nullable)
69        );
70
71        // Verify all values are null
72        for i in 0..5 {
73            assert!(
74                result
75                    .execute_scalar(i, &mut SESSION.create_execution_ctx())
76                    .unwrap()
77                    .is_null()
78            );
79        }
80    }
81
82    #[test]
83    fn test_cast_null_to_non_nullable_fails() {
84        let null_array = NullArray::new(5);
85        let result = null_array
86            .into_array()
87            .cast(DType::Primitive(PType::I32, Nullability::NonNullable));
88        assert!(result.is_err());
89    }
90
91    #[rstest]
92    #[case(NullArray::new(5))]
93    #[case(NullArray::new(1))]
94    #[case(NullArray::new(100))]
95    #[case(NullArray::new(0))]
96    fn test_cast_null_conformance(#[case] array: NullArray) {
97        test_cast_conformance(&array.into_array(), &mut SESSION.create_execution_ctx());
98    }
99}