Search in sources :

Example 1 with BindExpression

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

the class AstBuilder method visitFunctionCall.

@Override
public Node visitFunctionCall(SqlBaseParser.FunctionCallContext context) {
    Optional<Expression> filter = visitIfPresent(context.filter(), Expression.class);
    Optional<Window> window = visitIfPresent(context.over(), Window.class);
    Optional<OrderBy> orderBy = Optional.empty();
    if (context.ORDER() != null) {
        orderBy = Optional.of(new OrderBy(visit(context.sortItem(), SortItem.class)));
    }
    QualifiedName name = getQualifiedName(context.qualifiedName());
    boolean distinct = isDistinct(context.setQuantifier());
    boolean ignoreNulls = context.nullTreatment() != null && context.nullTreatment().IGNORE() != null;
    if (name.toString().equalsIgnoreCase("if")) {
        check(context.expression().size() == 2 || context.expression().size() == 3, "Invalid number of arguments for 'if' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'if' function", context);
        check(!distinct, "DISTINCT not valid for 'if' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'if' function", context);
        Expression elseExpression = null;
        if (context.expression().size() == 3) {
            elseExpression = (Expression) visit(context.expression(2));
        }
        return new IfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)), elseExpression);
    }
    if (name.toString().equalsIgnoreCase("nullif")) {
        check(context.expression().size() == 2, "Invalid number of arguments for 'nullif' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'nullif' function", context);
        check(!distinct, "DISTINCT not valid for 'nullif' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'nullif' function", context);
        return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
    }
    if (name.toString().equalsIgnoreCase("coalesce")) {
        check(context.expression().size() >= 2, "The 'coalesce' function must have at least two arguments", context);
        check(!window.isPresent(), "OVER clause not valid for 'coalesce' function", context);
        check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'coalesce' function", context);
        return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
    }
    if (name.toString().equalsIgnoreCase("try")) {
        check(context.expression().size() == 1, "The 'try' function must have exactly one argument", context);
        check(!window.isPresent(), "OVER clause not valid for 'try' function", context);
        check(!distinct, "DISTINCT not valid for 'try' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'try' function", context);
        return new TryExpression(getLocation(context), (Expression) visit(getOnlyElement(context.expression())));
    }
    if (name.toString().equalsIgnoreCase("$internal$bind")) {
        check(context.expression().size() >= 1, "The '$internal$bind' function must have at least one arguments", context);
        check(!window.isPresent(), "OVER clause not valid for '$internal$bind' function", context);
        check(!distinct, "DISTINCT not valid for '$internal$bind' function", context);
        check(!filter.isPresent(), "FILTER not valid for '$internal$bind' function", context);
        int numValues = context.expression().size() - 1;
        List<Expression> arguments = context.expression().stream().map(this::visit).map(Expression.class::cast).collect(toImmutableList());
        return new BindExpression(getLocation(context), arguments.subList(0, numValues), arguments.get(numValues));
    }
    return new FunctionCall(getLocation(context), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, ignoreNulls, visit(context.expression(), Expression.class));
}
Also used : Window(com.facebook.presto.sql.tree.Window) OrderBy(com.facebook.presto.sql.tree.OrderBy) IfExpression(com.facebook.presto.sql.tree.IfExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) TryExpression(com.facebook.presto.sql.tree.TryExpression) SortItem(com.facebook.presto.sql.tree.SortItem) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) BindExpression(com.facebook.presto.sql.tree.BindExpression) 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) BindExpression(com.facebook.presto.sql.tree.BindExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression)

Example 2 with BindExpression

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

the class TestLambdaCaptureDesugaringRewriter method testRewriteBasicLambda.

@Test
public void testRewriteBasicLambda() {
    final List<VariableReferenceExpression> variables = ImmutableList.of(new VariableReferenceExpression(Optional.empty(), "a", BIGINT), new VariableReferenceExpression(Optional.empty(), "x", BIGINT));
    final PlanVariableAllocator allocator = new PlanVariableAllocator(variables);
    assertEquals(rewrite(expression("x -> a + x"), allocator), new BindExpression(ImmutableList.of(expression("a")), new LambdaExpression(Stream.of("a_0", "x").map(Identifier::new).map(LambdaArgumentDeclaration::new).collect(toList()), expression("a_0 + x"))));
}
Also used : LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) BindExpression(com.facebook.presto.sql.tree.BindExpression) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) Test(org.testng.annotations.Test)

Aggregations

BindExpression (com.facebook.presto.sql.tree.BindExpression)2 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)2 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)1 PlanVariableAllocator (com.facebook.presto.sql.planner.PlanVariableAllocator)1 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)1 ArithmeticUnaryExpression (com.facebook.presto.sql.tree.ArithmeticUnaryExpression)1 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)1 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)1 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)1 Expression (com.facebook.presto.sql.tree.Expression)1 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)1 IfExpression (com.facebook.presto.sql.tree.IfExpression)1 InListExpression (com.facebook.presto.sql.tree.InListExpression)1 LambdaArgumentDeclaration (com.facebook.presto.sql.tree.LambdaArgumentDeclaration)1 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)1 NotExpression (com.facebook.presto.sql.tree.NotExpression)1 NullIfExpression (com.facebook.presto.sql.tree.NullIfExpression)1 OrderBy (com.facebook.presto.sql.tree.OrderBy)1 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)1 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)1