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


##########
parquet/src/arrow/array_reader/mod.rs:
##########
@@ -42,12 +42,13 @@ mod map_array;
 mod null_array;
 mod primitive_array;
 mod row_group_cache;
+mod row_number;
 mod struct_array;
 
 #[cfg(test)]
 mod test_util;
 
-// Note that this crate is public under the `experimental` feature flag.

Review Comment:
   shouldn't remove this comment



##########
parquet/src/arrow/arrow_reader/mod.rs:
##########
@@ -388,6 +388,8 @@ pub struct ArrowReaderOptions {
     /// If encryption is enabled, the file decryption properties can be 
provided
     #[cfg(feature = "encryption")]
     pub(crate) file_decryption_properties: 
Option<Arc<FileDecryptionProperties>>,
+
+    virtual_columns: Vec<Field>

Review Comment:
   maybe it should be `Vec<&Field>`



##########
parquet/src/arrow/schema/complex.rs:
##########
@@ -541,6 +567,46 @@ impl Visitor {
     }
 }
 
+/// Converts a virtual Arrow [`Field`] to a [`ParquetField`]
+///
+/// Virtual fields don't correspond to any data in the parquet file,
+/// but are computed at read time (e.g., row_number)
+///
+/// The levels are computed based on the parent context:
+/// - If nullable: def_level = parent_def_level + 1
+/// - If required: def_level = parent_def_level
+/// - rep_level = parent_rep_level (virtual fields are not repeated)
+fn convert_virtual_field(

Review Comment:
   the name used here is not aligned with what other `convert_` functions do



##########
parquet/src/arrow/array_reader/row_number.rs:
##########
@@ -0,0 +1,82 @@
+// 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::Result;
+use crate::file::metadata::RowGroupMetaData;
+use arrow_array::{ArrayRef, Int64Array};
+use arrow_schema::DataType;
+use std::any::Any;
+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>(
+        row_groups: impl Iterator<Item = &'a RowGroupMetaData>,
+    ) -> Result<Self> {
+        let ranges = row_groups
+            .map(|rg| {
+                let first_row_index = rg.first_row_index();
+                Ok(first_row_index..first_row_index + rg.num_rows())
+            })
+            .collect::<Result<Vec<_>>>()?;
+        Ok(Self {
+            buffered_row_numbers: Vec::new(),
+            remaining_row_numbers: ranges.into_iter().flatten(),
+        })
+    }
+}
+
+impl ArrayReader for RowNumberReader {
+    fn read_records(&mut self, batch_size: usize) -> Result<usize> {
+        let starting_len = self.buffered_row_numbers.len();
+        self.buffered_row_numbers
+            .extend((&mut self.remaining_row_numbers).take(batch_size));
+        Ok(self.buffered_row_numbers.len() - starting_len)
+    }
+
+    fn skip_records(&mut self, num_records: usize) -> Result<usize> {
+        // TODO: Use advance_by when it stabilizes to improve performance

Review Comment:
   TODO from original PR



##########
parquet/src/arrow/array_reader/builder.rs:
##########
@@ -15,24 +15,25 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use arrow_schema::{DataType, Field, Fields, SchemaBuilder};

Review Comment:
   shouldn't move this



##########
parquet/src/arrow/async_reader/mod.rs:
##########
@@ -1130,6 +1130,10 @@ impl RowGroups for InMemoryRowGroup<'_> {
             }
         }
     }
+
+    fn row_groups(&self) -> Box<dyn Iterator<Item = &RowGroupMetaData> + '_> {
+        Box::new(std::iter::once(self.metadata.row_group(self.row_group_idx)))

Review Comment:
   this duplicates a lot, not sure if anything can be done here



##########
parquet/src/arrow/arrow_reader/mod.rs:
##########
@@ -516,6 +518,22 @@ impl ArrowReaderOptions {
         }
     }
 
+    /// Include virtual columns in the output.
+    ///
+    /// Virtual columns are columns that are not part of the Parquet schema, 
but are added to the output by the reader.
+    ///
+    /// # Example
+    /// ```
+    /// let virtual_columns = vec![Field::new("row_number", 
ArrowDataType::Int64, false).with_extension_type(RowNumber)];
+    /// let options = 
ArrowReaderOptions::new().with_virtual_columns(virtual_columns);
+    pub fn with_virtual_columns(self, virtual_columns: Vec<Field>) -> Self {
+        // TODO @vustef: Make sure the passed fields are virtual columns.

Review Comment:
   I still have a bunch of TODOs to address



-- 
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