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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery
#include "mongo/platform/basic.h"
#include <memory>
#include "mongo/base/init.h"
#include "mongo/db/auth/action_set.h"
#include "mongo/db/auth/action_type.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/catalog/database.h"
#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/commands/test_commands_enabled.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/exec/and_hash.h"
#include "mongo/db/exec/and_sorted.h"
#include "mongo/db/exec/collection_scan.h"
#include "mongo/db/exec/delete.h"
#include "mongo/db/exec/fetch.h"
#include "mongo/db/exec/index_scan.h"
#include "mongo/db/exec/limit.h"
#include "mongo/db/exec/merge_sort.h"
#include "mongo/db/exec/or.h"
#include "mongo/db/exec/skip.h"
#include "mongo/db/exec/sort.h"
#include "mongo/db/exec/text.h"
#include "mongo/db/exec/working_set_common.h"
#include "mongo/db/index/fts_access_method.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/matcher/expression_parser.h"
#include "mongo/db/matcher/expression_text_base.h"
#include "mongo/db/matcher/extensions_callback_real.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/query/plan_executor.h"
#include "mongo/logv2/log.h"
namespace mongo {
using std::string;
using std::unique_ptr;
using std::vector;
/**
* A command for manually constructing a query tree and running it.
*
* db.runCommand({stageDebug: {collection: collname, plan: rootNode}})
*
* The value of the filter field is a BSONObj that specifies values that fields must have. What
* you'd pass to a matcher.
*
* Leaf Nodes:
*
* node -> {ixscan: {filter: {FILTER},
* args: {indexKeyPattern: kpObj, start: startObj,
* stop: stopObj, endInclusive: true/false, direction: -1/1,
* limit: int}}}
* node -> {cscan: {filter: {filter}, args: {direction: -1/1}}}
* TODO: language for text.
* node -> {text: {filter: {filter}, args: {search: "searchstr"}}}
*
* Internal Nodes:
*
* node -> {andHash: {args: { nodes: [node, node]}}}
* node -> {andSorted: {args: { nodes: [node, node]}}}
* node -> {or: {filter: {filter}, args: { dedup:bool, nodes:[node, node]}}}
* node -> {fetch: {filter: {filter}, args: {node: node}}}
* node -> {limit: {args: {node: node, num: posint}}}
* node -> {skip: {args: {node: node, num: posint}}}
* node -> {sort: {args: {node: node, pattern: objWithSortCriterion }}}
* node -> {mergeSort: {args: {nodes: [node, node], pattern: objWithSortCriterion}}}
* node -> {delete: {args: {node: node, isMulti: bool}}}
*
* Forthcoming Nodes:
*
* node -> {dedup: {filter: {filter}, args: {node: node, field: field}}}
* node -> {unwind: {filter: filter}, args: {node: node, field: field}}
*/
class StageDebugCmd : public BasicCommand {
public:
StageDebugCmd() : BasicCommand("stageDebug") {}
virtual bool supportsWriteConcern(const BSONObj& cmd) const override {
return false;
}
AllowedOnSecondary secondaryAllowed(ServiceContext*) const override {
return AllowedOnSecondary::kNever;
}
std::string help() const override {
return {};
}
virtual void addRequiredPrivileges(const std::string& dbname,
const BSONObj& cmdObj,
std::vector<Privilege>* out) const {
// Command is testing-only, and can only be enabled at command line. Hence, no auth
// check needed.
}
bool run(OperationContext* opCtx,
const string& dbname,
const BSONObj& cmdObj,
BSONObjBuilder& result) {
BSONElement argElt = cmdObj["stageDebug"];
if (argElt.eoo() || !argElt.isABSONObj()) {
return false;
}
BSONObj argObj = argElt.Obj();
// Pull out the collection name.
BSONElement collElt = argObj["collection"];
if (collElt.eoo() || (String != collElt.type())) {
return false;
}
const NamespaceString nss(dbname, collElt.String());
uassert(ErrorCodes::InvalidNamespace,
str::stream() << nss.toString() << " is not a valid namespace",
nss.isValid());
auto expCtx = make_intrusive<ExpressionContext>(
opCtx, std::unique_ptr<CollatorInterface>(nullptr), nss);
// Need a context to get the actual Collection*
// TODO A write lock is currently taken here to accommodate stages that perform writes
// (e.g. DeleteStage). This should be changed to use a read lock for read-only
// execution trees.
AutoGetCollection autoColl(opCtx, nss, MODE_IX);
// Make sure the collection is valid.
Collection* collection = autoColl.getCollection();
uassert(ErrorCodes::NamespaceNotFound,
str::stream() << "Couldn't find collection " << nss.ns(),
collection);
// Pull out the plan
BSONElement planElt = argObj["plan"];
if (planElt.eoo() || !planElt.isABSONObj()) {
return false;
}
BSONObj planObj = planElt.Obj();
// Parse the plan into these.
std::vector<std::unique_ptr<MatchExpression>> exprs;
unique_ptr<WorkingSet> ws(new WorkingSet());
std::unique_ptr<PlanStage> userRoot{
parseQuery(expCtx, collection, planObj, ws.get(), nss, &exprs)};
uassert(16911, "Couldn't parse plan from " + cmdObj.toString(), nullptr != userRoot);
// Add a fetch at the top for the user so we can get obj back for sure.
unique_ptr<PlanStage> rootFetch = std::make_unique<FetchStage>(
expCtx.get(), ws.get(), std::move(userRoot), nullptr, collection);
auto statusWithPlanExecutor = PlanExecutor::make(expCtx,
std::move(ws),
std::move(rootFetch),
collection,
PlanYieldPolicy::YieldPolicy::YIELD_AUTO);
fassert(28536, statusWithPlanExecutor.getStatus());
auto exec = std::move(statusWithPlanExecutor.getValue());
BSONArrayBuilder resultBuilder(result.subarrayStart("results"));
BSONObj obj;
PlanExecutor::ExecState state;
while (PlanExecutor::ADVANCED == (state = exec->getNext(&obj, nullptr))) {
resultBuilder.append(obj);
}
resultBuilder.done();
return true;
}
PlanStage* parseQuery(const boost::intrusive_ptr<ExpressionContext>& expCtx,
Collection* collection,
BSONObj obj,
WorkingSet* workingSet,
const NamespaceString& nss,
std::vector<std::unique_ptr<MatchExpression>>* exprs) {
OperationContext* opCtx = expCtx->opCtx;
BSONElement firstElt = obj.firstElement();
if (!firstElt.isABSONObj()) {
return nullptr;
}
BSONObj paramObj = firstElt.Obj();
MatchExpression* matcher = nullptr;
BSONObj nodeArgs;
// Every node has these two fields.
const string filterTag = "filter";
const string argsTag = "args";
BSONObjIterator it(paramObj);
while (it.more()) {
BSONElement e = it.next();
if (!e.isABSONObj()) {
return nullptr;
}
BSONObj argObj = e.Obj();
if (filterTag == e.fieldName()) {
auto statusWithMatcher =
MatchExpressionParser::parse(argObj,
expCtx,
ExtensionsCallbackReal(opCtx, &collection->ns()),
MatchExpressionParser::kAllowAllSpecialFeatures);
if (!statusWithMatcher.isOK()) {
return nullptr;
}
std::unique_ptr<MatchExpression> me = std::move(statusWithMatcher.getValue());
// exprs is what will wind up deleting this.
matcher = me.get();
verify(nullptr != matcher);
exprs->push_back(std::move(me));
} else if (argsTag == e.fieldName()) {
nodeArgs = argObj;
} else {
uasserted(16910,
"Unknown fieldname " + string(e.fieldName()) + " in query node " +
obj.toString());
return nullptr;
}
}
string nodeName = firstElt.fieldName();
if ("ixscan" == nodeName) {
const IndexDescriptor* desc;
if (BSONElement keyPatternElement = nodeArgs["keyPattern"]) {
// This'll throw if it's not an obj but that's OK.
BSONObj keyPatternObj = keyPatternElement.Obj();
std::vector<const IndexDescriptor*> indexes;
collection->getIndexCatalog()->findIndexesByKeyPattern(
opCtx, keyPatternObj, false, &indexes);
uassert(16890,
str::stream() << "Can't find index: " << keyPatternObj,
!indexes.empty());
uassert(ErrorCodes::AmbiguousIndexKeyPattern,
str::stream() << indexes.size()
<< " matching indexes for key pattern: " << keyPatternObj
<< ". Conflicting indexes: " << indexes[0]->infoObj() << ", "
<< indexes[1]->infoObj(),
indexes.size() == 1);
desc = indexes[0];
} else {
uassert(40306,
str::stream() << "Index 'name' must be a string in: " << nodeArgs,
nodeArgs["name"].type() == BSONType::String);
StringData name = nodeArgs["name"].valueStringData();
desc = collection->getIndexCatalog()->findIndexByName(opCtx, name);
uassert(40223, str::stream() << "Can't find index: " << name.toString(), desc);
}
IndexScanParams params(opCtx, desc);
params.bounds.isSimpleRange = true;
params.bounds.startKey = BSONObj::stripFieldNames(nodeArgs["startKey"].Obj());
params.bounds.endKey = BSONObj::stripFieldNames(nodeArgs["endKey"].Obj());
params.bounds.boundInclusion = IndexBounds::makeBoundInclusionFromBoundBools(
nodeArgs["startKeyInclusive"].Bool(), nodeArgs["endKeyInclusive"].Bool());
params.direction = nodeArgs["direction"].numberInt();
params.shouldDedup = desc->getEntry()->isMultikey();
return new IndexScan(expCtx.get(), collection, params, workingSet, matcher);
} else if ("andHash" == nodeName) {
uassert(
16921, "Nodes argument must be provided to AND", nodeArgs["nodes"].isABSONObj());
auto andStage = std::make_unique<AndHashStage>(expCtx.get(), workingSet);
int nodesAdded = 0;
BSONObjIterator it(nodeArgs["nodes"].Obj());
while (it.more()) {
BSONElement e = it.next();
uassert(16922, "node of AND isn't an obj?: " + e.toString(), e.isABSONObj());
std::unique_ptr<PlanStage> subNode{
parseQuery(expCtx, collection, e.Obj(), workingSet, nss, exprs)};
uassert(16923,
"Can't parse sub-node of AND: " + e.Obj().toString(),
nullptr != subNode);
andStage->addChild(std::move(subNode));
++nodesAdded;
}
uassert(16927, "AND requires more than one child", nodesAdded >= 2);
return andStage.release();
} else if ("andSorted" == nodeName) {
uassert(
16924, "Nodes argument must be provided to AND", nodeArgs["nodes"].isABSONObj());
auto andStage = std::make_unique<AndSortedStage>(expCtx.get(), workingSet);
int nodesAdded = 0;
BSONObjIterator it(nodeArgs["nodes"].Obj());
while (it.more()) {
BSONElement e = it.next();
uassert(16925, "node of AND isn't an obj?: " + e.toString(), e.isABSONObj());
std::unique_ptr<PlanStage> subNode{
parseQuery(expCtx, collection, e.Obj(), workingSet, nss, exprs)};
uassert(16926,
"Can't parse sub-node of AND: " + e.Obj().toString(),
nullptr != subNode);
andStage->addChild(std::move(subNode));
++nodesAdded;
}
uassert(16928, "AND requires more than one child", nodesAdded >= 2);
return andStage.release();
} else if ("or" == nodeName) {
uassert(
16934, "Nodes argument must be provided to AND", nodeArgs["nodes"].isABSONObj());
uassert(16935, "Dedup argument must be provided to OR", !nodeArgs["dedup"].eoo());
BSONObjIterator it(nodeArgs["nodes"].Obj());
auto orStage = std::make_unique<OrStage>(
expCtx.get(), workingSet, nodeArgs["dedup"].Bool(), matcher);
while (it.more()) {
BSONElement e = it.next();
if (!e.isABSONObj()) {
return nullptr;
}
std::unique_ptr<PlanStage> subNode{
parseQuery(expCtx, collection, e.Obj(), workingSet, nss, exprs)};
uassert(
16936, "Can't parse sub-node of OR: " + e.Obj().toString(), nullptr != subNode);
orStage->addChild(std::move(subNode));
}
return orStage.release();
} else if ("fetch" == nodeName) {
uassert(
16929, "Node argument must be provided to fetch", nodeArgs["node"].isABSONObj());
std::unique_ptr<PlanStage> subNode{
parseQuery(expCtx, collection, nodeArgs["node"].Obj(), workingSet, nss, exprs)};
uassert(28731,
"Can't parse sub-node of FETCH: " + nodeArgs["node"].Obj().toString(),
nullptr != subNode);
return new FetchStage(
expCtx.get(), workingSet, std::move(subNode), matcher, collection);
} else if ("limit" == nodeName) {
uassert(16937,
"Limit stage doesn't have a filter (put it on the child)",
nullptr == matcher);
uassert(
16930, "Node argument must be provided to limit", nodeArgs["node"].isABSONObj());
uassert(16931, "Num argument must be provided to limit", nodeArgs["num"].isNumber());
std::unique_ptr<PlanStage> subNode{
parseQuery(expCtx, collection, nodeArgs["node"].Obj(), workingSet, nss, exprs)};
uassert(28732,
"Can't parse sub-node of LIMIT: " + nodeArgs["node"].Obj().toString(),
nullptr != subNode);
return new LimitStage(
expCtx.get(), nodeArgs["num"].numberInt(), workingSet, std ::move(subNode));
} else if ("skip" == nodeName) {
uassert(16938,
"Skip stage doesn't have a filter (put it on the child)",
nullptr == matcher);
uassert(16932, "Node argument must be provided to skip", nodeArgs["node"].isABSONObj());
uassert(16933, "Num argument must be provided to skip", nodeArgs["num"].isNumber());
std::unique_ptr<PlanStage> subNode{
parseQuery(expCtx, collection, nodeArgs["node"].Obj(), workingSet, nss, exprs)};
uassert(28733,
"Can't parse sub-node of SKIP: " + nodeArgs["node"].Obj().toString(),
nullptr != subNode);
return new SkipStage(
expCtx.get(), nodeArgs["num"].numberInt(), workingSet, std::move(subNode));
} else if ("cscan" == nodeName) {
CollectionScanParams params;
// What direction?
uassert(16963,
"Direction argument must be specified and be a number",
nodeArgs["direction"].isNumber());
if (1 == nodeArgs["direction"].numberInt()) {
params.direction = CollectionScanParams::FORWARD;
} else {
params.direction = CollectionScanParams::BACKWARD;
}
return new CollectionScan(expCtx.get(), collection, params, workingSet, matcher);
} else if ("mergeSort" == nodeName) {
uassert(
16971, "Nodes argument must be provided to sort", nodeArgs["nodes"].isABSONObj());
uassert(16972,
"Pattern argument must be provided to sort",
nodeArgs["pattern"].isABSONObj());
MergeSortStageParams params;
params.pattern = nodeArgs["pattern"].Obj();
// Dedup is true by default.
auto mergeStage = std::make_unique<MergeSortStage>(expCtx.get(), params, workingSet);
BSONObjIterator it(nodeArgs["nodes"].Obj());
while (it.more()) {
BSONElement e = it.next();
uassert(16973, "node of mergeSort isn't an obj?: " + e.toString(), e.isABSONObj());
std::unique_ptr<PlanStage> subNode{
parseQuery(expCtx, collection, e.Obj(), workingSet, nss, exprs)};
uassert(16974,
"Can't parse sub-node of mergeSort: " + e.Obj().toString(),
nullptr != subNode);
mergeStage->addChild(std::move(subNode));
}
return mergeStage.release();
} else if ("text" == nodeName) {
string search = nodeArgs["search"].String();
vector<const IndexDescriptor*> idxMatches;
collection->getIndexCatalog()->findIndexByType(opCtx, "text", idxMatches);
uassert(17194, "Expected exactly one text index", idxMatches.size() == 1);
const IndexDescriptor* index = idxMatches[0];
const FTSAccessMethod* fam = dynamic_cast<const FTSAccessMethod*>(
collection->getIndexCatalog()->getEntry(index)->accessMethod());
invariant(fam);
TextStageParams params(fam->getSpec());
params.index = index;
// TODO: Deal with non-empty filters. This is a hack to put in covering information
// that can only be checked for equality. We ignore this now.
Status s = fam->getSpec().getIndexPrefix(BSONObj(), ¶ms.indexPrefix);
if (!s.isOK()) {
return nullptr;
}
params.spec = fam->getSpec();
params.query.setQuery(search);
params.query.setLanguage(fam->getSpec().defaultLanguage().str());
params.query.setCaseSensitive(TextMatchExpressionBase::kCaseSensitiveDefault);
params.query.setDiacriticSensitive(TextMatchExpressionBase::kDiacriticSensitiveDefault);
if (!params.query.parse(fam->getSpec().getTextIndexVersion()).isOK()) {
return nullptr;
}
return new TextStage(expCtx.get(), collection, params, workingSet, matcher);
} else if ("delete" == nodeName) {
uassert(18636,
"Delete stage doesn't have a filter (put it on the child)",
nullptr == matcher);
uassert(
18637, "node argument must be provided to delete", nodeArgs["node"].isABSONObj());
uassert(18638,
"isMulti argument must be provided to delete",
nodeArgs["isMulti"].type() == Bool);
PlanStage* subNode =
parseQuery(expCtx, collection, nodeArgs["node"].Obj(), workingSet, nss, exprs);
uassert(28734,
"Can't parse sub-node of DELETE: " + nodeArgs["node"].Obj().toString(),
nullptr != subNode);
auto params = std::make_unique<DeleteStageParams>();
params->isMulti = nodeArgs["isMulti"].Bool();
return new DeleteStage(
expCtx.get(), std::move(params), workingSet, collection, subNode);
} else {
return nullptr;
}
}
};
MONGO_REGISTER_TEST_COMMAND(StageDebugCmd);
} // namespace mongo
|