Search in sources :

Example 11 with MergePhase

use of io.crate.planner.node.dql.MergePhase in project crate by crate.

the class InsertPlannerTest method testInsertFromSubQueryGlobalAggregate.

@Test
public void testInsertFromSubQueryGlobalAggregate() throws Exception {
    Merge globalAggregate = e.plan("insert into users (name, id) (select arbitrary(name), count(*) from users)");
    MergePhase mergePhase = globalAggregate.mergePhase();
    assertThat(mergePhase.projections().size(), is(3));
    assertThat(mergePhase.projections().get(1), instanceOf(EvalProjection.class));
    assertThat(mergePhase.projections().get(2), instanceOf(ColumnIndexWriterProjection.class));
    ColumnIndexWriterProjection projection = (ColumnIndexWriterProjection) mergePhase.projections().get(2);
    assertThat(projection.columnReferences().size(), is(2));
    assertThat(projection.columnReferences().get(0).ident().columnIdent().fqn(), is("name"));
    assertThat(projection.columnReferences().get(1).ident().columnIdent().fqn(), is("id"));
    assertThat(projection.columnSymbols().size(), is(2));
    assertThat(((InputColumn) projection.columnSymbols().get(0)).index(), is(0));
    assertThat(((InputColumn) projection.columnSymbols().get(1)).index(), is(1));
    assertNotNull(projection.clusteredByIdent());
    assertThat(projection.clusteredByIdent().fqn(), is("id"));
    assertThat(projection.tableIdent().fqn(), is("doc.users"));
    assertThat(projection.partitionedBySymbols().isEmpty(), is(true));
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 12 with MergePhase

use of io.crate.planner.node.dql.MergePhase in project crate by crate.

the class InsertPlannerTest method testInsertFromSubQueryDistributedGroupByWithoutLimit.

@Test
public void testInsertFromSubQueryDistributedGroupByWithoutLimit() throws Exception {
    Merge planNode = e.plan("insert into users (id, name) (select name, count(*) from users group by name)");
    DistributedGroupBy groupBy = (DistributedGroupBy) planNode.subPlan();
    MergePhase mergePhase = groupBy.reducerMergeNode();
    assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
    ColumnIndexWriterProjection projection = (ColumnIndexWriterProjection) mergePhase.projections().get(2);
    assertThat(projection.primaryKeys().size(), is(1));
    assertThat(projection.primaryKeys().get(0).fqn(), is("id"));
    assertThat(projection.columnReferences().size(), is(2));
    assertThat(projection.columnReferences().get(0).ident().columnIdent().fqn(), is("id"));
    assertThat(projection.columnReferences().get(1).ident().columnIdent().fqn(), is("name"));
    assertNotNull(projection.clusteredByIdent());
    assertThat(projection.clusteredByIdent().fqn(), is("id"));
    assertThat(projection.tableIdent().fqn(), is("doc.users"));
    assertThat(projection.partitionedBySymbols().isEmpty(), is(true));
    MergePhase localMergeNode = planNode.mergePhase();
    assertThat(localMergeNode.projections().size(), is(1));
    assertThat(localMergeNode.projections().get(0), instanceOf(MergeCountProjection.class));
    assertThat(localMergeNode.finalProjection().get().outputs().size(), is(1));
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) DistributedGroupBy(io.crate.planner.node.dql.DistributedGroupBy) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 13 with MergePhase

use of io.crate.planner.node.dql.MergePhase in project crate by crate.

the class InsertPlannerTest method testInsertFromSubQueryNonDistributedGroupBy.

@Test
public void testInsertFromSubQueryNonDistributedGroupBy() throws Exception {
    Merge nonDistributedGroupBy = e.plan("insert into users (id, name) (select count(*), name from sys.nodes group by name)");
    MergePhase mergePhase = nonDistributedGroupBy.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class), instanceOf(ColumnIndexWriterProjection.class)));
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 14 with MergePhase

use of io.crate.planner.node.dql.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 Plan ensureOnHandler(Plan subPlan, Planner.Context plannerContext, List<Projection> projections) {
    ResultDescription resultDescription = subPlan.resultDescription();
    assert resultDescription != null : "all plans must have a result description. Plan without: " + subPlan;
    // 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(subPlan, projections, resultDescription, topN);
    }
    Collection<String> handlerNodeIds = getHandlerNodeIds(subPlan, plannerContext, resultDescription);
    MergePhase mergePhase = new MergePhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), "mergeOnHandler", resultDescription.nodeIds().size(), handlerNodeIds, resultDescription.streamOutputs(), addProjection(projections, topN), DistributionInfo.DEFAULT_SAME_NODE, resultDescription.orderBy());
    return new Merge(subPlan, mergePhase, TopN.NO_LIMIT, 0, resultDescription.numOutputs(), resultDescription.limit(), null);
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Projection(io.crate.planner.projection.Projection)

Example 15 with MergePhase

use of io.crate.planner.node.dql.MergePhase in project crate by crate.

the class DistributingDownstreamFactoryTest method createDownstream.

private BatchConsumer createDownstream(Set<String> downstreamExecutionNodes) {
    UUID jobId = UUID.randomUUID();
    Routing routing = new Routing(TreeMapBuilder.<String, Map<String, List<Integer>>>newMapBuilder().put("n1", TreeMapBuilder.<String, List<Integer>>newMapBuilder().put("i1", Arrays.asList(1, 2)).map()).map());
    RoutedCollectPhase collectPhase = new RoutedCollectPhase(jobId, 1, "collect", routing, RowGranularity.DOC, ImmutableList.<Symbol>of(), ImmutableList.<Projection>of(), WhereClause.MATCH_ALL, DistributionInfo.DEFAULT_MODULO);
    MergePhase mergePhase = new MergePhase(jobId, 2, "merge", 1, Collections.emptyList(), ImmutableList.<DataType>of(LongType.INSTANCE), ImmutableList.<Projection>of(), DistributionInfo.DEFAULT_BROADCAST, null);
    mergePhase.executionNodes(downstreamExecutionNodes);
    NodeOperation nodeOperation = NodeOperation.withDownstream(collectPhase, mergePhase, (byte) 0, "nodeName");
    return rowDownstreamFactory.create(nodeOperation, collectPhase.distributionInfo(), jobId, Paging.PAGE_SIZE);
}
Also used : MergePhase(io.crate.planner.node.dql.MergePhase) Routing(io.crate.metadata.Routing) ImmutableList(com.google.common.collect.ImmutableList) NodeOperation(io.crate.operation.NodeOperation) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase)

Aggregations

MergePhase (io.crate.planner.node.dql.MergePhase)30 Test (org.junit.Test)27 CrateUnitTest (io.crate.test.integration.CrateUnitTest)26 Merge (io.crate.planner.Merge)15 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)15 DistributedGroupBy (io.crate.planner.node.dql.DistributedGroupBy)13 Collect (io.crate.planner.node.dql.Collect)9 Reference (io.crate.metadata.Reference)3 Projection (io.crate.planner.projection.Projection)3 ImmutableList (com.google.common.collect.ImmutableList)2 InputColumn (io.crate.analyze.symbol.InputColumn)2 Symbol (io.crate.analyze.symbol.Symbol)2 Routing (io.crate.metadata.Routing)2 NodeOperation (io.crate.operation.NodeOperation)2 CountAggregation (io.crate.operation.aggregation.impl.CountAggregation)2 PositionalOrderBy (io.crate.planner.PositionalOrderBy)2 Aggregation (io.crate.analyze.symbol.Aggregation)1 FunctionIdent (io.crate.metadata.FunctionIdent)1 FunctionInfo (io.crate.metadata.FunctionInfo)1 ExecutionPhase (io.crate.planner.node.ExecutionPhase)1