alamb commented on a change in pull request #8211:
URL: https://github.com/apache/arrow/pull/8211#discussion_r497443225



##########
File path: rust/arrow/src/array/array.rs
##########
@@ -693,6 +694,61 @@ impl fmt::Debug for PrimitiveArray<BooleanType> {
     }
 }
 
+impl<'a, T: ArrowPrimitiveType> IntoIterator for &'a PrimitiveArray<T> {
+    type Item = Option<<T as ArrowPrimitiveType>::Native>;
+    type IntoIter = PrimitiveIter<'a, T>;
+
+    fn into_iter(self) -> Self::IntoIter {
+        PrimitiveIter::<'a, T>::new(self)
+    }
+}
+
+impl<'a, T: ArrowPrimitiveType> PrimitiveArray<T> {
+    /// constructs a new iterator
+    pub fn iter(&'a self) -> PrimitiveIter<'a, T> {
+        PrimitiveIter::<'a, T>::new(&self)
+    }
+}
+
+impl<T: ArrowPrimitiveType, Ptr: Borrow<Option<<T as 
ArrowPrimitiveType>::Native>>>
+    FromIterator<Ptr> for PrimitiveArray<T>
+{
+    fn from_iter<I: IntoIterator<Item = Ptr>>(iter: I) -> Self {
+        let iter = iter.into_iter();
+        let (_, data_len) = iter.size_hint();
+        let data_len = data_len.unwrap(); // panic if no upper bound.

Review comment:
       ```suggestion
           let data_len = data_len.expect("Iterator must be sized"); // panic 
if no upper bound.
   ```

##########
File path: rust/arrow/src/compute/kernels/cast.rs
##########
@@ -590,37 +590,27 @@ where
     FROM::Native: num::NumCast,
     TO::Native: num::NumCast,
 {
-    numeric_cast::<FROM, TO>(
+    Ok(Arc::new(numeric_cast::<FROM, TO>(
         from.as_any()
             .downcast_ref::<PrimitiveArray<FROM>>()
             .unwrap(),
-    )
-    .map(|to| Arc::new(to) as ArrayRef)
+    )))
 }
 
 /// Natural cast between numeric types
-fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
+fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
 where
     T: ArrowNumericType,
     R: ArrowNumericType,
     T::Native: num::NumCast,
     R::Native: num::NumCast,
 {
-    let mut b = PrimitiveBuilder::<R>::new(from.len());
-
-    for i in 0..from.len() {
-        if from.is_null(i) {
-            b.append_null()?;
-        } else {
-            // some casts return None, such as a negative value to 
u{8|16|32|64}
-            match num::cast::cast(from.value(i)) {
-                Some(v) => b.append_value(v)?,
-                None => b.append_null()?,
-            };
-        }
-    }
-
-    Ok(b.finish())
+    from.iter()

Review comment:
       this is a nice change. 




----------------------------------------------------------------
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:
[email protected]


Reply via email to