summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/setWindowFields/locf.js
blob: b6a7d7960b6251e1fbeb950c5c772cde38b8bdeb (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
/**
 * Test that $locf works as a window function.
 * @tags: [
 *   # Needed as $fill and $locf are 5.2 features.
 *   requires_fcv_52,
 * ]
 */
(function() {
"use strict";

load("jstests/aggregation/extras/window_function_helpers.js");
load("jstests/aggregation/extras/utils.js");  // For arrayEq.
load("jstests/libs/feature_flag_util.js");    // For isEnabled.

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

// Test that $locf doesn't parse with a window.
assert.commandFailedWithCode(coll.runCommand({
    aggregate: coll.getName(),
    pipeline: [{
        $setWindowFields: {
            sortBy: {_id: 1},
            output: {val: {$locf: {}, window: []}},
        }
    }],
    cursor: {}
}),
                             ErrorCodes.FailedToParse);

// Create some documents.
let collection = [
    {_id: 0, val: null},
    {_id: 1, val: 0},
    {_id: 2, val: 2},
    {_id: 3, val: null},
    {_id: 4},
    {_id: 5, val: "str"},
    {_id: 6, val: null},
    {_id: 7, rand: "rand"},
];
assert.commandWorked(coll.insert(collection));

let result = coll.aggregate([{
                     $setWindowFields: {
                         sortBy: {_id: 1},
                         output: {val: {$locf: "$val"}},
                     }
                 }])
                 .toArray();

let expected = [
    {_id: 0, val: null},
    {_id: 1, val: 0},
    {_id: 2, val: 2},
    {_id: 3, val: 2},
    {_id: 4, val: 2},
    {_id: 5, val: "str"},
    {_id: 6, val: "str"},
    {_id: 7, rand: "rand", val: "str"},
];
assertArrayEq({actual: result, expected: expected});

// Test projecting to a different field.
result = coll.aggregate([{
                 $setWindowFields: {
                     sortBy: {_id: 1},
                     output: {newVal: {$locf: "$val"}},
                 }

             }])
             .toArray();

expected = [
    {_id: 0, val: null},
    {_id: 1, val: 0, newVal: 0},
    {_id: 2, val: 2, newVal: 2},
    {_id: 3, val: null, newVal: 2},
    {_id: 4, newVal: 2},
    {_id: 5, val: "str", newVal: "str"},
    {_id: 6, val: null, newVal: "str"},
    {_id: 7, rand: "rand", newVal: "str"},
];
assertArrayEq({actual: result, expected: expected});

// Partitions don't mix values
collection = [
    {_id: 1, val: 0, part: 1},
    {_id: 2, val: 2, part: 2},
    {_id: 3, val: null, part: 1},
    {_id: 4, val: null, part: 2},
];
coll.drop();
assert.commandWorked(coll.insert(collection));

result = coll.aggregate([{
                 $setWindowFields: {
                     sortBy: {_id: 1},
                     output: {val: {$locf: "$val"}},
                     partitionBy: "$part",
                 }
             }])
             .toArray();

expected = [
    {_id: 1, val: 0, part: 1},
    {_id: 2, val: 2, part: 2},
    {_id: 3, val: 0, part: 1},
    {_id: 4, val: 2, part: 2},
];
assertArrayEq({actual: result, expected: expected});

// Values stay missing if all values are missing.
collection = [
    {_id: 1},
    {_id: 2},
    {_id: 3, val: null},
    {_id: 4, val: null},
];
coll.drop();
assert.commandWorked(coll.insert(collection));
expected = collection;

result = coll.aggregate([{
                 $setWindowFields: {
                     sortBy: {_id: 1},
                     output: {val: {$locf: "$val"}},
                 }
             }])
             .toArray();

assertArrayEq({actual: result, expected: expected});

collection = [
    {_id: 1, val: null},
    {_id: 2},
    {_id: 3, val: null},
    {_id: 4},
];
coll.drop();
assert.commandWorked(coll.insert(collection));
expected = collection;

result = coll.aggregate([{
                 $setWindowFields: {
                     sortBy: {_id: 1},
                     output: {val: {$locf: "$val"}},
                 }
             }])
             .toArray();

assertArrayEq({actual: result, expected: expected});
})();