summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_lastpoint.js
blob: d3bdc66d3bd5dfda8e087831a376b5ebdff57c16 (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
/**
 * Tests the optimization of "lastpoint"-type queries on time-series collections.
 *
 * @tags: [
 *   does_not_support_stepdowns,
 *   does_not_support_transactions,
 *   requires_timeseries,
 *   requires_pipeline_optimization,
 *   requires_fcv_53,
 *   # TODO (SERVER-63590): Investigate presence of getmore tag in timeseries jstests.
 *   requires_getmore,
 *   # Explain of a resolved view must be executed by mongos.
 *   directly_against_shardsvrs_incompatible,
 * ]
 */
(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");

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",
 *       usage_user: {$first: "$usage_user"}, ...
 *  }}
 */
function getGroupStage(time, extraFields = []) {
    const accumulator = time > 0 ? "$last" : "$first";
    let innerGroup = {_id: "$tags.hostid"};
    for (const f of extraFields.concat(["usage_user", "usage_guest", "usage_idle"])) {
        innerGroup[f] = {[accumulator]: "$" + f};
    }
    return {$group: innerGroup};
}

{
    const [tsColl, observerColl] = createBoringCollections();
    testAllTimeMetaDirections(
        tsColl, observerColl, ({time, canUseDistinct, canSortOnTimeUseDistinct, index}) => {
            const groupStage = getGroupStage(time);
            const expectCollscanNoSort = ({explain}) =>
                expectCollScan({explain, noSortInCursor: true});
            const getTestWithMatch = (matchStage, precedingFilter) => {
                return {
                    precedingFilter,
                    pipeline: [matchStage, {$sort: index}, groupStage],
                    expectStageWithIndex: (canUseDistinct ? expectDistinctScan : expectIxscan),
                };
            };

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

                // Test pipeline without a preceding $match stage with a sort on the index.
                {
                    pipeline: [{$sort: index}, 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"]}}},
                        {$sort: index},
                        getGroupStage(time, ["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, index, time}) => [{
            pipeline: [{$sort: index}, getGroupStage(time)],
            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}) => {
            const groupStage = getGroupStage(time);
            return [
                // Test pipeline with sort only on time and interesting metaField values.
                {
                    pipeline: [{$sort: {time}}, groupStage, 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: [{$sort: index}, groupStage, 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),
                }
            ];
        });
}
})();