private static boolean isEqualRange(TupleDomain<Symbol> tupleDomain, Symbol symbol, long upperBound) { if (tupleDomain.isNone()) { return false; } Domain domain = tupleDomain.getDomains().get().get(symbol); return domain.getValues().equals(ValueSet.ofRanges(Range.lessThanOrEqual(domain.getType(), upperBound))); }
private static boolean isEqualRange(TupleDomain<Symbol> tupleDomain, Symbol symbol, long upperBound) { if (tupleDomain.isNone()) { return false; } Domain domain = tupleDomain.getDomains().get().get(symbol); return domain.getValues().equals(ValueSet.ofRanges(Range.lessThanOrEqual(domain.getType(), upperBound))); }
@Test public void testBuildQueryNull() { TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of( COL1, Domain.create(ValueSet.ofRanges(greaterThan(BIGINT, 200L)), true))); Document query = MongoSession.buildQuery(tupleDomain); Document expected = new Document("$or", asList( new Document(COL1.getName(), new Document("$gt", 200L)), new Document(COL1.getName(), new Document("$exists", true).append("$eq", null)))); assertEquals(query, expected); } }
@Test public void testBuildQueryOr() { TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of( COL1, Domain.create(ValueSet.ofRanges(lessThan(BIGINT, 100L), greaterThan(BIGINT, 200L)), false))); Document query = MongoSession.buildQuery(tupleDomain); Document expected = new Document("$or", asList( new Document(COL1.getName(), new Document("$lt", 100L)), new Document(COL1.getName(), new Document("$gt", 200L)))); assertEquals(query, expected); }
private void testSimpleComparison(Expression expression, Symbol symbol, Range expectedDomainRange) { testSimpleComparison(expression, symbol, Domain.create(ValueSet.ofRanges(expectedDomainRange), false)); }
private void testSimpleComparison(Expression expression, Symbol symbol, Range expectedDomainRange) { testSimpleComparison(expression, symbol, Domain.create(ValueSet.ofRanges(expectedDomainRange), false)); }
@Test public void testBuildQueryIn() { TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of( COL2, Domain.create(ValueSet.ofRanges(equal(createUnboundedVarcharType(), utf8Slice("hello")), equal(createUnboundedVarcharType(), utf8Slice("world"))), false))); Document query = MongoSession.buildQuery(tupleDomain); Document expected = new Document(COL2.getName(), new Document("$in", ImmutableList.of("hello", "world"))); assertEquals(query, expected); }
@Test public void testBuildQuery() { TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of( COL1, Domain.create(ValueSet.ofRanges(range(BIGINT, 100L, false, 200L, true)), false), COL2, Domain.singleValue(createUnboundedVarcharType(), utf8Slice("a value")))); Document query = MongoSession.buildQuery(tupleDomain); Document expected = new Document() .append(COL1.getName(), new Document().append("$gt", 100L).append("$lte", 200L)) .append(COL2.getName(), new Document("$eq", "a value")); assertEquals(query, expected); }
@Test public void testBuildQueryStringType() { TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of( COL1, Domain.create(ValueSet.ofRanges(range(createUnboundedVarcharType(), utf8Slice("hello"), false, utf8Slice("world"), true)), false), COL2, Domain.create(ValueSet.ofRanges(greaterThanOrEqual(createUnboundedVarcharType(), utf8Slice("a value"))), false))); Document query = MongoSession.buildQuery(tupleDomain); Document expected = new Document() .append(COL1.getName(), new Document().append("$gt", "hello").append("$lte", "world")) .append(COL2.getName(), new Document("$gte", "a value")); assertEquals(query, expected); }
@Test public void testExpressionConstantFolding() { Expression originalExpression = comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), function("from_hex", stringLiteral("123456"))); ExtractionResult result = fromPredicate(originalExpression); assertEquals(result.getRemainingExpression(), TRUE_LITERAL); Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456")); assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_VARBINARY, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARBINARY, value)), false)))); Expression expression = toPredicate(result.getTupleDomain()); assertEquals(expression, comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), varbinaryLiteral(value))); }
@Test public void testFromValueSetOfRangesUnbounded() { PrestoThriftValueSet thriftValueSet = fromValueSet(ValueSet.ofRanges(Range.greaterThanOrEqual(BIGINT, 0L))); assertNotNull(thriftValueSet.getRangeValueSet()); assertEquals(thriftValueSet.getRangeValueSet().getRanges(), ImmutableList.of( new PrestoThriftRange(new PrestoThriftMarker(longValue(0), EXACTLY), new PrestoThriftMarker(null, BELOW)))); }
@Test public void testFromValueSetOfRangesUnbounded() { PrestoThriftValueSet thriftValueSet = fromValueSet(ValueSet.ofRanges(Range.greaterThanOrEqual(BIGINT, 0L))); assertNotNull(thriftValueSet.getRangeValueSet()); assertEquals(thriftValueSet.getRangeValueSet().getRanges(), ImmutableList.of( new PrestoThriftRange(new PrestoThriftMarker(longValue(0), EXACTLY), new PrestoThriftMarker(null, BELOW)))); }
@Test public void testExpressionConstantFolding() { Expression originalExpression = comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), function("from_hex", stringLiteral("123456"))); ExtractionResult result = fromPredicate(originalExpression); assertEquals(result.getRemainingExpression(), TRUE_LITERAL); Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456")); assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_VARBINARY, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARBINARY, value)), false)))); Expression expression = toPredicate(result.getTupleDomain()); assertEquals(expression, comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), varbinaryLiteral(value))); }
@Test void testMultipleCoercionsOnSymbolSide() { assertPredicateTranslates( comparison(GREATER_THAN, cast(cast(C_SMALLINT, REAL), DOUBLE), doubleLiteral(3.7)), withColumnDomains(ImmutableMap.of(C_SMALLINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(SMALLINT, 3L)), false)))); }
@Test void testMultipleCoercionsOnSymbolSide() { assertPredicateTranslates( comparison(GREATER_THAN, cast(cast(C_SMALLINT, REAL), DOUBLE), doubleLiteral(3.7)), withColumnDomains(ImmutableMap.of(C_SMALLINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(SMALLINT, 3L)), false)))); }
@Test public void testInPredicateWithNull() { assertPredicateTranslates( in(C_BIGINT, Arrays.asList(1L, 2L, null)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false)))); assertPredicateIsAlwaysFalse(not(in(C_BIGINT, Arrays.asList(1L, 2L, null)))); assertPredicateIsAlwaysFalse(in(C_BIGINT, Arrays.asList(new Long[] {null}))); assertPredicateIsAlwaysFalse(not(in(C_BIGINT, Arrays.asList(new Long[] {null})))); assertUnsupportedPredicate(isNull(in(C_BIGINT, Arrays.asList(1L, 2L, null)))); assertUnsupportedPredicate(isNotNull(in(C_BIGINT, Arrays.asList(1L, 2L, null)))); assertUnsupportedPredicate(isNull(in(C_BIGINT, Arrays.asList(new Long[] {null})))); assertUnsupportedPredicate(isNotNull(in(C_BIGINT, Arrays.asList(new Long[] {null})))); }
@Test public void testFromValueSetOfRangesBounded() { PrestoThriftValueSet thriftValueSet = fromValueSet(ValueSet.ofRanges( range(BIGINT, -10L, true, -1L, false), range(BIGINT, -1L, false, 100L, true))); assertNotNull(thriftValueSet.getRangeValueSet()); assertEquals(thriftValueSet.getRangeValueSet().getRanges(), ImmutableList.of( new PrestoThriftRange(new PrestoThriftMarker(longValue(-10), EXACTLY), new PrestoThriftMarker(longValue(-1), BELOW)), new PrestoThriftRange(new PrestoThriftMarker(longValue(-1), ABOVE), new PrestoThriftMarker(longValue(100), EXACTLY)))); }
@Test public void testFromValueSetOfRangesBounded() { PrestoThriftValueSet thriftValueSet = fromValueSet(ValueSet.ofRanges( range(BIGINT, -10L, true, -1L, false), range(BIGINT, -1L, false, 100L, true))); assertNotNull(thriftValueSet.getRangeValueSet()); assertEquals(thriftValueSet.getRangeValueSet().getRanges(), ImmutableList.of( new PrestoThriftRange(new PrestoThriftMarker(longValue(-10), EXACTLY), new PrestoThriftMarker(longValue(-1), BELOW)), new PrestoThriftRange(new PrestoThriftMarker(longValue(-1), ABOVE), new PrestoThriftMarker(longValue(100), EXACTLY)))); }
@Test public void testFromNotPredicate() { assertUnsupportedPredicate(not(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)))); assertUnsupportedPredicate(not(unprocessableExpression1(C_BIGINT))); assertPredicateIsAlwaysFalse(not(TRUE_LITERAL)); assertPredicateTranslates( not(equal(C_BIGINT, bigintLiteral(1L))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.greaterThan(BIGINT, 1L)), false)))); }
@Test public void testFromNotPredicate() { assertUnsupportedPredicate(not(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)))); assertUnsupportedPredicate(not(unprocessableExpression1(C_BIGINT))); assertPredicateIsAlwaysFalse(not(TRUE_LITERAL)); assertPredicateTranslates( not(equal(C_BIGINT, bigintLiteral(1L))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.greaterThan(BIGINT, 1L)), false)))); }