Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 136 additions & 6 deletions arrow-array/src/array/dictionary_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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);
Comment on lines +712 to +716
Copy link
Contributor

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?

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Extend<Option<V>> uses append_option. Hence the two FromIterator impls here. 👍🏼


builder.finish()
}
}

impl<T: ArrowDictionaryKeyType> Array for DictionaryArray<T> {
fn as_any(&self) -> &dyn Any {
self
Expand Down Expand Up @@ -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()
Expand All @@ -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"];
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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()
Expand Down
Loading