summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/column_scan_explain.js
blob: fe3e78190aa0c11a778d08647b4671261620ec7e (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
 * Tests the explain support for the COLUMN_SCAN stage.
 * @tags: [
 *   # Column store indexes are still under a feature flag and require full sbe.
 *   featureFlagColumnstoreIndexes,
 *   featureFlagSbeFull,
 * ]
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // For assertArrayEq
load("jstests/libs/sbe_explain_helpers.js");  // For getSbePlanStages.
load("jstests/libs/columnstore_util.js");     // For setUpServerForColumnStoreIndexTest.

if (!setUpServerForColumnStoreIndexTest(db)) {
    return;
}

const coll = db.column_scan_explain;
coll.drop();

assert.commandWorked(coll.createIndex({"$**": "columnstore"}));
const docs = [
    {_id: 0, x: 1, y: [{a: 2}, {a: 3}, {a: 4}]},
    {_id: 1, x: 1},
    {_id: 2, x: 1, y: [{b: 5}, {b: 6}, {b: 7}]},
    {_id: 3, x: 1, y: [{b: 5}, {b: [1, 2, {c: 5}]}, {c: 7}]},
    {_id: 4, x: 1, y: [{b: {c: 1}}]}
];
assert.commandWorked(coll.insertMany(docs, {ordered: false}));

// Test the explain output for a scan on two columns: one nested and one top-level.
(function testScanOnTwoColumns() {
    const explain = coll.find({}, {x: 1, 'y.a': 1}).explain("executionStats");

    // Validate that the plan was SBE and using a ColumnScan.
    const columnScanStages = getSbePlanStages(explain, "columnscan");
    assert.eq(columnScanStages.length, 1, `Could not find 'columnscan' stage: ${tojson(explain)}`);
    const columnScan = columnScanStages[0];

    assertArrayEq({
        actual: columnScan.paths,
        expected: ["_id", "x", "y.a"],
        extraErrorMsg: 'Paths used by column scan stage'
    });

    // Verify that the expected number of column fields were scanned.
    const columns = columnScan.columns;
    assert.eq(
        Object.keys(columns).length, 3, `Should access 3 columns but accessed: ${tojson(columns)}`);

    // We seek into each column once, when setting up the cursors. The dense column is the first
    // to hit EOF after iterating over all documents so other columns iterate at least one time
    // less.
    const expectedColumns = {
        "_id": {"numNexts": docs.length, "numSeeks": 1, "usedInOutput": true},
        "x": {"numNexts": docs.length, "numSeeks": 1, "usedInOutput": true},
        "y.a": {"numNexts": 1, "numSeeks": 1, "usedInOutput": true}
    };
    for (const [columnName, expectedObj] of Object.entries(expectedColumns)) {
        assert.eq(sortDoc(columns[columnName]),
                  sortDoc(expectedObj),
                  `Mismatching entry for column ${tojson(columnName)}`);
    }

    // Verifying parent column fields.
    const parentColumns = columnScan.parentColumns;
    assert.eq(Object.keys(parentColumns).length,
              1,
              `Should access 1 parent column but accessed: ${tojson(parentColumns)}`);
    // Expecting 4 lookups on the "y" parent column for the 3 docs which didn't have a "y.a"
    // value and 1 for an unsuccessful call to seek. We should not iterate over parent columns.
    assert.eq(sortDoc(parentColumns.y),
              {"numNexts": 0, "numSeeks": 4},
              'Mismatching entry for parent column "y"');

    // 'totalKeysExamined' should be equal to the sum of "next" and "seek" calls across all
    // columns.
    assert.eq(explain.executionStats.totalKeysExamined,
              columns["_id"].numNexts + columns["_id"].numSeeks + columns["x"].numNexts +
                  columns["x"].numSeeks + columns["y.a"].numNexts + columns["y.a"].numSeeks +
                  parentColumns["y"].numNexts + parentColumns["y"].numSeeks,
              `Mismatch in totalKeysExamined.`);

    assert.eq(columnScan.numRowStoreFetches, 0, 'Mismatch in numRowStoreFetches');
    assert.eq(columnScan.nReturned, docs.length, 'nReturned: should return all docs');

    // Validate QSN part.
    const columnScanPlanStages = getPlanStages(explain, "COLUMN_SCAN");
    assert.eq(
        columnScanPlanStages.length, 1, `Could not find 'COLUMN_SCAN' stage: ${tojson(explain)}`);
    assert(documentEq(columnScanPlanStages[0],
                      {"allFields": ["_id", "x", "y.a"], "extraFieldsPermitted": true},
                      false /* verbose */,
                      null /* valueComparator */,
                      ["stage", "planNodeId"]),
           `Mismatching column scan plan stage ${tojson(columnScanPlanStages[0])}`);
}());

// Test the explain output for a scan on a nonexistent field.
(function testNonexistentField() {
    const explain = coll.find({}, {z: 1}).explain("executionStats");

    // Validate that the plan was SBE and using a ColumnScan.
    const columnScanStages = getSbePlanStages(explain, "columnscan");
    assert.eq(columnScanStages.length, 1, `Could not find 'columnscan' stage: ${tojson(explain)}`);
    const columnScan = columnScanStages[0];

    assertArrayEq({
        actual: columnScan.paths,
        expected: ["_id", "z"],
        extraErrorMsg: 'Paths used by column scan stage'
    });

    // Verify that the expected number of column fields were scanned.
    const columns = columnScan.columns;
    assert.eq(
        Object.keys(columns).length, 2, `Should access 2 columns but accessed: ${tojson(columns)}`);
    const expectedColumns = {
        "_id": {"numNexts": docs.length, "numSeeks": 1, "usedInOutput": true},
        "z": {"numNexts": 0, "numSeeks": 1, "usedInOutput": true},
    };
    for (const [columnName, expectedObj] of Object.entries(expectedColumns)) {
        assert.eq(sortDoc(columns[columnName]),
                  sortDoc(expectedObj),
                  `Mismatching entry for column "${columnName}"`);
    }

    // Verifying parent column fields.
    const parentColumns = columnScan.parentColumns;
    assert.eq(parentColumns, {}, "Should not access parent columns");

    // 'totalKeysExamined' should be equal to the sum of "next" and "seek" calls across all
    // columns.
    assert.eq(explain.executionStats.totalKeysExamined,
              columns["_id"].numNexts + columns["_id"].numSeeks + columns["z"].numNexts +
                  columns["z"].numSeeks,
              `Mismatch in totalKeysExamined.`);

    assert.eq(columnScan.numRowStoreFetches, 0, 'Mismatch in numRowStoreFetches');
    assert.eq(columnScan.nReturned, docs.length, 'nReturned: should return all docs');

    // Validate QSN part.
    const columnScanPlanStages = getPlanStages(explain, "COLUMN_SCAN");
    assert.eq(
        columnScanPlanStages.length, 1, `Could not find 'COLUMN_SCAN' stage: ${tojson(explain)}`);
    assert(documentEq(columnScanPlanStages[0],
                      {"allFields": ["_id", "z"], "extraFieldsPermitted": false},
                      false /* verbose */,
                      null /* valueComparator */,
                      ["stage", "planNodeId"]),
           `Mismatching column scan plan stage ${tojson(columnScanPlanStages[0])}`);
}());

// Test the explain output for a scan on a 2-level nested field; and exclude the _id field to
// exercise explain for the hidden dense RowId column.
(function testMultipleNestedColumns() {
    const explain = coll.find({}, {'_id': 0, 'y.b.c': 1}).explain("executionStats");

    // Validate that the plan was SBE and using a ColumnScan.
    const columnScanStages = getSbePlanStages(explain, "columnscan");
    assert.eq(columnScanStages.length, 1, `Could not find 'columnscan' stage: ${tojson(explain)}`);
    const columnScan = columnScanStages[0];

    assertArrayEq({
        actual: columnScan.paths,
        expected: ["y.b.c"],
        extraErrorMsg: 'Paths used by column scan stage'
    });

    // Verify that the expected number of column fields were scanned.
    const columns = columnScan.columns;
    assert.eq(
        Object.keys(columns).length, 2, `Should access 2 columns but accessed: ${tojson(columns)}`);
    const expectedColumns = {
        "<<RowId Column>>": {"numNexts": docs.length, "numSeeks": 1, "usedInOutput": false},
        "y.b.c": {"numNexts": 1, "numSeeks": 1, "usedInOutput": true},
    };
    for (const [columnName, expectedObj] of Object.entries(expectedColumns)) {
        assert.eq(sortDoc(columns[columnName]),
                  sortDoc(expectedObj),
                  `Mismatching entry for column ${columnName}`);
    }

    // Verifying parent column fields.
    const parentColumns = columnScan.parentColumns;
    assert.eq(Object.keys(parentColumns).length,
              2,
              `Should access 1 parent column but accessed: ${tojson(parentColumns)}`);
    // Expecting 3 lookups on the "y" parent column for the 2 docs which didn't have a "y.b"
    // value and 1 unsuccessful call to seek. We should not iterate over parent columns.
    assert.eq(sortDoc(parentColumns.y),
              {"numNexts": 0, "numSeeks": 3},
              'Mismatching entry for parent column "y"');
    // Expecting 4 lookups on the "y.b" parent column for the 3 docs that didn't have a "y.b.c"
    // value and 1 unsuccessful call to seek.
    assert.eq(sortDoc(parentColumns['y.b']),
              {"numNexts": 0, "numSeeks": 4},
              'Mismatching entry for parent column "y.b"');

    // 'totalKeysExamined' should be equal to the sum of "next" and "seek" calls across all
    // columns.
    assert.eq(explain.executionStats.totalKeysExamined,
              columns["<<RowId Column>>"].numNexts + columns["<<RowId Column>>"].numSeeks +
                  columns["y.b.c"].numNexts + columns["y.b.c"].numSeeks +
                  parentColumns["y.b"].numNexts + parentColumns["y.b"].numSeeks +
                  parentColumns["y"].numNexts + parentColumns["y"].numSeeks,
              `Mismatch in totalKeysExamined.`);

    assert.eq(columnScan.numRowStoreFetches, 0, 'Mismatch in numRowStoreFetches');
    assert.eq(columnScan.nReturned, docs.length, 'nReturned: should return all docs');

    // Validate QSN part.
    const columnScanPlanStages = getPlanStages(explain, "COLUMN_SCAN");
    assert.eq(
        columnScanPlanStages.length, 1, `Could not find 'COLUMN_SCAN' stage: ${tojson(explain)}`);
    assert(documentEq(columnScanPlanStages[0],
                      {"allFields": ["y.b.c"], "extraFieldsPermitted": true},
                      false /* verbose */,
                      null /* valueComparator */,
                      ["stage", "planNodeId"]),
           `Mismatching column scan plan stage ${tojson(columnScanPlanStages[0])}`);
}());

// Test fallback to the row store.
(function testWithFallbackToRowstore() {
    const coll_rowstore = db.column_scan_explain_rowstore;
    coll_rowstore.drop();
    assert.commandWorked(coll_rowstore.createIndex({"$**": "columnstore"}));

    const docs_rowstore = [
        {_id: 0, x: {y: 42, z: 0}},
        {_id: 1, x: {y: {z: 0}}},  // fallback
        {_id: 2, x: [{y: 42}, 0]},
        {_id: 3, x: [{y: 42}, {z: 0}]},
        {_id: 4, x: [{y: [42, 43]}, {z: 0}]},
        {_id: 5, x: [{y: [42, {z: 0}]}, {z: 0}]},  // fallback
        {_id: 6, x: 42},
    ];
    coll_rowstore.insert(docs_rowstore);
    const explain = coll_rowstore.find({}, {_id: 0, "x.y": 1}).explain("executionStats");

    const columnScanStages = getSbePlanStages(explain, "columnscan");
    assert.eq(columnScanStages.length, 1, `Could not find 'columnscan' stage: ${tojson(explain)}`);
    const columnScan = columnScanStages[0];

    assert.eq(columnScan.numRowStoreFetches, 2, 'Mismatch in numRowStoreFetches');
    assert.eq(columnScan.nReturned, docs_rowstore.length, 'nReturned: should return all docs');
}());

// Test the QSN explain output for scans with different types of filters.
(function testScanWithFilters() {
    const explain =
        coll.find({x: 1, 'y.b': 5, $or: [{x: 2}, {'y.a': 3}]}, {_id: 0, x: 1}).explain();

    const columnScanPlanStages = getPlanStages(explain, "COLUMN_SCAN");
    assert.eq(
        columnScanPlanStages.length, 1, `Could not find 'COLUMN_SCAN' stage: ${tojson(explain)}`);
    assert(documentEq(columnScanPlanStages[0],
                      {
                          "allFields": ["x", "y.b", "y.a"],
                          "filtersByPath": {"x": {"$eq": 1}, "y.b": {"$eq": 5}},
                          "residualPredicate": {"$or": [{"x": {"$eq": 2}}, {"y.a": {"$eq": 3}}]},
                          "extraFieldsPermitted": true
                      },
                      false /* verbose */,
                      null /* valueComparator */,
                      ["stage", "planNodeId"]),
           `Mismatching column scan plan stage ${tojson(columnScanPlanStages[0])}`);
}());
}());