vortex_array/arrays/runend/array/
plugin.rs1use prost::Message;
7use vortex_error::VortexExpect;
8use vortex_error::VortexResult;
9use vortex_error::vortex_ensure_eq;
10use vortex_error::vortex_err;
11use vortex_session::VortexSession;
12
13use super::RunEnd;
14use super::RunEndArrayExt;
15use super::RunEndArraySlotsExt;
16use super::RunEndData;
17use super::RunEndMetadata;
18use super::RunEndSlots;
19use crate::Array;
20use crate::ArrayDeserialization;
21use crate::ArrayId;
22use crate::ArrayParts;
23use crate::ArrayPlugin;
24use crate::ArrayRef;
25use crate::ArraySerialization;
26use crate::IntoArray;
27use crate::VTable;
28use crate::VortexSessionExecute;
29use crate::dtype::DType;
30use crate::dtype::Nullability;
31use crate::dtype::PType;
32
33impl ArrayPlugin for RunEnd {
34 fn id(&self) -> ArrayId {
35 VTable::id(self)
36 }
37
38 fn serialize(
39 &self,
40 array: &ArrayRef,
41 _session: &VortexSession,
42 ) -> VortexResult<Option<ArraySerialization>> {
43 vortex_ensure_eq!(
44 array.encoding_id(),
45 ArrayPlugin::id(self),
46 "RunEnd plugin cannot serialize an array of another encoding",
47 );
48 let array = array.as_::<RunEnd>();
49 let metadata = RunEndMetadata {
50 ends_ptype: PType::try_from(array.ends().dtype()).vortex_expect("Must be a valid PType")
51 as i32,
52 num_runs: array.ends().len() as u64,
53 offset: array.offset() as u64,
54 }
55 .encode_to_vec();
56 Ok(Some(ArraySerialization::from_array(
57 ArrayPlugin::id(self),
58 array.array(),
59 metadata,
60 )))
61 }
62
63 fn deserialize(
64 &self,
65 parts: ArrayDeserialization<'_>,
66 session: &VortexSession,
67 ) -> VortexResult<ArrayRef> {
68 vortex_ensure_eq!(
69 parts.serialized_id,
70 ArrayPlugin::id(self),
71 "RunEnd plugin does not recognize the serialized ID",
72 );
73 let ArrayDeserialization {
74 dtype,
75 len,
76 metadata,
77 children,
78 ..
79 } = parts;
80 let metadata = RunEndMetadata::decode(metadata)?;
81 let ends_ptype = metadata.get_ends_ptype()?;
82 let ends_dtype = DType::Primitive(ends_ptype, Nullability::NonNullable);
83 let runs = usize::try_from(metadata.num_runs).map_err(|_| {
84 vortex_err!(
85 "RunEndMetadata num_runs {} exceeds usize range",
86 metadata.num_runs
87 )
88 })?;
89 let ends = children.get(0, &ends_dtype, runs)?;
90
91 let values = children.get(1, dtype, runs)?;
92 let offset = usize::try_from(metadata.offset).map_err(|_| {
93 vortex_err!(
94 "RunEndMetadata offset {} exceeds usize range",
95 metadata.offset
96 )
97 })?;
98 RunEndData::validate_parts(
99 &ends,
100 &values,
101 offset,
102 len,
103 &mut session.create_execution_ctx(),
104 )?;
105 let slots = RunEndSlots { ends, values }.into_slots();
106 let data = RunEndData::new(offset);
107 Ok(Array::try_from_parts(ArrayParts::new(
108 self.clone(),
109 dtype.clone(),
110 len,
111 data,
112 slots,
113 ))?
114 .into_array())
115 }
116}