Search in sources :

Example 1 with CollectExpression

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);
}
Also used : AggregationFunction(io.crate.operation.aggregation.AggregationFunction) Aggregation(io.crate.analyze.symbol.Aggregation) SumAggregation(io.crate.operation.aggregation.impl.SumAggregation) FunctionIdent(io.crate.metadata.FunctionIdent) InputCollectExpression(io.crate.operation.collect.InputCollectExpression) InputColumn(io.crate.analyze.symbol.InputColumn) FunctionInfo(io.crate.metadata.FunctionInfo) Aggregator(io.crate.operation.aggregation.Aggregator) CollectExpression(io.crate.operation.collect.CollectExpression) InputCollectExpression(io.crate.operation.collect.InputCollectExpression)

Example 2 with CollectExpression

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();
}
Also used : BatchIterator(io.crate.data.BatchIterator) RowsBatchIterator(io.crate.data.RowsBatchIterator) CollectExpression(io.crate.operation.collect.CollectExpression) InputCollectExpression(io.crate.operation.collect.InputCollectExpression) BulkRetryCoordinatorPool(org.elasticsearch.action.bulk.BulkRetryCoordinatorPool) RowN(io.crate.data.RowN) InputCollectExpression(io.crate.operation.collect.InputCollectExpression) BulkRequestExecutor(org.elasticsearch.action.bulk.BulkRequestExecutor) InputColumn(io.crate.analyze.symbol.InputColumn) TestingBatchConsumer(io.crate.testing.TestingBatchConsumer) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) TransportBulkCreateIndicesAction(org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with CollectExpression

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));
}
Also used : ArrayList(java.util.ArrayList) CollectExpression(io.crate.operation.collect.CollectExpression) AddFunction(io.crate.operation.scalar.arithmetic.AddFunction) RowN(io.crate.data.RowN) Input(io.crate.data.Input) Row(io.crate.data.Row) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 4 with CollectExpression

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));
}
Also used : ArrayList(java.util.ArrayList) CollectExpression(io.crate.operation.collect.CollectExpression) AddFunction(io.crate.operation.scalar.arithmetic.AddFunction) Input(io.crate.data.Input) RowN(io.crate.data.RowN) Row(io.crate.data.Row) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 5 with CollectExpression

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);
}
Also used : AddFunction(io.crate.operation.scalar.arithmetic.AddFunction) CollectExpression(io.crate.operation.collect.CollectExpression) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

CollectExpression (io.crate.operation.collect.CollectExpression)13 InputColumn (io.crate.analyze.symbol.InputColumn)6 Input (io.crate.data.Input)6 Test (org.junit.Test)6 InputFactory (io.crate.operation.InputFactory)5 InputCollectExpression (io.crate.operation.collect.InputCollectExpression)5 Symbol (io.crate.analyze.symbol.Symbol)4 CrateUnitTest (io.crate.test.integration.CrateUnitTest)4 Row (io.crate.data.Row)3 RowN (io.crate.data.RowN)3 AddFunction (io.crate.operation.scalar.arithmetic.AddFunction)3 BytesRef (org.apache.lucene.util.BytesRef)3 TransportBulkCreateIndicesAction (org.elasticsearch.action.admin.indices.create.TransportBulkCreateIndicesAction)3 BulkRetryCoordinatorPool (org.elasticsearch.action.bulk.BulkRetryCoordinatorPool)3 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)3 Aggregation (io.crate.analyze.symbol.Aggregation)2 BatchIterator (io.crate.data.BatchIterator)2 RowsBatchIterator (io.crate.data.RowsBatchIterator)2 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)2 FunctionIdent (io.crate.metadata.FunctionIdent)2