summaryrefslogtreecommitdiff
path: root/jstests/views/views_collation.js
blob: 915d06d31c4ed4cd42bbe988d7eee65981853ac0 (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
/**
 * Tests the behavior of operations when interacting with a view's default collation.
 */
(function() {
    "use strict";

    function runTest(conn) {
        let viewsDB = conn.getDB("views_collation");
        assert.commandWorked(viewsDB.dropDatabase());
        assert.commandWorked(viewsDB.runCommand({create: "simpleCollection"}));
        assert.commandWorked(
            viewsDB.runCommand({create: "ukCollection", collation: {locale: "uk"}}));
        assert.commandWorked(
            viewsDB.runCommand({create: "filCollection", collation: {locale: "fil"}}));

        // Creating a view without specifying a collation defaults to the simple collation.
        assert.commandWorked(viewsDB.runCommand({create: "simpleView", viewOn: "ukCollection"}));
        let listCollectionsOutput =
            viewsDB.runCommand({listCollections: 1, filter: {type: "view"}});
        assert.commandWorked(listCollectionsOutput);
        assert(!listCollectionsOutput.cursor.firstBatch[0].options.hasOwnProperty("collation"));

        // Operations that do not specify a collation succeed.
        assert.commandWorked(viewsDB.runCommand({aggregate: "simpleView", pipeline: []}));
        assert.commandWorked(viewsDB.runCommand({find: "simpleView"}));
        assert.commandWorked(viewsDB.runCommand({count: "simpleView"}));
        assert.commandWorked(viewsDB.runCommand({distinct: "simpleView", key: "x"}));

        // Operations that explicitly ask for the "simple" locale succeed against a view with the
        // simple collation.
        assert.commandWorked(viewsDB.runCommand(
            {aggregate: "simpleView", pipeline: [], collation: {locale: "simple"}}));
        assert.commandWorked(
            viewsDB.runCommand({find: "simpleView", collation: {locale: "simple"}}));
        assert.commandWorked(
            viewsDB.runCommand({count: "simpleView", collation: {locale: "simple"}}));
        assert.commandWorked(
            viewsDB.runCommand({distinct: "simpleView", key: "x", collation: {locale: "simple"}}));

        // Attempting to override a view's simple collation fails.
        assert.commandFailedWithCode(
            viewsDB.runCommand({aggregate: "simpleView", pipeline: [], collation: {locale: "en"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({find: "simpleView", collation: {locale: "fr"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({count: "simpleView", collation: {locale: "fil"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({distinct: "simpleView", key: "x", collation: {locale: "es"}}),
            ErrorCodes.OptionNotSupportedOnView);

        // Create a view with an explicit, non-simple collation.
        assert.commandWorked(
            viewsDB.createView("filView", "ukCollection", [], {collation: {locale: "fil"}}));
        listCollectionsOutput = viewsDB.runCommand({listCollections: 1, filter: {name: "filView"}});
        assert.commandWorked(listCollectionsOutput);
        assert.eq(listCollectionsOutput.cursor.firstBatch[0].options.collation.locale, "fil");

        // Operations that do not specify a collation succeed.
        assert.commandWorked(viewsDB.runCommand({aggregate: "filView", pipeline: []}));
        assert.commandWorked(viewsDB.runCommand({find: "filView"}));
        assert.commandWorked(viewsDB.runCommand({count: "filView"}));
        assert.commandWorked(viewsDB.runCommand({distinct: "filView", key: "x"}));

        // Operations with a matching collation succeed.
        assert.commandWorked(
            viewsDB.runCommand({aggregate: "filView", pipeline: [], collation: {locale: "fil"}}));
        assert.commandWorked(viewsDB.runCommand({find: "filView", collation: {locale: "fil"}}));
        assert.commandWorked(viewsDB.runCommand({count: "filView", collation: {locale: "fil"}}));
        assert.commandWorked(
            viewsDB.runCommand({distinct: "filView", key: "x", collation: {locale: "fil"}}));

        // Attempting to override the non-simple default collation of a view fails.
        assert.commandFailedWithCode(
            viewsDB.runCommand({aggregate: "filView", pipeline: [], collation: {locale: "en"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({aggregate: "filView", pipeline: [], collation: {locale: "simple"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({find: "filView", collation: {locale: "fr"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({find: "filView", collation: {locale: "simple"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({count: "filView", collation: {locale: "zh"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({count: "filView", collation: {locale: "simple"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({distinct: "filView", key: "x", collation: {locale: "es"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({distinct: "filView", key: "x", collation: {locale: "simple"}}),
            ErrorCodes.OptionNotSupportedOnView);

        const lookupSimpleView = {
            $lookup: {from: "simpleView", localField: "x", foreignField: "x", as: "result"}
        };
        const graphLookupSimpleView = {
            $graphLookup: {
                from: "simpleView",
                startWith: "$_id",
                connectFromField: "_id",
                connectToField: "matchedId",
                as: "matched"
            }
        };

        // You can lookup into a view with the simple collation if the collection also has the same
        // default collation.
        assert.commandWorked(
            viewsDB.runCommand({aggregate: "simpleCollection", pipeline: [lookupSimpleView]}));
        assert.commandWorked(
            viewsDB.runCommand({aggregate: "simpleCollection", pipeline: [graphLookupSimpleView]}));

        // You can lookup into a view with the simple collation if the operation has a matching
        // collation.
        assert.commandWorked(viewsDB.runCommand({
            aggregate: "ukCollection",
            pipeline: [lookupSimpleView],
            collation: {locale: "simple"}
        }));
        assert.commandWorked(viewsDB.runCommand({
            aggregate: "ukCollection",
            pipeline: [graphLookupSimpleView],
            collation: {locale: "simple"}
        }));

        // You can't lookup into a view with the simple collation if the operation has a conflicting
        // collation.
        assert.commandFailedWithCode(viewsDB.runCommand({
            aggregate: "simpleCollection",
            pipeline: [lookupSimpleView],
            collation: {locale: "en"}
        }),
                                     ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(viewsDB.runCommand({
            aggregate: "simpleCollection",
            pipeline: [graphLookupSimpleView],
            collation: {locale: "zh"}
        }),
                                     ErrorCodes.OptionNotSupportedOnView);

        const lookupFilView = {
            $lookup: {from: "filView", localField: "x", foreignField: "x", as: "result"}
        };
        const graphLookupFilView = {
            $graphLookup: {
                from: "filView",
                startWith: "$_id",
                connectFromField: "_id",
                connectToField: "matchedId",
                as: "matched"
            }
        };

        // You can lookup into a view with no operation collation specified if the collection's
        // collation matches the collation of the view.
        assert.commandWorked(
            viewsDB.runCommand({aggregate: "filCollection", pipeline: [lookupFilView]}));
        assert.commandWorked(
            viewsDB.runCommand({aggregate: "filCollection", pipeline: [graphLookupFilView]}));

        // You can lookup into a view with a non-simple collation if the operation's collation
        // matches.
        assert.commandWorked(viewsDB.runCommand(
            {aggregate: "ukCollection", pipeline: [lookupFilView], collation: {locale: "fil"}}));
        assert.commandWorked(viewsDB.runCommand({
            aggregate: "ukCollection",
            pipeline: [graphLookupFilView],
            collation: {locale: "fil"}
        }));

        // You can't lookup into a view when aggregating a collection whose default collation does
        // not match the view's default collation.
        assert.commandFailedWithCode(
            viewsDB.runCommand({aggregate: "simpleCollection", pipeline: [lookupFilView]}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({aggregate: "simpleCollection", pipeline: [graphLookupFilView]}),
            ErrorCodes.OptionNotSupportedOnView);

        // You can't lookup into a view when aggregating a collection and the operation's collation
        // does not match the view's default collation.
        assert.commandFailedWithCode(
            viewsDB.runCommand(
                {aggregate: "filCollection", pipeline: [lookupFilView], collation: {locale: "zh"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(viewsDB.runCommand({
            aggregate: "filCollection",
            pipeline: [graphLookupFilView],
            collation: {locale: "zh"}
        }),
                                     ErrorCodes.OptionNotSupportedOnView);

        // You may perform an aggregation involving multiple views if they all have the same default
        // collation.
        assert.commandWorked(viewsDB.runCommand(
            {create: "simpleView2", viewOn: "simpleCollection", collation: {locale: "simple"}}));
        assert.commandWorked(
            viewsDB.runCommand({aggregate: "simpleView2", pipeline: [lookupSimpleView]}));
        assert.commandWorked(
            viewsDB.runCommand({aggregate: "simpleView2", pipeline: [graphLookupSimpleView]}));

        // You may perform an aggregation involving multiple views and collections if all the views
        // have the same default collation.
        const graphLookupUkCollection = {
            $graphLookup: {
                from: "ukCollection",
                startWith: "$_id",
                connectFromField: "_id",
                connectToField: "matchedId",
                as: "matched"
            }
        };
        assert.commandWorked(viewsDB.runCommand(
            {aggregate: "simpleView2", pipeline: [lookupSimpleView, graphLookupUkCollection]}));

        // You cannot perform an aggregation involving multiple views if the views don't all have
        // the same default collation.
        assert.commandFailedWithCode(
            viewsDB.runCommand({aggregate: "filView", pipeline: [lookupSimpleView]}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({aggregate: "simpleView", pipeline: [lookupFilView]}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand(
                {aggregate: "simpleCollection", pipeline: [lookupFilView, graphLookupSimpleView]}),
            ErrorCodes.OptionNotSupportedOnView);

        // You cannot create a view that depends on another view with a different default collation.
        assert.commandFailedWithCode(
            viewsDB.runCommand({create: "zhView", viewOn: "filView", collation: {locale: "zh"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(viewsDB.runCommand({
            create: "zhView",
            viewOn: "simpleCollection",
            pipeline: [lookupFilView],
            collation: {locale: "zh"}
        }),
                                     ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(viewsDB.runCommand({
            create: "zhView",
            viewOn: "simpleCollection",
            pipeline: [graphLookupSimpleView],
            collation: {locale: "zh"}
        }),
                                     ErrorCodes.OptionNotSupportedOnView);

        // You cannot modify a view to depend on another view with a different default collation.
        assert.commandWorked(viewsDB.runCommand(
            {create: "esView", viewOn: "simpleCollection", collation: {locale: "es"}}));
        assert.commandFailedWithCode(viewsDB.runCommand({collMod: "esView", viewOn: "filView"}),
                                     ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand(
                {collMod: "esView", viewOn: "simpleCollection", pipeline: [lookupSimpleView]}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand(
                {collMod: "esView", viewOn: "simpleCollection", pipeline: [graphLookupFilView]}),
            ErrorCodes.OptionNotSupportedOnView);

        // Views cannot be dropped and recreated with a different collation if other views depend on
        // that view.
        assert.commandWorked(viewsDB.runCommand(
            {create: "filView2", viewOn: "filView", collation: {locale: "fil"}}));
        assert.commandWorked(viewsDB.runCommand({drop: "filView"}));
        assert.commandFailedWithCode(
            viewsDB.runCommand({create: "filView", viewOn: "simpleCollection"}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand(
                {create: "filView", viewOn: "simpleCollection", collation: {locale: "en"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandWorked(
            viewsDB.createView("filView", "ukCollection", [], {collation: {locale: "fil"}}));

        // Views cannot be dropped and recreated with a different collation if other views depend on
        // that view via $lookup or $graphLookup.
        assert.commandWorked(viewsDB.runCommand(
            {collMod: "filView2", viewOn: "simpleCollection", pipeline: [lookupFilView]}));
        assert.commandWorked(viewsDB.runCommand({drop: "filView"}));
        assert.commandFailedWithCode(
            viewsDB.runCommand({create: "filView", viewOn: "simpleCollection"}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand(
                {create: "filView", viewOn: "simpleCollection", collation: {locale: "en"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandWorked(viewsDB.runCommand(
            {create: "filView", viewOn: "ukCollection", pipeline: [], collation: {locale: "fil"}}));

        assert.commandWorked(viewsDB.runCommand(
            {collMod: "filView2", viewOn: "simpleCollection", pipeline: [graphLookupFilView]}));
        assert.commandWorked(viewsDB.runCommand({drop: "filView"}));
        assert.commandFailedWithCode(
            viewsDB.runCommand({create: "filView", viewOn: "simpleCollection"}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand(
                {create: "filView", viewOn: "simpleCollection", collation: {locale: "en"}}),
            ErrorCodes.OptionNotSupportedOnView);

        // If two views "A" and "C" have different collations and depend on the namespace "B", then
        // "B"
        // cannot be created as a view.
        assert.commandWorked(
            viewsDB.runCommand({create: "A", viewOn: "B", collation: {locale: "hsb"}}));
        assert.commandWorked(
            viewsDB.runCommand({create: "B", viewOn: "other", collation: {locale: "hsb"}}));
        assert.commandFailedWithCode(
            viewsDB.runCommand({create: "C", viewOn: "B", collation: {locale: "wae"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandWorked(viewsDB.runCommand({drop: "B"}));
        assert.commandWorked(
            viewsDB.runCommand({create: "C", viewOn: "B", collation: {locale: "wae"}}));
        assert.commandFailedWithCode(
            viewsDB.runCommand({create: "B", viewOn: "other", collation: {locale: "hsb"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(
            viewsDB.runCommand({create: "B", viewOn: "other", collation: {locale: "wae"}}),
            ErrorCodes.OptionNotSupportedOnView);
        assert.commandFailedWithCode(viewsDB.runCommand({create: "B", viewOn: "other"}),
                                     ErrorCodes.OptionNotSupportedOnView);
    }

    // Run the test on a standalone.
    let mongod = MongoRunner.runMongod({});
    runTest(mongod);
    MongoRunner.stopMongod(mongod);

    // Run the test on a sharded cluster.
    let cluster = new ShardingTest({shards: 1, mongos: 1});
    runTest(cluster);
    cluster.stop();
}());