Search in sources :

Example 1 with ExtractionResult

use of com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult in project presto by prestodb.

the class TestDomainTranslator method testFromOrPredicate.

@Test
public void testFromOrPredicate() throws Exception {
    Expression originalPredicate = or(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)));
    ExtractionResult result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.notNull(BIGINT))));
    originalPredicate = or(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(equal(C_BIGINT, bigintLiteral(2L)), unprocessableExpression2(C_BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));
    // Same unprocessableExpression means that we can do more extraction
    // If both sides are operating on the same single symbol
    originalPredicate = or(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(equal(C_BIGINT, bigintLiteral(2L)), unprocessableExpression1(C_BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), unprocessableExpression1(C_BIGINT));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));
    // And not if they have different symbols
    originalPredicate = or(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(equal(C_DOUBLE, doubleLiteral(2.0)), unprocessableExpression1(C_BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertTrue(result.getTupleDomain().isAll());
    // We can make another optimization if one side is the super set of the other side
    originalPredicate = or(and(greaterThan(C_BIGINT, bigintLiteral(1L)), greaterThan(C_DOUBLE, doubleLiteral(1.0)), unprocessableExpression1(C_BIGINT)), and(greaterThan(C_BIGINT, bigintLiteral(2L)), greaterThan(C_DOUBLE, doubleLiteral(2.0)), unprocessableExpression1(C_BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), unprocessableExpression1(C_BIGINT));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 1L)), false), C_DOUBLE, Domain.create(ValueSet.ofRanges(Range.greaterThan(DOUBLE, 1.0)), false))));
    // We can't make those inferences if the unprocessableExpressions are non-deterministic
    originalPredicate = or(and(equal(C_BIGINT, bigintLiteral(1L)), randPredicate(C_BIGINT, BIGINT)), and(equal(C_BIGINT, bigintLiteral(2L)), randPredicate(C_BIGINT, BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));
    // Test complements
    originalPredicate = not(or(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT))));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), and(not(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT))), not(and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));
    assertTrue(result.getTupleDomain().isAll());
    originalPredicate = not(or(not(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT))), not(and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), and(unprocessableExpression1(C_BIGINT), unprocessableExpression2(C_BIGINT)));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, false, 5L, false)), false))));
}
Also used : NotExpression(com.facebook.presto.sql.tree.NotExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ExtractionResult(com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult) Test(org.testng.annotations.Test)

Example 2 with ExtractionResult

use of com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult in project presto by prestodb.

the class TestDomainTranslator method testFromInPredicate.

@Test
public void testFromInPredicate() throws Exception {
    Expression originalExpression = in(C_BIGINT, ImmutableList.of(1L));
    ExtractionResult result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.singleValue(BIGINT, 1L))));
    originalExpression = in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1)));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.singleValue(COLOR, COLOR_VALUE_1))));
    originalExpression = in(C_BIGINT, ImmutableList.of(1L, 2L));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));
    originalExpression = in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2)));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2), false))));
    originalExpression = not(in(C_BIGINT, ImmutableList.of(1L, 2L)));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), 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))));
    originalExpression = not(in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2))));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2).complement(), false))));
// TODO update domain translator to properly handle cast
//        originalExpression = in(A, Arrays.asList(1L, 2L, (Expression) null));
//        result = fromPredicate(originalExpression, TYPES, COLUMN_HANDLES);
//        assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
//        assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(ACH, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));
//
//        originalExpression = not(in(A, Arrays.asList(1L, 2L, (Expression) null)));
//        result = fromPredicate(originalExpression, TYPES, COLUMN_HANDLES);
//        assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
//        assertTrue(result.getTupleDomain().isNone());
//
//        originalExpression = in(A, Arrays.asList((Expression) null));
//        result = fromPredicate(originalExpression, TYPES, COLUMN_HANDLES);
//        assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
//        assertTrue(result.getTupleDomain().isNone());
//
//        originalExpression = not(in(A, Arrays.asList((Expression) null)));
//        result = fromPredicate(originalExpression, TYPES, COLUMN_HANDLES);
//        assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
//        assertTrue(result.getTupleDomain().isNone());
}
Also used : NotExpression(com.facebook.presto.sql.tree.NotExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ExtractionResult(com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult) Test(org.testng.annotations.Test)

Example 3 with ExtractionResult

use of com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult in project presto by prestodb.

the class TestDomainTranslator method testFromAndPredicate.

@Test
public void testFromAndPredicate() throws Exception {
    Expression originalPredicate = and(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)));
    ExtractionResult result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), and(unprocessableExpression1(C_BIGINT), unprocessableExpression2(C_BIGINT)));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, false, 5L, false)), false))));
    // Test complements
    originalPredicate = not(and(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT))));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertTrue(result.getTupleDomain().isAll());
    originalPredicate = not(and(not(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT))), not(and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.notNull(BIGINT))));
}
Also used : NotExpression(com.facebook.presto.sql.tree.NotExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ExtractionResult(com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult) Test(org.testng.annotations.Test)

Example 4 with ExtractionResult

use of com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult in project presto by prestodb.

the class TestDomainTranslator method testFromNullLiteralPredicate.

@Test
public void testFromNullLiteralPredicate() throws Exception {
    Expression originalExpression = nullLiteral();
    ExtractionResult result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertTrue(result.getTupleDomain().isNone());
    originalExpression = not(nullLiteral());
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertTrue(result.getTupleDomain().isNone());
}
Also used : NotExpression(com.facebook.presto.sql.tree.NotExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ExtractionResult(com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult) Test(org.testng.annotations.Test)

Example 5 with ExtractionResult

use of com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult in project presto by prestodb.

the class TestDomainTranslator method testFromBetweenPredicate.

@Test
public void testFromBetweenPredicate() throws Exception {
    Expression originalExpression = between(C_BIGINT, bigintLiteral(1L), bigintLiteral(2L));
    ExtractionResult result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, true, 2L, true)), false))));
    originalExpression = between(cast(C_BIGINT, DOUBLE), cast(bigintLiteral(1L), DOUBLE), doubleLiteral(2.1));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, true, 2L, true)), false))));
    originalExpression = between(C_BIGINT, bigintLiteral(1L), nullLiteral(BIGINT));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertTrue(result.getTupleDomain().isNone());
    // Test complements
    originalExpression = not(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(2L)));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.greaterThan(BIGINT, 2L)), false))));
    originalExpression = not(between(cast(C_BIGINT, DOUBLE), cast(bigintLiteral(1L), DOUBLE), doubleLiteral(2.1)));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.greaterThan(BIGINT, 2L)), false))));
    originalExpression = not(between(C_BIGINT, bigintLiteral(1L), nullLiteral(BIGINT)));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L)), false))));
}
Also used : NotExpression(com.facebook.presto.sql.tree.NotExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ExtractionResult(com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult) Test(org.testng.annotations.Test)

Aggregations

ExtractionResult (com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult)24 Test (org.testng.annotations.Test)23 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)18 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)16 Expression (com.facebook.presto.sql.tree.Expression)16 InListExpression (com.facebook.presto.sql.tree.InListExpression)16 NotExpression (com.facebook.presto.sql.tree.NotExpression)16 Domain (com.facebook.presto.spi.predicate.Domain)1 TupleDomain (com.facebook.presto.spi.predicate.TupleDomain)1 Cast (com.facebook.presto.sql.tree.Cast)1 InPredicate (com.facebook.presto.sql.tree.InPredicate)1 Slice (io.airlift.slice.Slice)1 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)1