Skip to main content

vortex_array/arrays/primitive/compute/
fill_null.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Not;
5
6use vortex_buffer::Buffer;
7use vortex_error::VortexExpect;
8use vortex_error::VortexResult;
9
10use crate::ArrayRef;
11use crate::ExecutionCtx;
12use crate::IntoArray;
13use crate::array::ArrayView;
14use crate::arrays::BoolArray;
15use crate::arrays::Primitive;
16use crate::arrays::PrimitiveArray;
17use crate::dtype::ToBytes;
18use crate::match_each_ptype_width;
19use crate::scalar::Scalar;
20use crate::scalar_fn::fns::fill_null::FillNullKernel;
21use crate::validity::Validity;
22
23impl FillNullKernel for Primitive {
24    fn fill_null(
25        array: ArrayView<'_, Primitive>,
26        fill_value: &Scalar,
27        ctx: &mut ExecutionCtx,
28    ) -> VortexResult<Option<ArrayRef>> {
29        let result_validity = Validity::from(fill_value.dtype().nullability());
30
31        Ok(Some(match array.validity()? {
32            Validity::Array(is_valid) => {
33                let is_invalid = is_valid.execute::<BoolArray>(ctx)?.into_bit_buffer().not();
34                let fill_value = fill_value.as_primitive();
35                assert_eq!(array.ptype(), fill_value.ptype());
36                let fill_value = fill_value
37                    .pvalue()
38                    .vortex_expect("fill_null requires a non-null fill value");
39                let values = array.buffer_handle().to_host_sync();
40                let alignment = values.alignment();
41
42                match_each_ptype_width!(
43                    array.ptype(),
44                    |W| {
45                        let mut buffer =
46                            Buffer::<[u8; W]>::from_byte_buffer_aligned(values, alignment).into_mut();
47                        let mut fill_bytes = [0u8; W];
48                        fill_bytes.copy_from_slice(fill_value.to_le_bytes());
49
50                        for invalid_index in is_invalid.set_indices() {
51                            buffer[invalid_index] = fill_bytes;
52                        }
53
54                        PrimitiveArray::from_byte_buffer(
55                            buffer.freeze().into_byte_buffer(),
56                            array.ptype(),
57                            result_validity,
58                        )
59                        .into_array()
60                    },
61                    _ => { unreachable!("primitive widths are specialized") },
62                )
63            }
64            _ => unreachable!("checked in entry point"),
65        }))
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use std::sync::LazyLock;
72
73    use vortex_buffer::buffer;
74    use vortex_session::VortexSession;
75
76    use crate::IntoArray;
77    use crate::VortexSessionExecute;
78    use crate::arrays::PrimitiveArray;
79    use crate::arrays::primitive::compute::fill_null::BoolArray;
80    use crate::assert_arrays_eq;
81    use crate::builtins::ArrayBuiltins;
82    use crate::scalar::Scalar;
83    use crate::validity::Validity;
84
85    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
86
87    #[test]
88    fn fill_null_leading_none() {
89        let mut ctx = SESSION.create_execution_ctx();
90        let arr = PrimitiveArray::from_option_iter([None, Some(8u8), None, Some(10), None]);
91        let p = arr.into_array().fill_null(Scalar::from(42u8)).unwrap();
92        assert_arrays_eq!(
93            p,
94            PrimitiveArray::from_iter([42u8, 8, 42, 10, 42]),
95            &mut ctx
96        );
97        assert!(
98            p.validity()
99                .unwrap()
100                .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
101                .unwrap()
102                .all_true()
103        );
104    }
105
106    #[test]
107    fn fill_null_all_none() {
108        let mut ctx = SESSION.create_execution_ctx();
109        let arr = PrimitiveArray::from_option_iter([Option::<u8>::None, None, None, None, None]);
110
111        let p = arr.into_array().fill_null(Scalar::from(255u8)).unwrap();
112        assert_arrays_eq!(
113            p,
114            PrimitiveArray::from_iter([255u8, 255, 255, 255, 255]),
115            &mut ctx
116        );
117        assert!(
118            p.validity()
119                .unwrap()
120                .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
121                .unwrap()
122                .all_true()
123        );
124    }
125
126    #[test]
127    fn fill_null_nullable_non_null() {
128        let mut ctx = SESSION.create_execution_ctx();
129        let arr = PrimitiveArray::new(
130            buffer![8u8, 10, 12, 14, 16],
131            Validity::Array(BoolArray::from_iter([true, true, true, true, true]).into_array()),
132        );
133        let p = arr.into_array().fill_null(Scalar::from(255u8)).unwrap();
134        assert_arrays_eq!(
135            p,
136            PrimitiveArray::from_iter([8u8, 10, 12, 14, 16]),
137            &mut ctx
138        );
139        assert!(
140            p.validity()
141                .unwrap()
142                .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
143                .unwrap()
144                .all_true()
145        );
146    }
147
148    #[test]
149    fn fill_null_non_nullable() {
150        let mut ctx = SESSION.create_execution_ctx();
151        let arr = buffer![8u8, 10, 12, 14, 16].into_array();
152        let p = arr.fill_null(Scalar::from(255u8)).unwrap();
153        assert_arrays_eq!(
154            p,
155            PrimitiveArray::from_iter([8u8, 10, 12, 14, 16]),
156            &mut ctx
157        );
158        assert!(
159            p.validity()
160                .unwrap()
161                .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
162                .unwrap()
163                .all_true()
164        );
165    }
166}