summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/lookup/lookup_random.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/aggregation/sources/lookup/lookup_random.js')
-rw-r--r--jstests/aggregation/sources/lookup/lookup_random.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/jstests/aggregation/sources/lookup/lookup_random.js b/jstests/aggregation/sources/lookup/lookup_random.js
new file mode 100644
index 00000000000..3558e1d6061
--- /dev/null
+++ b/jstests/aggregation/sources/lookup/lookup_random.js
@@ -0,0 +1,41 @@
+// Operators that use a random generator ($sample and $rand) are not allowed to be cached
+// as part of a non-correlated prefix.
+// @tags: [assumes_unsharded_collection]
+(function() {
+"use strict";
+
+const coll = db.getCollection('lookup_random');
+coll.drop();
+assert.commandWorked(coll.insert(Array.from({length: 200}, (_, i) => ({_id: i}))));
+
+// $sample in the inner pipeline should be rerun per outer document.
+let result = coll.aggregate([
+ {$lookup: {
+ from: coll.getName(),
+ as: 'docs',
+ pipeline: [
+ {$sample: {size: 1}},
+ ],
+ }},
+ {$unwind: "$docs"},
+ {$group: {_id: null, sampled: {$addToSet: "$docs._id"}}},
+]).toArray();
+assert.eq(result.length, 1, result);
+assert.gt(result[0].sampled.length, 1, result);
+
+// $rand in the inner pipeline should be rerun per outer document.
+result = coll.aggregate([
+ {$lookup: {
+ from: coll.getName(),
+ as: 'docs',
+ pipeline: [
+ {$limit: 1},
+ {$set: {r: {$rand: {}}}},
+ ],
+ }},
+ {$unwind: "$docs"},
+ {$group: {_id: null, randomValues: {$addToSet: "$docs.r"}}},
+]).toArray();
+assert.eq(result.length, 1, result);
+assert.gt(result[0].randomValues.length, 1, result);
+})();