summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Zolnierz <nicholas.zolnierz@mongodb.com>2022-08-30 19:43:42 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-08-30 20:23:15 +0000
commit5521f98c392f8af2fbb678f3cbf8089b641d9dba (patch)
tree1c3f2b201281e6ef071fb6697bafe9003910c581
parentdbe3923c3771d5f4670416c5c783c68eb03208f3 (diff)
downloadmongo-5521f98c392f8af2fbb678f3cbf8089b641d9dba.tar.gz
SERVER-68691 Report variable references for $graphLookup 'restrictSearchWithMatch' filter
-rw-r--r--jstests/aggregation/sources/graphLookup/filter.js65
-rw-r--r--src/mongo/db/pipeline/document_source_graph_lookup.h4
2 files changed, 49 insertions, 20 deletions
diff --git a/jstests/aggregation/sources/graphLookup/filter.js b/jstests/aggregation/sources/graphLookup/filter.js
index c43d849a3a5..98433c195ba 100644
--- a/jstests/aggregation/sources/graphLookup/filter.js
+++ b/jstests/aggregation/sources/graphLookup/filter.js
@@ -6,8 +6,8 @@
load("jstests/libs/fixture_helpers.js"); // For isSharded.
-var local = db.local;
-var foreign = db.foreign;
+let local = db.local;
+let foreign = db.foreign;
local.drop();
foreign.drop();
@@ -21,15 +21,15 @@ if (FixtureHelpers.isSharded(foreign) && !isShardedLookupEnabled) {
return;
}
-var bulk = foreign.initializeUnorderedBulkOp();
-for (var i = 0; i < 100; i++) {
+let bulk = foreign.initializeUnorderedBulkOp();
+for (let i = 0; i < 100; i++) {
bulk.insert({_id: i, neighbors: [i - 1, i + 1]});
}
assert.commandWorked(bulk.execute());
-assert.commandWorked(local.insert({starting: 0}));
+assert.commandWorked(local.insert([{starting: 0, foo: 1}, {starting: 1, foo: 2}]));
// Assert that the graphLookup only retrieves ten documents, with _id from 0 to 9.
-var res = local
+let res = local
.aggregate({
$graphLookup: {
from: "foreign",
@@ -69,7 +69,7 @@ assert.commandWorked(foreign.insert({from: 2, to: 3, shouldBeIncluded: true}));
// Assert that the $graphLookup stops exploring when it finds a document that doesn't match the
// filter.
res = local
- .aggregate({
+ .aggregate([{
$graphLookup: {
from: "foreign",
startWith: "$starting",
@@ -78,14 +78,14 @@ res = local
as: "results",
restrictSearchWithMatch: {shouldBeIncluded: true}
}
- })
- .toArray()[0];
+ }, {$match: {starting: 0}}])
+ .toArray();
-assert.eq(res.results.length, 1);
+assert.eq(res[0].results.length, 1, tojson(res));
// $expr is allowed inside the 'restrictSearchWithMatch' match expression.
res = local
- .aggregate({
+ .aggregate([{
$graphLookup: {
from: "foreign",
startWith: "$starting",
@@ -94,14 +94,14 @@ res = local
as: "results",
restrictSearchWithMatch: {$expr: {$eq: ["$shouldBeIncluded", true]}}
}
- })
- .toArray()[0];
+ }, {$match: {starting: 0}}])
+ .toArray();
-assert.eq(res.results.length, 1);
+assert.eq(res[0].results.length, 1, tojson(res));
// $expr within `restrictSearchWithMatch` has access to variables declared at a higher level.
res = local
- .aggregate([{
+ .aggregate([{$sort: {starting: 1}}, {
$lookup: {
from: "local",
let : {foo: true},
@@ -115,11 +115,36 @@ res = local
restrictSearchWithMatch:
{$expr: {$eq: ["$shouldBeIncluded", "$$foo"]}}
}
- }],
+ }, {$sort: {starting: 1}}],
as: "array"
}
- }])
- .toArray()[0];
-
-assert.eq(res.array[0].results.length, 1);
+ }, {$match: {starting: 0}}])
+ .toArray();
+
+assert.eq(res[0].array[0].results.length, 1, tojson(res));
+
+// $graphLookup which references a let variable defined by $lookup should be treated as correlated.
+res = local.aggregate([{
+ $lookup: {
+ from: "local",
+ let : {foo: "$foo"},
+ pipeline: [{
+ $graphLookup: {
+ from: "foreign",
+ startWith: "$starting",
+ connectFromField: "to",
+ connectToField: "from",
+ as: "results",
+ restrictSearchWithMatch:
+ {$expr: {$eq: ["$from", "$$foo"]}}
+ }
+ }],
+ as: "array"
+ }
+}, {$sort: {starting: 1}}]).toArray();
+assert.eq(2, res.length);
+assert.eq(1, res[1].starting, tojson(res));
+assert.eq(2, res[1].array.length, tojson(res));
+assert.eq(0, res[1].array[0].results.length, tojson(res));
+assert.eq(0, res[1].array[1].results.length, tojson(res));
})();
diff --git a/src/mongo/db/pipeline/document_source_graph_lookup.h b/src/mongo/db/pipeline/document_source_graph_lookup.h
index bc0146f1829..a4ad79c8111 100644
--- a/src/mongo/db/pipeline/document_source_graph_lookup.h
+++ b/src/mongo/db/pipeline/document_source_graph_lookup.h
@@ -132,6 +132,10 @@ public:
DepsTracker::State getDependencies(DepsTracker* deps) const final {
_startWith->addDependencies(deps);
+ if (_additionalFilter) {
+ uassertStatusOK(MatchExpressionParser::parse(*_additionalFilter, _fromExpCtx))
+ ->addDependencies(deps);
+ }
return DepsTracker::State::SEE_NEXT;
};