diff options
author | Kevin Cherkauer <kevin.cherkauer@mongodb.com> | 2023-02-10 22:25:37 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-02-13 12:43:18 +0000 |
commit | f4b99751fb2383a94dce45685f12b97305a286d0 (patch) | |
tree | cfc35573e38c6b39a3423c13b42c0215ebfc4833 | |
parent | b5f7f35d46dab278abac976afe48ebf9632572b1 (diff) | |
download | mongo-f4b99751fb2383a94dce45685f12b97305a286d0.tar.gz |
SERVER-73822 Time-series $group rewrite ignores certain accumulators
(cherry picked from commit f9fd648140b48de6af36f1b96bd66b5b54d5deb7)
4 files changed, 38 insertions, 2 deletions
diff --git a/etc/backports_required_for_multiversion_tests.yml b/etc/backports_required_for_multiversion_tests.yml index 744c67e1e65..c66453c204b 100644 --- a/etc/backports_required_for_multiversion_tests.yml +++ b/etc/backports_required_for_multiversion_tests.yml @@ -306,6 +306,8 @@ last-continuous: ticket: SERVER-72620 - test_file: jstests/core/timeseries/bucket_unpacking_with_sort_extended_range.js ticket: SERVER-73110 + - test_file: jstests/core/timeseries/timeseries_groupby_reorder.js + ticket: SERVER-73822 suites: null last-lts: all: @@ -687,4 +689,6 @@ last-lts: ticket: SERVER-72620 - test_file: jstests/core/timeseries/bucket_unpacking_with_sort_extended_range.js ticket: SERVER-73110 + - 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 0f6ee2aef6d..c4c34bc1249 100644 --- a/jstests/core/timeseries/timeseries_groupby_reorder.js +++ b/jstests/core/timeseries/timeseries_groupby_reorder.js @@ -38,7 +38,18 @@ if (!isMongos(db)) { res.stages[1]); } -const res = coll.aggregate([{$group: {_id: '$meta', accmin: {$min: '$b'}, accmax: {$max: '$c'}}}]) - .toArray(); +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 256804b978f..0da1d9d1f75 100644 --- a/src/mongo/db/pipeline/document_source_internal_unpack_bucket.cpp +++ b/src/mongo/db/pipeline/document_source_internal_unpack_bucket.cpp @@ -815,6 +815,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', " |