summaryrefslogtreecommitdiff
path: root/jstests/core/index/wildcard/wildcard_index_filter.js
blob: 879e245c4224705d2ef26a0475007139770e2db7 (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
/**
 * Test that $** indexes obey index filter rules.
 *
 * Does not support stepdowns, because the stepdown/kill_primary passthroughs will reject commands
 * that may return different values after a failover; in this case, 'planCacheClearFilters'.
 * The test runs commands that are not allowed with security token: planCacheClearFilters,
 * planCacheListFilters, planCacheSetFilter.
 * @tags: [
 *   not_allowed_with_security_token,
 *   does_not_support_stepdowns,
 * ]
 */
(function() {
"use strict";

load("jstests/libs/analyze_plan.js");
load("jstests/libs/fixture_helpers.js");  // For 'isMongos()'.

const coll = db.wildcard_index_filter;

// Utility function to list index filters.
function getFilters() {
    const res = assert.commandWorked(coll.runCommand('planCacheListFilters'));
    assert(res.hasOwnProperty('filters'), 'filters missing from planCacheListFilters result');
    return res.filters;
}

// Sets an index filter given a query shape then confirms that the expected index was used to
// answer a query.
function assertExpectedIndexAnswersQueryWithFilter(
    filterQuery, filterIndexes, query, expectedIndexName, hint) {
    // Clear existing cache filters.
    assert.commandWorked(coll.runCommand('planCacheClearFilters'), 'planCacheClearFilters failed');

    // Make sure that the filter is set correctly.
    assert.commandWorked(
        coll.runCommand('planCacheSetFilter', {query: filterQuery, indexes: filterIndexes}));
    assert.eq(1,
              getFilters().length,
              'no change in query settings after successfully setting index filters');

    // Check that expectedIndex index was used over another index.
    let explain;
    if (hint === undefined) {
        explain = assert.commandWorked(coll.find(query).explain('executionStats'));
    } else {
        explain = assert.commandWorked(coll.find(query).hint(hint).explain('executionStats'));
    }

    const winningPlan = getWinningPlan(explain.queryPlanner);
    const planStages = getPlanStages(winningPlan, 'IXSCAN');

    if (FixtureHelpers.isMongos(db)) {
        assert.gte(planStages.length, 1, explain);
    } else {
        // If we're not running on a sharded cluster, there should be exactly one IXSCAN stage.
        assert.eq(planStages.length, 1, explain);
    }

    for (const stage of planStages) {
        assert(stage.hasOwnProperty('indexName'), stage);
        assert.eq(stage.indexName, expectedIndexName, stage);
    }
}

const indexWildcard = {
    "$**": 1
};
const indexA = {
    "a": 1
};
assert.commandWorked(coll.createIndex(indexWildcard));
assert.commandWorked(coll.createIndex(indexA));

assert.commandWorked(coll.insert({a: "a"}));

// Filtering on $** index. $** index is used over another index.
assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexWildcard], {a: "a"}, "$**_1");

// Filtering on regular index. $** index is not used over another index.
assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexA], {a: "a"}, "a_1");

assert.commandWorked(coll.insert({a: "a", b: "b"}));

const indexAB = {
    "a": 1,
    "b": 1
};
assert.commandWorked(coll.createIndex(indexAB));

// Filtering on $** index. $** index is used over another index for compound query.
assertExpectedIndexAnswersQueryWithFilter(
    {a: "a", b: "b"}, [indexWildcard], {a: "a", b: "b"}, "$**_1");

// Filtering on regular compound index. Check that $** index is not used over another index
// for compound query.
assertExpectedIndexAnswersQueryWithFilter({a: "a", b: "b"}, [indexAB], {a: "a", b: "b"}, "a_1_b_1");

// Filtering on $** index while hinting on another index. Index filter is prioritized.
assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexWildcard], {a: "a"}, "$**_1", indexA);

// Filtering on regular index while hinting on $** index. Index filter is prioritized.
assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexA], {a: "a"}, "a_1", indexWildcard);

// Index filter for $** index does not apply when query does not match filter query shape.
assertExpectedIndexAnswersQueryWithFilter({b: "b"}, [indexWildcard], {a: "a"}, "a_1", indexA);

const indexAWildcard = {
    "a.$**": 1
};
assert.commandWorked(coll.createIndex(indexAWildcard));

// Filtering on a path specified $** index. Check that the $** is used over other indices.
assertExpectedIndexAnswersQueryWithFilter({a: "a"}, [indexAWildcard], {a: "a"}, "a.$**_1");
})();