a-agmon commented on code in PR #588: URL: https://github.com/apache/iceberg-rust/pull/588#discussion_r1749162764
########## 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(), + (Operator::And, 2) => Some(Predicate::and( + valid_preds[0].clone(), + valid_preds[1].clone(), + )), + (Operator::Or, 2) => Some(Predicate::or( + valid_preds[0].clone(), + valid_preds[1].clone(), + )), + _ => None, + } + } +} + +// Implement TreeNodeVisitor for ExprToPredicateVisitor +impl<'n> TreeNodeVisitor<'n> for ExprToPredicateVisitor { + type Node = Expr; + + fn f_down(&mut self, _node: &'n Self::Node) -> Result<TreeNodeRecursion, DataFusionError> { + Ok(TreeNodeRecursion::Continue) + } + + fn f_up(&mut self, node: &'n Self::Node) -> Result<TreeNodeRecursion, DataFusionError> { + if let Expr::BinaryExpr(binary) = node { + match (&*binary.left, &binary.op, &*binary.right) { + // process simple expressions (involving a column, operator and literal) + (Expr::Column(col), op, Expr::Literal(lit)) => { Review Comment: Thanks for the comment @liurenjie1024 I have added support for both "directions" of binary statement: `X > 1` and `1 < X`. But I think there is something more to be clarified: Both `x>1` and `1<x` are binary statements. When the binary statement `foo > 1` is processed then there are 3 main leafs to handle: ``` 1 - Column(Column { relation: Some(Bare { table: "my_table" }), name: "foo" }) ``` This ^ will be ignored as its not a binary expr (its a column expr) ``` 2 - Literal(Int64(1)) ``` This ^ will also be ignored as its not a binary expr (its a literal expr) ``` 3 - BinaryExpr(BinaryExpr { left: Column(Column { relation: Some(Bare { table: "my_table" }), name: "foo" }), op: Gt, right: Literal(Int64(1)) }) ``` This binary expr will be captured, deconstructed, and processed by this match arm ``` match (&*binary.left, &binary.op, &*binary.right) { (Expr::Column(col), op, Expr::Literal(lit)) => { ... } ``` Therefore, in order to support `a>6` we dont need to capture `Expr:Column`, only the binary statement that includes it. On the other hand, I dont think we want to be too flexible because there are expr we want to avoid. For example, `Expr::SimilarTo` (regexp comparison) is something I think we should leave to DataFusion to handle. TBH, I think there are very few Exp we want to handle, rather than leaving them to DataFusion, which are mostly where Iceberg can improve the datafusion scan by using predicates at the metadata level and pruning data files to be scanned. -- 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