-
Notifications
You must be signed in to change notification settings - Fork 118
fix: canonicalize VarBin/FSST with >2GB buffers #5961
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
+146
−59
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| use itertools::Itertools; | ||
| use num_traits::AsPrimitive; | ||
| use vortex_buffer::Buffer; | ||
| use vortex_buffer::BufferMut; | ||
| use vortex_buffer::ByteBuffer; | ||
| use vortex_buffer::ByteBufferMut; | ||
| use vortex_dtype::NativePType; | ||
| use vortex_vector::binaryview::BinaryView; | ||
|
|
||
| /// Convert an offsets buffer to a buffer of element lengths. | ||
| #[inline] | ||
| pub fn offsets_to_lengths<P: NativePType>(offsets: &[P]) -> Buffer<P> { | ||
| offsets | ||
| .iter() | ||
| .tuple_windows::<(_, _)>() | ||
| .map(|(&start, &end)| end - start) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Maximum number of buffer bytes that can be referenced by a single `BinaryView` | ||
| pub const MAX_BUFFER_LEN: usize = i32::MAX as usize; | ||
|
|
||
| /// Split a large buffer of input `bytes` holding string data | ||
| pub fn build_views<P: NativePType + AsPrimitive<usize>>( | ||
| start_buf_index: u32, | ||
| max_buffer_len: usize, | ||
| mut bytes: ByteBufferMut, | ||
| lens: &[P], | ||
| ) -> (Vec<ByteBuffer>, Buffer<BinaryView>) { | ||
| let mut views = BufferMut::<BinaryView>::with_capacity(lens.len()); | ||
|
|
||
| let mut buffers = Vec::new(); | ||
| let mut buf_index = start_buf_index; | ||
|
|
||
| let mut offset = 0; | ||
| for &len in lens { | ||
| let len = len.as_(); | ||
| assert!(len <= max_buffer_len, "values cannot exceed max_buffer_len"); | ||
|
|
||
| if (offset + len) > max_buffer_len { | ||
| // Roll the buffer every 2GiB, to avoid overflowing VarBinView offset field | ||
| let rest = bytes.split_off(offset); | ||
|
|
||
| buffers.push(bytes.freeze()); | ||
| buf_index += 1; | ||
| offset = 0; | ||
|
|
||
| bytes = rest; | ||
| } | ||
| let view = BinaryView::make_view(&bytes[offset..][..len], buf_index, offset.as_()); | ||
| // SAFETY: we reserved the right capacity beforehand | ||
| unsafe { views.push_unchecked(view) }; | ||
| offset += len; | ||
| } | ||
|
|
||
| if !bytes.is_empty() { | ||
| buffers.push(bytes.freeze()); | ||
| } | ||
|
|
||
| (buffers, views.freeze()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use vortex_buffer::ByteBuffer; | ||
| use vortex_buffer::ByteBufferMut; | ||
| use vortex_vector::binaryview::BinaryView; | ||
|
|
||
| use crate::arrays::build_views::build_views; | ||
|
|
||
| #[test] | ||
| fn test_to_canonical_large() { | ||
| // We are testing generating views for raw data that should look like | ||
| // | ||
| // aaaaaaaaaaaaa ("a"*13) | ||
| // bbbbbbbbbbbbb ("b"*13) | ||
| // ccccccccccccc ("c"*13) | ||
| // ddddddddddddd ("d"*13) | ||
| // | ||
| // In real code, this would all fit in one buffer, but to unit test the splitting logic | ||
| // we split buffers at length 26, which should result in two buffers for the output array. | ||
| let raw_data = | ||
| ByteBufferMut::copy_from("aaaaaaaaaaaaabbbbbbbbbbbbbcccccccccccccddddddddddddd"); | ||
| let lens = vec![13u8; 4]; | ||
|
|
||
| let (buffers, views) = build_views(0, 26, raw_data, &lens); | ||
|
|
||
| assert_eq!( | ||
| buffers, | ||
| vec![ | ||
| ByteBuffer::copy_from("aaaaaaaaaaaaabbbbbbbbbbbbb"), | ||
| ByteBuffer::copy_from("cccccccccccccddddddddddddd"), | ||
| ] | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| views.as_slice(), | ||
| &[ | ||
| BinaryView::make_view(b"aaaaaaaaaaaaa", 0, 0), | ||
| BinaryView::make_view(b"bbbbbbbbbbbbb", 0, 13), | ||
| BinaryView::make_view(b"ccccccccccccc", 1, 0), | ||
| BinaryView::make_view(b"ddddddddddddd", 1, 13), | ||
| ] | ||
| ) | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -13,5 +13,6 @@ mod compute; | |
| mod vtable; | ||
| pub use vtable::VarBinViewVTable; | ||
|
|
||
| pub mod build_views; | ||
| #[cfg(test)] | ||
| mod tests; | ||
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.
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.
is this the test that takes a while to run?
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.
no this one is very small b/c i artificially constrain the max buffer len
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.
but now that we're hitting the same codepath for varbin/fsst it's ok