Search in sources :

Example 41 with FunctionCall

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

the class TestSqlParser method testSubstringRegisteredFunction.

@Test
public void testSubstringRegisteredFunction() {
    final String givenString = "ABCDEF";
    assertStatement(format("SELECT substring('%s', 2)", givenString), new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("substring"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2")))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement(format("SELECT substring('%s', 2, 3)", givenString), new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("substring"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2"), new LongLiteral("3")))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 42 with FunctionCall

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

the class TestSqlParser method testAggregationWithOrderBy.

@Test
public void testAggregationWithOrderBy() {
    assertExpression("array_agg(x ORDER BY x DESC)", new FunctionCall(QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(identifier("x"), DESCENDING, UNDEFINED)))), false, ImmutableList.of(identifier("x"))));
    assertStatement("SELECT array_agg(x ORDER BY t.y) FROM t", new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new DereferenceExpression(new Identifier("t"), identifier("y")), ASCENDING, UNDEFINED)))), false, ImmutableList.of(new Identifier("x")))), Optional.of(table(QualifiedName.of("t"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 43 with FunctionCall

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

the class TestSqlParser method testLambda.

@Test
public void testLambda() {
    assertExpression("() -> x", new LambdaExpression(ImmutableList.of(), new Identifier("x")));
    assertExpression("x -> sin(x)", new LambdaExpression(ImmutableList.of(new LambdaArgumentDeclaration(identifier("x"))), new FunctionCall(QualifiedName.of("sin"), ImmutableList.of(new Identifier("x")))));
    assertExpression("(x, y) -> mod(x, y)", new LambdaExpression(ImmutableList.of(new LambdaArgumentDeclaration(identifier("x")), new LambdaArgumentDeclaration(identifier("y"))), new FunctionCall(QualifiedName.of("mod"), ImmutableList.of(new Identifier("x"), new Identifier("y")))));
}
Also used : Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) Test(org.testng.annotations.Test)

Example 44 with FunctionCall

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

the class FunctionCallProvider method getExpectedValue.

public FunctionCall getExpectedValue(SymbolAliases aliases) {
    List<Expression> symbolReferences = toSymbolReferences(args, aliases);
    if (isWindowFunction) {
        return new ExpectedWindowFunctionCall(symbolReferences);
    }
    Optional<OrderBy> orderByClause = Optional.empty();
    if (!orderBy.isEmpty()) {
        orderByClause = Optional.of(new OrderBy(orderBy.stream().map(item -> new SortItem(Symbol.from(aliases.get(item.getField())).toSymbolReference(), item.getOrdering(), item.getNullOrdering())).collect(Collectors.toList())));
    }
    return new FunctionCall(name, Optional.empty(), Optional.empty(), orderByClause, distinct, symbolReferences);
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) SortItem(com.facebook.presto.sql.tree.SortItem) Expression(com.facebook.presto.sql.tree.Expression) FunctionCall(com.facebook.presto.sql.tree.FunctionCall)

Example 45 with FunctionCall

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

the class AggregationFunctionMatcher method getAssignedVariable.

@Override
public Optional<VariableReferenceExpression> getAssignedVariable(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    Optional<VariableReferenceExpression> result = Optional.empty();
    if (!(node instanceof AggregationNode)) {
        return result;
    }
    AggregationNode aggregationNode = (AggregationNode) node;
    FunctionCall expectedCall = callMaker.getExpectedValue(symbolAliases);
    for (Map.Entry<VariableReferenceExpression, Aggregation> assignment : aggregationNode.getAggregations().entrySet()) {
        if (verifyAggregation(metadata.getFunctionAndTypeManager(), assignment.getValue(), expectedCall)) {
            checkState(!result.isPresent(), "Ambiguous function calls in %s", aggregationNode);
            result = Optional.of(assignment.getKey());
        }
    }
    return result;
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Map(java.util.Map)

Aggregations

FunctionCall (com.facebook.presto.sql.tree.FunctionCall)46 Expression (com.facebook.presto.sql.tree.Expression)19 Test (org.testng.annotations.Test)19 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)15 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)14 Cast (com.facebook.presto.sql.tree.Cast)11 Identifier (com.facebook.presto.sql.tree.Identifier)10 ImmutableList (com.google.common.collect.ImmutableList)10 Map (java.util.Map)10 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)8 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)7 Symbol (com.facebook.presto.sql.planner.Symbol)7 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)7 Query (com.facebook.presto.sql.tree.Query)7 SymbolReference (com.facebook.presto.sql.tree.SymbolReference)7 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)6 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)6 OrderBy (com.facebook.presto.sql.tree.OrderBy)6 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)6 RowExpression (com.facebook.presto.spi.relation.RowExpression)5