vustef commented on code in PR #8715:
URL: https://github.com/apache/arrow-rs/pull/8715#discussion_r2537116990


##########
parquet/src/arrow/array_reader/row_number.rs:
##########
@@ -0,0 +1,108 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::arrow::array_reader::ArrayReader;
+use crate::errors::{ParquetError, Result};
+use crate::file::metadata::{ParquetMetaData, RowGroupMetaData};
+use arrow_array::{ArrayRef, Int64Array};
+use arrow_schema::DataType;
+use std::any::Any;
+use std::collections::HashSet;
+use std::sync::Arc;
+
+pub(crate) struct RowNumberReader {
+    buffered_row_numbers: Vec<i64>,
+    remaining_row_numbers: 
std::iter::Flatten<std::vec::IntoIter<std::ops::Range<i64>>>,
+}
+
+impl RowNumberReader {
+    pub(crate) fn try_new<'a>(
+        parquet_metadata: &'a ParquetMetaData,
+        row_groups: impl Iterator<Item = &'a RowGroupMetaData>,
+    ) -> Result<Self> {
+        // Collect ordinals from the selected row groups
+        let selected_ordinals: HashSet<i16> = row_groups
+            .map(|rg| {
+                rg.ordinal().ok_or_else(|| {
+                    ParquetError::General(
+                        "Row group missing ordinal field, required to compute 
row numbers"
+                            .to_string(),
+                    )
+                })
+            })
+            .collect::<Result<_>>()?;
+
+        // Iterate through all row groups once, computing first_row_index and 
creating ranges
+        // This is O(M) where M is total row groups, much better than O(N * O) 
where N is selected
+        let mut first_row_index: i64 = 0;
+        let mut ranges = Vec::new();
+
+        for rg in parquet_metadata.row_groups() {
+            if let Some(ordinal) = rg.ordinal() {
+                if selected_ordinals.contains(&ordinal) {
+                    ranges.push((ordinal, first_row_index..first_row_index + 
rg.num_rows()));
+                }
+            }
+            first_row_index += rg.num_rows();
+        }
+
+        // Sort ranges by ordinal to maintain original row group order
+        ranges.sort_by_key(|(ordinal, _)| *ordinal);

Review Comment:
   I messed this up when I started computing first row indexes...thank you for 
catching this. WIll follow up shortly with tests and the fix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to