This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch active_release
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/active_release by this push:
new 72f2407 Allow creation of String arrays from &Option<&str> iterators
(#680) (#686)
72f2407 is described below
commit 72f240735dc91b306689f7281e896385dc27f4c9
Author: Andrew Lamb <[email protected]>
AuthorDate: Thu Aug 12 12:43:31 2021 -0400
Allow creation of String arrays from &Option<&str> iterators (#680) (#686)
* Allow creation of String arrays from &Option<&str> iterators
* Add links in doc comments
Co-authored-by: Jorge Leitao <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
Co-authored-by: Jorge Leitao <[email protected]>
Co-authored-by: Pete Koomen <[email protected]>
Co-authored-by: Jorge Leitao <[email protected]>
---
arrow/src/array/array_string.rs | 32 +++++++++++++++++++++++++++-----
1 file changed, 27 insertions(+), 5 deletions(-)
diff --git a/arrow/src/array/array_string.rs b/arrow/src/array/array_string.rs
index 2fa4c48..d72dbb2 100644
--- a/arrow/src/array/array_string.rs
+++ b/arrow/src/array/array_string.rs
@@ -196,11 +196,26 @@ impl<OffsetSize: StringOffsetSizeTrait>
GenericStringArray<OffsetSize> {
}
}
+impl<'a, Ptr, OffsetSize: StringOffsetSizeTrait> FromIterator<&'a Option<Ptr>>
+ for GenericStringArray<OffsetSize>
+where
+ Ptr: AsRef<str> + 'a,
+{
+ /// Creates a [`GenericStringArray`] based on an iterator of `Option`
references.
+ fn from_iter<I: IntoIterator<Item = &'a Option<Ptr>>>(iter: I) -> Self {
+ // Convert each owned Ptr into &str and wrap in an owned `Option`
+ let iter = iter.into_iter().map(|o| o.as_ref().map(|p| p.as_ref()));
+ // Build a `GenericStringArray` with the resulting iterator
+ iter.collect::<GenericStringArray<OffsetSize>>()
+ }
+}
+
impl<'a, Ptr, OffsetSize: StringOffsetSizeTrait> FromIterator<Option<Ptr>>
for GenericStringArray<OffsetSize>
where
Ptr: AsRef<str>,
{
+ /// Creates a [`GenericStringArray`] based on an iterator of `Option`s
fn from_iter<I: IntoIterator<Item = Option<Ptr>>>(iter: I) -> Self {
let iter = iter.into_iter();
let (_, data_len) = iter.size_hint();
@@ -358,6 +373,7 @@ impl<T: StringOffsetSizeTrait> From<GenericListArray<T>>
for GenericStringArray<
#[cfg(test)]
mod tests {
+
use crate::array::{ListBuilder, StringBuilder};
use super::*;
@@ -484,17 +500,23 @@ mod tests {
#[test]
fn test_string_array_from_iter() {
- let data = vec![Some("hello"), None, Some("arrow")];
+ let data = [Some("hello"), None, Some("arrow")];
+ let data_vec = data.to_vec();
// from Vec<Option<&str>>
- let array1 = StringArray::from(data.clone());
+ let array1 = StringArray::from(data_vec.clone());
// from Iterator<Option<&str>>
- let array2: StringArray = data.clone().into_iter().collect();
+ let array2: StringArray = data_vec.clone().into_iter().collect();
// from Iterator<Option<String>>
- let array3: StringArray =
- data.into_iter().map(|x| x.map(|s| s.to_string())).collect();
+ let array3: StringArray = data_vec
+ .into_iter()
+ .map(|x| x.map(|s| s.to_string()))
+ .collect();
+ // from Iterator<&Option<&str>>
+ let array4: StringArray = data.iter().collect::<StringArray>();
assert_eq!(array1, array2);
assert_eq!(array2, array3);
+ assert_eq!(array3, array4);
}
#[test]