From 8c50bcffba13740439e569127263e49c1c043822 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Fri, 20 Nov 2026 16:46:59 -0500 Subject: [PATCH] Move the V2 part planner into vortex-scan Every host that reads a prepared V2 scan in parts should cut it the same way, so DuckDB's part planner becomes vortex_scan::prepare::part_root and DuckDB calls it. The code moves unchanged. Signed-off-by: Nicholas Gates Co-Authored-By: Claude Fable 5.2 --- vortex-duckdb/src/scan_v2.rs | 159 +-------------------------- vortex-scan/src/execution/mod.rs | 2 + vortex-scan/src/execution/parts.rs | 170 +++++++++++++++++++++++++++++ vortex-scan/src/prepare.rs | 1 + 4 files changed, 175 insertions(+), 157 deletions(-) create mode 100644 vortex-scan/src/execution/parts.rs diff --git a/vortex-duckdb/src/scan_v2.rs b/vortex-duckdb/src/scan_v2.rs index 4c778d14acf..cc8fc4305d5 100644 --- a/vortex-duckdb/src/scan_v2.rs +++ b/vortex-duckdb/src/scan_v2.rs @@ -14,7 +14,6 @@ use std::collections::VecDeque; use std::future; -use std::ops::Range; use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicU64; @@ -22,25 +21,16 @@ use std::sync::atomic::Ordering; use parking_lot::Mutex; use vortex::array::ArrayRef; -use vortex::error::VortexExpect; use vortex::error::VortexResult; -use vortex::io::request::IoConsumer; -use vortex::io::request::IoRequestId; -use vortex::io::request::IoResult; use vortex::io::request::IoService; use vortex::io::runtime::BlockingRuntime as _; use vortex::layout::scan::v2::ScanBuilder; -use vortex::mask::Mask; use vortex::scan::planning::driver::Progress; use vortex::scan::planning::driver::Run; use vortex::scan::planning::planner::Planner; -use vortex::scan::planning::planner::PlannerOutput; -use vortex::scan::planning::planner::State; use vortex::scan::planning::planner::WorkScope; -use vortex::scan::prepare::ChunkPlanner; use vortex::scan::prepare::PreparedScan; -use vortex::scan::prepare::PrunePlanner; -use vortex::scan::selection::Selection; +use vortex::scan::prepare::part_root; use crate::RUNTIME; @@ -89,7 +79,7 @@ impl FileScan { let rows = scan.rows().clone(); let mut root = Run::new(); root.admit( - compose(&scan, splits, dynamic), + part_root(&scan, splits, dynamic), WorkScope { file_ordinal: 0, rows, @@ -174,148 +164,3 @@ impl FileScan { Some(work) } } - -/// The planners of a scan, composed: with a dynamic filter, its pruning plans first over the scan's -/// rows, continuing into the parts of the rows they keep; otherwise, the parts directly. -fn compose(scan: &Arc, splits: &[u64], dynamic: bool) -> Box { - let rows = scan.rows().clone(); - let parts = { - let scan = Arc::clone(scan); - let parts = part_ranges(rows.clone(), splits); - let first_row = rows.start; - move |kept: Option| -> Box { - Box::new(PartPlanner::new(scan, parts, first_row, kept)) - } - }; - if dynamic && scan.filter().is_some() { - Box::new(PrunePlanner::new(Arc::clone(scan), rows, Box::new(parts))) - } else { - parts(None) - } -} - -/// Spawns a planner per part of a scan's rows that zone pruning left something of, in row order. -/// Parts are the file's layout splits, so a layout chunk, and the dictionary it carries, is -/// exported by one thread, which lets DuckDB evaluate an expression over that dictionary once. -/// -/// With a filter, each part prunes over its own rows before its chunks, on the thread that takes -/// it, like the per-split pruning of a V1 scan, so the pruning of a file's parts runs in parallel. -/// A dynamic filter, such as the bound a top-N pushes down, also prunes the scan as a whole before -/// any part is taken, so a file whose zones it rules out takes no thread. It has tightened by the -/// time a later part is taken, so pruning that part again skips more, from the zone maps the -/// scan-wide pruning read. -struct PartPlanner { - scan: Arc, - selection: Selection, - parts: std::vec::IntoIter>, - /// The first row of `kept`. - first_row: u64, - /// The rows scan-wide pruning kept, from `first_row`, or `None` when nothing was pruned. - kept: Option, -} - -impl PartPlanner { - fn new( - scan: Arc, - parts: Vec>, - first_row: u64, - kept: Option, - ) -> Self { - Self { - selection: scan.selection().clone(), - scan, - parts: parts.into_iter(), - first_row, - kept, - } - } - - /// The rows of `part` that scan-wide pruning kept, or `None` when nothing was pruned. - fn kept_in(&self, part: &Range) -> Option { - let kept = self.kept.as_ref()?; - let offset = - |row: u64| usize::try_from(row - self.first_row).vortex_expect("a part fits usize"); - Some(kept.slice(offset(part.start)..offset(part.end))) - } -} - -impl IoConsumer for PartPlanner { - fn set_io_result(&mut self, _request: IoRequestId, _result: IoResult) {} -} - -impl Planner for PartPlanner { - fn state(&self) -> State { - if self.parts.as_slice().is_empty() { - State::Done - } else { - State::NeedsCompute - } - } - - fn compute(&mut self) -> VortexResult { - while let Some(part) = self.parts.next() { - let kept = self.kept_in(&part); - if kept.as_ref().is_some_and(Mask::all_false) { - continue; - } - let chunks = { - let scan = Arc::clone(&self.scan); - let selection = self.selection.clone(); - let part = part.clone(); - move |again: Option| -> Box { - let kept = match (kept, again) { - (Some(kept), Some(again)) => Some(kept & &again), - (kept, again) => kept.or(again), - }; - Box::new(ChunkPlanner::new(scan, part, selection, kept)) - } - }; - let planner: Box = if self.scan.filter().is_some() { - Box::new(PrunePlanner::new( - Arc::clone(&self.scan), - part.clone(), - Box::new(chunks), - )) - } else { - chunks(None) - }; - let scope = WorkScope { - file_ordinal: 0, - rows: part, - }; - return Ok(PlannerOutput::Spawn(scope, planner)); - } - Ok(PlannerOutput::Done) - } -} - -/// The ranges between adjacent `splits`, clipped to `rows`, leaving out empty ones. -fn part_ranges(rows: Range, splits: &[u64]) -> Vec> { - splits - .windows(2) - .map(|w| w[0].max(rows.start)..w[1].min(rows.end)) - .filter(|part| part.start < part.end) - .collect() -} - -#[cfg(test)] -mod tests { - use std::ops::Range; - - use rstest::rstest; - - use super::part_ranges; - - #[rstest] - #[case::whole(0..30, &[0, 10, 20, 30], vec![0..10, 10..20, 20..30])] - #[case::clipped(5..25, &[0, 10, 20, 30], vec![5..10, 10..20, 20..25])] - #[case::inside_one_split(12..18, &[0, 10, 20, 30], vec![12..18])] - #[case::empty(5..5, &[0, 10], vec![])] - fn parts_follow_splits( - #[case] rows: Range, - #[case] splits: &[u64], - #[case] expected: Vec>, - ) { - assert_eq!(part_ranges(rows, splits), expected); - } -} diff --git a/vortex-scan/src/execution/mod.rs b/vortex-scan/src/execution/mod.rs index caa6a7ce099..577c1058e84 100644 --- a/vortex-scan/src/execution/mod.rs +++ b/vortex-scan/src/execution/mod.rs @@ -25,6 +25,7 @@ pub(crate) mod zone_map; mod adaptor; mod chunk; mod filter; +mod parts; mod projection; mod prune; mod selection; @@ -49,6 +50,7 @@ pub(crate) use self::chunk::rows_per_chunk; pub(crate) use self::chunk::scan_chunks; use self::filter::FilterPlanner; pub use self::filter::FilterPlans; +pub use self::parts::part_root; use self::projection::ProjectionPlanner; pub use self::prune::PruneNext; pub use self::prune::PrunePlanner; diff --git a/vortex-scan/src/execution/parts.rs b/vortex-scan/src/execution/parts.rs new file mode 100644 index 00000000000..606167b6878 --- /dev/null +++ b/vortex-scan/src/execution/parts.rs @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! Cutting a prepared scan into parts, the units a host's threads take. + +use std::ops::Range; +use std::sync::Arc; + +use vortex_error::VortexExpect; +use vortex_error::VortexResult; +use vortex_io::request::IoConsumer; +use vortex_io::request::IoRequestId; +use vortex_io::request::IoResult; +use vortex_mask::Mask; + +use super::ChunkPlanner; +use super::PreparedScan; +use super::PrunePlanner; +use crate::planning::planner::Planner; +use crate::planning::planner::PlannerOutput; +use crate::planning::planner::State; +use crate::planning::planner::WorkScope; +use crate::selection::Selection; + +/// The root planner of a scan cut into parts on `splits`, such as a file's layout split +/// boundaries: it spawns one planner per part, in row order. With a dynamic filter, its pruning +/// plans first over the scan's rows, continuing into the parts of the rows they keep; otherwise, +/// the parts directly. +pub fn part_root(scan: &Arc, splits: &[u64], dynamic: bool) -> Box { + let rows = scan.rows().clone(); + let parts = { + let scan = Arc::clone(scan); + let parts = part_ranges(rows.clone(), splits); + let first_row = rows.start; + move |kept: Option| -> Box { + Box::new(PartPlanner::new(scan, parts, first_row, kept)) + } + }; + if dynamic && scan.filter().is_some() { + Box::new(PrunePlanner::new(Arc::clone(scan), rows, Box::new(parts))) + } else { + parts(None) + } +} + +/// Spawns a planner per part of a scan's rows that zone pruning left something of, in row order. +/// Parts are the file's layout splits, so a layout chunk, and the dictionary it carries, is +/// read by one thread, which lets a host evaluate an expression over that dictionary once. +/// +/// With a filter, each part prunes over its own rows before its chunks, on the thread that takes +/// it, like the per-split pruning of a V1 scan, so the pruning of a file's parts runs in parallel. +/// A dynamic filter, such as the bound a top-N pushes down, also prunes the scan as a whole before +/// any part is taken, so a file whose zones it rules out takes no thread. It has tightened by the +/// time a later part is taken, so pruning that part again skips more, from the zone maps the +/// scan-wide pruning read. +struct PartPlanner { + scan: Arc, + selection: Selection, + parts: std::vec::IntoIter>, + /// The first row of `kept`. + first_row: u64, + /// The rows scan-wide pruning kept, from `first_row`, or `None` when nothing was pruned. + kept: Option, +} + +impl PartPlanner { + fn new( + scan: Arc, + parts: Vec>, + first_row: u64, + kept: Option, + ) -> Self { + Self { + selection: scan.selection().clone(), + scan, + parts: parts.into_iter(), + first_row, + kept, + } + } + + /// The rows of `part` that scan-wide pruning kept, or `None` when nothing was pruned. + fn kept_in(&self, part: &Range) -> Option { + let kept = self.kept.as_ref()?; + let offset = + |row: u64| usize::try_from(row - self.first_row).vortex_expect("a part fits usize"); + Some(kept.slice(offset(part.start)..offset(part.end))) + } +} + +impl IoConsumer for PartPlanner { + fn set_io_result(&mut self, _request: IoRequestId, _result: IoResult) {} +} + +impl Planner for PartPlanner { + fn state(&self) -> State { + if self.parts.as_slice().is_empty() { + State::Done + } else { + State::NeedsCompute + } + } + + fn compute(&mut self) -> VortexResult { + while let Some(part) = self.parts.next() { + let kept = self.kept_in(&part); + if kept.as_ref().is_some_and(Mask::all_false) { + continue; + } + let chunks = { + let scan = Arc::clone(&self.scan); + let selection = self.selection.clone(); + let part = part.clone(); + move |again: Option| -> Box { + let kept = match (kept, again) { + (Some(kept), Some(again)) => Some(kept & &again), + (kept, again) => kept.or(again), + }; + Box::new(ChunkPlanner::new(scan, part, selection, kept)) + } + }; + let planner: Box = if self.scan.filter().is_some() { + Box::new(PrunePlanner::new( + Arc::clone(&self.scan), + part.clone(), + Box::new(chunks), + )) + } else { + chunks(None) + }; + let scope = WorkScope { + file_ordinal: 0, + rows: part, + }; + return Ok(PlannerOutput::Spawn(scope, planner)); + } + Ok(PlannerOutput::Done) + } +} + +/// The ranges between adjacent `splits`, clipped to `rows`, leaving out empty ones. +fn part_ranges(rows: Range, splits: &[u64]) -> Vec> { + splits + .windows(2) + .map(|w| w[0].max(rows.start)..w[1].min(rows.end)) + .filter(|part| part.start < part.end) + .collect() +} + +#[cfg(test)] +mod tests { + use std::ops::Range; + + use rstest::rstest; + + use super::part_ranges; + + #[rstest] + #[case::whole(0..30, &[0, 10, 20, 30], vec![0..10, 10..20, 20..30])] + #[case::clipped(5..25, &[0, 10, 20, 30], vec![5..10, 10..20, 20..25])] + #[case::inside_one_split(12..18, &[0, 10, 20, 30], vec![12..18])] + #[case::empty(5..5, &[0, 10], vec![])] + fn parts_follow_splits( + #[case] rows: Range, + #[case] splits: &[u64], + #[case] expected: Vec>, + ) { + assert_eq!(part_ranges(rows, splits), expected); + } +} diff --git a/vortex-scan/src/prepare.rs b/vortex-scan/src/prepare.rs index 62de742a81b..625f614f7d9 100644 --- a/vortex-scan/src/prepare.rs +++ b/vortex-scan/src/prepare.rs @@ -24,6 +24,7 @@ pub use crate::execution::SegmentLocation; pub use crate::execution::cache::ScanCache; pub use crate::execution::cache::WeakScanCache; use crate::execution::io::segment_ranges; +pub use crate::execution::part_root; use crate::execution::rows_per_chunk; use crate::plan::optimize; use crate::plan::plan_row_idx_expression;