Search in sources :

Example 11 with Expression

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

the class TestEffectivePredicateExtractor method testAggregation.

@Test
public void testAggregation() throws Exception {
    PlanNode node = new AggregationNode(newId(), filter(baseTableScan, and(equals(AE, DE), equals(BE, EE), equals(CE, FE), lessThan(DE, bigintLiteral(10)), lessThan(CE, DE), greaterThan(AE, bigintLiteral(2)), equals(EE, FE))), ImmutableMap.of(C, fakeFunction("test"), D, fakeFunction("test")), ImmutableMap.of(C, fakeFunctionHandle("test", AGGREGATE), D, fakeFunctionHandle("test", AGGREGATE)), ImmutableMap.of(), ImmutableList.of(ImmutableList.of(A, B, C)), AggregationNode.Step.FINAL, Optional.empty(), Optional.empty());
    Expression effectivePredicate = EffectivePredicateExtractor.extract(node, TYPES);
    // Rewrite in terms of group by symbols
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(lessThan(AE, bigintLiteral(10)), lessThan(BE, AE), greaterThan(AE, bigintLiteral(2)), equals(BE, CE)));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Test(org.testng.annotations.Test)

Example 12 with Expression

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

the class TestExpressionVerifier method test.

@Test
public void test() {
    Expression actual = expression("NOT(orderkey = 3 AND custkey = 3 AND orderkey < 10)");
    SymbolAliases symbolAliases = SymbolAliases.builder().put("X", new SymbolReference("orderkey")).put("Y", new SymbolReference("custkey")).build();
    ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);
    assertTrue(verifier.process(actual, expression("NOT(X = 3 AND Y = 3 AND X < 10)")));
    assertThrows(() -> verifier.process(actual, expression("NOT(X = 3 AND Y = 3 AND Z < 10)")));
    assertFalse(verifier.process(actual, expression("NOT(X = 3 AND X = 3 AND X < 10)")));
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Test(org.testng.annotations.Test)

Example 13 with Expression

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

the class ExpressionVerifier method visitInPredicate.

@Override
protected Boolean visitInPredicate(InPredicate actual, Expression expectedExpression) {
    if (expectedExpression instanceof InPredicate) {
        InPredicate expected = (InPredicate) expectedExpression;
        if (actual.getValueList() instanceof InListExpression) {
            return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), expected.getValueList());
        } else {
            checkState(expected.getValueList() instanceof InListExpression, "ExpressionVerifier doesn't support unpacked expected values. Feel free to add support if needed");
            /*
                 * If the expected value is a value list, but the actual is e.g. a SymbolReference,
                 * we need to unpack the value from the list so that when we hit visitSymbolReference, the
                 * expected.toString() call returns something that the symbolAliases actually contains.
                 * For example, InListExpression.toString returns "(onlyitem)" rather than "onlyitem".
                 *
                 * This is required because actual passes through the analyzer, planner, and possibly optimizers,
                 * one of which sometimes takes the liberty of unpacking the InListExpression.
                 *
                 * Since the expected value doesn't go through all of that, we have to deal with the case
                 * of the actual value being unpacked, but the expected value being an InListExpression.
                 */
            List<Expression> values = ((InListExpression) expected.getValueList()).getValues();
            checkState(values.size() == 1, "Multiple expressions in expected value list %s, but actual value is not a list", values, actual.getValue());
            Expression onlyExpectedExpression = values.get(0);
            return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), onlyExpectedExpression);
        }
    }
    return false;
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 14 with Expression

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

the class OutputMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    int i = 0;
    for (String alias : aliases) {
        Expression expression = symbolAliases.get(alias);
        boolean found = false;
        while (i < node.getOutputSymbols().size()) {
            Symbol outputSymbol = node.getOutputSymbols().get(i++);
            if (expression.equals(outputSymbol.toSymbolReference())) {
                found = true;
                break;
            }
        }
        if (!found) {
            return NO_MATCH;
        }
    }
    return match();
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) Symbol(com.facebook.presto.sql.planner.Symbol)

Example 15 with Expression

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

the class TestEffectivePredicateExtractor method testSemiJoin.

@Test
public void testSemiJoin() throws Exception {
    PlanNode node = new SemiJoinNode(newId(), filter(baseTableScan, and(greaterThan(AE, bigintLiteral(10)), lessThan(AE, bigintLiteral(100)))), filter(baseTableScan, greaterThan(AE, bigintLiteral(5))), A, B, C, Optional.empty(), Optional.empty(), Optional.empty());
    Expression effectivePredicate = EffectivePredicateExtractor.extract(node, TYPES);
    // Currently, only pull predicates through the source plan
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(and(greaterThan(AE, bigintLiteral(10)), lessThan(AE, bigintLiteral(100)))));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) Test(org.testng.annotations.Test)

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