/**
* JOO wrong results: the $match absorbed into a $lookup+$unwind (the "additional filter") is
* applied *below* the $lookup's own sub-pipeline $project instead of above it.
*
* extractPredicatesFromLookup() parses getAbsorbedFilter() and prepends it to the foreign node's
* CanonicalQuery pipeline via addInitialSource(), ahead of any $project/$addFields the user wrote
* in the sub-pipeline. Without join optimization the absorbed filter runs after the sub-pipeline,
* so a filter on a field computed, renamed or removed by that $project behaves differently.
*/
const conn = MongoRunner.runMongod({
setParameter: {
internalEnableJoinOptimization: true,
featureFlagPathArrayness: true,
featureFlagCostBasedRanker: true,
internalEnableJoinPlanCache: false,
},
});
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.a.createIndex({x: 1}));
assert.commandWorked(db.b.createIndex({y: 1}));
function run(pipeline, joinOpt) {
assert.commandWorked(db.adminCommand({setParameter: 1, internalEnableJoinOptimization: joinOpt}));
return db.a.aggregate(pipeline).toArray();
}
const renamePipeline = [
{$lookup: {from: "b", localField: "x", foreignField: "y", pipeline: [{$project: {y: 1, ww: "$w"}}], as: "b"}},
{$unwind: "$b"},
{$match: {"b.ww": 5}},
];
let expected = run(renamePipeline, false);
assert.eq(expected.length, 1, tojson(expected));
assert.sameMembers(run(renamePipeline, true), expected, "renamed field");
const exclusionPipeline = [
{$lookup: {from: "b", localField: "x", foreignField: "y", pipeline: [{$project: {w: 0}}], as: "b"}},
{$unwind: "$b"},
{$match: {"b.w": {$exists: false}}},
];
expected = run(exclusionPipeline, false);
assert.eq(expected.length, 2, tojson(expected));
assert.sameMembers(run(exclusionPipeline, true), expected, "excluded field");
} finally {
MongoRunner.stopMongod(conn);
}