use of io.confluent.ksql.execution.expression.tree.FunctionCall in project ksql by confluentinc.
the class AggregateExpressionRewriter method visitFunctionCall.
@Override
public Optional<Expression> visitFunctionCall(final FunctionCall node, final ExpressionTreeRewriter.Context<Void> context) {
final FunctionName functionName = node.getName();
if (functionRegistry.isAggregate(functionName)) {
final ColumnName aggVarName = ColumnNames.aggregateColumn(aggVariableIndex);
aggVariableIndex++;
return Optional.of(new UnqualifiedColumnReferenceExp(node.getLocation(), aggVarName));
} else {
final List<Expression> arguments = new ArrayList<>();
for (final Expression argExpression : node.getArguments()) {
arguments.add(context.process(argExpression));
}
return Optional.of(new FunctionCall(node.getLocation(), node.getName(), arguments));
}
}
use of io.confluent.ksql.execution.expression.tree.FunctionCall 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.execution.expression.tree.FunctionCall 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.execution.expression.tree.FunctionCall in project ksql by confluentinc.
the class ExpressionFormatterTest method shouldFormatFunctionCountStar.
@Test
public void shouldFormatFunctionCountStar() {
final FunctionCall functionCall = new FunctionCall(FunctionName.of("COUNT"), Collections.emptyList());
assertThat(ExpressionFormatter.formatExpression(functionCall), equalTo("COUNT(*)"));
}
use of io.confluent.ksql.execution.expression.tree.FunctionCall in project ksql by confluentinc.
the class AstBuilderTest method shouldBuildLambdaFunctionWithMultipleLambdas.
@Test
public void shouldBuildLambdaFunctionWithMultipleLambdas() {
// Given:
final SingleStatementContext stmt = givenQuery("SELECT TRANSFORM_ARRAY(Col4, X => X + 5, (X,Y) => X + Y) FROM TEST1;");
// When:
final Query result = (Query) builder.buildStatement(stmt);
// Then:
assertThat(result.getSelect(), is(new Select(ImmutableList.of(new SingleColumn(new FunctionCall(FunctionName.of("TRANSFORM_ARRAY"), ImmutableList.of(column("COL4"), new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new IntegerLiteral(5))), new LambdaFunctionCall(ImmutableList.of("X", "Y"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new LambdaVariable("Y"))))), Optional.empty())))));
}
Aggregations