summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/densify/decimal.js
blob: 415cd3117b34bec01a06bab40aab5dfab94f65e0 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 * Test that $densify works with Decimal128 type values.
 * @tags: [
 *   # Needed as $densify is a 51 feature.
 *   requires_fcv_51,
 * ]
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // For arrayEq.

function buildErrorString(found, expected) {
    return "Expected:\n" + tojson(expected) + "\nGot:\n" + tojson(found);
}

const coll = db[jsTestName()];
coll.drop();

assert.commandWorked(coll.insert([
    {val: NumberDecimal(0)},
    {val: NumberDecimal(10)},
]));

let pipeline = [{$project: {_id: 0}}, {$densify: {field: "val", range: {step: 1, bounds: "full"}}}];

let expectedResult = [];
for (let i = 0; i <= 10; i++) {
    expectedResult.push({val: NumberDecimal(i)});
}

let result = coll.aggregate(pipeline).toArray();

assert(arrayEq(result, expectedResult), buildErrorString(result, expectedResult));

// Add a double in the middle of the range.
assert.commandWorked(coll.insert({val: 5}));
expectedResult = [];
// $densify uses the last type seen for generated documents.
for (let i = 0; i <= 10; i++) {
    if (i < 5 || i == 10) {
        expectedResult.push({val: NumberDecimal(i)});
    } else {
        expectedResult.push({val: i});
    }
}
result = coll.aggregate(pipeline).toArray();

assert(arrayEq(result, expectedResult), buildErrorString(result, expectedResult));

// Run the same test, but with a Decimal step.
pipeline = [
    {$project: {_id: 0}},
    {$densify: {field: "val", range: {step: NumberDecimal(1), bounds: "full"}}}
];
expectedResult = [];
for (let i = 0; i <= 10; i++) {
    if (i != 5) {
        expectedResult.push({val: NumberDecimal(i)});
    } else {
        expectedResult.push({val: i});
    }
}
result = coll.aggregate(pipeline).toArray();

assert(arrayEq(result, expectedResult), buildErrorString(result, expectedResult));

// Run the same test, but with decimals instead of integers.
coll.drop();
assert.commandWorked(coll.insert([
    {val: NumberDecimal(0)},
    {val: .5},
    {val: NumberDecimal(1)},
]));
pipeline = [{$project: {_id: 0}}, {$densify: {field: "val", range: {step: .1, bounds: "full"}}}];
// Note that all the results after .5 may not be precisely on the step, but instead be off by a
// vanishingly small amount.
expectedResult = [
    {val: NumberDecimal(0)},
    {val: NumberDecimal(".1")},
    {val: NumberDecimal(".2")},
    {val: NumberDecimal(".3")},
    {val: NumberDecimal(".4")},
    {val: .5},
    {val: .6},
    {val: .7},
    {val: .7999999999999999},
    {val: .8999999999999999},
    {val: .9999999999999999},
    {val: NumberDecimal(1)},
];
result = coll.aggregate(pipeline).toArray();
assert(arrayEq(result, expectedResult), buildErrorString(result, expectedResult));

// Repeat with a NumberDecimal step
pipeline = [
    {$project: {_id: 0}},
    {$densify: {field: "val", range: {step: NumberDecimal(".1"), bounds: "full"}}}
];
expectedResult = [
    {val: NumberDecimal(0)},
    {val: NumberDecimal(".1")},
    {val: NumberDecimal(".2")},
    {val: NumberDecimal(".3")},
    {val: NumberDecimal(".4")},
    {val: .5},
    {val: NumberDecimal(".6")},
    {val: NumberDecimal(".7")},
    {val: NumberDecimal(".8")},
    {val: NumberDecimal(".9")},
    {val: NumberDecimal(1)},
];
result = coll.aggregate(pipeline).toArray();

assert(arrayEq(result, expectedResult), buildErrorString(result, expectedResult));

// If the step is Decimal128, return Decimal128.
coll.drop();
assert.commandWorked(coll.insert([
    {val: 0},
    {val: 1},
]));

pipeline = [
    {$project: {_id: 0}},
    {$densify: {field: "val", range: {step: NumberDecimal(.001), bounds: "full"}}},
    // No need to check every value.
    {$limit: 2},
];
result = coll.aggregate(pipeline).toArray();
assert(arrayEq(result, [{val: 0}, {val: NumberDecimal(.001)}]));

// Decimal bounds fail if step is not decimal.
pipeline = [
    {$project: {_id: 0}},
    {$densify: {field: "val", range: {step: .1, bounds: [NumberDecimal(.1), NumberDecimal(.9)]}}},
    // No need to check every value.
    {$limit: 3},
];
assert.commandFailedWithCode(
    db.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}}), 5876900);

// Verify that if 'step' is not representable as a double precision is not lost during computation.
const preciseStep = NumberDecimal(.1243568735894448377382);
pipeline = [
    {$project: {_id: 0}},
    {$densify: {field: "val", range: {step: preciseStep, bounds: "full"}}},
    // No need to check every value.
    {$limit: 4},
];
result = coll.aggregate(pipeline).toArray();
expectedResult = [
    {val: 0},
    {val: preciseStep},
    {val: NumberDecimal(preciseStep * 2)},
    {val: NumberDecimal(preciseStep * 3)}
];
assert(arrayEq(result, expectedResult), buildErrorString(result, expectedResult));
})();