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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
/**
* 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 "mongo/db/query/canonical_query.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/commands/test_commands_enabled.h"
#include "mongo/db/cst/cst_parser.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/matcher/expression_array.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/query/canonical_query_encoder.h"
#include "mongo/db/query/collation/collator_factory_interface.h"
#include "mongo/db/query/indexability.h"
#include "mongo/db/query/projection_parser.h"
#include "mongo/db/query/query_planner_common.h"
namespace mongo {
namespace {
bool parsingCanProduceNoopMatchNodes(const ExtensionsCallback& extensionsCallback,
MatchExpressionParser::AllowedFeatureSet allowedFeatures) {
return extensionsCallback.hasNoopExtensions() &&
(allowedFeatures & MatchExpressionParser::AllowedFeatures::kText ||
allowedFeatures & MatchExpressionParser::AllowedFeatures::kJavascript);
}
} // namespace
// static
StatusWith<std::unique_ptr<CanonicalQuery>> CanonicalQuery::canonicalize(
OperationContext* opCtx,
const QueryMessage& qm,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
const ExtensionsCallback& extensionsCallback,
MatchExpressionParser::AllowedFeatureSet allowedFeatures) {
bool explain = false;
// Make FindCommandRequest.
auto status = query_request_helper::fromLegacyQueryMessage(qm, &explain);
if (!status.isOK()) {
return status.getStatus();
}
return CanonicalQuery::canonicalize(
opCtx, std::move(status.getValue()), explain, expCtx, extensionsCallback, allowedFeatures);
}
// static
StatusWith<std::unique_ptr<CanonicalQuery>> CanonicalQuery::canonicalize(
OperationContext* opCtx,
std::unique_ptr<FindCommandRequest> findCommand,
bool explain,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
const ExtensionsCallback& extensionsCallback,
MatchExpressionParser::AllowedFeatureSet allowedFeatures,
const ProjectionPolicies& projectionPolicies) {
auto status = query_request_helper::validateFindCommandRequest(*findCommand);
if (!status.isOK()) {
return status;
}
std::unique_ptr<CollatorInterface> collator;
if (!findCommand->getCollation().isEmpty()) {
auto statusWithCollator = CollatorFactoryInterface::get(opCtx->getServiceContext())
->makeFromBSON(findCommand->getCollation());
if (!statusWithCollator.isOK()) {
return statusWithCollator.getStatus();
}
collator = std::move(statusWithCollator.getValue());
}
// Make MatchExpression.
boost::intrusive_ptr<ExpressionContext> newExpCtx;
if (!expCtx.get()) {
invariant(findCommand->getNamespaceOrUUID().nss());
newExpCtx = make_intrusive<ExpressionContext>(opCtx,
std::move(collator),
*findCommand->getNamespaceOrUUID().nss(),
findCommand->getLegacyRuntimeConstants(),
findCommand->getLet());
} else {
newExpCtx = expCtx;
// A collator can enter through both the FindCommandRequest and ExpressionContext arguments.
// This invariant ensures that both collators are the same because downstream we
// pull the collator from only one of the ExpressionContext carrier.
if (collator.get() && expCtx->getCollator()) {
invariant(CollatorInterface::collatorsMatch(collator.get(), expCtx->getCollator()));
}
}
// Make the CQ we'll hopefully return.
std::unique_ptr<CanonicalQuery> cq(new CanonicalQuery());
cq->setExplain(explain);
StatusWithMatchExpression statusWithMatcher = [&]() -> StatusWithMatchExpression {
if (getTestCommandsEnabled() && internalQueryEnableCSTParser.load()) {
try {
return cst::parseToMatchExpression(
findCommand->getFilter(), newExpCtx, extensionsCallback);
} catch (const DBException& ex) {
return ex.toStatus();
}
} else {
return MatchExpressionParser::parse(
findCommand->getFilter(), newExpCtx, extensionsCallback, allowedFeatures);
}
}();
if (!statusWithMatcher.isOK()) {
return statusWithMatcher.getStatus();
}
std::unique_ptr<MatchExpression> me = std::move(statusWithMatcher.getValue());
Status initStatus =
cq->init(opCtx,
std::move(newExpCtx),
std::move(findCommand),
parsingCanProduceNoopMatchNodes(extensionsCallback, allowedFeatures),
std::move(me),
projectionPolicies);
if (!initStatus.isOK()) {
return initStatus;
}
return std::move(cq);
}
// static
StatusWith<std::unique_ptr<CanonicalQuery>> CanonicalQuery::canonicalize(
OperationContext* opCtx, const CanonicalQuery& baseQuery, MatchExpression* root) {
auto findCommand = std::make_unique<FindCommandRequest>(baseQuery.nss());
BSONObjBuilder builder;
root->serialize(&builder, true);
findCommand->setFilter(builder.obj());
findCommand->setProjection(baseQuery.getFindCommandRequest().getProjection().getOwned());
findCommand->setSort(baseQuery.getFindCommandRequest().getSort().getOwned());
findCommand->setCollation(baseQuery.getFindCommandRequest().getCollation().getOwned());
auto status = query_request_helper::validateFindCommandRequest(*findCommand);
if (!status.isOK()) {
return status;
}
// Make the CQ we'll hopefully return.
std::unique_ptr<CanonicalQuery> cq(new CanonicalQuery());
cq->setExplain(baseQuery.getExplain());
Status initStatus = cq->init(opCtx,
baseQuery.getExpCtx(),
std::move(findCommand),
baseQuery.canHaveNoopMatchNodes(),
root->shallowClone(),
ProjectionPolicies::findProjectionPolicies());
if (!initStatus.isOK()) {
return initStatus;
}
return std::move(cq);
}
Status CanonicalQuery::init(OperationContext* opCtx,
boost::intrusive_ptr<ExpressionContext> expCtx,
std::unique_ptr<FindCommandRequest> findCommand,
bool canHaveNoopMatchNodes,
std::unique_ptr<MatchExpression> root,
const ProjectionPolicies& projectionPolicies) {
_expCtx = expCtx;
_findCommand = std::move(findCommand);
_canHaveNoopMatchNodes = canHaveNoopMatchNodes;
// Normalize and validate tree.
_root = MatchExpression::normalize(std::move(root));
auto validStatus = isValid(_root.get(), *_findCommand);
if (!validStatus.isOK()) {
return validStatus.getStatus();
}
auto unavailableMetadata = validStatus.getValue();
// Validate the projection if there is one.
if (!_findCommand->getProjection().isEmpty()) {
try {
_proj.emplace(projection_ast::parse(expCtx,
_findCommand->getProjection(),
_root.get(),
_findCommand->getFilter(),
projectionPolicies));
// Fail if any of the projection's dependencies are unavailable.
DepsTracker{unavailableMetadata}.requestMetadata(_proj->metadataDeps());
} catch (const DBException& e) {
return e.toStatus();
}
_metadataDeps = _proj->metadataDeps();
}
if (_proj && _proj->metadataDeps()[DocumentMetadataFields::kSortKey] &&
_findCommand->getSort().isEmpty()) {
return Status(ErrorCodes::BadValue, "cannot use sortKey $meta projection without a sort");
}
// If there is a sort, parse it and add any metadata dependencies it induces.
try {
initSortPattern(unavailableMetadata);
} catch (const DBException& ex) {
return ex.toStatus();
}
// If the 'returnKey' option is set, then the plan should produce index key metadata.
if (_findCommand->getReturnKey()) {
_metadataDeps.set(DocumentMetadataFields::kIndexKey);
}
return Status::OK();
}
void CanonicalQuery::initSortPattern(QueryMetadataBitSet unavailableMetadata) {
if (_findCommand->getSort().isEmpty()) {
return;
}
// A $natural sort is really a hint, and should be handled as such. Furthermore, the downstream
// sort handling code may not expect a $natural sort.
//
// We have already validated that if there is a $natural sort and a hint, that the hint
// also specifies $natural with the same direction. Therefore, it is safe to clear the $natural
// sort and rewrite it as a $natural hint.
if (_findCommand->getSort()[query_request_helper::kNaturalSortField]) {
_findCommand->setHint(_findCommand->getSort().getOwned());
_findCommand->setSort(BSONObj{});
}
if (getTestCommandsEnabled() && internalQueryEnableCSTParser.load()) {
_sortPattern = cst::parseToSortPattern(_findCommand->getSort(), _expCtx);
} else {
_sortPattern = SortPattern{_findCommand->getSort(), _expCtx};
}
_metadataDeps |= _sortPattern->metadataDeps(unavailableMetadata);
// If the results of this query might have to be merged on a remote node, then that node might
// need the sort key metadata. Request that the plan generates this metadata.
if (_expCtx->needsMerge) {
_metadataDeps.set(DocumentMetadataFields::kSortKey);
}
}
void CanonicalQuery::setCollator(std::unique_ptr<CollatorInterface> collator) {
auto collatorRaw = collator.get();
// We must give the ExpressionContext the same collator.
_expCtx->setCollator(std::move(collator));
// The collator associated with the match expression tree is now invalid, since we have reset
// the collator owned by the ExpressionContext.
_root->setCollator(collatorRaw);
}
// static
bool CanonicalQuery::isSimpleIdQuery(const BSONObj& query) {
bool hasID = false;
BSONObjIterator it(query);
while (it.more()) {
BSONElement elt = it.next();
if (elt.fieldNameStringData() == "_id") {
// Verify that the query on _id is a simple equality.
hasID = true;
if (elt.type() == Object) {
// If the value is an object, it can't have a query operator
// (must be a literal object match).
if (elt.Obj().firstElementFieldName()[0] == '$') {
return false;
}
} else if (!Indexability::isExactBoundsGenerating(elt)) {
// The _id fild cannot be something like { _id : { $gt : ...
// But it can be BinData.
return false;
}
} else {
return false;
}
}
return hasID;
}
size_t CanonicalQuery::countNodes(const MatchExpression* root, MatchExpression::MatchType type) {
size_t sum = 0;
if (type == root->matchType()) {
sum = 1;
}
for (size_t i = 0; i < root->numChildren(); ++i) {
sum += countNodes(root->getChild(i), type);
}
return sum;
}
/**
* Does 'root' have a subtree of type 'subtreeType' with a node of type 'childType' inside?
*/
bool hasNodeInSubtree(MatchExpression* root,
MatchExpression::MatchType childType,
MatchExpression::MatchType subtreeType) {
if (subtreeType == root->matchType()) {
return QueryPlannerCommon::hasNode(root, childType);
}
for (size_t i = 0; i < root->numChildren(); ++i) {
if (hasNodeInSubtree(root->getChild(i), childType, subtreeType)) {
return true;
}
}
return false;
}
StatusWith<QueryMetadataBitSet> CanonicalQuery::isValid(MatchExpression* root,
const FindCommandRequest& findCommand) {
QueryMetadataBitSet unavailableMetadata{};
// There can only be one TEXT. If there is a TEXT, it cannot appear inside a NOR.
//
// Note that the query grammar (as enforced by the MatchExpression parser) forbids TEXT
// inside of value-expression clauses like NOT, so we don't check those here.
size_t numText = countNodes(root, MatchExpression::TEXT);
if (numText > 1) {
return Status(ErrorCodes::BadValue, "Too many text expressions");
} else if (1 == numText) {
if (hasNodeInSubtree(root, MatchExpression::TEXT, MatchExpression::NOR)) {
return Status(ErrorCodes::BadValue, "text expression not allowed in nor");
}
} else {
// Text metadata is not available.
unavailableMetadata.set(DocumentMetadataFields::kTextScore);
}
// There can only be one NEAR. If there is a NEAR, it must be either the root or the root
// must be an AND and its child must be a NEAR.
size_t numGeoNear = countNodes(root, MatchExpression::GEO_NEAR);
if (numGeoNear > 1) {
return Status(ErrorCodes::BadValue, "Too many geoNear expressions");
} else if (1 == numGeoNear) {
bool topLevel = false;
if (MatchExpression::GEO_NEAR == root->matchType()) {
topLevel = true;
} else if (MatchExpression::AND == root->matchType()) {
for (size_t i = 0; i < root->numChildren(); ++i) {
if (MatchExpression::GEO_NEAR == root->getChild(i)->matchType()) {
topLevel = true;
break;
}
}
}
if (!topLevel) {
return Status(ErrorCodes::BadValue, "geoNear must be top-level expr");
}
} else {
// Geo distance and geo point metadata are unavailable.
unavailableMetadata |= DepsTracker::kAllGeoNearData;
}
const BSONObj& sortObj = findCommand.getSort();
BSONElement sortNaturalElt = sortObj["$natural"];
const BSONObj& hintObj = findCommand.getHint();
BSONElement hintNaturalElt = hintObj["$natural"];
if (sortNaturalElt && sortObj.nFields() != 1) {
return Status(ErrorCodes::BadValue,
str::stream() << "Cannot include '$natural' in compound sort: " << sortObj);
}
if (hintNaturalElt && hintObj.nFields() != 1) {
return Status(ErrorCodes::BadValue,
str::stream() << "Cannot include '$natural' in compound hint: " << hintObj);
}
// NEAR cannot have a $natural sort or $natural hint.
if (numGeoNear > 0) {
if (sortNaturalElt) {
return Status(ErrorCodes::BadValue,
"geoNear expression not allowed with $natural sort order");
}
if (hintNaturalElt) {
return Status(ErrorCodes::BadValue,
"geoNear expression not allowed with $natural hint");
}
}
// TEXT and NEAR cannot both be in the query.
if (numText > 0 && numGeoNear > 0) {
return Status(ErrorCodes::BadValue, "text and geoNear not allowed in same query");
}
// TEXT and {$natural: ...} sort order cannot both be in the query.
if (numText > 0 && sortNaturalElt) {
return Status(ErrorCodes::BadValue, "text expression not allowed with $natural sort order");
}
// TEXT and hint cannot both be in the query.
if (numText > 0 && !hintObj.isEmpty()) {
return Status(ErrorCodes::BadValue, "text and hint not allowed in same query");
}
// TEXT and tailable are incompatible.
if (numText > 0 && findCommand.getTailable()) {
return Status(ErrorCodes::BadValue, "text and tailable cursor not allowed in same query");
}
// NEAR and tailable are incompatible.
if (numGeoNear > 0 && findCommand.getTailable()) {
return Status(ErrorCodes::BadValue,
"Tailable cursors and geo $near cannot be used together");
}
// $natural sort order must agree with hint.
if (sortNaturalElt) {
if (!hintObj.isEmpty() && !hintNaturalElt) {
return Status(ErrorCodes::BadValue, "index hint not allowed with $natural sort order");
}
if (hintNaturalElt) {
if (hintNaturalElt.numberInt() != sortNaturalElt.numberInt()) {
return Status(ErrorCodes::BadValue,
"$natural hint must be in the same direction as $natural sort order");
}
}
}
return unavailableMetadata;
}
int CanonicalQuery::getOptions() const {
int options = 0;
if (_findCommand->getTailable()) {
options |= QueryOption_CursorTailable;
}
if (_findCommand->getAwaitData()) {
options |= QueryOption_AwaitData;
}
if (_findCommand->getNoCursorTimeout()) {
options |= QueryOption_NoCursorTimeout;
}
if (_findCommand->getAllowPartialResults()) {
options |= QueryOption_PartialResults;
}
return options;
}
std::string CanonicalQuery::toString() const {
str::stream ss;
ss << "ns=" << _findCommand->getNamespaceOrUUID().nss().value_or(NamespaceString()).ns();
if (_findCommand->getBatchSize()) {
ss << " batchSize=" << *_findCommand->getBatchSize();
}
if (_findCommand->getLimit()) {
ss << " limit=" << *_findCommand->getLimit();
}
if (_findCommand->getSkip()) {
ss << " skip=" << *_findCommand->getSkip();
}
if (_findCommand->getNtoreturn()) {
ss << " ntoreturn=" << *_findCommand->getNtoreturn() << '\n';
}
// The expression tree puts an endl on for us.
ss << "Tree: " << _root->debugString();
ss << "Sort: " << _findCommand->getSort().toString() << '\n';
ss << "Proj: " << _findCommand->getProjection().toString() << '\n';
if (!_findCommand->getCollation().isEmpty()) {
ss << "Collation: " << _findCommand->getCollation().toString() << '\n';
}
return ss;
}
std::string CanonicalQuery::toStringShort() const {
str::stream ss;
ss << "ns: " << _findCommand->getNamespaceOrUUID().nss().value_or(NamespaceString()).ns()
<< " query: " << _findCommand->getFilter().toString()
<< " sort: " << _findCommand->getSort().toString()
<< " projection: " << _findCommand->getProjection().toString();
if (!_findCommand->getCollation().isEmpty()) {
ss << " collation: " << _findCommand->getCollation().toString();
}
if (_findCommand->getBatchSize()) {
ss << " batchSize: " << *_findCommand->getBatchSize();
}
if (_findCommand->getLimit()) {
ss << " limit: " << *_findCommand->getLimit();
}
if (_findCommand->getSkip()) {
ss << " skip: " << *_findCommand->getSkip();
}
if (_findCommand->getNtoreturn()) {
ss << " ntoreturn=" << *_findCommand->getNtoreturn();
}
return ss;
}
CanonicalQuery::QueryShapeString CanonicalQuery::encodeKey() const {
return canonical_query_encoder::encode(*this);
}
} // namespace mongo
|