-
Notifications
You must be signed in to change notification settings - Fork 847
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
feat(arrow-array): add from_iter
for DictionaryArray
for building BinaryDictionaryArray
#6922
Draft
rluvaton
wants to merge
1
commit into
apache:main
Choose a base branch
from
rluvaton:add-from-iter-for-binary-dictionary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -15,7 +15,9 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::builder::{PrimitiveDictionaryBuilder, StringDictionaryBuilder}; | ||
use crate::builder::{ | ||
BinaryDictionaryBuilder, PrimitiveDictionaryBuilder, StringDictionaryBuilder, | ||
}; | ||
use crate::cast::AsArray; | ||
use crate::iterator::ArrayIter; | ||
use crate::types::*; | ||
|
@@ -691,6 +693,61 @@ impl<'a, T: ArrowDictionaryKeyType> FromIterator<&'a str> for DictionaryArray<T> | |
} | ||
} | ||
|
||
/// Constructs a `DictionaryArray` from an iterator of optional bytes. | ||
/// | ||
/// # Example: | ||
/// ``` | ||
/// use arrow_array::{DictionaryArray, PrimitiveArray, BinaryArray, types::Int8Type}; | ||
/// | ||
/// let test: Vec<&[u8]> = vec![&[1, 3], &[4], &[2, 7], &[5, 6]]; | ||
/// let array: DictionaryArray<Int8Type> = test | ||
/// .iter() | ||
/// .map(|&x| if x == &[2, 7] { None } else { Some(x) }) | ||
/// .collect(); | ||
/// assert_eq!( | ||
/// "DictionaryArray {keys: PrimitiveArray<Int8>\n[\n 0,\n 1,\n null,\n 2,\n] values: BinaryArray\n[\n [1, 3],\n [4],\n [5, 6],\n]}\n", | ||
/// format!("{:?}", array) | ||
/// ); | ||
/// ``` | ||
impl<'a, T: ArrowDictionaryKeyType> FromIterator<Option<&'a [u8]>> for DictionaryArray<T> { | ||
fn from_iter<I: IntoIterator<Item = Option<&'a [u8]>>>(iter: I) -> Self { | ||
let it = iter.into_iter(); | ||
let (lower, _) = it.size_hint(); | ||
let mut builder = BinaryDictionaryBuilder::with_capacity(lower, 256, 1024); | ||
builder.extend(it); | ||
builder.finish() | ||
} | ||
} | ||
|
||
/// Constructs a `DictionaryArray` from an iterator of bytes. | ||
/// | ||
/// # Example: | ||
/// | ||
/// ``` | ||
/// use arrow_array::{DictionaryArray, PrimitiveArray, BinaryArray, types::Int8Type}; | ||
/// | ||
/// let test: Vec<&[u8]> = vec![&[1, 3], &[4], &[2, 7], &[5, 6]]; | ||
/// let array: DictionaryArray<Int8Type> = test.into_iter().collect(); | ||
/// assert_eq!( | ||
/// "DictionaryArray {keys: PrimitiveArray<Int8>\n[\n 0,\n 1,\n 2,\n 3,\n] values: BinaryArray\n[\n [1, 3],\n [4],\n [2, 7],\n [5, 6],\n]}\n", | ||
/// format!("{:?}", array) | ||
/// ); | ||
/// ``` | ||
impl<'a, T: ArrowDictionaryKeyType> FromIterator<&'a [u8]> for DictionaryArray<T> { | ||
fn from_iter<I: IntoIterator<Item = &'a [u8]>>(iter: I) -> Self { | ||
let it = iter.into_iter(); | ||
let (lower, _) = it.size_hint(); | ||
let mut builder = BinaryDictionaryBuilder::with_capacity(lower, 256, 1024); | ||
it.for_each(|i| { | ||
builder | ||
.append(i) | ||
.expect("Unable to append a value to a dictionary array."); | ||
}); | ||
Comment on lines
+741
to
+745
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. The |
||
|
||
builder.finish() | ||
} | ||
} | ||
|
||
impl<T: ArrowDictionaryKeyType> Array for DictionaryArray<T> { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
|
@@ -1193,7 +1250,7 @@ mod tests { | |
} | ||
|
||
#[test] | ||
fn test_dictionary_array_from_iter() { | ||
fn test_string_dictionary_array_from_iter() { | ||
let test = vec!["a", "a", "b", "c"]; | ||
let array: DictionaryArray<Int8Type> = test | ||
.iter() | ||
|
@@ -1211,6 +1268,25 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
fn test_binary_dictionary_array_from_iter() { | ||
let test: Vec<&[u8]> = vec![&[1, 3], &[4], &[2, 7], &[5, 6]]; | ||
let array: DictionaryArray<Int8Type> = test | ||
.iter() | ||
.map(|&x| if x == [2, 7] { None } else { Some(x) }) | ||
.collect(); | ||
assert_eq!( | ||
"DictionaryArray {keys: PrimitiveArray<Int8>\n[\n 0,\n 1,\n null,\n 2,\n] values: BinaryArray\n[\n [1, 3],\n [4],\n [5, 6],\n]}\n", | ||
format!("{array:?}") | ||
); | ||
|
||
let array: DictionaryArray<Int8Type> = test.into_iter().collect(); | ||
assert_eq!( | ||
"DictionaryArray {keys: PrimitiveArray<Int8>\n[\n 0,\n 1,\n 2,\n 3,\n] values: BinaryArray\n[\n [1, 3],\n [4],\n [2, 7],\n [5, 6],\n]}\n", | ||
format!("{array:?}") | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_dictionary_array_reverse_lookup_key() { | ||
let test = vec!["a", "a", "b", "c"]; | ||
|
@@ -1227,7 +1303,7 @@ mod tests { | |
} | ||
|
||
#[test] | ||
fn test_dictionary_keys_as_primitive_array() { | ||
fn test_string_dictionary_keys_as_primitive_array() { | ||
let test = vec!["a", "b", "c", "a"]; | ||
let array: DictionaryArray<Int8Type> = test.into_iter().collect(); | ||
|
||
|
@@ -1238,7 +1314,23 @@ mod tests { | |
} | ||
|
||
#[test] | ||
fn test_dictionary_keys_as_primitive_array_with_null() { | ||
fn test_binary_dictionary_keys_as_primitive_array() { | ||
let test = vec![ | ||
"a".as_bytes(), | ||
"b".as_bytes(), | ||
"c".as_bytes(), | ||
"a".as_bytes(), | ||
]; | ||
let array: DictionaryArray<Int8Type> = test.into_iter().collect(); | ||
|
||
let keys = array.keys(); | ||
assert_eq!(&DataType::Int8, keys.data_type()); | ||
assert_eq!(0, keys.null_count()); | ||
assert_eq!(&[0, 1, 2, 0], keys.values()); | ||
} | ||
|
||
#[test] | ||
fn test_string_dictionary_keys_as_primitive_array_with_null() { | ||
let test = vec![Some("a"), None, Some("b"), None, None, Some("a")]; | ||
let array: DictionaryArray<Int32Type> = test.into_iter().collect(); | ||
|
||
|
@@ -1259,8 +1351,46 @@ mod tests { | |
} | ||
|
||
#[test] | ||
fn test_dictionary_all_nulls() { | ||
let test = vec![None, None, None]; | ||
fn test_binary_dictionary_keys_as_primitive_array_with_null() { | ||
let test = vec![ | ||
Some("a".as_bytes()), | ||
None, | ||
Some("b".as_bytes()), | ||
None, | ||
None, | ||
Some("a".as_bytes()), | ||
]; | ||
let array: DictionaryArray<Int32Type> = test.into_iter().collect(); | ||
|
||
let keys = array.keys(); | ||
assert_eq!(&DataType::Int32, keys.data_type()); | ||
assert_eq!(3, keys.null_count()); | ||
|
||
assert!(keys.is_valid(0)); | ||
assert!(!keys.is_valid(1)); | ||
assert!(keys.is_valid(2)); | ||
assert!(!keys.is_valid(3)); | ||
assert!(!keys.is_valid(4)); | ||
assert!(keys.is_valid(5)); | ||
|
||
assert_eq!(0, keys.value(0)); | ||
assert_eq!(1, keys.value(2)); | ||
assert_eq!(0, keys.value(5)); | ||
} | ||
|
||
#[test] | ||
fn test_dictionary_all_string_nulls() { | ||
let test: Vec<Option<&str>> = vec![None, None, None]; | ||
let array: DictionaryArray<Int32Type> = test.into_iter().collect(); | ||
array | ||
.into_data() | ||
.validate_full() | ||
.expect("All null array has valid array data"); | ||
} | ||
|
||
#[test] | ||
fn test_dictionary_all_binary_nulls() { | ||
let test: Vec<Option<&[u8]>> = vec![None, None, None]; | ||
let array: DictionaryArray<Int32Type> = test.into_iter().collect(); | ||
array | ||
.into_data() | ||
|
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.
There is another
FromIterator<Option<&'a str>> for DictionaryArray<T>
for strings, which at first glance looks possible to combine. (Both use the GenericByteDictionaryBuilder with either strings, or bytes). Have you tried that, and do you think it's worthwhile?