summaryrefslogtreecommitdiff
path: root/jstests/sharding/agg_project_limit_pipe_split.js
diff options
context:
space:
mode:
authorjannaerin <golden.janna@gmail.com>2017-11-08 10:46:52 -0500
committerjannaerin <golden.janna@gmail.com>2017-12-07 15:57:06 -0500
commitbbebcbfde994ec14b9fabfe17779cfb5adcda211 (patch)
tree33bfffef16ac967e8e8a9733a0c55aef936c1e0e /jstests/sharding/agg_project_limit_pipe_split.js
parent09da9900b81ac80a422747f934ccc08813590d63 (diff)
downloadmongo-bbebcbfde994ec14b9fabfe17779cfb5adcda211.tar.gz
SERVER-24981 Rewrite $limit optimization
Diffstat (limited to 'jstests/sharding/agg_project_limit_pipe_split.js')
-rw-r--r--jstests/sharding/agg_project_limit_pipe_split.js82
1 files changed, 82 insertions, 0 deletions
diff --git a/jstests/sharding/agg_project_limit_pipe_split.js b/jstests/sharding/agg_project_limit_pipe_split.js
new file mode 100644
index 00000000000..010cd46c46e
--- /dev/null
+++ b/jstests/sharding/agg_project_limit_pipe_split.js
@@ -0,0 +1,82 @@
+// Tests that the correct number of results are returned when $limit is coalesced with $sort.
+(function() {
+ "use strict";
+ load("jstests/libs/analyze_plan.js");
+
+ const shardingTest = new ShardingTest({shards: 2});
+ const db = shardingTest.getDB("project_limit");
+ const coll = db.project_limit_pipe_split;
+ coll.drop();
+ assert.commandWorked(shardingTest.s0.adminCommand({enableSharding: db.getName()}));
+ assert.commandWorked(
+ shardingTest.s0.adminCommand({shardCollection: coll.getFullName(), key: {_id: "hashed"}}));
+ const bulkOp = coll.initializeOrderedBulkOp();
+ for (let i = 0; i < 400; ++i) {
+ bulkOp.insert({x: i, y: ["a", "b", "c"], z: Math.floor(i / 12)});
+ }
+ assert.writeOK(bulkOp.execute());
+
+ let agg = coll.aggregate([
+ {$match: {$or: [{z: 9}, {z: 10}]}},
+ {$sort: {x: -1}},
+ {$project: {x: 1, y: 1, z: 1, _id: 0}},
+ {$limit: 6},
+ ]);
+ assert.eq(
+ [
+ {"x": 131, "y": ["a", "b", "c"], "z": 10},
+ {"x": 130, "y": ["a", "b", "c"], "z": 10},
+ {"x": 129, "y": ["a", "b", "c"], "z": 10},
+ {"x": 128, "y": ["a", "b", "c"], "z": 10},
+ {"x": 127, "y": ["a", "b", "c"], "z": 10},
+ {"x": 126, "y": ["a", "b", "c"], "z": 10}
+ ],
+ agg.toArray());
+
+ agg = coll.aggregate([
+ {$sort: {x: 1}},
+ {$redact: "$$KEEP"},
+ {$project: {x: 1, y: 1, z: 1, _id: 0}},
+ {$limit: 6}
+ ]);
+ assert.eq(
+ [
+ {"x": 0, "y": ["a", "b", "c"], "z": 0},
+ {"x": 1, "y": ["a", "b", "c"], "z": 0},
+ {"x": 2, "y": ["a", "b", "c"], "z": 0},
+ {"x": 3, "y": ["a", "b", "c"], "z": 0},
+ {"x": 4, "y": ["a", "b", "c"], "z": 0},
+ {"x": 5, "y": ["a", "b", "c"], "z": 0}
+ ],
+ agg.toArray());
+
+ agg = coll.aggregate(
+ [{$sort: {x: -1}}, {$skip: 399}, {$project: {x: 1, y: 1, z: 1, _id: 0}}, {$limit: 6}]);
+ assert.eq([{"x": 0, "y": ["a", "b", "c"], "z": 0}], agg.toArray());
+
+ agg = coll.aggregate(
+ [{$sort: {x: -1}}, {$project: {x: 1, y: 1, z: 1, _id: 0}}, {$skip: 401}, {$limit: 6}]);
+ assert.eq(0, agg.itcount());
+
+ agg = coll.aggregate([
+ {$sort: {x: -1}},
+ {$skip: 4},
+ {$project: {x: 1, y: 1, z: 1, _id: 0}},
+ {$skip: 3},
+ {$limit: 30},
+ {$skip: 3},
+ {$limit: 6},
+ ]);
+ assert.eq(
+ [
+ {"x": 389, "y": ["a", "b", "c"], "z": 32},
+ {"x": 388, "y": ["a", "b", "c"], "z": 32},
+ {"x": 387, "y": ["a", "b", "c"], "z": 32},
+ {"x": 386, "y": ["a", "b", "c"], "z": 32},
+ {"x": 385, "y": ["a", "b", "c"], "z": 32},
+ {"x": 384, "y": ["a", "b", "c"], "z": 32}
+ ],
+ agg.toArray());
+
+ shardingTest.stop();
+})(); \ No newline at end of file