Skip to content
Merged
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
31 changes: 30 additions & 1 deletion rust/lance-datafusion-exec/src/chunker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,15 @@ pub fn chunk_concat_stream(
/// This stream _will_ combine record batches and so it can be fairly expensive as it will
/// likely force a copy of all incoming data. However, it can be useful when users require
/// precise batch sizing.
///
/// A zero `batch_size` yields one configuration error on the first poll and
/// then ends the stream.
pub struct StrictBatchSizeStream<S> {
inner: S,
batch_size: usize,
buffered: VecDeque<RecordBatch>,
buffered_rows: usize,
is_done: bool,
}

impl<S: Stream<Item = DataFusionResult<RecordBatch>> + Unpin> StrictBatchSizeStream<S> {
Expand All @@ -133,6 +137,7 @@ impl<S: Stream<Item = DataFusionResult<RecordBatch>> + Unpin> StrictBatchSizeStr
batch_size,
buffered: VecDeque::new(),
buffered_rows: 0,
is_done: false,
}
}

Expand Down Expand Up @@ -205,6 +210,15 @@ where

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
loop {
if self.is_done {
return Poll::Ready(None);
}
if self.batch_size == 0 {
self.is_done = true;
return Poll::Ready(Some(Err(DataFusionError::Configuration(
"StrictBatchSizeStream batch_size must be greater than zero".to_string(),
))));
}
if self.buffered_rows >= self.batch_size {
let batch_size = self.batch_size;
return Poll::Ready(Some(self.take_buffered(batch_size)));
Expand Down Expand Up @@ -244,7 +258,7 @@ mod tests {
use arrow::datatypes::{Int32Type, Int64Type};
use arrow_array::record_batch;
use datafusion_physical_plan::stream::RecordBatchStreamAdapter;
use futures::TryStreamExt;
use futures::{StreamExt, TryStreamExt};
use lance_datagen::{BatchCount, RowCount, array};

use crate::datagen::DatafusionDatagenExt;
Expand Down Expand Up @@ -284,6 +298,21 @@ mod tests {
}
}

#[tokio::test]
async fn test_strict_batch_size_stream_rejects_zero_batch_size() {
let batch = record_batch!(("value", Int32, [1, 2, 3])).unwrap();
let batches = futures::stream::iter([Ok::<_, datafusion_common::DataFusionError>(batch)]);

let mut stream = super::StrictBatchSizeStream::new(batches, 0);
let error = stream.next().await.unwrap().unwrap_err();
assert!(matches!(
error,
datafusion_common::DataFusionError::Configuration(message)
if message.contains("greater than zero")
));
assert!(stream.next().await.is_none());
}

#[tokio::test]
async fn test_strict_batch_size_stream_buffers_tiny_batches() {
let batches = (0..100).map(|value| {
Expand Down
Loading