Search in sources :

Example 1 with OrderedTopNProjection

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

the class ProjectionToProjectorVisitorTest method testTopNProjectionToSortingProjector.

@Test
public void testTopNProjectionToSortingProjector() throws Exception {
    List<Symbol> outputs = Arrays.asList(Literal.of("foo"), new InputColumn(0), new InputColumn(1));
    OrderedTopNProjection projection = new OrderedTopNProjection(TopN.NO_LIMIT, TopN.NO_OFFSET, outputs, Arrays.asList(new InputColumn(0), new InputColumn(1)), new boolean[] { false, false }, new boolean[] { false, false });
    Projector projector = visitor.create(projection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
    assertThat(projector, instanceOf(SortingProjector.class));
}
Also used : Projector(io.crate.data.Projector) SortingProjector(io.crate.execution.engine.sort.SortingProjector) SortingTopNProjector(io.crate.execution.engine.sort.SortingTopNProjector) GroupingProjector(io.crate.execution.engine.aggregation.GroupingProjector) SortingProjector(io.crate.execution.engine.sort.SortingProjector) 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)

Example 2 with OrderedTopNProjection

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

the class GroupByPlannerTest method testGroupByOrderByPartitionedClolumn.

@Test
public void testGroupByOrderByPartitionedClolumn() 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();
    Merge plan = e.plan("select date from clustered_parted group by date order by date");
    Merge reduceMerge = (Merge) plan.subPlan();
    OrderedTopNProjection topNProjection = (OrderedTopNProjection) reduceMerge.mergePhase().projections().get(1);
    Symbol orderBy = topNProjection.orderBy().get(0);
    assertThat(orderBy, instanceOf(InputColumn.class));
    assertThat(orderBy.valueType(), is(DataTypes.TIMESTAMPZ));
}
Also used : PartitionName(io.crate.metadata.PartitionName) Merge(io.crate.planner.Merge) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) RelationName(io.crate.metadata.RelationName) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 3 with OrderedTopNProjection

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

the class GroupByPlannerTest method testNonDistributedGroupByOnClusteredColumnSortedScalar.

@Test
public void testNonDistributedGroupByOnClusteredColumnSortedScalar() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge merge = e.plan("select count(*) + 1, id from users group by id order by count(*) + 1 limit 20");
    Collect collect = (Collect) merge.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertThat(collectPhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(OrderedTopNProjection.class), instanceOf(EvalProjection.class)));
    assertThat(((OrderedTopNProjection) collectPhase.projections().get(1)).orderBy().size(), is(1));
    assertThat(collectPhase.projections().get(0).requiredGranularity(), is(RowGranularity.SHARD));
    MergePhase mergePhase = merge.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(TopNProjection.class)));
    PositionalOrderBy positionalOrderBy = mergePhase.orderByPositions();
    assertThat(positionalOrderBy, notNullValue());
    assertThat(positionalOrderBy.indices().length, is(1));
    assertThat(positionalOrderBy.indices()[0], is(0));
    assertThat(positionalOrderBy.reverseFlags()[0], is(false));
    assertThat(positionalOrderBy.nullsFirst()[0], is(false));
}
Also used : PositionalOrderBy(io.crate.planner.PositionalOrderBy) MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) Collect(io.crate.planner.node.dql.Collect) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 4 with OrderedTopNProjection

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

the class GroupByPlannerTest method testNonDistributedGroupByOnClusteredColumnSorted.

@Test
public void testNonDistributedGroupByOnClusteredColumnSorted() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    Merge merge = e.plan("select count(*), id from users group by id order by 1 desc nulls last limit 20");
    Collect collect = ((Collect) merge.subPlan());
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    List<Projection> collectProjections = collectPhase.projections();
    assertThat(collectProjections, contains(instanceOf(GroupProjection.class), instanceOf(OrderedTopNProjection.class), // swap id, count(*) -> count(*), id
    instanceOf(EvalProjection.class)));
    assertThat(collectProjections.get(1), instanceOf(OrderedTopNProjection.class));
    assertThat(((OrderedTopNProjection) collectProjections.get(1)).orderBy().size(), is(1));
    assertThat(collectProjections.get(0).requiredGranularity(), is(RowGranularity.SHARD));
    MergePhase mergePhase = merge.mergePhase();
    assertThat(mergePhase.projections(), contains(instanceOf(TopNProjection.class)));
    PositionalOrderBy positionalOrderBy = mergePhase.orderByPositions();
    assertThat(positionalOrderBy, notNullValue());
    assertThat(positionalOrderBy.indices().length, is(1));
    assertThat(positionalOrderBy.indices()[0], is(0));
    assertThat(positionalOrderBy.reverseFlags()[0], is(true));
    assertThat(positionalOrderBy.nullsFirst()[0], is(false));
}
Also used : PositionalOrderBy(io.crate.planner.PositionalOrderBy) MergePhase(io.crate.execution.dsl.phases.MergePhase) Merge(io.crate.planner.Merge) Collect(io.crate.planner.node.dql.Collect) 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) OrderedTopNProjection(io.crate.execution.dsl.projection.OrderedTopNProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 5 with OrderedTopNProjection

use of io.crate.execution.dsl.projection.OrderedTopNProjection 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)

Aggregations

OrderedTopNProjection (io.crate.execution.dsl.projection.OrderedTopNProjection)9 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)8 Test (org.junit.Test)8 Symbol (io.crate.expression.symbol.Symbol)7 InputColumn (io.crate.expression.symbol.InputColumn)6 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)5 Merge (io.crate.planner.Merge)5 MergePhase (io.crate.execution.dsl.phases.MergePhase)4 Projector (io.crate.data.Projector)3 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)3 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)3 GroupingProjector (io.crate.execution.engine.aggregation.GroupingProjector)3 SortingProjector (io.crate.execution.engine.sort.SortingProjector)3 SortingTopNProjector (io.crate.execution.engine.sort.SortingTopNProjector)3 PositionalOrderBy (io.crate.planner.PositionalOrderBy)3 Collect (io.crate.planner.node.dql.Collect)3 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)2 FilterProjection (io.crate.execution.dsl.projection.FilterProjection)2 Projection (io.crate.execution.dsl.projection.Projection)2 TopNProjection (io.crate.execution.dsl.projection.TopNProjection)2