Skip to content

Commit 0993936

Browse files
authored
fix(explain-compat): handle missing execution stats in raw explain (#4624)
1 parent effbcd2 commit 0993936

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

packages/mongodb-explain-compat/lib/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function mapShardedFind(explain) {
175175
}
176176
queryPlanner.winningPlan.shards.forEach((shard, index) => {
177177
const winningPlan = shard.winningPlan.queryPlan;
178-
if (winningPlan) {
178+
if (winningPlan && executionStats) {
179179
const executionStages = mapStages(
180180
winningPlan,
181181
executionStats.executionStages.shards[index].executionStages
@@ -204,10 +204,12 @@ function _mapPlannerStage(planner) {
204204
const winningPlan = planner.queryPlanner.winningPlan.queryPlan;
205205
planner.queryPlanner.winningPlan = winningPlan;
206206

207-
planner.executionStats.executionStages = mapStages(
208-
winningPlan,
209-
planner.executionStats.executionStages
210-
);
207+
if (planner.executionStats) {
208+
planner.executionStats.executionStages = mapStages(
209+
winningPlan,
210+
planner.executionStats.executionStages
211+
);
212+
}
211213
}
212214
return planner;
213215
}

packages/mongodb-explain-compat/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
],
2626
"scripts": {
2727
"lint": "eslint **/*.js",
28-
"test": "npm run lint && npm run compile && nyc mocha --colors test/*.js",
28+
"test": "npm run compile && nyc mocha --colors test/*.js",
2929
"compile": "gen-esm-wrapper . ./.esm-wrapper.mjs",
3030
"prepublishOnly": "npm run compile && compass-scripts check-exports-exist",
3131
"test-ci": "npm run test",
3232
"depcheck": "depcheck",
33-
"bootstrap": "npm run compile"
33+
"bootstrap": "npm run compile",
34+
"check": "npm run lint && npm run depcheck",
35+
"check-ci": "npm run check"
3436
},
3537
"homepage": "https://github.com/mongodb-js/compass",
3638
"repository": {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"explainVersion": "2",
3+
"queryPlanner": {
4+
"namespace": "test.test",
5+
"indexFilterSet": false,
6+
"parsedQuery": {
7+
"foo": {
8+
"$eq": 1
9+
}
10+
},
11+
"queryHash": "AFE90209",
12+
"planCacheKey": "AFE90209",
13+
"maxIndexedOrSolutionsReached": false,
14+
"maxIndexedAndSolutionsReached": false,
15+
"maxScansToExplodeReached": false,
16+
"winningPlan": {
17+
"queryPlan": {
18+
"stage": "LIMIT",
19+
"planNodeId": 2,
20+
"limitAmount": 20,
21+
"inputStage": {
22+
"stage": "COLLSCAN",
23+
"planNodeId": 1,
24+
"filter": {
25+
"foo": {
26+
"$eq": 1
27+
}
28+
},
29+
"direction": "forward"
30+
}
31+
},
32+
"slotBasedPlan": {
33+
"slots": "$$RESULT=s4 $$RID=s5 env: { s1 = TimeZoneDatabase(America/Indiana/Vevay...Europe/Brussels) (timeZoneDB), s2 = Nothing (SEARCH_META), s3 = 1688644646710 (NOW) }",
34+
"stages": "[2] limit 20 \n[1] filter {fillEmpty (s8, false)} \n[1] traverse s8 s7 s6 [s4, s5] {s8 || s7} {s8} \nfrom \n [1] project [s6 = getField (s4, \"foo\")] \n [1] scan s4 s5 none none none none [] @\"5a530bd0-f7ef-4245-9568-c67723199e33\" true false \nin \n [1] project [s7 = fillEmpty (s6 == 1, false)] \n [1] limit 1 \n [1] coscan \n"
35+
}
36+
},
37+
"rejectedPlans": []
38+
}
39+
}

packages/mongodb-explain-compat/test/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const fs = require('fs');
66
const path = require('path');
77

88
function fixture(name) {
9-
return JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures', name + '.json'), 'utf8'));
9+
return JSON.parse(
10+
fs.readFileSync(path.join(__dirname, 'fixtures', name + '.json'), 'utf8')
11+
);
1012
}
1113

1214
describe('convertExplainCompat', () => {
@@ -104,4 +106,12 @@ describe('convertExplainCompat', () => {
104106
);
105107
});
106108
});
109+
110+
describe('explain mode "queryPlanner"', function () {
111+
it('should work without failing', function () {
112+
assert.doesNotThrow(() => {
113+
convertExplainCompat(fixture('query-planner-only'));
114+
});
115+
});
116+
});
107117
});

0 commit comments

Comments
 (0)