summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--etc/backports_required_for_multiversion_tests.yml4
-rw-r--r--jstests/core/timeseries/timeseries_groupby_reorder.js17
-rw-r--r--src/mongo/db/pipeline/document_source_internal_unpack_bucket.cpp2
-rw-r--r--src/mongo/db/pipeline/document_source_internal_unpack_bucket_test/group_reorder_test.cpp19
4 files changed, 39 insertions, 3 deletions
diff --git a/etc/backports_required_for_multiversion_tests.yml b/etc/backports_required_for_multiversion_tests.yml
index 3410f4e985c..036c9755b43 100644
--- a/etc/backports_required_for_multiversion_tests.yml
+++ b/etc/backports_required_for_multiversion_tests.yml
@@ -282,6 +282,8 @@ last-continuous:
ticket: SERVER-72416
- test_file: src/mongo/db/modules/enterprise/jstests/fcbis/oplog_rollover.js
ticket: SERVER-72422
+ - test_file: jstests/core/timeseries/timeseries_groupby_reorder.js
+ ticket: SERVER-73822
suites: null
last-lts:
all:
@@ -639,4 +641,6 @@ last-lts:
ticket: SERVER-72416
- test_file: src/mongo/db/modules/enterprise/jstests/fcbis/oplog_rollover.js
ticket: SERVER-72422
+ - test_file: jstests/core/timeseries/timeseries_groupby_reorder.js
+ ticket: SERVER-73822
suites: null
diff --git a/jstests/core/timeseries/timeseries_groupby_reorder.js b/jstests/core/timeseries/timeseries_groupby_reorder.js
index 29d07f8b4fa..c16ee864226 100644
--- a/jstests/core/timeseries/timeseries_groupby_reorder.js
+++ b/jstests/core/timeseries/timeseries_groupby_reorder.js
@@ -37,7 +37,18 @@ if (!isMongos(db)) {
});
}
-const res = coll.aggregate([{$group: {_id: '$meta', accmin: {$min: '$b'}, accmax: {$max: '$c'}}}])
- .toArray();
-assert.docEq(res, [{"_id": null, "accmin": 1, "accmax": 3}]);
+let res = coll.aggregate([{$group: {_id: '$meta', accmin: {$min: '$b'}, accmax: {$max: '$c'}}}])
+ .toArray();
+assert.docEq([{"_id": null, "accmin": 1, "accmax": 3}], res);
+
+// Test SERVER-73822 fix: complex $min and $max (i.e. not just straight field refs) work correctly.
+res = coll.aggregate([{
+ $group: {
+ _id: '$meta',
+ accmin: {$min: {$add: ["$b", "$c"]}},
+ accmax: {$max: {$add: ["$b", "$c"]}}
+ }
+ }])
+ .toArray();
+assert.docEq([{"_id": null, "accmin": 2, "accmax": 6}], res);
})();
diff --git a/src/mongo/db/pipeline/document_source_internal_unpack_bucket.cpp b/src/mongo/db/pipeline/document_source_internal_unpack_bucket.cpp
index c0b12367968..8fd2afbd6e9 100644
--- a/src/mongo/db/pipeline/document_source_internal_unpack_bucket.cpp
+++ b/src/mongo/db/pipeline/document_source_internal_unpack_bucket.cpp
@@ -792,6 +792,8 @@ DocumentSourceInternalUnpackBucket::rewriteGroupByMinMax(Pipeline::SourceContain
AccumulationExpression accExpr = stmt.expr;
accExpr.argument = newExpr;
accumulationStatements.emplace_back(stmt.fieldName, std::move(accExpr));
+ } else {
+ return {};
}
}
diff --git a/src/mongo/db/pipeline/document_source_internal_unpack_bucket_test/group_reorder_test.cpp b/src/mongo/db/pipeline/document_source_internal_unpack_bucket_test/group_reorder_test.cpp
index 6100897fd79..749c8c33a02 100644
--- a/src/mongo/db/pipeline/document_source_internal_unpack_bucket_test/group_reorder_test.cpp
+++ b/src/mongo/db/pipeline/document_source_internal_unpack_bucket_test/group_reorder_test.cpp
@@ -94,6 +94,25 @@ TEST_F(InternalUnpackBucketGroupReorder, MinMaxGroupOnMetadata) {
ASSERT_BSONOBJ_EQ(optimized, serialized[0]);
}
+// Test SERVER-73822 fix: complex $min and $max (i.e. not just straight field refs) work correctly.
+TEST_F(InternalUnpackBucketGroupReorder, MinMaxComplexGroupOnMetadata) {
+ auto unpackSpecObj = fromjson(
+ "{$_internalUnpackBucket: { include: ['a', 'b', 'c'], metaField: 'meta1', timeField: 't', "
+ "bucketMaxSpanSeconds: 3600}}");
+ auto groupSpecObj = fromjson(
+ "{$group: {_id: '$meta1.a.b', accmin: {$min: {$add: ['$b', {$const: 0}]}}, accmax: {$max: "
+ "{$add: [{$const: 0}, '$c']}}}}");
+
+ auto pipeline = Pipeline::parse(makeVector(unpackSpecObj, groupSpecObj), getExpCtx());
+ pipeline->optimizePipeline();
+
+ auto serialized = pipeline->serializeToBson();
+ ASSERT_EQ(2, serialized.size());
+ // Order of fields may be different between original 'unpackSpecObj' and 'serialized[0]'.
+ // ASSERT_BSONOBJ_EQ(unpackSpecObj, serialized[0]);
+ ASSERT_BSONOBJ_EQ(groupSpecObj, serialized[1]);
+}
+
TEST_F(InternalUnpackBucketGroupReorder, MinMaxGroupOnMetafield) {
auto unpackSpecObj = fromjson(
"{$_internalUnpackBucket: { include: ['a', 'b', 'c'], metaField: 'meta1', timeField: 't', "