alamb commented on code in PR #8437: URL: https://github.com/apache/arrow-datafusion/pull/8437#discussion_r1426953742
########## datafusion/physical-expr/src/utils/guarantee.rs: ########## @@ -0,0 +1,518 @@ +// 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. + +//! [`LiteralGuarantee`] predicate analysis to determine if a column is a +//! constant. + +use crate::utils::split_disjunction; +use crate::{split_conjunction, PhysicalExpr}; +use datafusion_common::{Column, ScalarValue}; +use datafusion_expr::Operator; +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; + +/// Represents a known guarantee that a column is either one of a set of values +/// or not one of a set of values. +/// +/// `LiteralGuarantee`s can be used to simplify expressions and skip data files +/// (or row groups in parquet files). For example, if we know that `a = 1` then +/// we can skip any partition that does not contain `a = 1`. +/// +/// See [`LiteralGuarantee::analyze`] to extract literal guarantees from a +/// predicate. +/// +/// # Details +/// A guarantee can be one of two forms: +/// +/// 1. One of particular set of values. For example, `(a = 1)`, `(a = 1 OR a = +/// 2) or `a IN (1, 2, 3)` +/// +/// 2. NOT one of a particular set of values. For example, `(a != 1)`, `(a != 1 +/// AND a != 2)` or `a NOT IN (1, 2, 3)` +/// +#[derive(Debug, Clone, PartialEq)] +pub struct LiteralGuarantee { + pub column: Column, + pub guarantee: Guarantee, + pub literals: HashSet<ScalarValue>, +} + +/// What is be guaranteed about the values? +#[derive(Debug, Clone, PartialEq)] +pub enum Guarantee { + /// `column` is one of a set of constant values + In, + /// `column` is NOT one of a set of constant values + NotIn, +} + +impl LiteralGuarantee { + /// Create a new instance of the guarantee if the provided operator is + /// supported. Returns None otherwise. See [`LiteralGuarantee::analyze`] to + /// create these structures from an expression. + fn try_new<'a>( + column_name: impl Into<String>, + op: &Operator, + literals: impl IntoIterator<Item = &'a ScalarValue>, + ) -> Option<Self> { + let guarantee = match op { + Operator::Eq => Guarantee::In, + Operator::NotEq => Guarantee::NotIn, + _ => return None, + }; + + let literals: HashSet<_> = literals.into_iter().cloned().collect(); + + Some(Self { + column: Column::from_name(column_name), + guarantee, + literals, + }) + } + + /// Return a list of `LiteralGuarantees` for the specified predicate that + /// hold if the predicate (filter) expression evaluates to `true`. + /// + /// `expr` should be a boolean expression + /// + /// Note: this function does not simplify the expression prior to analysis. + pub fn analyze(expr: &Arc<dyn PhysicalExpr>) -> Vec<LiteralGuarantee> { + split_conjunction(expr) + .into_iter() + .fold(GuaranteeBuilder::new(), |builder, expr| { + if let Some(cel) = ColOpLit::try_new(expr) { + return builder.aggregate_conjunct(cel); + } else { + // look for (col <op> literal) OR (col <op> literal) ... + let disjunctions = split_disjunction(expr); + + let terms = disjunctions + .iter() + .filter_map(|expr| ColOpLit::try_new(expr)) + .collect::<Vec<_>>(); + + if terms.is_empty() { + return builder; + } + + // if not all terms are of the form (col <op> literal), + // can't infer anything + if terms.len() != disjunctions.len() { + return builder; + } Review Comment: Thank k you -- I plan to have updated the code again by that time -- 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]
