This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-11688-4eff191376515003c8522a23103e276db73cb875 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 87b8013f9f83e8f7681eefc01c452eb951754eff Author: Huaijin <[email protected]> AuthorDate: Tue Oct 27 01:50:23 2026 +0000 refactor(parquet): share selector intersection and union traversal (#11688) # Which issue does this PR close? <!-- It is not necessary to raise an issue, but it is encouraged if it is a non-trivial enhancement/bug. If this PR relates to an existing issue, but does not fully resolve it, please change the wording below from "Closes #NNN" to "Relates to #NNN" or "Part of #NNN" to avoid false closing an issue. --> - Closes #NNN # 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. Please try to explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. --> Selector-backed intersection and union duplicate the same traversal and run-splitting logic. They differ only in how the skip flags are combined. Sharing this traversal reduces duplication and makes their common behavior easier to maintain. # What changes are included in this PR? <!-- 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. --> - Extract a private `combine_row_selections` helper. - Combine skip flags using OR for intersection and AND for union. - Preserve zero-length handling, output normalization, and the existing behavior of passing the longer operand's tail through unchanged. # Are these changes tested? <!-- 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, if so identify these tests)? If this PR claims a performance improvement, please include evidence such as benchmark results. DO NOT simply paste a list of commands ran to verify the code; this is what our CI is for and it pollutes the PR. ONLY explain if testing was included with this PR, or why it wasn't. --> # Are there any significant user-facing changes? <!-- Significant user-facing changes include: - Breaking public API change - Significant change to existing functionality (e.g. function returns something different) These need to be explicitly called out to ensure we have proper documentation if required and to ensure we follow proper semantic versioning. DO NOT restate the changes of this PR; if a bug was fixed and already explained above, we don't need this to be restated as a user-facing change. --> --- .../src/arrow/arrow_reader/selection/algebra.rs | 123 ++++----------------- 1 file changed, 22 insertions(+), 101 deletions(-) diff --git a/parquet/src/arrow/arrow_reader/selection/algebra.rs b/parquet/src/arrow/arrow_reader/selection/algebra.rs index 6e16ead75d..11cf63c221 100644 --- a/parquet/src/arrow/arrow_reader/selection/algebra.rs +++ b/parquet/src/arrow/arrow_reader/selection/algebra.rs @@ -138,57 +138,7 @@ pub(super) fn intersect_row_selections( left: &[RowSelector], right: &[RowSelector], ) -> RowSelection { - let mut l_iter = left.iter().copied().peekable(); - let mut r_iter = right.iter().copied().peekable(); - - let iter = std::iter::from_fn(move || { - loop { - let l = l_iter.peek_mut(); - let r = r_iter.peek_mut(); - - match (l, r) { - (Some(a), _) if a.row_count == 0 => { - l_iter.next().unwrap(); - } - (_, Some(b)) if b.row_count == 0 => { - r_iter.next().unwrap(); - } - (Some(l), Some(r)) => { - return match (l.skip, r.skip) { - // Keep both ranges - (false, false) => { - if l.row_count < r.row_count { - r.row_count -= l.row_count; - l_iter.next() - } else { - l.row_count -= r.row_count; - r_iter.next() - } - } - // skip at least one - _ => { - if l.row_count < r.row_count { - let skip = l.row_count; - r.row_count -= l.row_count; - l_iter.next(); - Some(RowSelector::skip(skip)) - } else { - let skip = r.row_count; - l.row_count -= skip; - r_iter.next(); - Some(RowSelector::skip(skip)) - } - } - }; - } - (Some(_), None) => return l_iter.next(), - (None, Some(_)) => return r_iter.next(), - (None, None) => return None, - } - } - }); - - iter.collect() + combine_row_selections(left, right, |l, r| l || r) } /// Combine two lists of `RowSelector` return the union of them @@ -200,6 +150,15 @@ pub(super) fn intersect_row_selections( /// /// This can be removed from here once RowSelection::union is in parquet::arrow pub(super) fn union_row_selections(left: &[RowSelector], right: &[RowSelector]) -> RowSelection { + combine_row_selections(left, right, |l, r| l && r) +} + +/// Combines overlapping selector runs, passing the longer side's tail through unchanged. +fn combine_row_selections( + left: &[RowSelector], + right: &[RowSelector], + combine_skip: impl Fn(bool, bool) -> bool, +) -> RowSelection { let mut l_iter = left.iter().copied().peekable(); let mut r_iter = right.iter().copied().peekable(); @@ -216,56 +175,18 @@ pub(super) fn union_row_selections(left: &[RowSelector], right: &[RowSelector]) r_iter.next().unwrap(); } (Some(l), Some(r)) => { - return match (l.skip, r.skip) { - // Skip both ranges - (true, true) => { - if l.row_count < r.row_count { - let skip = l.row_count; - r.row_count -= l.row_count; - l_iter.next(); - Some(RowSelector::skip(skip)) - } else { - let skip = r.row_count; - l.row_count -= skip; - r_iter.next(); - Some(RowSelector::skip(skip)) - } - } - // Keep rows from left - (false, true) => { - if l.row_count < r.row_count { - r.row_count -= l.row_count; - l_iter.next() - } else { - let r_row_count = r.row_count; - l.row_count -= r_row_count; - r_iter.next(); - Some(RowSelector::select(r_row_count)) - } - } - // Keep rows from right - (true, false) => { - if l.row_count < r.row_count { - let l_row_count = l.row_count; - r.row_count -= l_row_count; - l_iter.next(); - Some(RowSelector::select(l_row_count)) - } else { - l.row_count -= r.row_count; - r_iter.next() - } - } - // Keep at least one - _ => { - if l.row_count < r.row_count { - r.row_count -= l.row_count; - l_iter.next() - } else { - l.row_count -= r.row_count; - r_iter.next() - } - } - }; + let skip = combine_skip(l.skip, r.skip); + let row_count = l.row_count.min(r.row_count); + + if l.row_count < r.row_count { + r.row_count -= row_count; + l_iter.next(); + } else { + l.row_count -= row_count; + r_iter.next(); + } + + return Some(RowSelector { row_count, skip }); } (Some(_), None) => return l_iter.next(), (None, Some(_)) => return r_iter.next(),
