goutamadwant commented on code in PR #25819:
URL: https://github.com/apache/datafusion/pull/25819#discussion_r4372289144


##########
datafusion/catalog-listing/src/range_pruning.rs:
##########
@@ -0,0 +1,293 @@
+// 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 arrow::datatypes::{DataType, Schema};
+use datafusion_common::ScalarValue;
+use datafusion_datasource::file_groups::FileGroup;
+use datafusion_expr::logical_plan::RangePartitioning;
+use datafusion_expr::{Expr, Operator};
+use std::cmp::Ordering;
+
+/// Discard files only when a simple filter cannot match the declared range.
+/// Keep the empty group in place: its index is meaningful to range joins.
+pub(crate) fn prune_range_file_groups(
+    file_groups: &mut [FileGroup],
+    range: &RangePartitioning,
+    filters: &[Expr],
+    schema: &Schema,
+) {
+    let [sort_expr] = range.ordering() else {
+        return;
+    };
+    let Expr::Column(column) = sort_expr.expr.as_ref() else {
+        return;
+    };
+    let Ok(field) = schema.field_with_name(&column.name) else {
+        return;
+    };
+    let data_type = field.data_type();
+    // These types have the same total order in SQL comparisons and split 
points.
+    if !matches!(
+        data_type,
+        DataType::Int8
+            | DataType::Int16
+            | DataType::Int32
+            | DataType::Int64
+            | DataType::UInt8
+            | DataType::UInt16
+            | DataType::UInt32
+            | DataType::UInt64
+    ) {
+        return;
+    }
+
+    for (index, file_group) in file_groups.iter_mut().enumerate() {
+        let lower = index
+            .checked_sub(1)
+            .and_then(|i| range.split_points().get(i))
+            .and_then(|split| split.values().first());
+        let upper = range
+            .split_points()
+            .get(index)
+            .and_then(|split| split.values().first());
+        let (min, max) = if sort_expr.asc {
+            (
+                lower.map(|value| (value, false)),
+                upper.map(|value| (value, true)),
+            )
+        } else {
+            (
+                upper.map(|value| (value, true)),
+                lower.map(|value| (value, false)),
+            )
+        };
+
+        if filters
+            .iter()
+            .any(|filter| !may_match(filter, &column.name, data_type, min, 
max))
+        {
+            *file_group = FileGroup::new(vec![]);
+        }
+    }
+}
+
+type Bound<'a> = Option<(&'a ScalarValue, bool)>;
+
+fn may_match(
+    expr: &Expr,
+    column: &str,
+    data_type: &DataType,
+    min: Bound<'_>,
+    max: Bound<'_>,
+) -> bool {

Review Comment:
   so reworked this to reuse PruningPredicate through a range-statistics 
adapter. Unsupported cases conservatively keep the partitions. @stuhood pls 
verify if this looks good 



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to