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


##########
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:
   There is existing code for this that you should probably use: see 
`PruningPredicate`/`PruningStatistics` and/or `ExprIntervalGraph`.
   
   It looks like `PruningPredicate` might be the better fit, because it was 
built for this kind of evaluation.



##########
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,
+) {

Review Comment:
   So, operating on `logical_plan`'s `RangePartitioning` means that this all 
happens before split points are normalized during creation of 
`datafusion_physical_expr`'s `RangePartitioning`. That always means that 
followup work in #25437 (which will want to operate at the physical level) will 
not be able to reuse this logic. #25279 will also want to consume this at the 
`datafusion_physical_expr` level. cc @gstamatakis95
   
   Consider combining `PruningPredicate`/`PruningStatistics` with 
`datafusion_physical_expr::RangePartitioning` instead?



##########
datafusion/catalog-listing/src/table.rs:
##########
@@ -659,9 +660,20 @@ impl ListingTable {
             file_groups: mut partitioned_file_lists,
             statistics,
             grouped_by_partition: partitioned_by_file_group,
-        } = self
-            .list_files_for_scan(state, &partition_filters, 
statistic_file_limit)
-            .await?;
+        } = if let Some(output_partitioning @ LogicalPartitioning::Range(_)) =
+            declared_output_partitioning
+        {
+            self.list_files_for_declared_output_partitioning(
+                state,
+                output_partitioning,
+                &partition_filters,
+                &filters,
+            )
+            .await?
+        } else {
+            self.list_files_for_scan(state, &partition_filters, 
statistic_file_limit)
+                .await?
+        };

Review Comment:
   This logic should maybe be pushed down into `list_files_for_scan`, since it 
is a public API?



##########
datafusion/catalog-listing/src/table.rs:
##########
@@ -1061,9 +1075,18 @@ impl ListingTable {
         if !file_groups.is_empty() {
             file_groups.resize_with(file_group_count, || 
FileGroup::new(vec![]));
         }
-        let file_groups =
+        let mut file_groups =
             self.filter_declared_file_groups_by_partition_filters(file_groups, 
filters)?;
 
+        if let LogicalPartitioning::Range(range) = output_partitioning {
+            prune_range_file_groups(
+                &mut file_groups,
+                range,
+                range_filters,
+                &self.table_schema,
+            );
+        }

Review Comment:
   `collect_files_for_scan` is going to actually read the stats of all of the 
files it collects (via `do_collect_statistics_and_ordering`), but then shortly 
afterward we will prune partitions.
   
   Moving `split_files` + `prune_range_file_groups` before 
`do_collect_statistics_and_ordering` would avoid fetching statistics for files 
that will be pruned.



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