Search in sources :

Example 21 with RelFieldCollation

use of org.apache.calcite.rel.RelFieldCollation in project drill by apache.

the class WindowPrel method getPhysicalOperator.

@Override
public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException {
    Prel child = (Prel) this.getInput();
    PhysicalOperator childPOP = child.getPhysicalOperator(creator);
    final List<String> childFields = getInput().getRowType().getFieldNames();
    // We don't support distinct partitions
    checkState(groups.size() == 1, "Only one window is expected in WindowPrel");
    Group window = groups.get(0);
    List<NamedExpression> withins = Lists.newArrayList();
    List<NamedExpression> aggs = Lists.newArrayList();
    List<Order.Ordering> orderings = Lists.newArrayList();
    for (int group : BitSets.toIter(window.keys)) {
        FieldReference fr = new FieldReference(childFields.get(group), ExpressionPosition.UNKNOWN);
        withins.add(new NamedExpression(fr, fr));
    }
    for (AggregateCall aggCall : window.getAggregateCalls(this)) {
        FieldReference ref = new FieldReference(aggCall.getName());
        LogicalExpression expr = toDrill(aggCall, childFields);
        aggs.add(new NamedExpression(expr, ref));
    }
    for (RelFieldCollation fieldCollation : window.orderKeys.getFieldCollations()) {
        orderings.add(new Order.Ordering(fieldCollation.getDirection(), new FieldReference(childFields.get(fieldCollation.getFieldIndex())), fieldCollation.nullDirection));
    }
    WindowPOP windowPOP = new WindowPOP(childPOP, withins, aggs, orderings, window.isRows, WindowPOP.newBound(window.lowerBound), WindowPOP.newBound(window.upperBound));
    creator.addMetadata(this, windowPOP);
    return windowPOP;
}
Also used : Order(org.apache.drill.common.logical.data.Order) FieldReference(org.apache.drill.common.expression.FieldReference) WindowPOP(org.apache.drill.exec.physical.config.WindowPOP) AggregateCall(org.apache.calcite.rel.core.AggregateCall) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 22 with RelFieldCollation

use of org.apache.calcite.rel.RelFieldCollation in project drill by apache.

the class SortPrule method getDistributionField.

private List<DistributionField> getDistributionField(DrillSortRel rel) {
    List<DistributionField> distFields = Lists.newArrayList();
    for (RelFieldCollation relField : rel.getCollation().getFieldCollations()) {
        DistributionField field = new DistributionField(relField.getFieldIndex());
        distFields.add(field);
    }
    return distFields;
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) DistributionField(org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField)

Example 23 with RelFieldCollation

use of org.apache.calcite.rel.RelFieldCollation in project drill by apache.

the class DrillWindowRel method implement.

@Override
public LogicalOperator implement(DrillImplementor implementor) {
    final LogicalOperator inputOp = implementor.visitChild(this, 0, getInput());
    org.apache.drill.common.logical.data.Window.Builder builder = new org.apache.drill.common.logical.data.Window.Builder();
    final List<String> fields = getRowType().getFieldNames();
    final List<String> childFields = getInput().getRowType().getFieldNames();
    for (Group window : groups) {
        for (RelFieldCollation orderKey : window.orderKeys.getFieldCollations()) {
            builder.addOrdering(new Order.Ordering(orderKey.getDirection(), new FieldReference(fields.get(orderKey.getFieldIndex()))));
        }
        for (int group : BitSets.toIter(window.keys)) {
            FieldReference fr = new FieldReference(childFields.get(group), ExpressionPosition.UNKNOWN);
            builder.addWithin(fr, fr);
        }
        int groupCardinality = window.keys.cardinality();
        for (Ord<AggregateCall> aggCall : Ord.zip(window.getAggregateCalls(this))) {
            FieldReference ref = new FieldReference(fields.get(groupCardinality + aggCall.i));
            LogicalExpression expr = toDrill(aggCall.e, childFields);
            builder.addAggregation(ref, expr);
        }
    }
    builder.setInput(inputOp);
    org.apache.drill.common.logical.data.Window frame = builder.build();
    return frame;
}
Also used : Order(org.apache.drill.common.logical.data.Order) FieldReference(org.apache.drill.common.expression.FieldReference) LogicalOperator(org.apache.drill.common.logical.data.LogicalOperator) AggregateCall(org.apache.calcite.rel.core.AggregateCall) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 24 with RelFieldCollation

use of org.apache.calcite.rel.RelFieldCollation 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());
    }
    return new DrillSortRel(context.getCluster(), context.getLogicalTraits(), input, RelCollationImpl.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)

Example 25 with RelFieldCollation

use of org.apache.calcite.rel.RelFieldCollation in project lucene-solr by apache.

the class SolrSort method implement.

public void implement(Implementor implementor) {
    implementor.visitChild(0, getInput());
    List<RelFieldCollation> sortCollations = collation.getFieldCollations();
    if (!sortCollations.isEmpty()) {
        // Construct a series of order clauses from the desired collation
        final List<RelDataTypeField> fields = getRowType().getFieldList();
        for (RelFieldCollation fieldCollation : sortCollations) {
            final String name = fields.get(fieldCollation.getFieldIndex()).getName();
            String direction = "asc";
            if (fieldCollation.getDirection().equals(RelFieldCollation.Direction.DESCENDING)) {
                direction = "desc";
            }
            implementor.addOrder(name, direction);
        }
    }
    if (fetch != null) {
        implementor.setLimit(((RexLiteral) fetch).getValue().toString());
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Aggregations

RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)25 RelNode (org.apache.calcite.rel.RelNode)10 RexNode (org.apache.calcite.rex.RexNode)9 ImmutableList (com.google.common.collect.ImmutableList)8 ArrayList (java.util.ArrayList)5 FieldReference (org.apache.drill.common.expression.FieldReference)4 HiveSortLimit (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit)4 RelCollation (org.apache.calcite.rel.RelCollation)3 RexInputRef (org.apache.calcite.rex.RexInputRef)3 Mappings (org.apache.calcite.util.mapping.Mappings)3 Order (org.apache.drill.common.logical.data.Order)3 HiveRelCollation (org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelCollation)3 OrderByColumnSpec (io.druid.query.groupby.orderby.OrderByColumnSpec)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 AggregateCall (org.apache.calcite.rel.core.AggregateCall)2 Sort (org.apache.calcite.rel.core.Sort)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 RexLiteral (org.apache.calcite.rex.RexLiteral)2 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)2