Search in sources :

Example 1 with TriFunction

use of io.confluent.ksql.execution.codegen.helpers.TriFunction 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)

Aggregations

TriFunction (io.confluent.ksql.execution.codegen.helpers.TriFunction)1 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 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)1 LambdaFunctionCall (io.confluent.ksql.execution.expression.tree.LambdaFunctionCall)1 LambdaVariable (io.confluent.ksql.execution.expression.tree.LambdaVariable)1 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)1 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)1 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)1 Context (io.confluent.ksql.execution.interpreter.TermCompiler.Context)1 KsqlScalarFunction (io.confluent.ksql.function.KsqlScalarFunction)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1