Search in sources :

Example 1 with KsqlScalarFunction

use of io.confluent.ksql.function.KsqlScalarFunction 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));
}
Also used : KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) UdfFactory(io.confluent.ksql.function.UdfFactory) 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 2 with KsqlScalarFunction

use of io.confluent.ksql.function.KsqlScalarFunction 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)));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) UdfFactory(io.confluent.ksql.function.UdfFactory) 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 3 with KsqlScalarFunction

use of io.confluent.ksql.function.KsqlScalarFunction 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))"));
}
Also used : KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) UdfFactory(io.confluent.ksql.function.UdfFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) 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 4 with KsqlScalarFunction

use of io.confluent.ksql.function.KsqlScalarFunction 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" + "}))"));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) UdfFactory(io.confluent.ksql.function.UdfFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) 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) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) 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) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 5 with KsqlScalarFunction

use of io.confluent.ksql.function.KsqlScalarFunction in project ksql by confluentinc.

the class SqlToJavaVisitorTest method shouldImplicitlyCastFunctionCallParameters.

@Test
public void shouldImplicitlyCastFunctionCallParameters() {
    // 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, ParamTypes.LONG));
    // When:
    final String javaExpression = sqlToJavaVisitor.process(new FunctionCall(FunctionName.of("FOO"), ImmutableList.of(new DecimalLiteral(new BigDecimal("1.2")), 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 + "))"));
}
Also used : KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) UdfFactory(io.confluent.ksql.function.UdfFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) BigDecimal(java.math.BigDecimal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Aggregations

KsqlScalarFunction (io.confluent.ksql.function.KsqlScalarFunction)15 LambdaFunctionCall (io.confluent.ksql.execution.expression.tree.LambdaFunctionCall)14 UdfFactory (io.confluent.ksql.function.UdfFactory)14 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)13 Test (org.junit.Test)13 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)10 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)8 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)8 Expression (io.confluent.ksql.execution.expression.tree.Expression)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)7 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)7 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)7 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)7 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)7 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)7 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)7 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)6 LambdaVariable (io.confluent.ksql.execution.expression.tree.LambdaVariable)5 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)3