28
28
const String my_null_string("NULL", 4, default_charset_info);
30
/****************************************************************************/
32
/* Hybrid_type_traits {_real} */
34
void Hybrid_type_traits::fix_length_and_dec(Item *item, Item *arg) const
36
item->decimals= NOT_FIXED_DEC;
37
item->max_length= item->float_length(arg->decimals);
40
static const Hybrid_type_traits real_traits_instance;
42
const Hybrid_type_traits *Hybrid_type_traits::instance()
44
return &real_traits_instance;
47
int64_t Hybrid_type_traits::val_int(Hybrid_type *val,
50
return (int64_t) rint(val->real);
54
Hybrid_type_traits::val_decimal(Hybrid_type *val, my_decimal *) const
56
double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
62
Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8_t decimals) const
64
to->set_real(val->real, decimals, &my_charset_bin);
68
/* Hybrid_type_traits_decimal */
69
static const Hybrid_type_traits_decimal decimal_traits_instance;
71
const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
73
return &decimal_traits_instance;
78
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
80
item->decimals= arg->decimals;
81
item->max_length= cmin(arg->max_length + DECIMAL_LONGLONG_DIGITS,
82
(unsigned int)DECIMAL_MAX_STR_LENGTH);
86
void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
88
my_decimal_set_zero(&val->dec_buf[0]);
89
val->used_dec_buf_no= 0;
93
void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
95
my_decimal_add(E_DEC_FATAL_ERROR,
96
&val->dec_buf[val->used_dec_buf_no ^ 1],
97
&val->dec_buf[val->used_dec_buf_no],
98
f->val_decimal(&val->dec_buf[2]));
99
val->used_dec_buf_no^= 1;
105
what is '4' for scale?
107
void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
109
int2my_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
110
/* XXX: what is '4' for scale? */
111
my_decimal_div(E_DEC_FATAL_ERROR,
112
&val->dec_buf[val->used_dec_buf_no ^ 1],
113
&val->dec_buf[val->used_dec_buf_no],
114
&val->dec_buf[2], 4);
115
val->used_dec_buf_no^= 1;
120
Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
123
my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
124
unsigned_flag, &result);
130
Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
132
my_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
139
Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
140
uint8_t decimals) const
142
my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
143
decimals, false, &val->dec_buf[2]);
144
my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to);
148
/* Hybrid_type_traits_integer */
149
static const Hybrid_type_traits_integer integer_traits_instance;
151
const Hybrid_type_traits_integer *Hybrid_type_traits_integer::instance()
153
return &integer_traits_instance;
157
Hybrid_type_traits_integer::fix_length_and_dec(Item *item, Item *) const
160
item->max_length= MY_INT64_NUM_DECIMAL_DIGITS;
161
item->unsigned_flag= 0;
164
31
/*****************************************************************************
1084
left_is_superset(DTCollation *left, DTCollation *right)
1086
/* Allow convert to Unicode */
1087
if (left->collation->state & MY_CS_UNICODE &&
1088
(left->derivation < right->derivation ||
1089
(left->derivation == right->derivation &&
1090
!(right->collation->state & MY_CS_UNICODE))))
1092
/* Allow convert from ASCII */
1093
if (right->repertoire == MY_REPERTOIRE_ASCII &&
1094
(left->derivation < right->derivation ||
1095
(left->derivation == right->derivation &&
1096
!(left->repertoire == MY_REPERTOIRE_ASCII))))
1098
/* Disallow conversion otherwise */
1103
Aggregate two collations together taking
1104
into account their coercibility (aka derivation):.
1106
0 == DERIVATION_EXPLICIT - an explicitly written COLLATE clause @n
1107
1 == DERIVATION_NONE - a mix of two different collations @n
1108
2 == DERIVATION_IMPLICIT - a column @n
1109
3 == DERIVATION_COERCIBLE - a string constant.
1111
The most important rules are:
1112
-# If collations are the same:
1113
chose this collation, and the strongest derivation.
1114
-# If collations are different:
1115
- Character sets may differ, but only if conversion without
1116
data loss is possible. The caller provides flags whether
1117
character set conversion attempts should be done. If no
1118
flags are substituted, then the character sets must be the same.
1119
Currently processed flags are:
1120
MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
1121
MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
1122
- two EXPLICIT collations produce an error, e.g. this is wrong:
1123
CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
1124
- the side with smaller derivation value wins,
1125
i.e. a column is stronger than a string constant,
1126
an explicit COLLATE clause is stronger than a column.
1127
- if derivations are the same, we have DERIVATION_NONE,
1128
we'll wait for an explicit COLLATE clause which possibly can
1129
come from another argument later: for example, this is valid,
1130
but we don't know yet when collecting the first two arguments:
1132
CONCAT(latin1_swedish_ci_column,
1133
latin1_german1_ci_column,
1134
expr COLLATE latin1_german2_ci)
1138
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
1140
if (!my_charset_same(collation, dt.collation))
1143
We do allow to use binary strings (like BLOBS)
1144
together with character strings.
1145
Binaries have more precedence than a character
1146
string of the same derivation.
1148
if (collation == &my_charset_bin)
1150
if (derivation <= dt.derivation)
1157
else if (dt.collation == &my_charset_bin)
1159
if (dt.derivation <= derivation)
1168
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1169
left_is_superset(this, &dt))
1173
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1174
left_is_superset(&dt, this))
1178
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1179
derivation < dt.derivation &&
1180
dt.derivation >= DERIVATION_SYSCONST)
1184
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1185
dt.derivation < derivation &&
1186
derivation >= DERIVATION_SYSCONST)
1192
// Cannot apply conversion
1193
set(0, DERIVATION_NONE, 0);
1197
else if (derivation < dt.derivation)
1201
else if (dt.derivation < derivation)
1207
if (collation == dt.collation)
1213
if (derivation == DERIVATION_EXPLICIT)
1215
set(0, DERIVATION_NONE, 0);
1218
if (collation->state & MY_CS_BINSORT)
1220
if (dt.collation->state & MY_CS_BINSORT)
1225
const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname,
1226
MY_CS_BINSORT,MYF(0));
1227
set(bin, DERIVATION_NONE);
1230
repertoire|= dt.repertoire;
1234
/******************************/
1236
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname)
1238
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
1239
c1.collation->name,c1.derivation_name(),
1240
c2.collation->name,c2.derivation_name(),
1246
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
1249
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
1250
c1.collation->name,c1.derivation_name(),
1251
c2.collation->name,c2.derivation_name(),
1252
c3.collation->name,c3.derivation_name(),
1258
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
1262
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
1263
else if (count == 3)
1264
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
1265
args[2*item_sep]->collation, fname);
1267
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
1271
bool agg_item_collations(DTCollation &c, const char *fname,
1272
Item **av, uint32_t count, uint32_t flags, int item_sep)
1276
c.set(av[0]->collation);
1277
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1279
if (c.aggregate((*arg)->collation, flags))
1281
my_coll_agg_error(av, count, fname, item_sep);
1285
if ((flags & MY_COLL_DISALLOW_NONE) &&
1286
c.derivation == DERIVATION_NONE)
1288
my_coll_agg_error(av, count, fname, item_sep);
1295
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1296
Item **av, uint32_t count, uint32_t flags)
1298
return (agg_item_collations(c, fname, av, count,
1299
flags | MY_COLL_DISALLOW_NONE, 1));
1304
Collect arguments' character sets together.
1306
We allow to apply automatic character set conversion in some cases.
1307
The conditions when conversion is possible are:
1308
- arguments A and B have different charsets
1309
- A wins according to coercibility rules
1310
(i.e. a column is stronger than a string constant,
1311
an explicit COLLATE clause is stronger than a column)
1312
- character set of A is either superset for character set of B,
1313
or B is a string constant which can be converted into the
1314
character set of A without data loss.
1316
If all of the above is true, then it's possible to convert
1317
B into the character set of A, and then compare according
1318
to the collation of A.
1320
For functions with more than two arguments:
1322
collect(A,B,C) ::= collect(collect(A,B),C)
1324
Since this function calls Session::change_item_tree() on the passed Item **
1325
pointers, it is necessary to pass the original Item **'s, not copies.
1326
Otherwise their values will not be properly restored (see BUG#20769).
1327
If the items are not consecutive (eg. args[2] and args[5]), use the
1328
item_sep argument, ie.
1330
agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
1334
bool agg_item_charsets(DTCollation &coll, const char *fname,
1335
Item **args, uint32_t nargs, uint32_t flags, int item_sep)
1337
Item **arg, *safe_args[2];
1339
memset(safe_args, 0, sizeof(safe_args));
1341
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
1345
For better error reporting: save the first and the second argument.
1346
We need this only if the the number of args is 3 or 2:
1347
- for a longer argument list, "Illegal mix of collations"
1348
doesn't display each argument's characteristics.
1349
- if nargs is 1, then this error cannot happen.
1351
if (nargs >=2 && nargs <= 3)
1353
safe_args[0]= args[0];
1354
safe_args[1]= args[item_sep];
1357
Session *session= current_session;
1361
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1364
uint32_t dummy_offset;
1365
if (!String::needs_conversion(0, (*arg)->collation.collation,
1370
if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
1371
((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
1372
conv= new Item_func_conv_charset(*arg, coll.collation, 1);
1376
if (nargs >=2 && nargs <= 3)
1378
/* restore the original arguments for better error message */
1379
args[0]= safe_args[0];
1380
args[item_sep]= safe_args[1];
1382
my_coll_agg_error(args, nargs, fname, item_sep);
1384
break; // we cannot return here, we need to restore "arena".
1386
if ((*arg)->type() == Item::FIELD_ITEM)
1387
((Item_field *)(*arg))->no_const_subst= 1;
1389
If in statement prepare, then we create a converter for two
1390
constant items, do it once and then reuse it.
1391
If we're in execution of a prepared statement, arena is NULL,
1392
and the conv was created in runtime memory. This can be
1393
the case only if the argument is a parameter marker ('?'),
1394
because for all true constants the charset converter has already
1395
been created in prepare. In this case register the change for
1398
session->change_item_tree(arg, conv);
1400
We do not check conv->fixed, because Item_func_conv_charset which can
1401
be return by safe_charset_converter can't be fixed at creation
1403
conv->fix_fields(session, arg);
1410
950
void Item_ident_for_show::make_field(Send_field *tmp_field)
1412
952
tmp_field->table_name= tmp_field->org_table_name= table_name;