You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2021/08/12 15:44:22 UTC

[arrow-rs] 01/01: Allow creation of String arrays from &Option<&str> iterators (#680)

This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch cherry_pick_1a5c26bc
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 4093240dbce1455bb1cfe5f25b94bdc5b8a32577
Author: Pete Koomen <ko...@gmail.com>
AuthorDate: Thu Aug 12 08:42:28 2021 -0700

    Allow creation of String arrays from &Option<&str> iterators (#680)
    
    * Allow creation of String arrays from &Option<&str> iterators
    
    * Add links in doc comments
    
    Co-authored-by: Jorge Leitao <jo...@gmail.com>
    
    Co-authored-by: Andrew Lamb <an...@nerdnetworks.org>
    Co-authored-by: Jorge Leitao <jo...@gmail.com>
---
 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]