vortex_array/arrays/primitive/compute/
fill_null.rs1use 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::builtin::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 buffer.fill_where::<false>(&is_invalid, fill_bytes);
50
51 PrimitiveArray::from_byte_buffer(
52 buffer.freeze().into_byte_buffer(),
53 array.ptype(),
54 result_validity,
55 )
56 .into_array()
57 },
58 _ => { unreachable!("primitive widths are specialized") },
59 )
60 }
61 _ => unreachable!("checked in entry point"),
62 }))
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use std::sync::LazyLock;
69
70 use vortex_buffer::buffer;
71 use vortex_session::VortexSession;
72
73 use crate::IntoArray;
74 use crate::VortexSessionExecute;
75 use crate::arrays::PrimitiveArray;
76 use crate::arrays::primitive::compute::fill_null::BoolArray;
77 use crate::assert_arrays_eq;
78 use crate::builtins::ArrayBuiltins;
79 use crate::scalar::Scalar;
80 use crate::validity::Validity;
81
82 static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
83
84 #[test]
85 fn fill_null_leading_none() {
86 let mut ctx = SESSION.create_execution_ctx();
87 let arr = PrimitiveArray::from_option_iter([None, Some(8u8), None, Some(10), None]);
88 let p = arr.into_array().fill_null(Scalar::from(42u8)).unwrap();
89 assert_arrays_eq!(
90 p,
91 PrimitiveArray::from_iter([42u8, 8, 42, 10, 42]),
92 &mut ctx
93 );
94 assert!(
95 p.validity()
96 .unwrap()
97 .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
98 .unwrap()
99 .all_true()
100 );
101 }
102
103 #[test]
104 fn fill_null_all_none() {
105 let mut ctx = SESSION.create_execution_ctx();
106 let arr = PrimitiveArray::from_option_iter([Option::<u8>::None, None, None, None, None]);
107
108 let p = arr.into_array().fill_null(Scalar::from(255u8)).unwrap();
109 assert_arrays_eq!(
110 p,
111 PrimitiveArray::from_iter([255u8, 255, 255, 255, 255]),
112 &mut ctx
113 );
114 assert!(
115 p.validity()
116 .unwrap()
117 .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
118 .unwrap()
119 .all_true()
120 );
121 }
122
123 #[test]
124 fn fill_null_nullable_non_null() {
125 let mut ctx = SESSION.create_execution_ctx();
126 let arr = PrimitiveArray::new(
127 buffer![8u8, 10, 12, 14, 16],
128 Validity::Array(BoolArray::from_iter([true, true, true, true, true]).into_array()),
129 );
130 let p = arr.into_array().fill_null(Scalar::from(255u8)).unwrap();
131 assert_arrays_eq!(
132 p,
133 PrimitiveArray::from_iter([8u8, 10, 12, 14, 16]),
134 &mut ctx
135 );
136 assert!(
137 p.validity()
138 .unwrap()
139 .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
140 .unwrap()
141 .all_true()
142 );
143 }
144
145 #[test]
146 fn fill_null_non_nullable() {
147 let mut ctx = SESSION.create_execution_ctx();
148 let arr = buffer![8u8, 10, 12, 14, 16].into_array();
149 let p = arr.fill_null(Scalar::from(255u8)).unwrap();
150 assert_arrays_eq!(
151 p,
152 PrimitiveArray::from_iter([8u8, 10, 12, 14, 16]),
153 &mut ctx
154 );
155 assert!(
156 p.validity()
157 .unwrap()
158 .execute_mask(p.len(), &mut SESSION.create_execution_ctx())
159 .unwrap()
160 .all_true()
161 );
162 }
163}