Search in sources :

Example 1 with FunctionCall

use of io.confluent.ksql.parser.tree.FunctionCall in project ksql by confluentinc.

the class AstBuilder method visitFunctionCall.

@Override
public Node visitFunctionCall(SqlBaseParser.FunctionCallContext context) {
    Optional<Window> window = visitIfPresent(context.over(), Window.class);
    QualifiedName name = getQualifiedName(context.qualifiedName());
    boolean distinct = false;
    if (name.toString().equals("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);
        return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
    }
    return new FunctionCall(getLocation(context), getQualifiedName(context.qualifiedName()), window, distinct, visit(context.expression(), Expression.class));
}
Also used : Window(io.confluent.ksql.parser.tree.Window) InListExpression(io.confluent.ksql.parser.tree.InListExpression) NullIfExpression(io.confluent.ksql.parser.tree.NullIfExpression) SimpleCaseExpression(io.confluent.ksql.parser.tree.SimpleCaseExpression) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) LogicalBinaryExpression(io.confluent.ksql.parser.tree.LogicalBinaryExpression) TumblingWindowExpression(io.confluent.ksql.parser.tree.TumblingWindowExpression) ArithmeticBinaryExpression(io.confluent.ksql.parser.tree.ArithmeticBinaryExpression) NotExpression(io.confluent.ksql.parser.tree.NotExpression) HoppingWindowExpression(io.confluent.ksql.parser.tree.HoppingWindowExpression) SubscriptExpression(io.confluent.ksql.parser.tree.SubscriptExpression) SessionWindowExpression(io.confluent.ksql.parser.tree.SessionWindowExpression) SearchedCaseExpression(io.confluent.ksql.parser.tree.SearchedCaseExpression) LambdaExpression(io.confluent.ksql.parser.tree.LambdaExpression) SubqueryExpression(io.confluent.ksql.parser.tree.SubqueryExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) ArithmeticUnaryExpression(io.confluent.ksql.parser.tree.ArithmeticUnaryExpression) QualifiedName(io.confluent.ksql.parser.tree.QualifiedName) NullIfExpression(io.confluent.ksql.parser.tree.NullIfExpression) FunctionCall(io.confluent.ksql.parser.tree.FunctionCall)

Example 2 with FunctionCall

use of io.confluent.ksql.parser.tree.FunctionCall in project ksql by confluentinc.

the class AggregateExpressionRewriter method rewriteFunctionCall.

@Override
public Expression rewriteFunctionCall(FunctionCall node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
    String functionName = node.getName().getSuffix();
    if (functionRegistry.isAnAggregateFunction(functionName)) {
        String aggVarName = AGGREGATE_FUNCTION_VARIABLE_PREFIX + aggVariableIndex;
        aggVariableIndex++;
        return new QualifiedNameReference(QualifiedName.of(aggVarName));
    } else {
        List<Expression> arguments = new ArrayList<>();
        for (Expression argExpression : node.getArguments()) {
            arguments.add(treeRewriter.rewrite(argExpression, context));
        }
        return new FunctionCall(node.getName(), arguments);
    }
}
Also used : Expression(io.confluent.ksql.parser.tree.Expression) ArrayList(java.util.ArrayList) FunctionCall(io.confluent.ksql.parser.tree.FunctionCall) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference)

Example 3 with FunctionCall

use of io.confluent.ksql.parser.tree.FunctionCall in project ksql by confluentinc.

the class ExpressionFormatterTest method shouldFormatFunctionWithDistinct.

@Test
public void shouldFormatFunctionWithDistinct() {
    final FunctionCall functionCall = new FunctionCall(new NodeLocation(1, 1), QualifiedName.of("function", "COUNT"), true, Collections.singletonList(new StringLiteral("name")));
    assertThat(ExpressionFormatter.formatExpression(functionCall), equalTo("function.COUNT(DISTINCT 'name')"));
}
Also used : NodeLocation(io.confluent.ksql.parser.tree.NodeLocation) StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) FunctionCall(io.confluent.ksql.parser.tree.FunctionCall) Test(org.junit.Test)

Example 4 with FunctionCall

use of io.confluent.ksql.parser.tree.FunctionCall in project ksql by confluentinc.

the class ExpressionFormatterTest method shouldFormatFunctionCallWithCount.

@Test
public void shouldFormatFunctionCallWithCount() {
    final FunctionCall functionCall = new FunctionCall(QualifiedName.of("function", "COUNT"), Collections.singletonList(new StringLiteral("name")));
    assertThat(ExpressionFormatter.formatExpression(functionCall), equalTo("function.COUNT('name')"));
}
Also used : StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) FunctionCall(io.confluent.ksql.parser.tree.FunctionCall) Test(org.junit.Test)

Example 5 with FunctionCall

use of io.confluent.ksql.parser.tree.FunctionCall in project ksql by confluentinc.

the class ExpressionFormatterTest method shouldFormatFunctionCountStar.

@Test
public void shouldFormatFunctionCountStar() {
    final FunctionCall functionCall = new FunctionCall(QualifiedName.of("function", "COUNT"), Collections.emptyList());
    assertThat(ExpressionFormatter.formatExpression(functionCall), equalTo("function.COUNT(*)"));
}
Also used : FunctionCall(io.confluent.ksql.parser.tree.FunctionCall) Test(org.junit.Test)

Aggregations

FunctionCall (io.confluent.ksql.parser.tree.FunctionCall)8 StringLiteral (io.confluent.ksql.parser.tree.StringLiteral)4 Test (org.junit.Test)4 Expression (io.confluent.ksql.parser.tree.Expression)3 HoppingWindowExpression (io.confluent.ksql.parser.tree.HoppingWindowExpression)3 TumblingWindowExpression (io.confluent.ksql.parser.tree.TumblingWindowExpression)3 WindowExpression (io.confluent.ksql.parser.tree.WindowExpression)3 ArithmeticBinaryExpression (io.confluent.ksql.parser.tree.ArithmeticBinaryExpression)2 ArithmeticUnaryExpression (io.confluent.ksql.parser.tree.ArithmeticUnaryExpression)2 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)2 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)2 InListExpression (io.confluent.ksql.parser.tree.InListExpression)2 LambdaExpression (io.confluent.ksql.parser.tree.LambdaExpression)2 LogicalBinaryExpression (io.confluent.ksql.parser.tree.LogicalBinaryExpression)2 NodeLocation (io.confluent.ksql.parser.tree.NodeLocation)2 NotExpression (io.confluent.ksql.parser.tree.NotExpression)2 NullIfExpression (io.confluent.ksql.parser.tree.NullIfExpression)2 SearchedCaseExpression (io.confluent.ksql.parser.tree.SearchedCaseExpression)2 SessionWindowExpression (io.confluent.ksql.parser.tree.SessionWindowExpression)2 SimpleCaseExpression (io.confluent.ksql.parser.tree.SimpleCaseExpression)2