Search in sources :

Example 16 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project calcite by apache.

the class DruidQuery method isValid.

@Override
public boolean isValid(Litmus litmus, Context context) {
    if (!super.isValid(litmus, context)) {
        return false;
    }
    final String signature = signature();
    if (!isValidSignature(signature)) {
        return litmus.fail("invalid signature [{}]", signature);
    }
    if (rels.isEmpty()) {
        return litmus.fail("must have at least one rel");
    }
    for (int i = 0; i < rels.size(); i++) {
        final RelNode r = rels.get(i);
        if (i == 0) {
            if (!(r instanceof TableScan)) {
                return litmus.fail("first rel must be TableScan, was ", r);
            }
            if (r.getTable() != table) {
                return litmus.fail("first rel must be based on table table");
            }
        } else {
            final List<RelNode> inputs = r.getInputs();
            if (inputs.size() != 1 || inputs.get(0) != rels.get(i - 1)) {
                return litmus.fail("each rel must have a single input");
            }
            if (r instanceof Aggregate) {
                final Aggregate aggregate = (Aggregate) r;
                if (aggregate.getGroupSets().size() != 1 || aggregate.indicator) {
                    return litmus.fail("no grouping sets");
                }
            }
            if (r instanceof Filter) {
                final Filter filter = (Filter) r;
                final DruidJsonFilter druidJsonFilter = DruidJsonFilter.toDruidFilters(filter.getCondition(), filter.getInput().getRowType(), this);
                if (druidJsonFilter == null) {
                    return litmus.fail("invalid filter [{}]", filter.getCondition());
                }
            }
            if (r instanceof Sort) {
                final Sort sort = (Sort) r;
                if (sort.offset != null && RexLiteral.intValue(sort.offset) != 0) {
                    return litmus.fail("offset not supported");
                }
            }
        }
    }
    return true;
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) AbstractRelNode(org.apache.calcite.rel.AbstractRelNode) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) Sort(org.apache.calcite.rel.core.Sort) Aggregate(org.apache.calcite.rel.core.Aggregate)

Example 17 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project drill by axbaretto.

the class DrillReduceAggregatesRule method reduceAggs.

/*
  private boolean isMatch(AggregateCall call) {
    if (call.getAggregation() instanceof SqlAvgAggFunction) {
      final SqlAvgAggFunction.Subtype subtype =
          ((SqlAvgAggFunction) call.getAggregation()).getSubtype();
      return (subtype == SqlAvgAggFunction.Subtype.AVG);
    }
    return false;
  }
 */
/**
 * Reduces all calls to AVG, STDDEV_POP, STDDEV_SAMP, VAR_POP, VAR_SAMP in
 * the aggregates list to.
 *
 * <p>It handles newly generated common subexpressions since this was done
 * at the sql2rel stage.
 */
private void reduceAggs(RelOptRuleCall ruleCall, Aggregate oldAggRel) {
    RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
    List<AggregateCall> oldCalls = oldAggRel.getAggCallList();
    final int nGroups = oldAggRel.getGroupCount();
    List<AggregateCall> newCalls = new ArrayList<>();
    Map<AggregateCall, RexNode> aggCallMapping = new HashMap<>();
    List<RexNode> projList = new ArrayList<>();
    // pass through group key
    for (int i = 0; i < nGroups; ++i) {
        projList.add(rexBuilder.makeInputRef(getFieldType(oldAggRel, i), i));
    }
    // List of input expressions. If a particular aggregate needs more, it
    // will add an expression to the end, and we will create an extra
    // project.
    RelNode input = oldAggRel.getInput();
    List<RexNode> inputExprs = new ArrayList<>();
    for (RelDataTypeField field : input.getRowType().getFieldList()) {
        inputExprs.add(rexBuilder.makeInputRef(field.getType(), inputExprs.size()));
    }
    // create new agg function calls and rest of project list together
    for (AggregateCall oldCall : oldCalls) {
        projList.add(reduceAgg(oldAggRel, oldCall, newCalls, aggCallMapping, inputExprs));
    }
    final int extraArgCount = inputExprs.size() - input.getRowType().getFieldCount();
    if (extraArgCount > 0) {
        input = RelOptUtil.createProject(input, inputExprs, CompositeList.of(input.getRowType().getFieldNames(), Collections.<String>nCopies(extraArgCount, null)), false, relBuilderFactory.create(input.getCluster(), null));
    }
    Aggregate newAggRel = newAggregateRel(oldAggRel, input, newCalls);
    RelNode projectRel = RelOptUtil.createProject(newAggRel, projList, oldAggRel.getRowType().getFieldNames());
    ruleCall.transformTo(projectRel);
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) Aggregate(org.apache.calcite.rel.core.Aggregate) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) RexNode(org.apache.calcite.rex.RexNode)

Example 18 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project drill by apache.

the class MongoAggregateUtils method getAggregateOperations.

public static List<Bson> getAggregateOperations(Aggregate aggregate, RelDataType rowType) {
    List<String> inNames = mongoFieldNames(rowType);
    List<String> outNames = mongoFieldNames(aggregate.getRowType());
    Object id;
    if (aggregate.getGroupSet().cardinality() == 1) {
        String inName = inNames.get(aggregate.getGroupSet().nth(0));
        id = "$" + inName;
    } else {
        List<BsonElement> elements = StreamSupport.stream(aggregate.getGroupSet().spliterator(), false).map(inNames::get).map(inName -> new BsonElement(inName, new BsonString("$" + inName))).collect(Collectors.toList());
        id = new BsonDocument(elements);
    }
    int outNameIndex = aggregate.getGroupSet().cardinality();
    List<BsonField> accumList = new ArrayList<>();
    for (AggregateCall aggCall : aggregate.getAggCallList()) {
        accumList.add(bsonAggregate(inNames, outNames.get(outNameIndex++), aggCall));
    }
    List<Bson> operationsList = new ArrayList<>();
    operationsList.add(Aggregates.group(id, accumList).toBsonDocument());
    List<BsonElement> projectFields = new ArrayList<>();
    if (aggregate.getGroupSet().cardinality() == 1) {
        for (int index = 0; index < outNames.size(); index++) {
            String outName = outNames.get(index);
            projectFields.add(new BsonElement(maybeQuote(outName), new BsonString("$" + (index == 0 ? "_id" : outName))));
        }
    } else {
        projectFields.add(new BsonElement("_id", new BsonInt32(0)));
        for (int group : aggregate.getGroupSet()) {
            projectFields.add(new BsonElement(maybeQuote(outNames.get(group)), new BsonString("$_id." + outNames.get(group))));
        }
        outNameIndex = aggregate.getGroupSet().cardinality();
        for (AggregateCall ignored : aggregate.getAggCallList()) {
            String outName = outNames.get(outNameIndex++);
            projectFields.add(new BsonElement(maybeQuote(outName), new BsonString("$" + outName)));
        }
    }
    if (!aggregate.getGroupSet().isEmpty()) {
        operationsList.add(Aggregates.project(new BsonDocument(projectFields)).toBsonDocument());
    }
    return operationsList;
}
Also used : Document(org.bson.Document) RelDataType(org.apache.calcite.rel.type.RelDataType) Arrays(java.util.Arrays) BsonNull(org.bson.BsonNull) Accumulators(com.mongodb.client.model.Accumulators) BiFunction(java.util.function.BiFunction) Aggregates(com.mongodb.client.model.Aggregates) BsonString(org.bson.BsonString) Aggregate(org.apache.calcite.rel.core.Aggregate) Collectors(java.util.stream.Collectors) BsonField(com.mongodb.client.model.BsonField) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) MongoOp(org.apache.drill.exec.store.mongo.common.MongoOp) Bson(org.bson.conversions.Bson) BsonElement(org.bson.BsonElement) List(java.util.List) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) BsonArray(org.bson.BsonArray) AggregateCall(org.apache.calcite.rel.core.AggregateCall) StreamSupport(java.util.stream.StreamSupport) BsonInt32(org.bson.BsonInt32) ArrayList(java.util.ArrayList) BsonField(com.mongodb.client.model.BsonField) BsonString(org.bson.BsonString) Bson(org.bson.conversions.Bson) AggregateCall(org.apache.calcite.rel.core.AggregateCall) BsonElement(org.bson.BsonElement) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 19 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project drill by apache.

the class DrillReduceAggregatesRule method onMatch.

@Override
public void onMatch(RelOptRuleCall ruleCall) {
    Aggregate oldAggRel = (Aggregate) ruleCall.rels[0];
    reduceAggs(ruleCall, oldAggRel);
}
Also used : Aggregate(org.apache.calcite.rel.core.Aggregate) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate)

Example 20 with Aggregate

use of org.apache.calcite.rel.core.Aggregate in project drill by apache.

the class DrillFilterAggregateTransposeRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    final Filter filter = call.rel(0);
    final Aggregate aggregate = call.rel(1);
    return filter.getTraitSet().getTrait(ConventionTraitDef.INSTANCE) == aggregate.getTraitSet().getTrait(ConventionTraitDef.INSTANCE);
}
Also used : Filter(org.apache.calcite.rel.core.Filter) Aggregate(org.apache.calcite.rel.core.Aggregate)

Aggregations

Aggregate (org.apache.calcite.rel.core.Aggregate)75 RelNode (org.apache.calcite.rel.RelNode)44 ArrayList (java.util.ArrayList)39 AggregateCall (org.apache.calcite.rel.core.AggregateCall)37 RexNode (org.apache.calcite.rex.RexNode)32 RelBuilder (org.apache.calcite.tools.RelBuilder)27 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)27 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)23 RexBuilder (org.apache.calcite.rex.RexBuilder)22 Project (org.apache.calcite.rel.core.Project)21 HashMap (java.util.HashMap)17 RexInputRef (org.apache.calcite.rex.RexInputRef)15 Join (org.apache.calcite.rel.core.Join)14 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)14 RelDataType (org.apache.calcite.rel.type.RelDataType)14 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)14 Filter (org.apache.calcite.rel.core.Filter)13 HiveAggregate (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate)13 List (java.util.List)12 ImmutableList (com.google.common.collect.ImmutableList)11