summaryrefslogtreecommitdiff
path: root/jstests/aggregation/add_with_date.js
blob: b80c304e42c61a48c2f31ab3787ef27788d0282f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Test $add with date
(function() {
"use strict";

load("jstests/libs/sbe_assert_error_override.js");  // Override error-code-checking APIs.
load("jstests/libs/sbe_util.js");                   // For checkSBEEnabled.

const coll = db.getSiblingDB(jsTestName()).coll;
coll.drop();

function getResultOfExpression(expr) {
    const resultArray = coll.aggregate({$project: {computed: expr}}).toArray();
    assert.eq(1, resultArray.length, "ERROR from " + tojson(expr));
    return resultArray[0].computed;
}

assert.commandWorked(coll.insert({
    _id: 0,
    decimalVal: NumberDecimal("819.5359123621083"),
    doubleVal: 819.536,
    int64Val: NumberLong(820),
    int32Val: NumberInt(820),
    dateVal: ISODate("2019-01-30T07:30:10.137Z"),
    overflowDecimal: NumberDecimal("1e5000"),
    overflowDouble: 1e1000,
    overflowInt64: NumberLong("9223372036854775807"),
    nanDouble: NaN,
    nanDecimal: NumberDecimal("NaN"),
}));

const isSBEEnabled = checkSBEEnabled(db, ["featureFlagSbeFull"]);

// Adding a Decimal128 value to a date literal.
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$decimalVal", ISODate("2019-01-30T07:30:10.137Z")]}));
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: [ISODate("2019-01-30T07:30:10.137Z"), "$decimalVal"]}));

// Adding a Decimal128 literal to a date value.
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$dateVal", NumberDecimal("819.5359123621083")]}));
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: [NumberDecimal("819.5359123621083"), "$dateVal"]}));

// Adding a Decimal128 value to a date value.
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$dateVal", "$decimalVal"]}));
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$decimalVal", "$dateVal"]}));

// Adding a double value to a date value.
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$dateVal", "$doubleVal"]}));
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$doubleVal", "$dateVal"]}));

// Adding an int64_t value to date value.
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$dateVal", "$int64Val"]}));
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$int64Val", "$dateVal"]}));

// Adding an int32_t value to date value.
assert.eq(ISODate("2019-01-30T07:30:10.957Z"),
          getResultOfExpression({$add: ["$dateVal", "$int32Val"]}));
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.597Z"),
          getResultOfExpression({$add: ["$dateVal", "$decimalVal", "$doubleVal", "$int64Val"]}));
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.
assert.throwsWithCode(() => getResultOfExpression({$add: ["$dateVal", "$overflowDouble"]}),
                      ErrorCodes.Overflow);

assert.throwsWithCode(() => getResultOfExpression({$add: ["$dateVal", "$overflowInt64"]}),
                      ErrorCodes.Overflow);

assert.throwsWithCode(
    () => getResultOfExpression({$add: ["$dateVal", "$int64Val", "$overflowDouble"]}),
    ErrorCodes.Overflow);

assert.throwsWithCode(
    () => getResultOfExpression({$add: ["$int64Val", "$dateVal", "$overflowDouble"]}),
    ErrorCodes.Overflow);

// 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"]}),
                      ErrorCodes.Overflow);

assert.throwsWithCode(() => getResultOfExpression({$add: ["$nanDouble", "$dateVal"]}),
                      ErrorCodes.Overflow);

// 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.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")
              ]
          }));
}());