summaryrefslogtreecommitdiff
path: root/jstests/aggregation/add_with_date.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/aggregation/add_with_date.js')
-rw-r--r--jstests/aggregation/add_with_date.js50
1 files changed, 35 insertions, 15 deletions
diff --git a/jstests/aggregation/add_with_date.js b/jstests/aggregation/add_with_date.js
index 8e3cb8597b5..0372ef785bb 100644
--- a/jstests/aggregation/add_with_date.js
+++ b/jstests/aggregation/add_with_date.js
@@ -1,12 +1,13 @@
(function() {
"use strict";
+load("jstests/libs/sbe_assert_error_override.js");
const coll = db.getSiblingDB(jsTestName()).coll;
coll.drop();
function getResultOfExpression(expr) {
const resultArray = coll.aggregate({$project: {computed: expr}}).toArray();
- assert.eq(1, resultArray.length);
+ assert.eq(1, resultArray.length, "ERROR from " + tojson(expr));
return resultArray[0].computed;
}
@@ -61,13 +62,12 @@ assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
getResultOfExpression({$add: ["$int32Val", "$dateVal"]}));
// Addition with a date and multiple values of differing data types.
-assert.eq(ISODate("2019-01-30T07:30:12.596Z"),
+assert.eq(ISODate("2019-01-30T07:30:12.597Z"),
getResultOfExpression({$add: ["$dateVal", "$decimalVal", "$doubleVal", "$int64Val"]}));
-assert.eq(ISODate("2019-01-30T07:30:12.596Z"),
+assert.eq(ISODate("2019-01-30T07:30:12.597Z"),
getResultOfExpression({$add: ["$decimalVal", "$dateVal", "$doubleVal", "$int64Val"]}));
assert.eq(ISODate("2019-01-30T07:30:12.596Z"),
getResultOfExpression({$add: ["$decimalVal", "$doubleVal", "$int64Val", "$dateVal"]}));
-
// The result of an addition must remain in the range of int64_t in order to convert back to a Date;
// an overflow into the domain of double-precision floating point numbers triggers a query-fatal
// error.
@@ -85,13 +85,15 @@ assert.throwsWithCode(
() => getResultOfExpression({$add: ["$int64Val", "$dateVal", "$overflowDouble"]}),
ErrorCodes.Overflow);
-// One quirk of date addition semantics is that an overflow into the domain of Decimal128 is not
-// fatal and instead results in an invalid "NaN" Date value.
-const nanDate = new Date("");
-assert.eq(nanDate, getResultOfExpression({$add: ["$dateVal", "$overflowDecimal"]}));
-assert.eq(nanDate,
- getResultOfExpression({$add: ["$dateVal", "$overflowDouble", "$overflowDecimal"]}));
-assert.eq(nanDate, getResultOfExpression({$add: ["$int64Val", "$dateVal", "$overflowDecimal"]}));
+// An overflow into the domain of Decimal128 results in an overflow exception.
+assert.throwsWithCode(() => getResultOfExpression({$add: ["$dateVal", "$overflowDecimal"]}),
+ ErrorCodes.Overflow);
+assert.throwsWithCode(
+ () => getResultOfExpression({$add: ["$int64Val", "$dateVal", "$overflowDecimal"]}),
+ ErrorCodes.Overflow);
+assert.throwsWithCode(
+ () => getResultOfExpression({$add: ["$dateVal", "$overflowDouble", "$overflowDecimal"]}),
+ ErrorCodes.Overflow);
// Adding a double-typed NaN to a date value.
assert.throwsWithCode(() => getResultOfExpression({$add: ["$dateVal", "$nanDouble"]}),
@@ -100,14 +102,32 @@ assert.throwsWithCode(() => getResultOfExpression({$add: ["$dateVal", "$nanDoubl
assert.throwsWithCode(() => getResultOfExpression({$add: ["$nanDouble", "$dateVal"]}),
ErrorCodes.Overflow);
-// Adding a Decimal128-typed NaN to a date value.
-assert.eq(nanDate, getResultOfExpression({$add: ["$dateVal", "$nanDecimal"]}));
-assert.eq(nanDate, getResultOfExpression({$add: ["$nanDecimal", "$dateVal"]}));
+// An NaN Decimal128 added to date results in an overflow exception.
+assert.throwsWithCode(() => getResultOfExpression({$add: ["$dateVal", "$nanDecimal"]}),
+ ErrorCodes.Overflow);
+assert.throwsWithCode(() => getResultOfExpression({$add: ["$nanDecimal", "$dateVal"]}),
+ ErrorCodes.Overflow);
// Addition with a date, a double-typed NaN, and a third value.
assert.throwsWithCode(() => getResultOfExpression({$add: ["$dateVal", "$doubleVal", "$nanDouble"]}),
ErrorCodes.Overflow);
// Addition with a date, and both types of NaN.
-assert.eq(nanDate, getResultOfExpression({$add: ["$dateVal", "$nanDouble", "$nanDecimal"]}));
+assert.throwsWithCode(
+ () => getResultOfExpression({$add: ["$dateVal", "$nanDouble", "$nanDecimal"]}),
+ ErrorCodes.Overflow);
+
+// Throw error when there're two or more date in $add.
+assert.throwsWithCode(() => getResultOfExpression({$add: ["$dateVal", 1, "$dateVal"]}), 4974202);
+
+// Test very large long and verify that we're maintaining the precision of long arithmetic.
+// 2397083434877565865 and 239708343487756586 both cast to the same double value from longs
+assert.eq(ISODate("2019-01-30T07:30:10.958Z"), getResultOfExpression({
+ $add: [
+ "$dateVal",
+ NumberLong("2397083434877565865"),
+ "$doubleVal",
+ NumberLong("-2397083434877565864")
+ ]
+ }));
}());