summaryrefslogtreecommitdiff
path: root/jstests/core/plan_cache_list_shapes.js
blob: fb776a19adfb95c6d37896f857cb07ebcc621b85 (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
// Test using the $planCacheStats aggregation metadata source to list all of the query shapes cached
// for a particular collection.
//
// @tags: [
//   # If all chunks are moved off of a shard, it can cause the plan cache to miss commands.
//   assumes_balancer_off,
//   does_not_support_stepdowns,
//   # This test attempts to perform queries with plan cache filters set up. The former operation
//   # may be routed to a secondary in the replica set, whereas the latter must be routed to the
//   # primary.
//   assumes_read_concern_unchanged,
//   assumes_read_preference_unchanged,
//   assumes_unsharded_collection,
//   sbe_incompatible,
// ]
(function() {
const coll = db.jstests_plan_cache_list_shapes;
coll.drop();

function dumpPlanCacheState() {
    return coll.aggregate([{$planCacheStats: {}}]).toArray();
}

// Utility function to list query shapes in cache.
function getCachedQueryShapes() {
    return coll.aggregate([{$planCacheStats: {}}, {$replaceWith: '$createdFromQuery'}]).toArray();
}

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

// We need two indices so that the MultiPlanRunner is executed.
assert.commandWorked(coll.createIndex({a: 1}));
assert.commandWorked(coll.createIndex({a: 1, b: 1}));

// Run a query.
assert.eq(1, coll.find({a: 1, b: 1}, {_id: 1, a: 1}).sort({a: -1}).itcount());

// We now expect the two indices to be compared and a cache entry to exist.  Retrieve query
// shapes from the test collection Number of shapes should match queries executed by multi-plan
// runner.
let shapes = getCachedQueryShapes();
assert.eq(1, shapes.length, dumpPlanCacheState());
assert.eq({query: {a: 1, b: 1}, sort: {a: -1}, projection: {_id: 1, a: 1}}, shapes[0]);

// Running a different query shape should cause another entry to be cached.
assert.eq(1, coll.find({a: 1, b: 1}).itcount());
shapes = dumpPlanCacheState();
assert.eq(2, shapes.length, shapes);

// Check that each shape has a unique queryHash.
assert.neq(shapes[0]["queryHash"], shapes[1]["queryHash"]);

// Check that queries with different regex options have distinct shapes.

// Insert some documents with strings so we have something to search for.
for (let i = 0; i < 5; i++) {
    assert.commandWorked(coll.insert({a: 3, s: 'hello world'}));
}
assert.commandWorked(coll.insert({a: 3, s: 'hElLo wOrLd'}));

// Run a query with a regex. Also must include 'a' so that the query may use more than one
// index, and thus, must use the MultiPlanner.
const regexQuery = {
    s: {$regex: 'hello world', $options: 'm'},
    a: 3
};
assert.eq(5, coll.find(regexQuery).itcount());

shapes = getCachedQueryShapes();
assert.eq(3, shapes.length, shapes);

// Run the same query, but with different regex options. We expect that this should cause a
// shape to get added.
regexQuery.s.$options = 'mi';
// There is one more result since the query is now case sensitive.
assert.eq(6, coll.find(regexQuery).itcount());
shapes = getCachedQueryShapes();
assert.eq(4, shapes.length, shapes);
})();