mod stored;
use prost::Message;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure_eq;
use vortex_error::vortex_err;
use vortex_session::VortexSession;
use self::stored::StoredDecimal;
use super::Decimal;
use crate::ArrayRef;
use crate::IntoArray;
use crate::VortexSessionExecute;
use crate::array::Array;
use crate::array::ArrayDeserialization;
use crate::array::ArrayId;
use crate::array::ArrayPlugin;
use crate::array::ArraySerialization;
use crate::array::VTable;
use crate::arrays::Widen;
use crate::arrays::WidenArray;
use crate::arrays::decimal::DecimalDataParts;
use crate::dtype::DecimalType;
use crate::expr::stats::Precision;
#[derive(prost::Message)]
struct DecimalMetadata {
#[prost(int32, tag = "1")]
values_type: i32,
}
impl DecimalMetadata {
fn get_values_type(&self) -> VortexResult<DecimalType> {
DecimalType::try_from(self.values_type).map_err(|_| {
vortex_err!(
"DecimalMetadata values_type must be a known DecimalType, got {}",
self.values_type
)
})
}
}
#[derive(Clone, Debug)]
pub struct DecimalPlugin;
impl ArrayPlugin for DecimalPlugin {
fn id(&self) -> ArrayId {
VTable::id(&Decimal)
}
fn read_ids(&self) -> Vec<ArrayId> {
vec![self.id()]
}
fn write_ids(&self) -> Vec<ArrayId> {
vec![self.id()]
}
fn serialize(
&self,
array: &ArrayRef,
_session: &VortexSession,
) -> VortexResult<Option<ArraySerialization>> {
let Some(decimal) = array.as_opt::<Decimal>() else {
vortex_bail!(
"DecimalPlugin serializes only Decimal arrays, got {}",
array.encoding_id()
);
};
let metadata = DecimalMetadata {
values_type: decimal.values_type() as i32,
};
Ok(Some(ArraySerialization::from_array(
self.id(),
array,
metadata.encode_to_vec(),
)))
}
fn deserialize(
&self,
parts: ArrayDeserialization<'_>,
session: &VortexSession,
) -> VortexResult<ArrayRef> {
vortex_ensure_eq!(
parts.serialized_id,
self.id(),
"DecimalPlugin reads only its own serialized ID",
);
let stored = StoredDecimal::deserialize(
parts.dtype,
parts.len,
parts.metadata,
parts.buffers,
parts.children,
)?;
if !stored.is_legacy() {
return Ok(Array::<Decimal>::try_from_parts(stored.into_array_parts()?)?.into_array());
}
let parts = DecimalDataParts {
decimal_dtype: stored.decimal_dtype,
values: stored.values,
values_type: stored.values_type,
validity: stored.validity,
};
WidenArray::from_stored_decimal(
parts,
Precision::Absent,
&mut session.create_execution_ctx(),
)
}
fn is_supported_encoding(&self, id: &ArrayId) -> bool {
*id == VTable::id(&Decimal) || *id == VTable::id(&Widen)
}
}
#[cfg(test)]
mod tests;