summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/optimizer/props.h
blob: e7ac16f227db1f15c1312fe2d3fc047800608990 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
 *    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.
 */

#pragma once

#include <map>
#include <string>
#include <vector>

#include "mongo/db/query/optimizer/algebra/operator.h"
#include "mongo/db/query/optimizer/algebra/polyvalue.h"
#include "mongo/db/query/optimizer/defs.h"
#include "mongo/db/query/optimizer/metadata.h"
#include "mongo/util/assert_util.h"

namespace mongo::optimizer::properties {

/**
 * Tag for logical property types.
 */
class LogicalPropertyTag {};

/**
 * Tag for physical property types.
 */
class PhysPropertyTag {};

/**
 * Logical properties.
 */
class CardinalityEstimate;

class ProjectionAvailability;
class IndexingAvailability;
class CollectionAvailability;
class DistributionAvailability;

/**
 * Physical properties.
 */
class CollationRequirement;
class LimitSkipRequirement;
class ProjectionRequirement;
class DistributionRequirement;
class IndexingRequirement;
class RepetitionEstimate;
class LimitEstimate;

using LogicalProperty = algebra::PolyValue<CardinalityEstimate,
                                           ProjectionAvailability,
                                           IndexingAvailability,
                                           CollectionAvailability,
                                           DistributionAvailability>;

using PhysProperty = algebra::PolyValue<CollationRequirement,
                                        LimitSkipRequirement,
                                        ProjectionRequirement,
                                        DistributionRequirement,
                                        IndexingRequirement,
                                        RepetitionEstimate,
                                        LimitEstimate>;

using LogicalProps = opt::unordered_map<LogicalProperty::key_type, LogicalProperty>;
using PhysProps = opt::unordered_map<PhysProperty::key_type, PhysProperty>;

template <typename T,
          std::enable_if_t<std::is_base_of_v<LogicalPropertyTag, T>, bool> = true,
          typename... Args>
inline auto makeProperty(Args&&... args) {
    return LogicalProperty::make<T>(std::forward<Args>(args)...);
}

template <typename T,
          std::enable_if_t<std::is_base_of_v<PhysPropertyTag, T>, bool> = true,
          typename... Args>
inline auto makeProperty(Args&&... args) {
    return PhysProperty::make<T>(std::forward<Args>(args)...);
}

template <class P, std::enable_if_t<std::is_base_of_v<LogicalPropertyTag, P>, bool> = true>
static constexpr auto getPropertyKey() {
    return LogicalProperty::template tagOf<P>();
}

template <class P, std::enable_if_t<std::is_base_of_v<PhysPropertyTag, P>, bool> = true>
static constexpr auto getPropertyKey() {
    return PhysProperty::template tagOf<P>();
}

template <class P, class C>
bool hasProperty(const C& props) {
    return props.find(getPropertyKey<P>()) != props.cend();
}

template <class P, class C>
P& getProperty(C& props) {
    if (!hasProperty<P>(props)) {
        uasserted(6624022, "Property type does not exist.");
    }
    return *props.at(getPropertyKey<P>()).template cast<P>();
}

template <class P, class C>
const P& getPropertyConst(const C& props) {
    if (!hasProperty<P>(props)) {
        uasserted(6624023, "Property type does not exist.");
    }
    return *props.at(getPropertyKey<P>()).template cast<P>();
}

template <class P, class C>
void removeProperty(C& props) {
    props.erase(getPropertyKey<P>());
}

template <class P, class C>
bool setProperty(C& props, P property) {
    return props.emplace(getPropertyKey<P>(), makeProperty<P>(std::move(property))).second;
}

template <class P, class C>
void setPropertyOverwrite(C& props, P property) {
    props.insert_or_assign(getPropertyKey<P>(), makeProperty<P>(std::move(property)));
}

template <class C, typename... Args>
inline auto makeProps(Args&&... args) {
    C props;
    (setProperty(props, args), ...);
    return props;
}

template <typename... Args>
inline auto makeLogicalProps(Args&&... args) {
    return makeProps<LogicalProps>(std::forward<Args>(args)...);
}

template <typename... Args>
inline auto makePhysProps(Args&&... args) {
    return makeProps<PhysProps>(std::forward<Args>(args)...);
}

/**
 * A physical property which specifies how the collection (or intermediate result) is required to be
 * collated (sorted).
 */
class CollationRequirement final : public PhysPropertyTag {
public:
    CollationRequirement(ProjectionCollationSpec spec);

    bool operator==(const CollationRequirement& other) const;

    const ProjectionCollationSpec& getCollationSpec() const;
    ProjectionCollationSpec& getCollationSpec();

    bool hasClusteredOp() const;

    ProjectionNameSet getAffectedProjectionNames() const;

private:
    ProjectionCollationSpec _spec;
};

/**
 * A physical property which specifies what portion of the result in terms of window defined by the
 * limit and skip is to be returned.
 */
class LimitSkipRequirement final : public PhysPropertyTag {
public:
    static constexpr int64_t kMaxVal = std::numeric_limits<int64_t>::max();

    LimitSkipRequirement(int64_t limit, int64_t skip);

    bool operator==(const LimitSkipRequirement& other) const;

    bool hasLimit() const;

    int64_t getLimit() const;
    int64_t getSkip() const;
    int64_t getAbsoluteLimit() const;

    ProjectionNameSet getAffectedProjectionNames() const;

private:
    // Max number of documents to return. Maximum integer value means unlimited.
    int64_t _limit;
    // Documents to skip before start returning in result.
    int64_t _skip;
};

/**
 * A physical property which specifies required projections to be returned as part of the result.
 */
class ProjectionRequirement final : public PhysPropertyTag {
public:
    ProjectionRequirement(ProjectionNameOrderPreservingSet projections);

    bool operator==(const ProjectionRequirement& other) const;

    const ProjectionNameOrderPreservingSet& getProjections() const;
    ProjectionNameOrderPreservingSet& getProjections();

    ProjectionNameSet getAffectedProjectionNames() const;

private:
    ProjectionNameOrderPreservingSet _projections;
};

struct DistributionAndProjections {
    DistributionAndProjections(DistributionType type);
    DistributionAndProjections(DistributionType type, ProjectionNameVector projectionNames);

    bool operator==(const DistributionAndProjections& other) const;

    const DistributionType _type;

    /**
     * Defined for hash and range-based partitioning.
     */
    ProjectionNameVector _projectionNames;
};

/**
 * A physical property which specifies how the result is to be distributed (or partitioned) amongst
 * the computing partitions/nodes.
 */
class DistributionRequirement final : public PhysPropertyTag {
public:
    DistributionRequirement(DistributionAndProjections distributionAndProjections);

    bool operator==(const DistributionRequirement& other) const;

    const DistributionAndProjections& getDistributionAndProjections() const;
    DistributionAndProjections& getDistributionAndProjections();

    ProjectionNameSet getAffectedProjectionNames() const;

    bool getDisableExchanges() const;
    void setDisableExchanges(bool disableExchanges);

private:
    DistributionAndProjections _distributionAndProjections;

    // Heuristic used to disable exchanges right after Filter, Eval, and local GroupBy nodes.
    bool _disableExchanges;
};

/**
 * A physical property which describes if we intend to satisfy sargable predicates using an index.
 * With indexing requirement "Complete", we are requiring a regular physical
 * scan (both rid and row). With "Seek" (where we must have a non-empty RID projection name), we are
 * targeting a physical Seek. With "Index" (with or without RID projection name), we
 * are targeting a physical IndexScan. If in this case we have set RID projection, then we have
 * either gone for a Seek, or we have performed intersection. With empty RID we are targeting a
 * covered index scan.
 */
class IndexingRequirement final : public PhysPropertyTag {
public:
    IndexingRequirement();
    IndexingRequirement(IndexReqTarget indexReqTarget,
                        bool dedupRIDs,
                        GroupIdType satisfiedPartialIndexesGroupId);

    bool operator==(const IndexingRequirement& other) const;

    ProjectionNameSet getAffectedProjectionNames() const;

    IndexReqTarget getIndexReqTarget() const;

    bool getDedupRID() const;
    void setDedupRID(bool value);

    GroupIdType getSatisfiedPartialIndexesGroupId() const;

private:
    const IndexReqTarget _indexReqTarget;

    // If target == Index, specifies if we need to dedup RIDs.
    // Prior RID intersection removes the need to dedup.
    bool _dedupRID;

    // Set of indexes with partial indexes whose partial filters are satisfied considering the whole
    // query. Points to a group where can interrogate IndexingAvailability to find the satisfied
    // indexes.
    const GroupIdType _satisfiedPartialIndexesGroupId;
};

/**
 * A physical property that specifies how many times do we expect to execute the current subtree.
 * Typically generated via a NLJ where it is set on the inner side to reflect the outer side's
 * cardinality. This property affects costing of stateful physical operators such as sort and hash
 * groupby.
 */
class RepetitionEstimate final : public PhysPropertyTag {
public:
    RepetitionEstimate(CEType estimate);

    bool operator==(const RepetitionEstimate& other) const;

    ProjectionNameSet getAffectedProjectionNames() const;

    CEType getEstimate() const;

private:
    CEType _estimate;
};

/**
 * A physical property that specifies that the we will consider only some approximate number of
 * documents. Typically generated after enforcing a LimitSkipRequirement. This property affects
 * costing of stateful physical operators such as sort and hash groupby.
 */
class LimitEstimate final : public PhysPropertyTag {
public:
    LimitEstimate(CEType estimate);

    bool operator==(const LimitEstimate& other) const;

    ProjectionNameSet getAffectedProjectionNames() const;

    bool hasLimit() const;
    CEType getEstimate() const;

private:
    CEType _estimate;
};

/**
 * A logical property which specifies available projections for a given ABT tree.
 */
class ProjectionAvailability final : public LogicalPropertyTag {
public:
    ProjectionAvailability(ProjectionNameSet projections);

    bool operator==(const ProjectionAvailability& other) const;

    const ProjectionNameSet& getProjections() const;

private:
    ProjectionNameSet _projections;
};

/**
 * A logical property which provides an estimated row count for a given ABT tree.
 */
class CardinalityEstimate final : public LogicalPropertyTag {
public:
    CardinalityEstimate(CEType estimate);

    bool operator==(const CardinalityEstimate& other) const;

    CEType getEstimate() const;
    CEType& getEstimate();

    const PartialSchemaKeyCE& getPartialSchemaKeyCE() const;
    PartialSchemaKeyCE& getPartialSchemaKeyCE();

private:
    CEType _estimate;

    // Used for SargableNodes. Provide additional per partial schema key CE.
    PartialSchemaKeyCE _partialSchemaKeyCE;
};

/**
 * A logical property which specifies availability to index predicates in the ABT subtree and
 * contains the scan projection. The projection and definition name are here for convenience: it can
 * be retrieved using the scan group from the memo.
 */
class IndexingAvailability final : public LogicalPropertyTag {
public:
    IndexingAvailability(GroupIdType scanGroupId,
                         ProjectionName scanProjection,
                         std::string scanDefName,
                         bool eqPredsOnly,
                         bool hasProperInterval,
                         opt::unordered_set<std::string> satisfiedPartialIndexes);

    bool operator==(const IndexingAvailability& other) const;

    GroupIdType getScanGroupId() const;
    void setScanGroupId(GroupIdType scanGroupId);

    const ProjectionName& getScanProjection() const;
    const std::string& getScanDefName() const;

    const opt::unordered_set<std::string>& getSatisfiedPartialIndexes() const;
    opt::unordered_set<std::string>& getSatisfiedPartialIndexes();

    bool getEqPredsOnly() const;
    void setEqPredsOnly(bool value);

    bool hasProperInterval() const;
    void setHasProperInterval(bool hasProperInterval);

private:
    GroupIdType _scanGroupId;
    const ProjectionName _scanProjection;
    const std::string _scanDefName;

    // Specifies if all predicates in the current group and child group are equalities.
    // This is determined based on SargableNode exclusively containing equality intervals.
    bool _eqPredsOnly;

    // Set of indexes with partial indexes whose partial filters are satisfied for the current
    // group.
    opt::unordered_set<std::string> _satisfiedPartialIndexes;

    // True if there is at least one proper interval in a sargable node in this group.
    bool _hasProperInterval;
};


/**
 * Logical property which specifies which collections (scanDefs) are available for a particular
 * group. For example if the group contains a join of two tables, we would have (at least) two
 * collections in the set.
 */
class CollectionAvailability final : public LogicalPropertyTag {
public:
    CollectionAvailability(opt::unordered_set<std::string> scanDefSet);

    bool operator==(const CollectionAvailability& other) const;

    const opt::unordered_set<std::string>& getScanDefSet() const;
    opt::unordered_set<std::string>& getScanDefSet();

private:
    opt::unordered_set<std::string> _scanDefSet;
};

struct DistributionHash {
    size_t operator()(const DistributionAndProjections& distributionAndProjections) const;
};

using DistributionSet = opt::unordered_set<DistributionAndProjections, DistributionHash>;

/**
 * Logical property which specifies promising projections and distributions to attempt to enforce
 * during physical optimization. For example, a group containing a GroupByNode would add hash
 * partitioning on the group-by projections.
 */
class DistributionAvailability final : public LogicalPropertyTag {
public:
    DistributionAvailability(DistributionSet distributionSet);

    bool operator==(const DistributionAvailability& other) const;

    const DistributionSet& getDistributionSet() const;
    DistributionSet& getDistributionSet();

private:
    DistributionSet _distributionSet;
};

}  // namespace mongo::optimizer::properties