@SuppressWarnings("unchecked") private static <T> void updateMax(Map<Integer, Literal<?>> upperBounds, int id, Literal<T> max) { Literal<T> currentMax = (Literal<T>) upperBounds.get(id); if (currentMax == null || max.comparator().compare(max.value(), currentMax.value()) > 0) { upperBounds.put(id, max); } }
@Test public void testBinaryUnsignedComparator() { // b1 < b2 because comparison is unsigned, and -1 has msb set ByteBuffer b1 = ByteBuffer.wrap(new byte[] { 1, 1, 2 }); ByteBuffer b2 = ByteBuffer.wrap(new byte[] { 1, -1, 2 }); Comparator<ByteBuffer> cmp = Literal.of(b1).comparator(); Assert.assertTrue("Negative bytes should sort after positive bytes", cmp.compare(b1, b2) < 0); }
@Test public void testFixedUnsignedComparator() { // b1 < b2 because comparison is unsigned, and -1 has msb set ByteBuffer b1 = ByteBuffer.wrap(new byte[] { 1, 1, 2 }); ByteBuffer b2 = ByteBuffer.wrap(new byte[] { 1, -1, 2 }); Literal<ByteBuffer> fixedLit = Literal.of(b1).to(Types.FixedType.ofLength(3)); Comparator<ByteBuffer> cmp = fixedLit.comparator(); Assert.assertTrue("Negative bytes should sort after positive bytes", cmp.compare(b1, b2) < 0); }
private static Map<Integer, ByteBuffer> toBufferMap(Schema schema, Map<Integer, Literal<?>> map) { Map<Integer, ByteBuffer> bufferMap = Maps.newHashMap(); for (Map.Entry<Integer, Literal<?>> entry : map.entrySet()) { bufferMap.put(entry.getKey(), Conversions.toByteBuffer(schema.findType(entry.getKey()), entry.getValue().value())); } return bufferMap; } }
@Test public void testInvalidLongConversions() { testInvalidConversions(Literal.of(34L), Types.BooleanType.get(), Types.DateType.get(), Types.StringType.get(), Types.UUIDType.get(), Types.FixedType.ofLength(1), Types.BinaryType.get() ); }
@Test public void testLiterals() throws Exception { Literal[] literals = new Literal[] { Literal.of(false), Literal.of(34), Literal.of(35L), Literal.of(36.75F), Literal.of(8.75D), Literal.of("2017-11-29").to(Types.DateType.get()), Literal.of("11:30:07").to(Types.TimeType.get()), Literal.of("2017-11-29T11:30:07.123").to(Types.TimestampType.withoutZone()), Literal.of("2017-11-29T11:30:07.123+01:00").to(Types.TimestampType.withZone()), Literal.of("abc"), Literal.of(UUID.randomUUID()), Literal.of(new byte[] { 1, 2, 3 }).to(Types.FixedType.ofLength(3)), Literal.of(new byte[] { 3, 4, 5, 6 }).to(Types.BinaryType.get()), Literal.of(new BigDecimal("122.50")), }; for (Literal<?> lit : literals) { checkValue(lit); } }
@Test public void testStringAndUtf8() { String s1 = "abc"; Utf8 s2 = new Utf8("abc"); Comparator<CharSequence> stringComp = Literal.of(s1).comparator(); Assert.assertEquals("Should consider String and Utf8 equal", 0, stringComp.compare(s1, s2)); Comparator<CharSequence> utf8Comp = Literal.of(s2).comparator(); Assert.assertEquals("Should consider String and Utf8 equal", 0, utf8Comp.compare(s1, s2)); }
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); }
@Override public <T> Expression gt(BoundReference<T> ref, com.netflix.iceberg.expressions.Literal<T> lit) { return column(ref).gt(lit.value()).expr(); }
@Test public void testInvalidStringConversions() { // Strings can be used for types that are difficult to construct, like decimal or timestamp, // but are not intended to support parsing strings to any type testInvalidConversions(Literal.of("abc"), Types.BooleanType.get(), Types.IntegerType.get(), Types.LongType.get(), Types.FloatType.get(), Types.DoubleType.get(), Types.FixedType.ofLength(1), Types.BinaryType.get() ); }
@SuppressWarnings("unchecked") private static <T> void updateMin(Map<Integer, Literal<?>> lowerBounds, int id, Literal<T> min) { Literal<T> currentMin = (Literal<T>) lowerBounds.get(id); if (currentMin == null || min.comparator().compare(min.value(), currentMin.value()) < 0) { lowerBounds.put(id, min); } }
@Test(expected = DateTimeException.class) public void testTimestampWithoutZoneWithZoneInLiteral() { // Zone must not be present in literals when converting to timestamp without zone Literal<CharSequence> timestampStr = Literal.of("2017-08-18T14:21:01.919+07:00"); timestampStr.to(Types.TimestampType.withoutZone()); }
@Test public void testNaturalOrder() { Comparator<Long> cmp = Literal.of(34L).comparator(); Assert.assertTrue("Should use the natural order for non-null values", cmp.compare(33L, 34L) < 0); Assert.assertTrue("Should use signed ordering", cmp.compare(33L, -34L) > 0); }
@Override public <T> Expression ltEq(BoundReference<T> ref, com.netflix.iceberg.expressions.Literal<T> lit) { return column(ref).leq(lit.value()).expr(); }
@Test public void testInvalidIntegerConversions() { testInvalidConversions(Literal.of(34), Types.BooleanType.get(), Types.TimeType.get(), Types.TimestampType.withZone(), Types.TimestampType.withoutZone(), Types.StringType.get(), Types.UUIDType.get(), Types.FixedType.ofLength(1), Types.BinaryType.get() ); }