summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/densify/libs/parse_util.js
blob: 7ccedefd81d51810c204c7eef55f7f95c6d3a479 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 * Utility for testing the parsing of densify-like commands. I.e. $_internalDensify and $densify.
 */

let parseUtil = (function(db, coll, stageName, options = {}) {
    function run(stage, extraCommandArgs = options) {
        return coll.runCommand(Object.merge(
            {aggregate: coll.getName(), pipeline: [stage], cursor: {}}, extraCommandArgs));
    }

    function runTest(stageName) {
        // Required fields.
        const kIDLRequiredFieldErrorCode = 40414;
        assert.commandFailedWithCode(
            run({[stageName]: {field: "a", range: {step: 1.0}}}),
            kIDLRequiredFieldErrorCode,
            "BSON field '$densify.range.bounds' is missing but a required field");
        assert.commandFailedWithCode(run({[stageName]: {range: {step: 1.0, bounds: "full"}}}),
                                     kIDLRequiredFieldErrorCode,
                                     "BSON field '$densify.field' is missing but a required field");
        assert.commandFailedWithCode(
            run({[stageName]: {field: "a", range: {bounds: "full"}}}),
            kIDLRequiredFieldErrorCode,
            "BSON field '$densify.range.step' is missing but a required field");

        // Wrong types
        assert.commandFailedWithCode(
            run({[stageName]: {field: 1.0, range: {step: 1.0, bounds: "full"}}}),
            ErrorCodes.TypeMismatch,
            "BSON field '$densify.field' is the wrong type 'double', expected type 'string'");
        assert.commandFailedWithCode(
            run({[stageName]: {field: "a", range: {step: "invalid", bounds: "full"}}}),
            ErrorCodes.TypeMismatch,
            "BSON field '$densify.range.step' is the wrong type 'string', expected types '[int, decimal, double, long']");
        assert.commandFailedWithCode(
            run({
                [stageName]:
                    {field: "a", partitionByFields: "incorrect", range: {step: 1.0, bounds: "full"}}
            }),
            ErrorCodes.TypeMismatch,
            "BSON field '$densify.partitionByFields' is the wrong type 'string', expected type 'array'");
        assert.commandFailedWithCode(
            run({
                [stageName]: {
                    field: "a",
                    range: {
                        step: 1.0,
                        bounds: [new ISODate("2020-01-01"), new ISODate("2020-01-02")],
                        unit: 1000
                    }
                }
            }),
            ErrorCodes.TypeMismatch,
            "BSON field '$densify.range.unit' is the wrong type 'double', expected type 'string'");

        // Logical errors
        // Too few bounds
        assert.commandFailedWithCode(
            run({
                [stageName]: {
                    field: "a",
                    range: {step: 1.0, bounds: [new ISODate("2020-01-01")], unit: "second"}
                }
            }),
            5733403,
            "a bounding array in a range statement must have exactly two elements");
        // Too many elements
        assert.commandFailedWithCode(
            run({[stageName]: {field: "a", range: {step: 1.0, bounds: [0, 1, 2]}}}),
            5733403,
            "a bounding array in a range statement must have exactly two elements");
        // Negative step
        assert.commandFailedWithCode(
            run({[stageName]: {field: "a", range: {step: -1.0, bounds: [0, 1]}}}),
            5733401,
            "the step parameter in a range statement must be a strictly positive numeric value");
        // Field path expression instead of field path
        assert.commandFailedWithCode(
            run({[stageName]: {field: "$a", range: {step: 1.0, bounds: [0, 1]}}}),
            16410,
            "FieldPath field names may not start with '$'. Consider using $getField or $setField.");
        // Field path expression instead of field path
        assert.commandFailedWithCode(
            run({
                [stageName]:
                    {field: "a", partitionByFields: ["$b"], range: {step: 1.0, bounds: [0, 1]}}
            }),
            16410,
            "FieldPath field names may not start with '$'. Consider using $getField or $setField.");
        // Partition bounds but not partitionByFields
        assert.commandFailedWithCode(
            run({
                [stageName]:
                    {field: "a", partitionByFields: [], range: {step: 1.0, bounds: "partition"}}
            }),
            5733408,
            "one may not specify the bounds as 'partition' without specifying a non-empty array of partitionByFields. You may have meant to specify 'full' bounds.");
        assert.commandFailedWithCode(
            run({[stageName]: {field: "a", range: {step: 1.0, bounds: "partition"}}}),
            5733408,
            "one may not specify the bounds as 'partition' without specifying a non-empty array of partitionByFields. You may have meant to specify 'full' bounds.");
        // Left bound greater than right bound
        assert.commandFailedWithCode(
            run({[stageName]: {field: "a", range: {step: 1.0, bounds: [2, 1]}}}),
            5733402,
            "the bounds in a range statement must be the string 'full', 'partition', or an ascending array of two numbers or two dates");
        assert.commandFailedWithCode(
            run({
                [stageName]: {
                    field: "a",
                    range: {
                        step: 1.0,
                        bounds: [new ISODate("2020-01-01"), new ISODate("2019-01-01")],
                        unit: "second"
                    }
                }
            }),
            5733402,
            "the bounds in a range statement must be the string 'full', 'partition', or an ascending array of two numbers or two dates");
        // Unit with numeric bounds
        assert.commandFailedWithCode(
            run({[stageName]: {field: "a", range: {step: 1.0, bounds: [1, 2], unit: "second"}}}),
            5733409,
            "numeric bounds may not have unit parameter");
        // Mixed numeric and date bounds
        assert.commandFailedWithCode(
            run({
                [stageName]:
                    {field: "a", range: {step: 1.0, bounds: [1, new ISODate("2020-01-01")]}}
            }),
            5733406,
            "a bounding array must contain either both dates or both numeric types");
        assert.commandFailedWithCode(
            run({
                [stageName]: {
                    field: "a",
                    range: {step: 1.0, bounds: [new ISODate("2020-01-01"), 1], unit: "second"}
                }
            }),
            5733402,
            "a bounding array must be an ascending array of either two dates or two numbers");

        // Positive test cases
        assert.commandWorked(run({[stageName]: {field: "a", range: {step: 1.0, bounds: [1, 2]}}}));
        assert.commandWorked(run({
            [stageName]: {
                field: "a",
                range: {
                    step: 1.0,
                    bounds: [new ISODate("2020-01-01"), new ISODate("2021-01-01")],
                    unit: "day"
                }
            }
        }));
        assert.commandWorked(run({
            [stageName]: {
                field: "a",
                partitionByFields: ["b", "c"],
                range: {
                    step: 1.0,
                    bounds: [new ISODate("2020-01-01"), new ISODate("2021-01-01")],
                    unit: "week"
                }
            }
        }));
        assert.commandWorked(run({
            [stageName]: {
                field: "a",
                partitionByFields: ["b", "c"],
                range: {step: 1.0, bounds: "partition", unit: "second"}
            }
        }));
        assert.commandWorked(run({
            [stageName]: {
                field: "a",
                partitionByFields: ["b", "c"],
                range: {step: 1.0, bounds: "full", unit: "second"}
            }
        }));
        assert.commandWorked(run({
            [stageName]:
                {field: "a", partitionByFields: ["b", "c"], range: {step: 1.0, bounds: "full"}}
        }));
        assert.commandWorked(run({
            [stageName]: {
                field: "a",
                partitionByFields: [
                    "b",
                ],
                range: {step: 1.0, bounds: "partition"}
            }
        }));
        assert.commandWorked(run({[stageName]: {field: "a", range: {step: 1.0, bounds: "full"}}}));
    }

    runTest(stageName);
});