Dandandan commented on code in PR #20820: URL: https://github.com/apache/datafusion/pull/20820#discussion_r2928954893
########## datafusion/datasource/src/morsel/adapters.rs: ########## @@ -0,0 +1,147 @@ +// 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. + +use crate::PartitionedFile; +use crate::file_stream::FileOpener; +use crate::morsel::{Morsel, MorselPlan, MorselPlanner, Morselizer}; +use arrow::array::RecordBatch; +use datafusion_common::Result; +use futures::FutureExt; +use futures::stream::BoxStream; +use std::fmt::Debug; +use std::sync::{Arc, Mutex}; + +/// An adapter for `FileOpener` that allows it to be used as a `Morselizer` for +/// backwards compatibility. +/// +/// This is useful for file formats that do not support morselization, where we +/// can treat the entire file as a single morsel. +pub struct FileOpenerMorselizer { + file_opener: Arc<dyn FileOpener>, +} + +impl Debug for FileOpenerMorselizer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FileOpenerMorselizer") + .field("file_opener", &"...") + .finish() + } +} + +impl FileOpenerMorselizer { + pub fn new(file_opener: Arc<dyn FileOpener>) -> Self { + Self { file_opener } + } +} + +impl Morselizer for FileOpenerMorselizer { + fn morselize(&self, file: PartitionedFile) -> Result<Vec<Arc<dyn MorselPlanner>>> { + let opener = Arc::clone(&self.file_opener); + let planner = FileOpenFutureMorselPlanner::new(opener, file); + Ok(vec![Arc::new(planner)]) + } +} + +/// Adapter for `FileOpenFuture` that allows it to be used as a `MorselPlanner` +/// for backwards compatibility. +struct FileOpenFutureMorselPlanner { + file_opener: Arc<dyn FileOpener>, + stream: Arc<Mutex<Option<BoxStream<'static, Result<RecordBatch>>>>>, + file: Mutex<Option<PartitionedFile>>, +} + +impl Debug for FileOpenFutureMorselPlanner { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FileOpenFutureMorselPlanner") + .field("file_opener", &"...") + .field("stream", &"...") + .field("file", &self.file) + .finish() + } +} + +impl FileOpenFutureMorselPlanner { + pub fn new(file_opener: Arc<dyn FileOpener>, file: PartitionedFile) -> Self { + Self { + file_opener, + stream: Arc::new(Mutex::new(None)), + file: Mutex::new(Some(file)), + } + } +} + +impl MorselPlanner for FileOpenFutureMorselPlanner { + fn plan(&self) -> Result<Option<MorselPlan>> { + let mut morsel_plan = MorselPlan::new(); + let mut made_progress = false; + + // Note that plan should **not** do IO work so setup a callback if needed + if let Some(file) = self.file.lock().unwrap().take() { + let file_opener = Arc::clone(&self.file_opener); + let output_stream = Arc::clone(&self.stream); + let load_future = async move { + let stream = file_opener + // open the file to get a stream + .open(file)? Review Comment: This (currently) does some bunch of IO/CPU before creating the stream -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
