/**
* JOO wrong results (minor, deprecated BSON undefined): an $expr equi-join treats a BSON
* "undefined" value as different from a missing field, while aggregation $eq (classic $lookup and
* SBE $lookup without join optimization) treats them as equal.
*
* The hash join buckets fillEmpty(path, null): undefined stays undefined and never meets null, and
* the index probe seeks undefined while the index stores missing as null.
*/
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: undefined}, {_id: 3}]));
assert.commandWorked(db.b.insertMany([{_id: 1, y: undefined}, {_id: 3}]));
assert.commandWorked(db.a.createIndex({x: 1}));
assert.commandWorked(db.b.createIndex({y: 1}));
const pipeline = [
{$lookup: {from: "b", let: {v: "$x"}, pipeline: [{$match: {$expr: {$eq: ["$y", "$$v"]}}}], as: "b"}},
{$unwind: "$b"},
{$project: {_id: 1, bid: "$b._id"}},
];
function run(joinOpt) {
assert.commandWorked(db.adminCommand({setParameter: 1, internalEnableJoinOptimization: joinOpt}));
return db.a.aggregate(pipeline).toArray();
}
const expected = run(false);
assert.eq(expected.length, 4, tojson(expected));
assert.sameMembers(run(true), expected, "join optimization returned different results");
} finally {
MongoRunner.stopMongod(conn);
}