Search in sources :

Example 6 with DistributedGroupBy

use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.

the class GroupByPlannerTest method testGroupByHavingAndNoSelectListReOrderingWithLimit.

@Test
public void testGroupByHavingAndNoSelectListReOrderingWithLimit() throws Exception {
    Merge planNode = e.plan("select name, count(*) from users group by name having count(*) > 1 limit 100");
    DistributedGroupBy distributedGroupBy = (DistributedGroupBy) planNode.subPlan();
    // reducer
    MergePhase mergePhase = distributedGroupBy.reducerMergeNode();
    // group projection
    //      outputs: name, count(*)
    // filter projection
    //      outputs: name, count(*)
    // topN projection
    //      outputs: name, count(*)
    Projection projection = mergePhase.projections().get(1);
    assertThat(projection, instanceOf(FilterProjection.class));
    FilterProjection filterProjection = (FilterProjection) projection;
    Symbol countArgument = ((Function) filterProjection.query()).arguments().get(0);
    assertThat(countArgument, instanceOf(InputColumn.class));
    // pointing to second output from group projection
    assertThat(((InputColumn) countArgument).index(), is(1));
    // outputs: name, count(*)
    assertThat(((InputColumn) filterProjection.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) filterProjection.outputs().get(1)).index(), is(1));
    // outputs: name, count(*)
    TopNProjection topN = (TopNProjection) mergePhase.projections().get(2);
    assertThat(((InputColumn) topN.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) topN.outputs().get(1)).index(), is(1));
    MergePhase localMerge = planNode.mergePhase();
    // topN projection
    //      outputs: name, count(*)
    topN = (TopNProjection) localMerge.projections().get(0);
    assertThat(((InputColumn) topN.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) topN.outputs().get(1)).index(), is(1));
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Merge(io.crate.planner.Merge) DistributedGroupBy(io.crate.planner.node.dql.DistributedGroupBy) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 7 with DistributedGroupBy

use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.

the class GroupByPlannerTest method testGroupByWithHavingAndNoSelectListReordering.

@Test
public void testGroupByWithHavingAndNoSelectListReordering() throws Exception {
    Merge planNode = e.plan("select name, count(*) from users group by name having count(*) > 1");
    DistributedGroupBy distributedGroupBy = (DistributedGroupBy) planNode.subPlan();
    // reducer
    MergePhase reducerMerge = distributedGroupBy.reducerMergeNode();
    // group projection
    //      outputs: name, count(*)
    // filter projection
    //      outputs: name, count(*)
    assertThat(reducerMerge.projections(), contains(instanceOf(GroupProjection.class), instanceOf(FilterProjection.class), instanceOf(EvalProjection.class)));
    Projection projection = reducerMerge.projections().get(1);
    assertThat(projection, instanceOf(FilterProjection.class));
    FilterProjection filterProjection = (FilterProjection) projection;
    Symbol countArgument = ((Function) filterProjection.query()).arguments().get(0);
    assertThat(countArgument, instanceOf(InputColumn.class));
    // pointing to second output from group projection
    assertThat(((InputColumn) countArgument).index(), is(1));
    // outputs: name, count(*)
    assertThat(((InputColumn) filterProjection.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) filterProjection.outputs().get(1)).index(), is(1));
    // eval projection
    //      outputs: name, count(*)
    EvalProjection eval = (EvalProjection) reducerMerge.projections().get(2);
    assertThat(((InputColumn) eval.outputs().get(0)).index(), is(0));
    assertThat(((InputColumn) eval.outputs().get(1)).index(), is(1));
    MergePhase localMerge = planNode.mergePhase();
    assertThat(localMerge.projections(), empty());
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Merge(io.crate.planner.Merge) DistributedGroupBy(io.crate.planner.node.dql.DistributedGroupBy) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 8 with DistributedGroupBy

use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.

the class GroupByPlannerTest method testGroupByWithAggregationPlan.

@Test
public void testGroupByWithAggregationPlan() throws Exception {
    Merge distributedGroupByMerge = e.plan("select count(*), name from users group by name");
    DistributedGroupBy distributedGroupBy = (DistributedGroupBy) distributedGroupByMerge.subPlan();
    // distributed collect
    RoutedCollectPhase collectPhase = distributedGroupBy.collectPhase();
    assertThat(collectPhase.maxRowGranularity(), is(RowGranularity.DOC));
    assertThat(collectPhase.nodeIds().size(), is(2));
    assertThat(collectPhase.toCollect().size(), is(1));
    assertThat(collectPhase.projections().size(), is(1));
    assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
    assertThat(collectPhase.outputTypes().size(), is(2));
    assertEquals(DataTypes.STRING, collectPhase.outputTypes().get(0));
    assertEquals(CountAggregation.LongStateType.INSTANCE, collectPhase.outputTypes().get(1));
    MergePhase mergePhase = distributedGroupBy.reducerMergeNode();
    assertThat(mergePhase.numUpstreams(), is(2));
    assertThat(mergePhase.nodeIds().size(), is(2));
    assertEquals(mergePhase.inputTypes(), collectPhase.outputTypes());
    // for function evaluation and column-reordering there is always a EvalProjection
    assertThat(mergePhase.projections().size(), is(2));
    assertThat(mergePhase.projections().get(1), instanceOf(EvalProjection.class));
    assertThat(mergePhase.projections().get(0), instanceOf(GroupProjection.class));
    GroupProjection groupProjection = (GroupProjection) mergePhase.projections().get(0);
    InputColumn inputColumn = (InputColumn) groupProjection.values().get(0).inputs().get(0);
    assertThat(inputColumn.index(), is(1));
    assertThat(mergePhase.outputTypes().size(), is(2));
    assertEquals(DataTypes.LONG, mergePhase.outputTypes().get(0));
    assertEquals(DataTypes.STRING, mergePhase.outputTypes().get(1));
    MergePhase localMerge = distributedGroupByMerge.mergePhase();
    assertThat(localMerge.numUpstreams(), is(2));
    assertThat(localMerge.nodeIds().size(), is(1));
    assertThat(Iterables.getOnlyElement(localMerge.nodeIds()), is("noop_id"));
    assertEquals(mergePhase.outputTypes(), localMerge.inputTypes());
    assertThat(localMerge.projections(), empty());
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Merge(io.crate.planner.Merge) DistributedGroupBy(io.crate.planner.node.dql.DistributedGroupBy) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 9 with DistributedGroupBy

use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.

the class InsertPlannerTest method testInsertFromSubQueryDistributedGroupByWithoutLimit.

@Test
public void testInsertFromSubQueryDistributedGroupByWithoutLimit() throws Exception {
    Merge planNode = e.plan("insert into users (id, name) (select name, count(*) from users group by name)");
    DistributedGroupBy groupBy = (DistributedGroupBy) planNode.subPlan();
    MergePhase mergePhase = groupBy.reducerMergeNode();
    assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
    ColumnIndexWriterProjection projection = (ColumnIndexWriterProjection) mergePhase.projections().get(2);
    assertThat(projection.primaryKeys().size(), is(1));
    assertThat(projection.primaryKeys().get(0).fqn(), is("id"));
    assertThat(projection.columnReferences().size(), is(2));
    assertThat(projection.columnReferences().get(0).ident().columnIdent().fqn(), is("id"));
    assertThat(projection.columnReferences().get(1).ident().columnIdent().fqn(), is("name"));
    assertNotNull(projection.clusteredByIdent());
    assertThat(projection.clusteredByIdent().fqn(), is("id"));
    assertThat(projection.tableIdent().fqn(), is("doc.users"));
    assertThat(projection.partitionedBySymbols().isEmpty(), is(true));
    MergePhase localMergeNode = planNode.mergePhase();
    assertThat(localMergeNode.projections().size(), is(1));
    assertThat(localMergeNode.projections().get(0), instanceOf(MergeCountProjection.class));
    assertThat(localMergeNode.finalProjection().get().outputs().size(), is(1));
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) DistributedGroupBy(io.crate.planner.node.dql.DistributedGroupBy) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 10 with DistributedGroupBy

use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.

the class GroupByPlannerTest method testGroupByWithOrderOnAggregate.

@Test
public void testGroupByWithOrderOnAggregate() throws Exception {
    Merge merge = e.plan("select count(*), name from users group by name order by count(*)");
    assertThat(merge.mergePhase().orderByPositions(), notNullValue());
    DistributedGroupBy distributedGroupBy = (DistributedGroupBy) merge.subPlan();
    MergePhase mergePhase = distributedGroupBy.reducerMergeNode();
    assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(OrderedTopNProjection.class)));
    OrderedTopNProjection topNProjection = (OrderedTopNProjection) mergePhase.projections().get(1);
    Symbol orderBy = topNProjection.orderBy().get(0);
    assertThat(orderBy, instanceOf(InputColumn.class));
    assertThat(orderBy.valueType(), Is.<DataType>is(DataTypes.LONG));
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Merge(io.crate.planner.Merge) DistributedGroupBy(io.crate.planner.node.dql.DistributedGroupBy) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

DistributedGroupBy (io.crate.planner.node.dql.DistributedGroupBy)17 CrateUnitTest (io.crate.test.integration.CrateUnitTest)17 Test (org.junit.Test)17 Merge (io.crate.planner.Merge)14 MergePhase (io.crate.planner.node.dql.MergePhase)13 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)7 InputColumn (io.crate.analyze.symbol.InputColumn)1 Reference (io.crate.metadata.Reference)1 CountAggregation (io.crate.operation.aggregation.impl.CountAggregation)1 SymbolMatchers.isAggregation (io.crate.testing.SymbolMatchers.isAggregation)1 SymbolMatchers.isReference (io.crate.testing.SymbolMatchers.isReference)1