Search in sources :

Example 41 with FunctionCall

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

the class AstSanitizerTest method shouldAllowNestedLambdaFunctionsWithoutDuplicate.

@Test
public void shouldAllowNestedLambdaFunctionsWithoutDuplicate() {
    // Given:
    final Statement stmt = givenQuery("SELECT TRANSFORM_ARRAY(Col4, (X,Y,Z) => TRANSFORM_MAP(Col4, Q => 4, H => 5), (X,Y,Z) => 0) FROM TEST1;");
    // When:
    final Query result = (Query) AstSanitizer.sanitize(stmt, META_STORE, true);
    // Then:
    assertThat(result.getSelect(), is(new Select(ImmutableList.of(new SingleColumn(new FunctionCall(FunctionName.of("TRANSFORM_ARRAY"), ImmutableList.of(column(TEST1_NAME, "COL4"), new LambdaFunctionCall(ImmutableList.of("X", "Y", "Z"), new FunctionCall(FunctionName.of("TRANSFORM_MAP"), ImmutableList.of(column(TEST1_NAME, "COL4"), new LambdaFunctionCall(ImmutableList.of("Q"), new IntegerLiteral(4)), new LambdaFunctionCall(ImmutableList.of("H"), new IntegerLiteral(5))))), new LambdaFunctionCall(ImmutableList.of("X", "Y", "Z"), new IntegerLiteral(0)))), Optional.of(ColumnName.of("KSQL_COL_0")))))));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) Statement(io.confluent.ksql.parser.tree.Statement) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) Select(io.confluent.ksql.parser.tree.Select) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) 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 42 with FunctionCall

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

the class SchemaKGroupedTable method aggregate.

@Override
public SchemaKTable<GenericKey> aggregate(final List<ColumnName> nonAggregateColumns, final List<FunctionCall> aggregations, final Optional<WindowExpression> windowExpression, final FormatInfo valueFormat, final Stacker contextStacker) {
    if (windowExpression.isPresent()) {
        throw new KsqlException("Windowing not supported for table aggregations.");
    }
    final List<String> unsupportedFunctionNames = aggregations.stream().map(call -> UdafUtil.resolveAggregateFunction(functionRegistry, call, schema, ksqlConfig)).filter(function -> !(function instanceof TableAggregationFunction)).map(KsqlAggregateFunction::name).map(FunctionName::text).distinct().collect(Collectors.toList());
    if (!unsupportedFunctionNames.isEmpty()) {
        final String postfix = unsupportedFunctionNames.size() == 1 ? "" : "s";
        throw new KsqlException("The aggregation function" + postfix + " " + GrammaticalJoiner.and().join(unsupportedFunctionNames) + " cannot be applied to a table source, only to a stream source.");
    }
    final TableAggregate step = ExecutionStepFactory.tableAggregate(contextStacker, sourceTableStep, InternalFormats.of(keyFormat, valueFormat), nonAggregateColumns, aggregations);
    return new SchemaKTable<>(step, resolveSchema(step), keyFormat, ksqlConfig, functionRegistry);
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) GrammaticalJoiner(io.confluent.ksql.util.GrammaticalJoiner) KeyFormat(io.confluent.ksql.serde.KeyFormat) KGroupedTableHolder(io.confluent.ksql.execution.plan.KGroupedTableHolder) KsqlAggregateFunction(io.confluent.ksql.function.KsqlAggregateFunction) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) ExecutionStepFactory(io.confluent.ksql.execution.streams.ExecutionStepFactory) ExecutionStep(io.confluent.ksql.execution.plan.ExecutionStep) FunctionName(io.confluent.ksql.name.FunctionName) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) TableAggregate(io.confluent.ksql.execution.plan.TableAggregate) KsqlConfig(io.confluent.ksql.util.KsqlConfig) InternalFormats(io.confluent.ksql.serde.InternalFormats) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) UdafUtil(io.confluent.ksql.execution.function.UdafUtil) Objects(java.util.Objects) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) List(java.util.List) Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker) TableAggregationFunction(io.confluent.ksql.execution.function.TableAggregationFunction) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) GenericKey(io.confluent.ksql.GenericKey) FormatInfo(io.confluent.ksql.serde.FormatInfo) FunctionName(io.confluent.ksql.name.FunctionName) TableAggregate(io.confluent.ksql.execution.plan.TableAggregate) TableAggregationFunction(io.confluent.ksql.execution.function.TableAggregationFunction) KsqlException(io.confluent.ksql.util.KsqlException)

Example 43 with FunctionCall

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

the class AggregateAnalyzerTest method shouldNotThrowOnNonAggregateFunctionIfAllParamsAreInGroupBy.

@Test
public void shouldNotThrowOnNonAggregateFunctionIfAllParamsAreInGroupBy() {
    // Given:
    final Expression someExpression = mock(Expression.class);
    givenSelectExpression(new FunctionCall(FunctionName.of("UCASE"), ImmutableList.of(GROUP_BY_1, someExpression)));
    // When:
    analyzer.analyze(analysis, selects);
// Then: did not throw.
}
Also used : WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) Test(org.junit.Test)

Example 44 with FunctionCall

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

the class AggregateAnalyzerTest method shouldThrowOnNonExistentFunctionCall.

@Test
public void shouldThrowOnNonExistentFunctionCall() {
    // Given:
    givenSelectExpression(new FunctionCall(FunctionName.of("NOT_FOUND"), ImmutableList.of(COL2)));
    // When:
    final KsqlException e = assertThrows(KsqlException.class, () -> analyzer.analyze(analysis, selects));
    // Then:
    assertThat(e.getMessage(), containsString("Can't find any functions with the name 'NOT_FOUND'"));
}
Also used : FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 45 with FunctionCall

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

the class AggregateAnalyzerTest method shouldAddDefaultArgToFunctionCallWithNoArgs.

@Test
public void shouldAddDefaultArgToFunctionCallWithNoArgs() {
    // Given:
    final FunctionCall emptyFunc = new FunctionCall(FunctionName.of("COUNT"), new ArrayList<>());
    givenSelectExpression(emptyFunc);
    // When:
    final AggregateAnalysisResult result = analyzer.analyze(analysis, selects);
    // Then:
    assertThat(result.getAggregateFunctions(), hasSize(2));
    assertThat(result.getAggregateFunctions().get(1).getName(), is(emptyFunc.getName()));
    assertThat(result.getAggregateFunctions().get(1).getArguments(), contains(DEFAULT_ARGUMENT));
}
Also used : FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) 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