summaryrefslogtreecommitdiff
path: root/src/mongo/db/commands/index_filter_commands_test.cpp
blob: baeb4b985a70e75a120cf1b8a2615161134bd065 (plain)
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
/**
 *    Copyright (C) 2014 MongoDB 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/commands/index_filter_commands.h
 */

#include "mongo/db/commands/index_filter_commands.h"


#include "mongo/db/json.h"
#include "mongo/db/operation_context_noop.h"
#include "mongo/db/query/plan_ranker.h"
#include "mongo/db/query/query_solution.h"
#include "mongo/unittest/unittest.h"

using namespace mongo;

namespace {

    using std::unique_ptr;
    using std::unique_ptr;
    using std::string;
    using std::vector;

    static const char* ns = "somebogusns";

    /**
     * Utility function to get list of index filters from the query settings.
     */
    vector<BSONObj> getFilters(const QuerySettings& querySettings) {
        BSONObjBuilder bob;
        ASSERT_OK(ListFilters::list(querySettings, &bob));
        BSONObj resultObj = bob.obj();
        BSONElement filtersElt = resultObj.getField("filters");
        ASSERT_EQUALS(filtersElt.type(), mongo::Array);
        vector<BSONElement> filtersEltArray = filtersElt.Array();
        vector<BSONObj> filters;
        for (vector<BSONElement>::const_iterator i = filtersEltArray.begin();
             i != filtersEltArray.end(); ++i) {
            const BSONElement& elt = *i;

            ASSERT_TRUE(elt.isABSONObj());
            BSONObj obj = elt.Obj();

            // Check required fields.
            // query
            BSONElement queryElt = obj.getField("query");
            ASSERT_TRUE(queryElt.isABSONObj());

            // sort
            BSONElement sortElt = obj.getField("sort");
            ASSERT_TRUE(sortElt.isABSONObj());

            // projection
            BSONElement projectionElt = obj.getField("projection");
            ASSERT_TRUE(projectionElt.isABSONObj());

            // indexes
            BSONElement indexesElt = obj.getField("indexes");
            ASSERT_EQUALS(indexesElt.type(), mongo::Array);

            // All fields OK. Append to vector.
            filters.push_back(obj.getOwned());
        }

        return filters;
    }

    /**
     * Utility function to create a PlanRankingDecision
     */
    PlanRankingDecision* createDecision(size_t numPlans) {
        unique_ptr<PlanRankingDecision> why(new PlanRankingDecision());
        for (size_t i = 0; i < numPlans; ++i) {
            CommonStats common("COLLSCAN");
            unique_ptr<PlanStageStats> stats(new PlanStageStats(common, STAGE_COLLSCAN));
            stats->specific.reset(new CollectionScanStats());
            why->stats.mutableVector().push_back(stats.release());
            why->scores.push_back(0U);
            why->candidateOrder.push_back(i);
        }
        return why.release();
    }

    /**
     * Injects an entry into plan cache for query shape.
     */
    void addQueryShapeToPlanCache(PlanCache* planCache, const char* queryStr, const char* sortStr,
                                  const char* projectionStr) {
        BSONObj queryObj = fromjson(queryStr);
        BSONObj sortObj = fromjson(sortStr);
        BSONObj projectionObj = fromjson(projectionStr);

        // Create canonical query.
        CanonicalQuery* cqRaw;
        ASSERT_OK(CanonicalQuery::canonicalize(ns, queryObj, sortObj, projectionObj, &cqRaw));
        unique_ptr<CanonicalQuery> cq(cqRaw);

        QuerySolution qs;
        qs.cacheData.reset(new SolutionCacheData());
        qs.cacheData->tree.reset(new PlanCacheIndexTree());
        std::vector<QuerySolution*> solns;
        solns.push_back(&qs);
        ASSERT_OK(planCache->add(*cq, solns, createDecision(1U)));
    }

    /**
     * Checks if plan cache contains query shape.
     */
    bool planCacheContains(const PlanCache& planCache, const char* queryStr, const char* sortStr,
                           const char* projectionStr) {
        BSONObj queryObj = fromjson(queryStr);
        BSONObj sortObj = fromjson(sortStr);
        BSONObj projectionObj = fromjson(projectionStr);

        // Create canonical query.
        CanonicalQuery* cqRaw;
        ASSERT_OK(CanonicalQuery::canonicalize(ns, queryObj, sortObj, projectionObj, &cqRaw));
        unique_ptr<CanonicalQuery> cq(cqRaw);

        // Retrieve cache entries from plan cache.
        vector<PlanCacheEntry*> entries = planCache.getAllEntries();

        // Search keys.
        bool found = false;
        for (vector<PlanCacheEntry*>::const_iterator i = entries.begin(); i != entries.end(); i++) {
            PlanCacheEntry* entry = *i;

            // Canonicalizing query shape in cache entry to get cache key.
            // Alternatively, we could add key to PlanCacheEntry but that would be used in one place only.
            ASSERT_OK(CanonicalQuery::canonicalize(ns, entry->query, entry->sort,
                                                   entry->projection, &cqRaw));
            unique_ptr<CanonicalQuery> currentQuery(cqRaw);

            if (planCache.computeKey(*currentQuery) == planCache.computeKey(*cq)) {
                found = true;
            }
            // Release resources for cache entry after extracting key.
            delete entry;
        }
        return found;
    }

    /**
     * Tests for ListFilters
     */

    TEST(IndexFilterCommandsTest, ListFiltersEmpty) {
        QuerySettings empty;
        vector<BSONObj> filters = getFilters(empty);
        ASSERT_TRUE(filters.empty());
    }

    /**
     * Tests for ClearFilters
     */

    TEST(IndexFilterCommandsTest, ClearFiltersInvalidParameter) {
        QuerySettings empty;
        PlanCache planCache;
        OperationContextNoop txn;

        // If present, query has to be an object.
        ASSERT_NOT_OK(ClearFilters::clear(&txn, &empty, &planCache, ns, fromjson("{query: 1234}")));
        // If present, sort must be an object.
        ASSERT_NOT_OK(ClearFilters::clear(&txn, &empty, &planCache, ns,
                                        fromjson("{query: {a: 1}, sort: 1234}")));
        // If present, projection must be an object.
        ASSERT_NOT_OK(ClearFilters::clear(&txn, &empty, &planCache, ns,
                                        fromjson("{query: {a: 1}, projection: 1234}")));
        // Query must pass canonicalization.
        ASSERT_NOT_OK(ClearFilters::clear(&txn, &empty, &planCache, ns,
                                        fromjson("{query: {a: {$no_such_op: 1}}}")));
        // Sort present without query is an error.
        ASSERT_NOT_OK(ClearFilters::clear(&txn, &empty, &planCache, ns, fromjson("{sort: {a: 1}}")));
        // Projection present without query is an error.
        ASSERT_NOT_OK(ClearFilters::clear(&txn, &empty, &planCache, ns,
                                        fromjson("{projection: {_id: 0, a: 1}}")));
    }

    TEST(IndexFilterCommandsTest, ClearNonexistentHint) {
        QuerySettings querySettings;
        PlanCache planCache;
        OperationContextNoop txn;

        ASSERT_OK(SetFilter::set(&txn, &querySettings, &planCache, ns,
            fromjson("{query: {a: 1}, indexes: [{a: 1}]}")));
        vector<BSONObj> filters = getFilters(querySettings);
        ASSERT_EQUALS(filters.size(), 1U);

        // Clear nonexistent hint.
        // Command should succeed and cache should remain unchanged.
        ASSERT_OK(ClearFilters::clear(&txn, &querySettings, &planCache, ns, fromjson("{query: {b: 1}}")));
        filters = getFilters(querySettings);
        ASSERT_EQUALS(filters.size(), 1U);
    }

    /**
     * Tests for SetFilter
     */

    TEST(IndexFilterCommandsTest, SetFilterInvalidParameter) {
        QuerySettings empty;
        PlanCache planCache;
        OperationContextNoop txn;

        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns, fromjson("{}")));
        // Missing required query field.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns, fromjson("{indexes: [{a: 1}]}")));
        // Missing required indexes field.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns, fromjson("{query: {a: 1}}")));
        // Query has to be an object.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns,
            fromjson("{query: 1234, indexes: [{a: 1}, {b: 1}]}")));
        // Indexes field has to be an array.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns,
                                   fromjson("{query: {a: 1}, indexes: 1234}")));
        // Array indexes field cannot empty.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns,
                                   fromjson("{query: {a: 1}, indexes: []}")));
        // Elements in indexes have to be objects.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns,
                                   fromjson("{query: {a: 1}, indexes: [{a: 1}, 99]}")));
        // Objects in indexes cannot be empty.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns,
                                   fromjson("{query: {a: 1}, indexes: [{a: 1}, {}]}")));
        // If present, sort must be an object.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns,
            fromjson("{query: {a: 1}, sort: 1234, indexes: [{a: 1}, {b: 1}]}")));
        // If present, projection must be an object.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns,
            fromjson("{query: {a: 1}, projection: 1234, indexes: [{a: 1}, {b: 1}]}")));
        // Query must pass canonicalization.
        ASSERT_NOT_OK(SetFilter::set(&txn, &empty, &planCache, ns,
            fromjson("{query: {a: {$no_such_op: 1}}, indexes: [{a: 1}, {b: 1}]}")));
    }

    TEST(IndexFilterCommandsTest, SetAndClearFilters) {
        QuerySettings querySettings;
        PlanCache planCache;
        OperationContextNoop txn;

        // Inject query shape into plan cache.
        addQueryShapeToPlanCache(&planCache, "{a: 1, b: 1}", "{a: -1}", "{_id: 0, a: 1}");
        ASSERT_TRUE(planCacheContains(planCache, "{a: 1, b: 1}", "{a: -1}", "{_id: 0, a: 1}"));

        ASSERT_OK(SetFilter::set(&txn, &querySettings, &planCache, ns,
            fromjson("{query: {a: 1, b: 1}, sort: {a: -1}, projection: {_id: 0, a: 1}, "
                     "indexes: [{a: 1}]}")));
        vector<BSONObj> filters = getFilters(querySettings);
        ASSERT_EQUALS(filters.size(), 1U);

        // Query shape should not exist in plan cache after hint is updated.
        ASSERT_FALSE(planCacheContains(planCache, "{a: 1, b: 1}", "{a: -1}", "{_id: 0, a: 1}"));

        // Fields in filter should match criteria in most recent query settings update.
        ASSERT_EQUALS(filters[0].getObjectField("query"), fromjson("{a: 1, b: 1}"));
        ASSERT_EQUALS(filters[0].getObjectField("sort"), fromjson("{a: -1}"));
        ASSERT_EQUALS(filters[0].getObjectField("projection"), fromjson("{_id: 0, a: 1}"));

        // Replacing the hint for the same query shape ({a: 1, b: 1} and {b: 2, a: 3}
        // share same shape) should not change the query settings size.
        ASSERT_OK(SetFilter::set(&txn, &querySettings, &planCache, ns,
            fromjson("{query: {b: 2, a: 3}, sort: {a: -1}, projection: {_id: 0, a: 1}, "
                     "indexes: [{a: 1, b: 1}]}")));
        filters = getFilters(querySettings);
        ASSERT_EQUALS(filters.size(), 1U);

        // Add hint for different query shape.
        ASSERT_OK(SetFilter::set(&txn, &querySettings, &planCache, ns,
                                 fromjson("{query: {b: 1}, indexes: [{b: 1}]}")));
        filters = getFilters(querySettings);
        ASSERT_EQUALS(filters.size(), 2U);

        // Add hint for 3rd query shape. This is to prepare for ClearHint tests.
        ASSERT_OK(SetFilter::set(&txn, &querySettings, &planCache, ns,
                                 fromjson("{query: {a: 1}, indexes: [{a: 1}]}")));
        filters = getFilters(querySettings);
        ASSERT_EQUALS(filters.size(), 3U);

        // Add 2 entries to plan cache and check plan cache after clearing one/all filters.
        addQueryShapeToPlanCache(&planCache, "{a: 1}", "{}", "{}");
        addQueryShapeToPlanCache(&planCache, "{b: 1}", "{}", "{}");

        // Clear single hint.
        ASSERT_OK(ClearFilters::clear(&txn, &querySettings, &planCache, ns,
                                    fromjson("{query: {a: 1}}")));
        filters = getFilters(querySettings);
        ASSERT_EQUALS(filters.size(), 2U);

        // Query shape should not exist in plan cache after cleaing 1 hint.
        ASSERT_FALSE(planCacheContains(planCache, "{a: 1}", "{}", "{}"));
        ASSERT_TRUE(planCacheContains(planCache, "{b: 1}", "{}", "{}"));

        // Clear all filters
        ASSERT_OK(ClearFilters::clear(&txn, &querySettings, &planCache, ns, fromjson("{}")));
        filters = getFilters(querySettings);
        ASSERT_TRUE(filters.empty());

        // {b: 1} should be gone from plan cache after flushing query settings.
        ASSERT_FALSE(planCacheContains(planCache, "{b: 1}", "{}", "{}"));
    }

}  // namespace