Skip to content

Commit

Permalink
Add Projection Test
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithjay committed Jul 29, 2019
1 parent f3ede7a commit 29e5e48
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<CharSequence> transform = (Truncate<CharSequence>) spec.getFieldsBySourceId(1).get(0).transform();
String output = transform.toHumanString((String) literal.value());
Assert.assertEquals(expectedLiteral, output);
}
}

0 comments on commit 29e5e48

Please sign in to comment.