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
|
/**
* Copyright (C) 2015 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 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 GNU Affero General 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_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kQuery
#include "mongo/platform/basic.h"
#include "mongo/db/query/query_planner_test_fixture.h"
#include <algorithm>
#include "mongo/db/matcher/expression_parser.h"
#include "mongo/db/matcher/extensions_callback_disallow_extensions.h"
#include "mongo/db/matcher/extensions_callback_noop.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/query/query_knobs.h"
#include "mongo/db/query/query_planner.h"
#include "mongo/db/query/query_planner_test_lib.h"
#include "mongo/util/log.h"
#include "mongo/util/transitional_tools_do_not_use/vector_spooling.h"
namespace mongo {
using unittest::assertGet;
const NamespaceString QueryPlannerTest::nss("test.collection");
void QueryPlannerTest::setUp() {
opCtx = serviceContext.makeOperationContext();
internalQueryPlannerEnableHashIntersection.store(true);
params.options = QueryPlannerParams::INCLUDE_COLLSCAN;
addIndex(BSON("_id" << 1));
}
void QueryPlannerTest::addIndex(BSONObj keyPattern, bool multikey) {
params.indices.push_back(IndexEntry(keyPattern,
multikey,
false, // sparse
false, // unique
"hari_king_of_the_stove",
NULL, // filterExpr
BSONObj()));
}
void QueryPlannerTest::addIndex(BSONObj keyPattern, bool multikey, bool sparse) {
params.indices.push_back(IndexEntry(keyPattern,
multikey,
sparse,
false, // unique
"note_to_self_dont_break_build",
NULL, // filterExpr
BSONObj()));
}
void QueryPlannerTest::addIndex(BSONObj keyPattern, bool multikey, bool sparse, bool unique) {
params.indices.push_back(IndexEntry(keyPattern,
multikey,
sparse,
unique,
"sql_query_walks_into_bar_and_says_can_i_join_you?",
NULL, // filterExpr
BSONObj()));
}
void QueryPlannerTest::addIndex(BSONObj keyPattern, BSONObj infoObj) {
params.indices.push_back(IndexEntry(keyPattern,
false, // multikey
false, // sparse
false, // unique
"foo",
NULL, // filterExpr
infoObj));
}
void QueryPlannerTest::addIndex(BSONObj keyPattern, MatchExpression* filterExpr) {
params.indices.push_back(IndexEntry(keyPattern,
false, // multikey
false, // sparse
false, // unique
"foo",
filterExpr,
BSONObj()));
}
void QueryPlannerTest::addIndex(BSONObj keyPattern, const MultikeyPaths& multikeyPaths) {
invariant(multikeyPaths.size() == static_cast<size_t>(keyPattern.nFields()));
const bool multikey =
std::any_of(multikeyPaths.cbegin(),
multikeyPaths.cend(),
[](const std::set<size_t>& components) { return !components.empty(); });
const bool sparse = false;
const bool unique = false;
const char name[] = "my_index_with_path_level_multikey_info";
const MatchExpression* filterExpr = nullptr;
const BSONObj infoObj;
IndexEntry entry(keyPattern, multikey, sparse, unique, name, filterExpr, infoObj);
entry.multikeyPaths = multikeyPaths;
params.indices.push_back(entry);
}
void QueryPlannerTest::addIndex(BSONObj keyPattern, const CollatorInterface* collator) {
const bool sparse = false;
const bool unique = false;
const bool multikey = false;
const char name[] = "my_index_with_collator";
const MatchExpression* filterExpr = nullptr;
const BSONObj infoObj;
IndexEntry entry(keyPattern, multikey, sparse, unique, name, filterExpr, infoObj);
entry.collator = collator;
params.indices.push_back(entry);
}
void QueryPlannerTest::addIndex(BSONObj keyPattern,
const CollatorInterface* collator,
StringData indexName) {
const bool sparse = false;
const bool unique = false;
const bool multikey = false;
const auto name = indexName.toString();
const MatchExpression* filterExpr = nullptr;
const BSONObj infoObj;
IndexEntry entry(keyPattern, multikey, sparse, unique, name, filterExpr, infoObj);
entry.collator = collator;
params.indices.push_back(entry);
}
void QueryPlannerTest::addIndex(BSONObj keyPattern,
MatchExpression* filterExpr,
const CollatorInterface* collator) {
const bool sparse = false;
const bool unique = false;
const bool multikey = false;
const char name[] = "my_partial_index_with_collator";
const BSONObj infoObj;
IndexEntry entry(keyPattern, multikey, sparse, unique, name, filterExpr, infoObj);
entry.collator = collator;
params.indices.push_back(entry);
}
void QueryPlannerTest::runQuery(BSONObj query) {
runQuerySortProjSkipNToReturn(query, BSONObj(), BSONObj(), 0, 0);
}
void QueryPlannerTest::runQuerySortProj(const BSONObj& query,
const BSONObj& sort,
const BSONObj& proj) {
runQuerySortProjSkipNToReturn(query, sort, proj, 0, 0);
}
void QueryPlannerTest::runQuerySkipNToReturn(const BSONObj& query,
long long skip,
long long ntoreturn) {
runQuerySortProjSkipNToReturn(query, BSONObj(), BSONObj(), skip, ntoreturn);
}
void QueryPlannerTest::runQueryHint(const BSONObj& query, const BSONObj& hint) {
runQuerySortProjSkipNToReturnHint(query, BSONObj(), BSONObj(), 0, 0, hint);
}
void QueryPlannerTest::runQuerySortProjSkipNToReturn(const BSONObj& query,
const BSONObj& sort,
const BSONObj& proj,
long long skip,
long long ntoreturn) {
runQuerySortProjSkipNToReturnHint(query, sort, proj, skip, ntoreturn, BSONObj());
}
void QueryPlannerTest::runQuerySortHint(const BSONObj& query,
const BSONObj& sort,
const BSONObj& hint) {
runQuerySortProjSkipNToReturnHint(query, sort, BSONObj(), 0, 0, hint);
}
void QueryPlannerTest::runQueryHintMinMax(const BSONObj& query,
const BSONObj& hint,
const BSONObj& minObj,
const BSONObj& maxObj) {
runQueryFull(query, BSONObj(), BSONObj(), 0, 0, hint, minObj, maxObj, false);
}
void QueryPlannerTest::runQuerySortProjSkipNToReturnHint(const BSONObj& query,
const BSONObj& sort,
const BSONObj& proj,
long long skip,
long long ntoreturn,
const BSONObj& hint) {
runQueryFull(query, sort, proj, skip, ntoreturn, hint, BSONObj(), BSONObj(), false);
}
void QueryPlannerTest::runQuerySnapshot(const BSONObj& query) {
runQueryFull(query, BSONObj(), BSONObj(), 0, 0, BSONObj(), BSONObj(), BSONObj(), true);
}
void QueryPlannerTest::runQueryFull(const BSONObj& query,
const BSONObj& sort,
const BSONObj& proj,
long long skip,
long long ntoreturn,
const BSONObj& hint,
const BSONObj& minObj,
const BSONObj& maxObj,
bool snapshot) {
// Clean up any previous state from a call to runQueryFull
solns.clear();
cq.reset();
auto qr = stdx::make_unique<QueryRequest>(nss);
qr->setFilter(query);
qr->setSort(sort);
qr->setProj(proj);
if (skip) {
qr->setSkip(skip);
}
if (ntoreturn) {
if (ntoreturn < 0) {
ASSERT_NE(ntoreturn, std::numeric_limits<long long>::min());
ntoreturn = -ntoreturn;
qr->setWantMore(false);
}
qr->setNToReturn(ntoreturn);
}
qr->setHint(hint);
qr->setMin(minObj);
qr->setMax(maxObj);
qr->setSnapshot(snapshot);
auto statusWithCQ =
CanonicalQuery::canonicalize(opCtx.get(), std::move(qr), ExtensionsCallbackNoop());
ASSERT_OK(statusWithCQ.getStatus());
cq = std::move(statusWithCQ.getValue());
std::vector<QuerySolution*> solnsRaw;
ASSERT_OK(QueryPlanner::plan(*cq, params, &solnsRaw));
solns = transitional_tools_do_not_use::spool_vector(solnsRaw);
}
void QueryPlannerTest::runInvalidQuery(const BSONObj& query) {
runInvalidQuerySortProjSkipNToReturn(query, BSONObj(), BSONObj(), 0, 0);
}
void QueryPlannerTest::runInvalidQuerySortProj(const BSONObj& query,
const BSONObj& sort,
const BSONObj& proj) {
runInvalidQuerySortProjSkipNToReturn(query, sort, proj, 0, 0);
}
void QueryPlannerTest::runInvalidQuerySortProjSkipNToReturn(const BSONObj& query,
const BSONObj& sort,
const BSONObj& proj,
long long skip,
long long ntoreturn) {
runInvalidQuerySortProjSkipNToReturnHint(query, sort, proj, skip, ntoreturn, BSONObj());
}
void QueryPlannerTest::runInvalidQueryHint(const BSONObj& query, const BSONObj& hint) {
runInvalidQuerySortProjSkipNToReturnHint(query, BSONObj(), BSONObj(), 0, 0, hint);
}
void QueryPlannerTest::runInvalidQueryHintMinMax(const BSONObj& query,
const BSONObj& hint,
const BSONObj& minObj,
const BSONObj& maxObj) {
runInvalidQueryFull(query, BSONObj(), BSONObj(), 0, 0, hint, minObj, maxObj, false);
}
void QueryPlannerTest::runInvalidQuerySortProjSkipNToReturnHint(const BSONObj& query,
const BSONObj& sort,
const BSONObj& proj,
long long skip,
long long ntoreturn,
const BSONObj& hint) {
runInvalidQueryFull(query, sort, proj, skip, ntoreturn, hint, BSONObj(), BSONObj(), false);
}
void QueryPlannerTest::runInvalidQueryFull(const BSONObj& query,
const BSONObj& sort,
const BSONObj& proj,
long long skip,
long long ntoreturn,
const BSONObj& hint,
const BSONObj& minObj,
const BSONObj& maxObj,
bool snapshot) {
solns.clear();
cq.reset();
auto qr = stdx::make_unique<QueryRequest>(nss);
qr->setFilter(query);
qr->setSort(sort);
qr->setProj(proj);
if (skip) {
qr->setSkip(skip);
}
if (ntoreturn) {
if (ntoreturn < 0) {
ASSERT_NE(ntoreturn, std::numeric_limits<long long>::min());
ntoreturn = -ntoreturn;
qr->setWantMore(false);
}
qr->setNToReturn(ntoreturn);
}
qr->setHint(hint);
qr->setMin(minObj);
qr->setMax(maxObj);
qr->setSnapshot(snapshot);
auto statusWithCQ =
CanonicalQuery::canonicalize(opCtx.get(), std::move(qr), ExtensionsCallbackNoop());
ASSERT_OK(statusWithCQ.getStatus());
cq = std::move(statusWithCQ.getValue());
std::vector<QuerySolution*> solnsRaw;
Status s = QueryPlanner::plan(*cq, params, &solnsRaw);
solns = transitional_tools_do_not_use::spool_vector(solnsRaw);
ASSERT_NOT_OK(s);
}
void QueryPlannerTest::runQueryAsCommand(const BSONObj& cmdObj) {
solns.clear();
cq.reset();
invariant(nss.isValid());
const bool isExplain = false;
std::unique_ptr<QueryRequest> qr(
assertGet(QueryRequest::makeFromFindCommand(nss, cmdObj, isExplain)));
auto statusWithCQ =
CanonicalQuery::canonicalize(opCtx.get(), std::move(qr), ExtensionsCallbackNoop());
ASSERT_OK(statusWithCQ.getStatus());
cq = std::move(statusWithCQ.getValue());
std::vector<QuerySolution*> solnsRaw;
Status s = QueryPlanner::plan(*cq, params, &solnsRaw);
solns = transitional_tools_do_not_use::spool_vector(solnsRaw);
ASSERT_OK(s);
}
void QueryPlannerTest::runInvalidQueryAsCommand(const BSONObj& cmdObj) {
solns.clear();
cq.reset();
invariant(nss.isValid());
const bool isExplain = false;
std::unique_ptr<QueryRequest> qr(
assertGet(QueryRequest::makeFromFindCommand(nss, cmdObj, isExplain)));
auto statusWithCQ =
CanonicalQuery::canonicalize(opCtx.get(), std::move(qr), ExtensionsCallbackNoop());
ASSERT_OK(statusWithCQ.getStatus());
cq = std::move(statusWithCQ.getValue());
std::vector<QuerySolution*> solnsRaw;
Status status = QueryPlanner::plan(*cq, params, &solnsRaw);
solns = transitional_tools_do_not_use::spool_vector(solnsRaw);
ASSERT_NOT_OK(status);
}
size_t QueryPlannerTest::getNumSolutions() const {
return solns.size();
}
void QueryPlannerTest::dumpSolutions() const {
mongoutils::str::stream ost;
dumpSolutions(ost);
log() << std::string(ost);
}
void QueryPlannerTest::dumpSolutions(mongoutils::str::stream& ost) const {
for (auto&& soln : solns) {
ost << soln->toString() << '\n';
}
}
void QueryPlannerTest::assertNumSolutions(size_t expectSolutions) const {
if (getNumSolutions() == expectSolutions) {
return;
}
mongoutils::str::stream ss;
ss << "expected " << expectSolutions << " solutions but got " << getNumSolutions()
<< " instead. solutions generated: " << '\n';
dumpSolutions(ss);
FAIL(ss);
}
size_t QueryPlannerTest::numSolutionMatches(const std::string& solnJson) const {
BSONObj testSoln = fromjson(solnJson);
size_t matches = 0;
for (auto&& soln : solns) {
QuerySolutionNode* root = soln->root.get();
if (QueryPlannerTestLib::solutionMatches(testSoln, root)) {
++matches;
}
}
return matches;
}
void QueryPlannerTest::assertSolutionExists(const std::string& solnJson, size_t numMatches) const {
size_t matches = numSolutionMatches(solnJson);
if (numMatches == matches) {
return;
}
mongoutils::str::stream ss;
ss << "expected " << numMatches << " matches for solution " << solnJson << " but got "
<< matches << " instead. all solutions generated: " << '\n';
dumpSolutions(ss);
FAIL(ss);
}
void QueryPlannerTest::assertHasOneSolutionOf(const std::vector<std::string>& solnStrs) const {
size_t matches = 0;
for (std::vector<std::string>::const_iterator it = solnStrs.begin(); it != solnStrs.end();
++it) {
if (1U == numSolutionMatches(*it)) {
++matches;
}
}
if (1U == matches) {
return;
}
mongoutils::str::stream ss;
ss << "assertHasOneSolutionOf expected one matching solution"
<< " but got " << matches << " instead. all solutions generated: " << '\n';
dumpSolutions(ss);
FAIL(ss);
}
std::unique_ptr<MatchExpression> QueryPlannerTest::parseMatchExpression(
const BSONObj& obj, const CollatorInterface* collator) {
StatusWithMatchExpression status =
MatchExpressionParser::parse(obj, ExtensionsCallbackDisallowExtensions(), collator);
if (!status.isOK()) {
FAIL(str::stream() << "failed to parse query: " << obj.toString() << ". Reason: "
<< status.getStatus().toString());
}
return std::move(status.getValue());
}
} // namespace mongo
|