Skip to content

Commit 429f5a7

Browse files
authored
refactor: Optimize required_columns from BTreeSet to Vec in struct PushdownChecker (#19678)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #19673. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Changed [row_filter.rs](https://github.com/apache/datafusion/blob/main/datafusion/datasource-parquet/src/row_filter.rs) to use Vec instead of the BTreeSet <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Yes <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 567ba75 commit 429f5a7

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

datafusion/datasource-parquet/src/row_filter.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ struct PushdownChecker<'schema> {
276276
/// Does the expression reference any columns not present in the file schema?
277277
projected_columns: bool,
278278
/// Indices into the file schema of columns required to evaluate the expression.
279-
required_columns: BTreeSet<usize>,
279+
required_columns: Vec<usize>,
280280
/// Tracks the nested column behavior found during traversal.
281281
nested_behavior: NestedColumnSupport,
282282
/// Whether nested list columns are supported by the predicate semantics.
@@ -290,7 +290,7 @@ impl<'schema> PushdownChecker<'schema> {
290290
Self {
291291
non_primitive_columns: false,
292292
projected_columns: false,
293-
required_columns: BTreeSet::default(),
293+
required_columns: Vec::new(),
294294
nested_behavior: NestedColumnSupport::PrimitiveOnly,
295295
allow_list_columns,
296296
file_schema,
@@ -307,7 +307,8 @@ impl<'schema> PushdownChecker<'schema> {
307307
}
308308
};
309309

310-
self.required_columns.insert(idx);
310+
// Duplicates are handled by dedup() in into_sorted_columns()
311+
self.required_columns.push(idx);
311312
let data_type = self.file_schema.field(idx).data_type();
312313

313314
if DataType::is_nested(data_type) {
@@ -355,6 +356,21 @@ impl<'schema> PushdownChecker<'schema> {
355356
fn prevents_pushdown(&self) -> bool {
356357
self.non_primitive_columns || self.projected_columns
357358
}
359+
360+
/// Consumes the checker and returns sorted, deduplicated column indices
361+
/// wrapped in a `PushdownColumns` struct.
362+
///
363+
/// This method sorts the column indices and removes duplicates. The sort
364+
/// is required because downstream code relies on column indices being in
365+
/// ascending order for correct schema projection.
366+
fn into_sorted_columns(mut self) -> PushdownColumns {
367+
self.required_columns.sort_unstable();
368+
self.required_columns.dedup();
369+
PushdownColumns {
370+
required_columns: self.required_columns,
371+
nested: self.nested_behavior,
372+
}
373+
}
358374
}
359375

360376
impl TreeNodeVisitor<'_> for PushdownChecker<'_> {
@@ -390,9 +406,13 @@ enum NestedColumnSupport {
390406
Unsupported,
391407
}
392408

409+
/// Result of checking which columns are required for filter pushdown.
393410
#[derive(Debug)]
394411
struct PushdownColumns {
395-
required_columns: BTreeSet<usize>,
412+
/// Sorted, unique column indices into the file schema required to evaluate
413+
/// the filter expression. Must be in ascending order for correct schema
414+
/// projection matching.
415+
required_columns: Vec<usize>,
396416
nested: NestedColumnSupport,
397417
}
398418

@@ -411,10 +431,7 @@ fn pushdown_columns(
411431
let allow_list_columns = supports_list_predicates(expr);
412432
let mut checker = PushdownChecker::new(file_schema, allow_list_columns);
413433
expr.visit(&mut checker)?;
414-
Ok((!checker.prevents_pushdown()).then_some(PushdownColumns {
415-
required_columns: checker.required_columns,
416-
nested: checker.nested_behavior,
417-
}))
434+
Ok((!checker.prevents_pushdown()).then(|| checker.into_sorted_columns()))
418435
}
419436

420437
fn leaf_indices_for_roots(

0 commit comments

Comments
 (0)