summaryrefslogtreecommitdiff
path: root/jstests/aggregation/lookup_let_optimization.js
blob: 441419cf83821c7547d2065ad77c2034621477a3 (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
/**
 * This test ensures that stages dependent on a let variable optimizing to a constant in a $lookup
 * pipeline are evaluated correctly.
 * @tags: [requires_pipeline_optimization]
 */

load('jstests/aggregation/extras/utils.js');  // For assertArrayEq.
load("jstests/libs/fixture_helpers.js");      // For FixtureHelpers.

(function() {
"use strict";

const collName = "lookup_let_redact";
const coll = db[collName];
coll.drop();
assert.commandWorked(coll.insert([
    {_id: "true", test: true},
    {_id: "false", test: false},
]));

const admin = db.getSiblingDB("admin");

const setPipelineOptimizationMode = (mode) => {
    FixtureHelpers.runCommandOnEachPrimary(
        {db: admin, cmdObj: {configureFailPoint: 'disablePipelineOptimization', mode}});
};

const verifyAggregationForBothPipelineOptimizationModes = ({pipeline, expected}) => {
    // Verify results when pipeline optimization is disabled.
    setPipelineOptimizationMode('alwaysOn');
    assertArrayEq({actual: coll.aggregate(pipeline).toArray(), expected});

    // Verify results when pipeline optimization is enabled.
    setPipelineOptimizationMode('off');
    assertArrayEq({actual: coll.aggregate(pipeline).toArray(), expected});
};

// Get initial optimization mode.
const pipelineOptParameter = assert.commandWorked(
    db.adminCommand({getParameter: 1, "failpoint.disablePipelineOptimization": 1}));
const oldMode =
    pipelineOptParameter["failpoint.disablePipelineOptimization"].mode ? 'alwaysOn' : 'off';

// Verify $redact.
verifyAggregationForBothPipelineOptimizationModes({
    pipeline: [
        {$lookup: {
            from: collName,
            let: {iShouldPrune: "$test"},
            pipeline: [
                {$redact: {$cond: {if: "$$iShouldPrune", then: "$$PRUNE", else: "$$DESCEND"}}}
            ],
            as: "redacted"
        }}
    ],
    expected: [
        {_id: "true", test: true, redacted: []}, // Expect that documents were pruned.
        {_id: "false", test: false, redacted: [ // Expect that $redact descended instead.
            {_id: "true", test: true},
            {_id: "false", test: false} 
        ]}
    ]
});

// Verify $unionWith.
verifyAggregationForBothPipelineOptimizationModes({
    pipeline: [
        {$lookup: {
            from: collName,
            let: {iShouldMatch: "$test"},
            pipeline: [
                {$unionWith: {
                    coll: collName,
                    pipeline: [{$match: {$expr: {$eq: ["$$iShouldMatch", "$test"]}}}]
                }}
            ],
            as: "united"
        }}
    ],
    expected: [
        // $unionWith should match and include only the document with {test: true} in 'united'.
        {_id: "true", test: true, united: [
            {_id: "true", test: true},
            {_id: "true", test: true},
            {_id: "false", test: false},
        ]},
        // $unionWith should match and include only the document with {test: false} in 'united'.
        {_id: "false", test: false, united: [
            {_id: "false", test: false},
            {_id: "false", test: false},
            {_id: "true", test: true},
        ]}
    ]
});

// Reset optimization mode.
setPipelineOptimizationMode(oldMode);
}());