|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! A custom [`ArrayPlugin`] that lets you load in and deserialize a `BitPacked` array with interior |
| 5 | +//! patches as a `PatchedArray` that wraps a patchless `BitPacked` array. |
| 6 | +//! |
| 7 | +//! This enables zero-cost backward compatibility with previously written datasets. |
| 8 | +
|
| 9 | +use vortex_array::ArrayId; |
| 10 | +use vortex_array::ArrayPlugin; |
| 11 | +use vortex_array::ArrayRef; |
| 12 | +use vortex_array::IntoArray; |
| 13 | +use vortex_array::VortexSessionExecute; |
| 14 | +use vortex_array::arrays::Patched; |
| 15 | +use vortex_array::buffer::BufferHandle; |
| 16 | +use vortex_array::dtype::DType; |
| 17 | +use vortex_array::serde::ArrayChildren; |
| 18 | +use vortex_error::VortexResult; |
| 19 | +use vortex_error::vortex_err; |
| 20 | +use vortex_session::VortexSession; |
| 21 | + |
| 22 | +use crate::BitPacked; |
| 23 | +use crate::BitPackedArrayExt; |
| 24 | + |
| 25 | +/// Custom deserialization plugin that converts a BitPacked array with interior |
| 26 | +/// Patches into a PatchedArray holding a BitPacked array. |
| 27 | +#[derive(Debug, Clone)] |
| 28 | +pub struct BitPackedPatchedPlugin; |
| 29 | + |
| 30 | +impl ArrayPlugin for BitPackedPatchedPlugin { |
| 31 | + fn id(&self) -> ArrayId { |
| 32 | + // We reuse the existing `BitPacked` ID so that we can take over its |
| 33 | + // deserialization pathway. |
| 34 | + BitPacked::ID |
| 35 | + } |
| 36 | + |
| 37 | + fn deserialize( |
| 38 | + &self, |
| 39 | + dtype: &DType, |
| 40 | + len: usize, |
| 41 | + metadata: &[u8], |
| 42 | + buffers: &[BufferHandle], |
| 43 | + children: &dyn ArrayChildren, |
| 44 | + session: &VortexSession, |
| 45 | + ) -> VortexResult<ArrayRef> { |
| 46 | + let bitpacked = BitPacked |
| 47 | + .deserialize(dtype, len, metadata, buffers, children, session)? |
| 48 | + .try_downcast::<BitPacked>() |
| 49 | + .map_err(|_| { |
| 50 | + vortex_err!("BitPacked plugin should only deserialize vortex.bitpacked") |
| 51 | + })?; |
| 52 | + |
| 53 | + // Create a new BitPackedArray without the interior patches installed. |
| 54 | + let Some(patches) = bitpacked.patches() else { |
| 55 | + return Ok(bitpacked.into_array()); |
| 56 | + }; |
| 57 | + |
| 58 | + let packed = bitpacked.packed().clone(); |
| 59 | + let ptype = bitpacked.dtype().as_ptype(); |
| 60 | + let validity = bitpacked.validity()?; |
| 61 | + let bw = bitpacked.bit_width; |
| 62 | + let len = bitpacked.len(); |
| 63 | + let offset = bitpacked.offset(); |
| 64 | + |
| 65 | + let bitpacked_without_patches = |
| 66 | + BitPacked::try_new(packed, ptype, validity, None, bw, len, offset)?.into_array(); |
| 67 | + |
| 68 | + let patched = Patched::from_array_and_patches( |
| 69 | + bitpacked_without_patches, |
| 70 | + &patches, |
| 71 | + &mut session.create_execution_ctx(), |
| 72 | + )?; |
| 73 | + |
| 74 | + Ok(patched.into_array()) |
| 75 | + } |
| 76 | +} |
0 commit comments