Skip to main content

vortex_array/aggregate_fn/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5
6use vortex_error::VortexResult;
7use vortex_session::VortexSession;
8
9use crate::aggregate_fn::AggregateFn;
10use crate::aggregate_fn::AggregateFnId;
11use crate::aggregate_fn::AggregateFnRef;
12use crate::aggregate_fn::AggregateFnVTable;
13
14/// Reference-counted pointer to an aggregate function plugin.
15pub type AggregateFnPluginRef = Arc<dyn AggregateFnPlugin>;
16
17/// Registry trait for ID-based deserialization of aggregate functions.
18///
19/// Plugins are registered in the session by their [`AggregateFnId`]. When a serialized aggregate
20/// function is encountered, the session resolves the ID to the plugin and calls [`deserialize`]
21/// to reconstruct the value as an [`AggregateFnRef`].
22///
23/// [`deserialize`]: AggregateFnPlugin::deserialize
24pub trait AggregateFnPlugin: 'static + Send + Sync {
25    /// Returns the ID for this aggregate function.
26    fn id(&self) -> AggregateFnId;
27
28    /// Deserialize an aggregate function from serialized metadata.
29    fn deserialize(&self, metadata: &[u8], session: &VortexSession)
30    -> VortexResult<AggregateFnRef>;
31}
32
33impl std::fmt::Debug for dyn AggregateFnPlugin {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_tuple("AggregateFnPlugin")
36            .field(&self.id())
37            .finish()
38    }
39}
40
41impl<V: AggregateFnVTable> AggregateFnPlugin for V {
42    fn id(&self) -> AggregateFnId {
43        AggregateFnVTable::id(self)
44    }
45
46    fn deserialize(
47        &self,
48        metadata: &[u8],
49        session: &VortexSession,
50    ) -> VortexResult<AggregateFnRef> {
51        let options = AggregateFnVTable::deserialize(self, metadata, session)?;
52        Ok(AggregateFn::new(self.clone(), options).erased())
53    }
54}