summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/setWindowFields/count.js
blob: 89e2245704f60ee438030989f94e413d5f21c0b7 (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
/**
 * Test that $count works as a window function.
 */
(function() {
"use strict";

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

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

const nDocs = 10;
for (let i = 0; i < nDocs; i++) {
    assert.commandWorked(coll.insert({
        one: i,
        partition: i % 2,
        partitionSeq: Math.trunc(i / 2),
    }));
}

const origDocs = coll.find().sort({_id: 1});
function verifyResults(results, valueFunction) {
    for (let i = 0; i < results.length; i++) {
        // Use Object.assign to make a copy instead of pass a reference.
        const correctDoc = valueFunction(i, Object.assign({}, origDocs[i]));
        assert(documentEq(correctDoc, results[i]),
               "Got: " + tojson(results[i]) + "\nExpected: " + tojson(correctDoc) +
                   "\n at position " + i + "\n");
    }
}

// Run an unpartitioned count query using the window bounds specified as [lower, upper].
function runCountQuery(lower, upper) {
    return coll
        .aggregate([
            {
                $setWindowFields: {
                    sortBy: {one: 1},
                    output: {a: {$count: {}, window: {documents: [lower, upper]}}}
                }
            },
            {$sort: {_id: 1}}
        ])
        .toArray();
}

// Test left unbounded count.
let result = runCountQuery("unbounded", "unbounded");
verifyResults(result, function(num, baseObj) {
    baseObj.a = nDocs;
    return baseObj;
});

// Test left unbounded count.
result = runCountQuery("unbounded", "current");
verifyResults(result, function(num, baseObj) {
    baseObj.a = baseObj.one + 1;
    return baseObj;
});

// test sliding window using only previous vals.
result = runCountQuery(-1, "current");
verifyResults(result, function(num, baseObj) {
    baseObj.a = (baseObj.one == 0) ? 1 : 2;
    return baseObj;
});

// test sliding window using future vals.
result = runCountQuery(-1, 1);
verifyResults(result, function(num, baseObj) {
    if (baseObj.one == 0 || baseObj.one == nDocs - 1) {
        baseObj.a = 2;
    } else {
        baseObj.a = 3;
    }
    return baseObj;
});

// Run an count query partitioned over "$partition" using the window bounds specified as
// [lower, upper].
function runPartitionedCountQuery(lower, upper) {
    return coll
        .aggregate([
            {
                $setWindowFields: {
                    partitionBy: "$partition",
                    sortBy: {one: 1},
                    output: {a: {$count: {}, window: {documents: [lower, upper]}}}
                }
            },
            {$sort: {_id: 1}}
        ])
        .toArray();
}

// Test partitioned unbounded count.
result = runPartitionedCountQuery("unbounded", "unbounded");
verifyResults(result, function(num, baseObj) {
    baseObj.a = nDocs / 2;
    return baseObj;
});

// Test partitioned left unbounded count.
result = runPartitionedCountQuery("unbounded", "current");
verifyResults(result, function(num, baseObj) {
    baseObj.a = baseObj.partitionSeq + 1;
    return baseObj;
});

// test partitioned sliding window using only previous vals.
result = runPartitionedCountQuery(-1, "current");
verifyResults(result, function(num, baseObj) {
    baseObj.a = (baseObj.partitionSeq == 0) ? 1 : 2;
    return baseObj;
});

// test partitioned sliding window using future vals.
result = runPartitionedCountQuery(-1, 1);
verifyResults(result, function(num, baseObj) {
    if (baseObj.partitionSeq == 0 || baseObj.partitionSeq == Math.trunc(nDocs / 2) - 1) {
        baseObj.a = 2;
    } else {
        baseObj.a = 3;
    }
    return baseObj;
});
})();