use prost::Message;
use vortex_error::VortexResult;
use vortex_error::vortex_ensure;
use vortex_error::vortex_ensure_eq;
use vortex_session::VortexSession;
use super::VariantMetadataProto;
use crate::Array;
use crate::ArrayDeserialization;
use crate::ArrayId;
use crate::ArrayParts;
use crate::ArrayPlugin;
use crate::ArrayRef;
use crate::ArraySerialization;
use crate::IntoArray;
use crate::VTable;
use crate::array::EmptyArrayData;
use crate::arrays::Variant;
use crate::arrays::variant::VariantArraySlotsExt;
use crate::arrays::variant::VariantSlots;
use crate::dtype::DType;
impl ArrayPlugin for Variant {
fn id(&self) -> ArrayId {
VTable::id(self)
}
fn serialize(
&self,
array: &ArrayRef,
_session: &VortexSession,
) -> VortexResult<Option<ArraySerialization>> {
vortex_ensure_eq!(
array.encoding_id(),
ArrayPlugin::id(self),
"Variant plugin cannot serialize an array of another encoding",
);
let array = array.as_::<Variant>();
let shredded_dtype = array
.shredded()
.map(|shredded| shredded.dtype().try_into())
.transpose()?;
let metadata = VariantMetadataProto { shredded_dtype }.encode_to_vec();
Ok(Some(ArraySerialization::from_array(
ArrayPlugin::id(self),
array.array(),
metadata,
)))
}
fn deserialize(
&self,
parts: ArrayDeserialization<'_>,
session: &VortexSession,
) -> VortexResult<ArrayRef> {
vortex_ensure_eq!(
parts.serialized_id,
ArrayPlugin::id(self),
"Variant plugin does not recognize the serialized ID",
);
let ArrayDeserialization {
dtype,
len,
metadata,
buffers,
children,
..
} = parts;
vortex_ensure!(
buffers.is_empty(),
"VariantArray expects 0 buffers, got {}",
buffers.len()
);
let proto = VariantMetadataProto::decode(metadata)?;
let shredded_dtype = proto
.shredded_dtype
.as_ref()
.map(|dtype| DType::from_proto(dtype, session))
.transpose()?;
vortex_ensure!(matches!(dtype, DType::Variant(_)), "Expected Variant DType");
let expected_children = 1 + usize::from(shredded_dtype.is_some());
vortex_ensure_eq!(children.len(), expected_children);
let core_storage = children.get(0, dtype, len)?;
let shredded = shredded_dtype
.map(|dtype| children.get(1, &dtype, len))
.transpose()?;
Ok(Array::try_from_parts(ArrayParts::new(
self.clone(),
dtype.clone(),
len,
EmptyArrayData,
VariantSlots {
core_storage,
shredded,
}
.into_slots(),
))?
.into_array())
}
}