Search in sources :

Example 1 with Context

use of io.confluent.ksql.execution.interpreter.TermCompiler.Context 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)

Example 2 with Context

use of io.confluent.ksql.execution.interpreter.TermCompiler.Context in project ksql by confluentinc.

the class TermCompiler method visitCreateArrayExpression.

@Override
public Term visitCreateArrayExpression(final CreateArrayExpression exp, final Context context) {
    final List<Expression> expressions = CoercionUtil.coerceUserList(exp.getValues(), expressionTypeManager, context.getLambdaSqlTypeMapping()).expressions();
    final List<Term> arrayTerms = expressions.stream().map(value -> process(value, context)).collect(ImmutableList.toImmutableList());
    final SqlType sqlType = expressionTypeManager.getExpressionSqlType(exp, context.getLambdaSqlTypeMapping());
    return new CreateArrayTerm(arrayTerms, sqlType);
}
Also used : TimestampLiteral(io.confluent.ksql.execution.expression.tree.TimestampLiteral) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) GenericsUtil(io.confluent.ksql.function.GenericsUtil) Pair(io.confluent.ksql.util.Pair) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) LiteralTerms(io.confluent.ksql.execution.interpreter.terms.LiteralTerms) Map(java.util.Map) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) SchemaConverters.sqlToFunctionConverter(io.confluent.ksql.schema.ksql.SchemaConverters.sqlToFunctionConverter) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) BytesLiteral(io.confluent.ksql.execution.expression.tree.BytesLiteral) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) KsqlException(io.confluent.ksql.util.KsqlException) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) Iterables(com.google.common.collect.Iterables) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) ExpressionVisitor(io.confluent.ksql.execution.expression.tree.ExpressionVisitor) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ArrayList(java.util.ArrayList) InListEvaluator(io.confluent.ksql.execution.codegen.helpers.InListEvaluator) BetweenPredicate(io.confluent.ksql.execution.expression.tree.BetweenPredicate) Cast(io.confluent.ksql.execution.expression.tree.Cast) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) CoercionUtil(io.confluent.ksql.execution.util.CoercionUtil) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Context(io.confluent.ksql.execution.interpreter.TermCompiler.Context) LogicalBinaryTerms(io.confluent.ksql.execution.interpreter.terms.LogicalBinaryTerms) FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) Type(io.confluent.ksql.execution.expression.tree.Type) Kudf(io.confluent.ksql.function.udf.Kudf) ParamTypes(io.confluent.ksql.function.types.ParamTypes) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ArrayType(io.confluent.ksql.function.types.ArrayType) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) DecimalUtil(io.confluent.ksql.util.DecimalUtil) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) UdfFactory(io.confluent.ksql.function.UdfFactory) ArgumentInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.ArgumentInfo) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) DateLiteral(io.confluent.ksql.execution.expression.tree.DateLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Immutable(com.google.errorprone.annotations.Immutable) IntervalUnit(io.confluent.ksql.execution.expression.tree.IntervalUnit) 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) ImmutableMap(com.google.common.collect.ImmutableMap) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) IsNotNullPredicate(io.confluent.ksql.execution.expression.tree.IsNotNullPredicate) FunctionArgumentsUtil(io.confluent.ksql.execution.util.FunctionArgumentsUtil) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Streams(com.google.common.collect.Streams) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) String.format(java.lang.String.format) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) List(java.util.List) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) Optional(java.util.Optional) Column(io.confluent.ksql.schema.ksql.Column) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ArrayAccess(io.confluent.ksql.execution.codegen.helpers.ArrayAccess) IsNullPredicate(io.confluent.ksql.execution.expression.tree.IsNullPredicate) ParamType(io.confluent.ksql.function.types.ParamType) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) HashMap(java.util.HashMap) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) TimeLiteral(io.confluent.ksql.execution.expression.tree.TimeLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) 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) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) 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)

Example 3 with Context

use of io.confluent.ksql.execution.interpreter.TermCompiler.Context in project ksql by confluentinc.

the class TermCompiler method visitSearchedCaseExpression.

@Override
public Term visitSearchedCaseExpression(final SearchedCaseExpression node, final Context context) {
    final SqlType resultSchema = expressionTypeManager.getExpressionSqlType(node, context.getLambdaSqlTypeMapping());
    final List<Pair<Term, Term>> operandResultTerms = node.getWhenClauses().stream().map(whenClause -> Pair.of(process(whenClause.getOperand(), context), process(whenClause.getResult(), context))).collect(ImmutableList.toImmutableList());
    final Optional<Term> defaultValueTerm = node.getDefaultValue().map(exp -> process(node.getDefaultValue().get(), context));
    return new SearchedCaseTerm(operandResultTerms, defaultValueTerm, resultSchema);
}
Also used : TimestampLiteral(io.confluent.ksql.execution.expression.tree.TimestampLiteral) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) GenericsUtil(io.confluent.ksql.function.GenericsUtil) Pair(io.confluent.ksql.util.Pair) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) LiteralTerms(io.confluent.ksql.execution.interpreter.terms.LiteralTerms) Map(java.util.Map) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) SchemaConverters.sqlToFunctionConverter(io.confluent.ksql.schema.ksql.SchemaConverters.sqlToFunctionConverter) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) BytesLiteral(io.confluent.ksql.execution.expression.tree.BytesLiteral) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) KsqlException(io.confluent.ksql.util.KsqlException) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) Iterables(com.google.common.collect.Iterables) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) ExpressionVisitor(io.confluent.ksql.execution.expression.tree.ExpressionVisitor) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ArrayList(java.util.ArrayList) InListEvaluator(io.confluent.ksql.execution.codegen.helpers.InListEvaluator) BetweenPredicate(io.confluent.ksql.execution.expression.tree.BetweenPredicate) Cast(io.confluent.ksql.execution.expression.tree.Cast) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) CoercionUtil(io.confluent.ksql.execution.util.CoercionUtil) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Context(io.confluent.ksql.execution.interpreter.TermCompiler.Context) LogicalBinaryTerms(io.confluent.ksql.execution.interpreter.terms.LogicalBinaryTerms) FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) Type(io.confluent.ksql.execution.expression.tree.Type) Kudf(io.confluent.ksql.function.udf.Kudf) ParamTypes(io.confluent.ksql.function.types.ParamTypes) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ArrayType(io.confluent.ksql.function.types.ArrayType) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) DecimalUtil(io.confluent.ksql.util.DecimalUtil) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) UdfFactory(io.confluent.ksql.function.UdfFactory) ArgumentInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.ArgumentInfo) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) DateLiteral(io.confluent.ksql.execution.expression.tree.DateLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Immutable(com.google.errorprone.annotations.Immutable) IntervalUnit(io.confluent.ksql.execution.expression.tree.IntervalUnit) 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) ImmutableMap(com.google.common.collect.ImmutableMap) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) IsNotNullPredicate(io.confluent.ksql.execution.expression.tree.IsNotNullPredicate) FunctionArgumentsUtil(io.confluent.ksql.execution.util.FunctionArgumentsUtil) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Streams(com.google.common.collect.Streams) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) String.format(java.lang.String.format) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) List(java.util.List) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) Optional(java.util.Optional) Column(io.confluent.ksql.schema.ksql.Column) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ArrayAccess(io.confluent.ksql.execution.codegen.helpers.ArrayAccess) IsNullPredicate(io.confluent.ksql.execution.expression.tree.IsNullPredicate) ParamType(io.confluent.ksql.function.types.ParamType) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) HashMap(java.util.HashMap) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) TimeLiteral(io.confluent.ksql.execution.expression.tree.TimeLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) 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) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) Pair(io.confluent.ksql.util.Pair)

Example 4 with Context

use of io.confluent.ksql.execution.interpreter.TermCompiler.Context in project ksql by confluentinc.

the class InterpretedExpressionTest method shouldEvaluateLambda.

@SuppressWarnings("unchecked")
@Test
public void shouldEvaluateLambda() {
    // Given:
    final Expression lambda1 = new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new LambdaVariable("X")));
    final Expression lambda2 = new LambdaFunctionCall(ImmutableList.of("X", "Y"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("Y"), new LambdaVariable("X")));
    final Expression lambda3 = new LambdaFunctionCall(ImmutableList.of("X", "Y", "Z"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("Y"), new LambdaVariable("Z"))));
    final Context context1 = new Context(ImmutableMap.of("X", SqlTypes.INTEGER));
    final Context context2 = new Context(ImmutableMap.of("X", SqlTypes.INTEGER, "Y", SqlTypes.INTEGER));
    final Context context3 = new Context(ImmutableMap.of("X", SqlTypes.INTEGER, "Y", SqlTypes.INTEGER, "Z", SqlTypes.INTEGER));
    // When:
    InterpretedExpression interpreter1 = interpreter(lambda1, context1);
    InterpretedExpression interpreter2 = interpreter(lambda2, context2);
    InterpretedExpression interpreter3 = interpreter(lambda3, context3);
    // Then:
    final Function<Integer, Integer> func1 = (Function<Integer, Integer>) interpreter1.evaluate(ROW);
    assertThat(func1.apply(1), is(2));
    assertThat(func1.apply(2), is(3));
    final BiFunction<Integer, Integer, Integer> func2 = (BiFunction<Integer, Integer, Integer>) interpreter2.evaluate(ROW);
    assertThat(func2.apply(1, 2), is(3));
    assertThat(func2.apply(2, 4), is(6));
    final TriFunction<Integer, Integer, Integer, Integer> func3 = (TriFunction<Integer, Integer, Integer, Integer>) interpreter3.evaluate(ROW);
    assertThat(func3.apply(1, 2, 3), is(6));
    assertThat(func3.apply(2, 4, 6), is(12));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Context(io.confluent.ksql.execution.interpreter.TermCompiler.Context) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) BiFunction(java.util.function.BiFunction) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) Function(java.util.function.Function) TriFunction(io.confluent.ksql.execution.codegen.helpers.TriFunction) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) BiFunction(java.util.function.BiFunction) TriFunction(io.confluent.ksql.execution.codegen.helpers.TriFunction) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 5 with Context

use of io.confluent.ksql.execution.interpreter.TermCompiler.Context in project ksql by confluentinc.

the class TermCompiler method visitInPredicate.

@Override
public Term visitInPredicate(final InPredicate inPredicate, final Context context) {
    final InPredicate preprocessed = InListEvaluator.preprocess(inPredicate, expressionTypeManager, context.getLambdaSqlTypeMapping());
    final Term value = process(preprocessed.getValue(), context);
    final List<Term> valueList = preprocessed.getValueList().getValues().stream().map(v -> process(v, context)).collect(ImmutableList.toImmutableList());
    return new InPredicateTerm(value, valueList);
}
Also used : TimestampLiteral(io.confluent.ksql.execution.expression.tree.TimestampLiteral) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) GenericsUtil(io.confluent.ksql.function.GenericsUtil) Pair(io.confluent.ksql.util.Pair) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) LiteralTerms(io.confluent.ksql.execution.interpreter.terms.LiteralTerms) Map(java.util.Map) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) SchemaConverters.sqlToFunctionConverter(io.confluent.ksql.schema.ksql.SchemaConverters.sqlToFunctionConverter) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) BytesLiteral(io.confluent.ksql.execution.expression.tree.BytesLiteral) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) KsqlException(io.confluent.ksql.util.KsqlException) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) Iterables(com.google.common.collect.Iterables) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) ExpressionVisitor(io.confluent.ksql.execution.expression.tree.ExpressionVisitor) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ArrayList(java.util.ArrayList) InListEvaluator(io.confluent.ksql.execution.codegen.helpers.InListEvaluator) BetweenPredicate(io.confluent.ksql.execution.expression.tree.BetweenPredicate) Cast(io.confluent.ksql.execution.expression.tree.Cast) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) CoercionUtil(io.confluent.ksql.execution.util.CoercionUtil) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Context(io.confluent.ksql.execution.interpreter.TermCompiler.Context) LogicalBinaryTerms(io.confluent.ksql.execution.interpreter.terms.LogicalBinaryTerms) FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) Type(io.confluent.ksql.execution.expression.tree.Type) Kudf(io.confluent.ksql.function.udf.Kudf) ParamTypes(io.confluent.ksql.function.types.ParamTypes) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ArrayType(io.confluent.ksql.function.types.ArrayType) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) DecimalUtil(io.confluent.ksql.util.DecimalUtil) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) UdfFactory(io.confluent.ksql.function.UdfFactory) ArgumentInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.ArgumentInfo) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) DateLiteral(io.confluent.ksql.execution.expression.tree.DateLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Immutable(com.google.errorprone.annotations.Immutable) IntervalUnit(io.confluent.ksql.execution.expression.tree.IntervalUnit) 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) ImmutableMap(com.google.common.collect.ImmutableMap) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) IsNotNullPredicate(io.confluent.ksql.execution.expression.tree.IsNotNullPredicate) FunctionArgumentsUtil(io.confluent.ksql.execution.util.FunctionArgumentsUtil) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Streams(com.google.common.collect.Streams) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) String.format(java.lang.String.format) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) List(java.util.List) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) Optional(java.util.Optional) Column(io.confluent.ksql.schema.ksql.Column) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ArrayAccess(io.confluent.ksql.execution.codegen.helpers.ArrayAccess) IsNullPredicate(io.confluent.ksql.execution.expression.tree.IsNullPredicate) ParamType(io.confluent.ksql.function.types.ParamType) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) HashMap(java.util.HashMap) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) TimeLiteral(io.confluent.ksql.execution.expression.tree.TimeLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) 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) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm)

Aggregations

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