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