use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.
the class GroupByPlannerTest method testCountDistinctWithGroupBy.
@Test
public void testCountDistinctWithGroupBy() throws Exception {
Merge distributedGroupByMerge = e.plan("select count(distinct id), name from users group by name order by count(distinct id)");
DistributedGroupBy distributedGroupBy = (DistributedGroupBy) distributedGroupByMerge.subPlan();
RoutedCollectPhase collectPhase = distributedGroupBy.collectPhase();
// collect
assertThat(collectPhase.toCollect().get(0), instanceOf(Reference.class));
assertThat(collectPhase.toCollect().size(), is(2));
assertThat(((Reference) collectPhase.toCollect().get(0)).ident().columnIdent().name(), is("id"));
assertThat(((Reference) collectPhase.toCollect().get(1)).ident().columnIdent().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));
Aggregation aggregation = groupProjection.values().get(0);
assertThat(aggregation.toStep(), is(Aggregation.Step.PARTIAL));
Symbol aggregationInput = aggregation.inputs().get(0);
assertThat(aggregationInput.symbolType(), is(SymbolType.INPUT_COLUMN));
// reducer
MergePhase mergePhase = distributedGroupBy.reducerMergeNode();
assertThat(mergePhase.projections().size(), is(2));
Projection groupProjection1 = mergePhase.projections().get(0);
assertThat(groupProjection1, instanceOf(GroupProjection.class));
groupProjection = (GroupProjection) groupProjection1;
assertThat(groupProjection.keys().get(0), instanceOf(InputColumn.class));
assertThat(((InputColumn) groupProjection.keys().get(0)).index(), is(0));
assertThat(groupProjection.values().get(0), instanceOf(Aggregation.class));
Aggregation aggregationStep2 = groupProjection.values().get(0);
assertThat(aggregationStep2.toStep(), is(Aggregation.Step.FINAL));
OrderedTopNProjection topNProjection = (OrderedTopNProjection) mergePhase.projections().get(1);
Symbol collection_count = topNProjection.outputs().get(0);
assertThat(collection_count, instanceOf(Function.class));
// handler
MergePhase localMergeNode = distributedGroupByMerge.mergePhase();
assertThat(localMergeNode.projections(), empty());
}
use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.
the class GroupByPlannerTest method testGroupByWithHavingAndLimit.
@Test
public void testGroupByWithHavingAndLimit() throws Exception {
Merge planNode = e.plan("select count(*), name from users group by name having count(*) > 1 limit 100");
DistributedGroupBy distributedGroupBy = (DistributedGroupBy) planNode.subPlan();
// reducer
MergePhase mergePhase = distributedGroupBy.reducerMergeNode();
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: count(*), name
TopNProjection topN = (TopNProjection) mergePhase.projections().get(2);
assertThat(topN.outputs().get(0).valueType(), Is.<DataType>is(DataTypes.LONG));
assertThat(topN.outputs().get(1).valueType(), Is.<DataType>is(DataTypes.STRING));
MergePhase localMerge = planNode.mergePhase();
// topN projection
// outputs: count(*), name
topN = (TopNProjection) localMerge.projections().get(0);
assertThat(topN.outputs().get(0).valueType(), Is.<DataType>is(DataTypes.LONG));
assertThat(topN.outputs().get(1).valueType(), Is.<DataType>is(DataTypes.STRING));
}
use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.
the class GroupByPlannerTest method testGroupByHaving.
@Test
public void testGroupByHaving() throws Exception {
Merge distributedGroupByMerge = e.plan("select avg(date), name from users group by name having min(date) > '1970-01-01'");
DistributedGroupBy distributedGroupBy = (DistributedGroupBy) distributedGroupByMerge.subPlan();
RoutedCollectPhase collectPhase = distributedGroupBy.collectPhase();
assertThat(collectPhase.projections().size(), is(1));
assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
MergePhase mergePhase = distributedGroupBy.reducerMergeNode();
assertThat(mergePhase.projections().size(), is(3));
// grouping
assertThat(mergePhase.projections().get(0), instanceOf(GroupProjection.class));
GroupProjection groupProjection = (GroupProjection) mergePhase.projections().get(0);
assertThat(groupProjection.values().size(), is(2));
// filter the having clause
assertThat(mergePhase.projections().get(1), instanceOf(FilterProjection.class));
FilterProjection filterProjection = (FilterProjection) mergePhase.projections().get(1);
assertThat(mergePhase.projections().get(2), instanceOf(EvalProjection.class));
EvalProjection eval = (EvalProjection) mergePhase.projections().get(2);
assertThat(eval.outputs().get(0).valueType(), Is.<DataType>is(DataTypes.DOUBLE));
assertThat(eval.outputs().get(1).valueType(), Is.<DataType>is(DataTypes.STRING));
}
use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.
the class GroupByPlannerTest method testGroupByWithAggregationAndLimit.
@Test
public void testGroupByWithAggregationAndLimit() throws Exception {
Merge distributedGroupByMerge = e.plan("select count(*), name from users group by name limit 1 offset 1");
DistributedGroupBy distributedGroupBy = (DistributedGroupBy) distributedGroupByMerge.subPlan();
// distributed merge
MergePhase distributedMergePhase = distributedGroupBy.reducerMergeNode();
assertThat(distributedMergePhase.projections().get(0), instanceOf(GroupProjection.class));
assertThat(distributedMergePhase.projections().get(1), instanceOf(TopNProjection.class));
// limit must include offset because the real limit can only be applied on the handler
// after all rows have been gathered.
TopNProjection topN = (TopNProjection) distributedMergePhase.projections().get(1);
assertThat(topN.limit(), is(2));
assertThat(topN.offset(), is(0));
// local merge
MergePhase localMergePhase = distributedGroupByMerge.mergePhase();
assertThat(localMergePhase.projections().get(0), instanceOf(TopNProjection.class));
topN = (TopNProjection) localMergePhase.projections().get(0);
assertThat(topN.limit(), is(1));
assertThat(topN.offset(), is(1));
assertThat(topN.outputs().get(0), instanceOf(InputColumn.class));
assertThat(((InputColumn) topN.outputs().get(0)).index(), is(0));
assertThat(topN.outputs().get(1), instanceOf(InputColumn.class));
assertThat(((InputColumn) topN.outputs().get(1)).index(), is(1));
}
use of io.crate.planner.node.dql.DistributedGroupBy in project crate by crate.
the class GroupByPlannerTest method testGroupByWithHavingAndNoLimit.
@Test
public void testGroupByWithHavingAndNoLimit() throws Exception {
Merge planNode = e.plan("select count(*), name from users group by name having count(*) > 1");
DistributedGroupBy distributedGroupBy = (DistributedGroupBy) planNode.subPlan();
// reducer
MergePhase mergePhase = distributedGroupBy.reducerMergeNode();
// group 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));
assertThat(mergePhase.outputTypes().get(0), equalTo(DataTypes.LONG));
assertThat(mergePhase.outputTypes().get(1), equalTo(DataTypes.STRING));
mergePhase = planNode.mergePhase();
assertThat(mergePhase.outputTypes().get(0), equalTo(DataTypes.LONG));
assertThat(mergePhase.outputTypes().get(1), equalTo(DataTypes.STRING));
}
Aggregations