Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 68 additions & 49 deletions encodings/alp/src/alp/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ use std::hash::Hasher;

use prost::Message;
use vortex_array::Array;
use vortex_array::ArrayDeserialization;
use vortex_array::ArrayEq;
use vortex_array::ArrayHash;
use vortex_array::ArrayId;
use vortex_array::ArrayParts;
use vortex_array::ArrayPlugin;
use vortex_array::ArrayRef;
use vortex_array::ArraySerialization;
use vortex_array::ArraySlots;
use vortex_array::ArrayView;
use vortex_array::EqMode;
Expand All @@ -34,7 +37,6 @@ use vortex_array::patches::PatchesData;
use vortex_array::patches::PatchesMetadata;
use vortex_array::require_child;
use vortex_array::require_patches;
use vortex_array::serde::ArrayChildren;
use vortex_array::smallvec::smallvec;
use vortex_array::vtable::VTable;
use vortex_array::vtable::ValidityChild;
Expand Down Expand Up @@ -113,33 +115,75 @@ impl VTable for ALP {
vortex_array::vtable::with_empty_buffers(self, array, buffers)
}

fn serialize(
fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
ALPSlots::NAMES[idx].to_string()
}

fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
let array = require_child!(array, array.encoded(), ALPSlots::ENCODED => Primitive);
require_patches!(
array,
ALPSlots::PATCH_INDICES,
ALPSlots::PATCH_VALUES,
ALPSlots::PATCH_CHUNK_OFFSETS
);

Ok(ExecutionResult::done(
execute_decompress(array, ctx)?.into_array(),
))
}

fn reduce_parent(
array: ArrayView<'_, Self>,
parent: &ArrayRef,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
RULES.evaluate(array, parent, child_idx)
}
}

impl ArrayPlugin for ALP {
fn id(&self) -> ArrayId {
VTable::id(self)
}

fn serialize(
&self,
array: &ArrayRef,
_session: &VortexSession,
) -> VortexResult<Option<Vec<u8>>> {
let exponents = array.exponents();
Ok(Some(
ALPMetadata {
exp_e: exponents.e as u32,
exp_f: exponents.f as u32,
patches: array
.patches()
.map(|p| p.to_metadata(array.len(), array.dtype()))
.transpose()?,
}
.encode_to_vec(),
))
) -> VortexResult<Option<ArraySerialization>> {
let view = array.as_::<Self>();
let exponents = view.exponents();
let metadata = ALPMetadata {
exp_e: exponents.e as u32,
exp_f: exponents.f as u32,
patches: view
.patches()
.map(|p| p.to_metadata(view.len(), view.dtype()))
.transpose()?,
}
.encode_to_vec();

Ok(Some(ArraySerialization::from_array(
ArrayPlugin::id(self),
array,
metadata,
)))
}

fn deserialize(
&self,
dtype: &DType,
len: usize,
metadata: &[u8],
_buffers: &[BufferHandle],
children: &dyn ArrayChildren,
parts: ArrayDeserialization<'_>,
session: &VortexSession,
) -> VortexResult<ArrayParts<Self>> {
) -> VortexResult<ArrayRef> {
let ArrayDeserialization {
dtype,
len,
metadata,
children,
..
} = parts;

let metadata = ALPMetadata::decode(metadata)?;
let encoded_ptype = match &dtype {
DType::Primitive(PType::F32, n) => DType::Primitive(PType::I32, *n),
Expand Down Expand Up @@ -177,39 +221,14 @@ impl VTable for ALP {
},
patches,
);
Ok(ArrayParts::new(
Ok(Array::<Self>::try_from_parts(ArrayParts::new(
self.clone(),
dtype.clone(),
len,
data,
slots,
))
}

fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
ALPSlots::NAMES[idx].to_string()
}

fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
let array = require_child!(array, array.encoded(), ALPSlots::ENCODED => Primitive);
require_patches!(
array,
ALPSlots::PATCH_INDICES,
ALPSlots::PATCH_VALUES,
ALPSlots::PATCH_CHUNK_OFFSETS
);

Ok(ExecutionResult::done(
execute_decompress(array, ctx)?.into_array(),
))
}

fn reduce_parent(
array: ArrayView<'_, Self>,
parent: &ArrayRef,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
RULES.evaluate(array, parent, child_idx)
))?
.into_array())
}
}

Expand Down
22 changes: 12 additions & 10 deletions encodings/alp/src/alp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ mod tests {

use prost::Message;
use rstest::rstest;
use vortex_array::Array;
use vortex_array::ArrayDeserialization;
use vortex_array::ArrayPlugin;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::arrays::PrimitiveArray;
Expand All @@ -39,7 +40,6 @@ mod tests {
use vortex_array::dtype::PType;
use vortex_array::patches::PatchesMetadata;
use vortex_array::test_harness::check_metadata;
use vortex_array::vtable::VTable;
use vortex_error::VortexResult;
use vortex_session::VortexSession;

Expand Down Expand Up @@ -81,16 +81,18 @@ mod tests {
let encoded = alp_encode(values.as_view(), Some(Exponents { e: 0, f: 0 }), &mut ctx)?;
let sliced = <ALP as SliceKernel>::slice(encoded.as_view(), start..4096, &mut ctx)?
.expect("ALP slices");
let metadata = ALP::serialize(sliced.as_::<ALP>(), &session)?.expect("ALP serializes");
let parts = ALP.deserialize(
sliced.dtype(),
sliced.len(),
&metadata,
&[],
&sliced.children(),
let serialization = ALP.serialize(&sliced, &session)?.expect("ALP serializes");
let read = ALP.deserialize(
ArrayDeserialization::new(
serialization.serialized_id,
sliced.dtype(),
sliced.len(),
&serialization.metadata,
&[],
&sliced.children(),
),
&session,
)?;
let read = Array::<ALP>::try_from_parts(parts)?.into_array();
let expected = values.into_array().slice(start..4096)?;

let patch_index = ((start / 1024 + 1) * 1024).next_multiple_of(7) - start;
Expand Down
21 changes: 1 addition & 20 deletions encodings/alp/src/alp/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//!
//! This enables zero-cost backward compatibility with previously written datasets.

use vortex_array::Array;
use vortex_array::ArrayDeserialization;
use vortex_array::ArrayId;
use vortex_array::ArrayPlugin;
Expand All @@ -18,7 +17,6 @@ use vortex_array::VortexSessionExecute;
use vortex_array::arrays::Patched;
use vortex_error::VortexResult;
use vortex_error::vortex_ensure_eq;
use vortex_error::vortex_err;
use vortex_session::VortexSession;

use crate::ALP;
Expand All @@ -38,14 +36,6 @@ impl ArrayPlugin for ALPPatchedPlugin {
ArrayVTable::id(&ALP)
}

fn read_ids(&self) -> Vec<ArrayId> {
vec![self.id()]
}

fn write_ids(&self) -> Vec<ArrayId> {
vec![self.id()]
}

fn serialize(
&self,
array: &ArrayRef,
Expand All @@ -65,16 +55,7 @@ impl ArrayPlugin for ALPPatchedPlugin {
self.id(),
"ALP plugin does not recognize serialized ID"
);
let alp_array = Array::<ALP>::try_from_parts(ArrayVTable::deserialize(
&ALP,
parts.dtype,
parts.len,
parts.metadata,
parts.buffers,
parts.children,
session,
)?)
.map_err(|_| vortex_err!("ALP plugin should only deserialize vortex.alp"))?;
let alp_array = ALP.deserialize(parts, session)?.downcast::<ALP>();

// Check if there are interior patches to externalize.
let Some(patches) = alp_array.patches() else {
Expand Down
Loading
Loading