Skip to content
Closed
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
2 changes: 2 additions & 0 deletions vortex-datafusion/src/v2/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
use std::any::Any;
use std::fmt;
use std::fmt::Formatter;
use std::num::NonZeroUsize;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
Expand Down Expand Up @@ -498,6 +499,7 @@ impl VortexDataSource {
filter: self.filter.clone(),
limit: self.limit.map(|l| u64::try_from(l).unwrap_or(u64::MAX)),
ordered: self.ordered,
target_partitions: NonZeroUsize::new(self.target_parallelism),
..Default::default()
};
let data_source = Arc::clone(&self.data_source);
Expand Down
1 change: 1 addition & 0 deletions vortex-ffi/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ fn scan_request(opts: *const vx_scan_options) -> VortexResult<ScanRequest> {
partition_selection: Selection::All,
partition_range: None,
split_concurrency: None,
target_partitions: None,
})
}

Expand Down
3 changes: 1 addition & 2 deletions vortex-file/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ impl VortexFile {
/// Create a [`DataSource`](vortex_scan::DataSource) from this file whose scans run on the
/// planner-driven executor.
///
/// A scan has one partition covering the requested rows, which runs it with
/// [`ScanDriver`](crate::v2::ScanDriver) from
/// Each partition of a scan, a range of the file's rows, runs it with [`ScanDriver`](crate::v2::ScanDriver) from
/// [`Partition::execute`](vortex_scan::Partition::execute). Every scan of the source shares
/// one [`ScanCache`]: the file's, with [caching](Self::with_caching), or else one of its own.
/// A scan whose filter the file-level statistics prove false has no partitions. Stored footer
Expand Down
1 change: 1 addition & 0 deletions vortex-jni/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ fn build_scan_request(
partition_selection: Selection::All,
partition_range: None,
split_concurrency,
target_partitions: None,
})
}

Expand Down
1 change: 1 addition & 0 deletions vortex-layout/src/scan/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ impl BoundScanRequest {
ordered,
limit,
split_concurrency,
target_partitions: _,
} = request;

Ok(Self {
Expand Down
55 changes: 39 additions & 16 deletions vortex-scan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ pub type DataSourceRef = Arc<dyn DataSource>;
/// The DataSource may be used multiple times to create multiple scans, whereas each scan and each
/// partition of a scan can only be consumed once.
///
/// Partitions have indices. Partition index is stable throughout DataSource's
/// lifetime. For every scan requested on the DataSource, the index will stay
/// the same.
/// However, this means you should create another instance of a DataSource if
/// your environment changes e.g. you have a glob and another file is added to
/// the filesystem this glob references.
/// See MultiFileDataSource in vortex-file/src/multi/mod.rs
/// Every partition has an [`index`](Partition::index) naming the unit of the source it reads,
/// which is what [`ScanRequest::partition_range`] and [`ScanRequest::partition_selection`]
/// select. A [`PlanDataSource`](source::PlanDataSource) is one unit with index 0, and a source
/// with several units, such as one over several files, gives each its own index. The index is
/// stable for the lifetime of the DataSource, so every scan of it gives a unit the same index. It
/// is not unique within a scan: a unit may be split into several partitions that share its index.
///
/// Because indices are stable, create another DataSource when the data it covers changes, for
/// example when a file is added to the directory that a glob names. See `MultiFileDataSource` in
/// `vortex-file`.
#[async_trait]
pub trait DataSource: 'static + Send + Sync {
/// Returns the dtype of the source.
Expand Down Expand Up @@ -156,15 +159,22 @@ pub struct ScanRequest {
pub projection: Expression,
/// Filter expression, `None` implies no filter.
pub filter: Option<Expression>,
/// The per-partition row range to read. Row range will be applied
/// over every partition you scan.
/// The range of rows to read from each selected unit, in that unit's own row numbering.
///
/// Units are what [`Partition::index`] names: for a source over several files, each file is a
/// unit and the range applies to each file's rows. A unit that a scan splits into several
/// partitions applies the range once across them, so each row is read by one partition.
pub row_range: Option<Range<u64>>,
/// The per-partition row selection to read. Row selection will be applied
/// over every partition you scan.
/// The rows to read from each selected unit, by row index in that unit's own row numbering.
///
/// The indices are not relative to [`row_range`](Self::row_range): a scan reads the rows that
/// are both in the row range and selected. Like the row range, the selection applies to every
/// selected unit, and once across the partitions of a unit that a scan splits.
pub selection: Selection,
/// Partition selection to scan, which allows readers to skip unwanted partitions.
/// The units to scan, by [`Partition::index`], which allows readers to skip unwanted units.
pub partition_selection: Selection,
/// Partition range to scan, which allows readers to skip unwanted partitions.
/// The range of units to scan, by [`Partition::index`], which allows readers to skip unwanted
/// units.
pub partition_range: Option<Range<u64>>,
/// Whether the scan should preserve row order. If false, the scan may produce rows in any
/// order, for example to enable parallel execution across partitions.
Expand All @@ -183,6 +193,14 @@ pub struct ScanRequest {
/// others, such as one task of several on a machine, can set this to bound its memory. It does
/// not bound I/O: segment reads for later splits may still be coalesced ahead of them.
pub split_concurrency: Option<NonZeroUsize>,
/// The number of partitions the caller would like the scan to produce, as a hint.
///
/// A source may return more or fewer partitions than this, for example because it has more
/// units than the hint or because a unit is too small to be worth splitting. `None` lets the
/// source choose: a [`PlanDataSource`](source::PlanDataSource) then uses the machine's
/// available parallelism. The hint only affects scans that a source is free to split, which
/// are unordered and have no limit. Defaults to `None`.
pub target_partitions: Option<NonZeroUsize>,
}

impl Default for ScanRequest {
Expand All @@ -197,6 +215,7 @@ impl Default for ScanRequest {
limit: None,
partition_range: None,
split_concurrency: None,
target_partitions: None,
}
}
}
Expand Down Expand Up @@ -224,9 +243,13 @@ pub trait Partition: 'static + Send {
/// Downcast the partition to a concrete type.
fn as_any(&self) -> &dyn Any;

/// Some unique identifier for partition.
/// If you have an instance of a DataSource, the indices of emitted
/// partitions will stay stable for every scan in this DataSource.
/// The index of the unit of the source this partition reads, which
/// [`ScanRequest::partition_range`] and [`ScanRequest::partition_selection`] select.
///
/// For a [`PlanDataSource`](source::PlanDataSource) it is 0, since the source is one unit; a
/// source with several units gives each its own index. The index is stable for the lifetime of
/// the [`DataSource`], but not unique within a scan: a unit may be split into several
/// partitions that share its index.
fn index(&self) -> usize;

/// Returns an estimate of the row count for this partition.
Expand Down
94 changes: 74 additions & 20 deletions vortex-scan/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use vortex_io::segments::SegmentId;
pub use vortex_io::segments::SegmentSource;
use vortex_io::session::RuntimeSessionExt;
use vortex_session::VortexSession;
use vortex_utils::parallelism::get_available_parallelism;

use crate::DataSource;
use crate::DataSourceScan;
Expand Down Expand Up @@ -70,12 +71,18 @@ pub struct ScanInput {
/// lower its data to plans. Every scan of the source shares the input's [`ScanCache`], so scans
/// that overlap in time plan the input and register its reader once.
///
/// A scan of the source has one partition covering the request's row range, clamped to the
/// input's rows when the row count is known. The partition has [`index`](Partition::index) 0, so a
/// request's `partition_range` and `partition_selection` either select it or leave the scan with
/// no partitions.
/// An unordered scan without a limit over a known row range is split into contiguous row ranges
/// of near-equal length, one partition each, so engines that parallelise by partition spread a
/// single input across their workers. The split count is the request's
/// [`target_partitions`](ScanRequest::target_partitions), or else the available parallelism,
/// capped at one partition per 65,536 rows, rounded up. Ordered and limited scans, and scans
/// whose row range is unknown, have one partition covering the request's row range.
///
/// The partition's [`execute`](Partition::execute) runs the planner-driven scan of its rows with a
/// The source is one selectable unit: every partition has [`index`](Partition::index) 0, so a
/// request's `partition_range` and `partition_selection` either select all of a scan's partitions
/// or none of them.
///
/// A partition's [`execute`](Partition::execute) runs the planner-driven scan of its rows with a
/// [`ScanDriver`] on the session's runtime and applies the request's limit.
#[derive(Clone)]
pub struct PlanDataSource {
Expand All @@ -85,6 +92,12 @@ pub struct PlanDataSource {
byte_size: Precision<u64>,
}

/// The fewest rows a split partition covers.
///
/// Each partition prepares, plans and drives its own scan, so splitting a small range costs more
/// in per-partition setup than it gains in parallelism.
const MIN_PARTITION_ROWS: u64 = 65_536;

impl PlanDataSource {
/// A source scanning `input` with `session`. Without a cache, the input gets a fresh one that
/// all of the source's scans share.
Expand Down Expand Up @@ -119,6 +132,38 @@ impl PlanDataSource {
pub fn session(&self) -> &VortexSession {
&self.session
}

/// The row ranges an unordered, unlimited scan of `row_range` splits into, aiming for
/// `target_partitions`, or `None` when the scan keeps one partition.
fn split_range(
&self,
row_range: Option<&Range<u64>>,
ordered: bool,
limit: Option<u64>,
target_partitions: Option<NonZeroUsize>,
) -> Option<Vec<Range<u64>>> {
if ordered || limit.is_some() {
return None;
}
let rows = row_range
.cloned()
.or_else(|| self.row_count.as_exact().map(|count| 0..count))?;
let target_partitions = target_partitions.map_or_else(
|| get_available_parallelism().unwrap_or(1),
NonZeroUsize::get,
);
let len = rows.end - rows.start;
let count = len
.div_ceil(MIN_PARTITION_ROWS)
.min(target_partitions as u64)
.max(1);
Some(
(0..count)
.map(|i| rows.start + len * i / count..rows.start + len * (i + 1) / count)
.filter(|range| !range.is_empty())
.collect(),
)
}
}

#[async_trait]
Expand Down Expand Up @@ -162,22 +207,31 @@ impl DataSource for PlanDataSource {
partitions: Vec::new(),
}));
}
let partition = PlanPartition {
session: self.session.clone(),
input: self.input.clone(),
row_count: self.row_count,
projection,
filter,
row_range,
selection: request.selection,
ordered: request.ordered,
limit: request.limit,
split_concurrency: request.split_concurrency,
let ranges = match self.split_range(
row_range.as_ref(),
request.ordered,
request.limit,
request.target_partitions,
) {
Some(ranges) => ranges.into_iter().map(Some).collect(),
None => vec![row_range],
};
Ok(Box::new(PlanScan {
dtype,
partitions: vec![partition],
}))
let partitions = ranges
.into_iter()
.map(|row_range| PlanPartition {
session: self.session.clone(),
input: self.input.clone(),
row_count: self.row_count,
projection: projection.clone(),
filter: filter.clone(),
row_range,
selection: request.selection.clone(),
ordered: request.ordered,
limit: request.limit,
split_concurrency: request.split_concurrency,
})
.collect();
Ok(Box::new(PlanScan { dtype, partitions }))
}

async fn field_statistics(&self, _field_path: &FieldPath) -> VortexResult<StatsSet> {
Expand Down
Loading