Skip to content

Commit f1b917e

Browse files
committed
AggregateFnRef [de]serialize
Signed-off-by: blaginin <github@blaginin.me>
1 parent d260d42 commit f1b917e

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

vortex-array/src/aggregate_fn/erased.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ use std::hash::Hash;
1010
use std::hash::Hasher;
1111
use std::sync::Arc;
1212

13+
use arcref::ArcRef;
1314
use vortex_error::VortexExpect;
1415
use vortex_error::VortexResult;
16+
use vortex_error::vortex_bail;
17+
use vortex_error::vortex_err;
18+
use vortex_session::VortexSession;
1519
use vortex_utils::debug_with::DebugWith;
1620

1721
use crate::aggregate_fn::AccumulatorRef;
1822
use crate::aggregate_fn::AggregateFnId;
1923
use crate::aggregate_fn::AggregateFnVTable;
2024
use crate::aggregate_fn::GroupedAccumulatorRef;
2125
use crate::aggregate_fn::options::AggregateFnOptions;
26+
use crate::aggregate_fn::session::AggregateFnSessionExt;
2227
use crate::aggregate_fn::typed::AggregateFnInner;
2328
use crate::aggregate_fn::typed::DynAggregateFn;
2429
use crate::dtype::DType;
@@ -34,6 +39,37 @@ use crate::dtype::DType;
3439
pub struct AggregateFnRef(pub(super) Arc<dyn DynAggregateFn>);
3540

3641
impl AggregateFnRef {
42+
/// Deserialize an aggregate function from its ID and serialized metadata bytes.
43+
///
44+
/// Looks up the aggregate function plugin by ID in the session's registry
45+
/// and delegates deserialization to it.
46+
pub fn deserialize(id: &str, metadata: &[u8], session: &VortexSession) -> VortexResult<Self> {
47+
let agg_fn_id: AggregateFnId = ArcRef::new_arc(Arc::from(id));
48+
let plugin = session
49+
.aggregate_fns()
50+
.registry()
51+
.find(&agg_fn_id)
52+
.ok_or_else(|| vortex_err!("unknown aggregate function id: {}", id))?;
53+
let agg_fn = plugin.deserialize(metadata, session)?;
54+
55+
if agg_fn.id() != agg_fn_id {
56+
vortex_bail!(
57+
"Aggregate function ID mismatch: expected {}, got {}",
58+
agg_fn_id,
59+
agg_fn.id()
60+
);
61+
}
62+
63+
Ok(agg_fn)
64+
}
65+
66+
/// Serialize this aggregate function's options to bytes.
67+
///
68+
/// Returns `Ok(None)` if the function is not serializable.
69+
pub fn serialize(&self) -> VortexResult<Option<Vec<u8>>> {
70+
self.0.options_serialize()
71+
}
72+
3773
/// Returns the ID of this aggregate function.
3874
pub fn id(&self) -> AggregateFnId {
3975
self.0.id()

0 commit comments

Comments
 (0)