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));
}
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));
}
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));
}
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));
}
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;
}
Aggregations