~capttofu/drizzle/drizzle_memcached

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2008-11-18 23:19:19 UTC
  • mfrom: (584.1.16 devel)
  • Revision ID: brian@tangent.org-20081118231919-w9sr347dtiwhccml
Merge of Monty's work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
15
19
 
16
20
 
17
21
#include <drizzled/server_includes.h>
20
24
#include <drizzled/sql_select.h>
21
25
#include <drizzled/error.h>
22
26
#include <drizzled/show.h>
 
27
#include <drizzled/item/cmpfunc.h>
 
28
#include <drizzled/item/cache_row.h>
 
29
#include <drizzled/item/type_holder.h>
 
30
#include <drizzled/item/sum.h>
 
31
#include <drizzled/functions/str/conv_charset.h>
 
32
#include <drizzled/virtual_column_info.h>
 
33
#include <drizzled/sql_base.h>
23
34
 
24
35
#if defined(CMATH_NAMESPACE)
25
36
using namespace CMATH_NAMESPACE;
28
39
const String my_null_string("NULL", 4, default_charset_info);
29
40
 
30
41
 
 
42
const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;
 
43
 
 
44
 
31
45
/*****************************************************************************
32
46
** Item functions
33
47
*****************************************************************************/
41
55
}
42
56
 
43
57
 
 
58
bool Item::is_expensive_processor(unsigned char *)
 
59
{
 
60
  return 0;
 
61
}
 
62
 
 
63
void Item::fix_after_pullout(st_select_lex *, Item **)
 
64
{}
 
65
 
 
66
 
 
67
Field *Item::tmp_table_field(Table *)
 
68
{
 
69
  return 0;
 
70
}
 
71
 
 
72
 
 
73
const char *Item::full_name(void) const
 
74
{
 
75
  return name ? name : "???";
 
76
}
 
77
 
 
78
 
 
79
int64_t Item::val_int_endpoint(bool, bool *)
 
80
{
 
81
  assert(0);
 
82
  return 0;
 
83
}
 
84
 
 
85
 
44
86
/**
45
87
  @todo
46
88
    Make this functions class dependent
317
359
}
318
360
 
319
361
 
 
362
int Item::decimal_int_part() const
 
363
{
 
364
  return my_decimal_int_part(decimal_precision(), decimals);
 
365
}
 
366
 
 
367
 
 
368
void Item::print(String *str, enum_query_type)
 
369
{
 
370
  str->append(full_name());
 
371
}
 
372
 
 
373
 
320
374
void Item::print_item_w_name(String *str, enum_query_type query_type)
321
375
{
322
376
  print(str, query_type);
330
384
}
331
385
 
332
386
 
 
387
void Item::split_sum_func(Session *, Item **, List<Item> &)
 
388
{}
 
389
 
 
390
 
333
391
void Item::cleanup()
334
392
{
335
393
  fixed=0;
824
882
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
825
883
  {
826
884
    memset(ltime, 0, sizeof(*ltime));
827
 
    return 1;
 
885
    return true;
828
886
  }
829
 
  return 0;
830
 
}
 
887
  return false;
 
888
}
 
889
 
 
890
 
 
891
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
892
{
 
893
  return get_date(ltime,fuzzydate);
 
894
}
 
895
 
 
896
 
 
897
bool Item::is_null()
 
898
{
 
899
  return false;
 
900
}
 
901
 
 
902
 
 
903
void Item::update_null_value ()
 
904
{
 
905
  (void) val_int();
 
906
}
 
907
 
 
908
 
 
909
void Item::top_level_item(void)
 
910
{}
 
911
 
 
912
 
 
913
void Item::set_result_field(Field *)
 
914
{}
 
915
 
 
916
 
 
917
bool Item::is_result_field(void)
 
918
{
 
919
  return 0;
 
920
}
 
921
 
 
922
 
 
923
bool Item::is_bool_func(void)
 
924
{
 
925
  return 0;
 
926
}
 
927
 
 
928
 
 
929
void Item::save_in_result_field(bool)
 
930
{}
 
931
 
 
932
 
 
933
void Item::no_rows_in_result(void)
 
934
{}
 
935
 
 
936
 
 
937
Item *Item::copy_or_same(Session *)
 
938
{
 
939
  return this;
 
940
}
 
941
 
 
942
 
 
943
Item *Item::copy_andor_structure(Session *)
 
944
{
 
945
  return this;
 
946
}
 
947
 
 
948
 
 
949
Item *Item::real_item(void)
 
950
{
 
951
  return this;
 
952
}
 
953
 
 
954
 
 
955
Item *Item::get_tmp_table_item(Session *session)
 
956
{
 
957
  return copy_or_same(session);
 
958
}
 
959
 
831
960
 
832
961
const CHARSET_INFO *Item::default_charset()
833
962
{
835
964
}
836
965
 
837
966
 
838
 
/*
839
 
  Save value in field, but don't give any warnings
840
 
 
841
 
  NOTES
842
 
   This is used to temporary store and retrieve a value in a column,
843
 
   for example in opt_range to adjust the key value to fit the column.
844
 
*/
 
967
const CHARSET_INFO *Item::compare_collation()
 
968
{
 
969
  return NULL;
 
970
}
 
971
 
 
972
 
 
973
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
 
974
{
 
975
  return (this->*processor)(arg);
 
976
}
 
977
 
 
978
 
 
979
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
 
980
                    Item_transformer transformer, unsigned char *arg_t)
 
981
{
 
982
  if ((this->*analyzer) (arg_p))
 
983
    return ((this->*transformer) (arg_t));
 
984
  return 0;
 
985
}
 
986
 
 
987
 
 
988
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
 
989
{
 
990
  (*traverser)(this, arg);
 
991
}
 
992
 
 
993
 
 
994
bool Item::remove_dependence_processor(unsigned char *)
 
995
{
 
996
  return 0;
 
997
}
 
998
 
 
999
 
 
1000
bool Item::remove_fixed(unsigned char *)
 
1001
{
 
1002
  fixed= 0;
 
1003
  return 0;
 
1004
}
 
1005
 
 
1006
 
 
1007
bool Item::collect_item_field_processor(unsigned char *)
 
1008
{
 
1009
  return 0;
 
1010
}
 
1011
 
 
1012
 
 
1013
bool Item::find_item_in_field_list_processor(unsigned char *)
 
1014
{
 
1015
  return 0;
 
1016
}
 
1017
 
 
1018
 
 
1019
bool Item::change_context_processor(unsigned char *)
 
1020
{
 
1021
  return 0;
 
1022
}
 
1023
 
 
1024
bool Item::reset_query_id_processor(unsigned char *)
 
1025
{
 
1026
  return 0;
 
1027
}
 
1028
 
 
1029
 
 
1030
bool Item::register_field_in_read_map(unsigned char *)
 
1031
{
 
1032
  return 0;
 
1033
}
 
1034
 
 
1035
 
 
1036
bool Item::register_field_in_bitmap(unsigned char *)
 
1037
{
 
1038
  return 0;
 
1039
}
 
1040
 
 
1041
 
 
1042
bool Item::subst_argument_checker(unsigned char **arg)
 
1043
{
 
1044
  if (*arg)
 
1045
    *arg= NULL;
 
1046
  return true;
 
1047
}
 
1048
 
 
1049
 
 
1050
bool Item::check_vcol_func_processor(unsigned char *)
 
1051
{
 
1052
  return true;
 
1053
}
 
1054
 
 
1055
 
 
1056
Item *Item::equal_fields_propagator(unsigned char *)
 
1057
{
 
1058
  return this;
 
1059
}
 
1060
 
 
1061
 
 
1062
bool Item::set_no_const_sub(unsigned char *)
 
1063
{
 
1064
  return false;
 
1065
}
 
1066
 
 
1067
 
 
1068
Item *Item::replace_equal_field(unsigned char *)
 
1069
{
 
1070
  return this;
 
1071
}
 
1072
 
 
1073
 
 
1074
Item *Item::this_item(void)
 
1075
{
 
1076
  return this;
 
1077
}
 
1078
 
 
1079
 
 
1080
const Item *Item::this_item(void) const
 
1081
{
 
1082
  return this;
 
1083
}
 
1084
 
 
1085
 
 
1086
Item **Item::this_item_addr(Session *, Item **addr_arg)
 
1087
{
 
1088
  return addr_arg;
 
1089
}
 
1090
 
 
1091
 
 
1092
uint32_t Item::cols()
 
1093
{
 
1094
  return 1;
 
1095
}
 
1096
 
 
1097
 
 
1098
Item* Item::element_index(uint32_t)
 
1099
{
 
1100
  return this;
 
1101
}
 
1102
 
 
1103
 
 
1104
Item** Item::addr(uint32_t)
 
1105
{
 
1106
  return 0;
 
1107
}
 
1108
 
 
1109
 
 
1110
bool Item::null_inside()
 
1111
{
 
1112
  return 0;
 
1113
}
 
1114
 
 
1115
 
 
1116
void Item::bring_value()
 
1117
{}
 
1118
 
 
1119
 
 
1120
Item_field *Item::filed_for_view_update()
 
1121
{
 
1122
  return 0;
 
1123
}
 
1124
 
 
1125
Item *Item::neg_transformer(Session *)
 
1126
{
 
1127
  return NULL;
 
1128
}
 
1129
 
 
1130
 
 
1131
Item *Item::update_value_transformer(unsigned char *)
 
1132
{
 
1133
  return this;
 
1134
}
 
1135
 
 
1136
 
 
1137
void Item::delete_self()
 
1138
{
 
1139
  cleanup();
 
1140
  delete this;
 
1141
}
 
1142
 
 
1143
bool Item::result_as_int64_t()
 
1144
{
 
1145
  return false;
 
1146
}
 
1147
 
 
1148
 
 
1149
bool Item::is_expensive()
 
1150
{
 
1151
  if (is_expensive_cache < 0)
 
1152
    is_expensive_cache= walk(&Item::is_expensive_processor, 0,
 
1153
                             (unsigned char*)0);
 
1154
  return test(is_expensive_cache);
 
1155
}
 
1156
 
845
1157
 
846
1158
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
847
1159
{
892
1204
                               SUM items
893
1205
 
894
1206
  @note
895
 
    This is from split_sum_func2() for items that should be split
 
1207
    This is from split_sum_func() for items that should be split
896
1208
 
897
1209
    All found SUM items are added FIRST in the fields list and
898
1210
    we replace the item with a reference.
900
1212
    session->fatal_error() may be called if we are out of memory
901
1213
*/
902
1214
 
903
 
void Item::split_sum_func2(Session *session, Item **ref_pointer_array,
904
 
                           List<Item> &fields, Item **ref, 
905
 
                           bool skip_registered)
 
1215
void Item::split_sum_func(Session *session, Item **ref_pointer_array,
 
1216
                          List<Item> &fields, Item **ref,
 
1217
                          bool skip_registered)
906
1218
{
907
 
  /* An item of type Item_sum  is registered <=> ref_by != 0 */ 
908
 
  if (type() == SUM_FUNC_ITEM && skip_registered && 
 
1219
  /* An item of type Item_sum  is registered <=> ref_by != 0 */
 
1220
  if (type() == SUM_FUNC_ITEM && skip_registered &&
909
1221
      ((Item_sum *) this)->ref_by)
910
 
    return;                                                 
 
1222
    return;
911
1223
  if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
912
1224
      (type() == FUNC_ITEM &&
913
1225
       (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
960
1272
  tmp_field->decimals= field->decimals();
961
1273
}
962
1274
 
 
1275
 
 
1276
double Item_ident_for_show::val_real()
 
1277
{
 
1278
  return field->val_real();
 
1279
}
 
1280
 
 
1281
 
 
1282
int64_t Item_ident_for_show::val_int()
 
1283
{
 
1284
  return field->val_int();
 
1285
}
 
1286
 
 
1287
 
 
1288
String *Item_ident_for_show::val_str(String *str)
 
1289
{
 
1290
  return field->val_str(str);
 
1291
}
 
1292
 
 
1293
 
 
1294
my_decimal *Item_ident_for_show::val_decimal(my_decimal *dec)
 
1295
{
 
1296
  return field->val_decimal(dec);
 
1297
}
 
1298
 
 
1299
 
963
1300
/**********************************************/
964
1301
 
965
1302
Item_field::Item_field(Field *f)
1294
1631
}
1295
1632
 
1296
1633
 
 
1634
enum Item_result Item_field::result_type () const
 
1635
{
 
1636
  return field->result_type();
 
1637
}
 
1638
 
 
1639
 
 
1640
Item_result Item_field::cast_to_int_type() const
 
1641
{
 
1642
  return field->cast_to_int_type();
 
1643
}
 
1644
 
 
1645
 
 
1646
enum_field_types Item_field::field_type() const
 
1647
{
 
1648
  return field->type();
 
1649
}
 
1650
 
 
1651
 
1297
1652
void Item_field::fix_after_pullout(st_select_lex *new_parent, Item **)
1298
1653
{
1299
1654
  if (new_parent == depended_from)
1307
1662
}
1308
1663
 
1309
1664
 
 
1665
bool Item_field::is_null()
 
1666
{
 
1667
  return field->is_null();
 
1668
}
 
1669
 
 
1670
 
1310
1671
Item *Item_field::get_tmp_table_item(Session *session)
1311
1672
{
1312
1673
  Item_field *new_item= new Item_field(session, this);
3267
3628
  return;
3268
3629
}
3269
3630
 
 
3631
 
 
3632
bool Item_field::result_as_int64_t()
 
3633
{
 
3634
  return field->can_be_compared_as_int64_t();
 
3635
}
 
3636
 
 
3637
 
3270
3638
/**
3271
3639
  Find a field among specified multiple equalities.
3272
3640
 
3452
3820
}
3453
3821
 
3454
3822
 
 
3823
uint32_t Item_field::max_disp_length()
 
3824
{
 
3825
  return field->max_display_length();
 
3826
}
 
3827
 
 
3828
 
3455
3829
void Item::init_make_field(Send_field *tmp_field,
3456
3830
                           enum enum_field_types field_type_arg)
3457
3831
{
5417
5791
}
5418
5792
 
5419
5793
 
 
5794
bool Item_cache::eq_def(Field *field)
 
5795
{
 
5796
  return cached_field ? cached_field->eq_def (field) : false;
 
5797
}
 
5798
 
 
5799
 
5420
5800
void Item_cache_int::store(Item *item)
5421
5801
{
5422
5802
  value= item->val_int_result();
5518
5898
}
5519
5899
 
5520
5900
 
 
5901
Item_cache_str::Item_cache_str(const Item *item) :
 
5902
  Item_cache(), value(0),
 
5903
  is_varbinary(item->type() == FIELD_ITEM &&
 
5904
               ((const Item_field *) item)->field->type() ==
 
5905
               DRIZZLE_TYPE_VARCHAR &&
 
5906
               !((const Item_field *) item)->field->has_charset())
 
5907
{}
 
5908
 
5521
5909
void Item_cache_str::store(Item *item)
5522
5910
{
5523
5911
  value_buff.set(buffer, sizeof(buffer), item->collation.collation);
5581
5969
}
5582
5970
 
5583
5971
 
5584
 
bool Item_cache_row::allocate(uint32_t num)
5585
 
{
5586
 
  item_count= num;
5587
 
  Session *session= current_session;
5588
 
  return (!(values= 
5589
 
            (Item_cache **) session->calloc(sizeof(Item_cache *)*item_count)));
5590
 
}
5591
 
 
5592
 
 
5593
 
bool Item_cache_row::setup(Item * item)
5594
 
{
5595
 
  example= item;
5596
 
  if (!values && allocate(item->cols()))
5597
 
    return 1;
5598
 
  for (uint32_t i= 0; i < item_count; i++)
5599
 
  {
5600
 
    Item *el= item->element_index(i);
5601
 
    Item_cache *tmp;
5602
 
    if (!(tmp= values[i]= Item_cache::get_cache(el)))
5603
 
      return 1;
5604
 
    tmp->setup(el);
5605
 
  }
5606
 
  return 0;
5607
 
}
5608
 
 
5609
 
 
5610
 
void Item_cache_row::store(Item * item)
5611
 
{
5612
 
  null_value= 0;
5613
 
  item->bring_value();
5614
 
  for (uint32_t i= 0; i < item_count; i++)
5615
 
  {
5616
 
    values[i]->store(item->element_index(i));
5617
 
    null_value|= values[i]->null_value;
5618
 
  }
5619
 
}
5620
 
 
5621
 
 
5622
 
void Item_cache_row::illegal_method_call(const char *)
5623
 
{
5624
 
  assert(0);
5625
 
  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
5626
 
  return;
5627
 
}
5628
 
 
5629
 
 
5630
 
bool Item_cache_row::check_cols(uint32_t c)
5631
 
{
5632
 
  if (c != item_count)
5633
 
  {
5634
 
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
5635
 
    return 1;
5636
 
  }
5637
 
  return 0;
5638
 
}
5639
 
 
5640
 
 
5641
 
bool Item_cache_row::null_inside()
5642
 
{
5643
 
  for (uint32_t i= 0; i < item_count; i++)
5644
 
  {
5645
 
    if (values[i]->cols() > 1)
5646
 
    {
5647
 
      if (values[i]->null_inside())
5648
 
        return 1;
5649
 
    }
5650
 
    else
5651
 
    {
5652
 
      values[i]->update_null_value();
5653
 
      if (values[i]->null_value)
5654
 
        return 1;
5655
 
    }
5656
 
  }
5657
 
  return 0;
5658
 
}
5659
 
 
5660
 
 
5661
 
void Item_cache_row::bring_value()
5662
 
{
5663
 
  for (uint32_t i= 0; i < item_count; i++)
5664
 
    values[i]->bring_value();
5665
 
  return;
5666
 
}
5667
 
 
5668
 
 
5669
 
Item_type_holder::Item_type_holder(Session *session, Item *item)
5670
 
  :Item(session, item), enum_set_typelib(0), fld_type(get_real_type(item))
5671
 
{
5672
 
  assert(item->fixed);
5673
 
  maybe_null= item->maybe_null;
5674
 
  collation.set(item->collation);
5675
 
  get_full_info(item);
5676
 
  /* fix variable decimals which always is NOT_FIXED_DEC */
5677
 
  if (Field::result_merge_type(fld_type) == INT_RESULT)
5678
 
    decimals= 0;
5679
 
  prev_decimal_int_part= item->decimal_int_part();
5680
 
}
5681
 
 
5682
 
 
5683
 
/**
5684
 
  Return expression type of Item_type_holder.
5685
 
 
5686
 
  @return
5687
 
    Item_result (type of internal MySQL expression result)
5688
 
*/
5689
 
 
5690
 
Item_result Item_type_holder::result_type() const
5691
 
{
5692
 
  return Field::result_merge_type(fld_type);
5693
 
}
5694
 
 
5695
 
 
5696
 
/**
5697
 
  Find real field type of item.
5698
 
 
5699
 
  @return
5700
 
    type of field which should be created to store item value
5701
 
*/
5702
 
 
5703
 
enum_field_types Item_type_holder::get_real_type(Item *item)
5704
 
{
5705
 
  switch(item->type())
5706
 
  {
5707
 
  case FIELD_ITEM:
5708
 
  {
5709
 
    /*
5710
 
      Item_fields::field_type ask Field_type() but sometimes field return
5711
 
      a different type, like for enum/set, so we need to ask real type.
5712
 
    */
5713
 
    Field *field= ((Item_field *) item)->field;
5714
 
    enum_field_types type= field->real_type();
5715
 
    if (field->is_created_from_null_item)
5716
 
      return DRIZZLE_TYPE_NULL;
5717
 
    return type;
5718
 
  }
5719
 
  case SUM_FUNC_ITEM:
5720
 
  {
5721
 
    /*
5722
 
      Argument of aggregate function sometimes should be asked about field
5723
 
      type
5724
 
    */
5725
 
    Item_sum *item_sum= (Item_sum *) item;
5726
 
    if (item_sum->keep_field_type())
5727
 
      return get_real_type(item_sum->args[0]);
5728
 
    break;
5729
 
  }
5730
 
  case FUNC_ITEM:
5731
 
    if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC)
5732
 
    {
5733
 
      /*
5734
 
        There are work around of problem with changing variable type on the
5735
 
        fly and variable always report "string" as field type to get
5736
 
        acceptable information for client in send_field, so we make field
5737
 
        type from expression type.
5738
 
      */
5739
 
      switch (item->result_type()) {
5740
 
      case STRING_RESULT:
5741
 
        return DRIZZLE_TYPE_VARCHAR;
5742
 
      case INT_RESULT:
5743
 
        return DRIZZLE_TYPE_LONGLONG;
5744
 
      case REAL_RESULT:
5745
 
        return DRIZZLE_TYPE_DOUBLE;
5746
 
      case DECIMAL_RESULT:
5747
 
        return DRIZZLE_TYPE_NEWDECIMAL;
5748
 
      case ROW_RESULT:
5749
 
      default:
5750
 
        assert(0);
5751
 
        return DRIZZLE_TYPE_VARCHAR;
5752
 
      }
5753
 
    }
5754
 
    break;
5755
 
  default:
5756
 
    break;
5757
 
  }
5758
 
  return item->field_type();
5759
 
}
5760
 
 
5761
 
/**
5762
 
  Find field type which can carry current Item_type_holder type and
5763
 
  type of given Item.
5764
 
 
5765
 
  @param session     thread handler
5766
 
  @param item    given item to join its parameters with this item ones
5767
 
 
5768
 
  @retval
5769
 
    true   error - types are incompatible
5770
 
  @retval
5771
 
    false  OK
5772
 
*/
5773
 
 
5774
 
bool Item_type_holder::join_types(Session *, Item *item)
5775
 
{
5776
 
  uint32_t max_length_orig= max_length;
5777
 
  uint32_t decimals_orig= decimals;
5778
 
  fld_type= Field::field_type_merge(fld_type, get_real_type(item));
5779
 
  {
5780
 
    int item_decimals= item->decimals;
5781
 
    /* fix variable decimals which always is NOT_FIXED_DEC */
5782
 
    if (Field::result_merge_type(fld_type) == INT_RESULT)
5783
 
      item_decimals= 0;
5784
 
    decimals= cmax((int)decimals, item_decimals);
5785
 
  }
5786
 
  if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
5787
 
  {
5788
 
    decimals= cmin((int)cmax(decimals, item->decimals), DECIMAL_MAX_SCALE);
5789
 
    int precision= cmin(cmax(prev_decimal_int_part, item->decimal_int_part())
5790
 
                       + decimals, DECIMAL_MAX_PRECISION);
5791
 
    unsigned_flag&= item->unsigned_flag;
5792
 
    max_length= my_decimal_precision_to_length(precision, decimals,
5793
 
                                               unsigned_flag);
5794
 
  }
5795
 
 
5796
 
  switch (Field::result_merge_type(fld_type))
5797
 
  {
5798
 
  case STRING_RESULT:
5799
 
  {
5800
 
    const char *old_cs, *old_derivation;
5801
 
    uint32_t old_max_chars= max_length / collation.collation->mbmaxlen;
5802
 
    old_cs= collation.collation->name;
5803
 
    old_derivation= collation.derivation_name();
5804
 
    if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
5805
 
    {
5806
 
      my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
5807
 
               old_cs, old_derivation,
5808
 
               item->collation.collation->name,
5809
 
               item->collation.derivation_name(),
5810
 
               "UNION");
5811
 
      return(true);
5812
 
    }
5813
 
    /*
5814
 
      To figure out max_length, we have to take into account possible
5815
 
      expansion of the size of the values because of character set
5816
 
      conversions.
5817
 
     */
5818
 
    if (collation.collation != &my_charset_bin)
5819
 
    {
5820
 
      max_length= cmax(old_max_chars * collation.collation->mbmaxlen,
5821
 
                      display_length(item) /
5822
 
                      item->collation.collation->mbmaxlen *
5823
 
                      collation.collation->mbmaxlen);
5824
 
    }
5825
 
    else
5826
 
      set_if_bigger(max_length, display_length(item));
5827
 
    break;
5828
 
  }
5829
 
  case REAL_RESULT:
5830
 
  {
5831
 
    if (decimals != NOT_FIXED_DEC)
5832
 
    {
5833
 
      int delta1= max_length_orig - decimals_orig;
5834
 
      int delta2= item->max_length - item->decimals;
5835
 
      max_length= cmax(delta1, delta2) + decimals;
5836
 
      if (fld_type == DRIZZLE_TYPE_DOUBLE && max_length > DBL_DIG + 2) 
5837
 
      {
5838
 
        max_length= DBL_DIG + 7;
5839
 
        decimals= NOT_FIXED_DEC;
5840
 
      }
5841
 
    }
5842
 
    else
5843
 
      max_length= DBL_DIG+7;
5844
 
    break;
5845
 
  }
5846
 
  default:
5847
 
    max_length= cmax(max_length, display_length(item));
5848
 
  };
5849
 
  maybe_null|= item->maybe_null;
5850
 
  get_full_info(item);
5851
 
 
5852
 
  /* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
5853
 
  prev_decimal_int_part= decimal_int_part();
5854
 
  return(false);
5855
 
}
5856
 
 
5857
 
/**
5858
 
  Calculate lenth for merging result for given Item type.
5859
 
 
5860
 
  @param item  Item for length detection
5861
 
 
5862
 
  @return
5863
 
    length
5864
 
*/
5865
 
 
5866
 
uint32_t Item_type_holder::display_length(Item *item)
5867
 
{
5868
 
  if (item->type() == Item::FIELD_ITEM)
5869
 
    return ((Item_field *)item)->max_disp_length();
5870
 
 
5871
 
  switch (item->field_type())
5872
 
  {
5873
 
  case DRIZZLE_TYPE_TIMESTAMP:
5874
 
  case DRIZZLE_TYPE_TIME:
5875
 
  case DRIZZLE_TYPE_DATETIME:
5876
 
  case DRIZZLE_TYPE_DATE:
5877
 
  case DRIZZLE_TYPE_VARCHAR:
5878
 
  case DRIZZLE_TYPE_NEWDECIMAL:
5879
 
  case DRIZZLE_TYPE_ENUM:
5880
 
  case DRIZZLE_TYPE_BLOB:
5881
 
    return 4;
5882
 
  case DRIZZLE_TYPE_LONG:
5883
 
    return MY_INT32_NUM_DECIMAL_DIGITS;
5884
 
  case DRIZZLE_TYPE_DOUBLE:
5885
 
    return 53;
5886
 
  case DRIZZLE_TYPE_NULL:
5887
 
    return 0;
5888
 
  case DRIZZLE_TYPE_LONGLONG:
5889
 
    return 20;
5890
 
  default:
5891
 
    assert(0); // we should never go there
5892
 
    return 0;
5893
 
  }
5894
 
}
5895
 
 
5896
 
 
5897
 
/**
5898
 
  Make temporary table field according collected information about type
5899
 
  of UNION result.
5900
 
 
5901
 
  @param table  temporary table for which we create fields
5902
 
 
5903
 
  @return
5904
 
    created field
5905
 
*/
5906
 
 
5907
 
Field *Item_type_holder::make_field_by_type(Table *table)
5908
 
{
5909
 
  /*
5910
 
    The field functions defines a field to be not null if null_ptr is not 0
5911
 
  */
5912
 
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
5913
 
  Field *field;
5914
 
 
5915
 
  switch (fld_type) {
5916
 
  case DRIZZLE_TYPE_ENUM:
5917
 
    assert(enum_set_typelib);
5918
 
    field= new Field_enum((unsigned char *) 0, max_length, null_ptr, 0,
5919
 
                          Field::NONE, name,
5920
 
                          get_enum_pack_length(enum_set_typelib->count),
5921
 
                          enum_set_typelib, collation.collation);
5922
 
    if (field)
5923
 
      field->init(table);
5924
 
    return field;
5925
 
  case DRIZZLE_TYPE_NULL:
5926
 
    return make_string_field(table);
5927
 
  default:
5928
 
    break;
5929
 
  }
5930
 
  return tmp_table_field_from_field_type(table, 0);
5931
 
}
5932
 
 
5933
 
 
5934
 
/**
5935
 
  Get full information from Item about enum/set fields to be able to create
5936
 
  them later.
5937
 
 
5938
 
  @param item    Item for information collection
5939
 
*/
5940
 
void Item_type_holder::get_full_info(Item *item)
5941
 
{
5942
 
  if (fld_type == DRIZZLE_TYPE_ENUM)
5943
 
  {
5944
 
    if (item->type() == Item::SUM_FUNC_ITEM &&
5945
 
        (((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
5946
 
         ((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC))
5947
 
      item = ((Item_sum*)item)->args[0];
5948
 
    /*
5949
 
      We can have enum/set type after merging only if we have one enum|set
5950
 
      field (or MIN|MAX(enum|set field)) and number of NULL fields
5951
 
    */
5952
 
    assert((enum_set_typelib &&
5953
 
                 get_real_type(item) == DRIZZLE_TYPE_NULL) ||
5954
 
                (!enum_set_typelib &&
5955
 
                 item->type() == Item::FIELD_ITEM &&
5956
 
                 (get_real_type(item) == DRIZZLE_TYPE_ENUM) &&
5957
 
                 ((Field_enum*)((Item_field *) item)->field)->typelib));
5958
 
    if (!enum_set_typelib)
5959
 
    {
5960
 
      enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib;
5961
 
    }
5962
 
  }
5963
 
}
5964
 
 
5965
 
 
5966
 
double Item_type_holder::val_real()
5967
 
{
5968
 
  assert(0); // should never be called
5969
 
  return 0.0;
5970
 
}
5971
 
 
5972
 
 
5973
 
int64_t Item_type_holder::val_int()
5974
 
{
5975
 
  assert(0); // should never be called
5976
 
  return 0;
5977
 
}
5978
 
 
5979
 
my_decimal *Item_type_holder::val_decimal(my_decimal *)
5980
 
{
5981
 
  assert(0); // should never be called
5982
 
  return 0;
5983
 
}
5984
 
 
5985
 
String *Item_type_holder::val_str(String*)
5986
 
{
5987
 
  assert(0); // should never be called
5988
 
  return 0;
5989
 
}
5990
 
 
5991
 
void Item_result_field::cleanup()
5992
 
{
5993
 
  Item::cleanup();
5994
 
  result_field= 0;
5995
 
  return;
5996
 
}
5997
 
 
5998
5972
/**
5999
5973
  Dummy error processor used by default by Name_resolution_context.
6000
5974