Search in sources :

Example 46 with Ordering

use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.

the class OrderedPartitionRecordBatch method getCopier.

/**
 * Creates a copier that does a project for every Nth record from a
 * VectorContainer incoming into VectorContainer outgoing. Each Ordering in
 * orderings generates a column, and evaluation of the expression associated
 * with each Ordering determines the value of each column. These records will
 * later be sorted based on the values in each column, in the same order as
 * the orderings.
 */
private SampleCopier getCopier(SelectionVector4 sv4, VectorContainer incoming, VectorContainer outgoing, List<Ordering> orderings, List<ValueVector> localAllocationVectors) {
    ErrorCollector collector = new ErrorCollectorImpl();
    ClassGenerator<SampleCopier> cg = CodeGenerator.getRoot(SampleCopier.TEMPLATE_DEFINITION, context.getOptions());
    // Note: disabled for now. This may require some debugging:
    // no tests are available for this operator.
    // cg.getCodeGenerator().plainOldJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    // cg.getCodeGenerator().saveCodeForDebugging(true);
    int i = 0;
    for (Ordering od : orderings) {
        LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), incoming, collector, context.getFunctionRegistry());
        TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder().mergeFrom(expr.getMajorType()).clearMode().setMode(TypeProtos.DataMode.REQUIRED);
        TypeProtos.MajorType newType = builder.build();
        MaterializedField outputField = MaterializedField.create("f" + i++, newType);
        collector.reportErrors(logger);
        ValueVector vector = TypeHelper.getNewVector(outputField, oContext.getAllocator());
        localAllocationVectors.add(vector);
        TypedFieldId fid = outgoing.add(vector);
        ValueVectorWriteExpression write = new ValueVectorWriteExpression(fid, expr, true);
        HoldingContainer hc = cg.addExpr(write);
        cg.getEvalBlock()._if(hc.getValue().eq(JExpr.lit(0)))._then()._return(JExpr.FALSE);
    }
    cg.rotateBlock();
    cg.getEvalBlock()._return(JExpr.TRUE);
    outgoing.buildSchema(BatchSchema.SelectionVectorMode.NONE);
    try {
        SampleCopier sampleCopier = context.getImplementationClass(cg);
        sampleCopier.setupCopier(context, sv4, incoming, outgoing);
        return sampleCopier;
    } catch (SchemaChangeException e) {
        throw schemaChangeException(e, logger);
    }
}
Also used : ErrorCollector(org.apache.drill.common.expression.ErrorCollector) MaterializedField(org.apache.drill.exec.record.MaterializedField) TypeProtos(org.apache.drill.common.types.TypeProtos) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) ValueVector(org.apache.drill.exec.vector.ValueVector) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) ValueVectorWriteExpression(org.apache.drill.exec.expr.ValueVectorWriteExpression) Ordering(org.apache.drill.common.logical.data.Order.Ordering)

Example 47 with Ordering

use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.

the class DrillSortRel method convert.

public static RelNode convert(Order order, ConversionContext context) throws InvalidRelException {
    // if there are compound expressions in the order by, we need to convert into projects on either side.
    RelNode input = context.toRel(order.getInput());
    List<String> fields = input.getRowType().getFieldNames();
    // build a map of field names to indices.
    Map<String, Integer> fieldMap = Maps.newHashMap();
    int i = 0;
    for (String field : fields) {
        fieldMap.put(field, i);
        i++;
    }
    List<RelFieldCollation> collations = Lists.newArrayList();
    for (Ordering o : order.getOrderings()) {
        String fieldName = ExprHelper.getFieldName(o.getExpr());
        int fieldId = fieldMap.get(fieldName);
        RelFieldCollation c = new RelFieldCollation(fieldId, o.getDirection(), o.getNullDirection());
        collations.add(c);
    }
    return new DrillSortRel(context.getCluster(), context.getLogicalTraits(), input, RelCollations.of(collations));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Ordering(org.apache.drill.common.logical.data.Order.Ordering)

Aggregations

Ordering (org.apache.drill.common.logical.data.Order.Ordering)47 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)23 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)23 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)23 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)23 JConditional (com.sun.codemodel.JConditional)21 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)15 FieldReference (org.apache.drill.common.expression.FieldReference)14 Test (org.junit.Test)12 Sort (org.apache.drill.exec.physical.config.Sort)8 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)4 OperatorTest (org.apache.drill.categories.OperatorTest)4 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)4 ValueVector (org.apache.drill.exec.vector.ValueVector)4 BaseTest (org.apache.drill.test.BaseTest)4 DrillTest (org.apache.drill.test.DrillTest)4 VectorContainer (org.apache.drill.exec.record.VectorContainer)3 IOException (java.io.IOException)2 RelNode (org.apache.calcite.rel.RelNode)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)2