Skip to main content

vortex_scan/execution/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! The planners of a V2 scan, and the prepared plans they run.
5//!
6//! A [`PreparedScan`] holds a scan's plans over one file. An engine composes the planners over it
7//! and drives them: a [`PrunePlanner`] evaluates the filter's pruning plans over the scan's rows and
8//! emits the planner it was built to continue into, with the rows they keep; a [`ChunkPlanner`]
9//! cuts those rows into chunks and spawns a `FilterPlanner` or a `ProjectionPlanner` per
10//! chunk; a filter planner runs its chunk's conjuncts and emits a projection planner for the rows
11//! that survive; a projection planner emits one morsel per piece of its rows. A planner only
12//! says what it needs, compute or reads; where either runs is the engine's driver's decision.
13
14pub mod cache;
15pub mod driver;
16pub mod filter_expr;
17pub(crate) mod io;
18pub mod operator;
19pub(crate) mod operators;
20pub mod pipeline;
21pub mod planning;
22mod retain;
23pub(crate) mod zone_map;
24
25mod adaptor;
26mod chunk;
27mod filter;
28mod projection;
29mod prune;
30mod selection;
31
32use std::ops::Range;
33use std::sync::Arc;
34
35use vortex_io::request::IoService;
36use vortex_io::request::IoTarget;
37pub use vortex_io::segments::SegmentLocation;
38use vortex_mask::Mask;
39use vortex_session::VortexSession;
40
41pub use self::chunk::ChunkPlanner;
42pub(crate) use self::chunk::FILTERED_ROWS_PER_CHUNK;
43pub(crate) use self::chunk::MAX_ROWS_PER_CHUNK;
44pub(crate) use self::chunk::ROW_BLOCK_SIZE;
45#[cfg(test)]
46pub(crate) use self::chunk::row_chunks;
47pub(crate) use self::chunk::rows_per_chunk;
48#[cfg(test)]
49pub(crate) use self::chunk::scan_chunks;
50use self::filter::FilterPlanner;
51pub use self::filter::FilterPlans;
52use self::projection::ProjectionPlanner;
53pub use self::prune::PruneNext;
54pub use self::prune::PrunePlanner;
55use crate::execution::cache::ScanCache;
56use crate::execution::io::SegmentRanges;
57use crate::execution::io::SegmentScanIo;
58use crate::execution::pipeline::ValueCache;
59use crate::plan::PlanRef;
60use crate::planning::planner::Planner;
61use crate::planning::planner::WorkScope;
62use crate::selection::Selection;
63use crate::source::SegmentSource;
64
65/// The byte range a read of the segment at `location` asks for.
66fn segment_target(location: &SegmentLocation) -> IoTarget {
67    IoTarget::Range {
68        offset: location.offset,
69        len: location.length as usize,
70        alignment: location.alignment,
71    }
72}
73
74/// A scan's plans over one file, built once from the file's footer and read by every planner of
75/// the scan. Nothing here touches a segment, and no planner plans again.
76pub struct PreparedScan {
77    /// The session used for decoding and expression evaluation.
78    pub(crate) session: VortexSession,
79    /// The rows of the root row domain the scan covers.
80    pub(crate) rows: Range<u64>,
81    /// The rows of `rows` the scan selects.
82    pub(crate) selection: Selection,
83    /// Fetches segment bytes by id.
84    pub(crate) segments: Arc<dyn SegmentSource>,
85    /// The segments' byte ranges, back to their ids.
86    pub(crate) ranges: SegmentRanges,
87    /// Held for the scan's life, so scans of the file that start meanwhile share it.
88    #[expect(dead_code, reason = "kept alive, never read")]
89    pub(crate) cache: ScanCache,
90    /// The location of every segment, indexed by segment id.
91    pub(crate) locations: Arc<[SegmentLocation]>,
92    /// The optimized projection plan, over the scan's root row domain.
93    pub(crate) projection: PlanRef,
94    /// The filter's conjuncts, their plans and their pruning plans.
95    pub(crate) filter: Option<Arc<FilterPlans>>,
96    /// The global row index of the root row domain's first row.
97    pub(crate) row_offset: u64,
98    /// Whole values, such as dictionary values, shared by the chunks of this scan. Their plans
99    /// always read the same complete row domain, independently of a chunk's selection.
100    pub(crate) values: ValueCache,
101    /// The rows of one chunk, the unit of work a chunk planner emits.
102    pub(crate) chunk_rows: usize,
103}
104
105impl PreparedScan {
106    /// The rows of the file's row domain the scan covers.
107    pub fn rows(&self) -> &Range<u64> {
108        &self.rows
109    }
110
111    /// The rows of [`rows`](Self::rows) the scan selects.
112    pub fn selection(&self) -> &Selection {
113        &self.selection
114    }
115
116    /// The filter's plans, when the scan has a filter.
117    pub fn filter(&self) -> Option<&FilterPlans> {
118        self.filter.as_deref()
119    }
120
121    /// The service a driver takes IO sessions from: every read a planner of the scan requests is
122    /// served from it by segment.
123    pub fn io(&self) -> Arc<dyn IoService> {
124        Arc::new(SegmentScanIo::new(
125            Arc::clone(&self.segments),
126            Arc::clone(&self.ranges),
127        ))
128    }
129
130    /// The planner of one chunk's selected rows: its conjuncts when the scan has a filter, else
131    /// its projection.
132    fn split_planner(self: &Arc<Self>, selected: SelectedRows) -> Box<dyn Planner> {
133        if self.filter.is_some() {
134            Box::new(FilterPlanner::new(Arc::clone(self), selected))
135        } else {
136            Box::new(ProjectionPlanner::new(Arc::clone(self), selected))
137        }
138    }
139}
140
141/// The rows a planner works on: a chunk, and which of its rows are selected.
142struct SelectedRows {
143    /// The chunk the rows belong to.
144    scope: WorkScope,
145    /// Which rows of `scope.rows` are selected.
146    mask: Mask,
147}
148
149#[cfg(test)]
150mod tests;