1043
956
Item *real_itm= real_item();
1045
958
ref_pointer_array[el]= real_itm;
1046
if (!(item_ref= new Item_aggregate_ref(&thd->lex->current_select->context,
959
if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
1047
960
ref_pointer_array + el, 0, name)))
1048
961
return; // fatal_error is set
1049
962
if (type() == SUM_FUNC_ITEM)
1050
item_ref->depended_from= ((Item_sum *) this)->depended_from();
963
item_ref->depended_from= ((Item_sum *) this)->depended_from();
1051
964
fields.push_front(real_itm);
1052
thd->change_item_tree(ref, item_ref);
1058
left_is_superset(DTCollation *left, DTCollation *right)
1060
/* Allow convert to Unicode */
1061
if (left->collation->state & MY_CS_UNICODE &&
1062
(left->derivation < right->derivation ||
1063
(left->derivation == right->derivation &&
1064
!(right->collation->state & MY_CS_UNICODE))))
1066
/* Allow convert from ASCII */
1067
if (right->repertoire == MY_REPERTOIRE_ASCII &&
1068
(left->derivation < right->derivation ||
1069
(left->derivation == right->derivation &&
1070
!(left->repertoire == MY_REPERTOIRE_ASCII))))
1072
/* Disallow conversion otherwise */
1077
Aggregate two collations together taking
1078
into account their coercibility (aka derivation):.
1080
0 == DERIVATION_EXPLICIT - an explicitly written COLLATE clause @n
1081
1 == DERIVATION_NONE - a mix of two different collations @n
1082
2 == DERIVATION_IMPLICIT - a column @n
1083
3 == DERIVATION_COERCIBLE - a string constant.
1085
The most important rules are:
1086
-# If collations are the same:
1087
chose this collation, and the strongest derivation.
1088
-# If collations are different:
1089
- Character sets may differ, but only if conversion without
1090
data loss is possible. The caller provides flags whether
1091
character set conversion attempts should be done. If no
1092
flags are substituted, then the character sets must be the same.
1093
Currently processed flags are:
1094
MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
1095
MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
1096
- two EXPLICIT collations produce an error, e.g. this is wrong:
1097
CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
1098
- the side with smaller derivation value wins,
1099
i.e. a column is stronger than a string constant,
1100
an explicit COLLATE clause is stronger than a column.
1101
- if derivations are the same, we have DERIVATION_NONE,
1102
we'll wait for an explicit COLLATE clause which possibly can
1103
come from another argument later: for example, this is valid,
1104
but we don't know yet when collecting the first two arguments:
1106
CONCAT(latin1_swedish_ci_column,
1107
latin1_german1_ci_column,
1108
expr COLLATE latin1_german2_ci)
1112
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
1114
if (!my_charset_same(collation, dt.collation))
1117
We do allow to use binary strings (like BLOBS)
1118
together with character strings.
1119
Binaries have more precedence than a character
1120
string of the same derivation.
1122
if (collation == &my_charset_bin)
1124
if (derivation <= dt.derivation)
1131
else if (dt.collation == &my_charset_bin)
1133
if (dt.derivation <= derivation)
1142
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1143
left_is_superset(this, &dt))
1147
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1148
left_is_superset(&dt, this))
1152
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1153
derivation < dt.derivation &&
1154
dt.derivation >= DERIVATION_SYSCONST)
1158
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1159
dt.derivation < derivation &&
1160
derivation >= DERIVATION_SYSCONST)
1166
// Cannot apply conversion
1167
set(0, DERIVATION_NONE, 0);
1171
else if (derivation < dt.derivation)
1175
else if (dt.derivation < derivation)
1181
if (collation == dt.collation)
1187
if (derivation == DERIVATION_EXPLICIT)
1189
set(0, DERIVATION_NONE, 0);
1192
if (collation->state & MY_CS_BINSORT)
1194
if (dt.collation->state & MY_CS_BINSORT)
1199
const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname,
1200
MY_CS_BINSORT,MYF(0));
1201
set(bin, DERIVATION_NONE);
1204
repertoire|= dt.repertoire;
1208
/******************************/
1210
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname)
1212
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
1213
c1.collation->name,c1.derivation_name(),
1214
c2.collation->name,c2.derivation_name(),
1220
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
1223
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
1224
c1.collation->name,c1.derivation_name(),
1225
c2.collation->name,c2.derivation_name(),
1226
c3.collation->name,c3.derivation_name(),
1232
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
1236
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
1237
else if (count == 3)
1238
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
1239
args[2*item_sep]->collation, fname);
1241
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
1245
bool agg_item_collations(DTCollation &c, const char *fname,
1246
Item **av, uint32_t count, uint32_t flags, int item_sep)
1250
c.set(av[0]->collation);
1251
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1253
if (c.aggregate((*arg)->collation, flags))
1255
my_coll_agg_error(av, count, fname, item_sep);
1259
if ((flags & MY_COLL_DISALLOW_NONE) &&
1260
c.derivation == DERIVATION_NONE)
1262
my_coll_agg_error(av, count, fname, item_sep);
1269
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1270
Item **av, uint32_t count, uint32_t flags)
1272
return (agg_item_collations(c, fname, av, count,
1273
flags | MY_COLL_DISALLOW_NONE, 1));
1278
Collect arguments' character sets together.
1280
We allow to apply automatic character set conversion in some cases.
1281
The conditions when conversion is possible are:
1282
- arguments A and B have different charsets
1283
- A wins according to coercibility rules
1284
(i.e. a column is stronger than a string constant,
1285
an explicit COLLATE clause is stronger than a column)
1286
- character set of A is either superset for character set of B,
1287
or B is a string constant which can be converted into the
1288
character set of A without data loss.
1290
If all of the above is true, then it's possible to convert
1291
B into the character set of A, and then compare according
1292
to the collation of A.
1294
For functions with more than two arguments:
1296
collect(A,B,C) ::= collect(collect(A,B),C)
1298
Since this function calls THD::change_item_tree() on the passed Item **
1299
pointers, it is necessary to pass the original Item **'s, not copies.
1300
Otherwise their values will not be properly restored (see BUG#20769).
1301
If the items are not consecutive (eg. args[2] and args[5]), use the
1302
item_sep argument, ie.
1304
agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
1308
bool agg_item_charsets(DTCollation &coll, const char *fname,
1309
Item **args, uint32_t nargs, uint32_t flags, int item_sep)
1311
Item **arg, *safe_args[2];
1313
memset(safe_args, 0, sizeof(safe_args));
1315
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
1319
For better error reporting: save the first and the second argument.
1320
We need this only if the the number of args is 3 or 2:
1321
- for a longer argument list, "Illegal mix of collations"
1322
doesn't display each argument's characteristics.
1323
- if nargs is 1, then this error cannot happen.
1325
if (nargs >=2 && nargs <= 3)
1327
safe_args[0]= args[0];
1328
safe_args[1]= args[item_sep];
1331
THD *thd= current_thd;
1335
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1338
uint32_t dummy_offset;
1339
if (!String::needs_conversion(0, (*arg)->collation.collation,
1344
if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
1345
((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
1346
conv= new Item_func_conv_charset(*arg, coll.collation, 1);
1350
if (nargs >=2 && nargs <= 3)
1352
/* restore the original arguments for better error message */
1353
args[0]= safe_args[0];
1354
args[item_sep]= safe_args[1];
1356
my_coll_agg_error(args, nargs, fname, item_sep);
1358
break; // we cannot return here, we need to restore "arena".
1360
if ((*arg)->type() == Item::FIELD_ITEM)
1361
((Item_field *)(*arg))->no_const_subst= 1;
1363
If in statement prepare, then we create a converter for two
1364
constant items, do it once and then reuse it.
1365
If we're in execution of a prepared statement, arena is NULL,
1366
and the conv was created in runtime memory. This can be
1367
the case only if the argument is a parameter marker ('?'),
1368
because for all true constants the charset converter has already
1369
been created in prepare. In this case register the change for
1372
thd->change_item_tree(arg, conv);
1374
We do not check conv->fixed, because Item_func_conv_charset which can
1375
be return by safe_charset_converter can't be fixed at creation
1377
conv->fix_fields(thd, arg);
1384
void Item_ident_for_show::make_field(Send_field *tmp_field)
1386
tmp_field->table_name= tmp_field->org_table_name= table_name;
1387
tmp_field->db_name= db_name;
1388
tmp_field->col_name= tmp_field->org_col_name= field->field_name;
1389
tmp_field->charsetnr= field->charset()->number;
1390
tmp_field->length=field->field_length;
1391
tmp_field->type=field->type();
1392
tmp_field->flags= field->table->maybe_null ?
1393
(field->flags & ~NOT_NULL_FLAG) : field->flags;
1394
tmp_field->decimals= field->decimals();
1397
/**********************************************/
1399
Item_field::Item_field(Field *f)
1400
:Item_ident(0, NULL, *f->table_name, f->field_name),
1401
item_equal(0), no_const_subst(0),
1402
have_privileges(0), any_privileges(0)
1406
field_name and table_name should not point to garbage
1407
if this item is to be reused
1409
orig_table_name= orig_field_name= "";
1414
Constructor used inside setup_wild().
1416
Ensures that field, table, and database names will live as long as
1417
Item_field (this is important in prepared statements).
1420
Item_field::Item_field(THD *thd __attribute__((unused)),
1421
Name_resolution_context *context_arg,
1423
:Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1424
item_equal(0), no_const_subst(0),
1425
have_privileges(0), any_privileges(0)
1431
Item_field::Item_field(Name_resolution_context *context_arg,
1432
const char *db_arg,const char *table_name_arg,
1433
const char *field_name_arg)
1434
:Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
1435
field(0), result_field(0), item_equal(0), no_const_subst(0),
1436
have_privileges(0), any_privileges(0)
1438
SELECT_LEX *select= current_thd->lex->current_select;
1439
collation.set(DERIVATION_IMPLICIT);
1440
if (select && select->parsing_place != IN_HAVING)
1441
select->select_n_where_fields++;
1445
Constructor need to process subselect with temporary tables (see Item)
1448
Item_field::Item_field(THD *thd, Item_field *item)
1449
:Item_ident(thd, item),
1451
result_field(item->result_field),
1452
item_equal(item->item_equal),
1453
no_const_subst(item->no_const_subst),
1454
have_privileges(item->have_privileges),
1455
any_privileges(item->any_privileges)
1457
collation.set(DERIVATION_IMPLICIT);
1460
void Item_field::set_field(Field *field_par)
1462
field=result_field=field_par; // for easy coding with fields
1463
maybe_null=field->maybe_null();
1464
decimals= field->decimals();
1465
max_length= field_par->max_display_length();
1466
table_name= *field_par->table_name;
1467
field_name= field_par->field_name;
1468
db_name= field_par->table->s->db.str;
1469
alias_name_used= field_par->table->alias_name_used;
1470
unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
1471
collation.set(field_par->charset(), field_par->derivation());
1473
if (field->table->s->tmp_table == SYSTEM_TMP_TABLE)
1479
Reset this item to point to a field from the new temporary table.
1480
This is used when we create a new temporary table for each execution
1481
of prepared statement.
1484
void Item_field::reset_field(Field *f)
1487
/* 'name' is pointing at field->field_name of old field */
1488
name= (char*) f->field_name;
1491
const char *Item_ident::full_name() const
1494
if (!table_name || !field_name)
1495
return field_name ? field_name : name ? name : "tmp_field";
1496
if (db_name && db_name[0])
1498
tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1499
(uint) strlen(field_name)+3);
1500
strxmov(tmp,db_name,".",table_name,".",field_name,NULL);
1506
tmp= (char*) sql_alloc((uint) strlen(table_name) +
1507
(uint) strlen(field_name) + 2);
1508
strxmov(tmp, table_name, ".", field_name, NULL);
1511
tmp= (char*) field_name;
1516
void Item_ident::print(String *str,
1517
enum_query_type query_type __attribute__((unused)))
1519
THD *thd= current_thd;
1520
char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1521
const char *d_name= db_name, *t_name= table_name;
1522
if (lower_case_table_names== 1 ||
1523
(lower_case_table_names == 2 && !alias_name_used))
1525
if (table_name && table_name[0])
1527
my_stpcpy(t_name_buff, table_name);
1528
my_casedn_str(files_charset_info, t_name_buff);
1529
t_name= t_name_buff;
1531
if (db_name && db_name[0])
1533
my_stpcpy(d_name_buff, db_name);
1534
my_casedn_str(files_charset_info, d_name_buff);
1535
d_name= d_name_buff;
1539
if (!table_name || !field_name || !field_name[0])
1541
const char *nm= (field_name && field_name[0]) ?
1542
field_name : name ? name : "tmp_field";
1543
append_identifier(thd, str, nm, (uint) strlen(nm));
1546
if (db_name && db_name[0] && !alias_name_used)
1549
append_identifier(thd, str, d_name, (uint)strlen(d_name));
1552
append_identifier(thd, str, t_name, (uint)strlen(t_name));
1554
append_identifier(thd, str, field_name, (uint)strlen(field_name));
1560
append_identifier(thd, str, t_name, (uint) strlen(t_name));
1562
append_identifier(thd, str, field_name, (uint) strlen(field_name));
1565
append_identifier(thd, str, field_name, (uint) strlen(field_name));
1570
String *Item_field::val_str(String *str)
1573
if ((null_value=field->is_null()))
1575
str->set_charset(str_value.charset());
1576
return field->val_str(str,&str_value);
1580
double Item_field::val_real()
1583
if ((null_value=field->is_null()))
1585
return field->val_real();
1589
int64_t Item_field::val_int()
1592
if ((null_value=field->is_null()))
1594
return field->val_int();
1598
my_decimal *Item_field::val_decimal(my_decimal *decimal_value)
1600
if ((null_value= field->is_null()))
1602
return field->val_decimal(decimal_value);
1606
String *Item_field::str_result(String *str)
1608
if ((null_value=result_field->is_null()))
1610
str->set_charset(str_value.charset());
1611
return result_field->val_str(str,&str_value);
1614
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1616
if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1618
memset(ltime, 0, sizeof(*ltime));
1624
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1626
if ((null_value=result_field->is_null()) ||
1627
result_field->get_date(ltime,fuzzydate))
1629
memset(ltime, 0, sizeof(*ltime));
1635
bool Item_field::get_time(DRIZZLE_TIME *ltime)
1637
if ((null_value=field->is_null()) || field->get_time(ltime))
1639
memset(ltime, 0, sizeof(*ltime));
1645
double Item_field::val_result()
1647
if ((null_value=result_field->is_null()))
1649
return result_field->val_real();
1652
int64_t Item_field::val_int_result()
1654
if ((null_value=result_field->is_null()))
1656
return result_field->val_int();
1660
my_decimal *Item_field::val_decimal_result(my_decimal *decimal_value)
1662
if ((null_value= result_field->is_null()))
1664
return result_field->val_decimal(decimal_value);
1668
bool Item_field::val_bool_result()
1670
if ((null_value= result_field->is_null()))
1672
switch (result_field->result_type()) {
1674
return result_field->val_int() != 0;
1675
case DECIMAL_RESULT:
1677
my_decimal decimal_value;
1678
my_decimal *val= result_field->val_decimal(&decimal_value);
1680
return !my_decimal_is_zero(val);
1685
return result_field->val_real() != 0.0;
1689
return 0; // Shut up compiler
1694
bool Item_field::eq(const Item *item,
1695
bool binary_cmp __attribute__((unused))) const
1697
Item *real_item= ((Item *) item)->real_item();
1698
if (real_item->type() != FIELD_ITEM)
1701
Item_field *item_field= (Item_field*) real_item;
1702
if (item_field->field && field)
1703
return item_field->field == field;
1705
We may come here when we are trying to find a function in a GROUP BY
1706
clause from the select list.
1707
In this case the '100 % correct' way to do this would be to first
1708
run fix_fields() on the GROUP BY item and then retry this function, but
1709
I think it's better to relax the checking a bit as we will in
1710
most cases do the correct thing by just checking the field name.
1711
(In cases where we would choose wrong we would have to generate a
1714
return (!my_strcasecmp(system_charset_info, item_field->name,
1716
(!item_field->table_name || !table_name ||
1717
(!my_strcasecmp(table_alias_charset, item_field->table_name,
1719
(!item_field->db_name || !db_name ||
1720
(item_field->db_name && !strcmp(item_field->db_name,
1725
table_map Item_field::used_tables() const
1727
if (field->table->const_table)
1728
return 0; // const item
1729
return (depended_from ? OUTER_REF_TABLE_BIT : field->table->map);
1733
void Item_field::fix_after_pullout(st_select_lex *new_parent,
1734
Item **ref __attribute__((unused)))
1736
if (new_parent == depended_from)
1737
depended_from= NULL;
1738
Name_resolution_context *ctx= new Name_resolution_context();
1739
ctx->outer_context= NULL; // We don't build a complete name resolver
1740
ctx->select_lex= new_parent;
1741
ctx->first_name_resolution_table= context->first_name_resolution_table;
1742
ctx->last_name_resolution_table= context->last_name_resolution_table;
1747
Item *Item_field::get_tmp_table_item(THD *thd)
1749
Item_field *new_item= new Item_field(thd, this);
1751
new_item->field= new_item->result_field;
1755
int64_t Item_field::val_int_endpoint(bool left_endp __attribute__((unused)),
1756
bool *incl_endp __attribute__((unused)))
1758
int64_t res= val_int();
1759
return null_value? INT64_MIN : res;
1763
Create an item from a string we KNOW points to a valid int64_t
1764
end \\0 terminated number string.
1765
This is always 'signed'. Unsigned values are created with Item_uint()
1768
Item_int::Item_int(const char *str_arg, uint32_t length)
1770
char *end_ptr= (char*) str_arg + length;
1772
value= my_strtoll10(str_arg, &end_ptr, &error);
1773
max_length= (uint) (end_ptr - str_arg);
1774
name= (char*) str_arg;
1779
my_decimal *Item_int::val_decimal(my_decimal *decimal_value)
1781
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value);
1782
return decimal_value;
1785
String *Item_int::val_str(String *str)
1787
// following assert is redundant, because fixed=1 assigned in constructor
1789
str->set(value, &my_charset_bin);
1793
void Item_int::print(String *str,
1794
enum_query_type query_type __attribute__((unused)))
1796
// my_charset_bin is good enough for numbers
1797
str_value.set(value, &my_charset_bin);
1798
str->append(str_value);
1802
Item_uint::Item_uint(const char *str_arg, uint32_t length):
1803
Item_int(str_arg, length)
1809
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
1810
Item_int(str_arg, i, length)
1816
String *Item_uint::val_str(String *str)
1818
// following assert is redundant, because fixed=1 assigned in constructor
1820
str->set((uint64_t) value, &my_charset_bin);
1825
void Item_uint::print(String *str,
1826
enum_query_type query_type __attribute__((unused)))
1828
// latin1 is good enough for numbers
1829
str_value.set((uint64_t) value, default_charset());
1830
str->append(str_value);
1834
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1835
const CHARSET_INFO * const charset)
1837
str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1838
name= (char*) str_arg;
1839
decimals= (uint8_t) decimal_value.frac;
1841
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1842
decimals, unsigned_flag);
1845
Item_decimal::Item_decimal(int64_t val, bool unsig)
1847
int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1848
decimals= (uint8_t) decimal_value.frac;
1850
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1851
decimals, unsigned_flag);
1855
Item_decimal::Item_decimal(double val,
1856
int precision __attribute__((unused)),
1857
int scale __attribute__((unused)))
1859
double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1860
decimals= (uint8_t) decimal_value.frac;
1862
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1863
decimals, unsigned_flag);
1867
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1868
uint32_t decimal_par, uint32_t length)
1870
my_decimal2decimal(val_arg, &decimal_value);
1872
decimals= (uint8_t) decimal_par;
1878
Item_decimal::Item_decimal(my_decimal *value_par)
1880
my_decimal2decimal(value_par, &decimal_value);
1881
decimals= (uint8_t) decimal_value.frac;
1883
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1884
decimals, unsigned_flag);
1888
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
1890
binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1891
&decimal_value, precision, scale);
1892
decimals= (uint8_t) decimal_value.frac;
1894
max_length= my_decimal_precision_to_length(precision, decimals,
1899
int64_t Item_decimal::val_int()
1902
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1906
double Item_decimal::val_real()
1909
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1913
String *Item_decimal::val_str(String *result)
1915
result->set_charset(&my_charset_bin);
1916
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
1920
void Item_decimal::print(String *str,
1921
enum_query_type query_type __attribute__((unused)))
1923
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1924
str->append(str_value);
1928
bool Item_decimal::eq(const Item *item,
1929
bool binary_cmp __attribute__((unused))) const
1931
if (type() == item->type() && item->basic_const_item())
1934
We need to cast off const to call val_decimal(). This should
1935
be OK for a basic constant. Additionally, we can pass 0 as
1936
a true decimal constant will return its internal decimal
1937
storage and ignore the argument.
1939
Item *arg= (Item*) item;
1940
my_decimal *value= arg->val_decimal(0);
1941
return !my_decimal_cmp(&decimal_value, value);
1947
void Item_decimal::set_decimal_value(my_decimal *value_par)
1949
my_decimal2decimal(value_par, &decimal_value);
1950
decimals= (uint8_t) decimal_value.frac;
1951
unsigned_flag= !decimal_value.sign();
1952
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1953
decimals, unsigned_flag);
1957
String *Item_float::val_str(String *str)
1959
// following assert is redundant, because fixed=1 assigned in constructor
1961
str->set_real(value,decimals,&my_charset_bin);
1966
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1968
// following assert is redundant, because fixed=1 assigned in constructor
1970
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1971
return (decimal_value);
1975
void Item_string::print(String *str, enum_query_type query_type)
1977
if (query_type == QT_ORDINARY && is_cs_specified())
1980
str->append(collation.collation->csname);
1985
if (query_type == QT_ORDINARY ||
1986
my_charset_same(str_value.charset(), system_charset_info))
1988
str_value.print(str);
1992
THD *thd= current_thd;
1993
LEX_STRING utf8_lex_str;
1995
thd->convert_string(&utf8_lex_str,
1996
system_charset_info,
1997
str_value.c_ptr_safe(),
1999
str_value.charset());
2001
String utf8_str(utf8_lex_str.str,
2002
utf8_lex_str.length,
2003
system_charset_info);
2005
utf8_str.print(str);
2012
double Item_string::val_real()
2016
char *end, *org_end;
2018
const CHARSET_INFO * const cs= str_value.charset();
2020
org_end= (char*) str_value.ptr() + str_value.length();
2021
tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
2023
if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2026
We can use str_value.ptr() here as Item_string is gurantee to put an
2029
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2030
ER_TRUNCATED_WRONG_VALUE,
2031
ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
2040
Give error if we wanted a signed integer and we got an unsigned one
2042
int64_t Item_string::val_int()
2047
char *end= (char*) str_value.ptr()+ str_value.length();
2049
const CHARSET_INFO * const cs= str_value.charset();
2051
tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
2053
TODO: Give error if we wanted a signed integer and we got an unsigned
2057
(end != org_end && !check_if_only_end_space(cs, end, org_end)))
2059
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2060
ER_TRUNCATED_WRONG_VALUE,
2061
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2068
my_decimal *Item_string::val_decimal(my_decimal *decimal_value)
2070
return val_decimal_from_string(decimal_value);
2074
bool Item_null::eq(const Item *item,
2075
bool binary_cmp __attribute__((unused))) const
2076
{ return item->type() == type(); }
2079
double Item_null::val_real()
2081
// following assert is redundant, because fixed=1 assigned in constructor
2086
int64_t Item_null::val_int()
2088
// following assert is redundant, because fixed=1 assigned in constructor
2094
String *Item_null::val_str(String *str __attribute__((unused)))
2096
// following assert is redundant, because fixed=1 assigned in constructor
2102
my_decimal *Item_null::val_decimal(my_decimal *decimal_value __attribute__((unused)))
2108
Item *Item_null::safe_charset_converter(const CHARSET_INFO * const tocs)
2110
collation.set(tocs);
2114
/*********************** Item_param related ******************************/
2117
Default function of Item_param::set_param_func, so in case
2118
of malformed packet the server won't SIGSEGV.
2122
default_set_param_func(Item_param *param,
2123
unsigned char **pos __attribute__((unused)),
2124
ulong len __attribute__((unused)))
2130
Item_param::Item_param(uint32_t pos_in_query_arg) :
2132
item_result_type(STRING_RESULT),
2133
/* Don't pretend to be a literal unless value for this item is set. */
2134
item_type(PARAM_ITEM),
2135
param_type(DRIZZLE_TYPE_VARCHAR),
2136
pos_in_query(pos_in_query_arg),
2137
set_param_func(default_set_param_func),
2138
limit_clause_param(false)
2142
Since we can't say whenever this item can be NULL or cannot be NULL
2143
before mysql_stmt_execute(), so we assuming that it can be NULL until
2147
cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE);
2148
cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin);
2152
void Item_param::set_null()
2154
/* These are cleared after each execution by reset() method */
2157
Because of NULL and string values we need to set max_length for each new
2158
placeholder value: user can submit NULL for any placeholder type, and
2159
string length can be different in each execution.
2164
item_type= Item::NULL_ITEM;
2168
void Item_param::set_int(int64_t i, uint32_t max_length_arg)
2170
value.integer= (int64_t) i;
2172
max_length= max_length_arg;
2178
void Item_param::set_double(double d)
2182
max_length= DBL_DIG + 8;
2183
decimals= NOT_FIXED_DEC;
2190
Set decimal parameter value from string.
2192
@param str character string
2193
@param length string length
2196
As we use character strings to send decimal values in
2197
binary protocol, we use str2my_decimal to convert it to
2198
internal decimal value.
2201
void Item_param::set_decimal(char *str, ulong length)
2206
str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2207
state= DECIMAL_VALUE;
2208
decimals= decimal_value.frac;
2209
max_length= my_decimal_precision_to_length(decimal_value.precision(),
2210
decimals, unsigned_flag);
2217
Set parameter value from DRIZZLE_TIME value.
2219
@param tm datetime value to set (time_type is ignored)
2220
@param type type of datetime value
2221
@param max_length_arg max length of datetime value as string
2224
If we value to be stored is not normalized, zero value will be stored
2225
instead and proper warning will be produced. This function relies on
2226
the fact that even wrong value sent over binary protocol fits into
2227
MAX_DATE_STRING_REP_LENGTH buffer.
2229
void Item_param::set_time(DRIZZLE_TIME *tm,
2230
enum enum_drizzle_timestamp_type time_type,
2231
uint32_t max_length_arg)
2234
value.time.time_type= time_type;
2236
if (value.time.year > 9999 || value.time.month > 12 ||
2237
value.time.day > 31 ||
2238
((time_type != DRIZZLE_TIMESTAMP_TIME) && value.time.hour > 23) ||
2239
value.time.minute > 59 || value.time.second > 59)
2241
char buff[MAX_DATE_STRING_REP_LENGTH];
2242
uint32_t length= my_TIME_to_str(&value.time, buff);
2243
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2244
buff, length, time_type, 0);
2245
set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
2250
max_length= max_length_arg;
2256
bool Item_param::set_str(const char *str, ulong length)
2259
Assign string with no conversion: data is converted only after it's
2260
been written to the binary log.
2262
uint32_t dummy_errors;
2263
if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
2266
state= STRING_VALUE;
2269
/* max_length and decimals are set after charset conversion */
2270
/* sic: str may be not null-terminated */
2275
bool Item_param::set_longdata(const char *str, ulong length)
2278
If client character set is multibyte, end of long data packet
2279
may hit at the middle of a multibyte character. Additionally,
2280
if binary log is open we must write long data value to the
2281
binary log in character set of client. This is why we can't
2282
convert long data to connection character set as it comes
2283
(here), and first have to concatenate all pieces together,
2284
write query to the binary log and only then perform conversion.
2286
if (str_value.append(str, length, &my_charset_bin))
2288
state= LONG_DATA_VALUE;
2296
Set parameter value from user variable value.
2298
@param thd Current thread
2299
@param entry User variable structure (NULL means use NULL value)
2307
bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry)
2309
if (entry && entry->value)
2311
item_result_type= entry->type;
2312
unsigned_flag= entry->unsigned_flag;
2313
if (limit_clause_param)
2316
set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
2317
item_type= Item::INT_ITEM;
2318
return(!unsigned_flag && value.integer < 0 ? 1 : 0);
2320
switch (item_result_type) {
2322
set_double(*(double*)entry->value);
2323
item_type= Item::REAL_ITEM;
2326
set_int(*(int64_t*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
2327
item_type= Item::INT_ITEM;
2331
const CHARSET_INFO * const fromcs= entry->collation.collation;
2332
const CHARSET_INFO * const tocs= thd->variables.collation_connection;
2333
uint32_t dummy_offset;
2335
value.cs_info.character_set_of_placeholder=
2336
value.cs_info.character_set_client= fromcs;
2338
Setup source and destination character sets so that they
2339
are different only if conversion is necessary: this will
2340
make later checks easier.
2342
value.cs_info.final_character_set_of_str_value=
2343
String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
2346
Exact value of max_length is not known unless data is converted to
2347
charset of connection, so we have to set it later.
2349
item_type= Item::STRING_ITEM;
2351
if (set_str((const char *)entry->value, entry->length))
2355
case DECIMAL_RESULT:
2357
const my_decimal *ent_value= (const my_decimal *)entry->value;
2358
my_decimal2decimal(ent_value, &decimal_value);
2359
state= DECIMAL_VALUE;
2360
decimals= ent_value->frac;
2361
max_length= my_decimal_precision_to_length(ent_value->precision(),
2362
decimals, unsigned_flag);
2363
item_type= Item::DECIMAL_ITEM;
2378
Resets parameter after execution.
2381
We clear null_value here instead of setting it in set_* methods,
2382
because we want more easily handle case for long data.
2385
void Item_param::reset()
2387
/* Shrink string buffer if it's bigger than max possible CHAR column */
2388
if (str_value.alloced_length() > MAX_CHAR_WIDTH)
2391
str_value.length(0);
2392
str_value_ptr.length(0);
2394
We must prevent all charset conversions until data has been written
2397
str_value.set_charset(&my_charset_bin);
2398
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
2403
Don't reset item_type to PARAM_ITEM: it's only needed to guard
2404
us from item optimizations at prepare stage, when item doesn't yet
2405
contain a literal of some kind.
2406
In all other cases when this object is accessed its value is
2407
set (this assumption is guarded by 'state' and
2408
assertS(state != NO_VALUE) in all Item_param::get_*
2415
int Item_param::save_in_field(Field *field, bool no_conversions)
2417
field->set_notnull();
2421
return field->store(value.integer, unsigned_flag);
2423
return field->store(value.real);
2425
return field->store_decimal(&decimal_value);
2427
field->store_time(&value.time, value.time.time_type);
2430
case LONG_DATA_VALUE:
2431
return field->store(str_value.ptr(), str_value.length(),
2432
str_value.charset());
2434
return set_field_to_null_with_conversions(field, no_conversions);
2443
bool Item_param::get_time(DRIZZLE_TIME *res)
2445
if (state == TIME_VALUE)
2451
If parameter value isn't supplied assertion will fire in val_str()
2452
which is called from Item::get_time().
2454
return Item::get_time(res);
2458
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
2460
if (state == TIME_VALUE)
2465
return Item::get_date(res, fuzzydate);
2469
double Item_param::val_real()
2475
return (double) value.integer;
2479
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
2483
case LONG_DATA_VALUE:
2487
return my_strntod(str_value.charset(), (char*) str_value.ptr(),
2488
str_value.length(), &end_not_used, &dummy_err);
2492
This works for example when user says SELECT ?+0.0 and supplies
2493
time value for the placeholder.
2495
return uint64_t2double(TIME_to_uint64_t(&value.time));
2505
int64_t Item_param::val_int()
2509
return (int64_t) rint(value.real);
2511
return value.integer;
2515
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
2519
case LONG_DATA_VALUE:
2522
return my_strntoll(str_value.charset(), str_value.ptr(),
2523
str_value.length(), 10, (char**) 0, &dummy_err);
2526
return (int64_t) TIME_to_uint64_t(&value.time);
2536
my_decimal *Item_param::val_decimal(my_decimal *dec)
2540
return &decimal_value;
2542
double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
2545
int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec);
2548
case LONG_DATA_VALUE:
2549
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec);
2553
int64_t i= (int64_t) TIME_to_uint64_t(&value.time);
2554
int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
2566
String *Item_param::val_str(String* str)
2570
case LONG_DATA_VALUE:
2571
return &str_value_ptr;
2573
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2576
str->set(value.integer, &my_charset_bin);
2579
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2585
if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
2587
str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
2588
str->set_charset(&my_charset_bin);
2600
Return Param item values in string format, for generating the dynamic
2601
query used in update/binary logs.
2604
- Change interface and implementation to fill log data in place
2605
and avoid one more memcpy/alloc between str and log string.
2606
- In case of error we need to notify replication
2607
that binary log contains wrong statement
2610
const String *Item_param::query_val_str(String* str) const
2614
str->set_int(value.integer, unsigned_flag, &my_charset_bin);
2617
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2620
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2622
return &my_null_string;
2629
TODO: in case of error we need to notify replication
2630
that binary log contains wrong statement
2632
if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3))
2635
/* Create date string inplace */
2636
buf= str->c_ptr_quick();
2639
ptr+= (uint) my_TIME_to_str(&value.time, ptr);
2641
str->length((uint32_t) (ptr - buf));
2645
case LONG_DATA_VALUE:
2648
append_query_string(value.cs_info.character_set_client, &str_value, str);
2652
return &my_null_string;
2661
Convert string from client character set to the character set of
2665
bool Item_param::convert_str_value(THD *thd)
2668
if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2671
Check is so simple because all charsets were set up properly
2672
in setup_one_conversion_function, where typecode of
2673
placeholder was also taken into account: the variables are different
2674
here only if conversion is really necessary.
2676
if (value.cs_info.final_character_set_of_str_value !=
2677
value.cs_info.character_set_of_placeholder)
2679
rc= thd->convert_string(&str_value,
2680
value.cs_info.character_set_of_placeholder,
2681
value.cs_info.final_character_set_of_str_value);
2684
str_value.set_charset(value.cs_info.final_character_set_of_str_value);
2685
/* Here str_value is guaranteed to be in final_character_set_of_str_value */
2687
max_length= str_value.length();
2690
str_value_ptr is returned from val_str(). It must be not alloced
2691
to prevent it's modification by val_str() invoker.
2693
str_value_ptr.set(str_value.ptr(), str_value.length(),
2694
str_value.charset());
2695
/* Synchronize item charset with value charset */
2696
collation.set(str_value.charset(), DERIVATION_COERCIBLE);
2702
bool Item_param::basic_const_item() const
2704
if (state == NO_VALUE || state == TIME_VALUE)
2711
Item_param::clone_item()
2713
/* see comments in the header file */
2716
return new Item_null(name);
2718
return (unsigned_flag ?
2719
new Item_uint(name, value.integer, max_length) :
2720
new Item_int(name, value.integer, max_length));
2722
return new Item_float(name, value.real, decimals, max_length);
2724
case LONG_DATA_VALUE:
2725
return new Item_string(name, str_value.c_ptr_quick(), str_value.length(),
2726
str_value.charset());
2738
Item_param::eq(const Item *arg, bool binary_cmp) const
2741
if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2744
We need to cast off const to call val_int(). This should be OK for
2753
return value.integer == item->val_int() &&
2754
unsigned_flag == item->unsigned_flag;
2756
return value.real == item->val_real();
2758
case LONG_DATA_VALUE:
2760
return !stringcmp(&str_value, &item->str_value);
2761
return !sortcmp(&str_value, &item->str_value, collation.collation);
2768
/* End of Item_param related */
2770
void Item_param::print(String *str,
2771
enum_query_type query_type __attribute__((unused)))
2773
if (state == NO_VALUE)
2779
char buffer[STRING_BUFFER_USUAL_SIZE];
2780
String tmp(buffer, sizeof(buffer), &my_charset_bin);
2782
res= query_val_str(&tmp);
2788
/****************************************************************************
2790
****************************************************************************/
2792
void Item_copy_string::copy()
2794
String *res=item->val_str(&str_value);
2795
if (res && res != &str_value)
2796
str_value.copy(*res);
2797
null_value=item->null_value;
2801
String *Item_copy_string::val_str(String *str __attribute__((unused)))
2803
// Item_copy_string is used without fix_fields call
2810
my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value)
2812
// Item_copy_string is used without fix_fields call
2815
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value);
2816
return (decimal_value);
965
session->change_item_tree(ref, item_ref);
2821
970
Functions to convert item to field (for send_fields)
2825
bool Item::fix_fields(THD *thd __attribute__((unused)),
2826
Item **ref __attribute__((unused)))
974
bool Item::fix_fields(Session *, Item **)
2829
977
// We do not check fields which are fixed during construction
3188
1286
return (Item**) not_found_item;
3193
Resolve the name of an outer select column reference.
3195
The method resolves the column reference represented by 'this' as a column
3196
present in outer selects that contain current select.
3198
In prepared statements, because of cache, find_field_in_tables()
3199
can resolve fields even if they don't belong to current context.
3200
In this case this method only finds appropriate context and marks
3201
current select as dependent. The found reference of field should be
3202
provided in 'from_field'.
3204
@param[in] thd current thread
3205
@param[in,out] from_field found field reference or (Field*)not_found_field
3206
@param[in,out] reference view column if this item was resolved to a
3210
This is the inner loop of Item_field::fix_fields:
3212
for each outer query Q_k beginning from the inner-most one
3214
search for a column or derived column named col_ref_i
3215
[in table T_j] in the FROM clause of Q_k;
3217
if such a column is not found
3218
Search for a column or derived column named col_ref_i
3219
[in table T_j] in the SELECT and GROUP clauses of Q_k.
3224
1 column succefully resolved and fix_fields() should continue.
3226
0 column fully fixed and fix_fields() should return false
3232
Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
3234
enum_parsing_place place= NO_MATTER;
3235
bool field_found= (*from_field != not_found_field);
3236
bool upward_lookup= false;
3239
If there are outer contexts (outer selects, but current select is
3240
not derived table or view) try to resolve this reference in the
3243
We treat each subselect as a separate namespace, so that different
3244
subselects may contain columns with the same names. The subselects
3245
are searched starting from the innermost.
3247
Name_resolution_context *last_checked_context= context;
3248
Item **ref= (Item **) not_found_item;
3249
SELECT_LEX *current_sel= (SELECT_LEX *) thd->lex->current_select;
3250
Name_resolution_context *outer_context= 0;
3251
SELECT_LEX *select= 0;
3252
/* Currently derived tables cannot be correlated */
3253
if (current_sel->master_unit()->first_select()->linkage !=
3255
outer_context= context->outer_context;
3258
outer_context= outer_context->outer_context)
3260
select= outer_context->select_lex;
3261
Item_subselect *prev_subselect_item=
3262
last_checked_context->select_lex->master_unit()->item;
3263
last_checked_context= outer_context;
3264
upward_lookup= true;
3266
place= prev_subselect_item->parsing_place;
3268
If outer_field is set, field was already found by first call
3269
to find_field_in_tables(). Only need to find appropriate context.
3271
if (field_found && outer_context->select_lex !=
3272
cached_table->select_lex)
3275
In case of a view, find_field_in_tables() writes the pointer to
3276
the found view field into '*reference', in other words, it
3277
substitutes this Item_field with the found expression.
3279
if (field_found || (*from_field= find_field_in_tables(thd, this,
3281
first_name_resolution_table,
3283
last_name_resolution_table,
3285
IGNORE_EXCEPT_NON_UNIQUE,
3291
if (*from_field != view_ref_found)
3293
prev_subselect_item->used_tables_cache|= (*from_field)->table->map;
3294
prev_subselect_item->const_item_cache= 0;
3295
set_field(*from_field);
3296
if (!last_checked_context->select_lex->having_fix_field &&
3297
select->group_list.elements &&
3298
(place == SELECT_LIST || place == IN_HAVING))
3302
If an outer field is resolved in a grouping select then it
3303
is replaced for an Item_outer_ref object. Otherwise an
3304
Item_field object is used.
3305
The new Item_outer_ref object is saved in the inner_refs_list of
3306
the outer select. Here it is only created. It can be fixed only
3307
after the original field has been fixed and this is done in the
3308
fix_inner_refs() function.
3311
if (!(rf= new Item_outer_ref(context, this)))
3313
thd->change_item_tree(reference, rf);
3314
select->inner_refs_list.push_back(rf);
3315
rf->in_sum_func= thd->lex->in_sum_func;
3318
A reference is resolved to a nest level that's outer or the same as
3319
the nest level of the enclosing set function : adjust the value of
3320
max_arg_level for the function if it's needed.
3322
if (thd->lex->in_sum_func &&
3323
thd->lex->in_sum_func->nest_level >= select->nest_level)
3325
Item::Type ref_type= (*reference)->type();
3326
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3327
select->nest_level);
3328
set_field(*from_field);
3330
mark_as_dependent(thd, last_checked_context->select_lex,
3331
context->select_lex, this,
3332
((ref_type == REF_ITEM ||
3333
ref_type == FIELD_ITEM) ?
3334
(Item_ident*) (*reference) : 0));
3340
Item::Type ref_type= (*reference)->type();
3341
prev_subselect_item->used_tables_cache|=
3342
(*reference)->used_tables();
3343
prev_subselect_item->const_item_cache&=
3344
(*reference)->const_item();
3345
mark_as_dependent(thd, last_checked_context->select_lex,
3346
context->select_lex, this,
3347
((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3348
(Item_ident*) (*reference) :
3351
A reference to a view field had been found and we
3352
substituted it instead of this Item (find_field_in_tables
3353
does it by assigning the new value to *reference), so now
3354
we can return from this function.
3362
/* Search in SELECT and GROUP lists of the outer select. */
3363
if (place != IN_WHERE && place != IN_ON)
3365
if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
3366
return -1; /* Some error occurred (e.g. ambiguous names). */
3367
if (ref != not_found_item)
3369
assert(*ref && (*ref)->fixed);
3370
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
3371
prev_subselect_item->const_item_cache&= (*ref)->const_item();
3377
Reference is not found in this select => this subquery depend on
3378
outer select (or we just trying to find wrong identifier, in this
3379
case it does not matter which used tables bits we set)
3381
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
3382
prev_subselect_item->const_item_cache= 0;
3388
if (ref == not_found_item && *from_field == not_found_field)
3392
// We can't say exactly what absent table or field
3393
my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where);
3397
/* Call find_field_in_tables only to report the error */
3398
find_field_in_tables(thd, this,
3399
context->first_name_resolution_table,
3400
context->last_name_resolution_table,
3401
reference, REPORT_ALL_ERRORS,
3407
else if (ref != not_found_item)
3412
/* Should have been checked in resolve_ref_in_select_and_group(). */
3413
assert(*ref && (*ref)->fixed);
3415
Here, a subset of actions performed by Item_ref::set_properties
3416
is not enough. So we pass ptr to NULL into Item_[direct]_ref
3417
constructor, so no initialization is performed, and call
3421
*ref= NULL; // Don't call set_properties()
3422
rf= (place == IN_HAVING ?
3423
new Item_ref(context, ref, (char*) table_name,
3424
(char*) field_name, alias_name_used) :
3425
(!select->group_list.elements ?
3426
new Item_direct_ref(context, ref, (char*) table_name,
3427
(char*) field_name, alias_name_used) :
3428
new Item_outer_ref(context, ref, (char*) table_name,
3429
(char*) field_name, alias_name_used)));
3434
if (place != IN_HAVING && select->group_list.elements)
3436
outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3437
((Item_outer_ref*)rf)->in_sum_func= thd->lex->in_sum_func;
3439
thd->change_item_tree(reference, rf);
3441
rf is Item_ref => never substitute other items (in this case)
3442
during fix_fields() => we can use rf after fix_fields()
3444
assert(!rf->fixed); // Assured by Item_ref()
3445
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3448
mark_as_dependent(thd, last_checked_context->select_lex,
3449
context->select_lex, this,
3455
mark_as_dependent(thd, last_checked_context->select_lex,
3456
context->select_lex,
3457
this, (Item_ident*)*reference);
3458
if (last_checked_context->select_lex->having_fix_field)
3461
rf= new Item_ref(context,
3462
(cached_table->db[0] ? cached_table->db : 0),
3463
(char*) cached_table->alias, (char*) field_name);
3466
thd->change_item_tree(reference, rf);
3468
rf is Item_ref => never substitute other items (in this case)
3469
during fix_fields() => we can use rf after fix_fields()
3471
assert(!rf->fixed); // Assured by Item_ref()
3472
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3482
Resolve the name of a column reference.
3484
The method resolves the column reference represented by 'this' as a column
3485
present in one of: FROM clause, SELECT clause, GROUP BY clause of a query
3486
Q, or in outer queries that contain Q.
3488
The name resolution algorithm used is (where [T_j] is an optional table
3489
name that qualifies the column name):
3492
resolve_column_reference([T_j].col_ref_i)
3494
search for a column or derived column named col_ref_i
3495
[in table T_j] in the FROM clause of Q;
3497
if such a column is NOT found AND // Lookup in outer queries.
3498
there are outer queries
3500
for each outer query Q_k beginning from the inner-most one
3502
search for a column or derived column named col_ref_i
3503
[in table T_j] in the FROM clause of Q_k;
3505
if such a column is not found
3506
Search for a column or derived column named col_ref_i
3507
[in table T_j] in the SELECT and GROUP clauses of Q_k.
3513
Notice that compared to Item_ref::fix_fields, here we first search the FROM
3514
clause, and then we search the SELECT and GROUP BY clauses.
3516
@param[in] thd current thread
3517
@param[in,out] reference view column if this item was resolved to a
3526
bool Item_field::fix_fields(THD *thd, Item **reference)
3529
Field *from_field= (Field *)not_found_field;
3530
bool outer_fixed= false;
3532
if (!field) // If field is not checked
3535
In case of view, find_field_in_tables() write pointer to view field
3536
expression to 'reference', i.e. it substitute that expression instead
3539
if ((from_field= find_field_in_tables(thd, this,
3540
context->first_name_resolution_table,
3541
context->last_name_resolution_table,
3543
thd->lex->use_only_table_context ?
3545
IGNORE_EXCEPT_NON_UNIQUE,
3551
/* Look up in current select's item_list to find aliased fields */
3552
if (thd->lex->current_select->is_item_list_lookup)
3555
enum_resolution_type resolution;
3556
Item** res= find_item_in_list(this, thd->lex->current_select->item_list,
3557
&counter, REPORT_EXCEPT_NOT_FOUND,
3561
if (resolution == RESOLVED_AGAINST_ALIAS)
3562
alias_name_used= true;
3563
if (res != (Item **)not_found_item)
3565
if ((*res)->type() == Item::FIELD_ITEM)
3568
It's an Item_field referencing another Item_field in the select
3570
Use the field from the Item_field in the select list and leave
3571
the Item_field instance in place.
3574
Field *new_field= (*((Item_field**)res))->field;
3576
if (new_field == NULL)
3578
/* The column to which we link isn't valid. */
3579
my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
3580
current_thd->where);
3584
set_field(new_field);
3590
It's not an Item_field in the select list so we must make a new
3591
Item_ref to point to the Item in the select list and replace the
3592
Item_field created by the parser with the new Item_ref.
3594
Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3597
thd->change_item_tree(reference, rf);
3599
Because Item_ref never substitutes itself with other items
3600
in Item_ref::fix_fields(), we can safely use the original
3601
pointer to it even after fix_fields()
3603
return rf->fix_fields(thd, reference) || rf->check_cols(1);
3607
if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3611
goto mark_non_agg_field;
3613
else if (!from_field)
3616
if (!outer_fixed && cached_table && cached_table->select_lex &&
3617
context->select_lex &&
3618
cached_table->select_lex != context->select_lex)
3621
if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3625
goto mark_non_agg_field;
3629
if it is not expression from merged VIEW we will set this field.
3631
We can leave expression substituted from view for next PS/SP rexecution
3632
(i.e. do not register this substitution for reverting on cleanup()
3633
(register_item_tree_changing())), because this subtree will be
3634
fix_field'ed during setup_tables()->setup_underlying() (i.e. before
3635
all other expressions of query, and references on tables which do
3636
not present in query will not make problems.
3638
Also we suppose that view can't be changed during PS/SP life.
3640
if (from_field == view_ref_found)
3643
set_field(from_field);
3644
if (thd->lex->in_sum_func &&
3645
thd->lex->in_sum_func->nest_level ==
3646
thd->lex->current_select->nest_level)
3647
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3648
thd->lex->current_select->nest_level);
3650
else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3652
Table *table= field->table;
3653
MY_BITMAP *current_bitmap, *other_bitmap;
3654
if (thd->mark_used_columns == MARK_COLUMNS_READ)
3656
current_bitmap= table->read_set;
3657
other_bitmap= table->write_set;
3661
current_bitmap= table->write_set;
3662
other_bitmap= table->read_set;
3664
if (!bitmap_fast_test_and_set(current_bitmap, field->field_index))
3666
if (!bitmap_is_set(other_bitmap, field->field_index))
3668
/* First usage of column */
3669
table->used_fields++; // Used to optimize loops
3670
/* purecov: begin inspected */
3671
table->covering_keys.intersect(field->part_of_key);
3681
context->process_error(thd);
3685
Item *Item_field::safe_charset_converter(const CHARSET_INFO * const tocs)
3688
return Item::safe_charset_converter(tocs);
3692
void Item_field::cleanup()
3694
Item_ident::cleanup();
3696
Even if this object was created by direct link to field in setup_wild()
3697
it will be linked correctly next time by name of field and table alias.
3698
I.e. we can drop 'field'.
3700
field= result_field= 0;
3706
Find a field among specified multiple equalities.
3708
The function first searches the field among multiple equalities
3709
of the current level (in the cond_equal->current_level list).
3710
If it fails, it continues searching in upper levels accessed
3711
through a pointer cond_equal->upper_levels.
3712
The search terminates as soon as a multiple equality containing
3715
@param cond_equal reference to list of multiple equalities where
3716
the field (this object) is to be looked for
3719
- First Item_equal containing the field, if success
3723
Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal)
3725
Item_equal *item= 0;
3728
List_iterator_fast<Item_equal> li(cond_equal->current_level);
3729
while ((item= li++))
3731
if (item->contains(field))
3735
The field is not found in any of the multiple equalities
3736
of the current level. Look for it in upper levels
3738
cond_equal= cond_equal->upper_levels;
3745
Check whether a field can be substituted by an equal item.
3747
The function checks whether a substitution of the field
3748
occurrence for an equal item is valid.
3750
@param arg *arg != NULL <-> the field is in the context where
3751
substitution for an equal item is valid
3754
The following statement is not always true:
3758
This means substitution of an item for an equal item not always
3759
yields an equavalent condition. Here's an example:
3762
(LENGTH('a')=1) != (LENGTH('a ')=2)
3764
Such a substitution is surely valid if either the substituted
3765
field is not of a STRING type or if it is an argument of
3766
a comparison predicate.
3769
true substitution is valid
3774
bool Item_field::subst_argument_checker(unsigned char **arg)
3776
return (result_type() != STRING_RESULT) || (*arg);
3781
Set a pointer to the multiple equality the field reference belongs to
3784
The function looks for a multiple equality containing the field item
3785
among those referenced by arg.
3786
In the case such equality exists the function does the following.
3787
If the found multiple equality contains a constant, then the field
3788
reference is substituted for this constant, otherwise it sets a pointer
3789
to the multiple equality in the field item.
3792
@param arg reference to list of multiple equalities where
3793
the field (this object) is to be looked for
3796
This function is supposed to be called as a callback parameter in calls
3797
of the compile method.
3800
- pointer to the replacing constant item, if the field item was substituted
3801
- pointer to the field item, otherwise.
3804
Item *Item_field::equal_fields_propagator(unsigned char *arg)
3808
item_equal= find_item_equal((COND_EQUAL *) arg);
3811
item= item_equal->get_const();
3813
Disable const propagation for items used in different comparison contexts.
3814
This must be done because, for example, Item_hex_string->val_int() is not
3815
the same as (Item_hex_string->val_str() in BINARY column)->val_int().
3816
We cannot simply disable the replacement in a particular context (
3817
e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
3818
Items don't know the context they are in and there are functions like
3819
IF (<hex_string>, 'yes', 'no').
3820
The same problem occurs when comparing a DATE/TIME field with a
3821
DATE/TIME represented as an int and as a string.
3824
(cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
3832
Mark the item to not be part of substitution if it's not a binary item.
3834
See comments in Arg_comparator::set_compare_func() for details.
3837
bool Item_field::set_no_const_sub(unsigned char *arg __attribute__((unused)))
3839
if (field->charset() != &my_charset_bin)
3846
Replace an Item_field for an equal Item_field that evaluated earlier
3849
The function returns a pointer to an item that is taken from
3850
the very beginning of the item_equal list which the Item_field
3851
object refers to (belongs to) unless item_equal contains a constant
3852
item. In this case the function returns this constant item,
3853
(if the substitution does not require conversion).
3854
If the Item_field object does not refer any Item_equal object
3855
'this' is returned .
3857
@param arg a dummy parameter, is not used here
3861
This function is supposed to be called as a callback parameter in calls
3862
of the thransformer method.
3865
- pointer to a replacement Item_field if there is a better equal item or
3866
a pointer to a constant equal item;
3870
Item *Item_field::replace_equal_field(unsigned char *arg __attribute__((unused)))
3874
Item *const_item= item_equal->get_const();
3877
if (cmp_context != (Item_result)-1 &&
3878
const_item->cmp_context != cmp_context)
3882
Item_field *subst= item_equal->get_first();
3883
if (subst && !field->eq(subst->field))
3890
1289
void Item::init_make_field(Send_field *tmp_field,
3891
1290
enum enum_field_types field_type_arg)
4720
bool Item_field::send(Protocol *protocol,
4721
String *buffer __attribute__((unused)))
4723
return protocol->store(result_field);
4727
void Item_field::update_null_value()
4730
need to set no_errors to prevent warnings about type conversion
4733
THD *thd= field->table->in_use;
4736
no_errors= thd->no_errors;
4738
Item::update_null_value();
4739
thd->no_errors= no_errors;
4744
Add the field to the select list and substitute it for the reference to
4748
Item_field::update_value_transformer()
4749
select_arg current select
4752
If the field doesn't belong to the table being inserted into then it is
4753
added to the select list, pointer to it is stored in the ref_pointer_array
4754
of the select and the field itself is substituted for the Item_ref object.
4755
This is done in order to get correct values from update fields that
4756
belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
4761
ref if all conditions are met
4762
this field otherwise
4765
Item *Item_field::update_value_transformer(unsigned char *select_arg)
4767
SELECT_LEX *select= (SELECT_LEX*)select_arg;
4770
if (field->table != select->context.table_list->table)
4772
List<Item> *all_fields= &select->join->all_fields;
4773
Item **ref_pointer_array= select->ref_pointer_array;
4774
int el= all_fields->elements;
4777
ref_pointer_array[el]= (Item*)this;
4778
all_fields->push_front((Item*)this);
4779
ref= new Item_ref(&select->context, ref_pointer_array + el,
4780
table_name, field_name);
4787
void Item_field::print(String *str, enum_query_type query_type)
4789
if (field && field->table->const_table)
4791
char buff[MAX_FIELD_WIDTH];
4792
String tmp(buff,sizeof(buff),str->charset());
4793
field->val_str(&tmp);
4799
Item_ident::print(str, query_type);
4803
Item_ref::Item_ref(Name_resolution_context *context_arg,
4804
Item **item, const char *table_name_arg,
4805
const char *field_name_arg,
4806
bool alias_name_used_arg)
4807
:Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
4808
result_field(0), ref(item)
4810
alias_name_used= alias_name_used_arg;
4812
This constructor used to create some internals references over fixed items
4814
if (ref && *ref && (*ref)->fixed)
4820
Resolve the name of a reference to a column reference.
4822
The method resolves the column reference represented by 'this' as a column
4823
present in one of: GROUP BY clause, SELECT clause, outer queries. It is
4824
used typically for columns in the HAVING clause which are not under
4825
aggregate functions.
4828
Item_ref::ref is 0 or points to a valid item.
4831
The name resolution algorithm used is (where [T_j] is an optional table
4832
name that qualifies the column name):
4835
resolve_extended([T_j].col_ref_i)
4837
Search for a column or derived column named col_ref_i [in table T_j]
4838
in the SELECT and GROUP clauses of Q.
4840
if such a column is NOT found AND // Lookup in outer queries.
4841
there are outer queries
4843
for each outer query Q_k beginning from the inner-most one
4845
Search for a column or derived column named col_ref_i
4846
[in table T_j] in the SELECT and GROUP clauses of Q_k.
4848
if such a column is not found AND
4849
- Q_k is not a group query AND
4850
- Q_k is not inside an aggregate function
4852
- Q_(k-1) is not in a HAVING or SELECT clause of Q_k
4854
search for a column or derived column named col_ref_i
4855
[in table T_j] in the FROM clause of Q_k;
4862
This procedure treats GROUP BY and SELECT clauses as one namespace for
4863
column references in HAVING. Notice that compared to
4864
Item_field::fix_fields, here we first search the SELECT and GROUP BY
4865
clauses, and then we search the FROM clause.
4867
@param[in] thd current thread
4868
@param[in,out] reference view column if this item was resolved to a
4872
Here we could first find the field anyway, and then test this
4873
condition, so that we can give a better error message -
4874
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4875
ER_BAD_FIELD_ERROR which we produce now.
4883
bool Item_ref::fix_fields(THD *thd, Item **reference)
4885
enum_parsing_place place= NO_MATTER;
4887
SELECT_LEX *current_sel= thd->lex->current_select;
4889
if (!ref || ref == not_found_item)
4891
if (!(ref= resolve_ref_in_select_and_group(thd, this,
4892
context->select_lex)))
4893
goto error; /* Some error occurred (e.g. ambiguous names). */
4895
if (ref == not_found_item) /* This reference was not resolved. */
4897
Name_resolution_context *last_checked_context= context;
4898
Name_resolution_context *outer_context= context->outer_context;
4904
/* The current reference cannot be resolved in this query. */
4905
my_error(ER_BAD_FIELD_ERROR,MYF(0),
4906
this->full_name(), current_thd->where);
4911
If there is an outer context (select), and it is not a derived table
4912
(which do not support the use of outer fields for now), try to
4913
resolve this reference in the outer select(s).
4915
We treat each subselect as a separate namespace, so that different
4916
subselects may contain columns with the same names. The subselects are
4917
searched starting from the innermost.
4919
from_field= (Field*) not_found_field;
4923
SELECT_LEX *select= outer_context->select_lex;
4924
Item_subselect *prev_subselect_item=
4925
last_checked_context->select_lex->master_unit()->item;
4926
last_checked_context= outer_context;
4928
/* Search in the SELECT and GROUP lists of the outer select. */
4929
if (outer_context->resolve_in_select_list)
4931
if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
4932
goto error; /* Some error occurred (e.g. ambiguous names). */
4933
if (ref != not_found_item)
4935
assert(*ref && (*ref)->fixed);
4936
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
4937
prev_subselect_item->const_item_cache&= (*ref)->const_item();
4941
Set ref to 0 to ensure that we get an error in case we replaced
4942
this item with another item and still use this item in some
4943
other place of the parse tree.
4948
place= prev_subselect_item->parsing_place;
4950
Check table fields only if the subquery is used somewhere out of
4951
HAVING or the outer SELECT does not use grouping (i.e. tables are
4954
Here we could first find the field anyway, and then test this
4955
condition, so that we can give a better error message -
4956
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4957
ER_BAD_FIELD_ERROR which we produce now.
4959
if ((place != IN_HAVING ||
4960
(!select->with_sum_func &&
4961
select->group_list.elements == 0)))
4964
In case of view, find_field_in_tables() write pointer to view
4965
field expression to 'reference', i.e. it substitute that
4966
expression instead of this Item_ref
4968
from_field= find_field_in_tables(thd, this,
4970
first_name_resolution_table,
4972
last_name_resolution_table,
4974
IGNORE_EXCEPT_NON_UNIQUE,
4978
if (from_field == view_ref_found)
4980
Item::Type refer_type= (*reference)->type();
4981
prev_subselect_item->used_tables_cache|=
4982
(*reference)->used_tables();
4983
prev_subselect_item->const_item_cache&=
4984
(*reference)->const_item();
4985
assert((*reference)->type() == REF_ITEM);
4986
mark_as_dependent(thd, last_checked_context->select_lex,
4987
context->select_lex, this,
4988
((refer_type == REF_ITEM ||
4989
refer_type == FIELD_ITEM) ?
4990
(Item_ident*) (*reference) :
4993
view reference found, we substituted it instead of this
4998
if (from_field != not_found_field)
5000
if (cached_table && cached_table->select_lex &&
5001
outer_context->select_lex &&
5002
cached_table->select_lex != outer_context->select_lex)
5005
Due to cache, find_field_in_tables() can return field which
5006
doesn't belong to provided outer_context. In this case we have
5007
to find proper field context in order to fix field correcly.
5011
outer_context= outer_context->outer_context;
5012
select= outer_context->select_lex;
5013
prev_subselect_item=
5014
last_checked_context->select_lex->master_unit()->item;
5015
last_checked_context= outer_context;
5016
} while (outer_context && outer_context->select_lex &&
5017
cached_table->select_lex != outer_context->select_lex);
5019
prev_subselect_item->used_tables_cache|= from_field->table->map;
5020
prev_subselect_item->const_item_cache= 0;
5024
assert(from_field == not_found_field);
5026
/* Reference is not found => depend on outer (or just error). */
5027
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
5028
prev_subselect_item->const_item_cache= 0;
5030
outer_context= outer_context->outer_context;
5031
} while (outer_context);
5033
assert(from_field != 0 && from_field != view_ref_found);
5034
if (from_field != not_found_field)
5037
if (!(fld= new Item_field(from_field)))
5039
thd->change_item_tree(reference, fld);
5040
mark_as_dependent(thd, last_checked_context->select_lex,
5041
thd->lex->current_select, this, fld);
5043
A reference is resolved to a nest level that's outer or the same as
5044
the nest level of the enclosing set function : adjust the value of
5045
max_arg_level for the function if it's needed.
5047
if (thd->lex->in_sum_func &&
5048
thd->lex->in_sum_func->nest_level >=
5049
last_checked_context->select_lex->nest_level)
5050
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5051
last_checked_context->select_lex->nest_level);
5056
/* The item was not a table field and not a reference */
5057
my_error(ER_BAD_FIELD_ERROR, MYF(0),
5058
this->full_name(), current_thd->where);
5061
/* Should be checked in resolve_ref_in_select_and_group(). */
5062
assert(*ref && (*ref)->fixed);
5063
mark_as_dependent(thd, last_checked_context->select_lex,
5064
context->select_lex, this, this);
5066
A reference is resolved to a nest level that's outer or the same as
5067
the nest level of the enclosing set function : adjust the value of
5068
max_arg_level for the function if it's needed.
5070
if (thd->lex->in_sum_func &&
5071
thd->lex->in_sum_func->nest_level >=
5072
last_checked_context->select_lex->nest_level)
5073
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5074
last_checked_context->select_lex->nest_level);
5080
Check if this is an incorrect reference in a group function or forward
5081
reference. Do not issue an error if this is:
5082
1. outer reference (will be fixed later by the fix_inner_refs function);
5083
2. an unnamed reference inside an aggregate function.
5085
if (!((*ref)->type() == REF_ITEM &&
5086
((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
5087
(((*ref)->with_sum_func && name &&
5088
!(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
5089
current_sel->having_fix_field)) ||
5092
my_error(ER_ILLEGAL_REFERENCE, MYF(0),
5093
name, ((*ref)->with_sum_func?
5094
"reference to group function":
5095
"forward reference in item list"));
5101
if ((*ref)->check_cols(1))
5106
context->process_error(thd);
5111
void Item_ref::set_properties()
5113
max_length= (*ref)->max_length;
5114
maybe_null= (*ref)->maybe_null;
5115
decimals= (*ref)->decimals;
5116
collation.set((*ref)->collation);
5118
We have to remember if we refer to a sum function, to ensure that
5119
split_sum_func() doesn't try to change the reference.
5121
with_sum_func= (*ref)->with_sum_func;
5122
unsigned_flag= (*ref)->unsigned_flag;
5124
if (alias_name_used)
5126
if ((*ref)->type() == FIELD_ITEM)
5127
alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
5129
alias_name_used= true; // it is not field, so it is was resolved by alias
5133
void Item_ref::cleanup()
5135
Item_ident::cleanup();
5141
void Item_ref::print(String *str, enum_query_type query_type)
5145
if ((*ref)->type() != Item::CACHE_ITEM && ref_type() != VIEW_REF &&
5146
!table_name && name && alias_name_used)
5148
THD *thd= current_thd;
5149
append_identifier(thd, str, name, (uint) strlen(name));
5152
(*ref)->print(str, query_type);
5155
Item_ident::print(str, query_type);
5159
bool Item_ref::send(Protocol *prot, String *tmp)
5162
return prot->store(result_field);
5163
return (*ref)->send(prot, tmp);
5167
double Item_ref::val_result()
5171
if ((null_value= result_field->is_null()))
5173
return result_field->val_real();
5179
int64_t Item_ref::val_int_result()
5183
if ((null_value= result_field->is_null()))
5185
return result_field->val_int();
5191
String *Item_ref::str_result(String* str)
5195
if ((null_value= result_field->is_null()))
5197
str->set_charset(str_value.charset());
5198
return result_field->val_str(str, &str_value);
5200
return val_str(str);
5204
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
5208
if ((null_value= result_field->is_null()))
5210
return result_field->val_decimal(decimal_value);
5212
return val_decimal(decimal_value);
5216
bool Item_ref::val_bool_result()
5220
if ((null_value= result_field->is_null()))
5222
switch (result_field->result_type()) {
5224
return result_field->val_int() != 0;
5225
case DECIMAL_RESULT:
5227
my_decimal decimal_value;
5228
my_decimal *val= result_field->val_decimal(&decimal_value);
5230
return !my_decimal_is_zero(val);
5235
return result_field->val_real() != 0.0;
5245
double Item_ref::val_real()
5248
double tmp=(*ref)->val_result();
5249
null_value=(*ref)->null_value;
5254
int64_t Item_ref::val_int()
5257
int64_t tmp=(*ref)->val_int_result();
5258
null_value=(*ref)->null_value;
5263
bool Item_ref::val_bool()
5266
bool tmp= (*ref)->val_bool_result();
5267
null_value= (*ref)->null_value;
5272
String *Item_ref::val_str(String* tmp)
5275
tmp=(*ref)->str_result(tmp);
5276
null_value=(*ref)->null_value;
5281
bool Item_ref::is_null()
5284
return (*ref)->is_null();
5288
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
5290
return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5294
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
5296
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
5297
null_value= (*ref)->null_value;
5301
int Item_ref::save_in_field(Field *to, bool no_conversions)
5304
assert(!result_field);
5305
res= (*ref)->save_in_field(to, no_conversions);
5306
null_value= (*ref)->null_value;
5311
void Item_ref::save_org_in_field(Field *field)
5313
(*ref)->save_org_in_field(field);
5317
void Item_ref::make_field(Send_field *field)
5319
(*ref)->make_field(field);
5320
/* Non-zero in case of a view */
5322
field->col_name= name;
5324
field->table_name= table_name;
5326
field->db_name= db_name;
5330
Item *Item_ref::get_tmp_table_item(THD *thd)
5333
return (*ref)->get_tmp_table_item(thd);
5335
Item_field *item= new Item_field(result_field);
5338
item->table_name= table_name;
5339
item->db_name= db_name;
5345
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
5347
str->append(STRING_WITH_LEN("<ref_null_helper>("));
5349
(*ref)->print(str, query_type);
5356
double Item_direct_ref::val_real()
5358
double tmp=(*ref)->val_real();
5359
null_value=(*ref)->null_value;
5364
int64_t Item_direct_ref::val_int()
5366
int64_t tmp=(*ref)->val_int();
5367
null_value=(*ref)->null_value;
5372
String *Item_direct_ref::val_str(String* tmp)
5374
tmp=(*ref)->val_str(tmp);
5375
null_value=(*ref)->null_value;
5380
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
5382
my_decimal *tmp= (*ref)->val_decimal(decimal_value);
5383
null_value=(*ref)->null_value;
5388
bool Item_direct_ref::val_bool()
5390
bool tmp= (*ref)->val_bool();
5391
null_value=(*ref)->null_value;
5396
bool Item_direct_ref::is_null()
5398
return (*ref)->is_null();
5402
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
5404
return (null_value=(*ref)->get_date(ltime,fuzzydate));
5409
Prepare referenced field then call usual Item_direct_ref::fix_fields .
5411
@param thd thread handler
5412
@param reference reference on reference where this item stored
5420
bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference)
5422
/* view fild reference must be defined */
5424
/* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */
5425
if (!(*ref)->fixed &&
5426
((*ref)->fix_fields(thd, ref)))
5428
return Item_direct_ref::fix_fields(thd, reference);
5432
Prepare referenced outer field then call usual Item_direct_ref::fix_fields
5435
Item_outer_ref::fix_fields()
5437
reference reference on reference where this item stored
5444
bool Item_outer_ref::fix_fields(THD *thd, Item **reference)
5447
/* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5448
if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
5450
err= Item_direct_ref::fix_fields(thd, reference);
5453
if ((*ref)->type() == Item::FIELD_ITEM)
5454
table_name= ((Item_field*)outer_ref)->table_name;
5458
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
5460
if (depended_from == new_parent)
5463
outer_ref->fix_after_pullout(new_parent, ref);
5467
void Item_ref::fix_after_pullout(st_select_lex *new_parent,
5468
Item **refptr __attribute__((unused)))
5470
if (depended_from == new_parent)
5472
(*ref)->fix_after_pullout(new_parent, ref);
5473
depended_from= NULL;
5478
Compare two view column references for equality.
5480
A view column reference is considered equal to another column
5481
reference if the second one is a view column and if both column
5482
references resolve to the same item. It is assumed that both
5483
items are of the same type.
5485
@param item item to compare with
5486
@param binary_cmp make binary comparison
5489
true Referenced item is equal to given item
5494
bool Item_direct_view_ref::eq(const Item *item,
5495
bool binary_cmp __attribute__((unused))) const
5497
if (item->type() == REF_ITEM)
5499
Item_ref *item_ref= (Item_ref*) item;
5500
if (item_ref->ref_type() == VIEW_REF)
5502
Item *item_ref_ref= *(item_ref->ref);
5503
return ((*ref)->real_item() == item_ref_ref->real_item());
5509
1670
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
5511
1672
return item->type() == DEFAULT_VALUE_ITEM &&
5848
1932
return result == field->val_real();
5851
Item_cache* Item_cache::get_cache(const Item *item)
5853
switch (item->result_type()) {
5855
return new Item_cache_int();
5857
return new Item_cache_real();
5858
case DECIMAL_RESULT:
5859
return new Item_cache_decimal();
5861
return new Item_cache_str(item);
5863
return new Item_cache_row();
5865
// should never be in real life
5872
void Item_cache::print(String *str, enum_query_type query_type)
5874
str->append(STRING_WITH_LEN("<cache>("));
5876
example->print(str, query_type);
5878
Item::print(str, query_type);
5883
void Item_cache_int::store(Item *item)
5885
value= item->val_int_result();
5886
null_value= item->null_value;
5887
unsigned_flag= item->unsigned_flag;
5891
void Item_cache_int::store(Item *item, int64_t val_arg)
5894
null_value= item->null_value;
5895
unsigned_flag= item->unsigned_flag;
5899
String *Item_cache_int::val_str(String *str)
5902
str->set(value, default_charset());
5907
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
5910
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
5915
void Item_cache_real::store(Item *item)
5917
value= item->val_result();
5918
null_value= item->null_value;
5922
int64_t Item_cache_real::val_int()
5925
return (int64_t) rint(value);
5929
String* Item_cache_real::val_str(String *str)
5932
str->set_real(value, decimals, default_charset());
5937
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
5940
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
5945
void Item_cache_decimal::store(Item *item)
5947
my_decimal *val= item->val_decimal_result(&decimal_value);
5948
if (!(null_value= item->null_value) && val != &decimal_value)
5949
my_decimal2decimal(val, &decimal_value);
5952
double Item_cache_decimal::val_real()
5956
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
5960
int64_t Item_cache_decimal::val_int()
5964
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
5968
String* Item_cache_decimal::val_str(String *str)
5971
my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
5973
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
5977
my_decimal *Item_cache_decimal::val_decimal(my_decimal *val __attribute__((unused)))
5980
return &decimal_value;
5984
void Item_cache_str::store(Item *item)
5986
value_buff.set(buffer, sizeof(buffer), item->collation.collation);
5987
value= item->str_result(&value_buff);
5988
if ((null_value= item->null_value))
5990
else if (value != &value_buff)
5993
We copy string value to avoid changing value if 'item' is table field
5994
in queries like following (where t1.c is varchar):
5996
(select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
5997
(select c from t1 where a=t2.a)
6000
value_buff.copy(*value);
6005
double Item_cache_str::val_real()
6011
return my_strntod(value->charset(), (char*) value->ptr(),
6012
value->length(), &end_not_used, &err_not_used);
6017
int64_t Item_cache_str::val_int()
6022
return my_strntoll(value->charset(), value->ptr(),
6023
value->length(), 10, (char**) 0, &err);
6028
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
6032
string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
6039
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
6041
int res= Item_cache::save_in_field(field, no_conversions);
6047
bool Item_cache_row::allocate(uint32_t num)
6050
THD *thd= current_thd;
6052
(Item_cache **) thd->calloc(sizeof(Item_cache *)*item_count)));
6056
bool Item_cache_row::setup(Item * item)
6059
if (!values && allocate(item->cols()))
6061
for (uint32_t i= 0; i < item_count; i++)
6063
Item *el= item->element_index(i);
6065
if (!(tmp= values[i]= Item_cache::get_cache(el)))
6073
void Item_cache_row::store(Item * item)
6076
item->bring_value();
6077
for (uint32_t i= 0; i < item_count; i++)
6079
values[i]->store(item->element_index(i));
6080
null_value|= values[i]->null_value;
6085
void Item_cache_row::illegal_method_call(const char *method __attribute__((unused)))
6088
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
6093
bool Item_cache_row::check_cols(uint32_t c)
6095
if (c != item_count)
6097
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
6104
bool Item_cache_row::null_inside()
6106
for (uint32_t i= 0; i < item_count; i++)
6108
if (values[i]->cols() > 1)
6110
if (values[i]->null_inside())
6115
values[i]->update_null_value();
6116
if (values[i]->null_value)
6124
void Item_cache_row::bring_value()
6126
for (uint32_t i= 0; i < item_count; i++)
6127
values[i]->bring_value();
6132
Item_type_holder::Item_type_holder(THD *thd, Item *item)
6133
:Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
6135
assert(item->fixed);
6136
maybe_null= item->maybe_null;
6137
collation.set(item->collation);
6138
get_full_info(item);
6139
/* fix variable decimals which always is NOT_FIXED_DEC */
6140
if (Field::result_merge_type(fld_type) == INT_RESULT)
6142
prev_decimal_int_part= item->decimal_int_part();
6147
Return expression type of Item_type_holder.
6150
Item_result (type of internal MySQL expression result)
6153
Item_result Item_type_holder::result_type() const
6155
return Field::result_merge_type(fld_type);
6160
Find real field type of item.
6163
type of field which should be created to store item value
6166
enum_field_types Item_type_holder::get_real_type(Item *item)
6168
switch(item->type())
6173
Item_fields::field_type ask Field_type() but sometimes field return
6174
a different type, like for enum/set, so we need to ask real type.
6176
Field *field= ((Item_field *) item)->field;
6177
enum_field_types type= field->real_type();
6178
if (field->is_created_from_null_item)
6179
return DRIZZLE_TYPE_NULL;
6185
Argument of aggregate function sometimes should be asked about field
6188
Item_sum *item_sum= (Item_sum *) item;
6189
if (item_sum->keep_field_type())
6190
return get_real_type(item_sum->args[0]);
6194
if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC)
6197
There are work around of problem with changing variable type on the
6198
fly and variable always report "string" as field type to get
6199
acceptable information for client in send_field, so we make field
6200
type from expression type.
6202
switch (item->result_type()) {
6204
return DRIZZLE_TYPE_VARCHAR;
6206
return DRIZZLE_TYPE_LONGLONG;
6208
return DRIZZLE_TYPE_DOUBLE;
6209
case DECIMAL_RESULT:
6210
return DRIZZLE_TYPE_NEWDECIMAL;
6214
return DRIZZLE_TYPE_VARCHAR;
6221
return item->field_type();
6225
Find field type which can carry current Item_type_holder type and
6228
@param thd thread handler
6229
@param item given item to join its parameters with this item ones
6232
true error - types are incompatible
6237
bool Item_type_holder::join_types(THD *thd __attribute__((unused)),
6240
uint32_t max_length_orig= max_length;
6241
uint32_t decimals_orig= decimals;
6242
fld_type= Field::field_type_merge(fld_type, get_real_type(item));
6244
int item_decimals= item->decimals;
6245
/* fix variable decimals which always is NOT_FIXED_DEC */
6246
if (Field::result_merge_type(fld_type) == INT_RESULT)
6248
decimals= cmax((int)decimals, item_decimals);
6250
if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
6252
decimals= cmin((int)cmax(decimals, item->decimals), DECIMAL_MAX_SCALE);
6253
int precision= cmin(cmax(prev_decimal_int_part, item->decimal_int_part())
6254
+ decimals, DECIMAL_MAX_PRECISION);
6255
unsigned_flag&= item->unsigned_flag;
6256
max_length= my_decimal_precision_to_length(precision, decimals,
6260
switch (Field::result_merge_type(fld_type))
6264
const char *old_cs, *old_derivation;
6265
uint32_t old_max_chars= max_length / collation.collation->mbmaxlen;
6266
old_cs= collation.collation->name;
6267
old_derivation= collation.derivation_name();
6268
if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
6270
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
6271
old_cs, old_derivation,
6272
item->collation.collation->name,
6273
item->collation.derivation_name(),
6278
To figure out max_length, we have to take into account possible
6279
expansion of the size of the values because of character set
6282
if (collation.collation != &my_charset_bin)
6284
max_length= cmax(old_max_chars * collation.collation->mbmaxlen,
6285
display_length(item) /
6286
item->collation.collation->mbmaxlen *
6287
collation.collation->mbmaxlen);
6290
set_if_bigger(max_length, display_length(item));
6295
if (decimals != NOT_FIXED_DEC)
6297
int delta1= max_length_orig - decimals_orig;
6298
int delta2= item->max_length - item->decimals;
6299
max_length= cmax(delta1, delta2) + decimals;
6300
if (fld_type == DRIZZLE_TYPE_DOUBLE && max_length > DBL_DIG + 2)
6302
max_length= DBL_DIG + 7;
6303
decimals= NOT_FIXED_DEC;
6307
max_length= DBL_DIG+7;
6311
max_length= cmax(max_length, display_length(item));
6313
maybe_null|= item->maybe_null;
6314
get_full_info(item);
6316
/* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
6317
prev_decimal_int_part= decimal_int_part();
6322
Calculate lenth for merging result for given Item type.
6324
@param item Item for length detection
6330
uint32_t Item_type_holder::display_length(Item *item)
6332
if (item->type() == Item::FIELD_ITEM)
6333
return ((Item_field *)item)->max_disp_length();
6335
switch (item->field_type())
6337
case DRIZZLE_TYPE_TIMESTAMP:
6338
case DRIZZLE_TYPE_TIME:
6339
case DRIZZLE_TYPE_DATETIME:
6340
case DRIZZLE_TYPE_NEWDATE:
6341
case DRIZZLE_TYPE_VARCHAR:
6342
case DRIZZLE_TYPE_NEWDECIMAL:
6343
case DRIZZLE_TYPE_ENUM:
6344
case DRIZZLE_TYPE_BLOB:
6345
case DRIZZLE_TYPE_TINY:
6347
case DRIZZLE_TYPE_LONG:
6348
return MY_INT32_NUM_DECIMAL_DIGITS;
6349
case DRIZZLE_TYPE_DOUBLE:
6351
case DRIZZLE_TYPE_NULL:
6353
case DRIZZLE_TYPE_LONGLONG:
6356
assert(0); // we should never go there
6363
Make temporary table field according collected information about type
6366
@param table temporary table for which we create fields
6372
Field *Item_type_holder::make_field_by_type(Table *table)
6375
The field functions defines a field to be not null if null_ptr is not 0
6377
unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
6381
case DRIZZLE_TYPE_ENUM:
6382
assert(enum_set_typelib);
6383
field= new Field_enum((unsigned char *) 0, max_length, null_ptr, 0,
6385
get_enum_pack_length(enum_set_typelib->count),
6386
enum_set_typelib, collation.collation);
6390
case DRIZZLE_TYPE_NULL:
6391
return make_string_field(table);
6395
return tmp_table_field_from_field_type(table, 0);
6400
Get full information from Item about enum/set fields to be able to create
6403
@param item Item for information collection
6405
void Item_type_holder::get_full_info(Item *item)
6407
if (fld_type == DRIZZLE_TYPE_ENUM)
6409
if (item->type() == Item::SUM_FUNC_ITEM &&
6410
(((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
6411
((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC))
6412
item = ((Item_sum*)item)->args[0];
6414
We can have enum/set type after merging only if we have one enum|set
6415
field (or MIN|MAX(enum|set field)) and number of NULL fields
6417
assert((enum_set_typelib &&
6418
get_real_type(item) == DRIZZLE_TYPE_NULL) ||
6419
(!enum_set_typelib &&
6420
item->type() == Item::FIELD_ITEM &&
6421
(get_real_type(item) == DRIZZLE_TYPE_ENUM) &&
6422
((Field_enum*)((Item_field *) item)->field)->typelib));
6423
if (!enum_set_typelib)
6425
enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib;
6431
double Item_type_holder::val_real()
6433
assert(0); // should never be called
6438
int64_t Item_type_holder::val_int()
6440
assert(0); // should never be called
6444
my_decimal *Item_type_holder::val_decimal(my_decimal *)
6446
assert(0); // should never be called
6450
String *Item_type_holder::val_str(String*)
6452
assert(0); // should never be called
6456
void Item_result_field::cleanup()
6464
1936
Dummy error processor used by default by Name_resolution_context.