summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_index_use.js
blob: dda7471dd5a2f79336040f0cd49c6d5a78b020fb (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
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
/**
 * Tests index usage on meta and time fields for timeseries collections.
 *
 * @tags: [
 *   # Explain of a resolved view must be executed by mongos.
 *   directly_against_shardsvrs_incompatible,
 *   # Refusing to run a test that issues an aggregation command with explain because it may return
 *   # incomplete results if interrupted by a stepdown.
 *   does_not_support_stepdowns,
 *   # Tests that optimization produces expected query plans.
 *   requires_pipeline_optimization,
 *   # We need a timeseries collection.
 *   requires_timeseries,
 * ]
 */
(function() {
"use strict";

load("jstests/core/timeseries/libs/timeseries.js");
load("jstests/libs/analyze_plan.js");

const generateTest = (useHint) => {
    return (insert) => {
        const testDB = db.getSiblingDB(jsTestName());
        assert.commandWorked(testDB.dropDatabase());

        // Create timeseries collection.
        const timeFieldName = 'ts';
        const metaFieldName = 'mm';
        const coll = testDB.getCollection('t');
        const bucketsColl = testDB.getCollection('system.buckets.' + coll.getName());

        /**
         * Sets up an empty time-series collection with options 'collOpts' on namespace 't' using
         * 'timeFieldName' and 'metaFieldName'. Checks that the buckets collection is created, as
         * well.
         */
        function resetCollections(collOpts = {}) {
            coll.drop();  // implicitly drops bucketsColl.

            assert.commandWorked(testDB.createCollection(
                coll.getName(),
                Object.assign({timeseries: {timeField: timeFieldName, metaField: metaFieldName}},
                              collOpts)));
            if (TimeseriesTest.timeseriesScalabilityImprovementsEnabled(db)) {
                // When enabled, the {meta: 1, time: 1} index gets built by default on the
                // time-series bucket collection. When this index is present, the query planner will
                // use it, changing the expected behaviour of this test. Drop the index.
                assert.commandWorked(coll.dropIndex({[metaFieldName]: 1, [timeFieldName]: 1}));
            }

            const dbCollNames = testDB.getCollectionNames();
            assert.contains(bucketsColl.getName(),
                            dbCollNames,
                            "Failed to find namespace '" + bucketsColl.getName() +
                                "' amongst: " + tojson(dbCollNames));
        }

        /**
         * Creates the index specified by the spec and options, then explains the query to ensure
         * that the created index is used or was considered by multi-planner.
         * Runs the query and verifies that the expected number of documents are matched.
         * Finally, deletes the created index.
         */
        const testQueryUsesIndex = function(
            filter, numMatches, indexSpec, indexOpts = {}, queryOpts = {}) {
            assert.commandWorked(
                coll.createIndex(indexSpec, Object.assign({name: "testIndexName"}, indexOpts)));

            let query = coll.find(filter);
            if (useHint)
                query = query.hint(indexSpec);
            if (queryOpts.collation)
                query = query.collation(queryOpts.collation);

            assert.eq(numMatches, query.itcount());

            const explain = query.explain();
            if (useHint) {
                const ixscan = getAggPlanStage(explain, "IXSCAN");
                assert.neq(null, ixscan, tojson(explain));
                assert.eq("testIndexName", ixscan.indexName, tojson(ixscan));
            } else {
                let ixscan = getAggPlanStage(explain, "IXSCAN");
                // If ixscan is not present, check rejected plans
                if (ixscan === null) {
                    const rejectedPlans =
                        getRejectedPlans(getAggPlanStage(explain, "$cursor")["$cursor"]);
                    assert.eq(1, rejectedPlans.length);
                    const ixscans = getPlanStages(getRejectedPlan(rejectedPlans[0]), "IXSCAN");
                    assert.eq(1, ixscans.length);
                    ixscan = ixscans[0];
                }
                assert.eq("testIndexName", ixscan.indexName, tojson(ixscan));
            }
            assert.commandWorked(coll.dropIndex("testIndexName"));
        };

        /**
         * Creates the index specified by the spec and options, then explains the query to ensure
         * that the created index is used. Runs the query and verifies that the expected number of
         * documents are matched. Finally, deletes the created index.
         */
        const testAggregationUsesIndex = function(
            pipeline, numMatches, indexSpec, stageType = "IXSCAN", indexOpts = {}) {
            assert.commandWorked(
                coll.createIndex(indexSpec, Object.assign({name: "testIndexName"}, indexOpts)));

            let aggregation = coll.aggregate(pipeline);
            assert.eq(numMatches, aggregation.itcount());

            const options = useHint ? {hint: indexSpec} : {};
            const explain = coll.explain().aggregate(pipeline, options);
            const ixscan = getAggPlanStage(explain, stageType);
            assert.neq(null, ixscan, tojson(explain));
            assert.eq("testIndexName", ixscan.indexName, tojson(ixscan));

            assert.commandWorked(coll.dropIndex("testIndexName"));
        };

        /******************************* Tests scalar meta values *********************************/
        resetCollections();
        assert.commandWorked(insert(coll, [
            {_id: 0, [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'), [metaFieldName]: 2},
            {_id: 1, [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'), [metaFieldName]: 3},
            {_id: 2, [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'), [metaFieldName]: 2}
        ]));

        const timeDate = ISODate('2005-01-01 00:00:00.000Z');

        // Test ascending and descending index on timeField.
        if (!FixtureHelpers.isSharded(bucketsColl)) {
            // Skip if the collection is implicitly sharded: it may use the implicitly created
            // index.
            testQueryUsesIndex({[timeFieldName]: {$lte: timeDate}}, 2, {[timeFieldName]: 1});
            testQueryUsesIndex({[timeFieldName]: {$gte: timeDate}}, 1, {[timeFieldName]: -1});
        }

        // Test ascending and descending index on metaField.
        testQueryUsesIndex({[metaFieldName]: {$eq: 3}}, 1, {[metaFieldName]: 1});
        testQueryUsesIndex({[metaFieldName]: {$lt: 3}}, 2, {[metaFieldName]: -1});

        // Test compound indexes on metaField and timeField.
        if (!FixtureHelpers.isSharded(bucketsColl)) {
            // Skip if the collection is implicitly sharded: it may use the implicitly created
            // index.
            testQueryUsesIndex(
                {[metaFieldName]: {$gte: 2}}, 3, {[metaFieldName]: 1, [timeFieldName]: 1});
            testQueryUsesIndex(
                {[timeFieldName]: {$lt: timeDate}}, 2, {[timeFieldName]: 1, [metaFieldName]: 1});
            testQueryUsesIndex({[metaFieldName]: {$lte: 3}, [timeFieldName]: {$gte: timeDate}},
                               1,
                               {[metaFieldName]: 1, [timeFieldName]: 1});
            testQueryUsesIndex({[metaFieldName]: {$eq: 2}, [timeFieldName]: {$lte: timeDate}},
                               1,
                               {[metaFieldName]: -1, [timeFieldName]: -1});
            testQueryUsesIndex({[metaFieldName]: {$lt: 3}, [timeFieldName]: {$lte: timeDate}},
                               1,
                               {[timeFieldName]: 1, [metaFieldName]: -1});
        }

        /******************************** Tests object meta values ********************************/
        resetCollections();
        assert.commandWorked(insert(coll, [
            {_id: 0, [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'), [metaFieldName]: {a: 1}},
            {
                _id: 1,
                [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'),
                [metaFieldName]: {a: 4, b: 5, loc: [1.0, 2.0]}
            },
            {
                _id: 2,
                [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'),
                [metaFieldName]: {a: "1", c: 0, loc: [-1.0, -2.0]}
            }
        ]));

        // Test indexes on subfields of metaField.
        testQueryUsesIndex({[metaFieldName + '.a']: {$gt: 3}}, 1, {[metaFieldName + '.a']: 1});
        testQueryUsesIndex(
            {[metaFieldName + '.a']: {$type: 'string'}}, 1, {[metaFieldName + '.a']: -1});
        testQueryUsesIndex(
            {[metaFieldName + '.b']: {$gte: 0}}, 1, {[metaFieldName + '.b']: 1}, {sparse: true});
        testQueryUsesIndex({[metaFieldName]: {$eq: {a: 1}}}, 1, {[metaFieldName]: 1});
        testQueryUsesIndex({[metaFieldName]: {$in: [{a: 1}, {a: 4, b: 5, loc: [1.0, 2.0]}]}},
                           2,
                           {[metaFieldName]: 1});

        // Test compound indexes on multiple subfields of metaField.
        testQueryUsesIndex({[metaFieldName + '.a']: {$lt: 3}},
                           1,
                           {[metaFieldName + '.a']: 1, [metaFieldName + '.b']: -1});
        testQueryUsesIndex({[metaFieldName + '.a']: {$lt: 5}, [metaFieldName + '.b']: {$eq: 5}},
                           1,
                           {[metaFieldName + '.a']: -1, [metaFieldName + '.b']: 1});
        testQueryUsesIndex(
            {$or: [{[metaFieldName + '.a']: {$eq: 1}}, {[metaFieldName + '.a']: {$eq: "1"}}]},
            2,
            {[metaFieldName + '.a']: -1, [metaFieldName + '.b']: -1});
        testQueryUsesIndex({[metaFieldName + '.b']: {$lte: 5}},
                           1,
                           {[metaFieldName + '.b']: 1, [metaFieldName + '.c']: 1},
                           {sparse: true});
        testQueryUsesIndex({[metaFieldName + '.b']: {$lte: 5}, [metaFieldName + '.c']: {$lte: 4}},
                           0,
                           {[metaFieldName + '.b']: 1, [metaFieldName + '.c']: 1},
                           {sparse: true});

        // Test compound indexes on timeField and subfields of metaField.
        if (!FixtureHelpers.isSharded(bucketsColl)) {
            // Skip if the collection is implicitly sharded: it may use the implicitly created
            // index.
            testQueryUsesIndex({[metaFieldName + '.a']: {$gte: 2}},
                               1,
                               {[metaFieldName + '.a']: 1, [timeFieldName]: 1});
            testQueryUsesIndex({[timeFieldName]: {$lt: timeDate}},
                               2,
                               {[timeFieldName]: 1, [metaFieldName + '.a']: 1});
            testQueryUsesIndex(
                {[metaFieldName + '.a']: {$lte: 4}, [timeFieldName]: {$lte: timeDate}},
                2,
                {[metaFieldName + '.a']: 1, [timeFieldName]: 1});
            testQueryUsesIndex(
                {[metaFieldName + '.a']: {$lte: 4}, [timeFieldName]: {$lte: timeDate}},
                2,
                {[metaFieldName + '.a']: 1, [timeFieldName]: -1});
            testQueryUsesIndex(
                {[metaFieldName + '.a']: {$eq: "1"}, [timeFieldName]: {$gt: timeDate}},
                1,
                {[timeFieldName]: -1, [metaFieldName + '.a']: 1});
        }

        // Test wildcard indexes with metaField.
        testQueryUsesIndex({[metaFieldName + '.a']: {$lt: 3}}, 1, {[metaFieldName + '.$**']: 1});
        testQueryUsesIndex({[metaFieldName + '.b']: {$gt: 3}}, 1, {[metaFieldName + '.$**']: 1});

        // Test hashed indexes on metaField.
        testQueryUsesIndex({[metaFieldName]: {$eq: {a: 1}}}, 1, {[metaFieldName]: "hashed"});
        testQueryUsesIndex(
            {[metaFieldName + '.a']: {$eq: 1}}, 1, {[metaFieldName + '.a']: "hashed"});
        testQueryUsesIndex({[metaFieldName + '.a']: {$eq: 1}},
                           1,
                           {[metaFieldName + '.a']: "hashed", [metaFieldName + '.b']: -1});
        testQueryUsesIndex({[metaFieldName + '.a']: {$eq: 1}, [metaFieldName + '.b']: {$gt: 0}},
                           0,
                           {[metaFieldName + '.a']: "hashed", [metaFieldName + '.b']: -1});
        testQueryUsesIndex({[metaFieldName + '.a']: {$eq: 1}, [metaFieldName + '.b']: {$gt: 0}},
                           0,
                           {[metaFieldName + '.b']: -1, [metaFieldName + '.a']: "hashed"});

        // Test geo-type indexes on metaField.
        testQueryUsesIndex({
            [metaFieldName + '.loc']: {
                $geoWithin: {
                    $geometry:
                        {type: "Polygon", coordinates: [[[0, 0], [0, 3], [3, 3], [3, 0], [0, 0]]]}
                }
            }
        },
                           1,
                           {[metaFieldName + '.loc']: '2dsphere'});
        testQueryUsesIndex({[metaFieldName + '.loc']: {$geoWithin: {$center: [[1.01, 2.01], 0.1]}}},
                           1,
                           {[metaFieldName + '.loc']: '2d'});
        testAggregationUsesIndex(
            [
                {
                    $geoNear: {
                        near: {type: "Point", coordinates: [40.4, -70.4]},
                        distanceField: "dist",
                        spherical: true,
                        key: metaFieldName + '.loc'
                    }
                },
                {$limit: 1}
            ],
            1,
            {[metaFieldName + '.loc']: '2dsphere'},
            "GEO_NEAR_2DSPHERE");
        testAggregationUsesIndex(
            [
                {
                    $geoNear:
                        {near: [40.4, -70.4], distanceField: "dist", key: metaFieldName + '.loc'}
                },
                {$limit: 1}
            ],
            1,
            {[metaFieldName + '.loc']: '2d'},
            "GEO_NEAR_2D");

        /********************************* Tests array meta values ********************************/
        resetCollections();
        assert.commandWorked(insert(coll, [
            {
                _id: 0,
                [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'),
                [metaFieldName]: [1, 2, 3]
            },
            {
                _id: 1,
                [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'),
                [metaFieldName]: ['a', 'b']
            },
            {
                _id: 2,
                [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'),
                [metaFieldName]: [{a: 1}, {b: 0}]
            }
        ]));

        // Test multikey indexes on metaField.
        testQueryUsesIndex({[metaFieldName]: {$eq: {a: 1}}}, 1, {[metaFieldName]: 1});
        testQueryUsesIndex({[metaFieldName]: {$eq: 2}}, 1, {[metaFieldName]: 1});
        testQueryUsesIndex({[metaFieldName]: {$gte: {a: 1}}}, 1, {[metaFieldName]: -1});

        resetCollections();
        assert.commandWorked(insert(coll, [
            {
                _id: 0,
                [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'),
                [metaFieldName]: {a: [1, 2, 3], b: 0}
            },
            {
                _id: 1,
                [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'),
                [metaFieldName]: {a: ['a', 'b'], b: 1}
            },
            {
                _id: 2,
                [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'),
                [metaFieldName]: {a: [{b: 1}, {c: 0}]}
            }
        ]));

        // Test multikey indexes on subfields of metaFields.
        testQueryUsesIndex({[metaFieldName + '.a']: {$eq: {b: 1}}}, 1, {[metaFieldName + '.a']: 1});
        testQueryUsesIndex({[metaFieldName + '.a']: {$eq: 2}}, 1, {[metaFieldName + '.a']: 1});
        testQueryUsesIndex(
            {[metaFieldName + '.a']: {$gte: {a: 1}}}, 1, {[metaFieldName + '.a']: -1});
        testQueryUsesIndex(
            {[metaFieldName + '.a']: {$gte: 1}, [metaFieldName + '.b']: {$exists: 1}},
            1,
            {[metaFieldName + '.a']: -1, [metaFieldName + '.b']: 1});

        /********************************* Tests string meta values *******************************/
        const collation = {collation: {locale: 'en', strength: 1, numericOrdering: true}};

        // Create timeseries collection with a collation.
        resetCollections(collation);
        assert.commandWorked(insert(coll, [
            {
                _id: 0,
                [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'),
                [metaFieldName]: "hello hello"
            },
            {
                _id: 1,
                [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'),
                [metaFieldName]: "hello world"
            },
            {
                _id: 2,
                [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'),
                [metaFieldName]: "bye bye"
            }
        ]));

        // Test index on metaField when collection collation matches query collation.
        testQueryUsesIndex(
            {[metaFieldName]: {$eq: "bye bye"}}, 1, {[metaFieldName]: 1}, {}, collation);
        testQueryUsesIndex(
            {[metaFieldName]: {$gte: "hello hello"}}, 2, {[metaFieldName]: -1}, {}, collation);

        resetCollections(collation);
        assert.commandWorked(insert(coll, [
            {
                _id: 0,
                [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'),
                [metaFieldName]: {a: "hello hello", b: "hello"}
            },
            {
                _id: 1,
                [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'),
                [metaFieldName]: {a: "hello world", b: "hello"}
            },
            {
                _id: 2,
                [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'),
                [metaFieldName]: {a: "bye bye", b: "bye"}
            }
        ]));

        // Test index on subfields of metaField when collection collation matches query collation.
        testQueryUsesIndex({[metaFieldName + '.a']: {$eq: "bye bye"}},
                           1,
                           {[metaFieldName + '.a']: 1},
                           {},
                           collation);
        testQueryUsesIndex({[metaFieldName + '.b']: {$gt: "bye bye"}},
                           2,
                           {[metaFieldName + '.b']: -1},
                           {},
                           collation);

        /*********************************** Tests $expr predicates *******************************/
        resetCollections();
        assert.commandWorked(insert(coll, [
            {_id: 0, [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'), [metaFieldName]: 2},
            {_id: 1, [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'), [metaFieldName]: 3},
            {_id: 2, [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'), [metaFieldName]: 2}
        ]));

        if (!FixtureHelpers.isSharded(bucketsColl)) {
            // Skip if the collection is implicitly sharded: it may use the implicitly created
            // index.
            testAggregationUsesIndex([{
                                         $match: {
                                             $expr: {
                                                 $and: [
                                                     {$gt: ["$" + timeFieldName, timeDate]},
                                                     {$eq: ["$" + metaFieldName, 2]}
                                                 ]
                                             }
                                         }
                                     }],
                                     1,
                                     {[timeFieldName]: 1, [metaFieldName]: 1});
        }
    };
};

// Run the test twice, once without hinting the index, and again hinting the index by spec.
TimeseriesTest.run(generateTest(false));
TimeseriesTest.run(generateTest(true));
})();