Skip to content

Commit 2bc5703

Browse files
committed
perf(duckdb): avoid redundant legacy layout pruning in V2 scans
Retain the opened VortexFile and evaluate a dtype-checked file-statistics proof before V2 execution. V2 continues to evaluate its own zone and row predicates; V1 keeps its existing layout-reader pruning path. Cover bound predicate schema validation and retained versus excluded files. Signed-off-by: Joe Isaacs <joe.isaacs@live.co.uk>
1 parent da10665 commit 2bc5703

5 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎vortex-duckdb/src/e2e_test/vortex_scan_test.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,8 @@ fn query_i64_column(path: &str, query: &str) -> Result<Vec<i64>> {
14031403

14041404
#[rstest]
14051405
#[case::filtered_count("SELECT count(*) FROM ? WHERE b < 100")]
1406+
#[case::empty_count("SELECT count(*) FROM ? WHERE b > 5000")]
1407+
#[case::constant_projection("SELECT 1::BIGINT FROM ? WHERE b = 42")]
14061408
#[case::filtered_max("SELECT max(a) FROM ? WHERE b BETWEEN 300 AND 310")]
14071409
#[case::filtered_projection("SELECT a FROM ? WHERE b = 42 ORDER BY a")]
14081410
#[case::expression("SELECT a + b FROM ? WHERE a >= 50000 ORDER BY 1")]

‎vortex-duckdb/src/file_reader.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use vortex::error::VortexResult;
2121
use vortex::error::vortex_err;
2222
use vortex::error::vortex_panic;
2323
use vortex::file::Footer;
24+
use vortex::file::VortexFile;
2425
use vortex::file::multi::MultiFileSession;
2526
use vortex::file::multi::open_cached;
2627
use vortex::file::multi::parse_uri_or_path;
@@ -126,6 +127,7 @@ pub struct OpenFileReader {
126127

127128
/// The state of an open file that the threads scanning it share.
128129
pub struct FileScanState {
130+
file: VortexFile,
129131
reader: LayoutReaderRef,
130132
/// The file as the V2 executor reads it.
131133
scan_file: ScanFile,
@@ -146,6 +148,7 @@ impl OpenFileReader {
146148
scan_state: Some(Arc::new(FileScanState {
147149
reader: file.layout_reader()?,
148150
scan_file,
151+
file,
149152
v2: OnceLock::new(),
150153
cache: ConversionCache::default(),
151154
})),
@@ -164,10 +167,13 @@ impl OpenFileReader {
164167
&self.scan_state().reader
165168
}
166169

167-
fn can_skip(&self, filter: &Filter) -> VortexResult<bool> {
170+
fn can_skip(&self, filter: &Filter, layout_pruning: bool) -> VortexResult<bool> {
168171
let Some(filter) = &filter.filter else {
169172
return Ok(false);
170173
};
174+
if !layout_pruning {
175+
return self.scan_state().file.can_prune_bound(filter);
176+
}
171177
let row_count = self.reader().row_count();
172178
let row_range = 0..row_count;
173179
let mask = Mask::new_true(usize::try_from(row_count).unwrap_or(usize::MAX));
@@ -212,7 +218,7 @@ pub fn reader_bind(file: &OpenFileReader, result: &mut BindResultRef) -> VortexR
212218
/// whether the opened file should be skipped. If this function returns false,
213219
/// duckdb closes the file and doesn't call reader_try_initialize_scan on it.
214220
pub fn reader_initialize(file: &mut OpenFileReader, global: &GlobalState) -> VortexResult<bool> {
215-
if file.can_skip(&global.filter)? {
221+
if file.can_skip(&global.filter, global.layout_pruning)? {
216222
// DuckDB releases a skipped file under the global lock. Here only the
217223
// file-local lock is held, so drop the scan state now.
218224
file.scan_state = None;

‎vortex-duckdb/src/table_function.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use vortex::error::vortex_ensure;
3636
use vortex::error::vortex_err;
3737
use vortex::expr::BoundExpression;
3838
use vortex::expr::Expression;
39+
use vortex::expr::analysis::referenced_field_paths;
3940
use vortex::expr::col;
4041
use vortex::expr::proto::ExprSerializeProtoExt;
4142
#[cfg(unix)]
@@ -187,6 +188,8 @@ pub struct GlobalState {
187188
pub file_row_number_column_pos: Option<usize>,
188189
/// Whether the scan runs on the planner-driven V2 executor.
189190
pub scan_v2: bool,
191+
/// Whether initialization probes the legacy layout pruning path.
192+
pub layout_pruning: bool,
190193

191194
// Following fields are used only in aggregate scans.
192195
pub aggregate_state: Mutex<AggregateState>,
@@ -353,6 +356,10 @@ pub fn init_global(init_input: &TableInitInput) -> VortexResult<GlobalState> {
353356
);
354357

355358
let projection = optimize_and_bind(projection, &bind_data.dtype)?;
359+
let scan_v2 = crate::scan_v2();
360+
// For projections without input fields, the layout probe starts zone reads before V2
361+
// planning. Keep that overlap for count-only and constant-projection scans.
362+
let layout_pruning = !scan_v2 || referenced_field_paths(&projection)?.iter().next().is_none();
356363
Ok(GlobalState {
357364
projection,
358365
filter,
@@ -361,7 +368,8 @@ pub fn init_global(init_input: &TableInitInput) -> VortexResult<GlobalState> {
361368
aggregate_state: Mutex::new(AggregateState::default()),
362369
row_count: AtomicU64::new(0),
363370
file_row_number_column_pos,
364-
scan_v2: crate::scan_v2(),
371+
scan_v2,
372+
layout_pruning,
365373
})
366374
}
367375

‎vortex-file/src/file.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ use itertools::Itertools;
1414
use vortex_array::ArrayRef;
1515
use vortex_array::dtype::DType;
1616
use vortex_array::dtype::FieldMask;
17+
use vortex_array::expr::BoundExpression;
1718
use vortex_array::expr::Expression;
1819
use vortex_buffer::ByteBuffer;
1920
use vortex_error::VortexResult;
21+
use vortex_error::vortex_ensure;
2022
use vortex_layout::LayoutReader;
2123
use vortex_layout::scan::layout::LayoutReaderDataSource;
2224
use vortex_layout::scan::scan_builder::ScanBuilder;
@@ -294,6 +296,19 @@ impl VortexFile {
294296
)
295297
}
296298

299+
/// Returns whether file statistics rule out a predicate already bound to this file's dtype.
300+
/// This does not construct a layout reader or read data segments.
301+
pub fn can_prune_bound(&self, filter: &BoundExpression) -> VortexResult<bool> {
302+
vortex_ensure!(
303+
filter.is_root_bound_to(self.dtype()),
304+
"File pruning predicate is bound to a different dtype"
305+
);
306+
let Some(stats) = self.footer.statistics() else {
307+
return Ok(false);
308+
};
309+
is_falsified_by_file_stats(filter, self.row_count(), stats, &self.session)
310+
}
311+
297312
/// Return the file's natural row splits as root-coordinate ranges.
298313
///
299314
/// These are the ranges that the default [`SplitBy`] would use for an all-fields scan.

‎vortex-file/src/tests.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,6 +3324,16 @@ async fn test_can_prune_composite_predicates() -> VortexResult<()> {
33243324

33253325
// Bare comparisons: falsified directly by min/max stats.
33263326
assert!(file.can_prune(&gt(col("age"), lit(30)))?);
3327+
assert!(file.can_prune_bound(&gt(col("age"), lit(30)).bind(file.dtype())?)?);
3328+
assert!(!file.can_prune_bound(&gt(col("age"), lit(20)).bind(file.dtype())?)?);
3329+
let other_dtype = DType::struct_(
3330+
[("age", DType::Primitive(I32, Nullability::NonNullable))],
3331+
Nullability::NonNullable,
3332+
);
3333+
assert!(
3334+
file.can_prune_bound(&gt(col("age"), lit(30)).bind(&other_dtype)?)
3335+
.is_err()
3336+
);
33273337
assert!(file.can_prune(&lt(col("price"), lit(100)))?);
33283338

33293339
// Composite predicates whose falsifications are boolean trees.

0 commit comments

Comments
 (0)