rluvaton opened a new issue, #6846:
URL: https://github.com/apache/arrow-rs/issues/6846
**Which part is this question about**
library api
**Describe your question**
I have a `ListArray` and I want to filter each item in the list
```text
┌─────────────┐ -> ┌─────────────┐
│ [A,B,C] │ -> │ [A,C] │
├─────────────┤ -> ├─────────────┤
│ [] │ -> │ [] │
├─────────────┤ -> ├─────────────┤
│ NULL │ -> │ NULL │
├─────────────┤ -> ├─────────────┤
│ [D] │ -> │ [] │
├─────────────┤ -> ├─────────────┤
│ [NULL, F] │ -> │ [NULL, F] │
└─────────────┘ -> └─────────────┘
```
But I couldn't find a way to do it
I tried this:
```rust
#[cfg(test)]
mod tests {
use arrow::array::{Array, BooleanArray, ListArray};
use arrow::datatypes::Int32Type;
use arrow::error::ArrowError;
use std::ops::Deref;
#[test]
fn list_array() -> Result<(), ArrowError> {
// Create list
let list_array = create_list_for_test();
// go over each list in the array and filter items in it
let new_list: ListArray = list_array
.iter()
.map(|x| {
match x {
None => Ok(None),
Some(x) => {
let some_predicate =
BooleanArray::from((0..x.len()).map(|i| i % 2 == 0).collect::<Vec<_>>());
let val = arrow::compute::filter(x.deref(),
&some_predicate)?;
Ok(Some(val))
}
}
})
.collect::<Result<_, _>>()?;
println!("{:?}", new_list);
Ok(())
}
// This can be list of list of int32 or any other inner list type
fn create_list_for_test() -> ListArray {
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);
list_array
}
}
```
But got:
```
error[E0277]: a value of type `GenericListArray<i32>` cannot be built from
an iterator over elements of type `Option<Arc<dyn arrow::array::Array>>`
--> arrow-pg/src/lib.rs:31:24
|
31 | .collect::<Result<_, _>>()?;
| ------- ^^^^^^^^^^^^ value of type
`GenericListArray<i32>` cannot be built from
`std::iter::Iterator<Item=Option<Arc<dyn arrow::array::Array>>>`
| |
| required by a bound introduced by this call
|
= help: the trait `FromIterator<Option<Arc<dyn arrow::array::Array>>>`
is not implemented for `GenericListArray<i32>`, which is required by `Result<_,
_>: FromIterator<Result<Option<Arc<dyn arrow::array::Array>>, _>>`
= help: the trait `FromIterator<Result<A, E>>` is implemented for
`Result<V, E>`
= note: required for `Result<GenericListArray<i32>, _>` to implement
`FromIterator<Result<Option<Arc<dyn arrow::array::Array>>, _>>`
note: required by a bound in `collect`
-->
/Users/something/.rustup/toolchains/1.80-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2001:19
|
2001 | fn collect<B: FromIterator<Self::Item>>(self) -> B
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in
`Iterator::collect`
```
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]