Search in sources :

Example 21 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project drill by axbaretto.

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 22 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project drill by axbaretto.

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 23 with RelFieldCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project drill by axbaretto.

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 24 with RelFieldCollation

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

the class WindowPrule method getCollation.

/**
 * Create a RelCollation that has partition-by as the leading keys followed by order-by keys
 * @param window The window specification
 * @return a RelCollation with {partition-by keys, order-by keys}
 */
private RelCollation getCollation(Window.Group window) {
    List<RelFieldCollation> fields = Lists.newArrayList();
    for (int group : BitSets.toIter(window.keys)) {
        fields.add(new RelFieldCollation(group));
    }
    fields.addAll(window.orderKeys.getFieldCollations());
    return RelCollations.of(fields);
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 25 with RelFieldCollation

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

the class MongoPluginImplementor method implement.

@Override
public void implement(PluginSortRel sort) throws IOException {
    runAggregate = true;
    visitChild(sort.getInput());
    if (!sort.collation.getFieldCollations().isEmpty()) {
        BsonDocument sortKeys = new BsonDocument();
        List<RelDataTypeField> fields = sort.getRowType().getFieldList();
        for (RelFieldCollation fieldCollation : sort.collation.getFieldCollations()) {
            String name = fields.get(fieldCollation.getFieldIndex()).getName();
            sortKeys.put(name, new BsonInt32(direction(fieldCollation)));
        }
        operations.add(Aggregates.sort(sortKeys).toBsonDocument());
    }
    if (sort.offset != null) {
        operations.add(Aggregates.skip(rexLiteralIntValue((RexLiteral) sort.offset)).toBsonDocument());
    }
    if (sort.fetch != null) {
        operations.add(Aggregates.limit(rexLiteralIntValue((RexLiteral) sort.fetch)).toBsonDocument());
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) BsonString(org.bson.BsonString)

Aggregations

RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)101 ArrayList (java.util.ArrayList)36 RexNode (org.apache.calcite.rex.RexNode)36 RelCollation (org.apache.calcite.rel.RelCollation)33 RelNode (org.apache.calcite.rel.RelNode)28 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)17 RexInputRef (org.apache.calcite.rex.RexInputRef)17 RelDataType (org.apache.calcite.rel.type.RelDataType)14 ImmutableList (com.google.common.collect.ImmutableList)13 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)13 Sort (org.apache.calcite.rel.core.Sort)12 RelTraitSet (org.apache.calcite.plan.RelTraitSet)11 RexCall (org.apache.calcite.rex.RexCall)11 RexLiteral (org.apache.calcite.rex.RexLiteral)11 Project (org.apache.calcite.rel.core.Project)9 FieldReference (org.apache.drill.common.expression.FieldReference)9 HashMap (java.util.HashMap)8 RelOptCluster (org.apache.calcite.plan.RelOptCluster)8 Map (java.util.Map)7 AggregateCall (org.apache.calcite.rel.core.AggregateCall)7