summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/plan_cache_group_lookup.js
blob: 8975bdf96a0d8719143a3798042188e96bd9f64f (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
 * Test that plans with $group and $lookup lowered to SBE are cached and invalidated correctly.
 * @tags: [
 *   # TODO SERVER-67607: Test plan cache with CQF enabled.
 *   cqf_incompatible,
 * ]
 */
(function() {
"use strict";

load("jstests/libs/profiler.js");  // For getLatestProfilerEntry.
load("jstests/libs/sbe_util.js");  // For checkSBEEnabled.

const conn = MongoRunner.runMongod();
const db = conn.getDB("test");
const coll = db.plan_cache_pipeline;
const foreignColl = db.plan_cache_pipeline_foreign;

if (!checkSBEEnabled(db)) {
    jsTest.log("Skipping test because SBE is not enabled");
    MongoRunner.stopMongod(conn);
    return;
}

assert.commandWorked(coll.insert({a: 1}));
assert.commandWorked(coll.createIndex({a: 1, a1: 1}));
assert.commandWorked(coll.createIndex({a: 1, a2: 1}));
function setupForeignColl(index) {
    foreignColl.drop();
    assert.commandWorked(foreignColl.insert({b: 1}));
    if (index) {
        assert.commandWorked(foreignColl.createIndex(index));
    }
}
assert.commandWorked(db.setProfilingLevel(2));

/**
 * Assert that the last aggregation command has a corresponding plan cache entry with the desired
 * properties. 'version' is 1 if it's classic cache, 2 if it's SBE cache. 'isActive' is true if the
 * cache entry is active. 'fromMultiPlanner' is true if the query part of aggregation has been
 * multi-planned. 'fromPlanCache' is true if the winning plan was retrieved from the plan cache.
 * 'forcesClassicEngine' is true if the query is forced to use classic engine.
 */
function assertCacheUsage(
    {version, fromMultiPlanner, fromPlanCache, isActive, forcesClassicEngine = false}) {
    const profileObj = getLatestProfilerEntry(
        db, {op: "command", "command.pipeline": {$exists: true}, ns: coll.getFullName()});
    assert.eq(fromMultiPlanner, !!profileObj.fromMultiPlanner, profileObj);
    assert.eq(fromPlanCache, !!profileObj.fromPlanCache, profileObj);

    const entries = coll.getPlanCache().list();
    assert.eq(entries.length, 1, entries);
    const entry = entries[0];
    assert.eq(entry.version, version, entry);
    assert.eq(entry.isActive, isActive, entry);
    assert.eq(entry.planCacheKey, profileObj.planCacheKey, entry);

    const explain = coll.explain().aggregate(profileObj.command.pipeline);
    const queryPlanner = explain.hasOwnProperty("queryPlanner")
        ? explain.queryPlanner
        : explain.stages[0].$cursor.queryPlanner;
    if (!forcesClassicEngine) {
        assert(queryPlanner.winningPlan.hasOwnProperty("slotBasedPlan"), explain);
    }
    assert.eq(queryPlanner.planCacheKey, entry.planCacheKey, explain);

    return entry;
}

/**
 * Run the pipeline three times, assert that we have the following plan cache entries of "version".
 *      1. The pipeline runs from the multi-planner, saving an inactive cache entry.
 *      2. The pipeline runs from the multi-planner, activating the cache entry.
 *      3. The pipeline runs from cached solution planner, using the active cache entry.
 */
function testLoweredPipeline({pipeline, version, forcesClassicEngine = false}) {
    let results = coll.aggregate(pipeline).toArray();
    assert.eq(results.length, 1, results);
    const entry = assertCacheUsage({
        version: version,
        fromMultiPlanner: true,
        fromPlanCache: false,
        isActive: false,
        forcesClassicEngine
    });

    results = coll.aggregate(pipeline).toArray();
    assert.eq(results.length, 1, results);
    let nextEntry = assertCacheUsage({
        version: version,
        fromMultiPlanner: true,
        fromPlanCache: false,
        isActive: true,
        forcesClassicEngine
    });
    assert.eq(entry.planCacheKey, nextEntry.planCacheKey, {entry, nextEntry});

    results = coll.aggregate(pipeline).toArray();
    assert.eq(results.length, 1, results);
    nextEntry = assertCacheUsage({
        version: version,
        fromMultiPlanner: false,
        fromPlanCache: true,
        isActive: true,
        forcesClassicEngine
    });
    assert.eq(entry.planCacheKey, nextEntry.planCacheKey, {entry, nextEntry});

    return nextEntry;
}

const multiPlanningQueryStage = {
    $match: {a: 1}
};
const lookupStage = {
    $lookup: {from: foreignColl.getName(), localField: "a", foreignField: "b", as: "matched"}
};
const groupStage = {
    $group: {_id: "$a", out: {"$sum": 1}}
};

(function testLoweredPipelineCombination() {
    setupForeignColl();
    const expectedVersion = 2;

    coll.getPlanCache().clear();
    testLoweredPipeline(
        {pipeline: [multiPlanningQueryStage, lookupStage], version: expectedVersion});

    coll.getPlanCache().clear();
    testLoweredPipeline(
        {pipeline: [multiPlanningQueryStage, groupStage], version: expectedVersion});

    coll.getPlanCache().clear();
    testLoweredPipeline(
        {pipeline: [multiPlanningQueryStage, lookupStage, groupStage], version: expectedVersion});

    coll.getPlanCache().clear();
    testLoweredPipeline(
        {pipeline: [multiPlanningQueryStage, groupStage, lookupStage], version: expectedVersion});
})();

(function testPartiallyLoweredPipeline() {
    coll.getPlanCache().clear();
    setupForeignColl();
    testLoweredPipeline({
        pipeline: [multiPlanningQueryStage, lookupStage, {$_internalInhibitOptimization: {}}],
        version: 2
    });
})();

(function testNonExistentForeignCollectionCache() {
    coll.getPlanCache().clear();
    foreignColl.drop();
    const entryWithoutForeignColl =
        testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});

    coll.getPlanCache().clear();
    setupForeignColl();
    const entryWithForeignColl =
        testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});

    assert.neq(entryWithoutForeignColl.planCacheKey,
               entryWithForeignColl.planCacheKey,
               {entryWithoutForeignColl, entryWithForeignColl});
    assert.eq(entryWithoutForeignColl.queryHash,
              entryWithForeignColl.queryHash,
              {entryWithoutForeignColl, entryWithForeignColl});
})();

(function testForeignCollectionDropCacheInvalidation() {
    coll.getPlanCache().clear();
    setupForeignColl();
    testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});

    foreignColl.drop();
    testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});
})();

(function testForeignIndexDropCacheInvalidation() {
    coll.getPlanCache().clear();
    setupForeignColl({b: 1} /* index */);
    testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});

    assert.commandWorked(foreignColl.dropIndex({b: 1}));
    testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});
})();

(function testForeignIndexBuildCacheInvalidation() {
    coll.getPlanCache().clear();
    setupForeignColl({b: 1} /* index */);
    testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});

    assert.commandWorked(foreignColl.createIndex({c: 1}));
    testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});
})();

(function testLookupSbeAndClassicPlanCacheKey() {
    setupForeignColl({b: 1} /* index */);

    // When using SBE engine, the plan cache key of $match vs. $match + $lookup should be different.
    coll.getPlanCache().clear();
    let matchEntry = testLoweredPipeline({pipeline: [multiPlanningQueryStage], version: 2});

    coll.getPlanCache().clear();
    let lookupEntry =
        testLoweredPipeline({pipeline: [multiPlanningQueryStage, lookupStage], version: 2});
    assert.neq(matchEntry.planCacheKey, lookupEntry.planCacheKey, {matchEntry, lookupEntry});

    // When using classic engine, the plan cache key of $match vs. $match + $lookup should be the
    // same.
    assert.commandWorked(
        db.adminCommand({setParameter: 1, internalQueryFrameworkControl: "forceClassicEngine"}));

    coll.getPlanCache().clear();
    matchEntry = testLoweredPipeline(
        {pipeline: [multiPlanningQueryStage], version: 1, forcesClassicEngine: true});

    coll.getPlanCache().clear();
    lookupEntry = testLoweredPipeline(
        {pipeline: [multiPlanningQueryStage, lookupStage], version: 1, forcesClassicEngine: true});
    assert.eq(matchEntry.planCacheKey, lookupEntry.planCacheKey, {matchEntry, lookupEntry});

    assert.commandWorked(
        db.adminCommand({setParameter: 1, internalQueryFrameworkControl: "trySbeEngine"}));
})();

MongoRunner.stopMongod(conn);
})();