MySQL 8.0.39
Source Code Documentation
item_func.cc File Reference

This file defines all numerical Items. More...

#include "sql/item_func.h"
#include <algorithm>
#include <atomic>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <initializer_list>
#include <iosfwd>
#include <limits>
#include <memory>
#include <new>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include "integer_digits.h"
#include "m_string.h"
#include "map_helpers.h"
#include "mutex_lock.h"
#include "my_bit.h"
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_dbug.h"
#include "my_double2ulonglong.h"
#include "my_hostname.h"
#include "my_loglevel.h"
#include "my_psi_config.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "my_systime.h"
#include "my_thread.h"
#include "my_user.h"
#include "mysql/components/services/bits/mysql_cond_bits.h"
#include "mysql/components/services/bits/mysql_mutex_bits.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/components/services/bits/psi_mutex_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/mysql_lex_string.h"
#include "mysql/plugin_audit.h"
#include "mysql/psi/mysql_cond.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/service_mysql_password_policy.h"
#include "mysql/service_thd_wait.h"
#include "mysql/status_var.h"
#include "prealloced_array.h"
#include "sql-common/json_dom.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/binlog.h"
#include "sql/check_stack.h"
#include "sql/current_thd.h"
#include "sql/dd/info_schema/table_stats.h"
#include "sql/dd/info_schema/tablespace_stats.h"
#include "sql/dd/object_id.h"
#include "sql/dd/properties.h"
#include "sql/dd/types/abstract_table.h"
#include "sql/dd/types/column.h"
#include "sql/dd/types/index.h"
#include "sql/dd_sql_view.h"
#include "sql/dd_table_share.h"
#include "sql/debug_sync.h"
#include "sql/derror.h"
#include "sql/error_handler.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_json_func.h"
#include "sql/item_strfunc.h"
#include "sql/item_subselect.h"
#include "sql/key.h"
#include "sql/log_event.h"
#include "sql/mdl.h"
#include "sql/mysqld.h"
#include "sql/parse_tree_helpers.h"
#include "sql/parse_tree_node_base.h"
#include "sql/protocol.h"
#include "sql/psi_memory_key.h"
#include "sql/resourcegroups/resource_group.h"
#include "sql/resourcegroups/resource_group_basic_types.h"
#include "sql/resourcegroups/resource_group_mgr.h"
#include "sql/rpl_gtid.h"
#include "sql/rpl_mi.h"
#include "sql/rpl_msr.h"
#include "sql/rpl_rli.h"
#include "sql/sp.h"
#include "sql/sp_head.h"
#include "sql/sp_pcontext.h"
#include "sql/sql_array.h"
#include "sql/sql_audit.h"
#include "sql/sql_base.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_class.h"
#include "sql/sql_cmd.h"
#include "sql/sql_derived.h"
#include "sql/sql_error.h"
#include "sql/sql_exchange.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_load.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_parse.h"
#include "sql/sql_show.h"
#include "sql/sql_time.h"
#include "sql/strfunc.h"
#include "sql/system_variables.h"
#include "sql/thd_raii.h"
#include "sql/val_int_compare.h"
#include "sql_string.h"
#include "template_utils.h"
#include "thr_mutex.h"

Classes

class  Interruptible_wait
 Enables a session to wait on a condition until a timeout or a network disconnect occurs. More...
 
struct  User_level_lock
 For locks with EXPLICIT duration, MDL returns a new ticket every time a lock is granted. More...
 
class  User_level_lock_wait_error_handler
 When MDL detects a lock wait timeout, it pushes an error into the statement diagnostics area. More...
 
class  MDL_lock_get_owner_thread_id_visitor
 

Functions

static void free_user_var (user_var_entry *entry)
 
static int get_var_with_binlog (THD *thd, enum_sql_command sql_command, const Name_string &name, user_var_entry **out_entry)
 Get variable by name and, if necessary, put the record of variable use into the binary log. More...
 
bool check_reserved_words (const char *name)
 
void report_conversion_error (const CHARSET_INFO *to_cs, const char *from, size_t from_length, const CHARSET_INFO *from_cs)
 
bool simplify_string_args (THD *thd, const DTCollation &c, Item **args, uint nargs)
 Simplify the string arguments to a function, if possible. More...
 
Stringeval_string_arg_noinline (const CHARSET_INFO *to_cs, Item *arg, String *buffer)
 Evaluate an argument string and return it in the desired character set. More...
 
bool eval_const_cond (THD *thd, Item *cond, bool *value)
 Evaluate a constant condition, represented by an Item tree. More...
 
static bool test_if_sum_overflows_ull (ulonglong arg1, ulonglong arg2)
 Test if the sum of arguments overflows the ulonglong range. More...
 
bool param_type_uses_non_param_inner (THD *thd, uint arg_count, Item **args, enum_field_types def)
 For arguments of this Item_func ("args" array), all of them: find an argument that is not a dynamic parameter; if found, all dynamic parameters without a valid type get the type of this; if not found, they get type "def". More...
 
bool is_function_of_type (const Item *item, Item_func::Functype type)
 Checks if "item" is a function of the specified type. More...
 
bool contains_function_of_type (Item *item, Item_func::Functype type)
 Checks if "item" contains a function of the specified type. More...
 
Item_fieldget_gc_for_expr (const Item *func, Field *fld, Item_result type, Field **found)
 Return new Item_field if given expression matches GC. More...
 
static bool substitute_gc_expression (Item **expr, Item **value, List< Field > *gc_fields, Item_result type, Item_func *predicate)
 Attempt to substitute an expression with an equivalent generated column in a predicate. More...
 
static void gc_subst_overlaps_contains (Item **func, Item **vals, Json_wrapper &vals_wr, List< Field > *gc_fields)
 A helper function for Item_func::gc_subst_transformer, that tries to substitute the given JSON_CONTAINS or JSON_OVERLAPS function for one of GCs from the provided list. More...
 
bool reject_geometry_args (uint arg_count, Item **args, Item_result_field *me)
 
void unsupported_json_comparison (size_t arg_count, Item **args, const char *msg)
 Go through the arguments of a function and check if any of them are JSON. More...
 
static longlong val_int_from_str (Item *item, bool unsigned_flag, bool *null_value)
 
template longlong Item_func_bit_two_param::eval_int_op< std::bit_or< ulonglong > > (std::bit_or< ulonglong >)
 Instantiations of the above. More...
 
template longlong Item_func_bit_two_param::eval_int_op< std::bit_and< ulonglong > > (std::bit_and< ulonglong >)
 
template longlong Item_func_bit_two_param::eval_int_op< std::bit_xor< ulonglong > > (std::bit_xor< ulonglong >)
 
double my_double_round (double value, longlong dec, bool dec_unsigned, bool truncate)
 
static ulonglong my_unsigned_round (ulonglong value, ulonglong to, bool *round_overflow)
 
static int temporal_rank (enum_field_types type)
 
void mysql_ull_cleanup (THD *thd)
 Release all user level locks for this THD. More...
 
void mysql_ull_set_explicit_lock_duration (THD *thd)
 Set explicit duration for metadata locks corresponding to user level locks to protect them from being released at the end of transaction. More...
 
static bool check_and_convert_ull_name (char *buff, const String *org_name)
 Helper function which checks if user-level lock name is acceptable and converts it to system charset (utf8). More...
 
static void init_item_func_sleep_psi_keys ()
 
void item_func_sleep_init ()
 
void item_func_sleep_free ()
 
static user_var_entryget_variable (THD *thd, const Name_string &name, const CHARSET_INFO *cs)
 Get variable with given name; conditionally create it if non-existing. More...
 
static void update_table_read_set (const Field *field)
 Add field into table read set. More...
 
Itemget_system_variable (Parse_context *pc, enum_var_type scope, const LEX_CSTRING &prefix, const LEX_CSTRING &suffix, bool unsafe_for_replication)
 Return value of an system variable base[.name] as a constant item. More...
 
static void my_missing_function_error (const LEX_STRING &token, const char *func_name)
 
void uuid_short_init ()
 
static bool is_hidden_by_ndb (THD *thd, String *schema_name, String *table_name)
 Check if schema and table are hidden by NDB engine. More...
 
static bool check_table_and_trigger_access (Item **args, bool check_trigger_acl, bool *null_value)
 
static ulonglong get_table_statistics (Item **args, uint arg_count, dd::info_schema::enum_table_stats_type stype, bool *null_value)
 Get table statistics from dd::info_schema::get_table_statistics. More...
 
void retrieve_tablespace_statistics (THD *thd, Item **args, bool *null_value)
 Retrieve tablespace statistics from SE. More...
 

Variables

static const char separator = ','
 
mysql_mutex_t LOCK_item_func_sleep
 Lock which is used to implement interruptible wait for SLEEP() function. More...
 
static PSI_mutex_key key_LOCK_item_func_sleep
 
static PSI_mutex_info item_func_sleep_mutexes []
 
static bool item_func_sleep_inited = false
 
ulonglong uuid_value
 

Detailed Description

This file defines all numerical Items.

Function Documentation

◆ check_and_convert_ull_name()

static bool check_and_convert_ull_name ( char *  buff,
const String org_name 
)
static

Helper function which checks if user-level lock name is acceptable and converts it to system charset (utf8).

Error is emitted if name is not acceptable. Name is also lowercased to ensure that user-level lock names are treated in case-insensitive fashion even though MDL subsystem which used by implementation does binary comparison of keys.

Parameters
buffBuffer for lowercased name in system charset of NAME_LEN + 1 bytes length.
org_nameOriginal string passed as name parameter to user-level lock function.
Returns
True in case of error, false on success.

◆ check_reserved_words()

bool check_reserved_words ( const char *  name)

◆ check_table_and_trigger_access()

static bool check_table_and_trigger_access ( Item **  args,
bool  check_trigger_acl,
bool *  null_value 
)
static

◆ contains_function_of_type()

bool contains_function_of_type ( Item item,
Item_func::Functype  type 
)

Checks if "item" contains a function of the specified type.

◆ eval_const_cond()

bool eval_const_cond ( THD thd,
Item cond,
bool *  value 
)

Evaluate a constant condition, represented by an Item tree.

Parameters
thdThread handler
condThe constant condition to evaluate
[out]valueReturned value, either true or false
Returns
false if evaluation is successful, true otherwise

◆ eval_string_arg_noinline()

String * eval_string_arg_noinline ( const CHARSET_INFO to_cs,
Item arg,
String buffer 
)

Evaluate an argument string and return it in the desired character set.

Perform character set conversion if needed. Perform character set validation (from a binary string) if needed.

Parameters
to_csThe desired character set
argArgument to evaluate as a string value
bufferString buffer where argument is evaluated, if necessary
Returns
string pointer if success, NULL if error or NULL value

◆ free_user_var()

static void free_user_var ( user_var_entry entry)
static

◆ gc_subst_overlaps_contains()

static void gc_subst_overlaps_contains ( Item **  func,
Item **  vals,
Json_wrapper vals_wr,
List< Field > *  gc_fields 
)
static

A helper function for Item_func::gc_subst_transformer, that tries to substitute the given JSON_CONTAINS or JSON_OVERLAPS function for one of GCs from the provided list.

The function checks whether there's an index with matching expression and whether all scalars for lookup can be coerced to index's GC field without errors. If so, index's GC field substitutes the given function, args are replaced for array of coerced values in order to match GC's type. substitute_gc_expression() can't be used to these functions as it's tailored to handle regular single-valued indexes and doesn't ensure correct coercion of all values to lookup in multi-valued index.

Parameters
funcFunction to replace
valsArgs to replace
vals_wrJson_wrapper containing array of values for index lookup
gc_fieldsList of generated fields to look the function's substitute in

◆ get_gc_for_expr()

Item_field * get_gc_for_expr ( const Item func,
Field fld,
Item_result  type,
Field **  found 
)

Return new Item_field if given expression matches GC.

See also
substitute_gc()
Parameters
funcExpression to be replaced
fldGCs field
typeResult type to match with Field
[out]foundIf given, just return found field, without Item_field
Returns
item new Item_field for matched GC NULL otherwise

◆ get_system_variable()

Item * get_system_variable ( Parse_context pc,
enum_var_type  scope,
const LEX_CSTRING prefix,
const LEX_CSTRING suffix,
bool  unsafe_for_replication 
)

Return value of an system variable base[.name] as a constant item.

Create new Item_func_get_system_var object.

Parameters
pcCurrent parse context
scopeGlobal / session
prefixOptional prefix part of the variable name
suffixTrivial name of suffix part of the variable name
unsafe_for_replicationIf true and if the variable is written to a binlog then mark the statement as unsafe.
Note
If component.str = 0 then the variable name is in 'name'
Returns
  • 0 : error
  • # : constant item

◆ get_table_statistics()

static ulonglong get_table_statistics ( Item **  args,
uint  arg_count,
dd::info_schema::enum_table_stats_type  stype,
bool *  null_value 
)
static

Get table statistics from dd::info_schema::get_table_statistics.

Parameters
argsList of parameters in following order,
                   - Schema_name
                   - Table_name
                   - Engine_name
                   - se_private_id
                   - Hidden_table
                   - Tablespace_se_private_data
                   - Table_se_private_data (Used if stype is AUTO_INC)
                   - Partition name (optional argument).
arg_countNumber of arguments in 'args'
stypeType of statistics that is requested
[out]null_valueMarked true indicating NULL, if there is no value.
Returns
ulonglong representing the statistics requested.

◆ get_var_with_binlog()

static int get_var_with_binlog ( THD thd,
enum_sql_command  sql_command,
const Name_string name,
user_var_entry **  out_entry 
)
static

Get variable by name and, if necessary, put the record of variable use into the binary log.

When a user variable is invoked from an update query (INSERT, UPDATE etc), stores this variable and its value in thd->user_var_events, so that it can be written to the binlog (will be written just before the query is written, see log.cc).

Parameters
thdCurrent session.
sql_commandThe command the variable participates in.
nameVariable name
[out]out_entryvariable structure or NULL. The pointer is set regardless of whether function succeeded or not.
Return values
0OK
1Failed to put appropriate record into binary log

◆ get_variable()

static user_var_entry * get_variable ( THD thd,
const Name_string name,
const CHARSET_INFO cs 
)
static

Get variable with given name; conditionally create it if non-existing.

Parameters
thdthread context
namename of user variable
cscharacter set; = NULL: Do not create variable if non-existing. != NULL: Create variable with this character set.
Returns
pointer to variable entry. = NULL: variable does not exist (if cs == NULL), or could not create variable (if cs != NULL)

◆ init_item_func_sleep_psi_keys()

static void init_item_func_sleep_psi_keys ( )
static

◆ is_function_of_type()

bool is_function_of_type ( const Item item,
Item_func::Functype  type 
)

Checks if "item" is a function of the specified type.

◆ is_hidden_by_ndb()

static bool is_hidden_by_ndb ( THD thd,
String schema_name,
String table_name 
)
inlinestatic

Check if schema and table are hidden by NDB engine.

Parameters
thdThread handle.
schema_nameSchema name.
table_nameTable name.
Return values
trueIf schema and table are hidden by NDB.
falseIf schema and table are not hidden by NDB.

◆ Item_func_bit_two_param::eval_int_op< std::bit_and< ulonglong > >()

template longlong Item_func_bit_two_param::eval_int_op< std::bit_and< ulonglong > > ( std::bit_and< ulonglong )

◆ Item_func_bit_two_param::eval_int_op< std::bit_or< ulonglong > >()

template longlong Item_func_bit_two_param::eval_int_op< std::bit_or< ulonglong > > ( std::bit_or< ulonglong )

Instantiations of the above.

◆ Item_func_bit_two_param::eval_int_op< std::bit_xor< ulonglong > >()

template longlong Item_func_bit_two_param::eval_int_op< std::bit_xor< ulonglong > > ( std::bit_xor< ulonglong )

◆ item_func_sleep_free()

void item_func_sleep_free ( )

◆ item_func_sleep_init()

void item_func_sleep_init ( )

◆ my_double_round()

double my_double_round ( double  value,
longlong  dec,
bool  dec_unsigned,
bool  truncate 
)

◆ my_missing_function_error()

static void my_missing_function_error ( const LEX_STRING token,
const char *  func_name 
)
static

◆ my_unsigned_round()

static ulonglong my_unsigned_round ( ulonglong  value,
ulonglong  to,
bool *  round_overflow 
)
inlinestatic

◆ mysql_ull_cleanup()

void mysql_ull_cleanup ( THD thd)

Release all user level locks for this THD.

◆ mysql_ull_set_explicit_lock_duration()

void mysql_ull_set_explicit_lock_duration ( THD thd)

Set explicit duration for metadata locks corresponding to user level locks to protect them from being released at the end of transaction.

◆ param_type_uses_non_param_inner()

bool param_type_uses_non_param_inner ( THD thd,
uint  arg_count,
Item **  args,
enum_field_types  def 
)
inline

For arguments of this Item_func ("args" array), all of them: find an argument that is not a dynamic parameter; if found, all dynamic parameters without a valid type get the type of this; if not found, they get type "def".

Parameters
thdthread handler
arg_countnumber of arguments to check
argsarray of arguments, size 'arg_count'
defdefault type
Returns
false if success, true if error

◆ reject_geometry_args()

bool reject_geometry_args ( uint  arg_count,
Item **  args,
Item_result_field me 
)

◆ report_conversion_error()

void report_conversion_error ( const CHARSET_INFO to_cs,
const char *  from,
size_t  from_length,
const CHARSET_INFO from_cs 
)

◆ retrieve_tablespace_statistics()

void retrieve_tablespace_statistics ( THD thd,
Item **  args,
bool *  null_value 
)

Retrieve tablespace statistics from SE.

Parameters
thdThe current thread.
argsList of parameters in following order,
                   - Tablespace_name
                   - Engine_name
                   - Tablespace_se_private_data
[out]null_valueMarked true indicating NULL, if there is no value.

◆ simplify_string_args()

bool simplify_string_args ( THD thd,
const DTCollation c,
Item **  args,
uint  nargs 
)

Simplify the string arguments to a function, if possible.

Currently used to substitute const values with character strings in the desired character set. Only used during resolving.

Parameters
thdthread handler
cDesired character set and collation
argsPointer to argument array
nargsNumber of arguments to process
Returns
false if success, true if error

◆ substitute_gc_expression()

static bool substitute_gc_expression ( Item **  expr,
Item **  value,
List< Field > *  gc_fields,
Item_result  type,
Item_func predicate 
)
static

Attempt to substitute an expression with an equivalent generated column in a predicate.

Parameters
exprthe expression that should be substituted
valueif given, value will be coerced to GC field's type and the result will substitute the original value. Used by multi-valued index.
gc_fieldslist of indexed generated columns to check for equivalence with the expression
typethe acceptable type of the generated column that replaces the expression
predicatethe predicate in which the substitution is done
Returns
true on error, false on success

◆ temporal_rank()

static int temporal_rank ( enum_field_types  type)
static

◆ test_if_sum_overflows_ull()

static bool test_if_sum_overflows_ull ( ulonglong  arg1,
ulonglong  arg2 
)
inlinestatic

Test if the sum of arguments overflows the ulonglong range.

◆ unsupported_json_comparison()

void unsupported_json_comparison ( size_t  arg_count,
Item **  args,
const char *  msg 
)

Go through the arguments of a function and check if any of them are JSON.

If a JSON argument is found, raise a warning saying that this operation is not supported yet. This function is used to notify users that they are comparing JSON values using a mechanism that has not yet been updated to use the JSON comparator. JSON values are typically handled as strings in that case.

Parameters
arg_countthe number of arguments
argsthe arguments to go through looking for JSON values
msgthe message that explains what is not supported

◆ update_table_read_set()

static void update_table_read_set ( const Field field)
static

Add field into table read set.

Parameters
fieldfield to be added to the table read set.

◆ uuid_short_init()

void uuid_short_init ( )

◆ val_int_from_str()

static longlong val_int_from_str ( Item item,
bool  unsigned_flag,
bool *  null_value 
)
static

Variable Documentation

◆ item_func_sleep_inited

bool item_func_sleep_inited = false
static

◆ item_func_sleep_mutexes

PSI_mutex_info item_func_sleep_mutexes[]
static
Initial value:
= {
{&key_LOCK_item_func_sleep, "LOCK_item_func_sleep", PSI_FLAG_SINGLETON, 0,
#define PSI_DOCUMENT_ME
Definition: component_common.h:29
#define PSI_FLAG_SINGLETON
Singleton flag.
Definition: component_common.h:35
static PSI_mutex_key key_LOCK_item_func_sleep
Definition: item_func.cc:5735

◆ key_LOCK_item_func_sleep

PSI_mutex_key key_LOCK_item_func_sleep
static

◆ LOCK_item_func_sleep

mysql_mutex_t LOCK_item_func_sleep

Lock which is used to implement interruptible wait for SLEEP() function.

◆ separator

const char separator = ','
static

◆ uuid_value

ulonglong uuid_value