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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
/**
* 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_encoder.h"
#include <boost/iterator/transform_iterator.hpp>
#include "mongo/base/simple_string_data_comparator.h"
#include "mongo/db/matcher/expression_array.h"
#include "mongo/db/matcher/expression_geo.h"
#include "mongo/db/query/projection.h"
#include "mongo/logv2/log.h"
namespace mongo {
bool isQueryNegatingGTEorLTENull(const mongo::MatchExpression* tree) {
// If the query predicate is null, do not reuse the plan since empty arrays ([]) are
// encoded as 'null' in the index. Thus we cannot safely invert the index bounds since 'null'
// has special comparison semantics.
if (tree->matchType() != MatchExpression::NOT) {
return false;
}
const MatchExpression* child = tree->getChild(0);
switch (child->matchType()) {
case MatchExpression::GTE:
case MatchExpression::LTE:
return static_cast<const ComparisonMatchExpression*>(child)->getData().type() ==
BSONType::jstNULL;
default:
return false;
}
}
namespace {
// Delimiters for cache key encoding.
const char kEncodeChildrenBegin = '[';
const char kEncodeChildrenEnd = ']';
const char kEncodeChildrenSeparator = ',';
const char kEncodeCollationSection = '#';
const char kEncodeProjectionSection = '|';
const char kEncodeProjectionRequirementSeparator = '-';
const char kEncodeRegexFlagsSeparator = '/';
const char kEncodeSortSection = '~';
/**
* Encode user-provided string. Cache key delimiters seen in the
* user string are escaped with a backslash.
*/
void encodeUserString(StringData s, StringBuilder* keyBuilder) {
for (size_t i = 0; i < s.size(); ++i) {
char c = s[i];
switch (c) {
case kEncodeChildrenBegin:
case kEncodeChildrenEnd:
case kEncodeChildrenSeparator:
case kEncodeCollationSection:
case kEncodeProjectionSection:
case kEncodeProjectionRequirementSeparator:
case kEncodeRegexFlagsSeparator:
case kEncodeSortSection:
case '\\':
*keyBuilder << '\\';
// Fall through to default case.
default:
*keyBuilder << c;
}
}
}
/**
* String encoding of MatchExpression::MatchType.
*/
const char* encodeMatchType(MatchExpression::MatchType mt) {
switch (mt) {
case MatchExpression::AND:
return "an";
case MatchExpression::OR:
return "or";
case MatchExpression::NOR:
return "nr";
case MatchExpression::NOT:
return "nt";
case MatchExpression::ELEM_MATCH_OBJECT:
return "eo";
case MatchExpression::ELEM_MATCH_VALUE:
return "ev";
case MatchExpression::SIZE:
return "sz";
case MatchExpression::LTE:
return "le";
case MatchExpression::LT:
return "lt";
case MatchExpression::EQ:
return "eq";
case MatchExpression::GT:
return "gt";
case MatchExpression::GTE:
return "ge";
case MatchExpression::REGEX:
return "re";
case MatchExpression::MOD:
return "mo";
case MatchExpression::EXISTS:
return "ex";
case MatchExpression::MATCH_IN:
return "in";
case MatchExpression::TYPE_OPERATOR:
return "ty";
case MatchExpression::GEO:
return "go";
case MatchExpression::WHERE:
return "wh";
case MatchExpression::ALWAYS_FALSE:
return "af";
case MatchExpression::ALWAYS_TRUE:
return "at";
case MatchExpression::GEO_NEAR:
return "gn";
case MatchExpression::TEXT:
return "te";
case MatchExpression::BITS_ALL_SET:
return "ls";
case MatchExpression::BITS_ALL_CLEAR:
return "lc";
case MatchExpression::BITS_ANY_SET:
return "ys";
case MatchExpression::BITS_ANY_CLEAR:
return "yc";
case MatchExpression::EXPRESSION:
return "xp";
case MatchExpression::INTERNAL_EXPR_EQ:
return "eeq";
case MatchExpression::INTERNAL_EXPR_GT:
return "egt";
case MatchExpression::INTERNAL_EXPR_GTE:
return "ege";
case MatchExpression::INTERNAL_EXPR_LT:
return "elt";
case MatchExpression::INTERNAL_EXPR_LTE:
return "ele";
case MatchExpression::INTERNAL_SCHEMA_ALL_ELEM_MATCH_FROM_INDEX:
return "internalSchemaAllElemMatchFromIndex";
case MatchExpression::INTERNAL_SCHEMA_ALLOWED_PROPERTIES:
return "internalSchemaAllowedProperties";
case MatchExpression::INTERNAL_SCHEMA_BIN_DATA_ENCRYPTED_TYPE:
return "internalSchemaBinDataEncryptedType";
case MatchExpression::INTERNAL_SCHEMA_BIN_DATA_SUBTYPE:
return "internalSchemaBinDataSubType";
case MatchExpression::INTERNAL_SCHEMA_COND:
return "internalSchemaCond";
case MatchExpression::INTERNAL_SCHEMA_EQ:
return "internalSchemaEq";
case MatchExpression::INTERNAL_SCHEMA_FMOD:
return "internalSchemaFmod";
case MatchExpression::INTERNAL_SCHEMA_MIN_ITEMS:
return "internalSchemaMinItems";
case MatchExpression::INTERNAL_SCHEMA_MAX_ITEMS:
return "internalSchemaMaxItems";
case MatchExpression::INTERNAL_SCHEMA_UNIQUE_ITEMS:
return "internalSchemaUniqueItems";
case MatchExpression::INTERNAL_SCHEMA_XOR:
return "internalSchemaXor";
case MatchExpression::INTERNAL_SCHEMA_OBJECT_MATCH:
return "internalSchemaObjectMatch";
case MatchExpression::INTERNAL_SCHEMA_ROOT_DOC_EQ:
return "internalSchemaRootDocEq";
case MatchExpression::INTERNAL_SCHEMA_MIN_LENGTH:
return "internalSchemaMinLength";
case MatchExpression::INTERNAL_SCHEMA_MAX_LENGTH:
return "internalSchemaMaxLength";
case MatchExpression::INTERNAL_SCHEMA_MIN_PROPERTIES:
return "internalSchemaMinProperties";
case MatchExpression::INTERNAL_SCHEMA_MAX_PROPERTIES:
return "internalSchemaMaxProperties";
case MatchExpression::INTERNAL_SCHEMA_MATCH_ARRAY_INDEX:
return "internalSchemaMatchArrayIndex";
case MatchExpression::INTERNAL_SCHEMA_TYPE:
return "internalSchemaType";
default:
MONGO_UNREACHABLE;
}
}
/**
* Encodes GEO match expression.
* Encoding includes:
* - type of geo query (within/intersect/near)
* - geometry type
* - CRS (flat or spherical)
*/
void encodeGeoMatchExpression(const GeoMatchExpression* tree, StringBuilder* keyBuilder) {
const GeoExpression& geoQuery = tree->getGeoExpression();
// Type of geo query.
switch (geoQuery.getPred()) {
case GeoExpression::WITHIN:
*keyBuilder << "wi";
break;
case GeoExpression::INTERSECT:
*keyBuilder << "in";
break;
case GeoExpression::INVALID:
*keyBuilder << "id";
break;
}
// Geometry type.
// Only one of the shared_ptrs in GeoContainer may be non-NULL.
*keyBuilder << geoQuery.getGeometry().getDebugType();
// CRS (flat or spherical)
if (FLAT == geoQuery.getGeometry().getNativeCRS()) {
*keyBuilder << "fl";
} else if (SPHERE == geoQuery.getGeometry().getNativeCRS()) {
*keyBuilder << "sp";
} else if (STRICT_SPHERE == geoQuery.getGeometry().getNativeCRS()) {
*keyBuilder << "ss";
} else {
LOGV2_ERROR(23849,
"Unknown CRS type in geometry",
"crsType"_attr = (int)geoQuery.getGeometry().getNativeCRS(),
"geometryType"_attr = geoQuery.getGeometry().getDebugType());
MONGO_UNREACHABLE;
}
}
/**
* Encodes GEO_NEAR match expression.
* Encode:
* - isNearSphere
* - CRS (flat or spherical)
*/
void encodeGeoNearMatchExpression(const GeoNearMatchExpression* tree, StringBuilder* keyBuilder) {
const GeoNearExpression& nearQuery = tree->getData();
// isNearSphere
*keyBuilder << (nearQuery.isNearSphere ? "ns" : "nr");
// CRS (flat or spherical or strict-winding spherical)
switch (nearQuery.centroid->crs) {
case FLAT:
*keyBuilder << "fl";
break;
case SPHERE:
*keyBuilder << "sp";
break;
case STRICT_SPHERE:
*keyBuilder << "ss";
break;
case UNSET:
LOGV2_ERROR(23850,
"Unknown CRS type in point geometry for near query",
"crsType"_attr = (int)nearQuery.centroid->crs);
MONGO_UNREACHABLE;
break;
}
}
template <class T>
char encodeEnum(T val) {
// Ensure val can be encoded as a digit between '0' and '9' inclusive.
invariant(static_cast<int>(val) < 10);
return static_cast<char>(val) + '0';
}
void encodeCollation(const CollatorInterface* collation, StringBuilder* keyBuilder) {
if (!collation) {
return;
}
const Collation& spec = collation->getSpec();
*keyBuilder << kEncodeCollationSection;
*keyBuilder << spec.getLocale();
*keyBuilder << spec.getCaseLevel();
// Ensure that we can encode this value with a single ascii byte '0' through '9'.
*keyBuilder << encodeEnum(spec.getCaseFirst());
*keyBuilder << encodeEnum(spec.getStrength());
*keyBuilder << spec.getNumericOrdering();
*keyBuilder << encodeEnum(spec.getAlternate());
*keyBuilder << encodeEnum(spec.getMaxVariable());
*keyBuilder << spec.getNormalization();
*keyBuilder << spec.getBackwards();
// We do not encode 'spec.version' because query shape strings are never persisted, and need
// not be stable between versions.
}
template <class RegexIterator>
void encodeRegexFlagsForMatch(RegexIterator first, RegexIterator last, StringBuilder* keyBuilder) {
// We sort the flags, so that queries with the same regex flags in different orders will have
// the same shape. We then add them to a set, so that identical flags across multiple regexes
// will be deduplicated and the resulting set of unique flags will be ordered consistently.
// Regex flags are not validated at parse-time, so we also ensure that only valid flags
// contribute to the encoding.
static const auto maxValidFlags = RegexMatchExpression::kValidRegexFlags.size();
std::set<char> flags;
for (auto it = first; it != last && flags.size() < maxValidFlags; ++it) {
auto inserter = std::inserter(flags, flags.begin());
std::copy_if((*it)->getFlags().begin(), (*it)->getFlags().end(), inserter, [](auto flag) {
return RegexMatchExpression::kValidRegexFlags.count(flag);
});
}
if (!flags.empty()) {
*keyBuilder << kEncodeRegexFlagsSeparator;
for (const auto& flag : flags) {
invariant(RegexMatchExpression::kValidRegexFlags.count(flag));
encodeUserString(StringData(&flag, 1), keyBuilder);
}
*keyBuilder << kEncodeRegexFlagsSeparator;
}
}
// Helper overload to prepare a vector of unique_ptrs for the heavy-lifting function above.
void encodeRegexFlagsForMatch(const std::vector<std::unique_ptr<RegexMatchExpression>>& regexes,
StringBuilder* keyBuilder) {
const auto transformFunc = [](const auto& regex) { return regex.get(); };
encodeRegexFlagsForMatch(boost::make_transform_iterator(regexes.begin(), transformFunc),
boost::make_transform_iterator(regexes.end(), transformFunc),
keyBuilder);
}
// Helper that passes a range covering the entire source set into the heavy-lifting function above.
void encodeRegexFlagsForMatch(const std::vector<const RegexMatchExpression*>& regexes,
StringBuilder* keyBuilder) {
encodeRegexFlagsForMatch(regexes.begin(), regexes.end(), keyBuilder);
}
/**
* Traverses expression tree pre-order.
* Appends an encoding of each node's match type and path name
* to the output stream.
*/
void encodeKeyForMatch(const MatchExpression* tree, StringBuilder* keyBuilder) {
invariant(keyBuilder);
// Encode match type and path.
*keyBuilder << encodeMatchType(tree->matchType());
encodeUserString(tree->path(), keyBuilder);
// GEO and GEO_NEAR require additional encoding.
if (MatchExpression::GEO == tree->matchType()) {
encodeGeoMatchExpression(static_cast<const GeoMatchExpression*>(tree), keyBuilder);
} else if (MatchExpression::GEO_NEAR == tree->matchType()) {
encodeGeoNearMatchExpression(static_cast<const GeoNearMatchExpression*>(tree), keyBuilder);
}
// We encode regular expression flags such that different options produce different shapes.
if (MatchExpression::REGEX == tree->matchType()) {
encodeRegexFlagsForMatch({static_cast<const RegexMatchExpression*>(tree)}, keyBuilder);
} else if (MatchExpression::MATCH_IN == tree->matchType()) {
const auto* inMatch = static_cast<const InMatchExpression*>(tree);
if (!inMatch->getRegexes().empty()) {
// Append '_re' to distinguish an $in without regexes from an $in with regexes.
encodeUserString("_re"_sd, keyBuilder);
encodeRegexFlagsForMatch(inMatch->getRegexes(), keyBuilder);
}
}
// If the query predicate is minKey or maxKey it can't use the same plan as other GT/LT
// queries. If the index is multikey and the query involves one of these values, it needs
// to use INEXACT_FETCH and the bounds can't be inverted. Therefore these need their own
// shape.
if (tree->isGTMinKey())
*keyBuilder << "min";
else if (tree->isLTMaxKey())
*keyBuilder << "max";
// If the query predicate involves comparison to null, do not reuse the plan since empty arrays
// ([]) are encoded as null in the index. Thus we cannot safely invert the index bounds.
if (isQueryNegatingGTEorLTENull(tree)) {
*keyBuilder << "not_gte_lte_null";
}
// Traverse child nodes.
// Enclose children in [].
if (tree->numChildren() > 0) {
*keyBuilder << kEncodeChildrenBegin;
}
// Use comma to separate children encoding.
for (size_t i = 0; i < tree->numChildren(); ++i) {
if (i > 0) {
*keyBuilder << kEncodeChildrenSeparator;
}
encodeKeyForMatch(tree->getChild(i), keyBuilder);
}
if (tree->numChildren() > 0) {
*keyBuilder << kEncodeChildrenEnd;
}
}
/**
* Encodes sort order into cache key. Sort order is normalized because it provided by
* FindCommandRequest.
*/
void encodeKeyForSort(const BSONObj& sortObj, StringBuilder* keyBuilder) {
if (sortObj.isEmpty()) {
return;
}
*keyBuilder << kEncodeSortSection;
BSONObjIterator it(sortObj);
while (it.more()) {
BSONElement elt = it.next();
// $meta text score
if (query_request_helper::isTextScoreMeta(elt)) {
*keyBuilder << "t";
}
// Ascending
else if (elt.numberInt() == 1) {
*keyBuilder << "a";
}
// Descending
else {
*keyBuilder << "d";
}
encodeUserString(elt.fieldName(), keyBuilder);
// Sort argument separator
if (it.more()) {
*keyBuilder << ",";
}
}
}
/**
* Encodes projection AST into a cache key.
*
* For projections which have a finite set of required fields (inclusion-only projections), encodes
* those field names in order.
*
* For projections which require the entire document (exclusion projections, projections with
* expressions), the projection section is empty.
*/
void encodeKeyForProj(const projection_ast::Projection* proj, StringBuilder* keyBuilder) {
if (!proj || proj->requiresDocument()) {
// Don't encode anything for the projection section to indicate the entire document is
// required.
return;
}
std::vector<std::string> requiredFields = proj->getRequiredFields();
// If the only requirement is that $sortKey be included with some value, we just act as if the
// entire document is needed.
if (requiredFields.size() == 1 && requiredFields.front() == "$sortKey") {
return;
}
// If the projection just re-writes the entire document and has no dependencies on the original
// document (e.g. {a: "foo", _id: 0}), then just include the projection delimiter.
*keyBuilder << kEncodeProjectionSection;
// Encode the fields required by the projection in order.
std::sort(requiredFields.begin(), requiredFields.end());
bool isFirst = true;
for (auto&& requiredField : requiredFields) {
invariant(!requiredField.empty());
// Internal callers (e.g, from mongos) may add "$sortKey" to the projection. This is not
// part of the user query, and therefore are not considered part of the cache key.
if (requiredField == "$sortKey") {
continue;
}
if (!isFirst) {
*keyBuilder << kEncodeProjectionRequirementSeparator;
}
encodeUserString(requiredField, keyBuilder);
isFirst = false;
}
}
} // namespace
namespace canonical_query_encoder {
CanonicalQuery::QueryShapeString encode(const CanonicalQuery& cq) {
StringBuilder keyBuilder;
encodeKeyForMatch(cq.root(), &keyBuilder);
encodeKeyForSort(cq.getFindCommandRequest().getSort(), &keyBuilder);
encodeKeyForProj(cq.getProj(), &keyBuilder);
encodeCollation(cq.getCollator(), &keyBuilder);
return keyBuilder.str();
}
uint32_t computeHash(StringData key) {
return SimpleStringDataComparator::kInstance.hash(key);
}
} // namespace canonical_query_encoder
} // namespace mongo
|