vortex_array/arrays/decimal/vtable/plugin/
mod.rs1mod stored;
22
23use prost::Message;
24use vortex_error::VortexResult;
25use vortex_error::vortex_bail;
26use vortex_error::vortex_ensure_eq;
27use vortex_error::vortex_err;
28use vortex_session::VortexSession;
29
30use self::stored::StoredDecimal;
31use super::Decimal;
32use crate::ArrayRef;
33use crate::IntoArray;
34use crate::VortexSessionExecute;
35use crate::array::Array;
36use crate::array::ArrayDeserialization;
37use crate::array::ArrayId;
38use crate::array::ArrayPlugin;
39use crate::array::ArraySerialization;
40use crate::array::VTable;
41use crate::arrays::Widen;
42use crate::arrays::WidenArray;
43use crate::arrays::decimal::DecimalDataParts;
44use crate::dtype::DecimalType;
45use crate::stats::Precision;
46
47#[derive(prost::Message)]
49struct DecimalMetadata {
50 #[prost(int32, tag = "1")]
55 values_type: i32,
56}
57
58impl DecimalMetadata {
59 fn get_values_type(&self) -> VortexResult<DecimalType> {
60 DecimalType::try_from(self.values_type).map_err(|_| {
61 vortex_err!(
62 "DecimalMetadata values_type must be a known DecimalType, got {}",
63 self.values_type
64 )
65 })
66 }
67}
68
69impl ArrayPlugin for Decimal {
70 fn id(&self) -> ArrayId {
71 VTable::id(self)
72 }
73
74 fn serialize(
75 &self,
76 array: &ArrayRef,
77 _session: &VortexSession,
78 ) -> VortexResult<Option<ArraySerialization>> {
79 let Some(decimal) = array.as_opt::<Decimal>() else {
80 vortex_bail!(
81 "Decimal plugin serializes only Decimal arrays, got {}",
82 array.encoding_id()
83 );
84 };
85 let metadata = DecimalMetadata {
86 values_type: decimal.values_type() as i32,
87 };
88
89 Ok(Some(ArraySerialization::from_array(
90 ArrayPlugin::id(self),
91 array,
92 metadata.encode_to_vec(),
93 )))
94 }
95
96 fn deserialize(
97 &self,
98 parts: ArrayDeserialization<'_>,
99 session: &VortexSession,
100 ) -> VortexResult<ArrayRef> {
101 vortex_ensure_eq!(
102 parts.serialized_id,
103 ArrayPlugin::id(self),
104 "Decimal plugin reads only its own serialized ID",
105 );
106
107 let stored = StoredDecimal::deserialize(
108 parts.dtype,
109 parts.len,
110 parts.metadata,
111 parts.buffers,
112 parts.children,
113 )?;
114
115 if !stored.is_legacy() {
116 return Ok(Array::<Decimal>::try_from_parts(stored.into_array_parts())?.into_array());
117 }
118
119 let parts = DecimalDataParts {
122 decimal_dtype: stored.decimal_dtype,
123 values: stored.values,
124 values_type: stored.values_type,
125 validity: stored.validity,
126 };
127
128 WidenArray::from_stored_decimal(
130 parts,
131 Precision::Absent,
132 &mut session.create_execution_ctx(),
133 )
134 }
135
136 fn is_supported_encoding(&self, id: &ArrayId) -> bool {
137 *id == VTable::id(&Decimal) || *id == VTable::id(&Widen)
138 }
139}
140
141#[cfg(test)]
142mod tests;