MySQL 8.4.0
Source Code Documentation
item.cc File Reference
#include "sql/item.h"
#include "integer_digits.h"
#include "my_compiler.h"
#include "my_config.h"
#include <stdio.h>
#include <sys/time.h>
#include <stddef.h>
#include <algorithm>
#include <optional>
#include <utility>
#include "decimal.h"
#include "dig_vec.h"
#include "float.h"
#include "limits.h"
#include "m_string.h"
#include "my_alloc.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_macros.h"
#include "myisampack.h"
#include "mysql.h"
#include "mysql/strings/dtoa.h"
#include "mysql/strings/m_ctype.h"
#include "mysql/strings/my_strtoll10.h"
#include "mysql_time.h"
#include "nulls.h"
#include "sql-common/json_dom.h"
#include "sql/aggregate_check.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/current_thd.h"
#include "sql/derror.h"
#include "sql/error_handler.h"
#include "sql/gis/srid.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_create.h"
#include "sql/item_func.h"
#include "sql/item_json_func.h"
#include "sql/item_row.h"
#include "sql/item_strfunc.h"
#include "sql/item_subselect.h"
#include "sql/item_sum.h"
#include "sql/key.h"
#include "sql/log_event.h"
#include "sql/mysqld.h"
#include "sql/opt_explain_format.h"
#include "sql/protocol.h"
#include "sql/query_options.h"
#include "sql/select_lex_visitor.h"
#include "sql/sp.h"
#include "sql/sp_rcontext.h"
#include "sql/sql_base.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_class.h"
#include "sql/sql_derived.h"
#include "sql/sql_error.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_show.h"
#include "sql/sql_time.h"
#include "sql/sql_view.h"
#include "sql/system_variables.h"
#include "sql/thd_raii.h"
#include "sql/tztime.h"
#include "string_with_len.h"
#include "strmake.h"
#include "strxmov.h"
#include "template_utils.h"
#include "typelib.h"
#include "unsafe_string_append.h"

Classes

class  Item_aggregate_ref
 

Functions

static bool select_alias_referencable (enum_parsing_context place)
 Alias from select list can be referenced only from ORDER BY (SQL Standard) or from HAVING, GROUP BY, QUALIFY and a subquery in the select list (MySQL extension). More...
 
static enum_field_types real_data_type (Item *item)
 Find real data type of item. More...
 
void item_init (void)
 Init all special items. More...
 
static bool left_is_superset (DTCollation *left, DTCollation *right)
 
static void my_coll_agg_error (DTCollation &c1, DTCollation &c2, const char *fname)
 
static void my_coll_agg_error (DTCollation &c1, DTCollation &c2, DTCollation &c3, const char *fname)
 
static void my_coll_agg_error (Item **args, uint count, const char *fname, int item_sep)
 
static bool agg_item_collations (DTCollation &c, const char *fname, Item **av, uint count, uint flags, int item_sep)
 
bool agg_item_collations_for_comparison (DTCollation &c, const char *fname, Item **av, uint count, uint flags)
 
bool agg_item_set_converter (DTCollation &coll, const char *fname, Item **args, uint nargs, uint, int item_sep, bool only_consts)
 
bool agg_item_charsets (DTCollation &coll, const char *fname, Item **args, uint nargs, uint flags, int item_sep, bool only_consts)
 
static uint32 adjust_max_effective_column_length (Field *field_par, uint32 max_length)
 Calculate the max column length not taking into account the limitations over integer types. More...
 
double double_from_string_with_check (const CHARSET_INFO *cs, const char *cptr, const char *end)
 
longlong longlong_from_string_with_check (const CHARSET_INFO *cs, const char *cptr, const char *end, int unsigned_target)
 Converts a string to a longlong integer, with warnings. More...
 
static void mark_as_dependent (THD *thd, Query_block *last, Query_block *current, Item_ident *resolved_item, Item_ident *mark_item)
 Mark item and Query_blocks as dependent if item was resolved in outer SELECT. More...
 
static Item ** find_field_in_group_list (Item *find_item, ORDER *group_list)
 Search a GROUP BY clause for a field with a certain name. More...
 
static bool resolve_ref_in_select_and_group (THD *thd, Item_ident *ref, Query_block *select, Item ***found)
 Resolve a column reference in a query block. More...
 
bool is_null_on_empty_table (THD *thd, Item_field *i)
 Check if the column reference that is currently being resolved, will be set to NULL if its qualifying query returns zero rows. More...
 
static void convert_zerofill_number_to_string (Item **item, const Field_num *field)
 Convert a numeric value to a zero-filled string. More...
 
static type_conversion_status field_conv_with_cache (Field *to, Field *from, Field **last_to, uint32_t *to_is_memcpyable)
 Copies/converts data from “from” to “to”, but is faster on repeated execution with the same “to” field, as it caches the fields_are_memcpyable() and pack_length() calls. More...
 
static type_conversion_status save_int_value_in_field (Field *field, longlong nr, bool null_value, bool unsigned_flag)
 Store an int in a field. More...
 
static uint nr_of_decimals (const char *str, const char *end)
 
static uint char_val (char X)
 
Item_result item_cmp_type (Item_result a, Item_result b)
 
bool resolve_const_item (THD *thd, Item **ref, Item *comp_item)
 Substitute a const item with a simpler const item, if possible. More...
 
int stored_field_cmp_to_item (THD *thd, Field *field, Item *item)
 Compare the value stored in field with the expression from the query. More...
 
static const char * whence (const Item_field *cached_field)
 Get the name of the cached field of an Item_cache_json instance. More...
 
void convert_and_print (const String *from_str, String *to_str, const CHARSET_INFO *to_cs)
 Helper method: Convert string to the given charset, then print. More...
 
string ItemToString (const Item *item)
 
Item_fieldFindEqualField (Item_field *item_field, table_map reachable_tables, bool replace, bool *found)
 
static const ItemUnwrapArgForEq (const Item *item)
 Unwrap an Item argument so that Item::eq() can see the "real" item, and not just the wrapper. More...
 
bool ItemsAreEqual (const Item *a, const Item *b, bool binary_cmp)
 Returns true iff the two items are equal, as in a->eq(b), after unwrapping refs and Item_cache objects. More...
 
bool AllItemsAreEqual (const Item *const *a, const Item *const *b, int num_items, bool binary_cmp)
 Returns true iff all items in the two arrays (which must be of the same size) are equal, as in a->eq(b), after unwrapping refs and Item_cache objects. More...
 

Variables

const String my_null_string ("NULL", 4, default_charset_info)
 
const Name_string null_name_string (nullptr, 0)
 

Function Documentation

◆ adjust_max_effective_column_length()

static uint32 adjust_max_effective_column_length ( Field field_par,
uint32  max_length 
)
inlinestatic

Calculate the max column length not taking into account the limitations over integer types.

When storing data into fields the server currently just ignores the limits specified on integer types, e.g. 1234 can safely be stored in an int(2) and will not cause an error. Thus when creating temporary tables and doing transformations we must adjust the maximum field length to reflect this fact. We take the un-restricted maximum length and adjust it similarly to how the declared length is adjusted wrt unsignedness etc. TODO: this all needs to go when we disable storing 1234 in int(2).

Parameters
field_parOriginal field the use to calculate the lengths
max_lengthItem's calculated explicit max length
Returns
The adjusted max length

◆ agg_item_charsets()

bool agg_item_charsets ( DTCollation coll,
const char *  fname,
Item **  args,
uint  nargs,
uint  flags,
int  item_sep,
bool  only_consts 
)

◆ agg_item_collations()

static bool agg_item_collations ( DTCollation c,
const char *  fname,
Item **  av,
uint  count,
uint  flags,
int  item_sep 
)
static

◆ agg_item_collations_for_comparison()

bool agg_item_collations_for_comparison ( DTCollation c,
const char *  fname,
Item **  av,
uint  count,
uint  flags 
)

◆ agg_item_set_converter()

bool agg_item_set_converter ( DTCollation coll,
const char *  fname,
Item **  args,
uint  nargs,
uint  flags,
int  item_sep,
bool  only_consts 
)

◆ AllItemsAreEqual()

bool AllItemsAreEqual ( const Item *const *  a,
const Item *const *  b,
int  num_items,
bool  binary_cmp 
)

Returns true iff all items in the two arrays (which must be of the same size) are equal, as in a->eq(b), after unwrapping refs and Item_cache objects.

◆ char_val()

static uint char_val ( char  X)
inlinestatic

◆ convert_and_print()

void convert_and_print ( const String from_str,
String to_str,
const CHARSET_INFO to_cs 
)

Helper method: Convert string to the given charset, then print.

Parameters
from_strString to be converted.
to_strQuery string.
to_csCharacter set to which the string is to be converted.

◆ convert_zerofill_number_to_string()

static void convert_zerofill_number_to_string ( Item **  item,
const Field_num field 
)
static

Convert a numeric value to a zero-filled string.

Parameters
[in,out]itemthe item to operate on
fieldThe field that this value is equated to

This function converts a numeric value to a string. In this conversion the zero-fill flag of the field is taken into account. This is required so the resulting string value can be used instead of the field reference when propagating equalities.

◆ double_from_string_with_check()

double double_from_string_with_check ( const CHARSET_INFO cs,
const char *  cptr,
const char *  end 
)

◆ field_conv_with_cache()

static type_conversion_status field_conv_with_cache ( Field to,
Field from,
Field **  last_to,
uint32_t *  to_is_memcpyable 
)
inlinestatic

Copies/converts data from “from” to “to”, but is faster on repeated execution with the same “to” field, as it caches the fields_are_memcpyable() and pack_length() calls.

These are not terribly expensive in themselves, but it adds up to 5–10% in DBT-3 Q1 due to the repeated calls.

The “from” field must correspond to the same last_to / to_is_memcpyable pair as earlier calls, unless last_to is cleared to nullptr.

◆ find_field_in_group_list()

static Item ** find_field_in_group_list ( Item find_item,
ORDER group_list 
)
static

Search a GROUP BY clause for a field with a certain name.

Search the GROUP BY list for a column named as find_item. When searching preference is given to columns that are qualified with the same table (and database) name as the one being searched for.

Parameters
find_itemthe item being searched for
group_listGROUP BY clause
Returns
  • the found item on success
  • NULL if find_item is not in group_list

◆ FindEqualField()

Item_field * FindEqualField ( Item_field item_field,
table_map  reachable_tables,
bool  replace,
bool *  found 
)

◆ is_null_on_empty_table()

bool is_null_on_empty_table ( THD thd,
Item_field i 
)

Check if the column reference that is currently being resolved, will be set to NULL if its qualifying query returns zero rows.

This is true for non-aggregated column references in the SELECT list, if the query block uses aggregation without grouping. For example:

SELECT COUNT(*), col FROM t WHERE some_condition

Here, if the table t is empty, or some_condition doesn't match any rows in t, the query returns one row where col is NULL, even if col is a not-nullable column.

Such column references are rejected if the ONLY_FULL_GROUP_BY SQL mode is enabled, in a later resolution phase.

◆ item_cmp_type()

Item_result item_cmp_type ( Item_result  a,
Item_result  b 
)

◆ item_init()

void item_init ( void  )

Init all special items.

◆ ItemsAreEqual()

bool ItemsAreEqual ( const Item a,
const Item b,
bool  binary_cmp 
)

Returns true iff the two items are equal, as in a->eq(b), after unwrapping refs and Item_cache objects.

◆ ItemToString()

string ItemToString ( const Item item)

◆ left_is_superset()

static bool left_is_superset ( DTCollation left,
DTCollation right 
)
static

◆ longlong_from_string_with_check()

longlong longlong_from_string_with_check ( const CHARSET_INFO cs,
const char *  cptr,
const char *  end,
int  unsigned_target 
)

Converts a string to a longlong integer, with warnings.

Parameters
cscharset of string
cptrbeginning of string
endend of string
unsigned_targetIf 0, caller will use result as a signed integer; if 1: an unsigned integer; if -1: caller doesn't tell. This influences warnings.

◆ mark_as_dependent()

static void mark_as_dependent ( THD thd,
Query_block last,
Query_block current,
Item_ident resolved_item,
Item_ident mark_item 
)
static

Mark item and Query_blocks as dependent if item was resolved in outer SELECT.

Parameters
thdCurrent session.
lastselect from which current item depend
currentcurrent select
resolved_itemitem which was resolved in outer SELECT
mark_itemitem which should be marked; resolved_item will be marked anyway.

◆ my_coll_agg_error() [1/3]

static void my_coll_agg_error ( DTCollation c1,
DTCollation c2,
const char *  fname 
)
static

◆ my_coll_agg_error() [2/3]

static void my_coll_agg_error ( DTCollation c1,
DTCollation c2,
DTCollation c3,
const char *  fname 
)
static

◆ my_coll_agg_error() [3/3]

static void my_coll_agg_error ( Item **  args,
uint  count,
const char *  fname,
int  item_sep 
)
static

◆ nr_of_decimals()

static uint nr_of_decimals ( const char *  str,
const char *  end 
)
static

◆ real_data_type()

static enum_field_types real_data_type ( Item item)
static

Find real data type of item.

Returns
data type which should be used to store item value

◆ resolve_const_item()

bool resolve_const_item ( THD thd,
Item **  ref,
Item comp_item 
)

Substitute a const item with a simpler const item, if possible.

Parameters
thdCurrent session.
[in,out]refConst item to be processed, contains simplest possible item on return.
comp_itemItem that provides result type for generated const item
Returns
false if success, true if error

◆ resolve_ref_in_select_and_group()

static bool resolve_ref_in_select_and_group ( THD thd,
Item_ident ref,
Query_block select,
Item ***  found 
)
static

Resolve a column reference in a query block.

Resolve a column reference (usually inside a HAVING clause) against the SELECT and GROUP BY clauses of the query described by 'select'. The name resolution algorithm searches both the SELECT and GROUP BY clauses, and in case of a name conflict prefers GROUP BY column names over SELECT names. If both clauses contain different fields with the same names, a warning is issued that name of 'ref' is ambiguous. We extend ANSI SQL in that when no GROUP BY column is found, then a HAVING name is resolved as a possibly derived SELECT column.

Parameters
thdcurrent thread
refcolumn reference being resolved
selectthe select that ref is resolved against
[out]foundpointer to the found item = nullptr: item was not found
Note
The resolution procedure is:
  • Search for a column or derived column named col_ref_i [in table T_j] in the SELECT clause of Q.
  • Search for a column named col_ref_i [in table T_j] in the GROUP BY clause of Q.
  • If found different columns with the same name in GROUP BY and SELECT, issue a warning
  • return the found GROUP BY column if any,
  • else return the found SELECT column if any.
Returns
false if success, true if error In case of successful return, the value of "found" indicates whether the column was found or not.

◆ save_int_value_in_field()

static type_conversion_status save_int_value_in_field ( Field field,
longlong  nr,
bool  null_value,
bool  unsigned_flag 
)
static

Store an int in a field.

Parameters
fieldThe field where the int value is to be stored
nrThe value to store in field
null_valueTrue if the value to store is NULL, false otherwise
unsigned_flagWhether or not the int value is signed or unsigned
Return values
TYPE_OKStoring of value went fine without warnings or errors
!TYPE_OKWarning/error as indicated by type_conversion_status enum value

◆ select_alias_referencable()

static bool select_alias_referencable ( enum_parsing_context  place)
inlinestatic

Alias from select list can be referenced only from ORDER BY (SQL Standard) or from HAVING, GROUP BY, QUALIFY and a subquery in the select list (MySQL extension).

We don't allow it be referenced from the SELECT list, with one exception: it's accepted if nested in a subquery, which is inconsistent but necessary as our users have shown to rely on this workaround.

◆ stored_field_cmp_to_item()

int stored_field_cmp_to_item ( THD thd,
Field field,
Item item 
)

Compare the value stored in field with the expression from the query.

Parameters
thdCurrent session.
fieldField which the Item is stored in after conversion
itemOriginal expression from query
Returns
Returns an integer greater than, equal to, or less than 0 if the value stored in the field is greater than, equal to, or less than the original Item. A 0 may also be returned if out of memory.
Note
We use this in the range optimizer/partition pruning, because in some cases we can't store the value in the field without some precision/character loss.

We similarly use it to verify that expressions like BIGINT_FIELD <cmp> <literal value> is done correctly (as int/decimal/float according to literal type).

◆ UnwrapArgForEq()

static const Item * UnwrapArgForEq ( const Item item)
static

Unwrap an Item argument so that Item::eq() can see the "real" item, and not just the wrapper.

It unwraps Item_ref using real_item(), and also cache items and rollup group wrappers, since these may not have been added consistently to both sides compared by Item::eq().

◆ whence()

static const char * whence ( const Item_field cached_field)
inlinestatic

Get the name of the cached field of an Item_cache_json instance.

Variable Documentation

◆ my_null_string

const String my_null_string("NULL", 4, default_charset_info) ( "NULL"  ,
,
default_charset_info   
)

◆ null_name_string

const Name_string null_name_string(nullptr, 0) ( nullptr  ,
 
)