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 std::sync::LazyLock;
25
26    use vortex_session::VortexSession;
27
28    use crate::IntoArray as _;
29    use crate::VortexSessionExecute;
30    use crate::arrays::ConstantArray;
31    use crate::assert_arrays_eq;
32    use crate::builtins::ArrayBuiltins;
33    use crate::dtype::DType;
34    use crate::dtype::Nullability;
35    use crate::dtype::PType;
36    use crate::scalar::Scalar;
37
38    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
39
40    #[test]
41    fn test_null() {
42        let mut ctx = SESSION.create_execution_ctx();
43        let actual = ConstantArray::new(Scalar::null_native::<i32>(), 3)
44            .into_array()
45            .fill_null(Scalar::from(1))
46            .unwrap();
47        let expected = ConstantArray::new(Scalar::from(1), 3).into_array();
48
49        assert!(!actual.dtype().is_nullable());
50
51        assert_arrays_eq!(actual, expected, &mut ctx);
52    }
53
54    #[test]
55    fn test_non_null() {
56        let mut ctx = SESSION.create_execution_ctx();
57        let actual = ConstantArray::new(Scalar::from(Some(1)), 3)
58            .into_array()
59            .fill_null(Scalar::from(2))
60            .unwrap();
61        let expected = ConstantArray::new(Scalar::from(1), 3).into_array();
62
63        assert!(!actual.dtype().is_nullable());
64
65        assert_arrays_eq!(actual, expected, &mut ctx);
66    }
67
68    #[test]
69    fn test_non_nullable_with_nullable() {
70        let mut ctx = SESSION.create_execution_ctx();
71        let actual = ConstantArray::new(Scalar::from(1), 3)
72            .into_array()
73            .fill_null(Scalar::new(
74                DType::Primitive(PType::I32, Nullability::Nullable),
75                Some(2.into()),
76            ))
77            .unwrap();
78        let expected = ConstantArray::new(
79            Scalar::new(
80                DType::Primitive(PType::I32, Nullability::Nullable),
81                Some(1.into()),
82            ),
83            3,
84        )
85        .into_array();
86
87        assert!(!Scalar::from(1).dtype().is_nullable());
88
89        assert!(actual.dtype().is_nullable());
90
91        assert_arrays_eq!(actual, expected, &mut ctx);
92    }
93}