Search in sources :

Example 1 with SortingTopNProjector

use of io.crate.execution.engine.sort.SortingTopNProjector in project crate by crate.

the class ProjectionToProjectorVisitor method visitOrderedTopN.

@Override
public Projector visitOrderedTopN(OrderedTopNProjection projection, Context context) {
    /* OrderBy symbols are added to the rows to enable sorting on them post-collect. E.g.:
         *
         * outputs: [x]
         * orderBy: [y]
         *
         * topLevelInputs: [x, y]
         *                    /
         * orderByIndices: [1]
         */
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(context.txnCtx);
    ctx.add(projection.outputs());
    ctx.add(projection.orderBy());
    int numOutputs = projection.outputs().size();
    List<Input<?>> inputs = ctx.topLevelInputs();
    int[] orderByIndices = new int[inputs.size() - numOutputs];
    int idx = 0;
    for (int i = numOutputs; i < inputs.size(); i++) {
        orderByIndices[idx++] = i;
    }
    // priority queues implementation are backed by an arrayList
    int rowMemoryOverhead = 32;
    RowCellsAccountingWithEstimators rowAccounting = new RowCellsAccountingWithEstimators(Symbols.typeView(Lists2.concat(projection.outputs(), projection.orderBy())), context.ramAccounting, rowMemoryOverhead);
    if (projection.limit() > TopN.NO_LIMIT) {
        return new SortingTopNProjector(rowAccounting, inputs, ctx.expressions(), numOutputs, OrderingByPosition.arrayOrdering(orderByIndices, projection.reverseFlags(), projection.nullsFirst()), projection.limit(), projection.offset(), UNBOUNDED_COLLECTOR_THRESHOLD);
    }
    return new SortingProjector(rowAccounting, inputs, ctx.expressions(), numOutputs, OrderingByPosition.arrayOrdering(orderByIndices, projection.reverseFlags(), projection.nullsFirst()), projection.offset());
}
Also used : SortingTopNProjector(io.crate.execution.engine.sort.SortingTopNProjector) InputFactory(io.crate.expression.InputFactory) RowCellsAccountingWithEstimators(io.crate.breaker.RowCellsAccountingWithEstimators) Input(io.crate.data.Input) SortingProjector(io.crate.execution.engine.sort.SortingProjector) NestableCollectExpression(io.crate.execution.engine.collect.NestableCollectExpression) CollectExpression(io.crate.execution.engine.collect.CollectExpression)

Aggregations

RowCellsAccountingWithEstimators (io.crate.breaker.RowCellsAccountingWithEstimators)1 Input (io.crate.data.Input)1 CollectExpression (io.crate.execution.engine.collect.CollectExpression)1 NestableCollectExpression (io.crate.execution.engine.collect.NestableCollectExpression)1 SortingProjector (io.crate.execution.engine.sort.SortingProjector)1 SortingTopNProjector (io.crate.execution.engine.sort.SortingTopNProjector)1 InputFactory (io.crate.expression.InputFactory)1