alamb opened a new pull request #7:
URL: https://github.com/apache/arrow-rs/pull/7
(Here is a PR to the new arrow crate to see what how this process is
working!)
# Rationale / Usecase:
While writing tests (both in IOx and in DataFusion) where I need a single
`RecordBatch`, I often find myself doing something like this (copied directly
from IOx source code):
```rust
let schema = Arc::new(Schema::new(vec![
ArrowField::new("float_field", ArrowDataType::Float64, true),
ArrowField::new("time", ArrowDataType::Int64, true),
]));
let float_array: ArrayRef = Arc::new(Float64Array::from(vec![10.1, 20.1,
30.1, 40.1]));
let timestamp_array: ArrayRef = Arc::new(Int64Array::from(vec![1000, 2000,
3000, 4000]));
let batch = RecordBatch::try_new(schema, vec![float_array, timestamp_array])
.expect("created new record batch");
```
This is annoying because I have to redundantly (and verbosely) encode the
information that `float_field` is a Float64 both in the `Schema` and the
`Float64Array`
I would much rather be able to construct `RecordBatches` using a more Rust
like style to avoid the the redundancy and reduce the amount of typing /
redundancy:
# Proposed Change
As suggested in the comments from @returnString @nevi-me @jorgecarleitao in
the draft PR: https://github.com/apache/arrow/pull/10063 add `try_from_iter`
and `try_iter_with_null` functions
```rust
let record_batch = RecordBatch::try_from_iter(vec![
("a", Arc::new(a) as ArrayRef),
("b", Arc::new(b) as ArrayRef)
]).expect("valid conversion");
```
# TryFrom Implementation
Note I would really like to add a`TryFrom` implementation so I could write
```rust
let record_batch: RecordBatch = vec![
("a", Arc::new(a) as ArrayRef),
("b", Arc::new(b) as ArrayRef)
].try_into().expect("valid conversion");
```
However, when I tried to do so (with the following):
```rust
impl <I,F> TryFrom<I> for RecordBatch
where
I: IntoIterator<Item=(F, ArrayRef)>,
F: AsRef<str>,
{
type Error = ArrowError;
fn try_from(value: I) -> std::result::Result<Self, Self::Error> {
Self::try_from_iter(value)
}
}
```
I got the following compiler error
```
= note: conflicting implementation in crate `core`:
- impl<T, U> TryFrom<U> for T
where U: Into<T>;
```
Which appears to be a limitation of the Rust typesystem / compiler: See
https://github.com/rust-lang/rust/issues/50133. Any help / suggestions from
reviewers would be most appreciated.
--
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]