summaryrefslogtreecommitdiff
path: root/jstests/core/minmax_edge.js
blob: cf75edaef809676e50a6daabd378565849bba641 (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
/*
 * Test that .min() and .max() queries properly handle the edge cases with NaN and Infinity.
 * Other edge cases are covered by C++ unit tests.
 */
(function() {
    const t = db.minmax_edge;

    /*
     * Function to verify that the results of a query match the expected results.
     * Results is the cursor toArray, expectedIds is a list of _ids
     */
    function verifyResultIds(results, expectedIds) {
        // check they are the same length
        assert.eq(results.length, expectedIds.length);

        function compare(a, b) {
            if (a._id < b._id)
                return -1;
            if (a._id > b._id)
                return 1;
            return 0;
        }

        results.sort(compare);
        expectedIds.sort();

        for (var i = 0; i < results.length; i++) {
            assert.eq(results._id, expectedIds._ids);
        }
    }

    /*
     * Shortcut to drop the collection and insert these 3 test docs. Used to change the indices
     * regardless of any previous indices.
     */
    function reset(t) {
        t.drop();
        assert.writeOK(t.insert({_id: 0, a: 1, b: 1}));
        assert.writeOK(t.insert({_id: 1, a: 1, b: 2}));
        assert.writeOK(t.insert({_id: 2, a: 1, b: 3}));

        assert.writeOK(t.insert({_id: 3, a: 2, b: 1}));
        assert.writeOK(t.insert({_id: 4, a: 2, b: 2}));
        assert.writeOK(t.insert({_id: 5, a: 2, b: 3}));

        assert.writeOK(t.insert({_id: 6, a: 3, b: 1}));
        assert.writeOK(t.insert({_id: 7, a: 3, b: 2}));
        assert.writeOK(t.insert({_id: 8, a: 3, b: 3}));
    }

    // Two helpers to save typing
    function verifyMin(minDoc, idx, expectedIds) {
        verifyResultIds(t.find().min(minDoc).hint(idx).toArray(), expectedIds);
    }

    function verifyMax(minDoc, idx, expectedIds) {
        verifyResultIds(t.find().max(minDoc).hint(idx).toArray(), expectedIds);
    }

    // Basic ascending index.
    reset(t);
    let indexSpec = {a: 1};
    assert.commandWorked(t.createIndex(indexSpec));

    verifyMin({a: Infinity}, indexSpec, []);
    verifyMax({a: Infinity}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    verifyMin({a: -Infinity}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: -Infinity}, indexSpec, []);

    // NaN < all ints.
    verifyMin({a: NaN}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: NaN}, indexSpec, []);

    // {a: 1} > all ints.
    verifyMin({a: {a: 1}}, indexSpec, []);
    verifyMax({a: {a: 1}}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    // 'a' > all ints.
    verifyMin({a: 'a'}, indexSpec, []);
    verifyMax({a: 'a'}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    // Now with a compound index.
    reset(t);
    indexSpec = {a: 1, b: -1};

    assert.commandWorked(t.createIndex(indexSpec));

    // Same as single-key index assertions, with b field present.
    verifyMin({a: NaN, b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: NaN, b: 1}, indexSpec, []);

    verifyMin({a: Infinity, b: 1}, indexSpec, []);
    verifyMax({a: Infinity, b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    verifyMin({a: -Infinity, b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: -Infinity, b: 1}, indexSpec, []);

    verifyMin({a: {a: 1}, b: 1}, indexSpec, []);
    verifyMax({a: {a: 1}, b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    verifyMin({a: 'a', b: 1}, indexSpec, []);
    verifyMax({a: 'a', b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    // Edge cases on b values
    verifyMin({a: 1, b: Infinity}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMin({a: 2, b: Infinity}, indexSpec, [3, 4, 5, 6, 7, 8]);
    verifyMin({a: 3, b: Infinity}, indexSpec, [6, 7, 8]);
    verifyMax({a: 1, b: Infinity}, indexSpec, []);
    verifyMax({a: 2, b: Infinity}, indexSpec, [0, 1, 2]);
    verifyMax({a: 3, b: Infinity}, indexSpec, [0, 1, 2, 3, 4, 5]);

    verifyMin({a: 1, b: -Infinity}, indexSpec, [3, 4, 5, 6, 7, 8]);
    verifyMin({a: 2, b: -Infinity}, indexSpec, [6, 7, 8]);
    verifyMin({a: 3, b: -Infinity}, indexSpec, []);
    verifyMax({a: 1, b: -Infinity}, indexSpec, [0, 1, 2]);
    verifyMax({a: 2, b: -Infinity}, indexSpec, [0, 1, 2, 3, 4, 5]);
    verifyMax({a: 3, b: -Infinity}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    verifyMin({a: 2, b: NaN}, indexSpec, [6, 7, 8]);
    verifyMax({a: 2, b: NaN}, indexSpec, [0, 1, 2, 3, 4, 5]);

    verifyMin({a: 2, b: {b: 1}}, indexSpec, [3, 4, 5, 6, 7, 8]);
    verifyMax({a: 2, b: {b: 1}}, indexSpec, [0, 1, 2]);

    verifyMin({a: 2, b: 'b'}, indexSpec, [3, 4, 5, 6, 7, 8]);
    verifyMax({a: 2, b: 'b'}, indexSpec, [0, 1, 2]);

    // Test descending index.
    reset(t);
    indexSpec = {a: -1};
    assert.commandWorked(t.createIndex(indexSpec));

    verifyMin({a: NaN}, indexSpec, []);
    verifyMax({a: NaN}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    verifyMin({a: Infinity}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: Infinity}, indexSpec, []);

    verifyMin({a: -Infinity}, indexSpec, []);
    verifyMax({a: -Infinity}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    verifyMin({a: {a: 1}}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: {a: 1}}, indexSpec, []);

    verifyMin({a: 'a'}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: 'a'}, indexSpec, []);

    // Now with a compound index.
    reset(t);
    indexSpec = {a: -1, b: -1};
    assert.commandWorked(t.createIndex(indexSpec));

    // Same as single-key index assertions, with b field present.
    verifyMin({a: NaN, b: 1}, indexSpec, []);
    verifyMax({a: NaN, b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    verifyMin({a: Infinity, b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: Infinity, b: 1}, indexSpec, []);

    verifyMin({a: -Infinity, b: 1}, indexSpec, []);
    verifyMax({a: -Infinity, b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);

    verifyMin({a: {a: 1}, b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: {a: 1}, b: 1}, indexSpec, []);

    verifyMin({a: 'a', b: 1}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: 'a', b: 1}, indexSpec, []);

    // Edge cases on b values.
    verifyMin({a: 1, b: Infinity}, indexSpec, [0, 1, 2]);
    verifyMin({a: 2, b: Infinity}, indexSpec, [0, 1, 2, 3, 4, 5]);
    verifyMin({a: 3, b: Infinity}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: 1, b: Infinity}, indexSpec, [3, 4, 5, 6, 7, 8]);
    verifyMax({a: 2, b: Infinity}, indexSpec, [6, 7, 8]);
    verifyMax({a: 3, b: Infinity}, indexSpec, []);

    verifyMin({a: 1, b: -Infinity}, indexSpec, []);
    verifyMin({a: 2, b: -Infinity}, indexSpec, [0, 1, 2]);
    verifyMin({a: 3, b: -Infinity}, indexSpec, [0, 1, 2, 3, 4, 5]);
    verifyMax({a: 1, b: -Infinity}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
    verifyMax({a: 2, b: -Infinity}, indexSpec, [3, 4, 5, 6, 7, 8]);
    verifyMax({a: 3, b: -Infinity}, indexSpec, [6, 7, 8]);

    verifyMin({a: 2, b: NaN}, indexSpec, [0, 1, 2]);
    verifyMax({a: 2, b: NaN}, indexSpec, [3, 4, 5, 6, 7, 8]);

    verifyMin({a: 2, b: {b: 1}}, indexSpec, [3, 4, 5, 6, 7, 8]);
    verifyMax({a: 2, b: {b: 1}}, indexSpec, [0, 1, 2]);

    verifyMin({a: 2, b: 'b'}, indexSpec, [3, 4, 5, 6, 7, 8]);
    verifyMax({a: 2, b: 'b'}, indexSpec, [0, 1, 2]);

    // Now a couple cases with an extra compound index.
    t.drop();
    indexSpec = {a: 1, b: -1, c: 1};
    assert.commandWorked(t.createIndex(indexSpec));
    // The following documents are in order according to the index.
    t.insert({_id: 0, a: 1, b: 'b', c: 1});
    t.insert({_id: 1, a: 1, b: 'b', c: 2});
    t.insert({_id: 2, a: 1, b: 'a', c: 1});
    t.insert({_id: 3, a: 1, b: 'a', c: 2});
    t.insert({_id: 4, a: 2, b: 'b', c: 1});
    t.insert({_id: 5, a: 2, b: 'b', c: 2});
    t.insert({_id: 6, a: 2, b: 'a', c: 1});
    t.insert({_id: 7, a: 2, b: 'a', c: 2});

    verifyMin({a: 1, b: 'a', c: 1}, indexSpec, [2, 3, 4, 5, 6, 7]);
    verifyMin({a: 2, b: 'a', c: 2}, indexSpec, [7]);
    verifyMax({a: 1, b: 'a', c: 1}, indexSpec, [0, 1]);
    verifyMax({a: 2, b: 'a', c: 2}, indexSpec, [0, 1, 2, 3, 4, 5, 6]);

    verifyMin({a: Infinity, b: 'a', c: 2}, indexSpec, []);
    verifyMax({a: Infinity, b: 'a', c: 2}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7]);

    verifyMin({a: -Infinity, b: 'a', c: 2}, indexSpec, [0, 1, 2, 3, 4, 5, 6, 7]);
    verifyMax({a: -Infinity, b: 'a', c: 2}, indexSpec, []);

    // 'a' > Infinity, actually.
    verifyMin({a: 1, b: Infinity, c: 2}, indexSpec, [4, 5, 6, 7]);
    verifyMax({a: 1, b: Infinity, c: 2}, indexSpec, [0, 1, 2, 3]);

    // Also, 'a' > -Infinity.
    verifyMin({a: 1, b: -Infinity, c: 2}, indexSpec, [4, 5, 6, 7]);
    verifyMax({a: 1, b: -Infinity, c: 2}, indexSpec, [0, 1, 2, 3]);

    verifyMin({a: 1, b: 'a', c: Infinity}, indexSpec, [4, 5, 6, 7]);
    verifyMax({a: 1, b: 'a', c: Infinity}, indexSpec, [0, 1, 2, 3]);

    verifyMin({a: 1, b: 'a', c: -Infinity}, indexSpec, [2, 3, 4, 5, 6, 7]);
    verifyMax({a: 1, b: 'a', c: -Infinity}, indexSpec, [0, 1]);
})();