-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathaccumulator_grouped.rs
More file actions
332 lines (296 loc) · 11.7 KB
/
accumulator_grouped.rs
File metadata and controls
332 lines (296 loc) · 11.7 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use arrow_buffer::ArrowNativeType;
use vortex_buffer::Buffer;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_err;
use vortex_error::vortex_panic;
use vortex_mask::Mask;
use crate::AnyCanonical;
use crate::ArrayRef;
use crate::Canonical;
use crate::Columnar;
use crate::DynArray;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::aggregate_fn::Accumulator;
use crate::aggregate_fn::AggregateFn;
use crate::aggregate_fn::AggregateFnRef;
use crate::aggregate_fn::AggregateFnVTable;
use crate::aggregate_fn::DynAccumulator;
use crate::aggregate_fn::session::AggregateFnSessionExt;
use crate::arrays::ChunkedArray;
use crate::arrays::FixedSizeListArray;
use crate::arrays::ListViewArray;
use crate::builders::builder_with_capacity;
use crate::builtins::ArrayBuiltins;
use crate::dtype::DType;
use crate::dtype::IntegerPType;
use crate::executor::MAX_ITERATIONS;
use crate::match_each_integer_ptype;
use crate::vtable::ValidityHelper;
/// Reference-counted type-erased grouped accumulator.
pub type GroupedAccumulatorRef = Box<dyn DynGroupedAccumulator>;
/// An accumulator used for computing grouped aggregates.
///
/// Note that the groups must be processed in order, and the accumulator does not support random
/// access to groups.
pub struct GroupedAccumulator<V: AggregateFnVTable> {
/// The vtable of the aggregate function.
vtable: V,
/// The options of the aggregate function.
options: V::Options,
/// Type-erased aggregate function used for kernel dispatch.
aggregate_fn: AggregateFnRef,
/// The DType of the input.
dtype: DType,
/// The DType of the aggregate.
return_dtype: DType,
/// The DType of the partial accumulator state.
partial_dtype: DType,
/// The accumulated state for prior batches of groups.
partials: Vec<ArrayRef>,
}
impl<V: AggregateFnVTable> GroupedAccumulator<V> {
pub fn try_new(vtable: V, options: V::Options, dtype: DType) -> VortexResult<Self> {
let aggregate_fn = AggregateFn::new(vtable.clone(), options.clone()).erased();
let return_dtype = vtable.return_dtype(&options, &dtype).ok_or_else(|| {
vortex_err!(
"Aggregate function {} cannot be applied to dtype {}",
vtable.id(),
dtype
)
})?;
let partial_dtype = vtable.partial_dtype(&options, &dtype).ok_or_else(|| {
vortex_err!(
"Aggregate function {} cannot be applied to dtype {}",
vtable.id(),
dtype
)
})?;
Ok(Self {
vtable,
options,
aggregate_fn,
dtype,
return_dtype,
partial_dtype,
partials: vec![],
})
}
}
/// A trait object for type-erased grouped accumulators, used for dynamic dispatch when the aggregate
/// function is not known at compile time.
pub trait DynGroupedAccumulator: 'static + Send {
/// Accumulate a list of groups into the accumulator.
fn accumulate_list(&mut self, groups: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<()>;
/// Finish the accumulation and return the partial aggregate results for all groups.
/// Resets the accumulator state for the next round of accumulation.
fn flush(&mut self) -> VortexResult<ArrayRef>;
/// Finish the accumulation and return the final aggregate results for all groups.
/// Resets the accumulator state for the next round of accumulation.
fn finish(&mut self) -> VortexResult<ArrayRef>;
}
impl<V: AggregateFnVTable> DynGroupedAccumulator for GroupedAccumulator<V> {
fn accumulate_list(&mut self, groups: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<()> {
let elements_dtype = match groups.dtype() {
DType::List(elem, _) => elem,
DType::FixedSizeList(elem, ..) => elem,
_ => vortex_bail!(
"Input DType mismatch: expected List or FixedSizeList, got {}",
groups.dtype()
),
};
vortex_ensure!(
elements_dtype.as_ref() == &self.dtype,
"Input DType mismatch: expected {}, got {}",
self.dtype,
elements_dtype
);
// We first execute the groups until it is a ListView or FixedSizeList, since we only
// dispatch the aggregate kernel over the elements of these arrays.
let canonical = match groups.clone().execute::<Columnar>(ctx)? {
Columnar::Canonical(c) => c,
Columnar::Constant(c) => c.into_array().execute::<Canonical>(ctx)?,
};
match canonical {
Canonical::List(groups) => self.accumulate_list_view(&groups, ctx),
Canonical::FixedSizeList(groups) => self.accumulate_fixed_size_list(&groups, ctx),
_ => vortex_panic!("We checked the DType above, so this should never happen"),
}
}
fn flush(&mut self) -> VortexResult<ArrayRef> {
let states = std::mem::take(&mut self.partials);
Ok(ChunkedArray::try_new(states, self.partial_dtype.clone())?.into_array())
}
fn finish(&mut self) -> VortexResult<ArrayRef> {
let states = self.flush()?;
let results = self.vtable.finalize(states)?;
vortex_ensure!(
results.dtype() == &self.return_dtype,
"Return DType mismatch: expected {}, got {}",
self.return_dtype,
results.dtype()
);
Ok(results)
}
}
impl<V: AggregateFnVTable> GroupedAccumulator<V> {
fn accumulate_list_view(
&mut self,
groups: &ListViewArray,
ctx: &mut ExecutionCtx,
) -> VortexResult<()> {
let mut elements = groups.elements().clone();
let session = ctx.session().clone();
let kernels = &session.aggregate_fns().grouped_kernels;
for _ in 0..*MAX_ITERATIONS {
if elements.is::<AnyCanonical>() {
break;
}
let kernels_r = kernels.read();
if let Some(result) = kernels_r
.get(&(elements.encoding_id(), Some(self.aggregate_fn.id())))
.or_else(|| kernels_r.get(&(elements.encoding_id(), None)))
.and_then(|kernel| {
// SAFETY: we assume that elements execution is safe
let groups = unsafe {
ListViewArray::new_unchecked(
elements.clone(),
groups.offsets().clone(),
groups.sizes().clone(),
groups.validity(),
)
};
kernel
.grouped_aggregate(&self.aggregate_fn, &groups)
.transpose()
})
.transpose()?
{
return self.push_result(result);
}
// Execute one step and try again
elements = elements.execute(ctx)?;
}
// Otherwise, we iterate the offsets and sizes and accumulate each group one by one.
let elements = elements.execute::<Columnar>(ctx)?.into_array();
let offsets = groups.offsets();
let sizes = groups.sizes().cast(offsets.dtype().clone())?;
let validity = groups.validity().execute_mask(offsets.len(), ctx)?;
match_each_integer_ptype!(offsets.dtype().as_ptype(), |O| {
let offsets = offsets.clone().execute::<Buffer<O>>(ctx)?;
let sizes = sizes.execute::<Buffer<O>>(ctx)?;
self.accumulate_list_view_typed(
&elements,
offsets.as_ref(),
sizes.as_ref(),
&validity,
ctx,
)
})
}
fn accumulate_list_view_typed<O: IntegerPType>(
&mut self,
elements: &ArrayRef,
offsets: &[O],
sizes: &[O],
validity: &Mask,
ctx: &mut ExecutionCtx,
) -> VortexResult<()> {
let mut accumulator = Accumulator::try_new(
self.vtable.clone(),
self.options.clone(),
self.dtype.clone(),
)?;
let mut states = builder_with_capacity(&self.partial_dtype, offsets.len());
for (offset, size) in offsets.iter().zip(sizes.iter()) {
let offset = offset.to_usize().vortex_expect("Offset value is not usize");
let size = size.to_usize().vortex_expect("Size value is not usize");
if validity.value(offset) {
let group = elements.slice(offset..offset + size)?;
accumulator.accumulate(&group, ctx)?;
states.append_scalar(&accumulator.flush()?)?;
} else {
states.append_null()
}
}
self.push_result(states.finish())
}
fn accumulate_fixed_size_list(
&mut self,
groups: &FixedSizeListArray,
ctx: &mut ExecutionCtx,
) -> VortexResult<()> {
let mut elements = groups.elements().clone();
let session = ctx.session().clone();
let kernels = &session.aggregate_fns().grouped_kernels;
for _ in 0..64 {
if elements.is::<AnyCanonical>() {
break;
}
let kernels_r = kernels.read();
if let Some(result) = kernels_r
.get(&(elements.encoding_id(), Some(self.aggregate_fn.id())))
.or_else(|| kernels_r.get(&(elements.encoding_id(), None)))
.and_then(|kernel| {
// SAFETY: we assume that elements execution is safe
let groups = unsafe {
FixedSizeListArray::new_unchecked(
elements.clone(),
groups.list_size(),
groups.validity(),
groups.len(),
)
};
kernel
.grouped_aggregate_fixed_size(&self.aggregate_fn, &groups)
.transpose()
})
.transpose()?
{
return self.push_result(result);
}
// Execute one step and try again
elements = elements.execute(ctx)?;
}
// Otherwise, we iterate the offsets and sizes and accumulate each group one by one.
let elements = elements.execute::<Columnar>(ctx)?.into_array();
let validity = groups.validity().execute_mask(groups.len(), ctx)?;
let mut accumulator = Accumulator::try_new(
self.vtable.clone(),
self.options.clone(),
self.dtype.clone(),
)?;
let mut states = builder_with_capacity(&self.partial_dtype, groups.len());
let mut offset = 0;
let size = groups
.list_size()
.to_usize()
.vortex_expect("List size is not usize");
for i in 0..groups.len() {
if validity.value(i) {
let group = elements.slice(offset..offset + size)?;
accumulator.accumulate(&group, ctx)?;
states.append_scalar(&accumulator.flush()?)?;
} else {
states.append_null()
}
offset += size;
}
self.push_result(states.finish())
}
fn push_result(&mut self, state: ArrayRef) -> VortexResult<()> {
vortex_ensure!(
state.dtype() == &self.partial_dtype,
"State DType mismatch: expected {}, got {}",
self.partial_dtype,
state.dtype()
);
self.partials.push(state);
Ok(())
}
}