MySQL  8.0.27
Source Code Documentation
item_sum.cc File Reference

Sum functions (COUNT, MIN...) More...

#include "sql/item_sum.h"
#include <algorithm>
#include <bitset>
#include <cstring>
#include <functional>
#include <optional>
#include <string>
#include <utility>
#include "decimal.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_byteorder.h"
#include "my_compare.h"
#include "my_dbug.h"
#include "my_double2ulonglong.h"
#include "my_sys.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/aggregate_check.h"
#include "sql/create_field.h"
#include "sql/current_thd.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/derror.h"
#include "sql/field.h"
#include "sql/gis/gc_utils.h"
#include "sql/gis/geometries.h"
#include "sql/gis/geometry_extraction.h"
#include "sql/gis/relops.h"
#include "sql/handler.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_json_func.h"
#include "sql/item_subselect.h"
#include "sql/json_dom.h"
#include "sql/key_spec.h"
#include "sql/mysqld.h"
#include "sql/parse_tree_helpers.h"
#include "sql/parse_tree_node_base.h"
#include "sql/parse_tree_nodes.h"
#include "sql/parser_yystype.h"
#include "sql/sql_array.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_exception_handler.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_resolver.h"
#include "sql/sql_select.h"
#include "sql/sql_tmp_table.h"
#include "sql/srs_fetcher.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/temp_table_param.h"
#include "sql/uniques.h"
#include "sql/window.h"

Namespaces

 anonymous_namespace{item_sum.cc}
 

Functions

static int simple_str_key_cmp (const void *arg, const void *a, const void *b)
 Compare keys consisting of single field that cannot be compared as binary. More...
 
static enum enum_field_types calc_tmp_field_type (enum enum_field_types table_field_type, Item_result result_type)
 
static int simple_raw_key_cmp (const void *arg, const void *key1, const void *key2)
 
static int item_sum_distinct_walk (void *element, element_count, void *item)
 
template<class Char_op , class Int_op >
static void apply_bit_op (size_t buff_length, const uchar *s1_c_p, uchar *value_bits)
 Helper for Item_sum_bit::add_bits(). More...
 
static void add_sample (double *m, double *s, double *s2, ulonglong *count, double nr)
 Calculates the next recurrence value s,s2 using the current sample as input. More...
 
static void remove_sample (double *m, double *s, double *s2, ulonglong *count, double nr)
 Removes the earlier calculated recurrence value s,s2 for current sample from the current s,s2 values. More...
 
static void variance_fp_recurrence_next (double *m, double *s, double *s2, ulonglong *count, double nr, bool optimize, bool inverse)
 Calculates the next recurrence value for current sample. More...
 
static double variance_fp_recurrence_result (double s, double s2, ulonglong count, bool is_sample_variance, bool optimize)
 Calculates variance using one of the two algorithms (See Item_sum_variance) as specified. More...
 
static bool min_max_best_so_far (int comparison_result, bool is_min)
 Checks if a value should replace the minimum or maximum value seen so far in the MIN and MAX aggregate functions. More...
 
int group_concat_key_cmp_with_distinct (const void *arg, const void *key1, const void *key2)
 Compares the values for fields in expr list of GROUP_CONCAT. More...
 
int group_concat_key_cmp_with_order (const void *arg, const void *key1, const void *key2)
 function of sort for syntax: GROUP_CONCAT(expr,... More...
 
int dump_leaf_key (void *key_arg, element_count count[[maybe_unused]], void *item_arg)
 Append data from current leaf to item->result. More...
 
std::unique_ptr< gis::Geometrycollectionanonymous_namespace{item_sum.cc}::filtergeometries (std::unique_ptr< gis::Geometrycollection > geometrycollection, const dd::Spatial_reference_system *srs)
 

Detailed Description

Sum functions (COUNT, MIN...)

Function Documentation

◆ add_sample()

static void add_sample ( double *  m,
double *  s,
double *  s2,
ulonglong count,
double  nr 
)
static

Calculates the next recurrence value s,s2 using the current sample as input.

m is initialized to the first sample. Its not changed for the later calls.

Parameters
[in,out]mrecurrence value
[in,out]srecurrence value
[in,out]s2Square of the recurrence value s
[in,out]countNumber of rows for which m,s,s2 is calculated
[in]nrCurrent sample

◆ apply_bit_op()

template<class Char_op , class Int_op >
static void apply_bit_op ( size_t  buff_length,
const uchar s1_c_p,
uchar value_bits 
)
inlinestatic

Helper for Item_sum_bit::add_bits().

Does value_bits = s1_c_p bit_op value_bits.

Template Parameters
Char_opclass offering a bit operation for a uchar: AND, OR or XOR
Int_opclass offering a bit operation for a ulonglong: ditto
Parameters
buff_lengthlength of s1_c_p
s1_c_pfirst argument of bit op
[in,out]value_bitssecond argument of bit op, and result

◆ calc_tmp_field_type()

static enum enum_field_types calc_tmp_field_type ( enum enum_field_types  table_field_type,
Item_result  result_type 
)
static

◆ dump_leaf_key()

int dump_leaf_key ( void *  key_arg,
element_count count [[maybe_unused],
void *  item_arg 
)

Append data from current leaf to item->result.

To avoid duplicated warnings in Item_func_group_concat::val_str()

◆ group_concat_key_cmp_with_distinct()

int group_concat_key_cmp_with_distinct ( const void *  arg,
const void *  key1,
const void *  key2 
)

Compares the values for fields in expr list of GROUP_CONCAT.

GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
@ ASC
Definition: sql_yacc.h:67
@ BY
Definition: sql_yacc.h:93
@ DISTINCT
Definition: sql_yacc.h:185
@ DESC
Definition: sql_yacc.h:176
Definition: table.h:279
Return values
-1: key1 < key2
0: key1 = key2
1: key1 > key2

◆ group_concat_key_cmp_with_order()

int group_concat_key_cmp_with_order ( const void *  arg,
const void *  key1,
const void *  key2 
)

function of sort for syntax: GROUP_CONCAT(expr,...

ORDER BY col,... )

◆ item_sum_distinct_walk()

static int item_sum_distinct_walk ( void *  element,
element_count  ,
void *  item 
)
static

◆ min_max_best_so_far()

static bool min_max_best_so_far ( int  comparison_result,
bool  is_min 
)
static

Checks if a value should replace the minimum or maximum value seen so far in the MIN and MAX aggregate functions.

Parameters
comparison_resultthe result of comparing the current value with the min/max value seen so far (negative if it's smaller, 0 if it's equal, positive if it's greater)
is_mintrue if called by MIN, false if called by MAX
Returns
true if the current value should replace the min/max value seen so far

◆ remove_sample()

static void remove_sample ( double *  m,
double *  s,
double *  s2,
ulonglong count,
double  nr 
)
static

Removes the earlier calculated recurrence value s,s2 for current sample from the current s,s2 values.

Called when do_inverse() is true.

Parameters
[in]mrecurrence value
[in,out]srecurrence value
[in,out]s2Square of the recurrence value s
[in,out]countNumber of rows for which s,s2 is calculated
[in]nrCurrent sample

◆ simple_raw_key_cmp()

static int simple_raw_key_cmp ( const void *  arg,
const void *  key1,
const void *  key2 
)
static

◆ simple_str_key_cmp()

static int simple_str_key_cmp ( const void *  arg,
const void *  a,
const void *  b 
)
static

Compare keys consisting of single field that cannot be compared as binary.

Used by the Unique class to compare keys. Will do correct comparisons for all field types.

Parameters
argPointer to the relevant Field class instance
aleft key image
bright key image
Returns
comparison result
Return values
<0 if key1 < key2
=0 if key1 = key2
>0 if key1 > key2

◆ variance_fp_recurrence_next()

static void variance_fp_recurrence_next ( double *  m,
double *  s,
double *  s2,
ulonglong count,
double  nr,
bool  optimize,
bool  inverse 
)
static

Calculates the next recurrence value for current sample.

Parameters
[in,out]mrecurrence value
[in,out]srecurrence value
[in,out]s2Square of the recurrence value s
[in,out]countNumber of rows for which m,s,s2 is calculated
[in]nrCurrent sample
[in]optimizeIf set to true is Algorithm II is used to calculate m,s and s2. Else Algorithm I is used to calculate m,s.
[in]inverseIf set to true, we use formulas from Algorithm II to remove value calculated for s,s2 for sample "nr" from the the current value of (s,s2).

Note: variance_fp_recurrence_next and variance_fp_recurrence_result are used by Item_sum_variance and Item_variance_field classes, which are unrelated, and each need to calculate variance. The difference between the two classes is that the first is used for a mundane SELECT and when used with windowing functions, while the latter is used in a GROUPing SELECT.

◆ variance_fp_recurrence_result()

static double variance_fp_recurrence_result ( double  s,
double  s2,
ulonglong  count,
bool  is_sample_variance,
bool  optimize 
)
static

Calculates variance using one of the two algorithms (See Item_sum_variance) as specified.

Parameters
[in]srecurrence value
[in]s2Square of the recurrence value. Used only by Algorithm II
[in]countNumber of rows for which variance needs to be calculated.
[in]is_sample_variancetrue if calculating sample variance and false if population variance.
[in]optimizetrue if algorthm II is used to calculate variance.
Return values
returnscalculated variance value