-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
optimizer_utils.js
436 lines (391 loc) · 13.5 KB
/
optimizer_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import {getAggPlanStage, getQueryPlanner, isAggregationPlan} from "jstests/libs/analyze_plan.js";
import {FeatureFlagUtil} from "jstests/libs/feature_flag_util.js";
/**
* Utility for checking if the Cascades optimizer code path is enabled (checks framework control).
*/
export function checkCascadesOptimizerEnabled(theDB) {
const val = theDB.adminCommand({getParameter: 1, internalQueryFrameworkControl: 1})
.internalQueryFrameworkControl;
return val == "tryBonsai" || val == "tryBonsaiExperimental" || val == "forceBonsai";
}
// TODO SERVER-82185: Remove this once M2-eligibility checker + E2E parameterization implemented
export function checkPlanCacheParameterization(theDB) {
return theDB.adminCommand({getParameter: 1, internalCascadesOptimizerEnableParameterization: 1})
.internalCascadesOptimizerEnableParameterization;
}
/**
* Utility for checking if the experimental Cascades optimizer code path is enabled (checks
* framework control for M4+).
*/
export function checkExperimentalCascadesOptimizerEnabled(theDB) {
const val = theDB.adminCommand({getParameter: 1, internalQueryFrameworkControl: 1})
.internalQueryFrameworkControl;
return val == "tryBonsaiExperimental" || val == "forceBonsai";
}
/**
* Utility for checking if the Cascades optimizer feature flag is on.
*/
export function checkCascadesFeatureFlagEnabled(theDB) {
return FeatureFlagUtil.isPresentAndEnabled(theDB, "CommonQueryFramework");
}
/**
* Given the result of an explain command, returns whether the bonsai optimizer was used.
*/
export function usedBonsaiOptimizer(explain) {
function isCQF(queryPlanner) {
return queryPlanner.queryFramework === "cqf";
}
// This section handles the explain output for aggregations against sharded colls.
if (explain.hasOwnProperty("shards")) {
for (let shardName of Object.keys(explain.shards)) {
if (!isCQF(getQueryPlanner(explain.shards[shardName]))) {
return false;
}
}
return true;
} else if (explain.hasOwnProperty("queryPlanner") &&
explain.queryPlanner.winningPlan.hasOwnProperty("shards")) {
// This section handles the explain output for find queries against sharded colls.
for (let shardExplain of explain.queryPlanner.winningPlan.shards) {
if (!isCQF(shardExplain)) {
return false;
}
}
return true
}
// This section handles the explain output for unsharded queries.
return explain.hasOwnProperty("queryPlanner") && isCQF(explain.queryPlanner);
}
/**
* Given a query plan or explain output, follow the leftmost child until
* we reach a leaf stage, and return it.
*
* This is useful for finding the access path part of a plan, typically a PhysicalScan or IndexScan.
*/
export function leftmostLeafStage(node) {
for (;;) {
if (node.queryPlanner) {
node = node.queryPlanner;
} else if (node.winningPlan) {
node = node.winningPlan;
} else if (node.queryPlan) {
node = node.queryPlan;
} else if (node.child) {
node = node.child;
} else if (node.inputStage) {
node = node.inputStage;
} else if (node.leftChild) {
node = node.leftChild;
} else if (node.children) {
node = node.children[0];
} else {
break;
}
}
return node;
}
/**
* Retrieves the cardinality estimate from a node in explain.
*/
export function extractLogicalCEFromNode(node) {
const ce = node.properties.logicalProperties.cardinalityEstimate[0].ce;
assert.neq(ce, null, tojson(node));
return ce;
}
/**
* Get a very simplified version of a plan, which only includes nodeType and nesting structure.
*/
export function getPlanSkeleton(node, options = {}) {
const {extraKeepKeys = [], keepKeysDeep = [], printFilter = false, printLogicalCE = false} =
options;
const keepKeys = [
'nodeType',
'queryPlanner',
'winningPlan',
'queryPlan',
'child',
'children',
'leftChild',
'rightChild',
].concat(extraKeepKeys);
if (Array.isArray(node)) {
return node.map(n => getPlanSkeleton(n, options));
} else if (node === null || typeof node !== 'object') {
return node;
} else {
return Object.fromEntries(
Object.keys(node)
.filter(key => (keepKeys.includes(key) || keepKeysDeep.includes(key)))
.map(key => {
if (key === 'interval') {
return [key, prettyInterval(node[key])];
} else if (key === 'filter' && printFilter) {
return [key, prettyExpression(node[key])];
} else if (key === "properties" && printLogicalCE) {
return ["logicalCE", extractLogicalCEFromNode(node)];
} else if (keepKeysDeep.includes(key)) {
return [key, node[key]];
} else {
return [key, getPlanSkeleton(node[key], options)];
}
}));
}
}
/**
* Recur into every object and array; return any subtree that matches 'predicate'.
* Only calls 'predicate' on objects: not arrays or scalars.
*
* This is completely ignorant of the structure of a query: for example if there
* are literals match the predicate, it will also match those.
*/
export function findSubtrees(tree, predicate) {
let result = [];
const visit = subtree => {
if (typeof subtree === 'object' && subtree != null) {
if (Array.isArray(subtree)) {
for (const child of subtree) {
visit(child);
}
} else {
if (predicate(subtree)) {
result.push(subtree);
}
for (const key of Object.keys(subtree)) {
visit(subtree[key]);
}
}
}
};
visit(tree);
return result;
}
export function printBound(bound) {
if (!Array.isArray(bound.bound)) {
return [false, ""];
}
let result = "";
let first = true;
for (const element of bound.bound) {
if (element.nodeType !== "Const") {
return [false, ""];
}
result += tojson(element.value);
if (first) {
first = false;
} else {
result += " | ";
}
}
return [true, result];
}
export function prettyInterval(compoundInterval) {
// Takes an array of intervals, each one applying to one component of a compound index key.
// Try to format it as a string.
// If either bound is not Constant, return the original JSON unchanged.
const lowBound = compoundInterval.lowBound;
const highBound = compoundInterval.highBound;
const lowInclusive = lowBound.inclusive;
const highInclusive = highBound.inclusive;
assert.eq(typeof lowInclusive, 'boolean');
assert.eq(typeof highInclusive, 'boolean');
let result = '';
{
const res = printBound(lowBound);
if (!res[0]) {
return compoundInterval;
}
result += lowInclusive ? '[ ' : '( ';
result += res[1];
}
result += ", ";
{
const res = printBound(highBound);
if (!res[0]) {
return compoundInterval;
}
result += res[1];
result += highInclusive ? ' ]' : ' )';
}
return result.trim();
}
export function prettyExpression(expr) {
switch (expr.nodeType) {
case 'Variable':
return expr.name;
case 'Const':
return tojson(expr.value);
case 'FunctionCall':
return `${expr.name}(${expr.arguments.map(a => prettyExpression(a)).join(', ')})`;
case 'If': {
const if_ = prettyExpression(expr.condition);
const then_ = prettyExpression(expr.then);
const else_ = prettyExpression(expr.else);
return `if ${if_} then ${then_} else ${else_}`;
}
case 'Let': {
const x = expr.variable;
const b = prettyExpression(expr.bind);
const e = prettyExpression(expr.expression);
return `let ${x} = ${b} in ${e}`;
}
case 'LambdaAbstraction': {
return `(${expr.variable} -> ${prettyExpression(expr.input)})`;
}
case 'BinaryOp': {
const left = prettyExpression(expr.left);
const right = prettyExpression(expr.right);
const op = prettyOp(expr.op);
return `(${left} ${op} ${right})`;
}
case 'UnaryOp': {
const op = prettyOp(expr.op);
const input = prettyExpression(expr.input);
return `(${op} ${input})`;
}
default:
return tojson(expr);
}
}
export function prettyOp(op) {
// See src/mongo/db/query/optimizer/syntax/syntax.h, PATHSYNTAX_OPNAMES.
switch (op) {
/* comparison operations */
case 'Eq':
return '==';
case 'EqMember':
return 'in';
case 'Neq':
return '!=';
case 'Gt':
return '>';
case 'Gte':
return '>=';
case 'Lt':
return '<';
case 'Lte':
return '<=';
case 'Cmp3w':
return '<=>';
/* binary operations */
case 'Add':
return '+';
case 'Sub':
return '-';
case 'Mult':
return '*';
case 'Div':
return '/';
/* unary operations */
case 'Neg':
return '-';
/* logical operations */
case 'And':
return 'and';
case 'Or':
return 'or';
case 'Not':
return 'not';
default:
return op;
}
}
/**
* Helper function to remove UUIDs of collections in the supplied database from a V1 or V2 optimizer
* explain.
*/
export function removeUUIDsFromExplain(db, explain) {
const listCollsRes = db.runCommand({listCollections: 1}).cursor.firstBatch;
let plan = explain.queryPlanner.winningPlan.queryPlan.plan.toString();
for (let entry of listCollsRes) {
const uuidStr = entry.info.uuid.toString().slice(6).slice(0, -2);
plan = plan.replaceAll(uuidStr, "");
}
return plan;
}
export function navigateToPath(doc, path) {
let result = doc;
let components = path.split(".");
try {
for (; components.length > 0; components = components.slice(1)) {
assert(result.hasOwnProperty(components[0]));
result = result[components[0]];
}
return result;
} catch (e) {
const field = components[0];
const suffix = components.join('.');
jsTestLog(`Error navigating to path '${path}'\n` +
`because the suffix '${suffix}' does not exist,\n` +
`because the field '${field}' does not exist in this subtree:`);
printjson(result);
jsTestLog("The entire tree was: ");
printjson(doc);
throw e;
}
}
export function navigateToPlanPath(doc, path) {
return navigateToPath(doc, "queryPlanner.winningPlan.queryPlan." + path);
}
export function navigateToRootNode(doc) {
return navigateToPath(doc, "queryPlanner.winningPlan.queryPlan");
}
export function assertValueOnPathFn(value, doc, path, fn) {
try {
assert.eq(value, fn(doc, path));
} catch (e) {
jsTestLog("Assertion error.");
printjson(doc);
throw e;
}
}
export function assertValueOnPath(value, doc, path) {
assertValueOnPathFn(value, doc, path, navigateToPath);
}
export function assertValueOnPlanPath(value, doc, path) {
assertValueOnPathFn(value, doc, path, navigateToPlanPath);
}
export function runWithFastPathsDisabled(fn) {
const disableFastPath = [{key: "internalCascadesOptimizerDisableFastPath", value: true}];
return runWithParams(disableFastPath, fn);
}
export function runWithParams(keyValPairs, fn) {
let prevVals = [];
try {
for (let i = 0; i < keyValPairs.length; i++) {
const flag = keyValPairs[i].key;
const valIn = keyValPairs[i].value;
const val = (typeof valIn === 'object') ? JSON.stringify(valIn) : valIn;
let getParamObj = {};
getParamObj["getParameter"] = 1;
getParamObj[flag] = 1;
const prevVal = db.adminCommand(getParamObj);
prevVals.push(prevVal[flag]);
let setParamObj = {};
setParamObj["setParameter"] = 1;
setParamObj[flag] = val;
assert.commandWorked(db.adminCommand(setParamObj));
}
return fn();
} finally {
for (let i = 0; i < keyValPairs.length; i++) {
const flag = keyValPairs[i].key;
let setParamObj = {};
setParamObj["setParameter"] = 1;
setParamObj[flag] = prevVals[i];
assert.commandWorked(db.adminCommand(setParamObj));
}
}
}
export function round2(n) {
return (Math.round(n * 100) / 100);
}
/**
* Force cardinality estimation mode: "histogram", "heuristic", or "sampling". We need to force the
* use of the new optimizer.
*/
export function forceCE(mode) {
assert.commandWorked(
db.adminCommand({setParameter: 1, internalQueryFrameworkControl: "forceBonsai"}));
assert.commandWorked(
db.adminCommand({setParameter: 1, internalQueryCardinalityEstimatorMode: mode}));
}