/**
* Repro for a join-optimization tassert on a post-$lookup $match.
*
* The $lookup join keys are scalar, but the later $match compares dotted paths that traverse
* arrays in the joined documents. Join optimization adds that $match equality to the join graph
* without rechecking path arrayness, so CE treats the paths as scalar.
*
* @tags: [
* requires_fcv_90,
* requires_sbe,
* ]
*/
TestData.cleanUpCoreDumpsFromExpectedCrash = true;
const conn = MongoRunner.runMongod({
setParameter: {
featureFlagPathArrayness: true,
internalEnableJoinOptimization: true,
internalEnablePathArrayness: true,
internalJoinReorderMode: "bottomUp",
internalJoinMethod: "HJ",
},
});
assert(conn);
const testDB = conn.getDB(jsTestName());
const base = testDB.base;
const left = testDB.left;
const right = testDB.right;
assert.commandWorked(base.insertOne({_id: 0, lk: 1, rk: 1}));
assert.commandWorked(left.insertOne({_id: "left", lk: 1, arr: [{v: 1}]}));
assert.commandWorked(right.insertOne({_id: "right", rk: 1, arr: [{v: 1}]}));
assert.commandWorked(base.createIndex({lk: 1, rk: 1}));
assert.commandWorked(left.createIndex({lk: 1}));
assert.commandWorked(right.createIndex({rk: 1}));
const pipeline = [
{$lookup: {from: left.getName(), localField: "lk", foreignField: "lk", as: "left"}},
{$unwind: "$left"},
{$lookup: {from: right.getName(), localField: "rk", foreignField: "rk", as: "right"}},
{$unwind: "$right"},
{$match: {$expr: {$eq: ["$left.arr.v", "$right.arr.v"]}}},
{$project: {_id: 0, "left._id": 1, "right._id": 1}},
];
assert.commandWorked(testDB.adminCommand({setParameter: 1, internalEnableJoinOptimization: false}));
const naive = base.aggregate(pipeline).toArray();
assert.eq(
[{left: {_id: "left"}, right: {_id: "right"}}],
naive,
"Naive $lookup should match the two dotted paths through arrays",
);
assert.commandWorked(testDB.adminCommand({setParameter: 1, internalEnableJoinOptimization: true}));
const cmdRes = testDB.runCommand({aggregate: base.getName(), pipeline, cursor: {}});
assert.commandWorked(cmdRes);
MongoRunner.stopMongod(conn, null, {allowedExitCode: MongoRunner.EXIT_ABORT});