2010YOUY01 commented on code in PR #19609: URL: https://github.com/apache/datafusion/pull/19609#discussion_r2658692767
########## datafusion/physical-expr-common/src/physical_expr/pruning.rs: ########## @@ -0,0 +1,539 @@ +// 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. + +// Pruner Common Structs/Utilities + +//! This is the top-level comment for pruning via statistics propagation. +//! +//! TODO: This is a concise draft; it should be polished for readers with less +//! prior background. +//! +//! # Introduction +//! +//! This module helps skip scanning data micro-partitions by evaluating predicates +//! against container-level statistics. +//! +//! It supports pruning for complex and nested predicates through statistics +//! propagation. +//! +//! For examples of pruning nested predicates via statistics propagation, see: +//! <https://github.com/apache/datafusion/issues/19487> +//! +//! +//! +//! # Vectorized pruning intermediate representation +//! +//! Source statistics and intermediate pruning results are stored in Arrow arrays, +//! enabling vectorized evaluation across many containers. +//! +//! +//! +//! # Difference from [`super::PhysicalExpr::evaluate_bounds`] +//! +//! `evaluate_bounds()` derives per-column statistics for a single plan, aimed at +//! tasks like cardinality estimation and other planner fast paths. It reasons +//! about one container and may track richer distribution details. +//! Pruning must reason about *all* containers (potentially thousands) to decide +//! which to skip, so it favors a vectorized, array-backed representation with +//! lighter-weight stats. These are intentionally separate interfaces. +//! +//! +//! +//! # Core API/Data Structures +//! +//! The key structures involved in pruning are: +//! - [`PruningStatistics`]: the input source statistics for all containers +//! - [`super::PhysicalExpr::evaluate_pruning()`]: evaluates pruning behavior for predicates +//! - [`PruningIntermediate`]: the intermediate result produced during statistics propagation for pruning. Its internal representation uses Arrow Arrays, enabling vectorized evaluation for performance. + +use std::{iter::repeat_n, sync::Arc}; + +use arrow::array::{Array, ArrayRef, BooleanArray, BooleanBuilder, UInt64Array}; +use arrow::compute::kernels::boolean::and_kleene; +use datafusion_common::pruning::PruningStatistics; +use datafusion_common::{Result, ScalarValue, assert_eq_or_internal_err}; + +/// Physical representation of pruning outcomes for each container: +/// `true` = KeepAll, `false` = SkipAll, `null` = Unknown. +/// +/// Use `BooleanArray` so the propagation steps can use existing Arrow kernels for +/// both simplicity and performance. +/// +/// # Pruning results +/// - KeepAll: The pruning predicate evaluates to true for all rows within a micro +/// partition. Future filter evaluation can be skipped for that partition. +/// - SkipAll: The pruning predicate evaluates to false for all rows within a micro +/// partition. The partition can be skipped at scan time. +/// - UnknownOrMixed: The statistics are insufficient to prove KeepAll/SkipAll, or +/// the predicate is mixed. The predicate must be evaluated row-wise. +/// +/// Example (`SELECT * FROM t WHERE x >= 0`): +/// - micro_partition_a(min=0, max=10): KeepAll — can pass through `FilterExec` +/// without re-evaluating `x >= 0`. +/// - micro_partition_b(min=-10, max=-1): SkipAll — skip the partition entirely. +/// - micro_partition_c(min=-5, max=5): Unknown — must evaluate the predicate on rows. +/// +/// `PruningOutcome` provides utilities to convert between this semantic +/// representation and its tri-state boolean encoding. +/// +/// # Important invariants +/// Pruning results must be sound, but need not be complete: +/// - If a container is labeled `KeepAll` or `SkipAll`, that label must be correct. +/// - If a container is labeled `Unknown` but is actually `KeepAll`/`SkipAll`, +/// correctness is still preserved; it just means pruning was conservative. +/// +/// Propagation implementation can be refined to reduce `Unknown` cases to improve +/// pruning effectiveness. +#[derive(Debug, Clone)] +pub struct PruningResults { + results: Option<BooleanArray>, Review Comment: I put them all in the struct comment of `PruningResults`, will also add links in the fields and other related structs -- 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]
