use of org.bson.BsonElement in project drill by apache.
the class MongoPluginImplementor method implement.
@Override
public void implement(PluginProjectRel project) throws IOException {
runAggregate = runAggregate || project.getProjects().stream().anyMatch(expression -> !expression.isA(SqlKind.INPUT_REF));
visitChild(project.getInput());
if (runAggregate) {
RexToMongoTranslator translator = new RexToMongoTranslator((JavaTypeFactory) project.getCluster().getTypeFactory(), MongoAggregateUtils.mongoFieldNames(project.getInput().getRowType()));
List<BsonElement> items = new ArrayList<>();
for (Pair<RexNode, String> pair : project.getNamedProjects()) {
String name = pair.right;
BsonValue expr = pair.left.accept(translator);
items.add(expr.equals(new BsonString("$" + name)) ? new BsonElement(MongoAggregateUtils.maybeQuote(name), new BsonInt32(1)) : new BsonElement(MongoAggregateUtils.maybeQuote(name), expr));
}
BsonDocument projection = Aggregates.project(new BsonDocument(items)).toBsonDocument();
operations.add(projection);
List<String> outNames = MongoAggregateUtils.mongoFieldNames(project.getRowType());
this.columns = outNames.stream().map(SchemaPath::getSimplePath).collect(Collectors.toList());
} else {
List<String> outNames = MongoAggregateUtils.mongoFieldNames(project.getRowType());
this.columns = outNames.stream().map(SchemaPath::getSimplePath).collect(Collectors.toList());
}
}
use of org.bson.BsonElement 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 org.bson.BsonElement in project mongo-java-driver by mongodb.
the class BsonWriterHelper method writeElements.
static void writeElements(final BsonWriter writer, final List<BsonElement> bsonElements) {
for (BsonElement bsonElement : bsonElements) {
writer.writeName(bsonElement.getName());
getCodec(bsonElement.getValue()).encode(writer, bsonElement.getValue(), ENCODER_CONTEXT);
}
}
use of org.bson.BsonElement in project realm-java by realm.
the class UpdateDescription method toUpdateDocument.
/**
* Convert this update description to an update document.
*
* @return an update document with the appropriate $set and $unset documents.
*/
public BsonDocument toUpdateDocument() {
final List<BsonElement> unsets = new ArrayList<>();
for (final String removedField : this.removedFields) {
unsets.add(new BsonElement(removedField, new BsonBoolean(true)));
}
final BsonDocument updateDocument = new BsonDocument();
if (this.updatedFields.size() > 0) {
updateDocument.append("$set", this.updatedFields);
}
if (unsets.size() > 0) {
updateDocument.append("$unset", new BsonDocument(unsets));
}
return updateDocument;
}
use of org.bson.BsonElement in project brave by openzipkin.
the class TraceMongoCommandListenerTest method getCollectionName_allowListedCommandAndCollectionField.
@Test
public void getCollectionName_allowListedCommandAndCollectionField() {
BsonDocument command = new BsonDocument(Arrays.asList(new BsonElement("collection", new BsonString("coll")), new BsonElement("find", new BsonString("bar"))));
// command wins
assertThat(listener.getCollectionName(command, "find")).isEqualTo("bar");
}
Aggregations