marvinlanhenke commented on code in PR #367:
URL: https://github.com/apache/iceberg-rust/pull/367#discussion_r1636931579


##########
crates/iceberg/src/expr/visitors/manifest_evaluator.rs:
##########
@@ -103,98 +106,255 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> 
{
         reference: &BoundReference,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field = self.field_summary_for_reference(reference);
+
+        // contains_null encodes whether at least one partition value is null,
+        // lowerBound is null if all partition values are null
+        if ManifestFilterVisitor::are_all_null(field, 
&reference.field().field_type) {
+            ROWS_CANNOT_MATCH
+        } else {
+            ROWS_MIGHT_MATCH
+        }
     }
 
     fn is_nan(
         &mut self,
         reference: &BoundReference,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        Ok(self
-            .field_summary_for_reference(reference)
-            .contains_nan
-            .is_some())
+        let field = self.field_summary_for_reference(reference);
+        if let Some(contains_nan) = field.contains_nan {
+            if !contains_nan {
+                return ROWS_CANNOT_MATCH;
+            }
+        }
+
+        if ManifestFilterVisitor::are_all_null(field, 
&reference.field().field_type) {
+            return ROWS_CANNOT_MATCH;
+        }
+
+        ROWS_MIGHT_MATCH
     }
 
     fn not_nan(
         &mut self,
         reference: &BoundReference,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field = self.field_summary_for_reference(reference);
+        if let Some(contains_nan) = field.contains_nan {
+            // check if all values are nan
+            if contains_nan && !field.contains_null && 
field.lower_bound.is_none() {
+                return ROWS_CANNOT_MATCH;
+            }
+        }
+        ROWS_MIGHT_MATCH
     }
 
     fn less_than(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field = self.field_summary_for_reference(reference);
+        match &field.lower_bound {
+            Some(bound) if datum <= bound => ROWS_CANNOT_MATCH,
+            Some(_) => ROWS_MIGHT_MATCH,
+            None => ROWS_CANNOT_MATCH,
+        }
     }
 
     fn less_than_or_eq(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field = self.field_summary_for_reference(reference);
+        match &field.lower_bound {
+            Some(bound) if datum < bound => ROWS_CANNOT_MATCH,
+            Some(_) => ROWS_MIGHT_MATCH,
+            None => ROWS_CANNOT_MATCH,
+        }
     }
 
     fn greater_than(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field = self.field_summary_for_reference(reference);
+        match &field.upper_bound {
+            Some(bound) if datum >= bound => ROWS_CANNOT_MATCH,
+            Some(_) => ROWS_MIGHT_MATCH,
+            None => ROWS_CANNOT_MATCH,
+        }
     }
 
     fn greater_than_or_eq(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field = self.field_summary_for_reference(reference);
+        match &field.upper_bound {
+            Some(bound) if datum > bound => ROWS_CANNOT_MATCH,
+            Some(_) => ROWS_MIGHT_MATCH,
+            None => ROWS_CANNOT_MATCH,
+        }
     }
 
     fn eq(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field = self.field_summary_for_reference(reference);
+
+        if field.lower_bound.is_none() || field.upper_bound.is_none() {
+            return ROWS_CANNOT_MATCH;
+        }
+
+        if let Some(lower_bound) = &field.lower_bound {
+            if lower_bound > datum {
+                return ROWS_CANNOT_MATCH;
+            }
+        }
+
+        if let Some(upper_bound) = &field.upper_bound {
+            if upper_bound < datum {
+                return ROWS_CANNOT_MATCH;
+            }
+        }
+
+        ROWS_MIGHT_MATCH
     }
 
     fn not_eq(
         &mut self,
-        reference: &BoundReference,
-        literal: &Datum,
+        _reference: &BoundReference,
+        _datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        // because the bounds are not necessarily a min or max value, this 
cannot be answered using
+        // them. notEq(col, X) with (X, Y) doesn't guarantee that X is a value 
in col.
+        ROWS_MIGHT_MATCH
     }
 
     fn starts_with(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field = self.field_summary_for_reference(reference);
+
+        if field.lower_bound.is_none() || field.upper_bound.is_none() {
+            return ROWS_CANNOT_MATCH;
+        }
+
+        let PrimitiveLiteral::String(prefix) = datum.literal() else {
+            return Err(Error::new(
+                ErrorKind::Unexpected,
+                "Cannot perform starts_with on non-string value",
+            ));
+        };
+
+        let prefix_len = prefix.len();
+
+        if let Some(lower_bound) = &field.lower_bound {
+            let PrimitiveLiteral::String(lower_bound) = lower_bound.literal() 
else {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Cannot perform starts_with on non-string lower bound",
+                ));
+            };
+
+            let min_len = lower_bound.len().min(prefix_len);
+            if prefix.as_bytes().lt(&lower_bound.as_bytes()[..min_len]) {
+                return ROWS_CANNOT_MATCH;
+            }
+        }
+
+        if let Some(upper_bound) = &field.upper_bound {
+            let PrimitiveLiteral::String(upper_bound) = upper_bound.literal() 
else {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Cannot perform starts_with on non-string upper bound",
+                ));
+            };

Review Comment:
   > (e.g. lines 283 to 289)
   
   @s-akhtar-baig those were the lines I was referring to. 
   
   Something like that might work?
   
   ```Rust
   // mostly pseudo code
   fn check_boundaries(field: &FieldSummary) -> Result<&String> {
     match &field.lower_bound {
       Some(lower_bound) => {
         let PrimitiveLiteral::String(lower_bound) = lower_bound.literal() else 
{
           return Err(Error::new(ErrorKind::Unexpected, "Cannot perform..."));
         }
         Ok(lower_bound)
       }
      None => Err(Error::new(...))
     }
   }
   ```
   
   If that works you can probably create a small macro to avoid having a lower 
and upper bound variant?
   ```Rust
   macro_rules! check_boundaries {
       ($field:expr, $boundary:ident) => {
           match &$field.$boundary {
               Some(bound_value) => {
   //...the rest
   ```



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