use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForNestedLambdas.
@Test
public void shouldGenerateCorrectCodeForNestedLambdas() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
givenUdf("nested", udfFactory, udf, SqlTypes.DOUBLE);
when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), ParamTypes.DOUBLE, LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.INTEGER), ParamTypes.INTEGER)));
final Expression expression = new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("nested"), ImmutableList.of(ARRAYCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("A", "B"), new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("nested"), ImmutableList.of(ARRAYCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("Q", "V"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("Q"), new LambdaVariable("V"))))), new LambdaVariable("B"))))), new IntegerLiteral(5));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then
assertThat(javaExpression, equalTo("(((Double) nested_0.evaluate(COL4, (Double)NullSafe.apply(0,new Function() {\n" + " @Override\n" + " public Object apply(Object arg1) {\n" + " final Integer val = (Integer) arg1;\n" + " return val.doubleValue();\n" + " }\n" + "}), new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + " final Double A = (Double) arg1;\n" + " final Integer B = (Integer) arg2;\n" + " return (((Double) nested_1.evaluate(COL4, (Double)NullSafe.apply(0,new Function() {\n" + " @Override\n" + " public Object apply(Object arg1) {\n" + " final Integer val = (Integer) arg1;\n" + " return val.doubleValue();\n" + " }\n" + "}), new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + " final Double Q = (Double) arg1;\n" + " final Integer V = (Integer) arg2;\n" + " return (Q + V);\n" + " }\n" + "})) + B);\n" + " }\n" + "})) + 5)"));
}
use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForLambdaExpressionWithTwoArguments.
@Test
public void shouldGenerateCorrectCodeForLambdaExpressionWithTwoArguments() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
givenUdf("REDUCE", udfFactory, udf, SqlTypes.STRING);
when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), ParamTypes.DOUBLE, LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.DOUBLE), ParamTypes.DOUBLE)));
final Expression expression = new FunctionCall(FunctionName.of("REDUCE"), ImmutableList.of(ARRAYCOL, COL3, new LambdaFunctionCall(ImmutableList.of("X", "S"), (new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new LambdaVariable("S"))))));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then
assertThat(javaExpression, equalTo("((String) REDUCE_0.evaluate(COL4, COL3, new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + " final Double X = (Double) arg1;\n" + " final Double S = (Double) arg2;\n" + " return (X + S);\n" + " }\n" + "}))"));
}
use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.
the class FunctionArgumentsUtil method firstPassOverFunctionArguments.
private static List<SqlArgument> firstPassOverFunctionArguments(final List<Expression> arguments, final ExpressionTypeManager expressionTypeManager, final Map<String, SqlType> lambdaMapping) {
final List<SqlArgument> functionArgumentTypes = new ArrayList<>();
for (final Expression expression : arguments) {
if (expression instanceof LambdaFunctionCall) {
functionArgumentTypes.add(SqlArgument.of(SqlLambda.of(((LambdaFunctionCall) expression).getArguments().size())));
} else {
final SqlType resolvedArgType = expressionTypeManager.getExpressionSqlType(expression, new HashMap<>(lambdaMapping));
functionArgumentTypes.add(SqlArgument.of(resolvedArgType));
}
}
return functionArgumentTypes;
}
use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.
the class FunctionArgumentsUtilTest method shouldResolveLambdaWithoutGenerics.
@Test
public void shouldResolveLambdaWithoutGenerics() {
// Given:
givenUdfWithNameAndReturnType("SmallLambda", SqlTypes.DOUBLE);
when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), LambdaType.of(ImmutableList.of(ParamTypes.INTEGER), ParamTypes.INTEGER)));
final FunctionCall expression = new FunctionCall(FunctionName.of("SmallLambda"), ImmutableList.of(ARRAYCOL, new LambdaFunctionCall(ImmutableList.of("2.3"), new ArithmeticBinaryExpression(Operator.ADD, new DoubleLiteral(2.3), new DoubleLiteral(2.3)))));
// When:
final FunctionTypeInfo argumentsAndContexts = FunctionArgumentsUtil.getFunctionTypeInfo(expressionTypeManager, expression, udfFactory, Collections.emptyMap());
// Then:
assertThat(argumentsAndContexts.getReturnType(), is(SqlTypes.DOUBLE));
assertThat(argumentsAndContexts.getArgumentInfos().size(), is(2));
verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlLambda.of(1))));
verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER), SqlTypes.DOUBLE))));
}
use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldEvaluateLambdaInUDFWithArray.
@Test
public void shouldEvaluateLambdaInUDFWithArray() {
// Given:
givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.DOUBLE);
when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(DoubleType.INSTANCE), LambdaType.of(ImmutableList.of(DoubleType.INSTANCE), DoubleType.INSTANCE)));
final Expression expression = new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(ARRAYCOL, new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new IntegerLiteral(5)))));
// When:
final SqlType exprType = expressionTypeManager.getExpressionSqlType(expression);
// Then:
assertThat(exprType, is(SqlTypes.DOUBLE));
verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlLambda.of(1))));
verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.DOUBLE), SqlTypes.DOUBLE))));
}
Aggregations