vortex_array/aggregate_fn/
session.rs1use std::any::Any;
5use std::sync::Arc;
6
7use vortex_session::ArcSwapMap;
8use vortex_session::SessionExt;
9use vortex_session::SessionGuard;
10use vortex_session::SessionVar;
11
12use crate::aggregate_fn::AggregateFnId;
13use crate::aggregate_fn::AggregateFnPluginRef;
14use crate::aggregate_fn::AggregateFnRef;
15use crate::aggregate_fn::AggregateFnVTable;
16use crate::aggregate_fn::fns::all_nan::AllNan;
17use crate::aggregate_fn::fns::all_non_distinct::AllNonDistinct;
18use crate::aggregate_fn::fns::all_non_nan::AllNonNan;
19use crate::aggregate_fn::fns::all_non_null::AllNonNull;
20use crate::aggregate_fn::fns::all_null::AllNull;
21use crate::aggregate_fn::fns::bounded_max::BoundedMax;
22use crate::aggregate_fn::fns::bounded_min::BoundedMin;
23use crate::aggregate_fn::fns::count::Count;
24use crate::aggregate_fn::fns::count::CountGroupedKernel;
25use crate::aggregate_fn::fns::first::First;
26use crate::aggregate_fn::fns::is_constant::IsConstant;
27use crate::aggregate_fn::fns::is_sorted::IsSorted;
28use crate::aggregate_fn::fns::last::Last;
29use crate::aggregate_fn::fns::max::Max;
30use crate::aggregate_fn::fns::min::Min;
31use crate::aggregate_fn::fns::min_max::MinMax;
32use crate::aggregate_fn::fns::nan_count::NanCount;
33use crate::aggregate_fn::fns::null_count::NullCount;
34use crate::aggregate_fn::fns::sum::PrimitiveGroupedSumEncodingKernel;
35use crate::aggregate_fn::fns::sum::Sum;
36use crate::aggregate_fn::fns::uncompressed_size_in_bytes::UncompressedSizeInBytes;
37use crate::aggregate_fn::kernels::DynAggregateKernel;
38use crate::aggregate_fn::kernels::DynGroupedAggregateKernel;
39use crate::array::ArrayId;
40use crate::array::VTable;
41use crate::arrays::Chunked;
42use crate::arrays::Dict;
43use crate::arrays::Primitive;
44use crate::arrays::chunked::compute::aggregate::ChunkedArrayAggregate;
45use crate::arrays::dict::compute::is_constant::DictIsConstantKernel;
46use crate::arrays::dict::compute::is_sorted::DictIsSortedKernel;
47use crate::arrays::dict::compute::min_max::DictMinMaxKernel;
48use crate::dtype::DType;
49
50#[derive(Clone, Debug)]
56pub struct AggregateFnSession {
57 registry: AggregateFnRegistry,
58
59 kernels: AggregateKernelRegistry,
60 grouped_kernels: GroupedKernelRegistry,
61 grouped_encoding_kernels: GroupedEncodingKernelRegistry,
62}
63
64impl SessionVar for AggregateFnSession {
65 fn as_any(&self) -> &dyn Any {
66 self
67 }
68
69 fn as_any_mut(&mut self) -> &mut dyn Any {
70 self
71 }
72}
73
74type AggregateKernelKey = (ArrayId, Option<AggregateFnId>);
75type GroupedEncodingKernelKey = (ArrayId, AggregateFnId);
76
77type AggregateFnRegistry = ArcSwapMap<AggregateFnId, AggregateFnPluginRef>;
79type AggregateKernelRegistry = ArcSwapMap<AggregateKernelKey, &'static dyn DynAggregateKernel>;
81type GroupedKernelRegistry = ArcSwapMap<AggregateFnId, &'static dyn DynGroupedAggregateKernel>;
83type GroupedEncodingKernelRegistry =
85 ArcSwapMap<GroupedEncodingKernelKey, &'static dyn DynGroupedAggregateKernel>;
86
87impl Default for AggregateFnSession {
88 fn default() -> Self {
89 let this = Self {
90 registry: AggregateFnRegistry::default(),
91 kernels: AggregateKernelRegistry::default(),
92 grouped_kernels: GroupedKernelRegistry::default(),
93 grouped_encoding_kernels: GroupedEncodingKernelRegistry::default(),
94 };
95
96 this.register(AllNonDistinct);
98 this.register(AllNonNan);
99 this.register(AllNonNull);
100 this.register(AllNan);
101 this.register(AllNull);
102 this.register(BoundedMax);
103 this.register(BoundedMin);
104 this.register(First);
105 this.register(IsConstant);
106 this.register(IsSorted);
107 this.register(Last);
108 this.register(Max);
109 this.register(Min);
110 this.register(MinMax);
111 this.register(NanCount);
112 this.register(NullCount);
113 this.register(Sum);
114 this.register(UncompressedSizeInBytes);
115
116 this.register_aggregate_kernel(Chunked.id(), None::<AggregateFnId>, &ChunkedArrayAggregate);
118 this.register_aggregate_kernel(Dict.id(), Some(MinMax.id()), &DictMinMaxKernel);
119 this.register_aggregate_kernel(Dict.id(), Some(IsConstant.id()), &DictIsConstantKernel);
120 this.register_aggregate_kernel(Dict.id(), Some(IsSorted.id()), &DictIsSortedKernel);
121
122 this.register_grouped_kernel(Count.id(), &CountGroupedKernel);
124 this.register_grouped_encoding_kernel(
125 Primitive.id(),
126 Sum.id(),
127 &PrimitiveGroupedSumEncodingKernel,
128 );
129
130 this
131 }
132}
133
134impl AggregateFnSession {
135 pub fn find_plugin(&self, id: &AggregateFnId) -> Option<AggregateFnPluginRef> {
137 self.registry.get(id)
138 }
139
140 pub fn register<V: AggregateFnVTable>(&self, vtable: V) {
143 let id = vtable.id();
144 let pluginref = Arc::new(vtable) as AggregateFnPluginRef;
145 self.registry.insert(id, pluginref);
146 }
147
148 pub fn zone_stat_defaults(&self, input_dtype: &DType) -> Vec<AggregateFnRef> {
154 self.registry.read(|registry| {
155 let mut fns: Vec<AggregateFnRef> = registry
156 .values()
157 .filter_map(|plugin| plugin.zone_stat_default(input_dtype))
158 .collect();
159 fns.sort_by_key(|f| f.id());
160 fns
161 })
162 }
163
164 pub fn find_aggregate_kernel(
169 &self,
170 array_id: impl Into<ArrayId>,
171 agg_fn_id: impl Into<AggregateFnId>,
172 ) -> Option<&'static dyn DynAggregateKernel> {
173 let id = array_id.into();
174 let fn_id = agg_fn_id.into();
175 self.kernels.read(|kernels| {
176 kernels
177 .get(&(id, Some(fn_id)))
178 .or_else(|| kernels.get(&(id, None)))
179 .copied()
180 })
181 }
182
183 pub fn register_aggregate_kernel(
189 &self,
190 array_id: impl Into<ArrayId>,
191 agg_fn_id: Option<impl Into<AggregateFnId>>,
192 kernel: &'static dyn DynAggregateKernel,
193 ) {
194 let id = (array_id.into(), agg_fn_id.map(|id| id.into()));
195 self.kernels.insert(id, kernel);
196 }
197
198 pub fn find_grouped_kernel(
203 &self,
204 agg_fn_id: impl Into<AggregateFnId>,
205 ) -> Option<&'static dyn DynGroupedAggregateKernel> {
206 let fn_id = agg_fn_id.into();
207 self.grouped_kernels
208 .read(|kernels| kernels.get(&fn_id).copied())
209 }
210
211 pub fn register_grouped_kernel(
213 &self,
214 agg_fn_id: impl Into<AggregateFnId>,
215 kernel: &'static dyn DynGroupedAggregateKernel,
216 ) {
217 let fn_id = agg_fn_id.into();
218 self.grouped_kernels.insert(fn_id, kernel)
219 }
220
221 pub fn find_grouped_encoding_kernel(
226 &self,
227 array_id: impl Into<ArrayId>,
228 agg_fn_id: impl Into<AggregateFnId>,
229 ) -> Option<&'static dyn DynGroupedAggregateKernel> {
230 let id = array_id.into();
231 let fn_id = agg_fn_id.into();
232 self.grouped_encoding_kernels
233 .read(|kernels| kernels.get(&(id, fn_id)).copied())
234 }
235
236 pub fn register_grouped_encoding_kernel(
238 &self,
239 array_id: impl Into<ArrayId>,
240 agg_fn_id: impl Into<AggregateFnId>,
241 kernel: &'static dyn DynGroupedAggregateKernel,
242 ) {
243 let id = array_id.into();
244 let fn_id = agg_fn_id.into();
245 self.grouped_encoding_kernels.insert((id, fn_id), kernel)
246 }
247}
248
249pub trait AggregateFnSessionExt: SessionExt {
251 fn aggregate_fns(&self) -> SessionGuard<'_, AggregateFnSession> {
253 self.get::<AggregateFnSession>()
254 }
255}
256impl<S: SessionExt> AggregateFnSessionExt for S {}