use of io.crate.execution.dsl.phases.MergePhase 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.phases.MergePhase 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()));
}
use of io.crate.execution.dsl.phases.MergePhase in project crate by crate.
the class DistributingConsumerFactoryTest method createDownstream.
private RowConsumer createDownstream(Set<String> downstreamExecutionNodes) {
UUID jobId = UUID.randomUUID();
Routing routing = new Routing(Map.of("n1", Map.of("i1", IntArrayList.from(1, 2))));
RoutedCollectPhase collectPhase = new RoutedCollectPhase(jobId, 1, "collect", routing, RowGranularity.DOC, List.of(), List.of(), WhereClause.MATCH_ALL.queryOrFallback(), DistributionInfo.DEFAULT_MODULO);
MergePhase mergePhase = new MergePhase(jobId, 2, "merge", 1, 1, downstreamExecutionNodes, List.of(LongType.INSTANCE), List.of(), DistributionInfo.DEFAULT_BROADCAST, null);
NodeOperation nodeOperation = NodeOperation.withDownstream(collectPhase, mergePhase, (byte) 0);
return rowDownstreamFactory.create(nodeOperation, RamAccounting.NO_ACCOUNTING, collectPhase.distributionInfo(), jobId, Paging.PAGE_SIZE);
}
use of io.crate.execution.dsl.phases.MergePhase in project crate by crate.
the class Merge method ensureOnHandler.
/**
* Wrap the subPlan into a Merge plan if it isn't executed on the handler.
* @param projections projections to be applied on the subPlan or merge plan.
* These projections must not affect the limit, offset, order by or numOutputs
*
* If the subPlan contains a limit/offset or orderBy in its resultDescription this method will
* add a TopNProjection AFTER the projections.
*/
public static ExecutionPlan ensureOnHandler(ExecutionPlan subExecutionPlan, PlannerContext plannerContext, List<Projection> projections) {
ResultDescription resultDescription = subExecutionPlan.resultDescription();
assert resultDescription != null : "all plans must have a result description. Plan without: " + subExecutionPlan;
// If a sub-Plan applies a limit it is usually limit+offset
// So even if the execution is on the handler the limit may be too large and the final limit needs to be applied as well
Projection topN = ProjectionBuilder.topNOrEvalIfNeeded(resultDescription.limit(), resultDescription.offset(), resultDescription.numOutputs(), resultDescription.streamOutputs());
if (ExecutionPhases.executesOnHandler(plannerContext.handlerNode(), resultDescription.nodeIds())) {
return addProjections(subExecutionPlan, projections, resultDescription, topN);
}
maybeUpdatePageSizeHint(subExecutionPlan, resultDescription.maxRowsPerNode());
Collection<String> handlerNodeIds = Collections.singletonList(plannerContext.handlerNode());
MergePhase mergePhase = new MergePhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), "mergeOnHandler", resultDescription.nodeIds().size(), 1, handlerNodeIds, resultDescription.streamOutputs(), addProjection(projections, topN), DistributionInfo.DEFAULT_BROADCAST, resultDescription.orderBy());
return new Merge(subExecutionPlan, mergePhase, TopN.NO_LIMIT, 0, resultDescription.numOutputs(), resultDescription.limit(), resultDescription.orderBy());
}
use of io.crate.execution.dsl.phases.MergePhase in project crate by crate.
the class GroupByScalarPlannerTest method testGroupByWithScalarPlan.
@Test
public void testGroupByWithScalarPlan() throws Exception {
Merge merge = e.plan("select id + 1 from users group by id");
Collect collect = (Collect) merge.subPlan();
RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
assertEquals(DataTypes.LONG, collectPhase.outputTypes().get(0));
assertThat(collectPhase.maxRowGranularity(), is(RowGranularity.DOC));
assertThat(collectPhase.projections().size(), is(2));
assertThat(collectPhase.projections().get(0), instanceOf(GroupProjection.class));
assertThat(collectPhase.projections().get(0).requiredGranularity(), is(RowGranularity.SHARD));
assertThat(collectPhase.projections().get(1), instanceOf(EvalProjection.class));
assertThat(collectPhase.projections().get(1).outputs().get(0), instanceOf(Function.class));
assertThat(collectPhase.toCollect(), contains(isReference("id", DataTypes.LONG)));
GroupProjection groupProjection = (GroupProjection) collectPhase.projections().get(0);
assertThat(groupProjection.keys().get(0).valueType(), is(DataTypes.LONG));
assertThat(collectPhase.projections().get(1).outputs(), contains(isFunction("add")));
MergePhase mergePhase = merge.mergePhase();
assertEquals(DataTypes.LONG, mergePhase.inputTypes().iterator().next());
assertEquals(DataTypes.LONG, mergePhase.outputTypes().get(0));
}
Aggregations