|
| 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(crate) 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 serialize( |
| 38 | + &self, |
| 39 | + array: &ArrayRef, |
| 40 | + session: &VortexSession, |
| 41 | + ) -> VortexResult<Option<Vec<u8>>> { |
| 42 | + // delegate to BitPacked VTable for serialization |
| 43 | + BitPacked.serialize(array, session) |
| 44 | + } |
| 45 | + |
| 46 | + fn deserialize( |
| 47 | + &self, |
| 48 | + dtype: &DType, |
| 49 | + len: usize, |
| 50 | + metadata: &[u8], |
| 51 | + buffers: &[BufferHandle], |
| 52 | + children: &dyn ArrayChildren, |
| 53 | + session: &VortexSession, |
| 54 | + ) -> VortexResult<ArrayRef> { |
| 55 | + let bitpacked = BitPacked |
| 56 | + .deserialize(dtype, len, metadata, buffers, children, session)? |
| 57 | + .try_downcast::<BitPacked>() |
| 58 | + .map_err(|_| { |
| 59 | + vortex_err!("BitPacked plugin should only deserialize fastlanes.bitpacked") |
| 60 | + })?; |
| 61 | + |
| 62 | + // Create a new BitPackedArray without the interior patches installed. |
| 63 | + let Some(patches) = bitpacked.patches() else { |
| 64 | + return Ok(bitpacked.into_array()); |
| 65 | + }; |
| 66 | + |
| 67 | + let packed = bitpacked.packed().clone(); |
| 68 | + let ptype = bitpacked.dtype().as_ptype(); |
| 69 | + let validity = bitpacked.validity()?; |
| 70 | + let bw = bitpacked.bit_width; |
| 71 | + let len = bitpacked.len(); |
| 72 | + let offset = bitpacked.offset(); |
| 73 | + |
| 74 | + let bitpacked_without_patches = |
| 75 | + BitPacked::try_new(packed, ptype, validity, None, bw, len, offset)?.into_array(); |
| 76 | + |
| 77 | + let patched = Patched::from_array_and_patches( |
| 78 | + bitpacked_without_patches, |
| 79 | + &patches, |
| 80 | + &mut session.create_execution_ctx(), |
| 81 | + )?; |
| 82 | + |
| 83 | + Ok(patched.into_array()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod tests { |
| 89 | + use std::sync::LazyLock; |
| 90 | + |
| 91 | + use vortex_array::ArrayPlugin; |
| 92 | + use vortex_array::IntoArray; |
| 93 | + use vortex_array::arrays::PatchedArray; |
| 94 | + use vortex_array::arrays::PrimitiveArray; |
| 95 | + use vortex_array::arrays::patched::PatchedArrayExt; |
| 96 | + use vortex_array::buffer::BufferHandle; |
| 97 | + use vortex_array::session::ArraySession; |
| 98 | + use vortex_array::session::ArraySessionExt; |
| 99 | + use vortex_buffer::Buffer; |
| 100 | + use vortex_error::VortexResult; |
| 101 | + use vortex_error::vortex_err; |
| 102 | + use vortex_session::VortexSession; |
| 103 | + |
| 104 | + use super::BitPackedPatchedPlugin; |
| 105 | + use crate::BitPacked; |
| 106 | + use crate::BitPackedArray; |
| 107 | + use crate::BitPackedArrayExt; |
| 108 | + use crate::BitPackedData; |
| 109 | + |
| 110 | + static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { |
| 111 | + let session = VortexSession::empty().with::<ArraySession>(); |
| 112 | + session.arrays().register(BitPackedPatchedPlugin); |
| 113 | + session |
| 114 | + }); |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_decode_bitpacked_patches() -> VortexResult<()> { |
| 118 | + // Create values where some exceed the bit width, causing patches. |
| 119 | + // With bit_width=9, max value is 511. Values >=512 become patches. |
| 120 | + let values: Buffer<i32> = (0i32..=512).collect(); |
| 121 | + let parray = values.into_array(); |
| 122 | + let bitpacked = BitPackedData::encode(&parray, 9)?; |
| 123 | + |
| 124 | + assert!( |
| 125 | + bitpacked.patches().is_some(), |
| 126 | + "Expected BitPacked array to have patches" |
| 127 | + ); |
| 128 | + |
| 129 | + let array = bitpacked.as_array(); |
| 130 | + |
| 131 | + let metadata = array.metadata(&SESSION)?.unwrap_or_default(); |
| 132 | + let children = array.children(); |
| 133 | + let buffers = array |
| 134 | + .buffers() |
| 135 | + .into_iter() |
| 136 | + .map(BufferHandle::new_host) |
| 137 | + .collect::<Vec<_>>(); |
| 138 | + |
| 139 | + let deserialized = BitPackedPatchedPlugin.deserialize( |
| 140 | + array.dtype(), |
| 141 | + array.len(), |
| 142 | + &metadata, |
| 143 | + &buffers, |
| 144 | + &children, |
| 145 | + &SESSION, |
| 146 | + )?; |
| 147 | + |
| 148 | + let patched: PatchedArray = deserialized |
| 149 | + .try_downcast() |
| 150 | + .map_err(|a| vortex_err!("Expected Patched, got {}", a.encoding_id()))?; |
| 151 | + |
| 152 | + let inner_bitpacked: BitPackedArray = patched |
| 153 | + .base_array() |
| 154 | + .clone() |
| 155 | + .try_downcast() |
| 156 | + .map_err(|a| vortex_err!("Expected inner BitPacked, got {}", a.encoding_id()))?; |
| 157 | + |
| 158 | + assert!( |
| 159 | + inner_bitpacked.patches().is_none(), |
| 160 | + "Inner BitPacked should NOT have patches" |
| 161 | + ); |
| 162 | + |
| 163 | + Ok(()) |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn bitpacked_without_patches_stays_bitpacked() -> VortexResult<()> { |
| 168 | + // With bit_width=16, max value is 65535. All values 0..100 fit. |
| 169 | + let values: Buffer<i32> = (0i32..100).collect(); |
| 170 | + let parray = values.into_array(); |
| 171 | + let bitpacked = BitPackedData::encode(&parray, 16)?; |
| 172 | + |
| 173 | + assert!( |
| 174 | + bitpacked.patches().is_none(), |
| 175 | + "Expected BitPacked array without patches" |
| 176 | + ); |
| 177 | + |
| 178 | + let array = bitpacked.as_array(); |
| 179 | + |
| 180 | + let metadata = array.metadata(&SESSION)?.unwrap_or_default(); |
| 181 | + let children = array.children(); |
| 182 | + let buffers = array |
| 183 | + .buffers() |
| 184 | + .into_iter() |
| 185 | + .map(BufferHandle::new_host) |
| 186 | + .collect::<Vec<_>>(); |
| 187 | + |
| 188 | + let deserialized = BitPackedPatchedPlugin.deserialize( |
| 189 | + array.dtype(), |
| 190 | + array.len(), |
| 191 | + &metadata, |
| 192 | + &buffers, |
| 193 | + &children, |
| 194 | + &SESSION, |
| 195 | + )?; |
| 196 | + |
| 197 | + let result = deserialized |
| 198 | + .try_downcast::<BitPacked>() |
| 199 | + .map_err(|a| vortex_err!("Expected deserialize BitPacked, got {}", a.encoding_id()))?; |
| 200 | + |
| 201 | + assert!(result.patches().is_none(), "Result should not have patches"); |
| 202 | + |
| 203 | + Ok(()) |
| 204 | + } |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn primitive_array_returns_error() -> VortexResult<()> { |
| 208 | + let array = PrimitiveArray::from_iter([1i32, 2, 3]).into_array(); |
| 209 | + |
| 210 | + let metadata = array.metadata(&SESSION)?.unwrap_or_default(); |
| 211 | + let children = array.children(); |
| 212 | + let buffers = array |
| 213 | + .buffers() |
| 214 | + .into_iter() |
| 215 | + .map(BufferHandle::new_host) |
| 216 | + .collect::<Vec<_>>(); |
| 217 | + |
| 218 | + let result = BitPackedPatchedPlugin.deserialize( |
| 219 | + array.dtype(), |
| 220 | + array.len(), |
| 221 | + &metadata, |
| 222 | + &buffers, |
| 223 | + &children, |
| 224 | + &SESSION, |
| 225 | + ); |
| 226 | + |
| 227 | + assert!( |
| 228 | + result.is_err(), |
| 229 | + "Expected error when deserializing PrimitiveArray with BitPackedPatchedPlugin" |
| 230 | + ); |
| 231 | + |
| 232 | + Ok(()) |
| 233 | + } |
| 234 | +} |
0 commit comments