Skip to content

Commit cfed114

Browse files
committed
more
Signed-off-by: Robert Kruszewski <github@robertk.io>
1 parent d1f8107 commit cfed114

3 files changed

Lines changed: 42 additions & 26 deletions

File tree

vortex-array/src/aggregate_fn/accumulator.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,16 @@ impl<V: AggregateFnVTable> DynAccumulator for Accumulator<V> {
142142
}
143143

144144
let session = ctx.session().clone();
145-
let kernels = &session.aggregate_fns().kernels.load();
145+
let aggregate_fns = session.aggregate_fns();
146146

147147
// 1. Kernel registry first: a registered `(encoding, aggregate_fn)` kernel is strictly
148148
// more specific than the vtable's `try_accumulate` short-circuit. Checking the
149149
// registry first gives kernels for `Combined<V>` aggregates a chance to fire —
150150
// `Combined::try_accumulate` always returns true, so a later kernel check would be
151151
// unreachable.
152152
{
153-
let batch_id = batch.encoding_id();
154-
let kernel = kernels
155-
.get(&(batch_id, Some(self.aggregate_fn.id())))
156-
.or_else(|| kernels.get(&(batch_id, None)))
157-
.copied();
153+
let kernel =
154+
aggregate_fns.find_aggregate_kernel(batch.encoding_id(), self.aggregate_fn.id());
158155
if let Some(kernel) = kernel
159156
&& let Some(result) = kernel.aggregate(&self.aggregate_fn, batch, ctx)?
160157
{
@@ -185,11 +182,8 @@ impl<V: AggregateFnVTable> DynAccumulator for Accumulator<V> {
185182
break;
186183
}
187184

188-
let batch_id = batch.encoding_id();
189-
let kernel = kernels
190-
.get(&(batch_id, Some(self.aggregate_fn.id())))
191-
.or_else(|| kernels.get(&(batch_id, None)))
192-
.copied();
185+
let kernel =
186+
aggregate_fns.find_aggregate_kernel(batch.encoding_id(), self.aggregate_fn.id());
193187
if let Some(kernel) = kernel
194188
&& let Some(result) = kernel.aggregate(&self.aggregate_fn, &batch, ctx)?
195189
{

vortex-array/src/aggregate_fn/accumulator_grouped.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,15 @@ impl<V: AggregateFnVTable> GroupedAccumulator<V> {
163163
let mut elements = groups.elements().clone();
164164
let groups_validity = groups.validity()?;
165165
let session = ctx.session().clone();
166-
let kernels = &session.aggregate_fns().grouped_kernels.load();
166+
let aggregate_fns = session.aggregate_fns();
167167

168168
for _ in 0..max_iterations() {
169169
if elements.is::<AnyCanonical>() {
170170
break;
171171
}
172172

173-
if let Some(result) = kernels
174-
.get(&(elements.encoding_id(), Some(self.aggregate_fn.id())))
175-
.or_else(|| kernels.get(&(elements.encoding_id(), None)))
173+
if let Some(result) = aggregate_fns
174+
.find_groupped_kernel(elements.encoding_id(), self.aggregate_fn.id())
176175
.and_then(|kernel| {
177176
// SAFETY: we assume that elements execution is safe
178177
let groups = unsafe {
@@ -254,16 +253,15 @@ impl<V: AggregateFnVTable> GroupedAccumulator<V> {
254253
let mut elements = groups.elements().clone();
255254
let groups_validity = groups.validity()?;
256255
let session = ctx.session().clone();
257-
let kernels = &session.aggregate_fns().grouped_kernels.load();
256+
let aggregate_fns = session.aggregate_fns();
258257

259258
for _ in 0..64 {
260259
if elements.is::<AnyCanonical>() {
261260
break;
262261
}
263262

264-
if let Some(result) = kernels
265-
.get(&(elements.encoding_id(), Some(self.aggregate_fn.id())))
266-
.or_else(|| kernels.get(&(elements.encoding_id(), None)))
263+
if let Some(result) = aggregate_fns
264+
.find_groupped_kernel(elements.encoding_id(), self.aggregate_fn.id())
267265
.and_then(|kernel| {
268266
// SAFETY: we assume that elements execution is safe
269267
let groups = unsafe {

vortex-array/src/aggregate_fn/session.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use arc_swap::ArcSwap;
88
use vortex_session::Ref;
99
use vortex_session::SessionExt;
1010
use vortex_session::SessionVar;
11-
use vortex_session::registry::Registry;
1211
use vortex_utils::aliases::hash_map::HashMap;
1312

1413
use crate::aggregate_fn::AggregateFnId;
@@ -43,16 +42,13 @@ use crate::arrays::dict::compute::is_constant::DictIsConstantKernel;
4342
use crate::arrays::dict::compute::is_sorted::DictIsSortedKernel;
4443
use crate::arrays::dict::compute::min_max::DictMinMaxKernel;
4544

46-
/// Registry of aggregate function vtables.
47-
pub type AggregateFnRegistry = Registry<AggregateFnPluginRef>;
48-
4945
/// Session state for aggregate function vtables.
5046
#[derive(Debug)]
5147
pub struct AggregateFnSession {
5248
registry: ArcSwap<HashMap<AggregateFnId, AggregateFnPluginRef>>,
5349

54-
pub(super) kernels: ArcSwap<HashMap<KernelKey, &'static dyn DynAggregateKernel>>,
55-
pub(super) grouped_kernels: ArcSwap<HashMap<KernelKey, &'static dyn DynGroupedAggregateKernel>>,
50+
kernels: ArcSwap<HashMap<KernelKey, &'static dyn DynAggregateKernel>>,
51+
grouped_kernels: ArcSwap<HashMap<KernelKey, &'static dyn DynGroupedAggregateKernel>>,
5652
}
5753

5854
impl SessionVar for AggregateFnSession {
@@ -106,7 +102,7 @@ impl Default for AggregateFnSession {
106102
}
107103

108104
impl AggregateFnSession {
109-
/// Returns the aggregate function registry.
105+
/// Find plugin in the registry for the given id
110106
pub fn find_plugin(&self, id: &AggregateFnId) -> Option<AggregateFnPluginRef> {
111107
self.registry.load().get(id).cloned()
112108
}
@@ -123,6 +119,20 @@ impl AggregateFnSession {
123119
});
124120
}
125121

122+
pub fn find_aggregate_kernel(
123+
&self,
124+
array_id: impl Into<ArrayId>,
125+
agg_fn_id: impl Into<AggregateFnId>,
126+
) -> Option<&'static dyn DynAggregateKernel> {
127+
let loaded = self.kernels.load();
128+
let id = array_id.into();
129+
let fn_id = agg_fn_id.into();
130+
loaded
131+
.get(&(id, Some(fn_id)))
132+
.or_else(|| loaded.get(&(id, None)))
133+
.copied()
134+
}
135+
126136
/// Register an aggregate function kernel for a specific aggregate function and array type.
127137
pub fn register_aggregate_kernel(
128138
&self,
@@ -137,6 +147,20 @@ impl AggregateFnSession {
137147
existing
138148
});
139149
}
150+
151+
pub fn find_groupped_kernel(
152+
&self,
153+
array_id: impl Into<ArrayId>,
154+
agg_fn_id: impl Into<AggregateFnId>,
155+
) -> Option<&'static dyn DynGroupedAggregateKernel> {
156+
let loaded = self.grouped_kernels.load();
157+
let id = array_id.into();
158+
let fn_id = agg_fn_id.into();
159+
loaded
160+
.get(&(id, Some(fn_id)))
161+
.or_else(|| loaded.get(&(id, None)))
162+
.copied()
163+
}
140164
}
141165

142166
/// Extension trait for accessing aggregate function session data.

0 commit comments

Comments
 (0)