Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions datafusion/physical-plan/src/spill/in_progress_spill_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ impl InProgressSpillFile {
Ok(())
}

pub fn flush(&mut self) -> Result<()> {
if let Some(writer) = &mut self.writer {
writer.flush()?;
}
Ok(())
}

/// Returns a reference to the in-progress file, if it exists.
/// This can be used to get the file path for creating readers before the file is finished.
pub fn file(&self) -> Option<&RefCountedTempFile> {
Expand Down
5 changes: 5 additions & 0 deletions datafusion/physical-plan/src/spill/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ impl IPCStreamWriter {
Ok((delta_num_rows, delta_num_bytes))
}

pub fn flush(&mut self) -> Result<()> {
self.writer.flush()?;
Ok(())
}

/// Finish the writer
pub fn finish(&mut self) -> Result<()> {
self.writer.finish().map_err(Into::into)
Expand Down
13 changes: 13 additions & 0 deletions datafusion/physical-plan/src/spill/spill_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ impl SpillManager {

Ok(spawn_buffered(stream, self.batch_read_buffer_capacity))
}

/// Same as `read_spill_as_stream`, but without buffering.
pub fn read_spill_as_stream_unbuffered(
&self,
spill_file_path: RefCountedTempFile,
max_record_batch_memory: Option<usize>,
) -> Result<SendableRecordBatchStream> {
Ok(Box::pin(cooperative(SpillReaderStream::new(
Arc::clone(&self.schema),
spill_file_path,
max_record_batch_memory,
))))
}
}

pub(crate) trait GetSlicedSize {
Expand Down
8 changes: 7 additions & 1 deletion datafusion/physical-plan/src/spill/spill_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ impl SpillPoolWriter {
// Append the batch
if let Some(ref mut writer) = file_shared.writer {
writer.append_batch(batch)?;
// make sure we flush the writer for readers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if it is ok to flush each batch (will this result in too many small file IOs?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do this, or we need to ensure data is written before we try reading it, maybe we could do it periodically rather than each batch

writer.flush()?;
file_shared.batches_written += 1;
file_shared.estimated_size += batch_size;
}
Expand Down Expand Up @@ -535,7 +537,11 @@ impl Stream for SpillFile {
// Step 2: Lazy-create reader stream if needed
if self.reader.is_none() && should_read {
if let Some(file) = file {
match self.spill_manager.read_spill_as_stream(file, None) {
// we want this unbuffered because files are actively being written to
match self
.spill_manager
.read_spill_as_stream_unbuffered(file, None)
{
Ok(stream) => {
self.reader = Some(SpillFileReader {
stream,
Expand Down