summaryrefslogtreecommitdiff
path: root/jstests/core/hashed_partial_and_sparse_index.js
blob: 6a052fe4bb9d07d23e08a6d301ec7e3b7797e671 (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
/**
 * Tests to verify that the queries return correct results in the presence of partial hashed
 * index and sparse index. The test verifies compound hashed index with hashed prefix and non-hashed
 * prefix.
 * @tags: [
 * ]
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // For arrayEq().
load("jstests/libs/analyze_plan.js");         // For assertStagesForExplainOfCommand().

const coll = db.hashed_partial_index;
coll.drop();
assert.commandWorked(coll.insert({}));
assert.commandWorked(coll.insert({a: null}));
assert.commandWorked(coll.insert({a: 1}));
assert.commandWorked(coll.insert({b: 4}));
assert.commandWorked(coll.insert({a: 1, b: 6}));

/**
 * Runs find command with the 'filter' and validates that the output returned matches
 * 'expectedOutput'. Also runs explain() command on the same find command and validates that all
 * the 'expectedStages' are present in the plan returned.
 */
function validateFindCmdOutputAndPlan({filter, expectedStages, expectedOutput}) {
    const cmdObj = {find: coll.getName(), filter: filter, projection: {_id: 0}};
    if (expectedOutput) {
        const res = assert.commandWorked(coll.runCommand(cmdObj));
        const ouputArray = new DBCommandCursor(coll.getDB(), res).toArray();

        // We ignore the order since hashed index order is not predictable.
        assert(arrayEq(expectedOutput, ouputArray), ouputArray);
    }
    assertStagesForExplainOfCommand({coll: coll, cmdObj: cmdObj, expectedStages: expectedStages});
}

function testSparseHashedIndex(indexSpec) {
    assert.commandWorked(coll.dropIndexes());
    assert.commandWorked(coll.createIndex(indexSpec, {sparse: true}));

    // Verify index not used for null/missing queries with sparse index.
    validateFindCmdOutputAndPlan({filter: {a: null}, expectedStages: ["COLLSCAN"]});
    validateFindCmdOutputAndPlan({filter: {a: {$exists: false}}, expectedStages: ["COLLSCAN"]});

    // Verify index can be used for non-null queries with sparse index.
    validateFindCmdOutputAndPlan({
        filter: {a: {$exists: true}},
        expectedOutput: [{a: null}, {a: 1}, {a: 1, b: 6}],
        expectedStages: ["IXSCAN", "FETCH"]
    });

    validateFindCmdOutputAndPlan({
        filter: {a: 1, b: 6},
        expectedOutput: [{a: 1, b: 6}],
        expectedStages: ["IXSCAN", "FETCH"]
    });

    // Test {$exists: false} when hashed field is not a prefix and index is sparse.
    validateFindCmdOutputAndPlan({
        filter: {a: {$exists: false}},
        expectedOutput: [{b: 4}, {}],
        expectedStages: ["COLLSCAN"],
        stagesNotExpected: ["IXSCAN"]
    });
}

/**
 * Tests sparse indexes with non-hashed prefix.
 */
testSparseHashedIndex({a: 1, b: "hashed", c: -1});

/**
 * Test sparse indexes with hashed prefix.
 */
testSparseHashedIndex({a: "hashed", b: 1});

/**
 * Tests for partial indexes.
 */
[{b: "hashed", c: 1}, {b: 1, c: "hashed", d: 1}].forEach((index) => {
    assert.commandWorked(coll.dropIndexes());
    assert.commandWorked(coll.createIndex(index, {partialFilterExpression: {b: {$gt: 5}}}));

    // Verify that index is not used if the query predicate doesn't match the
    // 'partialFilterExpression'.
    validateFindCmdOutputAndPlan({filter: {b: 4}, expectedStages: ["COLLSCAN"]});

    // Verify that index is used if the query predicate matches the 'partialFilterExpression'.
    validateFindCmdOutputAndPlan(
        {filter: {b: 6}, expectedOutput: [{a: 1, b: 6}], expectedStages: ["IXSCAN", "FETCH"]});
});
})();