summaryrefslogtreecommitdiff
path: root/jstests/core/explode_for_sort_collation.js
blob: fcdb3629bcef6522df91cd89af5eb545bcc28665 (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
/**
 * Tests explode for sort query planner behavior with collated queries and indexes. This is a test
 * for SERVER-48993.
 * @tags: [requires_find_command]
 */
(function() {
"use strict";

const testDB = db.getSiblingDB(jsTestName());

// Drop the test database.
assert.commandWorked(testDB.dropDatabase());

const coll = testDB.explode_for_sort;

// Executes a test case that creates an index, inserts documents, issues a find query on a
// collection and compares the results with the expected collection.
function executeQueryTestCase(testCase) {
    jsTestLog(tojson(testCase));

    // Create a collection.
    coll.drop();
    assert.commandWorked(testDB.createCollection(coll.getName()));

    // Create an index.
    assert.commandWorked(coll.createIndex(testCase.indexKeyPattern, testCase.indexOptions));

    // Insert some documents into the collection.
    assert.commandWorked(coll.insert(testCase.inputDocuments));

    // Run a find query with optionally specified collation.
    let cursor = coll.find(testCase.filter).sort(testCase.sort);
    if (testCase.findCollation !== undefined) {
        cursor = cursor.collation(testCase.findCollation);
    }
    const actualResults = cursor.toArray();

    // Compare results to expected.
    assert.eq(actualResults, testCase.expectedResults);
}

const standardInputDocuments =
    [{_id: 0, a: 0, b: "CC"}, {_id: 1, a: 0, b: "AA"}, {_id: 2, a: 0, b: "bb"}];

const testCases = [
    {
        // Verifies that a non-collatable point-query on the prefix of the index key together with a
        // sort on a suffix of the index key returns correct results when the index is a compound
        // index with a non-simple collation and the query does not have an explicit collation.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "en_US", strength: 1}},
        filter: {a: 0},
        sort: {b: 1},
        inputDocuments: standardInputDocuments,
        expectedResults: [{_id: 1, a: 0, b: "AA"}, {_id: 0, a: 0, b: "CC"}, {_id: 2, a: 0, b: "bb"}]
    },
    {
        // Verifies that a non-collatable point-query on the prefix of the index key together with a
        // sort on a suffix of the index key returns correct results when the index is a compound
        // index with a non-simple collation  and the query explicitly specifies the simple
        // collation.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "en_US", strength: 1}},
        filter: {a: 0},
        sort: {b: 1},
        findCollation: {locale: "simple"},
        inputDocuments: standardInputDocuments,
        expectedResults: [{_id: 1, a: 0, b: "AA"}, {_id: 0, a: 0, b: "CC"}, {_id: 2, a: 0, b: "bb"}]
    },
    {
        // Verifies that a non-collatable point-query on the prefix of the index key together with a
        // sort on a suffix of the index key returns correct results when the index is a compound
        // index with a simple collation and the query explicitly specifies a non-simple collation.
        indexKeyPattern: {a: 1, b: 1},
        filter: {a: 0},
        sort: {b: 1},
        findCollation: {locale: "en_US", strength: 1},
        inputDocuments: standardInputDocuments,
        expectedResults: [{_id: 1, a: 0, b: "AA"}, {_id: 2, a: 0, b: "bb"}, {_id: 0, a: 0, b: "CC"}]
    },
    {
        // Verifies that a non-collatable point-query on the prefix of the index key together with a
        // sort on a suffix of the index key returns correct results when the index is a compound
        // index with a simple collation and the query explicitly specifies a non-simple collation.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "simple"}},
        filter: {a: 0},
        sort: {b: 1},
        findCollation: {locale: "en_US", strength: 1},
        inputDocuments: standardInputDocuments,
        expectedResults: [{_id: 1, a: 0, b: "AA"}, {_id: 2, a: 0, b: "bb"}, {_id: 0, a: 0, b: "CC"}]
    },
    {
        // Verifies that a non-collatable point-query on the prefix of the index key together with a
        // sort on a suffix of the index key returns correct results when the index is a compound
        // index with a non-simple collation that is different from the query's.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "en_US", strength: 5}},
        filter: {a: 0},
        sort: {b: 1},
        findCollation: {locale: "en_US", strength: 1},
        inputDocuments: standardInputDocuments,
        expectedResults: [{_id: 1, a: 0, b: "AA"}, {_id: 2, a: 0, b: "bb"}, {_id: 0, a: 0, b: "CC"}]
    },
    {
        // Verifies that a non-collatable point-query on the prefix of the index key, a collatable
        // range-query on the suffix, and a sort on the suffix of the index key returns correct
        // results when the index is a compound index with a non-simple collation and the query does
        // not have an explicit collation.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "en_US", strength: 1}},
        filter: {a: 0, b: {$gte: 'A', $lt: 'D'}},
        sort: {b: 1},
        inputDocuments: standardInputDocuments,
        expectedResults: [{_id: 1, a: 0, b: "AA"}, {_id: 0, a: 0, b: "CC"}]
    },
    {
        // Verifies that a non-collatable point-query on the prefix of the index key, a collatable
        // range-query and a sort on a prefix of a suffix of the index key returns correct results
        // when the index is a compound index with a non-simple collation and the query does not
        // have an explicit collation.
        indexKeyPattern: {a: 1, b: 1, c: 1},
        indexOptions: {collation: {locale: "en_US", strength: 1}},
        filter: {a: 0, b: {$gte: 'A', $lt: 'D'}},
        sort: {b: 1},
        inputDocuments: standardInputDocuments,
        expectedResults: [{_id: 1, a: 0, b: "AA"}, {_id: 0, a: 0, b: "CC"}]
    },
    {
        // Verifies that a non-collatable multi-point query on the prefix of the index key, a
        // collatable range-query on the suffix, and a sort on the suffix of the index key returns
        // correct results when the index is a compound index with a non-simple collation and the
        // query does not have an explicit collation.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "en_US", strength: 1}},
        filter: {a: {$in: [0, 2]}, b: {$gte: 'A', $lt: 'D'}},
        sort: {b: 1},
        inputDocuments: [
            {_id: 0, a: 0, b: "CC"},
            {_id: 1, a: 0, b: "AA"},
            {_id: 2, a: 0, b: "bb"},
            {_id: 3, a: 2, b: "BB"}
        ],
        expectedResults: [{_id: 1, a: 0, b: "AA"}, {_id: 3, a: 2, b: "BB"}, {_id: 0, a: 0, b: "CC"}]
    },
    {
        // Verifies that a non-collatable multi-point query on the prefix of the index key, a
        // non-collatable range-query on the suffix, and a sort on the suffix of the index key
        // returns correct results when the index is a compound index with a non-simple collation
        // and the query does not have an explicit collation.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "en_US", strength: 1}},
        filter: {a: {$in: [0, 2]}, b: {$gte: 0, $lt: 10}},
        sort: {b: 1},
        inputDocuments: [
            {_id: 0, a: 0, b: 6},
            {_id: 1, a: 0, b: 10},
            {_id: 2, a: 0, b: "bb"},
            {_id: 3, a: 2, b: 5},
            {_id: 4, a: 2, b: 4}
        ],
        expectedResults: [{_id: 4, a: 2, b: 4}, {_id: 3, a: 2, b: 5}, {_id: 0, a: 0, b: 6}]
    },
    {
        // Verifies that a non-collatable multi-point query on the prefix of the index key, a
        // non-collatable range-query on the suffix, and a sort on the suffix of the index key
        // returns correct results when the index is a compound index with a simple collation
        // and the query explicitly specifies a non-simple collation.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "simple"}},
        filter: {a: {$in: [0, 2]}, b: {$gte: 0, $lt: 10}},
        sort: {b: 1},
        findCollation: {locale: "en_US", strength: 1},
        inputDocuments: [
            {_id: 0, a: 0, b: 6},
            {_id: 1, a: 0, b: 10},
            {_id: 2, a: 0, b: "bb"},
            {_id: 3, a: 2, b: 5},
            {_id: 4, a: 2, b: 4}
        ],
        expectedResults: [{_id: 4, a: 2, b: 4}, {_id: 3, a: 2, b: 5}, {_id: 0, a: 0, b: 6}]
    },
    {
        // Verifies that a non-collatable point-query on the prefix of the index key, a
        // non-collatable range-query on the suffix, and a sort on the suffix of the index key
        // returns correct results when the index is a compound index with a non-simple collation
        // and the query does not have an explicit collation.
        indexKeyPattern: {a: 1, b: 1},
        indexOptions: {collation: {locale: "en_US", strength: 1}},
        filter: {a: 0, b: {$gte: 0, $lt: 10}},
        sort: {b: 1},
        inputDocuments: [
            {_id: 0, a: 0, b: 6},
            {_id: 1, a: 0, b: 5},
            {_id: 2, a: 0, b: "bb"},
            {_id: 3, a: 0, b: 4}
        ],
        expectedResults: [{_id: 3, a: 0, b: 4}, {_id: 1, a: 0, b: 5}, {_id: 0, a: 0, b: 6}]
    }
];

testCases.forEach(executeQueryTestCase);
}());