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
41 changes: 37 additions & 4 deletions python/python/lance/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ def scanner(
offset: Optional[int] = None,
nearest: Optional[dict] = None,
batch_size: Optional[int] = None,
batch_size_bytes: Optional[int] = None,
batch_readahead: Optional[int] = None,
fragment_readahead: Optional[int] = None,
scan_in_order: Optional[bool] = None,
Expand Down Expand Up @@ -795,9 +796,16 @@ def scanner(
}

batch_size: int, default None
The target size of batches returned. In some cases batches can be up to
twice this size (but never larger than this). In some cases batches can
be smaller than this size.
The maximum number of rows per batch. In some cases batches can be
smaller than this size. Note: this can be overridden by
``batch_size_bytes`` or by a dataset-level ``batch_size_bytes``
configured via ``FileReaderOptions``.
batch_size_bytes: int, default None
If set, the scanner will produce batches whose total size in bytes
is approximately this value, overriding the row-based ``batch_size``.
This can also be configured at the dataset level via
``FileReaderOptions``. A scanner-level setting takes precedence
over the dataset-level default.
io_buffer_size: int, default None
The size of the IO buffer. See ``ScannerBuilder.io_buffer_size``
for more information.
Expand Down Expand Up @@ -933,6 +941,7 @@ def setopt(opt, val):
setopt(builder.limit, limit)
setopt(builder.offset, offset)
setopt(builder.batch_size, batch_size)
setopt(builder.batch_size_bytes, batch_size_bytes)
setopt(builder.io_buffer_size, io_buffer_size)
setopt(builder.batch_readahead, batch_readahead)
setopt(builder.fragment_readahead, fragment_readahead)
Expand Down Expand Up @@ -1016,6 +1025,7 @@ def to_table(
offset: Optional[int] = None,
nearest: Optional[dict] = None,
batch_size: Optional[int] = None,
batch_size_bytes: Optional[int] = None,
batch_readahead: Optional[int] = None,
fragment_readahead: Optional[int] = None,
scan_in_order: Optional[bool] = None,
Expand Down Expand Up @@ -1143,6 +1153,7 @@ def to_table(
offset=offset,
nearest=nearest,
batch_size=batch_size,
batch_size_bytes=batch_size_bytes,
io_buffer_size=io_buffer_size,
batch_readahead=batch_readahead,
fragment_readahead=fragment_readahead,
Expand Down Expand Up @@ -1519,6 +1530,7 @@ def to_batches(
offset: Optional[int] = None,
nearest: Optional[dict] = None,
batch_size: Optional[int] = None,
batch_size_bytes: Optional[int] = None,
batch_readahead: Optional[int] = None,
fragment_readahead: Optional[int] = None,
scan_in_order: Optional[bool] = None,
Expand Down Expand Up @@ -1555,6 +1567,7 @@ def to_batches(
offset=offset,
nearest=nearest,
batch_size=batch_size,
batch_size_bytes=batch_size_bytes,
io_buffer_size=io_buffer_size,
batch_readahead=batch_readahead,
fragment_readahead=fragment_readahead,
Expand Down Expand Up @@ -4984,6 +4997,7 @@ def __init__(self, ds: LanceDataset):
self._columns_with_transform = None
self._nearest = None
self._batch_size: Optional[int] = None
self._batch_size_bytes: Optional[int] = None
self._io_buffer_size: Optional[int] = None
self._batch_readahead: Optional[int] = None
self._fragment_readahead: Optional[int] = None
Expand Down Expand Up @@ -5014,10 +5028,28 @@ def apply_defaults(self, default_opts: Dict[str, Any]) -> ScannerBuilder:
return self

def batch_size(self, batch_size: int) -> ScannerBuilder:
"""Set batch size for Scanner"""
"""Set the maximum number of rows per batch.

Note: this can be overridden by ``batch_size_bytes`` or by a
dataset-level ``batch_size_bytes`` configured via
``FileReaderOptions``.
"""
self._batch_size = batch_size
return self

def batch_size_bytes(self, batch_size_bytes: int) -> ScannerBuilder:
"""Set the target batch size in bytes.

When set, the scanner will produce batches whose total size in bytes
is approximately this value, overriding the row-based ``batch_size``.

This can also be configured at the dataset level via
``FileReaderOptions``. A scanner-level setting takes precedence
over the dataset-level default.
"""
self._batch_size_bytes = batch_size_bytes
return self

def io_buffer_size(self, io_buffer_size: int) -> ScannerBuilder:
"""
Set the I/O buffer size for the Scanner
Expand Down Expand Up @@ -5402,6 +5434,7 @@ def to_scanner(self) -> LanceScanner:
self._offset,
self._nearest,
self._batch_size,
self._batch_size_bytes,
self._io_buffer_size,
self._batch_readahead,
self._fragment_readahead,
Expand Down
6 changes: 5 additions & 1 deletion python/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl Dataset {
}

#[allow(clippy::too_many_arguments)]
#[pyo3(signature=(columns=None, columns_with_transform=None, filter=None, search_filter=None, prefilter=None, limit=None, offset=None, nearest=None, batch_size=None, io_buffer_size=None, batch_readahead=None, fragment_readahead=None, scan_in_order=None, fragments=None, with_row_id=None, with_row_address=None, use_stats=None, substrait_filter=None, fast_search=None, full_text_query=None, late_materialization=None, blob_handling=None, use_scalar_index=None, include_deleted_rows=None, scan_stats_callback=None, strict_batch_size=None, order_by=None, disable_scoring_autoprojection=None, substrait_aggregate=None))]
#[pyo3(signature=(columns=None, columns_with_transform=None, filter=None, search_filter=None, prefilter=None, limit=None, offset=None, nearest=None, batch_size=None, batch_size_bytes=None, io_buffer_size=None, batch_readahead=None, fragment_readahead=None, scan_in_order=None, fragments=None, with_row_id=None, with_row_address=None, use_stats=None, substrait_filter=None, fast_search=None, full_text_query=None, late_materialization=None, blob_handling=None, use_scalar_index=None, include_deleted_rows=None, scan_stats_callback=None, strict_batch_size=None, order_by=None, disable_scoring_autoprojection=None, substrait_aggregate=None))]
fn scanner(
self_: PyRef<'_, Self>,
columns: Option<Vec<String>>,
Expand All @@ -812,6 +812,7 @@ impl Dataset {
offset: Option<i64>,
nearest: Option<&Bound<PyDict>>,
batch_size: Option<usize>,
batch_size_bytes: Option<u64>,
io_buffer_size: Option<u64>,
batch_readahead: Option<usize>,
fragment_readahead: Option<usize>,
Expand Down Expand Up @@ -956,6 +957,9 @@ impl Dataset {
if let Some(batch_size) = batch_size {
scanner.batch_size(batch_size);
}
if let Some(batch_size_bytes) = batch_size_bytes {
scanner.batch_size_bytes(batch_size_bytes);
}
if let Some(io_buffer_size) = io_buffer_size {
scanner.io_buffer_size(io_buffer_size);
}
Expand Down
26 changes: 20 additions & 6 deletions rust/lance-file/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,21 @@ pub struct FileReaderOptions {
/// will be read in multiple chunks to control memory usage.
/// Default: 8MB (DEFAULT_READ_CHUNK_SIZE)
pub read_chunk_size: u64,
/// If set, the reader will produce batches whose total size in bytes
/// is approximately this value, overriding the row-based `batch_size`.
///
/// This can be set at the dataset level (via `ReadParams::file_reader_options`)
/// to provide a default for all scans, or at the scanner level (via
/// `Scanner::batch_size_bytes`) to override per scan.
pub batch_size_bytes: Option<u64>,
}

impl Default for FileReaderOptions {
fn default() -> Self {
Self {
decoder_config: DecoderConfig::default(),
read_chunk_size: DEFAULT_READ_CHUNK_SIZE,
batch_size_bytes: None,
}
}
}
Expand Down Expand Up @@ -871,6 +879,7 @@ impl FileReader {
projection: ReaderProjection,
filter: FilterExpression,
decoder_config: DecoderConfig,
batch_size_bytes: Option<u64>,
) -> Result<BoxStream<'static, ReadBatchTask>> {
debug!(
"Reading range {:?} with batch_size {} from file with {} rows and {} columns into schema with {} columns",
Expand All @@ -887,7 +896,7 @@ impl FileReader {
decoder_plugins,
io,
decoder_config,
batch_size_bytes: None,
batch_size_bytes,
};

let requested_rows = RequestedRows::Ranges(vec![range]);
Expand Down Expand Up @@ -921,6 +930,7 @@ impl FileReader {
projection,
filter,
self.options.decoder_config.clone(),
self.options.batch_size_bytes,
)
}

Expand All @@ -935,6 +945,7 @@ impl FileReader {
projection: ReaderProjection,
filter: FilterExpression,
decoder_config: DecoderConfig,
batch_size_bytes: Option<u64>,
) -> Result<BoxStream<'static, ReadBatchTask>> {
debug!(
"Taking {} rows spread across range {}..{} with batch_size {} from columns {:?}",
Expand All @@ -951,7 +962,7 @@ impl FileReader {
decoder_plugins,
io,
decoder_config,
batch_size_bytes: None,
batch_size_bytes,
};

let requested_rows = RequestedRows::Indices(indices);
Expand Down Expand Up @@ -983,6 +994,7 @@ impl FileReader {
projection,
FilterExpression::no_filter(),
self.options.decoder_config.clone(),
self.options.batch_size_bytes,
)
}

Expand All @@ -997,6 +1009,7 @@ impl FileReader {
projection: ReaderProjection,
filter: FilterExpression,
decoder_config: DecoderConfig,
batch_size_bytes: Option<u64>,
) -> Result<BoxStream<'static, ReadBatchTask>> {
let num_rows = ranges.iter().map(|r| r.end - r.start).sum::<u64>();
debug!(
Expand All @@ -1015,7 +1028,7 @@ impl FileReader {
decoder_plugins,
io,
decoder_config,
batch_size_bytes: None,
batch_size_bytes,
};

let requested_rows = RequestedRows::Ranges(ranges);
Expand Down Expand Up @@ -1047,6 +1060,7 @@ impl FileReader {
projection,
filter,
self.options.decoder_config.clone(),
self.options.batch_size_bytes,
)
}

Expand Down Expand Up @@ -1194,7 +1208,7 @@ impl FileReader {
decoder_plugins: self.decoder_plugins.clone(),
io: self.scheduler.clone(),
decoder_config: self.options.decoder_config.clone(),
batch_size_bytes: None,
batch_size_bytes: self.options.batch_size_bytes,
};

let requested_rows = RequestedRows::Indices(indices);
Expand Down Expand Up @@ -1234,7 +1248,7 @@ impl FileReader {
decoder_plugins: self.decoder_plugins.clone(),
io: self.scheduler.clone(),
decoder_config: self.options.decoder_config.clone(),
batch_size_bytes: None,
batch_size_bytes: self.options.batch_size_bytes,
};

let requested_rows = RequestedRows::Ranges(ranges);
Expand Down Expand Up @@ -1274,7 +1288,7 @@ impl FileReader {
decoder_plugins: self.decoder_plugins.clone(),
io: self.scheduler.clone(),
decoder_config: self.options.decoder_config.clone(),
batch_size_bytes: None,
batch_size_bytes: self.options.batch_size_bytes,
};

let requested_rows = RequestedRows::Ranges(vec![range]);
Expand Down
3 changes: 3 additions & 0 deletions rust/lance/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ pub struct ReadParams {
/// File reader options to use when reading data files.
///
/// This allows control over features like caching repetition indices and validation.
/// Options set here act as dataset-level defaults and can be overridden on a
/// per-scan basis via [`Scanner::batch_size_bytes`](crate::dataset::scanner::Scanner::batch_size_bytes) or
/// [`Scanner::with_file_reader_options`](crate::dataset::scanner::Scanner::with_file_reader_options).
pub file_reader_options: Option<FileReaderOptions>,
}

Expand Down
58 changes: 53 additions & 5 deletions rust/lance/src/dataset/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,10 @@ pub struct Scanner {
/// The batch size controls the maximum size of rows to return for each read.
batch_size: Option<usize>,

/// If set, the scanner will produce batches whose total size in bytes
/// is approximately this value, overriding the row-based `batch_size`.
batch_size_bytes: Option<u64>,

/// Number of batches to prefetch
batch_readahead: usize,

Expand Down Expand Up @@ -989,6 +993,7 @@ impl Scanner {
filter: LanceFilter::default(),
full_text_query: None,
batch_size: None,
batch_size_bytes: None,
batch_readahead: get_num_compute_intensive_cpus(),
fragment_readahead: None,
io_buffer_size: None,
Expand Down Expand Up @@ -1261,12 +1266,29 @@ impl Scanner {
Ok(self)
}

/// Set the batch size.
/// Set the maximum number of rows per batch.
///
/// Note: this can be overridden by [`Self::batch_size_bytes`] or by a dataset-level
/// `batch_size_bytes` set via [`ReadParams::file_reader_options`](crate::dataset::ReadParams::file_reader_options). When a byte-based
/// batch size is active, the row-based batch size is used only as an initial estimate.
pub fn batch_size(&mut self, batch_size: usize) -> &mut Self {
self.batch_size = Some(batch_size);
self
}

/// Set the target batch size in bytes.
///
/// When set, the scanner will produce batches whose total size in bytes
/// is approximately this value, overriding the row-based `batch_size`.
///
/// This can also be configured at the dataset level via
/// [`ReadParams::file_reader_options`](crate::dataset::ReadParams::file_reader_options). A scanner-level setting takes
/// precedence over the dataset-level default.
pub fn batch_size_bytes(&mut self, batch_size_bytes: u64) -> &mut Self {
self.batch_size_bytes = Some(batch_size_bytes);
self
}

/// Include deleted rows
///
/// These are rows that have been deleted from the dataset but are still present in the
Expand Down Expand Up @@ -1688,6 +1710,30 @@ impl Scanner {
self
}

/// Compute the resolved file reader options, merging the scanner's explicit
/// `file_reader_options`, the dataset-level defaults, and the `batch_size_bytes`
/// setting.
fn resolved_file_reader_options(&self) -> Option<FileReaderOptions> {
let base = self
.file_reader_options
.clone()
.or_else(|| self.dataset.file_reader_options.clone());
match (base, self.batch_size_bytes) {
(Some(mut opts), Some(bsb)) => {
if opts.batch_size_bytes.is_none() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we always set opts.batch_size_bytes = Some(bsb) no matter whether opts.batch_size_bytes is None or not?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we set opts.batch_size_bytes then we will override batch_size.

I don't want to change the default (yet) so that this can stay a non-breaking change. The default is that the user doesn't set batch_size_bytes on either the scanner settings or the dataset settings and so we use batch_size instead.

opts.batch_size_bytes = Some(bsb);
}
Some(opts)
}
(Some(opts), None) => Some(opts),
(None, Some(bsb)) => Some(FileReaderOptions {
batch_size_bytes: Some(bsb),
..Default::default()
}),
(None, None) => None,
}
}

/// Create a physical expression for a column that may be nested
fn create_column_expr(
column_name: &str,
Expand Down Expand Up @@ -2658,6 +2704,10 @@ impl Scanner {
read_options = read_options.with_batch_size(batch_size as u32);
}

if let Some(file_reader_options) = self.resolved_file_reader_options() {
read_options = read_options.with_file_reader_options(file_reader_options);
}

if let Some(fragment_readahead) = self.fragment_readahead {
read_options = read_options.with_fragment_readahead(fragment_readahead);
}
Expand Down Expand Up @@ -4003,6 +4053,7 @@ impl Scanner {
with_row_created_at_version,
with_make_deletions_null,
ordered_output: ordered,
file_reader_options: self.resolved_file_reader_options(),
};
Arc::new(LanceScanExec::new(
self.dataset.clone(),
Expand All @@ -4029,10 +4080,7 @@ impl Scanner {
with_row_address: self.projection_plan.physical_projection.with_row_addr,
make_deletions_null,
ordered_output: self.ordered,
file_reader_options: self
.file_reader_options
.clone()
.or_else(|| self.dataset.file_reader_options.clone()),
file_reader_options: self.resolved_file_reader_options(),
};

let fragments = if let Some(fragment) = self.fragments.as_ref() {
Expand Down
Loading
Loading