kbendick commented on a change in pull request #2062:
URL: https://github.com/apache/iceberg/pull/2062#discussion_r569816358



##########
File path: 
api/src/main/java/org/apache/iceberg/expressions/ManifestEvaluator.java
##########
@@ -329,5 +329,36 @@ public Boolean or(Boolean leftResult, Boolean rightResult) 
{
 
       return ROWS_MIGHT_MATCH;
     }
+
+    @Override
+    public <T> Boolean notStartsWith(BoundReference<T> ref, Literal<T> lit) {
+      int pos = Accessors.toPosition(ref.accessor());
+      PartitionFieldSummary fieldStats = stats.get(pos);
+
+      // values are all null (and stats exist) and literal cannot contain null
+      if (fieldStats.lowerBound() == null) {
+        return ROWS_CANNOT_MATCH;
+      }
+
+      ByteBuffer prefixAsBytes = lit.toByteBuffer();
+
+      Comparator<ByteBuffer> comparator = Comparators.unsignedBytes();
+
+      ByteBuffer lower = fieldStats.lowerBound();
+      // truncate lower bound so that its length in bytes is not greater than 
the length of prefix
+      int lowerLength = Math.min(prefixAsBytes.remaining(), lower.remaining());
+      int lowerCmp = comparator.compare(BinaryUtil.truncateBinary(lower, 
lowerLength), prefixAsBytes);
+      if (lowerCmp == 0) {
+        ByteBuffer upper = fieldStats.upperBound();
+        // truncate upper bound so that its length in bytes is not greater 
than the length of prefix
+        int upperLength = Math.min(prefixAsBytes.remaining(), 
upper.remaining());
+        int upperCmp = comparator.compare(BinaryUtil.truncateBinary(upper, 
upperLength), prefixAsBytes);
+        if (upperCmp == 0) {
+          return ROWS_CANNOT_MATCH;
+        }
+      }

Review comment:
       I have made the same changes as in `InclusiveMericsEvaluator`.

##########
File path: 
api/src/main/java/org/apache/iceberg/expressions/InclusiveMetricsEvaluator.java
##########
@@ -372,6 +372,38 @@ public Boolean or(Boolean leftResult, Boolean rightResult) 
{
       return ROWS_MIGHT_MATCH;
     }
 
+    @Override
+    public <T> Boolean notStartsWith(BoundReference<T> ref, Literal<T> lit) {
+      Integer id = ref.fieldId();
+
+      if (containsNullsOnly(id)) {
+        return ROWS_CANNOT_MATCH;
+      }
+
+      ByteBuffer prefixAsBytes = lit.toByteBuffer();
+
+      Comparator<ByteBuffer> comparator = Comparators.unsignedBytes();
+
+      if (lowerBounds != null && upperBounds != null &&

Review comment:
       I've updated to this definition.

##########
File path: 
api/src/test/java/org/apache/iceberg/expressions/TestInclusiveManifestEvaluator.java
##########
@@ -389,6 +396,39 @@ public void testStringStartsWith() {
     Assert.assertFalse("Should skip: range doesn't match", shouldRead);
   }
 
+  @Test
+  public void testStringNotStartsWith() {
+    boolean shouldRead = 
ManifestEvaluator.forRowFilter(notStartsWith("some_nulls", "a"), SPEC, 
false).eval(FILE);
+    Assert.assertTrue("Should read: range matches", shouldRead);
+
+    shouldRead = ManifestEvaluator.forRowFilter(notStartsWith("some_nulls", 
"aa"), SPEC, false).eval(FILE);
+    Assert.assertTrue("Should read: range matches", shouldRead);
+
+    shouldRead = ManifestEvaluator.forRowFilter(notStartsWith("some_nulls", 
"dddd"), SPEC, false).eval(FILE);
+    Assert.assertTrue("Should read: range matches", shouldRead);
+
+    shouldRead = ManifestEvaluator.forRowFilter(notStartsWith("some_nulls", 
"z"), SPEC, false).eval(FILE);
+    Assert.assertTrue("Should read: range matches", shouldRead);
+
+    shouldRead = ManifestEvaluator.forRowFilter(notStartsWith("no_nulls", 
"a"), SPEC, false).eval(FILE);
+    Assert.assertTrue("Should read: range matches", shouldRead);
+
+    shouldRead = ManifestEvaluator.forRowFilter(notStartsWith("some_nulls", 
"zzzz"), SPEC, false).eval(FILE);
+    Assert.assertTrue("Should read: range matches", shouldRead);
+
+    shouldRead = ManifestEvaluator.forRowFilter(notStartsWith("some_nulls", 
"1"), SPEC, false).eval(FILE);
+    Assert.assertTrue("Should read: range matches", shouldRead);

Review comment:
       I had some concerns about my original Spark based check, so I checked 
again. I was mistaken when I initially checked to see what Spark returns, as 
Spark implements SQL 3-way boolean logic, for example:
   
   ```
   scala> val df = Seq("aa", "bb", null, "b").toDF("word")
   df: org.apache.spark.sql.DataFrame = [word: string]
   scala> df.createOrReplaceTempView("t")
   scala> df.select(not($"word".startsWith("bb"))).show
   +--------------------------+
   |(NOT startswith(word, bb))|
   +--------------------------+
   |                      true|
   |                     false|
   |                      null|
   |                      true|
   +--------------------------+
   ```
   
   However, Iceberg does not implement 3-way boolean logic. So we need to make 
a decision on whether or not we want Iceberg to return `true` or `false` in 
this case.
   
   I discussed this offline with @rdblue and we've come up with the following 
rationale for an `all_nulls` file matching a `notStartsWith` filter:
   
   The argument in favor of returning `true` is stronger than deciding to 
return `false`, i.e. by saying that null does not start with "bb" or whatever 
the predicate's value is. The reason for this is that:
   (1) Logically, null does not start with "bb" (or any non-null value)
   (2) If we have to choose between Iceberg surfacing too many values or too 
few, we would want Iceberg to return more values and then allow the query 
engine to make the final decision on the null value. If we return too few 
values, the query engine might not return the results that it would have if the 
underlying table format were not Iceberg. However, if we return too many values 
from the predicate filters, as in this case `true` means maybe and so the query 
engine can still ensure that the result is correct (according to the query 
engines definition of correct). When `false` means definitely not, the query 
engine has no ability to catch anything.

##########
File path: 
api/src/test/java/org/apache/iceberg/expressions/TestPredicateBinding.java
##########
@@ -108,6 +110,25 @@ public void testComparisonPredicateBinding() {
     }
   }
 
+  @Test
+  @SuppressWarnings("unchecked")
+  public void tesPredicateBindingForStringPrefixComparisons() {

Review comment:
       Good eyes. I've fixed this typo.




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

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to