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


##########
crates/iceberg/src/expr/visitors/manifest_evaluator.rs:
##########
@@ -103,98 +106,245 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> 
{
         reference: &BoundReference,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field: &FieldSummary = 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 self.are_all_null(field, &reference.field().field_type) {
+            ROWS_CANNOT_MATCH
+        } else {
+            ROWS_MIGHT_MATCH
+        }
     }
 
+    #[allow(clippy::bool_comparison)]
     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: &FieldSummary = self.field_summary_for_reference(reference);
+        if field.contains_nan.is_some_and(|x| x == false) {
+            return ROWS_CANNOT_MATCH;
+        }
+
+        if self.are_all_null(field, &reference.field().field_type) {
+            return ROWS_CANNOT_MATCH;
+        }
+
+        ROWS_MIGHT_MATCH
     }
 
+    #[allow(clippy::bool_comparison)]
     fn not_nan(
         &mut self,
         reference: &BoundReference,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field: &FieldSummary = self.field_summary_for_reference(reference);
+        if field.contains_nan.is_some_and(|x| x == true)
+            && !field.contains_null
+            && field.lower_bound.is_none()
+        {
+            ROWS_CANNOT_MATCH
+        } else {
+            ROWS_MIGHT_MATCH
+        }
     }
 
     fn less_than(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field: &FieldSummary = self.field_summary_for_reference(reference);
+        if let Some(Literal::Primitive(lower_bound)) = &field.lower_bound {
+            Ok(lower_bound < datum.literal())
+        } else {
+            ROWS_CANNOT_MATCH
+        }
     }
 
     fn less_than_or_eq(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field: &FieldSummary = self.field_summary_for_reference(reference);
+        if let Some(Literal::Primitive(lower_bound)) = &field.lower_bound {
+            Ok(lower_bound <= datum.literal())
+        } else {
+            ROWS_CANNOT_MATCH
+        }
     }
 
     fn greater_than(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field: &FieldSummary = self.field_summary_for_reference(reference);
+        if let Some(Literal::Primitive(upper_bound)) = &field.upper_bound {
+            Ok(upper_bound > datum.literal())
+        } else {
+            ROWS_CANNOT_MATCH
+        }
     }
 
     fn greater_than_or_eq(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field: &FieldSummary = self.field_summary_for_reference(reference);
+        if let Some(Literal::Primitive(upper_bound)) = &field.upper_bound {
+            Ok(upper_bound >= datum.literal())
+        } else {
+            ROWS_CANNOT_MATCH
+        }
     }
 
     fn eq(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        Ok(self.less_than_or_eq(reference, datum, _predicate)?
+            && self.greater_than_or_eq(reference, datum, _predicate)?)
     }
 
     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: &FieldSummary = 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.chars().count();
+
+        if let Some(lower_bound) = &field.lower_bound {
+            let Literal::Primitive(PrimitiveLiteral::String(lower_bound)) = 
lower_bound else {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Cannot perform starts_with on non-string lower bound",
+                ));
+            };
+
+            let min_len = lower_bound.chars().count().min(prefix_len);
+            let truncated_lower_bound = 
lower_bound.chars().take(min_len).collect::<String>();
+            if prefix < &truncated_lower_bound {
+                return ROWS_CANNOT_MATCH;
+            }
+        }
+
+        if let Some(upper_bound) = &field.upper_bound {
+            let Literal::Primitive(PrimitiveLiteral::String(upper_bound)) = 
upper_bound else {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Cannot perform starts_with on non-string upper bound",
+                ));
+            };
+
+            let min_len = upper_bound.chars().count().min(prefix_len);
+            let truncated_upper_bound = 
upper_bound.chars().take(min_len).collect::<String>();
+            if prefix > &truncated_upper_bound {
+                return ROWS_CANNOT_MATCH;
+            }
+        }
+
+        ROWS_MIGHT_MATCH
     }
 
     fn not_starts_with(
         &mut self,
         reference: &BoundReference,
-        literal: &Datum,
+        datum: &Datum,
         _predicate: &BoundPredicate,
     ) -> crate::Result<bool> {
-        todo!()
+        let field: &FieldSummary = self.field_summary_for_reference(reference);
+
+        if field.contains_null || field.lower_bound.is_none() || 
field.upper_bound.is_none() {
+            return ROWS_MIGHT_MATCH;
+        }
+
+        let PrimitiveLiteral::String(prefix) = datum.literal() else {
+            return Err(Error::new(
+                ErrorKind::Unexpected,
+                "Cannot perform not_starts_with on non-string value",
+            ));
+        };
+
+        let prefix_len = prefix.chars().count();
+
+        // not_starts_with will match unless all values must start with the 
prefix. This happens when
+        // the lower and upper bounds both start with the prefix.
+        if let Some(lower_bound) = &field.lower_bound {
+            let Literal::Primitive(PrimitiveLiteral::String(lower_bound)) = 
lower_bound else {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Cannot perform not_starts_with on non-string lower bound",
+                ));
+            };
+
+            // if lower is shorter than the prefix then lower doesn't start 
with the prefix
+            if prefix_len > lower_bound.chars().count() {
+                return ROWS_MIGHT_MATCH;
+            }
+
+            let truncated_lower_bound = 
lower_bound.chars().take(prefix_len).collect::<String>();
+            if prefix == &truncated_lower_bound {
+                if let Some(upper_bound) = &field.upper_bound {
+                    let 
Literal::Primitive(PrimitiveLiteral::String(upper_bound)) = upper_bound
+                    else {
+                        return Err(Error::new(
+                            ErrorKind::Unexpected,
+                            "Cannot perform not_starts_with on non-string 
upper bound",
+                        ));
+                    };
+
+                    // if upper is shorter than the prefix then upper can't 
start with the prefix
+                    if prefix_len > upper_bound.chars().count() {
+                        return ROWS_MIGHT_MATCH;
+                    }
+
+                    let truncated_upper_bound =

Review Comment:
   On reflection, I think we can just do this:
   
   ```rust
   if prefix.as_bytes().eq(&lower_bound.as_bytes()[..prefix.len()]) {
   ```
   
   Since we know `prefix` is correctly formed UTF-8 as it is a `&String`, if 
all we are doing is comparing equality of the first n chars, if all of the 
initial bytes of the bound are the same as in `prefix` then the slice must also 
fall on a char boundary.
   
   



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