1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <drizzled/server_includes.h>
24
#include <drizzled/sql_select.h>
25
#include <drizzled/error.h>
26
#include <drizzled/show.h>
27
#include <drizzled/item/cmpfunc.h>
28
#include <drizzled/item/cache_row.h>
29
#include <drizzled/item/type_holder.h>
30
#include <drizzled/item/sum.h>
31
#include <drizzled/functions/str/conv_charset.h>
32
#include <drizzled/virtual_column_info.h>
33
#include <drizzled/sql_base.h>
36
#include <drizzled/field/str.h>
37
#include <drizzled/field/longstr.h>
38
#include <drizzled/field/num.h>
39
#include <drizzled/field/blob.h>
40
#include <drizzled/field/enum.h>
41
#include <drizzled/field/null.h>
42
#include <drizzled/field/date.h>
43
#include <drizzled/field/fdecimal.h>
44
#include <drizzled/field/real.h>
45
#include <drizzled/field/double.h>
46
#include <drizzled/field/long.h>
47
#include <drizzled/field/int64_t.h>
48
#include <drizzled/field/num.h>
49
#include <drizzled/field/timetype.h>
50
#include <drizzled/field/timestamp.h>
51
#include <drizzled/field/datetime.h>
52
#include <drizzled/field/fstring.h>
53
#include <drizzled/field/varstring.h>
56
#if defined(CMATH_NAMESPACE)
57
using namespace CMATH_NAMESPACE;
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
#ifdef USE_PRAGMA_IMPLEMENTATION
18
#pragma implementation // gcc: Class implementation
20
#include "mysql_priv.h"
21
#include "sql_select.h"
22
#include <drizzled/drizzled_error_messages.h>
60
24
const String my_null_string("NULL", 4, default_charset_info);
63
const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;
26
/****************************************************************************/
28
/* Hybrid_type_traits {_real} */
30
void Hybrid_type_traits::fix_length_and_dec(Item *item, Item *arg) const
32
item->decimals= NOT_FIXED_DEC;
33
item->max_length= item->float_length(arg->decimals);
36
static const Hybrid_type_traits real_traits_instance;
38
const Hybrid_type_traits *Hybrid_type_traits::instance()
40
return &real_traits_instance;
45
Hybrid_type_traits::val_decimal(Hybrid_type *val,
46
my_decimal *to __attribute__((unused))) const
48
double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
54
Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8_t decimals) const
56
to->set_real(val->real, decimals, &my_charset_bin);
60
/* Hybrid_type_traits_decimal */
61
static const Hybrid_type_traits_decimal decimal_traits_instance;
63
const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
65
return &decimal_traits_instance;
70
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
72
item->decimals= arg->decimals;
73
item->max_length= min(arg->max_length + DECIMAL_LONGLONG_DIGITS,
74
DECIMAL_MAX_STR_LENGTH);
78
void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
80
my_decimal_set_zero(&val->dec_buf[0]);
81
val->used_dec_buf_no= 0;
85
void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
87
my_decimal_add(E_DEC_FATAL_ERROR,
88
&val->dec_buf[val->used_dec_buf_no ^ 1],
89
&val->dec_buf[val->used_dec_buf_no],
90
f->val_decimal(&val->dec_buf[2]));
91
val->used_dec_buf_no^= 1;
97
what is '4' for scale?
99
void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
101
int2my_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
102
/* XXX: what is '4' for scale? */
103
my_decimal_div(E_DEC_FATAL_ERROR,
104
&val->dec_buf[val->used_dec_buf_no ^ 1],
105
&val->dec_buf[val->used_dec_buf_no],
106
&val->dec_buf[2], 4);
107
val->used_dec_buf_no^= 1;
112
Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
115
my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
116
unsigned_flag, &result);
122
Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
124
my_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
131
Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
132
uint8_t decimals) const
134
my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
135
decimals, false, &val->dec_buf[2]);
136
my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to);
140
/* Hybrid_type_traits_integer */
141
static const Hybrid_type_traits_integer integer_traits_instance;
143
const Hybrid_type_traits_integer *Hybrid_type_traits_integer::instance()
145
return &integer_traits_instance;
149
Hybrid_type_traits_integer::fix_length_and_dec(Item *item,
150
Item *arg __attribute__((unused))) const
153
item->max_length= MY_INT64_NUM_DECIMAL_DIGITS;
154
item->unsigned_flag= 0;
66
157
/*****************************************************************************
902
929
if (!(res=val_str(&tmp)) ||
903
930
str_to_time_with_warn(res->ptr(), res->length(), ltime))
905
memset(ltime, 0, sizeof(*ltime));
932
memset((char*) ltime, 0, sizeof(*ltime));
912
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
914
return get_date(ltime,fuzzydate);
924
void Item::update_null_value ()
930
void Item::top_level_item(void)
934
void Item::set_result_field(Field *)
938
bool Item::is_result_field(void)
944
bool Item::is_bool_func(void)
950
void Item::save_in_result_field(bool)
954
void Item::no_rows_in_result(void)
958
Item *Item::copy_or_same(Session *)
964
Item *Item::copy_andor_structure(Session *)
970
Item *Item::real_item(void)
976
Item *Item::get_tmp_table_item(Session *session)
978
return copy_or_same(session);
982
const CHARSET_INFO *Item::default_charset()
984
return current_session->variables.collation_connection;
988
const CHARSET_INFO *Item::compare_collation()
994
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
996
return (this->*processor)(arg);
1000
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
1001
Item_transformer transformer, unsigned char *arg_t)
1003
if ((this->*analyzer) (arg_p))
1004
return ((this->*transformer) (arg_t));
1009
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
1011
(*traverser)(this, arg);
1015
bool Item::remove_dependence_processor(unsigned char *)
1021
bool Item::remove_fixed(unsigned char *)
1028
bool Item::collect_item_field_processor(unsigned char *)
1034
bool Item::find_item_in_field_list_processor(unsigned char *)
1040
bool Item::change_context_processor(unsigned char *)
1045
bool Item::reset_query_id_processor(unsigned char *)
1051
bool Item::register_field_in_read_map(unsigned char *)
1057
bool Item::register_field_in_bitmap(unsigned char *)
1063
bool Item::subst_argument_checker(unsigned char **arg)
1071
bool Item::check_vcol_func_processor(unsigned char *)
1077
Item *Item::equal_fields_propagator(unsigned char *)
1083
bool Item::set_no_const_sub(unsigned char *)
1089
Item *Item::replace_equal_field(unsigned char *)
1095
Item *Item::this_item(void)
1101
const Item *Item::this_item(void) const
1107
Item **Item::this_item_addr(Session *, Item **addr_arg)
1113
uint32_t Item::cols()
1119
Item* Item::element_index(uint32_t)
1125
Item** Item::addr(uint32_t)
1131
bool Item::null_inside()
1137
void Item::bring_value()
1141
Item_field *Item::filed_for_view_update()
1146
Item *Item::neg_transformer(Session *)
1152
Item *Item::update_value_transformer(unsigned char *)
1158
void Item::delete_self()
1164
bool Item::result_as_int64_t()
1170
bool Item::is_expensive()
1172
if (is_expensive_cache < 0)
1173
is_expensive_cache= walk(&Item::is_expensive_processor, 0,
1175
return test(is_expensive_cache);
938
CHARSET_INFO *Item::default_charset()
940
return current_thd->variables.collation_connection;
945
Save value in field, but don't give any warnings
948
This is used to temporary store and retrieve a value in a column,
949
for example in opt_range to adjust the key value to fit the column.
1179
952
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
1182
Table *table= field->table;
1183
Session *session= table->in_use;
1184
enum_check_fields tmp= session->count_cuted_fields;
1185
ulong sql_mode= session->variables.sql_mode;
1186
session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
1187
session->count_cuted_fields= CHECK_FIELD_IGNORE;
955
TABLE *table= field->table;
956
THD *thd= table->in_use;
957
enum_check_fields tmp= thd->count_cuted_fields;
958
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
959
ulong sql_mode= thd->variables.sql_mode;
960
thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
961
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1188
962
res= save_in_field(field, no_conversions);
1189
session->count_cuted_fields= tmp;
1190
session->variables.sql_mode= sql_mode;
963
thd->count_cuted_fields= tmp;
964
dbug_tmp_restore_column_map(table->write_set, old_map);
965
thd->variables.sql_mode= sql_mode;
1264
1040
Item_ref to allow fields from view being stored in tmp table.
1266
1042
Item_aggregate_ref *item_ref;
1267
uint32_t el= fields.elements;
1043
uint el= fields.elements;
1268
1044
Item *real_itm= real_item();
1270
1046
ref_pointer_array[el]= real_itm;
1271
if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
1047
if (!(item_ref= new Item_aggregate_ref(&thd->lex->current_select->context,
1272
1048
ref_pointer_array + el, 0, name)))
1273
1049
return; // fatal_error is set
1274
1050
if (type() == SUM_FUNC_ITEM)
1275
1051
item_ref->depended_from= ((Item_sum *) this)->depended_from();
1276
1052
fields.push_front(real_itm);
1277
session->change_item_tree(ref, item_ref);
1053
thd->change_item_tree(ref, item_ref);
1059
left_is_superset(DTCollation *left, DTCollation *right)
1061
/* Allow convert to Unicode */
1062
if (left->collation->state & MY_CS_UNICODE &&
1063
(left->derivation < right->derivation ||
1064
(left->derivation == right->derivation &&
1065
!(right->collation->state & MY_CS_UNICODE))))
1067
/* Allow convert from ASCII */
1068
if (right->repertoire == MY_REPERTOIRE_ASCII &&
1069
(left->derivation < right->derivation ||
1070
(left->derivation == right->derivation &&
1071
!(left->repertoire == MY_REPERTOIRE_ASCII))))
1073
/* Disallow conversion otherwise */
1078
Aggregate two collations together taking
1079
into account their coercibility (aka derivation):.
1081
0 == DERIVATION_EXPLICIT - an explicitly written COLLATE clause @n
1082
1 == DERIVATION_NONE - a mix of two different collations @n
1083
2 == DERIVATION_IMPLICIT - a column @n
1084
3 == DERIVATION_COERCIBLE - a string constant.
1086
The most important rules are:
1087
-# If collations are the same:
1088
chose this collation, and the strongest derivation.
1089
-# If collations are different:
1090
- Character sets may differ, but only if conversion without
1091
data loss is possible. The caller provides flags whether
1092
character set conversion attempts should be done. If no
1093
flags are substituted, then the character sets must be the same.
1094
Currently processed flags are:
1095
MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
1096
MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
1097
- two EXPLICIT collations produce an error, e.g. this is wrong:
1098
CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
1099
- the side with smaller derivation value wins,
1100
i.e. a column is stronger than a string constant,
1101
an explicit COLLATE clause is stronger than a column.
1102
- if derivations are the same, we have DERIVATION_NONE,
1103
we'll wait for an explicit COLLATE clause which possibly can
1104
come from another argument later: for example, this is valid,
1105
but we don't know yet when collecting the first two arguments:
1107
CONCAT(latin1_swedish_ci_column,
1108
latin1_german1_ci_column,
1109
expr COLLATE latin1_german2_ci)
1113
bool DTCollation::aggregate(DTCollation &dt, uint flags)
1115
if (!my_charset_same(collation, dt.collation))
1118
We do allow to use binary strings (like BLOBS)
1119
together with character strings.
1120
Binaries have more precedence than a character
1121
string of the same derivation.
1123
if (collation == &my_charset_bin)
1125
if (derivation <= dt.derivation)
1132
else if (dt.collation == &my_charset_bin)
1134
if (dt.derivation <= derivation)
1143
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1144
left_is_superset(this, &dt))
1148
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1149
left_is_superset(&dt, this))
1153
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1154
derivation < dt.derivation &&
1155
dt.derivation >= DERIVATION_SYSCONST)
1159
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1160
dt.derivation < derivation &&
1161
derivation >= DERIVATION_SYSCONST)
1167
// Cannot apply conversion
1168
set(0, DERIVATION_NONE, 0);
1172
else if (derivation < dt.derivation)
1176
else if (dt.derivation < derivation)
1182
if (collation == dt.collation)
1188
if (derivation == DERIVATION_EXPLICIT)
1190
set(0, DERIVATION_NONE, 0);
1193
if (collation->state & MY_CS_BINSORT)
1195
if (dt.collation->state & MY_CS_BINSORT)
1200
CHARSET_INFO *bin= get_charset_by_csname(collation->csname,
1201
MY_CS_BINSORT,MYF(0));
1202
set(bin, DERIVATION_NONE);
1205
repertoire|= dt.repertoire;
1209
/******************************/
1211
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname)
1213
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
1214
c1.collation->name,c1.derivation_name(),
1215
c2.collation->name,c2.derivation_name(),
1221
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
1224
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
1225
c1.collation->name,c1.derivation_name(),
1226
c2.collation->name,c2.derivation_name(),
1227
c3.collation->name,c3.derivation_name(),
1233
void my_coll_agg_error(Item** args, uint count, const char *fname,
1237
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
1238
else if (count == 3)
1239
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
1240
args[2*item_sep]->collation, fname);
1242
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
1246
bool agg_item_collations(DTCollation &c, const char *fname,
1247
Item **av, uint count, uint flags, int item_sep)
1251
c.set(av[0]->collation);
1252
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1254
if (c.aggregate((*arg)->collation, flags))
1256
my_coll_agg_error(av, count, fname, item_sep);
1260
if ((flags & MY_COLL_DISALLOW_NONE) &&
1261
c.derivation == DERIVATION_NONE)
1263
my_coll_agg_error(av, count, fname, item_sep);
1270
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1271
Item **av, uint count, uint flags)
1273
return (agg_item_collations(c, fname, av, count,
1274
flags | MY_COLL_DISALLOW_NONE, 1));
1279
Collect arguments' character sets together.
1281
We allow to apply automatic character set conversion in some cases.
1282
The conditions when conversion is possible are:
1283
- arguments A and B have different charsets
1284
- A wins according to coercibility rules
1285
(i.e. a column is stronger than a string constant,
1286
an explicit COLLATE clause is stronger than a column)
1287
- character set of A is either superset for character set of B,
1288
or B is a string constant which can be converted into the
1289
character set of A without data loss.
1291
If all of the above is true, then it's possible to convert
1292
B into the character set of A, and then compare according
1293
to the collation of A.
1295
For functions with more than two arguments:
1297
collect(A,B,C) ::= collect(collect(A,B),C)
1299
Since this function calls THD::change_item_tree() on the passed Item **
1300
pointers, it is necessary to pass the original Item **'s, not copies.
1301
Otherwise their values will not be properly restored (see BUG#20769).
1302
If the items are not consecutive (eg. args[2] and args[5]), use the
1303
item_sep argument, ie.
1305
agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
1309
bool agg_item_charsets(DTCollation &coll, const char *fname,
1310
Item **args, uint nargs, uint flags, int item_sep)
1312
Item **arg, *safe_args[2];
1314
memset(safe_args, 0, sizeof(safe_args));
1316
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
1320
For better error reporting: save the first and the second argument.
1321
We need this only if the the number of args is 3 or 2:
1322
- for a longer argument list, "Illegal mix of collations"
1323
doesn't display each argument's characteristics.
1324
- if nargs is 1, then this error cannot happen.
1326
if (nargs >=2 && nargs <= 3)
1328
safe_args[0]= args[0];
1329
safe_args[1]= args[item_sep];
1332
THD *thd= current_thd;
1333
Query_arena *arena, backup;
1337
In case we're in statement prepare, create conversion item
1338
in its memory: it will be reused on each execute.
1342
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1345
uint32_t dummy_offset;
1346
if (!String::needs_conversion(0, (*arg)->collation.collation,
1351
if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
1352
((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
1353
conv= new Item_func_conv_charset(*arg, coll.collation, 1);
1357
if (nargs >=2 && nargs <= 3)
1359
/* restore the original arguments for better error message */
1360
args[0]= safe_args[0];
1361
args[item_sep]= safe_args[1];
1363
my_coll_agg_error(args, nargs, fname, item_sep);
1365
break; // we cannot return here, we need to restore "arena".
1367
if ((*arg)->type() == Item::FIELD_ITEM)
1368
((Item_field *)(*arg))->no_const_subst= 1;
1370
If in statement prepare, then we create a converter for two
1371
constant items, do it once and then reuse it.
1372
If we're in execution of a prepared statement, arena is NULL,
1373
and the conv was created in runtime memory. This can be
1374
the case only if the argument is a parameter marker ('?'),
1375
because for all true constants the charset converter has already
1376
been created in prepare. In this case register the change for
1379
thd->change_item_tree(arg, conv);
1381
We do not check conv->fixed, because Item_func_conv_charset which can
1382
be return by safe_charset_converter can't be fixed at creation
1384
conv->fix_fields(thd, arg);
1387
thd->restore_active_arena(arena, &backup);
4031
4081
\# Created field
4034
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
4084
Field *Item::tmp_table_field_from_field_type(TABLE *table, bool fixed_length __attribute__((unused)))
4037
4087
The field functions defines a field to be not null if null_ptr is not 0
4039
unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
4089
uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
4042
4092
switch (field_type()) {
4043
4093
case DRIZZLE_TYPE_NEWDECIMAL:
4044
field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
4094
field= new Field_new_decimal((uchar*) 0, max_length, null_ptr, 0,
4045
4095
Field::NONE, name, decimals, 0,
4046
4096
unsigned_flag);
4098
case DRIZZLE_TYPE_TINY:
4099
field= new Field_tiny((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4100
name, 0, unsigned_flag);
4102
case DRIZZLE_TYPE_SHORT:
4103
field= new Field_short((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4104
name, 0, unsigned_flag);
4048
4106
case DRIZZLE_TYPE_LONG:
4049
field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4107
field= new Field_long((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4050
4108
name, 0, unsigned_flag);
4052
4110
case DRIZZLE_TYPE_LONGLONG:
4053
field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4111
field= new Field_int64_t((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4054
4112
name, 0, unsigned_flag);
4056
4114
case DRIZZLE_TYPE_DOUBLE:
4057
field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4115
field= new Field_double((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4058
4116
name, decimals, 0, unsigned_flag);
4060
4118
case DRIZZLE_TYPE_NULL:
4061
field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
4119
field= new Field_null((uchar*) 0, max_length, Field::NONE,
4062
4120
name, &my_charset_bin);
4064
case DRIZZLE_TYPE_DATE:
4065
field= new Field_date(maybe_null, name, &my_charset_bin);
4122
case DRIZZLE_TYPE_NEWDATE:
4123
field= new Field_newdate(maybe_null, name, &my_charset_bin);
4067
4125
case DRIZZLE_TYPE_TIME:
4068
4126
field= new Field_time(maybe_null, name, &my_charset_bin);
6069
bool Item_cache_row::allocate(uint num)
6072
THD *thd= current_thd;
6074
(Item_cache **) thd->calloc(sizeof(Item_cache *)*item_count)));
6078
bool Item_cache_row::setup(Item * item)
6081
if (!values && allocate(item->cols()))
6083
for (uint i= 0; i < item_count; i++)
6085
Item *el= item->element_index(i);
6087
if (!(tmp= values[i]= Item_cache::get_cache(el)))
6095
void Item_cache_row::store(Item * item)
6098
item->bring_value();
6099
for (uint i= 0; i < item_count; i++)
6101
values[i]->store(item->element_index(i));
6102
null_value|= values[i]->null_value;
6107
void Item_cache_row::illegal_method_call(const char *method __attribute__((unused)))
6110
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
6115
bool Item_cache_row::check_cols(uint c)
6117
if (c != item_count)
6119
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
6126
bool Item_cache_row::null_inside()
6128
for (uint i= 0; i < item_count; i++)
6130
if (values[i]->cols() > 1)
6132
if (values[i]->null_inside())
6137
values[i]->update_null_value();
6138
if (values[i]->null_value)
6146
void Item_cache_row::bring_value()
6148
for (uint i= 0; i < item_count; i++)
6149
values[i]->bring_value();
6154
Item_type_holder::Item_type_holder(THD *thd, Item *item)
6155
:Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
6157
assert(item->fixed);
6158
maybe_null= item->maybe_null;
6159
collation.set(item->collation);
6160
get_full_info(item);
6161
/* fix variable decimals which always is NOT_FIXED_DEC */
6162
if (Field::result_merge_type(fld_type) == INT_RESULT)
6164
prev_decimal_int_part= item->decimal_int_part();
6169
Return expression type of Item_type_holder.
6172
Item_result (type of internal MySQL expression result)
6175
Item_result Item_type_holder::result_type() const
6177
return Field::result_merge_type(fld_type);
6182
Find real field type of item.
6185
type of field which should be created to store item value
6188
enum_field_types Item_type_holder::get_real_type(Item *item)
6190
switch(item->type())
6195
Item_fields::field_type ask Field_type() but sometimes field return
6196
a different type, like for enum/set, so we need to ask real type.
6198
Field *field= ((Item_field *) item)->field;
6199
enum_field_types type= field->real_type();
6200
if (field->is_created_from_null_item)
6201
return DRIZZLE_TYPE_NULL;
6207
Argument of aggregate function sometimes should be asked about field
6210
Item_sum *item_sum= (Item_sum *) item;
6211
if (item_sum->keep_field_type())
6212
return get_real_type(item_sum->args[0]);
6216
if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC)
6219
There are work around of problem with changing variable type on the
6220
fly and variable always report "string" as field type to get
6221
acceptable information for client in send_field, so we make field
6222
type from expression type.
6224
switch (item->result_type()) {
6226
return DRIZZLE_TYPE_VARCHAR;
6228
return DRIZZLE_TYPE_LONGLONG;
6230
return DRIZZLE_TYPE_DOUBLE;
6231
case DECIMAL_RESULT:
6232
return DRIZZLE_TYPE_NEWDECIMAL;
6236
return DRIZZLE_TYPE_VARCHAR;
6243
return item->field_type();
6247
Find field type which can carry current Item_type_holder type and
6250
@param thd thread handler
6251
@param item given item to join its parameters with this item ones
6254
true error - types are incompatible
6259
bool Item_type_holder::join_types(THD *thd __attribute__((unused)),
6262
uint max_length_orig= max_length;
6263
uint decimals_orig= decimals;
6264
fld_type= Field::field_type_merge(fld_type, get_real_type(item));
6266
int item_decimals= item->decimals;
6267
/* fix variable decimals which always is NOT_FIXED_DEC */
6268
if (Field::result_merge_type(fld_type) == INT_RESULT)
6270
decimals= max(decimals, item_decimals);
6272
if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
6274
decimals= min(max(decimals, item->decimals), DECIMAL_MAX_SCALE);
6275
int precision= min(max(prev_decimal_int_part, item->decimal_int_part())
6276
+ decimals, DECIMAL_MAX_PRECISION);
6277
unsigned_flag&= item->unsigned_flag;
6278
max_length= my_decimal_precision_to_length(precision, decimals,
6282
switch (Field::result_merge_type(fld_type))
6286
const char *old_cs, *old_derivation;
6287
uint32_t old_max_chars= max_length / collation.collation->mbmaxlen;
6288
old_cs= collation.collation->name;
6289
old_derivation= collation.derivation_name();
6290
if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
6292
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
6293
old_cs, old_derivation,
6294
item->collation.collation->name,
6295
item->collation.derivation_name(),
6300
To figure out max_length, we have to take into account possible
6301
expansion of the size of the values because of character set
6304
if (collation.collation != &my_charset_bin)
6306
max_length= max(old_max_chars * collation.collation->mbmaxlen,
6307
display_length(item) /
6308
item->collation.collation->mbmaxlen *
6309
collation.collation->mbmaxlen);
6312
set_if_bigger(max_length, display_length(item));
6317
if (decimals != NOT_FIXED_DEC)
6319
int delta1= max_length_orig - decimals_orig;
6320
int delta2= item->max_length - item->decimals;
6321
max_length= max(delta1, delta2) + decimals;
6322
if (fld_type == DRIZZLE_TYPE_DOUBLE && max_length > DBL_DIG + 2)
6324
max_length= DBL_DIG + 7;
6325
decimals= NOT_FIXED_DEC;
6329
max_length= DBL_DIG+7;
6333
max_length= max(max_length, display_length(item));
6335
maybe_null|= item->maybe_null;
6336
get_full_info(item);
6338
/* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
6339
prev_decimal_int_part= decimal_int_part();
6344
Calculate lenth for merging result for given Item type.
6346
@param item Item for length detection
6352
uint32_t Item_type_holder::display_length(Item *item)
6354
if (item->type() == Item::FIELD_ITEM)
6355
return ((Item_field *)item)->max_disp_length();
6357
switch (item->field_type())
6359
case DRIZZLE_TYPE_TIMESTAMP:
6360
case DRIZZLE_TYPE_TIME:
6361
case DRIZZLE_TYPE_DATETIME:
6362
case DRIZZLE_TYPE_NEWDATE:
6363
case DRIZZLE_TYPE_VARCHAR:
6364
case DRIZZLE_TYPE_NEWDECIMAL:
6365
case DRIZZLE_TYPE_ENUM:
6366
case DRIZZLE_TYPE_SET:
6367
case DRIZZLE_TYPE_BLOB:
6368
case DRIZZLE_TYPE_TINY:
6370
case DRIZZLE_TYPE_SHORT:
6372
case DRIZZLE_TYPE_LONG:
6373
return MY_INT32_NUM_DECIMAL_DIGITS;
6374
case DRIZZLE_TYPE_DOUBLE:
6376
case DRIZZLE_TYPE_NULL:
6378
case DRIZZLE_TYPE_LONGLONG:
6381
assert(0); // we should never go there
6388
Make temporary table field according collected information about type
6391
@param table temporary table for which we create fields
6397
Field *Item_type_holder::make_field_by_type(TABLE *table)
6400
The field functions defines a field to be not null if null_ptr is not 0
6402
uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
6406
case DRIZZLE_TYPE_ENUM:
6407
assert(enum_set_typelib);
6408
field= new Field_enum((uchar *) 0, max_length, null_ptr, 0,
6410
get_enum_pack_length(enum_set_typelib->count),
6411
enum_set_typelib, collation.collation);
6415
case DRIZZLE_TYPE_SET:
6416
assert(enum_set_typelib);
6417
field= new Field_set((uchar *) 0, max_length, null_ptr, 0,
6419
get_set_pack_length(enum_set_typelib->count),
6420
enum_set_typelib, collation.collation);
6424
case DRIZZLE_TYPE_NULL:
6425
return make_string_field(table);
6429
return tmp_table_field_from_field_type(table, 0);
6434
Get full information from Item about enum/set fields to be able to create
6437
@param item Item for information collection
6439
void Item_type_holder::get_full_info(Item *item)
6441
if (fld_type == DRIZZLE_TYPE_ENUM ||
6442
fld_type == DRIZZLE_TYPE_SET)
6444
if (item->type() == Item::SUM_FUNC_ITEM &&
6445
(((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
6446
((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC))
6447
item = ((Item_sum*)item)->args[0];
6449
We can have enum/set type after merging only if we have one enum|set
6450
field (or MIN|MAX(enum|set field)) and number of NULL fields
6452
assert((enum_set_typelib &&
6453
get_real_type(item) == DRIZZLE_TYPE_NULL) ||
6454
(!enum_set_typelib &&
6455
item->type() == Item::FIELD_ITEM &&
6456
(get_real_type(item) == DRIZZLE_TYPE_ENUM ||
6457
get_real_type(item) == DRIZZLE_TYPE_SET) &&
6458
((Field_enum*)((Item_field *) item)->field)->typelib));
6459
if (!enum_set_typelib)
6461
enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib;
6467
double Item_type_holder::val_real()
6469
assert(0); // should never be called
6474
int64_t Item_type_holder::val_int()
6476
assert(0); // should never be called
6480
my_decimal *Item_type_holder::val_decimal(my_decimal *)
6482
assert(0); // should never be called
6486
String *Item_type_holder::val_str(String*)
6488
assert(0); // should never be called
6492
void Item_result_field::cleanup()
5938
6500
Dummy error processor used by default by Name_resolution_context.
5944
void dummy_error_processor(Session *, void *)
6506
void dummy_error_processor(THD *thd __attribute__((unused)),
6507
void *data __attribute__((unused)))
5948
Create field for temporary table using type of given item.
5950
@param session Thread handler
5951
@param item Item to create a field for
5952
@param table Temporary table
5953
@param copy_func If set and item is a function, store copy of
5955
@param modify_item 1 if item->result_field should point to new
5956
item. This is relevent for how fill_record()
5958
If modify_item is 1 then fill_record() will
5959
update the record in the original table.
5960
If modify_item is 0 then fill_record() will
5961
update the temporary table
5962
@param convert_blob_length If >0 create a varstring(convert_blob_length)
5963
field instead of blob.
5971
static Field *create_tmp_field_from_item(Session *,
5972
Item *item, Table *table,
5973
Item ***copy_func, bool modify_item,
5974
uint32_t convert_blob_length)
5976
bool maybe_null= item->maybe_null;
5979
switch (item->result_type()) {
5981
new_field= new Field_double(item->max_length, maybe_null,
5982
item->name, item->decimals, true);
5986
Select an integer type with the minimal fit precision.
5987
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
5988
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
5989
Field_long : make them Field_int64_t.
5991
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
5992
new_field=new Field_int64_t(item->max_length, maybe_null,
5993
item->name, item->unsigned_flag);
5995
new_field=new Field_long(item->max_length, maybe_null,
5996
item->name, item->unsigned_flag);
5999
assert(item->collation.collation);
6001
enum enum_field_types type;
6003
DATE/TIME fields have STRING_RESULT result type.
6004
To preserve type they needed to be handled separately.
6006
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
6007
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_DATE ||
6008
type == DRIZZLE_TYPE_TIMESTAMP)
6009
new_field= item->tmp_table_field_from_field_type(table, 1);
6011
Make sure that the blob fits into a Field_varstring which has
6014
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
6015
convert_blob_length <= Field_varstring::MAX_SIZE &&
6016
convert_blob_length)
6017
new_field= new Field_varstring(convert_blob_length, maybe_null,
6018
item->name, table->s,
6019
item->collation.collation);
6021
new_field= item->make_string_field(table);
6022
new_field->set_derivation(item->collation.derivation);
6024
case DECIMAL_RESULT:
6026
uint8_t dec= item->decimals;
6027
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
6028
uint32_t len= item->max_length;
6031
Trying to put too many digits overall in a DECIMAL(prec,dec)
6032
will always throw a warning. We must limit dec to
6033
DECIMAL_MAX_SCALE however to prevent an assert() later.
6038
signed int overflow;
6040
dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
6043
If the value still overflows the field with the corrected dec,
6044
we'll throw out decimals rather than integers. This is still
6045
bad and of course throws a truncation warning.
6046
+1: for decimal point
6049
overflow= my_decimal_precision_to_length(intg + dec, dec,
6050
item->unsigned_flag) - len;
6053
dec= cmax(0, dec - overflow); // too long, discard fract
6055
len -= item->decimals - dec; // corrected value fits
6058
new_field= new Field_new_decimal(len, maybe_null, item->name,
6059
dec, item->unsigned_flag);
6064
// This case should never be choosen
6070
new_field->init(table);
6072
if (copy_func && item->is_result_field())
6073
*((*copy_func)++) = item; // Save for copy_funcs
6075
item->set_result_field(new_field);
6076
if (item->type() == Item::NULL_ITEM)
6077
new_field->is_created_from_null_item= true;
6081
Field *create_tmp_field(Session *session, Table *table,Item *item,
6082
Item::Type type, Item ***copy_func, Field **from_field,
6083
Field **default_field, bool group, bool modify_item,
6084
bool, bool make_copy_field,
6085
uint32_t convert_blob_length)
6088
Item::Type orig_type= type;
6091
if (type != Item::FIELD_ITEM &&
6092
item->real_item()->type() == Item::FIELD_ITEM)
6095
item= item->real_item();
6096
type= Item::FIELD_ITEM;
6100
case Item::SUM_FUNC_ITEM:
6102
Item_sum *item_sum=(Item_sum*) item;
6103
result= item_sum->create_tmp_field(group, table, convert_blob_length);
6105
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
6108
case Item::FIELD_ITEM:
6109
case Item::DEFAULT_VALUE_ITEM:
6111
Item_field *field= (Item_field*) item;
6112
bool orig_modify= modify_item;
6113
if (orig_type == Item::REF_ITEM)
6116
If item have to be able to store NULLs but underlaid field can't do it,
6117
create_tmp_field_from_field() can't be used for tmp field creation.
6119
if (field->maybe_null && !field->field->maybe_null())
6121
result= create_tmp_field_from_item(session, item, table, NULL,
6122
modify_item, convert_blob_length);
6123
*from_field= field->field;
6124
if (result && modify_item)
6125
field->result_field= result;
6128
result= create_tmp_field_from_field(session, (*from_field= field->field),
6129
orig_item ? orig_item->name :
6132
modify_item ? field :
6134
convert_blob_length);
6135
if (orig_type == Item::REF_ITEM && orig_modify)
6136
((Item_ref*)orig_item)->set_result_field(result);
6137
if (field->field->eq_def(result))
6138
*default_field= field->field;
6142
case Item::FUNC_ITEM:
6144
case Item::COND_ITEM:
6145
case Item::FIELD_AVG_ITEM:
6146
case Item::FIELD_STD_ITEM:
6147
case Item::SUBSELECT_ITEM:
6148
/* The following can only happen with 'CREATE TABLE ... SELECT' */
6149
case Item::PROC_ITEM:
6150
case Item::INT_ITEM:
6151
case Item::REAL_ITEM:
6152
case Item::DECIMAL_ITEM:
6153
case Item::STRING_ITEM:
6154
case Item::REF_ITEM:
6155
case Item::NULL_ITEM:
6156
case Item::VARBIN_ITEM:
6157
if (make_copy_field)
6159
assert(((Item_result_field*)item)->result_field);
6160
*from_field= ((Item_result_field*)item)->result_field;
6162
return create_tmp_field_from_item(session, item, table,
6163
(make_copy_field ? 0 : copy_func),
6164
modify_item, convert_blob_length);
6165
case Item::TYPE_HOLDER:
6166
result= ((Item_type_holder *)item)->make_field_by_type(table);
6167
result->set_derivation(item->collation.derivation);
6169
default: // Dosen't have to be stored
6176
6511
Wrapper of hide_view_error call for Name_resolution_context error