You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/01/23 12:46:55 UTC

[GitHub] [arrow] jorgecarleitao commented on a change in pull request #9293: ARROW-11349: [Rust] Add from_iter_values to create arrays from (non null) values

jorgecarleitao commented on a change in pull request #9293:
URL: https://github.com/apache/arrow/pull/9293#discussion_r563148896



##########
File path: rust/arrow/src/array/array_primitive.rs
##########
@@ -94,6 +94,32 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
         let offset = i + self.offset();
         unsafe { *self.raw_values.as_ptr().add(offset) }
     }
+
+    /// Creates a PrimitiveArray based on an iterator of values without nulls
+    pub fn from_iter_values<I: IntoIterator<Item = T::Native>>(iter: I) -> Self {
+        let iter = iter.into_iter();
+        let (_, data_len) = iter.size_hint();
+        let data_len = data_len.expect("Iterator must be sized"); // panic if no upper bound.
+
+        let mut val_buf = MutableBuffer::new(
+            data_len * mem::size_of::<<T as ArrowPrimitiveType>::Native>(),
+        );
+
+        iter.for_each(|item| {
+            val_buf.push(item);
+        });

Review comment:
       Yes, that is what I meant: it should now be possible to do
   
   ```rust
   let values = vec![0i32, 1];
   
   let values = values.iter().map(|x| x + 1);
   
   let buffer: Buffer: values.collect();
   ```
   
   wrt to the `len`: that is a good point that I have had not thought about. 👍 
   
   One option is to do what you wrote. Another could be (I haven't tried to compile this, as `Fn` could become `FnMut`):
   
   ```
   let mut count = 0;
   let iter = iter.map(|x| {
      count += 1;
   x
   });
   ```
   
   I would probably have taken your idea, though :)
   




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org