-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
serve results of a lazy df in a generator
- Loading branch information
Boruch Chalk
committed
May 31, 2024
1 parent
d7b4f72
commit 5b8d884
Showing
18 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
51 changes: 51 additions & 0 deletions
51
crates/polars-pipe/src/executors/sinks/output/batch_sink.rs
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,51 @@ | ||
use std::any::Any; | ||
|
||
use crossbeam_channel::Sender; | ||
use polars_core::prelude::*; | ||
|
||
use crate::operators::{ | ||
chunks_to_df_unchecked, DataChunk, FinalizedSink, PExecutionContext, Sink, SinkResult, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct BatchSink { | ||
sender: Sender<DataFrame>, | ||
} | ||
|
||
impl BatchSink { | ||
pub fn new(sender: Sender<DataFrame>) -> PolarsResult<Self> { | ||
Ok(Self { sender }) | ||
} | ||
} | ||
|
||
impl Sink for BatchSink { | ||
fn sink(&mut self, _context: &PExecutionContext, chunk: DataChunk) -> PolarsResult<SinkResult> { | ||
let df: DataFrame = chunks_to_df_unchecked(vec![chunk]); | ||
let result = self.sender.send(df); | ||
match result { | ||
Ok(..) => Ok(SinkResult::CanHaveMoreInput), | ||
Err(..) => Ok(SinkResult::Finished), | ||
} | ||
} | ||
|
||
fn combine(&mut self, _other: &mut dyn Sink) { | ||
// Nothing to do | ||
} | ||
|
||
fn split(&self, _thread_no: usize) -> Box<dyn Sink> { | ||
Box::new(self.clone()) | ||
} | ||
|
||
fn finalize(&mut self, _context: &PExecutionContext) -> PolarsResult<FinalizedSink> { | ||
let _ = self.sender.send(Default::default()); | ||
Ok(FinalizedSink::Finished(Default::default())) | ||
} | ||
|
||
fn as_any(&mut self) -> &mut dyn Any { | ||
self | ||
} | ||
|
||
fn fmt(&self) -> &str { | ||
"batch_sink" | ||
} | ||
} |
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
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
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,38 @@ | ||
use crossbeam_channel::Receiver; | ||
use polars::frame::DataFrame; | ||
use pyo3::prelude::*; | ||
|
||
use crate::dataframe::PyDataFrame; | ||
|
||
#[pyclass] | ||
pub struct DataFrameIter { | ||
pub df_receiver: Receiver<DataFrame>, | ||
pub limit: Option<usize>, | ||
pub num_rows: usize, | ||
} | ||
|
||
#[pymethods] | ||
impl DataFrameIter { | ||
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { | ||
slf | ||
} | ||
|
||
fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<PyDataFrame> { | ||
if slf.limit.is_some() && slf.num_rows >= slf.limit.unwrap() { | ||
return None; | ||
} | ||
let mut df = match slf.df_receiver.recv() { | ||
Ok(df) => df, | ||
Err(e) => { | ||
return None; | ||
} | ||
}; | ||
|
||
if slf.limit.is_some() && slf.limit.unwrap() - slf.num_rows < df.height() { | ||
let limit = slf.limit.unwrap() - slf.num_rows; | ||
df = df.head(Some(limit)); | ||
} | ||
slf.num_rows += df.height(); | ||
Some(df.into()) | ||
} | ||
} |
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
Oops, something went wrong.