use of io.crate.planner.node.dql.Collect in project crate by crate.
the class GroupByPlannerTest method testGroupByHavingNonDistributed.
@Test
public void testGroupByHavingNonDistributed() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
Merge merge = e.plan("select id from users group by id having id > 0");
Collect collect = (Collect) merge.subPlan();
RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
assertThat(collectPhase.where(), isSQL("(doc.users.id > 0::bigint)"));
assertThat(collectPhase.projections(), contains(instanceOf(GroupProjection.class)));
MergePhase localMergeNode = merge.mergePhase();
assertThat(localMergeNode.projections(), empty());
}
use of io.crate.planner.node.dql.Collect in project crate by crate.
the class GroupByPlannerTest method testNoDistributedGroupByOnAllPrimaryKeys.
@Test
public void testNoDistributedGroupByOnAllPrimaryKeys() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addPartitionedTable("create table doc.empty_parted (" + " id integer primary key," + " date timestamp with time zone primary key" + ") clustered by (id) partitioned by (date)").build();
Collect collect = e.plan("select count(*), id, date from empty_parted group by id, date limit 20");
RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
assertThat(collectPhase.projections().size(), is(3));
assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
assertThat(collectPhase.projections().get(0).requiredGranularity(), is(RowGranularity.SHARD));
assertThat(collectPhase.projections().get(1), instanceOf(TopNProjection.class));
assertThat(collectPhase.projections().get(2), instanceOf(EvalProjection.class));
}
use of io.crate.planner.node.dql.Collect in project crate by crate.
the class GroupByPlannerTest method testGroupByHaving.
@Test
public void testGroupByHaving() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
Merge distributedGroupByMerge = e.plan("select avg(date), name from users group by name having min(date) > '1970-01-01'");
Merge reduceMerge = (Merge) distributedGroupByMerge.subPlan();
CollectPhase collectPhase = ((Collect) reduceMerge.subPlan()).collectPhase();
assertThat(collectPhase.projections().size(), is(1));
assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
MergePhase reducePhase = reduceMerge.mergePhase();
assertThat(reducePhase.projections().size(), is(3));
// grouping
assertThat(reducePhase.projections().get(0), instanceOf(GroupProjection.class));
GroupProjection groupProjection = (GroupProjection) reducePhase.projections().get(0);
assertThat(groupProjection.values().size(), is(2));
// filter the having clause
assertThat(reducePhase.projections().get(1), instanceOf(FilterProjection.class));
FilterProjection filterProjection = (FilterProjection) reducePhase.projections().get(1);
assertThat(reducePhase.projections().get(2), instanceOf(EvalProjection.class));
EvalProjection eval = (EvalProjection) reducePhase.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.Collect 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());
}
use of io.crate.planner.node.dql.Collect in project crate by crate.
the class GroupByPlannerTest method testGroupByOnNodeLevel.
@Test
public void testGroupByOnNodeLevel() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).build();
Collect collect = e.plan("select count(*), name from sys.nodes group by name");
assertThat("number of nodeIds must be 1, otherwise there must be a merge", collect.resultDescription().nodeIds().size(), is(1));
RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
assertThat(collectPhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class)));
GroupProjection groupProjection = (GroupProjection) collectPhase.projections().get(0);
assertThat(Symbols.typeView(groupProjection.outputs()), contains(is(DataTypes.STRING), is(DataTypes.LONG)));
assertThat(Symbols.typeView(collectPhase.projections().get(1).outputs()), contains(is(DataTypes.LONG), is(DataTypes.STRING)));
}
Aggregations