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()));
}
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);
}
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)));
}
Aggregations