Fokko commented on code in PR #231:
URL: https://github.com/apache/iceberg-rust/pull/231#discussion_r1520407376


##########
crates/iceberg/src/expr/predicate.rs:
##########
@@ -172,6 +223,146 @@ pub enum Predicate {
     Set(SetExpression<Reference>),
 }
 
+impl Bind for Predicate {
+    type Bound = BoundPredicate;
+
+    fn bind(self, schema: SchemaRef, case_sensitive: bool) -> 
Result<BoundPredicate> {
+        match self {
+            Predicate::And(expr) => {
+                let bound_expr = expr.bind(schema, case_sensitive)?;
+
+                let [left, right] = bound_expr.inputs;
+                Ok(match (left, right) {
+                    (_, r) if matches!(&*r, &BoundPredicate::AlwaysFalse) => {
+                        BoundPredicate::AlwaysFalse
+                    }
+                    (l, _) if matches!(&*l, &BoundPredicate::AlwaysFalse) => {
+                        BoundPredicate::AlwaysFalse
+                    }
+                    (left, r) if matches!(&*r, &BoundPredicate::AlwaysTrue) => 
*left,
+                    (l, right) if matches!(&*l, &BoundPredicate::AlwaysTrue) 
=> *right,
+                    (left, right) => 
BoundPredicate::And(LogicalExpression::new([left, right])),
+                })
+            }
+            Predicate::Not(expr) => {
+                let bound_expr = expr.bind(schema, case_sensitive)?;
+                let [inner] = bound_expr.inputs;
+                Ok(match inner {
+                    e if matches!(&*e, &BoundPredicate::AlwaysTrue) => 
BoundPredicate::AlwaysFalse,
+                    e if matches!(&*e, &BoundPredicate::AlwaysFalse) => 
BoundPredicate::AlwaysTrue,
+                    e => BoundPredicate::Not(LogicalExpression::new([e])),
+                })
+            }
+            Predicate::Or(expr) => {
+                let bound_expr = expr.bind(schema, case_sensitive)?;
+                let [left, right] = bound_expr.inputs;
+                Ok(match (left, right) {
+                    (_, r) if matches!(&*r, &BoundPredicate::AlwaysTrue) => {
+                        BoundPredicate::AlwaysTrue
+                    }
+                    (l, _) if matches!(&*l, &BoundPredicate::AlwaysTrue) => {
+                        BoundPredicate::AlwaysTrue
+                    }
+                    (left, r) if matches!(&*r, &BoundPredicate::AlwaysFalse) 
=> *left,
+                    (l, right) if matches!(&*l, &BoundPredicate::AlwaysFalse) 
=> *right,
+                    (left, right) => 
BoundPredicate::Or(LogicalExpression::new([left, right])),
+                })
+            }
+            Predicate::Unary(expr) => {
+                let bound_expr = expr.bind(schema, case_sensitive)?;
+
+                match &bound_expr.op {
+                    &PredicateOperator::IsNull => {
+                        if bound_expr.term.field().required {
+                            return Ok(BoundPredicate::AlwaysFalse);
+                        }
+                    }
+                    &PredicateOperator::NotNull => {
+                        if bound_expr.term.field().required {
+                            return Ok(BoundPredicate::AlwaysTrue);
+                        }
+                    }
+                    &PredicateOperator::IsNan | &PredicateOperator::NotNan => {
+                        if 
!bound_expr.term.field().field_type.is_floating_type() {
+                            return Err(Error::new(
+                                ErrorKind::DataInvalid,
+                                format!(
+                                    "Expecting floating point type, but found 
{}",
+                                    bound_expr.term.field().field_type
+                                ),
+                            ));
+                        }
+                    }
+                    op => {
+                        return Err(Error::new(
+                            ErrorKind::Unexpected,
+                            format!("Expecting unary operator,but found {op}"),

Review Comment:
   ```suggestion
                               format!("Expecting unary operator, but found 
{op}"),
   ```



##########
crates/iceberg/src/expr/predicate.rs:
##########
@@ -172,6 +223,146 @@ pub enum Predicate {
     Set(SetExpression<Reference>),
 }
 
+impl Bind for Predicate {
+    type Bound = BoundPredicate;
+
+    fn bind(self, schema: SchemaRef, case_sensitive: bool) -> 
Result<BoundPredicate> {
+        match self {
+            Predicate::And(expr) => {
+                let bound_expr = expr.bind(schema, case_sensitive)?;
+
+                let [left, right] = bound_expr.inputs;
+                Ok(match (left, right) {
+                    (_, r) if matches!(&*r, &BoundPredicate::AlwaysFalse) => {
+                        BoundPredicate::AlwaysFalse
+                    }
+                    (l, _) if matches!(&*l, &BoundPredicate::AlwaysFalse) => {
+                        BoundPredicate::AlwaysFalse
+                    }
+                    (left, r) if matches!(&*r, &BoundPredicate::AlwaysTrue) => 
*left,
+                    (l, right) if matches!(&*l, &BoundPredicate::AlwaysTrue) 
=> *right,
+                    (left, right) => 
BoundPredicate::And(LogicalExpression::new([left, right])),
+                })
+            }
+            Predicate::Not(expr) => {
+                let bound_expr = expr.bind(schema, case_sensitive)?;
+                let [inner] = bound_expr.inputs;
+                Ok(match inner {
+                    e if matches!(&*e, &BoundPredicate::AlwaysTrue) => 
BoundPredicate::AlwaysFalse,
+                    e if matches!(&*e, &BoundPredicate::AlwaysFalse) => 
BoundPredicate::AlwaysTrue,
+                    e => BoundPredicate::Not(LogicalExpression::new([e])),
+                })
+            }
+            Predicate::Or(expr) => {
+                let bound_expr = expr.bind(schema, case_sensitive)?;
+                let [left, right] = bound_expr.inputs;
+                Ok(match (left, right) {
+                    (_, r) if matches!(&*r, &BoundPredicate::AlwaysTrue) => {
+                        BoundPredicate::AlwaysTrue
+                    }
+                    (l, _) if matches!(&*l, &BoundPredicate::AlwaysTrue) => {
+                        BoundPredicate::AlwaysTrue
+                    }

Review Comment:
   Out of curiosity, why not collapse this into a single statement?
   ```suggestion
                       (l, r) if matches!(&*r, &BoundPredicate::AlwaysTrue) || 
matches!(&*l, &BoundPredicate::AlwaysTrue) => {
                           BoundPredicate::AlwaysTrue
                       }
   ```



-- 
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

Reply via email to