Skip to main content

vortex_array/aggregate_fn/
session.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5
6use parking_lot::RwLock;
7use vortex_session::Ref;
8use vortex_session::SessionExt;
9use vortex_session::registry::Registry;
10use vortex_utils::aliases::hash_map::HashMap;
11
12use crate::aggregate_fn::AggregateFnId;
13use crate::aggregate_fn::AggregateFnPluginRef;
14use crate::aggregate_fn::AggregateFnVTable;
15use crate::aggregate_fn::fns::first::First;
16use crate::aggregate_fn::fns::is_constant::IsConstant;
17use crate::aggregate_fn::fns::is_sorted::IsSorted;
18use crate::aggregate_fn::fns::last::Last;
19use crate::aggregate_fn::fns::min_max::MinMax;
20use crate::aggregate_fn::fns::nan_count::NanCount;
21use crate::aggregate_fn::fns::sum::Sum;
22use crate::aggregate_fn::kernels::DynAggregateKernel;
23use crate::aggregate_fn::kernels::DynGroupedAggregateKernel;
24use crate::array::ArrayId;
25use crate::arrays::Chunked;
26use crate::arrays::Dict;
27use crate::arrays::chunked::compute::aggregate::ChunkedArrayAggregate;
28use crate::arrays::dict::compute::is_constant::DictIsConstantKernel;
29use crate::arrays::dict::compute::is_sorted::DictIsSortedKernel;
30use crate::arrays::dict::compute::min_max::DictMinMaxKernel;
31
32/// Registry of aggregate function vtables.
33pub type AggregateFnRegistry = Registry<AggregateFnPluginRef>;
34
35/// Session state for aggregate function vtables.
36#[derive(Debug)]
37pub struct AggregateFnSession {
38    registry: AggregateFnRegistry,
39
40    pub(super) kernels: RwLock<HashMap<KernelKey, &'static dyn DynAggregateKernel>>,
41    pub(super) grouped_kernels: RwLock<HashMap<KernelKey, &'static dyn DynGroupedAggregateKernel>>,
42}
43
44type KernelKey = (ArrayId, Option<AggregateFnId>);
45
46impl Default for AggregateFnSession {
47    fn default() -> Self {
48        let this = Self {
49            registry: AggregateFnRegistry::default(),
50            kernels: RwLock::new(HashMap::default()),
51            grouped_kernels: RwLock::new(HashMap::default()),
52        };
53
54        // Register the built-in aggregate functions
55        this.register(First);
56        this.register(IsConstant);
57        this.register(IsSorted);
58        this.register(Last);
59        this.register(MinMax);
60        this.register(NanCount);
61        this.register(Sum);
62
63        // Register the built-in aggregate kernels.
64        this.register_aggregate_kernel(Chunked::ID, None, &ChunkedArrayAggregate);
65        this.register_aggregate_kernel(Dict::ID, Some(MinMax.id()), &DictMinMaxKernel);
66        this.register_aggregate_kernel(Dict::ID, Some(IsConstant.id()), &DictIsConstantKernel);
67        this.register_aggregate_kernel(Dict::ID, Some(IsSorted.id()), &DictIsSortedKernel);
68
69        this
70    }
71}
72
73impl AggregateFnSession {
74    /// Returns the aggregate function registry.
75    pub fn registry(&self) -> &AggregateFnRegistry {
76        &self.registry
77    }
78
79    /// Register an aggregate function vtable in the session, replacing any existing vtable with
80    /// the same ID.
81    pub fn register<V: AggregateFnVTable>(&self, vtable: V) {
82        self.registry
83            .register(vtable.id(), Arc::new(vtable) as AggregateFnPluginRef);
84    }
85
86    /// Register an aggregate function kernel for a specific aggregate function and array type.
87    pub fn register_aggregate_kernel(
88        &self,
89        array_id: ArrayId,
90        agg_fn_id: Option<AggregateFnId>,
91        kernel: &'static dyn DynAggregateKernel,
92    ) {
93        self.kernels.write().insert((array_id, agg_fn_id), kernel);
94    }
95}
96
97/// Extension trait for accessing aggregate function session data.
98pub trait AggregateFnSessionExt: SessionExt {
99    /// Returns the aggregate function vtable registry.
100    fn aggregate_fns(&self) -> Ref<'_, AggregateFnSession> {
101        self.get::<AggregateFnSession>()
102    }
103}
104impl<S: SessionExt> AggregateFnSessionExt for S {}