/**
* JOO tassert 5182300 / 4822878 "Unsupported (match) expression in SBE stage builder": a $match
* that classic executes fine but SBE cannot lower, placed before the first $lookup or inside a
* $lookup sub-pipeline, is pushed into a join node's CanonicalQuery and lowered to SBE anyway.
*
* createCQForJoinPipeline() marks every join-node CanonicalQuery SBE-compatible unconditionally;
* the eligibility checks only reject $text. Without join optimization these pipelines run in the
* classic engine and succeed.
*/
const conn = MongoRunner.runMongod({
setParameter: {
internalEnableJoinOptimization: true,
featureFlagPathArrayness: true,
featureFlagCostBasedRanker: true,
},
});
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}, {_id: 2, y: 2}]));
assert.commandWorked(db.a.createIndex({x: 1}));
assert.commandWorked(db.b.createIndex({y: 1}));
const lookup = {$lookup: {from: "b", localField: "x", foreignField: "y", as: "b"}};
const pipelines = {
getField: [{$match: {$expr: {$eq: [{$getField: {field: "x", input: "$$ROOT"}}, 1]}}}, lookup, {$unwind: "$b"}],
jsonSchema: [{$match: {$jsonSchema: {properties: {x: {bsonType: "int"}}}}}, lookup, {$unwind: "$b"}],
subPipelineFunction: [
{
$lookup: {
from: "b",
localField: "x",
foreignField: "y",
pipeline: [{$match: {$expr: {$function: {body: "function(y) { return y == 1; }", args: ["$y"], lang: "js"}}}}],
as: "b",
},
},
{$unwind: "$b"},
],
};
for (const [name, pipeline] of Object.entries(pipelines)) {
assert.commandWorked(db.adminCommand({setParameter: 1, internalEnableJoinOptimization: false}));
const expected = db.a.aggregate(pipeline).toArray();
assert.commandWorked(db.adminCommand({setParameter: 1, internalEnableJoinOptimization: true}));
const res = db.runCommand({aggregate: "a", pipeline: pipeline, cursor: {}});
assert.commandWorked(res, name);
assert.sameMembers(res.cursor.firstBatch, expected, name);
}
} finally {
MongoRunner.stopMongod(conn);
}