use of io.confluent.ksql.function.UdfFactory in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldHandleFunctionCallsWithGenerics.
@Test
public void shouldHandleFunctionCallsWithGenerics() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
when(udf.newInstance(any())).thenReturn(new AddUdf());
givenUdf("FOO", udfFactory, udf);
when(udf.parameters()).thenReturn(ImmutableList.of(GenericType.of("T"), GenericType.of("T")));
// When:
InterpretedExpression interpreter1 = interpreter(new FunctionCall(FunctionName.of("FOO"), ImmutableList.of(new IntegerLiteral(1), new IntegerLiteral(1))));
final Object object = interpreter1.evaluate(ROW);
// Then:
assertThat(object, is(2));
}
use of io.confluent.ksql.function.UdfFactory in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldEvaluateLambda_functionCall.
@Test
public void shouldEvaluateLambda_functionCall() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
when(udf.newInstance(any())).thenReturn(new TransFormUdf());
givenUdf("TRANSFORM", udfFactory, udf);
when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(IntegerType.INSTANCE), LambdaType.of(ImmutableList.of(IntegerType.INSTANCE), IntegerType.INSTANCE)));
when(udf.getReturnType(any())).thenReturn(SqlTypes.array(SqlTypes.INTEGER));
// When:
InterpretedExpression interpreter1 = interpreter(new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(new CreateArrayExpression(ImmutableList.of(new IntegerLiteral(1), new IntegerLiteral(2))), new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new LambdaVariable("X"))))));
// Then:
assertThat(interpreter1.evaluate(ROW), is(ImmutableList.of(2, 3)));
}
use of io.confluent.ksql.function.UdfFactory in project ksql by confluentinc.
the class DescribeFunctionExecutor method describeNonAggregateFunction.
private static FunctionDescriptionList describeNonAggregateFunction(final KsqlExecutionContext executionContext, final FunctionName functionName, final String statementText) {
final UdfFactory udfFactory = executionContext.getMetaStore().getUdfFactory(functionName);
final ImmutableList.Builder<FunctionInfo> listBuilder = ImmutableList.builder();
udfFactory.eachFunction(func -> listBuilder.add(getFunctionInfo(func.parameterInfo(), func.declaredReturnType(), func.getDescription(), func.isVariadic())));
return createFunctionDescriptionList(statementText, udfFactory.getMetadata(), listBuilder.build(), FunctionType.SCALAR);
}
use of io.confluent.ksql.function.UdfFactory in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldHandleFunctionCallsWithGenerics.
@Test
public void shouldHandleFunctionCallsWithGenerics() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
givenUdf("FOO", udfFactory, udf, SqlTypes.STRING);
when(udf.parameters()).thenReturn(ImmutableList.of(GenericType.of("T"), GenericType.of("T")));
// When:
final String javaExpression = sqlToJavaVisitor.process(new FunctionCall(FunctionName.of("FOO"), ImmutableList.of(new IntegerLiteral(1), new IntegerLiteral(1))));
// Then:
assertThat(javaExpression, is("((String) FOO_0.evaluate(1, 1))"));
}
use of io.confluent.ksql.function.UdfFactory in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForFunctionWithMultipleLambdas.
@Test
public void shouldGenerateCorrectCodeForFunctionWithMultipleLambdas() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
givenUdf("function", udfFactory, udf, SqlTypes.STRING);
when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), ParamTypes.STRING, LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.STRING), ParamTypes.DOUBLE), LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.STRING), ParamTypes.STRING)));
final Expression expression = new FunctionCall(FunctionName.of("function"), ImmutableList.of(ARRAYCOL, COL1, new LambdaFunctionCall(ImmutableList.of("X", "S"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new LambdaVariable("X"))), new LambdaFunctionCall(ImmutableList.of("X", "S"), new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, new LambdaVariable("X"), new IntegerLiteral(10)), new StringLiteral("test")), new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, new LambdaVariable("X"), new IntegerLiteral(100)), new StringLiteral("test2"))), Optional.of(new LambdaVariable("S"))))));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then
assertThat(javaExpression, equalTo("((String) function_0.evaluate(COL4, COL1, new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + " final Double X = (Double) arg1;\n" + " final String S = (String) arg2;\n" + " return (X + X);\n" + " }\n" + "}, new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + " final Double X = (Double) arg1;\n" + " final String S = (String) arg2;\n" + " return ((java.lang.String)SearchedCaseFunction.searchedCaseFunction(ImmutableList.copyOf(Arrays.asList( SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(X)) == null || ((Object)(10)) == null) ? false : (X < 10)); }}, new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"test\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(X)) == null || ((Object)(100)) == null) ? false : (X < 100)); }}, new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"test2\"; }}))), new Supplier<java.lang.String>() { @Override public java.lang.String get() { return S; }}));\n" + " }\n" + "}))"));
}
Aggregations