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
|
#ifndef HISTOGRAMS_SINGLETON_INCLUDED
#define HISTOGRAMS_SINGLETON_INCLUDED
/* Copyright (c) 2016, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file sql/histograms/singleton.h
Singleton histogram.
This file defines the Singleton histogram. A Singleton histogram is a
histogram where only a value and it's frequency is stored. It allows us to
use less storage space, as well as estimating selectivity a bit more
efficient.
A singleton histogram converted to a JSON object, follows the following
"schema":
{
// Last time the histogram was updated. As of now, this means "when the
// histogram was created" (incremental updates are not supported). Date/time
// is given in UTC.
// -- J_DATETIME
"last-updated": "2015-11-04 15:19:51.000000",
// Histogram type. Always "singleton" for singleton histograms.
// -- J_STRING
"histogram-type": "singleton",
// Fraction of NULL values. This is the total fraction of NULL values in the
// original data set.
// -- J_DOUBLE
"null-values": 0.1,
// Histogram buckets. May be an empty array, if for instance the source
// only contains NULL values.
// -- J_ARRAY
"buckets":
[
[
// Value
// -- Data type depends on the source column.
42,
// Cumulative frequency
// -- J_DOUBLE
0.001978728666831561
]
]
}
*/
#include <stddef.h>
#include <string> // std::string
#include "my_inttypes.h"
#include "mysql_time.h"
#include "sql/histograms/histogram.h" // Histogram, Histogram_comparator,
#include "sql/histograms/value_map_type.h"
#include "sql/mem_root_allocator.h"
#include "sql/mem_root_array.h"
#include "sql/my_decimal.h"
#include "sql_string.h"
class Json_array;
class Json_object;
struct MEM_ROOT;
namespace histograms {
/**
Singleton histogram.
Singleton histograms do not have a public constructor, but are instead created
through the factory method Singleton<T>::create() and returned by pointer.
This is done to ensure that we can return nullptr in case memory allocations
carried out during construction fail.
Likewise, the Singleton class does not have a public copy constructor, but
instead implements a clone() method that returns nullptr in case of failure.
*/
struct Histogram_comparator;
template <class T>
class Value_map;
template <class T>
struct SingletonBucket {
T value;
double cumulative_frequency;
SingletonBucket(T value, double cumulative_frequency)
: value(value), cumulative_frequency(cumulative_frequency) {}
};
template <class T>
class Singleton : public Histogram {
public:
/**
Singleton histogram factory method.
Attempts to allocate and initialize a singleton histogram on the supplied
mem_root. This will not build the histogram, but only set its properties.
If the attempt to allocate the histogram fails or if an error occurs during
construction we return nullptr.
@param mem_root the mem_root where the histogram contents will be allocated
@param db_name name of the database this histogram represents
@param tbl_name name of the table this histogram represents
@param col_name name of the column this histogram represents
@param data_type the type of data that this histogram contains
@return A pointer to a Singleton histogram on success. Returns nullptr on
error.
*/
static Singleton<T> *create(MEM_ROOT *mem_root, const std::string &db_name,
const std::string &tbl_name,
const std::string &col_name,
Value_map_type data_type);
/**
Make a clone of this histogram on a MEM_ROOT.
@param mem_root the MEM_ROOT to allocate the new histogram contents on.
@return a copy of the histogram allocated on the provided MEM_ROOT.
*/
Histogram *clone(MEM_ROOT *mem_root) const override;
Singleton(const Singleton<T> &other) = delete;
/**
Build the Singleton histogram.
@param value_map values to create the histogram for
@param num_buckets the number of buckets specified/requested by the user
@return true on error, false otherwise
*/
bool build_histogram(const Value_map<T> &value_map, size_t num_buckets);
/**
Convert this histogram to a JSON object.
This function will take the contents of the current histogram and put
it in the output parameter "json_object".
@param[in,out] json_object output where the histogram is to be stored. The
caller is responsible for allocating/deallocating the JSON
object
@return true on error, false otherwise
*/
bool histogram_to_json(Json_object *json_object) const override;
/**
@return number of values/buckets in this histogram
*/
size_t get_num_buckets() const override { return m_buckets.size(); }
/**
Get the estimated number of distinct non-NULL values.
@return number of distinct non-NULL values
TODO(christiani): If the histogram is based on sampling, then this estimate
is potentially off by a factor 1/sampling_rate. It should be adjusted to an
actual estimate if we are going to use it.
*/
size_t get_num_distinct_values() const override { return get_num_buckets(); }
/**
Returns the histogram type as a readable string.
@return a readable string representation of the histogram type
*/
std::string histogram_type_to_str() const override;
/**
Find the number of values equal to 'value'.
This function will estimate the number of values that is equal to the
provided value.
@param value The value to estimate the selectivity for.
@return the selectivity between 0.0 and 1.0 inclusive.
*/
double get_equal_to_selectivity(const T &value) const;
/**
Find the number of values less than 'value'.
This function will estimate the number of values that is less than the
provided value.
@param value The value to estimate the selectivity for.
@return the selectivity between 0.0 and 1.0 inclusive.
*/
double get_less_than_selectivity(const T &value) const;
/**
Find the number of values greater than 'value'.
This function will estimate the number of values that is greater than the
provided value.
@param value The value to estimate the selectivity for.
@return the selectivity between 0.0 and 1.0 inclusive.
*/
double get_greater_than_selectivity(const T &value) const;
protected:
/**
Populate this histogram with contents from a JSON object.
@param json_object a JSON object that represents an Singleton histogram
@param context error context for validation
@return true on error, false otherwise.
*/
bool json_to_histogram(const Json_object &json_object,
Error_context *context) override;
private:
/// String representation of the histogram type SINGLETON.
static constexpr const char *singleton_str() { return "singleton"; }
/**
Singleton constructor.
This will not build the histogram, but only set its properties.
@param mem_root the mem_root where the histogram contents will be allocated
@param db_name name of the database this histogram represents
@param tbl_name name of the table this histogram represents
@param col_name name of the column this histogram represents
@param data_type the type of data that this histogram contains
@param[out] error is set to true if an error occurs
*/
Singleton(MEM_ROOT *mem_root, const std::string &db_name,
const std::string &tbl_name, const std::string &col_name,
Value_map_type data_type, bool *error);
/**
Singleton copy-constructor
This will take a copy of the histogram and all of its contents on the
provided MEM_ROOT.
@param mem_root the MEM_ROOT to allocate the new histogram on.
@param other the histogram to take a copy of
@param[out] error is set to true if an error occurs
*/
Singleton(MEM_ROOT *mem_root, const Singleton<T> &other, bool *error);
/**
Add value to a JSON bucket
This function adds the value to the supplied JSON array.
@param value the value to add
@param[out] json_bucket a JSON array where the bucket data is to be stored
@return true on error, false otherwise
*/
static bool add_value_json_bucket(const T &value, Json_array *json_bucket);
/**
Convert one bucket to a JSON object.
@param bucket the histogram bucket to convert
@param[out] json_bucket a JSON array where the bucket data is to be stored
@return true on error, false otherwise
*/
static bool create_json_bucket(const SingletonBucket<T> &bucket,
Json_array *json_bucket);
/// The buckets for this histogram [value, cumulative frequency].
Mem_root_array<SingletonBucket<T>> m_buckets;
};
} // namespace histograms
#endif
|