Search in sources :

Example 21 with Expression

use of com.facebook.presto.sql.tree.Expression 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)

Example 22 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class TestDomainTranslator method testFromBooleanLiteralPredicate.

@Test
public void testFromBooleanLiteralPredicate() throws Exception {
    Expression originalExpression = TRUE_LITERAL;
    ExtractionResult result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertTrue(result.getTupleDomain().isAll());
    originalExpression = not(TRUE_LITERAL);
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertTrue(result.getTupleDomain().isNone());
    originalExpression = FALSE_LITERAL;
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertTrue(result.getTupleDomain().isNone());
    originalExpression = not(FALSE_LITERAL);
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertTrue(result.getTupleDomain().isAll());
}
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 23 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class TestDomainTranslator method testFromIsNotNullPredicate.

@Test
public void testFromIsNotNullPredicate() throws Exception {
    Expression originalExpression = isNotNull(C_BIGINT);
    ExtractionResult result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.notNull(BIGINT))));
    originalExpression = isNotNull(C_HYPER_LOG_LOG);
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_HYPER_LOG_LOG, Domain.notNull(HYPER_LOG_LOG))));
    originalExpression = not(isNotNull(C_BIGINT));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.onlyNull(BIGINT))));
    originalExpression = not(isNotNull(C_HYPER_LOG_LOG));
    result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_HYPER_LOG_LOG, Domain.onlyNull(HYPER_LOG_LOG))));
}
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 24 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class TestDomainTranslator method testFromNotPredicate.

@Test
public void testFromNotPredicate() throws Exception {
    Expression originalPredicate = not(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)));
    ExtractionResult result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertTrue(result.getTupleDomain().isAll());
    originalPredicate = not(unprocessableExpression1(C_BIGINT));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertTrue(result.getTupleDomain().isAll());
    originalPredicate = not(TRUE_LITERAL);
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertTrue(result.getTupleDomain().isNone());
    originalPredicate = not(equal(C_BIGINT, bigintLiteral(1L)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.greaterThan(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)

Example 25 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class ExpressionFormatter method formatGroupBy.

static String formatGroupBy(List<GroupingElement> groupingElements, Optional<List<Expression>> parameters) {
    ImmutableList.Builder<String> resultStrings = ImmutableList.builder();
    for (GroupingElement groupingElement : groupingElements) {
        String result = "";
        if (groupingElement instanceof SimpleGroupBy) {
            Set<Expression> columns = ImmutableSet.copyOf(((SimpleGroupBy) groupingElement).getColumnExpressions());
            if (columns.size() == 1) {
                result = formatExpression(getOnlyElement(columns), parameters);
            } else {
                result = formatGroupingSet(columns, parameters);
            }
        } else if (groupingElement instanceof GroupingSets) {
            result = format("GROUPING SETS (%s)", Joiner.on(", ").join(((GroupingSets) groupingElement).getSets().stream().map(ExpressionFormatter::formatGroupingSet).iterator()));
        } else if (groupingElement instanceof Cube) {
            result = format("CUBE %s", formatGroupingSet(((Cube) groupingElement).getColumns()));
        } else if (groupingElement instanceof Rollup) {
            result = format("ROLLUP %s", formatGroupingSet(((Rollup) groupingElement).getColumns()));
        }
        resultStrings.add(result);
    }
    return Joiner.on(", ").join(resultStrings.build());
}
Also used : GroupingSets(com.facebook.presto.sql.tree.GroupingSets) GroupingElement(com.facebook.presto.sql.tree.GroupingElement) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) Rollup(com.facebook.presto.sql.tree.Rollup) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) TryExpression(com.facebook.presto.sql.tree.TryExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) Cube(com.facebook.presto.sql.tree.Cube) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

Expression (com.facebook.presto.sql.tree.Expression)137 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)74 Test (org.testng.annotations.Test)46 NotExpression (com.facebook.presto.sql.tree.NotExpression)42 InListExpression (com.facebook.presto.sql.tree.InListExpression)40 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)33 Type (com.facebook.presto.spi.type.Type)26 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)26 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)25 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)22 ImmutableList (com.google.common.collect.ImmutableList)22 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)19 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)18 Cast (com.facebook.presto.sql.tree.Cast)17 ArrayList (java.util.ArrayList)17 ExtractionResult (com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult)16 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)16 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)16 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)16 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)15