-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathprimitive.rs
More file actions
240 lines (219 loc) · 9.07 KB
/
Copy pathprimitive.rs
File metadata and controls
240 lines (219 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use itertools::Itertools;
use num_traits::ToPrimitive;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_panic;
use vortex_mask::AllOr;
use super::SumState;
use super::checked_add_i64;
use super::checked_add_u64;
use crate::arrays::PrimitiveArray;
use crate::match_each_native_ptype;
pub(super) fn accumulate_primitive(inner: &mut SumState, p: &PrimitiveArray) -> VortexResult<bool> {
let mask = p.validity_mask()?;
match mask.bit_buffer() {
AllOr::None => Ok(false),
AllOr::All => accumulate_primitive_all(inner, p),
AllOr::Some(validity) => accumulate_primitive_valid(inner, p, validity),
}
}
fn accumulate_primitive_all(inner: &mut SumState, p: &PrimitiveArray) -> VortexResult<bool> {
match inner {
SumState::Unsigned(acc) => match_each_native_ptype!(p.ptype(),
unsigned: |T| {
for &v in p.as_slice::<T>() {
if checked_add_u64(acc, v.to_u64().vortex_expect("unsigned to u64")) {
return Ok(true);
}
}
Ok(false)
},
signed: |_T| { vortex_panic!("unsigned sum state with signed input") },
floating: |_T| { vortex_panic!("unsigned sum state with float input") }
),
SumState::Signed(acc) => match_each_native_ptype!(p.ptype(),
unsigned: |_T| { vortex_panic!("signed sum state with unsigned input") },
signed: |T| {
for &v in p.as_slice::<T>() {
if checked_add_i64(acc, v.to_i64().vortex_expect("signed to i64")) {
return Ok(true);
}
}
Ok(false)
},
floating: |_T| { vortex_panic!("signed sum state with float input") }
),
SumState::Float(acc) => match_each_native_ptype!(p.ptype(),
unsigned: |_T| { vortex_panic!("float sum state with unsigned input") },
signed: |_T| { vortex_panic!("float sum state with signed input") },
floating: |T| {
for &v in p.as_slice::<T>() {
*acc += ToPrimitive::to_f64(&v).vortex_expect("float to f64");
}
Ok(false)
}
),
SumState::Decimal { .. } => vortex_panic!("decimal sum state with primitive input"),
}
}
fn accumulate_primitive_valid(
inner: &mut SumState,
p: &PrimitiveArray,
validity: &vortex_buffer::BitBuffer,
) -> VortexResult<bool> {
match inner {
SumState::Unsigned(acc) => match_each_native_ptype!(p.ptype(),
unsigned: |T| {
for (&v, valid) in p.as_slice::<T>().iter().zip_eq(validity.iter()) {
if valid && checked_add_u64(acc, v.to_u64().vortex_expect("unsigned to u64")) {
return Ok(true);
}
}
Ok(false)
},
signed: |_T| { vortex_panic!("unsigned sum state with signed input") },
floating: |_T| { vortex_panic!("unsigned sum state with float input") }
),
SumState::Signed(acc) => match_each_native_ptype!(p.ptype(),
unsigned: |_T| { vortex_panic!("signed sum state with unsigned input") },
signed: |T| {
for (&v, valid) in p.as_slice::<T>().iter().zip_eq(validity.iter()) {
if valid && checked_add_i64(acc, v.to_i64().vortex_expect("signed to i64")) {
return Ok(true);
}
}
Ok(false)
},
floating: |_T| { vortex_panic!("signed sum state with float input") }
),
SumState::Float(acc) => match_each_native_ptype!(p.ptype(),
unsigned: |_T| { vortex_panic!("float sum state with unsigned input") },
signed: |_T| { vortex_panic!("float sum state with signed input") },
floating: |T| {
for (&v, valid) in p.as_slice::<T>().iter().zip_eq(validity.iter()) {
if valid {
*acc += ToPrimitive::to_f64(&v).vortex_expect("float to f64");
}
}
Ok(false)
}
),
SumState::Decimal { .. } => vortex_panic!("decimal sum state with primitive input"),
}
}
#[cfg(test)]
mod tests {
use vortex_buffer::buffer;
use vortex_error::VortexResult;
use crate::IntoArray;
use crate::LEGACY_SESSION;
use crate::VortexSessionExecute;
use crate::aggregate_fn::Accumulator;
use crate::aggregate_fn::DynAccumulator;
use crate::aggregate_fn::EmptyOptions;
use crate::aggregate_fn::fns::sum::Sum;
use crate::aggregate_fn::fns::sum::sum;
use crate::arrays::PrimitiveArray;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::Nullability::Nullable;
use crate::dtype::PType;
use crate::scalar::Scalar;
use crate::validity::Validity;
#[test]
fn sum_i32() -> VortexResult<()> {
let arr = PrimitiveArray::new(buffer![1i32, 2, 3, 4], Validity::NonNullable).into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert_eq!(result.as_primitive().typed_value::<i64>(), Some(10));
Ok(())
}
#[test]
fn sum_u8() -> VortexResult<()> {
let arr = PrimitiveArray::new(buffer![10u8, 20, 30], Validity::NonNullable).into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert_eq!(result.as_primitive().typed_value::<u64>(), Some(60));
Ok(())
}
#[test]
fn sum_f64() -> VortexResult<()> {
let arr =
PrimitiveArray::new(buffer![1.5f64, 2.5, 3.0], Validity::NonNullable).into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert_eq!(result.as_primitive().typed_value::<f64>(), Some(7.0));
Ok(())
}
#[test]
fn sum_with_nulls() -> VortexResult<()> {
let arr = PrimitiveArray::from_option_iter([Some(2i32), None, Some(4)]).into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert_eq!(result.as_primitive().typed_value::<i64>(), Some(6));
Ok(())
}
#[test]
fn sum_all_null() -> VortexResult<()> {
let arr = PrimitiveArray::from_option_iter([None::<i32>, None, None]).into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert_eq!(result.as_primitive().typed_value::<i64>(), Some(0));
Ok(())
}
#[test]
fn sum_all_invalid_float() -> VortexResult<()> {
let arr = PrimitiveArray::from_option_iter::<f32, _>([None, None, None]).into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert_eq!(result, Scalar::primitive(0f64, Nullable));
Ok(())
}
#[test]
fn sum_buffer_i32() -> VortexResult<()> {
let arr = buffer![1, 1, 1, 1].into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert_eq!(result.as_primitive().as_::<i32>(), Some(4));
Ok(())
}
#[test]
fn sum_buffer_f64() -> VortexResult<()> {
let arr = buffer![1., 1., 1., 1.].into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert_eq!(result.as_primitive().as_::<f32>(), Some(4.));
Ok(())
}
#[test]
fn sum_empty_produces_zero() -> VortexResult<()> {
let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
let mut acc = Accumulator::try_new(Sum, EmptyOptions, dtype)?;
let result = acc.finish()?;
assert_eq!(result.as_primitive().typed_value::<i64>(), Some(0));
Ok(())
}
#[test]
fn sum_empty_f64_produces_zero() -> VortexResult<()> {
let dtype = DType::Primitive(PType::F64, Nullability::NonNullable);
let mut acc = Accumulator::try_new(Sum, EmptyOptions, dtype)?;
let result = acc.finish()?;
assert_eq!(result.as_primitive().typed_value::<f64>(), Some(0.0));
Ok(())
}
#[test]
fn sum_checked_overflow() -> VortexResult<()> {
let arr = PrimitiveArray::new(buffer![i64::MAX, 1i64], Validity::NonNullable).into_array();
let result = sum(&arr, &mut LEGACY_SESSION.create_execution_ctx())?;
assert!(result.is_null());
Ok(())
}
#[test]
fn sum_checked_overflow_is_saturated() -> VortexResult<()> {
let dtype = DType::Primitive(PType::I64, Nullability::NonNullable);
let mut acc = Accumulator::try_new(Sum, EmptyOptions, dtype)?;
assert!(!acc.is_saturated());
let batch =
PrimitiveArray::new(buffer![i64::MAX, 1i64], Validity::NonNullable).into_array();
acc.accumulate(&batch, &mut LEGACY_SESSION.create_execution_ctx())?;
assert!(acc.is_saturated());
// finish resets state, clearing saturation
drop(acc.finish()?);
assert!(!acc.is_saturated());
Ok(())
}
}