use std::sync::Arc;
use parking_lot::RwLock;
use vortex_session::Ref;
use vortex_session::SessionExt;
use vortex_session::registry::Registry;
use vortex_utils::aliases::hash_map::HashMap;
use crate::aggregate_fn::AggregateFnId;
use crate::aggregate_fn::AggregateFnPluginRef;
use crate::aggregate_fn::AggregateFnVTable;
use crate::aggregate_fn::fns::first::First;
use crate::aggregate_fn::fns::is_constant::IsConstant;
use crate::aggregate_fn::fns::is_sorted::IsSorted;
use crate::aggregate_fn::fns::last::Last;
use crate::aggregate_fn::fns::min_max::MinMax;
use crate::aggregate_fn::fns::nan_count::NanCount;
use crate::aggregate_fn::fns::sum::Sum;
use crate::aggregate_fn::kernels::DynAggregateKernel;
use crate::aggregate_fn::kernels::DynGroupedAggregateKernel;
use crate::array::ArrayId;
use crate::arrays::Chunked;
use crate::arrays::Dict;
use crate::arrays::chunked::compute::aggregate::ChunkedArrayAggregate;
use crate::arrays::dict::compute::is_constant::DictIsConstantKernel;
use crate::arrays::dict::compute::is_sorted::DictIsSortedKernel;
use crate::arrays::dict::compute::min_max::DictMinMaxKernel;
pub type AggregateFnRegistry = Registry<AggregateFnPluginRef>;
#[derive(Debug)]
pub struct AggregateFnSession {
registry: AggregateFnRegistry,
pub(super) kernels: RwLock<HashMap<KernelKey, &'static dyn DynAggregateKernel>>,
pub(super) grouped_kernels: RwLock<HashMap<KernelKey, &'static dyn DynGroupedAggregateKernel>>,
}
type KernelKey = (ArrayId, Option<AggregateFnId>);
impl Default for AggregateFnSession {
fn default() -> Self {
let this = Self {
registry: AggregateFnRegistry::default(),
kernels: RwLock::new(HashMap::default()),
grouped_kernels: RwLock::new(HashMap::default()),
};
this.register(First);
this.register(IsConstant);
this.register(IsSorted);
this.register(Last);
this.register(MinMax);
this.register(NanCount);
this.register(Sum);
this.register_aggregate_kernel(Chunked::ID, None, &ChunkedArrayAggregate);
this.register_aggregate_kernel(Dict::ID, Some(MinMax.id()), &DictMinMaxKernel);
this.register_aggregate_kernel(Dict::ID, Some(IsConstant.id()), &DictIsConstantKernel);
this.register_aggregate_kernel(Dict::ID, Some(IsSorted.id()), &DictIsSortedKernel);
this
}
}
impl AggregateFnSession {
pub fn registry(&self) -> &AggregateFnRegistry {
&self.registry
}
pub fn register<V: AggregateFnVTable>(&self, vtable: V) {
self.registry
.register(vtable.id(), Arc::new(vtable) as AggregateFnPluginRef);
}
pub fn register_aggregate_kernel(
&self,
array_id: ArrayId,
agg_fn_id: Option<AggregateFnId>,
kernel: &'static dyn DynAggregateKernel,
) {
self.kernels.write().insert((array_id, agg_fn_id), kernel);
}
}
pub trait AggregateFnSessionExt: SessionExt {
fn aggregate_fns(&self) -> Ref<'_, AggregateFnSession> {
self.get::<AggregateFnSession>()
}
}
impl<S: SessionExt> AggregateFnSessionExt for S {}