rdettai commented on a change in pull request #1138: URL: https://github.com/apache/arrow-datafusion/pull/1138#discussion_r732465434
########## File path: datafusion/src/physical_plan/file_format/file_stream.rs ########## @@ -0,0 +1,274 @@ +// 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. + +//! A generic stream over file format readers that can be used by +//! any file format that read its files from start to end. +//! +//! Note: Most traits here need to be marked `Sync + Send` to be +//! compliant with the `SendableRecordBatchStream` trait. + +use crate::{ + datasource::{object_store::ObjectStore, PartitionedFile}, + error::Result as DataFusionResult, + physical_plan::RecordBatchStream, +}; +use arrow::{ + datatypes::SchemaRef, + error::{ArrowError, Result as ArrowResult}, + record_batch::RecordBatch, +}; +use futures::Stream; +use std::{ + io::Read, + iter, + pin::Pin, + sync::Arc, + task::{Context, Poll}, +}; + +pub type FileIter = + Box<dyn Iterator<Item = DataFusionResult<Box<dyn Read + Send + Sync>>> + Send + Sync>; +pub type BatchIter = Box<dyn Iterator<Item = ArrowResult<RecordBatch>> + Send + Sync>; + +/// A stream that iterates record batch by record batch, file over file. +pub struct FileStream<F> +where + F: FnMut(Box<dyn Read + Send + Sync>, &Option<usize>) -> BatchIter Review comment: I agree that closures are a bit complex in Rust 😅... But I don't think that the usage we have for it here is particularly convoluted. The problem here is that different readers take different configurations (in particular CSV, with configurations such as `delimiter`). Thus you need a way to have this variable set of configurations in the context when you initialize the reader. You could achieve this with a trait. In fact, the closure here allows us to create an anonymous version of a trait that would look like: ```Rust pub trait ReaderOpener { pub fn open_reader(&mut self, file: Box<dyn Read+Send+Sync> remaining: &Option<usize>) -> BatchIter; } ``` Using the trait instead of a closure would require us to create an implem of that trait for each format (AvroReaderOpener, JsonReaderOpener...). These structs would contain all the configurations that are required for the reader initialization as fields. Here, instead of doing this explicit declaration, the closures capture the configurations they need. Under the hood, the closure is doing **exactly** the same thing as we would have done if we chose to declare the implementions of `ReaderOpener`, except that that `ReaderOpener` is replaced by `FnMut(Box<dyn Read + Send + Sync>, &Option<usize>) -> BatchIter` and the implementation types are created by the compiler. -- 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]
