wiedld commented on code in PR #6922:
URL: https://github.com/apache/arrow-rs/pull/6922#discussion_r1902142836
##########
arrow-array/src/array/dictionary_array.rs:
##########
@@ -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.");
+ });
Review Comment:
The `Extend<Option<V>>` uses append_option. Hence the two FromIterator impls
here. 👍🏼
##########
arrow-array/src/array/dictionary_array.rs:
##########
@@ -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);
Review Comment:
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](https://github.com/apache/arrow-rs/blob/b77d38d022079b106449ead3996f373edc906327/arrow-array/src/builder/generic_bytes_dictionary_builder.rs#L457),
or
[bytes](https://github.com/apache/arrow-rs/blob/b77d38d022079b106449ead3996f373edc906327/arrow-array/src/builder/generic_bytes_dictionary_builder.rs#L495)).
Have you tried that, and do you think it's worthwhile?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]