Search in sources :

Example 16 with LambdaFunctionCall

use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.

the class ExpressionTypeManagerTest method shouldFailToEvaluateLambdaWithMismatchedArgumentNumber.

@Test
public void shouldFailToEvaluateLambdaWithMismatchedArgumentNumber() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.DOUBLE);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(DoubleType.INSTANCE), LambdaType.of(ImmutableList.of(DoubleType.INSTANCE), StringType.INSTANCE)));
    final Expression expression = new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(ARRAYCOL, new LambdaFunctionCall(ImmutableList.of("X", "Y"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new IntegerLiteral(5)))));
    // When:
    final Exception e = assertThrows(Exception.class, () -> expressionTypeManager.getExpressionSqlType(expression));
    // Then:
    assertThat(e.getMessage(), Matchers.containsString("Was expecting 1 arguments but found 2, [X, Y]. Check your lambda statement."));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) 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) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 17 with LambdaFunctionCall

use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.

the class ExpressionTypeManagerTest method shouldEvaluateLambdaArgsToType.

@Test
public void shouldEvaluateLambdaArgsToType() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.STRING);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(DoubleType.INSTANCE), StringType.INSTANCE, LambdaType.of(ImmutableList.of(DoubleType.INSTANCE, StringType.INSTANCE), StringType.INSTANCE)));
    final Expression expression = new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(ARRAYCOL, new StringLiteral("Q"), new LambdaFunctionCall(ImmutableList.of("A", "B"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("A"), new LambdaVariable("B")))));
    // When:
    final Exception e = assertThrows(Exception.class, () -> expressionTypeManager.getExpressionSqlType(expression));
    // Then:
    assertThat(e.getMessage(), Matchers.containsString("Unsupported arithmetic types. DOUBLE STRING"));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) 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) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 18 with LambdaFunctionCall

use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.

the class ExpressionTypeManagerTest method shouldHandleNestedLambdas.

@Test
public void shouldHandleNestedLambdas() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.INTEGER);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(LongType.INSTANCE), IntegerType.INSTANCE, LambdaType.of(ImmutableList.of(DoubleType.INSTANCE, DoubleType.INSTANCE), StringType.INSTANCE), LambdaType.of(ImmutableList.of(DoubleType.INSTANCE, DoubleType.INSTANCE), StringType.INSTANCE)));
    final Expression expression = new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(ARRAYCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("A", "B"), new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("TRANSFORM"), 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 SqlType result = expressionTypeManager.getExpressionSqlType(expression);
    assertThat(result, is(SqlTypes.INTEGER));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) 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) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 19 with LambdaFunctionCall

use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall in project ksql by confluentinc.

the class AstSanitizerTest method shouldAllowNestedLambdaFunctionsWithoutDuplicate.

@Test
public void shouldAllowNestedLambdaFunctionsWithoutDuplicate() {
    // Given:
    final Statement stmt = givenQuery("SELECT TRANSFORM_ARRAY(Col4, (X,Y,Z) => TRANSFORM_MAP(Col4, Q => 4, H => 5), (X,Y,Z) => 0) FROM TEST1;");
    // When:
    final Query result = (Query) AstSanitizer.sanitize(stmt, META_STORE, true);
    // Then:
    assertThat(result.getSelect(), is(new Select(ImmutableList.of(new SingleColumn(new FunctionCall(FunctionName.of("TRANSFORM_ARRAY"), ImmutableList.of(column(TEST1_NAME, "COL4"), new LambdaFunctionCall(ImmutableList.of("X", "Y", "Z"), new FunctionCall(FunctionName.of("TRANSFORM_MAP"), ImmutableList.of(column(TEST1_NAME, "COL4"), new LambdaFunctionCall(ImmutableList.of("Q"), new IntegerLiteral(4)), new LambdaFunctionCall(ImmutableList.of("H"), new IntegerLiteral(5))))), new LambdaFunctionCall(ImmutableList.of("X", "Y", "Z"), new IntegerLiteral(0)))), Optional.of(ColumnName.of("KSQL_COL_0")))))));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) Statement(io.confluent.ksql.parser.tree.Statement) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) Select(io.confluent.ksql.parser.tree.Select) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 20 with LambdaFunctionCall

use of io.confluent.ksql.execution.expression.tree.LambdaFunctionCall 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

LambdaFunctionCall (io.confluent.ksql.execution.expression.tree.LambdaFunctionCall)22 Test (org.junit.Test)20 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)18 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)17 LambdaVariable (io.confluent.ksql.execution.expression.tree.LambdaVariable)17 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)15 Expression (io.confluent.ksql.execution.expression.tree.Expression)14 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)13 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)12 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)12 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)12 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)12 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)12 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)12 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)11 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)8 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)7 KsqlScalarFunction (io.confluent.ksql.function.KsqlScalarFunction)7 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)7 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)5