Search in sources :

Example 16 with ExecutionPlan

use of io.crate.planner.ExecutionPlan 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 17 with ExecutionPlan

use of io.crate.planner.ExecutionPlan in project crate by crate.

the class Order method build.

@Override
public ExecutionPlan build(PlannerContext plannerContext, Set<PlanHint> planHints, ProjectionBuilder projectionBuilder, int limit, int offset, @Nullable OrderBy order, @Nullable Integer pageSizeHint, Row params, SubQueryResults subQueryResults) {
    ExecutionPlan plan = source.build(plannerContext, planHints, projectionBuilder, limit, offset, orderBy, pageSizeHint, params, subQueryResults);
    if (plan.resultDescription().orderBy() != null) {
        // Collect applied ORDER BY eagerly to produce a optimized execution plan;
        if (source instanceof Collect) {
            return plan;
        }
    }
    if (plan.resultDescription().hasRemainingLimitOrOffset()) {
        plan = Merge.ensureOnHandler(plan, plannerContext);
    }
    InputColumns.SourceSymbols ctx = new InputColumns.SourceSymbols(source.outputs());
    List<Symbol> orderByInputColumns = InputColumns.create(this.orderBy.orderBySymbols(), ctx);
    ensureOrderByColumnsArePresentInOutputs(orderByInputColumns);
    OrderedTopNProjection topNProjection = new OrderedTopNProjection(Limit.limitAndOffset(limit, offset), 0, InputColumns.create(outputs, ctx), orderByInputColumns, this.orderBy.reverseFlags(), this.orderBy.nullsFirst());
    PositionalOrderBy positionalOrderBy = PositionalOrderBy.of(this.orderBy, outputs);
    plan.addProjection(topNProjection, limit, offset, positionalOrderBy);
    return plan;
}
Also used : InputColumns(io.crate.execution.dsl.projection.builder.InputColumns) PositionalOrderBy(io.crate.planner.PositionalOrderBy) ExecutionPlan(io.crate.planner.ExecutionPlan) Symbol(io.crate.expression.symbol.Symbol) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection)

Example 18 with ExecutionPlan

use of io.crate.planner.ExecutionPlan in project crate by crate.

the class GroupByPlannerTest method testGroupByOnClusteredByColumnPartitionedOnePartition.

@Test
public void testGroupByOnClusteredByColumnPartitionedOnePartition() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addPartitionedTable("create table doc.clustered_parted (" + "   id integer," + "   date timestamp with time zone," + "   city string" + ") clustered by (city) partitioned by (date) ", new PartitionName(new RelationName("doc", "clustered_parted"), singletonList("1395874800000")).asIndexName(), new PartitionName(new RelationName("doc", "clustered_parted"), singletonList("1395961200000")).asIndexName()).build();
    // only one partition hit
    Merge optimizedPlan = e.plan("select count(*), city from clustered_parted where date=1395874800000 group by city");
    Collect collect = (Collect) optimizedPlan.subPlan();
    assertThat(collect.collectPhase().projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class)));
    assertThat(collect.collectPhase().projections().get(0), instanceOf(GroupProjection.class));
    assertThat(optimizedPlan.mergePhase().projections().size(), is(0));
    // > 1 partition hit
    ExecutionPlan executionPlan = e.plan("select count(*), city from clustered_parted where date=1395874800000 or date=1395961200000 group by city");
    assertThat(executionPlan, instanceOf(Merge.class));
    assertThat(((Merge) executionPlan).subPlan(), instanceOf(Merge.class));
}
Also used : PartitionName(io.crate.metadata.PartitionName) ExecutionPlan(io.crate.planner.ExecutionPlan) Merge(io.crate.planner.Merge) Collect(io.crate.planner.node.dql.Collect) RelationName(io.crate.metadata.RelationName) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 19 with ExecutionPlan

use of io.crate.planner.ExecutionPlan in project crate by crate.

the class CollectTest method test.

@Test
public void test() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("CREATE TABLE t (x int)").build();
    TableStats tableStats = new TableStats();
    PlannerContext plannerCtx = e.getPlannerContext(clusterService.state());
    ProjectionBuilder projectionBuilder = new ProjectionBuilder(e.nodeCtx);
    QueriedSelectRelation analyzedRelation = e.analyze("SELECT 123 AS alias, 456 AS alias2 FROM t ORDER BY alias, 2");
    LogicalPlanner logicalPlanner = new LogicalPlanner(e.nodeCtx, tableStats, () -> clusterService.state().nodes().getMinNodeVersion());
    LogicalPlan operator = logicalPlanner.plan(analyzedRelation, plannerCtx);
    ExecutionPlan build = operator.build(plannerCtx, Set.of(), projectionBuilder, -1, 0, null, null, Row.EMPTY, SubQueryResults.EMPTY);
    assertThat((((RoutedCollectPhase) ((io.crate.planner.node.dql.Collect) build).collectPhase())).orderBy(), is(nullValue()));
}
Also used : PlannerContext(io.crate.planner.PlannerContext) ExecutionPlan(io.crate.planner.ExecutionPlan) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) TableStats(io.crate.statistics.TableStats) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

ExecutionPlan (io.crate.planner.ExecutionPlan)19 Symbol (io.crate.expression.symbol.Symbol)8 MergePhase (io.crate.execution.dsl.phases.MergePhase)4 ResultDescription (io.crate.planner.ResultDescription)4 List (java.util.List)4 DocTableRelation (io.crate.analyze.relations.DocTableRelation)3 NodeOperationTree (io.crate.execution.dsl.phases.NodeOperationTree)3 InputColumns (io.crate.execution.dsl.projection.builder.InputColumns)3 SelectSymbol (io.crate.expression.symbol.SelectSymbol)3 Merge (io.crate.planner.Merge)3 Collect (io.crate.planner.node.dql.Collect)3 ArrayList (java.util.ArrayList)3 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)2 Projection (io.crate.execution.dsl.projection.Projection)2 ProjectionBuilder (io.crate.execution.dsl.projection.builder.ProjectionBuilder)2 PartitionName (io.crate.metadata.PartitionName)2 RelationName (io.crate.metadata.RelationName)2 Join (io.crate.planner.node.dql.join.Join)2 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)2 Collection (java.util.Collection)2