[SERVER-81807] Cached SBE plans have let-bound constants baked in Created: 03/Oct/23  Updated: 02/Feb/24

Status: In Code Review
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: David Percy Assignee: Ruoxin Xu
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-62100 Refresh “let” variables on SBE plan r... Closed
Assigned Teams:
Query Optimization
Operating System: ALL
Steps To Reproduce:

const coll = db.example;
coll.drop();
assert.commandWorked(coll.insert({}));
 
let result;
 
result = assert
             .commandWorked(db.runCommand({
                 find: coll.getName(),
                 filter: {},
                 projection: {_id: "$$a"},
                 limit: 1,
                 let : {a: {$add: [2, 3]}},
             }))
             .cursor.firstBatch;
assert.eq(result, [{_id: 5}]);
 
jsTestLog('plan cache contents');
printjson(coll.getPlanCache().list());
 
result = assert
             .commandWorked(db.runCommand({
                 find: coll.getName(),
                 filter: {},
                 projection: {_id: "$$a"},
                 limit: 1,
                 let : {a: {$add: [200, 300]}},
             }))
             .cursor.firstBatch;
assert.eq(result, [{_id: 500}]);

Sprint: QO 2024-02-05, QO 2024-02-19
Participants:

 Description   

If you run two queries with the same filter, sort, and projection, they map to the same plan cache key. This happens even when they have different top-level let bindings.

The attached script fails on the default configuration (trySbeEngine). Printing the plan cache in between the two queries shows that the constant 5 is baked in to the plan:

[3] limit 1 
[2] project [s3 = makeBsonObj(MakeObjSpec(["_id" = Arg(0)], Closed), s1, 5L)] 
[1] scan s1 s2 none none none none none none [] @"97a0d744-17cb-4332-aee5-2f196db32553" true false 

Running with forceClassicEngine, the test passes, so this is specific to the SBE stagebuilders plan cache.


Generated at Thu Feb 08 06:47:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.