summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/setWindowFields/locf.js
blob: dfce32fae9dca5f707778ad477a0df2f243bc7bf (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
/**
 * 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.

if (!FeatureFlagUtil.isEnabled(db, "Fill")) {
    jsTestLog("Skipping as featureFlagFill is not enabled");
    return;
}

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(result, 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(result, 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));

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

assertArrayEq(result, collection);

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

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

assertArrayEq(result, collection);
})();