Anurag-S1ngh commented on code in PR #19164:
URL: https://github.com/apache/iceberg/pull/19164#discussion_r4811055629


##########
api/src/main/java/org/apache/iceberg/expressions/ResidualEvaluator.java:
##########
@@ -243,19 +243,15 @@ public <T> Expression predicate(BoundPredicate<T> pred) {
       for (PartitionField part : parts) {
 
         // checking the strict projection
-        UnboundPredicate<?> strictProjection =
-            ((Transform<T, ?>) part.transform()).projectStrict(part.name(), 
pred);
+        Expression strictProjection =
+            ((Transform<T, ?>) part.transform()).project(part.name(), pred, 
true);
         Expression strictResult = null;
 
         if (strictProjection != null) {
-          Expression bound = strictProjection.bind(spec.partitionType(), 
caseSensitive);
-          if (bound instanceof BoundPredicate) {
-            strictResult = super.predicate((BoundPredicate<?>) bound);
-          } else {
-            // if the result is not a predicate, then it must be a constant 
like alwaysTrue or
-            // alwaysFalse
-            strictResult = bound;
-          }
+          strictResult =
+              new Evaluator(spec.partitionType(), strictProjection, 
caseSensitive).eval(struct)

Review Comment:
   Pre-bound the projections per predicate and partition field. The bound 
predicate and its strict and inclusive evaluators are cached, so `residualFor` 
no longer builds or binds a projection per partition. There is no JMH setup in 
`iceberg-api`, so I did not add a benchmark; the per-call binding that produced 
the delta is gone.



##########
api/src/test/java/org/apache/iceberg/transforms/TestTruncatesProjection.java:
##########
@@ -298,6 +336,193 @@ public void testLongInclusiveUpperBound() {
         spec, notIn("value", value, value - 1), Expression.Operation.TRUE);
   }
 
+  private static Stream<Arguments> integerTypes() {
+    return Stream.of(
+        Arguments.of(Types.IntegerType.get(), (long) Integer.MIN_VALUE),
+        Arguments.of(Types.LongType.get(), Long.MIN_VALUE));
+  }
+
+  private static Object sourceValue(Type type, long value) {
+    if (type.typeId() == Type.TypeID.INTEGER) {
+      return (int) value;
+    }
+
+    return value;
+  }
+
+  @SuppressWarnings("unchecked")
+  private static Object transform(PartitionSpec spec, Type type, long value) {
+    Transform<Object, Object> transform =
+        (Transform<Object, Object>) spec.fields().get(0).transform();
+    return transform.bind(type).apply(sourceValue(type, value));
+  }
+
+  private static long firstNormalPartition(long minimum, int width) {
+    return minimum + (width - Math.floorMod(minimum, width));
+  }
+
+  private static boolean evalProjection(
+      PartitionSpec spec, UnboundPredicate<?> filter, boolean strict, Object 
partitionValue) {
+    Expression projection =
+        strict
+            ? Projections.strict(spec).project(filter)
+            : Projections.inclusive(spec).project(filter);
+    return new Evaluator(spec.partitionType(), projection, true)
+        .eval(TestHelpers.Row.of(partitionValue));
+  }
+
+  private static void assertProjectionEvaluation(
+      PartitionSpec spec,
+      UnboundPredicate<?> filter,
+      boolean strict,
+      Object partitionValue,
+      boolean expected) {
+    assertThat(evalProjection(spec, filter, strict, 
partitionValue)).isEqualTo(expected);
+  }
+
+  @ParameterizedTest

Review Comment:
   Added a bounded test over widths 1, 2, 3, 4, 8, 10, 1000, 2^29, 2^30-1, and 
2^30, with values at both type bounds, firstNormal, 0, and around each. It 
checks that inclusive never drops a matching source value and strict never 
accepts a non-matching one, plus residuals, legacy methods, and strict `<` / 
`<=` on null partitions. Reintroducing `firstNormal - 2` or dropping `notNull` 
makes the new tests fail.



##########
api/src/main/java/org/apache/iceberg/expressions/ResidualEvaluator.java:
##########
@@ -243,19 +243,15 @@ public <T> Expression predicate(BoundPredicate<T> pred) {
       for (PartitionField part : parts) {
 
         // checking the strict projection
-        UnboundPredicate<?> strictProjection =
-            ((Transform<T, ?>) part.transform()).projectStrict(part.name(), 
pred);
+        Expression strictProjection =
+            ((Transform<T, ?>) part.transform()).project(part.name(), pred, 
true);
         Expression strictResult = null;
 
         if (strictProjection != null) {
-          Expression bound = strictProjection.bind(spec.partitionType(), 
caseSensitive);
-          if (bound instanceof BoundPredicate) {
-            strictResult = super.predicate((BoundPredicate<?>) bound);
-          } else {
-            // if the result is not a predicate, then it must be a constant 
like alwaysTrue or
-            // alwaysFalse
-            strictResult = bound;
-          }
+          strictResult =
+              new Evaluator(spec.partitionType(), strictProjection, 
caseSensitive).eval(struct)

Review Comment:
   Removed the leaf overrides and the unused `Comparator`, `Set`, and `NaNUtil` 
imports, and `ResidualVisitor` now extends `ExpressionVisitor<Expression>`.



-- 
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: [email protected]

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