~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/item_strfunc.cc

MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
}
86
86
 
87
87
 
88
 
longlong Item_str_func::val_int()
 
88
int64_t Item_str_func::val_int()
89
89
{
90
90
  assert(fixed == 1);
91
91
  int err;
95
95
  return (res ?
96
96
          my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
97
97
                      &err) :
98
 
          (longlong) 0);
 
98
          (int64_t) 0);
99
99
}
100
100
 
101
101
 
280
280
 
281
281
void Item_func_concat::fix_length_and_dec()
282
282
{
283
 
  ulonglong max_result_length= 0;
 
283
  uint64_t max_result_length= 0;
284
284
 
285
285
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
286
286
    return;
449
449
 
450
450
void Item_func_concat_ws::fix_length_and_dec()
451
451
{
452
 
  ulonglong max_result_length;
 
452
  uint64_t max_result_length;
453
453
 
454
454
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
455
455
    return;
459
459
     it is done on parser level in sql_yacc.yy
460
460
     so, (arg_count - 2) is safe here.
461
461
  */
462
 
  max_result_length= (ulonglong) args[0]->max_length * (arg_count - 2);
 
462
  max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
463
463
  for (uint i=1 ; i < arg_count ; i++)
464
464
    max_result_length+=args[i]->max_length;
465
465
 
652
652
 
653
653
void Item_func_replace::fix_length_and_dec()
654
654
{
655
 
  ulonglong max_result_length= args[0]->max_length;
 
655
  uint64_t max_result_length= args[0]->max_length;
656
656
  int diff=(int) (args[2]->max_length - args[1]->max_length);
657
657
  if (diff > 0 && args[1]->max_length)
658
658
  {                                             // Calculate of maxreplaces
659
 
    ulonglong max_substrs= max_result_length/args[1]->max_length;
 
659
    uint64_t max_substrs= max_result_length/args[1]->max_length;
660
660
    max_result_length+= max_substrs * (uint) diff;
661
661
  }
662
662
  if (max_result_length >= MAX_BLOB_WIDTH)
675
675
{
676
676
  assert(fixed == 1);
677
677
  String *res,*res2;
678
 
  longlong start, length;  /* must be longlong to avoid truncation */
 
678
  int64_t start, length;  /* must be int64_t to avoid truncation */
679
679
 
680
680
  null_value=0;
681
681
  res=args[0]->val_str(str);
702
702
  if (length > res->length() - start)
703
703
    length= res->length() - start;
704
704
 
705
 
  if ((ulonglong) (res->length() - length + res2->length()) >
706
 
      (ulonglong) current_thd->variables.max_allowed_packet)
 
705
  if ((uint64_t) (res->length() - length + res2->length()) >
 
706
      (uint64_t) current_thd->variables.max_allowed_packet)
707
707
  {
708
708
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
709
709
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
722
722
 
723
723
void Item_func_insert::fix_length_and_dec()
724
724
{
725
 
  ulonglong max_result_length;
 
725
  uint64_t max_result_length;
726
726
 
727
727
  // Handle character set for args[0] and args[3].
728
728
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 3))
729
729
    return;
730
 
  max_result_length= ((ulonglong) args[0]->max_length+
731
 
                      (ulonglong) args[3]->max_length);
 
730
  max_result_length= ((uint64_t) args[0]->max_length+
 
731
                      (uint64_t) args[3]->max_length);
732
732
  if (max_result_length >= MAX_BLOB_WIDTH)
733
733
  {
734
734
    max_result_length= MAX_BLOB_WIDTH;
793
793
  assert(fixed == 1);
794
794
  String *res= args[0]->val_str(str);
795
795
 
796
 
  /* must be longlong to avoid truncation */
797
 
  longlong length= args[1]->val_int();
 
796
  /* must be int64_t to avoid truncation */
 
797
  int64_t length= args[1]->val_int();
798
798
  uint char_pos;
799
799
 
800
800
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
804
804
  if ((length <= 0) && (!args[1]->unsigned_flag))
805
805
    return &my_empty_string;
806
806
 
807
 
  if ((res->length() <= (ulonglong) length) ||
 
807
  if ((res->length() <= (uint64_t) length) ||
808
808
      (res->length() <= (char_pos= res->charpos((int) length))))
809
809
    return res;
810
810
 
838
838
{
839
839
  assert(fixed == 1);
840
840
  String *res= args[0]->val_str(str);
841
 
  /* must be longlong to avoid truncation */
842
 
  longlong length= args[1]->val_int();
 
841
  /* must be int64_t to avoid truncation */
 
842
  int64_t length= args[1]->val_int();
843
843
 
844
844
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
845
845
    return 0; /* purecov: inspected */
848
848
  if ((length <= 0) && (!args[1]->unsigned_flag))
849
849
    return &my_empty_string; /* purecov: inspected */
850
850
 
851
 
  if (res->length() <= (ulonglong) length)
 
851
  if (res->length() <= (uint64_t) length)
852
852
    return res; /* purecov: inspected */
853
853
 
854
854
  uint start=res->numchars();
871
871
{
872
872
  assert(fixed == 1);
873
873
  String *res  = args[0]->val_str(str);
874
 
  /* must be longlong to avoid truncation */
875
 
  longlong start= args[1]->val_int();
 
874
  /* must be int64_t to avoid truncation */
 
875
  int64_t start= args[1]->val_int();
876
876
  /* Assumes that the maximum length of a String is < INT_MAX32. */
877
877
  /* Limit so that code sees out-of-bound value properly. */
878
 
  longlong length= arg_count == 3 ? args[2]->val_int() : INT_MAX32;
879
 
  longlong tmp_length;
 
878
  int64_t length= arg_count == 3 ? args[2]->val_int() : INT_MAX32;
 
879
  int64_t tmp_length;
880
880
 
881
881
  if ((null_value=(args[0]->null_value || args[1]->null_value ||
882
882
                   (arg_count == 3 && args[2]->null_value))))
895
895
  /* if "unsigned_flag" is set, we have a *huge* positive number. */
896
896
  /* Assumes that the maximum length of a String is < INT_MAX32. */
897
897
  if ((!args[1]->unsigned_flag && (start < INT_MIN32 || start > INT_MAX32)) ||
898
 
      (args[1]->unsigned_flag && ((ulonglong) start > INT_MAX32)))
 
898
      (args[1]->unsigned_flag && ((uint64_t) start > INT_MAX32)))
899
899
    return &my_empty_string;
900
900
 
901
901
  start= ((start < 0) ? res->numchars() + start : start - 1);
907
907
  tmp_length= res->length() - start;
908
908
  length= min(length, tmp_length);
909
909
 
910
 
  if (!start && (longlong) res->length() == length)
 
910
  if (!start && (int64_t) res->length() == length)
911
911
    return res;
912
912
  tmp_value.set(*res, (uint32) start, (uint32) length);
913
913
  return &tmp_value;
1587
1587
    double nr= args[0]->val_real();
1588
1588
    if ((null_value=args[0]->null_value))
1589
1589
      return 0; /* purecov: inspected */
1590
 
    nr= my_double_round(nr, (longlong) dec, false, false);
 
1590
    nr= my_double_round(nr, (int64_t) dec, false, false);
1591
1591
    /* Here default_charset() is right as this is not an automatic conversion */
1592
1592
    str->set_real(nr, dec, default_charset());
1593
1593
    if (isnan(nr))
1662
1662
}
1663
1663
 
1664
1664
 
1665
 
longlong Item_func_elt::val_int()
 
1665
int64_t Item_func_elt::val_int()
1666
1666
{
1667
1667
  assert(fixed == 1);
1668
1668
  uint tmp;
1670
1670
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1671
1671
    return 0;
1672
1672
 
1673
 
  longlong result= args[tmp]->val_int();
 
1673
  int64_t result= args[tmp]->val_int();
1674
1674
  null_value= args[tmp]->null_value;
1675
1675
  return result;
1676
1676
}
1729
1729
String *Item_func_make_set::val_str(String *str)
1730
1730
{
1731
1731
  assert(fixed == 1);
1732
 
  ulonglong bits;
 
1732
  uint64_t bits;
1733
1733
  bool first_found=0;
1734
1734
  Item **ptr=args;
1735
1735
  String *result=&my_empty_string;
1739
1739
    return NULL;
1740
1740
 
1741
1741
  if (arg_count < 64)
1742
 
    bits &= ((ulonglong) 1 << arg_count)-1;
 
1742
    bits &= ((uint64_t) 1 << arg_count)-1;
1743
1743
 
1744
1744
  for (; bits; bits >>= 1, ptr++)
1745
1745
  {
1865
1865
  collation.set(args[0]->collation);
1866
1866
  if (args[1]->const_item())
1867
1867
  {
1868
 
    /* must be longlong to avoid truncation */
1869
 
    longlong count= args[1]->val_int();
 
1868
    /* must be int64_t to avoid truncation */
 
1869
    int64_t count= args[1]->val_int();
1870
1870
 
1871
1871
    /* Assumes that the maximum length of a String is < INT_MAX32. */
1872
1872
    /* Set here so that rest of code sees out-of-bound value as such. */
1873
1873
    if (count > INT_MAX32)
1874
1874
      count= INT_MAX32;
1875
1875
 
1876
 
    ulonglong max_result_length= (ulonglong) args[0]->max_length * count;
 
1876
    uint64_t max_result_length= (uint64_t) args[0]->max_length * count;
1877
1877
    if (max_result_length >= MAX_BLOB_WIDTH)
1878
1878
    {
1879
1879
      max_result_length= MAX_BLOB_WIDTH;
1898
1898
  assert(fixed == 1);
1899
1899
  uint length,tot_length;
1900
1900
  char *to;
1901
 
  /* must be longlong to avoid truncation */
1902
 
  longlong count= args[1]->val_int();
 
1901
  /* must be int64_t to avoid truncation */
 
1902
  int64_t count= args[1]->val_int();
1903
1903
  String *res= args[0]->val_str(str);
1904
1904
 
1905
1905
  if (args[0]->null_value || args[1]->null_value)
1911
1911
 
1912
1912
  /* Assumes that the maximum length of a String is < INT_MAX32. */
1913
1913
  /* Bounds check on count:  If this is triggered, we will error. */
1914
 
  if ((ulonglong) count > INT_MAX32)
 
1914
  if ((uint64_t) count > INT_MAX32)
1915
1915
    count= INT_MAX32;
1916
1916
  if (count == 1)                       // To avoid reallocs
1917
1917
    return res;
1950
1950
    return;
1951
1951
  if (args[1]->const_item())
1952
1952
  {
1953
 
    ulonglong length= 0;
 
1953
    uint64_t length= 0;
1954
1954
 
1955
1955
    if (collation.collation->mbmaxlen > 0)
1956
1956
    {
1957
 
      ulonglong temp= (ulonglong) args[1]->val_int();
 
1957
      uint64_t temp= (uint64_t) args[1]->val_int();
1958
1958
 
1959
1959
      /* Assumes that the maximum length of a String is < INT_MAX32. */
1960
1960
      /* Set here so that rest of code sees out-of-bound value as such. */
1985
1985
  uint32 res_byte_length,res_char_length,pad_char_length,pad_byte_length;
1986
1986
  char *to;
1987
1987
  const char *ptr_pad;
1988
 
  /* must be longlong to avoid truncation */
1989
 
  longlong count= args[1]->val_int();
1990
 
  longlong byte_count;
 
1988
  /* must be int64_t to avoid truncation */
 
1989
  int64_t count= args[1]->val_int();
 
1990
  int64_t byte_count;
1991
1991
  String *res= args[0]->val_str(str);
1992
1992
  String *rpad= args[2]->val_str(&rpad_str);
1993
1993
 
1997
1997
  null_value=0;
1998
1998
  /* Assumes that the maximum length of a String is < INT_MAX32. */
1999
1999
  /* Set here so that rest of code sees out-of-bound value as such. */
2000
 
  if ((ulonglong) count > INT_MAX32)
 
2000
  if ((uint64_t) count > INT_MAX32)
2001
2001
    count= INT_MAX32;
2002
2002
  if (count <= (res_char_length= res->numchars()))
2003
2003
  {                                             // String to pad is big enough
2007
2007
  pad_char_length= rpad->numchars();
2008
2008
 
2009
2009
  byte_count= count * collation.collation->mbmaxlen;
2010
 
  if ((ulonglong) byte_count > current_thd->variables.max_allowed_packet)
 
2010
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
2011
2011
  {
2012
2012
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2013
2013
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2053
2053
  
2054
2054
  if (args[1]->const_item())
2055
2055
  {
2056
 
    ulonglong length= 0;
 
2056
    uint64_t length= 0;
2057
2057
 
2058
2058
    if (collation.collation->mbmaxlen > 0)
2059
2059
    {
2060
 
      ulonglong temp= (ulonglong) args[1]->val_int();
 
2060
      uint64_t temp= (uint64_t) args[1]->val_int();
2061
2061
 
2062
2062
      /* Assumes that the maximum length of a String is < INT_MAX32. */
2063
2063
      /* Set here so that rest of code sees out-of-bound value as such. */
2086
2086
{
2087
2087
  assert(fixed == 1);
2088
2088
  uint32 res_char_length,pad_char_length;
2089
 
  /* must be longlong to avoid truncation */
2090
 
  longlong count= args[1]->val_int();
2091
 
  longlong byte_count;
 
2089
  /* must be int64_t to avoid truncation */
 
2090
  int64_t count= args[1]->val_int();
 
2091
  int64_t byte_count;
2092
2092
  String *res= args[0]->val_str(&tmp_value);
2093
2093
  String *pad= args[2]->val_str(&lpad_str);
2094
2094
 
2098
2098
  null_value=0;
2099
2099
  /* Assumes that the maximum length of a String is < INT_MAX32. */
2100
2100
  /* Set here so that rest of code sees out-of-bound value as such. */
2101
 
  if ((ulonglong) count > INT_MAX32)
 
2101
  if ((uint64_t) count > INT_MAX32)
2102
2102
    count= INT_MAX32;
2103
2103
 
2104
2104
  res_char_length= res->numchars();
2112
2112
  pad_char_length= pad->numchars();
2113
2113
  byte_count= count * collation.collation->mbmaxlen;
2114
2114
  
2115
 
  if ((ulonglong) byte_count > current_thd->variables.max_allowed_packet)
 
2115
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
2116
2116
  {
2117
2117
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2118
2118
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2151
2151
  assert(fixed == 1);
2152
2152
  String *res= args[0]->val_str(str);
2153
2153
  char *endptr,ans[65],*ptr;
2154
 
  longlong dec;
 
2154
  int64_t dec;
2155
2155
  int from_base= (int) args[1]->val_int();
2156
2156
  int to_base= (int) args[2]->val_int();
2157
2157
  int err;
2170
2170
    dec= my_strntoll(res->charset(), res->ptr(), res->length(),
2171
2171
                     -from_base, &endptr, &err);
2172
2172
  else
2173
 
    dec= (longlong) my_strntoull(res->charset(), res->ptr(), res->length(),
 
2173
    dec= (int64_t) my_strntoull(res->charset(), res->ptr(), res->length(),
2174
2174
                                 from_base, &endptr, &err);
2175
2175
 
2176
 
  ptr= longlong2str(dec, ans, to_base);
 
2176
  ptr= int64_t2str(dec, ans, to_base);
2177
2177
  if (str->copy(ans, (uint32) (ptr-ans), default_charset()))
2178
2178
    return &my_empty_string;
2179
2179
  return str;
2359
2359
  assert(fixed == 1);
2360
2360
  if (args[0]->result_type() != STRING_RESULT)
2361
2361
  {
2362
 
    ulonglong dec;
 
2362
    uint64_t dec;
2363
2363
    char ans[65],*ptr;
2364
 
    /* Return hex of unsigned longlong value */
 
2364
    /* Return hex of unsigned int64_t value */
2365
2365
    if (args[0]->result_type() == REAL_RESULT ||
2366
2366
        args[0]->result_type() == DECIMAL_RESULT)
2367
2367
    {
2368
2368
      double val= args[0]->val_real();
2369
2369
      if ((val <= (double) LONGLONG_MIN) || 
2370
 
          (val >= (double) (ulonglong) ULONGLONG_MAX))
2371
 
        dec=  ~(longlong) 0;
 
2370
          (val >= (double) (uint64_t) ULONGLONG_MAX))
 
2371
        dec=  ~(int64_t) 0;
2372
2372
      else
2373
 
        dec= (ulonglong) (val + (val > 0 ? 0.5 : -0.5));
 
2373
        dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
2374
2374
    }
2375
2375
    else
2376
 
      dec= (ulonglong) args[0]->val_int();
 
2376
      dec= (uint64_t) args[0]->val_int();
2377
2377
 
2378
2378
    if ((null_value= args[0]->null_value))
2379
2379
      return 0;
2380
 
    ptr= longlong2str(dec,ans,16);
 
2380
    ptr= int64_t2str(dec,ans,16);
2381
2381
    if (str->copy(ans,(uint32) (ptr-ans),default_charset()))
2382
2382
      return &my_empty_string;                  // End of memory
2383
2383
    return str;
2507
2507
String* Item_func_export_set::val_str(String* str)
2508
2508
{
2509
2509
  assert(fixed == 1);
2510
 
  ulonglong the_set = (ulonglong) args[0]->val_int();
 
2510
  uint64_t the_set = (uint64_t) args[0]->val_int();
2511
2511
  String yes_buf, *yes;
2512
2512
  yes = args[1]->val_str(&yes_buf);
2513
2513
  String no_buf, *no;
2515
2515
  String *sep = NULL, sep_buf ;
2516
2516
 
2517
2517
  uint num_set_values = 64;
2518
 
  ulonglong mask = 0x1;
 
2518
  uint64_t mask = 0x1;
2519
2519
  str->length(0);
2520
2520
  str->set_charset(collation.collation);
2521
2521
 
2682
2682
  return 0;
2683
2683
}
2684
2684
 
2685
 
longlong Item_func_uncompressed_length::val_int()
 
2685
int64_t Item_func_uncompressed_length::val_int()
2686
2686
{
2687
2687
  assert(fixed == 1);
2688
2688
  String *res= args[0]->val_str(&value);
2704
2704
  return uint4korr(res->ptr()) & 0x3FFFFFFF;
2705
2705
}
2706
2706
 
2707
 
longlong Item_func_crc32::val_int()
 
2707
int64_t Item_func_crc32::val_int()
2708
2708
{
2709
2709
  assert(fixed == 1);
2710
2710
  String *res=args[0]->val_str(&value);
2714
2714
    return 0; /* purecov: inspected */
2715
2715
  }
2716
2716
  null_value=0;
2717
 
  return (longlong) crc32(0L, (uchar*)res->ptr(), res->length());
 
2717
  return (int64_t) crc32(0L, (uchar*)res->ptr(), res->length());
2718
2718
}
2719
2719
 
2720
2720
#ifdef HAVE_COMPRESS
2847
2847
 
2848
2848
static struct rand_struct uuid_rand;
2849
2849
static uint nanoseq;
2850
 
static ulonglong uuid_time=0;
 
2850
static uint64_t uuid_time=0;
2851
2851
static char clock_seq_and_node_str[]="-0000-000000000000";
2852
2852
 
2853
2853
/**
2854
2854
  number of 100-nanosecond intervals between
2855
2855
  1582-10-15 00:00:00.00 and 1970-01-01 00:00:00.00.
2856
2856
*/
2857
 
#define UUID_TIME_OFFSET ((ulonglong) 141427 * 24 * 60 * 60 * 1000 * 10 )
 
2857
#define UUID_TIME_OFFSET ((uint64_t) 141427 * 24 * 60 * 60 * 1000 * 10 )
2858
2858
 
2859
2859
#define UUID_VERSION      0x1000
2860
2860
#define UUID_VARIANT      0x8000
2913
2913
    set_clock_seq_str();
2914
2914
  }
2915
2915
 
2916
 
  ulonglong tv=my_getsystime() + UUID_TIME_OFFSET + nanoseq;
 
2916
  uint64_t tv=my_getsystime() + UUID_TIME_OFFSET + nanoseq;
2917
2917
  if (unlikely(tv < uuid_time))
2918
2918
    set_clock_seq_str();
2919
2919
  else if (unlikely(tv == uuid_time))