use of io.crate.execution.dsl.projection.OrderedTopNProjection 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.execution.dsl.projection.OrderedTopNProjection in project crate by crate.
the class GroupByPlannerTest method testGroupByWithOrderOnAggregate.
@Test
public void testGroupByWithOrderOnAggregate() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
Merge merge = e.plan("select count(*), name from users group by name order by count(*)");
assertThat(merge.mergePhase().orderByPositions(), notNullValue());
Merge reducerMerge = (Merge) merge.subPlan();
MergePhase mergePhase = reducerMerge.mergePhase();
assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(OrderedTopNProjection.class), instanceOf(EvalProjection.class)));
OrderedTopNProjection topNProjection = (OrderedTopNProjection) mergePhase.projections().get(1);
Symbol orderBy = topNProjection.orderBy().get(0);
assertThat(orderBy, instanceOf(InputColumn.class));
assertThat(orderBy.valueType(), Is.is(DataTypes.LONG));
}
use of io.crate.execution.dsl.projection.OrderedTopNProjection in project crate by crate.
the class ProjectionToProjectorVisitorTest method testSortingTopNProjection.
@Test
public void testSortingTopNProjection() throws Exception {
List<Symbol> outputs = Arrays.asList(Literal.of("foo"), new InputColumn(0), new InputColumn(1));
OrderedTopNProjection projection = new OrderedTopNProjection(10, 0, outputs, Arrays.asList(new InputColumn(0), new InputColumn(1)), new boolean[] { false, false }, new boolean[] { false, false });
Projector projector = visitor.create(projection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
assertThat(projector, instanceOf(SortingTopNProjector.class));
}
use of io.crate.execution.dsl.projection.OrderedTopNProjection 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)));
}
Aggregations