-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathdistinct_query_planner_md.js
70 lines (61 loc) · 2.43 KB
/
distinct_query_planner_md.js
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
/**
* Tests that we generate DISTINCT_SCANs for the specific cases treated by the query planner
* (sort that is required only for distinct scan plans and manual covered distinct scan
* construction).
*
* @tags: [
* featureFlagShardFilteringDistinctScan,
* requires_fcv_82
* ]
*/
import {section, subSection} from "jstests/libs/pretty_md.js";
import {outputAggregationPlanAndResults} from "jstests/libs/query/golden_test_utils.js";
const coll = db[jsTestName()];
section("Sort Pattern Added for $groupByDistinctScan");
subSection("Suitable Index for Sort => Distinct Scan");
coll.drop();
coll.createIndex({a: 1, b: 1});
coll.insertMany([{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 3}]);
outputAggregationPlanAndResults(
coll, [{$group: {_id: "$a", accum: {$top: {output: "$b", sortBy: {a: 1, b: 1}}}}}]);
subSection("Suitable Index for Sort (Inverse Order) => Distinct Scan");
coll.drop();
coll.insertMany([{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 3}]);
coll.createIndex({a: -1, b: -1});
outputAggregationPlanAndResults(
coll, [{$group: {_id: "$a", accum: {$top: {output: "$b", sortBy: {a: 1, b: 1}}}}}]);
subSection("No Suitable Index for Sort => No Distinct Scan and No Blocking Sort");
coll.drop();
coll.createIndex({a: 1});
coll.insertMany([{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 3}]);
outputAggregationPlanAndResults(
coll, [{$group: {_id: "$a", accum: {$top: {output: "$b", sortBy: {a: 1, b: 1}}}}}]);
subSection("Suitable Index for Filter but Not for Sort => No Distinct Scan and No Blocking Sort");
coll.drop();
coll.insertMany([
{a: 1, b: 4},
{a: 3, b: 8},
{a: 5, b: 6},
{a: 5, b: 7},
{a: 5, b: 4},
{a: 6, b: 7},
{a: 6, b: 8},
{a: 7, b: 9},
{a: 7, b: 3}
]);
coll.createIndex({a: 1});
outputAggregationPlanAndResults(coll, [
{$match: {a: {$gt: 3}}},
{$group: {_id: "$a", accum: {$top: {output: "$b", sortBy: {a: 1, b: 1}}}}}
]);
section("Construction of Distinct Scan when No Sort and No Filter");
subSection("$group Stage with no $sort Stage and with suitable index => DISTINCT_SCAN");
coll.drop();
coll.createIndex({a: 1});
coll.insertMany([{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 3}]);
outputAggregationPlanAndResults(coll, [{$group: {_id: "$a"}}]);
subSection("$group Stage with no $sort Stage and with no suitable index => No DISTINCT_SCAN");
coll.drop();
coll.createIndex({b: 1, a: 1});
coll.insertMany([{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 3}]);
outputAggregationPlanAndResults(coll, [{$group: {_id: "$a"}}]);