Search in sources :

Example 1 with BsonField

use of com.mongodb.client.model.BsonField 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 2 with BsonField

use of com.mongodb.client.model.BsonField in project incubator-rya by apache.

the class AggregationPipelineQueryNode method distinct.

/**
 * Add a $group step to filter out redundant solutions.
 * @return True if the distinct operation was successfully appended.
 */
public boolean distinct() {
    List<String> key = new LinkedList<>();
    for (String varName : bindingNames) {
        key.add(hashFieldExpr(varName));
    }
    List<BsonField> reduceOps = new LinkedList<>();
    for (String field : FIELDS) {
        reduceOps.add(new BsonField(field, new Document("$first", "$" + field)));
    }
    pipeline.add(Aggregates.group(new Document("$concat", key), reduceOps));
    return true;
}
Also used : BsonField(com.mongodb.client.model.BsonField) Document(org.bson.Document) LinkedList(java.util.LinkedList)

Example 3 with BsonField

use of com.mongodb.client.model.BsonField in project drill by apache.

the class MongoAggregateUtils method bsonAggregate.

private static BsonField bsonAggregate(List<String> inNames, String outName, AggregateCall aggCall) {
    String aggregationName = aggCall.getAggregation().getName();
    List<Integer> args = aggCall.getArgList();
    if (aggregationName.equals(SqlStdOperatorTable.COUNT.getName())) {
        Object expr;
        if (args.size() == 0) {
            // count(*) case
            expr = 1;
        } else {
            assert args.size() == 1;
            String inName = inNames.get(args.get(0));
            expr = new BsonDocument(MongoOp.COND.getCompareOp(), new BsonArray(Arrays.asList(new Document(MongoOp.EQUAL.getCompareOp(), new BsonArray(Arrays.asList(new BsonString(quote(inName)), BsonNull.VALUE))).toBsonDocument(), new BsonInt32(0), new BsonInt32(1))));
        }
        return Accumulators.sum(maybeQuote(outName), expr);
    } else {
        BiFunction<String, Object, BsonField> mongoAccumulator = mongoAccumulator(aggregationName);
        if (mongoAccumulator != null) {
            return mongoAccumulator.apply(maybeQuote(outName), "$" + inNames.get(args.get(0)));
        }
    }
    return null;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonField(com.mongodb.client.model.BsonField) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Aggregations

BsonField (com.mongodb.client.model.BsonField)3 Document (org.bson.Document)3 BsonArray (org.bson.BsonArray)2 BsonDocument (org.bson.BsonDocument)2 BsonInt32 (org.bson.BsonInt32)2 BsonString (org.bson.BsonString)2 Accumulators (com.mongodb.client.model.Accumulators)1 Aggregates (com.mongodb.client.model.Aggregates)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 BiFunction (java.util.function.BiFunction)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 Aggregate (org.apache.calcite.rel.core.Aggregate)1 AggregateCall (org.apache.calcite.rel.core.AggregateCall)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)1 SqlValidatorUtil (org.apache.calcite.sql.validate.SqlValidatorUtil)1