Skip to content

Commit a9bf18c

Browse files
committed
Structural Sum finalize; thread ExecutionCtx through aggregate finalize
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
1 parent 33f352d commit a9bf18c

27 files changed

Lines changed: 87 additions & 66 deletions

File tree

vortex-array/benches/aggregate_grouped.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ where
163163
list_element_dtype(list_view),
164164
)
165165
.unwrap();
166-
acc.accumulate_list(list_view, &mut SESSION.create_execution_ctx())
167-
.unwrap();
166+
let mut ctx = SESSION.create_execution_ctx();
167+
acc.accumulate_list(list_view, &mut ctx).unwrap();
168168
let result = acc
169-
.finish()
169+
.finish(&mut ctx)
170170
.unwrap()
171-
.execute::<Canonical>(&mut SESSION.create_execution_ctx())
171+
.execute::<Canonical>(&mut ctx)
172172
.unwrap()
173173
.into_array();
174174
divan::black_box(result)

vortex-array/src/aggregate_fn/accumulator_grouped.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub trait DynGroupedAccumulator: 'static + Send {
238238

239239
/// Finish the accumulation and return the final aggregate results for all groups.
240240
/// Resets the accumulator state for the next round of accumulation.
241-
fn finish(&mut self) -> VortexResult<ArrayRef>;
241+
fn finish(&mut self, ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef>;
242242
}
243243

244244
impl<V: AggregateFnVTable> DynGroupedAccumulator for GroupedAccumulator<V> {
@@ -276,9 +276,15 @@ impl<V: AggregateFnVTable> DynGroupedAccumulator for GroupedAccumulator<V> {
276276
Ok(ChunkedArray::try_new(states, self.partial_dtype.clone())?.into_array())
277277
}
278278

279-
fn finish(&mut self) -> VortexResult<ArrayRef> {
280-
let states = self.flush()?;
281-
let results = self.vtable.finalize(states)?;
279+
fn finish(&mut self, ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
280+
// The single-batch case (one accumulate_list call) skips the chunked wrapper so that
281+
// finalize implementations can operate on the batch's states directly.
282+
let states = if self.partials.len() == 1 {
283+
self.partials.pop().vortex_expect("checked length")
284+
} else {
285+
self.flush()?
286+
};
287+
let results = self.vtable.finalize(states, ctx)?;
282288

283289
vortex_ensure!(
284290
results.dtype() == &self.return_dtype,

vortex-array/src/aggregate_fn/combined.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ impl<T: BinaryCombined> AggregateFnVTable for Combined<T> {
245245
unreachable!("Combined::try_accumulate handles all batches")
246246
}
247247

248-
fn finalize(&self, states: ArrayRef) -> VortexResult<ArrayRef> {
248+
fn finalize(&self, states: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
249249
let l_field = states.get_item(FieldName::from(self.0.left_name()))?;
250250
let r_field = states.get_item(FieldName::from(self.0.right_name()))?;
251-
let l_finalized = self.0.left().finalize(l_field)?;
252-
let r_finalized = self.0.right().finalize(r_field)?;
251+
let l_finalized = self.0.left().finalize(l_field, ctx)?;
252+
let r_finalized = self.0.right().finalize(r_field, ctx)?;
253253
BinaryCombined::finalize(&self.0, l_finalized, r_finalized)
254254
}
255255

vortex-array/src/aggregate_fn/fns/all_nan/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl AggregateFnVTable for AllNan {
121121
Ok(())
122122
}
123123

124-
fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> {
124+
fn finalize(&self, partials: ArrayRef, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
125125
Ok(partials)
126126
}
127127

vortex-array/src/aggregate_fn/fns/all_non_distinct/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl AggregateFnVTable for AllNonDistinct {
233233
}
234234
}
235235

236-
fn finalize(&self, _partials: ArrayRef) -> VortexResult<ArrayRef> {
236+
fn finalize(&self, _partials: ArrayRef, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
237237
vortex_bail!("AllNonDistinct does not support array finalization");
238238
}
239239

vortex-array/src/aggregate_fn/fns/all_non_nan/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl AggregateFnVTable for AllNonNan {
111111
Ok(())
112112
}
113113

114-
fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> {
114+
fn finalize(&self, partials: ArrayRef, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
115115
Ok(partials)
116116
}
117117

vortex-array/src/aggregate_fn/fns/all_non_null/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl AggregateFnVTable for AllNonNull {
101101
Ok(())
102102
}
103103

104-
fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> {
104+
fn finalize(&self, partials: ArrayRef, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
105105
Ok(partials)
106106
}
107107

vortex-array/src/aggregate_fn/fns/all_null/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl AggregateFnVTable for AllNull {
104104
Ok(())
105105
}
106106

107-
fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> {
107+
fn finalize(&self, partials: ArrayRef, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
108108
Ok(partials)
109109
}
110110

vortex-array/src/aggregate_fn/fns/bounded_max/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl AggregateFnVTable for BoundedMax {
279279
Ok(())
280280
}
281281

282-
fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> {
282+
fn finalize(&self, partials: ArrayRef, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
283283
partials.get_item(BOUNDED_MAX_BOUND)
284284
}
285285

vortex-array/src/aggregate_fn/fns/bounded_min/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl AggregateFnVTable for BoundedMin {
197197
Ok(())
198198
}
199199

200-
fn finalize(&self, partials: ArrayRef) -> VortexResult<ArrayRef> {
200+
fn finalize(&self, partials: ArrayRef, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
201201
Ok(partials)
202202
}
203203

0 commit comments

Comments
 (0)