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
|
#ifndef HISTOGRAMS_EQUI_HEIGHT_BUCKET_INCLUDED
#define HISTOGRAMS_EQUI_HEIGHT_BUCKET_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/equi_height_bucket.h
Equi-height bucket.
This file defines the class representing an equi-height bucket. A bucket holds
four different values:
- Lower inclusive value.
- Upper inclusive value.
- The cumulative frequency (between 0.0 and 1.0).
- Number of distinct values in this bucket.
*/
#include "my_base.h" // ha_rows
#include "my_inttypes.h"
#include "mysql_time.h"
#include "sql/my_decimal.h"
#include "sql_string.h"
class Json_array;
namespace histograms {
namespace equi_height {
/**
Equi-height bucket.
*/
template <class T>
class Bucket {
private:
/// Lower inclusive value contained in this bucket.
const T m_lower_inclusive;
/// Upper inclusive value contained in this bucket.
const T m_upper_inclusive;
/// The cumulative frequency. 0.0 <= m_cumulative_frequency <= 1.0.
const double m_cumulative_frequency;
/// Number of distinct values in this bucket.
const ha_rows m_num_distinct;
/**
Add values to a JSON bucket.
This function adds the lower and upper inclusive value to the supplied
JSON array. The lower value is added first.
@param lower_value The lower inclusive value to add.
@param upper_value The upper inclusive value to add.
@param[out] json_array A JSON array where the bucket data is stored.
@return True on error, false otherwise.
*/
static bool add_values_json_bucket(const T &lower_value, const T &upper_value,
Json_array *json_array);
public:
/**
Equi-height bucket constructor.
Does nothing more than setting the member variables.
@param lower Lower inclusive value.
@param upper Upper inclusive value.
@param freq The cumulative frequency.
@param num_distinct Number of distinct values in this bucket.
*/
Bucket(T lower, T upper, double freq, ha_rows num_distinct);
/**
@return Lower inclusive value.
*/
const T &get_lower_inclusive() const { return m_lower_inclusive; }
/**
@return Upper inclusive value.
*/
const T &get_upper_inclusive() const { return m_upper_inclusive; }
/**
@return Cumulative frequency.
*/
double get_cumulative_frequency() const { return m_cumulative_frequency; }
/**
@return Number of distinct values.
*/
ha_rows get_num_distinct() const { return m_num_distinct; }
/**
Convert this equi-height bucket to a JSON array.
This function will take the contents of the current equi-height bucket
and put it in the output parameter "json_array". The result is an array
with the following contents:
Index 0: Lower inclusive value.
Index 1: Upper inclusive value.
Index 2: Cumulative frequency.
Index 3: Number of distinct values.
@param[out] json_array Output where the bucket content is to be stored.
@return True on error, false otherwise.
*/
bool bucket_to_json(Json_array *json_array) const;
/**
Finds the relative location of "value" between bucket endpoints.
This is used to determine the fraction of a bucket to include in selectivity
estimates in the case where the query value lies inside a bucket.
get_distance_from_lower returns the fraction of all elements between bucket
endpoints [a, b] that lie in the interval [a, value), i.e., strictly less
than value. For some histogram types the return value is only an estimate.
@param value The value to calculate the distance for.
@return The distance between "value" and lower inclusive value.
*/
double get_distance_from_lower(const T &value) const;
/**
Returns the fraction of all elements between bucket endpoints [a, b] that
are strictly greater than "value". For some histogram types the return value
is only an estimate.
@return The distance between "value" and upper inclusive value.
*/
double get_distance_from_upper(const T &value) const;
};
} // namespace equi_height
} // namespace histograms
#endif
|