Search in sources :

Example 1 with FunctionCallTerm

use of io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm in project ksql by confluentinc.

the class TermCompiler method visitFunctionCall.

@Override
public Term visitFunctionCall(final FunctionCall node, final Context context) {
    final UdfFactory udfFactory = functionRegistry.getUdfFactory(node.getName());
    final FunctionTypeInfo argumentsAndContext = FunctionArgumentsUtil.getFunctionTypeInfo(expressionTypeManager, node, udfFactory, context.getLambdaSqlTypeMapping());
    final List<ArgumentInfo> argumentInfos = argumentsAndContext.getArgumentInfos();
    final KsqlScalarFunction function = argumentsAndContext.getFunction();
    final SqlType functionReturnSchema = argumentsAndContext.getReturnType();
    final Class<?> javaClass = SchemaConverters.sqlToJavaConverter().toJavaType(functionReturnSchema);
    final List<Expression> arguments = node.getArguments();
    final List<Term> args = new ArrayList<>();
    for (int i = 0; i < arguments.size(); i++) {
        final Expression arg = arguments.get(i);
        // lambda arguments and null values are considered to have null type
        final SqlType sqlType = argumentInfos.get(i).getSqlArgument().getSqlType().orElse(null);
        ;
        final ParamType paramType;
        if (i >= function.parameters().size() - 1 && function.isVariadic()) {
            paramType = ((ArrayType) Iterables.getLast(function.parameters())).element();
        } else {
            paramType = function.parameters().get(i);
        }
        // This will attempt to cast to the expected argument type and will throw an error if
        // it cannot be done.
        final Term argTerm = process(convertArgument(arg, sqlType, paramType), new Context(argumentInfos.get(i).getLambdaSqlTypeMapping()));
        args.add(argTerm);
    }
    final Kudf kudf = function.newInstance(ksqlConfig);
    return new FunctionCallTerm(kudf, args, javaClass, functionReturnSchema);
}
Also used : Context(io.confluent.ksql.execution.interpreter.TermCompiler.Context) ArrayList(java.util.ArrayList) UdfFactory(io.confluent.ksql.function.UdfFactory) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) CreateMapTerm(io.confluent.ksql.execution.interpreter.terms.CreateMapTerm) LambdaFunction2Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction2Term) SubscriptTerm(io.confluent.ksql.execution.interpreter.terms.SubscriptTerm) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) ParamType(io.confluent.ksql.function.types.ParamType) Kudf(io.confluent.ksql.function.udf.Kudf) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) ArgumentInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.ArgumentInfo)

Aggregations

ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)1 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)1 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)1 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)1 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)1 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)1 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)1 Expression (io.confluent.ksql.execution.expression.tree.Expression)1 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)1 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)1 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)1 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)1 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)1 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)1 Context (io.confluent.ksql.execution.interpreter.TermCompiler.Context)1 ColumnReferenceTerm (io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm)1 CreateArrayTerm (io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm)1 CreateMapTerm (io.confluent.ksql.execution.interpreter.terms.CreateMapTerm)1 DereferenceTerm (io.confluent.ksql.execution.interpreter.terms.DereferenceTerm)1 FunctionCallTerm (io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm)1