alamb commented on a change in pull request #9388: URL: https://github.com/apache/arrow/pull/9388#discussion_r579653792
########## File path: rust/arrow/src/array/array_list.rs ########## @@ -116,14 +116,68 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> { pub fn iter<'a>(&'a self) -> GenericListArrayIter<'a, OffsetSize> { GenericListArrayIter::<'a, OffsetSize>::new(&self) } -} - -impl<'a, S: OffsetSizeTrait> IntoIterator for &'a GenericListArray<S> { - type Item = Option<ArrayRef>; - type IntoIter = GenericListArrayIter<'a, S>; - fn into_iter(self) -> Self::IntoIter { - GenericListArrayIter::<'a, S>::new(self) + /// Creates a [`GenericListArray`] from an iterator of primitive values + /// # Example + /// ``` + /// # use arrow::array::ListArray; + /// # use arrow::datatypes::Int32Type; + /// let data = vec![ + /// Some(vec![Some(0), Some(1), Some(2)]), + /// None, + /// Some(vec![Some(3), None, Some(5)]), + /// Some(vec![Some(6), Some(7)]), + /// ]; + /// let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(data); + /// println!("{:?}", list_array); + /// ``` + pub fn from_iter_primitive<T, P, I>(iter: I) -> Self + where + T: ArrowPrimitiveType, + P: AsRef<[Option<<T as ArrowPrimitiveType>::Native>]> + + IntoIterator<Item = Option<<T as ArrowPrimitiveType>::Native>>, + I: IntoIterator<Item = Option<P>>, + { + let iterator = iter.into_iter(); + let (lower, _) = iterator.size_hint(); + + let mut offsets = + MutableBuffer::new((lower + 1) * std::mem::size_of::<OffsetSize>()); + let mut length_so_far = OffsetSize::zero(); + offsets.push(length_so_far); Review comment: Won't this result in a single element list if the input iterator is empty? However, I tried it locally with ``` #[test] fn test_from_iter_primitive_empty() { let data = vec![ ] as Vec<Option<Vec<Option<i32>>>>; let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(data); assert_eq!(list_array.len(), 0) } ``` And that test passed. So I guess 👍 Perhaps it is because the null buffer's length is used to set the length of the array (even though `offsets` has len of 1) ---------------------------------------------------------------- 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: us...@infra.apache.org