Search in sources :

Example 16 with MergePhase

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)));
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) Collect(io.crate.planner.node.dql.Collect) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) ColumnIndexWriterProjection(io.crate.execution.dsl.projection.ColumnIndexWriterProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 17 with MergePhase

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());
}
Also used : UnionExecutionPlan(io.crate.planner.UnionExecutionPlan) ExecutionPlan(io.crate.planner.ExecutionPlan) MergePhase(io.crate.execution.dsl.phases.MergePhase) UnionExecutionPlan(io.crate.planner.UnionExecutionPlan) ResultDescription(io.crate.planner.ResultDescription)

Example 18 with MergePhase

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));
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) ResultDescription(io.crate.planner.ResultDescription) Tuple(io.crate.common.collections.Tuple)

Example 19 with MergePhase

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));
}
Also used : FilterProjection(io.crate.execution.dsl.projection.FilterProjection) MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 20 with MergePhase

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);
}
Also used : Function(io.crate.expression.symbol.Function) MergePhase(io.crate.execution.dsl.phases.MergePhase) InputColumn(io.crate.expression.symbol.InputColumn) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) Before(org.junit.Before)

Aggregations

MergePhase (io.crate.execution.dsl.phases.MergePhase)35 Test (org.junit.Test)26 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)24 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)17 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)16 Merge (io.crate.planner.Merge)15 Collect (io.crate.planner.node.dql.Collect)14 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)13 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)12 InputColumn (io.crate.expression.symbol.InputColumn)12 Symbol (io.crate.expression.symbol.Symbol)10 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)9 Projection (io.crate.execution.dsl.projection.Projection)9 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)9 FilterProjection (io.crate.execution.dsl.projection.FilterProjection)8 ColumnIndexWriterProjection (io.crate.execution.dsl.projection.ColumnIndexWriterProjection)6 MergeCountProjection (io.crate.execution.dsl.projection.MergeCountProjection)5 ExecutionPlan (io.crate.planner.ExecutionPlan)4 ResultDescription (io.crate.planner.ResultDescription)4 Routing (io.crate.metadata.Routing)3