/
timeseries_index_use.js
230 lines (198 loc) · 9.63 KB
/
timeseries_index_use.js
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
/**
* Tests index usage on meta and time fields for timeseries collections.
*
* @tags: [
* assumes_no_implicit_collection_creation_after_drop,
* does_not_support_stepdowns,
* does_not_support_transactions,
* requires_fcv_49,
* requires_find_command,
* requires_getmore,
* ]
*/
(function() {
"use strict";
load("jstests/core/timeseries/libs/timeseries.js");
load("jstests/libs/analyze_plan.js");
if (!TimeseriesTest.timeseriesCollectionsEnabled(db.getMongo())) {
jsTestLog("Skipping test because the time-series collection feature flag is disabled");
return;
}
const testDB = db.getSiblingDB(jsTestName());
assert.commandWorked(testDB.dropDatabase());
// Create timeseries collection.
const timeFieldName = 'time';
const metaFieldName = 'meta';
const coll = testDB.getCollection('t');
const bucketsColl = testDB.getCollection('system.buckets.' + coll.getName());
/**
* Sets up an empty time-series collection with options 'collOpts' on namespace 't' using
* 'timeFieldName' and 'metaFieldName'. Checks that the buckets collection is created, as well.
*/
function resetCollections(collOpts = {}) {
coll.drop(); // implicitly drops bucketsColl.
assert.commandWorked(testDB.createCollection(
coll.getName(),
Object.assign({timeseries: {timeField: timeFieldName, metaField: metaFieldName}},
collOpts)));
const dbCollNames = testDB.getCollectionNames();
assert.contains(
bucketsColl.getName(),
dbCollNames,
"Failed to find namespace '" + bucketsColl.getName() + "' amongst: " + tojson(dbCollNames));
}
/**
* Creates the index specified by the spec and options, then explains the query to ensure that the
* created index is used. Runs the query and verifies that the expected number of documents are
* matched. Finally, deletes the created index.
*/
const testQueryUsesIndex = function(filter, numMatches, indexSpec, indexOpts = {}, queryOpts = {}) {
assert.commandWorked(
coll.createIndex(indexSpec, Object.assign({name: "testIndexName"}, indexOpts)));
let query = coll.find(filter);
if (queryOpts.collation)
query = query.collation(queryOpts.collation);
assert.eq(numMatches, query.itcount());
const explain = query.explain();
const ixscan = getAggPlanStage(explain, "IXSCAN");
assert.neq(null, ixscan, tojson(explain));
// TODO (SERVER-56238): Remove conditional once queries always use the 'testIndexName' index.
assert.eq(TimeseriesTest.supportsClusteredIndexes(db.getMongo()) ||
!filter.hasOwnProperty(timeFieldName) ||
!indexSpec.hasOwnProperty(timeFieldName)
? "testIndexName"
: "_id_",
ixscan.indexName,
tojson(ixscan));
assert.commandWorked(coll.dropIndex("testIndexName"));
};
/*********************************** Tests scalar meta values ***********************************/
resetCollections();
assert.commandWorked(coll.insert([
{_id: 0, [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'), [metaFieldName]: 2},
{_id: 1, [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'), [metaFieldName]: 3},
{_id: 2, [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'), [metaFieldName]: 2}
]));
const timeDate = ISODate('2005-01-01 00:00:00.000Z');
// Test ascending and descending index on timeField.
testQueryUsesIndex({'time': {$lte: timeDate}}, 2, {'time': 1});
testQueryUsesIndex({'time': {$gte: timeDate}}, 1, {'time': -1});
// Test ascending and descending index on metaField.
testQueryUsesIndex({'meta': {$eq: 3}}, 1, {'meta': 1});
testQueryUsesIndex({'meta': {$lt: 3}}, 2, {'meta': -1});
// Test compound indexes on metaField and timeField.
testQueryUsesIndex({'meta': {$gte: 2}}, 3, {'meta': 1, 'time': 1});
testQueryUsesIndex({'time': {$lt: timeDate}}, 2, {'time': 1, 'meta': 1});
testQueryUsesIndex({'meta': {$lte: 3}, 'time': {$gte: timeDate}}, 1, {'meta': 1, 'time': 1});
testQueryUsesIndex({'meta': {$eq: 2}, 'time': {$lte: timeDate}}, 1, {'meta': -1, 'time': -1});
testQueryUsesIndex({'meta': {$lt: 3}, 'time': {$lte: timeDate}}, 1, {'time': 1, 'meta': -1});
/*********************************** Tests object meta values ***********************************/
resetCollections();
assert.commandWorked(coll.insert([
{_id: 0, [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'), [metaFieldName]: {a: 1}},
{_id: 1, [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'), [metaFieldName]: {a: 4, b: 5}},
{_id: 2, [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'), [metaFieldName]: {a: "1", c: 0}}
]));
// Test indexes on subfields of metaField.
testQueryUsesIndex({'meta.a': {$gt: 3}}, 1, {'meta.a': 1});
testQueryUsesIndex({'meta.a': {$type: 'string'}}, 1, {'meta.a': -1});
testQueryUsesIndex({'meta.b': {$gte: 0}}, 1, {'meta.b': 1}, {sparse: true});
testQueryUsesIndex({'meta': {$eq: {a: 1}}}, 1, {'meta': 1});
testQueryUsesIndex({'meta': {$in: [{a: 1}, {a: 4, b: 5}]}}, 2, {'meta': 1});
// Test compound indexes on multiple subfields of metaField.
testQueryUsesIndex({'meta.a': {$lt: 3}}, 1, {'meta.a': 1, 'meta.b': -1});
testQueryUsesIndex({'meta.a': {$lt: 5}, 'meta.b': {$eq: 5}}, 1, {'meta.a': -1, 'meta.b': 1});
testQueryUsesIndex(
{$or: [{'meta.a': {$eq: 1}}, {'meta.a': {$eq: "1"}}]}, 2, {'meta.a': -1, 'meta.b': -1});
testQueryUsesIndex({'meta.b': {$lte: 5}}, 1, {'meta.b': 1, 'meta.c': 1}, {sparse: true});
testQueryUsesIndex(
{'meta.b': {$lte: 5}, 'meta.c': {$lte: 4}}, 0, {'meta.b': 1, 'meta.c': 1}, {sparse: true});
// Test compound indexes on timeField and subfields of metaField.
testQueryUsesIndex({'meta.a': {$gte: 2}}, 1, {'meta.a': 1, 'time': 1});
testQueryUsesIndex({'time': {$lt: timeDate}}, 2, {'time': 1, 'meta.a': 1});
testQueryUsesIndex({'meta.a': {$lte: 4}, 'time': {$lte: timeDate}}, 2, {'meta.a': 1, 'time': 1});
testQueryUsesIndex({'meta.a': {$lte: 4}, 'time': {$lte: timeDate}}, 2, {'meta.a': 1, 'time': -1});
testQueryUsesIndex({'meta.a': {$eq: "1"}, 'time': {$gt: timeDate}}, 1, {'time': -1, 'meta.a': 1});
// Test wildcard indexes with metaField.
testQueryUsesIndex({'meta.a': {$lt: 3}}, 1, {'meta.$**': 1});
testQueryUsesIndex({'meta.b': {$gt: 3}}, 1, {'meta.$**': 1});
// Test hashed indexes on metaField.
testQueryUsesIndex({'meta': {$eq: {a: 1}}}, 1, {'meta': "hashed"});
testQueryUsesIndex({'meta.a': {$eq: 1}}, 1, {'meta.a': "hashed"});
testQueryUsesIndex({'meta.a': {$eq: 1}}, 1, {'meta.a': "hashed", 'meta.b': -1});
testQueryUsesIndex({'meta.a': {$eq: 1}, 'meta.b': {$gt: 0}}, 0, {'meta.a': "hashed", 'meta.b': -1});
testQueryUsesIndex({'meta.a': {$eq: 1}, 'meta.b': {$gt: 0}}, 0, {'meta.b': -1, 'meta.a': "hashed"});
/************************************ Tests array meta values ************************************/
resetCollections();
assert.commandWorked(coll.insert([
{_id: 0, [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'), [metaFieldName]: [1, 2, 3]},
{_id: 1, [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'), [metaFieldName]: ['a', 'b']},
{
_id: 2,
[timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'),
[metaFieldName]: [{a: 1}, {b: 0}]
}
]));
// Test multikey indexes on metaField.
testQueryUsesIndex({'meta': {$eq: {a: 1}}}, 1, {'meta': 1});
testQueryUsesIndex({'meta': {$eq: 2}}, 1, {'meta': 1});
testQueryUsesIndex({'meta': {$gte: {a: 1}}}, 1, {'meta': -1});
resetCollections();
assert.commandWorked(coll.insert([
{
_id: 0,
[timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'),
[metaFieldName]: {a: [1, 2, 3], b: 0}
},
{
_id: 1,
[timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'),
[metaFieldName]: {a: ['a', 'b'], b: 1}
},
{
_id: 2,
[timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'),
[metaFieldName]: {a: [{b: 1}, {c: 0}]}
}
]));
// Test multikey indexes on subfields of metaFields.
testQueryUsesIndex({'meta.a': {$eq: {b: 1}}}, 1, {'meta.a': 1});
testQueryUsesIndex({'meta.a': {$eq: 2}}, 1, {'meta.a': 1});
testQueryUsesIndex({'meta.a': {$gte: {a: 1}}}, 1, {'meta.a': -1});
testQueryUsesIndex({'meta.a': {$gte: 1}, 'meta.b': {$exists: 1}}, 1, {'meta.a': -1, 'meta.b': 1});
/************************************ Tests string meta values ***********************************/
const collation = {
collation: {locale: 'en', strength: 1, numericOrdering: true}
};
// Create timeseries collection with a collation.
resetCollections(collation);
assert.commandWorked(coll.insert([
{_id: 0, [timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'), [metaFieldName]: "hello hello"},
{_id: 1, [timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'), [metaFieldName]: "hello world"},
{_id: 2, [timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'), [metaFieldName]: "bye bye"}
]));
// Test index on metaField when collection collation matches query collation.
testQueryUsesIndex({'meta': {$eq: "bye bye"}}, 1, {'meta': 1}, {}, collation);
testQueryUsesIndex({'meta': {$gte: "hello hello"}}, 2, {'meta': -1}, {}, collation);
resetCollections(collation);
assert.commandWorked(coll.insert([
{
_id: 0,
[timeFieldName]: ISODate('1990-01-01 00:00:00.000Z'),
[metaFieldName]: {a: "hello hello", b: "hello"}
},
{
_id: 1,
[timeFieldName]: ISODate('2000-01-01 00:00:00.000Z'),
[metaFieldName]: {a: "hello world", b: "hello"}
},
{
_id: 2,
[timeFieldName]: ISODate('2010-01-01 00:00:00.000Z'),
[metaFieldName]: {a: "bye bye", b: "bye"}
}
]));
// Test index on subfields of metaField when collection collation matches query collation.
testQueryUsesIndex({'meta.a': {$eq: "bye bye"}}, 1, {'meta.a': 1}, {}, collation);
testQueryUsesIndex({'meta.b': {$gt: "bye bye"}}, 2, {'meta.b': -1}, {}, collation);
})();