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-11276-8e9eec38ba841eb505c2925c078471e3dcab5559
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 3ece42b5784ddd89a86894dafa898834b8e8843a
Author: Chao Sun <[email protected]>
AuthorDate: Tue Nov 3 16:50:35 2026 +0000

    perf(parquet): avoid repeated page-index range discovery (#11276)
    
    # Which issue does this PR close?
    
    Closes #11275.
    
    # Rationale for this change
    
    When the Parquet metadata push decoder requests page-index bytes, it
    walks the selected column chunks to find the byte range. Resuming after
    the caller supplies those bytes repeats the same walk. Polling while the
    bytes are still missing repeats it again. #11157 already handles the
    case where both index policies are `Skip`.
    
    # What changes are included in this PR?
    
    Keep the requested byte range until the decoder can read it. Both
    `try_decode` and `try_decode_page_index` check the buffers on each
    attempt. Changing any page-index policy or selection mask discards the
    saved range so the next request reflects the new selection. A successful
    decode or an error also clears the range.
    
    # Are these changes tested?
    
    The regression tests change each policy and mask while a request is
    pending, compare the requested range and decoded result with a fresh
    decoder, and exercise polling across both decode methods after clearing
    buffered bytes. They also check that the cached range is empty after a
    missing required offset index or corrupt index bytes, including a
    successful retry with corrected bytes.
    
    An in-memory benchmark compared current main (`e2c48ef0`) with this
    change (`db07a741`). Times are per operation.
    
    | Operation | Column chunks | Main | This PR |
    | --- | ---: | ---: | ---: |
    | Poll while waiting for bytes | 10 | 79.6 ns | 40.9 ns |
    | Poll while waiting for bytes | 1,000 | 4.90 µs | 40.7 ns |
    | Poll while waiting for bytes | 100,000 | 1.43 ms | 40.6 ns |
    | Request bytes, push them, and decode indexes | 10 | 5.06 µs | 4.88 µs
    |
    | Request bytes, push them, and decode indexes | 1,000 | 434.6 µs |
    416.7 µs |
    
    The poll benchmark starts after the decoder has requested its range. The
    request/push/decode benchmark starts with decoded footer metadata and
    includes decoder construction, the first range discovery, supplying an
    in-memory slice, and index parsing. It verifies the requested span and
    compares the decoded metadata and indexes with the synchronous reader.
    Both timings exclude footer parsing and storage I/O.
    
    Both revisions used the same dependency versions, Rust 1.99.0 release
    builds with Parquet default features disabled, and one pinned CPU on a
    shared AMD EPYC-Milan host. Each result is the median of four process
    medians, with seven timed batches per process and interleaved base/head
    runs.
    
    # Are there any significant user-facing changes?
    
    No. The public API, requested byte ranges, and decoded output are
    unchanged.
    
    # AI assistance
    
    OpenAI Codex assisted with the implementation, regression tests,
    benchmark harness, and PR description.
---
 parquet/src/file/metadata/push_decoder.rs | 110 +++++++++++++++++++++++++++---
 1 file changed, 99 insertions(+), 11 deletions(-)

diff --git a/parquet/src/file/metadata/push_decoder.rs 
b/parquet/src/file/metadata/push_decoder.rs
index 8ad49a557d..b46b7c2b3e 100644
--- a/parquet/src/file/metadata/push_decoder.rs
+++ b/parquet/src/file/metadata/push_decoder.rs
@@ -232,6 +232,8 @@ pub struct ParquetMetaDataPushDecoder {
     column_index_mask: ColumnChunkMask,
     /// which rows and columns of the offset index should be read
     offset_index_mask: ColumnChunkMask,
+    /// Page-index range already requested for the current policies and masks.
+    pending_page_index_range: Option<Range<u64>>,
     /// Underlying buffers
     buffers: crate::util::push_buffers::PushBuffers,
     /// Encryption API
@@ -258,6 +260,7 @@ impl ParquetMetaDataPushDecoder {
             offset_index_policy: PageIndexPolicy::Optional,
             column_index_mask: ColumnChunkMask::all(),
             offset_index_mask: ColumnChunkMask::all(),
+            pending_page_index_range: None,
             buffers: crate::util::push_buffers::PushBuffers::new(file_len),
             metadata_parser: MetadataParser::new(),
         })
@@ -304,18 +307,21 @@ impl ParquetMetaDataPushDecoder {
     pub fn with_page_index_policy(mut self, page_index_policy: 
PageIndexPolicy) -> Self {
         self.column_index_policy = page_index_policy;
         self.offset_index_policy = page_index_policy;
+        self.pending_page_index_range = None;
         self
     }
 
     /// Set the policy for reading the ColumnIndex (part of the PageIndex)
     pub fn with_column_index_policy(mut self, column_index_policy: 
PageIndexPolicy) -> Self {
         self.column_index_policy = column_index_policy;
+        self.pending_page_index_range = None;
         self
     }
 
     /// Set the policy for reading the OffsetIndex (part of the PageIndex)
     pub fn with_offset_index_policy(mut self, offset_index_policy: 
PageIndexPolicy) -> Self {
         self.offset_index_policy = offset_index_policy;
+        self.pending_page_index_range = None;
         self
     }
 
@@ -325,18 +331,21 @@ impl ParquetMetaDataPushDecoder {
     pub fn with_page_index_mask(mut self, mask: ColumnChunkMask) -> Self {
         self.column_index_mask = mask.clone();
         self.offset_index_mask = mask;
+        self.pending_page_index_range = None;
         self
     }
 
     /// Select the row groups and columns for which column indexes are read.
     pub fn with_column_index_mask(mut self, mask: ColumnChunkMask) -> Self {
         self.column_index_mask = mask;
+        self.pending_page_index_range = None;
         self
     }
 
     /// Select the row groups and columns for which offset indexes are read.
     pub fn with_offset_index_mask(mut self, mask: ColumnChunkMask) -> Self {
         self.offset_index_mask = mask;
+        self.pending_page_index_range = None;
         self
     }
 
@@ -547,13 +556,16 @@ impl ParquetMetaDataPushDecoder {
         &mut self,
         metadata: Box<ParquetMetaData>,
     ) -> Result<DecodeResult<(Box<ParquetMetaData>, Option<PageIndex>)>> {
-        let range = range_for_page_index(
-            &metadata,
-            self.column_index_policy,
-            self.offset_index_policy,
-            &self.column_index_mask,
-            &self.offset_index_mask,
-        );
+        // Reuse the requested range on resume. Only keep it while waiting for 
bytes.
+        let range = self.pending_page_index_range.take().or_else(|| {
+            range_for_page_index(
+                &metadata,
+                self.column_index_policy,
+                self.offset_index_policy,
+                &self.column_index_mask,
+                &self.offset_index_mask,
+            )
+        });
 
         let Some(page_index_range) = range else {
             self.state = DecodeState::Finished;
@@ -561,6 +573,7 @@ impl ParquetMetaDataPushDecoder {
         };
 
         if !self.buffers.has_range(&page_index_range) {
+            self.pending_page_index_range = Some(page_index_range.clone());
             self.state = DecodeState::ReadingPageIndex(metadata);
             return Ok(DecodeResult::NeedsData(vec![page_index_range]));
         }
@@ -778,13 +791,79 @@ mod tests {
     }
 
     #[test]
-    fn test_page_index_decoder_no_indexes_requested() {
+    fn test_change_page_index_selection_while_waiting() {
+        type Decoder = ParquetMetaDataPushDecoder;
         let metadata = test_metadata_without_page_index();
+        let check = |initial: fn(Decoder) -> Decoder, change: fn(Decoder) -> 
Decoder| {
+            let new_decoder = || {
+                initial(Decoder::try_new_with_metadata(test_file_len(), 
metadata.clone()).unwrap())
+            };
+            let mut decoder = new_decoder();
+            let original = expect_needs_data(decoder.try_decode());
+            decoder = change(decoder);
+
+            let mut fresh = change(new_decoder());
+            let expected = expect_needs_data(fresh.try_decode());
+            assert_ne!(original, expected);
+            assert_eq!(expect_needs_data(decoder.try_decode()), expected);
+            push_ranges_to_metadata_decoder(&mut decoder, expected.clone());
+            push_ranges_to_metadata_decoder(&mut fresh, expected);
+            assert_eq!(
+                expect_data(decoder.try_decode()),
+                expect_data(fresh.try_decode())
+            );
+        };
+
+        let all = |decoder| decoder;
+        let one_chunk = |decoder: Decoder| {
+            
decoder.with_page_index_mask(ColumnChunkMask::row_groups_and_columns([0], [1]))
+        };
+        check(all, |d| d.with_column_index_policy(PageIndexPolicy::Skip));
+        check(all, |d| d.with_offset_index_policy(PageIndexPolicy::Skip));
+        check(all, one_chunk);
+        check(one_chunk, |d| {
+            d.with_column_index_mask(ColumnChunkMask::all())
+        });
+        check(one_chunk, |d| {
+            d.with_offset_index_mask(ColumnChunkMask::all())
+        });
+    }
+
+    #[test]
+    fn test_missing_required_offset_index_after_pending_request() {
+        let mut metadata = test_metadata_without_page_index().into_builder();
+        let mut groups = metadata.take_row_groups();
+        let column = &mut groups[0].columns_mut()[0];
+        *column = column
+            .clone()
+            .into_builder()
+            .set_offset_index_offset(None)
+            .set_offset_index_length(None)
+            .build()
+            .unwrap();
+        let metadata = metadata.set_row_groups(groups).build();
         let mut decoder =
-            ParquetMetaDataPushDecoder::try_new_with_metadata(test_file_len(), 
metadata)
-                .unwrap()
-                .with_page_index_policy(PageIndexPolicy::Skip);
+            ParquetMetaDataPushDecoder::try_new_with_metadata(test_file_len(), 
metadata).unwrap();
+
+        // The other chunks still require index bytes under either policy.
+        expect_needs_data(decoder.try_decode());
+        decoder = decoder.with_offset_index_policy(PageIndexPolicy::Required);
+        let ranges = expect_needs_data(decoder.try_decode());
+        push_ranges_to_metadata_decoder(&mut decoder, ranges);
+        let error = decoder.try_decode().unwrap_err();
+        assert!(
+            matches!(error, ParquetError::General(message) if message == 
"missing offset index")
+        );
+        assert!(decoder.pending_page_index_range.is_none());
+    }
 
+    #[test]
+    fn test_page_index_decoder_no_indexes_requested() {
+        let metadata = test_metadata_without_page_index();
+        let mut decoder =
+            ParquetMetaDataPushDecoder::try_new_with_metadata(test_file_len(), 
metadata).unwrap();
+        expect_needs_data(decoder.try_decode_page_index());
+        decoder = decoder.with_page_index_policy(PageIndexPolicy::Skip);
         assert!(expect_data(decoder.try_decode_page_index()).1.is_none());
         expect_finished(decoder.try_decode_page_index());
 
@@ -824,6 +903,7 @@ mod tests {
             .collect();
         decoder.push_ranges(ranges.clone(), invalid_data).unwrap();
         decoder.try_decode_page_index().unwrap_err();
+        assert!(decoder.pending_page_index_range.is_none());
 
         decoder.clear_all_ranges();
         push_ranges_to_metadata_decoder(&mut decoder, ranges);
@@ -861,10 +941,18 @@ mod tests {
         // data, so try_decode_page_index is now valid and asks for the same 
range
         let index_ranges_again = 
expect_needs_data(decoder.try_decode_page_index());
         assert_eq!(index_ranges, index_ranges_again);
+        assert_eq!(
+            decoder.pending_page_index_range.as_ref(),
+            index_ranges.first()
+        );
+        push_ranges_to_metadata_decoder(&mut decoder, index_ranges.clone());
+        decoder.clear_all_ranges();
+        assert_eq!(expect_needs_data(decoder.try_decode()), index_ranges);
         push_ranges_to_metadata_decoder(&mut decoder, index_ranges);
 
         // The regular decode path still works and attaches the page index
         let metadata = expect_data(decoder.try_decode());
+        assert!(decoder.pending_page_index_range.is_none());
         assert_eq!(metadata.num_row_groups(), 2);
         assert!(metadata.page_index().is_some_and(|idx| idx.is_complete()));
 

Reply via email to