jkylling commented on code in PR #7307:
URL: https://github.com/apache/arrow-rs/pull/7307#discussion_r2019443876


##########
parquet/src/arrow/array_reader/row_number.rs:
##########
@@ -0,0 +1,154 @@
+// 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::RowGroupMetaData;
+use arrow_array::{ArrayRef, Int64Array};
+use arrow_schema::DataType;
+use std::any::Any;
+use std::collections::VecDeque;
+use std::sync::Arc;
+
+pub(crate) struct RowNumberReader {
+    row_numbers: Vec<i64>,
+    row_groups: RowGroupSizeIterator,
+}
+
+impl RowNumberReader {
+    pub(crate) fn try_new<I>(row_groups: impl IntoIterator<Item = I>) -> 
Result<Self>
+    where
+        I: TryInto<RowGroupSize, Error = ParquetError>,
+    {
+        let row_groups = RowGroupSizeIterator::try_new(row_groups)?;
+        Ok(Self {
+            row_numbers: Vec::new(),
+            row_groups,
+        })
+    }
+}
+
+impl ArrayReader for RowNumberReader {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn get_data_type(&self) -> &DataType {
+        &DataType::Int64
+    }
+
+    fn read_records(&mut self, batch_size: usize) -> Result<usize> {
+        let read = self
+            .row_groups
+            .read_records(batch_size, &mut self.row_numbers);
+        Ok(read)
+    }
+
+    fn consume_batch(&mut self) -> Result<ArrayRef> {
+        Ok(Arc::new(Int64Array::from_iter(self.row_numbers.drain(..))))
+    }
+
+    fn skip_records(&mut self, num_records: usize) -> Result<usize> {
+        let skipped = self.row_groups.skip_records(num_records);
+        Ok(skipped)
+    }
+
+    fn get_def_levels(&self) -> Option<&[i16]> {
+        None
+    }
+
+    fn get_rep_levels(&self) -> Option<&[i16]> {
+        None
+    }
+}
+
+struct RowGroupSizeIterator {
+    row_groups: VecDeque<RowGroupSize>,
+}
+
+impl RowGroupSizeIterator {
+    fn try_new<I>(row_groups: impl IntoIterator<Item = I>) -> Result<Self>
+    where
+        I: TryInto<RowGroupSize, Error = ParquetError>,
+    {
+        Ok(Self {
+            row_groups: VecDeque::from(
+                row_groups
+                    .into_iter()
+                    .map(TryInto::try_into)
+                    .collect::<Result<Vec<_>>>()?,
+            ),
+        })

Review Comment:
   Yes, I believe we don't have access to all row groups when creating the 
array readers.
   
   I took a quick look at the corresponding Parquet reader implementations for 
Trino and parquet-java.
   Trino:
   * Has a boolean to include a row number column, 
https://github.com/trinodb/trino/blob/a54d38a30e486a94a365c7f12a94e47beb30b0fa/lib/trino-parquet/src/main/java/io/trino/parquet/reader/ParquetReader.java#L112
   * Includes this column when the boolean is set: 
https://github.com/trinodb/trino/blob/a54d38a30e486a94a365c7f12a94e47beb30b0fa/lib/trino-parquet/src/main/java/io/trino/parquet/reader/ParquetReader.java#L337
   * Has a special block reader for reading row indexes 
https://github.com/trinodb/trino/blob/a54d38a30e486a94a365c7f12a94e47beb30b0fa/lib/trino-parquet/src/main/java/io/trino/parquet/reader/ParquetReader.java#L385-L393
 I believe the positions play a similar role to our `RowSelectors`.
   * Gets row indexes from `RowGroupInfo`, a pruned version of  
https://github.com/trinodb/trino/blob/a54d38a30e486a94a365c7f12a94e47beb30b0fa/lib/trino-parquet/src/main/java/io/trino/parquet/reader/ParquetReader.java#L456
   * Populates the `fileRowOffset` by iterating through the row groups: 
https://github.com/trinodb/trino/blob/master/lib/trino-parquet/src/main/java/io/trino/parquet/metadata/ParquetMetadata.java#L107-L111
   
   parquet-java:
   * Has a method for tracking the current row index: 
https://github.com/apache/parquet-java/blob/7d1fe32c8c972710a9d780ec5e7d1f95d871374d/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetReader.java#L150-L155
   * This row index is based on an iterator which starts form a row group row 
index, 
https://github.com/apache/parquet-java/blob/7d1fe32c8c972710a9d780ec5e7d1f95d871374d/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/InternalParquetRecordReader.java#L311-L339
   * This row group row index is initialized by iterating through the row 
groups: 
https://github.com/apache/parquet-java/blob/7d1fe32c8c972710a9d780ec5e7d1f95d871374d/parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java#L1654-L1656
 (mapping obtained here: 
https://github.com/apache/parquet-java/blob/7d1fe32c8c972710a9d780ec5e7d1f95d871374d/parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java#L1496-L1506)
   
   Their approaches are rather similar to ours.
   
   One take away is that the above implementations do not be keep the full 
`RowGroupMetaData`s around as we do by requiring an iterator over 
`RowGroupMetadata` in the `RowGroups` trait. This is likely a good idea as this 
struct can be quite large. What do you think about changing the `RowGroups` 
trait to something like below?
   ```rust
   /// A collection of row groups
   pub trait RowGroups {
       /// Get the number of rows in this collection
       fn num_rows(&self) -> usize {
           self.row_group_infos.iter().map(|info| info.num_rows).sum()
       }
   
       /// Returns a [`PageIterator`] for the column chunks with the given leaf 
column index
       fn column_chunks(&self, i: usize) -> Result<Box<dyn PageIterator>>;
   
       /// Returns an iterator over the row groups in this collection
       fn row_group_infos(&self) -> Box<dyn Iterator<Item = &RowGroupInfo> + 
'_>;
   }
   
   struct RowGroupInfo {
       num_rows: usize,
       row_index: i64,
   }
   ```



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to