~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.cc

merged with up to date trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
463
463
      the call to save_in_field below overrides that value.
464
464
    */
465
465
    if (field_item->depended_from)
 
466
    {
466
467
      orig_field_val= field->val_int();
 
468
    }
 
469
 
467
470
    if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
468
471
    {
469
472
      Item *tmp= new Item_int_with_ref(field->val_int(), *item,
472
475
        session->change_item_tree(item, tmp);
473
476
      result= 1;                                        // Item was replaced
474
477
    }
 
478
 
475
479
    /* Restore the original field value. */
476
480
    if (field_item->depended_from)
477
481
    {
478
 
      result= field->store(orig_field_val, true);
 
482
      result= field->store(orig_field_val, field->isUnsigned());
479
483
      /* orig_field_val must be a valid value that can be restored back. */
480
484
      assert(!result);
481
485
    }
570
574
{
571
575
  owner= item;
572
576
  func= comparator_matrix[type]
573
 
                         [test(owner->functype() == Item_func::EQUAL_FUNC)];
 
577
    [test(owner->functype() == Item_func::EQUAL_FUNC)];
 
578
 
574
579
  switch (type) {
575
580
  case ROW_RESULT:
576
 
  {
577
 
    uint32_t n= (*a)->cols();
578
 
    if (n != (*b)->cols())
579
 
    {
580
 
      my_error(ER_OPERAND_COLUMNS, MYF(0), n);
581
 
      comparators= 0;
582
 
      return 1;
583
 
    }
584
 
    if (!(comparators= new Arg_comparator[n]))
585
 
      return 1;
586
 
    for (uint32_t i=0; i < n; i++)
587
 
    {
588
 
      if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
589
 
      {
590
 
        my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
591
 
        return 1;
592
 
      }
593
 
      comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
594
 
    }
595
 
    break;
596
 
  }
 
581
    {
 
582
      uint32_t n= (*a)->cols();
 
583
      if (n != (*b)->cols())
 
584
      {
 
585
        my_error(ER_OPERAND_COLUMNS, MYF(0), n);
 
586
        comparators= 0;
 
587
        return 1;
 
588
      }
 
589
      if (!(comparators= new Arg_comparator[n]))
 
590
        return 1;
 
591
      for (uint32_t i=0; i < n; i++)
 
592
      {
 
593
        if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
 
594
        {
 
595
          my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
 
596
          return 1;
 
597
        }
 
598
        comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
 
599
      }
 
600
      break;
 
601
    }
 
602
 
597
603
  case STRING_RESULT:
598
 
  {
599
 
    /*
600
 
      We must set cmp_charset here as we may be called from for an automatic
601
 
      generated item, like in natural join
602
 
    */
603
 
    if (cmp_collation.set((*a)->collation, (*b)->collation) ||
604
 
        cmp_collation.derivation == DERIVATION_NONE)
605
 
    {
606
 
      my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
607
 
      return 1;
608
 
    }
609
 
    if (cmp_collation.collation == &my_charset_bin)
610
604
    {
611
605
      /*
612
 
        We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
613
 
        without removing end space
 
606
        We must set cmp_charset here as we may be called from for an automatic
 
607
        generated item, like in natural join
614
608
      */
615
 
      if (func == &Arg_comparator::compare_string)
616
 
        func= &Arg_comparator::compare_binary_string;
617
 
      else if (func == &Arg_comparator::compare_e_string)
618
 
        func= &Arg_comparator::compare_e_binary_string;
 
609
      if (cmp_collation.set((*a)->collation, (*b)->collation) ||
 
610
          cmp_collation.derivation == DERIVATION_NONE)
 
611
      {
 
612
        my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
 
613
        return 1;
 
614
      }
 
615
      if (cmp_collation.collation == &my_charset_bin)
 
616
      {
 
617
        /*
 
618
          We are using BLOB/BINARY/VARBINARY, change to compare byte by byte,
 
619
          without removing end space
 
620
        */
 
621
        if (func == &Arg_comparator::compare_string)
 
622
          func= &Arg_comparator::compare_binary_string;
 
623
        else if (func == &Arg_comparator::compare_e_string)
 
624
          func= &Arg_comparator::compare_e_binary_string;
619
625
 
620
 
      /*
621
 
        As this is binary compassion, mark all fields that they can't be
622
 
        transformed. Otherwise we would get into trouble with comparisons
623
 
        like:
624
 
        WHERE col= 'j' AND col LIKE BINARY 'j'
625
 
        which would be transformed to:
626
 
        WHERE col= 'j'
 
626
        /*
 
627
          As this is binary compassion, mark all fields that they can't be
 
628
          transformed. Otherwise we would get into trouble with comparisons
 
629
like:
 
630
WHERE col= 'j' AND col LIKE BINARY 'j'
 
631
which would be transformed to:
 
632
WHERE col= 'j'
627
633
      */
628
 
      (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
629
 
      (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
634
        (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
635
        (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
636
      }
 
637
      break;
630
638
    }
631
 
    break;
632
 
  }
633
639
  case INT_RESULT:
634
 
  {
635
 
    if (func == &Arg_comparator::compare_int_signed)
636
 
    {
637
 
      if ((*a)->unsigned_flag)
638
 
        func= (((*b)->unsigned_flag)?
639
 
               &Arg_comparator::compare_int_unsigned :
640
 
               &Arg_comparator::compare_int_unsigned_signed);
641
 
      else if ((*b)->unsigned_flag)
642
 
        func= &Arg_comparator::compare_int_signed_unsigned;
643
 
    }
644
 
    else if (func== &Arg_comparator::compare_e_int)
645
 
    {
646
 
      if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
647
 
        func= &Arg_comparator::compare_e_int_diff_signedness;
648
 
    }
649
 
    break;
650
 
  }
 
640
    {
 
641
      if (func == &Arg_comparator::compare_int_signed)
 
642
      {
 
643
        if ((*a)->unsigned_flag)
 
644
          func= (((*b)->unsigned_flag)?
 
645
                 &Arg_comparator::compare_int_unsigned :
 
646
                 &Arg_comparator::compare_int_unsigned_signed);
 
647
        else if ((*b)->unsigned_flag)
 
648
          func= &Arg_comparator::compare_int_signed_unsigned;
 
649
      }
 
650
      else if (func== &Arg_comparator::compare_e_int)
 
651
      {
 
652
        if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
 
653
          func= &Arg_comparator::compare_e_int_diff_signedness;
 
654
      }
 
655
      break;
 
656
    }
651
657
  case DECIMAL_RESULT:
652
658
    break;
653
659
  case REAL_RESULT:
654
 
  {
655
 
    if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
656
660
    {
657
 
      precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
658
 
      if (func == &Arg_comparator::compare_real)
659
 
        func= &Arg_comparator::compare_real_fixed;
660
 
      else if (func == &Arg_comparator::compare_e_real)
661
 
        func= &Arg_comparator::compare_e_real_fixed;
 
661
      if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
 
662
      {
 
663
        precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
 
664
        if (func == &Arg_comparator::compare_real)
 
665
          func= &Arg_comparator::compare_real_fixed;
 
666
        else if (func == &Arg_comparator::compare_e_real)
 
667
          func= &Arg_comparator::compare_e_real_fixed;
 
668
      }
 
669
      break;
662
670
    }
663
 
    break;
664
 
  }
665
 
  default:
666
 
    assert(0);
667
 
  }
 
671
  }
 
672
 
668
673
  return 0;
669
674
}
670
675
 
697
702
{
698
703
  uint64_t value= 0;
699
704
  int error;
700
 
  DRIZZLE_TIME l_time;
 
705
  type::Time l_time;
701
706
  enum enum_drizzle_timestamp_type ret;
702
707
 
703
708
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
1168
1173
 
1169
1174
int Arg_comparator::compare_decimal()
1170
1175
{
1171
 
  my_decimal value1;
1172
 
  my_decimal *val1= (*a)->val_decimal(&value1);
 
1176
  type::Decimal value1;
 
1177
  type::Decimal *val1= (*a)->val_decimal(&value1);
1173
1178
  if (!(*a)->null_value)
1174
1179
  {
1175
 
    my_decimal value2;
1176
 
    my_decimal *val2= (*b)->val_decimal(&value2);
 
1180
    type::Decimal value2;
 
1181
    type::Decimal *val2= (*b)->val_decimal(&value2);
1177
1182
    if (!(*b)->null_value)
1178
1183
    {
1179
1184
      owner->null_value= 0;
1180
 
      return my_decimal_cmp(val1, val2);
 
1185
      return class_decimal_cmp(val1, val2);
1181
1186
    }
1182
1187
  }
1183
1188
  owner->null_value= 1;
1195
1200
 
1196
1201
int Arg_comparator::compare_e_decimal()
1197
1202
{
1198
 
  my_decimal value1, value2;
1199
 
  my_decimal *val1= (*a)->val_decimal(&value1);
1200
 
  my_decimal *val2= (*b)->val_decimal(&value2);
 
1203
  type::Decimal value1, value2;
 
1204
  type::Decimal *val1= (*a)->val_decimal(&value1);
 
1205
  type::Decimal *val2= (*b)->val_decimal(&value2);
1201
1206
  if ((*a)->null_value || (*b)->null_value)
1202
1207
    return test((*a)->null_value && (*b)->null_value);
1203
 
  return test(my_decimal_cmp(val1, val2) == 0);
 
1208
  return test(class_decimal_cmp(val1, val2) == 0);
1204
1209
}
1205
1210
 
1206
1211
 
1610
1615
 
1611
1616
void Item_in_optimizer::cleanup()
1612
1617
{
1613
 
  Item_bool_func::cleanup();
 
1618
  item::function::Boolean::cleanup();
1614
1619
  if (!save_cache)
1615
1620
    cache= 0;
1616
1621
  return;
1818
1823
          {
1819
1824
            range->type= DECIMAL_RESULT;
1820
1825
            range->dec.init();
1821
 
            my_decimal *dec= el->val_decimal(&range->dec);
 
1826
            type::Decimal *dec= el->val_decimal(&range->dec);
1822
1827
            if (dec != &range->dec)
1823
1828
            {
1824
1829
              range->dec= *dec;
1868
1873
{
1869
1874
  assert(fixed == 1);
1870
1875
  double value;
1871
 
  my_decimal dec_buf, *dec= NULL;
 
1876
  type::Decimal dec_buf, *dec= NULL;
1872
1877
  uint32_t i;
1873
1878
 
1874
1879
  if (use_decimal_comparison)
1876
1881
    dec= row->element_index(0)->val_decimal(&dec_buf);
1877
1882
    if (row->element_index(0)->null_value)
1878
1883
      return -1;
1879
 
    my_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
 
1884
    class_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
1880
1885
  }
1881
1886
  else
1882
1887
  {
1901
1906
        and we are comparing against a decimal
1902
1907
      */
1903
1908
      if (dec && range->type == DECIMAL_RESULT)
1904
 
        cmp_result= my_decimal_cmp(&range->dec, dec) <= 0;
 
1909
        cmp_result= class_decimal_cmp(&range->dec, dec) <= 0;
1905
1910
      else
1906
1911
        cmp_result= (range->dbl <= value);
1907
1912
      if (cmp_result)
1911
1916
    }
1912
1917
    interval_range *range= intervals+start;
1913
1918
    return ((dec && range->type == DECIMAL_RESULT) ?
1914
 
            my_decimal_cmp(dec, &range->dec) < 0 :
 
1919
            class_decimal_cmp(dec, &range->dec) < 0 :
1915
1920
            value < range->dbl) ? 0 : start + 1;
1916
1921
  }
1917
1922
 
1922
1927
        ((el->result_type() == DECIMAL_RESULT) ||
1923
1928
         (el->result_type() == INT_RESULT)))
1924
1929
    {
1925
 
      my_decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
 
1930
      type::Decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
1926
1931
      /* Skip NULL ranges. */
1927
1932
      if (el->null_value)
1928
1933
        continue;
1929
 
      if (my_decimal_cmp(e_dec, dec) > 0)
 
1934
      if (class_decimal_cmp(e_dec, dec) > 0)
1930
1935
        return i - 1;
1931
1936
    }
1932
1937
    else
2123
2128
  }
2124
2129
  else if (cmp_type == DECIMAL_RESULT)
2125
2130
  {
2126
 
    my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
 
2131
    type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2127
2132
               a_buf, *a_dec, b_buf, *b_dec;
2128
2133
    if ((null_value=args[0]->null_value))
2129
2134
      return 0;
2130
2135
    a_dec= args[1]->val_decimal(&a_buf);
2131
2136
    b_dec= args[2]->val_decimal(&b_buf);
2132
2137
    if (!args[1]->null_value && !args[2]->null_value)
2133
 
      return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2134
 
                          my_decimal_cmp(dec, b_dec) <= 0) != negated);
 
2138
      return (int64_t) ((class_decimal_cmp(dec, a_dec) >= 0 &&
 
2139
                          class_decimal_cmp(dec, b_dec) <= 0) != negated);
2135
2140
    if (args[1]->null_value && args[2]->null_value)
2136
2141
      null_value=1;
2137
2142
    else if (args[1]->null_value)
2138
 
      null_value= (my_decimal_cmp(dec, b_dec) <= 0);
 
2143
      null_value= (class_decimal_cmp(dec, b_dec) <= 0);
2139
2144
    else
2140
 
      null_value= (my_decimal_cmp(dec, a_dec) >= 0);
 
2145
      null_value= (class_decimal_cmp(dec, a_dec) >= 0);
2141
2146
  }
2142
2147
  else
2143
2148
  {
2195
2200
    max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
2196
2201
  }
2197
2202
  else
 
2203
  {
2198
2204
    max_length= max(args[0]->max_length, args[1]->max_length);
 
2205
  }
2199
2206
 
2200
2207
  switch (hybrid_type)
2201
2208
  {
2202
2209
  case STRING_RESULT:
2203
2210
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2204
2211
    break;
 
2212
 
2205
2213
  case DECIMAL_RESULT:
2206
2214
  case REAL_RESULT:
2207
2215
    break;
 
2216
 
2208
2217
  case INT_RESULT:
2209
2218
    decimals= 0;
2210
2219
    break;
 
2220
 
2211
2221
  case ROW_RESULT:
2212
 
  default:
2213
2222
    assert(0);
2214
2223
  }
 
2224
 
2215
2225
  cached_field_type= agg_field_type(args, 2);
2216
2226
}
2217
2227
 
2266
2276
}
2267
2277
 
2268
2278
 
2269
 
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
 
2279
type::Decimal *Item_func_ifnull::decimal_op(type::Decimal *decimal_value)
2270
2280
{
2271
2281
  assert(fixed == 1);
2272
 
  my_decimal *value= args[0]->val_decimal(decimal_value);
 
2282
  type::Decimal *value= args[0]->val_decimal(decimal_value);
2273
2283
  if (!args[0]->null_value)
2274
2284
  {
2275
2285
    null_value= 0;
2439
2449
}
2440
2450
 
2441
2451
 
2442
 
my_decimal *
2443
 
Item_func_if::val_decimal(my_decimal *decimal_value)
 
2452
type::Decimal *
 
2453
Item_func_if::val_decimal(type::Decimal *decimal_value)
2444
2454
{
2445
2455
  assert(fixed == 1);
2446
2456
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2447
 
  my_decimal *value= arg->val_decimal(decimal_value);
 
2457
  type::Decimal *value= arg->val_decimal(decimal_value);
2448
2458
  null_value= arg->null_value;
2449
2459
  return value;
2450
2460
}
2524
2534
}
2525
2535
 
2526
2536
 
2527
 
my_decimal *
2528
 
Item_func_nullif::val_decimal(my_decimal * decimal_value)
 
2537
type::Decimal *
 
2538
Item_func_nullif::val_decimal(type::Decimal * decimal_value)
2529
2539
{
2530
2540
  assert(fixed == 1);
2531
 
  my_decimal *res;
 
2541
  type::Decimal *res;
2532
2542
  if (!cmp.compare())
2533
2543
  {
2534
2544
    null_value=1;
2661
2671
}
2662
2672
 
2663
2673
 
2664
 
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
 
2674
type::Decimal *Item_func_case::val_decimal(type::Decimal *decimal_value)
2665
2675
{
2666
2676
  assert(fixed == 1);
2667
2677
  char buff[MAX_FIELD_WIDTH];
2668
2678
  String dummy_str(buff, sizeof(buff), default_charset());
2669
2679
  Item *item= find_item(&dummy_str);
2670
 
  my_decimal *res;
 
2680
  type::Decimal *res;
2671
2681
 
2672
2682
  if (!item)
2673
2683
  {
2710
2720
 
2711
2721
void Item_func_case::agg_num_lengths(Item *arg)
2712
2722
{
2713
 
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2723
  uint32_t len= class_decimal_length_to_precision(arg->max_length, arg->decimals,
2714
2724
                                           arg->unsigned_flag) - arg->decimals;
2715
2725
  set_if_bigger(max_length, len);
2716
2726
  set_if_bigger(decimals, arg->decimals);
2749
2759
  */
2750
2760
  if (first_expr_num != -1)
2751
2761
  {
2752
 
    uint32_t i;
2753
2762
    agg[0]= args[first_expr_num];
2754
2763
    left_result_type= agg[0]->result_type();
2755
2764
 
2759
2768
    if (!(found_types= collect_cmp_types(agg, nagg)))
2760
2769
      return;
2761
2770
 
2762
 
    for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
 
2771
    for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
2763
2772
    {
2764
2773
      if (found_types & (1 << i) && !cmp_items[i])
2765
2774
      {
2794
2803
      agg_num_lengths(args[i + 1]);
2795
2804
    if (else_expr_num != -1)
2796
2805
      agg_num_lengths(args[else_expr_num]);
2797
 
    max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
 
2806
    max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
2798
2807
                                               unsigned_flag);
2799
2808
  }
2800
2809
}
2845
2854
 
2846
2855
void Item_func_case::cleanup()
2847
2856
{
2848
 
  uint32_t i;
2849
2857
  Item_func::cleanup();
2850
 
  for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
 
2858
  for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
2851
2859
  {
2852
2860
    delete cmp_items[i];
2853
2861
    cmp_items[i]= 0;
2854
2862
  }
2855
 
  return;
2856
2863
}
2857
2864
 
2858
2865
 
2903
2910
}
2904
2911
 
2905
2912
 
2906
 
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
 
2913
type::Decimal *Item_func_coalesce::decimal_op(type::Decimal *decimal_value)
2907
2914
{
2908
2915
  assert(fixed == 1);
2909
2916
  null_value= 0;
2910
2917
  for (uint32_t i= 0; i < arg_count; i++)
2911
2918
  {
2912
 
    my_decimal *res= args[i]->val_decimal(decimal_value);
 
2919
    type::Decimal *res= args[i]->val_decimal(decimal_value);
2913
2920
    if (!args[i]->null_value)
2914
2921
      return res;
2915
2922
  }
2922
2929
{
2923
2930
  cached_field_type= agg_field_type(args, arg_count);
2924
2931
  agg_result_type(&hybrid_type, args, arg_count);
 
2932
 
2925
2933
  switch (hybrid_type) {
2926
2934
  case STRING_RESULT:
2927
2935
    count_only_length();
2928
2936
    decimals= NOT_FIXED_DEC;
2929
2937
    agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1);
2930
2938
    break;
 
2939
 
2931
2940
  case DECIMAL_RESULT:
2932
2941
    count_decimal_length();
2933
2942
    break;
 
2943
 
2934
2944
  case REAL_RESULT:
2935
2945
    count_real_length();
2936
2946
    break;
 
2947
 
2937
2948
  case INT_RESULT:
2938
2949
    count_only_length();
2939
2950
    decimals= 0;
2940
2951
    break;
 
2952
 
2941
2953
  case ROW_RESULT:
2942
 
  default:
2943
2954
    assert(0);
2944
2955
  }
2945
2956
}
3054
3065
}
3055
3066
 
3056
3067
 
3057
 
static int cmp_decimal(void *, my_decimal *a, my_decimal *b)
 
3068
static int cmp_decimal(void *, type::Decimal *a, type::Decimal *b)
3058
3069
{
3059
3070
  /*
3060
3071
    We need call of fixing buffer pointer, because fast sort just copy
3062
3073
  */
3063
3074
  a->fix_buffer_pointer();
3064
3075
  b->fix_buffer_pointer();
3065
 
  return my_decimal_cmp(a, b);
 
3076
  return class_decimal_cmp(a, b);
3066
3077
}
3067
3078
 
3068
3079
 
3231
3242
 
3232
3243
 
3233
3244
in_decimal::in_decimal(uint32_t elements)
3234
 
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
 
3245
  :in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
3235
3246
{}
3236
3247
 
3237
3248
 
3238
3249
void in_decimal::set(uint32_t pos, Item *item)
3239
3250
{
3240
 
  /* as far as 'item' is constant, we can store reference on my_decimal */
3241
 
  my_decimal *dec= ((my_decimal *)base) + pos;
 
3251
  /* as far as 'item' is constant, we can store reference on type::Decimal */
 
3252
  type::Decimal *dec= ((type::Decimal *)base) + pos;
3242
3253
  dec->len= DECIMAL_BUFF_LENGTH;
3243
3254
  dec->fix_buffer_pointer();
3244
 
  my_decimal *res= item->val_decimal(dec);
 
3255
  type::Decimal *res= item->val_decimal(dec);
3245
3256
  /* if item->val_decimal() is evaluated to NULL then res == 0 */
3246
3257
  if (!item->null_value && res != dec)
3247
 
    my_decimal2decimal(res, dec);
 
3258
    class_decimal2decimal(res, dec);
3248
3259
}
3249
3260
 
3250
3261
 
3251
3262
unsigned char *in_decimal::get_value(Item *item)
3252
3263
{
3253
 
  my_decimal *result= item->val_decimal(&val);
 
3264
  type::Decimal *result= item->val_decimal(&val);
3254
3265
  if (item->null_value)
3255
3266
    return 0;
3256
3267
  return (unsigned char *)result;
3263
3274
  switch (type) {
3264
3275
  case STRING_RESULT:
3265
3276
    return new cmp_item_sort_string(cs);
 
3277
 
3266
3278
  case INT_RESULT:
3267
3279
    return new cmp_item_int;
 
3280
 
3268
3281
  case REAL_RESULT:
3269
3282
    return new cmp_item_real;
 
3283
 
3270
3284
  case ROW_RESULT:
3271
3285
    return new cmp_item_row;
 
3286
 
3272
3287
  case DECIMAL_RESULT:
3273
3288
    return new cmp_item_decimal;
3274
 
  default:
3275
 
    assert(0);
3276
 
    break;
3277
3289
  }
 
3290
 
3278
3291
  return 0; // to satisfy compiler :)
3279
3292
}
3280
3293
 
3407
3420
 
3408
3421
void cmp_item_decimal::store_value(Item *item)
3409
3422
{
3410
 
  my_decimal *val= item->val_decimal(&value);
 
3423
  type::Decimal *val= item->val_decimal(&value);
3411
3424
  /* val may be zero if item is nnull */
3412
3425
  if (val && val != &value)
3413
 
    my_decimal2decimal(val, &value);
 
3426
    class_decimal2decimal(val, &value);
3414
3427
}
3415
3428
 
3416
3429
 
3417
3430
int cmp_item_decimal::cmp(Item *arg)
3418
3431
{
3419
 
  my_decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
 
3432
  type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
3420
3433
  if (arg->null_value)
3421
3434
    return 1;
3422
 
  return my_decimal_cmp(&value, tmp);
 
3435
  return class_decimal_cmp(&value, tmp);
3423
3436
}
3424
3437
 
3425
3438
 
3426
3439
int cmp_item_decimal::compare(cmp_item *arg)
3427
3440
{
3428
3441
  cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
3429
 
  return my_decimal_cmp(&value, &l_cmp->value);
 
3442
  return class_decimal_cmp(&value, &l_cmp->value);
3430
3443
}
3431
3444
 
3432
3445
 
3545
3558
  bool compare_as_datetime= false;
3546
3559
  Item *date_arg= 0;
3547
3560
  uint32_t found_types= 0;
3548
 
  uint32_t type_cnt= 0, i;
 
3561
  uint32_t type_cnt= 0;
3549
3562
  Item_result cmp_type= STRING_RESULT;
3550
3563
  left_result_type= args[0]->result_type();
3551
3564
  if (!(found_types= collect_cmp_types(args, arg_count, true)))
3559
3572
      break;
3560
3573
    }
3561
3574
  }
3562
 
  for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
 
3575
  for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
3563
3576
  {
3564
3577
    if (found_types & 1 << i)
3565
3578
    {
3666
3679
  if (type_cnt == 1 && const_itm && !nulls_in_row())
3667
3680
  {
3668
3681
    if (compare_as_datetime)
 
3682
    {
3669
3683
      array= new in_datetime(date_arg, arg_count - 1);
 
3684
    }
3670
3685
    else
3671
3686
    {
3672
3687
      /*
3692
3707
            cmp_type= INT_RESULT;
3693
3708
        }
3694
3709
      }
 
3710
 
3695
3711
      switch (cmp_type) {
3696
3712
      case STRING_RESULT:
3697
3713
        array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
3698
3714
                            cmp_collation.collation);
3699
3715
        break;
 
3716
 
3700
3717
      case INT_RESULT:
3701
3718
        array= new in_int64_t(arg_count-1);
3702
3719
        break;
 
3720
 
3703
3721
      case REAL_RESULT:
3704
3722
        array= new in_double(arg_count-1);
3705
3723
        break;
 
3724
 
3706
3725
      case ROW_RESULT:
3707
3726
        /*
3708
3727
          The row comparator was created at the beginning but only DATETIME
3711
3730
        */
3712
3731
        ((in_row*)array)->tmp.store_value(args[0]);
3713
3732
        break;
 
3733
 
3714
3734
      case DECIMAL_RESULT:
3715
3735
        array= new in_decimal(arg_count - 1);
3716
3736
        break;
3717
 
      default:
3718
 
        assert(0);
3719
 
        return;
3720
3737
      }
3721
3738
    }
 
3739
 
3722
3740
    if (array && !(session->is_fatal_error))            // If not EOM
3723
3741
    {
3724
3742
      uint32_t j=0;
3725
3743
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
3726
3744
      {
3727
 
        if (!args[arg_num]->null_value)                 // Skip NULL values
 
3745
        if (!args[arg_num]->null_value)                 // Skip NULL values
3728
3746
        {
3729
3747
          array->set(j,args[arg_num]);
3730
 
          j++;
 
3748
          j++;
3731
3749
        }
3732
 
        else
3733
 
          have_null= 1;
 
3750
        else
 
3751
          have_null= 1;
3734
3752
      }
3735
3753
      if ((array->used_count= j))
3736
 
        array->sort();
 
3754
        array->sort();
3737
3755
    }
3738
3756
  }
3739
3757
  else
3742
3760
      cmp_items[STRING_RESULT]= new cmp_item_datetime(date_arg);
3743
3761
    else
3744
3762
    {
3745
 
      for (i= 0; i <= (uint32_t) DECIMAL_RESULT; i++)
 
3763
      for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
3746
3764
      {
3747
3765
        if (found_types & (1 << i) && !cmp_items[i])
3748
3766
        {
3835
3853
 
3836
3854
 
3837
3855
Item_cond::Item_cond(Session *session, Item_cond *item)
3838
 
  :Item_bool_func(session, item),
 
3856
  :item::function::Boolean(session, item),
3839
3857
   abort_on_null(item->abort_on_null),
3840
3858
   and_tables_cache(item->and_tables_cache)
3841
3859
{
3862
3880
  void *orig_session_marker= session->session_marker;
3863
3881
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
3864
3882
  not_null_tables_cache= used_tables_cache= 0;
3865
 
  const_item_cache= 1;
 
3883
  const_item_cache= true;
3866
3884
 
3867
3885
  if (functype() == COND_OR_FUNC)
3868
3886
    session->session_marker= 0;
3937
3955
  Item *item;
3938
3956
 
3939
3957
  used_tables_cache=0;
3940
 
  const_item_cache=1;
 
3958
  const_item_cache= true;
3941
3959
 
3942
3960
  and_tables_cache= ~(table_map) 0; // Here and below we do as fix_fields does
3943
3961
  not_null_tables_cache= 0;
4126
4144
  Item *item;
4127
4145
 
4128
4146
  used_tables_cache=0;
4129
 
  const_item_cache=1;
 
4147
  const_item_cache= true;
4130
4148
  while ((item=li++))
4131
4149
  {
4132
4150
    item->update_used_tables();
4868
4886
}
4869
4887
 
4870
4888
Item_equal::Item_equal(Item_field *f1, Item_field *f2)
4871
 
  : Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
 
4889
  : item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
4872
4890
{
4873
 
  const_item_cache= 0;
 
4891
  const_item_cache= false;
4874
4892
  fields.push_back(f1);
4875
4893
  fields.push_back(f2);
4876
4894
}
4877
4895
 
4878
4896
Item_equal::Item_equal(Item *c, Item_field *f)
4879
 
  : Item_bool_func(), eval_item(0), cond_false(0)
 
4897
  : item::function::Boolean(), eval_item(0), cond_false(0)
4880
4898
{
4881
 
  const_item_cache= 0;
 
4899
  const_item_cache= false;
4882
4900
  fields.push_back(f);
4883
4901
  const_item= c;
4884
4902
}
4885
4903
 
4886
4904
 
4887
4905
Item_equal::Item_equal(Item_equal *item_equal)
4888
 
  : Item_bool_func(), eval_item(0), cond_false(0)
 
4906
  : item::function::Boolean(), eval_item(0), cond_false(0)
4889
4907
{
4890
 
  const_item_cache= 0;
 
4908
  const_item_cache= false;
4891
4909
  List_iterator_fast<Item_field> li(item_equal->fields);
4892
4910
  Item_field *item;
4893
4911
  while ((item= li++))
4911
4929
  func->set_cmp_func();
4912
4930
  func->quick_fix_field();
4913
4931
  if ((cond_false= !func->val_int()))
4914
 
    const_item_cache= 1;
 
4932
    const_item_cache= true;
4915
4933
}
4916
4934
 
4917
4935
void Item_equal::add(Item_field *f)
5057
5075
  List_iterator_fast<Item_field> li(fields);
5058
5076
  Item *item;
5059
5077
  not_null_tables_cache= used_tables_cache= 0;
5060
 
  const_item_cache= 0;
 
5078
  const_item_cache= false;
5061
5079
  while ((item= li++))
5062
5080
  {
5063
5081
    table_map tmp_table_map;