summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_index_partial.js
blob: 7d97209173ed81695483aecc61b6fcf6e22afddd (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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/**
 * Test creating and using partial indexes, on a time-series collection.
 *
 * @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,
 *   # We need a timeseries collection.
 *   requires_timeseries,
 * ]
 */
(function() {
"use strict";

load("jstests/libs/analyze_plan.js");
load("jstests/libs/feature_flag_util.js");

if (!FeatureFlagUtil.isEnabled(db, "TimeseriesMetricIndexes")) {
    jsTestLog(
        "Skipped test as the featureFlagTimeseriesMetricIndexes feature flag is not enabled.");
    return;
}

const coll = db.timeseries_index_partial;
const timeField = 'time';
const metaField = 'm';

coll.drop();
assert.commandWorked(db.createCollection(coll.getName(), {timeseries: {timeField, metaField}}));

const buckets = db.getCollection('system.buckets.' + coll.getName());
let extraIndexes = [];
let extraBucketIndexes = [];
if (FixtureHelpers.isSharded(buckets)) {
    // If the collection is sharded, expect an implicitly-created index on time.
    // It will appear differently in listIndexes depending on whether you look at the time-series
    // collection or the buckets collection.
    extraIndexes.push({
        "v": 2,
        "key": {"time": 1},
        "name": "control.min.time_1",
    });
    extraBucketIndexes.push({
        "v": 2,
        "key": {"control.min.time": 1},
        "name": "control.min.time_1",
    });
}

if (FeatureFlagUtil.isEnabled(db, "TimeseriesScalabilityImprovements")) {
    // When enabled, the {meta: 1, time: 1} index gets built by default on the time-series
    // bucket collection.
    extraIndexes.push({
        "v": 2,
        "key": {"m": 1, "time": 1},
        "name": "m_1_time_1",
    });
    extraBucketIndexes.push({
        "v": 2,
        "key": {"meta": 1, "control.min.time": 1, "control.max.time": 1},
        "name": "m_1_time_1",
    });
}

assert.sameMembers(coll.getIndexes(), extraIndexes);
assert.sameMembers(buckets.getIndexes(), extraBucketIndexes);

assert.commandWorked(coll.insert([
    // In bucket A, some but not all documents match the partial filter.
    {[timeField]: ISODate('2000-01-01T00:00:00Z'), [metaField]: {bucket: 'A'}, a: 0, b: 20},
    {[timeField]: ISODate('2000-01-01T00:00:01Z'), [metaField]: {bucket: 'A'}, a: 1, b: 16},
    {[timeField]: ISODate('2000-01-01T00:00:02Z'), [metaField]: {bucket: 'A'}, a: 2, b: 12},
    {[timeField]: ISODate('2000-01-01T00:00:03Z'), [metaField]: {bucket: 'A'}, a: 3, b: 8},
    {[timeField]: ISODate('2000-01-01T00:00:04Z'), [metaField]: {bucket: 'A'}, a: 4, b: 4},

    // In bucket B, no documents match the partial filter.
    {[timeField]: ISODate('2000-01-01T00:00:00Z'), [metaField]: {bucket: 'B'}, a: 5, b: 99},
    {[timeField]: ISODate('2000-01-01T00:00:01Z'), [metaField]: {bucket: 'B'}, a: 6, b: 99},
    {[timeField]: ISODate('2000-01-01T00:00:02Z'), [metaField]: {bucket: 'B'}, a: 7, b: 99},
    {[timeField]: ISODate('2000-01-01T00:00:03Z'), [metaField]: {bucket: 'B'}, a: 8, b: 99},
    {[timeField]: ISODate('2000-01-01T00:00:04Z'), [metaField]: {bucket: 'B'}, a: 9, b: 99},

    // In bucket C, every document matches the partial filter.
    {[timeField]: ISODate('2000-01-01T00:00:00Z'), [metaField]: {bucket: 'C'}, a: 10, b: 0},
    {[timeField]: ISODate('2000-01-01T00:00:01Z'), [metaField]: {bucket: 'C'}, a: 11, b: 0},
    {[timeField]: ISODate('2000-01-01T00:00:02Z'), [metaField]: {bucket: 'C'}, a: 12, b: 0},
    {[timeField]: ISODate('2000-01-01T00:00:03Z'), [metaField]: {bucket: 'C'}, a: 13, b: 0},
    {[timeField]: ISODate('2000-01-01T00:00:04Z'), [metaField]: {bucket: 'C'}, a: 14, b: 0},
]));
assert.eq(15, coll.count());
assert.eq(3, buckets.count());

// Expected partialFilterExpression to be an object.
assert.commandFailedWithCode(coll.createIndex({a: 1}, {partialFilterExpression: 123}), [10065]);

// Test creating and using a partial index.
{
    let ixscanInWinningPlan = 0;

    // Make sure the {a: 1} index was considered for this query.
    function checkPlan(predicate) {
        const explain = coll.find(predicate).explain();
        let scan = getAggPlanStage(explain, 'IXSCAN');
        // If scan is not present, check rejected plans
        if (scan === null) {
            const rejectedPlans = getRejectedPlans(getAggPlanStage(explain, "$cursor")["$cursor"]);
            if (rejectedPlans.length === 1) {
                const scans = getPlanStages(getRejectedPlan(rejectedPlans[0]), "IXSCAN");
                if (scans.length === 1) {
                    scan = scans[0];
                }
            }
        } else {
            ixscanInWinningPlan++;
        }
        const indexes = buckets.getIndexes();
        assert(scan,
               "Expected an index scan for predicate: " + tojson(predicate) +
                   " but got: " + tojson(explain) + "\nAvailable indexes were: " + tojson(indexes));
        assert.eq(scan.indexName, "a_1", scan);
    }
    // Make sure the query results match a collection-scan plan.
    function checkResults(predicate) {
        const result = coll.aggregate([{$match: predicate}], {hint: {a: 1}}).toArray();
        const unindexed =
            coll.aggregate([{$_internalInhibitOptimization: {}}, {$match: predicate}]).toArray();
        assert.docEq(result, unindexed);
    }
    function checkPlanAndResults(predicate) {
        checkPlan(predicate);
        checkResults(predicate);
    }
    const check = checkPlanAndResults;

    // Test some predicates on a metric field.
    {
        assert.commandWorked(coll.createIndex({a: 1}, {partialFilterExpression: {b: {$lt: 12}}}));
        // Query predicate mentions partialFilterExpression exactly.
        // The extra predicate on 'a' is necessary for the multiplanner to think an {a: 1} index is
        // relevant.
        check({a: {$lt: 999}, b: {$lt: 12}});
        // Query predicate is a subset of partialFilterExpression.
        check({a: {$lt: 999}, b: {$lt: 11}});

        assert.commandWorked(coll.dropIndex({a: 1}));
        assert.commandWorked(coll.createIndex({a: 1}, {partialFilterExpression: {b: {$lte: 12}}}));
        check({a: {$lt: 999}, b: {$lte: 12}});
        check({a: {$lt: 999}, b: {$lte: 11}});

        assert.commandWorked(coll.dropIndex({a: 1}));
        assert.commandWorked(coll.createIndex({a: 1}, {partialFilterExpression: {b: {$gt: 12}}}));
        check({a: {$lt: 999}, b: {$gt: 12}});
        check({a: {$lt: 999}, b: {$gt: 13}});

        assert.commandWorked(coll.dropIndex({a: 1}));
        assert.commandWorked(coll.createIndex({a: 1}, {partialFilterExpression: {b: {$gte: 12}}}));
        check({a: {$lt: 999}, b: {$gte: 12}});
        check({a: {$lt: 999}, b: {$gte: 13}});
    }

    // Test some predicates on the time field.
    {
        const t0 = ISODate('2000-01-01T00:00:00Z');
        const t1 = ISODate('2000-01-01T00:00:01Z');
        const t2 = ISODate('2000-01-01T00:00:02Z');

        assert.commandWorked(coll.dropIndex({a: 1}));
        assert.commandWorked(
            coll.createIndex({a: 1}, {partialFilterExpression: {[timeField]: {$lt: t1}}}));
        check({a: {$lt: 999}, [timeField]: {$lt: t1}});
        check({a: {$lt: 999}, [timeField]: {$lt: t0}});

        assert.commandWorked(coll.dropIndex({a: 1}));
        assert.commandWorked(
            coll.createIndex({a: 1}, {partialFilterExpression: {[timeField]: {$lte: t1}}}));
        check({a: {$lt: 999}, [timeField]: {$lte: t1}});
        check({a: {$lt: 999}, [timeField]: {$lte: t0}});

        assert.commandWorked(coll.dropIndex({a: 1}));
        assert.commandWorked(
            coll.createIndex({a: 1}, {partialFilterExpression: {[timeField]: {$gt: t1}}}));
        check({a: {$lt: 999}, [timeField]: {$gt: t1}});
        check({a: {$lt: 999}, [timeField]: {$gt: t2}});

        assert.commandWorked(coll.dropIndex({a: 1}));
        assert.commandWorked(
            coll.createIndex({a: 1}, {partialFilterExpression: {[timeField]: {$gte: t1}}}));
        check({a: {$lt: 999}, [timeField]: {$gte: t1}});
        check({a: {$lt: 999}, [timeField]: {$gte: t2}});
    }

    assert.commandWorked(coll.dropIndex({a: 1}));
    assert.sameMembers(coll.getIndexes(), extraIndexes);
    assert.sameMembers(buckets.getIndexes(), extraBucketIndexes);
    assert.gt(ixscanInWinningPlan, 0);
}

// Check that partialFilterExpression can use a mixture of metadata, time, and measurement fields,
// and that this is translated to the bucket-level predicate we expect.
assert.commandWorked(coll.createIndex({a: 1}, {
    partialFilterExpression: {
        $and: [
            {time: {$gt: ISODate("2000-01-01")}},
            {[metaField + '.bucket']: {$gte: "B"}},
            {b: {$gte: 0}},
        ]
    }
}));
assert.sameMembers(buckets.getIndexes(), extraBucketIndexes.concat([
    {
        "v": 2,
        "key": {"control.min.a": 1, "control.max.a": 1},
        "name": "a_1",

        "partialFilterExpression": {
            // Meta predicates are pushed down verbatim.
            ['meta.bucket']: {"$gte": "B"},
            $and: [
                {
                    $and: [
                        // $gt on time creates a bound on the max time.
                        {"control.max.time": {"$_internalExprGt": ISODate("2000-01-01T00:00:00Z")}},
                        // We also have a bound on the min time, derived from bucketMaxSpanSeconds.
                        {"control.min.time": {"$_internalExprGt": ISODate("1999-12-31T23:00:00Z")}},
                        // The min time is also encoded in the _id, so we have a bound on that as
                        // well.
                        {"_id": {"$gt": ObjectId("386d3570ffffffffffffffff")}},
                    ]
                },
                // $gt on a non-time field can only bound the control.max for that field.
                {"control.max.b": {"$_internalExprGte": 0}},
            ]
        },
        "originalSpec": {
            key: {a: 1},
            name: "a_1",
            partialFilterExpression: {
                $and: [
                    {time: {$gt: ISODate("2000-01-01")}},
                    {[metaField + '.bucket']: {$gte: "B"}},
                    {b: {$gte: 0}},
                ]
            },
            v: 2,
        }
    },
]));

// Test how partialFilterExpression interacts with collation.
{
    const numericCollation = {locale: "en_US", numericOrdering: true};
    coll.drop();
    assert.commandWorked(db.createCollection(coll.getName(), {
        timeseries: {timeField, metaField},
        collation: numericCollation,
    }));

    assert.commandWorked(coll.insert([
        {[timeField]: ISODate(), [metaField]: {x: "1000", y: 1}, a: "120"},
        {[timeField]: ISODate(), [metaField]: {x: "1000", y: 2}, a: "3"},
        {[timeField]: ISODate(), [metaField]: {x: "500", y: 3}, a: "120"},
        {[timeField]: ISODate(), [metaField]: {x: "500", y: 4}, a: "3"},
    ]));

    // Queries on the collection use the collection's collation by default.
    assert.docEq(
        coll.find({}, {_id: 0, [metaField + '.x']: 1}).sort({[metaField + '.x']: 1}).toArray(), [
            {[metaField]: {x: "500"}},
            {[metaField]: {x: "500"}},
            {[metaField]: {x: "1000"}},
            {[metaField]: {x: "1000"}},
        ]);
    assert.docEq(coll.find({}, {_id: 0, a: 1}).sort({a: 1}).toArray(), [
        {a: "3"},
        {a: "3"},
        {a: "120"},
        {a: "120"},
    ]);

    // Specifying a collation and partialFilterExpression together fails, even if the collation
    // matches the collection's default collation.
    assert.commandFailedWithCode(coll.createIndex({a: 1}, {
        name: "a_lt_25_simple",
        collation: {locale: "simple"},
        partialFilterExpression: {a: {$lt: "25"}}
    }),
                                 [ErrorCodes.IndexOptionsConflict]);
    assert.commandFailedWithCode(
        coll.createIndex({a: 1}, {
            name: "a_lt_25_numeric",
            collation: numericCollation,
            partialFilterExpression: {a: {$lt: "25"}}
        }),
        // The default collation is also numeric, so this index is equivalent to the previous.
        [ErrorCodes.IndexOptionsConflict]);

    assert.commandWorked(coll.createIndex(
        {a: 1}, {name: "a_lt_25_default", partialFilterExpression: {a: {$lt: "25"}}}));

    // Verify that the index contains what we expect.
    assert.docEq(coll.find({}, {_id: 0, a: 1}).hint("a_lt_25_default").toArray(),
                 [{a: "3"}, {a: "3"}]);

    // Verify that the index is used when possible.
    function checkPlanAndResult({predicate, collation, stageName, indexName, expectedResults}) {
        let cur = coll.find(predicate, {_id: 0, a: 1});
        if (collation) {
            cur.collation(collation);
        }

        const plan = cur.explain();
        const stage = getAggPlanStage(plan, stageName);
        assert(stage, "Expected a " + stageName + " stage: " + tojson(plan));
        if (indexName) {
            assert.eq(stage.indexName, indexName, stage);
        }

        const results = cur.toArray();
        assert.docEq(results, expectedResults);
    }

    // a < "25" can use the index, since the collations match.
    checkPlanAndResult({
        predicate: {a: {$lt: "25"}},
        collation: null,
        stageName: 'IXSCAN',
        indexName: 'a_lt_25_default',
        expectedResults: [{a: "3"}, {a: "3"}],
    });
    checkPlanAndResult({
        predicate: {a: {$lt: "25"}},
        collation: numericCollation,
        stageName: 'IXSCAN',
        indexName: 'a_lt_25_default',
        expectedResults: [{a: "3"}, {a: "3"}],
    });

    // Likewise a < "24" can use the index.
    checkPlanAndResult({
        predicate: {a: {$lt: "24"}},
        collation: null,
        stageName: 'IXSCAN',
        indexName: 'a_lt_25_default',
        expectedResults: [{a: "3"}, {a: "3"}],
    });
    checkPlanAndResult({
        predicate: {a: {$lt: "24"}},
        collation: numericCollation,
        stageName: 'IXSCAN',
        indexName: 'a_lt_25_default',
        expectedResults: [{a: "3"}, {a: "3"}],
    });

    // a < "30" can't use the index; it's not a subset.
    checkPlanAndResult({
        predicate: {a: {$lt: "30"}},
        collation: null,
        stageName: 'COLLSCAN',
        indexName: null,
        expectedResults: [{a: "3"}, {a: "3"}],
    });
    checkPlanAndResult({
        predicate: {a: {$lt: "30"}},
        collation: numericCollation,
        stageName: 'COLLSCAN',
        indexName: null,
        expectedResults: [{a: "3"}, {a: "3"}],
    });

    // a < "100" also can't use the index, because according to the numeric collation, "20" < "100".
    // Note that if we were using a simple collation we'd get the opposite outcome: "100" < "20",
    // because it would compare strings lexicographically instead of numerically.
    checkPlanAndResult({
        predicate: {a: {$lt: "100"}},
        collation: null,
        stageName: 'COLLSCAN',
        indexName: null,
        expectedResults: [{a: "3"}, {a: "3"}],
    });
    checkPlanAndResult({
        predicate: {a: {$lt: "100"}},
        collation: numericCollation,
        stageName: 'COLLSCAN',
        indexName: null,
        expectedResults: [{a: "3"}, {a: "3"}],
    });
}

// Test which types of predicates are allowed, and test that the bucket-level
// partialFilterExpression is what we expect.
{
    assert.commandWorked(coll.dropIndex({a: 1}));
    assert.sameMembers(coll.getIndexes(), extraIndexes);
    function checkPredicateDisallowed(predicate) {
        assert.commandFailedWithCode(coll.createIndex({a: 1}, {partialFilterExpression: predicate}),
                                     [5916301]);
    }
    function checkPredicateOK({input: predicate, output: expectedBucketPredicate}) {
        const name = 'example_pushdown_index';
        assert.commandWorked(coll.createIndex({a: 1}, {name, partialFilterExpression: predicate}));
        const indexes = buckets.getIndexes().filter(ix => ix.name === name);
        assert.eq(1, indexes.length, "Expected 1 index but got " + tojson(indexes));
        const actualBucketPredicate = indexes[0].partialFilterExpression;
        assert.eq(actualBucketPredicate,
                  expectedBucketPredicate,
                  "Expected the bucket-level predicate to be " + tojson(expectedBucketPredicate) +
                      " but it was " + tojson(actualBucketPredicate));
        assert.commandWorked(coll.dropIndex(name));
    }
    // A trivial, empty predicate is fine.
    // It doesn't seem useful but it's allowed on a normal collection.
    checkPredicateOK({
        input: {},
        output: {},
    });
    // Comparison with non-scalar.
    checkPredicateDisallowed({a: {}});
    checkPredicateDisallowed({a: {b: 3}});
    checkPredicateDisallowed({a: []});
    checkPredicateDisallowed({a: [1]});
    checkPredicateDisallowed({a: [1, 2]});
    // Always-false predicate on time (which is always a Date).
    checkPredicateDisallowed({time: 7});
    checkPredicateDisallowed({time: "abc"});

    // Scalar $eq is equivalent to a conjunction of $lte and $gte.
    checkPredicateOK({
        input: {a: 5},
        output: {
            $and: [
                {'control.min.a': {$_internalExprLte: 5}},
                {'control.max.a': {$_internalExprGte: 5}},
            ]
        }
    });

    // Comparisons with null/missing are not implemented. These would be slightly more complicated
    // because {$eq: null} actually means "null, or missing, or undefined",
    // while {$_internalExprEq: null} only matches null.
    checkPredicateDisallowed({a: null});
    checkPredicateDisallowed({a: {$eq: null}});
    checkPredicateDisallowed({a: {$ne: null}});

    // Regex queries are not allowed, but {$eq: /.../} is a simple scalar match, not a regex query.
    checkPredicateDisallowed({a: /a/});
    checkPredicateOK({
        input: {a: {$eq: /a/}},
        output: {
            $and: [
                {'control.min.a': {$_internalExprLte: /a/}},
                {'control.max.a': {$_internalExprGte: /a/}},
            ]
        }
    });

    // The min/max for a field is present iff at least one event in the bucket has that field.
    // So {$exists: true} queries can be mapped to the min/max for that field.
    // This can be used as an alternative to sparse indexes.
    checkPredicateOK({
        input: {a: {$exists: true}},
        output: {
            $and: [
                {'control.min.a': {$exists: true}},
                {'control.max.a': {$exists: true}},
            ]
        }
    });
    // However, this means we can't push down {$exists: false}.  A bucket where the min/max for a
    // field is non-missing may contain a mixture of missing / non-missing, so we can't exclude it
    // on the basis of the control fields.
    checkPredicateDisallowed({a: {$exists: false}});

    // $or on metadata, metric, or both.
    checkPredicateOK({
        input: {$or: [{[metaField + '.a']: {$lt: 5}}, {[metaField + '.b']: {$lt: 6}}]},
        output: {$or: [{'meta.a': {$lt: 5}}, {'meta.b': {$lt: 6}}]},
    });
    checkPredicateOK({
        input: {$or: [{'a': {$lt: 5}}, {b: {$lt: 6}}]},
        output: {
            $or: [
                {'control.min.a': {$_internalExprLt: 5}},
                {'control.min.b': {$_internalExprLt: 6}},
            ]
        }
    });
    checkPredicateOK({
        input: {$or: [{'a': {$lt: 5}}, {[metaField + '.b']: {$lt: 6}}]},
        output: {
            $or: [
                {'control.min.a': {$_internalExprLt: 5}},
                {'meta.b': {$lt: 6}},
            ]
        }
    });

    // If any argument of the $or is disallowed, we report the error even when an always-true
    // predicate appears in it.
    checkPredicateDisallowed({$or: [{}, {a: {}}]});

    // $in on metadata is fine.
    checkPredicateOK({
        input: {[metaField + '.a']: {$in: [1, 2, 5]}},
        output: {'meta.a': {$in: [1, 2, 5]}},
    });

    // $in on a metric is slightly complicated. $in is equivalent to a disjunction of {a: _}.
    // In a typical case this is the same as a disjunction of $eq:
    checkPredicateOK({
        input: {a: {$in: [1, 2, 5]}},
        output: {
            $or: [
                {
                    $and: [
                        {'control.min.a': {$_internalExprLte: 1}},
                        {'control.max.a': {$_internalExprGte: 1}},
                    ]
                },
                {
                    $and: [
                        {'control.min.a': {$_internalExprLte: 2}},
                        {'control.max.a': {$_internalExprGte: 2}},
                    ]
                },
                {
                    $and: [
                        {'control.min.a': {$_internalExprLte: 5}},
                        {'control.max.a': {$_internalExprGte: 5}},
                    ]
                },
            ]
        },
    });
    // Since {a: null} is not implemented, neither is {$in: [null]}.
    checkPredicateDisallowed({a: {$in: [null]}});
    // {a: {$in: [/abc/]}} is equivalent to {a: /abc/} which executes the regex, and is not allowed.
    checkPredicateDisallowed({a: {$in: [/abc/]}});

    // Predicates on time are pushed down, and also some extra predicates are inferred.
    // These inferred predicates don't necessarily result in fewer buckets being indexed:
    // we're just following the same rules for createIndex that we use when optimizing a query.
    checkPredicateOK({
        input: {[timeField]: {$lt: ISODate('2020-01-01T00:00:00Z')}},
        output: {
            $and: [
                {"control.min.time": {"$_internalExprLt": ISODate("2020-01-01T00:00:00Z")}},
                {"control.max.time": {"$_internalExprLt": ISODate("2020-01-01T01:00:00Z")}},
                {"_id": {"$lt": ObjectId("5e0be1000000000000000000")}},
            ]
        }
    });
    checkPredicateOK({
        input: {[timeField]: {$gt: ISODate('2020-01-01T00:00:00Z')}},
        output: {
            $and: [
                {"control.max.time": {"$_internalExprGt": ISODate("2020-01-01T00:00:00Z")}},
                {"control.min.time": {"$_internalExprGt": ISODate("2019-12-31T23:00:00Z")}},
                {"_id": {"$gt": ObjectId("5e0bd2f0ffffffffffffffff")}},
            ]
        }
    });
    checkPredicateOK({
        input: {[timeField]: {$lte: ISODate('2020-01-01T00:00:00Z')}},
        output: {
            $and: [
                {"control.min.time": {"$_internalExprLte": ISODate("2020-01-01T00:00:00Z")}},
                {"control.max.time": {"$_internalExprLte": ISODate("2020-01-01T01:00:00Z")}},
                {"_id": {"$lte": ObjectId("5e0be100ffffffffffffffff")}},
            ]
        }
    });
    checkPredicateOK({
        input: {[timeField]: {$gte: ISODate('2020-01-01T00:00:00Z')}},
        output: {
            $and: [
                {"control.max.time": {"$_internalExprGte": ISODate("2020-01-01T00:00:00Z")}},
                {"control.min.time": {"$_internalExprGte": ISODate("2019-12-31T23:00:00Z")}},
                {"_id": {"$gte": ObjectId("5e0bd2f00000000000000000")}},
            ]
        }
    });
    checkPredicateOK({
        input: {[timeField]: {$eq: ISODate('2020-01-01T00:00:00Z')}},
        output: {
            $and: [
                {"control.min.time": {"$_internalExprLte": ISODate("2020-01-01T00:00:00Z")}},
                {"control.min.time": {"$_internalExprGte": ISODate("2019-12-31T23:00:00Z")}},
                {"control.max.time": {"$_internalExprGte": ISODate("2020-01-01T00:00:00Z")}},
                {"control.max.time": {"$_internalExprLte": ISODate("2020-01-01T01:00:00Z")}},
                {"_id": {"$lte": ObjectId("5e0be100ffffffffffffffff")}},
                {"_id": {"$gte": ObjectId("5e0bd2f00000000000000000")}},
            ]
        }
    });
}
})();