MySQL 8.0.39
Source Code Documentation
equi_height_bucket.h
Go to the documentation of this file.
1#ifndef HISTOGRAMS_EQUI_HEIGHT_BUCKET_INCLUDED
2#define HISTOGRAMS_EQUI_HEIGHT_BUCKET_INCLUDED
3
4/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27/**
28 @file sql/histograms/equi_height_bucket.h
29 Equi-height bucket.
30
31 This file defines the class representing an equi-height bucket. A bucket holds
32 four different values:
33 - Lower inclusive value.
34 - Upper inclusive value.
35 - The cumulative frequency (between 0.0 and 1.0).
36 - Number of distinct values in this bucket.
37*/
38
39#include "my_base.h" // ha_rows
40#include "my_inttypes.h"
41#include "mysql_time.h"
42#include "sql/my_decimal.h"
43#include "sql_string.h"
44
45class Json_array;
46
47namespace histograms {
48namespace equi_height {
49
50/**
51 Equi-height bucket.
52*/
53template <class T>
54class Bucket {
55 private:
56 /// Lower inclusive value contained in this bucket.
58
59 /// Upper inclusive value contained in this bucket.
61
62 /// The cumulative frequency. 0.0 <= m_cumulative_frequency <= 1.0.
64
65 /// Number of distinct values in this bucket.
67
68 /**
69 Add values to a JSON bucket.
70
71 This function adds the lower and upper inclusive value to the supplied
72 JSON array. The lower value is added first.
73
74 @param lower_value The lower inclusive value to add.
75 @param upper_value The upper inclusive value to add.
76 @param[out] json_array A JSON array where the bucket data is stored.
77
78 @return True on error, false otherwise.
79 */
80 static bool add_values_json_bucket(const T &lower_value, const T &upper_value,
81 Json_array *json_array);
82
83 public:
84 /**
85 Equi-height bucket constructor.
86
87 Does nothing more than setting the member variables.
88
89 @param lower Lower inclusive value.
90 @param upper Upper inclusive value.
91 @param freq The cumulative frequency.
92 @param num_distinct Number of distinct values in this bucket.
93 */
94 Bucket(T lower, T upper, double freq, ha_rows num_distinct);
95
96 /**
97 @return Lower inclusive value.
98 */
99 const T &get_lower_inclusive() const { return m_lower_inclusive; }
100
101 /**
102 @return Upper inclusive value.
103 */
104 const T &get_upper_inclusive() const { return m_upper_inclusive; }
105
106 /**
107 @return Cumulative frequency.
108 */
110
111 /**
112 @return Number of distinct values.
113 */
115
116 /**
117 Convert this equi-height bucket to a JSON array.
118
119 This function will take the contents of the current equi-height bucket
120 and put it in the output parameter "json_array". The result is an array
121 with the following contents:
122 Index 0: Lower inclusive value.
123 Index 1: Upper inclusive value.
124 Index 2: Cumulative frequency.
125 Index 3: Number of distinct values.
126
127 @param[out] json_array Output where the bucket content is to be stored.
128
129 @return True on error, false otherwise.
130 */
131 bool bucket_to_json(Json_array *json_array) const;
132
133 /**
134 Finds the relative location of "value" between bucket endpoints.
135 This is used to determine the fraction of a bucket to include in selectivity
136 estimates in the case where the query value lies inside a bucket.
137
138 get_distance_from_lower returns the fraction of all elements between bucket
139 endpoints [a, b] that lie in the interval [a, value), i.e., strictly less
140 than value. For some histogram types the return value is only an estimate.
141
142 @param value The value to calculate the distance for.
143
144 @return The distance between "value" and lower inclusive value.
145 */
146 double get_distance_from_lower(const T &value) const;
147
148 /**
149 Returns the fraction of all elements between bucket endpoints [a, b] that
150 are strictly greater than "value". For some histogram types the return value
151 is only an estimate.
152
153 @return The distance between "value" and upper inclusive value.
154 */
155 double get_distance_from_upper(const T &value) const;
156};
157
158} // namespace equi_height
159} // namespace histograms
160
161#endif
Represents a JSON array container, i.e.
Definition: json_dom.h:520
Equi-height bucket.
Definition: equi_height_bucket.h:54
const T m_upper_inclusive
Upper inclusive value contained in this bucket.
Definition: equi_height_bucket.h:60
double get_distance_from_upper(const T &value) const
Returns the fraction of all elements between bucket endpoints [a, b] that are strictly greater than "...
Definition: equi_height_bucket.cc:435
const ha_rows m_num_distinct
Number of distinct values in this bucket.
Definition: equi_height_bucket.h:66
const T & get_upper_inclusive() const
Definition: equi_height_bucket.h:104
double get_distance_from_lower(const T &value) const
Finds the relative location of "value" between bucket endpoints.
Definition: equi_height_bucket.cc:203
double get_cumulative_frequency() const
Definition: equi_height_bucket.h:109
Bucket(T lower, T upper, double freq, ha_rows num_distinct)
Equi-height bucket constructor.
Definition: equi_height_bucket.cc:56
const T m_lower_inclusive
Lower inclusive value contained in this bucket.
Definition: equi_height_bucket.h:57
static bool add_values_json_bucket(const T &lower_value, const T &upper_value, Json_array *json_array)
Add values to a JSON bucket.
ha_rows get_num_distinct() const
Definition: equi_height_bucket.h:114
const T & get_lower_inclusive() const
Definition: equi_height_bucket.h:99
const double m_cumulative_frequency
The cumulative frequency. 0.0 <= m_cumulative_frequency <= 1.0.
Definition: equi_height_bucket.h:63
bool bucket_to_json(Json_array *json_array) const
Convert this equi-height bucket to a JSON array.
Definition: equi_height_bucket.cc:68
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1140
It is interface module to fixed precision decimals library.
Some integer typedefs for easier portability.
Time declarations shared between the server and client API: you should not add anything to this heade...
Definition: column_statistics.h:34
static std::string lower(std::string str)
Definition: config_parser.cc:65
Our own string classes, used pervasively throughout the executor.