Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8626a3f
Give layout scans a native split and open
gatesn Nov 19, 2026
d300a84
Spawn pool splits onto the session runtime
gatesn Nov 19, 2026
f1bf5cf
Drive layout pool tests with a background runtime
gatesn Nov 19, 2026
d10623d
Run pool splits the way execute_stream runs them
gatesn Nov 20, 2026
c638db4
Pass a pool's open slot on when its worker drops
gatesn Nov 20, 2026
0cd40e4
Request shared segments without an exclusive lock
gatesn Nov 20, 2026
e716709
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
c638960
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
7843fbc
Share an unordered pool's output instead of its work
gatesn Nov 20, 2026
52a122f
Run an unordered pool's units on the lane that takes them
gatesn Nov 20, 2026
530b943
Recheck whole-file pruning when a pooled file's first split is taken
gatesn Nov 20, 2026
9996743
Start an unordered pool's lanes only as units are ready
gatesn Nov 20, 2026
235d539
Read an unordered pool's opened files in turn
gatesn Nov 20, 2026
3cd0419
Hand out an unordered pool's files in runs of adjacent splits
gatesn Nov 20, 2026
03bc34a
Decode a single-file pool's splits on every core
gatesn Nov 20, 2026
8f81f78
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
bf0f763
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
5d18c27
Read V1 layout scans through split and open on develop's pipeline
gatesn Nov 20, 2026
a68063c
Test group_contiguous against ranges grouped by their first row
gatesn Nov 20, 2026
b414761
Keep develop's splits in flight when opening a file, and split inside…
gatesn Nov 20, 2026
90edccc
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
1685330
Keep multi-file split to runs of whole files
gatesn Nov 20, 2026
dc8caa3
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
570b1c3
Drop the layout pools' unused size and progress methods
gatesn Nov 20, 2026
1d1d7d7
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
40d7b33
Drop dtype from the split pool
gatesn Nov 20, 2026
485b83e
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
17ddf34
Cut multi-file scans inside files when there are fewer files than pieces
gatesn Nov 20, 2026
0b40ba0
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
86065ee
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 20, 2026
665ce62
Merge branch 'ngates/scan-split-open' into ngates/scan-native-pool
gatesn Nov 20, 2026
426cb19
Say what LayoutReaderScan::builder builds
gatesn Nov 20, 2026
00011de
Read two files at once per clone of an unordered multi-file scan
gatesn Nov 20, 2026
db19654
Poll a worker's only split on the thread that reads it
gatesn Nov 20, 2026
1d3a129
Merge branch 'ngates/scan-split-open' into ngates/scan-native-pool
gatesn Nov 20, 2026
1c91d11
Cut layout scans into fixed pieces for every request
gatesn Nov 20, 2026
4fa8141
Describe the read-ahead without reference to split
gatesn Nov 20, 2026
2f2556f
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 24, 2026
a74d391
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 24, 2026
5d00179
Merge ngates/scan-split-open into ngates/scan-native-pool
gatesn Nov 24, 2026
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
244 changes: 241 additions & 3 deletions vortex-layout/src/scan/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use futures::FutureExt;
use futures::Stream;
use futures::stream;
use futures::stream::StreamExt;
use vortex_array::ArrayRef;
use vortex_array::IntoArray;
use vortex_array::arrays::ConstantArray;
use vortex_array::dtype::DType;
Expand All @@ -30,6 +31,7 @@ use vortex_array::stream::SendableArrayStream;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_err;
use vortex_io::session::RuntimeSessionExt;
use vortex_mask::Mask;
use vortex_metrics::MetricsRegistry;
use vortex_scan::DataSource;
Expand All @@ -40,14 +42,17 @@ use vortex_scan::PartitionRef;
use vortex_scan::PartitionStream;
use vortex_scan::ScanRequest;
use vortex_scan::selection::Selection;
use vortex_scan::stream::ExhaustedFlag;
use vortex_scan::stream::ScanExec;
use vortex_session::VortexSession;

use crate::LayoutReaderRef;
use crate::scan::limit::RowLimit;
use crate::scan::metrics::ScanMetrics;
use crate::scan::metrics::ScanRecorder;
use crate::scan::repeated_scan::DEFAULT_SPLITS_PER_THREAD;
use crate::scan::scan_builder::ScanBuilder;
use crate::scan::split_queue::SplitQueue;

/// Cursor over absolute split-grid partition indices for a [`LayoutReaderDataSource`].
///
Expand Down Expand Up @@ -128,6 +133,20 @@ impl PartitionCursor {
Some((partition_idx, split_start..split_end))
}

/// The rows the cursor has left, when they are a whole run of the grid: no partition range
/// or selection trims them. A scan cut into pieces or opened into a queue reads its rows
/// without the grid, so only such a scan can be.
fn whole_rows(&self) -> Option<Range<u64>> {
if !matches!(self.partition_selection, Selection::All) {
return None;
}
let split_size = self.split_size.get();
let start = self.next_partition.saturating_mul(split_size);
let end = self.end_partition.saturating_mul(split_size);
let covers_end = end >= self.row_range.end || self.row_range.start >= self.row_range.end;
(start <= self.row_range.start && covers_end).then(|| self.row_range.clone())
}

fn remaining_count(&self) -> u64 {
if self.next_partition >= self.end_partition {
return 0;
Expand Down Expand Up @@ -338,17 +357,96 @@ struct LayoutReaderScan {
cursor: PartitionCursor,
}

impl LayoutReaderScan {
/// A scan builder over this scan's reader, with its selection, projection, filter, metrics
/// and split concurrency. Callers give it the row range to read.
fn builder(&self) -> ScanBuilder<ArrayRef> {
ScanBuilder::new(self.session.clone(), Arc::clone(&self.reader))
.with_selection(self.selection.clone())
.with_projection(self.projection.clone())
.with_some_filter(self.filter.clone())
.with_some_scan_metrics(self.metrics.clone())
.with_some_max_splits_in_flight(self.split_concurrency)
}

/// This scan over `rows` only. Only a scan without a limit is cut into pieces, so a piece
/// has no limit to share.
fn piece(&self, rows: Range<u64>) -> Self {
Self {
reader: Arc::clone(&self.reader),
session: self.session.clone(),
dtype: self.dtype.clone(),
projection: self.projection.clone(),
filter: self.filter.clone(),
remaining_limit: None,
row_limit: None,
ordered: self.ordered,
split_concurrency: self.split_concurrency,
selection: self.selection.clone(),
metrics: self.metrics.clone(),
cursor: PartitionCursor::new(rows, self.cursor.split_size, None, Selection::All),
}
}
}

impl DataSourceScan for LayoutReaderScan {
fn dtype(&self) -> &DType {
&self.dtype
}

fn split(self: Box<Self>, _n: NonZeroUsize) -> VortexResult<Vec<DataSourceScanRef>> {
Ok(vec![self])
/// Cuts the scan at the layout's natural split boundaries, into contiguous pieces of about
/// equal rows.
///
/// A scan with a limit stays whole, because the limit is one budget across all its rows. So
/// does one that selects partitions, because its pieces would read rows outside them.
fn split(self: Box<Self>, n: NonZeroUsize) -> VortexResult<Vec<DataSourceScanRef>> {
let rows = match self.cursor.whole_rows() {
Some(rows) if n.get() > 1 && self.remaining_limit.is_none() && !rows.is_empty() => rows,
_ => return Ok(vec![self]),
};
let ranges = self.builder().prepare()?.split_ranges(Some(rows));
let pieces: Vec<Range<u64>> = group_contiguous(ranges, n)
.into_iter()
.filter_map(|group| Some(group.first()?.start..group.last()?.end))
.collect();
if pieces.len() <= 1 {
return Ok(vec![self]);
}
Ok(pieces
.into_iter()
.map(|rows| Box::new(self.piece(rows)) as DataSourceScanRef)
.collect())
}

/// Opens the scan into a [`SplitQueue`] of its split futures, each stream of it keeping the
/// stream pipeline's per-thread count of them in flight.
///
/// Split futures cannot share a limit, and the streams of a queue finish splits out of row
/// order. So a scan with a limit, or an ordered one, opens into its partition pipeline and
/// hands its partitions to the streams. So does one that selects partitions, which the queue
/// does not know.
fn open(self: Box<Self>) -> VortexResult<ScanExec> {
Ok(ScanExec::from_partitions(self))
let rows = match self.cursor.whole_rows() {
Some(rows) if self.remaining_limit.is_none() && !self.ordered => rows,
_ => return Ok(ScanExec::from_partitions(self)),
};
// A cap on the scan's splits in flight also caps each stream's.
let per_worker = self
.split_concurrency
.map_or(DEFAULT_SPLITS_PER_THREAD, |max| {
max.get().min(DEFAULT_SPLITS_PER_THREAD)
});
let splits = self.builder().prepare()?.execute(Some(rows))?;
let exhausted = ExhaustedFlag::new();
let queue = Arc::new(SplitQueue::new(
self.session.handle(),
per_worker,
splits,
exhausted.clone(),
));
Ok(ScanExec::new(self.dtype, exhausted, move || {
Arc::clone(&queue).worker()
}))
}

fn partition_count(&self) -> Precision<usize> {
Expand All @@ -365,6 +463,33 @@ impl DataSourceScan for LayoutReaderScan {
}
}

/// Groups consecutive `ranges` into at most `n` contiguous groups of about equal row counts.
///
/// Never cuts a range: each range lands whole in one group.
pub(crate) fn group_contiguous(ranges: Vec<Range<u64>>, n: NonZeroUsize) -> Vec<Vec<Range<u64>>> {
let total = ranges
.iter()
.map(|range| u128::from(range.end - range.start))
.sum::<u128>()
.max(1);
let n_groups = n.get() as u128;
let mut groups: Vec<Vec<Range<u64>>> = Vec::new();
let mut current_group = None;
let mut before = 0u128;
for range in ranges {
let group = (before * n_groups / total).min(n_groups - 1);
before += u128::from(range.end - range.start);
if current_group != Some(group) {
current_group = Some(group);
groups.push(Vec::new());
}
if let Some(last) = groups.last_mut() {
last.push(range);
}
}
groups
}

impl Stream for LayoutReaderScan {
type Item = VortexResult<PartitionRef>;

Expand Down Expand Up @@ -1081,3 +1206,116 @@ mod tests {
Ok(())
}
}

#[cfg(test)]
mod split_open_tests {
use std::num::NonZeroUsize;
use std::sync::Arc;

use futures::TryStreamExt;
use futures::future::try_join_all;
use vortex_error::VortexResult;
use vortex_io::runtime::BlockingRuntime;
use vortex_io::runtime::single::SingleThreadRuntime;
use vortex_scan::DataSource;
use vortex_scan::ScanRequest;

use super::LayoutReaderDataSource;
use super::group_contiguous;
use crate::scan::test::TestLayoutReader;
use crate::scan::test::collect_scan_values;
use crate::scan::test::session_with_handle;

fn source(runtime: &SingleThreadRuntime) -> LayoutReaderDataSource {
LayoutReaderDataSource::new(
Arc::new(TestLayoutReader::new(64).with_split_size(4)),
session_with_handle(runtime.handle()),
)
}

/// The streams of an opened scan each claim splits until none is left, so together they read
/// every row once, and the scan is exhausted once the last split is claimed.
#[test]
fn opened_streams_read_every_row_once() -> VortexResult<()> {
let runtime = SingleThreadRuntime::default();
let exec = runtime
.block_on(source(&runtime).scan(ScanRequest::default()))?
.open()?;
assert!(!exec.is_exhausted());
let workers = (0..3).map(|_| exec.stream().try_collect::<Vec<_>>());
let arrays = runtime.block_on(try_join_all(workers))?;
let mut values = collect_scan_values(arrays.into_iter().flatten().map(Ok))?;
values.sort_unstable();
assert_eq!(values, (0..64).collect::<Vec<_>>());
assert!(exec.is_exhausted());
Ok(())
}

/// The pieces of an unordered scan are fixed too: each owns a contiguous run of splits,
/// which it may return in any order.
#[test]
fn unordered_pieces_own_their_rows() -> VortexResult<()> {
let runtime = SingleThreadRuntime::default();
let scan = runtime.block_on(source(&runtime).scan(ScanRequest::default()))?;
let pieces = scan.split(NonZeroUsize::try_from(4)?)?;
assert_eq!(pieces.len(), 4);
for (i, piece) in (0i32..).zip(pieces) {
let mut values = collect_scan_values(runtime.block_on_stream(piece.open()?.stream()))?;
values.sort_unstable();
assert_eq!(values, (16 * i..16 * (i + 1)).collect::<Vec<_>>());
}
Ok(())
}

/// An ordered scan is cut at the layout's split boundaries, and the pieces read the rows in
/// order.
#[test]
fn ordered_split_cuts_at_split_boundaries() -> VortexResult<()> {
let runtime = SingleThreadRuntime::default();
let scan = runtime.block_on(source(&runtime).scan(ScanRequest {
ordered: true,
..Default::default()
}))?;
let pieces = scan.split(NonZeroUsize::try_from(4)?)?;
assert_eq!(pieces.len(), 4);
let mut values = Vec::new();
for piece in pieces {
values.extend(collect_scan_values(
runtime.block_on_stream(piece.open()?.stream()),
)?);
}
assert_eq!(values, (0..64).collect::<Vec<_>>());
Ok(())
}

/// A limit is one budget across the scan, so a limited scan stays whole and opens into its
/// partitions, which apply the limit.
#[test]
fn limited_scan_stays_whole() -> VortexResult<()> {
let runtime = SingleThreadRuntime::default();
let source = LayoutReaderDataSource::new(
Arc::new(TestLayoutReader::new(64).with_split_size(4)),
session_with_handle(runtime.handle()),
);
let request = || ScanRequest {
limit: Some(10),
..Default::default()
};
let scan = runtime.block_on(source.scan(request()))?;
assert_eq!(scan.split(NonZeroUsize::try_from(4)?)?.len(), 1);

let stream = runtime.block_on(source.scan(request()))?.open()?.stream();
let values = collect_scan_values(runtime.block_on_stream(stream))?;
assert_eq!(values.len(), 10);
Ok(())
}

#[test]
fn group_contiguous_keeps_ranges_whole() -> VortexResult<()> {
// Each range joins the group its first row falls in.
let ranges = vec![0..10, 10..16, 16..26, 26..32];
let groups = group_contiguous(ranges, NonZeroUsize::try_from(2)?);
assert_eq!(groups, vec![vec![0..10, 10..16], vec![16..26, 26..32]]);
Ok(())
}
}
1 change: 1 addition & 0 deletions vortex-layout/src/scan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod multi;
pub mod repeated_scan;
pub mod scan_builder;
pub mod split_by;
mod split_queue;
mod splits;
mod tasks;
#[cfg(test)]
Expand Down
Loading