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-11684-513c92d5f12cbef5da09e30c144f2dcf857cdf91
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 4eff191376515003c8522a23103e276db73cb875
Author: Huaijin <[email protected]>
AuthorDate: Tue Oct 27 01:46:13 2026 +0000

    refactor(parquet): simplify `expand_to_batch_boundaries_from_selectors` 
(#11684)
    
    # 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.
    -->
    
    Selection runs already advance in row order, so expanding them to batch
    boundaries does not require sorting the resulting ranges before merging
    them.
    
    
    # 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.
    -->
    Expand and merge selected runs in a single pass, removing the redundant
    sort and intermediate range vector. Use `next_multiple_of` for the end
    boundary and clarify how overlapping and adjacent ranges are merged.
    
    
    # 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.
    -->
    
    existing tests case
    
    # 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.
    -->
---
 parquet/src/arrow/arrow_reader/selection/ranges.rs | 51 ++++++++--------------
 1 file changed, 18 insertions(+), 33 deletions(-)

diff --git a/parquet/src/arrow/arrow_reader/selection/ranges.rs 
b/parquet/src/arrow/arrow_reader/selection/ranges.rs
index 1069398601..0fcbdb84ad 100644
--- a/parquet/src/arrow/arrow_reader/selection/ranges.rs
+++ b/parquet/src/arrow/arrow_reader/selection/ranges.rs
@@ -110,8 +110,8 @@ where
     ranges
 }
 
-/// Grows each selected run to the batch boundaries containing it, merging the
-/// runs that overlap as a result.
+/// Grows each selected run to the batch boundaries containing it, merging
+/// overlapping or adjacent ranges.
 #[inline]
 pub(super) fn expand_to_batch_boundaries_from_selectors<I>(
     selectors: I,
@@ -121,46 +121,31 @@ pub(super) fn 
expand_to_batch_boundaries_from_selectors<I>(
 where
     I: IntoIterator<Item = RowSelector>,
 {
-    let mut expanded_ranges = Vec::new();
+    let mut ranges: Vec<Range<usize>> = Vec::new();
     let mut row_offset = 0;
 
+    // Selectors advance in row order, so expanded ranges can be merged as we 
go.
     for selector in selectors {
-        if !selector.skip {
-            let start = row_offset;
-            let end = row_offset + selector.row_count;
-
-            // Expand start to batch boundary
-            let expanded_start = (start / batch_size) * batch_size;
-            // Expand end to batch boundary
-            let expanded_end = end.div_ceil(batch_size) * batch_size;
-            let expanded_end = expanded_end.min(total_rows);
-
-            expanded_ranges.push(expanded_start..expanded_end);
-        }
+        let row_start = row_offset;
         row_offset += selector.row_count;
-    }
+        if selector.skip {
+            continue;
+        }
 
-    // Sort ranges by start position
-    expanded_ranges.sort_by_key(|range| range.start);
+        // Round the start down and the end up to cover complete batches.
+        let batch_start = (row_start / batch_size) * batch_size;
+        let batch_end = 
row_offset.next_multiple_of(batch_size).min(total_rows);
 
-    // Merge overlapping or consecutive ranges
-    let mut merged_ranges: Vec<Range<usize>> = Vec::new();
-    for range in expanded_ranges {
-        if let Some(last) = merged_ranges.last_mut() {
-            if range.start <= last.end {
-                // Overlapping or consecutive - merge them
-                last.end = last.end.max(range.end);
-            } else {
-                // No overlap - add new range
-                merged_ranges.push(range);
-            }
-        } else {
-            // First range
-            merged_ranges.push(range);
+        // Ends are non-decreasing because selectors advance in row order.
+        match ranges.last_mut() {
+            // The ranges overlap or touch, so extend the last range.
+            Some(last) if batch_start <= last.end => last.end = batch_end,
+            // There is a gap, or this is the first selected range.
+            _ => ranges.push(batch_start..batch_end),
         }
     }
 
-    RowSelection::from_consecutive_ranges(merged_ranges.into_iter(), 
total_rows)
+    RowSelection::from_consecutive_ranges(ranges.into_iter(), total_rows)
 }
 
 #[cfg(test)]

Reply via email to