MySQL  8.0.18
Source Code Documentation
item.cc File Reference
#include "sql/item.h"
#include "my_config.h"
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <utility>
#include "decimal.h"
#include "float.h"
#include "limits.h"
#include "my_alloc.h"
#include "my_dbug.h"
#include "my_macros.h"
#include "mysql.h"
#include "mysql_time.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/json_dom.h"
#include "sql/key.h"
#include "sql/log_event.h"
#include "sql/mysqld.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_error.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 "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 and a subquery in the select list (MySQL extension). 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 agg_item_charsets (DTCollation &coll, const char *fname, Item **args, uint nargs, uint flags, int item_sep)
 
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)
 
static void default_set_param_func (Item_param *param, uchar **pos, ulong len)
 Default function of Item_param::set_param_func, so in case of malformed packet the server won't SIGSEGV. 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 void mark_as_dependent (THD *thd, SELECT_LEX *last, SELECT_LEX *current, Item_ident *resolved_item, Item_ident *mark_item)
 Mark item and SELECT_LEXs 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 Item ** resolve_ref_in_select_and_group (THD *thd, Item_ident *ref, SELECT_LEX *select)
 Resolve a column reference in a sub-select. 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, Field_num *field)
 Convert a numeric value to a zero-filled string. More...
 
static uint nr_of_decimals (const char *str, const char *end)
 
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 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)
 

Variables

const String my_null_string ("NULL", 4, default_charset_info)
 
const Name_string null_name_string (NULL, 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 
)

◆ 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  ,
int  item_sep 
)

◆ char_val()

uint char_val ( char  X)
inline

◆ 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,
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.

◆ default_set_param_func()

static void default_set_param_func ( Item_param param,
uchar **  pos,
ulong  len 
)
static

Default function of Item_param::set_param_func, so in case of malformed packet the server won't SIGSEGV.

◆ double_from_string_with_check()

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

◆ 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

◆ 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.

◆ 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 
)

◆ mark_as_dependent()

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

Mark item and SELECT_LEXs 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

◆ 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 Item** resolve_ref_in_select_and_group ( THD thd,
Item_ident ref,
SELECT_LEX select 
)
static

Resolve a column reference in a sub-select.

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
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
  • NULL - there was an error, and the error was already reported
  • not_found_item - the item was not resolved, no error was reported
  • resolved item - if the item was resolved

◆ 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 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).

◆ whence()

static const char* whence ( const 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_name_string

const Name_string null_name_string(NULL, 0)