summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jstests/aggregation/sources/bucketauto/granularity_near_zero.js22
-rw-r--r--src/mongo/db/pipeline/granularity_rounder_preferred_numbers.cpp7
2 files changed, 29 insertions, 0 deletions
diff --git a/jstests/aggregation/sources/bucketauto/granularity_near_zero.js b/jstests/aggregation/sources/bucketauto/granularity_near_zero.js
new file mode 100644
index 00000000000..7f456f9428d
--- /dev/null
+++ b/jstests/aggregation/sources/bucketauto/granularity_near_zero.js
@@ -0,0 +1,22 @@
+// Tests when the granularity rounder needs to approach zero to round correctly. This test was
+// designed to reproduce SERVER-57091.
+(function() {
+"use strict";
+const coll = db.server57091;
+coll.drop();
+coll.insertOne({});
+
+const res =
+ coll.aggregate([{
+ $bucketAuto: {
+ groupBy: {
+ $reduce:
+ {input: [], initialValue: 4.940656484124654e-324, in : {$size: ["$$value"]}}
+ },
+ buckets: NumberLong("8"),
+ granularity: "R80"
+ }
+ }])
+ .toArray();
+assert.eq(res, [{_id: {min: 0, max: 1.02e-321}, count: 1}]);
+})();
diff --git a/src/mongo/db/pipeline/granularity_rounder_preferred_numbers.cpp b/src/mongo/db/pipeline/granularity_rounder_preferred_numbers.cpp
index d99f640521c..efdcf46e432 100644
--- a/src/mongo/db/pipeline/granularity_rounder_preferred_numbers.cpp
+++ b/src/mongo/db/pipeline/granularity_rounder_preferred_numbers.cpp
@@ -306,6 +306,13 @@ Value GranularityRounderPreferredNumbers::roundDown(Value value) {
multiplier /= 10.0;
}
+ // It is possible to get a number so small that the resulting multiplier would have to be
+ // smaller than the precision of a double, in which case the multiplier would equal 0. In
+ // this case, we can round down to 0.0.
+ if (multiplier == 0) {
+ return Value(0.0);
+ }
+
double previousMax;
while (number > (_baseSeries.back() * multiplier)) {
previousMax = _baseSeries.back() * multiplier;