protected Expression bind(UnboundPredicate<?> pred) { // instead of binding the predicate using the top-level schema, bind it to the partition data return pred.bind(partitionStruct); } }
@Override public Expression negate() { return new UnboundPredicate<>(op().negate(), ref(), literal()); }
BoundPredicate<?> bound = assertAndUnwrap(predicate.bind(spec.schema().asStruct())); "id", projected.ref().name()); Assert.assertEquals("Operation should match", bound.op(), projected.op()); bound.literal().value(), projected.literal().value()); } else { Assert.assertNull("Literal should be null", projected.literal());
private void removeTimeFilters(List<Expression> expressions, Expression expression) { if (expression.op() == Operation.AND) { And and = (And) expression; removeTimeFilters(expressions, and.left()); removeTimeFilters(expressions, and.right()); return; } else if (expression instanceof UnboundPredicate) { UnboundPredicate pred = (UnboundPredicate) expression; NamedReference ref = (NamedReference) pred.ref(); Literal<?> lit = pred.literal(); if (TIMESTAMP_NAMES.contains(ref.name())) { Literal<Long> tsLiteral = lit.to(Types.TimestampType.withoutZone()); long millis = toMillis(tsLiteral.value()); addTimestampFilter(Expressions.predicate(pred.op(), "timestamp_ms", millis)); return; } } expressions.add(expression); }
@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:")); } }
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); }
Or or1 = (Or) projection; UnboundPredicate<?> dateint1 = assertAndUnwrapUnbound(or1.left()); Assert.assertEquals("Should be a dateint predicate", "dateint", dateint1.ref().name()); Assert.assertEquals("Should be dateint=20180416", 20180416, dateint1.literal().value()); Assert.assertTrue(or1.right() instanceof Or); Or or2 = (Or) or1.right(); UnboundPredicate<?> dateint2 = assertAndUnwrapUnbound(or2.left()); Assert.assertEquals("Should be a dateint predicate", "dateint", dateint2.ref().name()); Assert.assertEquals("Should be dateint=20180415", 20180415, dateint2.literal().value()); UnboundPredicate<?> dateint3 = assertAndUnwrapUnbound(or2.right()); Assert.assertEquals("Should be a dateint predicate", "dateint", dateint3.ref().name()); Assert.assertEquals("Should be dateint=20180417", 20180417, dateint3.literal().value());
BoundPredicate<?> bound = assertAndUnwrap(predicate.bind(spec.schema().asStruct())); "id", projected.ref().name()); Assert.assertEquals("Operation should match", bound.op(), projected.op()); bound.literal().value(), projected.literal().value()); } else { Assert.assertNull("Literal should be null", projected.literal());
public Expression bind(Types.StructType struct) { Types.NestedField field = struct.field(ref().name()); ValidationException.check(field != null, "Cannot find field '%s' in struct: %s", ref().name(), struct); if (literal() == null) { switch (op()) { case IS_NULL: if (field.isRequired()) { Literal<T> lit = literal().to(field.type()); if (lit == null) { throw new ValidationException(String.format( "Invalid value for comparison inclusive type %s: %s (%s)", field.type(), literal().value(), literal().value().getClass().getName())); switch (op()) { case LT: case LT_EQ: switch (op()) { case GT: case GT_EQ: return new BoundPredicate<>(op(), new BoundReference<>(struct, field.fieldId()), lit);
UnboundPredicate<Double> lt = new UnboundPredicate<>( LT, ref("f"), (double) Float.MAX_VALUE * 2); Assert.assertEquals("Less than above max should be alwaysTrue", Expressions.alwaysTrue(), lt.bind(struct)); UnboundPredicate<Double> lteq = new UnboundPredicate<>( 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)); UnboundPredicate<Double> gt = new UnboundPredicate<>( GT, ref("f"), (double) Float.MAX_VALUE * -2); Assert.assertEquals("Greater than below min should be alwaysTrue", Expressions.alwaysTrue(), gt.bind(struct)); UnboundPredicate<Double> gteq = new UnboundPredicate<>( 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)); UnboundPredicate<Double> gtMax = new UnboundPredicate<>( GT, ref("f"), (double) Float.MAX_VALUE * 2); Assert.assertEquals("Greater than above max should be alwaysFalse", Expressions.alwaysFalse(), gtMax.bind(struct)); UnboundPredicate<Double> gteqMax = new UnboundPredicate<>( 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));
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)); }
Assert.assertEquals("Residual should be hour < 12", LT, unbound.op()); Assert.assertEquals("Residual should be hour < 12", "hour", unbound.ref().name()); Assert.assertEquals("Residual should be hour < 12", 12, unbound.literal().value()); Assert.assertEquals("Residual should be hour > 11", GT, unbound.op()); Assert.assertEquals("Residual should be hour > 11", "hour", unbound.ref().name()); Assert.assertEquals("Residual should be hour > 11", 11, unbound.literal().value());
UnboundPredicate<Long> lt = new UnboundPredicate<>( LT, ref("i"), (long) Integer.MAX_VALUE + 1L); Assert.assertEquals("Less than above max should be alwaysTrue", Expressions.alwaysTrue(), lt.bind(struct)); UnboundPredicate<Long> lteq = new UnboundPredicate<>( 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)); UnboundPredicate<Long> gt = new UnboundPredicate<>( GT, ref("i"), (long) Integer.MIN_VALUE - 1L); Assert.assertEquals("Greater than below min should be alwaysTrue", Expressions.alwaysTrue(), gt.bind(struct)); UnboundPredicate<Long> gteq = new UnboundPredicate<>( 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)); UnboundPredicate<Long> gtMax = new UnboundPredicate<>( GT, ref("i"), (long) Integer.MAX_VALUE + 1L); Assert.assertEquals("Greater than above max should be alwaysFalse", Expressions.alwaysFalse(), gtMax.bind(struct)); UnboundPredicate<Long> gteqMax = new UnboundPredicate<>( 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));
@Override public <T> Expression predicate(UnboundPredicate<T> pred) { return pred.bind(struct); } }
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); }
@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)"); } } }
protected Expression bind(UnboundPredicate<?> pred) { return pred.bind(schema.asStruct()); }
public static <T> UnboundPredicate<T> notNull(String name) { return new UnboundPredicate<>(Expression.Operation.NOT_NULL, ref(name)); }
@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()); } }