amogh-jahagirdar commented on code in PR #9710:
URL: https://github.com/apache/iceberg/pull/9710#discussion_r1507790990


##########
api/src/test/java/org/apache/iceberg/expressions/TestEvaluator.java:
##########
@@ -348,6 +352,102 @@ public void testNotStartsWith() {
         .isTrue();
   }
 
+  @Test
+  public void testEndsWith() {
+    StructType struct = StructType.of(required(24, "s", 
Types.StringType.get()));
+    Evaluator evaluator = new Evaluator(struct, endsWith("s", "abc"));
+    assertThat(evaluator.eval(TestHelpers.Row.of("abc")))
+        .as("abc endsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("bcx")))
+        .as("bcx endsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("abC")))
+        .as("abC endsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("c")))
+        .as("c endsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("aabc")))
+        .as("aabc endsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of((String) null)))
+        .as("null endsWith abc should be false")
+        .isFalse();
+  }
+
+  @Test
+  public void testNotEndsWith() {
+    StructType struct = StructType.of(required(24, "s", 
Types.StringType.get()));
+    Evaluator evaluator = new Evaluator(struct, notEndsWith("s", "abc"));
+    assertThat(evaluator.eval(TestHelpers.Row.of("abc")))
+        .as("abc notEndsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("abcx")))
+        .as("abcx notEndsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("abC")))
+        .as("abC notEndsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("c")))
+        .as("c notEndsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("ababc")))
+        .as("ababc notEndsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("AbAbc")))
+        .as("AbAbc notEndsWith abc should be true")
+        .isTrue();
+  }
+
+  @Test
+  public void testContains() {
+    StructType struct = StructType.of(required(24, "s", 
Types.StringType.get()));
+    Evaluator evaluator = new Evaluator(struct, contains("s", "abc"));
+    assertThat(evaluator.eval(TestHelpers.Row.of("abc")))
+        .as("abc contains abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("bx")))
+        .as("bx contains abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("abC")))
+        .as("abC contains abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("c")))
+        .as("c contains abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("aabca")))
+        .as("aabca contains abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of((String) null)))
+        .as("null contains abc should be false")
+        .isFalse();
+  }
+
+  @Test
+  public void testNotContains() {

Review Comment:
   Can you add a notContains test for a null row? 



##########
api/src/main/java/org/apache/iceberg/expressions/ExpressionVisitors.java:
##########
@@ -266,6 +294,22 @@ public <T> R notStartsWith(Bound<T> expr, Literal<T> lit) {
       throw new UnsupportedOperationException("Unsupported operation.");
     }
 
+    public <T> R endsWith(Bound<T> expr, Literal<T> lit) {
+      throw new UnsupportedOperationException("Unsupported operation.");

Review Comment:
   Could you include the relevant details in these exception messages? 
"endsWith is an unsupported operation" etc.



##########
api/src/test/java/org/apache/iceberg/transforms/TestNotEndsWith.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.transforms;
+
+import static org.apache.iceberg.expressions.Expressions.notEndsWith;
+import static org.apache.iceberg.types.Types.NestedField.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.TestHelpers;
+import org.apache.iceberg.expressions.Binder;
+import org.apache.iceberg.expressions.BoundPredicate;
+import org.apache.iceberg.expressions.Evaluator;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Projections;
+import org.apache.iceberg.expressions.True;
+import org.apache.iceberg.expressions.UnboundPredicate;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.Test;
+
+public class TestNotEndsWith {
+
+  private static final String COLUMN = "someStringCol";
+  private static final Schema SCHEMA = new Schema(optional(1, COLUMN, 
Types.StringType.get()));
+
+  @Test
+  public void testTruncateProjections() {
+    PartitionSpec spec = PartitionSpec.builderFor(SCHEMA).truncate(COLUMN, 
4).build();
+
+    TransformTestHelpers.assertProjectionInclusive(
+        spec, notEndsWith(COLUMN, "ab"), "ab", 
Expression.Operation.NOT_ENDS_WITH);
+    TransformTestHelpers.assertProjectionInclusive(
+        spec, notEndsWith(COLUMN, "abab"), "abab", 
Expression.Operation.NOT_EQ);
+    // When literal is longer than partition spec's truncation width, we 
always read for an
+    // inclusive projection when using notStartsWith.
+    Expression projection = 
Projections.inclusive(spec).project(notEndsWith(COLUMN, "ababab"));
+    assertThat(projection).isInstanceOf(True.class);
+
+    TransformTestHelpers.assertProjectionStrict(
+        spec, notEndsWith(COLUMN, "ab"), "ab", 
Expression.Operation.NOT_ENDS_WITH);
+    TransformTestHelpers.assertProjectionStrict(
+        spec, notEndsWith(COLUMN, "abab"), "abab", 
Expression.Operation.NOT_EQ);
+    TransformTestHelpers.assertProjectionStrict(
+        spec, notEndsWith(COLUMN, "abcdab"), "abcd", 
Expression.Operation.NOT_ENDS_WITH);
+  }
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void testEvaluatorTruncateString() {

Review Comment:
   I think we need more tests on different string length combinations and 
metrics evaluator. I think you can refer to `TestNotStartsWith` for good tests.



##########
api/src/test/java/org/apache/iceberg/transforms/TransformTestHelpers.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.transforms;
+
+import static org.apache.iceberg.TestHelpers.assertAndUnwrapUnbound;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Literal;
+import org.apache.iceberg.expressions.Projections;
+import org.apache.iceberg.expressions.UnboundPredicate;
+import org.apache.iceberg.types.Types;
+
+public class TransformTestHelpers {

Review Comment:
   Instead of a helper class, could we make this a base class for all the 
transform tests; `TestTransformBase` or something like that? Then TestContains, 
TestEndsWith  can all inherit that class and use the protected assertProjection 
methods.



##########
api/src/test/java/org/apache/iceberg/expressions/TestEvaluator.java:
##########
@@ -348,6 +352,102 @@ public void testNotStartsWith() {
         .isTrue();
   }
 
+  @Test
+  public void testEndsWith() {
+    StructType struct = StructType.of(required(24, "s", 
Types.StringType.get()));
+    Evaluator evaluator = new Evaluator(struct, endsWith("s", "abc"));
+    assertThat(evaluator.eval(TestHelpers.Row.of("abc")))
+        .as("abc endsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("bcx")))
+        .as("bcx endsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("abC")))
+        .as("abC endsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("c")))
+        .as("c endsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("aabc")))
+        .as("aabc endsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of((String) null)))
+        .as("null endsWith abc should be false")
+        .isFalse();
+  }
+
+  @Test
+  public void testNotEndsWith() {
+    StructType struct = StructType.of(required(24, "s", 
Types.StringType.get()));
+    Evaluator evaluator = new Evaluator(struct, notEndsWith("s", "abc"));
+    assertThat(evaluator.eval(TestHelpers.Row.of("abc")))
+        .as("abc notEndsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("abcx")))
+        .as("abcx notEndsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("abC")))
+        .as("abC notEndsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("c")))
+        .as("c notEndsWith abc should be true")
+        .isTrue();
+    assertThat(evaluator.eval(TestHelpers.Row.of("ababc")))
+        .as("ababc notEndsWith abc should be false")
+        .isFalse();
+    assertThat(evaluator.eval(TestHelpers.Row.of("AbAbc")))
+        .as("AbAbc notEndsWith abc should be true")
+        .isTrue();

Review Comment:
   Same here could you add an`notEndsWith" test for null? Also testing empty 
string would be nice.



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