vortex_array/aggregate_fn/
session.rs1use std::any::Any;
5use std::sync::Arc;
6
7use parking_lot::RwLock;
8use vortex_session::Ref;
9use vortex_session::SessionExt;
10use vortex_session::SessionVar;
11use vortex_session::registry::Registry;
12use vortex_utils::aliases::hash_map::HashMap;
13
14use crate::aggregate_fn::AggregateFnId;
15use crate::aggregate_fn::AggregateFnPluginRef;
16use crate::aggregate_fn::AggregateFnVTable;
17use crate::aggregate_fn::fns::all_non_distinct::AllNonDistinct;
18use crate::aggregate_fn::fns::first::First;
19use crate::aggregate_fn::fns::is_constant::IsConstant;
20use crate::aggregate_fn::fns::is_sorted::IsSorted;
21use crate::aggregate_fn::fns::last::Last;
22use crate::aggregate_fn::fns::min_max::MinMax;
23use crate::aggregate_fn::fns::nan_count::NanCount;
24use crate::aggregate_fn::fns::null_count::NullCount;
25use crate::aggregate_fn::fns::sum::Sum;
26use crate::aggregate_fn::fns::uncompressed_size_in_bytes::UncompressedSizeInBytes;
27use crate::aggregate_fn::kernels::DynAggregateKernel;
28use crate::aggregate_fn::kernels::DynGroupedAggregateKernel;
29use crate::array::ArrayId;
30use crate::array::VTable;
31use crate::arrays::Chunked;
32use crate::arrays::Dict;
33use crate::arrays::chunked::compute::aggregate::ChunkedArrayAggregate;
34use crate::arrays::dict::compute::is_constant::DictIsConstantKernel;
35use crate::arrays::dict::compute::is_sorted::DictIsSortedKernel;
36use crate::arrays::dict::compute::min_max::DictMinMaxKernel;
37
38pub type AggregateFnRegistry = Registry<AggregateFnPluginRef>;
40
41#[derive(Debug)]
43pub struct AggregateFnSession {
44 registry: AggregateFnRegistry,
45
46 pub(super) kernels: RwLock<HashMap<KernelKey, &'static dyn DynAggregateKernel>>,
47 pub(super) grouped_kernels: RwLock<HashMap<KernelKey, &'static dyn DynGroupedAggregateKernel>>,
48}
49
50impl SessionVar for AggregateFnSession {
51 fn as_any(&self) -> &dyn Any {
52 self
53 }
54
55 fn as_any_mut(&mut self) -> &mut dyn Any {
56 self
57 }
58}
59
60type KernelKey = (ArrayId, Option<AggregateFnId>);
61
62impl Default for AggregateFnSession {
63 fn default() -> Self {
64 let this = Self {
65 registry: AggregateFnRegistry::default(),
66 kernels: RwLock::new(HashMap::default()),
67 grouped_kernels: RwLock::new(HashMap::default()),
68 };
69
70 this.register(AllNonDistinct);
72 this.register(First);
73 this.register(IsConstant);
74 this.register(IsSorted);
75 this.register(Last);
76 this.register(MinMax);
77 this.register(NanCount);
78 this.register(NullCount);
79 this.register(Sum);
80 this.register(UncompressedSizeInBytes);
81
82 this.register_aggregate_kernel(Chunked.id(), None::<AggregateFnId>, &ChunkedArrayAggregate);
84 this.register_aggregate_kernel(Dict.id(), Some(MinMax.id()), &DictMinMaxKernel);
85 this.register_aggregate_kernel(Dict.id(), Some(IsConstant.id()), &DictIsConstantKernel);
86 this.register_aggregate_kernel(Dict.id(), Some(IsSorted.id()), &DictIsSortedKernel);
87
88 this
89 }
90}
91
92impl AggregateFnSession {
93 pub fn registry(&self) -> &AggregateFnRegistry {
95 &self.registry
96 }
97
98 pub fn register<V: AggregateFnVTable>(&self, vtable: V) {
101 self.registry
102 .register(vtable.id(), Arc::new(vtable) as AggregateFnPluginRef);
103 }
104
105 pub fn register_aggregate_kernel(
107 &self,
108 array_id: impl Into<ArrayId>,
109 agg_fn_id: Option<impl Into<AggregateFnId>>,
110 kernel: &'static dyn DynAggregateKernel,
111 ) {
112 self.kernels
113 .write()
114 .insert((array_id.into(), agg_fn_id.map(|id| id.into())), kernel);
115 }
116}
117
118pub trait AggregateFnSessionExt: SessionExt {
120 fn aggregate_fns(&self) -> Ref<'_, AggregateFnSession> {
122 self.get::<AggregateFnSession>()
123 }
124}
125impl<S: SessionExt> AggregateFnSessionExt for S {}