use of io.crate.execution.dsl.phases.MergePhase in project crate by crate.
the class InsertPlannerTest method testInsertFromSubQueryReduceOnCollectorGroupByWithCast.
@Test
public void testInsertFromSubQueryReduceOnCollectorGroupByWithCast() {
Merge merge = e.plan("insert into users (id, name) (select id, count(*) from users group by id)");
Collect nonDistributedGroupBy = (Collect) merge.subPlan();
RoutedCollectPhase collectPhase = ((RoutedCollectPhase) nonDistributedGroupBy.collectPhase());
assertThat(collectPhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
EvalProjection collectTopN = (EvalProjection) collectPhase.projections().get(1);
assertThat(collectTopN.outputs(), contains(isInputColumn(0), isFunction(ImplicitCastFunction.NAME, List.of(DataTypes.LONG, DataTypes.STRING))));
ColumnIndexWriterProjection columnIndexWriterProjection = (ColumnIndexWriterProjection) collectPhase.projections().get(2);
assertThat(columnIndexWriterProjection.columnReferencesExclPartition(), contains(isReference("id"), isReference("name")));
MergePhase mergePhase = merge.mergePhase();
assertThat(mergePhase.projections(), contains(instanceOf(MergeCountProjection.class)));
}
use of io.crate.execution.dsl.phases.MergePhase in project crate by crate.
the class Union method build.
@Override
public ExecutionPlan build(PlannerContext plannerContext, Set<PlanHint> hints, ProjectionBuilder projectionBuilder, int limit, int offset, @Nullable OrderBy order, @Nullable Integer pageSizeHint, Row params, SubQueryResults subQueryResults) {
Integer childPageSizeHint = limit != TopN.NO_LIMIT ? limitAndOffset(limit, offset) : null;
ExecutionPlan left = lhs.build(plannerContext, hints, projectionBuilder, limit + offset, TopN.NO_OFFSET, null, childPageSizeHint, params, subQueryResults);
ExecutionPlan right = rhs.build(plannerContext, hints, projectionBuilder, limit + offset, TopN.NO_OFFSET, null, childPageSizeHint, params, subQueryResults);
addCastsForIncompatibleObjects(right);
if (left.resultDescription().hasRemainingLimitOrOffset()) {
left = Merge.ensureOnHandler(left, plannerContext);
}
if (right.resultDescription().hasRemainingLimitOrOffset()) {
right = Merge.ensureOnHandler(right, plannerContext);
}
ResultDescription leftResultDesc = left.resultDescription();
ResultDescription rightResultDesc = right.resultDescription();
assert DataTypes.isCompatibleType(leftResultDesc.streamOutputs(), rightResultDesc.streamOutputs()) : "Left and right must output the same types, got " + "lhs=" + leftResultDesc.streamOutputs() + ", rhs=" + rightResultDesc.streamOutputs();
MergePhase mergePhase = new MergePhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), "union", leftResultDesc.nodeIds().size() + rightResultDesc.nodeIds().size(), 2, Collections.singletonList(plannerContext.handlerNode()), leftResultDesc.streamOutputs(), Collections.emptyList(), DistributionInfo.DEFAULT_BROADCAST, leftResultDesc.orderBy());
return new UnionExecutionPlan(left, right, mergePhase, limit, offset, lhs.outputs().size(), TopN.NO_LIMIT, leftResultDesc.orderBy());
}
use of io.crate.execution.dsl.phases.MergePhase in project crate by crate.
the class NestedLoopJoin method configureExecution.
private Tuple<Collection<String>, List<MergePhase>> configureExecution(ExecutionPlan left, ExecutionPlan right, PlannerContext plannerContext, boolean isDistributed) {
Collection<String> nlExecutionNodes = Set.of(plannerContext.handlerNode());
ResultDescription leftResultDesc = left.resultDescription();
ResultDescription rightResultDesc = right.resultDescription();
MergePhase leftMerge = null;
MergePhase rightMerge = null;
if (leftResultDesc.nodeIds().size() == 1 && leftResultDesc.nodeIds().equals(rightResultDesc.nodeIds()) && !rightResultDesc.hasRemainingLimitOrOffset()) {
// if the left and the right plan are executed on the same single node the mergePhase
// should be omitted. This is the case if the left and right table have only one shards which
// are on the same node
nlExecutionNodes = leftResultDesc.nodeIds();
left.setDistributionInfo(DistributionInfo.DEFAULT_SAME_NODE);
right.setDistributionInfo(DistributionInfo.DEFAULT_SAME_NODE);
} else if (isDistributed && !leftResultDesc.hasRemainingLimitOrOffset()) {
// run join phase distributed on all nodes of the left relation
nlExecutionNodes = leftResultDesc.nodeIds();
left.setDistributionInfo(DistributionInfo.DEFAULT_SAME_NODE);
right.setDistributionInfo(DistributionInfo.DEFAULT_BROADCAST);
rightMerge = JoinOperations.buildMergePhaseForJoin(plannerContext, rightResultDesc, nlExecutionNodes);
} else {
// run join phase non-distributed on the handler
left.setDistributionInfo(DistributionInfo.DEFAULT_BROADCAST);
right.setDistributionInfo(DistributionInfo.DEFAULT_BROADCAST);
if (JoinOperations.isMergePhaseNeeded(nlExecutionNodes, leftResultDesc, false)) {
leftMerge = JoinOperations.buildMergePhaseForJoin(plannerContext, leftResultDesc, nlExecutionNodes);
}
if (JoinOperations.isMergePhaseNeeded(nlExecutionNodes, rightResultDesc, false)) {
rightMerge = JoinOperations.buildMergePhaseForJoin(plannerContext, rightResultDesc, nlExecutionNodes);
}
}
return new Tuple<>(nlExecutionNodes, Arrays.asList(leftMerge, rightMerge));
}
use of io.crate.execution.dsl.phases.MergePhase in project crate by crate.
the class GroupByPlannerTest method testGroupByWithHavingAndNoLimit.
@Test
public void testGroupByWithHavingAndNoLimit() throws Exception {
var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
Merge planNode = e.plan("select count(*), name from users group by name having count(*) > 1");
Merge reducerMerge = (Merge) planNode.subPlan();
// reducer
MergePhase mergePhase = reducerMerge.mergePhase();
// 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));
}
use of io.crate.execution.dsl.phases.MergePhase in project crate by crate.
the class JoinPhaseTest method setup.
@Before
public void setup() {
topNProjection = new TopNProjection(10, 0, Collections.emptyList());
jobId = UUID.randomUUID();
mp1 = new MergePhase(jobId, 2, "merge", 1, 1, Collections.emptyList(), List.of(DataTypes.STRING), List.of(), DistributionInfo.DEFAULT_BROADCAST, null);
mp2 = new MergePhase(jobId, 3, "merge", 1, 1, Collections.emptyList(), List.of(DataTypes.STRING), List.of(), DistributionInfo.DEFAULT_BROADCAST, null);
joinCondition = new Function(EqOperator.SIGNATURE, List.of(new InputColumn(0, DataTypes.STRING), new InputColumn(1, DataTypes.STRING)), EqOperator.RETURN_TYPE);
}
Aggregations