Search in sources :

Example 21 with FunctionCall

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

the class ExpressionTypeManagerTest method shouldEvaluateAnyNumberOfArgumentLambda.

@Test
public void shouldEvaluateAnyNumberOfArgumentLambda() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.STRING);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(DoubleType.INSTANCE), StringType.INSTANCE, MapType.of(LongType.INSTANCE, DoubleType.INSTANCE), LambdaType.of(ImmutableList.of(DoubleType.INSTANCE, StringType.INSTANCE, LongType.INSTANCE, DoubleType.INSTANCE), StringType.INSTANCE)));
    final Expression expression = new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(ARRAYCOL, new StringLiteral("Q"), MAPCOL, new LambdaFunctionCall(ImmutableList.of("A", "B", "C", "D"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("C"), new IntegerLiteral(5)))));
    // When:
    final SqlType exprType = expressionTypeManager.getExpressionSqlType(expression);
    // Then:
    assertThat(exprType, is(SqlTypes.STRING));
    verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlTypes.STRING), SqlArgument.of(SqlTypes.map(SqlTypes.BIGINT, SqlTypes.DOUBLE)), SqlArgument.of(SqlLambda.of(4))));
    verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlTypes.STRING), SqlArgument.of(SqlTypes.map(SqlTypes.BIGINT, SqlTypes.DOUBLE)), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.DOUBLE, SqlTypes.STRING, SqlTypes.BIGINT, SqlTypes.DOUBLE), SqlTypes.BIGINT))));
}
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) 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 22 with FunctionCall

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

the class ExpressionTypeManagerTest method shouldHandleMultipleLambdasInSameFunctionCallWithDifferentVariableNames.

@Test
public void shouldHandleMultipleLambdasInSameFunctionCallWithDifferentVariableNames() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.INTEGER);
    when(function.parameters()).thenReturn(ImmutableList.of(MapType.of(LongType.INSTANCE, DoubleType.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(MAPCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("A", "B"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("A"), new LambdaVariable("B"))), new LambdaFunctionCall(ImmutableList.of("K", "V"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("K"), new LambdaVariable("V"))))), 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 23 with FunctionCall

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

the class ExpressionTypeManagerTest method shouldEvaluateLambdaInUDFWithMap.

@Test
public void shouldEvaluateLambdaInUDFWithMap() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.DOUBLE);
    when(function.parameters()).thenReturn(ImmutableList.of(MapType.of(DoubleType.INSTANCE, DoubleType.INSTANCE), LambdaType.of(ImmutableList.of(LongType.INSTANCE, DoubleType.INSTANCE), DoubleType.INSTANCE)));
    final Expression expression = new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(MAPCOL, new LambdaFunctionCall(ImmutableList.of("X", "Y"), 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.map(SqlTypes.BIGINT, SqlTypes.DOUBLE)), SqlArgument.of(SqlLambda.of(2))));
    verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.map(SqlTypes.BIGINT, SqlTypes.DOUBLE)), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.BIGINT, SqlTypes.DOUBLE), SqlTypes.BIGINT))));
}
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 24 with FunctionCall

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

the class FunctionArgumentsUtilTest method shouldResolveFunctionWithoutLambdas.

@Test
public void shouldResolveFunctionWithoutLambdas() {
    // Given:
    givenUdfWithNameAndReturnType("NoLambdas", SqlTypes.STRING);
    when(function.parameters()).thenReturn(ImmutableList.of(ParamTypes.STRING));
    final FunctionCall expression = new FunctionCall(FunctionName.of("NoLambdas"), ImmutableList.of(new StringLiteral("a")));
    // When:
    final FunctionTypeInfo argumentsAndContexts = FunctionArgumentsUtil.getFunctionTypeInfo(expressionTypeManager, expression, udfFactory, Collections.emptyMap());
    // Then:
    assertThat(argumentsAndContexts.getReturnType(), is(SqlTypes.STRING));
    assertThat(argumentsAndContexts.getArgumentInfos().size(), is(1));
    verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.STRING)));
    verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.STRING)));
}
Also used : FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) Test(org.junit.Test)

Example 25 with FunctionCall

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

the class FunctionArgumentsUtilTest method shouldResolveGenericsInLambdaFunction.

@Test
public void shouldResolveGenericsInLambdaFunction() {
    // Given:
    givenUdfWithNameAndReturnType("ComplexFunction", SqlTypes.DOUBLE);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(GenericType.of("X")), MapType.of(GenericType.of("K"), GenericType.of("V")), GenericType.of("Q"), LambdaType.of(ImmutableList.of(GenericType.of("K"), GenericType.of("V"), GenericType.of("Q")), GenericType.of("K")), LambdaType.of(ImmutableList.of(GenericType.of("Q"), GenericType.of("V")), GenericType.of("X"))));
    final FunctionCall expression = givenFunctionCallWithMultipleLambdas();
    // When:
    final FunctionTypeInfo argumentsAndContexts = FunctionArgumentsUtil.getFunctionTypeInfo(expressionTypeManager, expression, udfFactory, Collections.emptyMap());
    // Then:
    assertThat(argumentsAndContexts.getReturnType(), is(SqlTypes.DOUBLE));
    assertThat(argumentsAndContexts.getArgumentInfos().size(), is(5));
    assertThat(argumentsAndContexts.getArgumentInfos().get(3).getSqlArgument(), is(SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.BIGINT, SqlTypes.DOUBLE, SqlTypes.STRING), SqlTypes.BIGINT))));
    assertThat(argumentsAndContexts.getArgumentInfos().get(3).getLambdaSqlTypeMapping().get("X"), is(SqlTypes.BIGINT));
    assertThat(argumentsAndContexts.getArgumentInfos().get(3).getLambdaSqlTypeMapping().get("Y"), is(SqlTypes.DOUBLE));
    assertThat(argumentsAndContexts.getArgumentInfos().get(3).getLambdaSqlTypeMapping().get("Z"), is(SqlTypes.STRING));
    assertThat(argumentsAndContexts.getArgumentInfos().get(4).getSqlArgument(), is(SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.STRING, SqlTypes.DOUBLE), SqlTypes.DOUBLE))));
    assertThat(argumentsAndContexts.getArgumentInfos().get(4).getLambdaSqlTypeMapping().get("A"), is(SqlTypes.STRING));
    assertThat(argumentsAndContexts.getArgumentInfos().get(4).getLambdaSqlTypeMapping().get("B"), is(SqlTypes.DOUBLE));
    // in the first pass we should have
    verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlTypes.map(SqlTypes.BIGINT, SqlTypes.DOUBLE)), SqlArgument.of(SqlTypes.STRING), SqlArgument.of(SqlLambda.of(3)), SqlArgument.of(SqlLambda.of(2))));
    verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlTypes.map(SqlTypes.BIGINT, SqlTypes.DOUBLE)), SqlArgument.of(SqlTypes.STRING), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.BIGINT, SqlTypes.DOUBLE, SqlTypes.STRING), SqlTypes.BIGINT)), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.STRING, SqlTypes.DOUBLE), SqlTypes.DOUBLE))));
}
Also used : FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) Test(org.junit.Test)

Aggregations

FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)52 Test (org.junit.Test)47 LambdaFunctionCall (io.confluent.ksql.execution.expression.tree.LambdaFunctionCall)34 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)22 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)22 Expression (io.confluent.ksql.execution.expression.tree.Expression)21 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)17 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)17 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)16 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)16 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)16 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)16 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)16 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)16 LambdaVariable (io.confluent.ksql.execution.expression.tree.LambdaVariable)15 KsqlScalarFunction (io.confluent.ksql.function.KsqlScalarFunction)13 UdfFactory (io.confluent.ksql.function.UdfFactory)13 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)11 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)11 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)11