use of io.confluent.ksql.function.KsqlScalarFunction in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForLambdaExpressionWithTwoArguments.
@Test
public void shouldGenerateCorrectCodeForLambdaExpressionWithTwoArguments() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
givenUdf("REDUCE", udfFactory, udf, SqlTypes.STRING);
when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), ParamTypes.DOUBLE, LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.DOUBLE), ParamTypes.DOUBLE)));
final Expression expression = new FunctionCall(FunctionName.of("REDUCE"), ImmutableList.of(ARRAYCOL, COL3, new LambdaFunctionCall(ImmutableList.of("X", "S"), (new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new LambdaVariable("S"))))));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then
assertThat(javaExpression, equalTo("((String) REDUCE_0.evaluate(COL4, COL3, new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + " final Double X = (Double) arg1;\n" + " final Double S = (Double) arg2;\n" + " return (X + S);\n" + " }\n" + "}))"));
}
use of io.confluent.ksql.function.KsqlScalarFunction in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldImplicitlyCastFunctionCallParametersVariadic.
@Test
public void shouldImplicitlyCastFunctionCallParametersVariadic() {
// 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(ParamTypes.DOUBLE, ArrayType.of(ParamTypes.LONG)));
when(udf.isVariadic()).thenReturn(true);
// When:
final String javaExpression = sqlToJavaVisitor.process(new FunctionCall(FunctionName.of("FOO"), ImmutableList.of(new DecimalLiteral(new BigDecimal("1.2")), new IntegerLiteral(1), new IntegerLiteral(1))));
// Then:
final String doubleCast = CastEvaluator.generateCode("new BigDecimal(\"1.2\")", SqlTypes.decimal(2, 1), SqlTypes.DOUBLE, ksqlConfig);
final String longCast = CastEvaluator.generateCode("1", SqlTypes.INTEGER, SqlTypes.BIGINT, ksqlConfig);
assertThat(javaExpression, is("((String) FOO_0.evaluate(" + doubleCast + ", " + longCast + ", " + longCast + "))"));
}
use of io.confluent.ksql.function.KsqlScalarFunction 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.function.KsqlScalarFunction in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldHandleFunctionCalls_intParams.
@Test
public void shouldHandleFunctionCalls_intParams() {
// 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(IntegerType.INSTANCE, IntegerType.INSTANCE));
// 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.KsqlScalarFunction in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldHandleFunctionCalls_intervalParam.
@Test
public void shouldHandleFunctionCalls_intervalParam() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
when(udf.newInstance(any())).thenReturn(new toMillisUdf());
givenUdf("FOO", udfFactory, udf);
when(udf.parameters()).thenReturn(ImmutableList.of(IntervalUnitType.INSTANCE, IntegerType.INSTANCE));
// When:
InterpretedExpression interpreter1 = interpreter(new FunctionCall(FunctionName.of("FOO"), ImmutableList.of(new IntervalUnit(TimeUnit.SECONDS), new IntegerLiteral(1))));
final Object object = interpreter1.evaluate(ROW);
// Then:
assertThat(object, is(1000));
}
Aggregations