private void assertPredicateIsAlwaysTrue(Expression expression) { assertPredicateTranslates(expression, TupleDomain.all()); }
@Test public void testFromIsNotNullPredicate() { assertPredicateTranslates( isNotNull(C_BIGINT), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.notNull(BIGINT)))); assertPredicateTranslates( isNotNull(C_HYPER_LOG_LOG), withColumnDomains(ImmutableMap.of(C_HYPER_LOG_LOG, Domain.notNull(HYPER_LOG_LOG)))); assertPredicateTranslates( not(isNotNull(C_BIGINT)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.onlyNull(BIGINT)))); assertPredicateTranslates( not(isNotNull(C_HYPER_LOG_LOG)), withColumnDomains(ImmutableMap.of(C_HYPER_LOG_LOG, Domain.onlyNull(HYPER_LOG_LOG)))); }
@Test public void testFromIsNullPredicate() { assertPredicateTranslates( isNull(C_BIGINT), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.onlyNull(BIGINT)))); assertPredicateTranslates( isNull(C_HYPER_LOG_LOG), withColumnDomains(ImmutableMap.of(C_HYPER_LOG_LOG, Domain.onlyNull(HYPER_LOG_LOG)))); assertPredicateTranslates( not(isNull(C_BIGINT)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.notNull(BIGINT)))); assertPredicateTranslates( not(isNull(C_HYPER_LOG_LOG)), withColumnDomains(ImmutableMap.of(C_HYPER_LOG_LOG, Domain.notNull(HYPER_LOG_LOG)))); }
@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 testRoundTrip() { TupleDomain<Symbol> tupleDomain = withColumnDomains(ImmutableMap.<Symbol, Domain>builder() .put(C_BIGINT, Domain.singleValue(BIGINT, 1L)) .put(C_DOUBLE, Domain.onlyNull(DOUBLE)) .put(C_VARCHAR, Domain.notNull(VARCHAR)) .put(C_BOOLEAN, Domain.singleValue(BOOLEAN, true)) .put(C_BIGINT_1, Domain.singleValue(BIGINT, 2L)) .put(C_DOUBLE_1, Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(DOUBLE, 1.1), Range.equal(DOUBLE, 2.0), Range.range(DOUBLE, 3.0, false, 3.5, true)), true)) .put(C_VARCHAR_1, Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(VARCHAR, utf8Slice("2013-01-01")), Range.greaterThan(VARCHAR, utf8Slice("2013-10-01"))), false)) .put(C_TIMESTAMP, Domain.singleValue(TIMESTAMP, TIMESTAMP_VALUE)) .put(C_DATE, Domain.singleValue(DATE, DATE_VALUE)) .put(C_COLOR, Domain.singleValue(COLOR, COLOR_VALUE_1)) .put(C_HYPER_LOG_LOG, Domain.notNull(HYPER_LOG_LOG)) .build()); assertPredicateTranslates(toPredicate(tupleDomain), tupleDomain); }
@Test public void testFromInPredicate() { assertPredicateTranslates( in(C_BIGINT, ImmutableList.of(1L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.singleValue(BIGINT, 1L)))); assertPredicateTranslates( in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.singleValue(COLOR, COLOR_VALUE_1)))); assertPredicateTranslates( in(C_BIGINT, ImmutableList.of(1L, 2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false)))); assertPredicateTranslates( in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2), false)))); assertPredicateTranslates( not(in(C_BIGINT, ImmutableList.of(1L, 2L))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.range(BIGINT, 1L, false, 2L, false), Range.greaterThan(BIGINT, 2L)), false)))); assertPredicateTranslates( not(in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2)))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2).complement(), false)))); }
@Test public void testInPredicateWithCasts() { assertPredicateTranslates( new InPredicate( C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(1L, SMALLINT), BIGINT)))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.singleValue(BIGINT, 1L)))); assertPredicateTranslates( new InPredicate( cast(C_SMALLINT, BIGINT), new InListExpression(ImmutableList.of(toExpression(1L, BIGINT)))), withColumnDomains(ImmutableMap.of(C_SMALLINT, Domain.singleValue(SMALLINT, 1L)))); assertUnsupportedPredicate(new InPredicate( cast(C_BIGINT, INTEGER), new InListExpression(ImmutableList.of(toExpression(1L, INTEGER))))); }
@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})))); }
assertPredicateTranslates( comparison(GREATER_THAN, bigintLiteral(2L), C_BIGINT.toSymbolReference()), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 2L)), false)))); assertPredicateTranslates( comparison(GREATER_THAN_OR_EQUAL, bigintLiteral(2L), C_BIGINT.toSymbolReference()), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(BIGINT, 2L)), false)))); assertPredicateTranslates( comparison(LESS_THAN, bigintLiteral(2L), C_BIGINT.toSymbolReference()), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 2L)), false)))); assertPredicateTranslates( comparison(LESS_THAN_OR_EQUAL, bigintLiteral(2L), C_BIGINT.toSymbolReference()), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.greaterThanOrEqual(BIGINT, 2L)), false)))); assertPredicateTranslates(comparison(EQUAL, bigintLiteral(2L), C_BIGINT.toSymbolReference()), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 2L)), false)))); assertPredicateTranslates(comparison(EQUAL, colorLiteral(COLOR_VALUE_1), C_COLOR.toSymbolReference()), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1), false)))); assertPredicateTranslates(comparison(NOT_EQUAL, bigintLiteral(2L), C_BIGINT.toSymbolReference()), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 2L), Range.greaterThan(BIGINT, 2L)), false)))); assertPredicateTranslates( comparison(NOT_EQUAL, colorLiteral(COLOR_VALUE_1), C_COLOR.toSymbolReference()), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1).complement(), false))));
@Test public void testFromBetweenPredicate() { assertPredicateTranslates( between(C_BIGINT, bigintLiteral(1L), bigintLiteral(2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, true, 2L, true)), false)))); assertPredicateTranslates( between(cast(C_INTEGER, DOUBLE), cast(bigintLiteral(1L), DOUBLE), doubleLiteral(2.1)), withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.range(INTEGER, 1L, true, 2L, true)), false)))); assertPredicateIsAlwaysFalse(between(C_BIGINT, bigintLiteral(1L), nullLiteral(BIGINT))); // Test complements assertPredicateTranslates( not(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(2L))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.greaterThan(BIGINT, 2L)), false)))); assertPredicateTranslates( not(between(cast(C_INTEGER, DOUBLE), cast(bigintLiteral(1L), DOUBLE), doubleLiteral(2.1))), withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.lessThan(INTEGER, 1L), Range.greaterThan(INTEGER, 2L)), false)))); assertPredicateTranslates( not(between(C_BIGINT, bigintLiteral(1L), nullLiteral(BIGINT))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L)), false)))); }
assertPredicateTranslates( greaterThan(C_BIGINT, bigintLiteral(2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 2L)), false)))); assertPredicateTranslates( greaterThanOrEqual(C_BIGINT, bigintLiteral(2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.greaterThanOrEqual(BIGINT, 2L)), false)))); assertPredicateTranslates( lessThan(C_BIGINT, bigintLiteral(2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 2L)), false)))); assertPredicateTranslates( lessThanOrEqual(C_BIGINT, bigintLiteral(2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(BIGINT, 2L)), false)))); assertPredicateTranslates( equal(C_BIGINT, bigintLiteral(2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 2L)), false)))); assertPredicateTranslates( notEqual(C_BIGINT, bigintLiteral(2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 2L), Range.greaterThan(BIGINT, 2L)), false)))); assertPredicateTranslates( isDistinctFrom(C_BIGINT, bigintLiteral(2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 2L), Range.greaterThan(BIGINT, 2L)), true)))); assertPredicateTranslates(
@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 testFromInPredicateWithCastsAndNulls() { assertPredicateIsAlwaysFalse(new InPredicate( C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT))))); assertUnsupportedPredicate(not(new InPredicate( cast(C_SMALLINT, BIGINT), new InListExpression(ImmutableList.of(toExpression(null, BIGINT)))))); assertPredicateTranslates( new InPredicate( C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT), toExpression(1L, BIGINT)))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L)), false)))); assertPredicateIsAlwaysFalse(not(new InPredicate( C_BIGINT.toSymbolReference(), new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT), toExpression(1L, SMALLINT)))))); }
assertPredicateTranslates( greaterThan(C_DOUBLE, cast(bigintLiteral(2L), DOUBLE)), withColumnDomains(ImmutableMap.of(C_DOUBLE, Domain.create(ValueSet.ofRanges(Range.greaterThan(DOUBLE, 2.0)), false)))); assertPredicateTranslates( greaterThan(C_VARCHAR, stringLiteral("test", VARCHAR)), withColumnDomains(ImmutableMap.of(C_VARCHAR, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARCHAR, utf8Slice("test"))), false)))); assertPredicateTranslates( greaterThan(cast(C_INTEGER, DOUBLE), doubleLiteral(2.0)), withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.greaterThan(INTEGER, 2L)), false)))); assertPredicateTranslates( greaterThan(cast(C_INTEGER, DOUBLE), doubleLiteral(2.1)), withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.greaterThan(INTEGER, 2L)), false)))); assertPredicateTranslates( greaterThanOrEqual(cast(C_INTEGER, DOUBLE), doubleLiteral(2.0)), withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.greaterThanOrEqual(INTEGER, 2L)), false)))); assertPredicateTranslates( greaterThanOrEqual(cast(C_INTEGER, DOUBLE), doubleLiteral(2.1)), withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.greaterThan(INTEGER, 2L)), false)))); assertPredicateTranslates( lessThan(cast(C_INTEGER, DOUBLE), doubleLiteral(2.0)), withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.lessThan(INTEGER, 2L)), false)))); assertPredicateTranslates(
@Test public void testConjunctExpression() { Expression expression = and( comparison(GREATER_THAN, C_DOUBLE.toSymbolReference(), doubleLiteral(0)), comparison(GREATER_THAN, C_BIGINT.toSymbolReference(), bigintLiteral(0))); assertPredicateTranslates( expression, withColumnDomains(ImmutableMap.of( C_BIGINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 0L)), false), C_DOUBLE, Domain.create(ValueSet.ofRanges(Range.greaterThan(DOUBLE, .0)), false)))); assertEquals( toPredicate(fromPredicate(expression).getTupleDomain()), and( comparison(GREATER_THAN, C_BIGINT.toSymbolReference(), bigintLiteral(0)), comparison(GREATER_THAN, C_DOUBLE.toSymbolReference(), doubleLiteral(0)))); }
assertPredicateTranslates( greaterThan(C_VARCHAR, nullLiteral(VARCHAR)), withColumnDomains(ImmutableMap.of(C_VARCHAR, Domain.create(ValueSet.none(VARCHAR), false)))); assertPredicateIsAlwaysFalse(notEqual(C_COLOR, nullLiteral(COLOR))); assertPredicateTranslates( isDistinctFrom(C_BIGINT, nullLiteral(BIGINT)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.notNull(BIGINT)))); assertPredicateTranslates( isDistinctFrom(C_COLOR, nullLiteral(COLOR)), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.notNull(COLOR)))); assertPredicateIsAlwaysFalse(not(notEqual(C_COLOR, nullLiteral(COLOR)))); assertPredicateTranslates( not(isDistinctFrom(C_BIGINT, nullLiteral(BIGINT))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.onlyNull(BIGINT)))); assertPredicateTranslates( not(isDistinctFrom(C_COLOR, nullLiteral(COLOR))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.onlyNull(COLOR))));