summaryrefslogtreecommitdiff
path: root/jstests/core/collation_plan_cache.js
blob: 0eec77388e4f921df6a122194fa0b5ec7465cdb4 (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
// Integration testing for the plan cache and index filter commands with collation.
(function() {
    'use strict';

    var coll = db.collation_plan_cache;
    coll.drop();

    assert.writeOK(coll.insert({a: 'foo', b: 5}));

    // We need two indexes that each query can use so that a plan cache entry is created.
    assert.commandWorked(coll.createIndex({a: 1}, {collation: {locale: 'en_US'}}));
    assert.commandWorked(coll.createIndex({a: 1, b: 1}, {collation: {locale: 'en_US'}}));

    // We need an index with a different collation, so that string comparisons affect the query
    // shape.
    assert.commandWorked(coll.createIndex({b: 1}, {collation: {locale: 'fr_CA'}}));

    // listQueryShapes().

    // Run a query so that an entry is inserted into the cache.
    assert.commandWorked(
        coll.runCommand("find", {filter: {a: 'foo', b: 5}, collation: {locale: "en_US"}}),
        'find command failed');

    // The query shape should have been added.
    var shapes = coll.getPlanCache().listQueryShapes();
    assert.eq(1, shapes.length, 'unexpected cache size after running query');
    assert.eq(shapes[0],
              {
                query: {a: 'foo', b: 5},
                sort: {},
                projection: {},
                collation: {
                    locale: 'en_US',
                    caseLevel: false,
                    caseFirst: 'off',
                    strength: 3,
                    numericOrdering: false,
                    alternate: 'non-ignorable',
                    maxVariable: 'punct',
                    normalization: false,
                    backwards: false,
                    version: '57.1'
                }
              },
              'unexpected query shape returned from listQueryShapes()');

    coll.getPlanCache().clear();

    // getPlansByQuery().

    // Passing a query with an empty collation object should throw.
    assert.throws(function() {
        coll.getPlanCache().getPlansByQuery(
            {query: {a: 'foo', b: 5}, sort: {}, projection: {}, collation: {}});
    }, [], 'empty collation object should throw');

    // Passing a query with an invalid collation object should throw.
    assert.throws(function() {
        coll.getPlanCache().getPlansByQuery(
            {query: {a: 'foo', b: 5}, sort: {}, projection: {}, collation: {bad: "value"}});
    }, [], 'invalid collation object should throw');

    // Run a query so that an entry is inserted into the cache.
    assert.commandWorked(
        coll.runCommand("find", {filter: {a: 'foo', b: 5}, collation: {locale: "en_US"}}),
        'find command failed');

    // The query should have cached plans.
    assert.lt(
        0,
        coll.getPlanCache()
            .getPlansByQuery(
                {query: {a: 'foo', b: 5}, sort: {}, projection: {}, collation: {locale: 'en_US'}})
            .length,
        'unexpected number of cached plans for query');

    // Test passing the query, sort, projection, and collation to getPlansByQuery() as  separate
    // arguments.
    assert.lt(
        0,
        coll.getPlanCache().getPlansByQuery({a: 'foo', b: 5}, {}, {}, {locale: 'en_US'}).length,
        'unexpected number of cached plans for query');

    // Test passing the query, sort, projection, and collation to getPlansByQuery() as separate
    // arguments.
    assert.eq(0,
              coll.getPlanCache().getPlansByQuery({a: 'foo', b: 5}).length,
              'unexpected number of cached plans for query');

    // A query with a different collation should have no cached plans.
    assert.eq(
        0,
        coll.getPlanCache()
            .getPlansByQuery(
                {query: {a: 'foo', b: 5}, sort: {}, projection: {}, collation: {locale: 'fr_CA'}})
            .length,
        'unexpected number of cached plans for query');

    // A query with different string locations should have no cached plans.
    assert.eq(0,
              coll.getPlanCache()
                  .getPlansByQuery({
                      query: {a: 'foo', b: 'bar'},
                      sort: {},
                      projection: {},
                      collation: {locale: 'en_US'}
                  })
                  .length,
              'unexpected number of cached plans for query');

    coll.getPlanCache().clear();

    // clearPlansByQuery().

    // Passing a query with an empty collation object should throw.
    assert.throws(function() {
        coll.getPlanCache().clearPlansByQuery(
            {query: {a: 'foo', b: 5}, sort: {}, projection: {}, collation: {}});
    }, [], 'empty collation object should throw');

    // Passing a query with an invalid collation object should throw.
    assert.throws(function() {
        coll.getPlanCache().clearPlansByQuery(
            {query: {a: 'foo', b: 5}, sort: {}, projection: {}, collation: {bad: "value"}});
    }, [], 'invalid collation object should throw');

    // Run a query so that an entry is inserted into the cache.
    assert.commandWorked(
        coll.runCommand("find", {filter: {a: 'foo', b: 5}, collation: {locale: "en_US"}}),
        'find command failed');
    assert.eq(1,
              coll.getPlanCache().listQueryShapes().length,
              'unexpected cache size after running query');

    // Dropping a query shape with a different collation should have no effect.
    coll.getPlanCache().clearPlansByQuery(
        {query: {a: 'foo', b: 5}, sort: {}, projection: {}, collation: {locale: 'fr_CA'}});
    assert.eq(1,
              coll.getPlanCache().listQueryShapes().length,
              'unexpected cache size after dropping uncached query shape');

    // Dropping a query shape with different string locations should have no effect.
    coll.getPlanCache().clearPlansByQuery(
        {query: {a: 'foo', b: 'bar'}, sort: {}, projection: {}, collation: {locale: 'en_US'}});
    assert.eq(1,
              coll.getPlanCache().listQueryShapes().length,
              'unexpected cache size after dropping uncached query shape');

    // Dropping query shape.
    coll.getPlanCache().clearPlansByQuery(
        {query: {a: 'foo', b: 5}, sort: {}, projection: {}, collation: {locale: 'en_US'}});
    assert.eq(0,
              coll.getPlanCache().listQueryShapes().length,
              'unexpected cache size after dropping query shapes');

    // Index filter commands.

    // planCacheSetFilter should fail if 'collation' is an empty object.
    assert.commandFailed(
        coll.runCommand('planCacheSetFilter',
                        {query: {a: 'foo', b: 5}, collation: {}, indexes: [{a: 1, b: 1}]}),
        'planCacheSetFilter should fail on empty collation object');

    // planCacheSetFilter should fail if 'collation' is an invalid object.
    assert.commandFailed(
        coll.runCommand(
            'planCacheSetFilter',
            {query: {a: 'foo', b: 5}, collation: {bad: "value"}, indexes: [{a: 1, b: 1}]}),
        'planCacheSetFilter should fail on invalid collation object');

    // Set a plan cache filter.
    assert.commandWorked(
        coll.runCommand(
            'planCacheSetFilter',
            {query: {a: 'foo', b: 5}, collation: {locale: 'en_US'}, indexes: [{a: 1, b: 1}]}),
        'planCacheSetFilter failed');

    // Check the plan cache filter was added.
    var res = coll.runCommand('planCacheListFilters');
    assert.commandWorked(res, 'planCacheListFilters failed');
    assert.eq(1, res.filters.length, 'unexpected number of plan cache filters');
    assert.eq(res.filters[0],
              {
                query: {a: 'foo', b: 5},
                sort: {},
                projection: {},
                collation: {
                    locale: 'en_US',
                    caseLevel: false,
                    caseFirst: 'off',
                    strength: 3,
                    numericOrdering: false,
                    alternate: 'non-ignorable',
                    maxVariable: 'punct',
                    normalization: false,
                    backwards: false,
                    version: '57.1'
                },
                indexes: [{a: 1, b: 1}]
              },
              'unexpected plan cache filter');

    // planCacheClearFilters should fail if 'collation' is an empty object.
    assert.commandFailed(
        coll.runCommand('planCacheClearFilters', {query: {a: 'foo', b: 5}, collation: {}}),
        'planCacheClearFilters should fail on empty collation object');

    // planCacheSetFilter should fail if 'collation' is an invalid object.
    assert.commandFailed(coll.runCommand('planCacheClearFilters',
                                         {query: {a: 'foo', b: 5}, collation: {bad: 'value'}}),
                         'planCacheClearFilters should fail on invalid collation object');

    // Clearing a plan cache filter with no collation should have no effect.
    assert.commandWorked(coll.runCommand('planCacheClearFilters', {query: {a: 'foo', b: 5}}));
    assert.eq(1,
              coll.runCommand('planCacheListFilters').filters.length,
              'unexpected number of plan cache filters');

    // Clearing a plan cache filter with a different collation should have no effect.
    assert.commandWorked(coll.runCommand('planCacheClearFilters',
                                         {query: {a: 'foo', b: 5}, collation: {locale: 'fr_CA'}}));
    assert.eq(1,
              coll.runCommand('planCacheListFilters').filters.length,
              'unexpected number of plan cache filters');

    // Clearing a plan cache filter with different string locations should have no effect.
    assert.commandWorked(coll.runCommand(
        'planCacheClearFilters', {query: {a: 'foo', b: 'bar', collation: {locale: 'en_US'}}}));
    assert.eq(1,
              coll.runCommand('planCacheListFilters').filters.length,
              'unexpected number of plan cache filters');

    // Clear plan cache filter.
    assert.commandWorked(coll.runCommand('planCacheClearFilters',
                                         {query: {a: 'foo', b: 5}, collation: {locale: 'en_US'}}));
    assert.eq(0,
              coll.runCommand('planCacheListFilters').filters.length,
              'unexpected number of plan cache filters');
})();