~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_func.cc

Moved my_sys/my_pthread/my_nosys and mysys_err to mysys.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
  This file defines all numerical functions
22
22
*/
23
23
 
24
 
#include <drizzled/server_includes.h>
 
24
#ifdef USE_PRAGMA_IMPLEMENTATION
 
25
#pragma implementation                          // gcc: Class implementation
 
26
#endif
 
27
 
 
28
#include "mysql_priv.h"
 
29
#include "slave.h"                              // for wait_for_master_pos
25
30
#include "rpl_mi.h"
26
 
#include <mysys/my_bit.h>
27
 
#include <drizzled/drizzled_error_messages.h>
 
31
#include <m_ctype.h>
 
32
#include <hash.h>
 
33
#include <time.h>
 
34
#include <my_bit.h>
28
35
 
29
36
bool check_reserved_words(LEX_STRING *name)
30
37
{
91
98
      if (!(args=(Item**) thd->alloc(sizeof(Item*)*arg_count)))
92
99
        return;
93
100
    }
94
 
    memcpy(args, item->args, sizeof(Item*)*arg_count);
 
101
    memcpy((char*) args, (char*) item->args, sizeof(Item*)*arg_count);
95
102
  }
96
103
}
97
104
 
135
142
  assert(fixed == 0);
136
143
  Item **arg,**arg_end;
137
144
  void *save_thd_marker= thd->thd_marker;
138
 
  unsigned char buff[STACK_BUFF_ALLOC];                 // Max argument in function
 
145
  uchar buff[STACK_BUFF_ALLOC];                 // Max argument in function
139
146
  thd->thd_marker= 0;
140
147
  used_tables_cache= not_null_tables_cache= 0;
141
148
  const_item_cache=1;
211
218
 
212
219
 
213
220
bool Item_func::walk(Item_processor processor, bool walk_subquery,
214
 
                     unsigned char *argument)
 
221
                     uchar *argument)
215
222
{
216
223
  if (arg_count)
217
224
  {
270
277
    Item returned as the result of transformation of the root node
271
278
*/
272
279
 
273
 
Item *Item_func::transform(Item_transformer transformer, unsigned char *argument)
 
280
Item *Item_func::transform(Item_transformer transformer, uchar *argument)
274
281
{
275
282
  if (arg_count)
276
283
  {
319
326
    Item returned as the result of transformation of the root node
320
327
*/
321
328
 
322
 
Item *Item_func::compile(Item_analyzer analyzer, unsigned char **arg_p,
323
 
                         Item_transformer transformer, unsigned char *arg_t)
 
329
Item *Item_func::compile(Item_analyzer analyzer, uchar **arg_p,
 
330
                         Item_transformer transformer, uchar *arg_t)
324
331
{
325
332
  if (!(this->*analyzer)(arg_p))
326
333
    return 0;
333
340
        The same parameter value of arg_p must be passed
334
341
        to analyze any argument of the condition formula.
335
342
      */   
336
 
      unsigned char *arg_v= *arg_p;
 
343
      uchar *arg_v= *arg_p;
337
344
      Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
338
345
      if (new_item && *arg != new_item)
339
346
        current_thd->change_item_tree(arg, new_item);
359
366
{
360
367
  used_tables_cache=0;
361
368
  const_item_cache=1;
362
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
369
  for (uint i=0 ; i < arg_count ; i++)
363
370
  {
364
371
    args[i]->update_used_tables();
365
372
    used_tables_cache|=args[i]->used_tables();
389
396
}
390
397
 
391
398
 
392
 
void Item_func::print_args(String *str, uint32_t from, enum_query_type query_type)
 
399
void Item_func::print_args(String *str, uint from, enum_query_type query_type)
393
400
{
394
 
  for (uint32_t i=from ; i < arg_count ; i++)
 
401
  for (uint i=from ; i < arg_count ; i++)
395
402
  {
396
403
    if (i != from)
397
404
      str->append(',');
403
410
void Item_func::print_op(String *str, enum_query_type query_type)
404
411
{
405
412
  str->append('(');
406
 
  for (uint32_t i=0 ; i < arg_count-1 ; i++)
 
413
  for (uint i=0 ; i < arg_count-1 ; i++)
407
414
  {
408
415
    args[i]->print(str, query_type);
409
416
    str->append(' ');
431
438
      (func_type == Item_func::FUNC_SP &&
432
439
       my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
433
440
    return 0;
434
 
  for (uint32_t i=0; i < arg_count ; i++)
 
441
  for (uint i=0; i < arg_count ; i++)
435
442
    if (!args[i]->eq(item_func->args[i], binary_cmp))
436
443
      return 0;
437
444
  return 1;
438
445
}
439
446
 
440
447
 
441
 
Field *Item_func::tmp_table_field(Table *table)
 
448
Field *Item_func::tmp_table_field(TABLE *table)
442
449
{
443
450
  Field *field;
444
451
 
506
513
 
507
514
void Item_func::fix_num_length_and_dec()
508
515
{
509
 
  uint32_t fl_length= 0;
 
516
  uint fl_length= 0;
510
517
  decimals=0;
511
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
518
  for (uint i=0 ; i < arg_count ; i++)
512
519
  {
513
520
    set_if_bigger(decimals,args[i]->decimals);
514
521
    set_if_bigger(fl_length, args[i]->max_length);
536
543
  int max_int_part= 0;
537
544
  decimals= 0;
538
545
  unsigned_flag= 1;
539
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
546
  for (uint i=0 ; i < arg_count ; i++)
540
547
  {
541
548
    set_if_bigger(decimals, args[i]->decimals);
542
549
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
543
550
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
544
551
  }
545
 
  int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
552
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
546
553
  max_length= my_decimal_precision_to_length(precision, decimals,
547
554
                                             unsigned_flag);
548
555
}
556
563
{
557
564
  max_length= 0;
558
565
  unsigned_flag= 0;
559
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
566
  for (uint i=0 ; i < arg_count ; i++)
560
567
  {
561
568
    set_if_bigger(max_length, args[i]->max_length);
562
569
    set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
574
581
  uint32_t length= 0;
575
582
  decimals= 0;
576
583
  max_length= 0;
577
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
584
  for (uint i=0 ; i < arg_count ; i++)
578
585
  {
579
586
    if (decimals != NOT_FIXED_DEC)
580
587
    {
599
606
void Item_func::signal_divide_by_null()
600
607
{
601
608
  THD *thd= current_thd;
602
 
  push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
609
  if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO)
 
610
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO,
 
611
                 ER(ER_DIVISION_BY_ZERO));
603
612
  null_value= 1;
604
613
}
605
614
 
819
828
      return 0;
820
829
 
821
830
    char *end= (char*) res->ptr() + res->length();
822
 
    const CHARSET_INFO * const cs= str_value.charset();
 
831
    CHARSET_INFO *cs= str_value.charset();
823
832
    return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
824
833
  }
825
834
  default:
905
914
    char err_buff[128];
906
915
    String err_tmp(err_buff,(uint32_t) sizeof(err_buff), system_charset_info);
907
916
    err_tmp.copy(start, length, system_charset_info);
908
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
917
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
909
918
                        ER_TRUNCATED_WRONG_VALUE,
910
919
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
911
920
                        err_tmp.c_ptr());
930
939
  value= val_int_from_str(&error);
931
940
  if (value < 0 && error == 0)
932
941
  {
933
 
    push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
942
    push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
934
943
                 "Cast to signed converted positive out-of-range integer to "
935
944
                 "it's negative complement");
936
945
  }
971
980
 
972
981
  value= val_int_from_str(&error);
973
982
  if (error < 0)
974
 
    push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
983
    push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
975
984
                 "Cast to unsigned converted negative integer to it's "
976
985
                 "positive complement");
977
986
  return value;
1014
1023
{
1015
1024
  my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
1016
1025
  bool sign;
1017
 
  uint32_t precision;
 
1026
  uint precision;
1018
1027
 
1019
1028
  if ((null_value= args[0]->null_value))
1020
1029
    return NULL;
1039
1048
  return dec;
1040
1049
 
1041
1050
err:
1042
 
  push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
1051
  push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1043
1052
                      ER_WARN_DATA_OUT_OF_RANGE,
1044
1053
                      ER(ER_WARN_DATA_OUT_OF_RANGE),
1045
1054
                      name, 1);
1052
1061
  char len_buf[20*3 + 1];
1053
1062
  char *end;
1054
1063
 
1055
 
  uint32_t precision= my_decimal_length_to_precision(max_length, decimals,
 
1064
  uint precision= my_decimal_length_to_precision(max_length, decimals,
1056
1065
                                                 unsigned_flag);
1057
1066
  str->append(STRING_WITH_LEN("cast("));
1058
1067
  args[0]->print(str, query_type);
1120
1129
*/
1121
1130
void Item_func_additive_op::result_precision()
1122
1131
{
1123
 
  decimals= cmax(args[0]->decimals, args[1]->decimals);
1124
 
  int max_int_part= cmax(args[0]->decimal_precision() - args[0]->decimals,
 
1132
  decimals= max(args[0]->decimals, args[1]->decimals);
 
1133
  int max_int_part= max(args[0]->decimal_precision() - args[0]->decimals,
1125
1134
                        args[1]->decimal_precision() - args[1]->decimals);
1126
 
  int precision= cmin(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
 
1135
  int precision= min(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
1127
1136
 
1128
1137
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
1129
1138
  if (result_type() == INT_RESULT)
1143
1152
void Item_func_minus::fix_length_and_dec()
1144
1153
{
1145
1154
  Item_num_op::fix_length_and_dec();
1146
 
  if (unsigned_flag)
1147
 
    unsigned_flag= 0;
 
1155
  if (unsigned_flag &&
 
1156
      (current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
 
1157
    unsigned_flag=0;
1148
1158
}
1149
1159
 
1150
1160
 
1232
1242
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1233
1243
  else
1234
1244
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1235
 
  decimals= cmin(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
1236
 
  int precision= cmin(args[0]->decimal_precision() + args[1]->decimal_precision(),
1237
 
                     (unsigned int)DECIMAL_MAX_PRECISION);
 
1245
  decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
 
1246
  int precision= min(args[0]->decimal_precision() + args[1]->decimal_precision(),
 
1247
                     DECIMAL_MAX_PRECISION);
1238
1248
  max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
1239
1249
}
1240
1250
 
1281
1291
 
1282
1292
void Item_func_div::result_precision()
1283
1293
{
1284
 
  uint32_t precision=cmin(args[0]->decimal_precision() + prec_increment,
1285
 
                     (unsigned int)DECIMAL_MAX_PRECISION);
 
1294
  uint precision=min(args[0]->decimal_precision() + prec_increment,
 
1295
                     DECIMAL_MAX_PRECISION);
1286
1296
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
1287
1297
  if (result_type() == INT_RESULT)
1288
1298
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1289
1299
  else
1290
1300
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1291
 
  decimals= cmin(args[0]->decimals + prec_increment, (unsigned int)DECIMAL_MAX_SCALE);
 
1301
  decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1292
1302
  max_length= my_decimal_precision_to_length(precision, decimals,
1293
1303
                                             unsigned_flag);
1294
1304
}
1301
1311
  switch(hybrid_type) {
1302
1312
  case REAL_RESULT:
1303
1313
  {
1304
 
    decimals=cmax(args[0]->decimals,args[1]->decimals)+prec_increment;
 
1314
    decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
1305
1315
    set_if_smaller(decimals, NOT_FIXED_DEC);
1306
1316
    max_length=args[0]->max_length - args[0]->decimals + decimals;
1307
 
    uint32_t tmp=float_length(decimals);
 
1317
    uint tmp=float_length(decimals);
1308
1318
    set_if_smaller(max_length,tmp);
1309
1319
    break;
1310
1320
  }
1422
1432
 
1423
1433
void Item_func_mod::result_precision()
1424
1434
{
1425
 
  decimals= cmax(args[0]->decimals, args[1]->decimals);
1426
 
  max_length= cmax(args[0]->max_length, args[1]->max_length);
 
1435
  decimals= max(args[0]->decimals, args[1]->decimals);
 
1436
  max_length= max(args[0]->max_length, args[1]->max_length);
1427
1437
}
1428
1438
 
1429
1439
 
1715
1725
int64_t Item_func_shift_left::val_int()
1716
1726
{
1717
1727
  assert(fixed == 1);
1718
 
  uint32_t shift;
 
1728
  uint shift;
1719
1729
  uint64_t res= ((uint64_t) args[0]->val_int() <<
1720
1730
                  (shift=(uint) args[1]->val_int()));
1721
1731
  if (args[0]->null_value || args[1]->null_value)
1724
1734
    return 0;
1725
1735
  }
1726
1736
  null_value=0;
1727
 
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0L);
 
1737
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0LL);
1728
1738
}
1729
1739
 
1730
1740
int64_t Item_func_shift_right::val_int()
1731
1741
{
1732
1742
  assert(fixed == 1);
1733
 
  uint32_t shift;
 
1743
  uint shift;
1734
1744
  uint64_t res= (uint64_t) args[0]->val_int() >>
1735
1745
    (shift=(uint) args[1]->val_int());
1736
1746
  if (args[0]->null_value || args[1]->null_value)
1739
1749
    return 0;
1740
1750
  }
1741
1751
  null_value=0;
1742
 
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0);
 
1752
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0LL);
1743
1753
}
1744
1754
 
1745
1755
 
1758
1768
void Item_func_integer::fix_length_and_dec()
1759
1769
{
1760
1770
  max_length=args[0]->max_length - args[0]->decimals+1;
1761
 
  uint32_t tmp=float_length(decimals);
 
1771
  uint tmp=float_length(decimals);
1762
1772
  set_if_smaller(max_length,tmp);
1763
1773
  decimals=0;
1764
1774
}
1768
1778
  max_length= args[0]->max_length - (args[0]->decimals ?
1769
1779
                                     args[0]->decimals + 1 :
1770
1780
                                     0) + 2;
1771
 
  uint32_t tmp= float_length(decimals);
 
1781
  uint tmp= float_length(decimals);
1772
1782
  set_if_smaller(max_length,tmp);
1773
1783
  decimals= 0;
1774
1784
}
1932
1942
  if (args[0]->decimals == NOT_FIXED_DEC)
1933
1943
  {
1934
1944
    max_length= args[0]->max_length;
1935
 
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
1945
    decimals= min(decimals_to_set, NOT_FIXED_DEC);
1936
1946
    hybrid_type= REAL_RESULT;
1937
1947
    return;
1938
1948
  }
1941
1951
  case REAL_RESULT:
1942
1952
  case STRING_RESULT:
1943
1953
    hybrid_type= REAL_RESULT;
1944
 
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
1954
    decimals= min(decimals_to_set, NOT_FIXED_DEC);
1945
1955
    max_length= float_length(decimals);
1946
1956
    break;
1947
1957
  case INT_RESULT:
1958
1968
  case DECIMAL_RESULT:
1959
1969
  {
1960
1970
    hybrid_type= DECIMAL_RESULT;
1961
 
    decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
 
1971
    decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
1962
1972
    int decimals_delta= args[0]->decimals - decimals_to_set;
1963
1973
    int precision= args[0]->decimal_precision();
1964
1974
    int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
1965
1975
 
1966
1976
    precision-= decimals_delta - length_increase;
1967
 
    decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
 
1977
    decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
1968
1978
    max_length= my_decimal_precision_to_length(precision, decimals,
1969
1979
                                               unsigned_flag);
1970
1980
    break;
1989
1999
  tmp=(abs_dec < array_elements(log_10) ?
1990
2000
       log_10[abs_dec] : pow(10.0,(double) abs_dec));
1991
2001
 
1992
 
  if (dec_negative && isinf(tmp))
 
2002
  if (dec_negative && my_isinf(tmp))
1993
2003
    tmp2= 0;
1994
 
  else if (!dec_negative && isinf(value * tmp))
 
2004
  else if (!dec_negative && my_isinf(value * tmp))
1995
2005
    tmp2= value;
1996
2006
  else if (truncate)
1997
2007
  {
2064
2074
  my_decimal val, *value= args[0]->val_decimal(&val);
2065
2075
  int64_t dec= args[1]->val_int();
2066
2076
  if (dec >= 0 || args[1]->unsigned_flag)
2067
 
    dec= cmin(dec, (int64_t) decimals);
 
2077
    dec= min((uint64_t) dec, decimals);
2068
2078
  else if (dec < INT_MIN)
2069
2079
    dec= INT_MIN;
2070
2080
    
2099
2109
  if (arg_count)
2100
2110
  {                                     // Only use argument once in query
2101
2111
    /*
 
2112
      Allocate rand structure once: we must use thd->stmt_arena
 
2113
      to create rand in proper mem_root if it's a prepared statement or
 
2114
      stored procedure.
 
2115
 
2102
2116
      No need to send a Rand log event if seed was given eg: RAND(seed),
2103
2117
      as it will be replicated in the query as such.
2104
2118
    */
2105
2119
    if (!rand && !(rand= (struct rand_struct*)
2106
 
                   thd->alloc(sizeof(*rand))))
 
2120
                   thd->stmt_arena->alloc(sizeof(*rand))))
2107
2121
      return true;
2108
2122
 
2109
2123
    if (args[0]->const_item())
2170
2184
  maybe_null=0;
2171
2185
  cmp_type=args[0]->result_type();
2172
2186
 
2173
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2187
  for (uint i=0 ; i < arg_count ; i++)
2174
2188
  {
2175
2189
    set_if_bigger(max_length, args[i]->max_length);
2176
2190
    set_if_bigger(decimals, args[i]->decimals);
2219
2233
   #    index of the least/greatest argument
2220
2234
*/
2221
2235
 
2222
 
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
 
2236
uint Item_func_min_max::cmp_datetimes(uint64_t *value)
2223
2237
{
2224
2238
  uint64_t min_max= 0;
2225
 
  uint32_t min_max_idx= 0;
 
2239
  uint min_max_idx= 0;
2226
2240
 
2227
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2241
  for (uint i=0; i < arg_count ; i++)
2228
2242
  {
2229
2243
    Item **arg= args + i;
2230
2244
    bool is_null;
2253
2267
  if (compare_as_dates)
2254
2268
  {
2255
2269
    String *str_res;
2256
 
    uint32_t min_max_idx= cmp_datetimes(NULL);
 
2270
    uint min_max_idx= cmp_datetimes(NULL);
2257
2271
    if (null_value)
2258
2272
      return 0;
2259
2273
    str_res= args[min_max_idx]->val_str(str);
2289
2303
  {
2290
2304
    String *res= NULL;
2291
2305
 
2292
 
    for (uint32_t i=0; i < arg_count ; i++)
 
2306
    for (uint i=0; i < arg_count ; i++)
2293
2307
    {
2294
2308
      if (i == 0)
2295
2309
        res=args[i]->val_str(str);
2330
2344
    (void)cmp_datetimes(&result);
2331
2345
    return (double)result;
2332
2346
  }
2333
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2347
  for (uint i=0; i < arg_count ; i++)
2334
2348
  {
2335
2349
    if (i == 0)
2336
2350
      value= args[i]->val_real();
2357
2371
    (void)cmp_datetimes(&result);
2358
2372
    return (int64_t)result;
2359
2373
  }
2360
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2374
  for (uint i=0; i < arg_count ; i++)
2361
2375
  {
2362
2376
    if (i == 0)
2363
2377
      value=args[i]->val_int();
2386
2400
    uint64_t2decimal(value, dec);
2387
2401
    return dec;
2388
2402
  }
2389
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2403
  for (uint i=0; i < arg_count ; i++)
2390
2404
  {
2391
2405
    if (i == 0)
2392
2406
      res= args[i]->val_decimal(dec);
2525
2539
    String *field;
2526
2540
    if (!(field= args[0]->val_str(&value)))
2527
2541
      return 0;
2528
 
    for (uint32_t i=1 ; i < arg_count ; i++)
 
2542
    for (uint i=1 ; i < arg_count ; i++)
2529
2543
    {
2530
2544
      String *tmp_value=args[i]->val_str(&tmp);
2531
2545
      if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
2537
2551
    int64_t val= args[0]->val_int();
2538
2552
    if (args[0]->null_value)
2539
2553
      return 0;
2540
 
    for (uint32_t i=1; i < arg_count ; i++)
 
2554
    for (uint i=1; i < arg_count ; i++)
2541
2555
    {
2542
2556
      if (val == args[i]->val_int() && !args[i]->null_value)
2543
2557
        return (int64_t) (i);
2549
2563
               dec_buf, *dec= args[0]->val_decimal(&dec_buf);
2550
2564
    if (args[0]->null_value)
2551
2565
      return 0;
2552
 
    for (uint32_t i=1; i < arg_count; i++)
 
2566
    for (uint i=1; i < arg_count; i++)
2553
2567
    {
2554
2568
      dec_arg= args[i]->val_decimal(&dec_arg_buf);
2555
2569
      if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
2561
2575
    double val= args[0]->val_real();
2562
2576
    if (args[0]->null_value)
2563
2577
      return 0;
2564
 
    for (uint32_t i=1; i < arg_count ; i++)
 
2578
    for (uint i=1; i < arg_count ; i++)
2565
2579
    {
2566
2580
      if (val == args[i]->val_real() && !args[i]->null_value)
2567
2581
        return (int64_t) (i);
2575
2589
{
2576
2590
  maybe_null=0; max_length=3;
2577
2591
  cmp_type= args[0]->result_type();
2578
 
  for (uint32_t i=1; i < arg_count ; i++)
 
2592
  for (uint i=1; i < arg_count ; i++)
2579
2593
    cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
2580
2594
  if (cmp_type == STRING_RESULT)
2581
2595
    agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2592
2606
    return 0;
2593
2607
  }
2594
2608
  null_value=0;
2595
 
  return (int64_t) (res->length() ? (unsigned char) (*res)[0] : (unsigned char) 0);
 
2609
  return (int64_t) (res->length() ? (uchar) (*res)[0] : (uchar) 0);
2596
2610
}
2597
2611
 
2598
2612
int64_t Item_func_ord::val_int()
2612
2626
    register const char *str=res->ptr();
2613
2627
    register uint32_t n=0, l=my_ismbchar(res->charset(),str,str+res->length());
2614
2628
    if (!l)
2615
 
      return (int64_t)((unsigned char) *str);
 
2629
      return (int64_t)((uchar) *str);
2616
2630
    while (l--)
2617
 
      n=(n<<8)|(uint32_t)((unsigned char) *str++);
 
2631
      n=(n<<8)|(uint32_t)((uchar) *str++);
2618
2632
    return (int64_t) n;
2619
2633
  }
2620
2634
#endif
2621
 
  return (int64_t) ((unsigned char) (*res)[0]);
 
2635
  return (int64_t) ((uchar) (*res)[0]);
2622
2636
}
2623
2637
 
2624
2638
        /* Search after a string in a string of strings separated by ',' */
2629
2643
{
2630
2644
  decimals=0;
2631
2645
  max_length=3;                                 // 1-999
 
2646
  if (args[0]->const_item() && args[1]->type() == FIELD_ITEM)
 
2647
  {
 
2648
    Field *field= ((Item_field*) args[1])->field;
 
2649
    if (field->real_type() == DRIZZLE_TYPE_SET)
 
2650
    {
 
2651
      String *find=args[0]->val_str(&value);
 
2652
      if (find)
 
2653
      {
 
2654
        enum_value= find_type(((Field_enum*) field)->typelib,find->ptr(),
 
2655
                              find->length(), 0);
 
2656
        enum_bit=0;
 
2657
        if (enum_value)
 
2658
          enum_bit=1LL << (enum_value-1);
 
2659
      }
 
2660
    }
 
2661
  }
2632
2662
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2633
2663
}
2634
2664
 
2661
2691
  if ((diff=buffer->length() - find->length()) >= 0)
2662
2692
  {
2663
2693
    my_wc_t wc;
2664
 
    const CHARSET_INFO * const cs= cmp_collation.collation;
 
2694
    CHARSET_INFO *cs= cmp_collation.collation;
2665
2695
    const char *str_begin= buffer->ptr();
2666
2696
    const char *str_end= buffer->ptr();
2667
2697
    const char *real_end= str_end+buffer->length();
2668
 
    const unsigned char *find_str= (const unsigned char *) find->ptr();
2669
 
    uint32_t find_str_len= find->length();
 
2698
    const uchar *find_str= (const uchar *) find->ptr();
 
2699
    uint find_str_len= find->length();
2670
2700
    int position= 0;
2671
2701
    while (1)
2672
2702
    {
2673
2703
      int symbol_len;
2674
 
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (unsigned char*) str_end, 
2675
 
                                       (unsigned char*) real_end)) > 0)
 
2704
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end, 
 
2705
                                       (uchar*) real_end)) > 0)
2676
2706
      {
2677
2707
        const char *substr_end= str_end + symbol_len;
2678
2708
        bool is_last_item= (substr_end == real_end);
2682
2712
          position++;
2683
2713
          if (is_last_item && !is_separator)
2684
2714
            str_end= substr_end;
2685
 
          if (!my_strnncoll(cs, (const unsigned char *) str_begin,
 
2715
          if (!my_strnncoll(cs, (const uchar *) str_begin,
2686
2716
                            str_end - str_begin,
2687
2717
                            find_str, find_str_len))
2688
2718
            return (int64_t) position;
2696
2726
               wc == (my_wc_t) separator)
2697
2727
        return (int64_t) ++position;
2698
2728
      else
2699
 
        return 0L;
 
2729
        return 0LL;
2700
2730
    }
2701
2731
  }
2702
2732
  return 0;
2711
2741
  return (int64_t) my_count_bits(value);
2712
2742
}
2713
2743
 
 
2744
 
 
2745
/****************************************************************************
 
2746
** Functions to handle dynamic loadable functions
 
2747
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
 
2748
** Rewritten by monty.
 
2749
****************************************************************************/
 
2750
 
 
2751
void udf_handler::cleanup()
 
2752
{
 
2753
  if (!not_original)
 
2754
  {
 
2755
    if (initialized)
 
2756
    {
 
2757
      if (u_d->func_deinit != NULL)
 
2758
      {
 
2759
        Udf_func_deinit deinit= u_d->func_deinit;
 
2760
        (*deinit)(&initid);
 
2761
      }
 
2762
 
 
2763
      initialized= false;
 
2764
    }
 
2765
    if (buffers)                                // Because of bug in ecc
 
2766
      delete [] buffers;
 
2767
    buffers= 0;
 
2768
  }
 
2769
}
 
2770
 
 
2771
 
 
2772
bool
 
2773
udf_handler::fix_fields(THD *thd, Item_result_field *func,
 
2774
                        uint arg_count, Item **arguments)
 
2775
{
 
2776
  uchar buff[STACK_BUFF_ALLOC];                 // Max argument in function
 
2777
 
 
2778
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
 
2779
    return(true);                               // Fatal error flag is set!
 
2780
 
 
2781
  udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
 
2782
 
 
2783
  if (!tmp_udf)
 
2784
  {
 
2785
    my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
 
2786
    return(true);
 
2787
  }
 
2788
  u_d=tmp_udf;
 
2789
  args=arguments;
 
2790
 
 
2791
  /* Fix all arguments */
 
2792
  func->maybe_null=0;
 
2793
  used_tables_cache=0;
 
2794
  const_item_cache=1;
 
2795
 
 
2796
  if ((f_args.arg_count=arg_count))
 
2797
  {
 
2798
    if (!(f_args.arg_type= (Item_result*)
 
2799
          sql_alloc(f_args.arg_count*sizeof(Item_result))))
 
2800
 
 
2801
    {
 
2802
      return(true);
 
2803
    }
 
2804
    uint i;
 
2805
    Item **arg,**arg_end;
 
2806
    for (i=0, arg=arguments, arg_end=arguments+arg_count;
 
2807
         arg != arg_end ;
 
2808
         arg++,i++)
 
2809
    {
 
2810
      if (!(*arg)->fixed &&
 
2811
          (*arg)->fix_fields(thd, arg))
 
2812
        return(1);
 
2813
      // we can't assign 'item' before, because fix_fields() can change arg
 
2814
      Item *item= *arg;
 
2815
      if (item->check_cols(1))
 
2816
        return(true);
 
2817
      /*
 
2818
        TODO: We should think about this. It is not always
 
2819
        right way just to set an UDF result to return my_charset_bin
 
2820
        if one argument has binary sorting order.
 
2821
        The result collation should be calculated according to arguments
 
2822
        derivations in some cases and should not in other cases.
 
2823
        Moreover, some arguments can represent a numeric input
 
2824
        which doesn't effect the result character set and collation.
 
2825
        There is no a general rule for UDF. Everything depends on
 
2826
        the particular user defined function.
 
2827
      */
 
2828
      if (item->collation.collation->state & MY_CS_BINSORT)
 
2829
        func->collation.set(&my_charset_bin);
 
2830
      if (item->maybe_null)
 
2831
        func->maybe_null=1;
 
2832
      func->with_sum_func= func->with_sum_func || item->with_sum_func;
 
2833
      used_tables_cache|=item->used_tables();
 
2834
      const_item_cache&=item->const_item();
 
2835
      f_args.arg_type[i]=item->result_type();
 
2836
    }
 
2837
    //TODO: why all following memory is not allocated with 1 call of sql_alloc?
 
2838
    if (!(buffers=new String[arg_count]) ||
 
2839
        !(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
 
2840
        !(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
 
2841
        !(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
 
2842
        !(num_buffer= (char*) sql_alloc(arg_count *
 
2843
                                        ALIGN_SIZE(sizeof(double)))) ||
 
2844
        !(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
 
2845
        !(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
 
2846
                                                       sizeof(long))))
 
2847
    {
 
2848
      return(true);
 
2849
    }
 
2850
  }
 
2851
  func->fix_length_and_dec();
 
2852
  initid.max_length=func->max_length;
 
2853
  initid.maybe_null=func->maybe_null;
 
2854
  initid.const_item=const_item_cache;
 
2855
  initid.decimals=func->decimals;
 
2856
  initid.ptr=0;
 
2857
 
 
2858
  if (u_d->func_init)
 
2859
  {
 
2860
    char init_msg_buff[MYSQL_ERRMSG_SIZE];
 
2861
    char *to=num_buffer;
 
2862
    for (uint i=0; i < arg_count; i++)
 
2863
    {
 
2864
      /*
 
2865
       For a constant argument i, args->args[i] points to the argument value. 
 
2866
       For non-constant, args->args[i] is NULL.
 
2867
      */
 
2868
      f_args.args[i]= NULL;         /* Non-const unless updated below. */
 
2869
 
 
2870
      f_args.lengths[i]= arguments[i]->max_length;
 
2871
      f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
 
2872
      f_args.attributes[i]= arguments[i]->name;
 
2873
      f_args.attribute_lengths[i]= arguments[i]->name_length;
 
2874
 
 
2875
      if (arguments[i]->const_item())
 
2876
      {
 
2877
        switch (arguments[i]->result_type()) 
 
2878
        {
 
2879
        case STRING_RESULT:
 
2880
        case DECIMAL_RESULT:
 
2881
        {
 
2882
          String *res= arguments[i]->val_str(&buffers[i]);
 
2883
          if (arguments[i]->null_value)
 
2884
            continue;
 
2885
          f_args.args[i]= (char*) res->c_ptr();
 
2886
          f_args.lengths[i]= res->length();
 
2887
          break;
 
2888
        }
 
2889
        case INT_RESULT:
 
2890
          *((int64_t*) to)= arguments[i]->val_int();
 
2891
          if (arguments[i]->null_value)
 
2892
            continue;
 
2893
          f_args.args[i]= to;
 
2894
          to+= ALIGN_SIZE(sizeof(int64_t));
 
2895
          break;
 
2896
        case REAL_RESULT:
 
2897
          *((double*) to)= arguments[i]->val_real();
 
2898
          if (arguments[i]->null_value)
 
2899
            continue;
 
2900
          f_args.args[i]= to;
 
2901
          to+= ALIGN_SIZE(sizeof(double));
 
2902
          break;
 
2903
        case ROW_RESULT:
 
2904
        default:
 
2905
          // This case should never be chosen
 
2906
          assert(0);
 
2907
          break;
 
2908
        }
 
2909
      }
 
2910
    }
 
2911
    Udf_func_init init= u_d->func_init;
 
2912
    if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
 
2913
    {
 
2914
      my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
 
2915
               u_d->name.str, init_msg_buff);
 
2916
      return(true);
 
2917
    }
 
2918
    func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
 
2919
    func->maybe_null=initid.maybe_null;
 
2920
    const_item_cache=initid.const_item;
 
2921
    /* 
 
2922
      Keep used_tables_cache in sync with const_item_cache.
 
2923
      See the comment in Item_udf_func::update_used tables.
 
2924
    */  
 
2925
    if (!const_item_cache && !used_tables_cache)
 
2926
      used_tables_cache= RAND_TABLE_BIT;
 
2927
    func->decimals=min(initid.decimals,NOT_FIXED_DEC);
 
2928
  }
 
2929
  initialized=1;
 
2930
  if (error)
 
2931
  {
 
2932
    my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
 
2933
             u_d->name.str, ER(ER_UNKNOWN_ERROR));
 
2934
    return(true);
 
2935
  }
 
2936
  return(false);
 
2937
}
 
2938
 
 
2939
 
 
2940
bool udf_handler::get_arguments()
 
2941
{
 
2942
  if (error)
 
2943
    return 1;                                   // Got an error earlier
 
2944
  char *to= num_buffer;
 
2945
  uint str_count=0;
 
2946
  for (uint i=0; i < f_args.arg_count; i++)
 
2947
  {
 
2948
    f_args.args[i]=0;
 
2949
    switch (f_args.arg_type[i]) {
 
2950
    case STRING_RESULT:
 
2951
    case DECIMAL_RESULT:
 
2952
      {
 
2953
        String *res=args[i]->val_str(&buffers[str_count++]);
 
2954
        if (!(args[i]->null_value))
 
2955
        {
 
2956
          f_args.args[i]=    (char*) res->ptr();
 
2957
          f_args.lengths[i]= res->length();
 
2958
          break;
 
2959
        }
 
2960
      }
 
2961
    case INT_RESULT:
 
2962
      *((int64_t*) to) = args[i]->val_int();
 
2963
      if (!args[i]->null_value)
 
2964
      {
 
2965
        f_args.args[i]=to;
 
2966
        to+= ALIGN_SIZE(sizeof(int64_t));
 
2967
      }
 
2968
      break;
 
2969
    case REAL_RESULT:
 
2970
      *((double*) to)= args[i]->val_real();
 
2971
      if (!args[i]->null_value)
 
2972
      {
 
2973
        f_args.args[i]=to;
 
2974
        to+= ALIGN_SIZE(sizeof(double));
 
2975
      }
 
2976
      break;
 
2977
    case ROW_RESULT:
 
2978
    default:
 
2979
      // This case should never be chosen
 
2980
      assert(0);
 
2981
      break;
 
2982
    }
 
2983
  }
 
2984
  return 0;
 
2985
}
 
2986
 
 
2987
/**
 
2988
  @return
 
2989
    (String*)NULL in case of NULL values
 
2990
*/
 
2991
String *udf_handler::val_str(String *str,String *save_str)
 
2992
{
 
2993
  uchar is_null_tmp=0;
 
2994
  ulong res_length;
 
2995
 
 
2996
  if (get_arguments())
 
2997
    return(0);
 
2998
  char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
 
2999
    (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
 
3000
    u_d->func;
 
3001
 
 
3002
  if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
 
3003
  {                                             // This happens VERY seldom
 
3004
    if (str->alloc(MAX_FIELD_WIDTH))
 
3005
    {
 
3006
      error=1;
 
3007
      return(0);
 
3008
    }
 
3009
  }
 
3010
  char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
 
3011
                 &is_null_tmp, &error);
 
3012
  if (is_null_tmp || !res || error)             // The !res is for safety
 
3013
  {
 
3014
    return(0);
 
3015
  }
 
3016
  if (res == str->ptr())
 
3017
  {
 
3018
    str->length(res_length);
 
3019
    return(str);
 
3020
  }
 
3021
  save_str->set(res, res_length, str->charset());
 
3022
  return(save_str);
 
3023
}
 
3024
 
 
3025
 
 
3026
/*
 
3027
  For the moment, UDF functions are returning DECIMAL values as strings
 
3028
*/
 
3029
 
 
3030
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
 
3031
{
 
3032
  char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
 
3033
  ulong res_length= DECIMAL_MAX_STR_LENGTH;
 
3034
 
 
3035
  if (get_arguments())
 
3036
  {
 
3037
    *null_value=1;
 
3038
    return 0;
 
3039
  }
 
3040
  char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
 
3041
    (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
 
3042
    u_d->func;
 
3043
 
 
3044
  char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
 
3045
  if (is_null || error)
 
3046
  {
 
3047
    *null_value= 1;
 
3048
    return 0;
 
3049
  }
 
3050
  end= res+ res_length;
 
3051
  str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
 
3052
  return dec_buf;
 
3053
}
 
3054
 
 
3055
 
 
3056
void Item_udf_func::cleanup()
 
3057
{
 
3058
  udf.cleanup();
 
3059
  Item_func::cleanup();
 
3060
}
 
3061
 
 
3062
 
 
3063
void Item_udf_func::print(String *str, enum_query_type query_type)
 
3064
{
 
3065
  str->append(func_name());
 
3066
  str->append('(');
 
3067
  for (uint i=0 ; i < arg_count ; i++)
 
3068
  {
 
3069
    if (i != 0)
 
3070
      str->append(',');
 
3071
    args[i]->print_item_w_name(str, query_type);
 
3072
  }
 
3073
  str->append(')');
 
3074
}
 
3075
 
 
3076
 
 
3077
double Item_func_udf_float::val_real()
 
3078
{
 
3079
  assert(fixed == 1);
 
3080
  return(udf.val(&null_value));
 
3081
}
 
3082
 
 
3083
 
 
3084
String *Item_func_udf_float::val_str(String *str)
 
3085
{
 
3086
  assert(fixed == 1);
 
3087
  double nr= val_real();
 
3088
  if (null_value)
 
3089
    return 0;                                   /* purecov: inspected */
 
3090
  str->set_real(nr,decimals,&my_charset_bin);
 
3091
  return str;
 
3092
}
 
3093
 
 
3094
 
 
3095
int64_t Item_func_udf_int::val_int()
 
3096
{
 
3097
  assert(fixed == 1);
 
3098
  return(udf.val_int(&null_value));
 
3099
}
 
3100
 
 
3101
 
 
3102
String *Item_func_udf_int::val_str(String *str)
 
3103
{
 
3104
  assert(fixed == 1);
 
3105
  int64_t nr=val_int();
 
3106
  if (null_value)
 
3107
    return 0;
 
3108
  str->set_int(nr, unsigned_flag, &my_charset_bin);
 
3109
  return str;
 
3110
}
 
3111
 
 
3112
 
 
3113
int64_t Item_func_udf_decimal::val_int()
 
3114
{
 
3115
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3116
  int64_t result;
 
3117
  if (null_value)
 
3118
    return 0;
 
3119
  my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
3120
  return result;
 
3121
}
 
3122
 
 
3123
 
 
3124
double Item_func_udf_decimal::val_real()
 
3125
{
 
3126
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3127
  double result;
 
3128
  if (null_value)
 
3129
    return 0.0;
 
3130
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
 
3131
  return result;
 
3132
}
 
3133
 
 
3134
 
 
3135
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
 
3136
{
 
3137
  assert(fixed == 1);
 
3138
  return(udf.val_decimal(&null_value, dec_buf));
 
3139
}
 
3140
 
 
3141
 
 
3142
String *Item_func_udf_decimal::val_str(String *str)
 
3143
{
 
3144
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3145
  if (null_value)
 
3146
    return 0;
 
3147
  if (str->length() < DECIMAL_MAX_STR_LENGTH)
 
3148
    str->length(DECIMAL_MAX_STR_LENGTH);
 
3149
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
3150
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
 
3151
  return str;
 
3152
}
 
3153
 
 
3154
 
 
3155
void Item_func_udf_decimal::fix_length_and_dec()
 
3156
{
 
3157
  fix_num_length_and_dec();
 
3158
}
 
3159
 
 
3160
 
 
3161
/* Default max_length is max argument length */
 
3162
 
 
3163
void Item_func_udf_str::fix_length_and_dec()
 
3164
{
 
3165
  max_length=0;
 
3166
  for (uint i = 0; i < arg_count; i++)
 
3167
    set_if_bigger(max_length,args[i]->max_length);
 
3168
  return;
 
3169
}
 
3170
 
 
3171
String *Item_func_udf_str::val_str(String *str)
 
3172
{
 
3173
  assert(fixed == 1);
 
3174
  String *res=udf.val_str(str,&str_value);
 
3175
  null_value = !res;
 
3176
  return res;
 
3177
}
 
3178
 
 
3179
 
 
3180
/**
 
3181
  @note
 
3182
  This has to come last in the udf_handler methods, or C for AIX
 
3183
  version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
 
3184
*/
 
3185
 
 
3186
udf_handler::~udf_handler()
 
3187
{
 
3188
  /* Everything should be properly cleaned up by this moment. */
 
3189
  assert(not_original || !(initialized || buffers));
 
3190
}
 
3191
 
2714
3192
/*
2715
3193
** User level locks
2716
3194
*/
2720
3198
 
2721
3199
class User_level_lock
2722
3200
{
2723
 
  unsigned char *key;
 
3201
  uchar *key;
2724
3202
  size_t key_length;
2725
3203
 
2726
3204
public:
2730
3208
  my_thread_id thread_id;
2731
3209
  void set_thread(THD *thd) { thread_id= thd->thread_id; }
2732
3210
 
2733
 
  User_level_lock(const unsigned char *key_arg,uint32_t length, ulong id) 
 
3211
  User_level_lock(const uchar *key_arg,uint length, ulong id) 
2734
3212
    :key_length(length),count(1),locked(1), thread_id(id)
2735
3213
  {
2736
 
    key= (unsigned char*) my_memdup(key_arg,length,MYF(0));
 
3214
    key= (uchar*) my_memdup(key_arg,length,MYF(0));
2737
3215
    pthread_cond_init(&cond,NULL);
2738
3216
    if (key)
2739
3217
    {
2740
 
      if (my_hash_insert(&hash_user_locks,(unsigned char*) this))
 
3218
      if (my_hash_insert(&hash_user_locks,(uchar*) this))
2741
3219
      {
2742
 
        free(key);
 
3220
        my_free(key,MYF(0));
2743
3221
        key=0;
2744
3222
      }
2745
3223
    }
2748
3226
  {
2749
3227
    if (key)
2750
3228
    {
2751
 
      hash_delete(&hash_user_locks,(unsigned char*) this);
2752
 
      free(key);
 
3229
      hash_delete(&hash_user_locks,(uchar*) this);
 
3230
      my_free(key, MYF(0));
2753
3231
    }
2754
3232
    pthread_cond_destroy(&cond);
2755
3233
  }
2756
3234
  inline bool initialized() { return key != 0; }
2757
3235
  friend void item_user_lock_release(User_level_lock *ull);
2758
 
  friend unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
2759
 
                            bool not_used);
 
3236
  friend uchar *ull_get_key(const User_level_lock *ull, size_t *length,
 
3237
                            my_bool not_used);
2760
3238
};
2761
3239
 
2762
 
unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
2763
 
                   bool not_used __attribute__((unused)))
 
3240
uchar *ull_get_key(const User_level_lock *ull, size_t *length,
 
3241
                   my_bool not_used __attribute__((unused)))
2764
3242
{
2765
3243
  *length= ull->key_length;
2766
3244
  return ull->key;
2772
3250
void item_user_lock_init(void)
2773
3251
{
2774
3252
  pthread_mutex_init(&LOCK_user_locks,MY_MUTEX_INIT_SLOW);
2775
 
  hash_init(&hash_user_locks, system_charset_info,
 
3253
  hash_init(&hash_user_locks,system_charset_info,
2776
3254
            16,0,0,(hash_get_key) ull_get_key,NULL,0);
2777
3255
  item_user_lock_inited= 1;
2778
3256
}
2815
3293
    null_value = 1;
2816
3294
    return 0;
2817
3295
  }
 
3296
#ifdef HAVE_REPLICATION
2818
3297
  int64_t pos = (ulong)args[1]->val_int();
2819
3298
  int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
2820
3299
  if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
2822
3301
    null_value = 1;
2823
3302
    event_count=0;
2824
3303
  }
 
3304
#endif
2825
3305
  return event_count;
2826
3306
}
2827
3307
 
2828
3308
#ifdef EXTRA_DEBUG
2829
 
void debug_sync_point(const char* lock_name, uint32_t lock_timeout)
 
3309
void debug_sync_point(const char* lock_name, uint lock_timeout)
2830
3310
{
2831
3311
}
2832
3312
 
2882
3362
    {
2883
3363
      char buff[22];
2884
3364
      llstr(((int64_t) loop_count), buff);
2885
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
3365
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
2886
3366
                          ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
2887
3367
                          "count", buff, "benchmark");
2888
3368
    }
2934
3414
{
2935
3415
  user_var_entry *entry;
2936
3416
 
2937
 
  if (!(entry = (user_var_entry*) hash_search(hash, (unsigned char*) name.str,
 
3417
  if (!(entry = (user_var_entry*) hash_search(hash, (uchar*) name.str,
2938
3418
                                              name.length)) &&
2939
3419
      create_if_not_exists)
2940
3420
  {
2941
 
    uint32_t size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
 
3421
    uint size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
2942
3422
    if (!hash_inited(hash))
2943
3423
      return 0;
2944
3424
    if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
2964
3444
    entry->used_query_id=current_thd->query_id;
2965
3445
    entry->type=STRING_RESULT;
2966
3446
    memcpy(entry->name.str, name.str, name.length+1);
2967
 
    if (my_hash_insert(hash,(unsigned char*) entry))
 
3447
    if (my_hash_insert(hash,(uchar*) entry))
2968
3448
    {
2969
 
      free((char*) entry);
 
3449
      my_free((char*) entry,MYF(0));
2970
3450
      return 0;
2971
3451
    }
2972
3452
  }
3033
3513
    column read set or to register used fields in a view
3034
3514
*/
3035
3515
 
3036
 
bool Item_func_set_user_var::register_field_in_read_map(unsigned char *arg)
 
3516
bool Item_func_set_user_var::register_field_in_read_map(uchar *arg)
3037
3517
{
3038
3518
  if (result_field)
3039
3519
  {
3040
 
    Table *table= (Table *) arg;
 
3520
    TABLE *table= (TABLE *) arg;
3041
3521
    if (result_field->table == table || !table)
3042
3522
      bitmap_set_bit(result_field->table->read_set, result_field->field_index);
3043
3523
  }
3066
3546
*/
3067
3547
 
3068
3548
static bool
3069
 
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint32_t length,
3070
 
            Item_result type, const CHARSET_INFO * const cs, Derivation dv,
 
3549
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint length,
 
3550
            Item_result type, CHARSET_INFO *cs, Derivation dv,
3071
3551
            bool unsigned_arg)
3072
3552
{
3073
3553
  if (set_null)
3074
3554
  {
3075
3555
    char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3076
3556
    if (entry->value && entry->value != pos)
3077
 
      free(entry->value);
 
3557
      my_free(entry->value,MYF(0));
3078
3558
    entry->value= 0;
3079
3559
    entry->length= 0;
3080
3560
  }
3089
3569
      if (entry->value != pos)
3090
3570
      {
3091
3571
        if (entry->value)
3092
 
          free(entry->value);
 
3572
          my_free(entry->value,MYF(0));
3093
3573
        entry->value=pos;
3094
3574
      }
3095
3575
    }
3126
3606
 
3127
3607
 
3128
3608
bool
3129
 
Item_func_set_user_var::update_hash(void *ptr, uint32_t length,
 
3609
Item_func_set_user_var::update_hash(void *ptr, uint length,
3130
3610
                                    Item_result res_type,
3131
 
                                    const CHARSET_INFO * const cs, Derivation dv,
 
3611
                                    CHARSET_INFO *cs, Derivation dv,
3132
3612
                                    bool unsigned_arg)
3133
3613
{
3134
3614
  /*
3149
3629
 
3150
3630
/** Get the value of a variable as a double. */
3151
3631
 
3152
 
double user_var_entry::val_real(bool *null_value)
 
3632
double user_var_entry::val_real(my_bool *null_value)
3153
3633
{
3154
3634
  if ((*null_value= (value == 0)))
3155
3635
    return 0.0;
3177
3657
 
3178
3658
/** Get the value of a variable as an integer. */
3179
3659
 
3180
 
int64_t user_var_entry::val_int(bool *null_value) const
 
3660
int64_t user_var_entry::val_int(my_bool *null_value) const
3181
3661
{
3182
3662
  if ((*null_value= (value == 0)))
3183
 
    return 0L;
 
3663
    return 0LL;
3184
3664
 
3185
3665
  switch (type) {
3186
3666
  case REAL_RESULT:
3202
3682
    assert(1);                          // Impossible
3203
3683
    break;
3204
3684
  }
3205
 
  return 0L;                                    // Impossible
 
3685
  return 0LL;                                   // Impossible
3206
3686
}
3207
3687
 
3208
3688
 
3209
3689
/** Get the value of a variable as a string. */
3210
3690
 
3211
 
String *user_var_entry::val_str(bool *null_value, String *str,
3212
 
                                uint32_t decimals)
 
3691
String *user_var_entry::val_str(my_bool *null_value, String *str,
 
3692
                                uint decimals)
3213
3693
{
3214
3694
  if ((*null_value= (value == 0)))
3215
3695
    return (String*) 0;
3239
3719
 
3240
3720
/** Get the value of a variable as a decimal. */
3241
3721
 
3242
 
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
 
3722
my_decimal *user_var_entry::val_decimal(my_bool *null_value, my_decimal *val)
3243
3723
{
3244
3724
  if ((*null_value= (value == 0)))
3245
3725
    return 0;
3555
4035
      (result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
3556
4036
  {
3557
4037
    String *result;
3558
 
    const CHARSET_INFO * const cs= collation.collation;
 
4038
    CHARSET_INFO *cs= collation.collation;
3559
4039
    char buff[MAX_FIELD_WIDTH];         // Alloc buffer for small columns
3560
4040
    str_value.set_quick(buff, sizeof(buff), cs);
3561
4041
    result= entry->val_str(&null_value, &str_value, decimals);
3633
4113
{
3634
4114
  assert(fixed == 1);
3635
4115
  if (!var_entry)
3636
 
    return 0L;                          // No such variable
 
4116
    return 0LL;                         // No such variable
3637
4117
  return (var_entry->val_int(&null_value));
3638
4118
}
3639
4119
 
3726
4206
    return 0;
3727
4207
  }
3728
4208
 
3729
 
  uint32_t size;
 
4209
  uint size;
3730
4210
  /*
3731
4211
    First we need to store value of var_entry, when the next situation
3732
4212
    appears:
3763
4243
  }
3764
4244
  /* Mark that this variable has been used by this query */
3765
4245
  var_entry->used_query_id= thd->query_id;
3766
 
  if (insert_dynamic(&thd->user_var_events, (unsigned char*) &user_var_event))
 
4246
  if (insert_dynamic(&thd->user_var_events, (uchar*) &user_var_event))
3767
4247
    goto err;
3768
4248
 
3769
4249
  *out_entry= var_entry;
3882
4362
}
3883
4363
 
3884
4364
 
3885
 
void Item_user_var_as_out_param::set_null_value(const CHARSET_INFO * const cs)
 
4365
void Item_user_var_as_out_param::set_null_value(CHARSET_INFO* cs)
3886
4366
{
3887
4367
  ::update_hash(entry, true, 0, 0, STRING_RESULT, cs,
3888
4368
                DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
3889
4369
}
3890
4370
 
3891
4371
 
3892
 
void Item_user_var_as_out_param::set_value(const char *str, uint32_t length,
3893
 
                                           const CHARSET_INFO * const cs)
 
4372
void Item_user_var_as_out_param::set_value(const char *str, uint length,
 
4373
                                           CHARSET_INFO* cs)
3894
4374
{
3895
4375
  ::update_hash(entry, false, (void*)str, length, STRING_RESULT, cs,
3896
4376
                DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
4060
4540
  }
4061
4541
  
4062
4542
  pthread_mutex_lock(&LOCK_user_locks);
4063
 
  ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
 
4543
  ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4064
4544
                                       (size_t) res->length());
4065
4545
  pthread_mutex_unlock(&LOCK_user_locks);
4066
4546
  if (!ull || !ull->locked)
4079
4559
    return 0;
4080
4560
  
4081
4561
  pthread_mutex_lock(&LOCK_user_locks);
4082
 
  ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
 
4562
  ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4083
4563
                                       (size_t) res->length());
4084
4564
  pthread_mutex_unlock(&LOCK_user_locks);
4085
4565
  if (!ull || !ull->locked)