rdettai commented on a change in pull request #1138: URL: https://github.com/apache/arrow-datafusion/pull/1138#discussion_r730638434
########## 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 + + Send + + Unpin + + 'static, +{ + /// An iterator over record batches of the last file returned by file_iter + batch_iter: BatchIter, + /// An iterator over input files + file_iter: FileIter, + /// The stream schema (file schema after projection) + schema: SchemaRef, + /// The remaining number of records to parse, None if no limit + remain: Option<usize>, + /// A closure that takes a reader and an optional remaining number of lines + /// (before reaching the limit) and returns a batch iterator. If the file reader + /// is not capable of limiting the number of records in the last batch, the file + /// stream will take care of truncating it. + file_reader: F, +} + +impl<F> FileStream<F> +where + F: FnMut(Box<dyn Read + Send + Sync>, &Option<usize>) -> BatchIter + + Send + + Unpin + + 'static, +{ + pub fn new( + object_store: Arc<dyn ObjectStore>, + files: Vec<PartitionedFile>, + file_reader: F, + schema: SchemaRef, + limit: Option<usize>, + ) -> Self { + let read_iter = files.into_iter().map(move |f| -> DataFusionResult<_> { + object_store + .file_reader(f.file_meta.sized_file)? + .sync_reader() + }); + + Self { + file_iter: Box::new(read_iter), + batch_iter: Box::new(iter::empty()), + remain: limit, + schema, + file_reader, + } + } + + /// Acts as a flat_map of record batches over files. + fn next_batch(&mut self) -> Option<ArrowResult<RecordBatch>> { + match self.batch_iter.next() { + Some(batch) => Some(batch), + None => match self.file_iter.next() { + Some(Ok(f)) => { + self.batch_iter = (self.file_reader)(f, &self.remain); + self.next_batch() + } + Some(Err(e)) => Some(Err(ArrowError::ExternalError(Box::new(e)))), + None => None, + }, + } + } +} + +impl<F> Stream for FileStream<F> +where + F: FnMut(Box<dyn Read + Send + Sync>, &Option<usize>) -> BatchIter + + Send + + Unpin + + 'static, +{ + type Item = ArrowResult<RecordBatch>; + + fn poll_next( + mut self: Pin<&mut Self>, + _cx: &mut Context<'_>, + ) -> Poll<Option<Self::Item>> { + // check if finished or no limit + match self.remain { + Some(r) if r == 0 => return Poll::Ready(None), + None => return Poll::Ready(self.get_mut().next_batch()), + Some(r) => r, + }; + + Poll::Ready(match self.as_mut().next_batch() { + Some(Ok(item)) => { + if let Some(remain) = self.remain.as_mut() { + if *remain >= item.num_rows() { + *remain -= item.num_rows(); + Some(Ok(item)) + } else { + let len = *remain; + *remain = 0; + Some(Ok(RecordBatch::try_new( + item.schema(), + item.columns() + .iter() + .map(|column| column.slice(0, len)) + .collect(), Review comment: This factorizes the current implementations for `AvroStream` and `NdJsonStream` as it is today, but I am wondering if it is really worth it to slice the batch to match exactly the limit. According to the `TableProvider` [doc](https://github.com/apache/arrow-datafusion/blob/161fcd88f3f827d3c2d10d5de60ec84863d4a162/datafusion/src/datasource/datasource.rs#L82-L86), `limit` indicates that `// The datasource should return *at least* this number of rows if available.` The slicing operation is "zero copy" anyway, so mostly free, but it is surprising to have this extra operation if it is not required by the `TableProvider` API. -- 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]
