951
1041
Item_ref to allow fields from view being stored in tmp table.
953
1043
Item_aggregate_ref *item_ref;
954
uint32_t el= fields.elements;
1044
uint el= fields.elements;
955
1045
Item *real_itm= real_item();
957
1047
ref_pointer_array[el]= real_itm;
958
if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
1048
if (!(item_ref= new Item_aggregate_ref(&thd->lex->current_select->context,
959
1049
ref_pointer_array + el, 0, name)))
960
1050
return; // fatal_error is set
961
1051
if (type() == SUM_FUNC_ITEM)
962
item_ref->depended_from= ((Item_sum *) this)->depended_from();
1052
item_ref->depended_from= ((Item_sum *) this)->depended_from();
963
1053
fields.push_front(real_itm);
964
session->change_item_tree(ref, item_ref);
1054
thd->change_item_tree(ref, item_ref);
1060
left_is_superset(DTCollation *left, DTCollation *right)
1062
/* Allow convert to Unicode */
1063
if (left->collation->state & MY_CS_UNICODE &&
1064
(left->derivation < right->derivation ||
1065
(left->derivation == right->derivation &&
1066
!(right->collation->state & MY_CS_UNICODE))))
1068
/* Allow convert from ASCII */
1069
if (right->repertoire == MY_REPERTOIRE_ASCII &&
1070
(left->derivation < right->derivation ||
1071
(left->derivation == right->derivation &&
1072
!(left->repertoire == MY_REPERTOIRE_ASCII))))
1074
/* Disallow conversion otherwise */
1079
Aggregate two collations together taking
1080
into account their coercibility (aka derivation):.
1082
0 == DERIVATION_EXPLICIT - an explicitly written COLLATE clause @n
1083
1 == DERIVATION_NONE - a mix of two different collations @n
1084
2 == DERIVATION_IMPLICIT - a column @n
1085
3 == DERIVATION_COERCIBLE - a string constant.
1087
The most important rules are:
1088
-# If collations are the same:
1089
chose this collation, and the strongest derivation.
1090
-# If collations are different:
1091
- Character sets may differ, but only if conversion without
1092
data loss is possible. The caller provides flags whether
1093
character set conversion attempts should be done. If no
1094
flags are substituted, then the character sets must be the same.
1095
Currently processed flags are:
1096
MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
1097
MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
1098
- two EXPLICIT collations produce an error, e.g. this is wrong:
1099
CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
1100
- the side with smaller derivation value wins,
1101
i.e. a column is stronger than a string constant,
1102
an explicit COLLATE clause is stronger than a column.
1103
- if derivations are the same, we have DERIVATION_NONE,
1104
we'll wait for an explicit COLLATE clause which possibly can
1105
come from another argument later: for example, this is valid,
1106
but we don't know yet when collecting the first two arguments:
1108
CONCAT(latin1_swedish_ci_column,
1109
latin1_german1_ci_column,
1110
expr COLLATE latin1_german2_ci)
1114
bool DTCollation::aggregate(DTCollation &dt, uint flags)
1116
if (!my_charset_same(collation, dt.collation))
1119
We do allow to use binary strings (like BLOBS)
1120
together with character strings.
1121
Binaries have more precedence than a character
1122
string of the same derivation.
1124
if (collation == &my_charset_bin)
1126
if (derivation <= dt.derivation)
1133
else if (dt.collation == &my_charset_bin)
1135
if (dt.derivation <= derivation)
1144
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1145
left_is_superset(this, &dt))
1149
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1150
left_is_superset(&dt, this))
1154
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1155
derivation < dt.derivation &&
1156
dt.derivation >= DERIVATION_SYSCONST)
1160
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1161
dt.derivation < derivation &&
1162
derivation >= DERIVATION_SYSCONST)
1168
// Cannot apply conversion
1169
set(0, DERIVATION_NONE, 0);
1173
else if (derivation < dt.derivation)
1177
else if (dt.derivation < derivation)
1183
if (collation == dt.collation)
1189
if (derivation == DERIVATION_EXPLICIT)
1191
set(0, DERIVATION_NONE, 0);
1194
if (collation->state & MY_CS_BINSORT)
1196
if (dt.collation->state & MY_CS_BINSORT)
1201
CHARSET_INFO *bin= get_charset_by_csname(collation->csname,
1202
MY_CS_BINSORT,MYF(0));
1203
set(bin, DERIVATION_NONE);
1206
repertoire|= dt.repertoire;
1210
/******************************/
1212
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname)
1214
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
1215
c1.collation->name,c1.derivation_name(),
1216
c2.collation->name,c2.derivation_name(),
1222
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
1225
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
1226
c1.collation->name,c1.derivation_name(),
1227
c2.collation->name,c2.derivation_name(),
1228
c3.collation->name,c3.derivation_name(),
1234
void my_coll_agg_error(Item** args, uint count, const char *fname,
1238
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
1239
else if (count == 3)
1240
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
1241
args[2*item_sep]->collation, fname);
1243
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
1247
bool agg_item_collations(DTCollation &c, const char *fname,
1248
Item **av, uint count, uint flags, int item_sep)
1252
c.set(av[0]->collation);
1253
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1255
if (c.aggregate((*arg)->collation, flags))
1257
my_coll_agg_error(av, count, fname, item_sep);
1261
if ((flags & MY_COLL_DISALLOW_NONE) &&
1262
c.derivation == DERIVATION_NONE)
1264
my_coll_agg_error(av, count, fname, item_sep);
1271
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1272
Item **av, uint count, uint flags)
1274
return (agg_item_collations(c, fname, av, count,
1275
flags | MY_COLL_DISALLOW_NONE, 1));
1280
Collect arguments' character sets together.
1282
We allow to apply automatic character set conversion in some cases.
1283
The conditions when conversion is possible are:
1284
- arguments A and B have different charsets
1285
- A wins according to coercibility rules
1286
(i.e. a column is stronger than a string constant,
1287
an explicit COLLATE clause is stronger than a column)
1288
- character set of A is either superset for character set of B,
1289
or B is a string constant which can be converted into the
1290
character set of A without data loss.
1292
If all of the above is true, then it's possible to convert
1293
B into the character set of A, and then compare according
1294
to the collation of A.
1296
For functions with more than two arguments:
1298
collect(A,B,C) ::= collect(collect(A,B),C)
1300
Since this function calls THD::change_item_tree() on the passed Item **
1301
pointers, it is necessary to pass the original Item **'s, not copies.
1302
Otherwise their values will not be properly restored (see BUG#20769).
1303
If the items are not consecutive (eg. args[2] and args[5]), use the
1304
item_sep argument, ie.
1306
agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
1310
bool agg_item_charsets(DTCollation &coll, const char *fname,
1311
Item **args, uint nargs, uint flags, int item_sep)
1313
Item **arg, *safe_args[2];
1315
memset(safe_args, 0, sizeof(safe_args));
1317
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
1321
For better error reporting: save the first and the second argument.
1322
We need this only if the the number of args is 3 or 2:
1323
- for a longer argument list, "Illegal mix of collations"
1324
doesn't display each argument's characteristics.
1325
- if nargs is 1, then this error cannot happen.
1327
if (nargs >=2 && nargs <= 3)
1329
safe_args[0]= args[0];
1330
safe_args[1]= args[item_sep];
1333
THD *thd= current_thd;
1334
Query_arena *arena, backup;
1338
In case we're in statement prepare, create conversion item
1339
in its memory: it will be reused on each execute.
1343
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1346
uint32 dummy_offset;
1347
if (!String::needs_conversion(0, (*arg)->collation.collation,
1352
if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
1353
((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
1354
conv= new Item_func_conv_charset(*arg, coll.collation, 1);
1358
if (nargs >=2 && nargs <= 3)
1360
/* restore the original arguments for better error message */
1361
args[0]= safe_args[0];
1362
args[item_sep]= safe_args[1];
1364
my_coll_agg_error(args, nargs, fname, item_sep);
1366
break; // we cannot return here, we need to restore "arena".
1368
if ((*arg)->type() == Item::FIELD_ITEM)
1369
((Item_field *)(*arg))->no_const_subst= 1;
1371
If in statement prepare, then we create a converter for two
1372
constant items, do it once and then reuse it.
1373
If we're in execution of a prepared statement, arena is NULL,
1374
and the conv was created in runtime memory. This can be
1375
the case only if the argument is a parameter marker ('?'),
1376
because for all true constants the charset converter has already
1377
been created in prepare. In this case register the change for
1380
thd->change_item_tree(arg, conv);
1382
We do not check conv->fixed, because Item_func_conv_charset which can
1383
be return by safe_charset_converter can't be fixed at creation
1385
conv->fix_fields(thd, arg);
1388
thd->restore_active_arena(arena, &backup);
1393
void Item_ident_for_show::make_field(Send_field *tmp_field)
1395
tmp_field->table_name= tmp_field->org_table_name= table_name;
1396
tmp_field->db_name= db_name;
1397
tmp_field->col_name= tmp_field->org_col_name= field->field_name;
1398
tmp_field->charsetnr= field->charset()->number;
1399
tmp_field->length=field->field_length;
1400
tmp_field->type=field->type();
1401
tmp_field->flags= field->table->maybe_null ?
1402
(field->flags & ~NOT_NULL_FLAG) : field->flags;
1403
tmp_field->decimals= field->decimals();
1406
/**********************************************/
1408
Item_field::Item_field(Field *f)
1409
:Item_ident(0, NullS, *f->table_name, f->field_name),
1410
item_equal(0), no_const_subst(0),
1411
have_privileges(0), any_privileges(0)
1415
field_name and table_name should not point to garbage
1416
if this item is to be reused
1418
orig_table_name= orig_field_name= "";
1423
Constructor used inside setup_wild().
1425
Ensures that field, table, and database names will live as long as
1426
Item_field (this is important in prepared statements).
1429
Item_field::Item_field(THD *thd __attribute__((__unused__)),
1430
Name_resolution_context *context_arg,
1432
:Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1433
item_equal(0), no_const_subst(0),
1434
have_privileges(0), any_privileges(0)
1440
Item_field::Item_field(Name_resolution_context *context_arg,
1441
const char *db_arg,const char *table_name_arg,
1442
const char *field_name_arg)
1443
:Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
1444
field(0), result_field(0), item_equal(0), no_const_subst(0),
1445
have_privileges(0), any_privileges(0)
1447
SELECT_LEX *select= current_thd->lex->current_select;
1448
collation.set(DERIVATION_IMPLICIT);
1449
if (select && select->parsing_place != IN_HAVING)
1450
select->select_n_where_fields++;
1454
Constructor need to process subselect with temporary tables (see Item)
1457
Item_field::Item_field(THD *thd, Item_field *item)
1458
:Item_ident(thd, item),
1460
result_field(item->result_field),
1461
item_equal(item->item_equal),
1462
no_const_subst(item->no_const_subst),
1463
have_privileges(item->have_privileges),
1464
any_privileges(item->any_privileges)
1466
collation.set(DERIVATION_IMPLICIT);
1469
void Item_field::set_field(Field *field_par)
1471
field=result_field=field_par; // for easy coding with fields
1472
maybe_null=field->maybe_null();
1473
decimals= field->decimals();
1474
max_length= field_par->max_display_length();
1475
table_name= *field_par->table_name;
1476
field_name= field_par->field_name;
1477
db_name= field_par->table->s->db.str;
1478
alias_name_used= field_par->table->alias_name_used;
1479
unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
1480
collation.set(field_par->charset(), field_par->derivation());
1482
if (field->table->s->tmp_table == SYSTEM_TMP_TABLE)
1488
Reset this item to point to a field from the new temporary table.
1489
This is used when we create a new temporary table for each execution
1490
of prepared statement.
1493
void Item_field::reset_field(Field *f)
1496
/* 'name' is pointing at field->field_name of old field */
1497
name= (char*) f->field_name;
1500
const char *Item_ident::full_name() const
1503
if (!table_name || !field_name)
1504
return field_name ? field_name : name ? name : "tmp_field";
1505
if (db_name && db_name[0])
1507
tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1508
(uint) strlen(field_name)+3);
1509
strxmov(tmp,db_name,".",table_name,".",field_name,NullS);
1515
tmp= (char*) sql_alloc((uint) strlen(table_name) +
1516
(uint) strlen(field_name) + 2);
1517
strxmov(tmp, table_name, ".", field_name, NullS);
1520
tmp= (char*) field_name;
1525
void Item_ident::print(String *str,
1526
enum_query_type query_type __attribute__((__unused__)))
1528
THD *thd= current_thd;
1529
char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1530
const char *d_name= db_name, *t_name= table_name;
1531
if (lower_case_table_names== 1 ||
1532
(lower_case_table_names == 2 && !alias_name_used))
1534
if (table_name && table_name[0])
1536
strmov(t_name_buff, table_name);
1537
my_casedn_str(files_charset_info, t_name_buff);
1538
t_name= t_name_buff;
1540
if (db_name && db_name[0])
1542
strmov(d_name_buff, db_name);
1543
my_casedn_str(files_charset_info, d_name_buff);
1544
d_name= d_name_buff;
1548
if (!table_name || !field_name || !field_name[0])
1550
const char *nm= (field_name && field_name[0]) ?
1551
field_name : name ? name : "tmp_field";
1552
append_identifier(thd, str, nm, (uint) strlen(nm));
1555
if (db_name && db_name[0] && !alias_name_used)
1558
append_identifier(thd, str, d_name, (uint)strlen(d_name));
1561
append_identifier(thd, str, t_name, (uint)strlen(t_name));
1563
append_identifier(thd, str, field_name, (uint)strlen(field_name));
1569
append_identifier(thd, str, t_name, (uint) strlen(t_name));
1571
append_identifier(thd, str, field_name, (uint) strlen(field_name));
1574
append_identifier(thd, str, field_name, (uint) strlen(field_name));
1579
String *Item_field::val_str(String *str)
1582
if ((null_value=field->is_null()))
1584
str->set_charset(str_value.charset());
1585
return field->val_str(str,&str_value);
1589
double Item_field::val_real()
1592
if ((null_value=field->is_null()))
1594
return field->val_real();
1598
int64_t Item_field::val_int()
1601
if ((null_value=field->is_null()))
1603
return field->val_int();
1607
my_decimal *Item_field::val_decimal(my_decimal *decimal_value)
1609
if ((null_value= field->is_null()))
1611
return field->val_decimal(decimal_value);
1615
String *Item_field::str_result(String *str)
1617
if ((null_value=result_field->is_null()))
1619
str->set_charset(str_value.charset());
1620
return result_field->val_str(str,&str_value);
1623
bool Item_field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1625
if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1627
bzero((char*) ltime,sizeof(*ltime));
1633
bool Item_field::get_date_result(MYSQL_TIME *ltime,uint fuzzydate)
1635
if ((null_value=result_field->is_null()) ||
1636
result_field->get_date(ltime,fuzzydate))
1638
bzero((char*) ltime,sizeof(*ltime));
1644
bool Item_field::get_time(MYSQL_TIME *ltime)
1646
if ((null_value=field->is_null()) || field->get_time(ltime))
1648
bzero((char*) ltime,sizeof(*ltime));
1654
double Item_field::val_result()
1656
if ((null_value=result_field->is_null()))
1658
return result_field->val_real();
1661
int64_t Item_field::val_int_result()
1663
if ((null_value=result_field->is_null()))
1665
return result_field->val_int();
1669
my_decimal *Item_field::val_decimal_result(my_decimal *decimal_value)
1671
if ((null_value= result_field->is_null()))
1673
return result_field->val_decimal(decimal_value);
1677
bool Item_field::val_bool_result()
1679
if ((null_value= result_field->is_null()))
1681
switch (result_field->result_type()) {
1683
return result_field->val_int() != 0;
1684
case DECIMAL_RESULT:
1686
my_decimal decimal_value;
1687
my_decimal *val= result_field->val_decimal(&decimal_value);
1689
return !my_decimal_is_zero(val);
1694
return result_field->val_real() != 0.0;
1698
return 0; // Shut up compiler
1703
bool Item_field::eq(const Item *item,
1704
bool binary_cmp __attribute__((__unused__))) const
1706
Item *real_item= ((Item *) item)->real_item();
1707
if (real_item->type() != FIELD_ITEM)
1710
Item_field *item_field= (Item_field*) real_item;
1711
if (item_field->field && field)
1712
return item_field->field == field;
1714
We may come here when we are trying to find a function in a GROUP BY
1715
clause from the select list.
1716
In this case the '100 % correct' way to do this would be to first
1717
run fix_fields() on the GROUP BY item and then retry this function, but
1718
I think it's better to relax the checking a bit as we will in
1719
most cases do the correct thing by just checking the field name.
1720
(In cases where we would choose wrong we would have to generate a
1723
return (!my_strcasecmp(system_charset_info, item_field->name,
1725
(!item_field->table_name || !table_name ||
1726
(!my_strcasecmp(table_alias_charset, item_field->table_name,
1728
(!item_field->db_name || !db_name ||
1729
(item_field->db_name && !strcmp(item_field->db_name,
1734
table_map Item_field::used_tables() const
1736
if (field->table->const_table)
1737
return 0; // const item
1738
return (depended_from ? OUTER_REF_TABLE_BIT : field->table->map);
1742
void Item_field::fix_after_pullout(st_select_lex *new_parent,
1743
Item **ref __attribute__((__unused__)))
1745
if (new_parent == depended_from)
1746
depended_from= NULL;
1747
Name_resolution_context *ctx= new Name_resolution_context();
1748
ctx->outer_context= NULL; // We don't build a complete name resolver
1749
ctx->select_lex= new_parent;
1750
ctx->first_name_resolution_table= context->first_name_resolution_table;
1751
ctx->last_name_resolution_table= context->last_name_resolution_table;
1756
Item *Item_field::get_tmp_table_item(THD *thd)
1758
Item_field *new_item= new Item_field(thd, this);
1760
new_item->field= new_item->result_field;
1764
int64_t Item_field::val_int_endpoint(bool left_endp __attribute__((__unused__)),
1765
bool *incl_endp __attribute__((__unused__)))
1767
int64_t res= val_int();
1768
return null_value? INT64_MIN : res;
1772
Create an item from a string we KNOW points to a valid int64_t
1773
end \\0 terminated number string.
1774
This is always 'signed'. Unsigned values are created with Item_uint()
1777
Item_int::Item_int(const char *str_arg, uint length)
1779
char *end_ptr= (char*) str_arg + length;
1781
value= my_strtoll10(str_arg, &end_ptr, &error);
1782
max_length= (uint) (end_ptr - str_arg);
1783
name= (char*) str_arg;
1788
my_decimal *Item_int::val_decimal(my_decimal *decimal_value)
1790
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value);
1791
return decimal_value;
1794
String *Item_int::val_str(String *str)
1796
// following assert is redundant, because fixed=1 assigned in constructor
1798
str->set(value, &my_charset_bin);
1802
void Item_int::print(String *str,
1803
enum_query_type query_type __attribute__((__unused__)))
1805
// my_charset_bin is good enough for numbers
1806
str_value.set(value, &my_charset_bin);
1807
str->append(str_value);
1811
Item_uint::Item_uint(const char *str_arg, uint length):
1812
Item_int(str_arg, length)
1818
Item_uint::Item_uint(const char *str_arg, int64_t i, uint length):
1819
Item_int(str_arg, i, length)
1825
String *Item_uint::val_str(String *str)
1827
// following assert is redundant, because fixed=1 assigned in constructor
1829
str->set((uint64_t) value, &my_charset_bin);
1834
void Item_uint::print(String *str,
1835
enum_query_type query_type __attribute__((__unused__)))
1837
// latin1 is good enough for numbers
1838
str_value.set((uint64_t) value, default_charset());
1839
str->append(str_value);
1843
Item_decimal::Item_decimal(const char *str_arg, uint length,
1844
CHARSET_INFO *charset)
1846
str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1847
name= (char*) str_arg;
1848
decimals= (uint8) decimal_value.frac;
1850
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1851
decimals, unsigned_flag);
1854
Item_decimal::Item_decimal(int64_t val, bool unsig)
1856
int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1857
decimals= (uint8) decimal_value.frac;
1859
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1860
decimals, unsigned_flag);
1864
Item_decimal::Item_decimal(double val,
1865
int precision __attribute__((__unused__)),
1866
int scale __attribute__((__unused__)))
1868
double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1869
decimals= (uint8) decimal_value.frac;
1871
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1872
decimals, unsigned_flag);
1876
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1877
uint decimal_par, uint length)
1879
my_decimal2decimal(val_arg, &decimal_value);
1881
decimals= (uint8) decimal_par;
1887
Item_decimal::Item_decimal(my_decimal *value_par)
1889
my_decimal2decimal(value_par, &decimal_value);
1890
decimals= (uint8) decimal_value.frac;
1892
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1893
decimals, unsigned_flag);
1897
Item_decimal::Item_decimal(const uchar *bin, int precision, int scale)
1899
binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1900
&decimal_value, precision, scale);
1901
decimals= (uint8) decimal_value.frac;
1903
max_length= my_decimal_precision_to_length(precision, decimals,
1908
int64_t Item_decimal::val_int()
1911
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1915
double Item_decimal::val_real()
1918
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1922
String *Item_decimal::val_str(String *result)
1924
result->set_charset(&my_charset_bin);
1925
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
1929
void Item_decimal::print(String *str,
1930
enum_query_type query_type __attribute__((__unused__)))
1932
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1933
str->append(str_value);
1937
bool Item_decimal::eq(const Item *item,
1938
bool binary_cmp __attribute__((__unused__))) const
1940
if (type() == item->type() && item->basic_const_item())
1943
We need to cast off const to call val_decimal(). This should
1944
be OK for a basic constant. Additionally, we can pass 0 as
1945
a true decimal constant will return its internal decimal
1946
storage and ignore the argument.
1948
Item *arg= (Item*) item;
1949
my_decimal *value= arg->val_decimal(0);
1950
return !my_decimal_cmp(&decimal_value, value);
1956
void Item_decimal::set_decimal_value(my_decimal *value_par)
1958
my_decimal2decimal(value_par, &decimal_value);
1959
decimals= (uint8) decimal_value.frac;
1960
unsigned_flag= !decimal_value.sign();
1961
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1962
decimals, unsigned_flag);
1966
String *Item_float::val_str(String *str)
1968
// following assert is redundant, because fixed=1 assigned in constructor
1970
str->set_real(value,decimals,&my_charset_bin);
1975
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1977
// following assert is redundant, because fixed=1 assigned in constructor
1979
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1980
return (decimal_value);
1984
void Item_string::print(String *str, enum_query_type query_type)
1986
if (query_type == QT_ORDINARY && is_cs_specified())
1989
str->append(collation.collation->csname);
1994
if (query_type == QT_ORDINARY ||
1995
my_charset_same(str_value.charset(), system_charset_info))
1997
str_value.print(str);
2001
THD *thd= current_thd;
2002
LEX_STRING utf8_lex_str;
2004
thd->convert_string(&utf8_lex_str,
2005
system_charset_info,
2006
str_value.c_ptr_safe(),
2008
str_value.charset());
2010
String utf8_str(utf8_lex_str.str,
2011
utf8_lex_str.length,
2012
system_charset_info);
2014
utf8_str.print(str);
2021
double Item_string::val_real()
2025
char *end, *org_end;
2027
CHARSET_INFO *cs= str_value.charset();
2029
org_end= (char*) str_value.ptr() + str_value.length();
2030
tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
2032
if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2035
We can use str_value.ptr() here as Item_string is gurantee to put an
2038
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2039
ER_TRUNCATED_WRONG_VALUE,
2040
ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
2049
Give error if we wanted a signed integer and we got an unsigned one
2051
int64_t Item_string::val_int()
2056
char *end= (char*) str_value.ptr()+ str_value.length();
2058
CHARSET_INFO *cs= str_value.charset();
2060
tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
2062
TODO: Give error if we wanted a signed integer and we got an unsigned
2066
(end != org_end && !check_if_only_end_space(cs, end, org_end)))
2068
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2069
ER_TRUNCATED_WRONG_VALUE,
2070
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2077
my_decimal *Item_string::val_decimal(my_decimal *decimal_value)
2079
return val_decimal_from_string(decimal_value);
2083
bool Item_null::eq(const Item *item,
2084
bool binary_cmp __attribute__((__unused__))) const
2085
{ return item->type() == type(); }
2088
double Item_null::val_real()
2090
// following assert is redundant, because fixed=1 assigned in constructor
2095
int64_t Item_null::val_int()
2097
// following assert is redundant, because fixed=1 assigned in constructor
2103
String *Item_null::val_str(String *str __attribute__((__unused__)))
2105
// following assert is redundant, because fixed=1 assigned in constructor
2111
my_decimal *Item_null::val_decimal(my_decimal *decimal_value __attribute__((__unused__)))
2117
Item *Item_null::safe_charset_converter(CHARSET_INFO *tocs)
2119
collation.set(tocs);
2123
/*********************** Item_param related ******************************/
2126
Default function of Item_param::set_param_func, so in case
2127
of malformed packet the server won't SIGSEGV.
2131
default_set_param_func(Item_param *param,
2132
uchar **pos __attribute__((unused)),
2133
ulong len __attribute__((unused)))
2139
Item_param::Item_param(uint pos_in_query_arg) :
2141
item_result_type(STRING_RESULT),
2142
/* Don't pretend to be a literal unless value for this item is set. */
2143
item_type(PARAM_ITEM),
2144
param_type(MYSQL_TYPE_VARCHAR),
2145
pos_in_query(pos_in_query_arg),
2146
set_param_func(default_set_param_func),
2147
limit_clause_param(false)
2151
Since we can't say whenever this item can be NULL or cannot be NULL
2152
before mysql_stmt_execute(), so we assuming that it can be NULL until
2156
cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE);
2157
cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin);
2161
void Item_param::set_null()
2163
/* These are cleared after each execution by reset() method */
2166
Because of NULL and string values we need to set max_length for each new
2167
placeholder value: user can submit NULL for any placeholder type, and
2168
string length can be different in each execution.
2173
item_type= Item::NULL_ITEM;
2177
void Item_param::set_int(int64_t i, uint32 max_length_arg)
2179
value.integer= (int64_t) i;
2181
max_length= max_length_arg;
2187
void Item_param::set_double(double d)
2191
max_length= DBL_DIG + 8;
2192
decimals= NOT_FIXED_DEC;
2199
Set decimal parameter value from string.
2201
@param str character string
2202
@param length string length
2205
As we use character strings to send decimal values in
2206
binary protocol, we use str2my_decimal to convert it to
2207
internal decimal value.
2210
void Item_param::set_decimal(const char *str, ulong length)
2214
end= (char*) str+length;
2215
str2my_decimal(E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2216
state= DECIMAL_VALUE;
2217
decimals= decimal_value.frac;
2218
max_length= my_decimal_precision_to_length(decimal_value.precision(),
2219
decimals, unsigned_flag);
2226
Set parameter value from MYSQL_TIME value.
2228
@param tm datetime value to set (time_type is ignored)
2229
@param type type of datetime value
2230
@param max_length_arg max length of datetime value as string
2233
If we value to be stored is not normalized, zero value will be stored
2234
instead and proper warning will be produced. This function relies on
2235
the fact that even wrong value sent over binary protocol fits into
2236
MAX_DATE_STRING_REP_LENGTH buffer.
2238
void Item_param::set_time(MYSQL_TIME *tm, timestamp_type time_type,
2239
uint32 max_length_arg)
2242
value.time.time_type= time_type;
2244
if (value.time.year > 9999 || value.time.month > 12 ||
2245
value.time.day > 31 ||
2246
((time_type != MYSQL_TIMESTAMP_TIME) && value.time.hour > 23) ||
2247
value.time.minute > 59 || value.time.second > 59)
2249
char buff[MAX_DATE_STRING_REP_LENGTH];
2250
uint length= my_TIME_to_str(&value.time, buff);
2251
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2252
buff, length, time_type, 0);
2253
set_zero_time(&value.time, MYSQL_TIMESTAMP_ERROR);
2258
max_length= max_length_arg;
2264
bool Item_param::set_str(const char *str, ulong length)
2267
Assign string with no conversion: data is converted only after it's
2268
been written to the binary log.
2271
if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
2274
state= STRING_VALUE;
2277
/* max_length and decimals are set after charset conversion */
2278
/* sic: str may be not null-terminated */
2283
bool Item_param::set_longdata(const char *str, ulong length)
2286
If client character set is multibyte, end of long data packet
2287
may hit at the middle of a multibyte character. Additionally,
2288
if binary log is open we must write long data value to the
2289
binary log in character set of client. This is why we can't
2290
convert long data to connection character set as it comes
2291
(here), and first have to concatenate all pieces together,
2292
write query to the binary log and only then perform conversion.
2294
if (str_value.append(str, length, &my_charset_bin))
2296
state= LONG_DATA_VALUE;
2304
Set parameter value from user variable value.
2306
@param thd Current thread
2307
@param entry User variable structure (NULL means use NULL value)
2315
bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry)
2317
if (entry && entry->value)
2319
item_result_type= entry->type;
2320
unsigned_flag= entry->unsigned_flag;
2321
if (limit_clause_param)
2324
set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
2325
item_type= Item::INT_ITEM;
2326
return(!unsigned_flag && value.integer < 0 ? 1 : 0);
2328
switch (item_result_type) {
2330
set_double(*(double*)entry->value);
2331
item_type= Item::REAL_ITEM;
2334
set_int(*(int64_t*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
2335
item_type= Item::INT_ITEM;
2339
CHARSET_INFO *fromcs= entry->collation.collation;
2340
CHARSET_INFO *tocs= thd->variables.collation_connection;
2341
uint32 dummy_offset;
2343
value.cs_info.character_set_of_placeholder=
2344
value.cs_info.character_set_client= fromcs;
2346
Setup source and destination character sets so that they
2347
are different only if conversion is necessary: this will
2348
make later checks easier.
2350
value.cs_info.final_character_set_of_str_value=
2351
String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
2354
Exact value of max_length is not known unless data is converted to
2355
charset of connection, so we have to set it later.
2357
item_type= Item::STRING_ITEM;
2359
if (set_str((const char *)entry->value, entry->length))
2363
case DECIMAL_RESULT:
2365
const my_decimal *ent_value= (const my_decimal *)entry->value;
2366
my_decimal2decimal(ent_value, &decimal_value);
2367
state= DECIMAL_VALUE;
2368
decimals= ent_value->frac;
2369
max_length= my_decimal_precision_to_length(ent_value->precision(),
2370
decimals, unsigned_flag);
2371
item_type= Item::DECIMAL_ITEM;
2386
Resets parameter after execution.
2389
We clear null_value here instead of setting it in set_* methods,
2390
because we want more easily handle case for long data.
2393
void Item_param::reset()
2395
/* Shrink string buffer if it's bigger than max possible CHAR column */
2396
if (str_value.alloced_length() > MAX_CHAR_WIDTH)
2399
str_value.length(0);
2400
str_value_ptr.length(0);
2402
We must prevent all charset conversions until data has been written
2405
str_value.set_charset(&my_charset_bin);
2406
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
2411
Don't reset item_type to PARAM_ITEM: it's only needed to guard
2412
us from item optimizations at prepare stage, when item doesn't yet
2413
contain a literal of some kind.
2414
In all other cases when this object is accessed its value is
2415
set (this assumption is guarded by 'state' and
2416
assertS(state != NO_VALUE) in all Item_param::get_*
2423
int Item_param::save_in_field(Field *field, bool no_conversions)
2425
field->set_notnull();
2429
return field->store(value.integer, unsigned_flag);
2431
return field->store(value.real);
2433
return field->store_decimal(&decimal_value);
2435
field->store_time(&value.time, value.time.time_type);
2438
case LONG_DATA_VALUE:
2439
return field->store(str_value.ptr(), str_value.length(),
2440
str_value.charset());
2442
return set_field_to_null_with_conversions(field, no_conversions);
2451
bool Item_param::get_time(MYSQL_TIME *res)
2453
if (state == TIME_VALUE)
2459
If parameter value isn't supplied assertion will fire in val_str()
2460
which is called from Item::get_time().
2462
return Item::get_time(res);
2466
bool Item_param::get_date(MYSQL_TIME *res, uint fuzzydate)
2468
if (state == TIME_VALUE)
2473
return Item::get_date(res, fuzzydate);
2477
double Item_param::val_real()
2483
return (double) value.integer;
2487
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
2491
case LONG_DATA_VALUE:
2495
return my_strntod(str_value.charset(), (char*) str_value.ptr(),
2496
str_value.length(), &end_not_used, &dummy_err);
2500
This works for example when user says SELECT ?+0.0 and supplies
2501
time value for the placeholder.
2503
return uint64_t2double(TIME_to_uint64_t(&value.time));
2513
int64_t Item_param::val_int()
2517
return (int64_t) rint(value.real);
2519
return value.integer;
2523
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
2527
case LONG_DATA_VALUE:
2530
return my_strntoll(str_value.charset(), str_value.ptr(),
2531
str_value.length(), 10, (char**) 0, &dummy_err);
2534
return (int64_t) TIME_to_uint64_t(&value.time);
2544
my_decimal *Item_param::val_decimal(my_decimal *dec)
2548
return &decimal_value;
2550
double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
2553
int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec);
2556
case LONG_DATA_VALUE:
2557
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec);
2561
int64_t i= (int64_t) TIME_to_uint64_t(&value.time);
2562
int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
2574
String *Item_param::val_str(String* str)
2578
case LONG_DATA_VALUE:
2579
return &str_value_ptr;
2581
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2584
str->set(value.integer, &my_charset_bin);
2587
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2593
if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
2595
str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
2596
str->set_charset(&my_charset_bin);
2608
Return Param item values in string format, for generating the dynamic
2609
query used in update/binary logs.
2612
- Change interface and implementation to fill log data in place
2613
and avoid one more memcpy/alloc between str and log string.
2614
- In case of error we need to notify replication
2615
that binary log contains wrong statement
2618
const String *Item_param::query_val_str(String* str) const
2622
str->set_int(value.integer, unsigned_flag, &my_charset_bin);
2625
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2628
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2630
return &my_null_string;
2637
TODO: in case of error we need to notify replication
2638
that binary log contains wrong statement
2640
if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3))
2643
/* Create date string inplace */
2644
buf= str->c_ptr_quick();
2647
ptr+= (uint) my_TIME_to_str(&value.time, ptr);
2649
str->length((uint32) (ptr - buf));
2653
case LONG_DATA_VALUE:
2656
append_query_string(value.cs_info.character_set_client, &str_value, str);
2660
return &my_null_string;
2669
Convert string from client character set to the character set of
2673
bool Item_param::convert_str_value(THD *thd)
2676
if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2679
Check is so simple because all charsets were set up properly
2680
in setup_one_conversion_function, where typecode of
2681
placeholder was also taken into account: the variables are different
2682
here only if conversion is really necessary.
2684
if (value.cs_info.final_character_set_of_str_value !=
2685
value.cs_info.character_set_of_placeholder)
2687
rc= thd->convert_string(&str_value,
2688
value.cs_info.character_set_of_placeholder,
2689
value.cs_info.final_character_set_of_str_value);
2692
str_value.set_charset(value.cs_info.final_character_set_of_str_value);
2693
/* Here str_value is guaranteed to be in final_character_set_of_str_value */
2695
max_length= str_value.length();
2698
str_value_ptr is returned from val_str(). It must be not alloced
2699
to prevent it's modification by val_str() invoker.
2701
str_value_ptr.set(str_value.ptr(), str_value.length(),
2702
str_value.charset());
2703
/* Synchronize item charset with value charset */
2704
collation.set(str_value.charset(), DERIVATION_COERCIBLE);
2710
bool Item_param::basic_const_item() const
2712
if (state == NO_VALUE || state == TIME_VALUE)
2719
Item_param::clone_item()
2721
/* see comments in the header file */
2724
return new Item_null(name);
2726
return (unsigned_flag ?
2727
new Item_uint(name, value.integer, max_length) :
2728
new Item_int(name, value.integer, max_length));
2730
return new Item_float(name, value.real, decimals, max_length);
2732
case LONG_DATA_VALUE:
2733
return new Item_string(name, str_value.c_ptr_quick(), str_value.length(),
2734
str_value.charset());
2746
Item_param::eq(const Item *arg, bool binary_cmp) const
2749
if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2752
We need to cast off const to call val_int(). This should be OK for
2761
return value.integer == item->val_int() &&
2762
unsigned_flag == item->unsigned_flag;
2764
return value.real == item->val_real();
2766
case LONG_DATA_VALUE:
2768
return !stringcmp(&str_value, &item->str_value);
2769
return !sortcmp(&str_value, &item->str_value, collation.collation);
2776
/* End of Item_param related */
2778
void Item_param::print(String *str,
2779
enum_query_type query_type __attribute__((__unused__)))
2781
if (state == NO_VALUE)
2787
char buffer[STRING_BUFFER_USUAL_SIZE];
2788
String tmp(buffer, sizeof(buffer), &my_charset_bin);
2790
res= query_val_str(&tmp);
2796
/****************************************************************************
2798
****************************************************************************/
2800
void Item_copy_string::copy()
2802
String *res=item->val_str(&str_value);
2803
if (res && res != &str_value)
2804
str_value.copy(*res);
2805
null_value=item->null_value;
2809
String *Item_copy_string::val_str(String *str __attribute__((__unused__)))
2811
// Item_copy_string is used without fix_fields call
2818
my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value)
2820
// Item_copy_string is used without fix_fields call
2823
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value);
2824
return (decimal_value);
969
2829
Functions to convert item to field (for send_fields)
973
bool Item::fix_fields(Session *, Item **)
2833
bool Item::fix_fields(THD *thd __attribute__((__unused__)),
2834
Item **ref __attribute__((__unused__)))
976
2837
// We do not check fields which are fixed during construction
1285
3196
return (Item**) not_found_item;
3201
Resolve the name of an outer select column reference.
3203
The method resolves the column reference represented by 'this' as a column
3204
present in outer selects that contain current select.
3206
In prepared statements, because of cache, find_field_in_tables()
3207
can resolve fields even if they don't belong to current context.
3208
In this case this method only finds appropriate context and marks
3209
current select as dependent. The found reference of field should be
3210
provided in 'from_field'.
3212
@param[in] thd current thread
3213
@param[in,out] from_field found field reference or (Field*)not_found_field
3214
@param[in,out] reference view column if this item was resolved to a
3218
This is the inner loop of Item_field::fix_fields:
3220
for each outer query Q_k beginning from the inner-most one
3222
search for a column or derived column named col_ref_i
3223
[in table T_j] in the FROM clause of Q_k;
3225
if such a column is not found
3226
Search for a column or derived column named col_ref_i
3227
[in table T_j] in the SELECT and GROUP clauses of Q_k.
3232
1 column succefully resolved and fix_fields() should continue.
3234
0 column fully fixed and fix_fields() should return false
3240
Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
3242
enum_parsing_place place= NO_MATTER;
3243
bool field_found= (*from_field != not_found_field);
3244
bool upward_lookup= false;
3247
If there are outer contexts (outer selects, but current select is
3248
not derived table or view) try to resolve this reference in the
3251
We treat each subselect as a separate namespace, so that different
3252
subselects may contain columns with the same names. The subselects
3253
are searched starting from the innermost.
3255
Name_resolution_context *last_checked_context= context;
3256
Item **ref= (Item **) not_found_item;
3257
SELECT_LEX *current_sel= (SELECT_LEX *) thd->lex->current_select;
3258
Name_resolution_context *outer_context= 0;
3259
SELECT_LEX *select= 0;
3260
/* Currently derived tables cannot be correlated */
3261
if (current_sel->master_unit()->first_select()->linkage !=
3263
outer_context= context->outer_context;
3266
outer_context= outer_context->outer_context)
3268
select= outer_context->select_lex;
3269
Item_subselect *prev_subselect_item=
3270
last_checked_context->select_lex->master_unit()->item;
3271
last_checked_context= outer_context;
3272
upward_lookup= true;
3274
place= prev_subselect_item->parsing_place;
3276
If outer_field is set, field was already found by first call
3277
to find_field_in_tables(). Only need to find appropriate context.
3279
if (field_found && outer_context->select_lex !=
3280
cached_table->select_lex)
3283
In case of a view, find_field_in_tables() writes the pointer to
3284
the found view field into '*reference', in other words, it
3285
substitutes this Item_field with the found expression.
3287
if (field_found || (*from_field= find_field_in_tables(thd, this,
3289
first_name_resolution_table,
3291
last_name_resolution_table,
3293
IGNORE_EXCEPT_NON_UNIQUE,
3299
if (*from_field != view_ref_found)
3301
prev_subselect_item->used_tables_cache|= (*from_field)->table->map;
3302
prev_subselect_item->const_item_cache= 0;
3303
set_field(*from_field);
3304
if (!last_checked_context->select_lex->having_fix_field &&
3305
select->group_list.elements &&
3306
(place == SELECT_LIST || place == IN_HAVING))
3310
If an outer field is resolved in a grouping select then it
3311
is replaced for an Item_outer_ref object. Otherwise an
3312
Item_field object is used.
3313
The new Item_outer_ref object is saved in the inner_refs_list of
3314
the outer select. Here it is only created. It can be fixed only
3315
after the original field has been fixed and this is done in the
3316
fix_inner_refs() function.
3319
if (!(rf= new Item_outer_ref(context, this)))
3321
thd->change_item_tree(reference, rf);
3322
select->inner_refs_list.push_back(rf);
3323
rf->in_sum_func= thd->lex->in_sum_func;
3326
A reference is resolved to a nest level that's outer or the same as
3327
the nest level of the enclosing set function : adjust the value of
3328
max_arg_level for the function if it's needed.
3330
if (thd->lex->in_sum_func &&
3331
thd->lex->in_sum_func->nest_level >= select->nest_level)
3333
Item::Type ref_type= (*reference)->type();
3334
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3335
select->nest_level);
3336
set_field(*from_field);
3338
mark_as_dependent(thd, last_checked_context->select_lex,
3339
context->select_lex, this,
3340
((ref_type == REF_ITEM ||
3341
ref_type == FIELD_ITEM) ?
3342
(Item_ident*) (*reference) : 0));
3348
Item::Type ref_type= (*reference)->type();
3349
prev_subselect_item->used_tables_cache|=
3350
(*reference)->used_tables();
3351
prev_subselect_item->const_item_cache&=
3352
(*reference)->const_item();
3353
mark_as_dependent(thd, last_checked_context->select_lex,
3354
context->select_lex, this,
3355
((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3356
(Item_ident*) (*reference) :
3359
A reference to a view field had been found and we
3360
substituted it instead of this Item (find_field_in_tables
3361
does it by assigning the new value to *reference), so now
3362
we can return from this function.
3370
/* Search in SELECT and GROUP lists of the outer select. */
3371
if (place != IN_WHERE && place != IN_ON)
3373
if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
3374
return -1; /* Some error occurred (e.g. ambiguous names). */
3375
if (ref != not_found_item)
3377
assert(*ref && (*ref)->fixed);
3378
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
3379
prev_subselect_item->const_item_cache&= (*ref)->const_item();
3385
Reference is not found in this select => this subquery depend on
3386
outer select (or we just trying to find wrong identifier, in this
3387
case it does not matter which used tables bits we set)
3389
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
3390
prev_subselect_item->const_item_cache= 0;
3396
if (ref == not_found_item && *from_field == not_found_field)
3400
// We can't say exactly what absent table or field
3401
my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where);
3405
/* Call find_field_in_tables only to report the error */
3406
find_field_in_tables(thd, this,
3407
context->first_name_resolution_table,
3408
context->last_name_resolution_table,
3409
reference, REPORT_ALL_ERRORS,
3415
else if (ref != not_found_item)
3420
/* Should have been checked in resolve_ref_in_select_and_group(). */
3421
assert(*ref && (*ref)->fixed);
3423
Here, a subset of actions performed by Item_ref::set_properties
3424
is not enough. So we pass ptr to NULL into Item_[direct]_ref
3425
constructor, so no initialization is performed, and call
3429
*ref= NULL; // Don't call set_properties()
3430
rf= (place == IN_HAVING ?
3431
new Item_ref(context, ref, (char*) table_name,
3432
(char*) field_name, alias_name_used) :
3433
(!select->group_list.elements ?
3434
new Item_direct_ref(context, ref, (char*) table_name,
3435
(char*) field_name, alias_name_used) :
3436
new Item_outer_ref(context, ref, (char*) table_name,
3437
(char*) field_name, alias_name_used)));
3442
if (place != IN_HAVING && select->group_list.elements)
3444
outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3445
((Item_outer_ref*)rf)->in_sum_func= thd->lex->in_sum_func;
3447
thd->change_item_tree(reference, rf);
3449
rf is Item_ref => never substitute other items (in this case)
3450
during fix_fields() => we can use rf after fix_fields()
3452
assert(!rf->fixed); // Assured by Item_ref()
3453
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3456
mark_as_dependent(thd, last_checked_context->select_lex,
3457
context->select_lex, this,
3463
mark_as_dependent(thd, last_checked_context->select_lex,
3464
context->select_lex,
3465
this, (Item_ident*)*reference);
3466
if (last_checked_context->select_lex->having_fix_field)
3469
rf= new Item_ref(context,
3470
(cached_table->db[0] ? cached_table->db : 0),
3471
(char*) cached_table->alias, (char*) field_name);
3474
thd->change_item_tree(reference, rf);
3476
rf is Item_ref => never substitute other items (in this case)
3477
during fix_fields() => we can use rf after fix_fields()
3479
assert(!rf->fixed); // Assured by Item_ref()
3480
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3490
Resolve the name of a column reference.
3492
The method resolves the column reference represented by 'this' as a column
3493
present in one of: FROM clause, SELECT clause, GROUP BY clause of a query
3494
Q, or in outer queries that contain Q.
3496
The name resolution algorithm used is (where [T_j] is an optional table
3497
name that qualifies the column name):
3500
resolve_column_reference([T_j].col_ref_i)
3502
search for a column or derived column named col_ref_i
3503
[in table T_j] in the FROM clause of Q;
3505
if such a column is NOT found AND // Lookup in outer queries.
3506
there are outer queries
3508
for each outer query Q_k beginning from the inner-most one
3510
search for a column or derived column named col_ref_i
3511
[in table T_j] in the FROM clause of Q_k;
3513
if such a column is not found
3514
Search for a column or derived column named col_ref_i
3515
[in table T_j] in the SELECT and GROUP clauses of Q_k.
3521
Notice that compared to Item_ref::fix_fields, here we first search the FROM
3522
clause, and then we search the SELECT and GROUP BY clauses.
3524
@param[in] thd current thread
3525
@param[in,out] reference view column if this item was resolved to a
3534
bool Item_field::fix_fields(THD *thd, Item **reference)
3537
Field *from_field= (Field *)not_found_field;
3538
bool outer_fixed= false;
3540
if (!field) // If field is not checked
3543
In case of view, find_field_in_tables() write pointer to view field
3544
expression to 'reference', i.e. it substitute that expression instead
3547
if ((from_field= find_field_in_tables(thd, this,
3548
context->first_name_resolution_table,
3549
context->last_name_resolution_table,
3551
thd->lex->use_only_table_context ?
3553
IGNORE_EXCEPT_NON_UNIQUE,
3559
/* Look up in current select's item_list to find aliased fields */
3560
if (thd->lex->current_select->is_item_list_lookup)
3563
enum_resolution_type resolution;
3564
Item** res= find_item_in_list(this, thd->lex->current_select->item_list,
3565
&counter, REPORT_EXCEPT_NOT_FOUND,
3569
if (resolution == RESOLVED_AGAINST_ALIAS)
3570
alias_name_used= true;
3571
if (res != (Item **)not_found_item)
3573
if ((*res)->type() == Item::FIELD_ITEM)
3576
It's an Item_field referencing another Item_field in the select
3578
Use the field from the Item_field in the select list and leave
3579
the Item_field instance in place.
3582
Field *new_field= (*((Item_field**)res))->field;
3584
if (new_field == NULL)
3586
/* The column to which we link isn't valid. */
3587
my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
3588
current_thd->where);
3592
set_field(new_field);
3598
It's not an Item_field in the select list so we must make a new
3599
Item_ref to point to the Item in the select list and replace the
3600
Item_field created by the parser with the new Item_ref.
3602
Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3605
thd->change_item_tree(reference, rf);
3607
Because Item_ref never substitutes itself with other items
3608
in Item_ref::fix_fields(), we can safely use the original
3609
pointer to it even after fix_fields()
3611
return rf->fix_fields(thd, reference) || rf->check_cols(1);
3615
if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3619
goto mark_non_agg_field;
3621
else if (!from_field)
3624
if (!outer_fixed && cached_table && cached_table->select_lex &&
3625
context->select_lex &&
3626
cached_table->select_lex != context->select_lex)
3629
if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3633
goto mark_non_agg_field;
3637
if it is not expression from merged VIEW we will set this field.
3639
We can leave expression substituted from view for next PS/SP rexecution
3640
(i.e. do not register this substitution for reverting on cleanup()
3641
(register_item_tree_changing())), because this subtree will be
3642
fix_field'ed during setup_tables()->setup_underlying() (i.e. before
3643
all other expressions of query, and references on tables which do
3644
not present in query will not make problems.
3646
Also we suppose that view can't be changed during PS/SP life.
3648
if (from_field == view_ref_found)
3651
set_field(from_field);
3652
if (thd->lex->in_sum_func &&
3653
thd->lex->in_sum_func->nest_level ==
3654
thd->lex->current_select->nest_level)
3655
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3656
thd->lex->current_select->nest_level);
3658
else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3660
TABLE *table= field->table;
3661
MY_BITMAP *current_bitmap, *other_bitmap;
3662
if (thd->mark_used_columns == MARK_COLUMNS_READ)
3664
current_bitmap= table->read_set;
3665
other_bitmap= table->write_set;
3669
current_bitmap= table->write_set;
3670
other_bitmap= table->read_set;
3672
if (!bitmap_fast_test_and_set(current_bitmap, field->field_index))
3674
if (!bitmap_is_set(other_bitmap, field->field_index))
3676
/* First usage of column */
3677
table->used_fields++; // Used to optimize loops
3678
/* purecov: begin inspected */
3679
table->covering_keys.intersect(field->part_of_key);
3689
context->process_error(thd);
3693
Item *Item_field::safe_charset_converter(CHARSET_INFO *tocs)
3696
return Item::safe_charset_converter(tocs);
3700
void Item_field::cleanup()
3702
Item_ident::cleanup();
3704
Even if this object was created by direct link to field in setup_wild()
3705
it will be linked correctly next time by name of field and table alias.
3706
I.e. we can drop 'field'.
3708
field= result_field= 0;
3714
Find a field among specified multiple equalities.
3716
The function first searches the field among multiple equalities
3717
of the current level (in the cond_equal->current_level list).
3718
If it fails, it continues searching in upper levels accessed
3719
through a pointer cond_equal->upper_levels.
3720
The search terminates as soon as a multiple equality containing
3723
@param cond_equal reference to list of multiple equalities where
3724
the field (this object) is to be looked for
3727
- First Item_equal containing the field, if success
3731
Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal)
3733
Item_equal *item= 0;
3736
List_iterator_fast<Item_equal> li(cond_equal->current_level);
3737
while ((item= li++))
3739
if (item->contains(field))
3743
The field is not found in any of the multiple equalities
3744
of the current level. Look for it in upper levels
3746
cond_equal= cond_equal->upper_levels;
3753
Check whether a field can be substituted by an equal item.
3755
The function checks whether a substitution of the field
3756
occurrence for an equal item is valid.
3758
@param arg *arg != NULL <-> the field is in the context where
3759
substitution for an equal item is valid
3762
The following statement is not always true:
3766
This means substitution of an item for an equal item not always
3767
yields an equavalent condition. Here's an example:
3770
(LENGTH('a')=1) != (LENGTH('a ')=2)
3772
Such a substitution is surely valid if either the substituted
3773
field is not of a STRING type or if it is an argument of
3774
a comparison predicate.
3777
true substitution is valid
3782
bool Item_field::subst_argument_checker(uchar **arg)
3784
return (result_type() != STRING_RESULT) || (*arg);
3789
Convert a numeric value to a zero-filled string
3791
@param[in,out] item the item to operate on
3792
@param field The field that this value is equated to
3794
This function converts a numeric value to a string. In this conversion
3795
the zero-fill flag of the field is taken into account.
3796
This is required so the resulting string value can be used instead of
3797
the field reference when propagating equalities.
3800
static void convert_zerofill_number_to_string(Item **item, Field_num *field)
3802
char buff[MAX_FIELD_WIDTH],*pos;
3803
String tmp(buff,sizeof(buff), field->charset()), *res;
3805
res= (*item)->val_str(&tmp);
3806
field->prepend_zeros(res);
3807
pos= (char *) sql_strmake (res->ptr(), res->length());
3808
*item= new Item_string(pos, res->length(), field->charset());
3813
Set a pointer to the multiple equality the field reference belongs to
3816
The function looks for a multiple equality containing the field item
3817
among those referenced by arg.
3818
In the case such equality exists the function does the following.
3819
If the found multiple equality contains a constant, then the field
3820
reference is substituted for this constant, otherwise it sets a pointer
3821
to the multiple equality in the field item.
3824
@param arg reference to list of multiple equalities where
3825
the field (this object) is to be looked for
3828
This function is supposed to be called as a callback parameter in calls
3829
of the compile method.
3832
- pointer to the replacing constant item, if the field item was substituted
3833
- pointer to the field item, otherwise.
3836
Item *Item_field::equal_fields_propagator(uchar *arg)
3840
item_equal= find_item_equal((COND_EQUAL *) arg);
3843
item= item_equal->get_const();
3845
Disable const propagation for items used in different comparison contexts.
3846
This must be done because, for example, Item_hex_string->val_int() is not
3847
the same as (Item_hex_string->val_str() in BINARY column)->val_int().
3848
We cannot simply disable the replacement in a particular context (
3849
e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
3850
Items don't know the context they are in and there are functions like
3851
IF (<hex_string>, 'yes', 'no').
3852
The same problem occurs when comparing a DATE/TIME field with a
3853
DATE/TIME represented as an int and as a string.
3856
(cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
3858
else if (field && (field->flags & ZEROFILL_FLAG) && IS_NUM(field->type()))
3860
if (item && cmp_context != INT_RESULT)
3861
convert_zerofill_number_to_string(&item, (Field_num *)field);
3870
Mark the item to not be part of substitution if it's not a binary item.
3872
See comments in Arg_comparator::set_compare_func() for details.
3875
bool Item_field::set_no_const_sub(uchar *arg __attribute__((__unused__)))
3877
if (field->charset() != &my_charset_bin)
3884
Replace an Item_field for an equal Item_field that evaluated earlier
3887
The function returns a pointer to an item that is taken from
3888
the very beginning of the item_equal list which the Item_field
3889
object refers to (belongs to) unless item_equal contains a constant
3890
item. In this case the function returns this constant item,
3891
(if the substitution does not require conversion).
3892
If the Item_field object does not refer any Item_equal object
3893
'this' is returned .
3895
@param arg a dummy parameter, is not used here
3899
This function is supposed to be called as a callback parameter in calls
3900
of the thransformer method.
3903
- pointer to a replacement Item_field if there is a better equal item or
3904
a pointer to a constant equal item;
3908
Item *Item_field::replace_equal_field(uchar *arg __attribute__((__unused__)))
3912
Item *const_item= item_equal->get_const();
3915
if (cmp_context != (Item_result)-1 &&
3916
const_item->cmp_context != cmp_context)
3920
Item_field *subst= item_equal->get_first();
3921
if (subst && !field->eq(subst->field))
1288
3928
void Item::init_make_field(Send_field *tmp_field,
1289
3929
enum enum_field_types field_type_arg)
4792
bool Item_field::send(Protocol *protocol,
4793
String *buffer __attribute__((__unused__)))
4795
return protocol->store(result_field);
4799
void Item_field::update_null_value()
4802
need to set no_errors to prevent warnings about type conversion
4805
THD *thd= field->table->in_use;
4808
no_errors= thd->no_errors;
4810
Item::update_null_value();
4811
thd->no_errors= no_errors;
4816
Add the field to the select list and substitute it for the reference to
4820
Item_field::update_value_transformer()
4821
select_arg current select
4824
If the field doesn't belong to the table being inserted into then it is
4825
added to the select list, pointer to it is stored in the ref_pointer_array
4826
of the select and the field itself is substituted for the Item_ref object.
4827
This is done in order to get correct values from update fields that
4828
belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
4833
ref if all conditions are met
4834
this field otherwise
4837
Item *Item_field::update_value_transformer(uchar *select_arg)
4839
SELECT_LEX *select= (SELECT_LEX*)select_arg;
4842
if (field->table != select->context.table_list->table &&
4843
type() != Item::TRIGGER_FIELD_ITEM)
4845
List<Item> *all_fields= &select->join->all_fields;
4846
Item **ref_pointer_array= select->ref_pointer_array;
4847
int el= all_fields->elements;
4850
ref_pointer_array[el]= (Item*)this;
4851
all_fields->push_front((Item*)this);
4852
ref= new Item_ref(&select->context, ref_pointer_array + el,
4853
table_name, field_name);
4860
void Item_field::print(String *str, enum_query_type query_type)
4862
if (field && field->table->const_table)
4864
char buff[MAX_FIELD_WIDTH];
4865
String tmp(buff,sizeof(buff),str->charset());
4866
field->val_str(&tmp);
4872
Item_ident::print(str, query_type);
4876
Item_ref::Item_ref(Name_resolution_context *context_arg,
4877
Item **item, const char *table_name_arg,
4878
const char *field_name_arg,
4879
bool alias_name_used_arg)
4880
:Item_ident(context_arg, NullS, table_name_arg, field_name_arg),
4881
result_field(0), ref(item)
4883
alias_name_used= alias_name_used_arg;
4885
This constructor used to create some internals references over fixed items
4887
if (ref && *ref && (*ref)->fixed)
4893
Resolve the name of a reference to a column reference.
4895
The method resolves the column reference represented by 'this' as a column
4896
present in one of: GROUP BY clause, SELECT clause, outer queries. It is
4897
used typically for columns in the HAVING clause which are not under
4898
aggregate functions.
4901
Item_ref::ref is 0 or points to a valid item.
4904
The name resolution algorithm used is (where [T_j] is an optional table
4905
name that qualifies the column name):
4908
resolve_extended([T_j].col_ref_i)
4910
Search for a column or derived column named col_ref_i [in table T_j]
4911
in the SELECT and GROUP clauses of Q.
4913
if such a column is NOT found AND // Lookup in outer queries.
4914
there are outer queries
4916
for each outer query Q_k beginning from the inner-most one
4918
Search for a column or derived column named col_ref_i
4919
[in table T_j] in the SELECT and GROUP clauses of Q_k.
4921
if such a column is not found AND
4922
- Q_k is not a group query AND
4923
- Q_k is not inside an aggregate function
4925
- Q_(k-1) is not in a HAVING or SELECT clause of Q_k
4927
search for a column or derived column named col_ref_i
4928
[in table T_j] in the FROM clause of Q_k;
4935
This procedure treats GROUP BY and SELECT clauses as one namespace for
4936
column references in HAVING. Notice that compared to
4937
Item_field::fix_fields, here we first search the SELECT and GROUP BY
4938
clauses, and then we search the FROM clause.
4940
@param[in] thd current thread
4941
@param[in,out] reference view column if this item was resolved to a
4945
Here we could first find the field anyway, and then test this
4946
condition, so that we can give a better error message -
4947
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4948
ER_BAD_FIELD_ERROR which we produce now.
4956
bool Item_ref::fix_fields(THD *thd, Item **reference)
4958
enum_parsing_place place= NO_MATTER;
4960
SELECT_LEX *current_sel= thd->lex->current_select;
4962
if (!ref || ref == not_found_item)
4964
if (!(ref= resolve_ref_in_select_and_group(thd, this,
4965
context->select_lex)))
4966
goto error; /* Some error occurred (e.g. ambiguous names). */
4968
if (ref == not_found_item) /* This reference was not resolved. */
4970
Name_resolution_context *last_checked_context= context;
4971
Name_resolution_context *outer_context= context->outer_context;
4977
/* The current reference cannot be resolved in this query. */
4978
my_error(ER_BAD_FIELD_ERROR,MYF(0),
4979
this->full_name(), current_thd->where);
4984
If there is an outer context (select), and it is not a derived table
4985
(which do not support the use of outer fields for now), try to
4986
resolve this reference in the outer select(s).
4988
We treat each subselect as a separate namespace, so that different
4989
subselects may contain columns with the same names. The subselects are
4990
searched starting from the innermost.
4992
from_field= (Field*) not_found_field;
4996
SELECT_LEX *select= outer_context->select_lex;
4997
Item_subselect *prev_subselect_item=
4998
last_checked_context->select_lex->master_unit()->item;
4999
last_checked_context= outer_context;
5001
/* Search in the SELECT and GROUP lists of the outer select. */
5002
if (outer_context->resolve_in_select_list)
5004
if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
5005
goto error; /* Some error occurred (e.g. ambiguous names). */
5006
if (ref != not_found_item)
5008
assert(*ref && (*ref)->fixed);
5009
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
5010
prev_subselect_item->const_item_cache&= (*ref)->const_item();
5014
Set ref to 0 to ensure that we get an error in case we replaced
5015
this item with another item and still use this item in some
5016
other place of the parse tree.
5021
place= prev_subselect_item->parsing_place;
5023
Check table fields only if the subquery is used somewhere out of
5024
HAVING or the outer SELECT does not use grouping (i.e. tables are
5027
Here we could first find the field anyway, and then test this
5028
condition, so that we can give a better error message -
5029
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
5030
ER_BAD_FIELD_ERROR which we produce now.
5032
if ((place != IN_HAVING ||
5033
(!select->with_sum_func &&
5034
select->group_list.elements == 0)))
5037
In case of view, find_field_in_tables() write pointer to view
5038
field expression to 'reference', i.e. it substitute that
5039
expression instead of this Item_ref
5041
from_field= find_field_in_tables(thd, this,
5043
first_name_resolution_table,
5045
last_name_resolution_table,
5047
IGNORE_EXCEPT_NON_UNIQUE,
5051
if (from_field == view_ref_found)
5053
Item::Type refer_type= (*reference)->type();
5054
prev_subselect_item->used_tables_cache|=
5055
(*reference)->used_tables();
5056
prev_subselect_item->const_item_cache&=
5057
(*reference)->const_item();
5058
assert((*reference)->type() == REF_ITEM);
5059
mark_as_dependent(thd, last_checked_context->select_lex,
5060
context->select_lex, this,
5061
((refer_type == REF_ITEM ||
5062
refer_type == FIELD_ITEM) ?
5063
(Item_ident*) (*reference) :
5066
view reference found, we substituted it instead of this
5071
if (from_field != not_found_field)
5073
if (cached_table && cached_table->select_lex &&
5074
outer_context->select_lex &&
5075
cached_table->select_lex != outer_context->select_lex)
5078
Due to cache, find_field_in_tables() can return field which
5079
doesn't belong to provided outer_context. In this case we have
5080
to find proper field context in order to fix field correcly.
5084
outer_context= outer_context->outer_context;
5085
select= outer_context->select_lex;
5086
prev_subselect_item=
5087
last_checked_context->select_lex->master_unit()->item;
5088
last_checked_context= outer_context;
5089
} while (outer_context && outer_context->select_lex &&
5090
cached_table->select_lex != outer_context->select_lex);
5092
prev_subselect_item->used_tables_cache|= from_field->table->map;
5093
prev_subselect_item->const_item_cache= 0;
5097
assert(from_field == not_found_field);
5099
/* Reference is not found => depend on outer (or just error). */
5100
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
5101
prev_subselect_item->const_item_cache= 0;
5103
outer_context= outer_context->outer_context;
5104
} while (outer_context);
5106
assert(from_field != 0 && from_field != view_ref_found);
5107
if (from_field != not_found_field)
5110
if (!(fld= new Item_field(from_field)))
5112
thd->change_item_tree(reference, fld);
5113
mark_as_dependent(thd, last_checked_context->select_lex,
5114
thd->lex->current_select, this, fld);
5116
A reference is resolved to a nest level that's outer or the same as
5117
the nest level of the enclosing set function : adjust the value of
5118
max_arg_level for the function if it's needed.
5120
if (thd->lex->in_sum_func &&
5121
thd->lex->in_sum_func->nest_level >=
5122
last_checked_context->select_lex->nest_level)
5123
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5124
last_checked_context->select_lex->nest_level);
5129
/* The item was not a table field and not a reference */
5130
my_error(ER_BAD_FIELD_ERROR, MYF(0),
5131
this->full_name(), current_thd->where);
5134
/* Should be checked in resolve_ref_in_select_and_group(). */
5135
assert(*ref && (*ref)->fixed);
5136
mark_as_dependent(thd, last_checked_context->select_lex,
5137
context->select_lex, this, this);
5139
A reference is resolved to a nest level that's outer or the same as
5140
the nest level of the enclosing set function : adjust the value of
5141
max_arg_level for the function if it's needed.
5143
if (thd->lex->in_sum_func &&
5144
thd->lex->in_sum_func->nest_level >=
5145
last_checked_context->select_lex->nest_level)
5146
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5147
last_checked_context->select_lex->nest_level);
5153
Check if this is an incorrect reference in a group function or forward
5154
reference. Do not issue an error if this is:
5155
1. outer reference (will be fixed later by the fix_inner_refs function);
5156
2. an unnamed reference inside an aggregate function.
5158
if (!((*ref)->type() == REF_ITEM &&
5159
((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
5160
(((*ref)->with_sum_func && name &&
5161
!(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
5162
current_sel->having_fix_field)) ||
5165
my_error(ER_ILLEGAL_REFERENCE, MYF(0),
5166
name, ((*ref)->with_sum_func?
5167
"reference to group function":
5168
"forward reference in item list"));
5174
if ((*ref)->check_cols(1))
5179
context->process_error(thd);
5184
void Item_ref::set_properties()
5186
max_length= (*ref)->max_length;
5187
maybe_null= (*ref)->maybe_null;
5188
decimals= (*ref)->decimals;
5189
collation.set((*ref)->collation);
5191
We have to remember if we refer to a sum function, to ensure that
5192
split_sum_func() doesn't try to change the reference.
5194
with_sum_func= (*ref)->with_sum_func;
5195
unsigned_flag= (*ref)->unsigned_flag;
5197
if (alias_name_used)
5199
if ((*ref)->type() == FIELD_ITEM)
5200
alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
5202
alias_name_used= true; // it is not field, so it is was resolved by alias
5206
void Item_ref::cleanup()
5208
Item_ident::cleanup();
5214
void Item_ref::print(String *str, enum_query_type query_type)
5218
if ((*ref)->type() != Item::CACHE_ITEM && ref_type() != VIEW_REF &&
5219
!table_name && name && alias_name_used)
5221
THD *thd= current_thd;
5222
append_identifier(thd, str, name, (uint) strlen(name));
5225
(*ref)->print(str, query_type);
5228
Item_ident::print(str, query_type);
5232
bool Item_ref::send(Protocol *prot, String *tmp)
5235
return prot->store(result_field);
5236
return (*ref)->send(prot, tmp);
5240
double Item_ref::val_result()
5244
if ((null_value= result_field->is_null()))
5246
return result_field->val_real();
5252
int64_t Item_ref::val_int_result()
5256
if ((null_value= result_field->is_null()))
5258
return result_field->val_int();
5264
String *Item_ref::str_result(String* str)
5268
if ((null_value= result_field->is_null()))
5270
str->set_charset(str_value.charset());
5271
return result_field->val_str(str, &str_value);
5273
return val_str(str);
5277
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
5281
if ((null_value= result_field->is_null()))
5283
return result_field->val_decimal(decimal_value);
5285
return val_decimal(decimal_value);
5289
bool Item_ref::val_bool_result()
5293
if ((null_value= result_field->is_null()))
5295
switch (result_field->result_type()) {
5297
return result_field->val_int() != 0;
5298
case DECIMAL_RESULT:
5300
my_decimal decimal_value;
5301
my_decimal *val= result_field->val_decimal(&decimal_value);
5303
return !my_decimal_is_zero(val);
5308
return result_field->val_real() != 0.0;
5318
double Item_ref::val_real()
5321
double tmp=(*ref)->val_result();
5322
null_value=(*ref)->null_value;
5327
int64_t Item_ref::val_int()
5330
int64_t tmp=(*ref)->val_int_result();
5331
null_value=(*ref)->null_value;
5336
bool Item_ref::val_bool()
5339
bool tmp= (*ref)->val_bool_result();
5340
null_value= (*ref)->null_value;
5345
String *Item_ref::val_str(String* tmp)
5348
tmp=(*ref)->str_result(tmp);
5349
null_value=(*ref)->null_value;
5354
bool Item_ref::is_null()
5357
return (*ref)->is_null();
5361
bool Item_ref::get_date(MYSQL_TIME *ltime,uint fuzzydate)
5363
return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5367
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
5369
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
5370
null_value= (*ref)->null_value;
5374
int Item_ref::save_in_field(Field *to, bool no_conversions)
5377
assert(!result_field);
5378
res= (*ref)->save_in_field(to, no_conversions);
5379
null_value= (*ref)->null_value;
5384
void Item_ref::save_org_in_field(Field *field)
5386
(*ref)->save_org_in_field(field);
5390
void Item_ref::make_field(Send_field *field)
5392
(*ref)->make_field(field);
5393
/* Non-zero in case of a view */
5395
field->col_name= name;
5397
field->table_name= table_name;
5399
field->db_name= db_name;
5403
Item *Item_ref::get_tmp_table_item(THD *thd)
5406
return (*ref)->get_tmp_table_item(thd);
5408
Item_field *item= new Item_field(result_field);
5411
item->table_name= table_name;
5412
item->db_name= db_name;
5418
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
5420
str->append(STRING_WITH_LEN("<ref_null_helper>("));
5422
(*ref)->print(str, query_type);
5429
double Item_direct_ref::val_real()
5431
double tmp=(*ref)->val_real();
5432
null_value=(*ref)->null_value;
5437
int64_t Item_direct_ref::val_int()
5439
int64_t tmp=(*ref)->val_int();
5440
null_value=(*ref)->null_value;
5445
String *Item_direct_ref::val_str(String* tmp)
5447
tmp=(*ref)->val_str(tmp);
5448
null_value=(*ref)->null_value;
5453
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
5455
my_decimal *tmp= (*ref)->val_decimal(decimal_value);
5456
null_value=(*ref)->null_value;
5461
bool Item_direct_ref::val_bool()
5463
bool tmp= (*ref)->val_bool();
5464
null_value=(*ref)->null_value;
5469
bool Item_direct_ref::is_null()
5471
return (*ref)->is_null();
5475
bool Item_direct_ref::get_date(MYSQL_TIME *ltime,uint fuzzydate)
5477
return (null_value=(*ref)->get_date(ltime,fuzzydate));
5482
Prepare referenced field then call usual Item_direct_ref::fix_fields .
5484
@param thd thread handler
5485
@param reference reference on reference where this item stored
5493
bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference)
5495
/* view fild reference must be defined */
5497
/* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */
5498
if (!(*ref)->fixed &&
5499
((*ref)->fix_fields(thd, ref)))
5501
return Item_direct_ref::fix_fields(thd, reference);
5505
Prepare referenced outer field then call usual Item_direct_ref::fix_fields
5508
Item_outer_ref::fix_fields()
5510
reference reference on reference where this item stored
5517
bool Item_outer_ref::fix_fields(THD *thd, Item **reference)
5520
/* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5521
if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
5523
err= Item_direct_ref::fix_fields(thd, reference);
5526
if ((*ref)->type() == Item::FIELD_ITEM)
5527
table_name= ((Item_field*)outer_ref)->table_name;
5531
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
5533
if (depended_from == new_parent)
5536
outer_ref->fix_after_pullout(new_parent, ref);
5540
void Item_ref::fix_after_pullout(st_select_lex *new_parent,
5541
Item **refptr __attribute__((__unused__)))
5543
if (depended_from == new_parent)
5545
(*ref)->fix_after_pullout(new_parent, ref);
5546
depended_from= NULL;
5551
Compare two view column references for equality.
5553
A view column reference is considered equal to another column
5554
reference if the second one is a view column and if both column
5555
references resolve to the same item. It is assumed that both
5556
items are of the same type.
5558
@param item item to compare with
5559
@param binary_cmp make binary comparison
5562
true Referenced item is equal to given item
5567
bool Item_direct_view_ref::eq(const Item *item,
5568
bool binary_cmp __attribute__((__unused__))) const
5570
if (item->type() == REF_ITEM)
5572
Item_ref *item_ref= (Item_ref*) item;
5573
if (item_ref->ref_type() == VIEW_REF)
5575
Item *item_ref_ref= *(item_ref->ref);
5576
return ((*ref)->real_item() == item_ref_ref->real_item());
1669
5582
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
1671
5584
return item->type() == DEFAULT_VALUE_ITEM &&
1931
5921
return result == field->val_real();
5924
Item_cache* Item_cache::get_cache(const Item *item)
5926
switch (item->result_type()) {
5928
return new Item_cache_int();
5930
return new Item_cache_real();
5931
case DECIMAL_RESULT:
5932
return new Item_cache_decimal();
5934
return new Item_cache_str(item);
5936
return new Item_cache_row();
5938
// should never be in real life
5945
void Item_cache::print(String *str, enum_query_type query_type)
5947
str->append(STRING_WITH_LEN("<cache>("));
5949
example->print(str, query_type);
5951
Item::print(str, query_type);
5956
void Item_cache_int::store(Item *item)
5958
value= item->val_int_result();
5959
null_value= item->null_value;
5960
unsigned_flag= item->unsigned_flag;
5964
void Item_cache_int::store(Item *item, int64_t val_arg)
5967
null_value= item->null_value;
5968
unsigned_flag= item->unsigned_flag;
5972
String *Item_cache_int::val_str(String *str)
5975
str->set(value, default_charset());
5980
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
5983
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
5988
void Item_cache_real::store(Item *item)
5990
value= item->val_result();
5991
null_value= item->null_value;
5995
int64_t Item_cache_real::val_int()
5998
return (int64_t) rint(value);
6002
String* Item_cache_real::val_str(String *str)
6005
str->set_real(value, decimals, default_charset());
6010
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
6013
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
6018
void Item_cache_decimal::store(Item *item)
6020
my_decimal *val= item->val_decimal_result(&decimal_value);
6021
if (!(null_value= item->null_value) && val != &decimal_value)
6022
my_decimal2decimal(val, &decimal_value);
6025
double Item_cache_decimal::val_real()
6029
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
6033
int64_t Item_cache_decimal::val_int()
6037
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
6041
String* Item_cache_decimal::val_str(String *str)
6044
my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
6046
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
6050
my_decimal *Item_cache_decimal::val_decimal(my_decimal *val __attribute__((__unused__)))
6053
return &decimal_value;
6057
void Item_cache_str::store(Item *item)
6059
value_buff.set(buffer, sizeof(buffer), item->collation.collation);
6060
value= item->str_result(&value_buff);
6061
if ((null_value= item->null_value))
6063
else if (value != &value_buff)
6066
We copy string value to avoid changing value if 'item' is table field
6067
in queries like following (where t1.c is varchar):
6069
(select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
6070
(select c from t1 where a=t2.a)
6073
value_buff.copy(*value);
6078
double Item_cache_str::val_real()
6084
return my_strntod(value->charset(), (char*) value->ptr(),
6085
value->length(), &end_not_used, &err_not_used);
6090
int64_t Item_cache_str::val_int()
6095
return my_strntoll(value->charset(), value->ptr(),
6096
value->length(), 10, (char**) 0, &err);
6101
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
6105
string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
6112
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
6114
int res= Item_cache::save_in_field(field, no_conversions);
6115
return (is_varbinary && field->type() == MYSQL_TYPE_STRING &&
6116
value->length() < field->field_length) ? 1 : res;
6120
bool Item_cache_row::allocate(uint num)
6123
THD *thd= current_thd;
6125
(Item_cache **) thd->calloc(sizeof(Item_cache *)*item_count)));
6129
bool Item_cache_row::setup(Item * item)
6132
if (!values && allocate(item->cols()))
6134
for (uint i= 0; i < item_count; i++)
6136
Item *el= item->element_index(i);
6138
if (!(tmp= values[i]= Item_cache::get_cache(el)))
6146
void Item_cache_row::store(Item * item)
6149
item->bring_value();
6150
for (uint i= 0; i < item_count; i++)
6152
values[i]->store(item->element_index(i));
6153
null_value|= values[i]->null_value;
6158
void Item_cache_row::illegal_method_call(const char *method __attribute__((__unused__)))
6161
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
6166
bool Item_cache_row::check_cols(uint c)
6168
if (c != item_count)
6170
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
6177
bool Item_cache_row::null_inside()
6179
for (uint i= 0; i < item_count; i++)
6181
if (values[i]->cols() > 1)
6183
if (values[i]->null_inside())
6188
values[i]->update_null_value();
6189
if (values[i]->null_value)
6197
void Item_cache_row::bring_value()
6199
for (uint i= 0; i < item_count; i++)
6200
values[i]->bring_value();
6205
Item_type_holder::Item_type_holder(THD *thd, Item *item)
6206
:Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
6208
assert(item->fixed);
6209
maybe_null= item->maybe_null;
6210
collation.set(item->collation);
6211
get_full_info(item);
6212
/* fix variable decimals which always is NOT_FIXED_DEC */
6213
if (Field::result_merge_type(fld_type) == INT_RESULT)
6215
prev_decimal_int_part= item->decimal_int_part();
6220
Return expression type of Item_type_holder.
6223
Item_result (type of internal MySQL expression result)
6226
Item_result Item_type_holder::result_type() const
6228
return Field::result_merge_type(fld_type);
6233
Find real field type of item.
6236
type of field which should be created to store item value
6239
enum_field_types Item_type_holder::get_real_type(Item *item)
6241
switch(item->type())
6246
Item_fields::field_type ask Field_type() but sometimes field return
6247
a different type, like for enum/set, so we need to ask real type.
6249
Field *field= ((Item_field *) item)->field;
6250
enum_field_types type= field->real_type();
6251
if (field->is_created_from_null_item)
6252
return MYSQL_TYPE_NULL;
6253
/* work around about varchar type field detection */
6254
if (type == MYSQL_TYPE_STRING && field->type() == MYSQL_TYPE_VAR_STRING)
6255
return MYSQL_TYPE_VAR_STRING;
6261
Argument of aggregate function sometimes should be asked about field
6264
Item_sum *item_sum= (Item_sum *) item;
6265
if (item_sum->keep_field_type())
6266
return get_real_type(item_sum->args[0]);
6270
if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC)
6273
There are work around of problem with changing variable type on the
6274
fly and variable always report "string" as field type to get
6275
acceptable information for client in send_field, so we make field
6276
type from expression type.
6278
switch (item->result_type()) {
6280
return MYSQL_TYPE_VAR_STRING;
6282
return MYSQL_TYPE_LONGLONG;
6284
return MYSQL_TYPE_DOUBLE;
6285
case DECIMAL_RESULT:
6286
return MYSQL_TYPE_NEWDECIMAL;
6290
return MYSQL_TYPE_VAR_STRING;
6297
return item->field_type();
6301
Find field type which can carry current Item_type_holder type and
6304
@param thd thread handler
6305
@param item given item to join its parameters with this item ones
6308
true error - types are incompatible
6313
bool Item_type_holder::join_types(THD *thd __attribute__((__unused__)),
6316
uint max_length_orig= max_length;
6317
uint decimals_orig= decimals;
6318
fld_type= Field::field_type_merge(fld_type, get_real_type(item));
6320
int item_decimals= item->decimals;
6321
/* fix variable decimals which always is NOT_FIXED_DEC */
6322
if (Field::result_merge_type(fld_type) == INT_RESULT)
6324
decimals= max(decimals, item_decimals);
6326
if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
6328
decimals= min(max(decimals, item->decimals), DECIMAL_MAX_SCALE);
6329
int precision= min(max(prev_decimal_int_part, item->decimal_int_part())
6330
+ decimals, DECIMAL_MAX_PRECISION);
6331
unsigned_flag&= item->unsigned_flag;
6332
max_length= my_decimal_precision_to_length(precision, decimals,
6336
switch (Field::result_merge_type(fld_type))
6340
const char *old_cs, *old_derivation;
6341
uint32 old_max_chars= max_length / collation.collation->mbmaxlen;
6342
old_cs= collation.collation->name;
6343
old_derivation= collation.derivation_name();
6344
if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
6346
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
6347
old_cs, old_derivation,
6348
item->collation.collation->name,
6349
item->collation.derivation_name(),
6354
To figure out max_length, we have to take into account possible
6355
expansion of the size of the values because of character set
6358
if (collation.collation != &my_charset_bin)
6360
max_length= max(old_max_chars * collation.collation->mbmaxlen,
6361
display_length(item) /
6362
item->collation.collation->mbmaxlen *
6363
collation.collation->mbmaxlen);
6366
set_if_bigger(max_length, display_length(item));
6371
if (decimals != NOT_FIXED_DEC)
6373
int delta1= max_length_orig - decimals_orig;
6374
int delta2= item->max_length - item->decimals;
6375
max_length= max(delta1, delta2) + decimals;
6376
if (fld_type == MYSQL_TYPE_DOUBLE && max_length > DBL_DIG + 2)
6378
max_length= DBL_DIG + 7;
6379
decimals= NOT_FIXED_DEC;
6383
max_length= DBL_DIG+7;
6387
max_length= max(max_length, display_length(item));
6389
maybe_null|= item->maybe_null;
6390
get_full_info(item);
6392
/* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
6393
prev_decimal_int_part= decimal_int_part();
6398
Calculate lenth for merging result for given Item type.
6400
@param item Item for length detection
6406
uint32 Item_type_holder::display_length(Item *item)
6408
if (item->type() == Item::FIELD_ITEM)
6409
return ((Item_field *)item)->max_disp_length();
6411
switch (item->field_type())
6413
case MYSQL_TYPE_TIMESTAMP:
6414
case MYSQL_TYPE_TIME:
6415
case MYSQL_TYPE_DATETIME:
6416
case MYSQL_TYPE_YEAR:
6417
case MYSQL_TYPE_NEWDATE:
6418
case MYSQL_TYPE_VARCHAR:
6419
case MYSQL_TYPE_NEWDECIMAL:
6420
case MYSQL_TYPE_ENUM:
6421
case MYSQL_TYPE_SET:
6422
case MYSQL_TYPE_BLOB:
6423
case MYSQL_TYPE_VAR_STRING:
6424
case MYSQL_TYPE_STRING:
6425
case MYSQL_TYPE_TINY:
6427
case MYSQL_TYPE_SHORT:
6429
case MYSQL_TYPE_LONG:
6430
return MY_INT32_NUM_DECIMAL_DIGITS;
6431
case MYSQL_TYPE_DOUBLE:
6433
case MYSQL_TYPE_NULL:
6435
case MYSQL_TYPE_LONGLONG:
6438
assert(0); // we should never go there
6445
Make temporary table field according collected information about type
6448
@param table temporary table for which we create fields
6454
Field *Item_type_holder::make_field_by_type(TABLE *table)
6457
The field functions defines a field to be not null if null_ptr is not 0
6459
uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
6463
case MYSQL_TYPE_ENUM:
6464
assert(enum_set_typelib);
6465
field= new Field_enum((uchar *) 0, max_length, null_ptr, 0,
6467
get_enum_pack_length(enum_set_typelib->count),
6468
enum_set_typelib, collation.collation);
6472
case MYSQL_TYPE_SET:
6473
assert(enum_set_typelib);
6474
field= new Field_set((uchar *) 0, max_length, null_ptr, 0,
6476
get_set_pack_length(enum_set_typelib->count),
6477
enum_set_typelib, collation.collation);
6481
case MYSQL_TYPE_NULL:
6482
return make_string_field(table);
6486
return tmp_table_field_from_field_type(table, 0);
6491
Get full information from Item about enum/set fields to be able to create
6494
@param item Item for information collection
6496
void Item_type_holder::get_full_info(Item *item)
6498
if (fld_type == MYSQL_TYPE_ENUM ||
6499
fld_type == MYSQL_TYPE_SET)
6501
if (item->type() == Item::SUM_FUNC_ITEM &&
6502
(((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
6503
((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC))
6504
item = ((Item_sum*)item)->args[0];
6506
We can have enum/set type after merging only if we have one enum|set
6507
field (or MIN|MAX(enum|set field)) and number of NULL fields
6509
assert((enum_set_typelib &&
6510
get_real_type(item) == MYSQL_TYPE_NULL) ||
6511
(!enum_set_typelib &&
6512
item->type() == Item::FIELD_ITEM &&
6513
(get_real_type(item) == MYSQL_TYPE_ENUM ||
6514
get_real_type(item) == MYSQL_TYPE_SET) &&
6515
((Field_enum*)((Item_field *) item)->field)->typelib));
6516
if (!enum_set_typelib)
6518
enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib;
6524
double Item_type_holder::val_real()
6526
assert(0); // should never be called
6531
int64_t Item_type_holder::val_int()
6533
assert(0); // should never be called
6537
my_decimal *Item_type_holder::val_decimal(my_decimal *)
6539
assert(0); // should never be called
6543
String *Item_type_holder::val_str(String*)
6545
assert(0); // should never be called
6549
void Item_result_field::cleanup()
1935
6557
Dummy error processor used by default by Name_resolution_context.