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
|
/**
* 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/index_bounds.h"
#include "mongo/db/query/optimizer/node.h"
#include "mongo/db/query/optimizer/utils/abt_compare.h"
#include "mongo/db/query/optimizer/utils/utils.h"
namespace mongo::optimizer {
BoundRequirement BoundRequirement::makeMinusInf() {
return {true /*inclusive*/, Constant::minKey()};
}
BoundRequirement BoundRequirement::makePlusInf() {
return {true /*inclusive*/, Constant::maxKey()};
}
BoundRequirement::BoundRequirement(bool inclusive, ABT bound) : Base(inclusive, std::move(bound)) {
assertExprSort(_bound);
}
bool BoundRequirement::isMinusInf() const {
return _inclusive && _bound == Constant::minKey();
}
bool BoundRequirement::isPlusInf() const {
return _inclusive && _bound == Constant::maxKey();
}
IntervalRequirement::IntervalRequirement()
: IntervalRequirement(BoundRequirement::makeMinusInf(), BoundRequirement::makePlusInf()) {}
IntervalRequirement::IntervalRequirement(BoundRequirement lowBound, BoundRequirement highBound)
: Base(std::move(lowBound), std::move(highBound)) {}
bool IntervalRequirement::isFullyOpen() const {
return _lowBound.isMinusInf() && _highBound.isPlusInf();
}
bool IntervalRequirement::isConstant() const {
return getLowBound().getBound().is<Constant>() && getHighBound().getBound().is<Constant>();
}
bool isIntervalReqFullyOpenDNF(const IntervalReqExpr::Node& n) {
if (auto singular = IntervalReqExpr::getSingularDNF(n); singular && singular->isFullyOpen()) {
return true;
}
return false;
}
CompoundBoundRequirement::CompoundBoundRequirement(bool inclusive, ABTVector bound)
: Base(inclusive, std::move(bound)) {
for (const auto& expr : _bound) {
assertExprSort(expr);
}
}
bool CompoundBoundRequirement::isMinusInf() const {
return _inclusive && std::all_of(_bound.cbegin(), _bound.cend(), [](const ABT& element) {
return element == Constant::minKey();
});
}
bool CompoundBoundRequirement::isPlusInf() const {
return _inclusive && std::all_of(_bound.cbegin(), _bound.cend(), [](const ABT& element) {
return element == Constant::maxKey();
});
}
bool CompoundBoundRequirement::isConstant() const {
return std::all_of(
_bound.cbegin(), _bound.cend(), [](const ABT& element) { return element.is<Constant>(); });
}
size_t CompoundBoundRequirement::size() const {
return _bound.size();
}
void CompoundBoundRequirement::push_back(BoundRequirement bound) {
_inclusive &= bound.isInclusive();
_bound.push_back(std::move(bound.getBound()));
}
CompoundIntervalRequirement::CompoundIntervalRequirement()
: CompoundIntervalRequirement({true /*inclusive*/, {}}, {true /*inclusive*/, {}}) {}
CompoundIntervalRequirement::CompoundIntervalRequirement(CompoundBoundRequirement lowBound,
CompoundBoundRequirement highBound)
: Base(std::move(lowBound), std::move(highBound)) {}
bool CompoundIntervalRequirement::isFullyOpen() const {
return _lowBound.isMinusInf() && _highBound.isPlusInf();
}
size_t CompoundIntervalRequirement::size() const {
return _lowBound.size();
}
void CompoundIntervalRequirement::push_back(IntervalRequirement interval) {
_lowBound.push_back(std::move(interval.getLowBound()));
_highBound.push_back(std::move(interval.getHighBound()));
}
PartialSchemaKey::PartialSchemaKey() : PartialSchemaKey(make<PathIdentity>()) {}
PartialSchemaKey::PartialSchemaKey(ABT path) : PartialSchemaKey(boost::none, std::move(path)) {}
PartialSchemaKey::PartialSchemaKey(ProjectionName projectionName, ABT path)
: PartialSchemaKey(boost::optional<ProjectionName>{std::move(projectionName)},
std::move(path)) {}
PartialSchemaKey::PartialSchemaKey(boost::optional<ProjectionName> projectionName, ABT path)
: _projectionName(std::move(projectionName)), _path(std::move(path)) {
assertPathSort(_path);
}
bool PartialSchemaKey::operator==(const PartialSchemaKey& other) const {
return _projectionName == other._projectionName && _path == other._path;
}
PartialSchemaRequirement::PartialSchemaRequirement(
boost::optional<ProjectionName> boundProjectionName,
IntervalReqExpr::Node intervals,
const bool isPerfOnly)
: _boundProjectionName(std::move(boundProjectionName)),
_intervals(std::move(intervals)),
_isPerfOnly(isPerfOnly) {
tassert(6624154,
"Cannot have perf only requirement which also binds",
!_isPerfOnly || !_boundProjectionName);
}
bool PartialSchemaRequirement::operator==(const PartialSchemaRequirement& other) const {
return _boundProjectionName == other._boundProjectionName && _intervals == other._intervals &&
_isPerfOnly == other._isPerfOnly;
}
const boost::optional<ProjectionName>& PartialSchemaRequirement::getBoundProjectionName() const {
return _boundProjectionName;
}
const IntervalReqExpr::Node& PartialSchemaRequirement::getIntervals() const {
return _intervals;
}
bool PartialSchemaRequirement::getIsPerfOnly() const {
return _isPerfOnly;
}
bool PartialSchemaRequirement::mayReturnNull(const ConstFoldFn& constFold) const {
return _boundProjectionName && checkMaybeHasNull(getIntervals(), constFold);
};
bool IndexPathLessComparator::operator()(const ABT& path1, const ABT& path2) const {
return compareExprAndPaths(path1, path2) < 0;
}
int PartialSchemaKeyComparator::Cmp3W::operator()(const PartialSchemaKey& k1,
const PartialSchemaKey& k2) const {
if (const auto& p1 = k1._projectionName) {
if (const auto& p2 = k2._projectionName) {
const int projCmp = p1->compare(*p2);
if (projCmp != 0) {
return projCmp;
}
// Fallthrough to comparison below.
} else {
// p1 > p2 because nonempty > empty.
return 1;
}
} else if (k2._projectionName) {
// p1 < p2 because empty < nonempty
return -1;
}
// p1 == p2 so compare paths.
return compareExprAndPaths(k1._path, k2._path);
}
bool PartialSchemaKeyComparator::Less::operator()(const PartialSchemaKey& k1,
const PartialSchemaKey& k2) const {
return PartialSchemaKeyComparator::Cmp3W{}(k1, k2) < 0;
}
int PartialSchemaRequirementComparator::Cmp3W::operator()(
const PartialSchemaRequirement& req1, const PartialSchemaRequirement& req2) const {
int intervalCmp = compareIntervalExpr(req1.getIntervals(), req2.getIntervals());
if (intervalCmp != 0) {
return intervalCmp;
}
// Intervals are equal: compare the output bindings.
auto b1 = req1.getBoundProjectionName();
auto b2 = req2.getBoundProjectionName();
if (b1 && b2) {
return b1->compare(*b2);
} else if (b1) {
// b1 > b2 because nonempty > empty.
return 1;
} else if (b2) {
// b1 < b2 because empty < nonempty.
return -1;
} else {
// empty == empty.
return 0;
}
}
bool PartialSchemaRequirementComparator::Less::operator()(
const PartialSchemaRequirement& req1, const PartialSchemaRequirement& req2) const {
int intervalCmp = compareIntervalExpr(req1.getIntervals(), req2.getIntervals());
if (intervalCmp < 0) {
return true;
} else if (intervalCmp > 0) {
return false;
}
// Intervals are equal: compare the output bindings.
auto b1 = req1.getBoundProjectionName();
auto b2 = req2.getBoundProjectionName();
if (b1 && b2) {
return b1->compare(*b2) < 0;
} else {
// If either or both optionals are empty, then the only way for
// 'b1 < b2' is '{} < {anything}'.
return !b1 && b2;
}
}
ResidualRequirement::ResidualRequirement(PartialSchemaKey key,
PartialSchemaRequirement req,
size_t entryIndex)
: _key(std::move(key)), _req(std::move(req)), _entryIndex(entryIndex) {}
bool ResidualRequirement::operator==(const ResidualRequirement& other) const {
return _key == other._key && _req == other._req && _entryIndex == other._entryIndex;
}
ResidualRequirementWithOptionalCE::ResidualRequirementWithOptionalCE(PartialSchemaKey key,
PartialSchemaRequirement req,
boost::optional<CEType> ce)
: _key(std::move(key)), _req(std::move(req)), _ce(ce) {}
bool ResidualRequirementWithOptionalCE::operator==(
const ResidualRequirementWithOptionalCE& other) const {
return _key == other._key && _req == other._req && _ce == other._ce;
}
EqualityPrefixEntry::EqualityPrefixEntry(const size_t startPos)
: _startPos(startPos), _interval(CompoundIntervalReqExpr::makeSingularDNF()), _predPosSet() {}
bool EqualityPrefixEntry::operator==(const EqualityPrefixEntry& other) const {
return _startPos == other._startPos && _interval == other._interval &&
_predPosSet == other._predPosSet;
}
CandidateIndexEntry::CandidateIndexEntry(std::string indexDefName)
: _indexDefName(std::move(indexDefName)),
_fieldProjectionMap(),
_eqPrefixes(),
_correlatedProjNames(),
_residualRequirements(),
_predTypes(),
_intervalPrefixSize(0) {}
bool CandidateIndexEntry::operator==(const CandidateIndexEntry& other) const {
return _indexDefName == other._indexDefName &&
_fieldProjectionMap == other._fieldProjectionMap && _eqPrefixes == other._eqPrefixes &&
_correlatedProjNames == other._correlatedProjNames &&
_residualRequirements == other._residualRequirements && _predTypes == other._predTypes &&
_intervalPrefixSize == other._intervalPrefixSize;
}
bool ScanParams::operator==(const ScanParams& other) const {
return _fieldProjectionMap == other._fieldProjectionMap &&
_residualRequirements == other._residualRequirements;
}
} // namespace mongo::optimizer
|