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
|
/**
* Copyright (C) 2013 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.
*/
/**
* This file contains tests for mongo/db/query/planner_ixselect.cpp
*/
#include "mongo/db/query/planner_ixselect.h"
#include <memory>
#include "mongo/db/json.h"
#include "mongo/db/matcher/expression_parser.h"
#include "mongo/db/query/index_tag.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/text.h"
using namespace mongo;
namespace {
using std::unique_ptr;
using std::string;
using std::vector;
/**
* Utility function to create MatchExpression
*/
unique_ptr<MatchExpression> parseMatchExpression(const BSONObj& obj) {
StatusWithMatchExpression status = MatchExpressionParser::parse(obj);
ASSERT_TRUE(status.isOK());
return std::move(status.getValue());
}
/**
* Utility function to join elements in iterator range with comma
*/
template <typename Iter>
string toString(Iter begin, Iter end) {
mongoutils::str::stream ss;
ss << "[";
for (Iter i = begin; i != end; i++) {
if (i != begin) {
ss << " ";
}
ss << *i;
}
ss << "]";
return ss;
}
/**
* Test function for getFields()
* Parses query string to obtain MatchExpression which is passed together with prefix
* to QueryPlannerIXSelect::getFields()
* Results are compared with expected fields (parsed from expectedFieldsStr)
*/
void testGetFields(const char* query, const char* prefix, const char* expectedFieldsStr) {
BSONObj obj = fromjson(query);
unique_ptr<MatchExpression> expr(parseMatchExpression(obj));
unordered_set<string> fields;
QueryPlannerIXSelect::getFields(expr.get(), prefix, &fields);
// Verify results
// First, check that results contain a superset of expected fields.
vector<string> expectedFields = StringSplitter::split(expectedFieldsStr, ",");
for (vector<string>::const_iterator i = expectedFields.begin(); i != expectedFields.end();
i++) {
if (fields.find(*i) == fields.end()) {
mongoutils::str::stream ss;
ss << "getFields(query=" << query << ", prefix=" << prefix << "): unable to find " << *i
<< " in result: " << toString(fields.begin(), fields.end());
FAIL(ss);
}
}
// Next, confirm that results do not contain any unexpected fields.
if (fields.size() != expectedFields.size()) {
mongoutils::str::stream ss;
ss << "getFields(query=" << query << ", prefix=" << prefix
<< "): unexpected fields in result. expected: "
<< toString(expectedFields.begin(), expectedFields.end())
<< ". actual: " << toString(fields.begin(), fields.end());
FAIL(ss);
}
}
/**
* Basic test cases for getFields()
* Includes logical operators
*/
TEST(QueryPlannerIXSelectTest, GetFieldsBasic) {
// Arguments to test function: query, prefix, comma-delimited list of expected fields
testGetFields("{}", "", "");
testGetFields("{a: 1}", "", "a");
testGetFields("{a: 1}", "c.", "c.a");
testGetFields("{a: 1, b: 1}", "", "a,b");
testGetFields("{a: {$in: [1]}}", "", "a");
testGetFields("{$or: [{a: 1}, {b: 1}]}", "", "a,b");
}
/**
* Array test cases for getFields
*/
TEST(QueryPlannerIXSelectTest, GetFieldsArray) {
testGetFields("{a: {$elemMatch: {b: 1}}}", "", "a.b");
testGetFields("{a: {$all: [{$elemMatch: {b: 1}}]}}", "", "a.b");
}
/**
* Negation test cases for getFields()
* $ne, $nin, $nor
*/
TEST(QueryPlannerIXSelectTest, GetFieldsNegation) {
testGetFields("{a: {$ne: 1}}", "", "a");
testGetFields("{a: {$nin: [1]}}", "", "a");
testGetFields("{$nor: [{a: 1}, {b: 1}]}", "", "");
testGetFields("{$and: [{a: 1}, {a: {$ne: 2}}]}", "", "a");
}
/**
* Array negation test cases for getFields
*/
TEST(QueryPlannerIXSelectTest, GetFieldsArrayNegation) {
testGetFields("{a: {$elemMatch: {b: {$ne: 1}}}}", "", "a.b");
testGetFields("{a: {$all: [{$elemMatch: {b: {$ne: 1}}}]}}", "", "a.b");
}
/**
* Performs a pre-order traversal of expression tree. Validates
* that all tagged nodes contain an instance of RelevantTag.
*/
void findRelevantTaggedNodePaths(MatchExpression* root, vector<string>* paths) {
MatchExpression::TagData* tag = root->getTag();
if (tag) {
StringBuilder buf;
tag->debugString(&buf);
RelevantTag* r = dynamic_cast<RelevantTag*>(tag);
if (!r) {
mongoutils::str::stream ss;
ss << "tag is not instance of RelevantTag. tree: " << root->toString()
<< "; tag: " << buf.str();
FAIL(ss);
}
paths->push_back(r->path);
}
for (size_t i = 0; i < root->numChildren(); ++i) {
findRelevantTaggedNodePaths(root->getChild(i), paths);
}
}
/**
* Parses a MatchExpression from query string and passes that along with
* prefix to rateIndices.
* Verifies results against list of expected paths.
* For now, we're only interested in which nodes are tagged.
* In future, we may expand this test function to include
* validate which indices are assigned to a node.
*/
void testRateIndicesTaggedNodePaths(const char* query,
const char* prefix,
const char* expectedPathsStr) {
// Parse and rate query. Some of the nodes in the rated tree
// will be tagged after the rating process.
BSONObj obj = fromjson(query);
unique_ptr<MatchExpression> expr(parseMatchExpression(obj));
// Currently, we tag every indexable node even when no compatible
// index is available. Hence, it is fine to pass an empty vector of
// indices to rateIndices().
vector<IndexEntry> indices;
QueryPlannerIXSelect::rateIndices(expr.get(), prefix, indices);
// Retrieve a list of paths embedded in
// tagged nodes.
vector<string> paths;
findRelevantTaggedNodePaths(expr.get(), &paths);
// Compare with expected list of paths.
// First verify number of paths retrieved.
vector<string> expectedPaths = StringSplitter::split(expectedPathsStr, ",");
if (paths.size() != expectedPaths.size()) {
mongoutils::str::stream ss;
ss << "rateIndices(query=" << query << ", prefix=" << prefix
<< "): unexpected number of tagged nodes found. expected: "
<< toString(expectedPaths.begin(), expectedPaths.end())
<< ". actual: " << toString(paths.begin(), paths.end());
FAIL(ss);
}
// Next, check that value and order of each element match between the two lists.
for (vector<string>::const_iterator i = paths.begin(), j = expectedPaths.begin();
i != paths.end();
i++, j++) {
if (*i == *j) {
continue;
}
mongoutils::str::stream ss;
ss << "rateIndices(query=" << query << ", prefix=" << prefix
<< "): unexpected path found. expected: " << *j << " "
<< toString(expectedPaths.begin(), expectedPaths.end()) << ". actual: " << *i << " "
<< toString(paths.begin(), paths.end());
FAIL(ss);
}
}
/**
* Basic test cases for rateIndices().
* Includes logical operators.
*/
TEST(QueryPlannerIXSelectTest, RateIndicesTaggedNodePathsBasic) {
// Test arguments: query, prefix, comma-delimited list of expected paths
testRateIndicesTaggedNodePaths("{}", "", "");
testRateIndicesTaggedNodePaths("{a: 1}", "", "a");
testRateIndicesTaggedNodePaths("{a: 1}", "c.", "c.a");
testRateIndicesTaggedNodePaths("{a: 1, b: 1}", "", "a,b");
testRateIndicesTaggedNodePaths("{a: {$in: [1]}}", "", "a");
testRateIndicesTaggedNodePaths("{$or: [{a: 1}, {b: 1}]}", "", "a,b");
}
/**
* Array test cases for rateIndices().
*/
TEST(QueryPlannerIXSelectTest, RateIndicesTaggedNodePathArray) {
testRateIndicesTaggedNodePaths("{a: {$elemMatch: {b: 1}}}", "", "a.b");
testRateIndicesTaggedNodePaths("{a: {$all: [{$elemMatch: {b: 1}}]}}", "", "a.b");
}
/**
* Negation test cases for rateIndices().
*/
TEST(QueryPlannerIXSelectTest, RateIndicesTaggedNodePathsNegation) {
testRateIndicesTaggedNodePaths("{a: {$ne: 1}}", "", "a,a");
testRateIndicesTaggedNodePaths("{a: {$nin: [1]}}", "", "a,a");
testRateIndicesTaggedNodePaths("{$nor: [{a: 1}, {b: 1}]}", "", "");
testRateIndicesTaggedNodePaths("{$and: [{a: 1}, {a: {$ne: 2}}]}", "", "a,a,a");
}
/**
* Array negation test cases for rateIndices().
*/
TEST(QueryPlannerIXSelectTest, RateIndicesTaggedNodePathArrayNegation) {
testRateIndicesTaggedNodePaths("{a: {$elemMatch: {b: {$ne: 1}}}}", "", "a.b,a.b");
testRateIndicesTaggedNodePaths("{a: {$all: [{$elemMatch: {b: {$ne: 1}}}]}}", "", "a.b,a.b");
}
} // namespace
|