summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/setWindowFields/collation.js
blob: 235ccd7aaf8945d0012b4ef371cb094dde1fe0f1 (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
/**
 * Test that $setWindowFields respects collation for sorting, partitioning, and in the window
 * function.
 */
(function() {
"use strict";

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

assert.commandWorked(coll.insert({key: "02", val: "2"}));
assert.commandWorked(coll.insert({key: "2", val: "10"}));
assert.commandWorked(coll.insert({key: "10", val: "3"}));

// Numeric ordering compares numeric strings as numbers, so "10" > "2".
const collation = {
    collation: {locale: "en_US", numericOrdering: true}
};

// Test that sortBy respects collation.
let results =
    coll.aggregate(
            [{
                $setWindowFields: {
                    sortBy: {val: -1},
                    output:
                        {arr: {$push: "$val", window: {documents: ["unbounded", "unbounded"]}}}
                }
            }],
            collation)
        .toArray();
// Test document order before $_internalSetWindowFields rather than $setWindowFields output order.
assert.docEq(["10", "3", "2"], results[0].arr);

// Test that partitionBy and window function respect collation.
results =
    coll.aggregate(
            [
                {
                    $setWindowFields: {
                        partitionBy: "$key",
                        output: {
                            minStr: {$min: "$val", window: {documents: ["unbounded", "unbounded"]}},
                            maxStr: {$max: "$val", window: {documents: ["unbounded", "unbounded"]}},
                        }
                    }
                },
                {$project: {_id: 0, val: 0}}
            ],
            collation)
        .toArray();
// Test that "02" and "2" are in the same partition, and the underlying min/max functions also
// respect the collation.
assert.sameMembers(results, [
    {key: "02", minStr: "2", maxStr: "10"},
    {key: "2", minStr: "2", maxStr: "10"},
    {key: "10", minStr: "3", maxStr: "3"},
]);

// Control group: no collation.
results = coll.aggregate([{
                  $setWindowFields: {
                      partitionBy: "$key",
                      output: {
                          minStr: {$min: "$val", window: {documents: ["unbounded", "unbounded"]}},
                          maxStr: {$max: "$val", window: {documents: ["unbounded", "unbounded"]}},
                      }
                  }
              }])
              .toArray();
assert.eq("02", results[0].key);
assert.eq("10", results[1].key);
assert.eq("2", results[2].key);
assert.eq("2", results[0].minStr);
assert.eq("2", results[0].maxStr);
})();