-
Notifications
You must be signed in to change notification settings - Fork 931
Deduplicate strings/binarys when building view types #6005
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
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ use std::sync::Arc; | |
use arrow_buffer::{Buffer, BufferBuilder, NullBufferBuilder, ScalarBuffer}; | ||
use arrow_data::ByteView; | ||
use arrow_schema::ArrowError; | ||
use hashbrown::hash_table::Entry; | ||
use hashbrown::HashTable; | ||
|
||
use crate::builder::ArrayBuilder; | ||
use crate::types::bytes::ByteArrayNativeType; | ||
|
@@ -57,6 +59,9 @@ pub struct GenericByteViewBuilder<T: ByteViewType + ?Sized> { | |
completed: Vec<Buffer>, | ||
in_progress: Vec<u8>, | ||
block_size: u32, | ||
/// Some if deduplicating strings | ||
/// map `<string hash> -> <index to the views>` | ||
string_tracker: Option<(HashTable<usize>, ahash::RandomState)>, | ||
phantom: PhantomData<T>, | ||
} | ||
|
||
|
@@ -74,6 +79,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewBuilder<T> { | |
completed: vec![], | ||
in_progress: vec![], | ||
block_size: DEFAULT_BLOCK_SIZE, | ||
string_tracker: None, | ||
phantom: Default::default(), | ||
} | ||
} | ||
|
@@ -83,6 +89,20 @@ impl<T: ByteViewType + ?Sized> GenericByteViewBuilder<T> { | |
Self { block_size, ..self } | ||
} | ||
|
||
/// Deduplicate strings while building the array | ||
/// | ||
/// This will potentially decrease the memory usage if the array have repeated strings | ||
/// It will also increase the time to build the array as it needs to hash the strings | ||
pub fn with_deduplicate_strings(self) -> Self { | ||
Self { | ||
string_tracker: Some(( | ||
HashTable::with_capacity(self.views_builder.capacity()), | ||
Default::default(), | ||
)), | ||
..self | ||
} | ||
} | ||
|
||
/// Append a new data block returning the new block offset | ||
/// | ||
/// Note: this will first flush any in-progress block | ||
|
@@ -179,6 +199,26 @@ impl<T: ByteViewType + ?Sized> GenericByteViewBuilder<T> { | |
self.completed.push(block); | ||
} | ||
|
||
/// Returns the value at the given index | ||
/// Useful if we want to know what value has been inserted to the builder | ||
fn get_value(&self, index: usize) -> &[u8] { | ||
let view = self.views_builder.as_slice().get(index).unwrap(); | ||
let len = *view as u32; | ||
if len <= 12 { | ||
// # Safety | ||
// The view is valid from the builder | ||
unsafe { GenericByteViewArray::<T>::inline_value(view, len as usize) } | ||
} else { | ||
let view = ByteView::from(*view); | ||
if view.buffer_index < self.completed.len() as u32 { | ||
let block = &self.completed[view.buffer_index as usize]; | ||
&block[view.offset as usize..view.offset as usize + view.length as usize] | ||
} else { | ||
&self.in_progress[view.offset as usize..view.offset as usize + view.length as usize] | ||
} | ||
} | ||
} | ||
|
||
/// Appends a value into the builder | ||
/// | ||
/// # Panics | ||
|
@@ -199,6 +239,40 @@ impl<T: ByteViewType + ?Sized> GenericByteViewBuilder<T> { | |
return; | ||
} | ||
|
||
// Deduplication if: | ||
// (1) deduplication is enabled. | ||
// (2) len > 12 | ||
if let Some((mut ht, hasher)) = self.string_tracker.take() { | ||
let hash_val = hasher.hash_one(v); | ||
let hasher_fn = |v: &_| hasher.hash_one(v); | ||
|
||
let entry = ht.entry( | ||
hash_val, | ||
|idx| { | ||
let stored_value = self.get_value(*idx); | ||
v == stored_value | ||
}, | ||
hasher_fn, | ||
); | ||
match entry { | ||
Entry::Occupied(occupied) => { | ||
// If the string already exists, we will directly use the view | ||
let idx = occupied.get(); | ||
self.views_builder | ||
.append(self.views_builder.as_slice()[*idx]); | ||
self.null_buffer_builder.append_non_null(); | ||
self.string_tracker = Some((ht, hasher)); | ||
return; | ||
} | ||
Entry::Vacant(vacant) => { | ||
// o.w. we insert the (string hash -> view index) | ||
// the idx is current length of views_builder, as we are inserting a new view | ||
vacant.insert(self.views_builder.len()); | ||
} | ||
} | ||
self.string_tracker = Some((ht, hasher)); | ||
} | ||
|
||
let required_cap = self.in_progress.len() + v.len(); | ||
if self.in_progress.capacity() < required_cap { | ||
self.flush_in_progress(); | ||
|
@@ -357,6 +431,42 @@ mod tests { | |
use super::*; | ||
use crate::Array; | ||
|
||
#[test] | ||
fn test_string_view_deduplicate() { | ||
XiangpengHao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let value_1 = "long string to test string view"; | ||
let value_2 = "not so similar string but long"; | ||
|
||
let mut builder = StringViewBuilder::new() | ||
.with_deduplicate_strings() | ||
.with_block_size(value_1.len() as u32 * 2); // so that we will have multiple buffers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
|
||
let values = vec![ | ||
Some(value_1), | ||
Some(value_2), | ||
Some("short"), | ||
Some(value_1), | ||
None, | ||
Some(value_2), | ||
Some(value_1), | ||
]; | ||
builder.extend(values.clone()); | ||
|
||
let array = builder.finish_cloned(); | ||
array.to_data().validate_full().unwrap(); | ||
assert_eq!(array.data_buffers().len(), 1); // without duplication we would need 3 buffers. | ||
let actual: Vec<_> = array.iter().collect(); | ||
assert_eq!(actual, values); | ||
|
||
let view0 = array.views().first().unwrap(); | ||
let view3 = array.views().get(3).unwrap(); | ||
let view6 = array.views().get(6).unwrap(); | ||
|
||
assert_eq!(view0, view3); | ||
assert_eq!(view0, view6); | ||
|
||
assert_eq!(array.views().get(1), array.views().get(5)); | ||
} | ||
|
||
#[test] | ||
fn test_string_view() { | ||
let b1 = Buffer::from(b"world\xFFbananas\xF0\x9F\x98\x81"); | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.