stuhood commented on code in PR #25819: URL: https://github.com/apache/datafusion/pull/25819#discussion_r4372857139
########## datafusion/pruning/src/range.rs: ########## @@ -0,0 +1,419 @@ +// 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 std::collections::HashSet; +use std::sync::Arc; + +use arrow::array::{ArrayRef, BooleanArray, UInt64Array}; +use datafusion_common::{Column, ScalarValue}; +use datafusion_physical_expr::RangePartitioning; +use datafusion_physical_expr::expressions::Column as PhysicalColumn; + +use crate::PruningStatistics; + +/// Bounds implied by the effective split points of a [`RangePartitioning`]. +/// +/// Each partition is a container for [`crate::PruningPredicate`]. The returned +/// mask can be used by scans or physical operators without changing partition +/// identities. The range metadata must describe the rows' actual distribution. +/// +/// Currently, min/max bounds are provided for a single direct integer column. +/// Other keys are unknown, including lexicographic multi-column ranges: applying +/// each component's split points independently would not give valid bounds. +/// Exclusive integer endpoints are converted to closed bounds with checked +/// arithmetic. Overflow leaves the bound unknown rather than wrapping it. +/// +/// # Public Only for Internal Use: +/// `datafusion-catalog-listing` uses this adapter to prune declared file groups. +/// It can also be shared by physical optimizers without depending on catalog +/// implementations; it is not yet a supported downstream extension point. +#[doc(hidden)] +pub struct RangePruningStatistics<'a> { + range: &'a RangePartitioning, +} + +impl<'a> RangePruningStatistics<'a> { + /// Create statistics from normalized physical range metadata. + pub fn new(range: &'a RangePartitioning) -> Self { + Self { range } + } + + fn matches_column(&self, column: &Column) -> bool { + let [sort] = self.range.ordering().as_ref() else { + return false; + }; + sort.expr + .downcast_ref::<PhysicalColumn>() + .is_some_and(|key| key.name() == column.name()) + } + + fn bounds(&self, column: &Column, minimum: bool) -> Option<ArrayRef> { + if !self.matches_column(column) { + return None; + } + let data_type = self.range.split_points().first()?.values()[0].data_type(); + if !data_type.is_integer() { Review Comment: `PruningStatistics` does not require exact tight bounds — any over-approximation `[min, max]` that contains all values in the partition is valid. So to support non-integer datatypes, you should be able to only tighten bounds for overflows with +/- 1 when integers are in use, and otherwise just clone the boundary type...? The two cases to watch for are `data_type.is_nested()` and `has_float_leaf(&data_type)`: continuing to bail out with `None` would be the right choice there... but everything else should be prunable. -- 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]
