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::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
38/// Registry of aggregate function vtables.
39pub type AggregateFnRegistry = Registry<AggregateFnPluginRef>;
40
41/// Session state for aggregate function vtables.
42#[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        // Register the built-in aggregate functions
71        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        // Register the built-in aggregate kernels.
83        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    /// Returns the aggregate function registry.
94    pub fn registry(&self) -> &AggregateFnRegistry {
95        &self.registry
96    }
97
98    /// Register an aggregate function vtable in the session, replacing any existing vtable with
99    /// the same ID.
100    pub fn register<V: AggregateFnVTable>(&self, vtable: V) {
101        self.registry
102            .register(vtable.id(), Arc::new(vtable) as AggregateFnPluginRef);
103    }
104
105    /// Register an aggregate function kernel for a specific aggregate function and array type.
106    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
118/// Extension trait for accessing aggregate function session data.
119pub trait AggregateFnSessionExt: SessionExt {
120    /// Returns the aggregate function vtable registry.
121    fn aggregate_fns(&self) -> Ref<'_, AggregateFnSession> {
122        self.get::<AggregateFnSession>()
123    }
124}
125impl<S: SessionExt> AggregateFnSessionExt for S {}