summaryrefslogtreecommitdiff
path: root/jstests/core/sparse_index_supports_ne_null.js
blob: 590bacc5021a6204bb267615e651b9a67b14c68b (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
/*
 * Test that sparse indexes can be used for {$ne: null} queries. Includes tests for (sparse)
 * compound indexes and for cases when {$ne: null} is within an $elemMatch.
 *
 * Cannot run on a sharded collection because different shards may have different plans available
 * depending on how the collection is sharded. (For example, if one shard's index goes multikey,
 * but another's is still not multikey, they may need to use different plans for certain queries).
 * @tags: [assumes_unsharded_collection]
 */
(function() {
"use strict";
load("jstests/libs/analyze_plan.js");  // For getPlanStages.

const coll = db.sparse_index_supports_ne_null;
coll.drop();

function checkQuery({query, shouldUseIndex, nResultsExpected, indexKeyPattern}) {
    const explain = assert.commandWorked(coll.find(query).explain());
    const ixScans = getPlanStages(explain.queryPlanner.winningPlan, "IXSCAN");

    if (shouldUseIndex) {
        assert.gte(ixScans.length, 1, explain);
        assert.eq(ixScans[0].keyPattern, indexKeyPattern);
    } else {
        assert.eq(ixScans.length, 0, explain);
    }

    assert.eq(coll.find(query).itcount(), nResultsExpected);
}

// Non compound case.
(function() {
const query = {
    a: {$ne: null}
};
const elemMatchQuery = {
    a: {$elemMatch: {$ne: null}}
};
const keyPattern = {
    a: 1
};

assert.commandWorked(coll.insert({a: 1}));
assert.commandWorked(coll.insert({a: {x: 1}}));
assert.commandWorked(coll.insert({a: null}));
assert.commandWorked(coll.insert({a: undefined}));

assert.commandWorked(coll.createIndex(keyPattern, {sparse: true}));

// Be sure the index is used.
checkQuery({query: query, shouldUseIndex: true, nResultsExpected: 2, indexKeyPattern: keyPattern});
checkQuery({
    query: elemMatchQuery,
    shouldUseIndex: true,
    nResultsExpected: 0,
    indexKeyPattern: keyPattern
});

// When the index becomes multikey, it cannot support {$ne: null} queries.
assert.commandWorked(coll.insert({a: [1, 2, 3]}));
checkQuery({query: query, shouldUseIndex: false, nResultsExpected: 3, indexKeyPattern: keyPattern});
// But it can support queries with {$ne: null} within an $elemMatch.
checkQuery({
    query: elemMatchQuery,
    shouldUseIndex: true,
    nResultsExpected: 1,
    indexKeyPattern: keyPattern
});
})();

// Compound case.
(function() {
const query = {
    a: {$ne: null}
};
const elemMatchQuery = {
    a: {$elemMatch: {$ne: null}}
};
const keyPattern = {
    a: 1,
    b: 1
};

coll.drop();
assert.commandWorked(coll.insert({a: 1, b: 1}));
assert.commandWorked(coll.insert({a: {x: 1}, b: 1}));
assert.commandWorked(coll.insert({a: null, b: 1}));
assert.commandWorked(coll.insert({a: undefined, b: 1}));

assert.commandWorked(coll.createIndex(keyPattern, {sparse: true}));

// Be sure the index is used.
checkQuery({query: query, shouldUseIndex: true, nResultsExpected: 2, indexKeyPattern: keyPattern});
checkQuery({
    query: elemMatchQuery,
    shouldUseIndex: true,
    nResultsExpected: 0,
    indexKeyPattern: keyPattern
});

// When the index becomes multikey on the second field, it should still be usable.
assert.commandWorked(coll.insert({a: 1, b: [1, 2, 3]}));
checkQuery({query: query, shouldUseIndex: true, nResultsExpected: 3, indexKeyPattern: keyPattern});
checkQuery({
    query: elemMatchQuery,
    shouldUseIndex: true,
    nResultsExpected: 0,
    indexKeyPattern: keyPattern
});

// When the index becomes multikey on the first field, it should no longer be usable.
assert.commandWorked(coll.insert({a: [1, 2, 3], b: 1}));
checkQuery({query: query, shouldUseIndex: false, nResultsExpected: 4, indexKeyPattern: keyPattern});
// Queries which use a $elemMatch should still be able to use the index.
checkQuery({
    query: elemMatchQuery,
    shouldUseIndex: true,
    nResultsExpected: 1,
    indexKeyPattern: keyPattern
});
})();

// Nested field multikey with $elemMatch.
(function() {
const keyPattern = {
    "a.b.c.d": 1
};
coll.drop();
assert.commandWorked(coll.insert({a: {b: [{c: {d: 1}}]}}));
assert.commandWorked(coll.insert({a: {b: [{c: {d: {e: 1}}}]}}));
assert.commandWorked(coll.insert({a: {b: [{c: {d: null}}]}}));
assert.commandWorked(coll.insert({a: {b: [{c: {d: undefined}}]}}));

assert.commandWorked(coll.createIndex(keyPattern, {sparse: true}));

const query = {
    "a.b.c.d": {$ne: null}
};
// $elemMatch object can only use the index when none of the paths below the $elemMatch is
// not multikey.
const elemMatchObjectQuery = {
    "a.b": {$elemMatch: {"c.d": {$ne: null}}}
};
// $elemMatch value can always use the index.
const elemMatchValueQuery = {
    "a.b.c.d": {$elemMatch: {$ne: null}}
};

// 'a.b' is multikey, so the index isn't used.
checkQuery({query: query, shouldUseIndex: false, nResultsExpected: 2, indexKeyPattern: keyPattern});
// Since the multikey portion is above the $elemMatch, the $elemMatch query may use the
// index.
checkQuery({
    query: elemMatchObjectQuery,
    shouldUseIndex: true,
    nResultsExpected: 2,
    indexKeyPattern: keyPattern
});
checkQuery({
    query: elemMatchValueQuery,
    shouldUseIndex: true,
    nResultsExpected: 0,
    indexKeyPattern: keyPattern
});

// Make the index become multikey on 'a' (another field above the $elemMatch).
assert.commandWorked(coll.insert({a: [{b: [{c: {d: 1}}]}]}));
checkQuery({query: query, shouldUseIndex: false, nResultsExpected: 3, indexKeyPattern: keyPattern});
// The only multikey paths are still above the $elemMatch, queries which use a $elemMatch
// should still be able to use the index.
checkQuery({
    query: elemMatchObjectQuery,
    shouldUseIndex: true,
    nResultsExpected: 3,
    indexKeyPattern: keyPattern
});
checkQuery({
    query: elemMatchValueQuery,
    shouldUseIndex: true,
    nResultsExpected: 0,
    indexKeyPattern: keyPattern
});

// Make the index multikey for 'a.b.c'. Now the $elemMatch query may not use the index.
assert.commandWorked(coll.insert({a: {b: [{c: [{d: 1}]}]}}));
checkQuery({query: query, shouldUseIndex: false, nResultsExpected: 4, indexKeyPattern: keyPattern});
checkQuery({
    query: elemMatchObjectQuery,
    shouldUseIndex: false,
    nResultsExpected: 4,
    indexKeyPattern: keyPattern
});
checkQuery({
    query: elemMatchValueQuery,
    shouldUseIndex: true,
    nResultsExpected: 0,
    indexKeyPattern: keyPattern
});
})();
})();