vortex_array/aggregate_fn/
accumulator_grouped.rs1use arrow_buffer::ArrowNativeType;
5use vortex_buffer::Buffer;
6use vortex_error::VortexExpect;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_error::vortex_ensure;
10use vortex_error::vortex_err;
11use vortex_error::vortex_panic;
12use vortex_mask::Mask;
13
14use crate::ArrayRef;
15use crate::Canonical;
16use crate::Columnar;
17use crate::ExecutionCtx;
18use crate::IntoArray;
19use crate::aggregate_fn::Accumulator;
20use crate::aggregate_fn::AggregateFn;
21use crate::aggregate_fn::AggregateFnRef;
22use crate::aggregate_fn::AggregateFnVTable;
23use crate::aggregate_fn::DynAccumulator;
24use crate::aggregate_fn::session::AggregateFnSessionExt;
25use crate::arrays::ChunkedArray;
26use crate::arrays::FixedSizeListArray;
27use crate::arrays::ListViewArray;
28use crate::arrays::fixed_size_list::FixedSizeListArrayExt;
29use crate::arrays::fixed_size_list::FixedSizeListArraySlotsExt;
30use crate::arrays::listview::ListViewArraySlotsExt;
31use crate::builders::builder_with_capacity;
32use crate::builtins::ArrayBuiltins;
33use crate::columnar::AnyColumnar;
34use crate::dtype::DType;
35use crate::executor::max_iterations;
36use crate::match_each_integer_ptype;
37
38pub type GroupedAccumulatorRef = Box<dyn DynGroupedAccumulator>;
40
41pub enum GroupedArray {
47 ListView(ListViewArray),
49 FixedSizeList(FixedSizeListArray),
51}
52
53impl From<ListViewArray> for GroupedArray {
54 fn from(groups: ListViewArray) -> Self {
55 Self::ListView(groups)
56 }
57}
58
59impl From<FixedSizeListArray> for GroupedArray {
60 fn from(groups: FixedSizeListArray) -> Self {
61 Self::FixedSizeList(groups)
62 }
63}
64
65impl GroupedArray {
66 pub fn elements(&self) -> &ArrayRef {
68 match self {
69 Self::ListView(groups) => groups.elements(),
70 Self::FixedSizeList(groups) => groups.elements(),
71 }
72 }
73
74 pub fn group_ranges(&self, ctx: &mut ExecutionCtx) -> VortexResult<GroupRanges> {
76 match self {
77 Self::ListView(groups) => list_view_group_ranges(groups, ctx),
78 Self::FixedSizeList(groups) => Ok(fixed_size_list_group_ranges(groups)),
79 }
80 }
81
82 pub fn group_validity(&self, ctx: &mut ExecutionCtx) -> VortexResult<Mask> {
84 match self {
85 Self::ListView(groups) => groups.validity()?.execute_mask(groups.len(), ctx),
86 Self::FixedSizeList(groups) => groups.validity()?.execute_mask(groups.len(), ctx),
87 }
88 }
89
90 pub fn len(&self) -> usize {
92 match self {
93 Self::ListView(groups) => groups.len(),
94 Self::FixedSizeList(groups) => groups.len(),
95 }
96 }
97
98 pub fn is_empty(&self) -> bool {
100 self.len() == 0
101 }
102
103 pub fn all_groups_valid(&self, ctx: &mut ExecutionCtx) -> VortexResult<bool> {
105 Ok(self.group_validity(ctx)?.all_true())
106 }
107
108 unsafe fn with_elements_unchecked(&self, elements: ArrayRef) -> VortexResult<Self> {
109 Ok(match self {
110 Self::ListView(groups) => unsafe {
111 ListViewArray::new_unchecked(
112 elements,
113 groups.offsets().clone(),
114 groups.sizes().clone(),
115 groups.validity()?,
116 )
117 }
118 .into(),
119 Self::FixedSizeList(groups) => unsafe {
120 FixedSizeListArray::new_unchecked(
121 elements,
122 groups.list_size(),
123 groups.validity()?,
124 groups.len(),
125 )
126 }
127 .into(),
128 })
129 }
130}
131
132pub enum GroupRanges {
134 ListView {
136 ranges: Vec<(usize, usize)>,
138 },
139 FixedSizeList {
141 len: usize,
143 size: usize,
145 },
146}
147
148impl GroupRanges {
149 pub fn len(&self) -> usize {
151 match self {
152 Self::ListView { ranges } => ranges.len(),
153 Self::FixedSizeList { len, .. } => *len,
154 }
155 }
156
157 pub fn is_empty(&self) -> bool {
159 self.len() == 0
160 }
161
162 fn range(&self, index: usize) -> (usize, usize) {
164 match self {
165 Self::ListView { ranges } => ranges[index],
166 Self::FixedSizeList { len, size } => {
167 assert!(index < *len, "range index out of bounds");
168 (index * size, *size)
169 }
170 }
171 }
172
173 pub fn iter(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
175 (0..self.len()).map(|index| self.range(index))
176 }
177}
178
179pub struct GroupedAccumulator<V: AggregateFnVTable> {
184 vtable: V,
186 options: V::Options,
188 aggregate_fn: AggregateFnRef,
190 dtype: DType,
192 return_dtype: DType,
194 partial_dtype: DType,
196 partials: Vec<ArrayRef>,
198}
199
200impl<V: AggregateFnVTable> GroupedAccumulator<V> {
201 pub fn try_new(vtable: V, options: V::Options, dtype: DType) -> VortexResult<Self> {
202 let aggregate_fn = AggregateFn::new(vtable.clone(), options.clone()).erased();
203 let return_dtype = vtable.return_dtype(&options, &dtype).ok_or_else(|| {
204 vortex_err!(
205 "Aggregate function {} cannot be applied to dtype {}",
206 vtable.id(),
207 dtype
208 )
209 })?;
210 let partial_dtype = vtable.partial_dtype(&options, &dtype).ok_or_else(|| {
211 vortex_err!(
212 "Aggregate function {} cannot be applied to dtype {}",
213 vtable.id(),
214 dtype
215 )
216 })?;
217
218 Ok(Self {
219 vtable,
220 options,
221 aggregate_fn,
222 dtype,
223 return_dtype,
224 partial_dtype,
225 partials: vec![],
226 })
227 }
228}
229
230pub trait DynGroupedAccumulator: 'static + Send {
233 fn accumulate_list(&mut self, groups: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<()>;
235
236 fn flush(&mut self) -> VortexResult<ArrayRef>;
239
240 fn finish(&mut self) -> VortexResult<ArrayRef>;
243}
244
245impl<V: AggregateFnVTable> DynGroupedAccumulator for GroupedAccumulator<V> {
246 fn accumulate_list(&mut self, groups: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<()> {
247 let elements_dtype = match groups.dtype() {
248 DType::List(elem, _) => elem,
249 DType::FixedSizeList(elem, ..) => elem,
250 _ => vortex_bail!(
251 "Input DType mismatch: expected List or FixedSizeList, got {}",
252 groups.dtype()
253 ),
254 };
255 vortex_ensure!(
256 elements_dtype.as_ref() == &self.dtype,
257 "Input DType mismatch: expected {}, got {}",
258 self.dtype,
259 elements_dtype
260 );
261
262 let canonical = match groups.clone().execute::<Columnar>(ctx)? {
265 Columnar::Canonical(c) => c,
266 Columnar::Constant(c) => c.into_array().execute::<Canonical>(ctx)?,
267 };
268 match canonical {
269 Canonical::List(groups) => self.accumulate_grouped_array(groups.into(), ctx),
270 Canonical::FixedSizeList(groups) => self.accumulate_grouped_array(groups.into(), ctx),
271 _ => vortex_panic!("We checked the DType above, so this should never happen"),
272 }
273 }
274
275 fn flush(&mut self) -> VortexResult<ArrayRef> {
276 let states = std::mem::take(&mut self.partials);
277 Ok(ChunkedArray::try_new(states, self.partial_dtype.clone())?.into_array())
278 }
279
280 fn finish(&mut self) -> VortexResult<ArrayRef> {
281 let states = self.flush()?;
282 let results = self.vtable.finalize(states)?;
283
284 vortex_ensure!(
285 results.dtype() == &self.return_dtype,
286 "Return DType mismatch: expected {}, got {}",
287 self.return_dtype,
288 results.dtype()
289 );
290
291 Ok(results)
292 }
293}
294
295impl<V: AggregateFnVTable> GroupedAccumulator<V> {
296 fn accumulate_grouped_array(
297 &mut self,
298 groups: GroupedArray,
299 ctx: &mut ExecutionCtx,
300 ) -> VortexResult<()> {
301 let mut elements = groups.elements().clone();
302 let session = ctx.session().clone();
303
304 for _ in 0..max_iterations() {
305 if let Some(kernel) = session
307 .aggregate_fns()
308 .find_grouped_encoding_kernel(elements.encoding_id(), self.aggregate_fn.id())
309 {
310 let kernel_groups = unsafe { groups.with_elements_unchecked(elements.clone())? };
312 if let Some(result) =
313 kernel.grouped_aggregate(&self.aggregate_fn, &kernel_groups, ctx)?
314 {
315 return self.push_result(result);
316 }
317 }
318
319 if let Some(kernel) = session
321 .aggregate_fns()
322 .find_grouped_kernel(self.aggregate_fn.id())
323 {
324 let kernel_groups = unsafe { groups.with_elements_unchecked(elements.clone())? };
327 if let Some(result) =
328 kernel.grouped_aggregate(&self.aggregate_fn, &kernel_groups, ctx)?
329 {
330 return self.push_result(result);
331 }
332 }
333
334 if elements.is::<AnyColumnar>() {
335 break;
336 }
337
338 elements = elements.execute(ctx)?;
340 }
341
342 let elements = elements.execute::<Columnar>(ctx)?.into_array();
343 let grouped = unsafe { groups.with_elements_unchecked(elements)? };
346
347 self.accumulate_grouped_fallback(&grouped, ctx)
349 }
350
351 fn accumulate_grouped_fallback(
352 &mut self,
353 grouped: &GroupedArray,
354 ctx: &mut ExecutionCtx,
355 ) -> VortexResult<()> {
356 let mut accumulator = Accumulator::try_new(
357 self.vtable.clone(),
358 self.options.clone(),
359 self.dtype.clone(),
360 )?;
361 let mut states = builder_with_capacity(&self.partial_dtype, grouped.len());
362 let group_ranges = grouped.group_ranges(ctx)?;
363 let group_validity = grouped.group_validity(ctx)?;
364
365 for ((offset, size), valid) in group_ranges.iter().zip(group_validity.iter()) {
366 if valid {
367 let group = grouped.elements().slice(offset..offset + size)?;
368 accumulator.accumulate(&group, ctx)?;
369 states.append_scalar(&accumulator.flush()?)?;
370 } else {
371 states.append_null()
372 }
373 }
374
375 self.push_result(states.finish())
376 }
377
378 fn push_result(&mut self, state: ArrayRef) -> VortexResult<()> {
379 vortex_ensure!(
380 state.dtype() == &self.partial_dtype,
381 "State DType mismatch: expected {}, got {}",
382 self.partial_dtype,
383 state.dtype()
384 );
385 self.partials.push(state);
386 Ok(())
387 }
388}
389fn list_view_group_ranges(
390 groups: &ListViewArray,
391 ctx: &mut ExecutionCtx,
392) -> VortexResult<GroupRanges> {
393 let offsets = groups.offsets();
394 let sizes = groups.sizes().cast(offsets.dtype().clone())?;
395
396 let ranges = match_each_integer_ptype!(offsets.dtype().as_ptype(), |O| {
397 let offsets = offsets.clone().execute::<Buffer<O>>(ctx)?;
398 let sizes = sizes.execute::<Buffer<O>>(ctx)?;
399 offsets
400 .as_ref()
401 .iter()
402 .zip(sizes.as_ref().iter())
403 .map(|(offset, size)| {
404 (
405 offset.to_usize().vortex_expect("Offset value is not usize"),
406 size.to_usize().vortex_expect("Size value is not usize"),
407 )
408 })
409 .collect::<Vec<_>>()
410 });
411
412 Ok(GroupRanges::ListView { ranges })
413}
414
415fn fixed_size_list_group_ranges(groups: &FixedSizeListArray) -> GroupRanges {
416 GroupRanges::FixedSizeList {
417 len: groups.len(),
418 size: groups.list_size() as usize,
419 }
420}