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/02/01 17:39:14 UTC
[GitHub] [arrow] jorgecarleitao opened a new pull request #9388: [Rust] Added API to build ListArray of Primitives from an iterator
jorgecarleitao opened a new pull request #9388:
URL: https://github.com/apache/arrow/pull/9388
This PR enables creating a `ListArray` of primitive types out of a an iterator of options, as follows:
```rust
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);
```
which simplifies some code around building these, as well enabling kernels that operate on `ListArrays` (which are currently a bit painful to write).
Note that this is built on top of #9370 , as this requires the `PrimitiveArray::from_iter` to be growable. We can wait for that to be reviewed.
----------------------------------------------------------------
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
[GitHub] [arrow] alamb commented on a change in pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #9388:
URL: https://github.com/apache/arrow/pull/9388#discussion_r579660483
##########
File path: rust/arrow/src/compute/kernels/concat.rs
##########
@@ -223,66 +223,36 @@ mod tests {
#[test]
fn test_concat_primitive_list_arrays() -> Result<()> {
- fn populate_list1(
- b: &mut ListBuilder<PrimitiveBuilder<Int64Type>>,
- ) -> Result<()> {
- b.values().append_value(-1)?;
- b.values().append_value(-1)?;
- b.values().append_value(2)?;
- b.values().append_null()?;
- b.values().append_null()?;
- b.append(true)?;
- b.append(true)?;
- b.append(false)?;
- b.values().append_value(10)?;
- b.append(true)?;
- Ok(())
- }
-
- fn populate_list2(
- b: &mut ListBuilder<PrimitiveBuilder<Int64Type>>,
- ) -> Result<()> {
- b.append(false)?;
- b.values().append_value(100)?;
- b.values().append_null()?;
- b.values().append_value(101)?;
- b.append(true)?;
- b.values().append_value(102)?;
- b.append(true)?;
- Ok(())
- }
-
- fn populate_list3(
- b: &mut ListBuilder<PrimitiveBuilder<Int64Type>>,
- ) -> Result<()> {
- b.values().append_value(1000)?;
- b.values().append_value(1001)?;
- b.append(true)?;
- Ok(())
- }
-
- let mut builder_in1 = ListBuilder::new(PrimitiveArray::<Int64Type>::builder(0));
- let mut builder_in2 = ListBuilder::new(PrimitiveArray::<Int64Type>::builder(0));
- let mut builder_in3 = ListBuilder::new(PrimitiveArray::<Int64Type>::builder(0));
- populate_list1(&mut builder_in1)?;
- populate_list2(&mut builder_in2)?;
- populate_list3(&mut builder_in3)?;
-
- let mut builder_expected =
- ListBuilder::new(PrimitiveArray::<Int64Type>::builder(0));
- populate_list1(&mut builder_expected)?;
- populate_list2(&mut builder_expected)?;
- populate_list3(&mut builder_expected)?;
-
- let array_result = concat(&[
- &builder_in1.finish(),
- &builder_in2.finish(),
- &builder_in3.finish(),
- ])?;
+ let list1 = vec![
+ Some(vec![Some(-1), Some(-1), Some(2), None, None]),
Review comment:
This is much nicer, BTW
----------------------------------------------------------------
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
[GitHub] [arrow] jorgecarleitao commented on a change in pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #9388:
URL: https://github.com/apache/arrow/pull/9388#discussion_r579655167
##########
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:
Variable-length arrays are stored as follows: a `values` buffer and an `offset` buffer. Consider the `values`:
```
[1, 3, 5, 7, 9, 11]
```
When the offset buffer is `[0, 6]`, the array has 1 item:
```
[
[1, 3, 5, 7, 9, 11]
]
```
when the offset buffer is `[0, 5, 6]`, the array has 2 items,
```
[
[1, 3, 5, 7, 9] # i.e [0,5]
[11] # i.e. [5,6]
]
```
In other words, the offset buffer "splits" the values in contiguous chunks. The following are invariants:
1. `offset.len() > 0`
2. `array.len() == offsets.len() - 1`
3. `offsets[i+1] >= offsets[i]` for all `i` in bounds
4. `offsets[array.len()] == values.len()`
If any of these do not hold, the code either panics (case 1 and 3), or results in out of bound accesses (case 2 and 4).
----------------------------------------------------------------
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
[GitHub] [arrow] alamb closed pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
alamb closed pull request #9388:
URL: https://github.com/apache/arrow/pull/9388
----------------------------------------------------------------
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
[GitHub] [arrow] alamb commented on pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #9388:
URL: https://github.com/apache/arrow/pull/9388#issuecomment-782669680
I am rerunning the tests on this branch after merging to latest master to ensure all is well. Then I will merge
----------------------------------------------------------------
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
[GitHub] [arrow] github-actions[bot] commented on pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #9388:
URL: https://github.com/apache/arrow/pull/9388#issuecomment-771046020
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
Thanks for opening a pull request!
Could you open an issue for this pull request on JIRA?
https://issues.apache.org/jira/browse/ARROW
Then could you also rename pull request title in the following format?
ARROW-${JIRA_ID}: [${COMPONENT}] ${SUMMARY}
See also:
* [Other pull requests](https://github.com/apache/arrow/pulls/)
* [Contribution Guidelines - How to contribute patches](https://arrow.apache.org/docs/developers/contributing.html#how-to-contribute-patches)
----------------------------------------------------------------
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
[GitHub] [arrow] codecov-io commented on pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
codecov-io commented on pull request #9388:
URL: https://github.com/apache/arrow/pull/9388#issuecomment-779584348
# [Codecov](https://codecov.io/gh/apache/arrow/pull/9388?src=pr&el=h1) Report
> Merging [#9388](https://codecov.io/gh/apache/arrow/pull/9388?src=pr&el=desc) (9cdbdbb) into [master](https://codecov.io/gh/apache/arrow/commit/ab307365198d5724a0b3331a05704d0fe4bcd710?el=desc) (ab30736) will **increase** coverage by `0.00%`.
> The diff coverage is `96.87%`.
[](https://codecov.io/gh/apache/arrow/pull/9388?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #9388 +/- ##
=======================================
Coverage 82.12% 82.13%
=======================================
Files 243 243
Lines 54737 54757 +20
=======================================
+ Hits 44951 44972 +21
+ Misses 9786 9785 -1
```
| [Impacted Files](https://codecov.io/gh/apache/arrow/pull/9388?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [rust/arrow/src/array/array\_list.rs](https://codecov.io/gh/apache/arrow/pull/9388/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvYXJyYXkvYXJyYXlfbGlzdC5ycw==) | `94.05% <95.55%> (+0.64%)` | :arrow_up: |
| [rust/arrow/src/compute/kernels/concat.rs](https://codecov.io/gh/apache/arrow/pull/9388/diff?src=pr&el=tree#diff-cnVzdC9hcnJvdy9zcmMvY29tcHV0ZS9rZXJuZWxzL2NvbmNhdC5ycw==) | `100.00% <100.00%> (ø)` | |
| [rust/parquet/src/encodings/encoding.rs](https://codecov.io/gh/apache/arrow/pull/9388/diff?src=pr&el=tree#diff-cnVzdC9wYXJxdWV0L3NyYy9lbmNvZGluZ3MvZW5jb2RpbmcucnM=) | `95.05% <0.00%> (+0.19%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow/pull/9388?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/arrow/pull/9388?src=pr&el=footer). Last update [ab30736...9cdbdbb](https://codecov.io/gh/apache/arrow/pull/9388?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
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
[GitHub] [arrow] github-actions[bot] commented on pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #9388:
URL: https://github.com/apache/arrow/pull/9388#issuecomment-771053570
https://issues.apache.org/jira/browse/ARROW-11459
----------------------------------------------------------------
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
[GitHub] [arrow] alamb commented on a change in pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #9388:
URL: https://github.com/apache/arrow/pull/9388#discussion_r579660523
##########
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:
"Today I learned" -- thank you @jorgecarleitao !
----------------------------------------------------------------
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
[GitHub] [arrow] alamb commented on a change in pull request #9388: ARROW-11459: [Rust] Added API to build ListArray of Primitives from an iterator
Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org