~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/item_func.cc

  • Committer: Brian Aker
  • Date: 2008-07-14 22:40:46 UTC
  • Revision ID: brian@tangent.org-20080714224046-x183907w9wp1txwv
Removed sql_manager. Ever heard of just setting up the OS to sync when you
want it to?

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