Skip to content

Handle new explain output in tests #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class AggregateOperationSpecification extends OperationFunctionalSpecification {
def result = execute(operation, async)

then:
result.containsKey('stages') || result.containsKey('queryPlanner')
result.containsKey('stages') || result.containsKey('queryPlanner') || result.containsKey('shards')

where:
async << [true, false]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,50 @@ public void testExplainOfAggregateWithNewResponseStructure() {
AggregateIterable<BsonDocument> iterable = collection
.aggregate(singletonList(Aggregates.match(Filters.eq("_id", 1))));

Document explainDocument = iterable.explain();
assertNotNull(explainDocument);
Document explainDocument = getAggregateExplainDocument(iterable.explain());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an explanatory comment related to such a structure change at the top of this method, should it be updated / removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. That's not quite the place for it (since it's about skipping the tests) but I added a note above the new helper method instead.

assertTrue(explainDocument.containsKey("queryPlanner"));
assertTrue(explainDocument.containsKey("executionStats"));

explainDocument = iterable.explain(ExplainVerbosity.QUERY_PLANNER);
explainDocument = getAggregateExplainDocument(iterable.explain(ExplainVerbosity.QUERY_PLANNER));
assertNotNull(explainDocument);
assertTrue(explainDocument.containsKey("queryPlanner"));
assertFalse(explainDocument.containsKey("executionStats"));

BsonDocument explainBsonDocument = iterable.explain(BsonDocument.class);
BsonDocument explainBsonDocument = getAggregateExplainDocument(iterable.explain(BsonDocument.class));
assertNotNull(explainBsonDocument);
assertTrue(explainBsonDocument.containsKey("queryPlanner"));
assertTrue(explainBsonDocument.containsKey("executionStats"));

explainBsonDocument = iterable.explain(BsonDocument.class, ExplainVerbosity.QUERY_PLANNER);
explainBsonDocument = getAggregateExplainDocument(iterable.explain(BsonDocument.class, ExplainVerbosity.QUERY_PLANNER));
assertNotNull(explainBsonDocument);
assertTrue(explainBsonDocument.containsKey("queryPlanner"));
assertFalse(explainBsonDocument.containsKey("executionStats"));
}

// Post-MongoDB 7.0, sharded cluster responses move the explain plan document into a "shards" document, which a plan for each shard.
// This method grabs the explain plan document from the first shard when this new structure is present.
private static Document getAggregateExplainDocument(final Document rootAggregateExplainDocument) {
assertNotNull(rootAggregateExplainDocument);
Document aggregateExplainDocument = rootAggregateExplainDocument;
if (rootAggregateExplainDocument.containsKey("shards")) {
Document shardDocument = rootAggregateExplainDocument.get("shards", Document.class);
String firstKey = shardDocument.keySet().iterator().next();
aggregateExplainDocument = shardDocument.get(firstKey, Document.class);
}
return aggregateExplainDocument;
}

private static BsonDocument getAggregateExplainDocument(final BsonDocument rootAggregateExplainDocument) {
assertNotNull(rootAggregateExplainDocument);
BsonDocument aggregateExplainDocument = rootAggregateExplainDocument;
if (rootAggregateExplainDocument.containsKey("shards")) {
BsonDocument shardDocument = rootAggregateExplainDocument.getDocument("shards");
String firstKey = shardDocument.getFirstKey();
aggregateExplainDocument = shardDocument.getDocument(firstKey);
}
return aggregateExplainDocument;
}

@Test
public void testExplainOfAggregateWithOldResponseStructure() {
// Aggregate explain is supported on earlier versions, but the structure of the response on which we're asserting in this test
Expand Down