summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/optimizer/props.cpp
blob: c0079a78ed2f972bca2d49ebca6e07da6c498288 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
 *    Copyright (C) 2022-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/db/query/optimizer/props.h"
#include "mongo/db/query/optimizer/utils/utils.h"
#include "mongo/util/assert_util.h"

namespace mongo::optimizer::properties {

CollationRequirement::CollationRequirement(ProjectionCollationSpec spec) : _spec(std::move(spec)) {
    uassert(6624302, "Empty collation spec", !_spec.empty());

    ProjectionNameSet projections;
    for (const auto& entry : _spec) {
        uassert(6624021, "Repeated projection name", projections.insert(entry.first).second);
    }
}

bool CollationRequirement::operator==(const CollationRequirement& other) const {
    return _spec == other._spec;
}

const ProjectionCollationSpec& CollationRequirement::getCollationSpec() const {
    return _spec;
}

ProjectionCollationSpec& CollationRequirement::getCollationSpec() {
    return _spec;
}

bool CollationRequirement::hasClusteredOp() const {
    for (const auto& [projName, op] : _spec) {
        if (op == CollationOp::Clustered) {
            return true;
        }
    }
    return false;
}

ProjectionNameSet CollationRequirement::getAffectedProjectionNames() const {
    ProjectionNameSet result;
    for (const auto& entry : _spec) {
        result.insert(entry.first);
    }
    return result;
}

LimitSkipRequirement::LimitSkipRequirement(const int64_t limit, const int64_t skip)
    : _limit((limit < 0) ? kMaxVal : limit), _skip(skip) {}

bool LimitSkipRequirement::operator==(const LimitSkipRequirement& other) const {
    return _skip == other._skip && _limit == other._limit;
}

int64_t LimitSkipRequirement::getLimit() const {
    return _limit;
}

int64_t LimitSkipRequirement::getSkip() const {
    return _skip;
}

int64_t LimitSkipRequirement::getAbsoluteLimit() const {
    return hasLimit() ? (_skip + _limit) : kMaxVal;
}

ProjectionNameSet LimitSkipRequirement::getAffectedProjectionNames() const {
    return {};
}

bool LimitSkipRequirement::hasLimit() const {
    return _limit != kMaxVal;
}

ProjectionRequirement::ProjectionRequirement(ProjectionNameOrderPreservingSet projections)
    : _projections(std::move(projections)) {}

bool ProjectionRequirement::operator==(const ProjectionRequirement& other) const {
    return _projections.isEqualIgnoreOrder(other.getProjections());
}

ProjectionNameSet ProjectionRequirement::getAffectedProjectionNames() const {
    ProjectionNameSet result;
    for (const ProjectionName& projection : _projections.getVector()) {
        result.insert(projection);
    }
    return result;
}

const ProjectionNameOrderPreservingSet& ProjectionRequirement::getProjections() const {
    return _projections;
}

ProjectionNameOrderPreservingSet& ProjectionRequirement::getProjections() {
    return _projections;
}

DistributionAndProjections::DistributionAndProjections(DistributionType type)
    : DistributionAndProjections(type, {}) {}

DistributionAndProjections::DistributionAndProjections(DistributionType type,
                                                       ProjectionNameVector projectionNames)
    : _type(type), _projectionNames(std::move(projectionNames)) {
    uassert(6624096,
            "Must have projection names when distributed under hash or range partitioning",
            (_type != DistributionType::HashPartitioning &&
             _type != DistributionType::RangePartitioning) ||
                !_projectionNames.empty());
}

bool DistributionAndProjections::operator==(const DistributionAndProjections& other) const {
    return _type == other._type && _projectionNames == other._projectionNames;
}

DistributionRequirement::DistributionRequirement(
    DistributionAndProjections distributionAndProjections)
    : _distributionAndProjections(std::move(distributionAndProjections)),
      _disableExchanges(false) {}

bool DistributionRequirement::operator==(const DistributionRequirement& other) const {
    return _distributionAndProjections == other._distributionAndProjections &&
        _disableExchanges == other._disableExchanges;
}

const DistributionAndProjections& DistributionRequirement::getDistributionAndProjections() const {
    return _distributionAndProjections;
}

DistributionAndProjections& DistributionRequirement::getDistributionAndProjections() {
    return _distributionAndProjections;
}

ProjectionNameSet DistributionRequirement::getAffectedProjectionNames() const {
    ProjectionNameSet result;
    for (const ProjectionName& projectionName : _distributionAndProjections._projectionNames) {
        result.insert(projectionName);
    }
    return result;
}

bool DistributionRequirement::getDisableExchanges() const {
    return _disableExchanges;
}

void DistributionRequirement::setDisableExchanges(const bool disableExchanges) {
    _disableExchanges = disableExchanges;
}

IndexingRequirement::IndexingRequirement()
    : IndexingRequirement(IndexReqTarget::Complete, true /*dedupRID*/, {}) {}

IndexingRequirement::IndexingRequirement(IndexReqTarget indexReqTarget,
                                         bool dedupRID,
                                         GroupIdType satisfiedPartialIndexesGroupId)
    : _indexReqTarget(indexReqTarget),
      _dedupRID(dedupRID),
      _satisfiedPartialIndexesGroupId(std::move(satisfiedPartialIndexesGroupId)) {
    uassert(6624097,
            "Avoiding dedup is only allowed for Index target",
            _dedupRID || _indexReqTarget == IndexReqTarget::Index);
}

bool IndexingRequirement::operator==(const IndexingRequirement& other) const {
    return _indexReqTarget == other._indexReqTarget && _dedupRID == other._dedupRID &&
        _satisfiedPartialIndexesGroupId == other._satisfiedPartialIndexesGroupId;
}

ProjectionNameSet IndexingRequirement::getAffectedProjectionNames() const {
    // Specifically not returning ridProjectionName (even if present).
    return {};
}

IndexReqTarget IndexingRequirement::getIndexReqTarget() const {
    return _indexReqTarget;
}

bool IndexingRequirement::getDedupRID() const {
    return _dedupRID;
}

void IndexingRequirement::setDedupRID(const bool value) {
    _dedupRID = value;
}

GroupIdType IndexingRequirement::getSatisfiedPartialIndexesGroupId() const {
    return _satisfiedPartialIndexesGroupId;
}

RepetitionEstimate::RepetitionEstimate(const CEType estimate) : _estimate(estimate) {}

bool RepetitionEstimate::operator==(const RepetitionEstimate& other) const {
    return _estimate == other._estimate;
}

ProjectionNameSet RepetitionEstimate::getAffectedProjectionNames() const {
    return {};
}

CEType RepetitionEstimate::getEstimate() const {
    return _estimate;
}

LimitEstimate::LimitEstimate(const CEType estimate) : _estimate(estimate) {}

bool LimitEstimate::operator==(const LimitEstimate& other) const {
    return _estimate == other._estimate;
}

ProjectionNameSet LimitEstimate::getAffectedProjectionNames() const {
    return {};
}

bool LimitEstimate::hasLimit() const {
    return _estimate >= 0.0;
}

CEType LimitEstimate::getEstimate() const {
    return _estimate;
}

ProjectionAvailability::ProjectionAvailability(ProjectionNameSet projections)
    : _projections(std::move(projections)) {}

bool ProjectionAvailability::operator==(const ProjectionAvailability& other) const {
    return _projections == other._projections;
}

const ProjectionNameSet& ProjectionAvailability::getProjections() const {
    return _projections;
}

CardinalityEstimate::CardinalityEstimate(const CEType estimate)
    : _estimate(estimate), _partialSchemaKeyCE() {}

bool CardinalityEstimate::operator==(const CardinalityEstimate& other) const {
    return _estimate == other._estimate && _partialSchemaKeyCE == other._partialSchemaKeyCE;
}

CEType CardinalityEstimate::getEstimate() const {
    return _estimate;
}

CEType& CardinalityEstimate::getEstimate() {
    return _estimate;
}

const PartialSchemaKeyCE& CardinalityEstimate::getPartialSchemaKeyCE() const {
    return _partialSchemaKeyCE;
}

PartialSchemaKeyCE& CardinalityEstimate::getPartialSchemaKeyCE() {
    return _partialSchemaKeyCE;
}

IndexingAvailability::IndexingAvailability(GroupIdType scanGroupId,
                                           ProjectionName scanProjection,
                                           std::string scanDefName,
                                           const bool eqPredsOnly,
                                           const bool hasProperInterval,
                                           opt::unordered_set<std::string> satisfiedPartialIndexes)
    : _scanGroupId(scanGroupId),
      _scanProjection(std::move(scanProjection)),
      _scanDefName(std::move(scanDefName)),
      _eqPredsOnly(eqPredsOnly),
      _satisfiedPartialIndexes(std::move(satisfiedPartialIndexes)),
      _hasProperInterval(hasProperInterval) {}

bool IndexingAvailability::operator==(const IndexingAvailability& other) const {
    return _scanGroupId == other._scanGroupId && _scanProjection == other._scanProjection &&
        _scanDefName == other._scanDefName && _eqPredsOnly == other._eqPredsOnly &&
        _satisfiedPartialIndexes == other._satisfiedPartialIndexes &&
        _hasProperInterval == other._hasProperInterval;
}

GroupIdType IndexingAvailability::getScanGroupId() const {
    return _scanGroupId;
}

void IndexingAvailability::setScanGroupId(const GroupIdType scanGroupId) {
    _scanGroupId = scanGroupId;
}

const ProjectionName& IndexingAvailability::getScanProjection() const {
    return _scanProjection;
}

const std::string& IndexingAvailability::getScanDefName() const {
    return _scanDefName;
}

const opt::unordered_set<std::string>& IndexingAvailability::getSatisfiedPartialIndexes() const {
    return _satisfiedPartialIndexes;
}

opt::unordered_set<std::string>& IndexingAvailability::getSatisfiedPartialIndexes() {
    return _satisfiedPartialIndexes;
}

bool IndexingAvailability::getEqPredsOnly() const {
    return _eqPredsOnly;
}

void IndexingAvailability::setEqPredsOnly(const bool value) {
    _eqPredsOnly = value;
}

bool IndexingAvailability::hasProperInterval() const {
    return _hasProperInterval;
}

void IndexingAvailability::setHasProperInterval(const bool hasProperInterval) {
    _hasProperInterval = hasProperInterval;
}

CollectionAvailability::CollectionAvailability(opt::unordered_set<std::string> scanDefSet)
    : _scanDefSet(std::move(scanDefSet)) {}

bool CollectionAvailability::operator==(const CollectionAvailability& other) const {
    return _scanDefSet == other._scanDefSet;
}

const opt::unordered_set<std::string>& CollectionAvailability::getScanDefSet() const {
    return _scanDefSet;
}

opt::unordered_set<std::string>& CollectionAvailability::getScanDefSet() {
    return _scanDefSet;
}

size_t DistributionHash::operator()(
    const DistributionAndProjections& distributionAndProjections) const {
    size_t result = 0;
    updateHash(result, std::hash<DistributionType>()(distributionAndProjections._type));
    for (const ProjectionName& projectionName : distributionAndProjections._projectionNames) {
        updateHash(result, std::hash<ProjectionName>()(projectionName));
    }
    return result;
}

DistributionAvailability::DistributionAvailability(DistributionSet distributionSet)
    : _distributionSet(std::move(distributionSet)) {}

bool DistributionAvailability::operator==(const DistributionAvailability& other) const {
    return _distributionSet == other._distributionSet;
}

const DistributionSet& DistributionAvailability::getDistributionSet() const {
    return _distributionSet;
}

DistributionSet& DistributionAvailability::getDistributionSet() {
    return _distributionSet;
}

}  // namespace mongo::optimizer::properties