summaryrefslogtreecommitdiff
path: root/jstests/core/index_bounds_timestamp.js
blob: 1f7cc261c30dc1cf5dd729191a2279abff3b04db (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
// Index bounds generation tests for Timestamp values.
// This file tests whether index bounds for timestamps are generated properly in terms of
// inclusiveness and exactness.

(function() {
    "use strict";

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

    // Setup the test collection.
    let coll = db.index_bounds_timestamp;
    coll.drop();

    // Create an index on the ts and _id fields.
    assert.commandWorked(coll.createIndex({ts: 1, _id: 1}));

    // Insert some test documents.
    // NOTE: Inserting Timestamp() or Timestamp(0, 0) into a collection creates a Timestamp for the
    // current time. Max Timestamp value is Timestamp(2^32 - 1, 2^32 - 1).
    const documents = [
        {_id: 0, ts: new Timestamp(0, 1)},
        {_id: 1, ts: new Timestamp(0, Math.pow(2, 31))},
        {_id: 2, ts: new Timestamp(0, Math.pow(2, 32) - 1)},
        {_id: 3, ts: new Timestamp(1, 0)},
        {_id: 4, ts: new Timestamp(Math.pow(2, 32) - 1, Math.pow(2, 32) - 1)}
    ];
    assert.writeOK(coll.insert(documents));

    // Sanity check the timestamp bounds generation plan.
    let plan;

    // Check that count over (Timestamp(0, 0), Timestamp(2^32 - 1, 2^32 - 1)] is a covered query.
    plan = coll.explain("executionStats").find({ts: {$gt: Timestamp(0, 0)}}).count();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gt count should be a covered query");
    assertExplainCount({explainResults: plan, expectedCount: 5});

    // Check that find over (Timestamp(0, 0), Timestamp(2^32 - 1, 2^32 - 1)] does not require a
    // FETCH stage when the query is covered by an index.
    plan =
        coll.explain("executionStats").find({ts: {$gt: Timestamp(0, 0)}}, {ts: 1, _id: 0}).finish();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gt find with project should be a covered query");

    // Check that count over [Timestamp(0, 0), Timestamp(2^32 - 1, 2^32 - 1)] is a covered query.
    plan = coll.explain("executionStats").find({ts: {$gte: Timestamp(0, 0)}}).count();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gte count should be a covered query");
    assertExplainCount({explainResults: plan, expectedCount: 5});

    // Check that find over [Timestamp(0, 0), Timestamp(2^32 - 1, 2^32 - 1)] does not require a
    // FETCH stage when the query is covered by an index.
    plan = coll.explain("executionStats")
               .find({ts: {$gte: Timestamp(0, 0)}}, {ts: 1, _id: 0})
               .finish();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gte find with project should be a covered query");

    // Check that count over [Timestamp(0, 0), Timestamp(1, 0)) is a covered query.
    plan = coll.explain("executionStats").find({ts: {$lt: Timestamp(1, 0)}}).count();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $lt count should be a covered query");
    assertExplainCount({explainResults: plan, expectedCount: 3});

    // Check that find over [Timestamp(0, 0), Timestamp(1, 0)) does not require a FETCH stage when
    // the query is covered by an index.
    plan =
        coll.explain("executionStats").find({ts: {$lt: Timestamp(1, 0)}}, {ts: 1, _id: 0}).finish();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $lt find with project should be a covered query");

    // Check that count over [Timestamp(0, 0), Timestamp(1, 0)] is a covered query.
    plan = coll.explain("executionStats").find({ts: {$lte: Timestamp(1, 0)}}).count();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $lte count should be a covered query");
    assertExplainCount({explainResults: plan, expectedCount: 4});

    // Check that find over [Timestamp(0, 0), Timestamp(1, 0)] does not require a FETCH stage when
    // the query is covered by an index.
    plan = coll.explain("executionStats")
               .find({ts: {$lte: Timestamp(1, 0)}}, {ts: 1, _id: 0})
               .finish();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $lte find with project should be a covered query");

    // Check that count over (Timestamp(0, 1), Timestamp(1, 0)) is a covered query.
    plan = coll.explain("executionStats")
               .find({ts: {$gt: Timestamp(0, 1), $lt: Timestamp(1, 0)}})
               .count();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gt, $lt count should be a covered query");
    assertExplainCount({explainResults: plan, expectedCount: 2});

    // Check that find over (Timestamp(0, 1), Timestamp(1, 0)) does not require a FETCH stage when
    // the query is covered by an index.
    plan = coll.explain("executionStats")
               .find({ts: {$gt: Timestamp(0, 1), $lt: Timestamp(1, 0)}}, {ts: 1, _id: 0})
               .finish();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gt, $lt find with project should be a covered query");

    // Check that count over (Timestamp(0, 1), Timestamp(1, 0)] is a covered query.
    plan = coll.explain("executionStats")
               .find({ts: {$gt: Timestamp(0, 1), $lte: Timestamp(1, 0)}})
               .count();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gt, $lte count should be a covered query");
    assertExplainCount({explainResults: plan, expectedCount: 3});

    // Check that find over (Timestamp(0, 1), Timestamp(1, 0)] does not require a FETCH stage when
    // the query is covered by an index.
    plan = coll.explain("executionStats")
               .find({ts: {$gt: Timestamp(0, 1), $lte: Timestamp(1, 0)}}, {ts: 1, _id: 0})
               .finish();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gt, $lte find with project should be a covered query");

    // Check that count over [Timestamp(0, 1), Timestamp(1, 0)) is a covered query.
    plan = coll.explain("executionStats")
               .find({ts: {$gte: Timestamp(0, 1), $lt: Timestamp(1, 0)}})
               .count();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gte, $lt count should be a covered query");
    assertExplainCount({explainResults: plan, expectedCount: 3});

    // Check that find over [Timestamp(0, 1), Timestamp(1, 0)) does not require a FETCH stage when
    // the query is covered by an index.
    plan = coll.explain("executionStats")
               .find({ts: {$gte: Timestamp(0, 1), $lt: Timestamp(1, 0)}}, {ts: 1, _id: 0})
               .finish();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gte, $lt find with project should be a covered query");

    // Check that count over [Timestamp(0, 1), Timestamp(1, 0)] is a covered query.
    plan = coll.explain("executionStats")
               .find({ts: {$gte: Timestamp(0, 1), $lte: Timestamp(1, 0)}})
               .count();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gte, $lte count should be a covered query");
    assertExplainCount({explainResults: plan, expectedCount: 4});

    // Check that find over [Timestamp(0, 1), Timestamp(1, 0)] does not require a FETCH stage when
    // the query is covered by an index.
    plan = coll.explain("executionStats")
               .find({ts: {$gte: Timestamp(0, 1), $lte: Timestamp(1, 0)}}, {ts: 1, _id: 0})
               .finish();
    assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
           "ts $gte, $lte find with project should be a covered query");
})();