summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_lastpoint_top.js
blob: 14260a6efdaf11411c7c26959cf6f25ce32044a6 (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
/**
 * Tests the optimization of "lastpoint"-type queries on time-series collections.
 *
 * The test runs commands that are not allowed with security token: top.
 * @tags: [
 *   not_allowed_with_security_token,
 *   # Explain of a resolved view must be executed by mongos.
 *   directly_against_shardsvrs_incompatible,
 *   # Testing last point optimization.
 *   requires_pipeline_optimization,
 *   # Refusing to run a test that issues an aggregation command with explain because it may return
 *   # incomplete results if interrupted by a stepdown.
 *   does_not_support_stepdowns,
 *   # We need a timeseries collection.
 *   requires_timeseries,
 * ]
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");
load("jstests/core/timeseries/libs/timeseries_agg_helpers.js");
load("jstests/core/timeseries/libs/timeseries_lastpoint_helpers.js");
load("jstests/libs/analyze_plan.js");
load("jstests/libs/feature_flag_util.js");

const testDB = TimeseriesAggTests.getTestDb();
assert.commandWorked(testDB.dropDatabase());

// Do not run the rest of the tests if the lastpoint optimization is disabled.
if (!FeatureFlagUtil.isEnabled(db, "LastPointQuery")) {
    return;
}

/**
 * Returns a lastpoint $group stage of the form:
 * {$group: {
 *      _id: "$tags.hostid",
 *      mostRecent: {$topN: {
 *          n: 1, sortBy, output: {usage_user: "$usage_user", ...}
 *      }}
 * }}
 */
function getGroupStage({time, sortBy, n, extraFields = []}) {
    let output = {};
    for (const f of extraFields.concat(["usage_user", "usage_guest", "usage_idle"])) {
        output[f] = {[f]: "$" + f};
    }

    const accumulator = ((time < 0) ? "$top" : "$bottom");
    const mostRecent =
        n ? {[accumulator + "N"]: {sortBy, output, n}} : {[accumulator]: {sortBy, output}};
    return {$group: {_id: "$tags.hostid", mostRecent}};
}

{
    const [tsColl, observerColl] = createBoringCollections();
    testAllTimeMetaDirections(
        tsColl, observerColl, ({time, index, canUseDistinct, canSortOnTimeUseDistinct}) => {
            const expectCollscanNoSort = ({explain}) =>
                expectCollScan({explain, noSortInCursor: true});

            // Try both $top/$bottom and $topN/$bottomN variations of the rewrite.
            return [1, undefined].flatMap(n => {
                const groupStage = getGroupStage({time, sortBy: index, n});
                const getTestWithMatch = (matchStage, precedingFilter) => {
                    return {
                        precedingFilter,
                        pipeline: [matchStage, groupStage],
                        expectStageWithIndex: (canUseDistinct ? expectDistinctScan : expectIxscan),
                    };
                };

                return [
                    // Test pipeline without a preceding $match stage with sort only on time.
                    {
                        pipeline: [getGroupStage({time, sortBy: {time}, n})],
                        expectStageWithIndex:
                            (canSortOnTimeUseDistinct ? expectDistinctScan : expectCollScan),
                    },

                    // Test pipeline without a preceding $match stage with a sort on the index.
                    {
                        pipeline: [groupStage],
                        expectStageWithIndex:
                            (canUseDistinct ? expectDistinctScan : expectCollScan),
                    },

                    // Test pipeline with a projection to ensure that we correctly evaluate
                    // computedMetaProjFields in the rewrite. Note that we can't get a DISTINCT_SCAN
                    // here due to the projection.
                    {
                        pipeline: [
                            {$set: {abc: {$add: [1, "$tags.hostid"]}}},
                            getGroupStage({time, sortBy: index, n, extraFields: ["abc"]}),
                        ],
                        expectStageWithIndex: expectCollscanNoSort,
                        expectStageNoIndex: expectCollscanNoSort,
                    },

                    // Test pipeline with an equality $match stage.
                    getTestWithMatch({$match: {"tags.hostid": 0}}, {"meta.hostid": {$eq: 0}}),

                    // Test pipeline with an inequality $match stage.
                    getTestWithMatch({$match: {"tags.hostid": {$ne: 0}}},
                                     {"meta.hostid": {$not: {$eq: 0}}}),

                    // Test pipeline with a $match stage that uses a $gt query.
                    getTestWithMatch({$match: {"tags.hostid": {$gt: 5}}},
                                     {"meta.hostid": {$gt: 5}}),

                    // Test pipeline with a $match stage that uses a $lt query.
                    getTestWithMatch({$match: {"tags.hostid": {$lt: 5}}},
                                     {"meta.hostid": {$lt: 5}}),
                ];
            });
        });
}

// Test pipeline without a preceding $match stage which has an extra idle measurement. This verifies
// that the query rewrite correctly returns missing fields.
{
    const [tsColl, observerColl] = createBoringCollections(true /* includeIdleMeasurements */);
    testAllTimeMetaDirections(
        tsColl, observerColl, ({canUseDistinct, time, index}) => [1, undefined].map(n => {
            return {
                pipeline: [getGroupStage({time, sortBy: index, n})],
                expectStageWithIndex: (canUseDistinct ? expectDistinctScan : expectCollScan),
            };
        }));
}

// Test interesting metaField values.
{
    const [tsColl, observerColl] = createInterestingCollections();
    const expectIxscanNoSort = ({explain}) => expectIxscan({explain, noSortInCursor: true});

    // Verifies that the '_id' of each group matches one of the equivalent '_id' values.
    const mapToEquivalentIdStage = getMapInterestingValuesToEquivalentsStage();

    testAllTimeMetaDirections(tsColl, observerColl, ({
                                                        canUseDistinct,
                                                        canSortOnTimeUseDistinct,
                                                        time,
                                                        index
                                                    }) => {
        return [1, undefined].flatMap(
            n => [
                // Test pipeline with sort only on time and interesting metaField values.
                {
                    pipeline: [getGroupStage({time, sortBy: {time}, n}), mapToEquivalentIdStage],
                    // We get an index scan here because the index on interesting values is
                    // multikey.
                    expectStageWithIndex:
                        (canSortOnTimeUseDistinct ? expectIxscanNoSort : expectCollScan),
                },
                // Test pipeline without a preceding $match stage and interesting metaField values.
                {
                    pipeline: [getGroupStage({time, sortBy: index, n}), mapToEquivalentIdStage],
                    // We get an index scan here because the index on interesting values is
                    // multikey, so we cannot have a DISTINCT_SCAN.
                    expectStageWithIndex: (canUseDistinct ? expectIxscanNoSort : expectCollScan),
                },
        ]);
    });
}
})();