/**
* JOO tassert 4946301 "undefined slot accessor": a nested loop join whose left (outer) side is a
* foreign node with a sub-pipeline $project that renames the field a later join reads.
*
* Plan that fails (b is the sub-pipeline node): HJ( NLJ(PROJECTION(COLLSCAN b), COLLSCAN a), c ).
* The join method is forced here; the same plan is chosen naturally with e.g. 2/1/2 documents.
*/
const conn = MongoRunner.runMongod({
setParameter: {
internalEnableJoinOptimization: true,
featureFlagPathArrayness: true,
featureFlagCostBasedRanker: true,
internalEnableJoinPlanCache: false,
internalJoinMethod: "NLJ",
},
});
const db = conn.getDB("test");
try {
assert.commandWorked(db.a.insertMany([{_id: 1, x: 1}, {_id: 2, x: 2}]));
assert.commandWorked(db.b.insertMany([{_id: 1, y: 1, w: 5}, {_id: 2, y: 2, w: 6}]));
assert.commandWorked(db.c.insertMany([{_id: 1, z: 5}, {_id: 2, z: 1}]));
assert.commandWorked(db.a.createIndex({x: 1}));
assert.commandWorked(db.b.createIndex({y: 1}));
assert.commandWorked(db.b.createIndex({w: 1}));
assert.commandWorked(db.c.createIndex({z: 1}));
const pipeline = [
{$lookup: {from: "b", localField: "x", foreignField: "y", pipeline: [{$project: {y: "$w", w: "$y"}}], as: "b"}},
{$unwind: "$b"},
{$lookup: {from: "c", localField: "b.y", foreignField: "z", as: "c"}},
{$unwind: "$c"},
];
assert.commandWorked(db.adminCommand({setParameter: 1, internalEnableJoinOptimization: false}));
const expected = db.a.aggregate(pipeline).toArray();
assert.eq(expected.length, 1, tojson(expected));
assert.commandWorked(db.adminCommand({setParameter: 1, internalEnableJoinOptimization: true}));
const res = db.runCommand({aggregate: "a", pipeline: pipeline, cursor: {}});
assert.commandWorked(res);
assert.sameMembers(res.cursor.firstBatch, expected);
} finally {
MongoRunner.stopMongod(conn);
}