-
Notifications
You must be signed in to change notification settings - Fork 926
Implement Extend for ArrayBuilder (#1841) #3563
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
tustvold
merged 3 commits into
apache:master
from
tustvold:implement-extend-for-builders
Jan 20, 2023
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 |
---|---|---|
|
@@ -214,7 +214,7 @@ where | |
K: ArrowDictionaryKeyType, | ||
T: ByteArrayType, | ||
{ | ||
/// Append a primitive value to the array. Return an existing index | ||
/// Append a value to the array. Return an existing index | ||
/// if already present in the values array or a new index if the | ||
/// value is appended to the values array. | ||
/// | ||
|
@@ -255,12 +255,34 @@ where | |
Ok(key) | ||
} | ||
|
||
/// Infallibly append a value to this builder | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the resulting length of the dictionary values array would exceed `T::Native::MAX` | ||
pub fn append_value(&mut self, value: impl AsRef<T::Native>) { | ||
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. These are the new APIs for #3562 |
||
self.append(value).expect("dictionary key overflow"); | ||
} | ||
|
||
/// Appends a null slot into the builder | ||
#[inline] | ||
pub fn append_null(&mut self) { | ||
self.keys_builder.append_null() | ||
} | ||
|
||
/// Append an `Option` value into the builder | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the resulting length of the dictionary values array would exceed `T::Native::MAX` | ||
#[inline] | ||
pub fn append_option(&mut self, value: Option<impl AsRef<T::Native>>) { | ||
match value { | ||
None => self.append_null(), | ||
Some(v) => self.append_value(v), | ||
}; | ||
} | ||
|
||
/// Builds the `DictionaryArray` and reset this builder. | ||
pub fn finish(&mut self) -> DictionaryArray<K> { | ||
self.dedup.clear(); | ||
|
@@ -297,6 +319,17 @@ where | |
} | ||
} | ||
|
||
impl<K: ArrowDictionaryKeyType, T: ByteArrayType, V: AsRef<T::Native>> Extend<Option<V>> | ||
for GenericByteDictionaryBuilder<K, T> | ||
{ | ||
#[inline] | ||
fn extend<I: IntoIterator<Item = Option<V>>>(&mut self, iter: I) { | ||
for v in iter { | ||
self.append_option(v) | ||
} | ||
} | ||
} | ||
|
||
fn get_bytes<'a, K: ArrowNativeType, T: ByteArrayType>( | ||
values: &'a GenericByteBuilder<T>, | ||
key: &K, | ||
|
@@ -405,7 +438,7 @@ mod tests { | |
|
||
use crate::array::Array; | ||
use crate::array::Int8Array; | ||
use crate::types::{Int16Type, Int8Type}; | ||
use crate::types::{Int16Type, Int32Type, Int8Type, Utf8Type}; | ||
use crate::{BinaryArray, StringArray}; | ||
|
||
fn test_bytes_dictionary_builder<T>(values: Vec<&T::Native>) | ||
|
@@ -622,4 +655,14 @@ mod tests { | |
vec![b"abc", b"def"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_extend() { | ||
let mut builder = GenericByteDictionaryBuilder::<Int32Type, Utf8Type>::new(); | ||
builder.extend(["a", "b", "c", "a", "b", "c"].into_iter().map(Some)); | ||
builder.extend(["c", "d", "a"].into_iter().map(Some)); | ||
let dict = builder.finish(); | ||
assert_eq!(dict.keys().values(), &[0, 1, 2, 0, 1, 2, 2, 3, 0]); | ||
assert_eq!(dict.values().len(), 4); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -111,6 +111,10 @@ where | |
} | ||
|
||
/// Finish the current variable-length list array slot | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the length of [`Self::values`] exceeds `OffsetSize::MAX` | ||
#[inline] | ||
pub fn append(&mut self, is_valid: bool) { | ||
self.offsets_builder | ||
|
@@ -178,10 +182,32 @@ where | |
} | ||
} | ||
|
||
impl<O, B, V, E> Extend<Option<V>> for GenericListBuilder<O, B> | ||
where | ||
O: OffsetSizeTrait, | ||
B: ArrayBuilder + Extend<E>, | ||
V: IntoIterator<Item = E>, | ||
{ | ||
#[inline] | ||
fn extend<T: IntoIterator<Item = Option<V>>>(&mut self, iter: T) { | ||
for v in iter { | ||
match v { | ||
Some(elements) => { | ||
self.values_builder.extend(elements); | ||
self.append(true); | ||
} | ||
None => self.append(false), | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::builder::{Int32Builder, ListBuilder}; | ||
use crate::cast::as_primitive_array; | ||
use crate::types::Int32Type; | ||
use crate::{Array, Int32Array}; | ||
use arrow_buffer::Buffer; | ||
use arrow_schema::DataType; | ||
|
@@ -364,4 +390,25 @@ mod tests { | |
list_array.values().data().child_data()[0].buffers()[0].clone() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_extend() { | ||
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. 👍 thank you for this test |
||
let mut builder = ListBuilder::new(Int32Builder::new()); | ||
builder.extend([ | ||
Some(vec![Some(1), Some(2), Some(7), None]), | ||
Some(vec![]), | ||
Some(vec![Some(4), Some(5)]), | ||
None, | ||
]); | ||
|
||
let array = builder.finish(); | ||
assert_eq!(array.value_offsets(), [0, 4, 4, 6, 6]); | ||
assert_eq!(array.null_count(), 1); | ||
assert!(array.is_null(3)); | ||
let a_values = array.values(); | ||
let elements = as_primitive_array::<Int32Type>(a_values.as_ref()); | ||
assert_eq!(elements.values(), &[1, 2, 7, 0, 4, 5]); | ||
assert_eq!(elements.null_count(), 1); | ||
assert!(elements.is_null(3)); | ||
} | ||
} |
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
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.
that is certainly nicer