use of io.confluent.ksql.execution.expression.tree.FunctionCall 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))));
}
use of io.confluent.ksql.execution.expression.tree.FunctionCall in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldHandleNestedUdfs.
@Test
public void shouldHandleNestedUdfs() {
// Given:
givenUdfWithNameAndReturnType("EXTRACTJSONFIELD", SqlTypes.STRING);
final UdfFactory outerFactory = mock(UdfFactory.class);
final KsqlScalarFunction function = mock(KsqlScalarFunction.class);
givenUdfWithNameAndReturnType("LCASE", SqlTypes.STRING, outerFactory, function);
final Expression inner = new FunctionCall(FunctionName.of("EXTRACTJSONFIELD"), ImmutableList.of(COL1, new StringLiteral("$.name)")));
final Expression expression = new FunctionCall(FunctionName.of("LCASE"), ImmutableList.of(inner));
// When/Then:
assertThat(expressionTypeManager.getExpressionSqlType(expression), equalTo(SqlTypes.STRING));
}
use of io.confluent.ksql.execution.expression.tree.FunctionCall in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldEvaluateTypeForUDF.
@Test
public void shouldEvaluateTypeForUDF() {
// Given:
givenUdfWithNameAndReturnType("FLOOR", SqlTypes.DOUBLE);
final Expression expression = new FunctionCall(FunctionName.of("FLOOR"), ImmutableList.of(COL3));
// When:
final SqlType exprType = expressionTypeManager.getExpressionSqlType(expression);
// Then:
assertThat(exprType, is(SqlTypes.DOUBLE));
verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.DOUBLE)));
verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.DOUBLE)));
}
use of io.confluent.ksql.execution.expression.tree.FunctionCall 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."));
}
use of io.confluent.ksql.execution.expression.tree.FunctionCall in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldEvaluateTypeForStringUDF.
@Test
public void shouldEvaluateTypeForStringUDF() {
// Given:
givenUdfWithNameAndReturnType("LCASE", SqlTypes.STRING);
final Expression expression = new FunctionCall(FunctionName.of("LCASE"), ImmutableList.of(COL2));
// When:
final SqlType exprType = expressionTypeManager.getExpressionSqlType(expression);
// Then:
assertThat(exprType, is(SqlTypes.STRING));
verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.STRING)));
verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.STRING)));
}
Aggregations