tustvold commented on code in PR #6527: URL: https://github.com/apache/arrow-rs/pull/6527#discussion_r1792330083
########## arrow/examples/chunked_arrays.rs: ########## @@ -0,0 +1,106 @@ +// 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. + +//! This example demonstrates using Vec<ArrayRef> as an alternative to ChunkedArray. +use arrow::array::{ArrayRef, AsArray, Float32Array, StringArray}; +use arrow::record_batch::RecordBatch; +use arrow_array::cast::as_string_array; +use arrow_array::types::Float32Type; +use std::sync::Arc; + +fn main() { + let batches = [ + RecordBatch::try_from_iter(vec![ + ( + "label", + Arc::new(StringArray::from(vec!["A", "B", "C"])) as ArrayRef, + ), + ( + "value", + Arc::new(Float32Array::from(vec![0.1, 0.2, 0.3])) as ArrayRef, + ), + ]) + .unwrap(), + RecordBatch::try_from_iter(vec![ + ( + "label", + Arc::new(StringArray::from(vec!["D", "E"])) as ArrayRef, + ), + ( + "value", + Arc::new(Float32Array::from(vec![0.4, 0.5])) as ArrayRef, + ), + ]) + .unwrap(), + ]; + + // chunked_array_by_index is an array of two Vec<ArrayRef> where each Vec<ArrayRef> is a column + let mut chunked_array_by_index = [Vec::new(), Vec::new()]; + for batch in &batches { + for (i, array) in batch.columns().iter().enumerate() { + chunked_array_by_index[i].push(array.clone()); + } + } + + // downcast and iterate over the values - column 0 is the labels and column 1 is the values + let labels: Vec<&str> = chunked_array_by_index[0] + .iter() + .flat_map(|x| as_string_array(x).iter()) Review Comment: Using AsArray is typically more ergonomic ########## arrow/examples/chunked_arrays.rs: ########## @@ -0,0 +1,106 @@ +// 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. + +//! This example demonstrates using Vec<ArrayRef> as an alternative to ChunkedArray. +use arrow::array::{ArrayRef, AsArray, Float32Array, StringArray}; +use arrow::record_batch::RecordBatch; +use arrow_array::cast::as_string_array; +use arrow_array::types::Float32Type; +use std::sync::Arc; + +fn main() { + let batches = [ + RecordBatch::try_from_iter(vec![ + ( + "label", + Arc::new(StringArray::from(vec!["A", "B", "C"])) as ArrayRef, + ), + ( + "value", + Arc::new(Float32Array::from(vec![0.1, 0.2, 0.3])) as ArrayRef, + ), + ]) + .unwrap(), + RecordBatch::try_from_iter(vec![ + ( + "label", + Arc::new(StringArray::from(vec!["D", "E"])) as ArrayRef, + ), + ( + "value", + Arc::new(Float32Array::from(vec![0.4, 0.5])) as ArrayRef, + ), + ]) + .unwrap(), + ]; + + // chunked_array_by_index is an array of two Vec<ArrayRef> where each Vec<ArrayRef> is a column + let mut chunked_array_by_index = [Vec::new(), Vec::new()]; + for batch in &batches { + for (i, array) in batch.columns().iter().enumerate() { + chunked_array_by_index[i].push(array.clone()); + } + } Review Comment: Collecting into separate Vec like this is unnecessary, see below ########## arrow/src/lib.rs: ########## @@ -345,6 +345,67 @@ //! orchestrates the primitives exported by this crate into an embeddable query engine, with //! SQL and DataFrame frontends, and heavily influences this crate's roadmap. //! +//! The Rust implementation does not provide the ChunkedArray abstraction implemented by the Python +//! and C++ Arrow implementations. The recommended alternative is to use one of the following: +//! - `Vec<ArrayRef>` a simple, eager version of a `ChunkedArray` +//! - `impl Iterator<Item=ArrayRef>` a lazy version of a `ChunkedArray` +//! - `impl Stream<Item=ArrayRef>` a lazy async version of a `ChunkedArray` +//! +//! Similar patterns can be applied at the `RecordBatch` level. For example, [DataFusion] makes +//! extensive use of [RecordBatchStream]. +//! +//! This approach integrates well into the Rust ecosystem, simplifies the implementation and +//! encourages the use of performant lazy and async patterns. +//! +//! Aside from providing a slightly less convenient API, one other downside is the lack of support +//! for processing compute kernels across chunked arrays. But this use case is well-supported by +//! [DataFusion]. +//! Review Comment: ```suggestion ``` I'm not sure I agree with this statement, iterators are fairly ergonomic once you get the hang of them -- 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]
