Search in sources :

Example 1 with TableAggregate

use of io.confluent.ksql.execution.plan.TableAggregate in project ksql by confluentinc.

the class StepSchemaResolverTest method shouldResolveSchemaForTableAggregate.

@Test
public void shouldResolveSchemaForTableAggregate() {
    // Given:
    givenAggregateFunction("SUM", SqlTypes.BIGINT);
    final TableAggregate step = new TableAggregate(PROPERTIES, groupedTableSource, formats, ImmutableList.of(ColumnName.of("ORANGE")), ImmutableList.of(functionCall("SUM", "APPLE")));
    // When:
    final LogicalSchema result = resolver.resolve(step, SCHEMA);
    // Then:
    assertThat(result, is(LogicalSchema.builder().keyColumn(ColumnName.of("K0"), SqlTypes.INTEGER).valueColumn(ColumnName.of("ORANGE"), SqlTypes.INTEGER).valueColumn(ColumnNames.aggregateColumn(0), SqlTypes.BIGINT).build()));
}
Also used : TableAggregate(io.confluent.ksql.execution.plan.TableAggregate) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Test(org.junit.Test)

Example 2 with TableAggregate

use of io.confluent.ksql.execution.plan.TableAggregate 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 3 with TableAggregate

use of io.confluent.ksql.execution.plan.TableAggregate in project ksql by confluentinc.

the class TableAggregateBuilderTest method init.

@Before
@SuppressWarnings({ "unchecked", "rawtypes" })
public void init() {
    when(buildContext.buildKeySerde(any(), any(), any())).thenReturn(keySerde);
    when(buildContext.buildValueSerde(any(), any(), any())).thenReturn(valueSerde);
    when(buildContext.getFunctionRegistry()).thenReturn(functionRegistry);
    when(buildContext.getKsqlConfig()).thenReturn(KsqlConfig.empty());
    when(aggregateParamsFactory.createUndoable(any(), any(), any(), any(), any())).thenReturn(aggregateParams);
    when(aggregateParams.getAggregator()).thenReturn((KudafAggregator) aggregator);
    when(aggregateParams.getUndoAggregator()).thenReturn(Optional.of(undoAggregator));
    when(aggregateParams.getInitializer()).thenReturn(initializer);
    when(aggregateParams.getAggregateSchema()).thenReturn(AGGREGATE_SCHEMA);
    when(aggregateParams.getSchema()).thenReturn(AGGREGATE_SCHEMA);
    when(aggregator.getResultMapper()).thenReturn(resultMapper);
    when(materializedFactory.<GenericKey, KeyValueStore<Bytes, byte[]>>create(any(), any(), any())).thenReturn(materialized);
    when(groupedTable.aggregate(any(), any(), any(), any(Materialized.class))).thenReturn(aggregated);
    when(aggregated.transformValues(any(), any(Named.class))).thenReturn((KTable) aggregatedWithResults);
    aggregate = new TableAggregate(new ExecutionStepPropertiesV1(CTX), sourceStep, Formats.of(KEY_FORMAT, VALUE_FORMAT, SerdeFeatures.of(), SerdeFeatures.of()), NON_AGG_COLUMNS, FUNCTIONS);
    when(sourceStep.build(any(), eq(planInfo))).thenReturn(KGroupedTableHolder.of(groupedTable, INPUT_SCHEMA));
    planBuilder = new KSPlanBuilder(buildContext, mock(SqlPredicateFactory.class), aggregateParamsFactory, new StreamsFactories(mock(GroupedFactory.class), mock(JoinedFactory.class), materializedFactory, mock(StreamJoinedFactory.class), mock(ConsumedFactory.class)));
}
Also used : Named(org.apache.kafka.streams.kstream.Named) TableAggregate(io.confluent.ksql.execution.plan.TableAggregate) ExecutionStepPropertiesV1(io.confluent.ksql.execution.plan.ExecutionStepPropertiesV1) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) GenericKey(io.confluent.ksql.GenericKey) Materialized(org.apache.kafka.streams.kstream.Materialized) Before(org.junit.Before)

Aggregations

TableAggregate (io.confluent.ksql.execution.plan.TableAggregate)3 GenericKey (io.confluent.ksql.GenericKey)2 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)2 Stacker (io.confluent.ksql.execution.context.QueryContext.Stacker)1 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)1 TableAggregationFunction (io.confluent.ksql.execution.function.TableAggregationFunction)1 UdafUtil (io.confluent.ksql.execution.function.UdafUtil)1 ExecutionStep (io.confluent.ksql.execution.plan.ExecutionStep)1 ExecutionStepPropertiesV1 (io.confluent.ksql.execution.plan.ExecutionStepPropertiesV1)1 KGroupedTableHolder (io.confluent.ksql.execution.plan.KGroupedTableHolder)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 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