vortex_array/arrays/scalar_fn/
plugin.rs1use vortex_error::VortexResult;
5use vortex_error::vortex_ensure_eq;
6use vortex_session::VortexSession;
7
8use crate::ArrayDeserialization;
9use crate::ArrayId;
10use crate::ArrayPlugin;
11use crate::ArrayRef;
12use crate::ArraySerialization;
13use crate::IntoArray;
14use crate::arrays::ScalarFnArray;
15use crate::arrays::scalar_fn::ExactScalarFn;
16use crate::arrays::scalar_fn::ScalarFnArrayView;
17use crate::dtype::DType;
18use crate::scalar_fn::ScalarFnVTable;
19use crate::scalar_fn::TypedScalarFnInstance;
20use crate::serde::ArrayChildren;
21
22pub struct ScalarFnArrayPlugin<V: ScalarFnVTable>(V);
24
25impl<V: ScalarFnVTable> ScalarFnArrayPlugin<V> {
26 pub fn new(vtable: V) -> Self {
28 Self(vtable)
29 }
30}
31
32pub trait ScalarFnArrayVTable: ScalarFnVTable {
33 fn serialize(
38 &self,
39 view: &ScalarFnArrayView<Self>,
40 session: &VortexSession,
41 ) -> VortexResult<Option<Vec<u8>>>;
42
43 fn deserialize(
45 &self,
46 dtype: &DType,
47 len: usize,
48 metadata: &[u8],
49 children: &dyn ArrayChildren,
50 session: &VortexSession,
51 ) -> VortexResult<ScalarFnArrayParts<Self>>;
52}
53
54pub struct ScalarFnArrayParts<V: ScalarFnVTable> {
56 pub options: V::Options,
57 pub children: Vec<ArrayRef>,
58}
59
60impl<V: ScalarFnVTable + ScalarFnArrayVTable> ArrayPlugin for ScalarFnArrayPlugin<V> {
61 fn id(&self) -> ArrayId {
62 self.0.id()
63 }
64
65 fn read_ids(&self) -> Vec<ArrayId> {
66 vec![self.id()]
67 }
68
69 fn write_ids(&self) -> Vec<ArrayId> {
70 vec![self.id()]
71 }
72
73 fn serialize(
74 &self,
75 array: &ArrayRef,
76 session: &VortexSession,
77 ) -> VortexResult<Option<ArraySerialization>> {
78 let scalar_fn = array.as_::<ExactScalarFn<V>>();
80 Ok(
81 <V as ScalarFnArrayVTable>::serialize(&self.0, &scalar_fn, session)?
82 .map(|metadata| ArraySerialization::from_array(self.id(), array, metadata)),
83 )
84 }
85
86 fn deserialize(
87 &self,
88 parts: ArrayDeserialization<'_>,
89 session: &VortexSession,
90 ) -> VortexResult<ArrayRef> {
91 vortex_ensure_eq!(
92 parts.serialized_id,
93 self.id(),
94 "scalar function array plugin does not recognize serialized ID",
95 );
96 let len = parts.len;
97 let scalar_parts = <V as ScalarFnArrayVTable>::deserialize(
98 &self.0,
99 parts.dtype,
100 parts.len,
101 parts.metadata,
102 parts.children,
103 session,
104 )?;
105 Ok(ScalarFnArray::try_new_with_len(
106 TypedScalarFnInstance::new(self.0.clone(), scalar_parts.options).erased(),
107 scalar_parts.children,
108 len,
109 )?
110 .into_array())
111 }
112}