vortex_array/arrays/decimal/compute/
fill_null.rs1use std::cmp::max;
5use std::ops::Not;
6
7use vortex_buffer::BitBuffer;
8use vortex_error::VortexExpect;
9use vortex_error::VortexResult;
10
11use super::cast::upcast_decimal_values;
12use crate::ArrayRef;
13use crate::ExecutionCtx;
14use crate::IntoArray;
15use crate::array::ArrayView;
16use crate::arrays::BoolArray;
17use crate::arrays::Decimal;
18use crate::arrays::DecimalArray;
19use crate::dtype::NativeDecimalType;
20use crate::match_each_decimal_value_type;
21use crate::scalar::DecimalValue;
22use crate::scalar::Scalar;
23use crate::scalar_fn::fns::fill_null::FillNullKernel;
24use crate::validity::Validity;
25
26impl FillNullKernel for Decimal {
27 fn fill_null(
28 array: ArrayView<'_, Decimal>,
29 fill_value: &Scalar,
30 ctx: &mut ExecutionCtx,
31 ) -> VortexResult<Option<ArrayRef>> {
32 let result_validity = Validity::from(fill_value.dtype().nullability());
33
34 Ok(Some(match array.validity()? {
35 Validity::Array(is_valid) => {
36 let is_invalid = is_valid.execute::<BoolArray>(ctx)?.into_bit_buffer().not();
37 let decimal_scalar = fill_value.as_decimal();
38 let decimal_value = decimal_scalar
39 .decimal_value()
40 .vortex_expect("fill_null requires a non-null fill value");
41 match_each_decimal_value_type!(array.values_type(), |T| {
42 fill_invalid_positions::<T>(
43 array,
44 &is_invalid,
45 &decimal_value,
46 result_validity,
47 )?
48 })
49 }
50 _ => unreachable!("checked in entry point"),
51 }))
52 }
53}
54
55fn fill_invalid_positions<T: NativeDecimalType>(
56 array: ArrayView<'_, Decimal>,
57 is_invalid: &BitBuffer,
58 decimal_value: &DecimalValue,
59 result_validity: Validity,
60) -> VortexResult<ArrayRef> {
61 match decimal_value.cast::<T>() {
62 Some(fill_val) => fill_buffer::<T>(array, is_invalid, fill_val, result_validity),
63 None => {
64 let target = max(array.values_type(), decimal_value.decimal_type());
65 let upcasted = upcast_decimal_values(array, target)?;
66 match_each_decimal_value_type!(upcasted.values_type(), |U| {
67 let upcasted = upcasted.as_view();
68 fill_invalid_positions::<U>(upcasted, is_invalid, decimal_value, result_validity)
69 })
70 }
71 }
72}
73
74fn fill_buffer<T: NativeDecimalType>(
75 array: ArrayView<'_, Decimal>,
76 is_invalid: &BitBuffer,
77 fill_val: T,
78 result_validity: Validity,
79) -> VortexResult<ArrayRef> {
80 let mut buffer = array.buffer::<T>().into_mut();
81 for invalid_index in is_invalid.set_indices() {
82 buffer[invalid_index] = fill_val;
83 }
84 Ok(DecimalArray::new(buffer.freeze(), array.decimal_dtype(), result_validity).into_array())
85}
86
87#[cfg(test)]
88mod tests {
89 use std::sync::LazyLock;
90
91 use rstest::rstest;
92 use vortex_buffer::buffer;
93 use vortex_session::VortexSession;
94
95 use crate::IntoArray;
96 use crate::VortexSessionExecute;
97 use crate::arrays::DecimalArray;
98 use crate::assert_arrays_eq;
99 use crate::builtins::ArrayBuiltins;
100 use crate::dtype::DecimalDType;
101 use crate::dtype::DecimalType;
102 use crate::dtype::Nullability;
103 use crate::scalar::DecimalValue;
104 use crate::scalar::Scalar;
105 use crate::validity::Validity;
106
107 static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
108
109 #[test]
110 fn fill_null_leading_none() {
111 let mut ctx = SESSION.create_execution_ctx();
112 let decimal_dtype = DecimalDType::new(19, 2);
113 let arr = DecimalArray::from_option_iter(
114 [None, Some(800i128), None, Some(1000i128), None],
115 decimal_dtype,
116 );
117 let p = arr
118 .into_array()
119 .fill_null(Scalar::decimal(
120 DecimalValue::I128(4200i128),
121 DecimalDType::new(19, 2),
122 Nullability::NonNullable,
123 ))
124 .unwrap()
125 .execute::<DecimalArray>(&mut ctx)
126 .unwrap();
127 assert_arrays_eq!(
128 p,
129 DecimalArray::from_iter([4200, 800, 4200, 1000, 4200], decimal_dtype),
130 &mut ctx
131 );
132 assert_eq!(
133 p.buffer::<i128>().as_slice(),
134 vec![4200, 800, 4200, 1000, 4200]
135 );
136 assert!(
137 p.as_ref()
138 .validity()
139 .unwrap()
140 .execute_mask(p.as_ref().len(), &mut SESSION.create_execution_ctx())
141 .unwrap()
142 .all_true()
143 );
144 }
145
146 #[test]
147 fn fill_null_all_none() {
148 let mut ctx = SESSION.create_execution_ctx();
149 let decimal_dtype = DecimalDType::new(19, 2);
150
151 let arr = DecimalArray::from_option_iter(
152 [Option::<i128>::None, None, None, None, None],
153 decimal_dtype,
154 );
155
156 let p = arr
157 .into_array()
158 .fill_null(Scalar::decimal(
159 DecimalValue::I128(25500i128),
160 DecimalDType::new(19, 2),
161 Nullability::NonNullable,
162 ))
163 .unwrap();
164 assert_arrays_eq!(
165 p,
166 DecimalArray::from_iter([25500, 25500, 25500, 25500, 25500], decimal_dtype),
167 &mut ctx
168 );
169 }
170
171 #[rstest]
173 #[case(DecimalValue::I16(200), DecimalType::I16)]
174 #[case(DecimalValue::I128(200), DecimalType::I128)]
175 fn fill_null_overflow_upcasts(#[case] fill: DecimalValue, #[case] expected_type: DecimalType) {
176 let mut ctx = SESSION.create_execution_ctx();
177 let decimal_dtype = DecimalDType::new(3, 0);
178 let arr = DecimalArray::from_option_iter([None, Some(10i8), None], decimal_dtype);
179 let result = arr
181 .into_array()
182 .fill_null(Scalar::decimal(
183 fill,
184 DecimalDType::new(3, 0),
185 Nullability::NonNullable,
186 ))
187 .unwrap()
188 .execute::<DecimalArray>(&mut ctx)
189 .unwrap();
190 assert_eq!(result.values_type(), expected_type);
191 assert_arrays_eq!(
192 result,
193 DecimalArray::from_iter([200i16, 10, 200], decimal_dtype),
194 &mut ctx
195 );
196 }
197
198 #[test]
199 fn fill_null_non_nullable() {
200 let mut ctx = SESSION.create_execution_ctx();
201 let decimal_dtype = DecimalDType::new(19, 2);
202
203 let arr = DecimalArray::new(
204 buffer![800i128, 1000i128, 1200i128, 1400i128, 1600i128],
205 decimal_dtype,
206 Validity::NonNullable,
207 );
208 let p = arr
209 .into_array()
210 .fill_null(Scalar::decimal(
211 DecimalValue::I128(25500i128),
212 DecimalDType::new(19, 2),
213 Nullability::NonNullable,
214 ))
215 .unwrap();
216 assert_arrays_eq!(
217 p,
218 DecimalArray::from_iter([800i128, 1000, 1200, 1400, 1600], decimal_dtype),
219 &mut ctx
220 );
221 }
222}