public static <T> UnboundPredicate<T> predicate(Operation op, String name) { Preconditions.checkArgument(op == Operation.IS_NULL || op == Operation.NOT_NULL, "Cannot create %s predicate without a value", op); return new UnboundPredicate<>(op, ref(name)); }
public static <T> UnboundPredicate<T> predicate(Operation op, String name, T value) { Preconditions.checkArgument(op != Operation.IS_NULL && op != Operation.NOT_NULL, "Cannot create %s predicate inclusive a value", op); return new UnboundPredicate<>(op, ref(name), value); }
public static <T> UnboundPredicate<T> predicate(Operation op, String name, Literal<T> lit) { Preconditions.checkArgument(op != Operation.IS_NULL && op != Operation.NOT_NULL, "Cannot create %s predicate inclusive a value", op); return new UnboundPredicate<>(op, ref(name), lit); }
public static <T> UnboundPredicate<T> isNull(String name) { return new UnboundPredicate<>(Expression.Operation.IS_NULL, ref(name)); }
public static <T> UnboundPredicate<T> lessThan(String name, T value) { return new UnboundPredicate<>(Expression.Operation.LT, ref(name), value); }
public static <T> UnboundPredicate<T> greaterThanOrEqual(String name, T value) { return new UnboundPredicate<>(Expression.Operation.GT_EQ, ref(name), value); }
public static <T> UnboundPredicate<T> notEqual(String name, T value) { return new UnboundPredicate<>(Expression.Operation.NOT_EQ, ref(name), value); }
public static <T> UnboundPredicate<T> notNull(String name) { return new UnboundPredicate<>(Expression.Operation.NOT_NULL, ref(name)); }
public static <T> UnboundPredicate<T> lessThanOrEqual(String name, T value) { return new UnboundPredicate<>(Expression.Operation.LT_EQ, ref(name), value); }
public static <T> UnboundPredicate<T> greaterThan(String name, T value) { return new UnboundPredicate<>(Expression.Operation.GT, ref(name), value); }
public static <T> UnboundPredicate<T> equal(String name, T value) { return new UnboundPredicate<>(Expression.Operation.EQ, ref(name), value); }
@Test public void testMissingField() { StructType struct = StructType.of( required(13, "x", Types.IntegerType.get()) ); UnboundPredicate<Integer> unbound = new UnboundPredicate<>(LT, ref("missing"), 6); try { unbound.bind(struct); Assert.fail("Binding a missing field should fail"); } catch (ValidationException e) { Assert.assertTrue("Validation should complain about missing field", e.getMessage().contains("Cannot find field 'missing' in struct:")); } }
LT, ref("f"), (double) Float.MAX_VALUE * 2); Assert.assertEquals("Less than above max should be alwaysTrue", Expressions.alwaysTrue(), lt.bind(struct)); LT_EQ, ref("f"), (double) Float.MAX_VALUE * 2); Assert.assertEquals("Less than or equal above max should be alwaysTrue", Expressions.alwaysTrue(), lteq.bind(struct)); GT, ref("f"), (double) Float.MAX_VALUE * -2); Assert.assertEquals("Greater than below min should be alwaysTrue", Expressions.alwaysTrue(), gt.bind(struct)); GT_EQ, ref("f"), (double) Float.MAX_VALUE * -2); Assert.assertEquals("Greater than or equal below min should be alwaysTrue", Expressions.alwaysTrue(), gteq.bind(struct)); GT, ref("f"), (double) Float.MAX_VALUE * 2); Assert.assertEquals("Greater than above max should be alwaysFalse", Expressions.alwaysFalse(), gtMax.bind(struct)); GT_EQ, ref("f"), (double) Float.MAX_VALUE * 2); Assert.assertEquals("Greater than or equal above max should be alwaysFalse", Expressions.alwaysFalse(), gteqMax.bind(struct)); LT, ref("f"), (double) Float.MAX_VALUE * -2); Assert.assertEquals("Less than below min should be alwaysFalse", Expressions.alwaysFalse(), ltMin.bind(struct)); LT_EQ, ref("f"), (double) Float.MAX_VALUE * -2);
LT, ref("i"), (long) Integer.MAX_VALUE + 1L); Assert.assertEquals("Less than above max should be alwaysTrue", Expressions.alwaysTrue(), lt.bind(struct)); LT_EQ, ref("i"), (long) Integer.MAX_VALUE + 1L); Assert.assertEquals("Less than or equal above max should be alwaysTrue", Expressions.alwaysTrue(), lteq.bind(struct)); GT, ref("i"), (long) Integer.MIN_VALUE - 1L); Assert.assertEquals("Greater than below min should be alwaysTrue", Expressions.alwaysTrue(), gt.bind(struct)); GT_EQ, ref("i"), (long) Integer.MIN_VALUE - 1L); Assert.assertEquals("Greater than or equal below min should be alwaysTrue", Expressions.alwaysTrue(), gteq.bind(struct)); GT, ref("i"), (long) Integer.MAX_VALUE + 1L); Assert.assertEquals("Greater than above max should be alwaysFalse", Expressions.alwaysFalse(), gtMax.bind(struct)); GT_EQ, ref("i"), (long) Integer.MAX_VALUE + 1L); Assert.assertEquals("Greater than or equal above max should be alwaysFalse", Expressions.alwaysFalse(), gteqMax.bind(struct)); LT, ref("i"), (long) Integer.MIN_VALUE - 1L); Assert.assertEquals("Less than below min should be alwaysFalse", Expressions.alwaysFalse(), ltMin.bind(struct)); LT_EQ, ref("i"), (long) Integer.MIN_VALUE - 1L);
@Test public void testInvalidConversions() { StructType struct = StructType.of(required(16, "f", Types.FloatType.get())); for (Expression.Operation op : COMPARISONS) { UnboundPredicate<String> unbound = new UnboundPredicate<>(op, ref("f"), "12.40"); try { unbound.bind(struct); Assert.fail("Should not convert string to float"); } catch (ValidationException e) { Assert.assertEquals("Should ", e.getMessage(), "Invalid value for comparison inclusive type float: 12.40 (java.lang.String)"); } } }
@Test @SuppressWarnings("unchecked") public void testLiteralConversion() { StructType struct = StructType.of(required(15, "d", Types.DecimalType.of(9, 2))); for (Expression.Operation op : COMPARISONS) { UnboundPredicate<String> unbound = new UnboundPredicate<>(op, ref("d"), "12.40"); Expression expr = unbound.bind(struct); BoundPredicate<BigDecimal> bound = assertAndUnwrap(expr); Assert.assertEquals("Should convert literal value to decimal", new BigDecimal("12.40"), bound.literal().value()); Assert.assertEquals("Should reference correct field ID", 15, bound.ref().fieldId()); Assert.assertEquals("Should not change the comparison operation", op, bound.op()); } }
@Test @SuppressWarnings("unchecked") public void testComparisonPredicateBinding() { StructType struct = StructType.of(required(14, "x", Types.IntegerType.get())); for (Expression.Operation op : COMPARISONS) { UnboundPredicate<Integer> unbound = new UnboundPredicate<>(op, ref("x"), 5); Expression expr = unbound.bind(struct); BoundPredicate<Integer> bound = assertAndUnwrap(expr); Assert.assertEquals("Should not alter literal value", Integer.valueOf(5), bound.literal().value()); Assert.assertEquals("Should reference correct field ID", 14, bound.ref().fieldId()); Assert.assertEquals("Should not change the comparison operation", op, bound.op()); } }
@Test @SuppressWarnings("unchecked") public void testMultipleFields() { StructType struct = StructType.of( required(10, "x", Types.IntegerType.get()), required(11, "y", Types.IntegerType.get()), required(12, "z", Types.IntegerType.get()) ); UnboundPredicate<Integer> unbound = new UnboundPredicate<>(LT, ref("y"), 6); Expression expr = unbound.bind(struct); BoundPredicate<Integer> bound = assertAndUnwrap(expr); Assert.assertEquals("Should reference correct field ID", 11, bound.ref().fieldId()); Assert.assertEquals("Should not change the comparison operation", LT, bound.op()); Assert.assertEquals("Should not alter literal value", Integer.valueOf(6), bound.literal().value()); }
@Test public void testNotNull() { StructType optional = StructType.of(optional(21, "s", Types.StringType.get())); UnboundPredicate<?> unbound = new UnboundPredicate<>(NOT_NULL, ref("s")); Expression expr = unbound.bind(optional); BoundPredicate<?> bound = assertAndUnwrap(expr); Assert.assertEquals("Should use the same operation", NOT_NULL, bound.op()); Assert.assertEquals("Should use the correct field", 21, bound.ref().fieldId()); Assert.assertNull("Should not have a literal value", bound.literal()); StructType required = StructType.of(required(22, "s", Types.StringType.get())); Assert.assertEquals("NotNull inclusive a required field should be alwaysTrue", Expressions.alwaysTrue(), unbound.bind(required)); } }
@Test @SuppressWarnings("unchecked") public void testIsNull() { StructType optional = StructType.of(optional(19, "s", Types.StringType.get())); UnboundPredicate<?> unbound = new UnboundPredicate<>(IS_NULL, ref("s")); Expression expr = unbound.bind(optional); BoundPredicate<?> bound = assertAndUnwrap(expr); Assert.assertEquals("Should use the same operation", IS_NULL, bound.op()); Assert.assertEquals("Should use the correct field", 19, bound.ref().fieldId()); Assert.assertNull("Should not have a literal value", bound.literal()); StructType required = StructType.of(required(20, "s", Types.StringType.get())); Assert.assertEquals("IsNull inclusive a required field should be alwaysFalse", Expressions.alwaysFalse(), unbound.bind(required)); }