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);
}
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);
}
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);
}
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));
}
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);
}
Aggregations