21 #include <drizzled/sql_select.h>
22 #include <drizzled/error.h>
24 #include <drizzled/item/cmpfunc.h>
25 #include <drizzled/item/cache_row.h>
26 #include <drizzled/item/type_holder.h>
27 #include <drizzled/item/sum.h>
28 #include <drizzled/item/copy_string.h>
29 #include <drizzled/function/str/conv_charset.h>
30 #include <drizzled/sql_base.h>
31 #include <drizzled/util/convert.h>
32 #include <drizzled/plugin/client.h>
33 #include <drizzled/time_functions.h>
34 #include <drizzled/field/str.h>
35 #include <drizzled/field/num.h>
36 #include <drizzled/field/blob.h>
37 #include <drizzled/field/date.h>
38 #include <drizzled/field/datetime.h>
39 #include <drizzled/field/decimal.h>
40 #include <drizzled/field/double.h>
41 #include <drizzled/field/enum.h>
42 #include <drizzled/field/epoch.h>
43 #include <drizzled/field/int32.h>
44 #include <drizzled/field/int64.h>
45 #include <drizzled/field/microtime.h>
46 #include <drizzled/field/null.h>
47 #include <drizzled/field/real.h>
48 #include <drizzled/field/size.h>
49 #include <drizzled/field/time.h>
50 #include <drizzled/field/varstring.h>
51 #include <drizzled/current_session.h>
52 #include <drizzled/session.h>
53 #include <drizzled/internal/m_string.h>
54 #include <drizzled/item/ref.h>
55 #include <drizzled/item/subselect.h>
56 #include <drizzled/sql_lex.h>
57 #include <drizzled/system_variables.h>
68 const String my_null_string(
"NULL", 4, default_charset_info);
70 bool Item::is_expensive_processor(
unsigned char *)
75 void Item::fix_after_pullout(Select_Lex *,
Item **)
83 const char *Item::full_name(
void)
const
85 return name ? name :
"???";
88 int64_t Item::val_int_endpoint(
bool,
bool *)
100 return val_int() != 0;
107 return not val->isZero();
113 return val_real() != 0.0;
126 double nr= val_real();
130 str->set_real(nr, decimals, &my_charset_bin);
134 String *Item::val_string_from_int(String *str)
136 int64_t nr= val_int();
140 str->set_int(nr, unsigned_flag, &my_charset_bin);
144 String *Item::val_string_from_decimal(String *str)
146 type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
150 class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals,
false, &dec_buf);
155 type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
157 double nr= val_real();
161 double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
162 return (decimal_value);
165 type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
167 int64_t nr= val_int();
171 int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
172 return decimal_value;
175 type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
178 if (!(res= val_str(&str_value)))
181 if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
184 res->charset()) & E_DEC_BAD_NUM)
186 push_warning_printf(&getSession(),
187 DRIZZLE_ERROR::WARN_LEVEL_WARN,
188 ER_TRUNCATED_WRONG_VALUE,
189 ER(ER_TRUNCATED_WRONG_VALUE),
"DECIMAL",
192 return decimal_value;
195 type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
199 if (get_date(ltime, TIME_FUZZY_DATE))
201 decimal_value->set_zero();
205 return date2_class_decimal(<ime, decimal_value);
208 type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
214 decimal_value->set_zero();
217 return date2_class_decimal(<ime, decimal_value);
220 double Item::val_real_from_decimal()
224 type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
227 class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
231 int64_t Item::val_int_from_decimal()
235 type::Decimal value, *dec_val= val_decimal(&value);
239 dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
244 bool Item::save_time_in_field(Field *field)
249 return set_field_to_null(field);
251 field->set_notnull();
253 return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
256 bool Item::save_date_in_field(Field *field)
260 if (get_date(ltime, TIME_FUZZY_DATE))
261 return set_field_to_null(field);
263 field->set_notnull();
265 return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
275 return set_field_to_null(field);
277 field->set_notnull();
279 return field->store(result->ptr(), result->length(), collation.collation);
283 is_expensive_cache(-1),
292 unsigned_flag(false),
293 with_sum_func(false),
294 is_autogenerated_name(true),
295 with_subselect(false),
296 collation(&my_charset_bin, DERIVATION_COERCIBLE),
297 _session(*current_session)
310 if (getSession().lex().current_select)
312 enum_parsing_place place= getSession().lex().current_select->parsing_place;
313 if (place == SELECT_LIST || place == IN_HAVING)
314 getSession().lex().current_select->select_n_having_items++;
319 is_expensive_cache(-1),
320 str_value(item->str_value),
322 name_length(item->name_length),
323 max_length(item->max_length),
324 marker(item->marker),
325 decimals(item->decimals),
327 maybe_null(item->maybe_null),
328 null_value(item->null_value),
329 unsigned_flag(item->unsigned_flag),
330 with_sum_func(item->with_sum_func),
331 is_autogenerated_name(item->is_autogenerated_name),
332 with_subselect(item->with_subselect),
333 collation(item->collation),
334 cmp_context(item->cmp_context),
342 uint32_t Item::float_length(uint32_t decimals_par)
const
344 return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
347 uint32_t Item::decimal_precision()
const
349 Item_result restype= result_type();
351 if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
352 return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
353 (uint32_t) DECIMAL_MAX_PRECISION);
354 return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
357 int Item::decimal_int_part()
const
359 return class_decimal_int_part(decimal_precision(), decimals);
364 str->append(full_name(), strlen(full_name()));
367 void Item::print_item_w_name(
String *str)
373 str->append(STRING_WITH_LEN(
" AS "));
378 void Item::split_sum_func(Session *, Item **, List<Item> &)
389 return (this->*transformer)(arg);
392 bool Item::check_cols(uint32_t c)
396 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
402 void Item::set_name(
const char *str, uint32_t length,
const charset_info_st* cs)
407 std::cerr <<
"non-empty empty name: " << str << std::endl;
415 uint32_t orig_len= length;
416 while (length && not cs->isgraph(*str))
425 push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY), str + length - orig_len);
427 push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES), str + length - orig_len);
440 return type() == item->type() &&
443 not system_charset_info->strcasecmp(
name, item->
name);
449 return conv->safe ? conv : NULL;
460 else if (result_type() == STRING_RESULT)
462 char buff[type::Time::MAX_STRING_LENGTH];
463 String tmp(buff,
sizeof(buff), &my_charset_bin);
465 if (not res || str_to_datetime_with_warn(getSession(), *res, ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
473 type::datetime_t date_value;
475 ltime.convert(date_value, value, fuzzydate);
477 if (not type::is_valid(date_value))
479 char buff[DECIMAL_LONGLONG_DIGITS];
480 char* end= internal::int64_t10_to_str(value, buff, -10);
481 make_truncated_value_warning(getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
str_ref(buff, (
int) (end-buff)), type::DRIZZLE_TIMESTAMP_NONE, NULL);
496 char buff[type::Time::MAX_STRING_LENGTH];
497 String tmp(buff,
sizeof(buff),&my_charset_bin);
499 if (not res || str_to_time_with_warn(getSession(), *res, ltime))
508 bool Item::get_date_result(
type::Time <ime,uint32_t fuzzydate)
529 bool Item::is_result_field(
void)
534 bool Item::is_bool_func(
void)
539 void Item::save_in_result_field(
bool)
550 Item *Item::copy_andor_structure(Session *)
555 Item *Item::real_item(
void)
560 const Item *Item::real_item(
void)
const
565 Item *Item::get_tmp_table_item(Session *session)
567 return copy_or_same(session);
570 const charset_info_st *Item::default_charset()
572 return current_session->variables.getCollation();
575 const charset_info_st *Item::compare_collation()
580 bool Item::walk(Item_processor processor,
bool,
unsigned char *arg)
582 return (this->*processor)(arg);
585 Item*
Item::compile(Item_analyzer analyzer,
unsigned char **arg_p, Item_transformer transformer,
unsigned char *arg_t)
587 return (this->*analyzer)(arg_p)
588 ? (this->*transformer)(arg_t)
592 void Item::traverse_cond(Cond_traverser traverser,
void *arg, traverse_order)
594 (*traverser)(
this, arg);
597 bool Item::remove_dependence_processor(
unsigned char *)
602 bool Item::collect_item_field_processor(
unsigned char *)
607 bool Item::find_item_in_field_list_processor(
unsigned char *)
612 bool Item::change_context_processor(
unsigned char *)
617 bool Item::register_field_in_read_map(
unsigned char *)
622 bool Item::subst_argument_checker(
unsigned char **arg)
629 Item *Item::equal_fields_propagator(
unsigned char *)
634 bool Item::set_no_const_sub(
unsigned char *)
639 Item *Item::replace_equal_field(
unsigned char *)
644 uint32_t Item::cols()
649 Item* Item::element_index(uint32_t)
654 Item** Item::addr(uint32_t)
659 bool Item::null_inside()
664 void Item::bring_value()
667 Item *Item::neg_transformer(Session *)
672 Item *Item::update_value_transformer(
unsigned char *)
677 void Item::delete_self()
690 if (is_expensive_cache < 0)
691 is_expensive_cache= walk(&Item::is_expensive_processor, 0, NULL);
692 return test(is_expensive_cache);
704 const char *table_name_arg,
const char *field_name_arg)
705 :
Item_ref(context_arg, item, table_name_arg, field_name_arg) {}
716 void Item::split_sum_func(
Session *session,
Item **ref_pointer_array,
718 bool skip_registered)
721 if (type() == SUM_FUNC_ITEM &&
726 if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
727 (type() == FUNC_ITEM &&
728 (((
Item_func *)
this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
729 ((
Item_func *)
this)->functype() == Item_func::TRIG_COND_FUNC)))
732 split_sum_func(session, ref_pointer_array, fields);
734 else if ((type() == SUM_FUNC_ITEM || (
used_tables() & ~PARAM_TABLE_BIT)) &&
735 type() != SUBSELECT_ITEM &&
749 uint32_t el= fields.size();
750 Item *real_itm= real_item();
752 ref_pointer_array[el]= real_itm;
753 item_ref=
new Item_aggregate_ref(&session->lex().current_select->context, ref_pointer_array + el, 0,
name);
754 if (type() == SUM_FUNC_ITEM)
755 item_ref->depended_from= ((
Item_sum *)
this)->depended_from();
756 fields.push_front(real_itm);
776 const char *db_name= (resolved_item->db_name ?
777 resolved_item->db_name :
"");
778 const char *table_name= (resolved_item->table_name ?
779 resolved_item->table_name :
"");
782 mark_item->depended_from= last;
783 current->mark_as_dependent(last);
784 if (session->lex().describe & DESCRIBE_EXTENDED)
786 char warn_buff[DRIZZLE_ERRMSG_SIZE];
787 snprintf(warn_buff,
sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
788 db_name, (db_name[0] ?
"." :
""),
789 table_name, (table_name [0] ?
"." :
""),
790 resolved_item->field_name,
791 current->select_number, last->select_number);
792 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
793 ER_WARN_FIELD_RESOLVED, warn_buff);
798 Select_Lex *last_select,
799 Select_Lex *current_sel,
809 Select_Lex *previous_select= current_sel;
810 for (; previous_select->outer_select() != last_select;
811 previous_select= previous_select->outer_select())
813 Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
814 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
815 prev_subselect_item->const_item_cache=
false;
818 Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
820 if (found_field == view_ref_found)
822 Item::Type type= found_item->type();
823 prev_subselect_item->used_tables_cache|= found_item->
used_tables();
824 dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ?
829 prev_subselect_item->used_tables_cache|= found_field->getTable()->
map;
830 prev_subselect_item->const_item_cache=
false;
853 const char *table_name;
854 const char *field_name;
855 Order *found_group= NULL;
856 int found_match_degree= 0;
858 int cur_match_degree= 0;
859 char name_buff[NAME_LEN+1];
861 if (find_item->type() == Item::FIELD_ITEM ||
862 find_item->type() == Item::REF_ITEM)
865 table_name= ((
Item_ident*) find_item)->table_name;
866 field_name= ((
Item_ident*) find_item)->field_name;
874 strncpy(name_buff, db_name,
sizeof(name_buff)-1);
875 files_charset_info->casedn_str(name_buff);
879 assert(field_name != 0);
881 for (
Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
883 if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
888 assert(cur_field->field_name != 0);
890 if (system_charset_info->strcasecmp(cur_field->field_name, field_name))
894 if (cur_field->table_name && table_name)
897 if (table_alias_charset->strcasecmp(cur_field->table_name, table_name))
902 if (cur_field->db_name && db_name)
905 if (system_charset_info->strcasecmp(cur_field->db_name, db_name))
914 if (cur_match_degree > found_match_degree)
916 found_match_degree= cur_match_degree;
917 found_group= cur_group;
919 else if (found_group &&
920 (cur_match_degree == found_match_degree) &&
921 ! (*(found_group->item))->eq(cur_field, 0))
928 my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
935 return found_group->item;
942 Item **group_by_ref= NULL;
943 Item **select_ref= NULL;
944 Order *group_list= (
Order*) select->group_list.first;
945 bool ambiguous_fields=
false;
953 if (!(select_ref= find_item_in_list(session,
954 ref, *(select->get_item_list()),
955 &counter, REPORT_EXCEPT_NOT_FOUND,
958 if (resolution == RESOLVED_AGAINST_ALIAS)
959 ref->alias_name_used=
true;
962 if (select->having_fix_field && !ref->with_sum_func && group_list)
967 if (group_by_ref && (select_ref != not_found_item) &&
968 !((*group_by_ref)->eq(*select_ref, 0)))
970 ambiguous_fields=
true;
971 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
972 ER(ER_NON_UNIQ_ERROR), ref->full_name(),
978 if (select_ref != not_found_item || group_by_ref)
980 if (select_ref != not_found_item && !ambiguous_fields)
982 assert(*select_ref != 0);
983 if (!select->ref_pointer_array[counter])
985 my_error(ER_ILLEGAL_REFERENCE, MYF(0),
986 ref->
name,
"forward reference in item list");
989 assert((*select_ref)->fixed);
990 return (select->ref_pointer_array + counter);
998 return (
Item**) not_found_item;
1001 void Item::init_make_field(SendField *tmp_field, enum_field_types field_type_arg)
1003 tmp_field->db_name=
"";
1004 tmp_field->org_table_name=
"";
1005 tmp_field->org_col_name=
"";
1006 tmp_field->table_name=
"";
1007 tmp_field->col_name=
name;
1008 tmp_field->charsetnr= collation.collation->number;
1009 tmp_field->flags= (
maybe_null ? 0 : NOT_NULL_FLAG) | (collation.collation->binary_compare() ? BINARY_FLAG : 0);
1010 tmp_field->type= field_type_arg;
1011 tmp_field->length= max_length;
1012 tmp_field->decimals= decimals;
1015 void Item::make_field(SendField *tmp_field)
1017 init_make_field(tmp_field, field_type());
1020 enum_field_types Item::string_field_type()
const
1022 enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
1023 if (max_length >= 65536)
1024 f_type= DRIZZLE_TYPE_BLOB;
1028 enum_field_types Item::field_type()
const
1030 switch (result_type()) {
1032 return string_field_type();
1034 return DRIZZLE_TYPE_LONGLONG;
1035 case DECIMAL_RESULT:
1036 return DRIZZLE_TYPE_DECIMAL;
1038 return DRIZZLE_TYPE_DOUBLE;
1046 bool Item::is_datetime()
1048 return field::isDateTime(field_type());
1051 String *Item::check_well_formed_result(String *str,
bool send_error)
1054 const charset_info_st *
const cs= str->charset();
1055 int well_formed_error;
1056 uint32_t wlen= cs->cset->well_formed_len(*cs, *str, str->length(), &well_formed_error);
1057 if (wlen < str->length())
1060 enum DRIZZLE_ERROR::enum_warning_level level;
1061 uint32_t diff= str->length() - wlen;
1062 set_if_smaller(diff, 3U);
1063 (void) drizzled_string_to_hex(hexbuf, str->ptr() + wlen, diff);
1066 my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
1067 cs->csname, hexbuf);
1071 level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
1075 push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
1076 ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1085 if (collation.collation != cs)
1087 save_cs= collation.collation;
1088 collation.collation= cs;
1090 if (item->collation.collation != cs)
1092 save_item_cs= item->collation.collation;
1093 item->collation.collation= cs;
1095 bool res=
eq(item, binary_cmp);
1097 collation.collation= save_cs;
1099 item->collation.collation= save_item_cs;
1106 assert(collation.collation);
1107 if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1110 collation.collation);
1114 table->setVariableWidth();
1116 collation.collation);
1129 unsigned char *null_ptr=
maybe_null ? (
unsigned char*)
"" : 0;
1132 switch (field_type()) {
1133 case DRIZZLE_TYPE_DECIMAL:
1142 case DRIZZLE_TYPE_LONG:
1143 field=
new field::Int32((
unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
name);
1145 case DRIZZLE_TYPE_LONGLONG:
1146 field=
new field::Int64((
unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
name);
1148 case DRIZZLE_TYPE_DOUBLE:
1149 field=
new Field_double((
unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1150 name, decimals, 0, unsigned_flag);
1152 case DRIZZLE_TYPE_NULL:
1155 case DRIZZLE_TYPE_DATE:
1159 case DRIZZLE_TYPE_MICROTIME:
1163 case DRIZZLE_TYPE_TIMESTAMP:
1166 case DRIZZLE_TYPE_DATETIME:
1169 case DRIZZLE_TYPE_TIME:
1172 case DRIZZLE_TYPE_BOOLEAN:
1173 case DRIZZLE_TYPE_UUID:
1174 case DRIZZLE_TYPE_IPV6:
1175 case DRIZZLE_TYPE_ENUM:
1176 case DRIZZLE_TYPE_VARCHAR:
1178 case DRIZZLE_TYPE_BLOB:
1194 int Item::save_in_field(
Field *field,
bool no_conversions)
1197 if (result_type() == STRING_RESULT)
1201 char buff[MAX_FIELD_WIDTH];
1202 str_value.set_quick(buff,
sizeof(buff), cs);
1212 field->set_notnull();
1213 error=field->store(result->ptr(),result->length(),cs);
1216 else if (result_type() == REAL_RESULT &&
1217 field->result_type() == STRING_RESULT)
1222 field->set_notnull();
1223 error= field->store(nr);
1225 else if (result_type() == REAL_RESULT)
1229 return set_field_to_null(field);
1230 field->set_notnull();
1231 error=field->store(nr);
1233 else if (result_type() == DECIMAL_RESULT)
1235 type::Decimal decimal_value;
1236 type::Decimal *value=
val_decimal(&decimal_value);
1239 field->set_notnull();
1240 error=field->store_decimal(value);
1247 field->set_notnull();
1248 error=field->store(nr, unsigned_flag);
1264 bool *cache_flag= (
bool*)*arg;
1267 Item *item= real_item();
1274 item->type() == SUBSELECT_ITEM ||
1280 (item->type() == Item::FUNC_ITEM &&
1281 ((
Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1301 *((
bool*)arg)=
false;
1302 Item_cache *cache= Item_cache::get_cache(
this);
1314 switch (field_type())
1316 case DRIZZLE_TYPE_DATE:
1317 case DRIZZLE_TYPE_NULL:
1318 case DRIZZLE_TYPE_ENUM:
1319 case DRIZZLE_TYPE_BLOB:
1320 case DRIZZLE_TYPE_VARCHAR:
1321 case DRIZZLE_TYPE_BOOLEAN:
1322 case DRIZZLE_TYPE_UUID:
1323 case DRIZZLE_TYPE_IPV6:
1324 case DRIZZLE_TYPE_DECIMAL:
1327 client->store(res->ptr(), res->length());
1330 case DRIZZLE_TYPE_LONG:
1334 client->store((int32_t)nr);
1337 case DRIZZLE_TYPE_LONGLONG:
1343 client->store((uint64_t)nr);
1345 client->store((int64_t)nr);
1349 case DRIZZLE_TYPE_DOUBLE:
1353 client->store(nr, decimals, buffer);
1356 case DRIZZLE_TYPE_TIME:
1364 case DRIZZLE_TYPE_DATETIME:
1365 case DRIZZLE_TYPE_MICROTIME:
1366 case DRIZZLE_TYPE_TIMESTAMP:
1379 uint32_t Item::max_char_length()
const
1381 return max_length / collation.collation->mbmaxlen;
1384 void Item::fix_char_length(uint32_t max_char_length_arg)
1386 max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1389 Item_result item_cmp_type(Item_result a,Item_result b)
1391 if (a == STRING_RESULT && b == STRING_RESULT)
1392 return STRING_RESULT;
1394 if (a == INT_RESULT && b == INT_RESULT)
1396 else if (a == ROW_RESULT || b == ROW_RESULT)
1399 if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1400 (b == INT_RESULT || b == DECIMAL_RESULT))
1401 return DECIMAL_RESULT;
1406 void resolve_const_item(Session *session, Item **ref, Item *comp_item)
1409 Item *new_item= NULL;
1410 if (item->basic_const_item())
1412 Item_result res_type=item_cmp_type(comp_item->result_type(), item->result_type());
1413 const char *name=item->name;
1419 char buff[MAX_FIELD_WIDTH];
1420 String tmp(buff,
sizeof(buff),&my_charset_bin),*result;
1421 result=item->val_str(&tmp);
1422 if (item->null_value)
1423 new_item=
new Item_null(name);
1425 new_item=
new Item_string(name, memory::sql_strdup(*result), result->length(), result->charset());
1430 new_item= item->null_value ? (Item*)
new Item_null(name) : (Item*)
new Item_int(name, item->val_int(), item->max_length);
1434 if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1444 Item_row *item_row= (Item_row*) item;
1445 Item_row *comp_item_row= (Item_row*) comp_item;
1453 assert(item->result_type() == comp_item->result_type());
1454 assert(item_row->cols() == comp_item_row->cols());
1455 for (uint32_t col= item_row->cols(); col--; )
1456 resolve_const_item(session, item_row->addr(col), comp_item_row->element_index(col));
1462 double result= item->val_real();
1463 uint32_t length=item->max_length,decimals=item->decimals;
1464 bool null_value=item->null_value;
1465 new_item= (null_value ? (Item*)
new Item_null(name) : (Item*)
1466 new Item_float(name, result, decimals, length));
1469 case DECIMAL_RESULT:
1471 type::Decimal decimal_value;
1472 type::Decimal *result= item->val_decimal(&decimal_value);
1473 uint32_t length= item->max_length, decimals= item->decimals;
1474 bool null_value= item->null_value;
1475 new_item= (null_value ?
1476 (Item*)
new Item_null(name) :
1477 (Item*) new Item_decimal(name, result, length, decimals));
1489 Item_result res_type=item_cmp_type(field->result_type(),
1490 item->result_type());
1491 if (res_type == STRING_RESULT)
1493 char item_buff[MAX_FIELD_WIDTH];
1494 char field_buff[MAX_FIELD_WIDTH];
1495 String item_tmp(item_buff,
sizeof(item_buff),&my_charset_bin),*item_result;
1496 String field_tmp(field_buff,
sizeof(field_buff),&my_charset_bin);
1497 item_result=item->
val_str(&item_tmp);
1500 field->val_str_internal(&field_tmp);
1501 return not stringcmp(&field_tmp,item_result);
1504 if (res_type == INT_RESULT)
1507 if (res_type == DECIMAL_RESULT)
1510 field_buf, *field_val;
1514 field_val= field->val_decimal(&field_buf);
1522 return result == field->val_real();
1550 Item ***copy_func,
bool modify_item,
1551 uint32_t convert_blob_length)
1554 Field *new_field= NULL;
1556 switch (item->result_type()) {
1558 new_field=
new Field_double(item->max_length, maybe_null,
1559 item->
name, item->decimals,
true);
1569 if (item->unsigned_flag)
1571 new_field=
new field::Size(item->max_length, maybe_null, item->
name, item->unsigned_flag);
1573 else if (item->max_length >= MY_INT32_NUM_DECIMAL_DIGITS - 1)
1575 new_field=
new field::Int64(item->max_length, maybe_null, item->
name, item->unsigned_flag);
1579 new_field=
new field::Int32(item->max_length, maybe_null, item->
name, item->unsigned_flag);
1585 assert(item->collation.collation);
1591 if (field::isDateTime(item->field_type()))
1599 else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1600 convert_blob_length <= Field_varstring::MAX_SIZE &&
1601 convert_blob_length)
1603 table->setVariableWidth();
1605 item->
name, item->collation.collation);
1611 new_field->set_derivation(item->collation.derivation);
1614 case DECIMAL_RESULT:
1616 uint8_t dec= item->decimals;
1617 uint8_t intg= ((
Item_decimal *) item)->decimal_precision() - dec;
1618 uint32_t len= item->max_length;
1628 signed int overflow;
1630 dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1639 overflow= class_decimal_precision_to_length(intg + dec, dec,
1640 item->unsigned_flag) - len;
1643 dec= max(0, dec - overflow);
1645 len-= item->decimals - dec;
1652 item->unsigned_flag);
1663 new_field->init(table);
1665 if (copy_func && item->is_result_field())
1666 *((*copy_func)++) = item;
1671 if (item->type() == Item::NULL_ITEM)
1683 Field **default_field,
1686 bool make_copy_field,
1687 uint32_t convert_blob_length)
1690 Item::Type orig_type= type;
1693 if (type != Item::FIELD_ITEM &&
1694 item->real_item()->type() == Item::FIELD_ITEM)
1697 item= item->real_item();
1698 type= Item::FIELD_ITEM;
1702 case Item::SUM_FUNC_ITEM:
1705 result= item_sum->create_tmp_field(group, table, convert_blob_length);
1707 my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
1710 case Item::FIELD_ITEM:
1711 case Item::DEFAULT_VALUE_ITEM:
1714 bool orig_modify= modify_item;
1715 if (orig_type == Item::REF_ITEM)
1721 if (field->
maybe_null && !field->field->maybe_null())
1724 modify_item, convert_blob_length);
1725 *from_field= field->field;
1726 if (result && modify_item)
1727 field->result_field= result;
1732 orig_item ? orig_item->
name :
1735 modify_item ? field :
1737 convert_blob_length);
1739 if (orig_type == Item::REF_ITEM && orig_modify)
1740 ((
Item_ref*)orig_item)->set_result_field(result);
1741 if (field->field->
eq_def(result))
1742 *default_field= field->field;
1746 case Item::FUNC_ITEM:
1748 case Item::COND_ITEM:
1749 case Item::FIELD_AVG_ITEM:
1750 case Item::FIELD_STD_ITEM:
1751 case Item::SUBSELECT_ITEM:
1753 case Item::PROC_ITEM:
1754 case Item::INT_ITEM:
1755 case Item::REAL_ITEM:
1756 case Item::DECIMAL_ITEM:
1757 case Item::STRING_ITEM:
1758 case Item::REF_ITEM:
1759 case Item::NULL_ITEM:
1760 case Item::VARBIN_ITEM:
1761 if (make_copy_field)
1767 (make_copy_field ? 0 : copy_func),
1768 modify_item, convert_blob_length);
1769 case Item::TYPE_HOLDER:
1771 result->set_derivation(item->collation.derivation);
1778 std::ostream& operator<<(std::ostream& output,
const Item &item)
1780 switch (item.type())
1782 case drizzled::Item::SUBSELECT_ITEM :
1783 case drizzled::Item::FIELD_ITEM :
1784 case drizzled::Item::SUM_FUNC_ITEM :
1785 case drizzled::Item::STRING_ITEM :
1786 case drizzled::Item::INT_ITEM :
1787 case drizzled::Item::REAL_ITEM :
1788 case drizzled::Item::NULL_ITEM :
1789 case drizzled::Item::VARBIN_ITEM :
1790 case drizzled::Item::COPY_STR_ITEM :
1791 case drizzled::Item::FIELD_AVG_ITEM :
1792 case drizzled::Item::DEFAULT_VALUE_ITEM :
1793 case drizzled::Item::PROC_ITEM :
1794 case drizzled::Item::COND_ITEM :
1795 case drizzled::Item::REF_ITEM :
1796 case drizzled::Item::FIELD_STD_ITEM :
1797 case drizzled::Item::FIELD_VARIANCE_ITEM :
1798 case drizzled::Item::INSERT_VALUE_ITEM :
1799 case drizzled::Item::ROW_ITEM:
1800 case drizzled::Item::CACHE_ITEM :
1801 case drizzled::Item::TYPE_HOLDER :
1802 case drizzled::Item::PARAM_ITEM :
1803 case drizzled::Item::DECIMAL_ITEM :
1804 case drizzled::Item::FUNC_ITEM :
1805 case drizzled::Item::BOOLEAN_ITEM :
1808 output << item.full_name();
1810 output << drizzled::display::type(item.type());