a-agmon commented on code in PR #588: URL: https://github.com/apache/iceberg-rust/pull/588#discussion_r1749163419
########## crates/integrations/datafusion/src/physical_plan/expr_to_predicate.rs: ########## @@ -0,0 +1,312 @@ +// 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::VecDeque; + +use datafusion::common::tree_node::{TreeNodeRecursion, TreeNodeVisitor}; +use datafusion::common::Column; +use datafusion::error::DataFusionError; +use datafusion::logical_expr::{Expr, Operator}; +use datafusion::scalar::ScalarValue; +use iceberg::expr::{Predicate, Reference}; +use iceberg::spec::Datum; + +pub struct ExprToPredicateVisitor { + stack: VecDeque<Option<Predicate>>, +} +impl ExprToPredicateVisitor { + /// Create a new predicate conversion visitor. + pub fn new() -> Self { + Self { + stack: VecDeque::new(), + } + } + /// Get the predicate from the stack. + pub fn get_predicate(&self) -> Option<Predicate> { + self.stack + .iter() + .filter_map(|opt| opt.clone()) + .reduce(Predicate::and) + } + + /// Convert a column expression to an iceberg predicate. + fn convert_column_expr( + &self, + col: &Column, + op: &Operator, + lit: &ScalarValue, + ) -> Option<Predicate> { + let reference = Reference::new(col.name.clone()); + let datum = scalar_value_to_datum(lit)?; + Some(binary_op_to_predicate(reference, op, datum)) + } + + /// Convert a compound expression to an iceberg predicate. + /// + /// The strategy is to support the following cases: + /// - if its an AND expression then the result will be the valid predicates, whether there are 2 or just 1 + /// - if its an OR expression then a predicate will be returned only if there are 2 valid predicates on both sides + fn convert_compound_expr(&self, valid_preds: &[Predicate], op: &Operator) -> Option<Predicate> { + let valid_preds_count = valid_preds.len(); + match (op, valid_preds_count) { + (Operator::And, 1) => valid_preds.first().cloned(), Review Comment: I basically designed the conversion of filters to be greedy, which means we need to try and convert as many filters as we can in order to scan less files. suppose we have a case like this: ``` SELECT * FROM T WHERE A > 10 AND REGEXP_LIKE(name, '[a-z]A[a-zA-Z]{2}'); ``` This will translate into an expr that looks something like this ``` BinaryExpr(BinaryExpr AND RegExpExpr) ``` After we processed both leafs: `( BinaryExpr and RegExpExpr)`, we have on the stack just one valid expression because the `RegExpExpr` is not supported. Then we get to the parent: `BinaryExpr(A AND B)`, we try to get the 2 child expressions from the stack, but in this case we get only one (`A > 10`) because the second one is not supported (`REGEXP_LIKE`). In this case, because its a logical **AND** expr, we proceed with just one valid predicate. Thats the case you are highlighinting - cases where we process logical AND expr where just one side is valid. in this case - we are processing with just one predicate. If it was an **OR** expr that was wrapping both then we would have failed the whole predicate. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org