-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use LazyTableProvider by default for write_to_deltalake for mem…
…ory efficiency This defaults write_to_deltalake in Python to attempt to use the LazytableProvider for a more stream-like execution. It's currently opted out for schewma evolution since that's not supported by default. Some improvements in schema mismatch detection inside of the operations::write module are required as well Signed-off-by: R. Tyler Croy <rtyler@brokenco.de>
- Loading branch information
Showing
6 changed files
with
106 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//! The write module contains shared code used for writes by the write_to_deltalake function and | ||
//! the merge cod | ||
use deltalake::arrow::ffi_stream::ArrowArrayStreamReader; | ||
use deltalake::datafusion::catalog::TableProvider; | ||
use deltalake::datafusion::physical_plan::memory::LazyBatchGenerator; | ||
use deltalake::delta_datafusion::LazyTableProvider; | ||
use deltalake::DeltaResult; | ||
use parking_lot::RwLock; | ||
use std::fmt::{self}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
/// Convert an [ArrowArrayStreamReader] into a [LazyTableProvider] | ||
pub(crate) fn to_lazy_table(source: ArrowArrayStreamReader) -> DeltaResult<Arc<dyn TableProvider>> { | ||
use deltalake::arrow::array::RecordBatchReader; | ||
let schema = source.schema(); | ||
let arrow_stream: Arc<Mutex<ArrowArrayStreamReader>> = Arc::new(Mutex::new(source)); | ||
let arrow_stream_batch_generator: Arc<RwLock<dyn LazyBatchGenerator>> = | ||
Arc::new(RwLock::new(ArrowStreamBatchGenerator::new(arrow_stream))); | ||
|
||
Ok(Arc::new(LazyTableProvider::try_new( | ||
schema.clone(), | ||
vec![arrow_stream_batch_generator], | ||
)?)) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct ArrowStreamBatchGenerator { | ||
pub array_stream: Arc<Mutex<ArrowArrayStreamReader>>, | ||
} | ||
|
||
impl fmt::Display for ArrowStreamBatchGenerator { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"ArrowStreamBatchGenerator {{ array_stream: {:?} }}", | ||
self.array_stream | ||
) | ||
} | ||
} | ||
|
||
impl ArrowStreamBatchGenerator { | ||
pub fn new(array_stream: Arc<Mutex<ArrowArrayStreamReader>>) -> Self { | ||
Self { array_stream } | ||
} | ||
} | ||
|
||
impl LazyBatchGenerator for ArrowStreamBatchGenerator { | ||
fn generate_next_batch( | ||
&mut self, | ||
) -> deltalake::datafusion::error::Result<Option<deltalake::arrow::array::RecordBatch>> { | ||
let mut stream_reader = self.array_stream.lock().map_err(|_| { | ||
deltalake::datafusion::error::DataFusionError::Execution( | ||
"Failed to lock the ArrowArrayStreamReader".to_string(), | ||
) | ||
})?; | ||
|
||
match stream_reader.next() { | ||
Some(Ok(record_batch)) => Ok(Some(record_batch)), | ||
Some(Err(err)) => Err(deltalake::datafusion::error::DataFusionError::ArrowError( | ||
err, None, | ||
)), | ||
None => Ok(None), // End of stream | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters