Search in sources :

Example 6 with Aggregation

use of io.crate.expression.symbol.Aggregation in project crate by crate.

the class GroupProjectionTest method testStreaming.

@Test
public void testStreaming() 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.CLUSTER);
    BytesStreamOutput out = new BytesStreamOutput();
    Projection.toStream(p, out);
    StreamInput in = out.bytes().streamInput();
    GroupProjection p2 = (GroupProjection) Projection.fromStream(in);
    assertEquals(p, p2);
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) CountAggregation(io.crate.execution.engine.aggregation.impl.CountAggregation) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 7 with Aggregation

use of io.crate.expression.symbol.Aggregation in project crate by crate.

the class SymbolPrinterTest method testFormatAggregation.

@Test
public void testFormatAggregation() throws Exception {
    Signature signature = Signature.aggregate("agg", DataTypes.INTEGER.getTypeSignature(), DataTypes.LONG.getTypeSignature());
    Aggregation a = new Aggregation(signature, DataTypes.LONG, Collections.singletonList(Literal.of(-127)));
    assertPrint(a, "agg(-127)");
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) Signature(io.crate.metadata.functions.Signature) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 8 with Aggregation

use of io.crate.expression.symbol.Aggregation in project crate by crate.

the class GroupByPlannerTest method testCountDistinctWithGroupBy.

@Test
public void testCountDistinctWithGroupBy() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge distributedGroupByMerge = e.plan("select count(distinct id), name from users group by name order by count(distinct id)");
    Merge reducerMerge = (Merge) distributedGroupByMerge.subPlan();
    CollectPhase collectPhase = ((Collect) reducerMerge.subPlan()).collectPhase();
    // collect
    assertThat(collectPhase.toCollect().get(0), instanceOf(Reference.class));
    assertThat(collectPhase.toCollect().size(), is(2));
    assertThat(((Reference) collectPhase.toCollect().get(0)).column().name(), is("id"));
    assertThat(((Reference) collectPhase.toCollect().get(1)).column().name(), is("name"));
    Projection projection = collectPhase.projections().get(0);
    assertThat(projection, instanceOf(GroupProjection.class));
    GroupProjection groupProjection = (GroupProjection) projection;
    Symbol groupKey = groupProjection.keys().get(0);
    assertThat(groupKey, instanceOf(InputColumn.class));
    assertThat(((InputColumn) groupKey).index(), is(1));
    assertThat(groupProjection.values().size(), is(1));
    assertThat(groupProjection.mode(), is(AggregateMode.ITER_PARTIAL));
    Aggregation aggregation = groupProjection.values().get(0);
    Symbol aggregationInput = aggregation.inputs().get(0);
    assertThat(aggregationInput.symbolType(), is(SymbolType.INPUT_COLUMN));
    // reducer
    MergePhase mergePhase = reducerMerge.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(OrderedTopNProjection.class), instanceOf(EvalProjection.class)));
    Projection groupProjection1 = mergePhase.projections().get(0);
    groupProjection = (GroupProjection) groupProjection1;
    assertThat(groupProjection.keys().get(0), instanceOf(InputColumn.class));
    assertThat(((InputColumn) groupProjection.keys().get(0)).index(), is(0));
    assertThat(groupProjection.mode(), is(AggregateMode.PARTIAL_FINAL));
    assertThat(groupProjection.values().get(0), instanceOf(Aggregation.class));
    OrderedTopNProjection topNProjection = (OrderedTopNProjection) mergePhase.projections().get(1);
    Symbol collection_count = topNProjection.outputs().get(0);
    assertThat(collection_count, SymbolMatchers.isInputColumn(0));
    // handler
    MergePhase localMergeNode = distributedGroupByMerge.mergePhase();
    assertThat(localMergeNode.projections(), Matchers.emptyIterable());
}
Also used : SymbolMatchers.isAggregation(io.crate.testing.SymbolMatchers.isAggregation) Aggregation(io.crate.expression.symbol.Aggregation) CountAggregation(io.crate.execution.engine.aggregation.impl.CountAggregation) MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) Collect(io.crate.planner.node.dql.Collect) Reference(io.crate.metadata.Reference) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 9 with Aggregation

use of io.crate.expression.symbol.Aggregation in project crate by crate.

the class MergeNodeTest method testSerialization.

@Test
public void testSerialization() 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.PARTIAL_FINAL, RowGranularity.CLUSTER);
    TopNProjection topNProjection = new TopNProjection(10, 0, Symbols.typeView(groupProjection.outputs()));
    List<Projection> projections = Arrays.asList(groupProjection, topNProjection);
    MergePhase node = new MergePhase(UUID.randomUUID(), 0, "merge", 2, 1, Set.of("node1", "node2"), List.of(DataTypes.UNDEFINED, DataTypes.STRING), projections, DistributionInfo.DEFAULT_BROADCAST, null);
    BytesStreamOutput output = new BytesStreamOutput();
    node.writeTo(output);
    StreamInput input = output.bytes().streamInput();
    MergePhase node2 = new MergePhase(input);
    assertThat(node.numUpstreams(), is(node2.numUpstreams()));
    assertThat(node.nodeIds(), is(node2.nodeIds()));
    assertThat(node.jobId(), is(node2.jobId()));
    assertEquals(node.inputTypes(), node2.inputTypes());
    assertThat(node.phaseId(), is(node2.phaseId()));
    assertThat(node.distributionInfo(), is(node2.distributionInfo()));
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) CountAggregation(io.crate.execution.engine.aggregation.impl.CountAggregation) MergePhase(io.crate.execution.dsl.phases.MergePhase) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) StreamInput(org.elasticsearch.common.io.stream.StreamInput) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 10 with Aggregation

use of io.crate.expression.symbol.Aggregation in project crate by crate.

the class ProjectionToProjectorVisitorTest method testGroupProjector.

@Test
public void testGroupProjector() throws Exception {
    // in(0)  in(1)      in(0),      in(2)
    // select  race, avg(age), count(race), gender  ... group by race, gender
    List<Symbol> keys = Arrays.asList(new InputColumn(0, DataTypes.STRING), new InputColumn(2, DataTypes.STRING));
    List<Aggregation> aggregations = 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))));
    GroupProjection projection = new GroupProjection(keys, aggregations, AggregateMode.ITER_FINAL, RowGranularity.CLUSTER);
    Projector projector = visitor.create(projection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
    assertThat(projector, instanceOf(GroupingProjector.class));
    // use a topN projection in order to get sorted outputs
    List<Symbol> outputs = Arrays.asList(new InputColumn(0, DataTypes.STRING), new InputColumn(1, DataTypes.STRING), new InputColumn(2, DataTypes.DOUBLE), new InputColumn(3, DataTypes.LONG));
    OrderedTopNProjection topNProjection = new OrderedTopNProjection(10, 0, outputs, List.of(new InputColumn(2, DataTypes.DOUBLE)), new boolean[] { false }, new boolean[] { false });
    Projector topNProjector = visitor.create(topNProjection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
    String human = "human";
    String vogon = "vogon";
    String male = "male";
    String female = "female";
    List<Object[]> rows = new ArrayList<>();
    rows.add($(human, 34, male));
    rows.add($(human, 22, female));
    rows.add($(vogon, 40, male));
    rows.add($(vogon, 48, male));
    rows.add($(human, 34, male));
    BatchIterator<Row> batchIterator = topNProjector.apply(projector.apply(InMemoryBatchIterator.of(new CollectionBucket(rows), SENTINEL, true)));
    TestingRowConsumer consumer = new TestingRowConsumer();
    consumer.accept(batchIterator, null);
    Bucket bucket = consumer.getBucket();
    assertThat(bucket, contains(isRow(human, female, 22.0, 1L), isRow(human, male, 34.0, 2L), isRow(vogon, male, 44.0, 2L)));
}
Also used : Projector(io.crate.data.Projector) SortingProjector(io.crate.execution.engine.sort.SortingProjector) SortingTopNProjector(io.crate.execution.engine.sort.SortingTopNProjector) GroupingProjector(io.crate.execution.engine.aggregation.GroupingProjector) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) Aggregation(io.crate.expression.symbol.Aggregation) CountAggregation(io.crate.execution.engine.aggregation.impl.CountAggregation) Bucket(io.crate.data.Bucket) CollectionBucket(io.crate.data.CollectionBucket) InputColumn(io.crate.expression.symbol.InputColumn) GroupingProjector(io.crate.execution.engine.aggregation.GroupingProjector) Row(io.crate.data.Row) TestingHelpers.isRow(io.crate.testing.TestingHelpers.isRow) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) CollectionBucket(io.crate.data.CollectionBucket) TestingRowConsumer(io.crate.testing.TestingRowConsumer) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

Aggregation (io.crate.expression.symbol.Aggregation)11 Test (org.junit.Test)10 InputColumn (io.crate.expression.symbol.InputColumn)9 Symbol (io.crate.expression.symbol.Symbol)9 CountAggregation (io.crate.execution.engine.aggregation.impl.CountAggregation)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)6 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)4 StreamInput (org.elasticsearch.common.io.stream.StreamInput)4 Row (io.crate.data.Row)3 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)3 Function (io.crate.expression.symbol.Function)3 ArrayList (java.util.ArrayList)3 Bucket (io.crate.data.Bucket)2 CollectionBucket (io.crate.data.CollectionBucket)2 Projector (io.crate.data.Projector)2 MergePhase (io.crate.execution.dsl.phases.MergePhase)2 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)2 Projection (io.crate.execution.dsl.projection.Projection)2 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)2 AggregationContext (io.crate.execution.engine.aggregation.AggregationContext)2