/**
* JOO wrong results: a localField/foreignField (loose equality) join drops rows whose join key is
* NaN when the join is executed as a nested loop join or an indexed nested loop join.
*
* MQL equality treats NaN as equal to NaN (classic $lookup, SBE $lookup, and the JOO hash join all
* return the row). The NLJ and INLJ stage builders lower the loose predicate as
* fillEmpty(l, null) == fillEmpty(r, null), i.e. the SBE "eq" instruction, which uses IEEE
* semantics and evaluates NaN == NaN to false. The INLJ index probe does find the NaN keys; the
* residual equality filter then rejects them.
*
* The join method is forced here for determinism; with a larger foreign collection the optimizer
* picks INLJ on its own and the same rows go missing.
*/
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: NaN}, {_id: 2, x: 1}]));
assert.commandWorked(db.b.insertMany([{_id: 1, y: NaN}, {_id: 2, y: 1}]));
assert.commandWorked(db.a.createIndex({x: 1}));
assert.commandWorked(db.b.createIndex({y: 1}));
const pipeline = [
{$lookup: {from: "b", localField: "x", foreignField: "y", as: "b"}},
{$unwind: "$b"},
{$project: {_id: 1, bid: "$b._id"}},
];
assert.commandWorked(db.adminCommand({setParameter: 1, internalEnableJoinOptimization: false}));
const expected = db.a.aggregate(pipeline).toArray();
assert.sameMembers(expected, [{_id: 1, bid: 1}, {_id: 2, bid: 2}]);
assert.commandWorked(db.adminCommand({setParameter: 1, internalEnableJoinOptimization: true}));
for (const method of ["NLJ", "INLJ"]) {
assert.commandWorked(db.adminCommand({setParameter: 1, internalJoinMethod: method}));
assert.sameMembers(db.a.aggregate(pipeline).toArray(), expected, method);
}
} finally {
MongoRunner.stopMongod(conn);
}