summaryrefslogtreecommitdiff
path: root/jstests/core/wildcard_index_partial_index.js
blob: 730c399705179d999ac3082bb64e9069b1898d60 (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
/**
 * Test that $** indexes work when provided with a partial filter expression.
 * @tags: [
 *   sbe_incompatible,
 * ]
 */
(function() {
"use strict";

load("jstests/libs/analyze_plan.js");  // For isIxScan, isCollscan.

const coll = db.wildcard_partial_index;

function testPartialWildcardIndex(indexKeyPattern, indexOptions) {
    coll.drop();

    assert.commandWorked(coll.createIndex(indexKeyPattern, indexOptions));
    assert.commandWorked(coll.insert({x: 5, a: 2}));  // Not in index.
    assert.commandWorked(coll.insert({x: 6, a: 1}));  // In index.

    // find() operations that should use the index.
    let explain = coll.explain("executionStats").find({x: 6, a: 1}).finish();
    assert.eq(1, explain.executionStats.nReturned);
    assert(isIxscan(db, explain.queryPlanner.winningPlan));
    explain = coll.explain("executionStats").find({x: {$gt: 1}, a: 1}).finish();
    assert.eq(1, explain.executionStats.nReturned);
    assert(isIxscan(db, explain.queryPlanner.winningPlan));
    explain = coll.explain("executionStats").find({x: 6, a: {$lte: 1}}).finish();
    assert.eq(1, explain.executionStats.nReturned);
    assert(isIxscan(db, explain.queryPlanner.winningPlan));

    // find() operations that should not use the index.
    explain = coll.explain("executionStats").find({x: 6, a: {$lt: 1.6}}).finish();
    assert.eq(1, explain.executionStats.nReturned);
    assert(isCollscan(db, explain.queryPlanner.winningPlan));

    explain = coll.explain("executionStats").find({x: 6}).finish();
    assert.eq(1, explain.executionStats.nReturned);
    assert(isCollscan(db, explain.queryPlanner.winningPlan));

    explain = coll.explain("executionStats").find({a: {$gte: 0}}).finish();
    assert.eq(2, explain.executionStats.nReturned);
    assert(isCollscan(db, explain.queryPlanner.winningPlan));
}

// Case where the partial filter expression is on a field in the index.
testPartialWildcardIndex({"$**": 1}, {partialFilterExpression: {a: {$lte: 1.5}}});

// Case where the partial filter expression is on a field not included in the index.
testPartialWildcardIndex({"x.$**": 1}, {partialFilterExpression: {a: {$lte: 1.5}}});

// This part of this test is designed to reproduce SERVER-48614. Historically, the correctness of
// the following queries was impacted by a bug in the plan caching system.
coll.drop();
assert.commandWorked(coll.createIndex({"$**": 1}, {partialFilterExpression: {x: 1}}));
assert.commandWorked(coll.insert({x: 1}));

// Produce an active plan cache entry for a query that can use the index.
for (let i = 0; i < 2; ++i) {
    assert.eq(0, coll.find({x: 1, y: 1}).itcount());
}
// Run a query with a similar shape, but which is not eligible to use the cached plan. This query
// should match the document in the collection (but would fail to match if it incorrectly indexed
// the $eq:null predicate using the wildcard index).
assert.eq(1, coll.find({x: 1, y: null}).itcount());
})();