summaryrefslogtreecommitdiff
path: root/jstests/aggregation/accumulators/accumulator_js_size_limits.js
blob: 80bbcc49e07dfef1f36da1f447d0d72dec4df98b (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
// Test several different kinds of size limits on user-defined (Javascript) accumulators.
(function() {
"use strict";

const coll = db.accumulator_js_size_limits;

function runExample(groupKey, accumulatorSpec) {
    return coll.runCommand({
        aggregate: coll.getName(),
        cursor: {},
        pipeline: [{
            $group: {
                _id: groupKey,
                accumulatedField: {$accumulator: accumulatorSpec},
            }
        }]
    });
}

// Accumulator tries to create too long a String; it can't be serialized to BSON.
coll.drop();
assert.commandWorked(coll.insert({}));
let res = runExample(1, {
    init: function() {
        return "a".repeat(20 * 1024 * 1024);
    },
    accumulate: function() {
        throw 'accumulate should not be called';
    },
    accumulateArgs: [],
    merge: function() {
        throw 'merge should not be called';
    },
    finalize: function() {
        throw 'finalize should not be called';
    },
    lang: 'js',
});
assert.commandFailedWithCode(res, [10334]);

// Accumulator tries to return BSON larger than 16MB from JS.
assert(coll.drop());
assert.commandWorked(coll.insert({}));
res = runExample(1, {
    init: function() {
        const str = "a".repeat(1 * 1024 * 1024);
        return Array.from({length: 20}, () => str);
    },
    accumulate: function() {
        throw 'accumulate should not be called';
    },
    accumulateArgs: [],
    merge: function() {
        throw 'merge should not be called';
    },
    finalize: function() {
        throw 'finalize should not be called';
    },
    lang: 'js',
});
assert.commandFailedWithCode(res, [17260]);

// $group size limit exceeded, and cannot spill.
assert(coll.drop());
assert.commandWorked(coll.insert(Array.from({length: 200}, (_, i) => ({_id: i}))));
// By grouping on _id, each group contains only 1 document. This means it creates many
// AccumulatorState instances.
res = runExample("$_id", {
    init: function() {
        // Each accumulator state is big enough to be expensive, but not big enough to hit the BSON
        // size limit.
        return "a".repeat(1 * 1024 * 1024);
    },
    accumulate: function(state) {
        return state;
    },
    accumulateArgs: [1],
    merge: function(state1, state2) {
        return state1;
    },
    finalize: function(state) {
        return state.length;
    },
    lang: 'js',
});
assert.commandFailedWithCode(res, [16945]);
})();