Search in sources :

Example 6 with Sort

use of org.apache.drill.exec.physical.config.Sort in project drill by axbaretto.

the class BaseSortWrapper method generateComparisons.

protected void generateComparisons(ClassGenerator<?> g, VectorAccessible batch, org.slf4j.Logger logger) {
    g.setMappingSet(MAIN_MAPPING);
    Sort popConfig = context.getOperatorDefn();
    for (Ordering od : popConfig.getOrderings()) {
        // first, we rewrite the evaluation stack for each side of the comparison.
        ErrorCollector collector = new ErrorCollectorImpl();
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFragmentContext().getFunctionRegistry());
        if (collector.hasErrors()) {
            throw UserException.unsupportedError().message("Failure while materializing expression. " + collector.toErrorString()).build(logger);
        }
        g.setMappingSet(LEFT_MAPPING);
        HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(RIGHT_MAPPING);
        HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(MAIN_MAPPING);
        // next we wrap the two comparison sides and add the expression block for the comparison.
        LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFragmentContext().getFunctionRegistry());
        HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
        if (od.getDirection() == Direction.ASCENDING) {
            jc._then()._return(out.getValue());
        } else {
            jc._then()._return(out.getValue().minus());
        }
        g.rotateBlock();
    }
    g.rotateBlock();
    g.getEvalBlock()._return(JExpr.lit(0));
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) Ordering(org.apache.drill.common.logical.data.Order.Ordering) Sort(org.apache.drill.exec.physical.config.Sort) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JConditional(com.sun.codemodel.JConditional)

Example 7 with Sort

use of org.apache.drill.exec.physical.config.Sort in project drill by axbaretto.

the class MergeSortWrapper method merge.

/**
 * Merge the set of in-memory batches to produce a single logical output in the given
 * destination container, indexed by an SV4.
 *
 * @param batchGroups the complete set of in-memory batches
 * @param batch the record batch (operator) for the sort operator
 * @param destContainer the vector container for the sort operator
 * @return the sv4 for this operator
 */
public void merge(List<BatchGroup.InputBatch> batchGroups, int outputBatchSize) {
    // Add the buffered batches to a collection that MSorter can use.
    // The builder takes ownership of the batches and will release them if
    // an error occurs.
    builder = new SortRecordBatchBuilder(context.getAllocator());
    for (BatchGroup.InputBatch group : batchGroups) {
        RecordBatchData rbd = new RecordBatchData(group.getContainer(), context.getAllocator());
        rbd.setSv2(group.getSv2());
        builder.add(rbd);
    }
    batchGroups.clear();
    try {
        builder.build(destContainer);
        sv4 = builder.getSv4();
        Sort popConfig = context.getOperatorDefn();
        mSorter = createNewMSorter(popConfig.getOrderings(), MAIN_MAPPING, LEFT_MAPPING, RIGHT_MAPPING);
        mSorter.setup(context.getFragmentContext(), context.getAllocator(), sv4, destContainer, sv4.getCount(), outputBatchSize);
    } catch (SchemaChangeException e) {
        throw UserException.unsupportedError(e).message("Unexpected schema change - likely code error.").build(logger);
    }
    // For testing memory-leaks, inject exception after mSorter finishes setup
    context.injectUnchecked(ExternalSortBatch.INTERRUPTION_AFTER_SETUP);
    mSorter.sort();
    // For testing memory-leak purpose, inject exception after mSorter finishes sorting
    context.injectUnchecked(ExternalSortBatch.INTERRUPTION_AFTER_SORT);
    sv4 = mSorter.getSV4();
// destContainer.buildSchema(SelectionVectorMode.FOUR_BYTE);
}
Also used : SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) RecordBatchData(org.apache.drill.exec.physical.impl.sort.RecordBatchData) Sort(org.apache.drill.exec.physical.config.Sort) SortRecordBatchBuilder(org.apache.drill.exec.physical.impl.sort.SortRecordBatchBuilder)

Example 8 with Sort

use of org.apache.drill.exec.physical.config.Sort in project drill by axbaretto.

the class TestSortImpl method makeSortImpl.

/**
 * Create the sort implementation to be used by test.
 *
 * @param fixture operator fixture
 * @param sortOrder sort order as specified by {@link Ordering}
 * @param nullOrder null order as specified by {@link Ordering}
 * @param outputBatch where the sort should write its output
 * @return the sort initialized sort implementation, ready to
 * do work
 */
public static SortImpl makeSortImpl(OperatorFixture fixture, String sortOrder, String nullOrder, VectorContainer outputBatch) {
    FieldReference expr = FieldReference.getWithQuotedRef("key");
    Ordering ordering = new Ordering(sortOrder, expr, nullOrder);
    Sort popConfig = new Sort(null, Lists.newArrayList(ordering), false);
    OperatorContext opContext = fixture.newOperatorContext(popConfig);
    QueryId queryId = QueryId.newBuilder().setPart1(1234).setPart2(5678).build();
    FragmentHandle handle = FragmentHandle.newBuilder().setMajorFragmentId(2).setMinorFragmentId(3).setQueryId(queryId).build();
    SortConfig sortConfig = new SortConfig(opContext.getFragmentContext().getConfig(), opContext.getFragmentContext().getOptions());
    SpillSet spillSet = new SpillSet(opContext.getFragmentContext().getConfig(), handle, popConfig);
    PriorityQueueCopierWrapper copierHolder = new PriorityQueueCopierWrapper(opContext);
    SpilledRuns spilledRuns = new SpilledRuns(opContext, spillSet, copierHolder);
    return new SortImpl(opContext, sortConfig, spilledRuns, outputBatch);
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) OperatorContext(org.apache.drill.exec.ops.OperatorContext) QueryId(org.apache.drill.exec.proto.UserBitShared.QueryId) Ordering(org.apache.drill.common.logical.data.Order.Ordering) Sort(org.apache.drill.exec.physical.config.Sort) FragmentHandle(org.apache.drill.exec.proto.ExecProtos.FragmentHandle) SpillSet(org.apache.drill.exec.physical.impl.spill.SpillSet)

Example 9 with Sort

use of org.apache.drill.exec.physical.config.Sort in project drill by axbaretto.

the class TestSorter method makeSortConfig.

public static Sort makeSortConfig(String key, String sortOrder, String nullOrder) {
    FieldReference expr = FieldReference.getWithQuotedRef(key);
    Ordering ordering = new Ordering(sortOrder, expr, nullOrder);
    return new Sort(null, Lists.newArrayList(ordering), false);
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) Ordering(org.apache.drill.common.logical.data.Order.Ordering) Sort(org.apache.drill.exec.physical.config.Sort)

Example 10 with Sort

use of org.apache.drill.exec.physical.config.Sort in project drill by apache.

the class TestSortImpl method makeSortImpl.

/**
 * Create the sort implementation to be used by test.
 *
 * @param fixture operator fixture
 * @param sortOrder sort order as specified by {@link Ordering}
 * @param nullOrder null order as specified by {@link Ordering}
 * @return the initialized sort implementation, ready to do work
 */
public static SortImpl makeSortImpl(OperatorFixture fixture, String sortOrder, String nullOrder) {
    FieldReference expr = FieldReference.getWithQuotedRef("key");
    Ordering ordering = new Ordering(sortOrder, expr, nullOrder);
    Sort popConfig = new Sort(null, Lists.newArrayList(ordering), false);
    OperatorContext opContext = fixture.newOperatorContext(popConfig);
    QueryId queryId = QueryId.newBuilder().setPart1(1234).setPart2(5678).build();
    FragmentHandle handle = FragmentHandle.newBuilder().setMajorFragmentId(2).setMinorFragmentId(3).setQueryId(queryId).build();
    SortConfig sortConfig = new SortConfig(opContext.getFragmentContext().getConfig(), opContext.getFragmentContext().getOptions());
    SpillSet spillSet = new SpillSet(opContext.getFragmentContext().getConfig(), handle, popConfig);
    PriorityQueueCopierWrapper copierHolder = new PriorityQueueCopierWrapper(opContext);
    SpilledRuns spilledRuns = new SpilledRuns(opContext, spillSet, copierHolder);
    dest = new VectorContainer(opContext.getAllocator());
    return new SortImpl(opContext, sortConfig, spilledRuns, dest);
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) OperatorContext(org.apache.drill.exec.ops.OperatorContext) QueryId(org.apache.drill.exec.proto.UserBitShared.QueryId) Ordering(org.apache.drill.common.logical.data.Order.Ordering) Sort(org.apache.drill.exec.physical.config.Sort) FragmentHandle(org.apache.drill.exec.proto.ExecProtos.FragmentHandle) SpillSet(org.apache.drill.exec.physical.impl.spill.SpillSet) VectorContainer(org.apache.drill.exec.record.VectorContainer)

Aggregations

Sort (org.apache.drill.exec.physical.config.Sort)14 Ordering (org.apache.drill.common.logical.data.Order.Ordering)8 FieldReference (org.apache.drill.common.expression.FieldReference)6 OperatorTest (org.apache.drill.categories.OperatorTest)4 OperatorContext (org.apache.drill.exec.ops.OperatorContext)4 Test (org.junit.Test)4 BatchSchema (org.apache.drill.exec.record.BatchSchema)3 VectorContainer (org.apache.drill.exec.record.VectorContainer)3 JConditional (com.sun.codemodel.JConditional)2 ArrayList (java.util.ArrayList)2 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)2 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)2 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)2 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)2 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)2 RecordBatchData (org.apache.drill.exec.physical.impl.sort.RecordBatchData)2 SortRecordBatchBuilder (org.apache.drill.exec.physical.impl.sort.SortRecordBatchBuilder)2 SpillSet (org.apache.drill.exec.physical.impl.spill.SpillSet)2 FragmentHandle (org.apache.drill.exec.proto.ExecProtos.FragmentHandle)2 QueryId (org.apache.drill.exec.proto.UserBitShared.QueryId)2