Skip to content

Commit 641e6cc

Browse files
Dandandanclaude
andcommitted
perf(parquet): build row-filter candidates once per file, share to stealers
`build_row_filter` does two things at very different costs: 1. Walk the predicate, split conjuncts, build a `FilterCandidate` per conjunct (resolves ProjectionMask + projected schema + required-bytes estimate), and optionally reorder by cost. This is schema/metadata work that is identical for every open of the same file. 2. Bind each candidate to the current open's metrics counters. Before this change, both ran per open — so a 226-RG file split into 226 chunks paid the analysis cost 226×. After this change, the donor (or an un-split file open) builds the `Vec<FilterCandidate>` once in `prepare_filters`; donated chunks carry it through `ParquetOpenChunk`; each `build_stream` does only the cheap metric binding via the new `row_filter_from_candidates`. Refactor: - Split `row_filter::build_row_filter` into `build_row_filter_candidates` (expensive, metrics-free) and `row_filter_from_candidates` (cheap, per-open). `build_row_filter` becomes a thin wrapper. - `FilterCandidate` now `Clone`. - `FiltersPreparedParquetOpen` gains `row_filter_candidates: Option<Arc<Vec<FilterCandidate>>>`, built in `prepare_filters` from the donor's rewritten predicate. - `ParquetOpenChunk` carries the same `Arc` across the handoff so stealers reuse it in `build_stream`. - `build_stream` now calls `row_filter_from_candidates` on the cached vec instead of re-running the full builder. Correctness: each open still gets its own metric bindings — only the candidate analysis is shared. Existing tests pass (103 lib + 200 integration). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 16967cd commit 641e6cc

2 files changed

Lines changed: 91 additions & 31 deletions

File tree

‎datafusion/datasource-parquet/src/opener.rs‎

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
//! [`ParquetMorselizer`] state machines for opening Parquet files
1919
2020
use crate::page_filter::PagePruningAccessPlanFilter;
21-
use crate::row_filter::build_projection_read_plan;
21+
use crate::row_filter::{
22+
FilterCandidate, build_projection_read_plan, build_row_filter_candidates,
23+
row_filter_from_candidates,
24+
};
2225
use crate::row_group_filter::{BloomFilterStatistics, RowGroupAccessPlanFilter};
2326
use crate::{
2427
ParquetAccessPlan, ParquetFileMetrics, ParquetFileReaderFactory,
25-
apply_file_schema_type_coercions, coerce_int96_to_resolution, row_filter,
28+
apply_file_schema_type_coercions, coerce_int96_to_resolution,
2629
};
2730
use arrow::array::{RecordBatch, RecordBatchOptions};
2831
use arrow::datatypes::DataType;
@@ -326,6 +329,7 @@ pub(crate) struct ParquetOpenChunk {
326329
pub projection: ProjectionExprs,
327330
pub pruning_predicate: Option<Arc<PruningPredicate>>,
328331
pub page_pruning_predicate: Option<Arc<PagePruningAccessPlanFilter>>,
332+
pub row_filter_candidates: Option<Arc<Vec<FilterCandidate>>>,
329333
}
330334

331335
impl fmt::Debug for ParquetOpenChunk {
@@ -353,6 +357,11 @@ struct FiltersPreparedParquetOpen {
353357
loaded: MetadataLoadedParquetOpen,
354358
pruning_predicate: Option<Arc<PruningPredicate>>,
355359
page_pruning_predicate: Option<Arc<PagePruningAccessPlanFilter>>,
360+
/// Row-filter conjunct candidates built once here and shared with
361+
/// sibling stealers via `ParquetOpenChunk` so each open doesn't
362+
/// redo the schema-walk + ProjectionMask build. Only the cheap
363+
/// per-open metric binding happens at `build_stream` time.
364+
row_filter_candidates: Option<Arc<Vec<FilterCandidate>>>,
356365
}
357366

358367
/// State of [`ParquetOpenState`]
@@ -769,6 +778,7 @@ impl ParquetMorselizer {
769778
projection,
770779
pruning_predicate,
771780
page_pruning_predicate,
781+
row_filter_candidates,
772782
} = chunk;
773783

774784
let prepared = PreparedParquetOpen {
@@ -816,6 +826,7 @@ impl ParquetMorselizer {
816826
},
817827
pruning_predicate,
818828
page_pruning_predicate,
829+
row_filter_candidates,
819830
},
820831
row_groups: RowGroupAccessPlanFilter::new(access_plan),
821832
})
@@ -999,6 +1010,32 @@ impl MetadataLoadedParquetOpen {
9991010
None
10001011
};
10011012

1013+
// Build row-filter candidates once here — the expensive part of
1014+
// pushdown-filter construction (schema walk, ProjectionMask
1015+
// build, cost-based reorder). Stealers of donated chunks reuse
1016+
// this via `ParquetOpenChunk`.
1017+
let row_filter_candidates = if let Some(predicate) = prepared
1018+
.pushdown_filters
1019+
.then_some(prepared.predicate.as_ref())
1020+
.flatten()
1021+
{
1022+
match build_row_filter_candidates(
1023+
predicate,
1024+
&physical_file_schema,
1025+
reader_metadata.metadata(),
1026+
prepared.reorder_predicates,
1027+
) {
1028+
Ok(Some(cs)) => Some(Arc::new(cs)),
1029+
Ok(None) => None,
1030+
Err(e) => {
1031+
debug!("Ignoring error building row filter for '{predicate:?}': {e}");
1032+
None
1033+
}
1034+
}
1035+
} else {
1036+
None
1037+
};
1038+
10021039
Ok(FiltersPreparedParquetOpen {
10031040
loaded: MetadataLoadedParquetOpen {
10041041
prepared,
@@ -1007,6 +1044,7 @@ impl MetadataLoadedParquetOpen {
10071044
},
10081045
pruning_predicate,
10091046
page_pruning_predicate,
1047+
row_filter_candidates,
10101048
})
10111049
}
10121050
}
@@ -1282,6 +1320,7 @@ impl RowGroupsPrunedParquetOpen {
12821320
projection: self.prepared.loaded.prepared.projection.clone(),
12831321
pruning_predicate: self.prepared.pruning_predicate.clone(),
12841322
page_pruning_predicate: self.prepared.page_pruning_predicate.clone(),
1323+
row_filter_candidates: self.prepared.row_filter_candidates.clone(),
12851324
};
12861325

12871326
let keep_idx = eligible[0];
@@ -1309,6 +1348,7 @@ impl RowGroupsPrunedParquetOpen {
13091348
loaded,
13101349
pruning_predicate: _,
13111350
page_pruning_predicate,
1351+
row_filter_candidates,
13121352
} = prepared;
13131353
let MetadataLoadedParquetOpen {
13141354
prepared,
@@ -1319,25 +1359,16 @@ impl RowGroupsPrunedParquetOpen {
13191359
let file_metadata = Arc::clone(reader_metadata.metadata());
13201360
let rg_metadata = file_metadata.row_groups();
13211361

1322-
// Filter pushdown: evaluate predicates during scan
1323-
let row_filter = if let Some(predicate) = prepared
1324-
.pushdown_filters
1325-
.then_some(prepared.predicate.clone())
1326-
.flatten()
1362+
// Filter pushdown: evaluate predicates during scan. The
1363+
// expensive candidate construction ran once in `prepare_filters`;
1364+
// here we only do the cheap per-open metric binding.
1365+
let row_filter = if let Some(candidates) = row_filter_candidates.as_deref()
1366+
&& prepared.pushdown_filters
13271367
{
1328-
let row_filter = row_filter::build_row_filter(
1329-
&predicate,
1330-
&prepared.physical_file_schema,
1331-
file_metadata.as_ref(),
1332-
prepared.reorder_predicates,
1333-
&prepared.file_metrics,
1334-
);
1335-
1336-
match row_filter {
1337-
Ok(Some(filter)) => Some(filter),
1338-
Ok(None) => None,
1368+
match row_filter_from_candidates(candidates, &prepared.file_metrics) {
1369+
Ok(filter) => Some(filter),
13391370
Err(e) => {
1340-
debug!("Ignoring error building row filter for '{predicate:?}': {e}");
1371+
debug!("Ignoring error building row filter: {e}");
13411372
None
13421373
}
13431374
}

‎datafusion/datasource-parquet/src/row_filter.rs‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ impl ArrowPredicate for DatafusionArrowPredicate {
177177
/// of evaluating the resulting expression.
178178
///
179179
/// See the module level documentation for more information.
180+
#[derive(Clone)]
180181
pub(crate) struct FilterCandidate {
181182
expr: Arc<dyn PhysicalExpr>,
182183
/// Estimate for the total number of bytes that will need to be processed
@@ -1019,10 +1020,28 @@ pub fn build_row_filter(
10191020
reorder_predicates: bool,
10201021
file_metrics: &ParquetFileMetrics,
10211022
) -> Result<Option<RowFilter>> {
1022-
let rows_pruned = &file_metrics.pushdown_rows_pruned;
1023-
let rows_matched = &file_metrics.pushdown_rows_matched;
1024-
let time = &file_metrics.row_pushdown_eval_time;
1023+
let Some(candidates) =
1024+
build_row_filter_candidates(expr, file_schema, metadata, reorder_predicates)?
1025+
else {
1026+
return Ok(None);
1027+
};
1028+
row_filter_from_candidates(&candidates, file_metrics).map(Some)
1029+
}
10251030

1031+
/// Expensive, metrics-free first phase of row-filter construction.
1032+
///
1033+
/// Splits the predicate into conjuncts, resolves each one against the file
1034+
/// schema and parquet metadata (building [`ProjectionMask`]s and cost
1035+
/// estimates), and optionally reorders candidates by estimated cost. The
1036+
/// result is the same for every open of a given (predicate, file_schema,
1037+
/// file_metadata) triple, so a donor may build it once and share it with
1038+
/// sibling stealers.
1039+
pub(crate) fn build_row_filter_candidates(
1040+
expr: &Arc<dyn PhysicalExpr>,
1041+
file_schema: &SchemaRef,
1042+
metadata: &ParquetMetaData,
1043+
reorder_predicates: bool,
1044+
) -> Result<Option<Vec<FilterCandidate>>> {
10261045
// Split into conjuncts:
10271046
// `a = 1 AND b = 2 AND c = 3` -> [`a = 1`, `b = 2`, `c = 3`]
10281047
let predicates = split_conjunction(expr);
@@ -1039,14 +1058,29 @@ pub fn build_row_filter(
10391058
.flatten()
10401059
.collect();
10411060

1042-
// no candidates
10431061
if candidates.is_empty() {
10441062
return Ok(None);
10451063
}
10461064

10471065
if reorder_predicates {
10481066
candidates.sort_unstable_by_key(|c| c.required_bytes);
10491067
}
1068+
Ok(Some(candidates))
1069+
}
1070+
1071+
/// Cheap per-open second phase: wire each candidate up with the current
1072+
/// file's row-filter metrics.
1073+
///
1074+
/// Called separately from [`build_row_filter_candidates`] so that the
1075+
/// expensive candidate construction can be shared across sibling opens
1076+
/// of the same file.
1077+
pub(crate) fn row_filter_from_candidates(
1078+
candidates: &[FilterCandidate],
1079+
file_metrics: &ParquetFileMetrics,
1080+
) -> Result<RowFilter> {
1081+
let rows_pruned = &file_metrics.pushdown_rows_pruned;
1082+
let rows_matched = &file_metrics.pushdown_rows_matched;
1083+
let time = &file_metrics.row_pushdown_eval_time;
10501084

10511085
// To avoid double-counting metrics when multiple predicates are used:
10521086
// - All predicates should count rows_pruned (cumulative pruned rows)
@@ -1055,31 +1089,26 @@ pub fn build_row_filter(
10551089
let total_candidates = candidates.len();
10561090

10571091
candidates
1058-
.into_iter()
1092+
.iter()
10591093
.enumerate()
10601094
.map(|(idx, candidate)| {
10611095
let is_last = idx == total_candidates - 1;
1062-
1063-
// All predicates share the pruned counter (cumulative)
10641096
let predicate_rows_pruned = rows_pruned.clone();
1065-
1066-
// Only the last predicate tracks matched rows (final result)
10671097
let predicate_rows_matched = if is_last {
10681098
rows_matched.clone()
10691099
} else {
10701100
metrics::Count::new()
10711101
};
1072-
10731102
DatafusionArrowPredicate::try_new(
1074-
candidate,
1103+
candidate.clone(),
10751104
predicate_rows_pruned,
10761105
predicate_rows_matched,
10771106
time.clone(),
10781107
)
10791108
.map(|pred| Box::new(pred) as _)
10801109
})
10811110
.collect::<Result<Vec<_>, _>>()
1082-
.map(|filters| Some(RowFilter::new(filters)))
1111+
.map(RowFilter::new)
10831112
}
10841113

10851114
#[cfg(test)]

0 commit comments

Comments
 (0)