use of io.crate.operation.collect.CollectExpression in project crate by crate.
the class GroupingIntegerCollectorBenchmark method createGroupBySumCollector.
private GroupingCollector createGroupBySumCollector(Functions functions) {
InputCollectExpression keyInput = new InputCollectExpression(0);
List<Input<?>> keyInputs = Arrays.<Input<?>>asList(keyInput);
CollectExpression[] collectExpressions = new CollectExpression[] { keyInput };
FunctionIdent functionIdent = new FunctionIdent(SumAggregation.NAME, Arrays.asList(DataTypes.INTEGER));
FunctionInfo functionInfo = new FunctionInfo(functionIdent, DataTypes.INTEGER, FunctionInfo.Type.AGGREGATE);
AggregationFunction sumAgg = (AggregationFunction) functions.get(functionIdent);
Aggregation aggregation = Aggregation.finalAggregation(functionInfo, Arrays.asList(new InputColumn(0)), Aggregation.Step.ITER);
Aggregator[] aggregators = new Aggregator[] { new Aggregator(RAM_ACCOUNTING_CONTEXT, aggregation, sumAgg, new Input[] { keyInput }) };
return GroupingCollector.singleKey(collectExpressions, aggregators, RAM_ACCOUNTING_CONTEXT, keyInputs.get(0), DataTypes.INTEGER);
}
use of io.crate.operation.collect.CollectExpression in project crate by crate.
the class IndexWriterProjectorUnitTest method testNullPKValue.
@Test
public void testNullPKValue() throws Throwable {
InputCollectExpression sourceInput = new InputCollectExpression(0);
List<CollectExpression<Row, ?>> collectExpressions = Collections.<CollectExpression<Row, ?>>singletonList(sourceInput);
final IndexWriterProjector indexWriter = new IndexWriterProjector(clusterService, TestingHelpers.getFunctions(), new IndexNameExpressionResolver(Settings.EMPTY), Settings.EMPTY, mock(TransportBulkCreateIndicesAction.class), mock(BulkRequestExecutor.class), () -> "foo", mock(BulkRetryCoordinatorPool.class, Answers.RETURNS_DEEP_STUBS.get()), rawSourceReference, ImmutableList.of(ID_IDENT), Arrays.<Symbol>asList(new InputColumn(1)), null, null, sourceInput, collectExpressions, 20, null, null, false, false, UUID.randomUUID());
RowN rowN = new RowN(new Object[] { new BytesRef("{\"y\": \"x\"}"), null });
BatchIterator batchIterator = RowsBatchIterator.newInstance(Collections.singletonList(rowN), rowN.numColumns());
batchIterator = indexWriter.apply(batchIterator);
TestingBatchConsumer testingBatchConsumer = new TestingBatchConsumer();
testingBatchConsumer.accept(batchIterator, null);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("A primary key value must not be NULL");
testingBatchConsumer.getResult();
}
use of io.crate.operation.collect.CollectExpression in project crate by crate.
the class InputFactoryTest method testProcessGroupByProjectionSymbols.
@Test
public void testProcessGroupByProjectionSymbols() throws Exception {
// select x, y * 2 ... group by x, y * 2
// keys: [ in(0), in(1) + 10 ]
Function add = AddFunction.of(new InputColumn(1, DataTypes.INTEGER), Literal.of(10));
List<Symbol> keys = Arrays.asList(new InputColumn(0, DataTypes.LONG), add);
InputFactory.Context<CollectExpression<Row, ?>> ctx = factory.ctxForAggregations();
ctx.add(keys);
ArrayList<CollectExpression<Row, ?>> expressions = new ArrayList<>(ctx.expressions());
assertThat(expressions.size(), is(2));
// keyExpressions: [ in0, in1 ]
RowN row = new RowN(new Object[] { 1L, 2L });
for (CollectExpression<Row, ?> expression : expressions) {
expression.setNextRow(row);
}
assertThat((Long) expressions.get(0).value(), is(1L));
// raw input value
assertThat((Long) expressions.get(1).value(), is(2L));
// inputs: [ x, add ]
List<Input<?>> inputs = ctx.topLevelInputs();
assertThat(inputs.size(), is(2));
assertThat((Long) inputs.get(0).value(), is(1L));
// + 10
assertThat((Long) inputs.get(1).value(), is(12L));
}
use of io.crate.operation.collect.CollectExpression 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 ]
Function add = AddFunction.of(new InputColumn(1, DataTypes.INTEGER), Literal.of(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 = Arrays.asList(Aggregation.partialAggregation(countX.info(), countX.valueType(), Arrays.<Symbol>asList(new InputColumn(0))));
InputFactory.Context<CollectExpression<Row, ?>> ctx = factory.ctxForAggregations();
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(new Object[] { 1L, 2L });
for (CollectExpression<Row, ?> expression : expressions) {
expression.setNextRow(row);
}
assertThat((Long) expressions.get(0).value(), is(1L));
// raw input value
assertThat((Long) expressions.get(1).value(), is(2L));
assertThat(keyInputs.size(), is(2));
assertThat((Long) keyInputs.get(0).value(), is(1L));
// 2 + 10
assertThat((Long) keyInputs.get(1).value(), is(12L));
}
use of io.crate.operation.collect.CollectExpression 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.<Symbol>asList(Aggregation.finalAggregation(countX.info(), Arrays.<Symbol>asList(new InputColumn(0)), Aggregation.Step.ITER), Aggregation.finalAggregation(avgX.info(), Arrays.<Symbol>asList(new InputColumn(0)), Aggregation.Step.ITER));
InputFactory.Context<CollectExpression<Row, ?>> ctx = factory.ctxForAggregations();
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