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