summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/aggregation/sources')
-rw-r--r--jstests/aggregation/sources/documents/basics.js (renamed from jstests/aggregation/sources/documents.js)0
-rw-r--r--jstests/aggregation/sources/documents/subpipeline_validation.js215
-rw-r--r--jstests/aggregation/sources/lookup/lookup_absorb_match.js104
-rw-r--r--jstests/aggregation/sources/lookup/lookup_collation.js45
-rw-r--r--jstests/aggregation/sources/lookup/lookup_foreign_collation.js121
-rw-r--r--jstests/aggregation/sources/redact/root_redact.js28
-rw-r--r--jstests/aggregation/sources/setWindowFields/comprehensive_parse.js51
-rw-r--r--jstests/aggregation/sources/setWindowFields/time.js92
-rw-r--r--jstests/aggregation/sources/unionWith/unionWith_explain.js30
9 files changed, 508 insertions, 178 deletions
diff --git a/jstests/aggregation/sources/documents.js b/jstests/aggregation/sources/documents/basics.js
index ed90e9ac7aa..ed90e9ac7aa 100644
--- a/jstests/aggregation/sources/documents.js
+++ b/jstests/aggregation/sources/documents/basics.js
diff --git a/jstests/aggregation/sources/documents/subpipeline_validation.js b/jstests/aggregation/sources/documents/subpipeline_validation.js
new file mode 100644
index 00000000000..7d4ae87584f
--- /dev/null
+++ b/jstests/aggregation/sources/documents/subpipeline_validation.js
@@ -0,0 +1,215 @@
+// Tests the validation logic for combinations of "collectionless" stages like $documents with or
+// around sub-pipelines. For the cases that should be legal, we mostly just care the the command
+// succeeds. However, we will use 'resultsEq' to test correct semantics while we are here, gaining
+// more coverage.
+// TODO SERVER-94226 consider extending this test to cases like $currentOp and $queryStats as well.
+// This test uses stages like $documents which are not permitted inside a $facet stage.
+// @tags: [do_not_wrap_aggregations_in_facets]
+(function() {
+"use strict";
+load("jstests/aggregation/extras/utils.js"); // For 'resultsEq.'
+load("jstests/libs/fixture_helpers.js"); // For 'isMongos.'
+
+const coll = db[jsTestName()];
+coll.drop();
+
+const targetCollForMerge = db["target_coll"];
+targetCollForMerge.drop();
+assert.commandWorked(coll.insert({_id: 0, arr: [{}, {}]}));
+
+{
+ // Tests for an aggregation over a collection (i.e. {aggregate: "collName"} commands) with a
+ // $documents stage used in a sub-pipeline. Each of these cases should be legal, which is most
+ // of the value of the assertion. We will use 'resultsEq' to test correct semantics while we are
+ // here.
+
+ // $lookup.
+ assert(resultsEq(coll.aggregate([
+ {$lookup: {
+ let: {documents: "$arr"},
+ pipeline: [
+ {$documents: "$$documents"},
+ ],
+ as: "duplicated"
+ }},
+ ]).toArray(), [{_id: 0, arr: [{}, {}], duplicated: [{}, {}]}]));
+
+ // $unionWith.
+ assert(resultsEq(coll.aggregate([
+ {
+ $unionWith: {
+ pipeline: [
+ {$documents: [{_id: "gen"}]},
+ ],
+ }
+ },
+ ])
+ .toArray(),
+ [{_id: 0, arr: [{}, {}]}, {_id: "gen"}]));
+
+ // Both, and more nesting.
+ assert(resultsEq(coll.aggregate([{
+ $unionWith: {
+ coll: coll.getName(),
+ pipeline: [{
+ $lookup: {
+ pipeline: [
+ {$documents: []},
+ {$unionWith: {coll: coll.getName(), pipeline: []}}
+ ],
+ as: "nest"
+ }
+ }]
+ }
+ }])
+ .toArray(),
+ [
+ {_id: 0, arr: [{}, {}]},
+ {_id: 0, arr: [{}, {}], nest: [{_id: 0, arr: [{}, {}]}]}
+ ]));
+}
+
+{
+ // Tests for a db-level aggregate (i.e. {aggregate: 1} commands) with sub-pipelines on regular
+ // collections.
+
+ // $facet
+ assert(resultsEq(db.aggregate([
+ {
+ $documents: [
+ {x: 1, y: 1, val: 1},
+ {x: 2, y: 2, val: 1},
+ {x: 3, y: 1, val: 2},
+ {x: 2, y: 2, val: 1}
+ ]
+ },
+ {
+ $facet: {
+ sumByX: [{$group: {_id: "$x", sum: {$sum: "$val"}}}],
+ sumByY: [{$group: {_id: "$y", sum: {$sum: "$val"}}}]
+ }
+ }
+ ]).toArray(),
+ [{
+ sumByX: [{_id: 1, sum: 1}, {_id: 2, sum: 2}, {_id: 3, sum: 2}],
+ sumByY: [{_id: 1, sum: 3}, {_id: 2, sum: 2}]
+ }]));
+
+ if (!FixtureHelpers.isMongos(db)) {
+ // This doesn't work on mongos in v7.0 and earlier - waiting for SERVER-65534.
+
+ // $lookup.
+ assert(resultsEq(db.aggregate([
+ {$documents: [{x: 1, arr: [{x: 2}]}, {y: 1, arr: []}]},
+ {$lookup: {
+ let: {documents: "$arr"},
+ pipeline: [
+ {$documents: "$$documents"},
+ ],
+ as: "duplicated"
+ }},
+ ]).toArray(),
+ [
+ {x: 1, arr: [{x: 2}], duplicated: [{x: 2}]},
+ {y: 1, arr: [], duplicated: []}
+ ]));
+
+ // $merge.
+ assert.doesNotThrow(() => db.aggregate([
+ {
+ $documents: [
+ {_id: 2, x: "foo"},
+ {_id: 4, x: "bar"},
+ ]
+ },
+ {
+ $merge: {
+ into: targetCollForMerge.getName(),
+ on: "_id",
+ whenMatched: [{$set: {x: {$setUnion: ["$x", "$$new.x"]}}}]
+ }
+ }
+ ]));
+ assert(resultsEq(targetCollForMerge.find({}, {_id: 1}).toArray(), [{_id: 2}, {_id: 4}]));
+
+ // $unionWith
+ assert(resultsEq(db.aggregate([
+ {$documents: [{_id: 2}, {_id: 4}]},
+ {$unionWith: {coll: coll.getName(), pipeline: []}}
+ ]).toArray(),
+ [{_id: 2}, {_id: 4}, {_id: 0, arr: [{}, {}]}]));
+
+ // All of the above, plus nesting.
+ const results =
+ db.aggregate([
+ {$documents: [{_id: "first"}]},
+ {
+ $unionWith: {
+ pipeline: [
+ {$documents: [{_id: "uw"}]},
+ {$unionWith: {pipeline: [{$documents: [{_id: "uw_2"}]}]}},
+ {
+ $facet: {
+ allTogether:
+ [{$group: {_id: null, all: {$addToSet: "$_id"}}}],
+ countEach: [{$group: {_id: "$_id", count: {$sum: 1}}}],
+ }
+ },
+ {
+ $lookup:
+ {pipeline: [{$documents: [{x: "lu1"}, {x: "lu2"}]}], as: "xs"}
+ },
+ {$set: {xs: {$map: {input: "$xs", in : "$$this.x"}}}}
+ ]
+ },
+ },
+ ]).toArray();
+ assert(resultsEq(results,
+ [
+ {_id: "first"},
+ {
+ allTogether: [{_id: null, all: ["uw", "uw_2"]}],
+ countEach: [{_id: "uw", count: 1}, {_id: "uw_2", count: 1}],
+ xs: ["lu1", "lu2"]
+ }
+ ]),
+ results);
+ }
+}
+
+// Test for invalid combinations.
+
+// To use $documents inside a $lookup, there must not be a "from" argument.
+// As of SERVER-94144, this does not throw on 7.0 and older branches.
+assert.doesNotThrow(
+ () => coll.aggregate([{$lookup: {from: "foo", pipeline: [{$documents: []}], as: "lustuff"}}]));
+assert.doesNotThrow(
+ () => coll.aggregate([
+ {$lookup: {
+ from: "foo",
+ let: {docs: "$arr"},
+ pipeline: [
+ {$documents: "$$docs"},
+ {$lookup: {
+ from: "foo",
+ let: {x: "$x", y: "$y"},
+ pipeline: [
+ {$match: {$expr: {$and: [
+ {$eq: ["$x", "$$x"]},
+ {$eq: ["$y", "$$y"]}
+ ]}}}
+ ],
+ as: "doesnt_matter"
+ }}
+ ],
+ as: "lustuff"
+ }}]));
+
+// To use $documents inside a $unionWith, there must not be a "coll" argument.
+// As of SERVER-94144, this does not throw on 7.0 and older branches.
+assert.doesNotThrow(
+ () => coll.aggregate([{$unionWith: {coll: "foo", pipeline: [{$documents: []}]}}]));
+
+// Cannot use $documents inside of $facet.
+assert.throwsWithCode(() => coll.aggregate([{$facet: {test: [{$documents: []}]}}]), 40600);
+})();
diff --git a/jstests/aggregation/sources/lookup/lookup_absorb_match.js b/jstests/aggregation/sources/lookup/lookup_absorb_match.js
index be589615f5c..4220cbcb71a 100644
--- a/jstests/aggregation/sources/lookup/lookup_absorb_match.js
+++ b/jstests/aggregation/sources/lookup/lookup_absorb_match.js
@@ -136,7 +136,7 @@ expected =
[{_id: "dog", locationId: "doghouse", location: {_id: "doghouse", coordinates: [25.0, 60.0]}}];
assert.eq(result, expected);
-// Test that a $match with $jsonSchema works as expected although ineligable for absorbtion by a
+// Test that a $match with $jsonSchema works as expected although ineligible for absorbtion by a
// $lookup.
result = testDB.animals
.aggregate([
@@ -167,7 +167,7 @@ expected =
[{_id: "bull", locationId: "bullpen", location: {_id: "bullpen", coordinates: [-25.0, -60.0]}}];
assert.eq(result, expected);
-// Test that a more complex $match with $jsonSchema works as expected although ineligable for
+// Test that a more complex $match with $jsonSchema works as expected although ineligible for
// absorbtion by a $lookup.
result = testDB.animals
.aggregate([
@@ -194,8 +194,100 @@ expected =
[{_id: "bull", locationId: "bullpen", location: {_id: "bullpen", coordinates: [-25.0, -60.0]}}];
assert.eq(result, expected);
-// Test that a $match with $alwaysTrue works as expected although ineligable for absorbtion by a
-// $lookup.
+// Test that $match with a $jsonSchema property that will internally translate to a match
+// expression node that has a path that is prefixed by the 'as' field in the lookup and that has
+// children that can operate on that path (in this case, $_internalSchemaAllElemMatchFromIndex)
+// works as expected although ineligible for absorbtion by a $lookup. Note that the jsonSchema below
+// ensures that all elements of 'location.coordinates' are above 0 since the 'items' field is an
+// object.
+result = testDB.animals
+ .aggregate([
+ {
+ $lookup: {
+ from: "locations",
+ localField: "locationId",
+ foreignField: "_id",
+ as: "location"
+ }
+ },
+ {$unwind: "$location"},
+ {
+ $match: {
+ $jsonSchema: {
+ properties: {"location.coordinates": {items: {minimum: 0}}}
+ }
+ }
+ },
+ {$project: {"location.extra": false, "colors": false}}
+ ])
+ .toArray();
+
+expected =
+ [{_id: "dog", locationId: "doghouse", location: {_id: "doghouse", coordinates: [25.0, 60.0]}}];
+assert.eq(result, expected);
+
+// Test that $match with a $jsonSchema property that will internally translate to a match
+// expression node that has a path that is prefixed by the 'as' field in the lookup and that has
+// children that can operate on that path (in this case, $_internalSchemaMatchArrayIndex) works as
+// expected although ineligible for absorbtion by a $lookup. Note that the jsonSchema below ensures
+// that the first element of 'location.coordinates' is above 0 since the 'items' field is an array.
+result = testDB.animals
+ .aggregate([
+ {
+ $lookup: {
+ from: "locations",
+ localField: "locationId",
+ foreignField: "_id",
+ as: "location"
+ }
+ },
+ {$unwind: "$location"},
+ {
+ $match: {
+ $jsonSchema: {
+ properties: {"location.coordinates": {items: [{minimum: 0}]}}
+ }
+ }
+ },
+ {$project: {"location.extra": false, "colors": false}}
+ ])
+ .toArray();
+
+expected =
+ [{_id: "dog", locationId: "doghouse", location: {_id: "doghouse", coordinates: [25.0, 60.0]}}];
+assert.eq(result, expected);
+
+// Test that $match with a $jsonSchema property that will internally translate to a match
+// expression node that has a path that is prefixed by the 'as' field in the lookup and that has
+// children that can operate that path (in this case, $_internalSchemaObjectMatch) works as expected
+// although ineligible for absorbtion by a $lookup.
+result = testDB.animals
+ .aggregate([
+ {
+ $lookup: {
+ from: "locations",
+ localField: "locationId",
+ foreignField: "_id",
+ as: "location"
+ }
+ },
+ {$unwind: "$location"},
+ {
+ $match: {
+ $jsonSchema: {
+ properties: {"location.extra": {type: 'object', properties: {"breeds": {type: 'string'}}}}
+ }
+ }
+ },
+ {$project: {"location.extra": false, "colors": false}}
+ ])
+ .toArray();
+expected =
+ [{_id: "bull", locationId: "bullpen", location: {_id: "bullpen", coordinates: [-25.0, -60.0]}}];
+assert.eq(result, expected);
+
+// Test that a $match with $alwaysTrue works as expected although ineligible for absorbtion
+// by a $lookup. The $sort is to guarantee records are returned in the expected order.
result = testDB.animals
.aggregate([
{
@@ -220,7 +312,7 @@ expected = [
];
assert.eq(result, expected);
-// Test that a $match with $alwaysFalse works as expected although ineligable for absorbtion by a
+// Test that a $match with $alwaysFalse works as expected although ineligible for absorbtion by a
// $lookup.
result = testDB.animals
.aggregate([
@@ -242,7 +334,7 @@ result = testDB.animals
expected = [];
assert.eq(result, expected);
-// Test that a $match with $expr works as expected although ineligable for absorbtion by a $lookup.
+// Test that a $match with $expr works as expected although ineligible for absorbtion by a $lookup.
result = testDB.animals
.aggregate([
{
diff --git a/jstests/aggregation/sources/lookup/lookup_collation.js b/jstests/aggregation/sources/lookup/lookup_collation.js
index 50ec057ae79..36f18b7e44c 100644
--- a/jstests/aggregation/sources/lookup/lookup_collation.js
+++ b/jstests/aggregation/sources/lookup/lookup_collation.js
@@ -3,14 +3,11 @@
* when performing comparisons on a foreign collection with a different default collation. Exercises
* the fix for SERVER-43350.
*
- * Collation can be set at three different levels for $lookup stage
+ * Collation can be set at two different levels for $lookup stage
* 1. on the local collection (collation on the foreign collection is always ignored)
- * 2. on the $lookup stage via '_internalCollation' property
- * 3. on the aggregation command via 'collation' property in options
+ * 2. on the aggregation command via 'collation' property in options
*
- * The three settings have the following precedence:
- * 1. '_internalCollation' overrides all others
- * 2. 'collation' option overrides local collection's collation
+ * The 'collation' command option overrides local collection's collation.
*/
load("jstests/aggregation/extras/utils.js"); // For anyEq.
load("jstests/libs/analyze_plan.js"); // For getAggPlanStages, getWinningPlan.
@@ -137,29 +134,6 @@ let explain;
}
})();
-// Collation set on $lookup stage with '_internalCollation' should override collation of the local
-// collection and on the command.
-(function testStageCollationPrecedence() {
- for (let lookupInto of [lookupWithPipeline, lookupNoPipeline]) {
- let lookupStage = lookupInto(collAa);
- lookupStage.$lookup._internalCollation = caseInsensitive;
- results = collAa.aggregate([lookupStage], {collation: caseSensitive}).toArray();
- assertArrayEq({
- actual: results,
- expected: resultCaseInsensitive,
- extraErrorMsg: " Case-insensitive collation on stage, running: " + tojson(lookupInto)
- });
-
- lookupStage.$lookup._internalCollation = caseSensitive;
- results = collAA.aggregate([lookupStage], {collation: caseInsensitive}).toArray();
- assertArrayEq({
- actual: results,
- expected: resultCaseSensistive,
- extraErrorMsg: " Case-sensitive collation on stage, running: " + tojson(lookupInto)
- });
- }
-})();
-
// In presense of indexes lookup might choose a different strategy for the join, that relies on the
// index (INLJ). It should respect the effective collation of $lookup.
(function testCollationWithIndexes() {
@@ -213,19 +187,6 @@ let explain;
explain = collAa.explain().aggregate([lookupInto(collAa_indexed)],
{collation: {locale: "fr"}, allowDiskUse: false});
assertNestedLoopJoinStrategy(explain);
-
- // Stage-level collation overrides collection-level and command-level collations.
- let lookupStage = lookupInto(collAa_indexed);
- lookupStage.$lookup._internalCollation = caseInsensitive;
- results = collAa.aggregate([lookupStage], {collation: caseSensitive}).toArray();
- assertArrayEq({
- actual: results,
- expected: resultCaseInsensitive,
- extraErrorMsg: " Case-insensitive collation on stage, foreign is indexed, running: " +
- tojson(lookupInto)
- });
- explain = collAa.explain().aggregate([lookupStage], {collation: caseSensitive});
- assertIndexJoinStrategy(explain);
}
})();
})();
diff --git a/jstests/aggregation/sources/lookup/lookup_foreign_collation.js b/jstests/aggregation/sources/lookup/lookup_foreign_collation.js
index 6937fe42854..7d6e9a4578e 100644
--- a/jstests/aggregation/sources/lookup/lookup_foreign_collation.js
+++ b/jstests/aggregation/sources/lookup/lookup_foreign_collation.js
@@ -96,12 +96,10 @@ function setup() {
// localColl: Local Collection
// foreignColl: Foreign Collection
// commandCollation: Collation set on the aggregate command. Pass null for default collation.
- // lookupCollation: Collation specified in the $lookup stage. Pass null for default
- // collation. expectedResults: Results expected from the aggregate invocation
+ // expectedResults: Results expected from the aggregate invocation
//
- function assertExpectedResultSet(
- localColl, foreignColl, commandCollation, lookupCollation, expectedResults) {
- const lookupWithPipeline = {$lookup: {from: foreignColl.getName(),
+ function assertExpectedResultSet(localColl, foreignColl, commandCollation, expectedResults) {
+ const lookupWithPipeline = {$lookup: {from: foreignColl.getName(),
as: "foreignMatch",
let: {l_id: "$_id"},
pipeline: [{$match: {$expr: {$eq: ["$_id", "$$l_id"]}}}]}};
@@ -111,11 +109,6 @@ function setup() {
foreignField: "_id",
as: "foreignMatch"}};
- if (lookupCollation) {
- lookupWithPipeline.$lookup._internalCollation = lookupCollation;
- lookupWithLocalForeignField.$lookup._internalCollation = lookupCollation;
- }
-
const aggOptions = {};
if (commandCollation) {
aggOptions.collation = commandCollation;
@@ -129,112 +122,20 @@ function setup() {
}
// Baseline test, confirming simple binary comparison when no collation has been specified on
- // the command, $lookup stage or collections.
- assertExpectedResultSet(localColl, foreignColl, null, null, resultSetCaseSensitive);
+ // the command or the collection.
+ assertExpectedResultSet(localColl, foreignColl, null, resultSetCaseSensitive);
// When a collation has been specified on the $lookup stage, it will always be used to join
// local and foreign collections.
for (const local of [localColl, localCaseInsensitiveColl]) {
for (const foreign of [foreignColl, foreignCaseInsensitiveColl]) {
- for (const command of [null, simpleCollation, caseInsensitiveCollation]) {
- // Case insensitive collation specified in the $lookup stage results in a case
- // insensitive join.
- assertExpectedResultSet(
- local, foreign, command, caseInsensitiveCollation, resultSetCaseInsensitive);
-
- // Simple collation specified in the $lookup stage results in a case sensitive join.
- assertExpectedResultSet(
- local, foreign, command, simpleCollation, resultSetCaseSensitive);
- }
+ // Case insensitive collation results in a case insensitive join.
+ assertExpectedResultSet(
+ local, foreign, caseInsensitiveCollation, resultSetCaseInsensitive);
+
+ // Simple collation results in a case sensitive join.
+ assertExpectedResultSet(local, foreign, simpleCollation, resultSetCaseSensitive);
}
}
})();
-
-(function testNestedLookupStagesWithDifferentCollations() {
- setup();
-
- const lookupWithPipeline = {$lookup: {from: foreignColl.getName(),
- as: "foreignMatch",
- let: {l_id: "$_id"},
- pipeline: [{$match: {$expr: {$eq: ["$_id", "$$l_id"]}}},
- {$lookup: {from: localColl.getName(),
- as: "foreignMatch2",
- let: {l_id: "$_id"},
- pipeline: [{$match: {$expr: {$eq: ["$_id", "$$l_id"]}}}],
- _internalCollation: simpleCollation}}],
- _internalCollation: caseInsensitiveCollation}};
-
- const resultSet = [
- {_id: "a", foreignMatch: [{_id: "a", "foreignMatch2": [{"_id": "a"}]}]},
- {_id: "b", foreignMatch: [{_id: "B", "foreignMatch2": []}]},
- {_id: "c", foreignMatch: [{_id: "c", "foreignMatch2": [{"_id": "c"}]}]},
- {_id: "d", foreignMatch: [{_id: "D", "foreignMatch2": []}]},
- {_id: "e", foreignMatch: [{_id: "e", "foreignMatch2": [{"_id": "e"}]}]}
- ];
-
- const results = localColl.aggregate([lookupWithPipeline]).toArray();
- assert(anyEq(results, resultSet), tojson(results));
-})();
-
-(function testMatchOnUnwoundAsFieldAbsorptionOptimization() {
- setup();
-
- // A $lookup stage with a collation that differs from the collection and command collation
- // will not absorb a $match on unwound results.
- let pipeline = [{$lookup: {from: foreignColl.getName(),
- as: "foreignMatch",
- let: {l_id: "$_id"},
- pipeline: [{$match: {$expr: {$eq: ["$_id", "$$l_id"]}}}],
- _internalCollation: caseInsensitiveCollation}},
- {$unwind: "$foreignMatch"},
- {$match: {"foreignMatch._id": "b"}}];
-
- let results = localColl.aggregate(pipeline).toArray();
- assert.eq(0, results.length);
-
- let explain = localColl.explain().aggregate(pipeline);
- let lastStage = explain.stages[explain.stages.length - 1];
- assert(lastStage.hasOwnProperty("$match"), tojson(explain));
- assert.eq({$match: {"foreignMatch._id": {$eq: "b"}}},
- lastStage,
- "The $match stage should not be optimized into the $lookup stage" + tojson(explain));
-
- // A $lookup stage with a collation that matches the command collation will absorb a $match
- // stage.
- pipeline = [{$lookup: {from: foreignColl.getName(),
- as: "foreignMatch",
- let: {l_id: "$_id"},
- pipeline: [{$match: {$expr: {$eq: ["$_id", "$$l_id"]}}}],
- _internalCollation: caseInsensitiveCollation}},
- {$unwind: "$foreignMatch"},
- {$match: {"foreignMatch._id": "b"}}];
-
- let expectedResults = [{"_id": "b", "foreignMatch": {"_id": "B"}}];
-
- results = localColl.aggregate(pipeline, {collation: caseInsensitiveCollation}).toArray();
- assert(anyEq(results, expectedResults), tojson(results));
-
- explain = localColl.explain().aggregate(pipeline, {collation: caseInsensitiveCollation});
- lastStage = explain.stages[explain.stages.length - 1];
- assert(lastStage.hasOwnProperty("$lookup"), tojson(explain));
-
- // A $lookup stage with a collation that matches the local collection collation will absorb
- // a $match stage.
- pipeline = [{$lookup: {from: foreignColl.getName(),
- as: "foreignMatch",
- let: {l_id: "$_id"},
- pipeline: [{$match: {$expr: {$eq: ["$_id", "$$l_id"]}}}],
- _internalCollation: caseInsensitiveCollation}},
- {$unwind: "$foreignMatch"},
- {$match: {"foreignMatch._id": "b"}}];
-
- expectedResults = [{"_id": "b", "foreignMatch": {"_id": "B"}}];
-
- results = localCaseInsensitiveColl.aggregate(pipeline).toArray();
- assert(anyEq(results, expectedResults), tojson(results));
-
- explain = localCaseInsensitiveColl.explain().aggregate(pipeline);
- lastStage = explain.stages[explain.stages.length - 1];
- assert(lastStage.hasOwnProperty("$lookup"), tojson(explain));
-})();
})();
diff --git a/jstests/aggregation/sources/redact/root_redact.js b/jstests/aggregation/sources/redact/root_redact.js
new file mode 100644
index 00000000000..a1851e54726
--- /dev/null
+++ b/jstests/aggregation/sources/redact/root_redact.js
@@ -0,0 +1,28 @@
+// Usage of $$ROOT variable can lead to Document cache reallocation and possible memory corruption.
+// Test is checking that this is not the case.
+(function() {
+"use strict";
+
+const bigStr = 'X'.repeat(1024);
+const doc = {
+ _id: 0,
+ d: true,
+ sub: {d: false, str: bigStr},
+ str: bigStr,
+};
+
+db.test.drop();
+assert.commandWorked(db.test.insertOne(doc));
+
+const pipeline = [{
+ $redact: {
+ $cond: {
+ if: "$d",
+ then: "$$DESCEND",
+ else: {$cond: {if: {$objectToArray: "$$ROOT"}, then: "$$KEEP", else: "$$PRUNE"}}
+ }
+ }
+}];
+
+assert.eq([doc], db.test.aggregate(pipeline).toArray());
+})();
diff --git a/jstests/aggregation/sources/setWindowFields/comprehensive_parse.js b/jstests/aggregation/sources/setWindowFields/comprehensive_parse.js
index dbe509e2c59..4dd4cc90565 100644
--- a/jstests/aggregation/sources/setWindowFields/comprehensive_parse.js
+++ b/jstests/aggregation/sources/setWindowFields/comprehensive_parse.js
@@ -65,6 +65,7 @@ const windows = {
// The list of sort definitions to test.
const sortBys = {
none: null,
+ expr: {partitionSeq: {$meta: "randVal"}},
asc: {partitionSeq: 1},
desc: {partitionSeq: -1},
asc_date: {date: 1},
@@ -104,18 +105,11 @@ function constructQuery(wf, window, sortBy, partitionBy) {
}
// Given an element of each of the lists above, what is the expected
-// result. The output should be 'SKIP', 'OK' or the expected integer
+// result. The output should be 'OK' or the expected integer
// error code.
function expectedResult(wfType, windowType, sortType, partitionType) {
// Static errors all come first.
- // Skip range windows over dates or that are over descending windows.
- if (windowType.endsWith('range')) {
- if (sortType.endsWith('date') || sortType.startsWith('desc')) {
- return 'SKIP';
- }
- }
-
// Derivative and integral require an ascending sort
// and an explicit window.
if (wfType.startsWith('derivative')) {
@@ -129,6 +123,11 @@ function expectedResult(wfType, windowType, sortType, partitionType) {
return ErrorCodes.FailedToParse;
}
+ // '$derivative requires a non-expression sortBy'.
+ if (sortType == "expr") {
+ return ErrorCodes.FailedToParse;
+ }
+
} else if (wfType.startsWith('integral')) {
// Integral requires a sort.
if (sortType == 'none') {
@@ -140,6 +139,11 @@ function expectedResult(wfType, windowType, sortType, partitionType) {
return ErrorCodes.FailedToParse;
}
+ // '$integral requires a non-expression sortBy'.
+ if (sortType == "expr") {
+ return ErrorCodes.FailedToParse;
+ }
+
} else if (wfType.startsWith('expMovingAvg')) {
// $expMovingAvg doesn't accept a window.
if (windowType != 'none') {
@@ -179,9 +183,23 @@ function expectedResult(wfType, windowType, sortType, partitionType) {
}
// Range based windows require a sort over a single field.
- if (windowType.endsWith('range') && (sortType == 'none' || sortType == 'multi')) {
- // 'Range-based window require sortBy a single field'.
- return 5339902;
+ if (windowType.endsWith('range')) {
+ if (sortType == 'none' || sortType == 'multi') {
+ // 'Range-based window require sortBy a single field'.
+ return 5339902;
+ }
+ if (sortType == 'expr') {
+ // 'Range-based bounds require a non-expression sortBy'
+ return 8947400;
+ }
+ if (sortType.startsWith('desc')) {
+ // 'Range-based bounds require an ascending sortBy'.
+ return 8947401;
+ }
+ if (sortType.endsWith('date') && !partitionType.endsWith('array')) {
+ // 'For windows that involve date or time ranges, a unit must be provided.'
+ return 5429413;
+ }
}
if (partitionType === 'static_array') {
@@ -238,10 +256,6 @@ function* makeTests() {
expectedResult: expectedResult(wfType, windowType, sortType, partitionType)
};
- if (test.expectedResult == 'SKIP') {
- continue;
- }
-
yield test;
}
}
@@ -251,13 +265,16 @@ function* makeTests() {
// Run all the combinations generated in makeTests.
for (const test of makeTests()) {
+ const errorMsg = "Command was: " + tojson(test.query);
if (test.expectedResult == ErrorCodes.OK) {
assert.commandWorked(
- coll.runCommand({aggregate: coll.getName(), pipeline: [test.query], cursor: {}}));
+ coll.runCommand({aggregate: coll.getName(), pipeline: [test.query], cursor: {}}),
+ errorMsg);
} else {
assert.commandFailedWithCode(
coll.runCommand({aggregate: coll.getName(), pipeline: [test.query], cursor: {}}),
- test.expectedResult);
+ test.expectedResult,
+ errorMsg);
}
}
})();
diff --git a/jstests/aggregation/sources/setWindowFields/time.js b/jstests/aggregation/sources/setWindowFields/time.js
index e4f7d7cfa6c..7ef72c7243b 100644
--- a/jstests/aggregation/sources/setWindowFields/time.js
+++ b/jstests/aggregation/sources/setWindowFields/time.js
@@ -196,4 +196,96 @@ error = assert.throws(() => {
run([range('unbounded', 'unbounded')]);
});
assert.commandFailedWithCode(error, 5429513);
+
+// Test with small bound over large data values
+coll.drop();
+assert.commandWorked(coll.insert([
+ {_id: 0, time: new Date("2023-07-01T20:53:50.932Z"), "str": "ric"},
+ {_id: 1, time: new Date("2023-07-02T20:57:42.383Z"), "str": "alc"},
+ {_id: 2, time: new Date("2023-07-04T01:39:14.265Z"), "str": ""},
+ {_id: 3, time: new Date("2023-07-04T04:01:29.983Z"), "str": "ric"},
+ {_id: 4, time: new Date("2023-07-05T15:08:22.541Z"), "str": ""},
+ {_id: 5, time: new Date("2023-07-05T22:56:30.949Z"), "str": ""},
+]));
+
+const res = coll.aggregate([
+ {
+ $setWindowFields: {
+ partitionBy: "$str",
+ sortBy: {"any": -1},
+ output: {"num": {$sum: {$pow: [9, 20]}}}
+ }
+ },
+ {
+ $setWindowFields: {
+ sortBy: {"num": 1},
+ output: {"res": {$max: "$num", window: {range: [-4, "current"]}}}
+ }
+ },
+ {$project: {res: 1, _id: 0}},
+ ])
+ .toArray();
+
+assert.sameMembers(res, [
+ {"res": 12157665459056929000},
+ {"res": 24315330918113858000},
+ {"res": 24315330918113858000},
+ {"res": 36472996377170790000},
+ {"res": 36472996377170790000},
+ {"res": 36472996377170790000}
+]);
+
+// Test with large bound over large data values
+const res2 =
+ coll.aggregate([
+ {
+ $setWindowFields: {
+ partitionBy: "$str",
+ sortBy: {"any": -1},
+ output: {"num": {$sum: {$pow: [8, 20]}}}
+ }
+ },
+ {
+ $setWindowFields: {
+ sortBy: {"num": 1},
+ output:
+ {"res": {$max: "$num", window: {range: [-{$pow: [9, 20]}, "current"]}}}
+ }
+ },
+ {$project: {res: 1, _id: 0}},
+ ])
+ .toArray();
+
+assert.sameMembers(res2, [
+ {"res": 1152921504606847000},
+ {"res": 2305843009213694000},
+ {"res": 2305843009213694000},
+ {"res": 3458764513820541000},
+ {"res": 3458764513820541000},
+ {"res": 3458764513820541000}
+]);
+
+// Test with large bound over small data values
+const res3 =
+ coll.aggregate([
+ {
+ $setWindowFields: {
+ partitionBy: "$str",
+ sortBy: {"any": -1},
+ output: {"num": {$sum: {$pow: [2, 2]}}}
+ }
+ },
+ {
+ $setWindowFields: {
+ sortBy: {"num": 1},
+ output:
+ {"res": {$max: "$num", window: {range: [-{$pow: [9, 20]}, "current"]}}}
+ }
+ },
+ {$project: {res: 1, _id: 0}},
+ ])
+ .toArray();
+
+assert.sameMembers(res3,
+ [{"res": 4}, {"res": 8}, {"res": 8}, {"res": 12}, {"res": 12}, {"res": 12}]);
})();
diff --git a/jstests/aggregation/sources/unionWith/unionWith_explain.js b/jstests/aggregation/sources/unionWith/unionWith_explain.js
index 60d6a7ae4a4..a600b7fbe0f 100644
--- a/jstests/aggregation/sources/unionWith/unionWith_explain.js
+++ b/jstests/aggregation/sources/unionWith/unionWith_explain.js
@@ -105,8 +105,18 @@ function assertExplainEq(union, regular) {
executionStatsIngoredFields),
buildErrorString(unionStats, regularStats, "executionStages"));
} else if ("stages" in regular) {
- assert(arrayEqWithIgnoredFields(union, regular.stages, stagesIgnoredFields),
- buildErrorString(union, regular, "stages"));
+ // For explains run with the runCommand({explain: ...}) format.
+ if (regular.stages.length > 1 && "$cursor" in regular.stages[0] &&
+ "executionStats" in regular.stages[0]["$cursor"]) {
+ assert(
+ arrayEqWithIgnoredFields(union,
+ regular.stages,
+ [...stagesIgnoredFields, ...executionStatsIngoredFields]),
+ buildErrorString(union, regular, "stages with executionStats"));
+ } else {
+ assert(arrayEqWithIgnoredFields(union, regular.stages, stagesIgnoredFields),
+ buildErrorString(union, regular, "stages"));
+ }
} else if ("queryPlanner" in regular) {
assert.eq(union.length, 1, "Expected single union stage");
const unionCursor = union[0].$cursor;
@@ -123,7 +133,7 @@ function assertExplainEq(union, regular) {
function assertExplainMatch(unionExplain, regularExplain) {
const unionStage = getUnionWithStage(unionExplain);
- assert(unionStage);
+ assert(unionStage, unionExplain);
const unionSubExplain = unionStage.$unionWith.pipeline;
assertExplainEq(unionSubExplain, regularExplain);
}
@@ -133,6 +143,20 @@ function testPipeline(pipeline) {
{explain: true});
let queryResult = collB.aggregate(pipeline, {explain: true});
assertExplainMatch(unionResult, queryResult);
+
+ // Alternative explain invocation. This is a regression test for SERVER-89344.
+ if (!FixtureHelpers.isMongos(db)) {
+ unionResult = db.runCommand({
+ explain: {
+ "aggregate": collA.getName(),
+ "pipeline": [{$unionWith: {coll: collB.getName(), pipeline: pipeline}}],
+ "cursor": {}
+ }
+ });
+ queryResult = db.runCommand(
+ {explain: {"aggregate": collB.getName(), "pipeline": pipeline, "cursor": {}}});
+ assertExplainMatch(unionResult, queryResult);
+ }
}
testPipeline([{$addFields: {bump: true}}]);