summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/subtract.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/aggregation/expressions/subtract.js')
-rw-r--r--jstests/aggregation/expressions/subtract.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/jstests/aggregation/expressions/subtract.js b/jstests/aggregation/expressions/subtract.js
index 113635c7b53..34dd2615ade 100644
--- a/jstests/aggregation/expressions/subtract.js
+++ b/jstests/aggregation/expressions/subtract.js
@@ -1,3 +1,6 @@
+load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and assertErrMsgContains.
+load("jstests/libs/sbe_assert_error_override.js"); // Override error-code-checking APIs.
+
// Tests for $subtract aggregation expression
(function() {
"use strict";
@@ -15,6 +18,20 @@ assert.commandWorked(
assert.commandWorked(coll.insert({_id: 5, lhs: new Date(1912392670000), rhs: 70000}));
assert.commandWorked(
coll.insert({_id: 6, lhs: new Date(1912392670000), rhs: new Date(1912392600000)}));
+// Doubles are rounded to int64 when subtracted from Date
+assert.commandWorked(coll.insert({_id: 7, lhs: new Date(1683794065002), rhs: 0.5}));
+assert.commandWorked(coll.insert({_id: 8, lhs: new Date(1683794065002), rhs: 1.4}));
+assert.commandWorked(coll.insert({_id: 9, lhs: new Date(1683794065002), rhs: 1.5}));
+assert.commandWorked(coll.insert({_id: 10, lhs: new Date(1683794065002), rhs: 1.7}));
+// Decimals are rounded to int64, when tie rounded to even, when subtracted from Date
+assert.commandWorked(
+ coll.insert({_id: 11, lhs: new Date(1683794065002), rhs: new NumberDecimal("1.4")}));
+assert.commandWorked(
+ coll.insert({_id: 12, lhs: new Date(1683794065002), rhs: new NumberDecimal("1.5")}));
+assert.commandWorked(
+ coll.insert({_id: 13, lhs: new Date(1683794065002), rhs: new NumberDecimal("1.7")}));
+assert.commandWorked(
+ coll.insert({_id: 14, lhs: new Date(1683794065002), rhs: new NumberDecimal("2.5")}));
const result =
coll.aggregate([{$project: {diff: {$subtract: ["$lhs", "$rhs"]}}}, {$sort: {_id: 1}}])
@@ -26,4 +43,31 @@ assert.eq(result[3].diff, 10.0);
assert.eq(result[4].diff, NumberDecimal("9990.00005"));
assert.eq(result[5].diff, new Date(1912392600000));
assert.eq(result[6].diff, 70000);
+assert.eq(result[7].diff, new Date(1683794065001));
+assert.eq(result[8].diff, new Date(1683794065001));
+assert.eq(result[9].diff, new Date(1683794065000));
+assert.eq(result[10].diff, new Date(1683794065000));
+assert.eq(result[11].diff, new Date(1683794065001));
+assert.eq(result[12].diff, new Date(1683794065000));
+assert.eq(result[13].diff, new Date(1683794065000));
+assert.eq(result[14].diff, new Date(1683794065000));
+
+// Following cases will report overflow error
+coll.drop();
+
+assert.commandWorked(coll.insert([{
+ _id: 0,
+ veryBigNegativeLong: NumberLong("-9223372036854775808"),
+ veryBigNegativeDouble: -9223372036854775808,
+ veryBigNegativeDecimal: NumberDecimal("-9223372036854775808")
+}]));
+
+let pipeline = [{$project: {res: {$subtract: [new Date(10), "$veryBigNegativeLong"]}}}];
+assertErrCodeAndErrMsgContains(coll, pipeline, ErrorCodes.Overflow, "date overflow");
+
+pipeline = [{$project: {res: {$subtract: [new Date(10), "$veryBigNegativeDouble"]}}}];
+assertErrCodeAndErrMsgContains(coll, pipeline, ErrorCodes.Overflow, "date overflow");
+
+pipeline = [{$project: {res: {$subtract: [new Date(10), "$veryBigNegativeDecimal"]}}}];
+assertErrCodeAndErrMsgContains(coll, pipeline, ErrorCodes.Overflow, "date overflow");
}());