~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_func.cc

  • Committer: Monty Taylor
  • Date: 2008-08-09 02:24:34 UTC
  • mto: (266.1.8 codestyle)
  • mto: This revision was merged to the branch mainline in revision 279.
  • Revision ID: monty@inaugust.com-20080809022434-97na8pnugghskxip
Removed mystringslt conv lib and replaced it with a noinst lib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
135
135
  assert(fixed == 0);
136
136
  Item **arg,**arg_end;
137
137
  void *save_thd_marker= thd->thd_marker;
138
 
  unsigned char buff[STACK_BUFF_ALLOC];                 // Max argument in function
 
138
  uchar buff[STACK_BUFF_ALLOC];                 // Max argument in function
139
139
  thd->thd_marker= 0;
140
140
  used_tables_cache= not_null_tables_cache= 0;
141
141
  const_item_cache=1;
211
211
 
212
212
 
213
213
bool Item_func::walk(Item_processor processor, bool walk_subquery,
214
 
                     unsigned char *argument)
 
214
                     uchar *argument)
215
215
{
216
216
  if (arg_count)
217
217
  {
270
270
    Item returned as the result of transformation of the root node
271
271
*/
272
272
 
273
 
Item *Item_func::transform(Item_transformer transformer, unsigned char *argument)
 
273
Item *Item_func::transform(Item_transformer transformer, uchar *argument)
274
274
{
275
275
  if (arg_count)
276
276
  {
319
319
    Item returned as the result of transformation of the root node
320
320
*/
321
321
 
322
 
Item *Item_func::compile(Item_analyzer analyzer, unsigned char **arg_p,
323
 
                         Item_transformer transformer, unsigned char *arg_t)
 
322
Item *Item_func::compile(Item_analyzer analyzer, uchar **arg_p,
 
323
                         Item_transformer transformer, uchar *arg_t)
324
324
{
325
325
  if (!(this->*analyzer)(arg_p))
326
326
    return 0;
333
333
        The same parameter value of arg_p must be passed
334
334
        to analyze any argument of the condition formula.
335
335
      */   
336
 
      unsigned char *arg_v= *arg_p;
 
336
      uchar *arg_v= *arg_p;
337
337
      Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
338
338
      if (new_item && *arg != new_item)
339
339
        current_thd->change_item_tree(arg, new_item);
359
359
{
360
360
  used_tables_cache=0;
361
361
  const_item_cache=1;
362
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
362
  for (uint i=0 ; i < arg_count ; i++)
363
363
  {
364
364
    args[i]->update_used_tables();
365
365
    used_tables_cache|=args[i]->used_tables();
389
389
}
390
390
 
391
391
 
392
 
void Item_func::print_args(String *str, uint32_t from, enum_query_type query_type)
 
392
void Item_func::print_args(String *str, uint from, enum_query_type query_type)
393
393
{
394
 
  for (uint32_t i=from ; i < arg_count ; i++)
 
394
  for (uint i=from ; i < arg_count ; i++)
395
395
  {
396
396
    if (i != from)
397
397
      str->append(',');
403
403
void Item_func::print_op(String *str, enum_query_type query_type)
404
404
{
405
405
  str->append('(');
406
 
  for (uint32_t i=0 ; i < arg_count-1 ; i++)
 
406
  for (uint i=0 ; i < arg_count-1 ; i++)
407
407
  {
408
408
    args[i]->print(str, query_type);
409
409
    str->append(' ');
431
431
      (func_type == Item_func::FUNC_SP &&
432
432
       my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
433
433
    return 0;
434
 
  for (uint32_t i=0; i < arg_count ; i++)
 
434
  for (uint i=0; i < arg_count ; i++)
435
435
    if (!args[i]->eq(item_func->args[i], binary_cmp))
436
436
      return 0;
437
437
  return 1;
438
438
}
439
439
 
440
440
 
441
 
Field *Item_func::tmp_table_field(Table *table)
 
441
Field *Item_func::tmp_table_field(TABLE *table)
442
442
{
443
443
  Field *field;
444
444
 
506
506
 
507
507
void Item_func::fix_num_length_and_dec()
508
508
{
509
 
  uint32_t fl_length= 0;
 
509
  uint fl_length= 0;
510
510
  decimals=0;
511
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
511
  for (uint i=0 ; i < arg_count ; i++)
512
512
  {
513
513
    set_if_bigger(decimals,args[i]->decimals);
514
514
    set_if_bigger(fl_length, args[i]->max_length);
536
536
  int max_int_part= 0;
537
537
  decimals= 0;
538
538
  unsigned_flag= 1;
539
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
539
  for (uint i=0 ; i < arg_count ; i++)
540
540
  {
541
541
    set_if_bigger(decimals, args[i]->decimals);
542
542
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
543
543
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
544
544
  }
545
 
  int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
545
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
546
546
  max_length= my_decimal_precision_to_length(precision, decimals,
547
547
                                             unsigned_flag);
548
548
}
556
556
{
557
557
  max_length= 0;
558
558
  unsigned_flag= 0;
559
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
559
  for (uint i=0 ; i < arg_count ; i++)
560
560
  {
561
561
    set_if_bigger(max_length, args[i]->max_length);
562
562
    set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
574
574
  uint32_t length= 0;
575
575
  decimals= 0;
576
576
  max_length= 0;
577
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
577
  for (uint i=0 ; i < arg_count ; i++)
578
578
  {
579
579
    if (decimals != NOT_FIXED_DEC)
580
580
    {
599
599
void Item_func::signal_divide_by_null()
600
600
{
601
601
  THD *thd= current_thd;
602
 
  push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
602
  if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO)
 
603
    push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO,
 
604
                 ER(ER_DIVISION_BY_ZERO));
603
605
  null_value= 1;
604
606
}
605
607
 
1014
1016
{
1015
1017
  my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
1016
1018
  bool sign;
1017
 
  uint32_t precision;
 
1019
  uint precision;
1018
1020
 
1019
1021
  if ((null_value= args[0]->null_value))
1020
1022
    return NULL;
1052
1054
  char len_buf[20*3 + 1];
1053
1055
  char *end;
1054
1056
 
1055
 
  uint32_t precision= my_decimal_length_to_precision(max_length, decimals,
 
1057
  uint precision= my_decimal_length_to_precision(max_length, decimals,
1056
1058
                                                 unsigned_flag);
1057
1059
  str->append(STRING_WITH_LEN("cast("));
1058
1060
  args[0]->print(str, query_type);
1120
1122
*/
1121
1123
void Item_func_additive_op::result_precision()
1122
1124
{
1123
 
  decimals= cmax(args[0]->decimals, args[1]->decimals);
1124
 
  int max_int_part= cmax(args[0]->decimal_precision() - args[0]->decimals,
 
1125
  decimals= max(args[0]->decimals, args[1]->decimals);
 
1126
  int max_int_part= max(args[0]->decimal_precision() - args[0]->decimals,
1125
1127
                        args[1]->decimal_precision() - args[1]->decimals);
1126
 
  int precision= cmin(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
 
1128
  int precision= min(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
1127
1129
 
1128
1130
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
1129
1131
  if (result_type() == INT_RESULT)
1143
1145
void Item_func_minus::fix_length_and_dec()
1144
1146
{
1145
1147
  Item_num_op::fix_length_and_dec();
1146
 
  if (unsigned_flag)
1147
 
    unsigned_flag= 0;
 
1148
  if (unsigned_flag &&
 
1149
      (current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
 
1150
    unsigned_flag=0;
1148
1151
}
1149
1152
 
1150
1153
 
1232
1235
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1233
1236
  else
1234
1237
    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);
 
1238
  decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
 
1239
  int precision= min(args[0]->decimal_precision() + args[1]->decimal_precision(),
 
1240
                     DECIMAL_MAX_PRECISION);
1238
1241
  max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
1239
1242
}
1240
1243
 
1281
1284
 
1282
1285
void Item_func_div::result_precision()
1283
1286
{
1284
 
  uint32_t precision=cmin(args[0]->decimal_precision() + prec_increment,
1285
 
                     (unsigned int)DECIMAL_MAX_PRECISION);
 
1287
  uint precision=min(args[0]->decimal_precision() + prec_increment,
 
1288
                     DECIMAL_MAX_PRECISION);
1286
1289
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
1287
1290
  if (result_type() == INT_RESULT)
1288
1291
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1289
1292
  else
1290
1293
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1291
 
  decimals= cmin(args[0]->decimals + prec_increment, (unsigned int)DECIMAL_MAX_SCALE);
 
1294
  decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1292
1295
  max_length= my_decimal_precision_to_length(precision, decimals,
1293
1296
                                             unsigned_flag);
1294
1297
}
1301
1304
  switch(hybrid_type) {
1302
1305
  case REAL_RESULT:
1303
1306
  {
1304
 
    decimals=cmax(args[0]->decimals,args[1]->decimals)+prec_increment;
 
1307
    decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
1305
1308
    set_if_smaller(decimals, NOT_FIXED_DEC);
1306
1309
    max_length=args[0]->max_length - args[0]->decimals + decimals;
1307
 
    uint32_t tmp=float_length(decimals);
 
1310
    uint tmp=float_length(decimals);
1308
1311
    set_if_smaller(max_length,tmp);
1309
1312
    break;
1310
1313
  }
1422
1425
 
1423
1426
void Item_func_mod::result_precision()
1424
1427
{
1425
 
  decimals= cmax(args[0]->decimals, args[1]->decimals);
1426
 
  max_length= cmax(args[0]->max_length, args[1]->max_length);
 
1428
  decimals= max(args[0]->decimals, args[1]->decimals);
 
1429
  max_length= max(args[0]->max_length, args[1]->max_length);
1427
1430
}
1428
1431
 
1429
1432
 
1715
1718
int64_t Item_func_shift_left::val_int()
1716
1719
{
1717
1720
  assert(fixed == 1);
1718
 
  uint32_t shift;
 
1721
  uint shift;
1719
1722
  uint64_t res= ((uint64_t) args[0]->val_int() <<
1720
1723
                  (shift=(uint) args[1]->val_int()));
1721
1724
  if (args[0]->null_value || args[1]->null_value)
1724
1727
    return 0;
1725
1728
  }
1726
1729
  null_value=0;
1727
 
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0L);
 
1730
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0LL);
1728
1731
}
1729
1732
 
1730
1733
int64_t Item_func_shift_right::val_int()
1731
1734
{
1732
1735
  assert(fixed == 1);
1733
 
  uint32_t shift;
 
1736
  uint shift;
1734
1737
  uint64_t res= (uint64_t) args[0]->val_int() >>
1735
1738
    (shift=(uint) args[1]->val_int());
1736
1739
  if (args[0]->null_value || args[1]->null_value)
1739
1742
    return 0;
1740
1743
  }
1741
1744
  null_value=0;
1742
 
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0);
 
1745
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0LL);
1743
1746
}
1744
1747
 
1745
1748
 
1758
1761
void Item_func_integer::fix_length_and_dec()
1759
1762
{
1760
1763
  max_length=args[0]->max_length - args[0]->decimals+1;
1761
 
  uint32_t tmp=float_length(decimals);
 
1764
  uint tmp=float_length(decimals);
1762
1765
  set_if_smaller(max_length,tmp);
1763
1766
  decimals=0;
1764
1767
}
1768
1771
  max_length= args[0]->max_length - (args[0]->decimals ?
1769
1772
                                     args[0]->decimals + 1 :
1770
1773
                                     0) + 2;
1771
 
  uint32_t tmp= float_length(decimals);
 
1774
  uint tmp= float_length(decimals);
1772
1775
  set_if_smaller(max_length,tmp);
1773
1776
  decimals= 0;
1774
1777
}
1932
1935
  if (args[0]->decimals == NOT_FIXED_DEC)
1933
1936
  {
1934
1937
    max_length= args[0]->max_length;
1935
 
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
1938
    decimals= min(decimals_to_set, NOT_FIXED_DEC);
1936
1939
    hybrid_type= REAL_RESULT;
1937
1940
    return;
1938
1941
  }
1941
1944
  case REAL_RESULT:
1942
1945
  case STRING_RESULT:
1943
1946
    hybrid_type= REAL_RESULT;
1944
 
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
1947
    decimals= min(decimals_to_set, NOT_FIXED_DEC);
1945
1948
    max_length= float_length(decimals);
1946
1949
    break;
1947
1950
  case INT_RESULT:
1958
1961
  case DECIMAL_RESULT:
1959
1962
  {
1960
1963
    hybrid_type= DECIMAL_RESULT;
1961
 
    decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
 
1964
    decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
1962
1965
    int decimals_delta= args[0]->decimals - decimals_to_set;
1963
1966
    int precision= args[0]->decimal_precision();
1964
1967
    int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
1965
1968
 
1966
1969
    precision-= decimals_delta - length_increase;
1967
 
    decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
 
1970
    decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
1968
1971
    max_length= my_decimal_precision_to_length(precision, decimals,
1969
1972
                                               unsigned_flag);
1970
1973
    break;
1989
1992
  tmp=(abs_dec < array_elements(log_10) ?
1990
1993
       log_10[abs_dec] : pow(10.0,(double) abs_dec));
1991
1994
 
1992
 
  if (dec_negative && isinf(tmp))
 
1995
  if (dec_negative && my_isinf(tmp))
1993
1996
    tmp2= 0;
1994
 
  else if (!dec_negative && isinf(value * tmp))
 
1997
  else if (!dec_negative && my_isinf(value * tmp))
1995
1998
    tmp2= value;
1996
1999
  else if (truncate)
1997
2000
  {
2064
2067
  my_decimal val, *value= args[0]->val_decimal(&val);
2065
2068
  int64_t dec= args[1]->val_int();
2066
2069
  if (dec >= 0 || args[1]->unsigned_flag)
2067
 
    dec= cmin(dec, (int64_t) decimals);
 
2070
    dec= min((uint64_t) dec, decimals);
2068
2071
  else if (dec < INT_MIN)
2069
2072
    dec= INT_MIN;
2070
2073
    
2099
2102
  if (arg_count)
2100
2103
  {                                     // Only use argument once in query
2101
2104
    /*
 
2105
      Allocate rand structure once: we must use thd->stmt_arena
 
2106
      to create rand in proper mem_root if it's a prepared statement or
 
2107
      stored procedure.
 
2108
 
2102
2109
      No need to send a Rand log event if seed was given eg: RAND(seed),
2103
2110
      as it will be replicated in the query as such.
2104
2111
    */
2105
2112
    if (!rand && !(rand= (struct rand_struct*)
2106
 
                   thd->alloc(sizeof(*rand))))
 
2113
                   thd->stmt_arena->alloc(sizeof(*rand))))
2107
2114
      return true;
2108
2115
 
2109
2116
    if (args[0]->const_item())
2170
2177
  maybe_null=0;
2171
2178
  cmp_type=args[0]->result_type();
2172
2179
 
2173
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2180
  for (uint i=0 ; i < arg_count ; i++)
2174
2181
  {
2175
2182
    set_if_bigger(max_length, args[i]->max_length);
2176
2183
    set_if_bigger(decimals, args[i]->decimals);
2219
2226
   #    index of the least/greatest argument
2220
2227
*/
2221
2228
 
2222
 
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
 
2229
uint Item_func_min_max::cmp_datetimes(uint64_t *value)
2223
2230
{
2224
2231
  uint64_t min_max= 0;
2225
 
  uint32_t min_max_idx= 0;
 
2232
  uint min_max_idx= 0;
2226
2233
 
2227
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2234
  for (uint i=0; i < arg_count ; i++)
2228
2235
  {
2229
2236
    Item **arg= args + i;
2230
2237
    bool is_null;
2253
2260
  if (compare_as_dates)
2254
2261
  {
2255
2262
    String *str_res;
2256
 
    uint32_t min_max_idx= cmp_datetimes(NULL);
 
2263
    uint min_max_idx= cmp_datetimes(NULL);
2257
2264
    if (null_value)
2258
2265
      return 0;
2259
2266
    str_res= args[min_max_idx]->val_str(str);
2289
2296
  {
2290
2297
    String *res= NULL;
2291
2298
 
2292
 
    for (uint32_t i=0; i < arg_count ; i++)
 
2299
    for (uint i=0; i < arg_count ; i++)
2293
2300
    {
2294
2301
      if (i == 0)
2295
2302
        res=args[i]->val_str(str);
2330
2337
    (void)cmp_datetimes(&result);
2331
2338
    return (double)result;
2332
2339
  }
2333
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2340
  for (uint i=0; i < arg_count ; i++)
2334
2341
  {
2335
2342
    if (i == 0)
2336
2343
      value= args[i]->val_real();
2357
2364
    (void)cmp_datetimes(&result);
2358
2365
    return (int64_t)result;
2359
2366
  }
2360
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2367
  for (uint i=0; i < arg_count ; i++)
2361
2368
  {
2362
2369
    if (i == 0)
2363
2370
      value=args[i]->val_int();
2386
2393
    uint64_t2decimal(value, dec);
2387
2394
    return dec;
2388
2395
  }
2389
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2396
  for (uint i=0; i < arg_count ; i++)
2390
2397
  {
2391
2398
    if (i == 0)
2392
2399
      res= args[i]->val_decimal(dec);
2525
2532
    String *field;
2526
2533
    if (!(field= args[0]->val_str(&value)))
2527
2534
      return 0;
2528
 
    for (uint32_t i=1 ; i < arg_count ; i++)
 
2535
    for (uint i=1 ; i < arg_count ; i++)
2529
2536
    {
2530
2537
      String *tmp_value=args[i]->val_str(&tmp);
2531
2538
      if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
2537
2544
    int64_t val= args[0]->val_int();
2538
2545
    if (args[0]->null_value)
2539
2546
      return 0;
2540
 
    for (uint32_t i=1; i < arg_count ; i++)
 
2547
    for (uint i=1; i < arg_count ; i++)
2541
2548
    {
2542
2549
      if (val == args[i]->val_int() && !args[i]->null_value)
2543
2550
        return (int64_t) (i);
2549
2556
               dec_buf, *dec= args[0]->val_decimal(&dec_buf);
2550
2557
    if (args[0]->null_value)
2551
2558
      return 0;
2552
 
    for (uint32_t i=1; i < arg_count; i++)
 
2559
    for (uint i=1; i < arg_count; i++)
2553
2560
    {
2554
2561
      dec_arg= args[i]->val_decimal(&dec_arg_buf);
2555
2562
      if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
2561
2568
    double val= args[0]->val_real();
2562
2569
    if (args[0]->null_value)
2563
2570
      return 0;
2564
 
    for (uint32_t i=1; i < arg_count ; i++)
 
2571
    for (uint i=1; i < arg_count ; i++)
2565
2572
    {
2566
2573
      if (val == args[i]->val_real() && !args[i]->null_value)
2567
2574
        return (int64_t) (i);
2575
2582
{
2576
2583
  maybe_null=0; max_length=3;
2577
2584
  cmp_type= args[0]->result_type();
2578
 
  for (uint32_t i=1; i < arg_count ; i++)
 
2585
  for (uint i=1; i < arg_count ; i++)
2579
2586
    cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
2580
2587
  if (cmp_type == STRING_RESULT)
2581
2588
    agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2592
2599
    return 0;
2593
2600
  }
2594
2601
  null_value=0;
2595
 
  return (int64_t) (res->length() ? (unsigned char) (*res)[0] : (unsigned char) 0);
 
2602
  return (int64_t) (res->length() ? (uchar) (*res)[0] : (uchar) 0);
2596
2603
}
2597
2604
 
2598
2605
int64_t Item_func_ord::val_int()
2612
2619
    register const char *str=res->ptr();
2613
2620
    register uint32_t n=0, l=my_ismbchar(res->charset(),str,str+res->length());
2614
2621
    if (!l)
2615
 
      return (int64_t)((unsigned char) *str);
 
2622
      return (int64_t)((uchar) *str);
2616
2623
    while (l--)
2617
 
      n=(n<<8)|(uint32_t)((unsigned char) *str++);
 
2624
      n=(n<<8)|(uint32_t)((uchar) *str++);
2618
2625
    return (int64_t) n;
2619
2626
  }
2620
2627
#endif
2621
 
  return (int64_t) ((unsigned char) (*res)[0]);
 
2628
  return (int64_t) ((uchar) (*res)[0]);
2622
2629
}
2623
2630
 
2624
2631
        /* Search after a string in a string of strings separated by ',' */
2629
2636
{
2630
2637
  decimals=0;
2631
2638
  max_length=3;                                 // 1-999
 
2639
  if (args[0]->const_item() && args[1]->type() == FIELD_ITEM)
 
2640
  {
 
2641
    Field *field= ((Item_field*) args[1])->field;
 
2642
    if (field->real_type() == DRIZZLE_TYPE_SET)
 
2643
    {
 
2644
      String *find=args[0]->val_str(&value);
 
2645
      if (find)
 
2646
      {
 
2647
        enum_value= find_type(((Field_enum*) field)->typelib,find->ptr(),
 
2648
                              find->length(), 0);
 
2649
        enum_bit=0;
 
2650
        if (enum_value)
 
2651
          enum_bit=1LL << (enum_value-1);
 
2652
      }
 
2653
    }
 
2654
  }
2632
2655
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2633
2656
}
2634
2657
 
2665
2688
    const char *str_begin= buffer->ptr();
2666
2689
    const char *str_end= buffer->ptr();
2667
2690
    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();
 
2691
    const uchar *find_str= (const uchar *) find->ptr();
 
2692
    uint find_str_len= find->length();
2670
2693
    int position= 0;
2671
2694
    while (1)
2672
2695
    {
2673
2696
      int symbol_len;
2674
 
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (unsigned char*) str_end, 
2675
 
                                       (unsigned char*) real_end)) > 0)
 
2697
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end, 
 
2698
                                       (uchar*) real_end)) > 0)
2676
2699
      {
2677
2700
        const char *substr_end= str_end + symbol_len;
2678
2701
        bool is_last_item= (substr_end == real_end);
2682
2705
          position++;
2683
2706
          if (is_last_item && !is_separator)
2684
2707
            str_end= substr_end;
2685
 
          if (!my_strnncoll(cs, (const unsigned char *) str_begin,
 
2708
          if (!my_strnncoll(cs, (const uchar *) str_begin,
2686
2709
                            str_end - str_begin,
2687
2710
                            find_str, find_str_len))
2688
2711
            return (int64_t) position;
2696
2719
               wc == (my_wc_t) separator)
2697
2720
        return (int64_t) ++position;
2698
2721
      else
2699
 
        return 0L;
 
2722
        return 0LL;
2700
2723
    }
2701
2724
  }
2702
2725
  return 0;
2711
2734
  return (int64_t) my_count_bits(value);
2712
2735
}
2713
2736
 
 
2737
 
 
2738
/****************************************************************************
 
2739
** Functions to handle dynamic loadable functions
 
2740
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
 
2741
** Rewritten by monty.
 
2742
****************************************************************************/
 
2743
 
 
2744
void udf_handler::cleanup()
 
2745
{
 
2746
  if (!not_original)
 
2747
  {
 
2748
    if (initialized)
 
2749
    {
 
2750
      if (u_d->func_deinit != NULL)
 
2751
      {
 
2752
        Udf_func_deinit deinit= u_d->func_deinit;
 
2753
        (*deinit)(&initid);
 
2754
      }
 
2755
 
 
2756
      initialized= false;
 
2757
    }
 
2758
    if (buffers)                                // Because of bug in ecc
 
2759
      delete [] buffers;
 
2760
    buffers= 0;
 
2761
  }
 
2762
}
 
2763
 
 
2764
 
 
2765
bool
 
2766
udf_handler::fix_fields(THD *thd, Item_result_field *func,
 
2767
                        uint arg_count, Item **arguments)
 
2768
{
 
2769
  uchar buff[STACK_BUFF_ALLOC];                 // Max argument in function
 
2770
 
 
2771
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
 
2772
    return(true);                               // Fatal error flag is set!
 
2773
 
 
2774
  udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
 
2775
 
 
2776
  if (!tmp_udf)
 
2777
  {
 
2778
    my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
 
2779
    return(true);
 
2780
  }
 
2781
  u_d=tmp_udf;
 
2782
  args=arguments;
 
2783
 
 
2784
  /* Fix all arguments */
 
2785
  func->maybe_null=0;
 
2786
  used_tables_cache=0;
 
2787
  const_item_cache=1;
 
2788
 
 
2789
  if ((f_args.arg_count=arg_count))
 
2790
  {
 
2791
    if (!(f_args.arg_type= (Item_result*)
 
2792
          sql_alloc(f_args.arg_count*sizeof(Item_result))))
 
2793
 
 
2794
    {
 
2795
      return(true);
 
2796
    }
 
2797
    uint i;
 
2798
    Item **arg,**arg_end;
 
2799
    for (i=0, arg=arguments, arg_end=arguments+arg_count;
 
2800
         arg != arg_end ;
 
2801
         arg++,i++)
 
2802
    {
 
2803
      if (!(*arg)->fixed &&
 
2804
          (*arg)->fix_fields(thd, arg))
 
2805
        return(1);
 
2806
      // we can't assign 'item' before, because fix_fields() can change arg
 
2807
      Item *item= *arg;
 
2808
      if (item->check_cols(1))
 
2809
        return(true);
 
2810
      /*
 
2811
        TODO: We should think about this. It is not always
 
2812
        right way just to set an UDF result to return my_charset_bin
 
2813
        if one argument has binary sorting order.
 
2814
        The result collation should be calculated according to arguments
 
2815
        derivations in some cases and should not in other cases.
 
2816
        Moreover, some arguments can represent a numeric input
 
2817
        which doesn't effect the result character set and collation.
 
2818
        There is no a general rule for UDF. Everything depends on
 
2819
        the particular user defined function.
 
2820
      */
 
2821
      if (item->collation.collation->state & MY_CS_BINSORT)
 
2822
        func->collation.set(&my_charset_bin);
 
2823
      if (item->maybe_null)
 
2824
        func->maybe_null=1;
 
2825
      func->with_sum_func= func->with_sum_func || item->with_sum_func;
 
2826
      used_tables_cache|=item->used_tables();
 
2827
      const_item_cache&=item->const_item();
 
2828
      f_args.arg_type[i]=item->result_type();
 
2829
    }
 
2830
    //TODO: why all following memory is not allocated with 1 call of sql_alloc?
 
2831
    if (!(buffers=new String[arg_count]) ||
 
2832
        !(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
 
2833
        !(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
 
2834
        !(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
 
2835
        !(num_buffer= (char*) sql_alloc(arg_count *
 
2836
                                        ALIGN_SIZE(sizeof(double)))) ||
 
2837
        !(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
 
2838
        !(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
 
2839
                                                       sizeof(long))))
 
2840
    {
 
2841
      return(true);
 
2842
    }
 
2843
  }
 
2844
  func->fix_length_and_dec();
 
2845
  initid.max_length=func->max_length;
 
2846
  initid.maybe_null=func->maybe_null;
 
2847
  initid.const_item=const_item_cache;
 
2848
  initid.decimals=func->decimals;
 
2849
  initid.ptr=0;
 
2850
 
 
2851
  if (u_d->func_init)
 
2852
  {
 
2853
    char init_msg_buff[DRIZZLE_ERRMSG_SIZE];
 
2854
    char *to=num_buffer;
 
2855
    for (uint i=0; i < arg_count; i++)
 
2856
    {
 
2857
      /*
 
2858
       For a constant argument i, args->args[i] points to the argument value. 
 
2859
       For non-constant, args->args[i] is NULL.
 
2860
      */
 
2861
      f_args.args[i]= NULL;         /* Non-const unless updated below. */
 
2862
 
 
2863
      f_args.lengths[i]= arguments[i]->max_length;
 
2864
      f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
 
2865
      f_args.attributes[i]= arguments[i]->name;
 
2866
      f_args.attribute_lengths[i]= arguments[i]->name_length;
 
2867
 
 
2868
      if (arguments[i]->const_item())
 
2869
      {
 
2870
        switch (arguments[i]->result_type()) 
 
2871
        {
 
2872
        case STRING_RESULT:
 
2873
        case DECIMAL_RESULT:
 
2874
        {
 
2875
          String *res= arguments[i]->val_str(&buffers[i]);
 
2876
          if (arguments[i]->null_value)
 
2877
            continue;
 
2878
          f_args.args[i]= (char*) res->c_ptr();
 
2879
          f_args.lengths[i]= res->length();
 
2880
          break;
 
2881
        }
 
2882
        case INT_RESULT:
 
2883
          *((int64_t*) to)= arguments[i]->val_int();
 
2884
          if (arguments[i]->null_value)
 
2885
            continue;
 
2886
          f_args.args[i]= to;
 
2887
          to+= ALIGN_SIZE(sizeof(int64_t));
 
2888
          break;
 
2889
        case REAL_RESULT:
 
2890
          *((double*) to)= arguments[i]->val_real();
 
2891
          if (arguments[i]->null_value)
 
2892
            continue;
 
2893
          f_args.args[i]= to;
 
2894
          to+= ALIGN_SIZE(sizeof(double));
 
2895
          break;
 
2896
        case ROW_RESULT:
 
2897
        default:
 
2898
          // This case should never be chosen
 
2899
          assert(0);
 
2900
          break;
 
2901
        }
 
2902
      }
 
2903
    }
 
2904
    Udf_func_init init= u_d->func_init;
 
2905
    if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
 
2906
    {
 
2907
      my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
 
2908
               u_d->name.str, init_msg_buff);
 
2909
      return(true);
 
2910
    }
 
2911
    func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
 
2912
    func->maybe_null=initid.maybe_null;
 
2913
    const_item_cache=initid.const_item;
 
2914
    /* 
 
2915
      Keep used_tables_cache in sync with const_item_cache.
 
2916
      See the comment in Item_udf_func::update_used tables.
 
2917
    */  
 
2918
    if (!const_item_cache && !used_tables_cache)
 
2919
      used_tables_cache= RAND_TABLE_BIT;
 
2920
    func->decimals=min(initid.decimals,NOT_FIXED_DEC);
 
2921
  }
 
2922
  initialized=1;
 
2923
  if (error)
 
2924
  {
 
2925
    my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
 
2926
             u_d->name.str, ER(ER_UNKNOWN_ERROR));
 
2927
    return(true);
 
2928
  }
 
2929
  return(false);
 
2930
}
 
2931
 
 
2932
 
 
2933
bool udf_handler::get_arguments()
 
2934
{
 
2935
  if (error)
 
2936
    return 1;                                   // Got an error earlier
 
2937
  char *to= num_buffer;
 
2938
  uint str_count=0;
 
2939
  for (uint i=0; i < f_args.arg_count; i++)
 
2940
  {
 
2941
    f_args.args[i]=0;
 
2942
    switch (f_args.arg_type[i]) {
 
2943
    case STRING_RESULT:
 
2944
    case DECIMAL_RESULT:
 
2945
      {
 
2946
        String *res=args[i]->val_str(&buffers[str_count++]);
 
2947
        if (!(args[i]->null_value))
 
2948
        {
 
2949
          f_args.args[i]=    (char*) res->ptr();
 
2950
          f_args.lengths[i]= res->length();
 
2951
          break;
 
2952
        }
 
2953
      }
 
2954
    case INT_RESULT:
 
2955
      *((int64_t*) to) = args[i]->val_int();
 
2956
      if (!args[i]->null_value)
 
2957
      {
 
2958
        f_args.args[i]=to;
 
2959
        to+= ALIGN_SIZE(sizeof(int64_t));
 
2960
      }
 
2961
      break;
 
2962
    case REAL_RESULT:
 
2963
      *((double*) to)= args[i]->val_real();
 
2964
      if (!args[i]->null_value)
 
2965
      {
 
2966
        f_args.args[i]=to;
 
2967
        to+= ALIGN_SIZE(sizeof(double));
 
2968
      }
 
2969
      break;
 
2970
    case ROW_RESULT:
 
2971
    default:
 
2972
      // This case should never be chosen
 
2973
      assert(0);
 
2974
      break;
 
2975
    }
 
2976
  }
 
2977
  return 0;
 
2978
}
 
2979
 
 
2980
/**
 
2981
  @return
 
2982
    (String*)NULL in case of NULL values
 
2983
*/
 
2984
String *udf_handler::val_str(String *str,String *save_str)
 
2985
{
 
2986
  uchar is_null_tmp=0;
 
2987
  ulong res_length;
 
2988
 
 
2989
  if (get_arguments())
 
2990
    return(0);
 
2991
  char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
 
2992
    (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
 
2993
    u_d->func;
 
2994
 
 
2995
  if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
 
2996
  {                                             // This happens VERY seldom
 
2997
    if (str->alloc(MAX_FIELD_WIDTH))
 
2998
    {
 
2999
      error=1;
 
3000
      return(0);
 
3001
    }
 
3002
  }
 
3003
  char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
 
3004
                 &is_null_tmp, &error);
 
3005
  if (is_null_tmp || !res || error)             // The !res is for safety
 
3006
  {
 
3007
    return(0);
 
3008
  }
 
3009
  if (res == str->ptr())
 
3010
  {
 
3011
    str->length(res_length);
 
3012
    return(str);
 
3013
  }
 
3014
  save_str->set(res, res_length, str->charset());
 
3015
  return(save_str);
 
3016
}
 
3017
 
 
3018
 
 
3019
/*
 
3020
  For the moment, UDF functions are returning DECIMAL values as strings
 
3021
*/
 
3022
 
 
3023
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
 
3024
{
 
3025
  char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
 
3026
  ulong res_length= DECIMAL_MAX_STR_LENGTH;
 
3027
 
 
3028
  if (get_arguments())
 
3029
  {
 
3030
    *null_value=1;
 
3031
    return 0;
 
3032
  }
 
3033
  char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
 
3034
    (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
 
3035
    u_d->func;
 
3036
 
 
3037
  char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
 
3038
  if (is_null || error)
 
3039
  {
 
3040
    *null_value= 1;
 
3041
    return 0;
 
3042
  }
 
3043
  end= res+ res_length;
 
3044
  str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
 
3045
  return dec_buf;
 
3046
}
 
3047
 
 
3048
 
 
3049
void Item_udf_func::cleanup()
 
3050
{
 
3051
  udf.cleanup();
 
3052
  Item_func::cleanup();
 
3053
}
 
3054
 
 
3055
 
 
3056
void Item_udf_func::print(String *str, enum_query_type query_type)
 
3057
{
 
3058
  str->append(func_name());
 
3059
  str->append('(');
 
3060
  for (uint i=0 ; i < arg_count ; i++)
 
3061
  {
 
3062
    if (i != 0)
 
3063
      str->append(',');
 
3064
    args[i]->print_item_w_name(str, query_type);
 
3065
  }
 
3066
  str->append(')');
 
3067
}
 
3068
 
 
3069
 
 
3070
double Item_func_udf_float::val_real()
 
3071
{
 
3072
  assert(fixed == 1);
 
3073
  return(udf.val(&null_value));
 
3074
}
 
3075
 
 
3076
 
 
3077
String *Item_func_udf_float::val_str(String *str)
 
3078
{
 
3079
  assert(fixed == 1);
 
3080
  double nr= val_real();
 
3081
  if (null_value)
 
3082
    return 0;                                   /* purecov: inspected */
 
3083
  str->set_real(nr,decimals,&my_charset_bin);
 
3084
  return str;
 
3085
}
 
3086
 
 
3087
 
 
3088
int64_t Item_func_udf_int::val_int()
 
3089
{
 
3090
  assert(fixed == 1);
 
3091
  return(udf.val_int(&null_value));
 
3092
}
 
3093
 
 
3094
 
 
3095
String *Item_func_udf_int::val_str(String *str)
 
3096
{
 
3097
  assert(fixed == 1);
 
3098
  int64_t nr=val_int();
 
3099
  if (null_value)
 
3100
    return 0;
 
3101
  str->set_int(nr, unsigned_flag, &my_charset_bin);
 
3102
  return str;
 
3103
}
 
3104
 
 
3105
 
 
3106
int64_t Item_func_udf_decimal::val_int()
 
3107
{
 
3108
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3109
  int64_t result;
 
3110
  if (null_value)
 
3111
    return 0;
 
3112
  my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
3113
  return result;
 
3114
}
 
3115
 
 
3116
 
 
3117
double Item_func_udf_decimal::val_real()
 
3118
{
 
3119
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3120
  double result;
 
3121
  if (null_value)
 
3122
    return 0.0;
 
3123
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
 
3124
  return result;
 
3125
}
 
3126
 
 
3127
 
 
3128
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
 
3129
{
 
3130
  assert(fixed == 1);
 
3131
  return(udf.val_decimal(&null_value, dec_buf));
 
3132
}
 
3133
 
 
3134
 
 
3135
String *Item_func_udf_decimal::val_str(String *str)
 
3136
{
 
3137
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3138
  if (null_value)
 
3139
    return 0;
 
3140
  if (str->length() < DECIMAL_MAX_STR_LENGTH)
 
3141
    str->length(DECIMAL_MAX_STR_LENGTH);
 
3142
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
3143
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
 
3144
  return str;
 
3145
}
 
3146
 
 
3147
 
 
3148
void Item_func_udf_decimal::fix_length_and_dec()
 
3149
{
 
3150
  fix_num_length_and_dec();
 
3151
}
 
3152
 
 
3153
 
 
3154
/* Default max_length is max argument length */
 
3155
 
 
3156
void Item_func_udf_str::fix_length_and_dec()
 
3157
{
 
3158
  max_length=0;
 
3159
  for (uint i = 0; i < arg_count; i++)
 
3160
    set_if_bigger(max_length,args[i]->max_length);
 
3161
  return;
 
3162
}
 
3163
 
 
3164
String *Item_func_udf_str::val_str(String *str)
 
3165
{
 
3166
  assert(fixed == 1);
 
3167
  String *res=udf.val_str(str,&str_value);
 
3168
  null_value = !res;
 
3169
  return res;
 
3170
}
 
3171
 
 
3172
 
 
3173
/**
 
3174
  @note
 
3175
  This has to come last in the udf_handler methods, or C for AIX
 
3176
  version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
 
3177
*/
 
3178
 
 
3179
udf_handler::~udf_handler()
 
3180
{
 
3181
  /* Everything should be properly cleaned up by this moment. */
 
3182
  assert(not_original || !(initialized || buffers));
 
3183
}
 
3184
 
2714
3185
/*
2715
3186
** User level locks
2716
3187
*/
2720
3191
 
2721
3192
class User_level_lock
2722
3193
{
2723
 
  unsigned char *key;
 
3194
  uchar *key;
2724
3195
  size_t key_length;
2725
3196
 
2726
3197
public:
2730
3201
  my_thread_id thread_id;
2731
3202
  void set_thread(THD *thd) { thread_id= thd->thread_id; }
2732
3203
 
2733
 
  User_level_lock(const unsigned char *key_arg,uint32_t length, ulong id) 
 
3204
  User_level_lock(const uchar *key_arg,uint length, ulong id) 
2734
3205
    :key_length(length),count(1),locked(1), thread_id(id)
2735
3206
  {
2736
 
    key= (unsigned char*) my_memdup(key_arg,length,MYF(0));
 
3207
    key= (uchar*) my_memdup(key_arg,length,MYF(0));
2737
3208
    pthread_cond_init(&cond,NULL);
2738
3209
    if (key)
2739
3210
    {
2740
 
      if (my_hash_insert(&hash_user_locks,(unsigned char*) this))
 
3211
      if (my_hash_insert(&hash_user_locks,(uchar*) this))
2741
3212
      {
2742
 
        free(key);
 
3213
        my_free(key,MYF(0));
2743
3214
        key=0;
2744
3215
      }
2745
3216
    }
2748
3219
  {
2749
3220
    if (key)
2750
3221
    {
2751
 
      hash_delete(&hash_user_locks,(unsigned char*) this);
2752
 
      free(key);
 
3222
      hash_delete(&hash_user_locks,(uchar*) this);
 
3223
      my_free(key, MYF(0));
2753
3224
    }
2754
3225
    pthread_cond_destroy(&cond);
2755
3226
  }
2756
3227
  inline bool initialized() { return key != 0; }
2757
3228
  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);
 
3229
  friend uchar *ull_get_key(const User_level_lock *ull, size_t *length,
 
3230
                            my_bool not_used);
2760
3231
};
2761
3232
 
2762
 
unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
2763
 
                   bool not_used __attribute__((unused)))
 
3233
uchar *ull_get_key(const User_level_lock *ull, size_t *length,
 
3234
                   my_bool not_used __attribute__((unused)))
2764
3235
{
2765
3236
  *length= ull->key_length;
2766
3237
  return ull->key;
2815
3286
    null_value = 1;
2816
3287
    return 0;
2817
3288
  }
 
3289
#ifdef HAVE_REPLICATION
2818
3290
  int64_t pos = (ulong)args[1]->val_int();
2819
3291
  int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
2820
3292
  if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
2822
3294
    null_value = 1;
2823
3295
    event_count=0;
2824
3296
  }
 
3297
#endif
2825
3298
  return event_count;
2826
3299
}
2827
3300
 
2828
3301
#ifdef EXTRA_DEBUG
2829
 
void debug_sync_point(const char* lock_name, uint32_t lock_timeout)
 
3302
void debug_sync_point(const char* lock_name, uint lock_timeout)
2830
3303
{
2831
3304
}
2832
3305
 
2934
3407
{
2935
3408
  user_var_entry *entry;
2936
3409
 
2937
 
  if (!(entry = (user_var_entry*) hash_search(hash, (unsigned char*) name.str,
 
3410
  if (!(entry = (user_var_entry*) hash_search(hash, (uchar*) name.str,
2938
3411
                                              name.length)) &&
2939
3412
      create_if_not_exists)
2940
3413
  {
2941
 
    uint32_t size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
 
3414
    uint size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
2942
3415
    if (!hash_inited(hash))
2943
3416
      return 0;
2944
3417
    if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
2964
3437
    entry->used_query_id=current_thd->query_id;
2965
3438
    entry->type=STRING_RESULT;
2966
3439
    memcpy(entry->name.str, name.str, name.length+1);
2967
 
    if (my_hash_insert(hash,(unsigned char*) entry))
 
3440
    if (my_hash_insert(hash,(uchar*) entry))
2968
3441
    {
2969
 
      free((char*) entry);
 
3442
      my_free((char*) entry,MYF(0));
2970
3443
      return 0;
2971
3444
    }
2972
3445
  }
3033
3506
    column read set or to register used fields in a view
3034
3507
*/
3035
3508
 
3036
 
bool Item_func_set_user_var::register_field_in_read_map(unsigned char *arg)
 
3509
bool Item_func_set_user_var::register_field_in_read_map(uchar *arg)
3037
3510
{
3038
3511
  if (result_field)
3039
3512
  {
3040
 
    Table *table= (Table *) arg;
 
3513
    TABLE *table= (TABLE *) arg;
3041
3514
    if (result_field->table == table || !table)
3042
3515
      bitmap_set_bit(result_field->table->read_set, result_field->field_index);
3043
3516
  }
3066
3539
*/
3067
3540
 
3068
3541
static bool
3069
 
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint32_t length,
 
3542
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint length,
3070
3543
            Item_result type, const CHARSET_INFO * const cs, Derivation dv,
3071
3544
            bool unsigned_arg)
3072
3545
{
3074
3547
  {
3075
3548
    char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3076
3549
    if (entry->value && entry->value != pos)
3077
 
      free(entry->value);
 
3550
      my_free(entry->value,MYF(0));
3078
3551
    entry->value= 0;
3079
3552
    entry->length= 0;
3080
3553
  }
3089
3562
      if (entry->value != pos)
3090
3563
      {
3091
3564
        if (entry->value)
3092
 
          free(entry->value);
 
3565
          my_free(entry->value,MYF(0));
3093
3566
        entry->value=pos;
3094
3567
      }
3095
3568
    }
3126
3599
 
3127
3600
 
3128
3601
bool
3129
 
Item_func_set_user_var::update_hash(void *ptr, uint32_t length,
 
3602
Item_func_set_user_var::update_hash(void *ptr, uint length,
3130
3603
                                    Item_result res_type,
3131
3604
                                    const CHARSET_INFO * const cs, Derivation dv,
3132
3605
                                    bool unsigned_arg)
3149
3622
 
3150
3623
/** Get the value of a variable as a double. */
3151
3624
 
3152
 
double user_var_entry::val_real(bool *null_value)
 
3625
double user_var_entry::val_real(my_bool *null_value)
3153
3626
{
3154
3627
  if ((*null_value= (value == 0)))
3155
3628
    return 0.0;
3177
3650
 
3178
3651
/** Get the value of a variable as an integer. */
3179
3652
 
3180
 
int64_t user_var_entry::val_int(bool *null_value) const
 
3653
int64_t user_var_entry::val_int(my_bool *null_value) const
3181
3654
{
3182
3655
  if ((*null_value= (value == 0)))
3183
 
    return 0L;
 
3656
    return 0LL;
3184
3657
 
3185
3658
  switch (type) {
3186
3659
  case REAL_RESULT:
3202
3675
    assert(1);                          // Impossible
3203
3676
    break;
3204
3677
  }
3205
 
  return 0L;                                    // Impossible
 
3678
  return 0LL;                                   // Impossible
3206
3679
}
3207
3680
 
3208
3681
 
3209
3682
/** Get the value of a variable as a string. */
3210
3683
 
3211
 
String *user_var_entry::val_str(bool *null_value, String *str,
3212
 
                                uint32_t decimals)
 
3684
String *user_var_entry::val_str(my_bool *null_value, String *str,
 
3685
                                uint decimals)
3213
3686
{
3214
3687
  if ((*null_value= (value == 0)))
3215
3688
    return (String*) 0;
3239
3712
 
3240
3713
/** Get the value of a variable as a decimal. */
3241
3714
 
3242
 
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
 
3715
my_decimal *user_var_entry::val_decimal(my_bool *null_value, my_decimal *val)
3243
3716
{
3244
3717
  if ((*null_value= (value == 0)))
3245
3718
    return 0;
3633
4106
{
3634
4107
  assert(fixed == 1);
3635
4108
  if (!var_entry)
3636
 
    return 0L;                          // No such variable
 
4109
    return 0LL;                         // No such variable
3637
4110
  return (var_entry->val_int(&null_value));
3638
4111
}
3639
4112
 
3726
4199
    return 0;
3727
4200
  }
3728
4201
 
3729
 
  uint32_t size;
 
4202
  uint size;
3730
4203
  /*
3731
4204
    First we need to store value of var_entry, when the next situation
3732
4205
    appears:
3763
4236
  }
3764
4237
  /* Mark that this variable has been used by this query */
3765
4238
  var_entry->used_query_id= thd->query_id;
3766
 
  if (insert_dynamic(&thd->user_var_events, (unsigned char*) &user_var_event))
 
4239
  if (insert_dynamic(&thd->user_var_events, (uchar*) &user_var_event))
3767
4240
    goto err;
3768
4241
 
3769
4242
  *out_entry= var_entry;
3889
4362
}
3890
4363
 
3891
4364
 
3892
 
void Item_user_var_as_out_param::set_value(const char *str, uint32_t length,
 
4365
void Item_user_var_as_out_param::set_value(const char *str, uint length,
3893
4366
                                           const CHARSET_INFO * const cs)
3894
4367
{
3895
4368
  ::update_hash(entry, false, (void*)str, length, STRING_RESULT, cs,
4060
4533
  }
4061
4534
  
4062
4535
  pthread_mutex_lock(&LOCK_user_locks);
4063
 
  ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
 
4536
  ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4064
4537
                                       (size_t) res->length());
4065
4538
  pthread_mutex_unlock(&LOCK_user_locks);
4066
4539
  if (!ull || !ull->locked)
4079
4552
    return 0;
4080
4553
  
4081
4554
  pthread_mutex_lock(&LOCK_user_locks);
4082
 
  ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
 
4555
  ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4083
4556
                                       (size_t) res->length());
4084
4557
  pthread_mutex_unlock(&LOCK_user_locks);
4085
4558
  if (!ull || !ull->locked)