summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/columnstore_index_rowstore_settings.js
blob: 22a2475cdc05cc7e53ca68de522fde455b1b14a2 (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
/**
 * Tests for settings of the fallback to the rowstore scanning when CSI cannot be used to
 * reconstruct the result of a query.
 *
 * @tags: [
 *   # column store indexes are still under a feature flag
 *   featureFlagColumnstoreIndexes
 * ]
 */

(function() {
'use strict';

load("jstests/libs/columnstore_util.js");  // For setUpServerForColumnStoreIndexTest.

const mongod = MongoRunner.runMongod({});
const db = mongod.getDB("test");

if (!setUpServerForColumnStoreIndexTest(db)) {
    MongoRunner.stopMongod(mongod);
    return;
}

const coll = db.columnstore_index_rowstore_settings;

const defaultMin = assert
                       .commandWorked(db.adminCommand(
                           {getParameter: 1, internalQueryColumnRowstoreScanMinBatchSize: 1}))
                       .internalQueryColumnRowstoreScanMinBatchSize;
const defaultMax = assert
                       .commandWorked(db.adminCommand(
                           {getParameter: 1, internalQueryColumnRowstoreScanMaxBatchSize: 1}))
                       .internalQueryColumnRowstoreScanMaxBatchSize;

function setBatchSizeMinAndMax({min: min, max: max}) {
    assert.commandWorked(
        db.adminCommand({setParameter: 1, internalQueryColumnRowstoreScanMinBatchSize: min}));

    assert.commandWorked(
        db.adminCommand({setParameter: 1, internalQueryColumnRowstoreScanMaxBatchSize: max}));
}

function getRowstoreStats(explainExec) {
    return {
        fetches:
            parseInt(JSON.stringify(explainExec).split('"numRowStoreFetches":')[1].split(",")[0]),
        scans: parseInt(JSON.stringify(explainExec).split('"numRowStoreScans":')[1].split(",")[0])
    };
}

(function testSuppressScan() {
    setBatchSizeMinAndMax({min: 0, max: defaultMax});

    const docs = [
        {obj: {x: 40}},
        {obj: {x: 41}},
        {obj: {x: 42}},
        {obj: 43},
        {obj: {x: 44}},
    ];

    coll.drop();
    coll.insert(docs);
    assert.commandWorked(coll.createIndex({"$**": "columnstore"}));

    const explain = coll.explain("executionStats").find({}, {obj: 1}).finish();
    const stats = getRowstoreStats(explain);

    assert.eq(0, stats.scans, "Scanning of the row store should be suppressed. " + tojson(explain));
    assert.eq(docs.length - 1,
              stats.fetches,
              "All records with sub-object should be fetched and only those. " + tojson(explain));
})();

(function testScanToEof() {
    setBatchSizeMinAndMax({min: 5, max: defaultMax});

    const docs = [
        {obj: 40},
        {obj: {x: 41}},  // triggers fetch and switching into the scan mode for the remaining docs
        {obj: 42},
        {obj: 43},
        {obj: 44},
    ];

    coll.drop();
    coll.insert(docs);
    assert.commandWorked(coll.createIndex({"$**": "columnstore"}));

    const explain = coll.explain("executionStats").find({}, {obj: 1}).finish();
    const stats = getRowstoreStats(explain);

    assert.eq(1,
              stats.fetches,
              "The only 'bad' document should trigger a single fetch. " + tojson(explain));
    assert.eq(
        3, stats.scans, "All documents after the 'bad' one should be scanned. " + tojson(explain));
})();

(function testAllReadsMustUseRowstore() {
    setBatchSizeMinAndMax({min: 2, max: defaultMax});

    const count = 1000;
    coll.drop();
    for (let i = 0; i < count; i++) {
        coll.insert({obj: {x: i}});
    }
    assert.commandWorked(coll.createIndex({"$**": "columnstore"}));

    const explain = coll.explain("executionStats").find({}, {obj: 1}).finish();
    const stats = getRowstoreStats(explain);

    assert.eq(count,
              stats.fetches + stats.scans,
              "All docs should need reads from rowstore. " + tojson(explain));
    assert.lt(
        stats.fetches,
        20,
        "Number of fetches should be small due to exponential increase of the scanned batch size. " +
            tojson(explain));
})();

(function testRecoverAfterRowstoreScan() {
    setBatchSizeMinAndMax({min: 3, max: defaultMax});

    const docs = [
        {obj: 40},
        {obj: {x: 41}},  // triggers fetch and switching into the scan mode
        {obj: {x: 42}},  // it's a 'bad' doc but it will be scanned, not fetched
        {obj: 43},
        {obj: 44},  // triggers exit from the scan mode
        {obj: 45},
        {obj: {x: 46}},  // triggers fetch and switching into the scan mode
        {obj: 47},
        {obj: {x: 48}},  // it's a 'bad' doc but it will be scanned, not fetched
        {obj: 49},       // triggers exit from the scan mode
        {obj: 50},
    ];

    coll.drop();
    coll.insert(docs);
    assert.commandWorked(coll.createIndex({"$**": "columnstore"}));

    const explain = coll.explain("executionStats").find({}, {obj: 1}).finish();
    const stats = getRowstoreStats(explain);

    assert.eq(2, stats.fetches, "Expected number of fetches. " + tojson(explain));
    assert.eq(6, stats.scans, "Expected number of scans. " + tojson(explain));
})();

(function testScanBatchSizeResetAfterGoodRead() {
    setBatchSizeMinAndMax({min: 1, max: defaultMax});

    const docs = [
        {obj: 40},
        {obj: {x: 41}},  // triggers fetch and switching into the scan mode
        {obj: 42},       // scanned
        {obj: {x: 43}},  // 'bad' again -> fetch and grow batch size
        {obj: 44},
        {obj: 45},
        {obj: 46},
        {obj: 47},
        {obj: 48},
        {obj: 49},       // by now should have exited from the scan mode even with a bigger batch
        {obj: {x: 50}},  // triggers fetch and switching into the scan mode with the min batch size
        {obj: 51},       // scanned
        {obj: {x: 52}},  // checked and must trigger fetch (if the batch size isn't reset it would
                         // be scanned)
    ];

    coll.drop();
    coll.insert(docs);
    assert.commandWorked(coll.createIndex({"$**": "columnstore"}));

    const explain = coll.explain("executionStats").find({}, {obj: 1}).finish();
    const stats = getRowstoreStats(explain);

    assert.eq(4, stats.fetches, "Expected number of fetches. " + tojson(explain));
    assert.gte(stats.scans, 4, "Expected number of scans. " + tojson(explain));
})();

(function testNeverScanIfHavePerpathFilters() {
    setBatchSizeMinAndMax({min: defaultMin, max: defaultMax});

    const docs = [
        {a: 5, obj: 40},
        {a: 5, obj: {x: 41}},
        {a: 5, obj: {x: 42}},
        {a: 5, obj: {x: 43}},
        {a: 5, obj: 44},
    ];

    coll.drop();
    coll.insert(docs);
    assert.commandWorked(coll.createIndex({"$**": "columnstore"}));

    const explain = coll.explain("executionStats").find({a: 5}, {obj: 1}).finish();
    const stats = getRowstoreStats(explain);

    assert.eq(3, stats.fetches, "Expected number of fetches. " + tojson(explain));
    assert.eq(0, stats.scans, "Expected number of scans. " + tojson(explain));
})();

(function testMaxSizeOfBatch() {
    setBatchSizeMinAndMax({min: 1, max: 9});

    const count = 1000;
    coll.drop();
    for (let i = 0; i < count; i++) {
        coll.insert({obj: {x: i}});
    }
    assert.commandWorked(coll.createIndex({"$**": "columnstore"}));

    const explain = coll.explain("executionStats").find({}, {obj: 1}).finish();
    const stats = getRowstoreStats(explain);

    // All records have to be read from the row store. By default, we'd keep growing the batch size
    // until it gets pretty large and, essentially, we settle on just scanning the row store with
    // infrequent checks against the index. The checks come paired with fetches. So when the max is
    // set low, we should see many fetches.
    assert.eq(count,
              stats.fetches + stats.scans,
              "All docs have to be read from the rowstore one way or another. " + tojson(explain));
    assert.gt(stats.fetches, count / 10, "Expected number of fetches. " + tojson(explain));
})();

(function testMaxIsLowerThanMin() {
    setBatchSizeMinAndMax({min: 99, max: 3});

    const count = 1000;
    coll.drop();
    for (let i = 0; i < count; i++) {
        coll.insert({obj: {x: i}});
    }
    assert.commandWorked(coll.createIndex({"$**": "columnstore"}));

    const explain = coll.explain("executionStats").find({}, {obj: 1}).finish();
    const stats = getRowstoreStats(explain);

    // All records have to be read from the row store. When max is set below min, the batch size
    // will be fixed at min.
    assert.eq(count,
              stats.fetches + stats.scans,
              "All docs have to be read from the rowstore one way or another. " + tojson(explain));
    assert.eq(stats.fetches, count / 100, "Expected number of fetches. " + tojson(explain));
})();

MongoRunner.stopMongod(mongod);
}());