summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/ce/heuristic_predicate_estimation.h
blob: 07fc4fd05cb6152afc20c6d78a68f68c7de35ddb (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
/**
 *    Copyright (C) 2023-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/utils/ce_math.h"
#include "mongo/db/query/optimizer/utils/memo_utils.h"

namespace mongo::optimizer::ce {
constexpr SelectivityType kDefaultFilterSel{0.1};
constexpr SelectivityType kDefaultExistsSel{0.70};

// The selectivities used in the piece-wise function for open-range intervals.
// Note that we assume a smaller input cardinality will result in a less selective range.
constexpr SelectivityType kSmallCardOpenRangeSel{0.70};
constexpr SelectivityType kMediumCardOpenRangeSel{0.45};
constexpr SelectivityType kLargeCardOpenRangeSel{0.33};

// The selectivities used in the piece-wise function for closed-range intervals.
// Note that we assume a smaller input cardinality will result in a less selective range.
constexpr SelectivityType kSmallCardClosedRangeSel{0.50};
constexpr SelectivityType kMediumCardClosedRangeSel{0.33};
constexpr SelectivityType kLargeCardClosedRangeSel{0.20};

// Global and Local selectivity should multiply to the Complete selectivity.
constexpr SelectivityType kDefaultCompleteGroupSel{0.01};
constexpr SelectivityType kDefaultLocalGroupSel{0.02};
constexpr SelectivityType kDefaultGlobalGroupSel{0.5};

// Since there are only two boolean values, when we have a count of arrays containing booleans, we
// estimate the default selectivity of booleans in arrays as 1/2.
constexpr double kDefaultArrayBoolSel{0.5};

// The following constants are the steps used in the piece-wise functions that select selectivies
// based on input cardinality.
constexpr CEType kSmallLimit{20.0};
constexpr CEType kMediumLimit{100.0};

// Assumed average number of elements in an array. This is a unitless constant.
constexpr double kDefaultAverageArraySize{10.0};

/**
 * Returns the heuristic selectivity of equalities. To avoid super small selectivities for small
 * cardinalities, that would result in 0 cardinality for many small inputs, the estimate is scaled
 * as inputCard grows. The bigger inputCard, the smaller the selectivity.
 */
SelectivityType heuristicEqualitySel(CEType inputCard);

/**
 * Returns the heuristic selectivity of intervals with bounds on both ends. These intervals are
 * considered less selective than equalities.
 * Examples: (a > 'abc' AND a < 'hta'), (0 < b <= 13)
 */
SelectivityType heuristicClosedRangeSel(CEType inputCard);

/**
 * Returns the heuristic selectivity of intervals open on one end. These intervals are considered
 * less selective than those with both ends specified by the user query.
 * Examples: (a > 'xyz'), (b <= 13)
 */
SelectivityType heuristicOpenRangeSel(CEType inputCard);

/**
 * Returns the heuristic selectivity based on the kind of operation in a predicate, e.g. $eq, $lt.
 */
SelectivityType heuristicOperationSel(Operations op, CEType inputCard);

/**
 * Returns the heuristic selectivity of the given 'interval'.
 */
SelectivityType heuristicIntervalSel(const IntervalRequirement& interval, CEType inputCard);

/**
 * Returns the heuristically estimated cardinality of the given 'interval'.
 */
CEType heuristicIntervalCard(const IntervalRequirement& interval, CEType inputCard);

/**
 * Returns the heuristic selectivity of an interval expressed as two PathCompare nodes.
 */
SelectivityType heuristicIntervalSel(const PathCompare& left,
                                     const PathCompare& right,
                                     CEType inputCard);

}  // namespace mongo::optimizer::ce