use of io.crate.expression.symbol.Aggregation in project crate by crate.
the class GroupProjectionTest method testStreaming2.
@Test
public void testStreaming2() throws Exception {
List<Symbol> keys = Collections.singletonList(new InputColumn(0, DataTypes.STRING));
List<Aggregation> aggregations = Collections.singletonList(new Aggregation(CountAggregation.COUNT_STAR_SIGNATURE, CountAggregation.COUNT_STAR_SIGNATURE.getReturnType().createType(), Collections.emptyList()));
GroupProjection groupProjection = new GroupProjection(keys, aggregations, AggregateMode.ITER_FINAL, RowGranularity.CLUSTER);
BytesStreamOutput out = new BytesStreamOutput();
Projection.toStream(groupProjection, out);
StreamInput in = out.bytes().streamInput();
GroupProjection p2 = (GroupProjection) Projection.fromStream(in);
assertThat(p2.keys().size(), is(1));
assertThat(p2.values().size(), is(1));
}
use of io.crate.expression.symbol.Aggregation in project crate by crate.
the class GroupProjectionTest method testStreamingGranularity.
@Test
public void testStreamingGranularity() throws Exception {
List<Symbol> keys = List.of(new InputColumn(0, DataTypes.STRING), new InputColumn(1, DataTypes.SHORT));
List<Aggregation> aggregations = List.of();
GroupProjection p = new GroupProjection(keys, aggregations, AggregateMode.ITER_FINAL, RowGranularity.SHARD);
BytesStreamOutput out = new BytesStreamOutput();
Projection.toStream(p, out);
StreamInput in = out.bytes().streamInput();
GroupProjection p2 = (GroupProjection) Projection.fromStream(in);
assertEquals(p, p2);
}
use of io.crate.expression.symbol.Aggregation in project crate by crate.
the class ProjectionToProjectorVisitorTest method testAggregationProjector.
@Test
public void testAggregationProjector() throws Exception {
AggregationProjection projection = new AggregationProjection(Arrays.asList(new Aggregation(avgSignature, avgSignature.getReturnType().createType(), Collections.singletonList(new InputColumn(1))), new Aggregation(CountAggregation.SIGNATURE, CountAggregation.SIGNATURE.getReturnType().createType(), Collections.singletonList(new InputColumn(0)))), RowGranularity.SHARD, AggregateMode.ITER_FINAL);
Projector projector = visitor.create(projection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
assertThat(projector, instanceOf(AggregationPipe.class));
BatchIterator<Row> batchIterator = projector.apply(InMemoryBatchIterator.of(new CollectionBucket(Arrays.asList($("foo", 10), $("bar", 20))), SENTINEL, true));
TestingRowConsumer consumer = new TestingRowConsumer();
consumer.accept(batchIterator, null);
Bucket rows = consumer.getBucket();
assertThat(rows.size(), is(1));
assertThat(rows, contains(isRow(15.0, 2L)));
}
use of io.crate.expression.symbol.Aggregation in project crate by crate.
the class InputFactoryTest method testProcessGroupByProjectionSymbolsAggregation.
@Test
public void testProcessGroupByProjectionSymbolsAggregation() throws Exception {
// select count(x), x, y * 2 ... group by x, y * 2
// keys: [ in(0), in(1) + 10 ]
List<Symbol> keys = Arrays.asList(new InputColumn(0, DataTypes.LONG), add);
Function countX = (Function) expressions.asSymbol("count(x)");
// values: [ count(in(0)) ]
List<Aggregation> values = List.of(new Aggregation(countX.signature(), countX.valueType(), List.of(new InputColumn(0))));
InputFactory.Context<CollectExpression<Row, ?>> ctx = factory.ctxForAggregations(txnCtx);
ctx.add(keys);
// inputs: [ x, add ]
List<Input<?>> keyInputs = ctx.topLevelInputs();
ctx.add(values);
List<AggregationContext> aggregations = ctx.aggregations();
assertThat(aggregations.size(), is(1));
// collectExpressions: [ in0, in1 ]
List<CollectExpression<Row, ?>> expressions = new ArrayList<>(ctx.expressions());
assertThat(expressions.size(), is(2));
List<Input<?>> allInputs = ctx.topLevelInputs();
// only 2 because count is no input
assertThat(allInputs.size(), is(2));
RowN row = new RowN(1L, 2L);
for (CollectExpression<Row, ?> expression : expressions) {
expression.setNextRow(row);
}
assertThat(expressions.get(0).value(), is(1L));
// raw input value
assertThat(expressions.get(1).value(), is(2L));
assertThat(keyInputs.size(), is(2));
assertThat(keyInputs.get(0).value(), is(1L));
// 2 + 10
assertThat(keyInputs.get(1).value(), is(12));
}
use of io.crate.expression.symbol.Aggregation in project crate by crate.
the class InputFactoryTest method testAggregationSymbolsInputReuse.
@Test
public void testAggregationSymbolsInputReuse() throws Exception {
Function countX = (Function) expressions.asSymbol("count(x)");
Function avgX = (Function) expressions.asSymbol("avg(x)");
List<Symbol> aggregations = Arrays.asList(new Aggregation(countX.signature(), countX.signature().getReturnType().createType(), List.of(new InputColumn(0))), new Aggregation(avgX.signature(), avgX.signature().getReturnType().createType(), List.of(new InputColumn(0))));
InputFactory.Context<CollectExpression<Row, ?>> ctx = factory.ctxForAggregations(txnCtx);
ctx.add(aggregations);
List<AggregationContext> aggregationContexts = ctx.aggregations();
Input<?> inputCount = aggregationContexts.get(0).inputs()[0];
Input<?> inputAverage = aggregationContexts.get(1).inputs()[0];
assertSame(inputCount, inputAverage);
}
Aggregations