summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/column_scan_explain.js
blob: 2bb87cbfe6919548a42fdf7488b87e486e62016a (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
/**
 * Tests the explain support for the COLUMN_SCAN stage.
 * @tags: [
 *   # column store indexes are still under a feature flag and require full sbe
 *   uses_column_store_index,
 *   featureFlagColumnstoreIndexes,
 *   featureFlagSbeFull,
 * ]
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // For assertArrayEq
load("jstests/libs/analyze_plan.js");         // For planHasStage.
load("jstests/libs/sbe_util.js");             // For checkSBEEnabled.
load("jstests/libs/sbe_explain_helpers.js");  // For getSbePlanStages.

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));

// 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");

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

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

    // Verifying column fields.
    const columns = columnScan.columns;
    assert.eq(
        Object.keys(columns).length, 4, `Should access 4 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 = {
        "<<RowId Column>>": {"numNexts": docs.length, "numSeeks": 1, "usedInOutput": false},
        "_id": {"numNexts": docs.length - 1, "numSeeks": 1, "usedInOutput": true},
        "x": {"numNexts": docs.length - 1, "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["<<RowId Column>>"].numNexts + columns["<<RowId Column>>"].numSeeks +
                  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');
}());

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

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

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

    // Verifying column fields.
    const columns = columnScan.columns;
    assert.eq(
        Object.keys(columns).length, 3, `Should access 3 columns but accessed: ${tojson(columns)}`);
    const expectedColumns = {
        "<<RowId Column>>": {"numNexts": docs.length, "numSeeks": 1, "usedInOutput": false},
        "_id": {"numNexts": docs.length - 1, "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["<<RowId Column>>"].numNexts + columns["<<RowId Column>>"].numSeeks +
                  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');
}());

// Test the explain output for a scan on a 2-level nested field.
(function testMultipleNestedColumns() {
    const explain = coll.find({}, {'y.b.c': 1}).explain("executionStats");

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

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

    // Verifying column fields.
    const columns = columnScan.columns;
    assert.eq(
        Object.keys(columns).length, 3, `Should access 3 columns but accessed: ${tojson(columns)}`);
    const expectedColumns = {
        "<<RowId Column>>": {"numNexts": docs.length, "numSeeks": 1, "usedInOutput": false},
        "_id": {"numNexts": docs.length - 1, "numSeeks": 1, "usedInOutput": true},
        "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["_id"].numNexts + columns["_id"].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');
}());

// 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, "COLUMN_SCAN");
    assert.eq(columnScanStages.length, 1, `Could not find 'COLUMN_SCAN' 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');
}());
}());