Skip to main content

vortex_array/arrays/constant/compute/
fill_null.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::ArrayRef;
7use crate::array::ArrayView;
8use crate::arrays::Constant;
9use crate::scalar::Scalar;
10use crate::scalar_fn::fns::fill_null::FillNullReduce;
11use crate::scalar_fn::fns::fill_null::fill_null_constant;
12
13impl FillNullReduce for Constant {
14    fn fill_null(
15        array: ArrayView<'_, Constant>,
16        fill_value: &Scalar,
17    ) -> VortexResult<Option<ArrayRef>> {
18        fill_null_constant(array, fill_value).map(Some)
19    }
20}
21
22#[cfg(test)]
23mod test {
24    use crate::IntoArray as _;
25    use crate::VortexSessionExecute;
26    use crate::array_session;
27    use crate::arrays::ConstantArray;
28    use crate::assert_arrays_eq;
29    use crate::builtins::ArrayBuiltins;
30    use crate::dtype::DType;
31    use crate::dtype::Nullability;
32    use crate::dtype::PType;
33    use crate::scalar::Scalar;
34
35    #[test]
36    fn test_null() {
37        let mut ctx = array_session().create_execution_ctx();
38        let actual = ConstantArray::new(Scalar::null_native::<i32>(), 3)
39            .into_array()
40            .fill_null(Scalar::from(1))
41            .unwrap();
42        let expected = ConstantArray::new(Scalar::from(1), 3).into_array();
43
44        assert!(!actual.dtype().is_nullable());
45
46        assert_arrays_eq!(actual, expected, &mut ctx);
47    }
48
49    #[test]
50    fn test_non_null() {
51        let mut ctx = array_session().create_execution_ctx();
52        let actual = ConstantArray::new(Scalar::from(Some(1)), 3)
53            .into_array()
54            .fill_null(Scalar::from(1))
55            .unwrap();
56        let expected = ConstantArray::new(Scalar::from(1), 3).into_array();
57
58        assert!(!actual.dtype().is_nullable());
59
60        assert_arrays_eq!(actual, expected, &mut ctx);
61    }
62
63    #[test]
64    fn test_non_nullable_with_nullable() {
65        let mut ctx = array_session().create_execution_ctx();
66        let actual = ConstantArray::new(Scalar::from(1), 3)
67            .into_array()
68            .fill_null(Scalar::new(
69                DType::Primitive(PType::I32, Nullability::Nullable),
70                Some(1.into()),
71            ))
72            .unwrap();
73        let expected = ConstantArray::new(
74            Scalar::new(
75                DType::Primitive(PType::I32, Nullability::Nullable),
76                Some(1.into()),
77            ),
78            3,
79        )
80        .into_array();
81
82        assert!(!Scalar::from(1).dtype().is_nullable());
83
84        assert!(actual.dtype().is_nullable());
85
86        assert_arrays_eq!(actual, expected, &mut ctx);
87    }
88}