diff options
author | Dan Larkin-York <dan.larkin-york@mongodb.com> | 2022-03-16 21:35:32 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-03-16 22:20:33 +0000 |
commit | 75c7f1ed4f99d3fa3bfbf942cf086b68c9a47286 (patch) | |
tree | b5e3996ea4bf0af1e93050f2faf271172476d84f /jstests | |
parent | ec5381df31ecc465aa3f008d60dbee2c34d1fb94 (diff) | |
download | mongo-75c7f1ed4f99d3fa3bfbf942cf086b68c9a47286.tar.gz |
SERVER-64347 Add support for descending sort to the bounded sorter
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/core/timeseries/timeseries_internal_bounded_sort.js | 126 |
1 files changed, 70 insertions, 56 deletions
diff --git a/jstests/core/timeseries/timeseries_internal_bounded_sort.js b/jstests/core/timeseries/timeseries_internal_bounded_sort.js index 0c113e1eab1..f16ae458143 100644 --- a/jstests/core/timeseries/timeseries_internal_bounded_sort.js +++ b/jstests/core/timeseries/timeseries_internal_bounded_sort.js @@ -51,71 +51,85 @@ assert.commandWorked(coll.createIndex({t: 1})); const unpackStage = getAggPlanStage(coll.explain().aggregate(), '$_internalUnpackBucket'); -function assertSorted(result) { - let prev = {t: -Infinity}; +function assertSorted(result, ascending) { + let prev = ascending ? {t: -Infinity} : {t: Infinity}; for (const doc of result) { - assert.lte(+prev.t, +doc.t, 'Found two docs not in time order: ' + tojson({prev, doc})); + if (ascending) { + assert.lte(+prev.t, + +doc.t, + 'Found two docs not in ascending time order: ' + tojson({prev, doc})); + } else { + assert.gte(+prev.t, + +doc.t, + 'Found two docs not in descending time order: ' + tojson({prev, doc})); + } + prev = doc; } } -// Test sorting the whole collection. -{ - const naive = buckets - .aggregate([ - unpackStage, - {$_internalInhibitOptimization: {}}, - {$sort: {t: 1}}, - ]) - .toArray(); - assertSorted(naive); +function runTest(ascending) { + // Test sorting the whole collection. + { + const naive = buckets + .aggregate([ + unpackStage, + {$_internalInhibitOptimization: {}}, + {$sort: {t: ascending ? 1 : -1}}, + ]) + .toArray(); + assertSorted(naive, ascending); - const opt = buckets - .aggregate([ - {$sort: {'control.min.t': 1}}, - unpackStage, - { - $_internalBoundedSort: { - sortKey: {t: 1}, - bound: bucketMaxSpanSeconds, - } - }, - ]) - .toArray(); - assertSorted(opt); + const opt = buckets + .aggregate([ + {$sort: {'control.min.t': ascending ? 1 : -1}}, + unpackStage, + { + $_internalBoundedSort: { + sortKey: {t: ascending ? 1 : -1}, + bound: bucketMaxSpanSeconds, + } + }, + ]) + .toArray(); + assertSorted(opt, ascending); - assert.eq(naive, opt); -} + assert.eq(naive, opt); + } -// Test $sort + $limit. -{ - const naive = buckets - .aggregate([ - unpackStage, - {$_internalInhibitOptimization: {}}, - {$sort: {t: 1}}, - {$limit: 100}, - ]) - .toArray(); - assertSorted(naive); - assert.eq(100, naive.length); + // Test $sort + $limit. + { + const naive = buckets + .aggregate([ + unpackStage, + {$_internalInhibitOptimization: {}}, + {$sort: {t: ascending ? 1 : -1}}, + {$limit: 100}, + ]) + .toArray(); + assertSorted(naive, ascending); + assert.eq(100, naive.length); - const opt = buckets - .aggregate([ - {$sort: {'control.min.t': 1}}, - unpackStage, - { - $_internalBoundedSort: { - sortKey: {t: 1}, - bound: bucketMaxSpanSeconds, - } - }, - {$limit: 100}, - ]) - .toArray(); - assertSorted(opt); - assert.eq(100, opt.length); + const opt = buckets + .aggregate([ + {$sort: {'control.min.t': ascending ? 1 : -1}}, + unpackStage, + { + $_internalBoundedSort: { + sortKey: {t: ascending ? 1 : -1}, + bound: bucketMaxSpanSeconds, + } + }, + {$limit: 100}, + ]) + .toArray(); + assertSorted(opt, ascending); + assert.eq(100, opt.length); - assert.eq(naive, opt); + assert.eq(naive, opt); + } } + +runTest(true); // ascending +runTest(false); // descending })(); |