Search in sources :

Example 26 with FunctionCall

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

the class StreamFlatMapBuilder method build.

public static <K> KStreamHolder<K> build(final KStreamHolder<K> stream, final StreamFlatMap<K> step, final RuntimeBuildContext buildContext) {
    final List<FunctionCall> tableFunctions = step.getTableFunctions();
    final LogicalSchema schema = stream.getSchema();
    final Builder<TableFunctionApplier> tableFunctionAppliersBuilder = ImmutableList.builder();
    final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, buildContext.getKsqlConfig(), buildContext.getFunctionRegistry());
    for (final FunctionCall functionCall : tableFunctions) {
        final List<CompiledExpression> compiledExpressionList = new ArrayList<>(functionCall.getArguments().size());
        for (final Expression expression : functionCall.getArguments()) {
            final CompiledExpression compiledExpression = codeGenRunner.buildCodeGenFromParseTree(expression, "Table function");
            compiledExpressionList.add(compiledExpression);
        }
        final KsqlTableFunction tableFunction = UdtfUtil.resolveTableFunction(buildContext.getFunctionRegistry(), functionCall, schema);
        final TableFunctionApplier tableFunctionApplier = new TableFunctionApplier(tableFunction, compiledExpressionList);
        tableFunctionAppliersBuilder.add(tableFunctionApplier);
    }
    final QueryContext queryContext = step.getProperties().getQueryContext();
    final ProcessingLogger processingLogger = buildContext.getProcessingLogger(queryContext);
    final ImmutableList<TableFunctionApplier> tableFunctionAppliers = tableFunctionAppliersBuilder.build();
    final KStream<K, GenericRow> mapped = stream.getStream().flatTransformValues(() -> new KsTransformer<>(new KudtfFlatMapper<>(tableFunctionAppliers, processingLogger)), Named.as(StreamsUtil.buildOpName(queryContext)));
    return stream.withStream(mapped, buildSchema(stream.getSchema(), step.getTableFunctions(), buildContext.getFunctionRegistry()));
}
Also used : ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) ArrayList(java.util.ArrayList) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) QueryContext(io.confluent.ksql.execution.context.QueryContext) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) GenericRow(io.confluent.ksql.GenericRow) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) KsqlTableFunction(io.confluent.ksql.function.KsqlTableFunction) TableFunctionApplier(io.confluent.ksql.execution.function.udtf.TableFunctionApplier) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) KudtfFlatMapper(io.confluent.ksql.execution.function.udtf.KudtfFlatMapper)

Example 27 with FunctionCall

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForNestedLambdas.

@Test
public void shouldGenerateCorrectCodeForNestedLambdas() {
    // Given:
    final UdfFactory udfFactory = mock(UdfFactory.class);
    final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
    givenUdf("nested", udfFactory, udf, SqlTypes.DOUBLE);
    when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), ParamTypes.DOUBLE, LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.INTEGER), ParamTypes.INTEGER)));
    final Expression expression = new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("nested"), ImmutableList.of(ARRAYCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("A", "B"), new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("nested"), ImmutableList.of(ARRAYCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("Q", "V"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("Q"), new LambdaVariable("V"))))), new LambdaVariable("B"))))), new IntegerLiteral(5));
    // When:
    final String javaExpression = sqlToJavaVisitor.process(expression);
    // Then
    assertThat(javaExpression, equalTo("(((Double) nested_0.evaluate(COL4, (Double)NullSafe.apply(0,new Function() {\n" + " @Override\n" + " public Object apply(Object arg1) {\n" + "   final Integer val = (Integer) arg1;\n" + "   return val.doubleValue();\n" + " }\n" + "}), new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + "   final Double A = (Double) arg1;\n" + "   final Integer B = (Integer) arg2;\n" + "   return (((Double) nested_1.evaluate(COL4, (Double)NullSafe.apply(0,new Function() {\n" + " @Override\n" + " public Object apply(Object arg1) {\n" + "   final Integer val = (Integer) arg1;\n" + "   return val.doubleValue();\n" + " }\n" + "}), new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + "   final Double Q = (Double) arg1;\n" + "   final Integer V = (Integer) arg2;\n" + "   return (Q + V);\n" + " }\n" + "})) + B);\n" + " }\n" + "})) + 5)"));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) 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) 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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 28 with FunctionCall

use of io.confluent.ksql.execution.expression.tree.FunctionCall 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" + "}))"));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) 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) 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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 29 with FunctionCall

use of io.confluent.ksql.execution.expression.tree.FunctionCall 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 + "))"));
}
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)

Example 30 with FunctionCall

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

the class FunctionArgumentsUtilTest method shouldResolveLambdaWithoutGenerics.

@Test
public void shouldResolveLambdaWithoutGenerics() {
    // Given:
    givenUdfWithNameAndReturnType("SmallLambda", SqlTypes.DOUBLE);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), LambdaType.of(ImmutableList.of(ParamTypes.INTEGER), ParamTypes.INTEGER)));
    final FunctionCall expression = new FunctionCall(FunctionName.of("SmallLambda"), ImmutableList.of(ARRAYCOL, new LambdaFunctionCall(ImmutableList.of("2.3"), new ArithmeticBinaryExpression(Operator.ADD, new DoubleLiteral(2.3), new DoubleLiteral(2.3)))));
    // When:
    final FunctionTypeInfo argumentsAndContexts = FunctionArgumentsUtil.getFunctionTypeInfo(expressionTypeManager, expression, udfFactory, Collections.emptyMap());
    // Then:
    assertThat(argumentsAndContexts.getReturnType(), is(SqlTypes.DOUBLE));
    assertThat(argumentsAndContexts.getArgumentInfos().size(), is(2));
    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.INTEGER), SqlTypes.DOUBLE))));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) 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