use vortex_array::ArrayRef;
use vortex_array::ArraySlots;
use vortex_array::Canonical;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::arrays::ListArray;
use vortex_array::arrays::ListViewArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::list::ListArrayExt;
use vortex_array::arrays::listview::ListViewArrayExt;
use vortex_array::arrays::primitive::PrimitiveArrayExt;
use vortex_error::VortexResult;
use super::ROOT_SCHEME_ID;
use crate::CascadingCompressor;
use crate::scheme::CompressorContext;
pub(super) mod root_list_children {
pub const OFFSETS: usize = 1;
pub const SIZES: usize = 2;
}
impl CascadingCompressor {
pub(super) fn compress_list_array(
&self,
list_array: ListArray,
compress_ctx: CompressorContext,
exec_ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let list_array = list_array.reset_offsets(true, exec_ctx)?;
let compressed_elems = self.compress(list_array.elements(), exec_ctx)?;
let offset_ctx =
compress_ctx.descend_with_scheme(ROOT_SCHEME_ID, root_list_children::OFFSETS);
let list_offsets_primitive = list_array
.offsets()
.clone()
.execute::<PrimitiveArray>(exec_ctx)?
.narrow(exec_ctx)?;
let compressed_offsets = self.compress_canonical(
Canonical::Primitive(list_offsets_primitive),
offset_ctx,
exec_ctx,
)?;
Ok(
ListArray::try_new(compressed_elems, compressed_offsets, list_array.validity()?)?
.into_array(),
)
}
pub(super) fn compress_list_view_array(
&self,
list_view: ListViewArray,
compress_ctx: CompressorContext,
exec_ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let compressed_elems = self.compress(list_view.elements(), exec_ctx)?;
let offset_ctx = compress_ctx
.clone()
.descend_with_scheme(ROOT_SCHEME_ID, root_list_children::OFFSETS);
let list_view_offsets_primitive = list_view
.offsets()
.clone()
.execute::<PrimitiveArray>(exec_ctx)?
.narrow(exec_ctx)?;
let compressed_offsets = self.compress_canonical(
Canonical::Primitive(list_view_offsets_primitive),
offset_ctx,
exec_ctx,
)?;
let sizes_ctx = compress_ctx.descend_with_scheme(ROOT_SCHEME_ID, root_list_children::SIZES);
let list_view_sizes_primitive = list_view
.sizes()
.clone()
.execute::<PrimitiveArray>(exec_ctx)?
.narrow(exec_ctx)?;
let compressed_sizes = self.compress_canonical(
Canonical::Primitive(list_view_sizes_primitive),
sizes_ctx,
exec_ctx,
)?;
Ok(ListViewArray::try_new(
compressed_elems,
compressed_offsets,
compressed_sizes,
list_view.validity()?,
)?
.into_array())
}
pub(super) fn compress_physical_slots(
&self,
array: &ArrayRef,
exec_ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let slots = array
.slots()
.iter()
.map(|slot| {
slot.as_ref()
.map(|child| self.compress(child, exec_ctx))
.transpose()
})
.collect::<VortexResult<ArraySlots>>()?;
unsafe { array.clone().with_slots(slots) }
}
}