~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_cmpfunc.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
    return INT_RESULT;
43
43
}
44
44
 
45
 
static void agg_result_type(Item_result *type, Item **items, uint nitems)
 
45
static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
46
46
{
47
47
  Item **item, **item_end;
48
48
  bool unsigned_flag= 0;
90
90
 
91
91
static int cmp_row_type(Item* item1, Item* item2)
92
92
{
93
 
  uint n= item1->cols();
 
93
  uint32_t n= item1->cols();
94
94
  if (item2->check_cols(n))
95
95
    return 1;
96
 
  for (uint i=0; i<n; i++)
 
96
  for (uint32_t i=0; i<n; i++)
97
97
  {
98
98
    if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
99
99
        (item1->element_index(i)->result_type() == ROW_RESULT &&
127
127
    0  otherwise
128
128
*/
129
129
 
130
 
static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
 
130
static int agg_cmp_type(Item_result *type, Item **items, uint32_t nitems)
131
131
{
132
 
  uint i;
 
132
  uint32_t i;
133
133
  type[0]= items[0]->result_type();
134
134
  for (i= 1 ; i < nitems ; i++)
135
135
  {
166
166
  @return aggregated field type.
167
167
*/
168
168
 
169
 
enum_field_types agg_field_type(Item **items, uint nitems)
 
169
enum_field_types agg_field_type(Item **items, uint32_t nitems)
170
170
{
171
 
  uint i;
 
171
  uint32_t i;
172
172
  if (!nitems || items[0]->result_type() == ROW_RESULT )
173
173
    return (enum_field_types)-1;
174
174
  enum_field_types res= items[0]->field_type();
194
194
    Bitmap of collected types - otherwise
195
195
*/
196
196
 
197
 
static uint collect_cmp_types(Item **items, uint nitems)
 
197
static uint32_t collect_cmp_types(Item **items, uint32_t nitems)
198
198
{
199
 
  uint i;
200
 
  uint found_types;
 
199
  uint32_t i;
 
200
  uint32_t found_types;
201
201
  Item_result left_result= items[0]->result_type();
202
202
  assert(nitems > 1);
203
203
  found_types= 0;
510
510
  switch (type) {
511
511
  case ROW_RESULT:
512
512
  {
513
 
    uint n= (*a)->cols();
 
513
    uint32_t n= (*a)->cols();
514
514
    if (n != (*b)->cols())
515
515
    {
516
516
      my_error(ER_OPERAND_COLUMNS, MYF(0), n);
519
519
    }
520
520
    if (!(comparators= new Arg_comparator[n]))
521
521
      return 1;
522
 
    for (uint i=0; i < n; i++)
 
522
    for (uint32_t i=0; i < n; i++)
523
523
    {
524
524
      if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
525
525
      {
1079
1079
    if ((res2= (*b)->val_str(&owner->tmp_value2)))
1080
1080
    {
1081
1081
      owner->null_value= 0;
1082
 
      uint res1_length= res1->length();
1083
 
      uint res2_length= res2->length();
 
1082
      uint32_t res1_length= res1->length();
 
1083
      uint32_t res2_length= res2->length();
1084
1084
      int cmp= memcmp(res1->ptr(), res2->ptr(), cmin(res1_length,res2_length));
1085
1085
      return cmp ? cmp : (int) (res1_length - res2_length);
1086
1086
    }
1337
1337
  bool was_null= 0;
1338
1338
  (*a)->bring_value();
1339
1339
  (*b)->bring_value();
1340
 
  uint n= (*a)->cols();
1341
 
  for (uint i= 0; i<n; i++)
 
1340
  uint32_t n= (*a)->cols();
 
1341
  for (uint32_t i= 0; i<n; i++)
1342
1342
  {
1343
1343
    res= comparators[i].compare();
1344
1344
    if (owner->null_value)
1380
1380
{
1381
1381
  (*a)->bring_value();
1382
1382
  (*b)->bring_value();
1383
 
  uint n= (*a)->cols();
1384
 
  for (uint i= 0; i<n; i++)
 
1383
  uint32_t n= (*a)->cols();
 
1384
  for (uint32_t i= 0; i<n; i++)
1385
1385
  {
1386
1386
    if (!comparators[i].compare())
1387
1387
      return 0;
1459
1459
  }
1460
1460
  else
1461
1461
  {
1462
 
    uint n= cache->cols();
1463
 
    for (uint i= 0; i < n; i++)
 
1462
    uint32_t n= cache->cols();
 
1463
    for (uint32_t i= 0; i < n; i++)
1464
1464
    {
1465
1465
      if (args[0]->element_index(i)->used_tables())
1466
1466
        ((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1547
1547
        }
1548
1548
        else
1549
1549
        {
1550
 
          uint i;
1551
 
          uint ncols= cache->cols();
 
1550
          uint32_t i;
 
1551
          uint32_t ncols= cache->cols();
1552
1552
          /*
1553
1553
            Turn off the predicates that are based on column compares for
1554
1554
            which the left part is currently NULL
1753
1753
    return 0;
1754
1754
  if (negated != ((Item_func_opt_neg *) item_func)->negated)
1755
1755
    return 0;
1756
 
  for (uint i=0; i < arg_count ; i++)
 
1756
  for (uint32_t i=0; i < arg_count ; i++)
1757
1757
    if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
1758
1758
      return 0;
1759
1759
  return 1;
1762
1762
 
1763
1763
void Item_func_interval::fix_length_and_dec()
1764
1764
{
1765
 
  uint rows= row->cols();
 
1765
  uint32_t rows= row->cols();
1766
1766
  
1767
1767
  use_decimal_comparison= ((row->element_index(0)->result_type() ==
1768
1768
                            DECIMAL_RESULT) ||
1772
1772
  {
1773
1773
    bool not_null_consts= true;
1774
1774
 
1775
 
    for (uint i= 1; not_null_consts && i < rows; i++)
 
1775
    for (uint32_t i= 1; not_null_consts && i < rows; i++)
1776
1776
    {
1777
1777
      Item *el= row->element_index(i);
1778
1778
      not_null_consts&= el->const_item() & !el->is_null();
1784
1784
    {
1785
1785
      if (use_decimal_comparison)
1786
1786
      {
1787
 
        for (uint i= 1; i < rows; i++)
 
1787
        for (uint32_t i= 1; i < rows; i++)
1788
1788
        {
1789
1789
          Item *el= row->element_index(i);
1790
1790
          interval_range *range= intervals + (i-1);
1809
1809
      }
1810
1810
      else
1811
1811
      {
1812
 
        for (uint i= 1; i < rows; i++)
 
1812
        for (uint32_t i= 1; i < rows; i++)
1813
1813
        {
1814
1814
          intervals[i-1].dbl= row->element_index(i)->val_real();
1815
1815
        }
1844
1844
  assert(fixed == 1);
1845
1845
  double value;
1846
1846
  my_decimal dec_buf, *dec= NULL;
1847
 
  uint i;
 
1847
  uint32_t i;
1848
1848
 
1849
1849
  if (use_decimal_comparison)
1850
1850
  {
1862
1862
 
1863
1863
  if (intervals)
1864
1864
  {                                     // Use binary search to find interval
1865
 
    uint start,end;
 
1865
    uint32_t start,end;
1866
1866
    start= 0;
1867
1867
    end=   row->cols()-2;
1868
1868
    while (start != end)
1869
1869
    {
1870
 
      uint mid= (start + end + 1) / 2;
 
1870
      uint32_t mid= (start + end + 1) / 2;
1871
1871
      interval_range *range= intervals + mid;
1872
1872
      bool cmp_result;
1873
1873
      /*
2200
2200
}
2201
2201
 
2202
2202
 
2203
 
uint Item_func_ifnull::decimal_precision() const
 
2203
uint32_t Item_func_ifnull::decimal_precision() const
2204
2204
{
2205
2205
  int max_int_part=cmax(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2206
2206
  return cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2382
2382
}
2383
2383
 
2384
2384
 
2385
 
uint Item_func_if::decimal_precision() const
 
2385
uint32_t Item_func_if::decimal_precision() const
2386
2386
{
2387
2387
  int precision=(cmax(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2388
2388
                 decimals);
2554
2554
 
2555
2555
Item *Item_func_case::find_item(String *str __attribute__((unused)))
2556
2556
{
2557
 
  uint value_added_map= 0;
 
2557
  uint32_t value_added_map= 0;
2558
2558
 
2559
2559
  if (first_expr_num == -1)
2560
2560
  {
2561
 
    for (uint i=0 ; i < ncases ; i+=2)
 
2561
    for (uint32_t i=0 ; i < ncases ; i+=2)
2562
2562
    {
2563
2563
      // No expression between CASE and the first WHEN
2564
2564
      if (args[i]->val_bool())
2569
2569
  else
2570
2570
  {
2571
2571
    /* Compare every WHEN argument with it and return the first match */
2572
 
    for (uint i=0 ; i < ncases ; i+=2)
 
2572
    for (uint32_t i=0 ; i < ncases ; i+=2)
2573
2573
    {
2574
2574
      cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2575
2575
      assert(cmp_type != ROW_RESULT);
2693
2693
 
2694
2694
void Item_func_case::agg_num_lengths(Item *arg)
2695
2695
{
2696
 
  uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2696
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2697
2697
                                           arg->unsigned_flag) - arg->decimals;
2698
2698
  set_if_bigger(max_length, len); 
2699
2699
  set_if_bigger(decimals, arg->decimals);
2704
2704
void Item_func_case::fix_length_and_dec()
2705
2705
{
2706
2706
  Item **agg;
2707
 
  uint nagg;
2708
 
  uint found_types= 0;
 
2707
  uint32_t nagg;
 
2708
  uint32_t found_types= 0;
2709
2709
  if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2710
2710
    return;
2711
2711
  
2732
2732
  */
2733
2733
  if (first_expr_num != -1)
2734
2734
  {
2735
 
    uint i;
 
2735
    uint32_t i;
2736
2736
    agg[0]= args[first_expr_num];
2737
2737
    left_result_type= agg[0]->result_type();
2738
2738
 
2766
2766
  unsigned_flag= true;
2767
2767
  if (cached_result_type == STRING_RESULT)
2768
2768
  {
2769
 
    for (uint i= 0; i < ncases; i+= 2)
 
2769
    for (uint32_t i= 0; i < ncases; i+= 2)
2770
2770
      agg_str_lengths(args[i + 1]);
2771
2771
    if (else_expr_num != -1)
2772
2772
      agg_str_lengths(args[else_expr_num]);
2773
2773
  }
2774
2774
  else
2775
2775
  {
2776
 
    for (uint i= 0; i < ncases; i+= 2)
 
2776
    for (uint32_t i= 0; i < ncases; i+= 2)
2777
2777
      agg_num_lengths(args[i + 1]);
2778
2778
    if (else_expr_num != -1) 
2779
2779
      agg_num_lengths(args[else_expr_num]);
2783
2783
}
2784
2784
 
2785
2785
 
2786
 
uint Item_func_case::decimal_precision() const
 
2786
uint32_t Item_func_case::decimal_precision() const
2787
2787
{
2788
2788
  int max_int_part=0;
2789
 
  for (uint i=0 ; i < ncases ; i+=2)
 
2789
  for (uint32_t i=0 ; i < ncases ; i+=2)
2790
2790
    set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2791
2791
 
2792
2792
  if (else_expr_num != -1) 
2808
2808
    args[first_expr_num]->print(str, query_type);
2809
2809
    str->append(' ');
2810
2810
  }
2811
 
  for (uint i=0 ; i < ncases ; i+=2)
 
2811
  for (uint32_t i=0 ; i < ncases ; i+=2)
2812
2812
  {
2813
2813
    str->append(STRING_WITH_LEN("when "));
2814
2814
    args[i]->print(str, query_type);
2828
2828
 
2829
2829
void Item_func_case::cleanup()
2830
2830
{
2831
 
  uint i;
 
2831
  uint32_t i;
2832
2832
  Item_func::cleanup();
2833
2833
  for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2834
2834
  {
2847
2847
{
2848
2848
  assert(fixed == 1);
2849
2849
  null_value=0;
2850
 
  for (uint i=0 ; i < arg_count ; i++)
 
2850
  for (uint32_t i=0 ; i < arg_count ; i++)
2851
2851
  {
2852
2852
    String *res;
2853
2853
    if ((res=args[i]->val_str(str)))
2861
2861
{
2862
2862
  assert(fixed == 1);
2863
2863
  null_value=0;
2864
 
  for (uint i=0 ; i < arg_count ; i++)
 
2864
  for (uint32_t i=0 ; i < arg_count ; i++)
2865
2865
  {
2866
2866
    int64_t res=args[i]->val_int();
2867
2867
    if (!args[i]->null_value)
2875
2875
{
2876
2876
  assert(fixed == 1);
2877
2877
  null_value=0;
2878
 
  for (uint i=0 ; i < arg_count ; i++)
 
2878
  for (uint32_t i=0 ; i < arg_count ; i++)
2879
2879
  {
2880
2880
    double res= args[i]->val_real();
2881
2881
    if (!args[i]->null_value)
2890
2890
{
2891
2891
  assert(fixed == 1);
2892
2892
  null_value= 0;
2893
 
  for (uint i= 0; i < arg_count; i++)
 
2893
  for (uint32_t i= 0; i < arg_count; i++)
2894
2894
  {
2895
2895
    my_decimal *res= args[i]->val_decimal(decimal_value);
2896
2896
    if (!args[i]->null_value)
3056
3056
  if (!result || !used_count)
3057
3057
    return 0;                           // Null value
3058
3058
 
3059
 
  uint start,end;
 
3059
  uint32_t start,end;
3060
3060
  start=0; end=used_count-1;
3061
3061
  while (start != end)
3062
3062
  {
3063
 
    uint mid=(start+end+1)/2;
 
3063
    uint32_t mid=(start+end+1)/2;
3064
3064
    int res;
3065
3065
    if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3066
3066
      return 1;
3072
3072
  return (int) ((*compare)(collation, base+start*size, result) == 0);
3073
3073
}
3074
3074
 
3075
 
in_string::in_string(uint elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
 
3075
in_string::in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
3076
3076
  :in_vector(elements, sizeof(String), cmp_func, cs),
3077
3077
   tmp(buff, sizeof(buff), &my_charset_bin)
3078
3078
{}
3082
3082
  if (base)
3083
3083
  {
3084
3084
    // base was allocated with help of sql_alloc => following is OK
3085
 
    for (uint i=0 ; i < count ; i++)
 
3085
    for (uint32_t i=0 ; i < count ; i++)
3086
3086
      ((String*) base)[i].free();
3087
3087
  }
3088
3088
}
3089
3089
 
3090
 
void in_string::set(uint pos,Item *item)
 
3090
void in_string::set(uint32_t pos,Item *item)
3091
3091
{
3092
3092
  String *str=((String*) base)+pos;
3093
3093
  String *res=item->val_str(str);
3115
3115
  return (unsigned char*) item->val_str(&tmp);
3116
3116
}
3117
3117
 
3118
 
in_row::in_row(uint elements, Item * item __attribute__((unused)))
 
3118
in_row::in_row(uint32_t elements, Item * item __attribute__((unused)))
3119
3119
{
3120
3120
  base= (char*) new cmp_item_row[count= elements];
3121
3121
  size= sizeof(cmp_item_row);
3142
3142
  return (unsigned char *)&tmp;
3143
3143
}
3144
3144
 
3145
 
void in_row::set(uint pos, Item *item)
 
3145
void in_row::set(uint32_t pos, Item *item)
3146
3146
{
3147
3147
  ((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3148
3148
  return;
3149
3149
}
3150
3150
 
3151
 
in_int64_t::in_int64_t(uint elements)
 
3151
in_int64_t::in_int64_t(uint32_t elements)
3152
3152
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
3153
3153
{}
3154
3154
 
3155
 
void in_int64_t::set(uint pos,Item *item)
 
3155
void in_int64_t::set(uint32_t pos,Item *item)
3156
3156
{
3157
3157
  struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3158
3158
  
3169
3169
  return (unsigned char*) &tmp;
3170
3170
}
3171
3171
 
3172
 
void in_datetime::set(uint pos,Item *item)
 
3172
void in_datetime::set(uint32_t pos,Item *item)
3173
3173
{
3174
3174
  Item **tmp_item= &item;
3175
3175
  bool is_null;
3190
3190
  return (unsigned char*) &tmp;
3191
3191
}
3192
3192
 
3193
 
in_double::in_double(uint elements)
 
3193
in_double::in_double(uint32_t elements)
3194
3194
  :in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3195
3195
{}
3196
3196
 
3197
 
void in_double::set(uint pos,Item *item)
 
3197
void in_double::set(uint32_t pos,Item *item)
3198
3198
{
3199
3199
  ((double*) base)[pos]= item->val_real();
3200
3200
}
3208
3208
}
3209
3209
 
3210
3210
 
3211
 
in_decimal::in_decimal(uint elements)
 
3211
in_decimal::in_decimal(uint32_t elements)
3212
3212
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3213
3213
{}
3214
3214
 
3215
3215
 
3216
 
void in_decimal::set(uint pos, Item *item)
 
3216
void in_decimal::set(uint32_t pos, Item *item)
3217
3217
{
3218
3218
  /* as far as 'item' is constant, we can store reference on my_decimal */
3219
3219
  my_decimal *dec= ((my_decimal *)base) + pos;
3282
3282
{
3283
3283
  if (comparators)
3284
3284
  {
3285
 
    for (uint i= 0; i < n; i++)
 
3285
    for (uint32_t i= 0; i < n; i++)
3286
3286
    {
3287
3287
      if (comparators[i])
3288
3288
        delete comparators[i];
3307
3307
  {
3308
3308
    item->bring_value();
3309
3309
    item->null_value= 0;
3310
 
    for (uint i=0; i < n; i++)
 
3310
    for (uint32_t i=0; i < n; i++)
3311
3311
    {
3312
3312
      if (!comparators[i])
3313
3313
        if (!(comparators[i]=
3335
3335
  {
3336
3336
    item->bring_value();
3337
3337
    item->null_value= 0;
3338
 
    for (uint i=0; i < n; i++)
 
3338
    for (uint32_t i=0; i < n; i++)
3339
3339
    {
3340
3340
      if (!(comparators[i]= tmpl->comparators[i]->make_same()))
3341
3341
        break;                                  // new failed
3357
3357
  }
3358
3358
  bool was_null= 0;
3359
3359
  arg->bring_value();
3360
 
  for (uint i=0; i < n; i++)
 
3360
  for (uint32_t i=0; i < n; i++)
3361
3361
  {
3362
3362
    if (comparators[i]->cmp(arg->element_index(i)))
3363
3363
    {
3373
3373
int cmp_item_row::compare(cmp_item *c)
3374
3374
{
3375
3375
  cmp_item_row *l_cmp= (cmp_item_row *) c;
3376
 
  for (uint i=0; i < n; i++)
 
3376
  for (uint32_t i=0; i < n; i++)
3377
3377
  {
3378
3378
    int res;
3379
3379
    if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3522
3522
  /* true <=> arguments values will be compared as DATETIMEs. */
3523
3523
  bool compare_as_datetime= false;
3524
3524
  Item *date_arg= 0;
3525
 
  uint found_types= 0;
3526
 
  uint type_cnt= 0, i;
 
3525
  uint32_t found_types= 0;
 
3526
  uint32_t type_cnt= 0, i;
3527
3527
  Item_result cmp_type= STRING_RESULT;
3528
3528
  left_result_type= args[0]->result_type();
3529
3529
  if (!(found_types= collect_cmp_types(args, arg_count)))
3579
3579
    /* All DATE/DATETIME fields/functions has the STRING result type. */
3580
3580
    if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3581
3581
    {
3582
 
      uint col, cols= args[0]->cols();
 
3582
      uint32_t col, cols= args[0]->cols();
3583
3583
 
3584
3584
      for (col= 0; col < cols; col++)
3585
3585
      {
3700
3700
    }
3701
3701
    if (array && !(thd->is_fatal_error))                // If not EOM
3702
3702
    {
3703
 
      uint j=0;
3704
 
      for (uint i=1 ; i < arg_count ; i++)
 
3703
      uint32_t j=0;
 
3704
      for (uint32_t i=1 ; i < arg_count ; i++)
3705
3705
      {
3706
3706
        array->set(j,args[i]);
3707
3707
        if (!args[i]->null_value)                       // Skip NULL values
3780
3780
{
3781
3781
  cmp_item *in_item;
3782
3782
  assert(fixed == 1);
3783
 
  uint value_added_map= 0;
 
3783
  uint32_t value_added_map= 0;
3784
3784
  if (array)
3785
3785
  {
3786
3786
    int tmp=array->find(args[0]);
3788
3788
    return (int64_t) (!null_value && tmp != negated);
3789
3789
  }
3790
3790
 
3791
 
  for (uint i= 1 ; i < arg_count ; i++)
 
3791
  for (uint32_t i= 1 ; i < arg_count ; i++)
3792
3792
  {
3793
3793
    Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3794
3794
    in_item= cmp_items[(uint)cmp_type];
4447
4447
                                         escape_str->charset(), cs, &unused))
4448
4448
        {
4449
4449
          char ch;
4450
 
          uint errors;
 
4450
          uint32_t errors;
4451
4451
          uint32_t cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
4452
4452
                                          escape_str->length(),
4453
4453
                                          escape_str->charset(), &errors);
4959
4959
  fields.push_back(f);
4960
4960
}
4961
4961
 
4962
 
uint Item_equal::members()
 
4962
uint32_t Item_equal::members()
4963
4963
{
4964
4964
  return fields.elements;
4965
4965
}