23 #include "my_global.h"                           
   30 #include "sql_class.h"                           
   35 #include "sql_parse.h"                           
   43 #include <ft_global.h> 
   47 #include "sp_rcontext.h" 
   51 #include <mysql/plugin.h> 
   60   if (!my_strcasecmp(system_charset_info, name->str, 
"GLOBAL") ||
 
   61       !my_strcasecmp(system_charset_info, name->str, 
"LOCAL") ||
 
   62       !my_strcasecmp(system_charset_info, name->str, 
"SESSION"))
 
   76   return ((
Item_func*) cond)->val_int() ? TRUE : FALSE;
 
   83 static inline bool test_if_sum_overflows_ull(ulonglong arg1, ulonglong arg2)
 
   85   return ULONGLONG_MAX - arg1 < arg2;
 
   88 void Item_func::set_arguments(
List<Item> &list)
 
   91   arg_count=list.elements;
 
   93   if (arg_count <= 2 || (args=(
Item**) sql_alloc(
sizeof(
Item*)*arg_count)))
 
   97     Item **save_args= args;
 
  101       *(save_args++)= item;
 
  102       with_sum_func|=item->with_sum_func;
 
  114 Item_func::Item_func(THD *thd, 
Item_func *item)
 
  117    allowed_arg_cols(item->allowed_arg_cols),
 
  118    used_tables_cache(item->used_tables_cache),
 
  119    not_null_tables_cache(item->not_null_tables_cache),
 
  120    arg_count(item->arg_count)
 
  128       if (!(args=(
Item**) thd->alloc(
sizeof(
Item*)*arg_count)))
 
  131     memcpy((
char*) args, (
char*) item->args, 
sizeof(
Item*)*arg_count);
 
  170 Item_func::fix_fields(THD *thd, 
Item **ref)
 
  172   DBUG_ASSERT(fixed == 0 || basic_const_item());
 
  174   Item **arg,**arg_end;
 
  177   Switch_resolve_place SRP(thd->lex->current_select ?
 
  178                            &thd->lex->current_select->resolve_place : NULL,
 
  179                            st_select_lex::RESOLVE_NONE,
 
  180                            thd->lex->current_select);
 
  196     for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
 
  203       if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
 
  207       if (allowed_arg_cols)
 
  209         if (item->check_cols(allowed_arg_cols))
 
  215         DBUG_ASSERT(arg == args); 
 
  216         allowed_arg_cols= item->cols();
 
  217         DBUG_ASSERT(allowed_arg_cols); 
 
  220       if (item->maybe_null)
 
  223       with_sum_func= with_sum_func || item->with_sum_func;
 
  228       with_stored_program|=   item->has_stored_program();
 
  231   fix_length_and_dec();
 
  240                                   st_select_lex *removed_select)
 
  242   Item **arg,**arg_end;
 
  250     for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
 
  252       Item *
const item= *arg;
 
  263 bool Item_func::walk(Item_processor processor, 
bool walk_subquery,
 
  268     Item **arg,**arg_end;
 
  269     for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
  271       if ((*arg)->walk(processor, walk_subquery, argument))
 
  275   return (this->*processor)(argument);
 
  278 void Item_func::traverse_cond(Cond_traverser traverser,
 
  279                               void *argument, traverse_order order)
 
  283     Item **arg,**arg_end;
 
  287       (*traverser)(
this, argument);
 
  288       for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
  290         (*arg)->traverse_cond(traverser, argument, order);
 
  294       for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
  296         (*arg)->traverse_cond(traverser, argument, order);
 
  298       (*traverser)(
this, argument);
 
  302     (*traverser)(
this, argument);
 
  325   DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
 
  329     Item **arg,**arg_end;
 
  330     for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
  342       if (*arg != new_item)
 
  343         current_thd->change_item_tree(arg, new_item);
 
  346   return (this->*transformer)(argument);
 
  376                          Item_transformer transformer, uchar *arg_t)
 
  378   if (!(this->*analyzer)(arg_p))
 
  382     Item **arg,**arg_end;
 
  383     for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
  389       uchar *arg_v= *arg_p;
 
  390       Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
 
  391       if (new_item == NULL)
 
  393       if (*arg != new_item)
 
  394         current_thd->change_item_tree(arg, new_item);
 
  397   return (this->*transformer)(arg_t);
 
  407   Item **arg, **arg_end;
 
  408   for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
 
  413 void Item_func::update_used_tables()
 
  417   with_subselect= 
false;
 
  418   with_stored_program= 
false;
 
  419   for (uint 
i=0 ; 
i < arg_count ; 
i++)
 
  421     args[
i]->update_used_tables();
 
  425     with_stored_program|= args[
i]->has_stored_program();
 
  430 table_map Item_func::used_tables()
 const 
  436 table_map Item_func::not_null_tables()
 const 
  444   str->append(func_name());
 
  446   print_args(str, 0, query_type);
 
  451 void Item_func::print_args(
String *str, uint from, enum_query_type query_type)
 
  453   for (uint 
i=from ; 
i < arg_count ; 
i++)
 
  457     args[
i]->
print(str, query_type);
 
  462 void Item_func::print_op(
String *str, enum_query_type query_type)
 
  465   for (uint 
i=0 ; 
i < arg_count-1 ; 
i++)
 
  467     args[
i]->
print(str, query_type);
 
  469     str->append(func_name());
 
  472   args[arg_count-1]->
print(str, query_type);
 
  482   if (item->type() != FUNC_ITEM)
 
  485   Item_func::Functype func_type;
 
  486   if ((func_type= functype()) != item_func->functype() ||
 
  487       arg_count != item_func->arg_count ||
 
  488       (func_type != Item_func::FUNC_SP &&
 
  489        func_name() != item_func->func_name()) ||
 
  490       (func_type == Item_func::FUNC_SP &&
 
  491        my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
 
  493   for (uint 
i=0; 
i < arg_count ; 
i++)
 
  494     if (!args[
i]->
eq(item_func->args[
i], binary_cmp))
 
  504   switch (result_type()) {
 
  506     if (max_char_length() > MY_INT32_NUM_DECIMAL_DIGITS)
 
  507       field= 
new Field_longlong(max_char_length(), maybe_null, item_name.
ptr(),
 
  510       field= 
new Field_long(max_char_length(), maybe_null, item_name.
ptr(),
 
  514     field= 
new Field_double(max_char_length(), maybe_null, item_name.
ptr(), decimals);
 
  520     field= Field_new_decimal::create_from_item(
this);
 
  538   longlong nr= val_int();
 
  541   int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
  542   return decimal_value;
 
  548   DBUG_ASSERT(fixed == 1);
 
  549   double nr= val_real();
 
  552   str->set_real(nr, decimals, collation.collation);
 
  560   double nr= val_real();
 
  563   double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
  564   return decimal_value;
 
  568 void Item_func::fix_num_length_and_dec()
 
  572   for (uint 
i=0 ; 
i < arg_count ; 
i++)
 
  574     set_if_bigger(decimals,args[
i]->decimals);
 
  575     set_if_bigger(fl_length, args[
i]->max_length);
 
  577   max_length=float_length(decimals);
 
  578   if (fl_length > max_length)
 
  580     decimals= NOT_FIXED_DEC;
 
  581     max_length= float_length(NOT_FIXED_DEC);
 
  586 void Item_func_numhybrid::fix_num_length_and_dec()
 
  603   if (field_type() != MYSQL_TYPE_DATE)
 
  605     for (uint 
i= 0; 
i < nitems; 
i++)
 
  606       set_if_bigger(decimals,
 
  607                     field_type() == MYSQL_TYPE_TIME ?
 
  610   set_if_smaller(decimals, DATETIME_MAX_DECIMALS);
 
  611   uint len= decimals ? (decimals + 1) : 0;
 
  612   switch (field_type())
 
  614     case MYSQL_TYPE_DATETIME:
 
  615     case MYSQL_TYPE_TIMESTAMP:
 
  616       len+= MAX_DATETIME_WIDTH;
 
  618     case MYSQL_TYPE_DATE:
 
  619     case MYSQL_TYPE_NEWDATE:
 
  620       len+= MAX_DATE_WIDTH;
 
  622     case MYSQL_TYPE_TIME:
 
  623       len+= MAX_TIME_WIDTH;
 
  628   fix_char_length(len);
 
  641   for (uint 
i=0 ; 
i < arg_count ; 
i++)
 
  643     set_if_bigger(decimals, args[
i]->decimals);
 
  644     set_if_bigger(max_int_part, args[
i]->decimal_int_part());
 
  645     set_if_smaller(unsigned_flag, args[
i]->unsigned_flag);
 
  648   fix_char_length(my_decimal_precision_to_length_no_truncation(precision,
 
  660   uint32 char_length= 0;
 
  662   for (uint 
i= 0; 
i < nitems; 
i++)
 
  664     set_if_bigger(char_length, item[
i]->max_char_length());
 
  665     set_if_smaller(unsigned_flag, item[
i]->unsigned_flag);
 
  667   fix_char_length(char_length);
 
  681   for (uint 
i=0 ; 
i < arg_count ; 
i++)
 
  683     if (decimals != NOT_FIXED_DEC)
 
  685       set_if_bigger(decimals, args[
i]->decimals);
 
  686       set_if_bigger(length, (args[
i]->max_length - args[
i]->decimals));
 
  688     set_if_bigger(max_length, args[
i]->max_length);
 
  690   if (decimals != NOT_FIXED_DEC)
 
  694     if (length < max_length)  
 
  695       max_length= UINT_MAX32;
 
  714   if (agg_arg_charsets_for_string_result(collation, items, nitems))
 
  716   if (is_temporal_type(field_type))
 
  720     decimals= NOT_FIXED_DEC;
 
  727 void Item_func::signal_divide_by_null()
 
  729   THD *thd= current_thd;
 
  730   if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO)
 
  731     push_warning(thd, Sql_condition::WARN_LEVEL_WARN, ER_DIVISION_BY_ZERO,
 
  732                  ER(ER_DIVISION_BY_ZERO));
 
  737 Item *Item_func::get_tmp_table_item(THD *thd)
 
  739   if (!with_sum_func && !const_item())
 
  741   return copy_or_same(thd);
 
  744 double Item_int_func::val_real()
 
  746   DBUG_ASSERT(fixed == 1);
 
  748   return unsigned_flag ? (double) ((ulonglong) val_int()) : (
double) val_int();
 
  754   DBUG_ASSERT(fixed == 1);
 
  755   longlong nr=val_int();
 
  758   str->set_int(nr, unsigned_flag, collation.collation);
 
  763 void Item_func_connection_id::fix_length_and_dec()
 
  765   Item_int_func::fix_length_and_dec();
 
  770 bool Item_func_connection_id::fix_fields(THD *thd, 
Item **ref)
 
  772   if (Item_int_func::fix_fields(thd, ref))
 
  774   thd->thread_specific_used= TRUE;
 
  775   value= thd->variables.pseudo_thread_id;
 
  787   DBUG_ENTER(
"Item_num_op::find_num_type");
 
  788   DBUG_PRINT(
"info", (
"name %s", func_name()));
 
  789   DBUG_ASSERT(arg_count == 2);
 
  793   DBUG_ASSERT(r0 != STRING_RESULT && r1 != STRING_RESULT);
 
  795   if (r0 == REAL_RESULT || r1 == REAL_RESULT)
 
  801     DBUG_ASSERT(!args[0]->is_temporal() || !args[1]->is_temporal());
 
  803     max_length= float_length(decimals);
 
  804     hybrid_type= REAL_RESULT;
 
  806   else if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
 
  808     hybrid_type= DECIMAL_RESULT;
 
  813     DBUG_ASSERT(r0 == INT_RESULT && r1 == INT_RESULT);
 
  815     hybrid_type=INT_RESULT;
 
  818   DBUG_PRINT(
"info", (
"Type: %s",
 
  819              (hybrid_type == REAL_RESULT ? 
"REAL_RESULT" :
 
  820               hybrid_type == DECIMAL_RESULT ? 
"DECIMAL_RESULT" :
 
  821               hybrid_type == INT_RESULT ? 
"INT_RESULT" :
 
  835   DBUG_ENTER(
"Item_func_num1::find_num_type");
 
  836   DBUG_PRINT(
"info", (
"name %s", func_name()));
 
  837   switch (hybrid_type= args[0]->result_type()) {
 
  839     unsigned_flag= args[0]->unsigned_flag;
 
  843     hybrid_type= REAL_RESULT;
 
  844     max_length= float_length(decimals);
 
  851   DBUG_PRINT(
"info", (
"Type: %s",
 
  852                       (hybrid_type == REAL_RESULT ? 
"REAL_RESULT" :
 
  853                        hybrid_type == DECIMAL_RESULT ? 
"DECIMAL_RESULT" :
 
  854                        hybrid_type == INT_RESULT ? 
"INT_RESULT" :
 
  860 void Item_func_num1::fix_num_length_and_dec()
 
  862   decimals= args[0]->decimals;
 
  863   max_length= args[0]->max_length;
 
  867 void Item_func_numhybrid::fix_length_and_dec()
 
  869   fix_num_length_and_dec();
 
  876   DBUG_ASSERT(fixed == 1);
 
  877   switch (hybrid_type) {
 
  881     if (!(val= decimal_op(&decimal_value)))
 
  883     my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, FALSE, val);
 
  884     str->set_charset(collation.collation);
 
  885     my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
 
  890     longlong nr= int_op();
 
  893     str->set_int(nr, unsigned_flag, collation.collation);
 
  898     double nr= real_op();
 
  901     str->set_real(nr, decimals, collation.collation);
 
  905     switch (field_type()) {
 
  906     case MYSQL_TYPE_DATETIME:
 
  907     case MYSQL_TYPE_TIMESTAMP:
 
  908       return val_string_from_datetime(str);
 
  909     case MYSQL_TYPE_DATE:
 
  910       return val_string_from_date(str);
 
  911     case MYSQL_TYPE_TIME:
 
  912       return val_string_from_time(str);
 
  916     return str_op(&str_value);
 
  924 double Item_func_numhybrid::val_real()
 
  926   DBUG_ASSERT(fixed == 1);
 
  927   switch (hybrid_type) {
 
  932     if (!(val= decimal_op(&decimal_value)))
 
  934     my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
 
  939     longlong result= int_op();
 
  940     return unsigned_flag ? (double) ((ulonglong) result) : (
double) result;
 
  946     switch (field_type())
 
  948     case MYSQL_TYPE_TIME:
 
  949     case MYSQL_TYPE_DATE:
 
  950     case MYSQL_TYPE_DATETIME:
 
  951     case MYSQL_TYPE_TIMESTAMP:
 
  952       return val_real_from_decimal();
 
  958     String *res= str_op(&str_value);
 
  959     return (res ? my_strntod(res->charset(), (
char*) res->ptr(), res->length(),
 
  960                              &end_not_used, &err_not_used) : 0.0);
 
  969 longlong Item_func_numhybrid::val_int()
 
  971   DBUG_ASSERT(fixed == 1);
 
  972   switch (hybrid_type) {
 
  976     if (!(val= decimal_op(&decimal_value)))
 
  979     my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
 
  985     return (longlong) rint(real_op());
 
  988     switch (field_type())
 
  990     case MYSQL_TYPE_DATE:
 
  991       return val_int_from_date();
 
  992     case MYSQL_TYPE_DATETIME:
 
  993     case MYSQL_TYPE_TIMESTAMP:
 
  994       return val_int_from_datetime();
 
  995     case MYSQL_TYPE_TIME:
 
  996       return val_int_from_time();
 
 1002     if (!(res= str_op(&str_value)))
 
 1005     char *end= (
char*) res->ptr() + res->length();
 
 1007     return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
 
 1019   DBUG_ASSERT(fixed == 1);
 
 1020   switch (hybrid_type) {
 
 1021   case DECIMAL_RESULT:
 
 1022     val= decimal_op(decimal_value);
 
 1026     longlong result= int_op();
 
 1027     int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
 
 1032     double result= (double)real_op();
 
 1033     double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
 
 1038     switch (field_type())
 
 1040     case MYSQL_TYPE_DATE:
 
 1041     case MYSQL_TYPE_DATETIME:
 
 1042     case MYSQL_TYPE_TIMESTAMP:
 
 1043       return val_decimal_from_date(decimal_value);
 
 1044     case MYSQL_TYPE_TIME:
 
 1045       return val_decimal_from_time(decimal_value);
 
 1050     if (!(res= str_op(&str_value)))
 
 1053     str2my_decimal(E_DEC_FATAL_ERROR, (
char*) res->ptr(),
 
 1054                    res->length(), res->charset(), decimal_value);
 
 1065 bool Item_func_numhybrid::get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
 1067   DBUG_ASSERT(fixed == 1);
 
 1068   switch (field_type())
 
 1070   case MYSQL_TYPE_DATE:
 
 1071   case MYSQL_TYPE_DATETIME:
 
 1072   case MYSQL_TYPE_TIMESTAMP:
 
 1073     return date_op(ltime, fuzzydate);
 
 1074   case MYSQL_TYPE_TIME:
 
 1082 bool Item_func_numhybrid::get_time(
MYSQL_TIME *ltime)
 
 1084   DBUG_ASSERT(fixed == 1);
 
 1085   switch (field_type())
 
 1087   case MYSQL_TYPE_TIME:
 
 1088     return time_op(ltime);
 
 1089   case MYSQL_TYPE_DATE:
 
 1091   case MYSQL_TYPE_DATETIME:
 
 1092   case MYSQL_TYPE_TIMESTAMP:
 
 1102   str->append(STRING_WITH_LEN(
"cast("));
 
 1103   args[0]->
print(str, query_type);
 
 1104   str->append(STRING_WITH_LEN(
" as signed)"));
 
 1109 longlong Item_func_signed::val_int_from_str(
int *error)
 
 1111   char buff[MAX_FIELD_WIDTH], *end, *start;
 
 1113   String tmp(buff,
sizeof(buff), &my_charset_bin), *res;
 
 1122   if (!(res= args[0]->val_str(&tmp)))
 
 1129   start= (
char *)res->ptr();
 
 1130   length= res->length();
 
 1133   end= start + length;
 
 1134   value= cs->cset->strtoll10(cs, start, &end, error);
 
 1135   if (*error > 0 || end != start+ length)
 
 1138     push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
 
 1139                         ER_TRUNCATED_WRONG_VALUE,
 
 1140                         ER(ER_TRUNCATED_WRONG_VALUE), 
"INTEGER",
 
 1147 longlong Item_func_signed::val_int()
 
 1152   if (args[0]->cast_to_int_type() != STRING_RESULT ||
 
 1153       args[0]->is_temporal())
 
 1155     value= args[0]->val_int();
 
 1156     null_value= args[0]->null_value; 
 
 1160   value= val_int_from_str(&error);
 
 1161   if (value < 0 && error == 0)
 
 1163     push_warning(current_thd, Sql_condition::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
 1164                  "Cast to signed converted positive out-of-range integer to " 
 1165                  "it's negative complement");
 
 1173   str->append(STRING_WITH_LEN(
"cast("));
 
 1174   args[0]->
print(str, query_type);
 
 1175   str->append(STRING_WITH_LEN(
" as unsigned)"));
 
 1180 longlong Item_func_unsigned::val_int()
 
 1185   if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
 
 1187     my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
 
 1188     if (!(null_value= args[0]->null_value))
 
 1189       my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
 
 1194   else if (args[0]->cast_to_int_type() != STRING_RESULT ||
 
 1195            args[0]->is_temporal())
 
 1197     value= args[0]->val_int();
 
 1198     null_value= args[0]->null_value; 
 
 1202   value= val_int_from_str(&error);
 
 1204     push_warning(current_thd, Sql_condition::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
 1205                  "Cast to unsigned converted negative integer to it's " 
 1206                  "positive complement");
 
 1213   my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
 1216   my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
 
 1221 double Item_decimal_typecast::val_real()
 
 1223   my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
 1227   my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
 
 1232 longlong Item_decimal_typecast::val_int()
 
 1234   my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
 1238   my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
 
 1245   my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
 
 1249   if ((null_value= args[0]->null_value))
 
 1251   my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, FALSE, dec);
 
 1257       my_decimal_set_zero(dec);
 
 1261   precision= my_decimal_length_to_precision(max_length,
 
 1262                                             decimals, unsigned_flag);
 
 1263   if (precision - decimals < (uint) my_decimal_intg(dec))
 
 1265     max_my_decimal(dec, precision, decimals);
 
 1272   push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
 
 1273                       ER_WARN_DATA_OUT_OF_RANGE,
 
 1274                       ER(ER_WARN_DATA_OUT_OF_RANGE),
 
 1275                       item_name.
ptr(), 1L);
 
 1282   char len_buf[20*3 + 1];
 
 1285   uint precision= my_decimal_length_to_precision(max_length, decimals,
 
 1287   str->append(STRING_WITH_LEN(
"cast("));
 
 1288   args[0]->
print(str, query_type);
 
 1289   str->append(STRING_WITH_LEN(
" as decimal("));
 
 1291   end=int10_to_str(precision, len_buf,10);
 
 1292   str->append(len_buf, (uint32) (end - len_buf));
 
 1296   end=int10_to_str(decimals, len_buf,10);
 
 1297   str->append(len_buf, (uint32) (end - len_buf));
 
 1306   double value= args[0]->val_real() + args[1]->val_real();
 
 1307   if ((null_value=args[0]->null_value || args[1]->null_value))
 
 1315   longlong val0= args[0]->val_int();
 
 1316   longlong val1= args[1]->val_int();
 
 1317   longlong res= val0 + val1;
 
 1318   bool     res_unsigned= FALSE;
 
 1320   if ((null_value= args[0]->null_value || args[1]->null_value))
 
 1328   if (args[0]->unsigned_flag)
 
 1330     if (args[1]->unsigned_flag || val1 >= 0)
 
 1332       if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) val1))
 
 1339       if ((ulonglong) val0 > (ulonglong) LONGLONG_MAX)
 
 1345     if (args[1]->unsigned_flag)
 
 1349         if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) val1))
 
 1355         if ((ulonglong) val1 > (ulonglong) LONGLONG_MAX)
 
 1361       if (val0 >=0 && val1 >= 0)
 
 1363       else if (val0 < 0 && val1 < 0 && res >= 0)
 
 1370   return raise_integer_overflow();
 
 1389   val1= args[0]->val_decimal(&value1);
 
 1390   if ((null_value= args[0]->null_value))
 
 1392   val2= args[1]->val_decimal(&value2);
 
 1393   if (!(null_value= (args[1]->null_value ||
 
 1398     return decimal_value;
 
 1407   decimals= max(args[0]->decimals, args[1]->decimals);
 
 1408   int arg1_int= args[0]->decimal_precision() - args[0]->decimals;
 
 1409   int arg2_int= args[1]->decimal_precision() - args[1]->decimals;
 
 1410   int precision= max(arg1_int, arg2_int) + 1 + decimals;
 
 1413   if (result_type() == INT_RESULT)
 
 1414     unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
 1416     unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
 1417   max_length= my_decimal_precision_to_length_no_truncation(precision, decimals,
 
 1429   Item_num_op::fix_length_and_dec();
 
 1430   if (unsigned_flag &&
 
 1431       (current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
 
 1438   double value= args[0]->val_real() - args[1]->val_real();
 
 1439   if ((null_value=args[0]->null_value || args[1]->null_value))
 
 1447   longlong val0= args[0]->val_int();
 
 1448   longlong val1= args[1]->val_int();
 
 1449   longlong res= val0 - val1;
 
 1450   bool     res_unsigned= FALSE;
 
 1452   if ((null_value= args[0]->null_value || args[1]->null_value))
 
 1460   if (args[0]->unsigned_flag)
 
 1462     if (args[1]->unsigned_flag)
 
 1464       if ((ulonglong) val0 < (ulonglong) val1)
 
 1476         if ((ulonglong) val0 > (ulonglong) val1)
 
 1481         if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) -val1))
 
 1489     if (args[1]->unsigned_flag)
 
 1491       if ((ulonglong) (val0 - LONGLONG_MIN) < (ulonglong) val1)
 
 1496       if (val0 > 0 && val1 < 0)
 
 1498       else if (val0 < 0 && val1 > 0 && res >= 0)
 
 1505   return raise_integer_overflow();
 
 1518   val1= args[0]->val_decimal(&value1);
 
 1519   if ((null_value= args[0]->null_value))
 
 1521   val2= args[1]->val_decimal(&value2);
 
 1522   if (!(null_value= (args[1]->null_value ||
 
 1525                                                             decimal_value, val1,
 
 1527     return decimal_value;
 
 1534   DBUG_ASSERT(fixed == 1);
 
 1535   double value= args[0]->val_real() * args[1]->val_real();
 
 1536   if ((null_value=args[0]->null_value || args[1]->null_value))
 
 1544   DBUG_ASSERT(fixed == 1);
 
 1545   longlong a= args[0]->val_int();
 
 1546   longlong b= args[1]->val_int();
 
 1548   ulonglong res0, res1;
 
 1549   ulong a0, a1, b0, b1;
 
 1550   bool     res_unsigned= FALSE;
 
 1551   bool     a_negative= FALSE, b_negative= FALSE;
 
 1553   if ((null_value= args[0]->null_value || args[1]->null_value))
 
 1575   if (!args[0]->unsigned_flag && a < 0)
 
 1580   if (!args[1]->unsigned_flag && b < 0)
 
 1586   a0= 0xFFFFFFFFUL & a;
 
 1587   a1= ((ulonglong) a) >> 32;
 
 1588   b0= 0xFFFFFFFFUL & b;
 
 1589   b1= ((ulonglong) b) >> 32;
 
 1594   res1= (ulonglong) a1 * b0 + (ulonglong) a0 * b1;
 
 1595   if (res1 > 0xFFFFFFFFUL)
 
 1599   res0= (ulonglong) a0 * b0;
 
 1601   if (test_if_sum_overflows_ull(res1, res0))
 
 1605   if (a_negative != b_negative)
 
 1607     if ((ulonglong) res > (ulonglong) LONGLONG_MIN + 1)
 
 1617   return raise_integer_overflow();
 
 1627   val1= args[0]->val_decimal(&value1);
 
 1628   if ((null_value= args[0]->null_value))
 
 1630   val2= args[1]->val_decimal(&value2);
 
 1631   if (!(null_value= (args[1]->null_value ||
 
 1634                                                             decimal_value, val1,
 
 1636     return decimal_value;
 
 1641 void Item_func_mul::result_precision()
 
 1644   if (result_type() == INT_RESULT)
 
 1645     unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
 1647     unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
 1648   decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
 
 1649   uint est_prec = args[0]->decimal_precision() + args[1]->decimal_precision();
 
 1651   max_length= my_decimal_precision_to_length_no_truncation(precision, decimals,
 
 1658   DBUG_ASSERT(fixed == 1);
 
 1659   double value= args[0]->val_real();
 
 1660   double val2= args[1]->val_real();
 
 1661   if ((null_value= args[0]->null_value || args[1]->null_value))
 
 1665     signal_divide_by_null();
 
 1678   val1= args[0]->val_decimal(&value1);
 
 1679   if ((null_value= args[0]->null_value))
 
 1681   val2= args[1]->val_decimal(&value2);
 
 1682   if ((null_value= args[1]->null_value))
 
 1689                                                   prec_increment))) > 3)
 
 1691     if (err == E_DEC_DIV_ZERO)
 
 1692       signal_divide_by_null();
 
 1696   return decimal_value;
 
 1700 void Item_func_div::result_precision()
 
 1702   uint precision= min<uint>(args[0]->decimal_precision() +
 
 1703                             args[1]->decimals + prec_increment,
 
 1707   if (result_type() == INT_RESULT)
 
 1708     unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
 1710     unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
 1711   decimals= min<uint>(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
 
 1712   max_length= my_decimal_precision_to_length_no_truncation(precision, decimals,
 
 1717 void Item_func_div::fix_length_and_dec()
 
 1719   DBUG_ENTER(
"Item_func_div::fix_length_and_dec");
 
 1720   prec_increment= current_thd->variables.div_precincrement;
 
 1721   Item_num_op::fix_length_and_dec();
 
 1722   switch(hybrid_type) {
 
 1725     decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
 
 1726     set_if_smaller(decimals, NOT_FIXED_DEC);
 
 1727     uint tmp=float_length(decimals);
 
 1728     if (decimals == NOT_FIXED_DEC)
 
 1732       max_length=args[0]->max_length - args[0]->decimals + decimals;
 
 1733       set_if_smaller(max_length,tmp);
 
 1738     hybrid_type= DECIMAL_RESULT;
 
 1739     DBUG_PRINT(
"info", (
"Type changed: DECIMAL_RESULT"));
 
 1742   case DECIMAL_RESULT:
 
 1754 longlong Item_func_int_div::val_int()
 
 1756   DBUG_ASSERT(fixed == 1);
 
 1762   if (args[0]->result_type() != INT_RESULT ||
 
 1763       args[1]->result_type() != INT_RESULT)
 
 1766     my_decimal *val0p= args[0]->val_decimal(&tmp);
 
 1767     if ((null_value= args[0]->null_value))
 
 1771     my_decimal *val1p= args[1]->val_decimal(&tmp);
 
 1772     if ((null_value= args[1]->null_value))
 
 1777     if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, &tmp,
 
 1778                              &val0, &val1, 0)) > 3)
 
 1780       if (err == E_DEC_DIV_ZERO)
 
 1781         signal_divide_by_null();
 
 1786     const bool do_truncate= 
true;
 
 1787     if (my_decimal_round(E_DEC_FATAL_ERROR, &tmp, 0, do_truncate, &truncated))
 
 1791     if (my_decimal2int(E_DEC_FATAL_ERROR, &truncated, unsigned_flag, &res) &
 
 1793       raise_integer_overflow();
 
 1797   longlong val0=args[0]->val_int();
 
 1798   longlong val1=args[1]->val_int();
 
 1799   bool val0_negative, val1_negative, res_negative;
 
 1800   ulonglong uval0, uval1, res;
 
 1801   if ((null_value= (args[0]->null_value || args[1]->null_value)))
 
 1805     signal_divide_by_null();
 
 1809   val0_negative= !args[0]->unsigned_flag && val0 < 0;
 
 1810   val1_negative= !args[1]->unsigned_flag && val1 < 0;
 
 1811   res_negative= val0_negative != val1_negative;
 
 1812   uval0= (ulonglong) (val0_negative ? -val0 : val0);
 
 1813   uval1= (ulonglong) (val1_negative ? -val1 : val1);
 
 1817     if (res > (ulonglong) LONGLONG_MAX)
 
 1818       return raise_integer_overflow();
 
 1819     res= (ulonglong) (-(longlong) res);
 
 1825 void Item_func_int_div::fix_length_and_dec()
 
 1827   Item_result argtype= args[0]->result_type();
 
 1829   max_length=args[0]->max_length -
 
 1830     (argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
 
 1831      args[0]->decimals : 0);
 
 1833   unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
 
 1839   DBUG_ASSERT(fixed == 1);
 
 1840   longlong val0= args[0]->val_int();
 
 1841   longlong val1= args[1]->val_int();
 
 1842   bool val0_negative, val1_negative;
 
 1843   ulonglong uval0, uval1;
 
 1846   if ((null_value= args[0]->null_value || args[1]->null_value))
 
 1850     signal_divide_by_null();
 
 1859   val0_negative= !args[0]->unsigned_flag && val0 < 0;
 
 1860   val1_negative= !args[1]->unsigned_flag && val1 < 0;
 
 1861   uval0= (ulonglong) (val0_negative ? -val0 : val0);
 
 1862   uval1= (ulonglong) (val1_negative ? -val1 : val1);
 
 1870   DBUG_ASSERT(fixed == 1);
 
 1871   double value= args[0]->val_real();
 
 1872   double val2=  args[1]->val_real();
 
 1873   if ((null_value= args[0]->null_value || args[1]->null_value))
 
 1877     signal_divide_by_null();
 
 1880   return fmod(value,val2);
 
 1889   val1= args[0]->val_decimal(&value1);
 
 1890   if ((null_value= args[0]->null_value))
 
 1892   val2= args[1]->val_decimal(&value2);
 
 1893   if ((null_value= args[1]->null_value))
 
 1895   switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
 
 1897   case E_DEC_TRUNCATED:
 
 1899     return decimal_value;
 
 1900   case E_DEC_DIV_ZERO:
 
 1901     signal_divide_by_null();
 
 1909 void Item_func_mod::result_precision()
 
 1911   decimals= max(args[0]->decimals, args[1]->decimals);
 
 1912   max_length= max(args[0]->max_length, args[1]->max_length);
 
 1916 void Item_func_mod::fix_length_and_dec()
 
 1918   Item_num_op::fix_length_and_dec();
 
 1920   unsigned_flag= args[0]->unsigned_flag;
 
 1926   double value= args[0]->val_real();
 
 1927   null_value= args[0]->null_value;
 
 1934   longlong value= args[0]->val_int();
 
 1935   if ((null_value= args[0]->null_value))
 
 1937   if (args[0]->unsigned_flag &&
 
 1938       (ulonglong) value > (ulonglong) LONGLONG_MAX + 1ULL)
 
 1939     return raise_integer_overflow();
 
 1942   if (value == LONGLONG_MIN &&
 
 1943       !args[0]->unsigned_flag &&
 
 1945     return raise_integer_overflow();
 
 1952   my_decimal val, *value= args[0]->val_decimal(&val);
 
 1953   if (!(null_value= args[0]->null_value))
 
 1955     my_decimal2decimal(value, decimal_value);
 
 1956     my_decimal_neg(decimal_value);
 
 1957     return decimal_value;
 
 1963 void Item_func_neg::fix_num_length_and_dec()
 
 1965   decimals= args[0]->decimals;
 
 1967   max_length= args[0]->max_length + 1;
 
 1971 void Item_func_neg::fix_length_and_dec()
 
 1973   DBUG_ENTER(
"Item_func_neg::fix_length_and_dec");
 
 1974   Item_func_num1::fix_length_and_dec();
 
 1982   if (hybrid_type == INT_RESULT && args[0]->const_item())
 
 1984     longlong val= args[0]->val_int();
 
 1985     if ((ulonglong) val >= (ulonglong) LONGLONG_MIN &&
 
 1986         ((ulonglong) val != (ulonglong) LONGLONG_MIN ||
 
 1987           args[0]->type() != INT_ITEM))        
 
 1993       hybrid_type= DECIMAL_RESULT;
 
 1994       DBUG_PRINT(
"info", (
"Type changed: DECIMAL_RESULT"));
 
 2004   double value= args[0]->val_real();
 
 2005   null_value= args[0]->null_value;
 
 2012   longlong value= args[0]->val_int();
 
 2013   if ((null_value= args[0]->null_value))
 
 2018   if (value == LONGLONG_MIN)
 
 2019     return raise_integer_overflow();
 
 2020   return (value >= 0) ? value : -value;
 
 2026   my_decimal val, *value= args[0]->val_decimal(&val);
 
 2027   if (!(null_value= args[0]->null_value))
 
 2029     my_decimal2decimal(value, decimal_value);
 
 2030     if (decimal_value->sign())
 
 2031       my_decimal_neg(decimal_value);
 
 2032     return decimal_value;
 
 2038 void Item_func_abs::fix_length_and_dec()
 
 2040   Item_func_num1::fix_length_and_dec();
 
 2041   unsigned_flag= args[0]->unsigned_flag;
 
 2048   DBUG_ASSERT(fixed == 1);
 
 2049   double value= args[0]->val_real();
 
 2050   if ((null_value= args[0]->null_value))
 
 2054     signal_divide_by_null();
 
 2068   DBUG_ASSERT(fixed == 1);
 
 2069   double value= args[0]->val_real();
 
 2070   if ((null_value= args[0]->null_value))
 
 2074     signal_divide_by_null();
 
 2079     double value2= args[1]->val_real();
 
 2080     if ((null_value= args[1]->null_value))
 
 2082     if (value2 <= 0.0 || value == 1.0)
 
 2084       signal_divide_by_null();
 
 2087     return log(value2) / log(value);
 
 2092 double Item_func_log2::val_real()
 
 2094   DBUG_ASSERT(fixed == 1);
 
 2095   double value= args[0]->val_real();
 
 2097   if ((null_value=args[0]->null_value))
 
 2101     signal_divide_by_null();
 
 2104   return log(value) / M_LN2;
 
 2107 double Item_func_log10::val_real()
 
 2109   DBUG_ASSERT(fixed == 1);
 
 2110   double value= args[0]->val_real();
 
 2111   if ((null_value= args[0]->null_value))
 
 2115     signal_divide_by_null();
 
 2118   return log10(value);
 
 2121 double Item_func_exp::val_real()
 
 2123   DBUG_ASSERT(fixed == 1);
 
 2124   double value= args[0]->val_real();
 
 2125   if ((null_value=args[0]->null_value))
 
 2130 double Item_func_sqrt::val_real()
 
 2132   DBUG_ASSERT(fixed == 1);
 
 2133   double value= args[0]->val_real();
 
 2134   if ((null_value=(args[0]->null_value || value < 0)))
 
 2139 double Item_func_pow::val_real()
 
 2141   DBUG_ASSERT(fixed == 1);
 
 2142   double value= args[0]->val_real();
 
 2143   double val2= args[1]->val_real();
 
 2144   if ((null_value=(args[0]->null_value || args[1]->null_value)))
 
 2151 double Item_func_acos::val_real()
 
 2153   DBUG_ASSERT(fixed == 1);
 
 2155   DEBUG_SYNC(current_thd, 
"before_acos_function");
 
 2157   volatile double value= args[0]->val_real();
 
 2158   if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
 
 2163 double Item_func_asin::val_real()
 
 2165   DBUG_ASSERT(fixed == 1);
 
 2167   volatile double value= args[0]->val_real();
 
 2168   if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
 
 2173 double Item_func_atan::val_real()
 
 2175   DBUG_ASSERT(fixed == 1);
 
 2176   double value= args[0]->val_real();
 
 2177   if ((null_value=args[0]->null_value))
 
 2181     double val2= args[1]->val_real();
 
 2182     if ((null_value=args[1]->null_value))
 
 2189 double Item_func_cos::val_real()
 
 2191   DBUG_ASSERT(fixed == 1);
 
 2192   double value= args[0]->val_real();
 
 2193   if ((null_value=args[0]->null_value))
 
 2198 double Item_func_sin::val_real()
 
 2200   DBUG_ASSERT(fixed == 1);
 
 2201   double value= args[0]->val_real();
 
 2202   if ((null_value=args[0]->null_value))
 
 2207 double Item_func_tan::val_real()
 
 2209   DBUG_ASSERT(fixed == 1);
 
 2210   double value= args[0]->val_real();
 
 2211   if ((null_value=args[0]->null_value))
 
 2217 double Item_func_cot::val_real()
 
 2219   DBUG_ASSERT(fixed == 1);
 
 2220   double value= args[0]->val_real();
 
 2221   if ((null_value=args[0]->null_value))
 
 2230 longlong Item_func_shift_left::val_int()
 
 2232   DBUG_ASSERT(fixed == 1);
 
 2234   ulonglong res= ((ulonglong) args[0]->val_int() <<
 
 2235                   (shift=(uint) args[1]->val_int()));
 
 2236   if (args[0]->null_value || args[1]->null_value)
 
 2242   return (shift < 
sizeof(longlong)*8 ? (longlong) res : LL(0));
 
 2245 longlong Item_func_shift_right::val_int()
 
 2247   DBUG_ASSERT(fixed == 1);
 
 2249   ulonglong res= (ulonglong) args[0]->val_int() >>
 
 2250     (shift=(uint) args[1]->val_int());
 
 2251   if (args[0]->null_value || args[1]->null_value)
 
 2257   return (shift < 
sizeof(longlong)*8 ? (longlong) res : LL(0));
 
 2261 longlong Item_func_bit_neg::val_int()
 
 2263   DBUG_ASSERT(fixed == 1);
 
 2264   ulonglong res= (ulonglong) args[0]->val_int();
 
 2265   if ((null_value=args[0]->null_value))
 
 2273 void Item_func_integer::fix_length_and_dec()
 
 2275   max_length=args[0]->max_length - args[0]->decimals+1;
 
 2276   uint tmp=float_length(decimals);
 
 2277   set_if_smaller(max_length,tmp);
 
 2281 void Item_func_int_val::fix_num_length_and_dec()
 
 2283   ulonglong tmp_max_length= (ulonglong ) args[0]->max_length - 
 
 2284     (args[0]->decimals ? args[0]->decimals + 1 : 0) + 2;
 
 2285   max_length= tmp_max_length > (ulonglong) 4294967295
U ?
 
 2286     (uint32) 4294967295
U : (uint32) tmp_max_length;
 
 2287   uint tmp= float_length(decimals);
 
 2288   set_if_smaller(max_length,tmp);
 
 2295   DBUG_ENTER(
"Item_func_int_val::find_num_type");
 
 2296   DBUG_PRINT(
"info", (
"name %s", func_name()));
 
 2297   switch(hybrid_type= args[0]->result_type())
 
 2301     hybrid_type= REAL_RESULT;
 
 2302     max_length= float_length(decimals);
 
 2305   case DECIMAL_RESULT:
 
 2310     if ((args[0]->max_length - args[0]->decimals) >=
 
 2311         (DECIMAL_LONGLONG_DIGITS - 2))
 
 2313       hybrid_type= DECIMAL_RESULT;
 
 2317       unsigned_flag= args[0]->unsigned_flag;
 
 2318       hybrid_type= INT_RESULT;
 
 2324   DBUG_PRINT(
"info", (
"Type: %s",
 
 2325                       (hybrid_type == REAL_RESULT ? 
"REAL_RESULT" :
 
 2326                        hybrid_type == DECIMAL_RESULT ? 
"DECIMAL_RESULT" :
 
 2327                        hybrid_type == INT_RESULT ? 
"INT_RESULT" :
 
 2328                        "--ILLEGAL!!!--")));
 
 2337   switch (args[0]->result_type()) {
 
 2339     result= args[0]->val_int();
 
 2340     null_value= args[0]->null_value;
 
 2342   case DECIMAL_RESULT:
 
 2346       my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
 2364   volatile double value= args[0]->val_real();
 
 2365   null_value= args[0]->null_value;
 
 2372   my_decimal val, *value= args[0]->val_decimal(&val);
 
 2373   if (!(null_value= (args[0]->null_value ||
 
 2374                      my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
 
 2375                                         decimal_value) > 1)))
 
 2376     return decimal_value;
 
 2384   switch (args[0]->result_type()) {
 
 2386     result= args[0]->val_int();
 
 2387     null_value= args[0]->null_value;
 
 2389   case DECIMAL_RESULT:
 
 2393       my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
 2411   volatile double value= args[0]->val_real();
 
 2412   null_value= args[0]->null_value;
 
 2413   return floor(value);
 
 2419   my_decimal val, *value= args[0]->val_decimal(&val);
 
 2420   if (!(null_value= (args[0]->null_value ||
 
 2421                      my_decimal_floor(E_DEC_FATAL_ERROR, value,
 
 2422                                       decimal_value) > 1)))
 
 2423     return decimal_value;
 
 2428 void Item_func_round::fix_length_and_dec()
 
 2430   int      decimals_to_set;
 
 2434   unsigned_flag= args[0]->unsigned_flag;
 
 2435   if (!args[1]->const_item())
 
 2437     decimals= args[0]->decimals;
 
 2438     max_length= float_length(decimals);
 
 2439     if (args[0]->result_type() == DECIMAL_RESULT)
 
 2442       hybrid_type= DECIMAL_RESULT;
 
 2445       hybrid_type= REAL_RESULT;
 
 2449   val1= args[1]->val_int();
 
 2450   if ((null_value= args[1]->is_null()))
 
 2453   val1_unsigned= args[1]->unsigned_flag;
 
 2455     decimals_to_set= val1_unsigned ? INT_MAX : 0;
 
 2457     decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (
int) val1;
 
 2459   if (args[0]->decimals == NOT_FIXED_DEC)
 
 2461     decimals= min(decimals_to_set, NOT_FIXED_DEC);
 
 2462     max_length= float_length(decimals);
 
 2463     hybrid_type= REAL_RESULT;
 
 2467   switch (args[0]->result_type()) {
 
 2470     hybrid_type= REAL_RESULT;
 
 2471     decimals= min(decimals_to_set, NOT_FIXED_DEC);
 
 2472     max_length= float_length(decimals);
 
 2475     if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
 
 2477       int length_can_increase= 
test(!truncate && (val1 < 0) && !val1_unsigned);
 
 2478       max_length= args[0]->max_length + length_can_increase;
 
 2480       hybrid_type= INT_RESULT;
 
 2485   case DECIMAL_RESULT:
 
 2487     hybrid_type= DECIMAL_RESULT;
 
 2488     decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
 
 2489     int decimals_delta= args[0]->decimals - decimals_to_set;
 
 2490     int precision= args[0]->decimal_precision();
 
 2491     int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
 
 2493     precision-= decimals_delta - length_increase;
 
 2494     decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
 
 2495     max_length= my_decimal_precision_to_length_no_truncation(precision,
 
 2505 double my_double_round(
double value, longlong dec, 
bool dec_unsigned,
 
 2509   bool dec_negative= (dec < 0) && !dec_unsigned;
 
 2510   ulonglong abs_dec= dec_negative ? -dec : dec;
 
 2516   volatile double tmp2;
 
 2518   tmp=(abs_dec < array_elements(log_10) ?
 
 2519        log_10[abs_dec] : pow(10.0,(
double) abs_dec));
 
 2522   volatile double value_div_tmp= value / tmp;
 
 2523   volatile double value_mul_tmp= value * tmp;
 
 2525   if (dec_negative && my_isinf(tmp))
 
 2527   else if (!dec_negative && my_isinf(value_mul_tmp))
 
 2532       tmp2= dec < 0 ? floor(value_div_tmp) * tmp : floor(value_mul_tmp) / tmp;
 
 2534       tmp2= dec < 0 ? ceil(value_div_tmp) * tmp : ceil(value_mul_tmp) / tmp;
 
 2537     tmp2=dec < 0 ? rint(value_div_tmp) * tmp : rint(value_mul_tmp) / tmp;
 
 2545   double value= args[0]->val_real();
 
 2547   if (!(null_value= args[0]->null_value || args[1]->null_value))
 
 2548     return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
 
 2559 static inline ulonglong my_unsigned_round(ulonglong value, ulonglong 
to)
 
 2561   ulonglong tmp= value / to * 
to;
 
 2562   return (value - tmp < (to >> 1)) ? tmp : tmp + 
to;
 
 2568   longlong value= args[0]->val_int();
 
 2569   longlong dec= args[1]->val_int();
 
 2572   if ((null_value= args[0]->null_value || args[1]->null_value))
 
 2574   if ((dec >= 0) || args[1]->unsigned_flag)
 
 2580   if(abs_dec >= array_elements(log_10_int))
 
 2583   tmp= log_10_int[abs_dec];
 
 2586     value= (unsigned_flag) ?
 
 2587       ((ulonglong) value / tmp) * tmp : (value / tmp) * tmp;
 
 2589     value= (unsigned_flag || value >= 0) ?
 
 2590       my_unsigned_round((ulonglong) value, tmp) :
 
 2591       -(longlong) my_unsigned_round((ulonglong) -value, tmp);
 
 2598   my_decimal val, *value= args[0]->val_decimal(&val);
 
 2599   longlong dec= args[1]->val_int();
 
 2600   if (dec >= 0 || args[1]->unsigned_flag)
 
 2601     dec= min<ulonglong>(dec, decimals);
 
 2602   else if (dec < INT_MIN)
 
 2605   if (!(null_value= (args[0]->null_value || args[1]->null_value ||
 
 2606                      my_decimal_round(E_DEC_FATAL_ERROR, value, (
int) dec,
 
 2607                                       truncate, decimal_value) > 1))) 
 
 2608     return decimal_value;
 
 2613 void Item_func_rand::seed_random(
Item *arg)
 
 2619   uint32 tmp= (uint32) arg->val_int();
 
 2620   randominit(rand, (uint32) (tmp*0x10001L+55555555L),
 
 2621              (uint32) (tmp*0x10000001L));
 
 2625 bool Item_func_rand::fix_fields(THD *thd,
Item **ref)
 
 2627   if (Item_real_func::fix_fields(thd, ref))
 
 2641                    thd->stmt_arena->alloc(
sizeof(*rand))))
 
 2651     if (!thd->rand_used)
 
 2654       thd->rand_saved_seed1= thd->rand.seed1;
 
 2655       thd->rand_saved_seed2= thd->rand.seed2;
 
 2663 double Item_func_rand::val_real()
 
 2665   DBUG_ASSERT(fixed == 1);
 
 2668     if (!args[0]->const_item())
 
 2669       seed_random(args[0]);
 
 2670     else if (first_eval)
 
 2678       seed_random(args[0]);
 
 2681   return my_rnd(rand);
 
 2684 longlong Item_func_sign::val_int()
 
 2686   DBUG_ASSERT(fixed == 1);
 
 2687   double value= args[0]->val_real();
 
 2688   null_value=args[0]->null_value;
 
 2689   return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
 
 2693 double Item_func_units::val_real()
 
 2695   DBUG_ASSERT(fixed == 1);
 
 2696   double value= args[0]->val_real();
 
 2697   if ((null_value=args[0]->null_value))
 
 2703 void Item_func_min_max::fix_length_and_dec()
 
 2705   uint string_arg_count= 0;
 
 2707   bool datetime_found= FALSE;
 
 2713   for (uint 
i=0 ; 
i < arg_count ; 
i++)
 
 2715     set_if_bigger(max_length, args[
i]->max_length);
 
 2716     set_if_bigger(decimals, args[
i]->decimals);
 
 2717     set_if_bigger(max_int_part, args[
i]->decimal_int_part());
 
 2718     if (args[
i]->maybe_null)
 
 2720     cmp_type= item_cmp_type(cmp_type,
 
 2722     if (args[
i]->result_type() == STRING_RESULT)
 
 2724     if (args[
i]->result_type() != ROW_RESULT &&
 
 2725         args[
i]->is_temporal_with_date())
 
 2727       datetime_found= TRUE;
 
 2728       if (!datetime_item || args[
i]->field_type() == MYSQL_TYPE_DATETIME)
 
 2729         datetime_item= args[
i];
 
 2733   if (string_arg_count == arg_count)
 
 2736     agg_arg_charsets_for_string_result_with_comparison(collation,
 
 2741       compare_as_dates= TRUE;
 
 2751   else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
 
 2753     collation.set_numeric();
 
 2754     fix_char_length(my_decimal_precision_to_length_no_truncation(max_int_part +
 
 2759   else if (cmp_type == REAL_RESULT)
 
 2760     fix_char_length(float_length(decimals));
 
 2783 uint Item_func_min_max::cmp_datetimes(longlong *value)
 
 2785   longlong UNINIT_VAR(min_max);
 
 2786   uint min_max_idx= 0;
 
 2788   for (uint 
i=0; 
i < arg_count ; 
i++)
 
 2790     Item **arg= args + 
i;
 
 2792     longlong res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
 
 2795     if (thd->is_error())
 
 2801     if ((null_value= args[i]->null_value))
 
 2803     if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
 
 2815 uint Item_func_min_max::cmp_times(longlong *value)
 
 2817   longlong UNINIT_VAR(min_max);
 
 2818   uint min_max_idx= 0;
 
 2819   for (uint i=0; i < arg_count ; i++)
 
 2822     if ((null_value= args[i]->null_value))
 
 2824     if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
 
 2838   DBUG_ASSERT(fixed == 1);
 
 2839   if (compare_as_dates)
 
 2852       cmp_datetimes(&result);
 
 2856       TIME_from_longlong_packed(<ime, field_type(), result);
 
 2857       return (null_value= my_TIME_to_str(<ime, str, decimals)) ?
 
 2867       uint min_max_idx= cmp_datetimes(NULL);
 
 2870       str_res= args[min_max_idx]->val_str(str);
 
 2871       if (args[min_max_idx]->null_value)
 
 2877       str_res->set_charset(collation.collation);
 
 2885     longlong nr=val_int();
 
 2888     str->set_int(nr, unsigned_flag, collation.collation);
 
 2891   case DECIMAL_RESULT:
 
 2893     my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
 
 2896     my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
 
 2901     double nr= val_real();
 
 2904     str->set_real(nr, decimals, collation.collation);
 
 2910     for (uint i=0; i < arg_count ; i++)
 
 2913         res=args[
i]->val_str(str);
 
 2917         res2= args[
i]->val_str(res == str ? &tmp_value : str);
 
 2920           int cmp= sortcmp(res,res2,collation.collation);
 
 2921           if ((cmp_sign < 0 ? cmp : -cmp) < 0)
 
 2925       if ((null_value= args[i]->null_value))
 
 2928     res->set_charset(collation.collation);
 
 2941 bool Item_func_min_max::get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
 2943   DBUG_ASSERT(fixed == 1);
 
 2944   if (compare_as_dates)
 
 2947     cmp_datetimes(&result);
 
 2950     TIME_from_longlong_packed(ltime, datetime_item->field_type(), result);
 
 2952     return check_date(ltime, non_zero_date(ltime), fuzzydate, &warnings);
 
 2955   switch (field_type())
 
 2957   case MYSQL_TYPE_TIME:
 
 2959   case MYSQL_TYPE_DATETIME:
 
 2960   case MYSQL_TYPE_TIMESTAMP:
 
 2961   case MYSQL_TYPE_DATE:
 
 2969 bool Item_func_min_max::get_time(
MYSQL_TIME *ltime)
 
 2971   DBUG_ASSERT(fixed == 1);
 
 2972   if (compare_as_dates)
 
 2975     cmp_datetimes(&result);
 
 2978     TIME_from_longlong_packed(ltime, datetime_item->field_type(), result);
 
 2979     datetime_to_time(ltime);
 
 2983   switch (field_type())
 
 2985   case MYSQL_TYPE_TIME:
 
 2991       TIME_from_longlong_time_packed(ltime, result);
 
 2995   case MYSQL_TYPE_DATE:
 
 2996   case MYSQL_TYPE_TIMESTAMP:
 
 2997   case MYSQL_TYPE_DATETIME:
 
 3006 double Item_func_min_max::val_real()
 
 3008   DBUG_ASSERT(fixed == 1);
 
 3010   if (compare_as_dates)
 
 3013     (void)cmp_datetimes(&result);
 
 3014     return double_from_datetime_packed(datetime_item->field_type(), result);
 
 3016   for (uint i=0; i < arg_count ; i++)
 
 3019       value= args[
i]->val_real();
 
 3022       double tmp= args[
i]->val_real();
 
 3023       if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
 3026     if ((null_value= args[i]->null_value))
 
 3033 longlong Item_func_min_max::val_int()
 
 3035   DBUG_ASSERT(fixed == 1);
 
 3037   if (compare_as_dates)
 
 3040     (void)cmp_datetimes(&result);
 
 3041     return longlong_from_datetime_packed(datetime_item->field_type(), result);
 
 3060   for (uint i=0; i < arg_count ; i++)
 
 3063       value=args[
i]->val_int();
 
 3066       longlong tmp=args[
i]->val_int();
 
 3067       if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
 3070     if ((null_value= args[i]->null_value))
 
 3079   DBUG_ASSERT(fixed == 1);
 
 3082   if (compare_as_dates)
 
 3085     (void)cmp_datetimes(&value);
 
 3086     return my_decimal_from_datetime_packed(dec, datetime_item->field_type(),
 
 3089   for (uint i=0; i < arg_count ; i++)
 
 3092       res= args[
i]->val_decimal(dec);
 
 3095       tmp= args[
i]->val_decimal(&tmp_buf);      
 
 3098         if (tmp == &tmp_buf)
 
 3101           my_decimal2decimal(tmp, dec);
 
 3108     if ((null_value= args[i]->null_value))
 
 3121     my_decimal_round(E_DEC_FATAL_ERROR, res, decimals, 
false, res);
 
 3127 longlong Item_func_length::val_int()
 
 3129   DBUG_ASSERT(fixed == 1);
 
 3130   String *res=args[0]->val_str(&value);
 
 3137   return (longlong) res->length();
 
 3141 longlong Item_func_char_length::val_int()
 
 3143   DBUG_ASSERT(fixed == 1);
 
 3144   String *res=args[0]->val_str(&value);
 
 3151   return (longlong) res->numchars();
 
 3155 longlong Item_func_coercibility::val_int()
 
 3157   DBUG_ASSERT(fixed == 1);
 
 3159   return (longlong) args[0]->collation.derivation;
 
 3163 void Item_func_locate::fix_length_and_dec()
 
 3165   max_length= MY_INT32_NUM_DECIMAL_DIGITS;
 
 3166   agg_arg_charsets_for_comparison(cmp_collation, args, 2);
 
 3170 longlong Item_func_locate::val_int()
 
 3172   DBUG_ASSERT(fixed == 1);
 
 3173   String *a=args[0]->val_str(&value1);
 
 3174   String *b=args[1]->val_str(&value2);
 
 3188     start0= start= args[2]->val_int() - 1;
 
 3190     if ((start < 0) || (start > a->length()))
 
 3194     start= a->charpos((
int) start);
 
 3196     if (start + b->length() > a->length())
 
 3203   if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
 
 3205                                             (uint) (a->length()-start),
 
 3206                                             b->ptr(), b->length(),
 
 3209   return (longlong) match.mb_len + start0 + 1;
 
 3215   str->append(STRING_WITH_LEN(
"locate("));
 
 3216   args[1]->
print(str, query_type);
 
 3218   args[0]->
print(str, query_type);
 
 3222     args[2]->
print(str, query_type);
 
 3228 longlong Item_func_validate_password_strength::val_int()
 
 3230   String *field= args[0]->val_str(&value);
 
 3231   if ((null_value= args[0]->null_value))
 
 3233   return (check_password_strength(field));
 
 3237 longlong Item_func_field::val_int()
 
 3239   DBUG_ASSERT(fixed == 1);
 
 3241   if (cmp_type == STRING_RESULT)
 
 3244     if (!(field= args[0]->val_str(&value)))
 
 3246     for (uint i=1 ; i < arg_count ; i++)
 
 3248       String *tmp_value=args[
i]->val_str(&tmp);
 
 3249       if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
 
 3250         return (longlong) (
i);
 
 3253   else if (cmp_type == INT_RESULT)
 
 3255     longlong val= args[0]->val_int();
 
 3256     if (args[0]->null_value)
 
 3258     for (uint i=1; i < arg_count ; i++)
 
 3260       if (val == args[i]->val_int() && !args[i]->null_value)
 
 3261         return (longlong) (
i);
 
 3264   else if (cmp_type == DECIMAL_RESULT)
 
 3267                dec_buf, *dec= args[0]->val_decimal(&dec_buf);
 
 3268     if (args[0]->null_value)
 
 3270     for (uint i=1; i < arg_count; i++)
 
 3272       dec_arg= args[
i]->val_decimal(&dec_arg_buf);
 
 3274         return (longlong) (
i);
 
 3279     double val= args[0]->val_real();
 
 3280     if (args[0]->null_value)
 
 3282     for (uint i=1; i < arg_count ; i++)
 
 3284       if (val == args[i]->val_real() && !args[i]->null_value)
 
 3285         return (longlong) (
i);
 
 3292 void Item_func_field::fix_length_and_dec()
 
 3294   maybe_null=0; max_length=3;
 
 3295   cmp_type= args[0]->result_type();
 
 3296   for (uint i=1; i < arg_count ; i++)
 
 3297     cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
 
 3298   if (cmp_type == STRING_RESULT)
 
 3299     agg_arg_charsets_for_comparison(cmp_collation, args, arg_count);
 
 3303 longlong Item_func_ascii::val_int()
 
 3305   DBUG_ASSERT(fixed == 1);
 
 3306   String *res=args[0]->val_str(&value);
 
 3313   return (longlong) (res->length() ? (uchar) (*res)[0] : (uchar) 0);
 
 3316 longlong Item_func_ord::val_int()
 
 3318   DBUG_ASSERT(fixed == 1);
 
 3319   String *res=args[0]->val_str(&value);
 
 3326   if (!res->length()) 
return 0;
 
 3328   if (use_mb(res->charset()))
 
 3330     register const char *str=res->ptr();
 
 3331     register uint32 
n=0, l=my_ismbchar(res->charset(),str,str+res->length());
 
 3333       return (longlong)((uchar) *str);
 
 3335       n=(n<<8)|(uint32)((uchar) *str++);
 
 3336     return (longlong) 
n;
 
 3339   return (longlong) ((uchar) (*res)[0]);
 
 3346 void Item_func_find_in_set::fix_length_and_dec()
 
 3350   if (args[0]->const_item() && args[1]->type() == FIELD_ITEM)
 
 3353     if (field->real_type() == MYSQL_TYPE_SET)
 
 3355       String *find=args[0]->val_str(&value);
 
 3359         DBUG_ASSERT(!args[0]->null_value);
 
 3360         enum_value= find_type(((
Field_enum*) field)->typelib,find->ptr(),
 
 3364           enum_bit=LL(1) << (enum_value-1);
 
 3368   agg_arg_charsets_for_comparison(cmp_collation, args, 2);
 
 3371 static const char separator=
',';
 
 3373 longlong Item_func_find_in_set::val_int()
 
 3375   DBUG_ASSERT(fixed == 1);
 
 3379     DBUG_ASSERT(args[0]->const_item());
 
 3381     ulonglong tmp= (ulonglong) args[1]->val_int();
 
 3382     null_value= args[1]->null_value;
 
 3398   String *find=args[0]->val_str(&value);
 
 3399   String *buffer=args[1]->val_str(&value2);
 
 3400   if (!find || !buffer)
 
 3408   if ((diff=buffer->length() - find->length()) >= 0)
 
 3412     const char *str_begin= buffer->ptr();
 
 3413     const char *str_end= buffer->ptr();
 
 3414     const char *real_end= str_end+buffer->length();
 
 3415     const uchar *find_str= (
const uchar *) find->ptr();
 
 3416     uint find_str_len= find->length();
 
 3421       if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end, 
 
 3422                                        (uchar*) real_end)) > 0)
 
 3424         const char *substr_end= str_end + symbol_len;
 
 3425         bool is_last_item= (substr_end == real_end);
 
 3426         bool is_separator= (wc == (my_wc_t) separator);
 
 3427         if (is_separator || is_last_item)
 
 3430           if (is_last_item && !is_separator)
 
 3431             str_end= substr_end;
 
 3432           if (!my_strnncoll(cs, (
const uchar *) str_begin,
 
 3433                             (uint) (str_end - str_begin),
 
 3434                             find_str, find_str_len))
 
 3435             return (longlong) position;
 
 3437             str_begin= substr_end;
 
 3439         str_end= substr_end;
 
 3441       else if (str_end - str_begin == 0 &&
 
 3442                find_str_len == 0 &&
 
 3443                wc == (my_wc_t) separator)
 
 3444         return (longlong) ++position;
 
 3452 longlong Item_func_bit_count::val_int()
 
 3454   DBUG_ASSERT(fixed == 1);
 
 3455   ulonglong value= (ulonglong) args[0]->val_int();
 
 3456   if ((null_value= args[0]->null_value))
 
 3458   return (longlong) my_count_bits(value);
 
 3470 void udf_handler::cleanup()
 
 3476       if (u_d->func_deinit != NULL)
 
 3478         Udf_func_deinit deinit= u_d->func_deinit;
 
 3493                         uint arg_count, 
Item **arguments)
 
 3496   DBUG_ENTER(
"Item_udf_func::fix_fields");
 
 3501   udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length,1);
 
 3505     my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str);
 
 3516   if ((f_args.arg_count=arg_count))
 
 3518     if (!(f_args.arg_type= (Item_result*)
 
 3519           sql_alloc(f_args.arg_count*
sizeof(Item_result))))
 
 3526     Item **arg,**arg_end;
 
 3527     for (i=0, arg=arguments, arg_end=arguments+arg_count;
 
 3531       if (!(*arg)->fixed &&
 
 3532           (*arg)->fix_fields(thd, arg))
 
 3536       if (item->check_cols(1))
 
 3549       if (item->collation.collation->state & MY_CS_BINSORT)
 
 3550         func->collation.set(&my_charset_bin);
 
 3551       if (item->maybe_null)
 
 3553       func->with_sum_func= func->with_sum_func || item->with_sum_func;
 
 3556       f_args.arg_type[
i]=item->result_type();
 
 3559     if (!(buffers=
new String[arg_count]) ||
 
 3560         !(f_args.args= (
char**) sql_alloc(arg_count * 
sizeof(
char *))) ||
 
 3561         !(f_args.lengths= (ulong*) sql_alloc(arg_count * 
sizeof(
long))) ||
 
 3562         !(f_args.maybe_null= (
char*) sql_alloc(arg_count * 
sizeof(
char))) ||
 
 3563         !(num_buffer= (
char*) sql_alloc(arg_count *
 
 3564                                         ALIGN_SIZE(
sizeof(
double)))) ||
 
 3565         !(f_args.attributes= (
char**) sql_alloc(arg_count * 
sizeof(
char *))) ||
 
 3566         !(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
 
 3573   func->fix_length_and_dec();
 
 3574   initid.max_length=func->max_length;
 
 3575   initid.maybe_null=func->maybe_null;
 
 3577   initid.decimals=func->decimals;
 
 3582     char init_msg_buff[MYSQL_ERRMSG_SIZE];
 
 3583     char *to=num_buffer;
 
 3584     for (uint i=0; i < arg_count; i++)
 
 3590       f_args.args[
i]= NULL;         
 
 3592       f_args.lengths[
i]= arguments[
i]->max_length;
 
 3593       f_args.maybe_null[
i]= (char) arguments[i]->maybe_null;
 
 3594       f_args.attributes[
i]= (
char*) arguments[i]->item_name.
ptr();
 
 3595       f_args.attribute_lengths[
i]= arguments[
i]->item_name.
length();
 
 3597       if (arguments[i]->const_item())
 
 3599         switch (arguments[i]->result_type()) 
 
 3602         case DECIMAL_RESULT:
 
 3604           String *res= arguments[
i]->val_str(&buffers[i]);
 
 3605           if (arguments[i]->null_value)
 
 3607           f_args.args[
i]= (
char*) res->c_ptr_safe();
 
 3608           f_args.lengths[
i]= res->length();
 
 3612           *((longlong*) to)= arguments[
i]->val_int();
 
 3613           if (arguments[i]->null_value)
 
 3616           to+= ALIGN_SIZE(
sizeof(longlong));
 
 3619           *((
double*) to)= arguments[
i]->val_real();
 
 3620           if (arguments[i]->null_value)
 
 3623           to+= ALIGN_SIZE(
sizeof(
double));
 
 3633     Udf_func_init init= u_d->func_init;
 
 3634     if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
 
 3636       my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
 
 3637                u_d->name.str, init_msg_buff);
 
 3641     func->max_length= min<size_t>(initid.max_length, MAX_BLOB_WIDTH);
 
 3642     func->maybe_null=initid.maybe_null;
 
 3650     func->decimals= min<uint>(initid.decimals, NOT_FIXED_DEC);
 
 3655     my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
 
 3656              u_d->name.str, ER(ER_UNKNOWN_ERROR));
 
 3663 bool udf_handler::get_arguments()
 
 3667   char *to= num_buffer;
 
 3669   for (uint i=0; i < f_args.arg_count; i++)
 
 3672     switch (f_args.arg_type[i]) {
 
 3674     case DECIMAL_RESULT:
 
 3676         String *res=args[
i]->val_str(&buffers[str_count++]);
 
 3677         if (!(args[i]->null_value))
 
 3679           f_args.args[
i]=    (
char*) res->ptr();
 
 3680           f_args.lengths[
i]= res->length();
 
 3685       *((longlong*) to) = args[
i]->val_int();
 
 3686       if (!args[i]->null_value)
 
 3689         to+= ALIGN_SIZE(
sizeof(longlong));
 
 3693       *((
double*) to)= args[
i]->val_real();
 
 3694       if (!args[i]->null_value)
 
 3697         to+= ALIGN_SIZE(
sizeof(
double));
 
 3716   uchar is_null_tmp=0;
 
 3718   DBUG_ENTER(
"udf_handler::val_str");
 
 3720   if (get_arguments())
 
 3723     (
char* (*)(UDF_INIT *, UDF_ARGS *, 
char *, ulong *, uchar *, uchar *))
 
 3726   if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
 
 3728     if (str->alloc(MAX_FIELD_WIDTH))
 
 3734   char *res=func(&initid, &f_args, (
char*) str->ptr(), &res_length,
 
 3735                  &is_null_tmp, &error);
 
 3736   DBUG_PRINT(
"info", (
"udf func returned, res_length: %lu", res_length));
 
 3737   if (is_null_tmp || !res || error)             
 
 3739     DBUG_PRINT(
"info", (
"Null or error"));
 
 3742   if (res == str->ptr())
 
 3744     str->length(res_length);
 
 3745     DBUG_PRINT(
"exit", (
"str: %*.s", (
int) str->length(), str->ptr()));
 
 3748   save_str->set(res, res_length, str->charset());
 
 3749   DBUG_PRINT(
"exit", (
"save_str: %s", save_str->ptr()));
 
 3750   DBUG_RETURN(save_str);
 
 3763   if (get_arguments())
 
 3768   char *(*func)(UDF_INIT *, UDF_ARGS *, 
char *, ulong *, uchar *, uchar *)=
 
 3769     (
char* (*)(UDF_INIT *, UDF_ARGS *, 
char *, ulong *, uchar *, uchar *))
 
 3772   char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
 
 3773   if (is_null || error)
 
 3778   end= res+ res_length;
 
 3779   str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
 
 3784 void Item_udf_func::cleanup()
 
 3787   Item_func::cleanup();
 
 3793   str->append(func_name());
 
 3795   for (uint i=0 ; i < arg_count ; i++)
 
 3799     args[
i]->print_item_w_name(str, query_type);
 
 3805 double Item_func_udf_float::val_real()
 
 3807   DBUG_ASSERT(fixed == 1);
 
 3808   DBUG_ENTER(
"Item_func_udf_float::val");
 
 3809   DBUG_PRINT(
"info",(
"result_type: %d  arg_count: %d",
 
 3810                      args[0]->result_type(), arg_count));
 
 3811   DBUG_RETURN(udf.val(&null_value));
 
 3817   DBUG_ASSERT(fixed == 1);
 
 3818   double nr= val_real();
 
 3821   str->set_real(nr,decimals,&my_charset_bin);
 
 3826 longlong Item_func_udf_int::val_int()
 
 3828   DBUG_ASSERT(fixed == 1);
 
 3829   DBUG_ENTER(
"Item_func_udf_int::val_int");
 
 3830   DBUG_RETURN(udf.val_int(&null_value));
 
 3836   DBUG_ASSERT(fixed == 1);
 
 3837   longlong nr=val_int();
 
 3840   str->set_int(nr, unsigned_flag, &my_charset_bin);
 
 3845 longlong Item_func_udf_decimal::val_int()
 
 3847   my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
 3851   my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
 3856 double Item_func_udf_decimal::val_real()
 
 3858   my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
 3862   my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
 
 3869   DBUG_ASSERT(fixed == 1);
 
 3870   DBUG_ENTER(
"Item_func_udf_decimal::val_decimal");
 
 3871   DBUG_PRINT(
"info",(
"result_type: %d  arg_count: %d",
 
 3872                      args[0]->result_type(), arg_count));
 
 3874   DBUG_RETURN(udf.val_decimal(&null_value, dec_buf));
 
 3880   my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
 3885   my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, FALSE, &dec_buf);
 
 3886   my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 
'0', str);
 
 3891 void Item_func_udf_decimal::fix_length_and_dec()
 
 3893   fix_num_length_and_dec();
 
 3899 void Item_func_udf_str::fix_length_and_dec()
 
 3901   DBUG_ENTER(
"Item_func_udf_str::fix_length_and_dec");
 
 3903   for (uint i = 0; i < arg_count; i++)
 
 3904     set_if_bigger(max_length,args[i]->max_length);
 
 3910   DBUG_ASSERT(fixed == 1);
 
 3911   String *res=udf.val_str(str,&str_value);
 
 3923 udf_handler::~udf_handler()
 
 3926   DBUG_ASSERT(not_original || !(initialized || buffers));
 
 3930 bool udf_handler::get_arguments() { 
return 0; }
 
 3938 static HASH hash_user_locks;
 
 3949   my_thread_id thread_id;
 
 3950   void set_thread(THD *thd) { thread_id= thd->thread_id; }
 
 3953     :key_length(length),count(1),locked(1), thread_id(
id)
 
 3955     key= (uchar*) my_memdup(key_arg,length,MYF(0));
 
 3959       if (my_hash_insert(&hash_user_locks,(uchar*) 
this))
 
 3970       my_hash_delete(&hash_user_locks,(uchar*) 
this);
 
 3975   inline bool initialized() { 
return key != 0; }
 
 3982                    my_bool not_used __attribute__((unused)))
 
 3984   *length= ull->key_length;
 
 3988 #ifdef HAVE_PSI_INTERFACE 
 3989 static PSI_mutex_key key_LOCK_user_locks;
 
 3991 static PSI_mutex_info all_user_mutexes[]=
 
 3993   { &key_LOCK_user_locks, 
"LOCK_user_locks", PSI_FLAG_GLOBAL}
 
 3996 static void init_user_lock_psi_keys(
void)
 
 4000   count= array_elements(all_user_mutexes);
 
 4005 static bool item_user_lock_inited= 0;
 
 4007 void item_user_lock_init(
void)
 
 4009 #ifdef HAVE_PSI_INTERFACE 
 4010   init_user_lock_psi_keys();
 
 4013   mysql_mutex_init(key_LOCK_user_locks, &LOCK_user_locks, MY_MUTEX_INIT_SLOW);
 
 4014   my_hash_init(&hash_user_locks,system_charset_info,
 
 4015             16,0,0,(my_hash_get_key) ull_get_key,NULL,0);
 
 4016   item_user_lock_inited= 1;
 
 4019 void item_user_lock_free(
void)
 
 4021   if (item_user_lock_inited)
 
 4023     item_user_lock_inited= 0;
 
 4024     my_hash_free(&hash_user_locks);
 
 4046   DBUG_ASSERT(fixed == 1);
 
 4047   THD* thd = current_thd;
 
 4048   String *log_name = args[0]->val_str(&value);
 
 4052   if (thd->slave_thread || !log_name || !log_name->length())
 
 4057 #ifdef HAVE_REPLICATION 
 4058   longlong pos = (ulong)args[1]->val_int();
 
 4059   longlong timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
 
 4060   if (active_mi == NULL ||
 
 4061       (event_count = active_mi->rli->wait_for_pos(thd, log_name, pos, timeout)) == -2)
 
 4070 longlong Item_master_gtid_set_wait::val_int()
 
 4072   DBUG_ASSERT(fixed == 1);
 
 4073   THD* thd = current_thd;
 
 4074   String *gtid= args[0]->val_str(&value);
 
 4078   if (thd->slave_thread || !gtid || 0 == gtid_mode)
 
 4084 #if defined(HAVE_REPLICATION) 
 4085   longlong timeout = (arg_count== 2) ? args[1]->val_int() : 0;
 
 4086   if (active_mi && active_mi->rli)
 
 4088     if ((event_count = active_mi->rli->wait_for_gtid_set(thd, gtid, timeout))
 
 4112   DBUG_ENTER(
"Item_func_gtid_subset::val_int()");
 
 4113   if (args[0]->null_value || args[1]->null_value)
 
 4118   String *string1, *string2;
 
 4119   const char *charp1, *charp2;
 
 4121   enum_return_status status;
 
 4123   if ((string1= args[0]->val_str(&buf1)) != NULL &&
 
 4124       (charp1= string1->c_ptr_safe()) != NULL &&
 
 4125       (string2= args[1]->val_str(&buf2)) != NULL &&
 
 4126       (charp2= string2->c_ptr_safe()) != NULL)
 
 4130     const Gtid_set sub_set(&sid_map, charp1, &status);
 
 4131     if (status == RETURN_STATUS_OK)
 
 4133       const Gtid_set super_set(&sid_map, charp2, &status);
 
 4134       if (status == RETURN_STATUS_OK)
 
 4135         ret= sub_set.
is_subset(&super_set) ? 1 : 0;
 
 4153   static const ulonglong m_interrupt_interval;
 
 4167     void set_timeout(ulonglong timeout)
 
 4175       set_timespec_nsec(m_abs_timeout, timeout);
 
 4184 const ulonglong Interruptible_wait::m_interrupt_interval= 5 * ULL(1000000000);
 
 4206     set_timespec_nsec(timeout, m_interrupt_interval);
 
 4209     if (cmp_timespec(timeout, m_abs_timeout) > 0)
 
 4210       timeout= m_abs_timeout;
 
 4213     if (error == ETIMEDOUT || error == ETIME)
 
 4216       if (!cmp_timespec(timeout, m_abs_timeout) || !m_thd->is_connected())
 
 4241   DBUG_ASSERT(fixed == 1);
 
 4242   String *res=args[0]->val_str(&value);
 
 4243   ulonglong timeout= args[1]->val_int();
 
 4244   THD *thd=current_thd;
 
 4248   DBUG_ENTER(
"Item_func_get_lock::val_int");
 
 4257   if (thd->slave_thread)
 
 4262   if (!res || !res->length())
 
 4268   DBUG_PRINT(
"info", (
"lock %.*s, thd=%ld", res->length(), res->ptr(),
 
 4269                       (long) thd->real_id));
 
 4274     item_user_lock_release(thd->ull);
 
 4279                                                  (uchar*) res->ptr(),
 
 4280                                                  (size_t) res->length()))))
 
 4284     if (!ull || !ull->initialized())
 
 4291     ull->set_thread(thd);
 
 4294     DBUG_PRINT(
"info", (
"made new lock"));
 
 4298   DBUG_PRINT(
"info", (
"ull->count=%d", ull->count));
 
 4304   THD_STAGE_INFO(thd, stage_user_lock);
 
 4305   thd->mysys_var->current_mutex= &LOCK_user_locks;
 
 4306   thd->mysys_var->current_cond=  &ull->cond;
 
 4308   timed_cond.
set_timeout(timeout * ULL(1000000000));
 
 4312   while (ull->locked && !thd->killed)
 
 4314     DBUG_PRINT(
"info", (
"waiting on lock"));
 
 4315     error= timed_cond.
wait(&ull->cond, &LOCK_user_locks);
 
 4316     if (error == ETIMEDOUT || error == ETIME)
 
 4318       DBUG_PRINT(
"info", (
"lock wait timeout"));
 
 4341     ull->set_thread(thd);
 
 4342     ull->thread_id= thd->thread_id;
 
 4345     DBUG_PRINT(
"info", (
"got the lock"));
 
 4350   thd->mysys_var->current_mutex= 0;
 
 4351   thd->mysys_var->current_cond=  0;
 
 4354   DBUG_RETURN(!error ? 1 : 0);
 
 4368   DBUG_ASSERT(fixed == 1);
 
 4369   String *res=args[0]->val_str(&value);
 
 4372   THD *thd=current_thd;
 
 4373   DBUG_ENTER(
"Item_func_release_lock::val_int");
 
 4374   if (!res || !res->length())
 
 4379   DBUG_PRINT(
"info", (
"lock %.*s", res->length(), res->ptr()));
 
 4385                                                 (
const uchar*) res->ptr(),
 
 4386                                                 (size_t) res->length()))))
 
 4392     DBUG_PRINT(
"info", (
"ull->locked=%d ull->thread=%lu thd=%lu", 
 
 4394                         (
long)ull->thread_id,
 
 4395                         (
long)thd->thread_id));
 
 4396     if (ull->locked && current_thd->thread_id == ull->thread_id)
 
 4398       DBUG_PRINT(
"info", (
"release lock"));
 
 4400       item_user_lock_release(ull);
 
 4405   DBUG_RETURN(result);
 
 4409 longlong Item_func_last_insert_id::val_int()
 
 4411   THD *thd= current_thd;
 
 4412   DBUG_ASSERT(fixed == 1);
 
 4415     longlong value= args[0]->val_int();
 
 4416     null_value= args[0]->null_value;
 
 4424     thd->arg_of_last_insert_id_function= TRUE;
 
 4425     thd->first_successful_insert_id_in_prev_stmt= value;
 
 4429     static_cast<longlong
>(thd->read_first_successful_insert_id_in_prev_stmt());
 
 4433 bool Item_func_last_insert_id::fix_fields(THD *thd, 
Item **ref)
 
 4435   thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
 
 4436   return Item_int_func::fix_fields(thd, ref);
 
 4442 longlong Item_func_benchmark::val_int()
 
 4444   DBUG_ASSERT(fixed == 1);
 
 4445   char buff[MAX_FIELD_WIDTH];
 
 4446   String tmp(buff,
sizeof(buff), &my_charset_bin);
 
 4448   THD *thd=current_thd;
 
 4449   ulonglong loop_count;
 
 4451   loop_count= (ulonglong) args[0]->val_int();
 
 4453   if (args[0]->null_value ||
 
 4454       (!args[0]->unsigned_flag && (((longlong) loop_count) < 0)))
 
 4456     if (!args[0]->null_value)
 
 4459       llstr(((longlong) loop_count), buff);
 
 4460       push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
 
 4461                           ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
 
 4462                           "count", buff, 
"benchmark");
 
 4470   for (ulonglong loop=0 ; loop < loop_count && !thd->killed; loop++)
 
 4472     switch (args[1]->result_type()) {
 
 4474       (void) args[1]->val_real();
 
 4477       (void) args[1]->val_int();
 
 4480       (void) args[1]->val_str(&tmp);
 
 4482     case DECIMAL_RESULT:
 
 4483       (void) args[1]->val_decimal(&tmp_decimal);
 
 4498   str->append(STRING_WITH_LEN(
"benchmark("));
 
 4499   args[0]->
print(str, query_type);
 
 4501   args[1]->
print(str, query_type);
 
 4510   THD *thd= current_thd;
 
 4516   DBUG_ASSERT(fixed == 1);
 
 4518   timeout= args[0]->val_real();
 
 4528   if (timeout < 0.00001)
 
 4531   timed_cond.
set_timeout((ulonglong) (timeout * 1000000000.0));
 
 4536   THD_STAGE_INFO(thd, stage_user_sleep);
 
 4537   thd->mysys_var->current_mutex= &LOCK_user_locks;
 
 4538   thd->mysys_var->current_cond=  &cond;
 
 4542   while (!thd->killed)
 
 4544     error= timed_cond.
wait(&cond, &LOCK_user_locks);
 
 4545     if (error == ETIMEDOUT || error == ETIME)
 
 4552   thd->mysys_var->current_mutex= 0;
 
 4553   thd->mysys_var->current_cond=  0;
 
 4558   return test(!error);          
 
 4562 static user_var_entry *get_variable(
HASH *hash, 
const Name_string &name,
 
 4563                                     bool create_if_not_exists)
 
 4565   user_var_entry *
entry;
 
 4567   if (!(entry = (user_var_entry*) my_hash_search(hash, (uchar*) name.
ptr(),
 
 4569       create_if_not_exists)
 
 4571     if (!my_hash_inited(hash))
 
 4573     if (!(entry= user_var_entry::create(name)))
 
 4575     if (my_hash_insert(hash,(uchar*) 
entry))
 
 4585 void Item_func_set_user_var::cleanup()
 
 4587   Item_func::cleanup();
 
 4592 bool Item_func_set_user_var::set_entry(THD *thd, 
bool create_if_not_exists)
 
 4594   if (entry && thd->thread_id == entry_thread_id)
 
 4596   if (!(entry= get_variable(&thd->user_vars, name, create_if_not_exists)))
 
 4601   entry_thread_id= thd->thread_id;
 
 4611   if (!delayed_non_constness)
 
 4612     entry->update_query_id= thd->query_id;
 
 4622 bool Item_func_set_user_var::fix_fields(THD *thd, 
Item **ref)
 
 4624   DBUG_ASSERT(fixed == 0);
 
 4626   if (Item_func::fix_fields(thd, ref) || set_entry(thd, TRUE))
 
 4643   null_item= (args[0]->type() == NULL_ITEM);
 
 4644   if (!entry->collation.collation || !null_item)
 
 4645     entry->collation.set(args[0]->collation.derivation == DERIVATION_NUMERIC ?
 
 4646                          default_charset() : args[0]->collation.collation,
 
 4647                          DERIVATION_IMPLICIT);
 
 4648   collation.set(entry->collation.collation, DERIVATION_IMPLICIT);
 
 4649   cached_result_type= args[0]->result_type();
 
 4655 Item_func_set_user_var::fix_length_and_dec()
 
 4657   maybe_null=args[0]->maybe_null;
 
 4658   decimals=args[0]->decimals;
 
 4659   collation.set(DERIVATION_IMPLICIT);
 
 4660   if (args[0]->collation.derivation == DERIVATION_NUMERIC)
 
 4661     fix_length_and_charset(args[0]->max_char_length(), default_charset());
 
 4664     fix_length_and_charset(args[0]->max_char_length(),
 
 4665                            args[0]->collation.collation);
 
 4667   unsigned_flag= args[0]->unsigned_flag;
 
 4679 bool Item_func_set_user_var::register_field_in_read_map(uchar *arg)
 
 4684     if (result_field->table == table || !table)
 
 4685       bitmap_set_bit(result_field->table->read_set, result_field->field_index);
 
 4691 bool user_var_entry::realloc(uint length)
 
 4693   if (length <= extra_size)
 
 4697     m_ptr= internal_buffer_ptr();
 
 4702     if (m_length != length)
 
 4704       if (m_ptr == internal_buffer_ptr())
 
 4706       if (!(m_ptr= (
char*) my_realloc(m_ptr, length,
 
 4707                                       MYF(MY_ALLOW_ZERO_PTR | MY_WME |
 
 4726 bool user_var_entry::store(
void *from, uint length, Item_result 
type)
 
 4729   if (realloc(length + 
test(type == STRING_RESULT)))
 
 4731   if (type == STRING_RESULT)
 
 4733   memmove(m_ptr, from, length);
 
 4734   if (type == DECIMAL_RESULT)
 
 4760 bool user_var_entry::store(
void *ptr, uint length, Item_result type,
 
 4764   if (store(ptr, length, type))
 
 4766   collation.set(cs, dv);
 
 4767   unsigned_flag= unsigned_arg;
 
 4773 Item_func_set_user_var::update_hash(
void *ptr, uint length,
 
 4774                                     Item_result res_type,
 
 4783   if (args[0]->
type() == Item::FIELD_ITEM)
 
 4784     null_value= ((
Item_field*)args[0])->field->is_null();
 
 4786     null_value= args[0]->null_value;
 
 4787   if (null_value && null_item)
 
 4788     res_type= entry->type();                    
 
 4790     entry->set_null_value(res_type);
 
 4791   else if (entry->store(ptr, length, res_type, cs, dv, unsigned_arg))
 
 4802 double user_var_entry::val_real(my_bool *null_value)
 
 4804   if ((*null_value= (m_ptr == 0)))
 
 4809     return *(
double*) m_ptr;
 
 4811     return (
double) *(longlong*) m_ptr;
 
 4812   case DECIMAL_RESULT:
 
 4815     my_decimal2double(E_DEC_FATAL_ERROR, (
my_decimal *) m_ptr, &result);
 
 4819     return my_atof(m_ptr);                    
 
 4830 longlong user_var_entry::val_int(my_bool *null_value)
 const 
 4832   if ((*null_value= (m_ptr == 0)))
 
 4837     return (longlong) *(
double*) m_ptr;
 
 4839     return *(longlong*) m_ptr;
 
 4840   case DECIMAL_RESULT:
 
 4843     my_decimal2int(E_DEC_FATAL_ERROR, (
my_decimal *) m_ptr, 0, &result);
 
 4849     return my_strtoll10(m_ptr, (
char**) 0, &error);
 
 4861 String *user_var_entry::val_str(my_bool *null_value, 
String *str,
 
 4864   if ((*null_value= (m_ptr == 0)))
 
 4869     str->set_real(*(
double*) m_ptr, decimals, collation.collation);
 
 4873       str->set(*(longlong*) m_ptr, collation.collation);
 
 4875       str->set(*(ulonglong*) m_ptr, collation.collation);
 
 4877   case DECIMAL_RESULT:
 
 4878     str_set_decimal((
my_decimal *) m_ptr, str, collation.collation);
 
 4881     if (str->copy(m_ptr, m_length, collation.collation))
 
 4894   if ((*null_value= (m_ptr == 0)))
 
 4899     double2my_decimal(E_DEC_FATAL_ERROR, *(
double*) m_ptr, val);
 
 4902     int2my_decimal(E_DEC_FATAL_ERROR, *(longlong*) m_ptr, 0, val);
 
 4904   case DECIMAL_RESULT:
 
 4905     my_decimal2decimal((
my_decimal *) m_ptr, val);
 
 4908     str2my_decimal(E_DEC_FATAL_ERROR, m_ptr, m_length,
 
 4909                    collation.collation, val);
 
 4935   DBUG_ENTER(
"Item_func_set_user_var::check");
 
 4936   if (use_result_field && !result_field)
 
 4937     use_result_field= FALSE;
 
 4939   switch (cached_result_type) {
 
 4942     save_result.vreal= use_result_field ? result_field->val_real() :
 
 4943                         args[0]->val_real();
 
 4948     save_result.vint= use_result_field ? result_field->val_int() :
 
 4950     unsigned_flag= use_result_field ? ((
Field_num*)result_field)->unsigned_flag:
 
 4951                     args[0]->unsigned_flag;
 
 4956     save_result.vstr= use_result_field ? result_field->val_str(&value) :
 
 4957                        args[0]->val_str(&value);
 
 4960   case DECIMAL_RESULT:
 
 4962     save_result.vdec= use_result_field ?
 
 4963                        result_field->val_decimal(&decimal_buff) :
 
 4964                        args[0]->val_decimal(&decimal_buff);
 
 4986   DBUG_ENTER(
"Item_func_set_user_var::save_item_result");
 
 4988   switch (cached_result_type) {
 
 4990     save_result.vreal= item->val_result();
 
 4993     save_result.vint= item->val_int_result();
 
 4994     unsigned_flag= item->unsigned_flag;
 
 4997     save_result.vstr= item->str_result(&value);
 
 4999   case DECIMAL_RESULT:
 
 5000     save_result.vdec= item->val_decimal_result(&decimal_buff);
 
 5013   if (delayed_non_constness)
 
 5014     entry->update_query_id= current_thd->query_id;
 
 5038   DBUG_ENTER(
"Item_func_set_user_var::update");
 
 5040   switch (cached_result_type) {
 
 5043     res= update_hash((
void*) &save_result.vreal,
sizeof(save_result.vreal),
 
 5044                      REAL_RESULT, default_charset(), DERIVATION_IMPLICIT, 0);
 
 5049     res= update_hash((
void*) &save_result.vint, 
sizeof(save_result.vint),
 
 5050                      INT_RESULT, default_charset(), DERIVATION_IMPLICIT,
 
 5056     if (!save_result.vstr)                                      
 
 5057       res= update_hash((
void*) 0, 0, STRING_RESULT, &my_charset_bin,
 
 5058                        DERIVATION_IMPLICIT, 0);
 
 5060       res= update_hash((
void*) save_result.vstr->ptr(),
 
 5061                        save_result.vstr->length(), STRING_RESULT,
 
 5062                        save_result.vstr->charset(),
 
 5063                        DERIVATION_IMPLICIT, 0);
 
 5066   case DECIMAL_RESULT:
 
 5068     if (!save_result.vdec)                                      
 
 5069       res= update_hash((
void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
 
 5070                        DERIVATION_IMPLICIT, 0);
 
 5072       res= update_hash((
void*) save_result.vdec,
 
 5074                        default_charset(), DERIVATION_IMPLICIT, 0);
 
 5087 double Item_func_set_user_var::val_real()
 
 5089   DBUG_ASSERT(fixed == 1);
 
 5092   return entry->val_real(&null_value);
 
 5095 longlong Item_func_set_user_var::val_int()
 
 5097   DBUG_ASSERT(fixed == 1);
 
 5100   return entry->val_int(&null_value);
 
 5105   DBUG_ASSERT(fixed == 1);
 
 5108   return entry->val_str(&null_value, str, decimals);
 
 5114   DBUG_ASSERT(fixed == 1);
 
 5117   return entry->val_decimal(&null_value, val);
 
 5121 double Item_func_set_user_var::val_result()
 
 5123   DBUG_ASSERT(fixed == 1);
 
 5126   return entry->val_real(&null_value);
 
 5129 longlong Item_func_set_user_var::val_int_result()
 
 5131   DBUG_ASSERT(fixed == 1);
 
 5134   return entry->val_int(&null_value);
 
 5137 bool Item_func_set_user_var::val_bool_result()
 
 5139   DBUG_ASSERT(fixed == 1);
 
 5142   return entry->val_int(&null_value) != 0;
 
 5145 String *Item_func_set_user_var::str_result(
String *str)
 
 5147   DBUG_ASSERT(fixed == 1);
 
 5150   return entry->val_str(&null_value, str, decimals);
 
 5156   DBUG_ASSERT(fixed == 1);
 
 5159   return entry->val_decimal(&null_value, val);
 
 5163 bool Item_func_set_user_var::is_null_result()
 
 5165   DBUG_ASSERT(fixed == 1);
 
 5172 void Item_func_set_user_var::print_assignment(
String *str,
 
 5173                                               enum_query_type query_type)
 
 5175   str->append(STRING_WITH_LEN(
"@"));
 
 5177   str->append(STRING_WITH_LEN(
":="));
 
 5178   args[0]->
print(str, query_type);
 
 5184   str->append(STRING_WITH_LEN(
"("));
 
 5185   print_assignment(str, query_type);
 
 5186   str->append(STRING_WITH_LEN(
")"));
 
 5195     return protocol->
store(result_field);
 
 5200 void Item_func_set_user_var::make_field(
Send_field *tmp_field)
 
 5204     result_field->make_field(tmp_field);
 
 5205     DBUG_ASSERT(tmp_field->table_name != 0);
 
 5206     if (Item::item_name.is_set())
 
 5207       tmp_field->col_name=Item::item_name.
ptr();    
 
 5210     Item::make_field(tmp_field);
 
 5252 type_conversion_status
 
 5254                                       bool can_use_result_field)
 
 5256   bool use_result_field= (!can_use_result_field ? 0 :
 
 5257                           (result_field && result_field != field));
 
 5258   type_conversion_status error;
 
 5261   check(use_result_field);
 
 5264   if (result_type() == STRING_RESULT ||
 
 5265       (result_type() == REAL_RESULT &&
 
 5266       field->result_type() == STRING_RESULT))
 
 5270     char buff[MAX_FIELD_WIDTH];         
 
 5271     str_value.set_quick(buff, 
sizeof(buff), cs);
 
 5272     result= entry->val_str(&null_value, &str_value, decimals);
 
 5276       str_value.set_quick(0, 0, cs);
 
 5282     field->set_notnull();
 
 5283     error=field->store(result->ptr(),result->length(),cs);
 
 5284     str_value.set_quick(0, 0, cs);
 
 5286   else if (result_type() == REAL_RESULT)
 
 5288     double nr= entry->val_real(&null_value);
 
 5290       return set_field_to_null(field);
 
 5291     field->set_notnull();
 
 5292     error=field->store(nr);
 
 5294   else if (result_type() == DECIMAL_RESULT)
 
 5297     my_decimal *val= entry->val_decimal(&null_value, &decimal_value);
 
 5299       return set_field_to_null(field);
 
 5300     field->set_notnull();
 
 5301     error=field->store_decimal(val);
 
 5305     longlong nr= entry->val_int(&null_value);
 
 5308     field->set_notnull();
 
 5309     error=field->store(nr, unsigned_flag);
 
 5316 Item_func_get_user_var::val_str(
String *str)
 
 5318   DBUG_ASSERT(fixed == 1);
 
 5319   DBUG_ENTER(
"Item_func_get_user_var::val_str");
 
 5321     DBUG_RETURN((
String*) 0);                   
 
 5322   DBUG_RETURN(var_entry->val_str(&null_value, str, decimals));
 
 5326 double Item_func_get_user_var::val_real()
 
 5328   DBUG_ASSERT(fixed == 1);
 
 5331   return (var_entry->val_real(&null_value));
 
 5337   DBUG_ASSERT(fixed == 1);
 
 5340   return var_entry->val_decimal(&null_value, dec);
 
 5344 longlong Item_func_get_user_var::val_int()
 
 5346   DBUG_ASSERT(fixed == 1);
 
 5349   return (var_entry->val_int(&null_value));
 
 5375 get_var_with_binlog(THD *thd, enum_sql_command sql_command,
 
 5379   user_var_entry *var_entry;
 
 5380   var_entry= get_variable(&thd->user_vars, name, 0);
 
 5388   if (!(opt_bin_log && 
 
 5389        (is_update_query(sql_command) || thd->in_sub_stmt)))
 
 5391     *out_entry= var_entry;
 
 5415     LEX *sav_lex= thd->lex, lex_tmp;
 
 5422     if (sql_set_variables(thd, &tmp_var_list))
 
 5428     if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
 
 5431   else if (var_entry->used_query_id == thd->query_id ||
 
 5432            mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
 
 5439     *out_entry= var_entry;
 
 5458         alloc_root(thd->user_var_events_alloc, size)))
 
 5461   user_var_event->value= (
char*) user_var_event +
 
 5463   user_var_event->user_var_event= var_entry;
 
 5464   user_var_event->type= var_entry->type();
 
 5465   user_var_event->charset_number= var_entry->collation.collation->number;
 
 5466   user_var_event->unsigned_flag= var_entry->unsigned_flag;
 
 5467   if (!var_entry->ptr())
 
 5470     user_var_event->length= 0;
 
 5471     user_var_event->value= 0;
 
 5475     user_var_event->length= var_entry->length();
 
 5476     memcpy(user_var_event->value, var_entry->ptr(),
 
 5477            var_entry->length());
 
 5480   var_entry->used_query_id= thd->query_id;
 
 5481   if (insert_dynamic(&thd->user_var_events, &user_var_event))
 
 5484   *out_entry= var_entry;
 
 5488   *out_entry= var_entry;
 
 5492 void Item_func_get_user_var::fix_length_and_dec()
 
 5494   THD *thd=current_thd;
 
 5497   decimals=NOT_FIXED_DEC;
 
 5498   max_length=MAX_BLOB_WIDTH;
 
 5500   error= get_var_with_binlog(thd, thd->lex->sql_command, name, &var_entry);
 
 5507   if (!error && var_entry)
 
 5509     m_cached_result_type= var_entry->type();
 
 5510     unsigned_flag= var_entry->unsigned_flag;
 
 5511     max_length= var_entry->length();
 
 5513     collation.set(var_entry->collation);
 
 5514     switch(m_cached_result_type) {
 
 5516       fix_char_length(DBL_DIG + 8);
 
 5519       fix_char_length(MAX_BIGINT_WIDTH);
 
 5523       max_length= MAX_BLOB_WIDTH - 1;
 
 5525     case DECIMAL_RESULT:
 
 5527       decimals= DECIMAL_MAX_SCALE;
 
 5537     collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
 
 5539     m_cached_result_type= STRING_RESULT;
 
 5540     max_length= MAX_BLOB_WIDTH;
 
 5545 bool Item_func_get_user_var::const_item()
 const 
 5547   return (!var_entry || current_thd->query_id != var_entry->update_query_id);
 
 5551 enum Item_result Item_func_get_user_var::result_type()
 const 
 5553   return m_cached_result_type;
 
 5559   str->append(STRING_WITH_LEN(
"(@"));
 
 5560   append_identifier(current_thd, str, name);
 
 5571   if (item->type() != FUNC_ITEM ||
 
 5572       ((
Item_func*) item)->functype() != functype())
 
 5575   return name.
eq_bin(other->name);
 
 5579 bool Item_func_get_user_var::set_value(THD *thd,
 
 5587   return (!suv || suv->fix_fields(thd, it) || suv->
check(0) || suv->
update());
 
 5591 bool Item_user_var_as_out_param::fix_fields(THD *thd, 
Item **ref)
 
 5593   DBUG_ASSERT(fixed == 0);
 
 5594   DBUG_ASSERT(thd->lex->exchange);
 
 5595   if (Item::fix_fields(thd, ref) ||
 
 5596       !(entry= get_variable(&thd->user_vars, name, 1)))
 
 5598   entry->set_type(STRING_RESULT);
 
 5604   entry->collation.set(thd->lex->exchange->cs ? 
 
 5605                        thd->lex->exchange->cs :
 
 5606                        thd->variables.collation_database);
 
 5607   entry->update_query_id= thd->query_id;
 
 5612 void Item_user_var_as_out_param::set_null_value(
const CHARSET_INFO* cs)
 
 5614   entry->set_null_value(STRING_RESULT);
 
 5618 void Item_user_var_as_out_param::set_value(
const char *str, uint length,
 
 5621   entry->store((
void*) str, length, STRING_RESULT, cs,
 
 5622                DERIVATION_IMPLICIT, 0 );
 
 5626 double Item_user_var_as_out_param::val_real()
 
 5633 longlong Item_user_var_as_out_param::val_int()
 
 5640 String* Item_user_var_as_out_param::val_str(
String *str)
 
 5657   append_identifier(current_thd, str, name);
 
 5661 Item_func_get_system_var::
 
 5662 Item_func_get_system_var(
sys_var *var_arg, enum_var_type var_type_arg,
 
 5663                        LEX_STRING *component_arg, 
const char *name_arg,
 
 5664                        size_t name_len_arg)
 
 5665   :var(var_arg), var_type(var_type_arg), orig_var_type(var_type_arg),
 
 5666   component(*component_arg), cache_present(0)
 
 5669   item_name.
copy(name_arg, (uint) name_len_arg);
 
 5675   return var->is_written_to_binlog(var_type);
 
 5679 void Item_func_get_system_var::update_null_value()
 
 5681   THD *thd= current_thd;
 
 5682   int save_no_errors= thd->no_errors;
 
 5683   thd->no_errors= TRUE;
 
 5684   Item::update_null_value();
 
 5685   thd->no_errors= save_no_errors;
 
 5689 void Item_func_get_system_var::fix_length_and_dec()
 
 5695   if (var->check_type(var_type))
 
 5697     if (var_type != OPT_DEFAULT)
 
 5699       my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0),
 
 5700                var->name.str, var_type == OPT_GLOBAL ? 
"SESSION" : 
"GLOBAL");
 
 5704     var_type= OPT_GLOBAL;
 
 5707   switch (var->show_type())
 
 5713       unsigned_flag= TRUE;
 
 5714       collation.set_numeric();
 
 5715       fix_char_length(MY_INT64_NUM_DECIMAL_DIGITS);
 
 5718     case SHOW_SIGNED_LONG:
 
 5719       unsigned_flag= FALSE;
 
 5720       collation.set_numeric();
 
 5721       fix_char_length(MY_INT64_NUM_DECIMAL_DIGITS);
 
 5727       cptr= var->show_type() == SHOW_CHAR ? 
 
 5728         (
char*) var->value_ptr(current_thd, var_type, &component) :
 
 5729         *(
char**) var->value_ptr(current_thd, var_type, &component);
 
 5731         max_length= system_charset_info->cset->numchars(system_charset_info,
 
 5733                                                         cptr + strlen(cptr));
 
 5735       collation.set(system_charset_info, DERIVATION_SYSCONST);
 
 5736       max_length*= system_charset_info->mbmaxlen;
 
 5737       decimals=NOT_FIXED_DEC;
 
 5739     case SHOW_LEX_STRING:
 
 5743         max_length= system_charset_info->cset->numchars(system_charset_info,
 
 5745                                                         ls->str + ls->length);
 
 5747         collation.set(system_charset_info, DERIVATION_SYSCONST);
 
 5748         max_length*= system_charset_info->mbmaxlen;
 
 5749         decimals=NOT_FIXED_DEC;
 
 5754       unsigned_flag= FALSE;
 
 5755       collation.set_numeric();
 
 5760       unsigned_flag= FALSE;
 
 5762       collation.set_numeric();
 
 5763       fix_char_length(DBL_DIG + 6);
 
 5766       my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
 
 5774   str->append(item_name);
 
 5778 enum Item_result Item_func_get_system_var::result_type()
 const 
 5780   switch (var->show_type())
 
 5786     case SHOW_SIGNED_LONG:
 
 5792     case SHOW_LEX_STRING:
 
 5793       return STRING_RESULT;
 
 5797       my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
 
 5798       return STRING_RESULT;                   
 
 5803 enum_field_types Item_func_get_system_var::field_type()
 const 
 5805   switch (var->show_type())
 
 5811     case SHOW_SIGNED_LONG:
 
 5814       return MYSQL_TYPE_LONGLONG;
 
 5817     case SHOW_LEX_STRING:
 
 5818       return MYSQL_TYPE_VARCHAR;
 
 5820       return MYSQL_TYPE_DOUBLE;
 
 5822       my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
 
 5823       return MYSQL_TYPE_VARCHAR;              
 
 5832 #define get_sys_var_safe(type) \ 
 5835   mysql_mutex_lock(&LOCK_global_system_variables); \ 
 5836   value= *(type*) var->value_ptr(thd, var_type, &component); \ 
 5837   mysql_mutex_unlock(&LOCK_global_system_variables); \ 
 5838   cache_present |= GET_SYS_VAR_CACHE_LONG; \ 
 5839   used_query_id= thd->query_id; \ 
 5840   cached_llval= null_value ? 0 : (longlong) value; \ 
 5841   cached_null_value= null_value; \ 
 5842   return cached_llval; \ 
 5846 longlong Item_func_get_system_var::val_int()
 
 5848   THD *thd= current_thd;
 
 5850   if (cache_present && thd->query_id == used_query_id)
 
 5852     if (cache_present & GET_SYS_VAR_CACHE_LONG)
 
 5854       null_value= cached_null_value;
 
 5855       return cached_llval;
 
 5857     else if (cache_present & GET_SYS_VAR_CACHE_DOUBLE)
 
 5859       null_value= cached_null_value;
 
 5860       cached_llval= (longlong) cached_dval;
 
 5861       cache_present|= GET_SYS_VAR_CACHE_LONG;
 
 5862       return cached_llval;
 
 5864     else if (cache_present & GET_SYS_VAR_CACHE_STRING)
 
 5866       null_value= cached_null_value;
 
 5868         cached_llval= longlong_from_string_with_check (cached_strval.charset(),
 
 5869                                                        cached_strval.c_ptr(),
 
 5870                                                        cached_strval.c_ptr() +
 
 5871                                                        cached_strval.length());
 
 5874       cache_present|= GET_SYS_VAR_CACHE_LONG;
 
 5875       return cached_llval;
 
 5879   switch (var->show_type())
 
 5881     case SHOW_INT:      get_sys_var_safe (uint);
 
 5882     case SHOW_LONG:     get_sys_var_safe (ulong);
 
 5883     case SHOW_SIGNED_LONG: get_sys_var_safe (
long);
 
 5884     case SHOW_LONGLONG: get_sys_var_safe (ulonglong);
 
 5885     case SHOW_HA_ROWS:  get_sys_var_safe (ha_rows);
 
 5886     case SHOW_BOOL:     get_sys_var_safe (
bool);
 
 5887     case SHOW_MY_BOOL:  get_sys_var_safe (my_bool);
 
 5890         double dval= val_real();
 
 5892         used_query_id= thd->query_id;
 
 5893         cached_llval= (longlong) dval;
 
 5894         cache_present|= GET_SYS_VAR_CACHE_LONG;
 
 5895         return cached_llval;
 
 5899     case SHOW_LEX_STRING:
 
 5901         String *str_val= val_str(NULL);
 
 5903         if (str_val && str_val->length())
 
 5904           cached_llval= longlong_from_string_with_check (system_charset_info,
 
 5914         cache_present|= GET_SYS_VAR_CACHE_LONG;
 
 5915         return cached_llval;
 
 5919       my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str); 
 
 5927   THD *thd= current_thd;
 
 5929   if (cache_present && thd->query_id == used_query_id)
 
 5931     if (cache_present & GET_SYS_VAR_CACHE_STRING)
 
 5933       null_value= cached_null_value;
 
 5934       return null_value ? NULL : &cached_strval;
 
 5936     else if (cache_present & GET_SYS_VAR_CACHE_LONG)
 
 5938       null_value= cached_null_value;
 
 5940         cached_strval.set (cached_llval, collation.collation);
 
 5941       cache_present|= GET_SYS_VAR_CACHE_STRING;
 
 5942       return null_value ? NULL : &cached_strval;
 
 5944     else if (cache_present & GET_SYS_VAR_CACHE_DOUBLE)
 
 5946       null_value= cached_null_value;
 
 5948         cached_strval.set_real (cached_dval, decimals, collation.collation);
 
 5949       cache_present|= GET_SYS_VAR_CACHE_STRING;
 
 5950       return null_value ? NULL : &cached_strval;
 
 5954   str= &cached_strval;
 
 5955   switch (var->show_type())
 
 5959     case SHOW_LEX_STRING:
 
 5962       char *cptr= var->show_type() == SHOW_CHAR ? 
 
 5963         (
char*) var->value_ptr(thd, var_type, &component) :
 
 5964         *(
char**) var->value_ptr(thd, var_type, &component);
 
 5967         size_t len= var->show_type() == SHOW_LEX_STRING ?
 
 5968           ((
LEX_STRING*)(var->value_ptr(thd, var_type, &component)))->length :
 
 5970         if (str->copy(cptr, len, collation.collation))
 
 5987     case SHOW_SIGNED_LONG:
 
 5992       str->set (val_int(), collation.collation);
 
 5995       str->set_real (val_real(), decimals, collation.collation);
 
 5999       my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
 
 6004   cache_present|= GET_SYS_VAR_CACHE_STRING;
 
 6005   used_query_id= thd->query_id;
 
 6006   cached_null_value= null_value;
 
 6011 double Item_func_get_system_var::val_real()
 
 6013   THD *thd= current_thd;
 
 6015   if (cache_present && thd->query_id == used_query_id)
 
 6017     if (cache_present & GET_SYS_VAR_CACHE_DOUBLE)
 
 6019       null_value= cached_null_value;
 
 6022     else if (cache_present & GET_SYS_VAR_CACHE_LONG)
 
 6024       null_value= cached_null_value;
 
 6025       cached_dval= (double)cached_llval;
 
 6026       cache_present|= GET_SYS_VAR_CACHE_DOUBLE;
 
 6029     else if (cache_present & GET_SYS_VAR_CACHE_STRING)
 
 6031       null_value= cached_null_value;
 
 6033         cached_dval= double_from_string_with_check (cached_strval.charset(),
 
 6034                                                     cached_strval.c_ptr(),
 
 6035                                                     cached_strval.c_ptr() +
 
 6036                                                     cached_strval.length());
 
 6039       cache_present|= GET_SYS_VAR_CACHE_DOUBLE;
 
 6044   switch (var->show_type())
 
 6048       cached_dval= *(
double*) var->value_ptr(thd, var_type, &component);
 
 6050       used_query_id= thd->query_id;
 
 6051       cached_null_value= null_value;
 
 6054       cache_present|= GET_SYS_VAR_CACHE_DOUBLE;
 
 6057     case SHOW_LEX_STRING:
 
 6061         char *cptr= var->show_type() == SHOW_CHAR ? 
 
 6062           (
char*) var->value_ptr(thd, var_type, &component) :
 
 6063           *(
char**) var->value_ptr(thd, var_type, &component);
 
 6066           cached_dval= double_from_string_with_check (system_charset_info, 
 
 6067                                                 cptr, cptr + strlen (cptr));
 
 6074         used_query_id= thd->query_id;
 
 6075         cached_null_value= null_value;
 
 6076         cache_present|= GET_SYS_VAR_CACHE_DOUBLE;
 
 6081     case SHOW_SIGNED_LONG:
 
 6086         cached_dval= (double) val_int();
 
 6087         cache_present|= GET_SYS_VAR_CACHE_DOUBLE;
 
 6088         used_query_id= thd->query_id;
 
 6089         cached_null_value= null_value;
 
 6092       my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
 
 6104   if (item->type() != FUNC_ITEM ||
 
 6105       ((
Item_func*) item)->functype() != functype())
 
 6108   return (var == other->var && var_type == other->var_type);
 
 6112 void Item_func_get_system_var::cleanup()
 
 6114   Item_func::cleanup();
 
 6116   var_type= orig_var_type;
 
 6117   cached_strval.free();
 
 6121 void Item_func_match::init_search(
bool no_order)
 
 6123   DBUG_ENTER(
"Item_func_match::init_search");
 
 6142       table->file->ft_handler= ft_handler;
 
 6146   if (key == NO_SUCH_KEY)
 
 6149     fields.push_back(
new Item_string(
" ",1, cmp_collation.collation));
 
 6150     for (uint i=1; i < arg_count; i++)
 
 6151       fields.push_back(args[i]);
 
 6159     concat_ws->quick_fix_field();
 
 6164     join_key=master->join_key=join_key|master->join_key;
 
 6165     master->init_search(no_order);
 
 6166     ft_handler=master->ft_handler;
 
 6167     join_key=master->join_key;
 
 6174   if (!(ft_tmp=key_item()->val_str(&value)))
 
 6177     value.set(
"",0,cmp_collation.collation);
 
 6180   if (ft_tmp->charset() != cmp_collation.collation)
 
 6183     search_value.copy(ft_tmp->ptr(), ft_tmp->length(), ft_tmp->charset(),
 
 6184                       cmp_collation.collation, &dummy_errors);
 
 6185     ft_tmp= &search_value;
 
 6188   if (join_key && !no_order)
 
 6190   ft_handler=table->file->ft_init_ext(
flags, key, ft_tmp);
 
 6193     table->file->ft_handler=ft_handler;
 
 6199 bool Item_func_match::fix_fields(THD *thd, 
Item **ref)
 
 6201   DBUG_ASSERT(fixed == 0);
 
 6202   Item *UNINIT_VAR(item);                        
 
 6213   if (Item_func::fix_fields(thd, ref) ||
 
 6214       !args[0]->const_during_execution())
 
 6216     my_error(ER_WRONG_ARGUMENTS,MYF(0),
"AGAINST");
 
 6220   bool allows_multi_table_search= 
true;
 
 6222   for (uint i=1 ; i < arg_count ; i++)
 
 6225     if (item->type() == Item::REF_ITEM)
 
 6226       args[i]= item= *((
Item_ref *)item)->ref;
 
 6227     if (item->type() != Item::FIELD_ITEM)
 
 6229       my_error(ER_WRONG_ARGUMENTS, MYF(0), 
"AGAINST");
 
 6232     allows_multi_table_search &= 
 
 6233       allows_search_on_non_indexed_columns(((
Item_field *)item)->field->table);
 
 6244   if (key == NO_SUCH_KEY && !allows_multi_table_search)
 
 6246     my_error(ER_WRONG_ARGUMENTS,MYF(0),
"MATCH");
 
 6252     my_error(ER_TABLE_CANT_HANDLE_FT, MYF(0));
 
 6255   table->fulltext_searched=1;
 
 6256   return agg_item_collations_for_comparison(cmp_collation, func_name(),
 
 6257                                             args+1, arg_count-1, 0);
 
 6260 bool Item_func_match::fix_index()
 
 6263   uint ft_to_key[MAX_KEY], ft_cnt[MAX_KEY], fts=0, keynr;
 
 6264   uint max_cnt=0, mkeys=0, 
i;
 
 6273   if (key == NO_SUCH_KEY)
 
 6279   for (keynr=0 ; keynr < table->s->keys ; keynr++)
 
 6281     if ((table->key_info[keynr].
flags & HA_FULLTEXT) &&
 
 6282         (
flags & FT_BOOL ? table->keys_in_use_for_query.is_set(keynr) :
 
 6283                            table->s->keys_in_use.is_set(keynr)))
 
 6286       ft_to_key[fts]=keynr;
 
 6295   for (i=1; i < arg_count; i++)
 
 6298     for (keynr=0 ; keynr < fts ; keynr++)
 
 6300       KEY *ft_key=&table->key_info[ft_to_key[keynr]];
 
 6303       for (uint part=0 ; part < key_parts ; part++)
 
 6305         if (item->field->eq(ft_key->key_part[part].field))
 
 6311   for (keynr=0 ; keynr < fts ; keynr++)
 
 6313     if (ft_cnt[keynr] > max_cnt)
 
 6316       max_cnt=ft_cnt[mkeys]=ft_cnt[keynr];
 
 6317       ft_to_key[mkeys]=ft_to_key[keynr];
 
 6320     if (max_cnt && ft_cnt[keynr] == max_cnt)
 
 6323       ft_cnt[mkeys]=ft_cnt[keynr];
 
 6324       ft_to_key[mkeys]=ft_to_key[keynr];
 
 6329   for (keynr=0 ; keynr <= mkeys ; keynr++)
 
 6332     if (max_cnt < arg_count-1 ||
 
 6333         max_cnt < table->key_info[ft_to_key[keynr]].user_defined_key_parts)
 
 6336     key=ft_to_key[keynr];
 
 6342   if (allows_search_on_non_indexed_columns(table))
 
 6347   my_message(ER_FT_MATCHING_KEY_NOT_FOUND,
 
 6348              ER(ER_FT_MATCHING_KEY_NOT_FOUND), MYF(0));
 
 6357   if (item->type() != FUNC_ITEM ||
 
 6358       ((
Item_func*)item)->functype() != FT_FUNC ||
 
 6364   if (key == ifm->key && table == ifm->table &&
 
 6365       key_item()->
eq(ifm->key_item(), binary_cmp))
 
 6372 double Item_func_match::val_real()
 
 6374   DBUG_ASSERT(fixed == 1);
 
 6375   DBUG_ENTER(
"Item_func_match::val");
 
 6376   if (ft_handler == NULL)
 
 6379   if (key != NO_SUCH_KEY && table->null_row) 
 
 6384     if (table->file->ft_handler)
 
 6385       DBUG_RETURN(ft_handler->please->get_relevance(ft_handler));
 
 6389   if (key == NO_SUCH_KEY)
 
 6391     String *a= concat_ws->val_str(&value);
 
 6392     if ((null_value= (a == 0)) || !a->length())
 
 6394     DBUG_RETURN(ft_handler->please->find_relevance(ft_handler,
 
 6395                                       (uchar *)a->ptr(), a->length()));
 
 6397   DBUG_RETURN(ft_handler->please->find_relevance(ft_handler,
 
 6398                                                  table->record[0], 0));
 
 6403   str->append(STRING_WITH_LEN(
"(match "));
 
 6404   print_args(str, 1, query_type);
 
 6405   str->append(STRING_WITH_LEN(
" against ("));
 
 6406   args[0]->
print(str, query_type);
 
 6407   if (
flags & FT_BOOL)
 
 6408     str->append(STRING_WITH_LEN(
" in boolean mode"));
 
 6409   else if (
flags & FT_EXPAND)
 
 6410     str->append(STRING_WITH_LEN(
" with query expansion"));
 
 6411   str->append(STRING_WITH_LEN(
"))"));
 
 6414 longlong Item_func_bit_xor::val_int()
 
 6416   DBUG_ASSERT(fixed == 1);
 
 6417   ulonglong arg1= (ulonglong) args[0]->val_int();
 
 6418   ulonglong arg2= (ulonglong) args[1]->val_int();
 
 6419   if ((null_value= (args[0]->null_value || args[1]->null_value)))
 
 6421   return (longlong) (arg1 ^ arg2);
 
 6454     base_name= &component;
 
 6455     component_name= &
name;
 
 6460     component_name= &component;                 
 
 6463   if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
 
 6469       my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
 
 6473   thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
 
 6475   set_if_smaller(component_name->length, MAX_SYS_VAR_LENGTH);
 
 6477   var->do_deprecated_warning(thd);
 
 6497   DBUG_ASSERT(fixed == 1);
 
 6498   String *res=args[0]->val_str(&value);
 
 6502   if (!res || !res->length())
 
 6509   ull= (
User_level_lock *) my_hash_search(&hash_user_locks, (uchar*) res->ptr(),
 
 6510                                           (size_t) res->length());
 
 6512   if (!ull || !ull->locked)
 
 6517 longlong Item_func_is_used_lock::val_int()
 
 6519   DBUG_ASSERT(fixed == 1);
 
 6520   String *res=args[0]->val_str(&value);
 
 6524   if (!res || !res->length())
 
 6528   ull= (
User_level_lock *) my_hash_search(&hash_user_locks, (uchar*) res->ptr(),
 
 6529                                           (size_t) res->length());
 
 6531   if (!ull || !ull->locked)
 
 6535   return ull->thread_id;
 
 6539 longlong Item_func_row_count::val_int()
 
 6541   DBUG_ASSERT(fixed == 1);
 
 6542   THD *thd= current_thd;
 
 6544   return thd->get_row_count_func();
 
 6551   :
Item_func(), context(context_arg), m_name(name), m_sp(NULL), sp_result_field(NULL)
 
 6554   m_name->init_qname(current_thd);
 
 6557   with_stored_program= 
true;
 
 6563   :
Item_func(list), context(context_arg), m_name(name), m_sp(NULL),sp_result_field(NULL)
 
 6566   m_name->init_qname(current_thd);
 
 6569   with_stored_program= 
true;
 
 6574 Item_func_sp::cleanup()
 
 6576   if (sp_result_field)
 
 6578     delete sp_result_field;
 
 6579     sp_result_field= NULL;
 
 6582   dummy_table->alias= NULL;
 
 6583   Item_func::cleanup();
 
 6585   with_stored_program= 
true;
 
 6589 Item_func_sp::func_name()
 const 
 6591   THD *thd= current_thd;
 
 6594               m_name->m_name.length)*2 + 
 
 6600   String qname((
char *)alloc_root(thd->mem_root, len), len,
 
 6601                system_charset_info);
 
 6606     append_identifier(thd, &qname, m_name->m_db.str, m_name->m_db.length);
 
 6609   append_identifier(thd, &qname, m_name->m_name.str, m_name->m_name.length);
 
 6616   return m_sp->
m_chistics->detistic ? 0 : RAND_TABLE_BIT;
 
 6620 void my_missing_function_error(
const LEX_STRING &token, 
const char *func_name)
 
 6622   if (token.length && is_lex_native_function (&token))
 
 6623     my_error(ER_FUNC_INEXISTENT_NAME_COLLISION, MYF(0), func_name);
 
 6625     my_error(ER_SP_DOES_NOT_EXIST, MYF(0), 
"FUNCTION", func_name);
 
 6648 Item_func_sp::init_result_field(THD *thd)
 
 6650   LEX_STRING empty_name= { C_STRING_WITH_LEN(
"") };
 
 6652   DBUG_ENTER(
"Item_func_sp::init_result_field");
 
 6654   DBUG_ASSERT(m_sp == NULL);
 
 6655   DBUG_ASSERT(sp_result_field == NULL);
 
 6657   if (!(m_sp= sp_find_routine(thd, SP_TYPE_FUNCTION, m_name,
 
 6658                                &thd->sp_func_cache, TRUE)))
 
 6660     my_missing_function_error (m_name->m_name, m_name->m_qname.str);
 
 6661     context->process_error(thd);
 
 6671   share= dummy_table->s;
 
 6672   dummy_table->alias = 
"";
 
 6673   dummy_table->maybe_null = maybe_null;
 
 6674   dummy_table->in_use= thd;
 
 6675   dummy_table->copy_blobs= TRUE;
 
 6676   share->table_cache_key = empty_name;
 
 6677   share->table_name = empty_name;
 
 6685   if (sp_result_field->pack_length() > 
sizeof(result_buf))
 
 6688     if (!(tmp= sql_alloc(sp_result_field->pack_length())))
 
 6690     sp_result_field->move_field((uchar*) tmp);
 
 6693     sp_result_field->move_field(result_buf);
 
 6695   sp_result_field->set_null_ptr((uchar *) &null_value, 1);
 
 6708   DBUG_ENTER(
"Item_func_sp::fix_length_and_dec");
 
 6710   DBUG_ASSERT(sp_result_field);
 
 6711   decimals= sp_result_field->decimals();
 
 6712   max_length= sp_result_field->field_length;
 
 6713   collation.set(sp_result_field->charset());
 
 6715   unsigned_flag= 
test(sp_result_field->flags & UNSIGNED_FLAG);
 
 6721 void Item_func_sp::update_null_value()
 
 6751 Item_func_sp::execute()
 
 6753   THD *thd= current_thd;
 
 6757   if (execute_impl(thd))
 
 6760     context->process_error(thd);
 
 6762       thd->send_kill_message();
 
 6768   null_value= sp_result_field->is_null();
 
 6785 Item_func_sp::execute_impl(THD *thd)
 
 6787   bool err_status= TRUE;
 
 6788   Sub_statement_state statement_state;
 
 6789 #ifndef NO_EMBEDDED_ACCESS_CHECKS 
 6790   Security_context *save_security_ctx= thd->security_ctx;
 
 6792   enum enum_sp_data_access access=
 
 6793     (m_sp->
m_chistics->daccess == SP_DEFAULT_ACCESS) ?
 
 6794      SP_DEFAULT_ACCESS_MAPPING : m_sp->
m_chistics->daccess;
 
 6796   DBUG_ENTER(
"Item_func_sp::execute_impl");
 
 6798 #ifndef NO_EMBEDDED_ACCESS_CHECKS 
 6799   if (context->security_ctx)
 
 6802     thd->security_ctx= context->security_ctx;
 
 6813   if (!m_sp->
m_chistics->detistic && !trust_function_creators &&
 
 6814       (access == SP_CONTAINS_SQL || access == SP_MODIFIES_SQL_DATA) &&
 
 6815       (mysql_bin_log.is_open() &&
 
 6816        thd->variables.binlog_format == BINLOG_FORMAT_STMT))
 
 6818     my_error(ER_BINLOG_UNSAFE_ROUTINE, MYF(0));
 
 6827   thd->reset_sub_statement_state(&statement_state, SUB_STMT_FUNCTION);
 
 6829   thd->restore_sub_statement_state(&statement_state);
 
 6832 #ifndef NO_EMBEDDED_ACCESS_CHECKS 
 6833   thd->security_ctx= save_security_ctx;
 
 6836   DBUG_RETURN(err_status);
 
 6841 Item_func_sp::make_field(
Send_field *tmp_field)
 
 6843   DBUG_ENTER(
"Item_func_sp::make_field");
 
 6844   DBUG_ASSERT(sp_result_field);
 
 6845   sp_result_field->make_field(tmp_field);
 
 6847     tmp_field->col_name= item_name.
ptr();
 
 6852 enum enum_field_types
 
 6853 Item_func_sp::field_type()
 const 
 6855   DBUG_ENTER(
"Item_func_sp::field_type");
 
 6856   DBUG_ASSERT(sp_result_field);
 
 6857   DBUG_RETURN(sp_result_field->type());
 
 6861 Item_func_sp::result_type()
 const 
 6863   DBUG_ENTER(
"Item_func_sp::result_type");
 
 6864   DBUG_PRINT(
"info", (
"m_sp = %p", (
void *) m_sp));
 
 6865   DBUG_ASSERT(sp_result_field);
 
 6866   DBUG_RETURN(sp_result_field->result_type());
 
 6869 longlong Item_func_found_rows::val_int()
 
 6871   DBUG_ASSERT(fixed == 1);
 
 6872   return current_thd->found_rows();
 
 6877 Item_func_sp::tmp_table_field(
TABLE *t_arg)
 
 6879   DBUG_ENTER(
"Item_func_sp::tmp_table_field");
 
 6881   DBUG_ASSERT(sp_result_field);
 
 6882   DBUG_RETURN(sp_result_field);
 
 6903   DBUG_ENTER(
"Item_func_sp::sp_check_access");
 
 6905 #ifndef NO_EMBEDDED_ACCESS_CHECKS 
 6906   if (check_routine_access(thd, EXECUTE_ACL,
 
 6907                            m_sp->m_db.str, m_sp->m_name.str, 0, FALSE))
 
 6916 Item_func_sp::fix_fields(THD *thd, 
Item **ref)
 
 6919 #ifndef NO_EMBEDDED_ACCESS_CHECKS 
 6920   Security_context *save_security_ctx= thd->security_ctx;
 
 6923   DBUG_ENTER(
"Item_func_sp::fix_fields");
 
 6924   DBUG_ASSERT(fixed == 0);
 
 6926 #ifndef NO_EMBEDDED_ACCESS_CHECKS 
 6931   if (!(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW) ||
 
 6932       (thd->lex->sql_command == SQLCOM_CREATE_VIEW))
 
 6934     if (context->security_ctx)
 
 6937       thd->security_ctx= context->security_ctx;
 
 6943     res= check_routine_access(thd, EXECUTE_ACL, m_name->m_db.str,
 
 6944                               m_name->m_name.str, 0, FALSE);
 
 6945     thd->security_ctx= save_security_ctx;
 
 6949       context->process_error(thd);
 
 6960   res= init_result_field(thd);
 
 6965   res= Item_func::fix_fields(thd, ref);
 
 6968   with_stored_program= 
true;
 
 6975   if (thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW)
 
 6988 #ifndef NO_EMBEDDED_ACCESS_CHECKS 
 6992     Security_context *save_secutiry_ctx;
 
 6995       m_sp->m_security_ctx.restore_security_context(thd, save_secutiry_ctx);
 
 7004 void Item_func_sp::update_used_tables()
 
 7006   Item_func::update_used_tables();
 
 7012   with_stored_program= 
true;
 
 7034 ulonglong uuid_value;
 
 7036 void uuid_short_init()
 
 7038   uuid_value= ((((ulonglong) server_id) << 56) + 
 
 7039                (((ulonglong) server_start_time) << 24));
 
 7043 longlong Item_func_uuid_short::val_int()
 
 7049   return (longlong) val;