summaryrefslogtreecommitdiff
path: root/jstests/core/hashed_index_queries.js
blob: 75f5b75bc42cb20abfa0c4f7e3e48f910196441c (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
/**
 * Test to verify the behaviour of find, count, distinct operations in the presence of compound
 * hashed indexes.
 *
 * @tags: [requires_fcv_44]
 */
(function() {
"use strict";
load("jstests/aggregation/extras/utils.js");  // For arrayEq().
load("jstests/libs/analyze_plan.js");         // For assertStagesForExplainOfCommand().

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

for (let i = 0; i < 100; i++) {
    assert.commandWorked(assert.commandWorked(
        coll.insert({a: i, b: {subObj: "str_" + (i % 13)}, c: NumberInt(i % 10)})));
    assert.commandWorked(coll.insert({a: i, b: (i % 13), c: NumberInt(i % 10)}));
}

/**
 * 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});
}

/**
 * Runs count command with the 'filter' and validates that the output returned matches
 * 'expectedOutput'. Also runs explain() command on the same count command and validates that all
 * the 'expectedStages' are present in the plan returned.
 */
function validateCountCmdOutputAndPlan({filter, expectedStages, expectedOutput}) {
    const cmdObj = {count: coll.getName(), query: filter};
    if (expectedOutput) {
        const res = assert.commandWorked(coll.runCommand(cmdObj));
        assert.eq(res.n, expectedOutput);
    }
    assertStagesForExplainOfCommand({coll: coll, cmdObj: cmdObj, expectedStages: expectedStages});
}
/**
 * Tests for 'find' operation when hashed field is prefix.
 */
assert.commandWorked(coll.createIndex({b: "hashed", c: -1}));

// Verify that index is not used for a range query on a hashed field.
validateFindCmdOutputAndPlan({filter: {b: {$gt: 10, $lt: 12}}, expectedStages: ["COLLSCAN"]});

// Verify that index is not used for a query on a hashed field's sub-object.
validateFindCmdOutputAndPlan({filter: {"b.subObj": "str_10"}, expectedStages: ["COLLSCAN"]});

// Verify that index is used for a query on a hashed field.
validateFindCmdOutputAndPlan({
    filter: {b: {subObj: "str_11"}},
    expectedOutput: [
        {a: 11, b: {subObj: "str_11"}, c: 1},
        {a: 24, b: {subObj: "str_11"}, c: 4},
        {a: 37, b: {subObj: "str_11"}, c: 7},
        {a: 50, b: {subObj: "str_11"}, c: 0},
        {a: 63, b: {subObj: "str_11"}, c: 3},
        {a: 76, b: {subObj: "str_11"}, c: 6},
        {a: 89, b: {subObj: "str_11"}, c: 9},
    ],
    expectedStages: ["IXSCAN", "FETCH"],
});

/**
 * Tests for 'find' operation when hashed field is not a prefix.
 */
assert.commandWorked(coll.dropIndexes());
assert.commandWorked(coll.createIndex({a: 1, b: "hashed", c: -1}));

// Verify $in query can use point interval bounds on hashed fields and non-hashed fields.
validateFindCmdOutputAndPlan({
    filter:
        {a: {$in: [38, 37]}, b: {$in: [{subObj: "str_12"}, {subObj: "str_11"}]}, c: {$in: [7, 8]}},
    expectedOutput: [{a: 37, b: {subObj: "str_11"}, c: 7}, {a: 38, b: {subObj: "str_12"}, c: 8}],
    expectedStages: ["IXSCAN", "FETCH"]
});

// Verify that a range query on a non-hashed prefix field can use index.
validateFindCmdOutputAndPlan({
    filter: {a: {$gt: 25, $lt: 29}, b: 0},
    expectedOutput: [{a: 26, b: 0, c: 6}],
    expectedStages: ["IXSCAN", "FETCH"]
});

/**
 * Tests for 'count' operation when hashed field is prefix.
 */
assert.commandWorked(coll.dropIndexes());
assert.commandWorked(coll.createIndex({b: "hashed", a: 1}));

// Verify that index is not used for a range query on a hashed field.
validateCountCmdOutputAndPlan(
    {filter: {b: {$gt: 10, $lt: 12}}, expectedOutput: 7, expectedStages: ["COLLSCAN"]});

// Verify that index is used for a query on a hashed field.
validateCountCmdOutputAndPlan(
    {filter: {b: {subObj: "str_10"}}, expectedOutput: 7, expectedStages: ["IXSCAN", "FETCH"]});

/**
 * Tests for 'count' operation when hashed field is not a prefix.
 */
assert.commandWorked(coll.dropIndexes());
assert.commandWorked(coll.createIndex({a: 1, b: "hashed", c: -1}));

// Verify that range query on a non-hashed prefix field can use index.
validateCountCmdOutputAndPlan({
    filter: {a: {$gt: 25, $lt: 29}, b: 0},
    expectedOutput: 1,
    expectedStages: ["IXSCAN", "FETCH"]
});

/**
 * Tests for 'distinct' operation when hashed field is not a prefix.
 */
assert.commandWorked(coll.dropIndexes());
assert.commandWorked(coll.createIndex({a: -1, b: "hashed", c: 1}));

// 'distinct' on non-hashed prefix fields cannot use DISTINCT_SCAN.
assert.eq(100, coll.distinct("a").length);
let plan = coll.explain("executionStats").distinct("a");
assert(isCollscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);

// 'distinct' on non-prefix fields cannot use index.
assert.eq(26, coll.distinct("b").length);
plan = coll.explain("executionStats").distinct("b");
assert(isCollscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);
assert.eq(10, coll.distinct("c").length);
plan = coll.explain("executionStats").distinct("c");
assert(isCollscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);

// 'distinct' with query predicate can use index for the query part.
assert.eq([2], coll.distinct("c", {a: 12, b: {subObj: "str_12"}}));
plan = coll.explain("executionStats").distinct("c", {a: 12, b: {subObj: "str_12"}});
assert(isIxscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);
assert(planHasStage(db, plan.queryPlanner.winningPlan, "FETCH"), plan.queryPlanner.winningPlan);

/**
 * Tests for 'distinct' operation when hashed field is a prefix.
 */
assert.commandWorked(coll.dropIndexes());
assert.commandWorked(coll.createIndex({b: "hashed", c: 1}));

// 'distinct' on hashed prefix field cannot use index.
assert.eq(26, coll.distinct("b").length);
plan = coll.explain("executionStats").distinct("b");
assert(isCollscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);

// 'distinct' with query predicate can use index for the query part.
assert.eq([1], coll.distinct("b", {b: 1}));
plan = coll.explain("executionStats").distinct("b", {b: 1});
assert(isIxscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);
assert(planHasStage(db, plan.queryPlanner.winningPlan, "FETCH"), plan.queryPlanner.winningPlan);

// 'distinct' with query predicate cannot use index when query cannot use index.
assert.eq([5], coll.distinct("b", {b: {$lt: 6, $gt: 4}}));
plan = coll.explain("executionStats").distinct("b", {b: {$lt: 6, $gt: 4}});
assert(isCollscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);

// 'distinct' with query predicate can use index for the query part.
assert.eq([2], coll.distinct("c", {a: 12, b: 12}));
plan = coll.explain("executionStats").distinct("c", {a: 12, b: 12});
assert(isIxscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);
assert(planHasStage(db, plan.queryPlanner.winningPlan, "FETCH"), plan.queryPlanner.winningPlan);

// 'distinct' on non-prefix fields cannot use index.
assert.eq(10, coll.distinct("c").length);
plan = coll.explain("executionStats").distinct("c");
assert(isCollscan(db, plan.queryPlanner.winningPlan), plan.queryPlanner.winningPlan);
})();