Search in sources :

Example 26 with MergePhase

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));
}
Also used : 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) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 27 with MergePhase

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()));
}
Also used : Aggregation(io.crate.expression.symbol.Aggregation) CountAggregation(io.crate.execution.engine.aggregation.impl.CountAggregation) MergePhase(io.crate.execution.dsl.phases.MergePhase) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) StreamInput(org.elasticsearch.common.io.stream.StreamInput) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 28 with MergePhase

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);
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) Routing(io.crate.metadata.Routing) NodeOperation(io.crate.execution.dsl.phases.NodeOperation) UUID(java.util.UUID) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase)

Example 29 with MergePhase

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());
}
Also used : MergePhase(io.crate.execution.dsl.phases.MergePhase) Projection(io.crate.execution.dsl.projection.Projection)

Example 30 with MergePhase

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));
}
Also used : SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) Function(io.crate.expression.symbol.Function) MergePhase(io.crate.execution.dsl.phases.MergePhase) Collect(io.crate.planner.node.dql.Collect) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

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