Search in sources :

Example 1 with BsonElement

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());
    }
}
Also used : ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) BsonElement(org.bson.BsonElement) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) SchemaPath(org.apache.drill.common.expression.SchemaPath) BsonString(org.bson.BsonString) RexNode(org.apache.calcite.rex.RexNode) BsonValue(org.bson.BsonValue)

Example 2 with BsonElement

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;
}
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 3 with BsonElement

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);
    }
}
Also used : BsonElement(org.bson.BsonElement)

Example 4 with BsonElement

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;
}
Also used : BsonElement(org.bson.BsonElement) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) BsonBoolean(org.bson.BsonBoolean)

Example 5 with BsonElement

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");
}
Also used : BsonElement(org.bson.BsonElement) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) TraceMongoCommandListener.getNonEmptyBsonString(brave.mongodb.TraceMongoCommandListener.getNonEmptyBsonString) Test(org.junit.Test)

Aggregations

BsonElement (org.bson.BsonElement)9 BsonDocument (org.bson.BsonDocument)6 BsonString (org.bson.BsonString)6 ArrayList (java.util.ArrayList)5 TraceMongoCommandListener.getNonEmptyBsonString (brave.mongodb.TraceMongoCommandListener.getNonEmptyBsonString)2 BsonInt32 (org.bson.BsonInt32)2 Test (org.junit.Test)2 MongoNamespace (com.mongodb.MongoNamespace)1 Accumulators (com.mongodb.client.model.Accumulators)1 Aggregates (com.mongodb.client.model.Aggregates)1 BsonField (com.mongodb.client.model.BsonField)1 Arrays (java.util.Arrays)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 RexNode (org.apache.calcite.rex.RexNode)1