-
Notifications
You must be signed in to change notification settings - Fork 156
add env flag for Patched array #7314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
841a9ec
saev
a10y a6975e3
build PatchedArray with env var VORTEX_EXPERIMENTAL_PATCHED_ARRAY.
a10y 09b1496
run benchmark with PatchedArray
a10y 966603e
fixup
a10y fe4b34d
locks
a10y a115ed7
undo
a10y b2a085f
add allowed
a10y d697682
public flag
a10y af75dfb
open up flag
a10y 4c5742d
address comments
a10y ff84c99
update locks
a10y 0bb0b7b
run unstable fuzz job with env flag
a10y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| //! A custom [`ArrayPlugin`] that lets you load in and deserialize a `BitPacked` array with interior | ||
| //! patches as a `PatchedArray` that wraps a patchless `BitPacked` array. | ||
| //! | ||
| //! This enables zero-cost backward compatibility with previously written datasets. | ||
|
|
||
|
a10y marked this conversation as resolved.
|
||
| use vortex_array::ArrayId; | ||
| use vortex_array::ArrayPlugin; | ||
| use vortex_array::ArrayRef; | ||
| use vortex_array::IntoArray; | ||
| use vortex_array::VortexSessionExecute; | ||
| use vortex_array::arrays::Patched; | ||
| use vortex_array::buffer::BufferHandle; | ||
| use vortex_array::dtype::DType; | ||
| use vortex_array::serde::ArrayChildren; | ||
| use vortex_error::VortexResult; | ||
| use vortex_error::vortex_err; | ||
| use vortex_session::VortexSession; | ||
|
|
||
| use crate::BitPacked; | ||
| use crate::BitPackedArrayExt; | ||
|
|
||
| /// Custom deserialization plugin that converts a BitPacked array with interior | ||
| /// Patches into a PatchedArray holding a BitPacked array. | ||
| #[derive(Debug, Clone)] | ||
| pub(crate) struct BitPackedPatchedPlugin; | ||
|
|
||
| impl ArrayPlugin for BitPackedPatchedPlugin { | ||
| fn id(&self) -> ArrayId { | ||
| // We reuse the existing `BitPacked` ID so that we can take over its | ||
| // deserialization pathway. | ||
| BitPacked::ID | ||
| } | ||
|
|
||
| fn serialize( | ||
| &self, | ||
| array: &ArrayRef, | ||
| session: &VortexSession, | ||
| ) -> VortexResult<Option<Vec<u8>>> { | ||
| // delegate to BitPacked VTable for serialization | ||
| BitPacked.serialize(array, session) | ||
| } | ||
|
|
||
| fn deserialize( | ||
| &self, | ||
| dtype: &DType, | ||
| len: usize, | ||
| metadata: &[u8], | ||
| buffers: &[BufferHandle], | ||
| children: &dyn ArrayChildren, | ||
| session: &VortexSession, | ||
| ) -> VortexResult<ArrayRef> { | ||
| let bitpacked = BitPacked | ||
| .deserialize(dtype, len, metadata, buffers, children, session)? | ||
| .try_downcast::<BitPacked>() | ||
| .map_err(|_| { | ||
| vortex_err!("BitPacked plugin should only deserialize fastlanes.bitpacked") | ||
| })?; | ||
|
|
||
| // Create a new BitPackedArray without the interior patches installed. | ||
| let Some(patches) = bitpacked.patches() else { | ||
| return Ok(bitpacked.into_array()); | ||
| }; | ||
|
|
||
| let packed = bitpacked.packed().clone(); | ||
| let ptype = bitpacked.dtype().as_ptype(); | ||
| let validity = bitpacked.validity()?; | ||
| let bw = bitpacked.bit_width; | ||
|
a10y marked this conversation as resolved.
|
||
| let len = bitpacked.len(); | ||
| let offset = bitpacked.offset(); | ||
|
|
||
| let bitpacked_without_patches = | ||
| BitPacked::try_new(packed, ptype, validity, None, bw, len, offset)?.into_array(); | ||
|
|
||
| let patched = Patched::from_array_and_patches( | ||
| bitpacked_without_patches, | ||
| &patches, | ||
| &mut session.create_execution_ctx(), | ||
| )?; | ||
|
|
||
| Ok(patched.into_array()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::LazyLock; | ||
|
|
||
| use vortex_array::ArrayPlugin; | ||
| use vortex_array::IntoArray; | ||
| use vortex_array::arrays::PatchedArray; | ||
| use vortex_array::arrays::PrimitiveArray; | ||
| use vortex_array::arrays::patched::PatchedArrayExt; | ||
| use vortex_array::buffer::BufferHandle; | ||
| use vortex_array::session::ArraySession; | ||
| use vortex_array::session::ArraySessionExt; | ||
| use vortex_buffer::Buffer; | ||
| use vortex_error::VortexResult; | ||
| use vortex_error::vortex_err; | ||
| use vortex_session::VortexSession; | ||
|
|
||
| use super::BitPackedPatchedPlugin; | ||
| use crate::BitPacked; | ||
| use crate::BitPackedArray; | ||
| use crate::BitPackedArrayExt; | ||
| use crate::BitPackedData; | ||
|
|
||
| static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { | ||
| let session = VortexSession::empty().with::<ArraySession>(); | ||
| session.arrays().register(BitPackedPatchedPlugin); | ||
| session | ||
| }); | ||
|
|
||
| #[test] | ||
| fn test_decode_bitpacked_patches() -> VortexResult<()> { | ||
| // Create values where some exceed the bit width, causing patches. | ||
| // With bit_width=9, max value is 511. Values >=512 become patches. | ||
| let values: Buffer<i32> = (0i32..=512).collect(); | ||
| let parray = values.into_array(); | ||
| let bitpacked = BitPackedData::encode(&parray, 9)?; | ||
|
|
||
| assert!( | ||
| bitpacked.patches().is_some(), | ||
| "Expected BitPacked array to have patches" | ||
| ); | ||
|
|
||
| let array = bitpacked.as_array(); | ||
|
|
||
| let metadata = array.metadata(&SESSION)?.unwrap_or_default(); | ||
| let children = array.children(); | ||
| let buffers = array | ||
| .buffers() | ||
| .into_iter() | ||
| .map(BufferHandle::new_host) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let deserialized = BitPackedPatchedPlugin.deserialize( | ||
| array.dtype(), | ||
| array.len(), | ||
| &metadata, | ||
| &buffers, | ||
| &children, | ||
| &SESSION, | ||
| )?; | ||
|
|
||
| let patched: PatchedArray = deserialized | ||
| .try_downcast() | ||
| .map_err(|a| vortex_err!("Expected Patched, got {}", a.encoding_id()))?; | ||
|
|
||
| let inner_bitpacked: BitPackedArray = patched | ||
| .base_array() | ||
| .clone() | ||
| .try_downcast() | ||
| .map_err(|a| vortex_err!("Expected inner BitPacked, got {}", a.encoding_id()))?; | ||
|
|
||
| assert!( | ||
| inner_bitpacked.patches().is_none(), | ||
| "Inner BitPacked should NOT have patches" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitpacked_without_patches_stays_bitpacked() -> VortexResult<()> { | ||
| // With bit_width=16, max value is 65535. All values 0..100 fit. | ||
| let values: Buffer<i32> = (0i32..100).collect(); | ||
| let parray = values.into_array(); | ||
| let bitpacked = BitPackedData::encode(&parray, 16)?; | ||
|
|
||
| assert!( | ||
| bitpacked.patches().is_none(), | ||
| "Expected BitPacked array without patches" | ||
| ); | ||
|
|
||
| let array = bitpacked.as_array(); | ||
|
|
||
| let metadata = array.metadata(&SESSION)?.unwrap_or_default(); | ||
| let children = array.children(); | ||
| let buffers = array | ||
| .buffers() | ||
| .into_iter() | ||
| .map(BufferHandle::new_host) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let deserialized = BitPackedPatchedPlugin.deserialize( | ||
| array.dtype(), | ||
| array.len(), | ||
| &metadata, | ||
| &buffers, | ||
| &children, | ||
| &SESSION, | ||
| )?; | ||
|
|
||
| let result = deserialized | ||
| .try_downcast::<BitPacked>() | ||
| .map_err(|a| vortex_err!("Expected deserialize BitPacked, got {}", a.encoding_id()))?; | ||
|
|
||
| assert!(result.patches().is_none(), "Result should not have patches"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn primitive_array_returns_error() -> VortexResult<()> { | ||
| let array = PrimitiveArray::from_iter([1i32, 2, 3]).into_array(); | ||
|
|
||
| let metadata = array.metadata(&SESSION)?.unwrap_or_default(); | ||
| let children = array.children(); | ||
| let buffers = array | ||
| .buffers() | ||
| .into_iter() | ||
| .map(BufferHandle::new_host) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let result = BitPackedPatchedPlugin.deserialize( | ||
| array.dtype(), | ||
| array.len(), | ||
| &metadata, | ||
| &buffers, | ||
| &children, | ||
| &SESSION, | ||
| ); | ||
|
|
||
| assert!( | ||
| result.is_err(), | ||
| "Expected error when deserializing PrimitiveArray with BitPackedPatchedPlugin" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems odd we need both?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should convert cargo feature flags to environment variables/config. It's not useful if you're using a non rust package and have to recompile the world to try out a feature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I think this is something we want as a runtime/env flag not a compile flag