diff options
author | James Wahlin <james@mongodb.com> | 2019-01-25 12:24:17 -0500 |
---|---|---|
committer | James Wahlin <james@mongodb.com> | 2019-01-25 14:41:06 -0500 |
commit | 9b28edefa6d92d9b2d2bf0dbcf55dc38766b016a (patch) | |
tree | 22025a719e5ac7a7d1912e662d8e7b0b320057cf | |
parent | cb7c8ceba4bf7d6007a250799bfa35c129dd3e58 (diff) | |
download | mongo-9b28edefa6d92d9b2d2bf0dbcf55dc38766b016a.tar.gz |
SERVER-39198 modernize distinct_index1.js & assert on write error
-rw-r--r-- | jstests/core/distinct_index1.js | 129 |
1 files changed, 68 insertions, 61 deletions
diff --git a/jstests/core/distinct_index1.js b/jstests/core/distinct_index1.js index 1fc7288c812..bd1822b7a8c 100644 --- a/jstests/core/distinct_index1.js +++ b/jstests/core/distinct_index1.js @@ -1,74 +1,81 @@ -// @tags: [assumes_balancer_off] -t = db.distinct_index1; -t.drop(); +/** + * Analyzes execution stats for indexed distinct. + * @tags: [assumes_balancer_off] + */ +(function() { + load("jstests/libs/analyze_plan.js"); // For getPlanStage. -load("jstests/libs/analyze_plan.js"); + const coll = db.distinct_index1; + coll.drop(); -function r(x) { - return Math.floor(Math.sqrt(x * 123123)) % 10; -} + function getHash(num) { + return Math.floor(Math.sqrt(num * 123123)) % 10; + } -function d(k, q) { - return t.explain("executionStats").distinct(k, q || {}); -} + function getDistinctExplainWithExecutionStats(field, query) { + const explain = coll.explain("executionStats").distinct(field, query || {}); + assert(explain.hasOwnProperty("executionStats"), explain); + return explain; + } -for (i = 0; i < 1000; i++) { - o = {a: r(i * 5), b: r(i)}; - t.insert(o); -} + const bulk = coll.initializeUnorderedBulkOp(); + for (let i = 0; i < 1000; i++) { + bulk.insert({a: getHash(i * 5), b: getHash(i)}); + } + assert.commandWorked(bulk.execute()); -x = d("a"); -// Collection scan looks at all 1000 documents and gets 1000 -// distinct values. Looks at 0 index keys. -assert.eq(1000, x.executionStats.nReturned); -assert.eq(0, x.executionStats.totalKeysExamined); -assert.eq(1000, x.executionStats.totalDocsExamined); + let explain = getDistinctExplainWithExecutionStats("a"); + // Collection scan looks at all 1000 documents and gets 1000 distinct values. Looks at 0 index + // keys. + assert.eq(1000, explain.executionStats.nReturned); + assert.eq(0, explain.executionStats.totalKeysExamined); + assert.eq(1000, explain.executionStats.totalDocsExamined); -x = d("a", {a: {$gt: 5}}); -// Collection scan looks at all 1000 documents and gets 398 -// distinct values which match the query. Looks at 0 index keys. -assert.eq(398, x.executionStats.nReturned); -assert.eq(0, x.executionStats.totalKeysExamined); -assert.eq(1000, x.executionStats.totalDocsExamined); + explain = getDistinctExplainWithExecutionStats("a", {a: {$gt: 5}}); + // Collection scan looks at all 1000 documents and gets 398 distinct values which match the + // query. Looks at 0 index keys. + assert.eq(398, explain.executionStats.nReturned); + assert.eq(0, explain.executionStats.totalKeysExamined); + assert.eq(1000, explain.executionStats.totalDocsExamined); -x = d("b", {a: {$gt: 5}}); -// Collection scan looks at all 1000 documents and gets 398 -// distinct values which match the query. Looks at 0 index keys. -assert.eq(398, x.executionStats.nReturned); -assert.eq(0, x.executionStats.totalKeysExamined); -assert.eq(1000, x.executionStats.totalDocsExamined); + explain = getDistinctExplainWithExecutionStats("b", {a: {$gt: 5}}); + // Collection scan looks at all 1000 documents and gets 398 distinct values which match the + // query. Looks at 0 index keys. + assert.eq(398, explain.executionStats.nReturned); + assert.eq(0, explain.executionStats.totalKeysExamined); + assert.eq(1000, explain.executionStats.totalDocsExamined); -t.ensureIndex({a: 1}); + assert.commandWorked(coll.createIndex({a: 1})); -x = d("a"); -// There are only 10 values. We use the fast distinct hack and only examine each value once. -assert.eq(10, x.executionStats.nReturned); -assert.lte(10, x.executionStats.totalKeysExamined); + explain = getDistinctExplainWithExecutionStats("a"); + // There are only 10 values. We use the fast distinct hack and only examine each value once. + assert.eq(10, explain.executionStats.nReturned); + assert.lte(10, explain.executionStats.totalKeysExamined); -x = d("a", {a: {$gt: 5}}); -// Only 4 values of a are >= 5 and we use the fast distinct hack. -assert.eq(4, x.executionStats.nReturned); -assert.eq(4, x.executionStats.totalKeysExamined); -assert.eq(0, x.executionStats.totalDocsExamined); + explain = getDistinctExplainWithExecutionStats("a", {a: {$gt: 5}}); + // Only 4 values of a are >= 5 and we use the fast distinct hack. + assert.eq(4, explain.executionStats.nReturned); + assert.eq(4, explain.executionStats.totalKeysExamined); + assert.eq(0, explain.executionStats.totalDocsExamined); -x = d("b", {a: {$gt: 5}}); -// We can't use the fast distinct hack here because we're distinct-ing over 'b'. -assert.eq(398, x.executionStats.nReturned); -assert.eq(398, x.executionStats.totalKeysExamined); -assert.eq(398, x.executionStats.totalDocsExamined); + explain = getDistinctExplainWithExecutionStats("b", {a: {$gt: 5}}); + // We can't use the fast distinct hack here because we're distinct-ing over 'b'. + assert.eq(398, explain.executionStats.nReturned); + assert.eq(398, explain.executionStats.totalKeysExamined); + assert.eq(398, explain.executionStats.totalDocsExamined); -// Test that a distinct over a trailing field of the index can be covered. -t.dropIndexes(); -t.ensureIndex({a: 1, b: 1}); -x = d("b", {a: {$gt: 5}, b: {$gt: 5}}); -printjson(x); -assert.lte(x.executionStats.nReturned, 171); -assert.eq(0, x.executionStats.totalDocsExamined); + // Test that a distinct over a trailing field of the index can be covered. + assert.commandWorked(coll.dropIndexes()); + assert.commandWorked(coll.createIndex({a: 1, b: 1})); + explain = getDistinctExplainWithExecutionStats("b", {a: {$gt: 5}, b: {$gt: 5}}); + assert.lte(explain.executionStats.nReturned, 171); + assert.eq(0, explain.executionStats.totalDocsExamined); -// Should use an index scan over the hashed index. -t.dropIndexes(); -t.ensureIndex({a: "hashed"}); -x = d("a", {$or: [{a: 3}, {a: 5}]}); -assert.eq(188, x.executionStats.nReturned); -var indexScanStage = getPlanStage(x.executionStats.executionStages, "IXSCAN"); -assert.eq("hashed", indexScanStage.keyPattern.a); + // Should use an index scan over the hashed index. + assert.commandWorked(coll.dropIndexes()); + assert.commandWorked(coll.createIndex({a: "hashed"})); + explain = getDistinctExplainWithExecutionStats("a", {$or: [{a: 3}, {a: 5}]}); + assert.eq(188, explain.executionStats.nReturned); + const indexScanStage = getPlanStage(explain.executionStats.executionStages, "IXSCAN"); + assert.eq("hashed", indexScanStage.keyPattern.a); +})(); |