Search in sources :

Example 21 with Projection

use of io.crate.execution.dsl.projection.Projection in project crate by crate.

the class WindowDefinitionTest method testUnboundedPrecedingUnboundedFollowingFrameIsAllowed.

@Test
public void testUnboundedPrecedingUnboundedFollowingFrameIsAllowed() {
    Collect collect = e.plan("select sum(col1) over(RANGE BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING) FROM " + "unnest([1, 2, 1, 1, 1, 4])");
    List<Projection> projections = collect.collectPhase().projections();
    assertThat(projections.size(), is(2));
    WindowAggProjection windowProjection = null;
    for (Projection projection : projections) {
        if (projection instanceof WindowAggProjection) {
            windowProjection = (WindowAggProjection) projection;
            break;
        }
    }
    assertThat(windowProjection, is(notNullValue()));
    List<? extends Symbol> outputs = windowProjection.outputs();
    // IC and window function
    assertThat(outputs.size(), is(2));
    WindowFunction windowFunction = null;
    for (Symbol output : outputs) {
        if (output instanceof WindowFunction) {
            windowFunction = (WindowFunction) output;
        }
    }
    assertThat(windowFunction, is(notNullValue()));
    assertThat(windowFunction.windowDefinition().windowFrameDefinition().start().type(), is(UNBOUNDED_PRECEDING));
    assertThat(windowFunction.windowDefinition().windowFrameDefinition().end().type(), is(UNBOUNDED_FOLLOWING));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) Collect(io.crate.planner.node.dql.Collect) Symbol(io.crate.expression.symbol.Symbol) Projection(io.crate.execution.dsl.projection.Projection) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 22 with Projection

use of io.crate.execution.dsl.projection.Projection 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 23 with Projection

use of io.crate.execution.dsl.projection.Projection in project crate by crate.

the class SubQueryPlannerTest method testJoinWithAggregationOnSubSelectsWithLimitAndOffset.

@Test
public void testJoinWithAggregationOnSubSelectsWithLimitAndOffset() throws Exception {
    Join join = e.plan("select t1.a, count(*) from " + " (select i, a from t1 order by a limit 10 offset 2) t1 " + "join" + " (select i from t2 order by i desc limit 5 offset 5) t2 " + "on t1.i = t2.i " + "group by t1.a");
    QueryThenFetch qtf = (QueryThenFetch) join.left();
    Collect left = (Collect) qtf.subPlan();
    assertThat("1 node, otherwise mergePhases would be required", left.nodeIds().size(), is(1));
    assertThat(((RoutedCollectPhase) left.collectPhase()).orderBy(), isSQL("doc.t1.a"));
    assertThat(left.collectPhase().projections(), contains(isTopN(10, 2), instanceOf(FetchProjection.class)));
    assertThat(left.collectPhase().toCollect(), isSQL("doc.t1._fetchid, doc.t1.a"));
    Collect right = (Collect) join.right();
    assertThat("1 node, otherwise mergePhases would be required", right.nodeIds().size(), is(1));
    assertThat(((RoutedCollectPhase) right.collectPhase()).orderBy(), isSQL("doc.t2.i DESC"));
    assertThat(right.collectPhase().projections(), contains(isTopN(5, 5)));
    List<Projection> nlProjections = join.joinPhase().projections();
    assertThat(nlProjections, contains(instanceOf(EvalProjection.class), instanceOf(GroupProjection.class)));
}
Also used : QueryThenFetch(io.crate.planner.node.dql.QueryThenFetch) Collect(io.crate.planner.node.dql.Collect) Join(io.crate.planner.node.dql.join.Join) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) AggregationProjection(io.crate.execution.dsl.projection.AggregationProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) FetchProjection(io.crate.execution.dsl.projection.FetchProjection) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 24 with Projection

use of io.crate.execution.dsl.projection.Projection in project crate by crate.

the class GlobalAggregatePlannerTest method testAggregateOnSubQueryNoFetchBecauseColumnInUse.

@Test
public void testAggregateOnSubQueryNoFetchBecauseColumnInUse() throws Exception {
    Collect plan = e.plan("select sum(x) from (select x, i from t1 order by x limit 10) ti");
    List<Projection> projections = plan.collectPhase().projections();
    assertThat(projections, contains(instanceOf(TopNProjection.class), instanceOf(AggregationProjection.class)));
}
Also used : Collect(io.crate.planner.node.dql.Collect) FilterProjection(io.crate.execution.dsl.projection.FilterProjection) AggregationProjection(io.crate.execution.dsl.projection.AggregationProjection) Projection(io.crate.execution.dsl.projection.Projection) TopNProjection(io.crate.execution.dsl.projection.TopNProjection) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

Projection (io.crate.execution.dsl.projection.Projection)24 Test (org.junit.Test)16 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)15 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)15 FilterProjection (io.crate.execution.dsl.projection.FilterProjection)14 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)14 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)13 OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)12 Collect (io.crate.planner.node.dql.Collect)12 Symbol (io.crate.expression.symbol.Symbol)10 MergePhase (io.crate.execution.dsl.phases.MergePhase)9 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)8 InputColumn (io.crate.expression.symbol.InputColumn)8 AggregationProjection (io.crate.execution.dsl.projection.AggregationProjection)7 Merge (io.crate.planner.Merge)7 FetchProjection (io.crate.execution.dsl.projection.FetchProjection)5 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)4 QueryThenFetch (io.crate.planner.node.dql.QueryThenFetch)3 Join (io.crate.planner.node.dql.join.Join)3 CollectPhase (io.crate.execution.dsl.phases.CollectPhase)2