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