summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/lookup_pushdown_semantics.js
blob: f03da9f76c0089605bf66f7981bba5013281f509 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * Tests correctness of output of pushing $lookup into the find layer.
 */
(function() {
"use strict";

load("jstests/libs/sbe_util.js");  // For 'checkSBEEnabled()'.
load("jstests/aggregation/extras/utils.js");

// Standalone cases.
const conn = MongoRunner.runMongod({setParameter: "featureFlagSBELookupPushdown=true"});
assert.neq(null, conn, "mongod was unable to start up");
const db = conn.getDB("lookup_pushdown");
if (!checkSBEEnabled(db, ["featureFlagSBELookupPushdown"])) {
    jsTestLog("Skipping test because either the sbe lookup pushdown feature flag is disabled or" +
              " sbe itself is disabled");
    MongoRunner.stopMongod(conn);
    return;
}

const localColl = db["lookup_pushdown_local"];
const foreignColl = db["lookup_pushdown_foreign"];

/**
 * Executes $lookup with exactly one record in the foreign collection, so we don't need to check the
 * content of the "as" field but only that it's not empty for local records with ids in
 * 'idsExpectToMatch'.
 */
function runTest_SingleForeignRecord({
    testDescription,
    localRecords,
    localField,
    foreignRecord,
    foreignField,
    foreignIndex,
    idsExpectedToMatch
}) {
    assert('object' === typeof (foreignRecord) && !Array.isArray(foreignRecord),
           "foreignRecord should be a single document");

    localColl.drop();
    assert.commandWorked(localColl.insert(localRecords));

    foreignColl.drop();
    assert.commandWorked(foreignColl.insert(foreignRecord));

    if (foreignIndex) {
        assert.commandWorked(foreignColl.createIndex(foreignIndex));
    }

    const results = localColl.aggregate([{
        $lookup: {
            from: foreignColl.getName(),
            localField: localField,
            foreignField: foreignField,
            as: "matched"
        }
    }]).toArray();

    // Build the array of ids for the results that have non-empty array in the "matched" field.
    const matchedIds = results
                           .filter(function(x) {
                               return tojson(x.matched) != tojson([]);
                           })
                           .map(x => (x._id));

    // Order of the elements within the arrays is not significant for 'assertArrayEq'.
    assertArrayEq({
        actual: matchedIds,
        expected: idsExpectedToMatch,
        extraErrorMsg: " **TEST** " + testDescription
    });
}

/**
 * Executes $lookup with exactly one record in the local collection and checks that the "as" field
 * for it contains documents with ids from `idsExpectedToMatch`.
 */
function runTest_SingleLocalRecord({
    testDescription,
    localRecord,
    localField,
    foreignRecords,
    foreignField,
    foreignIndex,
    idsExpectedToMatch
}) {
    assert('object' === typeof (localRecord) && !Array.isArray(localRecord),
           "localRecord should be a single document");

    localColl.drop();
    assert.commandWorked(localColl.insert(localRecord));

    foreignColl.drop();
    assert.commandWorked(foreignColl.insert(foreignRecords));

    if (foreignIndex) {
        assert.commandWorked(foreignColl.createIndex(foreignIndex));
    }

    const results = localColl.aggregate([{
        $lookup: {
            from: foreignColl.getName(),
            localField: localField,
            foreignField: foreignField,
            as: "matched"
        }
    }]).toArray();
    assert.eq(1, results.length);

    // Extract matched foreign ids from the "matched" field.
    const matchedIds = results[0].matched.map(x => x._id);

    // Order of the elements within the arrays is not significant for 'assertArrayEq'.
    assertArrayEq({
        actual: matchedIds,
        expected: idsExpectedToMatch,
        extraErrorMsg: " **TEST** " + testDescription
    });
}

(function testMatchingTopLevelFieldToScalar() {
    const docs = [
        {_id: 0, a: NumberInt(0)},
        {_id: 1, a: 3.14},
        {_id: 2, a: NumberDecimal(3.14)},
        {_id: 3, a: "abc"},
    ];

    docs.forEach(doc => {
        runTest_SingleForeignRecord({
            testDescription:
                "Top-level field in local and top-level scalar in foreign with index on foreign field and produces single match",
            localRecords: docs,
            localField: "a",
            foreignRecord: {b: doc.a},
            foreignField: "b",
            foreignIndex: {b: 1},
            idsExpectedToMatch: [doc._id]
        });
        runTest_SingleLocalRecord({
            testDescription:
                "Top-level scalar in local and top-level field in foreign with index on foreign field and produces single match",
            localRecord: {b: doc.a},
            localField: "b",
            foreignRecords: docs,
            foreignField: "a",
            foreignIndex: {a: 1},
            idsExpectedToMatch: [doc._id]
        });
    });

    runTest_SingleForeignRecord({
        testDescription:
            "Top-level field in local and top-level scalar in foreign with index on foreign field and produces no match",
        localRecords: docs,
        localField: "a",
        foreignRecord: {b: 'xxx'},
        foreignField: "b",
        foreignIndex: {b: 1},
        idsExpectedToMatch: []
    });
    runTest_SingleLocalRecord({
        testDescription:
            "Top-level scalar in local and top-level field in foreign with index on foreign field and produces no match",
        localRecord: {b: 'xxx'},
        localField: "b",
        foreignRecords: docs,
        foreignField: "a",
        foreignIndex: {a: 1},
        idsExpectedToMatch: []
    });
})();

MongoRunner.stopMongod(conn);
}());