use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure_eq;
use vortex_session::VortexSession;
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::Extension;
use crate::arrays::extension::array::ExtensionSlots;
use crate::dtype::DType;
impl ArrayPlugin for Extension {
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),
"Extension plugin cannot serialize an array of another encoding",
);
Ok(Some(ArraySerialization::from_array(
ArrayPlugin::id(self),
array,
vec![],
)))
}
fn deserialize(
&self,
parts: ArrayDeserialization<'_>,
_session: &VortexSession,
) -> VortexResult<ArrayRef> {
vortex_ensure_eq!(
parts.serialized_id,
ArrayPlugin::id(self),
"Extension plugin does not recognize the serialized ID",
);
let ArrayDeserialization {
dtype,
len,
metadata,
children,
..
} = parts;
if !metadata.is_empty() {
vortex_bail!(
"ExtensionArray expects empty metadata, got {} bytes",
metadata.len()
);
}
let DType::Extension(ext_dtype) = dtype else {
vortex_bail!("Not an extension DType");
};
if children.len() != 1 {
vortex_bail!("Expected 1 child, got {}", children.len());
}
let storage = children.get(0, ext_dtype.storage_dtype(), len)?;
Ok(Array::try_from_parts(ArrayParts::new(
self.clone(),
dtype.clone(),
len,
EmptyArrayData,
ExtensionSlots { storage }.into_slots(),
))?
.into_array())
}
}