933
1046
Item_ref to allow fields from view being stored in tmp table.
935
1048
Item_aggregate_ref *item_ref;
936
uint32_t el= fields.elements;
1049
uint el= fields.elements;
937
1050
Item *real_itm= real_item();
939
1052
ref_pointer_array[el]= real_itm;
940
if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
1053
if (!(item_ref= new Item_aggregate_ref(&thd->lex->current_select->context,
941
1054
ref_pointer_array + el, 0, name)))
942
1055
return; // fatal_error is set
943
1056
if (type() == SUM_FUNC_ITEM)
944
item_ref->depended_from= ((Item_sum *) this)->depended_from();
1057
item_ref->depended_from= ((Item_sum *) this)->depended_from();
945
1058
fields.push_front(real_itm);
946
session->change_item_tree(ref, item_ref);
1059
thd->change_item_tree(ref, item_ref);
1065
left_is_superset(DTCollation *left, DTCollation *right)
1067
/* Allow convert to Unicode */
1068
if (left->collation->state & MY_CS_UNICODE &&
1069
(left->derivation < right->derivation ||
1070
(left->derivation == right->derivation &&
1071
!(right->collation->state & MY_CS_UNICODE))))
1073
/* Allow convert from ASCII */
1074
if (right->repertoire == MY_REPERTOIRE_ASCII &&
1075
(left->derivation < right->derivation ||
1076
(left->derivation == right->derivation &&
1077
!(left->repertoire == MY_REPERTOIRE_ASCII))))
1079
/* Disallow conversion otherwise */
1084
Aggregate two collations together taking
1085
into account their coercibility (aka derivation):.
1087
0 == DERIVATION_EXPLICIT - an explicitly written COLLATE clause @n
1088
1 == DERIVATION_NONE - a mix of two different collations @n
1089
2 == DERIVATION_IMPLICIT - a column @n
1090
3 == DERIVATION_COERCIBLE - a string constant.
1092
The most important rules are:
1093
-# If collations are the same:
1094
chose this collation, and the strongest derivation.
1095
-# If collations are different:
1096
- Character sets may differ, but only if conversion without
1097
data loss is possible. The caller provides flags whether
1098
character set conversion attempts should be done. If no
1099
flags are substituted, then the character sets must be the same.
1100
Currently processed flags are:
1101
MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
1102
MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
1103
- two EXPLICIT collations produce an error, e.g. this is wrong:
1104
CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
1105
- the side with smaller derivation value wins,
1106
i.e. a column is stronger than a string constant,
1107
an explicit COLLATE clause is stronger than a column.
1108
- if derivations are the same, we have DERIVATION_NONE,
1109
we'll wait for an explicit COLLATE clause which possibly can
1110
come from another argument later: for example, this is valid,
1111
but we don't know yet when collecting the first two arguments:
1113
CONCAT(latin1_swedish_ci_column,
1114
latin1_german1_ci_column,
1115
expr COLLATE latin1_german2_ci)
1119
bool DTCollation::aggregate(DTCollation &dt, uint flags)
1121
if (!my_charset_same(collation, dt.collation))
1124
We do allow to use binary strings (like BLOBS)
1125
together with character strings.
1126
Binaries have more precedence than a character
1127
string of the same derivation.
1129
if (collation == &my_charset_bin)
1131
if (derivation <= dt.derivation)
1138
else if (dt.collation == &my_charset_bin)
1140
if (dt.derivation <= derivation)
1149
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1150
left_is_superset(this, &dt))
1154
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1155
left_is_superset(&dt, this))
1159
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1160
derivation < dt.derivation &&
1161
dt.derivation >= DERIVATION_SYSCONST)
1165
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1166
dt.derivation < derivation &&
1167
derivation >= DERIVATION_SYSCONST)
1173
// Cannot apply conversion
1174
set(0, DERIVATION_NONE, 0);
1178
else if (derivation < dt.derivation)
1182
else if (dt.derivation < derivation)
1188
if (collation == dt.collation)
1194
if (derivation == DERIVATION_EXPLICIT)
1196
set(0, DERIVATION_NONE, 0);
1199
if (collation->state & MY_CS_BINSORT)
1201
if (dt.collation->state & MY_CS_BINSORT)
1206
CHARSET_INFO *bin= get_charset_by_csname(collation->csname,
1207
MY_CS_BINSORT,MYF(0));
1208
set(bin, DERIVATION_NONE);
1211
repertoire|= dt.repertoire;
1215
/******************************/
1217
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname)
1219
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
1220
c1.collation->name,c1.derivation_name(),
1221
c2.collation->name,c2.derivation_name(),
1227
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
1230
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
1231
c1.collation->name,c1.derivation_name(),
1232
c2.collation->name,c2.derivation_name(),
1233
c3.collation->name,c3.derivation_name(),
1239
void my_coll_agg_error(Item** args, uint count, const char *fname,
1243
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
1244
else if (count == 3)
1245
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
1246
args[2*item_sep]->collation, fname);
1248
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
1252
bool agg_item_collations(DTCollation &c, const char *fname,
1253
Item **av, uint count, uint flags, int item_sep)
1257
c.set(av[0]->collation);
1258
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1260
if (c.aggregate((*arg)->collation, flags))
1262
my_coll_agg_error(av, count, fname, item_sep);
1266
if ((flags & MY_COLL_DISALLOW_NONE) &&
1267
c.derivation == DERIVATION_NONE)
1269
my_coll_agg_error(av, count, fname, item_sep);
1276
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1277
Item **av, uint count, uint flags)
1279
return (agg_item_collations(c, fname, av, count,
1280
flags | MY_COLL_DISALLOW_NONE, 1));
1285
Collect arguments' character sets together.
1287
We allow to apply automatic character set conversion in some cases.
1288
The conditions when conversion is possible are:
1289
- arguments A and B have different charsets
1290
- A wins according to coercibility rules
1291
(i.e. a column is stronger than a string constant,
1292
an explicit COLLATE clause is stronger than a column)
1293
- character set of A is either superset for character set of B,
1294
or B is a string constant which can be converted into the
1295
character set of A without data loss.
1297
If all of the above is true, then it's possible to convert
1298
B into the character set of A, and then compare according
1299
to the collation of A.
1301
For functions with more than two arguments:
1303
collect(A,B,C) ::= collect(collect(A,B),C)
1305
Since this function calls THD::change_item_tree() on the passed Item **
1306
pointers, it is necessary to pass the original Item **'s, not copies.
1307
Otherwise their values will not be properly restored (see BUG#20769).
1308
If the items are not consecutive (eg. args[2] and args[5]), use the
1309
item_sep argument, ie.
1311
agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
1315
bool agg_item_charsets(DTCollation &coll, const char *fname,
1316
Item **args, uint nargs, uint flags, int item_sep)
1318
Item **arg, *safe_args[2];
1320
memset(safe_args, 0, sizeof(safe_args));
1322
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
1326
For better error reporting: save the first and the second argument.
1327
We need this only if the the number of args is 3 or 2:
1328
- for a longer argument list, "Illegal mix of collations"
1329
doesn't display each argument's characteristics.
1330
- if nargs is 1, then this error cannot happen.
1332
if (nargs >=2 && nargs <= 3)
1334
safe_args[0]= args[0];
1335
safe_args[1]= args[item_sep];
1338
THD *thd= current_thd;
1339
Query_arena *arena, backup;
1343
In case we're in statement prepare, create conversion item
1344
in its memory: it will be reused on each execute.
1348
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1351
uint32 dummy_offset;
1352
if (!String::needs_conversion(0, (*arg)->collation.collation,
1357
if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
1358
((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
1359
conv= new Item_func_conv_charset(*arg, coll.collation, 1);
1363
if (nargs >=2 && nargs <= 3)
1365
/* restore the original arguments for better error message */
1366
args[0]= safe_args[0];
1367
args[item_sep]= safe_args[1];
1369
my_coll_agg_error(args, nargs, fname, item_sep);
1371
break; // we cannot return here, we need to restore "arena".
1373
if ((*arg)->type() == Item::FIELD_ITEM)
1374
((Item_field *)(*arg))->no_const_subst= 1;
1376
If in statement prepare, then we create a converter for two
1377
constant items, do it once and then reuse it.
1378
If we're in execution of a prepared statement, arena is NULL,
1379
and the conv was created in runtime memory. This can be
1380
the case only if the argument is a parameter marker ('?'),
1381
because for all true constants the charset converter has already
1382
been created in prepare. In this case register the change for
1385
thd->change_item_tree(arg, conv);
1387
We do not check conv->fixed, because Item_func_conv_charset which can
1388
be return by safe_charset_converter can't be fixed at creation
1390
conv->fix_fields(thd, arg);
1393
thd->restore_active_arena(arena, &backup);
1398
void Item_ident_for_show::make_field(Send_field *tmp_field)
1400
tmp_field->table_name= tmp_field->org_table_name= table_name;
1401
tmp_field->db_name= db_name;
1402
tmp_field->col_name= tmp_field->org_col_name= field->field_name;
1403
tmp_field->charsetnr= field->charset()->number;
1404
tmp_field->length=field->field_length;
1405
tmp_field->type=field->type();
1406
tmp_field->flags= field->table->maybe_null ?
1407
(field->flags & ~NOT_NULL_FLAG) : field->flags;
1408
tmp_field->decimals= field->decimals();
1411
/**********************************************/
1413
Item_field::Item_field(Field *f)
1414
:Item_ident(0, NullS, *f->table_name, f->field_name),
1415
item_equal(0), no_const_subst(0),
1416
have_privileges(0), any_privileges(0)
1420
field_name and table_name should not point to garbage
1421
if this item is to be reused
1423
orig_table_name= orig_field_name= "";
1428
Constructor used inside setup_wild().
1430
Ensures that field, table, and database names will live as long as
1431
Item_field (this is important in prepared statements).
1434
Item_field::Item_field(THD *thd, Name_resolution_context *context_arg,
1436
:Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1437
item_equal(0), no_const_subst(0),
1438
have_privileges(0), any_privileges(0)
1444
Item_field::Item_field(Name_resolution_context *context_arg,
1445
const char *db_arg,const char *table_name_arg,
1446
const char *field_name_arg)
1447
:Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
1448
field(0), result_field(0), item_equal(0), no_const_subst(0),
1449
have_privileges(0), any_privileges(0)
1451
SELECT_LEX *select= current_thd->lex->current_select;
1452
collation.set(DERIVATION_IMPLICIT);
1453
if (select && select->parsing_place != IN_HAVING)
1454
select->select_n_where_fields++;
1458
Constructor need to process subselect with temporary tables (see Item)
1461
Item_field::Item_field(THD *thd, Item_field *item)
1462
:Item_ident(thd, item),
1464
result_field(item->result_field),
1465
item_equal(item->item_equal),
1466
no_const_subst(item->no_const_subst),
1467
have_privileges(item->have_privileges),
1468
any_privileges(item->any_privileges)
1470
collation.set(DERIVATION_IMPLICIT);
1473
void Item_field::set_field(Field *field_par)
1475
field=result_field=field_par; // for easy coding with fields
1476
maybe_null=field->maybe_null();
1477
decimals= field->decimals();
1478
max_length= field_par->max_display_length();
1479
table_name= *field_par->table_name;
1480
field_name= field_par->field_name;
1481
db_name= field_par->table->s->db.str;
1482
alias_name_used= field_par->table->alias_name_used;
1483
unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
1484
collation.set(field_par->charset(), field_par->derivation());
1486
if (field->table->s->tmp_table == SYSTEM_TMP_TABLE)
1492
Reset this item to point to a field from the new temporary table.
1493
This is used when we create a new temporary table for each execution
1494
of prepared statement.
1497
void Item_field::reset_field(Field *f)
1500
/* 'name' is pointing at field->field_name of old field */
1501
name= (char*) f->field_name;
1504
const char *Item_ident::full_name() const
1507
if (!table_name || !field_name)
1508
return field_name ? field_name : name ? name : "tmp_field";
1509
if (db_name && db_name[0])
1511
tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1512
(uint) strlen(field_name)+3);
1513
strxmov(tmp,db_name,".",table_name,".",field_name,NullS);
1519
tmp= (char*) sql_alloc((uint) strlen(table_name) +
1520
(uint) strlen(field_name) + 2);
1521
strxmov(tmp, table_name, ".", field_name, NullS);
1524
tmp= (char*) field_name;
1529
void Item_ident::print(String *str, enum_query_type query_type)
1531
THD *thd= current_thd;
1532
char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1533
const char *d_name= db_name, *t_name= table_name;
1534
if (lower_case_table_names== 1 ||
1535
(lower_case_table_names == 2 && !alias_name_used))
1537
if (table_name && table_name[0])
1539
strmov(t_name_buff, table_name);
1540
my_casedn_str(files_charset_info, t_name_buff);
1541
t_name= t_name_buff;
1543
if (db_name && db_name[0])
1545
strmov(d_name_buff, db_name);
1546
my_casedn_str(files_charset_info, d_name_buff);
1547
d_name= d_name_buff;
1551
if (!table_name || !field_name || !field_name[0])
1553
const char *nm= (field_name && field_name[0]) ?
1554
field_name : name ? name : "tmp_field";
1555
append_identifier(thd, str, nm, (uint) strlen(nm));
1558
if (db_name && db_name[0] && !alias_name_used)
1561
append_identifier(thd, str, d_name, (uint)strlen(d_name));
1564
append_identifier(thd, str, t_name, (uint)strlen(t_name));
1566
append_identifier(thd, str, field_name, (uint)strlen(field_name));
1572
append_identifier(thd, str, t_name, (uint) strlen(t_name));
1574
append_identifier(thd, str, field_name, (uint) strlen(field_name));
1577
append_identifier(thd, str, field_name, (uint) strlen(field_name));
1582
String *Item_field::val_str(String *str)
1584
DBUG_ASSERT(fixed == 1);
1585
if ((null_value=field->is_null()))
1587
str->set_charset(str_value.charset());
1588
return field->val_str(str,&str_value);
1592
double Item_field::val_real()
1594
DBUG_ASSERT(fixed == 1);
1595
if ((null_value=field->is_null()))
1597
return field->val_real();
1601
longlong Item_field::val_int()
1603
DBUG_ASSERT(fixed == 1);
1604
if ((null_value=field->is_null()))
1606
return field->val_int();
1610
my_decimal *Item_field::val_decimal(my_decimal *decimal_value)
1612
if ((null_value= field->is_null()))
1614
return field->val_decimal(decimal_value);
1618
String *Item_field::str_result(String *str)
1620
if ((null_value=result_field->is_null()))
1622
str->set_charset(str_value.charset());
1623
return result_field->val_str(str,&str_value);
1626
bool Item_field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1628
if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1630
bzero((char*) ltime,sizeof(*ltime));
1636
bool Item_field::get_date_result(MYSQL_TIME *ltime,uint fuzzydate)
1638
if ((null_value=result_field->is_null()) ||
1639
result_field->get_date(ltime,fuzzydate))
1641
bzero((char*) ltime,sizeof(*ltime));
1647
bool Item_field::get_time(MYSQL_TIME *ltime)
1649
if ((null_value=field->is_null()) || field->get_time(ltime))
1651
bzero((char*) ltime,sizeof(*ltime));
1657
double Item_field::val_result()
1659
if ((null_value=result_field->is_null()))
1661
return result_field->val_real();
1664
longlong Item_field::val_int_result()
1666
if ((null_value=result_field->is_null()))
1668
return result_field->val_int();
1672
my_decimal *Item_field::val_decimal_result(my_decimal *decimal_value)
1674
if ((null_value= result_field->is_null()))
1676
return result_field->val_decimal(decimal_value);
1680
bool Item_field::val_bool_result()
1682
if ((null_value= result_field->is_null()))
1684
switch (result_field->result_type()) {
1686
return result_field->val_int() != 0;
1687
case DECIMAL_RESULT:
1689
my_decimal decimal_value;
1690
my_decimal *val= result_field->val_decimal(&decimal_value);
1692
return !my_decimal_is_zero(val);
1697
return result_field->val_real() != 0.0;
1701
return 0; // Shut up compiler
1706
bool Item_field::eq(const Item *item, bool binary_cmp) const
1708
Item *real_item= ((Item *) item)->real_item();
1709
if (real_item->type() != FIELD_ITEM)
1712
Item_field *item_field= (Item_field*) real_item;
1713
if (item_field->field && field)
1714
return item_field->field == field;
1716
We may come here when we are trying to find a function in a GROUP BY
1717
clause from the select list.
1718
In this case the '100 % correct' way to do this would be to first
1719
run fix_fields() on the GROUP BY item and then retry this function, but
1720
I think it's better to relax the checking a bit as we will in
1721
most cases do the correct thing by just checking the field name.
1722
(In cases where we would choose wrong we would have to generate a
1725
return (!my_strcasecmp(system_charset_info, item_field->name,
1727
(!item_field->table_name || !table_name ||
1728
(!my_strcasecmp(table_alias_charset, item_field->table_name,
1730
(!item_field->db_name || !db_name ||
1731
(item_field->db_name && !strcmp(item_field->db_name,
1736
table_map Item_field::used_tables() const
1738
if (field->table->const_table)
1739
return 0; // const item
1740
return (depended_from ? OUTER_REF_TABLE_BIT : field->table->map);
1744
void Item_field::fix_after_pullout(st_select_lex *new_parent, Item **ref)
1746
if (new_parent == depended_from)
1747
depended_from= NULL;
1748
Name_resolution_context *ctx= new Name_resolution_context();
1749
ctx->outer_context= NULL; // We don't build a complete name resolver
1750
ctx->select_lex= new_parent;
1751
ctx->first_name_resolution_table= context->first_name_resolution_table;
1752
ctx->last_name_resolution_table= context->last_name_resolution_table;
1757
Item *Item_field::get_tmp_table_item(THD *thd)
1759
Item_field *new_item= new Item_field(thd, this);
1761
new_item->field= new_item->result_field;
1765
longlong Item_field::val_int_endpoint(bool left_endp, bool *incl_endp)
1767
longlong res= val_int();
1768
return null_value? LONGLONG_MIN : res;
1772
Create an item from a string we KNOW points to a valid longlong
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
1797
DBUG_ASSERT(fixed == 1);
1798
str->set(value, &my_charset_bin);
1802
void Item_int::print(String *str, enum_query_type query_type)
1804
// my_charset_bin is good enough for numbers
1805
str_value.set(value, &my_charset_bin);
1806
str->append(str_value);
1810
Item_uint::Item_uint(const char *str_arg, uint length):
1811
Item_int(str_arg, length)
1817
Item_uint::Item_uint(const char *str_arg, longlong i, uint length):
1818
Item_int(str_arg, i, length)
1824
String *Item_uint::val_str(String *str)
1826
// following assert is redundant, because fixed=1 assigned in constructor
1827
DBUG_ASSERT(fixed == 1);
1828
str->set((ulonglong) value, &my_charset_bin);
1833
void Item_uint::print(String *str, enum_query_type query_type)
1835
// latin1 is good enough for numbers
1836
str_value.set((ulonglong) value, default_charset());
1837
str->append(str_value);
1841
Item_decimal::Item_decimal(const char *str_arg, uint length,
1842
CHARSET_INFO *charset)
1844
str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1845
name= (char*) str_arg;
1846
decimals= (uint8) decimal_value.frac;
1848
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1849
decimals, unsigned_flag);
1852
Item_decimal::Item_decimal(longlong val, bool unsig)
1854
int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1855
decimals= (uint8) decimal_value.frac;
1857
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1858
decimals, unsigned_flag);
1862
Item_decimal::Item_decimal(double val, int precision, int scale)
1864
double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1865
decimals= (uint8) decimal_value.frac;
1867
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1868
decimals, unsigned_flag);
1872
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1873
uint decimal_par, uint length)
1875
my_decimal2decimal(val_arg, &decimal_value);
1877
decimals= (uint8) decimal_par;
1883
Item_decimal::Item_decimal(my_decimal *value_par)
1885
my_decimal2decimal(value_par, &decimal_value);
1886
decimals= (uint8) decimal_value.frac;
1888
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1889
decimals, unsigned_flag);
1893
Item_decimal::Item_decimal(const uchar *bin, int precision, int scale)
1895
binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1896
&decimal_value, precision, scale);
1897
decimals= (uint8) decimal_value.frac;
1899
max_length= my_decimal_precision_to_length(precision, decimals,
1904
longlong Item_decimal::val_int()
1907
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1911
double Item_decimal::val_real()
1914
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1918
String *Item_decimal::val_str(String *result)
1920
result->set_charset(&my_charset_bin);
1921
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
1925
void Item_decimal::print(String *str, enum_query_type query_type)
1927
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1928
str->append(str_value);
1932
bool Item_decimal::eq(const Item *item, bool binary_cmp) const
1934
if (type() == item->type() && item->basic_const_item())
1937
We need to cast off const to call val_decimal(). This should
1938
be OK for a basic constant. Additionally, we can pass 0 as
1939
a true decimal constant will return its internal decimal
1940
storage and ignore the argument.
1942
Item *arg= (Item*) item;
1943
my_decimal *value= arg->val_decimal(0);
1944
return !my_decimal_cmp(&decimal_value, value);
1950
void Item_decimal::set_decimal_value(my_decimal *value_par)
1952
my_decimal2decimal(value_par, &decimal_value);
1953
decimals= (uint8) decimal_value.frac;
1954
unsigned_flag= !decimal_value.sign();
1955
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1956
decimals, unsigned_flag);
1960
String *Item_float::val_str(String *str)
1962
// following assert is redundant, because fixed=1 assigned in constructor
1963
DBUG_ASSERT(fixed == 1);
1964
str->set_real(value,decimals,&my_charset_bin);
1969
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1971
// following assert is redundant, because fixed=1 assigned in constructor
1972
DBUG_ASSERT(fixed == 1);
1973
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1974
return (decimal_value);
1978
void Item_string::print(String *str, enum_query_type query_type)
1980
if (query_type == QT_ORDINARY && is_cs_specified())
1983
str->append(collation.collation->csname);
1988
if (query_type == QT_ORDINARY ||
1989
my_charset_same(str_value.charset(), system_charset_info))
1991
str_value.print(str);
1995
THD *thd= current_thd;
1996
LEX_STRING utf8_lex_str;
1998
thd->convert_string(&utf8_lex_str,
1999
system_charset_info,
2000
str_value.c_ptr_safe(),
2002
str_value.charset());
2004
String utf8_str(utf8_lex_str.str,
2005
utf8_lex_str.length,
2006
system_charset_info);
2008
utf8_str.print(str);
2015
double Item_string::val_real()
2017
DBUG_ASSERT(fixed == 1);
2019
char *end, *org_end;
2021
CHARSET_INFO *cs= str_value.charset();
2023
org_end= (char*) str_value.ptr() + str_value.length();
2024
tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
2026
if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2029
We can use str_value.ptr() here as Item_string is gurantee to put an
2032
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2033
ER_TRUNCATED_WRONG_VALUE,
2034
ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
2043
Give error if we wanted a signed integer and we got an unsigned one
2045
longlong Item_string::val_int()
2047
DBUG_ASSERT(fixed == 1);
2050
char *end= (char*) str_value.ptr()+ str_value.length();
2052
CHARSET_INFO *cs= str_value.charset();
2054
tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
2056
TODO: Give error if we wanted a signed integer and we got an unsigned
2060
(end != org_end && !check_if_only_end_space(cs, end, org_end)))
2062
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2063
ER_TRUNCATED_WRONG_VALUE,
2064
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2071
my_decimal *Item_string::val_decimal(my_decimal *decimal_value)
2073
return val_decimal_from_string(decimal_value);
2077
bool Item_null::eq(const Item *item, bool binary_cmp) const
2078
{ return item->type() == type(); }
2081
double Item_null::val_real()
2083
// following assert is redundant, because fixed=1 assigned in constructor
2084
DBUG_ASSERT(fixed == 1);
2088
longlong Item_null::val_int()
2090
// following assert is redundant, because fixed=1 assigned in constructor
2091
DBUG_ASSERT(fixed == 1);
2096
String *Item_null::val_str(String *str)
2098
// following assert is redundant, because fixed=1 assigned in constructor
2099
DBUG_ASSERT(fixed == 1);
2104
my_decimal *Item_null::val_decimal(my_decimal *decimal_value)
2110
Item *Item_null::safe_charset_converter(CHARSET_INFO *tocs)
2112
collation.set(tocs);
2116
/*********************** Item_param related ******************************/
2119
Default function of Item_param::set_param_func, so in case
2120
of malformed packet the server won't SIGSEGV.
2124
default_set_param_func(Item_param *param,
2125
uchar **pos __attribute__((unused)),
2126
ulong len __attribute__((unused)))
2132
Item_param::Item_param(uint pos_in_query_arg) :
2134
item_result_type(STRING_RESULT),
2135
/* Don't pretend to be a literal unless value for this item is set. */
2136
item_type(PARAM_ITEM),
2137
param_type(MYSQL_TYPE_VARCHAR),
2138
pos_in_query(pos_in_query_arg),
2139
set_param_func(default_set_param_func),
2140
limit_clause_param(false)
2144
Since we can't say whenever this item can be NULL or cannot be NULL
2145
before mysql_stmt_execute(), so we assuming that it can be NULL until
2149
cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE);
2150
cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin);
2154
void Item_param::set_null()
2156
DBUG_ENTER("Item_param::set_null");
2157
/* These are cleared after each execution by reset() method */
2160
Because of NULL and string values we need to set max_length for each new
2161
placeholder value: user can submit NULL for any placeholder type, and
2162
string length can be different in each execution.
2167
item_type= Item::NULL_ITEM;
2171
void Item_param::set_int(longlong i, uint32 max_length_arg)
2173
DBUG_ENTER("Item_param::set_int");
2174
value.integer= (longlong) i;
2176
max_length= max_length_arg;
2182
void Item_param::set_double(double d)
2184
DBUG_ENTER("Item_param::set_double");
2187
max_length= DBL_DIG + 8;
2188
decimals= NOT_FIXED_DEC;
2195
Set decimal parameter value from string.
2197
@param str character string
2198
@param length string length
2201
As we use character strings to send decimal values in
2202
binary protocol, we use str2my_decimal to convert it to
2203
internal decimal value.
2206
void Item_param::set_decimal(const char *str, ulong length)
2209
DBUG_ENTER("Item_param::set_decimal");
2211
end= (char*) str+length;
2212
str2my_decimal(E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2213
state= DECIMAL_VALUE;
2214
decimals= decimal_value.frac;
2215
max_length= my_decimal_precision_to_length(decimal_value.precision(),
2216
decimals, unsigned_flag);
2223
Set parameter value from MYSQL_TIME value.
2225
@param tm datetime value to set (time_type is ignored)
2226
@param type type of datetime value
2227
@param max_length_arg max length of datetime value as string
2230
If we value to be stored is not normalized, zero value will be stored
2231
instead and proper warning will be produced. This function relies on
2232
the fact that even wrong value sent over binary protocol fits into
2233
MAX_DATE_STRING_REP_LENGTH buffer.
2235
void Item_param::set_time(MYSQL_TIME *tm, timestamp_type time_type,
2236
uint32 max_length_arg)
2238
DBUG_ENTER("Item_param::set_time");
2241
value.time.time_type= time_type;
2243
if (value.time.year > 9999 || value.time.month > 12 ||
2244
value.time.day > 31 ||
2245
((time_type != MYSQL_TIMESTAMP_TIME) && value.time.hour > 23) ||
2246
value.time.minute > 59 || value.time.second > 59)
2248
char buff[MAX_DATE_STRING_REP_LENGTH];
2249
uint length= my_TIME_to_str(&value.time, buff);
2250
make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2251
buff, length, time_type, 0);
2252
set_zero_time(&value.time, MYSQL_TIMESTAMP_ERROR);
2257
max_length= max_length_arg;
2263
bool Item_param::set_str(const char *str, ulong length)
2265
DBUG_ENTER("Item_param::set_str");
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, don't add DBUG_PRINT here */
2283
bool Item_param::set_longdata(const char *str, ulong length)
2285
DBUG_ENTER("Item_param::set_longdata");
2288
If client character set is multibyte, end of long data packet
2289
may hit at the middle of a multibyte character. Additionally,
2290
if binary log is open we must write long data value to the
2291
binary log in character set of client. This is why we can't
2292
convert long data to connection character set as it comes
2293
(here), and first have to concatenate all pieces together,
2294
write query to the binary log and only then perform conversion.
2296
if (str_value.append(str, length, &my_charset_bin))
2298
state= LONG_DATA_VALUE;
2306
Set parameter value from user variable value.
2308
@param thd Current thread
2309
@param entry User variable structure (NULL means use NULL value)
2317
bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry)
2319
DBUG_ENTER("Item_param::set_from_user_var");
2320
if (entry && entry->value)
2322
item_result_type= entry->type;
2323
unsigned_flag= entry->unsigned_flag;
2324
if (limit_clause_param)
2327
set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
2328
item_type= Item::INT_ITEM;
2329
DBUG_RETURN(!unsigned_flag && value.integer < 0 ? 1 : 0);
2331
switch (item_result_type) {
2333
set_double(*(double*)entry->value);
2334
item_type= Item::REAL_ITEM;
2337
set_int(*(longlong*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
2338
item_type= Item::INT_ITEM;
2342
CHARSET_INFO *fromcs= entry->collation.collation;
2343
CHARSET_INFO *tocs= thd->variables.collation_connection;
2344
uint32 dummy_offset;
2346
value.cs_info.character_set_of_placeholder=
2347
value.cs_info.character_set_client= fromcs;
2349
Setup source and destination character sets so that they
2350
are different only if conversion is necessary: this will
2351
make later checks easier.
2353
value.cs_info.final_character_set_of_str_value=
2354
String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
2357
Exact value of max_length is not known unless data is converted to
2358
charset of connection, so we have to set it later.
2360
item_type= Item::STRING_ITEM;
2362
if (set_str((const char *)entry->value, entry->length))
2366
case DECIMAL_RESULT:
2368
const my_decimal *ent_value= (const my_decimal *)entry->value;
2369
my_decimal2decimal(ent_value, &decimal_value);
2370
state= DECIMAL_VALUE;
2371
decimals= ent_value->frac;
2372
max_length= my_decimal_precision_to_length(ent_value->precision(),
2373
decimals, unsigned_flag);
2374
item_type= Item::DECIMAL_ITEM;
2389
Resets parameter after execution.
2392
We clear null_value here instead of setting it in set_* methods,
2393
because we want more easily handle case for long data.
2396
void Item_param::reset()
2398
DBUG_ENTER("Item_param::reset");
2399
/* Shrink string buffer if it's bigger than max possible CHAR column */
2400
if (str_value.alloced_length() > MAX_CHAR_WIDTH)
2403
str_value.length(0);
2404
str_value_ptr.length(0);
2406
We must prevent all charset conversions until data has been written
2409
str_value.set_charset(&my_charset_bin);
2410
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
2415
Don't reset item_type to PARAM_ITEM: it's only needed to guard
2416
us from item optimizations at prepare stage, when item doesn't yet
2417
contain a literal of some kind.
2418
In all other cases when this object is accessed its value is
2419
set (this assumption is guarded by 'state' and
2420
DBUG_ASSERTS(state != NO_VALUE) in all Item_param::get_*
2427
int Item_param::save_in_field(Field *field, bool no_conversions)
2429
field->set_notnull();
2433
return field->store(value.integer, unsigned_flag);
2435
return field->store(value.real);
2437
return field->store_decimal(&decimal_value);
2439
field->store_time(&value.time, value.time.time_type);
2442
case LONG_DATA_VALUE:
2443
return field->store(str_value.ptr(), str_value.length(),
2444
str_value.charset());
2446
return set_field_to_null_with_conversions(field, no_conversions);
2455
bool Item_param::get_time(MYSQL_TIME *res)
2457
if (state == TIME_VALUE)
2463
If parameter value isn't supplied assertion will fire in val_str()
2464
which is called from Item::get_time().
2466
return Item::get_time(res);
2470
bool Item_param::get_date(MYSQL_TIME *res, uint fuzzydate)
2472
if (state == TIME_VALUE)
2477
return Item::get_date(res, fuzzydate);
2481
double Item_param::val_real()
2487
return (double) value.integer;
2491
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
2495
case LONG_DATA_VALUE:
2499
return my_strntod(str_value.charset(), (char*) str_value.ptr(),
2500
str_value.length(), &end_not_used, &dummy_err);
2504
This works for example when user says SELECT ?+0.0 and supplies
2505
time value for the placeholder.
2507
return ulonglong2double(TIME_to_ulonglong(&value.time));
2517
longlong Item_param::val_int()
2521
return (longlong) rint(value.real);
2523
return value.integer;
2527
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
2531
case LONG_DATA_VALUE:
2534
return my_strntoll(str_value.charset(), str_value.ptr(),
2535
str_value.length(), 10, (char**) 0, &dummy_err);
2538
return (longlong) TIME_to_ulonglong(&value.time);
2548
my_decimal *Item_param::val_decimal(my_decimal *dec)
2552
return &decimal_value;
2554
double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
2557
int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec);
2560
case LONG_DATA_VALUE:
2561
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec);
2565
longlong i= (longlong) TIME_to_ulonglong(&value.time);
2566
int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
2578
String *Item_param::val_str(String* str)
2582
case LONG_DATA_VALUE:
2583
return &str_value_ptr;
2585
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2588
str->set(value.integer, &my_charset_bin);
2591
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2597
if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
2599
str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
2600
str->set_charset(&my_charset_bin);
2612
Return Param item values in string format, for generating the dynamic
2613
query used in update/binary logs.
2616
- Change interface and implementation to fill log data in place
2617
and avoid one more memcpy/alloc between str and log string.
2618
- In case of error we need to notify replication
2619
that binary log contains wrong statement
2622
const String *Item_param::query_val_str(String* str) const
2626
str->set_int(value.integer, unsigned_flag, &my_charset_bin);
2629
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2632
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2634
return &my_null_string;
2641
TODO: in case of error we need to notify replication
2642
that binary log contains wrong statement
2644
if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3))
2647
/* Create date string inplace */
2648
buf= str->c_ptr_quick();
2651
ptr+= (uint) my_TIME_to_str(&value.time, ptr);
2653
str->length((uint32) (ptr - buf));
2657
case LONG_DATA_VALUE:
2660
append_query_string(value.cs_info.character_set_client, &str_value, str);
2664
return &my_null_string;
2673
Convert string from client character set to the character set of
2677
bool Item_param::convert_str_value(THD *thd)
2680
if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2683
Check is so simple because all charsets were set up properly
2684
in setup_one_conversion_function, where typecode of
2685
placeholder was also taken into account: the variables are different
2686
here only if conversion is really necessary.
2688
if (value.cs_info.final_character_set_of_str_value !=
2689
value.cs_info.character_set_of_placeholder)
2691
rc= thd->convert_string(&str_value,
2692
value.cs_info.character_set_of_placeholder,
2693
value.cs_info.final_character_set_of_str_value);
2696
str_value.set_charset(value.cs_info.final_character_set_of_str_value);
2697
/* Here str_value is guaranteed to be in final_character_set_of_str_value */
2699
max_length= str_value.length();
2702
str_value_ptr is returned from val_str(). It must be not alloced
2703
to prevent it's modification by val_str() invoker.
2705
str_value_ptr.set(str_value.ptr(), str_value.length(),
2706
str_value.charset());
2707
/* Synchronize item charset with value charset */
2708
collation.set(str_value.charset(), DERIVATION_COERCIBLE);
2714
bool Item_param::basic_const_item() const
2716
if (state == NO_VALUE || state == TIME_VALUE)
2723
Item_param::clone_item()
2725
/* see comments in the header file */
2728
return new Item_null(name);
2730
return (unsigned_flag ?
2731
new Item_uint(name, value.integer, max_length) :
2732
new Item_int(name, value.integer, max_length));
2734
return new Item_float(name, value.real, decimals, max_length);
2736
case LONG_DATA_VALUE:
2737
return new Item_string(name, str_value.c_ptr_quick(), str_value.length(),
2738
str_value.charset());
2750
Item_param::eq(const Item *arg, bool binary_cmp) const
2753
if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2756
We need to cast off const to call val_int(). This should be OK for
2765
return value.integer == item->val_int() &&
2766
unsigned_flag == item->unsigned_flag;
2768
return value.real == item->val_real();
2770
case LONG_DATA_VALUE:
2772
return !stringcmp(&str_value, &item->str_value);
2773
return !sortcmp(&str_value, &item->str_value, collation.collation);
2780
/* End of Item_param related */
2782
void Item_param::print(String *str, enum_query_type query_type)
2784
if (state == NO_VALUE)
2790
char buffer[STRING_BUFFER_USUAL_SIZE];
2791
String tmp(buffer, sizeof(buffer), &my_charset_bin);
2793
res= query_val_str(&tmp);
2799
/****************************************************************************
2801
****************************************************************************/
2803
void Item_copy_string::copy()
2805
String *res=item->val_str(&str_value);
2806
if (res && res != &str_value)
2807
str_value.copy(*res);
2808
null_value=item->null_value;
2812
String *Item_copy_string::val_str(String *str)
2814
// Item_copy_string is used without fix_fields call
2821
my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value)
2823
// Item_copy_string is used without fix_fields call
2826
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value);
2827
return (decimal_value);
951
2832
Functions to convert item to field (for send_fields)
955
bool Item::fix_fields(Session *, Item **)
2836
bool Item::fix_fields(THD *thd, Item **ref)
958
2839
// We do not check fields which are fixed during construction
959
assert(fixed == 0 || basic_const_item());
2840
DBUG_ASSERT(fixed == 0 || basic_const_item());
2845
double Item_ref_null_helper::val_real()
2847
DBUG_ASSERT(fixed == 1);
2848
double tmp= (*ref)->val_result();
2849
owner->was_null|= null_value= (*ref)->null_value;
2854
longlong Item_ref_null_helper::val_int()
2856
DBUG_ASSERT(fixed == 1);
2857
longlong tmp= (*ref)->val_int_result();
2858
owner->was_null|= null_value= (*ref)->null_value;
2863
my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value)
2865
DBUG_ASSERT(fixed == 1);
2866
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
2867
owner->was_null|= null_value= (*ref)->null_value;
2872
bool Item_ref_null_helper::val_bool()
2874
DBUG_ASSERT(fixed == 1);
2875
bool val= (*ref)->val_bool_result();
2876
owner->was_null|= null_value= (*ref)->null_value;
2881
String* Item_ref_null_helper::val_str(String* s)
2883
DBUG_ASSERT(fixed == 1);
2884
String* tmp= (*ref)->str_result(s);
2885
owner->was_null|= null_value= (*ref)->null_value;
2890
bool Item_ref_null_helper::get_date(MYSQL_TIME *ltime, uint fuzzydate)
2892
return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
966
Mark item and Select_Lexs as dependent if item was resolved in
2897
Mark item and SELECT_LEXs as dependent if item was resolved in
969
@param session thread handler
2900
@param thd thread handler
970
2901
@param last select from which current item depend
971
2902
@param current current select
972
2903
@param resolved_item item which was resolved in outer SELECT(for warning)
1249
3180
if (select_ref != not_found_item && !ambiguous_fields)
1251
assert(*select_ref != 0);
3182
DBUG_ASSERT(*select_ref != 0);
1252
3183
if (!select->ref_pointer_array[counter])
1254
3185
my_error(ER_ILLEGAL_REFERENCE, MYF(0),
1255
3186
ref->name, "forward reference in item list");
1258
assert((*select_ref)->fixed);
3189
DBUG_ASSERT((*select_ref)->fixed);
1259
3190
return (select->ref_pointer_array + counter);
1261
3192
if (group_by_ref)
1262
3193
return group_by_ref;
1264
3195
return NULL; /* So there is no compiler warning. */
1267
3198
return (Item**) not_found_item;
3203
Resolve the name of an outer select column reference.
3205
The method resolves the column reference represented by 'this' as a column
3206
present in outer selects that contain current select.
3208
In prepared statements, because of cache, find_field_in_tables()
3209
can resolve fields even if they don't belong to current context.
3210
In this case this method only finds appropriate context and marks
3211
current select as dependent. The found reference of field should be
3212
provided in 'from_field'.
3214
@param[in] thd current thread
3215
@param[in,out] from_field found field reference or (Field*)not_found_field
3216
@param[in,out] reference view column if this item was resolved to a
3220
This is the inner loop of Item_field::fix_fields:
3222
for each outer query Q_k beginning from the inner-most one
3224
search for a column or derived column named col_ref_i
3225
[in table T_j] in the FROM clause of Q_k;
3227
if such a column is not found
3228
Search for a column or derived column named col_ref_i
3229
[in table T_j] in the SELECT and GROUP clauses of Q_k.
3234
1 column succefully resolved and fix_fields() should continue.
3236
0 column fully fixed and fix_fields() should return false
3242
Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
3244
enum_parsing_place place= NO_MATTER;
3245
bool field_found= (*from_field != not_found_field);
3246
bool upward_lookup= false;
3249
If there are outer contexts (outer selects, but current select is
3250
not derived table or view) try to resolve this reference in the
3253
We treat each subselect as a separate namespace, so that different
3254
subselects may contain columns with the same names. The subselects
3255
are searched starting from the innermost.
3257
Name_resolution_context *last_checked_context= context;
3258
Item **ref= (Item **) not_found_item;
3259
SELECT_LEX *current_sel= (SELECT_LEX *) thd->lex->current_select;
3260
Name_resolution_context *outer_context= 0;
3261
SELECT_LEX *select= 0;
3262
/* Currently derived tables cannot be correlated */
3263
if (current_sel->master_unit()->first_select()->linkage !=
3265
outer_context= context->outer_context;
3268
outer_context= outer_context->outer_context)
3270
select= outer_context->select_lex;
3271
Item_subselect *prev_subselect_item=
3272
last_checked_context->select_lex->master_unit()->item;
3273
last_checked_context= outer_context;
3274
upward_lookup= true;
3276
place= prev_subselect_item->parsing_place;
3278
If outer_field is set, field was already found by first call
3279
to find_field_in_tables(). Only need to find appropriate context.
3281
if (field_found && outer_context->select_lex !=
3282
cached_table->select_lex)
3285
In case of a view, find_field_in_tables() writes the pointer to
3286
the found view field into '*reference', in other words, it
3287
substitutes this Item_field with the found expression.
3289
if (field_found || (*from_field= find_field_in_tables(thd, this,
3291
first_name_resolution_table,
3293
last_name_resolution_table,
3295
IGNORE_EXCEPT_NON_UNIQUE,
3301
if (*from_field != view_ref_found)
3303
prev_subselect_item->used_tables_cache|= (*from_field)->table->map;
3304
prev_subselect_item->const_item_cache= 0;
3305
set_field(*from_field);
3306
if (!last_checked_context->select_lex->having_fix_field &&
3307
select->group_list.elements &&
3308
(place == SELECT_LIST || place == IN_HAVING))
3312
If an outer field is resolved in a grouping select then it
3313
is replaced for an Item_outer_ref object. Otherwise an
3314
Item_field object is used.
3315
The new Item_outer_ref object is saved in the inner_refs_list of
3316
the outer select. Here it is only created. It can be fixed only
3317
after the original field has been fixed and this is done in the
3318
fix_inner_refs() function.
3321
if (!(rf= new Item_outer_ref(context, this)))
3323
thd->change_item_tree(reference, rf);
3324
select->inner_refs_list.push_back(rf);
3325
rf->in_sum_func= thd->lex->in_sum_func;
3328
A reference is resolved to a nest level that's outer or the same as
3329
the nest level of the enclosing set function : adjust the value of
3330
max_arg_level for the function if it's needed.
3332
if (thd->lex->in_sum_func &&
3333
thd->lex->in_sum_func->nest_level >= select->nest_level)
3335
Item::Type ref_type= (*reference)->type();
3336
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3337
select->nest_level);
3338
set_field(*from_field);
3340
mark_as_dependent(thd, last_checked_context->select_lex,
3341
context->select_lex, this,
3342
((ref_type == REF_ITEM ||
3343
ref_type == FIELD_ITEM) ?
3344
(Item_ident*) (*reference) : 0));
3350
Item::Type ref_type= (*reference)->type();
3351
prev_subselect_item->used_tables_cache|=
3352
(*reference)->used_tables();
3353
prev_subselect_item->const_item_cache&=
3354
(*reference)->const_item();
3355
mark_as_dependent(thd, last_checked_context->select_lex,
3356
context->select_lex, this,
3357
((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3358
(Item_ident*) (*reference) :
3361
A reference to a view field had been found and we
3362
substituted it instead of this Item (find_field_in_tables
3363
does it by assigning the new value to *reference), so now
3364
we can return from this function.
3372
/* Search in SELECT and GROUP lists of the outer select. */
3373
if (place != IN_WHERE && place != IN_ON)
3375
if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
3376
return -1; /* Some error occurred (e.g. ambiguous names). */
3377
if (ref != not_found_item)
3379
DBUG_ASSERT(*ref && (*ref)->fixed);
3380
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
3381
prev_subselect_item->const_item_cache&= (*ref)->const_item();
3387
Reference is not found in this select => this subquery depend on
3388
outer select (or we just trying to find wrong identifier, in this
3389
case it does not matter which used tables bits we set)
3391
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
3392
prev_subselect_item->const_item_cache= 0;
3395
DBUG_ASSERT(ref != 0);
3398
if (ref == not_found_item && *from_field == not_found_field)
3402
// We can't say exactly what absent table or field
3403
my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where);
3407
/* Call find_field_in_tables only to report the error */
3408
find_field_in_tables(thd, this,
3409
context->first_name_resolution_table,
3410
context->last_name_resolution_table,
3411
reference, REPORT_ALL_ERRORS,
3417
else if (ref != not_found_item)
3422
/* Should have been checked in resolve_ref_in_select_and_group(). */
3423
DBUG_ASSERT(*ref && (*ref)->fixed);
3425
Here, a subset of actions performed by Item_ref::set_properties
3426
is not enough. So we pass ptr to NULL into Item_[direct]_ref
3427
constructor, so no initialization is performed, and call
3431
*ref= NULL; // Don't call set_properties()
3432
rf= (place == IN_HAVING ?
3433
new Item_ref(context, ref, (char*) table_name,
3434
(char*) field_name, alias_name_used) :
3435
(!select->group_list.elements ?
3436
new Item_direct_ref(context, ref, (char*) table_name,
3437
(char*) field_name, alias_name_used) :
3438
new Item_outer_ref(context, ref, (char*) table_name,
3439
(char*) field_name, alias_name_used)));
3444
if (place != IN_HAVING && select->group_list.elements)
3446
outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3447
((Item_outer_ref*)rf)->in_sum_func= thd->lex->in_sum_func;
3449
thd->change_item_tree(reference, rf);
3451
rf is Item_ref => never substitute other items (in this case)
3452
during fix_fields() => we can use rf after fix_fields()
3454
DBUG_ASSERT(!rf->fixed); // Assured by Item_ref()
3455
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3458
mark_as_dependent(thd, last_checked_context->select_lex,
3459
context->select_lex, this,
3465
mark_as_dependent(thd, last_checked_context->select_lex,
3466
context->select_lex,
3467
this, (Item_ident*)*reference);
3468
if (last_checked_context->select_lex->having_fix_field)
3471
rf= new Item_ref(context,
3472
(cached_table->db[0] ? cached_table->db : 0),
3473
(char*) cached_table->alias, (char*) field_name);
3476
thd->change_item_tree(reference, rf);
3478
rf is Item_ref => never substitute other items (in this case)
3479
during fix_fields() => we can use rf after fix_fields()
3481
DBUG_ASSERT(!rf->fixed); // Assured by Item_ref()
3482
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3492
Resolve the name of a column reference.
3494
The method resolves the column reference represented by 'this' as a column
3495
present in one of: FROM clause, SELECT clause, GROUP BY clause of a query
3496
Q, or in outer queries that contain Q.
3498
The name resolution algorithm used is (where [T_j] is an optional table
3499
name that qualifies the column name):
3502
resolve_column_reference([T_j].col_ref_i)
3504
search for a column or derived column named col_ref_i
3505
[in table T_j] in the FROM clause of Q;
3507
if such a column is NOT found AND // Lookup in outer queries.
3508
there are outer queries
3510
for each outer query Q_k beginning from the inner-most one
3512
search for a column or derived column named col_ref_i
3513
[in table T_j] in the FROM clause of Q_k;
3515
if such a column is not found
3516
Search for a column or derived column named col_ref_i
3517
[in table T_j] in the SELECT and GROUP clauses of Q_k.
3523
Notice that compared to Item_ref::fix_fields, here we first search the FROM
3524
clause, and then we search the SELECT and GROUP BY clauses.
3526
@param[in] thd current thread
3527
@param[in,out] reference view column if this item was resolved to a
3536
bool Item_field::fix_fields(THD *thd, Item **reference)
3538
DBUG_ASSERT(fixed == 0);
3539
Field *from_field= (Field *)not_found_field;
3540
bool outer_fixed= false;
3542
if (!field) // If field is not checked
3545
In case of view, find_field_in_tables() write pointer to view field
3546
expression to 'reference', i.e. it substitute that expression instead
3549
if ((from_field= find_field_in_tables(thd, this,
3550
context->first_name_resolution_table,
3551
context->last_name_resolution_table,
3553
thd->lex->use_only_table_context ?
3555
IGNORE_EXCEPT_NON_UNIQUE,
3561
/* Look up in current select's item_list to find aliased fields */
3562
if (thd->lex->current_select->is_item_list_lookup)
3565
enum_resolution_type resolution;
3566
Item** res= find_item_in_list(this, thd->lex->current_select->item_list,
3567
&counter, REPORT_EXCEPT_NOT_FOUND,
3571
if (resolution == RESOLVED_AGAINST_ALIAS)
3572
alias_name_used= true;
3573
if (res != (Item **)not_found_item)
3575
if ((*res)->type() == Item::FIELD_ITEM)
3578
It's an Item_field referencing another Item_field in the select
3580
Use the field from the Item_field in the select list and leave
3581
the Item_field instance in place.
3584
Field *new_field= (*((Item_field**)res))->field;
3586
if (new_field == NULL)
3588
/* The column to which we link isn't valid. */
3589
my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
3590
current_thd->where);
3594
set_field(new_field);
3600
It's not an Item_field in the select list so we must make a new
3601
Item_ref to point to the Item in the select list and replace the
3602
Item_field created by the parser with the new Item_ref.
3604
Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3607
thd->change_item_tree(reference, rf);
3609
Because Item_ref never substitutes itself with other items
3610
in Item_ref::fix_fields(), we can safely use the original
3611
pointer to it even after fix_fields()
3613
return rf->fix_fields(thd, reference) || rf->check_cols(1);
3617
if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3621
goto mark_non_agg_field;
3623
else if (!from_field)
3626
if (!outer_fixed && cached_table && cached_table->select_lex &&
3627
context->select_lex &&
3628
cached_table->select_lex != context->select_lex)
3631
if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3635
goto mark_non_agg_field;
3639
if it is not expression from merged VIEW we will set this field.
3641
We can leave expression substituted from view for next PS/SP rexecution
3642
(i.e. do not register this substitution for reverting on cleanup()
3643
(register_item_tree_changing())), because this subtree will be
3644
fix_field'ed during setup_tables()->setup_underlying() (i.e. before
3645
all other expressions of query, and references on tables which do
3646
not present in query will not make problems.
3648
Also we suppose that view can't be changed during PS/SP life.
3650
if (from_field == view_ref_found)
3653
set_field(from_field);
3654
if (thd->lex->in_sum_func &&
3655
thd->lex->in_sum_func->nest_level ==
3656
thd->lex->current_select->nest_level)
3657
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3658
thd->lex->current_select->nest_level);
3660
else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3662
TABLE *table= field->table;
3663
MY_BITMAP *current_bitmap, *other_bitmap;
3664
if (thd->mark_used_columns == MARK_COLUMNS_READ)
3666
current_bitmap= table->read_set;
3667
other_bitmap= table->write_set;
3671
current_bitmap= table->write_set;
3672
other_bitmap= table->read_set;
3674
if (!bitmap_fast_test_and_set(current_bitmap, field->field_index))
3676
if (!bitmap_is_set(other_bitmap, field->field_index))
3678
/* First usage of column */
3679
table->used_fields++; // Used to optimize loops
3680
/* purecov: begin inspected */
3681
table->covering_keys.intersect(field->part_of_key);
3691
context->process_error(thd);
3695
Item *Item_field::safe_charset_converter(CHARSET_INFO *tocs)
3698
return Item::safe_charset_converter(tocs);
3702
void Item_field::cleanup()
3704
DBUG_ENTER("Item_field::cleanup");
3705
Item_ident::cleanup();
3707
Even if this object was created by direct link to field in setup_wild()
3708
it will be linked correctly next time by name of field and table alias.
3709
I.e. we can drop 'field'.
3711
field= result_field= 0;
3717
Find a field among specified multiple equalities.
3719
The function first searches the field among multiple equalities
3720
of the current level (in the cond_equal->current_level list).
3721
If it fails, it continues searching in upper levels accessed
3722
through a pointer cond_equal->upper_levels.
3723
The search terminates as soon as a multiple equality containing
3726
@param cond_equal reference to list of multiple equalities where
3727
the field (this object) is to be looked for
3730
- First Item_equal containing the field, if success
3734
Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal)
3736
Item_equal *item= 0;
3739
List_iterator_fast<Item_equal> li(cond_equal->current_level);
3740
while ((item= li++))
3742
if (item->contains(field))
3746
The field is not found in any of the multiple equalities
3747
of the current level. Look for it in upper levels
3749
cond_equal= cond_equal->upper_levels;
3756
Check whether a field can be substituted by an equal item.
3758
The function checks whether a substitution of the field
3759
occurrence for an equal item is valid.
3761
@param arg *arg != NULL <-> the field is in the context where
3762
substitution for an equal item is valid
3765
The following statement is not always true:
3769
This means substitution of an item for an equal item not always
3770
yields an equavalent condition. Here's an example:
3773
(LENGTH('a')=1) != (LENGTH('a ')=2)
3775
Such a substitution is surely valid if either the substituted
3776
field is not of a STRING type or if it is an argument of
3777
a comparison predicate.
3780
true substitution is valid
3785
bool Item_field::subst_argument_checker(uchar **arg)
3787
return (result_type() != STRING_RESULT) || (*arg);
3792
Convert a numeric value to a zero-filled string
3794
@param[in,out] item the item to operate on
3795
@param field The field that this value is equated to
3797
This function converts a numeric value to a string. In this conversion
3798
the zero-fill flag of the field is taken into account.
3799
This is required so the resulting string value can be used instead of
3800
the field reference when propagating equalities.
3803
static void convert_zerofill_number_to_string(Item **item, Field_num *field)
3805
char buff[MAX_FIELD_WIDTH],*pos;
3806
String tmp(buff,sizeof(buff), field->charset()), *res;
3808
res= (*item)->val_str(&tmp);
3809
field->prepend_zeros(res);
3810
pos= (char *) sql_strmake (res->ptr(), res->length());
3811
*item= new Item_string(pos, res->length(), field->charset());
3816
Set a pointer to the multiple equality the field reference belongs to
3819
The function looks for a multiple equality containing the field item
3820
among those referenced by arg.
3821
In the case such equality exists the function does the following.
3822
If the found multiple equality contains a constant, then the field
3823
reference is substituted for this constant, otherwise it sets a pointer
3824
to the multiple equality in the field item.
3827
@param arg reference to list of multiple equalities where
3828
the field (this object) is to be looked for
3831
This function is supposed to be called as a callback parameter in calls
3832
of the compile method.
3835
- pointer to the replacing constant item, if the field item was substituted
3836
- pointer to the field item, otherwise.
3839
Item *Item_field::equal_fields_propagator(uchar *arg)
3843
item_equal= find_item_equal((COND_EQUAL *) arg);
3846
item= item_equal->get_const();
3848
Disable const propagation for items used in different comparison contexts.
3849
This must be done because, for example, Item_hex_string->val_int() is not
3850
the same as (Item_hex_string->val_str() in BINARY column)->val_int().
3851
We cannot simply disable the replacement in a particular context (
3852
e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
3853
Items don't know the context they are in and there are functions like
3854
IF (<hex_string>, 'yes', 'no').
3855
The same problem occurs when comparing a DATE/TIME field with a
3856
DATE/TIME represented as an int and as a string.
3859
(cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
3861
else if (field && (field->flags & ZEROFILL_FLAG) && IS_NUM(field->type()))
3863
if (item && cmp_context != INT_RESULT)
3864
convert_zerofill_number_to_string(&item, (Field_num *)field);
3873
Mark the item to not be part of substitution if it's not a binary item.
3875
See comments in Arg_comparator::set_compare_func() for details.
3878
bool Item_field::set_no_const_sub(uchar *arg)
3880
if (field->charset() != &my_charset_bin)
3887
Replace an Item_field for an equal Item_field that evaluated earlier
3890
The function returns a pointer to an item that is taken from
3891
the very beginning of the item_equal list which the Item_field
3892
object refers to (belongs to) unless item_equal contains a constant
3893
item. In this case the function returns this constant item,
3894
(if the substitution does not require conversion).
3895
If the Item_field object does not refer any Item_equal object
3896
'this' is returned .
3898
@param arg a dummy parameter, is not used here
3902
This function is supposed to be called as a callback parameter in calls
3903
of the thransformer method.
3906
- pointer to a replacement Item_field if there is a better equal item or
3907
a pointer to a constant equal item;
3911
Item *Item_field::replace_equal_field(uchar *arg)
3915
Item *const_item= item_equal->get_const();
3918
if (cmp_context != (Item_result)-1 &&
3919
const_item->cmp_context != cmp_context)
3923
Item_field *subst= item_equal->get_first();
3924
if (subst && !field->eq(subst->field))
1270
3931
void Item::init_make_field(Send_field *tmp_field,
1271
3932
enum enum_field_types field_type_arg)
1583
4713
switch ((f_type=field_type())) {
1585
case DRIZZLE_TYPE_NULL:
1586
case DRIZZLE_TYPE_ENUM:
1587
case DRIZZLE_TYPE_BLOB:
1588
case DRIZZLE_TYPE_VARCHAR:
1589
case DRIZZLE_TYPE_NEWDECIMAL:
4715
case MYSQL_TYPE_NULL:
4716
case MYSQL_TYPE_DECIMAL:
4717
case MYSQL_TYPE_ENUM:
4718
case MYSQL_TYPE_SET:
4719
case MYSQL_TYPE_TINY_BLOB:
4720
case MYSQL_TYPE_MEDIUM_BLOB:
4721
case MYSQL_TYPE_LONG_BLOB:
4722
case MYSQL_TYPE_BLOB:
4723
case MYSQL_TYPE_STRING:
4724
case MYSQL_TYPE_VAR_STRING:
4725
case MYSQL_TYPE_VARCHAR:
4726
case MYSQL_TYPE_NEWDECIMAL:
1592
4729
if ((res=val_str(buffer)))
1593
4730
result= protocol->store(res->ptr(),res->length(),res->charset());
1596
case DRIZZLE_TYPE_LONG:
1601
result= protocol->store((int32_t)nr);
1604
case DRIZZLE_TYPE_LONGLONG:
1611
result= protocol->store((uint64_t)nr);
1613
result= protocol->store((int64_t)nr);
1617
case DRIZZLE_TYPE_DOUBLE:
4733
case MYSQL_TYPE_TINY:
4738
result= protocol->store_tiny(nr);
4741
case MYSQL_TYPE_SHORT:
4742
case MYSQL_TYPE_YEAR:
4747
result= protocol->store_short(nr);
4750
case MYSQL_TYPE_LONG:
4755
result= protocol->store_long(nr);
4758
case MYSQL_TYPE_LONGLONG:
4763
result= protocol->store_longlong(nr, unsigned_flag);
4766
case MYSQL_TYPE_FLOAT:
4769
nr= (float) val_real();
4771
result= protocol->store(nr, decimals, buffer);
4774
case MYSQL_TYPE_DOUBLE:
1619
4776
double nr= val_real();
1620
4777
if (!null_value)
1621
4778
result= protocol->store(nr, decimals, buffer);
1624
case DRIZZLE_TYPE_DATETIME:
1625
case DRIZZLE_TYPE_TIMESTAMP:
4781
case MYSQL_TYPE_DATETIME:
4782
case MYSQL_TYPE_DATE:
4783
case MYSQL_TYPE_TIMESTAMP:
1628
4786
get_date(&tm, TIME_FUZZY_DATE);
1629
4787
if (!null_value)
1630
result= protocol->store(&tm);
4789
if (f_type == MYSQL_TYPE_DATE)
4790
return protocol->store_date(&tm);
4792
result= protocol->store(&tm);
4796
case MYSQL_TYPE_TIME:
4801
result= protocol->store_time(&tm);
1634
4805
if (null_value)
1635
result= protocol->store();
4806
result= protocol->store_null();
4811
bool Item_field::send(Protocol *protocol, String *buffer)
4813
return protocol->store(result_field);
4817
void Item_field::update_null_value()
4820
need to set no_errors to prevent warnings about type conversion
4823
THD *thd= field->table->in_use;
4826
no_errors= thd->no_errors;
4828
Item::update_null_value();
4829
thd->no_errors= no_errors;
4834
Add the field to the select list and substitute it for the reference to
4838
Item_field::update_value_transformer()
4839
select_arg current select
4842
If the field doesn't belong to the table being inserted into then it is
4843
added to the select list, pointer to it is stored in the ref_pointer_array
4844
of the select and the field itself is substituted for the Item_ref object.
4845
This is done in order to get correct values from update fields that
4846
belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
4851
ref if all conditions are met
4852
this field otherwise
4855
Item *Item_field::update_value_transformer(uchar *select_arg)
4857
SELECT_LEX *select= (SELECT_LEX*)select_arg;
4860
if (field->table != select->context.table_list->table &&
4861
type() != Item::TRIGGER_FIELD_ITEM)
4863
List<Item> *all_fields= &select->join->all_fields;
4864
Item **ref_pointer_array= select->ref_pointer_array;
4865
int el= all_fields->elements;
4868
ref_pointer_array[el]= (Item*)this;
4869
all_fields->push_front((Item*)this);
4870
ref= new Item_ref(&select->context, ref_pointer_array + el,
4871
table_name, field_name);
4878
void Item_field::print(String *str, enum_query_type query_type)
4880
if (field && field->table->const_table)
4882
char buff[MAX_FIELD_WIDTH];
4883
String tmp(buff,sizeof(buff),str->charset());
4884
field->val_str(&tmp);
4890
Item_ident::print(str, query_type);
4894
Item_ref::Item_ref(Name_resolution_context *context_arg,
4895
Item **item, const char *table_name_arg,
4896
const char *field_name_arg,
4897
bool alias_name_used_arg)
4898
:Item_ident(context_arg, NullS, table_name_arg, field_name_arg),
4899
result_field(0), ref(item)
4901
alias_name_used= alias_name_used_arg;
4903
This constructor used to create some internals references over fixed items
4905
if (ref && *ref && (*ref)->fixed)
4911
Resolve the name of a reference to a column reference.
4913
The method resolves the column reference represented by 'this' as a column
4914
present in one of: GROUP BY clause, SELECT clause, outer queries. It is
4915
used typically for columns in the HAVING clause which are not under
4916
aggregate functions.
4919
Item_ref::ref is 0 or points to a valid item.
4922
The name resolution algorithm used is (where [T_j] is an optional table
4923
name that qualifies the column name):
4926
resolve_extended([T_j].col_ref_i)
4928
Search for a column or derived column named col_ref_i [in table T_j]
4929
in the SELECT and GROUP clauses of Q.
4931
if such a column is NOT found AND // Lookup in outer queries.
4932
there are outer queries
4934
for each outer query Q_k beginning from the inner-most one
4936
Search for a column or derived column named col_ref_i
4937
[in table T_j] in the SELECT and GROUP clauses of Q_k.
4939
if such a column is not found AND
4940
- Q_k is not a group query AND
4941
- Q_k is not inside an aggregate function
4943
- Q_(k-1) is not in a HAVING or SELECT clause of Q_k
4945
search for a column or derived column named col_ref_i
4946
[in table T_j] in the FROM clause of Q_k;
4953
This procedure treats GROUP BY and SELECT clauses as one namespace for
4954
column references in HAVING. Notice that compared to
4955
Item_field::fix_fields, here we first search the SELECT and GROUP BY
4956
clauses, and then we search the FROM clause.
4958
@param[in] thd current thread
4959
@param[in,out] reference view column if this item was resolved to a
4963
Here we could first find the field anyway, and then test this
4964
condition, so that we can give a better error message -
4965
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4966
ER_BAD_FIELD_ERROR which we produce now.
4974
bool Item_ref::fix_fields(THD *thd, Item **reference)
4976
enum_parsing_place place= NO_MATTER;
4977
DBUG_ASSERT(fixed == 0);
4978
SELECT_LEX *current_sel= thd->lex->current_select;
4980
if (!ref || ref == not_found_item)
4982
if (!(ref= resolve_ref_in_select_and_group(thd, this,
4983
context->select_lex)))
4984
goto error; /* Some error occurred (e.g. ambiguous names). */
4986
if (ref == not_found_item) /* This reference was not resolved. */
4988
Name_resolution_context *last_checked_context= context;
4989
Name_resolution_context *outer_context= context->outer_context;
4995
/* The current reference cannot be resolved in this query. */
4996
my_error(ER_BAD_FIELD_ERROR,MYF(0),
4997
this->full_name(), current_thd->where);
5002
If there is an outer context (select), and it is not a derived table
5003
(which do not support the use of outer fields for now), try to
5004
resolve this reference in the outer select(s).
5006
We treat each subselect as a separate namespace, so that different
5007
subselects may contain columns with the same names. The subselects are
5008
searched starting from the innermost.
5010
from_field= (Field*) not_found_field;
5014
SELECT_LEX *select= outer_context->select_lex;
5015
Item_subselect *prev_subselect_item=
5016
last_checked_context->select_lex->master_unit()->item;
5017
last_checked_context= outer_context;
5019
/* Search in the SELECT and GROUP lists of the outer select. */
5020
if (outer_context->resolve_in_select_list)
5022
if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
5023
goto error; /* Some error occurred (e.g. ambiguous names). */
5024
if (ref != not_found_item)
5026
DBUG_ASSERT(*ref && (*ref)->fixed);
5027
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
5028
prev_subselect_item->const_item_cache&= (*ref)->const_item();
5032
Set ref to 0 to ensure that we get an error in case we replaced
5033
this item with another item and still use this item in some
5034
other place of the parse tree.
5039
place= prev_subselect_item->parsing_place;
5041
Check table fields only if the subquery is used somewhere out of
5042
HAVING or the outer SELECT does not use grouping (i.e. tables are
5045
Here we could first find the field anyway, and then test this
5046
condition, so that we can give a better error message -
5047
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
5048
ER_BAD_FIELD_ERROR which we produce now.
5050
if ((place != IN_HAVING ||
5051
(!select->with_sum_func &&
5052
select->group_list.elements == 0)))
5055
In case of view, find_field_in_tables() write pointer to view
5056
field expression to 'reference', i.e. it substitute that
5057
expression instead of this Item_ref
5059
from_field= find_field_in_tables(thd, this,
5061
first_name_resolution_table,
5063
last_name_resolution_table,
5065
IGNORE_EXCEPT_NON_UNIQUE,
5069
if (from_field == view_ref_found)
5071
Item::Type refer_type= (*reference)->type();
5072
prev_subselect_item->used_tables_cache|=
5073
(*reference)->used_tables();
5074
prev_subselect_item->const_item_cache&=
5075
(*reference)->const_item();
5076
DBUG_ASSERT((*reference)->type() == REF_ITEM);
5077
mark_as_dependent(thd, last_checked_context->select_lex,
5078
context->select_lex, this,
5079
((refer_type == REF_ITEM ||
5080
refer_type == FIELD_ITEM) ?
5081
(Item_ident*) (*reference) :
5084
view reference found, we substituted it instead of this
5089
if (from_field != not_found_field)
5091
if (cached_table && cached_table->select_lex &&
5092
outer_context->select_lex &&
5093
cached_table->select_lex != outer_context->select_lex)
5096
Due to cache, find_field_in_tables() can return field which
5097
doesn't belong to provided outer_context. In this case we have
5098
to find proper field context in order to fix field correcly.
5102
outer_context= outer_context->outer_context;
5103
select= outer_context->select_lex;
5104
prev_subselect_item=
5105
last_checked_context->select_lex->master_unit()->item;
5106
last_checked_context= outer_context;
5107
} while (outer_context && outer_context->select_lex &&
5108
cached_table->select_lex != outer_context->select_lex);
5110
prev_subselect_item->used_tables_cache|= from_field->table->map;
5111
prev_subselect_item->const_item_cache= 0;
5115
DBUG_ASSERT(from_field == not_found_field);
5117
/* Reference is not found => depend on outer (or just error). */
5118
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
5119
prev_subselect_item->const_item_cache= 0;
5121
outer_context= outer_context->outer_context;
5122
} while (outer_context);
5124
DBUG_ASSERT(from_field != 0 && from_field != view_ref_found);
5125
if (from_field != not_found_field)
5128
if (!(fld= new Item_field(from_field)))
5130
thd->change_item_tree(reference, fld);
5131
mark_as_dependent(thd, last_checked_context->select_lex,
5132
thd->lex->current_select, this, fld);
5134
A reference is resolved to a nest level that's outer or the same as
5135
the nest level of the enclosing set function : adjust the value of
5136
max_arg_level for the function if it's needed.
5138
if (thd->lex->in_sum_func &&
5139
thd->lex->in_sum_func->nest_level >=
5140
last_checked_context->select_lex->nest_level)
5141
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5142
last_checked_context->select_lex->nest_level);
5147
/* The item was not a table field and not a reference */
5148
my_error(ER_BAD_FIELD_ERROR, MYF(0),
5149
this->full_name(), current_thd->where);
5152
/* Should be checked in resolve_ref_in_select_and_group(). */
5153
DBUG_ASSERT(*ref && (*ref)->fixed);
5154
mark_as_dependent(thd, last_checked_context->select_lex,
5155
context->select_lex, this, this);
5157
A reference is resolved to a nest level that's outer or the same as
5158
the nest level of the enclosing set function : adjust the value of
5159
max_arg_level for the function if it's needed.
5161
if (thd->lex->in_sum_func &&
5162
thd->lex->in_sum_func->nest_level >=
5163
last_checked_context->select_lex->nest_level)
5164
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5165
last_checked_context->select_lex->nest_level);
5171
Check if this is an incorrect reference in a group function or forward
5172
reference. Do not issue an error if this is:
5173
1. outer reference (will be fixed later by the fix_inner_refs function);
5174
2. an unnamed reference inside an aggregate function.
5176
if (!((*ref)->type() == REF_ITEM &&
5177
((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
5178
(((*ref)->with_sum_func && name &&
5179
!(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
5180
current_sel->having_fix_field)) ||
5183
my_error(ER_ILLEGAL_REFERENCE, MYF(0),
5184
name, ((*ref)->with_sum_func?
5185
"reference to group function":
5186
"forward reference in item list"));
5192
if ((*ref)->check_cols(1))
5197
context->process_error(thd);
5202
void Item_ref::set_properties()
5204
max_length= (*ref)->max_length;
5205
maybe_null= (*ref)->maybe_null;
5206
decimals= (*ref)->decimals;
5207
collation.set((*ref)->collation);
5209
We have to remember if we refer to a sum function, to ensure that
5210
split_sum_func() doesn't try to change the reference.
5212
with_sum_func= (*ref)->with_sum_func;
5213
unsigned_flag= (*ref)->unsigned_flag;
5215
if (alias_name_used)
5217
if ((*ref)->type() == FIELD_ITEM)
5218
alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
5220
alias_name_used= true; // it is not field, so it is was resolved by alias
5224
void Item_ref::cleanup()
5226
DBUG_ENTER("Item_ref::cleanup");
5227
Item_ident::cleanup();
5233
void Item_ref::print(String *str, enum_query_type query_type)
5237
if ((*ref)->type() != Item::CACHE_ITEM && ref_type() != VIEW_REF &&
5238
!table_name && name && alias_name_used)
5240
THD *thd= current_thd;
5241
append_identifier(thd, str, name, (uint) strlen(name));
5244
(*ref)->print(str, query_type);
5247
Item_ident::print(str, query_type);
5251
bool Item_ref::send(Protocol *prot, String *tmp)
5254
return prot->store(result_field);
5255
return (*ref)->send(prot, tmp);
5259
double Item_ref::val_result()
5263
if ((null_value= result_field->is_null()))
5265
return result_field->val_real();
5271
longlong Item_ref::val_int_result()
5275
if ((null_value= result_field->is_null()))
5277
return result_field->val_int();
5283
String *Item_ref::str_result(String* str)
5287
if ((null_value= result_field->is_null()))
5289
str->set_charset(str_value.charset());
5290
return result_field->val_str(str, &str_value);
5292
return val_str(str);
5296
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
5300
if ((null_value= result_field->is_null()))
5302
return result_field->val_decimal(decimal_value);
5304
return val_decimal(decimal_value);
5308
bool Item_ref::val_bool_result()
5312
if ((null_value= result_field->is_null()))
5314
switch (result_field->result_type()) {
5316
return result_field->val_int() != 0;
5317
case DECIMAL_RESULT:
5319
my_decimal decimal_value;
5320
my_decimal *val= result_field->val_decimal(&decimal_value);
5322
return !my_decimal_is_zero(val);
5327
return result_field->val_real() != 0.0;
5337
double Item_ref::val_real()
5340
double tmp=(*ref)->val_result();
5341
null_value=(*ref)->null_value;
5346
longlong Item_ref::val_int()
5349
longlong tmp=(*ref)->val_int_result();
5350
null_value=(*ref)->null_value;
5355
bool Item_ref::val_bool()
5358
bool tmp= (*ref)->val_bool_result();
5359
null_value= (*ref)->null_value;
5364
String *Item_ref::val_str(String* tmp)
5367
tmp=(*ref)->str_result(tmp);
5368
null_value=(*ref)->null_value;
5373
bool Item_ref::is_null()
5376
return (*ref)->is_null();
5380
bool Item_ref::get_date(MYSQL_TIME *ltime,uint fuzzydate)
5382
return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5386
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
5388
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
5389
null_value= (*ref)->null_value;
5393
int Item_ref::save_in_field(Field *to, bool no_conversions)
5396
DBUG_ASSERT(!result_field);
5397
res= (*ref)->save_in_field(to, no_conversions);
5398
null_value= (*ref)->null_value;
5403
void Item_ref::save_org_in_field(Field *field)
5405
(*ref)->save_org_in_field(field);
5409
void Item_ref::make_field(Send_field *field)
5411
(*ref)->make_field(field);
5412
/* Non-zero in case of a view */
5414
field->col_name= name;
5416
field->table_name= table_name;
5418
field->db_name= db_name;
5422
Item *Item_ref::get_tmp_table_item(THD *thd)
5425
return (*ref)->get_tmp_table_item(thd);
5427
Item_field *item= new Item_field(result_field);
5430
item->table_name= table_name;
5431
item->db_name= db_name;
5437
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
5439
str->append(STRING_WITH_LEN("<ref_null_helper>("));
5441
(*ref)->print(str, query_type);
5448
double Item_direct_ref::val_real()
5450
double tmp=(*ref)->val_real();
5451
null_value=(*ref)->null_value;
5456
longlong Item_direct_ref::val_int()
5458
longlong tmp=(*ref)->val_int();
5459
null_value=(*ref)->null_value;
5464
String *Item_direct_ref::val_str(String* tmp)
5466
tmp=(*ref)->val_str(tmp);
5467
null_value=(*ref)->null_value;
5472
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
5474
my_decimal *tmp= (*ref)->val_decimal(decimal_value);
5475
null_value=(*ref)->null_value;
5480
bool Item_direct_ref::val_bool()
5482
bool tmp= (*ref)->val_bool();
5483
null_value=(*ref)->null_value;
5488
bool Item_direct_ref::is_null()
5490
return (*ref)->is_null();
5494
bool Item_direct_ref::get_date(MYSQL_TIME *ltime,uint fuzzydate)
5496
return (null_value=(*ref)->get_date(ltime,fuzzydate));
5501
Prepare referenced field then call usual Item_direct_ref::fix_fields .
5503
@param thd thread handler
5504
@param reference reference on reference where this item stored
5512
bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference)
5514
/* view fild reference must be defined */
5516
/* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */
5517
if (!(*ref)->fixed &&
5518
((*ref)->fix_fields(thd, ref)))
5520
return Item_direct_ref::fix_fields(thd, reference);
5524
Prepare referenced outer field then call usual Item_direct_ref::fix_fields
5527
Item_outer_ref::fix_fields()
5529
reference reference on reference where this item stored
5536
bool Item_outer_ref::fix_fields(THD *thd, Item **reference)
5539
/* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5540
if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
5542
err= Item_direct_ref::fix_fields(thd, reference);
5545
if ((*ref)->type() == Item::FIELD_ITEM)
5546
table_name= ((Item_field*)outer_ref)->table_name;
5550
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
5552
if (depended_from == new_parent)
5555
outer_ref->fix_after_pullout(new_parent, ref);
5559
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **refptr)
5561
if (depended_from == new_parent)
5563
(*ref)->fix_after_pullout(new_parent, ref);
5564
depended_from= NULL;
5569
Compare two view column references for equality.
5571
A view column reference is considered equal to another column
5572
reference if the second one is a view column and if both column
5573
references resolve to the same item. It is assumed that both
5574
items are of the same type.
5576
@param item item to compare with
5577
@param binary_cmp make binary comparison
5580
true Referenced item is equal to given item
5585
bool Item_direct_view_ref::eq(const Item *item, bool binary_cmp) const
5587
if (item->type() == REF_ITEM)
5589
Item_ref *item_ref= (Item_ref*) item;
5590
if (item_ref->ref_type() == VIEW_REF)
5592
Item *item_ref_ref= *(item_ref->ref);
5593
return ((*ref)->real_item() == item_ref_ref->real_item());
1640
5599
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
1642
return item->type() == DEFAULT_VALUE_ITEM &&
5601
return item->type() == DEFAULT_VALUE_ITEM &&
1643
5602
((Item_default_value *)item)->arg->eq(arg, binary_cmp);
1647
bool Item_default_value::fix_fields(Session *session, Item **)
5606
bool Item_default_value::fix_fields(THD *thd, Item **items)
1649
5608
Item *real_arg;
1650
5609
Item_field *field_arg;
1651
5610
Field *def_field;
5611
DBUG_ASSERT(fixed == 0);
1659
if (!arg->fixed && arg->fix_fields(session, &arg))
5618
if (!arg->fixed && arg->fix_fields(thd, &arg))
1902
5936
return result == field->val_real();
5939
Item_cache* Item_cache::get_cache(const Item *item)
5941
switch (item->result_type()) {
5943
return new Item_cache_int();
5945
return new Item_cache_real();
5946
case DECIMAL_RESULT:
5947
return new Item_cache_decimal();
5949
return new Item_cache_str(item);
5951
return new Item_cache_row();
5953
// should never be in real life
5960
void Item_cache::print(String *str, enum_query_type query_type)
5962
str->append(STRING_WITH_LEN("<cache>("));
5964
example->print(str, query_type);
5966
Item::print(str, query_type);
5971
void Item_cache_int::store(Item *item)
5973
value= item->val_int_result();
5974
null_value= item->null_value;
5975
unsigned_flag= item->unsigned_flag;
5979
void Item_cache_int::store(Item *item, longlong val_arg)
5982
null_value= item->null_value;
5983
unsigned_flag= item->unsigned_flag;
5987
String *Item_cache_int::val_str(String *str)
5989
DBUG_ASSERT(fixed == 1);
5990
str->set(value, default_charset());
5995
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
5997
DBUG_ASSERT(fixed == 1);
5998
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
6003
void Item_cache_real::store(Item *item)
6005
value= item->val_result();
6006
null_value= item->null_value;
6010
longlong Item_cache_real::val_int()
6012
DBUG_ASSERT(fixed == 1);
6013
return (longlong) rint(value);
6017
String* Item_cache_real::val_str(String *str)
6019
DBUG_ASSERT(fixed == 1);
6020
str->set_real(value, decimals, default_charset());
6025
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
6027
DBUG_ASSERT(fixed == 1);
6028
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
6033
void Item_cache_decimal::store(Item *item)
6035
my_decimal *val= item->val_decimal_result(&decimal_value);
6036
if (!(null_value= item->null_value) && val != &decimal_value)
6037
my_decimal2decimal(val, &decimal_value);
6040
double Item_cache_decimal::val_real()
6044
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
6048
longlong Item_cache_decimal::val_int()
6052
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
6056
String* Item_cache_decimal::val_str(String *str)
6059
my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
6061
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
6065
my_decimal *Item_cache_decimal::val_decimal(my_decimal *val)
6068
return &decimal_value;
6072
void Item_cache_str::store(Item *item)
6074
value_buff.set(buffer, sizeof(buffer), item->collation.collation);
6075
value= item->str_result(&value_buff);
6076
if ((null_value= item->null_value))
6078
else if (value != &value_buff)
6081
We copy string value to avoid changing value if 'item' is table field
6082
in queries like following (where t1.c is varchar):
6084
(select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
6085
(select c from t1 where a=t2.a)
6088
value_buff.copy(*value);
6093
double Item_cache_str::val_real()
6095
DBUG_ASSERT(fixed == 1);
6099
return my_strntod(value->charset(), (char*) value->ptr(),
6100
value->length(), &end_not_used, &err_not_used);
6105
longlong Item_cache_str::val_int()
6107
DBUG_ASSERT(fixed == 1);
6110
return my_strntoll(value->charset(), value->ptr(),
6111
value->length(), 10, (char**) 0, &err);
6116
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
6118
DBUG_ASSERT(fixed == 1);
6120
string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
6127
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
6129
int res= Item_cache::save_in_field(field, no_conversions);
6130
return (is_varbinary && field->type() == MYSQL_TYPE_STRING &&
6131
value->length() < field->field_length) ? 1 : res;
6135
bool Item_cache_row::allocate(uint num)
6138
THD *thd= current_thd;
6140
(Item_cache **) thd->calloc(sizeof(Item_cache *)*item_count)));
6144
bool Item_cache_row::setup(Item * item)
6147
if (!values && allocate(item->cols()))
6149
for (uint i= 0; i < item_count; i++)
6151
Item *el= item->element_index(i);
6153
if (!(tmp= values[i]= Item_cache::get_cache(el)))
6161
void Item_cache_row::store(Item * item)
6164
item->bring_value();
6165
for (uint i= 0; i < item_count; i++)
6167
values[i]->store(item->element_index(i));
6168
null_value|= values[i]->null_value;
6173
void Item_cache_row::illegal_method_call(const char *method)
6175
DBUG_ENTER("Item_cache_row::illegal_method_call");
6176
DBUG_PRINT("error", ("!!! %s method was called for row item", method));
6178
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
6183
bool Item_cache_row::check_cols(uint c)
6185
if (c != item_count)
6187
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
6194
bool Item_cache_row::null_inside()
6196
for (uint i= 0; i < item_count; i++)
6198
if (values[i]->cols() > 1)
6200
if (values[i]->null_inside())
6205
values[i]->update_null_value();
6206
if (values[i]->null_value)
6214
void Item_cache_row::bring_value()
6216
for (uint i= 0; i < item_count; i++)
6217
values[i]->bring_value();
6222
Item_type_holder::Item_type_holder(THD *thd, Item *item)
6223
:Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
6225
DBUG_ASSERT(item->fixed);
6226
maybe_null= item->maybe_null;
6227
collation.set(item->collation);
6228
get_full_info(item);
6229
/* fix variable decimals which always is NOT_FIXED_DEC */
6230
if (Field::result_merge_type(fld_type) == INT_RESULT)
6232
prev_decimal_int_part= item->decimal_int_part();
6237
Return expression type of Item_type_holder.
6240
Item_result (type of internal MySQL expression result)
6243
Item_result Item_type_holder::result_type() const
6245
return Field::result_merge_type(fld_type);
6250
Find real field type of item.
6253
type of field which should be created to store item value
6256
enum_field_types Item_type_holder::get_real_type(Item *item)
6258
switch(item->type())
6263
Item_fields::field_type ask Field_type() but sometimes field return
6264
a different type, like for enum/set, so we need to ask real type.
6266
Field *field= ((Item_field *) item)->field;
6267
enum_field_types type= field->real_type();
6268
if (field->is_created_from_null_item)
6269
return MYSQL_TYPE_NULL;
6270
/* work around about varchar type field detection */
6271
if (type == MYSQL_TYPE_STRING && field->type() == MYSQL_TYPE_VAR_STRING)
6272
return MYSQL_TYPE_VAR_STRING;
6278
Argument of aggregate function sometimes should be asked about field
6281
Item_sum *item_sum= (Item_sum *) item;
6282
if (item_sum->keep_field_type())
6283
return get_real_type(item_sum->args[0]);
6287
if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC)
6290
There are work around of problem with changing variable type on the
6291
fly and variable always report "string" as field type to get
6292
acceptable information for client in send_field, so we make field
6293
type from expression type.
6295
switch (item->result_type()) {
6297
return MYSQL_TYPE_VAR_STRING;
6299
return MYSQL_TYPE_LONGLONG;
6301
return MYSQL_TYPE_DOUBLE;
6302
case DECIMAL_RESULT:
6303
return MYSQL_TYPE_NEWDECIMAL;
6307
return MYSQL_TYPE_VAR_STRING;
6314
return item->field_type();
6318
Find field type which can carry current Item_type_holder type and
6321
@param thd thread handler
6322
@param item given item to join its parameters with this item ones
6325
true error - types are incompatible
6330
bool Item_type_holder::join_types(THD *thd, Item *item)
6332
uint max_length_orig= max_length;
6333
uint decimals_orig= decimals;
6334
DBUG_ENTER("Item_type_holder::join_types");
6335
DBUG_PRINT("info:", ("was type %d len %d, dec %d name %s",
6336
fld_type, max_length, decimals,
6337
(name ? name : "<NULL>")));
6338
DBUG_PRINT("info:", ("in type %d len %d, dec %d",
6339
get_real_type(item),
6340
item->max_length, item->decimals));
6341
fld_type= Field::field_type_merge(fld_type, get_real_type(item));
6343
int item_decimals= item->decimals;
6344
/* fix variable decimals which always is NOT_FIXED_DEC */
6345
if (Field::result_merge_type(fld_type) == INT_RESULT)
6347
decimals= max(decimals, item_decimals);
6349
if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
6351
decimals= min(max(decimals, item->decimals), DECIMAL_MAX_SCALE);
6352
int precision= min(max(prev_decimal_int_part, item->decimal_int_part())
6353
+ decimals, DECIMAL_MAX_PRECISION);
6354
unsigned_flag&= item->unsigned_flag;
6355
max_length= my_decimal_precision_to_length(precision, decimals,
6359
switch (Field::result_merge_type(fld_type))
6363
const char *old_cs, *old_derivation;
6364
uint32 old_max_chars= max_length / collation.collation->mbmaxlen;
6365
old_cs= collation.collation->name;
6366
old_derivation= collation.derivation_name();
6367
if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
6369
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
6370
old_cs, old_derivation,
6371
item->collation.collation->name,
6372
item->collation.derivation_name(),
6377
To figure out max_length, we have to take into account possible
6378
expansion of the size of the values because of character set
6381
if (collation.collation != &my_charset_bin)
6383
max_length= max(old_max_chars * collation.collation->mbmaxlen,
6384
display_length(item) /
6385
item->collation.collation->mbmaxlen *
6386
collation.collation->mbmaxlen);
6389
set_if_bigger(max_length, display_length(item));
6394
if (decimals != NOT_FIXED_DEC)
6396
int delta1= max_length_orig - decimals_orig;
6397
int delta2= item->max_length - item->decimals;
6398
max_length= max(delta1, delta2) + decimals;
6399
if (fld_type == MYSQL_TYPE_FLOAT && max_length > FLT_DIG + 2)
6401
max_length= FLT_DIG + 6;
6402
decimals= NOT_FIXED_DEC;
6404
if (fld_type == MYSQL_TYPE_DOUBLE && max_length > DBL_DIG + 2)
6406
max_length= DBL_DIG + 7;
6407
decimals= NOT_FIXED_DEC;
6411
max_length= (fld_type == MYSQL_TYPE_FLOAT) ? FLT_DIG+6 : DBL_DIG+7;
6415
max_length= max(max_length, display_length(item));
6417
maybe_null|= item->maybe_null;
6418
get_full_info(item);
6420
/* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
6421
prev_decimal_int_part= decimal_int_part();
6422
DBUG_PRINT("info", ("become type: %d len: %u dec: %u",
6423
(int) fld_type, max_length, (uint) decimals));
6428
Calculate lenth for merging result for given Item type.
6430
@param item Item for length detection
6436
uint32 Item_type_holder::display_length(Item *item)
6438
if (item->type() == Item::FIELD_ITEM)
6439
return ((Item_field *)item)->max_disp_length();
6441
switch (item->field_type())
6443
case MYSQL_TYPE_DECIMAL:
6444
case MYSQL_TYPE_TIMESTAMP:
6445
case MYSQL_TYPE_DATE:
6446
case MYSQL_TYPE_TIME:
6447
case MYSQL_TYPE_DATETIME:
6448
case MYSQL_TYPE_YEAR:
6449
case MYSQL_TYPE_NEWDATE:
6450
case MYSQL_TYPE_VARCHAR:
6451
case MYSQL_TYPE_NEWDECIMAL:
6452
case MYSQL_TYPE_ENUM:
6453
case MYSQL_TYPE_SET:
6454
case MYSQL_TYPE_TINY_BLOB:
6455
case MYSQL_TYPE_MEDIUM_BLOB:
6456
case MYSQL_TYPE_LONG_BLOB:
6457
case MYSQL_TYPE_BLOB:
6458
case MYSQL_TYPE_VAR_STRING:
6459
case MYSQL_TYPE_STRING:
6460
case MYSQL_TYPE_TINY:
6462
case MYSQL_TYPE_SHORT:
6464
case MYSQL_TYPE_LONG:
6465
return MY_INT32_NUM_DECIMAL_DIGITS;
6466
case MYSQL_TYPE_FLOAT:
6468
case MYSQL_TYPE_DOUBLE:
6470
case MYSQL_TYPE_NULL:
6472
case MYSQL_TYPE_LONGLONG:
6475
DBUG_ASSERT(0); // we should never go there
6482
Make temporary table field according collected information about type
6485
@param table temporary table for which we create fields
6491
Field *Item_type_holder::make_field_by_type(TABLE *table)
6494
The field functions defines a field to be not null if null_ptr is not 0
6496
uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
6500
case MYSQL_TYPE_ENUM:
6501
DBUG_ASSERT(enum_set_typelib);
6502
field= new Field_enum((uchar *) 0, max_length, null_ptr, 0,
6504
get_enum_pack_length(enum_set_typelib->count),
6505
enum_set_typelib, collation.collation);
6509
case MYSQL_TYPE_SET:
6510
DBUG_ASSERT(enum_set_typelib);
6511
field= new Field_set((uchar *) 0, max_length, null_ptr, 0,
6513
get_set_pack_length(enum_set_typelib->count),
6514
enum_set_typelib, collation.collation);
6518
case MYSQL_TYPE_NULL:
6519
return make_string_field(table);
6523
return tmp_table_field_from_field_type(table, 0);
6528
Get full information from Item about enum/set fields to be able to create
6531
@param item Item for information collection
6533
void Item_type_holder::get_full_info(Item *item)
6535
if (fld_type == MYSQL_TYPE_ENUM ||
6536
fld_type == MYSQL_TYPE_SET)
6538
if (item->type() == Item::SUM_FUNC_ITEM &&
6539
(((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
6540
((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC))
6541
item = ((Item_sum*)item)->args[0];
6543
We can have enum/set type after merging only if we have one enum|set
6544
field (or MIN|MAX(enum|set field)) and number of NULL fields
6546
DBUG_ASSERT((enum_set_typelib &&
6547
get_real_type(item) == MYSQL_TYPE_NULL) ||
6548
(!enum_set_typelib &&
6549
item->type() == Item::FIELD_ITEM &&
6550
(get_real_type(item) == MYSQL_TYPE_ENUM ||
6551
get_real_type(item) == MYSQL_TYPE_SET) &&
6552
((Field_enum*)((Item_field *) item)->field)->typelib));
6553
if (!enum_set_typelib)
6555
enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib;
6561
double Item_type_holder::val_real()
6563
DBUG_ASSERT(0); // should never be called
6568
longlong Item_type_holder::val_int()
6570
DBUG_ASSERT(0); // should never be called
6574
my_decimal *Item_type_holder::val_decimal(my_decimal *)
6576
DBUG_ASSERT(0); // should never be called
6580
String *Item_type_holder::val_str(String*)
6582
DBUG_ASSERT(0); // should never be called
6586
void Item_result_field::cleanup()
6588
DBUG_ENTER("Item_result_field::cleanup()");
1906
6595
Dummy error processor used by default by Name_resolution_context.