diff --git a/api/src/test/java/org/apache/iceberg/transforms/TestStartsWithProjection.java b/api/src/test/java/org/apache/iceberg/transforms/TestStartsWithProjection.java new file mode 100644 index 000000000000..46172d36a8fa --- /dev/null +++ b/api/src/test/java/org/apache/iceberg/transforms/TestStartsWithProjection.java @@ -0,0 +1,41 @@ +package org.apache.iceberg.transforms; + +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +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; +import org.junit.Assert; +import org.junit.Test; + +import static org.apache.iceberg.TestHelpers.assertAndUnwrapUnbound; +import static org.apache.iceberg.expressions.Expressions.startsWith; +import static org.apache.iceberg.types.Types.NestedField.optional; + +public class TestStartsWithProjection { + private static final Schema SCHEMA = new Schema(optional(1, "someStringCol", Types.StringType.get())); + @Test + public void assertTruncateProjections(){ + PartitionSpec spec = PartitionSpec.builderFor(SCHEMA).truncate("someStringCol", 4).build(); + + assertTruncateProjectionInclusive(spec, startsWith("someStringCol", "ab"), "ab"); + assertTruncateProjectionInclusive(spec, startsWith("someStringCol", "abab"), "abab"); + assertTruncateProjectionInclusive(spec, startsWith("someStringCol", "ababab"), "abab"); + + } + + private void assertTruncateProjectionInclusive(PartitionSpec spec, UnboundPredicate filter, + String expectedLiteral) { + Expression projection = Projections.inclusive(spec).project(filter); + UnboundPredicate predicate = assertAndUnwrapUnbound(projection); + + Assert.assertEquals(predicate.op(), Expression.Operation.STARTS_WITH); + + Literal literal = predicate.literal(); + Truncate transform = (Truncate) spec.getFieldsBySourceId(1).get(0).transform(); + String output = transform.toHumanString((String) literal.value()); + Assert.assertEquals(expectedLiteral, output); + } +}