SteNicholas commented on code in PR #1469:
URL: https://github.com/apache/paimon-rust/pull/1469#discussion_r4670656149


##########
crates/paimon/src/table/data_evolution_reader/candidate_ranges.rs:
##########
@@ -0,0 +1,276 @@
+// 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 super::{DataEvolutionReader, FieldSource, SourcePlan};
+use crate::arrow::filtering::build_field_mapping;
+use crate::arrow::format::parquet::{MergedReadMetadata, ParquetFormatReader};
+use crate::predicate_stats::stats_evolution_is_safe;
+use crate::spec::{CoreOptions, DataFileMeta, DataType, Predicate};
+use crate::table::data_file_reader::{open_file_reader, to_local_row_ranges};
+use crate::table::row_id_predicate::intersect_sorted_ranges;
+use crate::table::row_sidecar;
+use crate::table::RowRange;
+use crate::{DataSplit, Error};
+use parquet::arrow::arrow_reader::RowSelection;
+use std::collections::HashSet;
+
+#[cfg(test)]
+#[path = "candidate_ranges_tests.rs"]
+mod tests;
+
+impl DataEvolutionReader {
+    /// Logical row IDs that may match in the winning providers. An empty list
+    /// skips the group and `None` leaves its selection unchanged.
+    pub(super) async fn candidate_ranges(
+        &self,
+        split: &DataSplit,
+        plan: &mut SourcePlan,
+        selected_ranges: Option<&[RowRange]>,
+        sidecar_ranges: Option<&[RowRange]>,
+    ) -> Option<Vec<RowRange>> {
+        let FieldSource::DataFile { file, .. } = plan.sources.first()? else {
+            return None;
+        };
+        let (first, last) = file.row_id_range()?;
+        self.compute_candidate_ranges(split, plan, first, selected_ranges, 
sidecar_ranges)
+            .await
+            .unwrap_or_else(|error| {
+                log::warn!(
+                    "Parquet statistics pruning failed for row IDs 
{first}..={last} in {}. Using the original row selection ({error})",
+                    split.bucket_path()
+                );
+                None
+            })
+    }
+
+    async fn compute_candidate_ranges(
+        &self,
+        split: &DataSplit,
+        plan: &mut SourcePlan,
+        first_row_id: i64,
+        selected_ranges: Option<&[RowRange]>,
+        sidecar_ranges: Option<&[RowRange]>,
+    ) -> crate::Result<Option<Vec<RowRange>>> {
+        for source in &plan.sources {
+            if !self.can_use_source_stats(source, sidecar_ranges)? {
+                return Ok(None);
+            }
+        }
+        let reader = ParquetFormatReader::with_options(&self.table_options, 
None)?;
+        let mut candidates: Option<Vec<RowRange>> = None;
+        let mut deferred_sources = Vec::new();
+        let mut loaded_metadata = Vec::new();
+        for source in &mut plan.sources {
+            let FieldSource::DataFile {
+                file,
+                data_fields,
+                read_fields,
+                parquet_metadata,
+            } = source
+            else {
+                return Ok(None);
+            };
+            if read_fields.is_empty() {
+                continue;
+            }
+            let file_fields = 
data_fields.as_deref().unwrap_or(&self.table_fields);
+            let winning_ids: HashSet<_> = read_fields.iter().map(|field| 
field.id()).collect();
+            let mut mapping = build_field_mapping(&self.table_fields, 
file_fields);
+            for (index, field) in mapping.iter_mut().zip(&self.table_fields) {
+                *index = index.filter(|&i| {
+                    winning_ids.contains(&field.id())
+                        && supports_stats(field.data_type())
+                        && !self.partition_fields.iter().any(|p| p.id() == 
field.id())
+                        && stats_evolution_is_safe(file_fields[i].data_type(), 
field.data_type())
+                });
+            }
+            // Remap a whole conjunct so an OR or NOT spanning providers stays 
residual.
+            let predicates: Vec<_> = self
+                .predicates
+                .iter()
+                .filter_map(|p| p.remap_field_index(&mapping))
+                .filter(|p| !matches!(p, Predicate::AlwaysTrue))
+                .collect();
+            if predicates.is_empty() {
+                deferred_sources.push((file, parquet_metadata));
+                continue;
+            }
+            let metadata = self
+                .cache_source_metadata(split, file, &reader, parquet_metadata)

Review Comment:
   Fixed in 
[`edc2753b`](https://github.com/apache/paimon-rust/pull/1469/commits/edc2753b4e5c6375e581e8ac36d8c0c82b269374).
 When the requested rows fit in one row group, we check the footer first. If 
its statistics prove that every row matches or none can match, we skip the 
`ColumnIndex`. Matching sparse reads keep the `OffsetIndex` and still evaluate 
the residual. When statistics produce candidates, projected page boundaries 
still require independent row counts, even if the candidate intersection leaves 
the requested selection unchanged.
   
   The [counting-I/O 
regression](https://github.com/SteNicholas/paimon-rust/blob/edc2753b4e5c6375e581e8ac36d8c0c82b269374/crates/paimon/src/table/data_evolution_reader/candidate_ranges_tests.rs#L492-L530)
 covers the 100,000-row, 49-column fixture with 64 rows per page and a narrow 
fixture with one-row pages. With pushdown on or off, the nullable nonselective 
filter returns the same rows and reads the same column-data and total bytes. 
The test also covers two selected rows in one row group and verifies that a 
footer-rejected selection decodes no rows.
   



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