Search in sources :

Example 1 with TableAggregationFunction

use of io.confluent.ksql.execution.function.TableAggregationFunction 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 2 with TableAggregationFunction

use of io.confluent.ksql.execution.function.TableAggregationFunction in project ksql by confluentinc.

the class KudafUndoAggregator method apply.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public GenericRow apply(final GenericKey k, final GenericRow rowValue, final GenericRow aggRowValue) {
    final GenericRow result = GenericRow.fromList(aggRowValue.values());
    for (int idx = 0; idx < nonAggColumnCount; idx++) {
        result.set(idx, rowValue.get(idx));
    }
    for (int idx = nonAggColumnCount; idx < columnCount; idx++) {
        final TableAggregationFunction function = aggregateFunctions.get(idx - nonAggColumnCount);
        final Object argument = rowValue.get(function.getArgIndexInValue());
        final Object previous = result.get(idx);
        result.set(idx, function.undo(argument, previous));
    }
    return result;
}
Also used : GenericRow(io.confluent.ksql.GenericRow) TableAggregationFunction(io.confluent.ksql.execution.function.TableAggregationFunction)

Aggregations

TableAggregationFunction (io.confluent.ksql.execution.function.TableAggregationFunction)2 GenericKey (io.confluent.ksql.GenericKey)1 GenericRow (io.confluent.ksql.GenericRow)1 Stacker (io.confluent.ksql.execution.context.QueryContext.Stacker)1 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)1 UdafUtil (io.confluent.ksql.execution.function.UdafUtil)1 ExecutionStep (io.confluent.ksql.execution.plan.ExecutionStep)1 KGroupedTableHolder (io.confluent.ksql.execution.plan.KGroupedTableHolder)1 TableAggregate (io.confluent.ksql.execution.plan.TableAggregate)1 ExecutionStepFactory (io.confluent.ksql.execution.streams.ExecutionStepFactory)1 FunctionRegistry (io.confluent.ksql.function.FunctionRegistry)1 KsqlAggregateFunction (io.confluent.ksql.function.KsqlAggregateFunction)1 ColumnName (io.confluent.ksql.name.ColumnName)1 FunctionName (io.confluent.ksql.name.FunctionName)1 WindowExpression (io.confluent.ksql.parser.tree.WindowExpression)1 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)1 FormatInfo (io.confluent.ksql.serde.FormatInfo)1 InternalFormats (io.confluent.ksql.serde.InternalFormats)1 KeyFormat (io.confluent.ksql.serde.KeyFormat)1 GrammaticalJoiner (io.confluent.ksql.util.GrammaticalJoiner)1