~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2010-07-30 20:31:19 UTC
  • mto: This revision was merged to the branch mainline in revision 1679.
  • Revision ID: brian@gaz-20100730203119-89g2ye4zwnvcacxg
First pass in encapsulating row

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
34
34
 
35
35
#include "drizzled/field/str.h"
36
36
#include "drizzled/field/num.h"
37
 
 
38
37
#include "drizzled/field/blob.h"
 
38
#include "drizzled/field/enum.h"
 
39
#include "drizzled/field/null.h"
39
40
#include "drizzled/field/date.h"
40
 
#include "drizzled/field/datetime.h"
41
41
#include "drizzled/field/decimal.h"
 
42
#include "drizzled/field/real.h"
42
43
#include "drizzled/field/double.h"
43
 
#include "drizzled/field/enum.h"
44
 
#include "drizzled/field/epoch.h"
45
 
#include "drizzled/field/int32.h"
46
 
#include "drizzled/field/int64.h"
47
 
#include "drizzled/field/microtime.h"
48
 
#include "drizzled/field/null.h"
49
 
#include "drizzled/field/real.h"
50
 
#include "drizzled/field/size.h"
51
 
#include "drizzled/field/time.h"
 
44
#include "drizzled/field/long.h"
 
45
#include "drizzled/field/int64_t.h"
 
46
#include "drizzled/field/num.h"
 
47
#include "drizzled/field/timestamp.h"
 
48
#include "drizzled/field/datetime.h"
52
49
#include "drizzled/field/varstring.h"
53
 
 
54
50
#include "drizzled/internal/m_string.h"
55
51
 
56
52
#include <cstdio>
96
92
  {
97
93
    case INT_RESULT:
98
94
      return val_int() != 0;
99
 
 
100
95
    case DECIMAL_RESULT:
101
96
    {
102
 
      type::Decimal decimal_value;
103
 
      type::Decimal *val= val_decimal(&decimal_value);
 
97
      my_decimal decimal_value;
 
98
      my_decimal *val= val_decimal(&decimal_value);
104
99
      if (val)
105
 
        return not val->isZero();
 
100
        return !my_decimal_is_zero(val);
106
101
      return false;
107
102
    }
108
 
 
109
103
    case REAL_RESULT:
110
104
    case STRING_RESULT:
111
105
      return val_real() != 0.0;
112
 
 
113
106
    case ROW_RESULT:
 
107
    default:
114
108
      assert(0);
115
 
      abort();
 
109
      return false;
116
110
  }
117
 
 
118
 
  assert(0);
119
 
  abort();
120
111
}
121
112
 
122
113
String *Item::val_string_from_real(String *str)
141
132
 
142
133
String *Item::val_string_from_decimal(String *str)
143
134
{
144
 
  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
 
135
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
145
136
  if (null_value)
146
137
    return NULL;
147
138
 
148
 
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
149
 
  class_decimal2string(&dec_buf, 0, str);
 
139
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
140
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
150
141
  return str;
151
142
}
152
143
 
153
 
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
 
144
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
154
145
{
155
146
  double nr= val_real();
156
147
  if (null_value)
157
148
    return NULL;
158
149
 
159
 
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
150
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
160
151
  return (decimal_value);
161
152
}
162
153
 
163
 
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
 
154
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
164
155
{
165
156
  int64_t nr= val_int();
166
157
  if (null_value)
167
158
    return NULL;
168
159
 
169
 
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
160
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
170
161
  return decimal_value;
171
162
}
172
163
 
173
 
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
 
164
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
174
165
{
175
166
  String *res;
176
167
  char *end_ptr;
178
169
    return NULL;
179
170
 
180
171
  end_ptr= (char*) res->ptr()+ res->length();
181
 
  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
 
172
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
182
173
                     res->ptr(), 
183
174
                     res->length(), 
184
 
                     res->charset()) & E_DEC_BAD_NUM)
 
175
                     res->charset(),
 
176
                     decimal_value) & E_DEC_BAD_NUM)
185
177
  {
186
 
    push_warning_printf(&getSession(), 
 
178
    push_warning_printf(current_session, 
187
179
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
188
180
                        ER_TRUNCATED_WRONG_VALUE,
189
181
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
192
184
  return decimal_value;
193
185
}
194
186
 
195
 
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
 
187
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
196
188
{
197
189
  assert(fixed);
198
 
  type::Time ltime;
199
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
190
  DRIZZLE_TIME ltime;
 
191
  if (get_date(&ltime, TIME_FUZZY_DATE))
200
192
  {
201
 
    decimal_value->set_zero();
 
193
    my_decimal_set_zero(decimal_value);
202
194
    null_value= 1;                               // set NULL, stop processing
203
195
    return NULL;
204
196
  }
205
 
  return date2_class_decimal(&ltime, decimal_value);
 
197
  return date2my_decimal(&ltime, decimal_value);
206
198
}
207
199
 
208
 
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
 
200
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
209
201
{
210
202
  assert(fixed);
211
 
  type::Time ltime;
212
 
  if (get_time(ltime))
 
203
  DRIZZLE_TIME ltime;
 
204
  if (get_time(&ltime))
213
205
  {
214
 
    decimal_value->set_zero();
 
206
    my_decimal_set_zero(decimal_value);
215
207
    return NULL;
216
208
  }
217
 
  return date2_class_decimal(&ltime, decimal_value);
 
209
  return date2my_decimal(&ltime, decimal_value);
218
210
}
219
211
 
220
212
double Item::val_real_from_decimal()
221
213
{
222
214
  /* Note that fix_fields may not be called for Item_avg_field items */
223
215
  double result;
224
 
  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
 
216
  my_decimal value_buff, *dec_val= val_decimal(&value_buff);
225
217
  if (null_value)
226
218
    return 0.0;
227
 
  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
 
219
  my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
228
220
  return result;
229
221
}
230
222
 
232
224
{
233
225
  /* Note that fix_fields may not be called for Item_avg_field items */
234
226
  int64_t result;
235
 
  type::Decimal value, *dec_val= val_decimal(&value);
236
 
 
 
227
  my_decimal value, *dec_val= val_decimal(&value);
237
228
  if (null_value)
238
229
    return 0;
239
 
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
240
 
 
 
230
  my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
241
231
  return result;
242
232
}
243
233
 
244
 
bool Item::save_time_in_field(Field *field)
 
234
int Item::save_time_in_field(Field *field)
245
235
{
246
 
  type::Time ltime;
247
 
 
248
 
  if (get_time(ltime))
 
236
  DRIZZLE_TIME ltime;
 
237
  if (get_time(&ltime))
249
238
    return set_field_to_null(field);
250
 
 
251
239
  field->set_notnull();
252
 
 
253
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
 
240
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
254
241
}
255
242
 
256
 
bool Item::save_date_in_field(Field *field)
 
243
int Item::save_date_in_field(Field *field)
257
244
{
258
 
  type::Time ltime;
259
 
 
260
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
245
  DRIZZLE_TIME ltime;
 
246
  if (get_date(&ltime, TIME_FUZZY_DATE))
261
247
    return set_field_to_null(field);
262
 
 
263
248
  field->set_notnull();
264
 
 
265
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
 
249
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
266
250
}
267
251
 
268
252
/**
273
257
{
274
258
  if (null_value)
275
259
    return set_field_to_null(field);
276
 
 
277
260
  field->set_notnull();
278
 
 
279
261
  return field->store(result->ptr(), result->length(), collation.collation);
280
262
}
281
263
 
294
276
  with_sum_func(false),
295
277
  is_autogenerated_name(true),
296
278
  with_subselect(false),
297
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
298
 
  _session(*current_session)
 
279
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
299
280
{
300
281
  cmp_context= (Item_result)-1;
301
282
 
302
283
  /* Put item in free list so that we can free all items at end */
303
 
  next= getSession().free_list;
304
 
  getSession().free_list= this;
 
284
  Session *session= current_session;
 
285
  next= session->free_list;
 
286
  session->free_list= this;
305
287
 
306
288
  /*
307
289
    Item constructor can be called during execution other then SQL_COM
308
290
    command => we should check session->lex->current_select on zero (session->lex
309
291
    can be uninitialised)
310
292
  */
311
 
  if (getSession().lex->current_select)
 
293
  if (session->lex->current_select)
312
294
  {
313
 
    enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
 
295
    enum_parsing_place place= session->lex->current_select->parsing_place;
314
296
    if (place == SELECT_LIST || place == IN_HAVING)
315
 
      getSession().getLex()->current_select->select_n_having_items++;
 
297
      session->lex->current_select->select_n_having_items++;
316
298
  }
317
299
}
318
300
 
333
315
  is_autogenerated_name(item->is_autogenerated_name),
334
316
  with_subselect(item->with_subselect),
335
317
  collation(item->collation),
336
 
  cmp_context(item->cmp_context),
337
 
  _session(*session)
 
318
  cmp_context(item->cmp_context)
338
319
{
339
320
  /* Put this item in the session's free list */
340
 
  next= getSession().free_list;
341
 
  getSession().free_list= this;
 
321
  next= session->free_list;
 
322
  session->free_list= this;
342
323
}
343
324
 
344
325
uint32_t Item::float_length(uint32_t decimals_par) const
351
332
  Item_result restype= result_type();
352
333
 
353
334
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
354
 
    return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
335
    return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
355
336
               (uint32_t) DECIMAL_MAX_PRECISION);
356
337
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
357
338
}
358
339
 
359
340
int Item::decimal_int_part() const
360
341
{
361
 
  return class_decimal_int_part(decimal_precision(), decimals);
 
342
  return my_decimal_int_part(decimal_precision(), decimals);
362
343
}
363
344
 
364
345
void Item::print(String *str, enum_query_type)
436
417
    if (orig_len != length && ! is_autogenerated_name)
437
418
    {
438
419
      if (length == 0)
439
 
        push_warning_printf(&getSession(), 
 
420
        push_warning_printf(current_session, 
440
421
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
441
422
                            ER_NAME_BECOMES_EMPTY, 
442
423
                            ER(ER_NAME_BECOMES_EMPTY),
443
424
                            str + length - orig_len);
444
425
      else
445
 
        push_warning_printf(&getSession(),
 
426
        push_warning_printf(current_session, 
446
427
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
447
428
                            ER_REMOVED_SPACES, 
448
429
                            ER(ER_REMOVED_SPACES),
471
452
  return conv->safe ? conv : NULL;
472
453
}
473
454
 
474
 
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
 
455
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
475
456
{
476
 
  do
477
 
  {
478
 
    if (is_null())
479
 
    {
480
 
      break;
481
 
    }
482
 
    else if (result_type() == STRING_RESULT)
483
 
    {
484
 
      char buff[type::Time::MAX_STRING_LENGTH];
485
 
      String tmp(buff,sizeof(buff), &my_charset_bin),*res;
486
 
      if (!(res=val_str(&tmp)) ||
487
 
          str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
488
 
                                    &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
489
 
      {
490
 
        break;
491
 
      }
492
 
    }
493
 
    else
494
 
    {
495
 
      int64_t value= val_int();
496
 
      type::datetime_t date_value;
497
 
 
498
 
      ltime.convert(date_value, value, fuzzydate);
499
 
 
500
 
      if (not type::is_valid(date_value))
501
 
      {
502
 
        char buff[DECIMAL_LONGLONG_DIGITS], *end;
503
 
        end= internal::int64_t10_to_str(value, buff, -10);
504
 
        make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
505
 
                                     buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
506
 
        break;
507
 
      }
508
 
    }
509
 
 
510
 
    return false;
511
 
  } while (0);
512
 
 
513
 
  ltime.reset();
514
 
 
 
457
  if (result_type() == STRING_RESULT)
 
458
  {
 
459
    char buff[40];
 
460
    String tmp(buff,sizeof(buff), &my_charset_bin),*res;
 
461
    if (!(res=val_str(&tmp)) ||
 
462
        str_to_datetime_with_warn(res->ptr(), res->length(),
 
463
                                  ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
464
      goto err;
 
465
  }
 
466
  else
 
467
  {
 
468
    int64_t value= val_int();
 
469
    int was_cut;
 
470
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
 
471
    {
 
472
      char buff[22], *end;
 
473
      end= internal::int64_t10_to_str(value, buff, -10);
 
474
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
475
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
 
476
                                   NULL);
 
477
      goto err;
 
478
    }
 
479
  }
 
480
  return false;
 
481
 
 
482
err:
 
483
  memset(ltime, 0, sizeof(*ltime));
515
484
  return true;
516
485
}
517
486
 
518
 
bool Item::get_time(type::Time &ltime)
 
487
bool Item::get_time(DRIZZLE_TIME *ltime)
519
488
{
520
 
  char buff[type::Time::MAX_STRING_LENGTH];
 
489
  char buff[40];
521
490
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
522
 
  if (!(res=val_str(&tmp)) or
523
 
      str_to_time_with_warn(&getSession(), res->ptr(), res->length(), &ltime))
 
491
  if (!(res=val_str(&tmp)) ||
 
492
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
524
493
  {
525
 
    ltime.reset();
526
 
 
 
494
    memset(ltime, 0, sizeof(*ltime));
527
495
    return true;
528
496
  }
529
 
 
530
497
  return false;
531
498
}
532
499
 
533
 
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
 
500
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
534
501
{
535
 
  return get_date(ltime, fuzzydate);
 
502
  return get_date(ltime,fuzzydate);
536
503
}
537
504
 
538
505
bool Item::is_null()
849
816
  {
850
817
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
851
818
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
852
 
    prev_subselect_item->const_item_cache= false;
 
819
    prev_subselect_item->const_item_cache= 0;
853
820
  }
854
821
  {
855
822
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
864
831
    }
865
832
    else
866
833
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
867
 
    prev_subselect_item->const_item_cache= false;
 
834
    prev_subselect_item->const_item_cache= 0;
868
835
    mark_as_dependent(session, last_select, current_sel, resolved_item,
869
836
                      dependent);
870
837
  }
884
851
    - the found item on success
885
852
    - NULL if find_item is not in group_list
886
853
*/
887
 
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
 
854
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
888
855
{
889
856
  const char *db_name;
890
857
  const char *table_name;
891
858
  const char *field_name;
892
 
  Order *found_group= NULL;
 
859
  order_st *found_group= NULL;
893
860
  int found_match_degree= 0;
894
861
  Item_ident *cur_field;
895
862
  int cur_match_degree= 0;
915
882
 
916
883
  assert(field_name != 0);
917
884
 
918
 
  for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
885
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
919
886
  {
920
887
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
921
888
    {
940
907
        if (cur_field->db_name && db_name)
941
908
        {
942
909
          /* If field_name is also qualified by a database name. */
943
 
          if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
944
 
          {
 
910
          if (strcasecmp(cur_field->db_name, db_name))
945
911
            /* Same field names, different databases. */
946
912
            return NULL;
947
 
          }
948
913
          ++cur_match_degree;
949
914
        }
950
915
      }
963
928
          best match, they must reference the same column, otherwise the field
964
929
          is ambiguous.
965
930
        */
966
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
 
931
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), current_session->where);
967
932
        return NULL;
968
933
      }
969
934
    }
971
936
 
972
937
  if (found_group)
973
938
    return found_group->item;
974
 
 
975
 
  return NULL;
 
939
  else
 
940
    return NULL;
976
941
}
977
942
 
978
943
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
979
944
{
980
945
  Item **group_by_ref= NULL;
981
946
  Item **select_ref= NULL;
982
 
  Order *group_list= (Order*) select->group_list.first;
 
947
  order_st *group_list= (order_st*) select->group_list.first;
983
948
  bool ambiguous_fields= false;
984
949
  uint32_t counter;
985
950
  enum_resolution_type resolution;
999
964
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
1000
965
  if (select->having_fix_field && !ref->with_sum_func && group_list)
1001
966
  {
1002
 
    group_by_ref= find_field_in_group_list(session, ref, group_list);
 
967
    group_by_ref= find_field_in_group_list(ref, group_list);
1003
968
 
1004
969
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
1005
970
    if (group_by_ref && (select_ref != not_found_item) &&
1008
973
      ambiguous_fields= true;
1009
974
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
1010
975
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
1011
 
                          session->where());
 
976
                          current_session->where);
1012
977
 
1013
978
    }
1014
979
  }
1037
1002
}
1038
1003
 
1039
1004
void Item::init_make_field(SendField *tmp_field,
1040
 
                           enum enum_field_types field_type_arg)
 
1005
                           enum enum_field_types field_type_arg)
1041
1006
{
1042
1007
  char *empty_name= (char*) "";
1043
1008
  tmp_field->db_name=   empty_name;
1079
1044
  case REAL_RESULT:    
1080
1045
    return DRIZZLE_TYPE_DOUBLE;
1081
1046
  case ROW_RESULT:
 
1047
  default:
1082
1048
    assert(0);
 
1049
    return DRIZZLE_TYPE_VARCHAR;
1083
1050
  }
1084
 
 
1085
 
  abort();
1086
1051
}
1087
1052
 
1088
1053
bool Item::is_datetime()
1089
1054
{
1090
1055
  switch (field_type())
1091
1056
  {
1092
 
    case DRIZZLE_TYPE_TIME:
1093
1057
    case DRIZZLE_TYPE_DATE:
1094
1058
    case DRIZZLE_TYPE_DATETIME:
1095
1059
    case DRIZZLE_TYPE_TIMESTAMP:
1096
 
    case DRIZZLE_TYPE_MICROTIME:
1097
1060
      return true;
1098
 
    case DRIZZLE_TYPE_BLOB:
1099
 
    case DRIZZLE_TYPE_VARCHAR:
1100
 
    case DRIZZLE_TYPE_DOUBLE:
1101
 
    case DRIZZLE_TYPE_DECIMAL:
1102
 
    case DRIZZLE_TYPE_ENUM:
1103
 
    case DRIZZLE_TYPE_LONG:
1104
 
    case DRIZZLE_TYPE_LONGLONG:
1105
 
    case DRIZZLE_TYPE_NULL:
1106
 
    case DRIZZLE_TYPE_UUID:
1107
 
    case DRIZZLE_TYPE_BOOLEAN:
1108
 
      return false;
 
1061
    default:
 
1062
      break;
1109
1063
  }
1110
 
 
1111
 
  assert(0);
1112
 
  abort();
 
1064
  return false;
1113
1065
}
1114
1066
 
1115
1067
String *Item::check_well_formed_result(String *str, bool send_error)
1122
1074
                                       str->length(), &well_formed_error);
1123
1075
  if (wlen < str->length())
1124
1076
  {
 
1077
    Session *session= current_session;
1125
1078
    char hexbuf[7];
1126
1079
    enum DRIZZLE_ERROR::enum_warning_level level;
1127
1080
    uint32_t diff= str->length() - wlen;
1138
1091
      null_value= 1;
1139
1092
      str= 0;
1140
1093
    }
1141
 
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
 
1094
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1142
1095
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1143
1096
  }
1144
1097
  return str;
1171
1124
  Field *field;
1172
1125
  assert(collation.collation);
1173
1126
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1174
 
  {
1175
1127
    field= new Field_blob(max_length, maybe_null, name,
1176
1128
                          collation.collation);
1177
 
  }
1178
1129
  else
1179
 
  {
1180
 
    table->setVariableWidth();
1181
 
    field= new Field_varstring(max_length, maybe_null, name,
 
1130
    field= new Field_varstring(max_length, maybe_null, name, table->getMutableShare(),
1182
1131
                               collation.collation);
1183
 
  }
1184
1132
 
1185
1133
  if (field)
1186
1134
    field->init(table);
1193
1141
    The field functions defines a field to be not null if null_ptr is not 0
1194
1142
  */
1195
1143
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1196
 
  Field *field= NULL;
 
1144
  Field *field;
1197
1145
 
1198
1146
  switch (field_type()) {
1199
1147
  case DRIZZLE_TYPE_DECIMAL:
1203
1151
                                 0,
1204
1152
                                 Field::NONE,
1205
1153
                                 name,
1206
 
                                 decimals);
 
1154
                                 decimals,
 
1155
                                 0,
 
1156
                                 unsigned_flag);
1207
1157
    break;
1208
1158
  case DRIZZLE_TYPE_LONG:
1209
 
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1159
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1160
                          name, 0, unsigned_flag);
1210
1161
    break;
1211
1162
  case DRIZZLE_TYPE_LONGLONG:
1212
 
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1163
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1164
                              name, 0, unsigned_flag);
1213
1165
    break;
1214
1166
  case DRIZZLE_TYPE_DOUBLE:
1215
1167
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1216
 
                            name, decimals, 0, unsigned_flag);
 
1168
                            name, decimals, 0, unsigned_flag);
1217
1169
    break;
1218
1170
  case DRIZZLE_TYPE_NULL:
1219
1171
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1221
1173
  case DRIZZLE_TYPE_DATE:
1222
1174
    field= new Field_date(maybe_null, name, &my_charset_bin);
1223
1175
    break;
1224
 
 
1225
 
  case DRIZZLE_TYPE_MICROTIME:
1226
 
    field= new field::Microtime(maybe_null, name);
1227
 
    break;
1228
 
 
1229
1176
  case DRIZZLE_TYPE_TIMESTAMP:
1230
 
    field= new field::Epoch(maybe_null, name);
 
1177
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1231
1178
    break;
1232
1179
  case DRIZZLE_TYPE_DATETIME:
1233
1180
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1234
1181
    break;
1235
 
  case DRIZZLE_TYPE_TIME:
1236
 
    field= new field::Time(maybe_null, name, &my_charset_bin);
1237
 
    break;
1238
 
  case DRIZZLE_TYPE_BOOLEAN:
1239
 
  case DRIZZLE_TYPE_UUID:
 
1182
  default:
 
1183
    /* This case should never be chosen */
 
1184
    assert(0);
 
1185
    /* Fall through to make_string_field() */
1240
1186
  case DRIZZLE_TYPE_ENUM:
1241
1187
  case DRIZZLE_TYPE_VARCHAR:
1242
1188
    return make_string_field(table);
1243
1189
  case DRIZZLE_TYPE_BLOB:
 
1190
    if (this->type() == Item::TYPE_HOLDER)
 
1191
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
 
1192
                            1);
 
1193
    else
1244
1194
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1245
1195
    break;                                      // Blob handled outside of case
1246
1196
  }
1247
 
  assert(field);
1248
 
 
1249
1197
  if (field)
1250
1198
    field->init(table);
1251
1199
  return field;
1297
1245
  }
1298
1246
  else if (result_type() == DECIMAL_RESULT)
1299
1247
  {
1300
 
    type::Decimal decimal_value;
1301
 
    type::Decimal *value= val_decimal(&decimal_value);
 
1248
    my_decimal decimal_value;
 
1249
    my_decimal *value= val_decimal(&decimal_value);
1302
1250
    if (null_value)
1303
1251
      return set_field_to_null_with_conversions(field, no_conversions);
1304
1252
    field->set_notnull();
1380
1328
  enum_field_types f_type;
1381
1329
 
1382
1330
  switch ((f_type=field_type())) {
1383
 
  case DRIZZLE_TYPE_DATE:
 
1331
  default:
1384
1332
  case DRIZZLE_TYPE_NULL:
1385
1333
  case DRIZZLE_TYPE_ENUM:
1386
1334
  case DRIZZLE_TYPE_BLOB:
1387
1335
  case DRIZZLE_TYPE_VARCHAR:
1388
 
  case DRIZZLE_TYPE_BOOLEAN:
1389
 
  case DRIZZLE_TYPE_UUID:
1390
1336
  case DRIZZLE_TYPE_DECIMAL:
1391
 
    {
1392
 
      String *res;
1393
 
      if ((res=val_str(buffer)))
1394
 
        result= client->store(res->ptr(),res->length());
1395
 
      break;
1396
 
    }
 
1337
  {
 
1338
    String *res;
 
1339
    if ((res=val_str(buffer)))
 
1340
      result= client->store(res->ptr(),res->length());
 
1341
    break;
 
1342
  }
1397
1343
  case DRIZZLE_TYPE_LONG:
1398
 
    {
1399
 
      int64_t nr;
1400
 
      nr= val_int();
1401
 
      if (!null_value)
1402
 
        result= client->store((int32_t)nr);
1403
 
      break;
1404
 
    }
 
1344
  {
 
1345
    int64_t nr;
 
1346
    nr= val_int();
 
1347
    if (!null_value)
 
1348
      result= client->store((int32_t)nr);
 
1349
    break;
 
1350
  }
1405
1351
  case DRIZZLE_TYPE_LONGLONG:
 
1352
  {
 
1353
    int64_t nr;
 
1354
    nr= val_int();
 
1355
    if (!null_value)
1406
1356
    {
1407
 
      int64_t nr;
1408
 
      nr= val_int();
1409
 
      if (!null_value)
1410
 
      {
1411
 
        if (unsigned_flag)
1412
 
          result= client->store((uint64_t)nr);
1413
 
        else
1414
 
          result= client->store((int64_t)nr);
1415
 
      }
1416
 
      break;
 
1357
      if (unsigned_flag)
 
1358
        result= client->store((uint64_t)nr);
 
1359
      else
 
1360
        result= client->store((int64_t)nr);
1417
1361
    }
 
1362
    break;
 
1363
  }
1418
1364
  case DRIZZLE_TYPE_DOUBLE:
1419
 
    {
1420
 
      double nr= val_real();
1421
 
      if (!null_value)
1422
 
        result= client->store(nr, decimals, buffer);
1423
 
      break;
1424
 
    }
1425
 
  case DRIZZLE_TYPE_TIME:
1426
 
    {
1427
 
      type::Time tm;
1428
 
      get_time(tm);
1429
 
      if (not null_value)
1430
 
        result= client->store(&tm);
1431
 
      break;
1432
 
    }
 
1365
  {
 
1366
    double nr= val_real();
 
1367
    if (!null_value)
 
1368
      result= client->store(nr, decimals, buffer);
 
1369
    break;
 
1370
  }
1433
1371
  case DRIZZLE_TYPE_DATETIME:
1434
 
  case DRIZZLE_TYPE_MICROTIME:
1435
1372
  case DRIZZLE_TYPE_TIMESTAMP:
1436
 
    {
1437
 
      type::Time tm;
1438
 
      get_date(tm, TIME_FUZZY_DATE);
1439
 
      if (!null_value)
1440
 
        result= client->store(&tm);
1441
 
      break;
1442
 
    }
 
1373
  {
 
1374
    DRIZZLE_TIME tm;
 
1375
    get_date(&tm, TIME_FUZZY_DATE);
 
1376
    if (!null_value)
 
1377
      result= client->store(&tm);
 
1378
    break;
 
1379
  }
1443
1380
  }
1444
1381
  if (null_value)
1445
1382
    result= client->store();
1446
 
 
1447
1383
  return result;
1448
1384
}
1449
1385
 
1450
 
uint32_t Item::max_char_length() const
1451
 
{
1452
 
  return max_length / collation.collation->mbmaxlen;
1453
 
}
1454
 
 
1455
 
void Item::fix_length_and_charset(uint32_t max_char_length_arg, CHARSET_INFO *cs)
1456
 
1457
 
  max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1458
 
  collation.collation= cs;
1459
 
}
1460
 
 
1461
 
void Item::fix_char_length(uint32_t max_char_length_arg)
1462
 
1463
 
  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1464
 
}
1465
 
 
1466
 
void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
1467
 
1468
 
  uint64_t max_result_length= max_char_length_arg *
1469
 
    collation.collation->mbmaxlen;
1470
 
 
1471
 
  if (max_result_length >= MAX_BLOB_WIDTH)
1472
 
  { 
1473
 
    max_length= MAX_BLOB_WIDTH;
1474
 
    maybe_null= false;
1475
 
  }
1476
 
  else
1477
 
  {
1478
 
    max_length= max_result_length;
1479
 
  }
1480
 
}
1481
 
 
1482
 
void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
1483
 
1484
 
  collation.set(&my_charset_bin);
1485
 
  fix_char_length(max_char_length_arg);
1486
 
}
1487
 
 
1488
1386
Item_result item_cmp_type(Item_result a,Item_result b)
1489
1387
{
1490
1388
  if (a == STRING_RESULT && b == STRING_RESULT)
1491
1389
    return STRING_RESULT;
1492
 
 
1493
1390
  if (a == INT_RESULT && b == INT_RESULT)
1494
1391
    return INT_RESULT;
1495
1392
  else if (a == ROW_RESULT || b == ROW_RESULT)
1496
1393
    return ROW_RESULT;
1497
 
 
1498
1394
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1499
1395
      (b == INT_RESULT || b == DECIMAL_RESULT))
1500
1396
    return DECIMAL_RESULT;
1501
 
 
1502
1397
  return REAL_RESULT;
1503
1398
}
1504
1399
 
1514
1409
 
1515
1410
  switch (res_type) {
1516
1411
  case STRING_RESULT:
 
1412
  {
 
1413
    char buff[MAX_FIELD_WIDTH];
 
1414
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
 
1415
    result=item->val_str(&tmp);
 
1416
    if (item->null_value)
 
1417
      new_item= new Item_null(name);
 
1418
    else
1517
1419
    {
1518
 
      char buff[MAX_FIELD_WIDTH];
1519
 
      String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1520
 
      result=item->val_str(&tmp);
1521
 
      if (item->null_value)
1522
 
        new_item= new Item_null(name);
1523
 
      else
1524
 
      {
1525
 
        uint32_t length= result->length();
1526
 
        char *tmp_str= memory::sql_strmake(result->ptr(), length);
1527
 
        new_item= new Item_string(name, tmp_str, length, result->charset());
1528
 
      }
1529
 
      break;
 
1420
      uint32_t length= result->length();
 
1421
      char *tmp_str= memory::sql_strmake(result->ptr(), length);
 
1422
      new_item= new Item_string(name, tmp_str, length, result->charset());
1530
1423
    }
 
1424
    break;
 
1425
  }
1531
1426
  case INT_RESULT:
1532
 
    {
1533
 
      int64_t result=item->val_int();
1534
 
      uint32_t length=item->max_length;
1535
 
      bool null_value=item->null_value;
1536
 
      new_item= (null_value ? (Item*) new Item_null(name) :
1537
 
                 (Item*) new Item_int(name, result, length));
1538
 
      break;
1539
 
    }
 
1427
  {
 
1428
    int64_t result=item->val_int();
 
1429
    uint32_t length=item->max_length;
 
1430
    bool null_value=item->null_value;
 
1431
    new_item= (null_value ? (Item*) new Item_null(name) :
 
1432
               (Item*) new Item_int(name, result, length));
 
1433
    break;
 
1434
  }
1540
1435
  case ROW_RESULT:
1541
 
    if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1542
 
    {
1543
 
      /*
1544
 
        Substitute constants only in Item_rows. Don't affect other Items
1545
 
        with ROW_RESULT (eg Item_singlerow_subselect).
 
1436
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
 
1437
  {
 
1438
    /*
 
1439
      Substitute constants only in Item_rows. Don't affect other Items
 
1440
      with ROW_RESULT (eg Item_singlerow_subselect).
1546
1441
 
1547
 
        For such Items more optimal is to detect if it is constant and replace
1548
 
        it with Item_row. This would optimize queries like this:
1549
 
        SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1550
 
      */
1551
 
      Item_row *item_row= (Item_row*) item;
1552
 
      Item_row *comp_item_row= (Item_row*) comp_item;
1553
 
      uint32_t col;
1554
 
      new_item= 0;
1555
 
      /*
1556
 
        If item and comp_item are both Item_rows and have same number of cols
1557
 
        then process items in Item_row one by one.
1558
 
        We can't ignore NULL values here as this item may be used with <=>, in
1559
 
        which case NULL's are significant.
1560
 
      */
1561
 
      assert(item->result_type() == comp_item->result_type());
1562
 
      assert(item_row->cols() == comp_item_row->cols());
1563
 
      col= item_row->cols();
1564
 
      while (col-- > 0)
1565
 
        resolve_const_item(session, item_row->addr(col),
1566
 
                           comp_item_row->element_index(col));
1567
 
      break;
1568
 
    }
1569
 
    /* Fallthrough */
 
1442
      For such Items more optimal is to detect if it is constant and replace
 
1443
      it with Item_row. This would optimize queries like this:
 
1444
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
 
1445
    */
 
1446
    Item_row *item_row= (Item_row*) item;
 
1447
    Item_row *comp_item_row= (Item_row*) comp_item;
 
1448
    uint32_t col;
 
1449
    new_item= 0;
 
1450
    /*
 
1451
      If item and comp_item are both Item_rows and have same number of cols
 
1452
      then process items in Item_row one by one.
 
1453
      We can't ignore NULL values here as this item may be used with <=>, in
 
1454
      which case NULL's are significant.
 
1455
    */
 
1456
    assert(item->result_type() == comp_item->result_type());
 
1457
    assert(item_row->cols() == comp_item_row->cols());
 
1458
    col= item_row->cols();
 
1459
    while (col-- > 0)
 
1460
      resolve_const_item(session, item_row->addr(col),
 
1461
                         comp_item_row->element_index(col));
 
1462
    break;
 
1463
  }
 
1464
  /* Fallthrough */
1570
1465
  case REAL_RESULT:
1571
 
    {                                           // It must REAL_RESULT
1572
 
      double result= item->val_real();
1573
 
      uint32_t length=item->max_length,decimals=item->decimals;
1574
 
      bool null_value=item->null_value;
1575
 
      new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1576
 
                 new Item_float(name, result, decimals, length));
1577
 
      break;
1578
 
    }
 
1466
  {                                             // It must REAL_RESULT
 
1467
    double result= item->val_real();
 
1468
    uint32_t length=item->max_length,decimals=item->decimals;
 
1469
    bool null_value=item->null_value;
 
1470
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
 
1471
               new Item_float(name, result, decimals, length));
 
1472
    break;
 
1473
  }
1579
1474
  case DECIMAL_RESULT:
1580
 
    {
1581
 
      type::Decimal decimal_value;
1582
 
      type::Decimal *result= item->val_decimal(&decimal_value);
1583
 
      uint32_t length= item->max_length, decimals= item->decimals;
1584
 
      bool null_value= item->null_value;
1585
 
      new_item= (null_value ?
1586
 
                 (Item*) new Item_null(name) :
1587
 
                 (Item*) new Item_decimal(name, result, length, decimals));
1588
 
      break;
1589
 
    }
1590
 
  }
1591
 
 
 
1475
  {
 
1476
    my_decimal decimal_value;
 
1477
    my_decimal *result= item->val_decimal(&decimal_value);
 
1478
    uint32_t length= item->max_length, decimals= item->decimals;
 
1479
    bool null_value= item->null_value;
 
1480
    new_item= (null_value ?
 
1481
               (Item*) new Item_null(name) :
 
1482
               (Item*) new Item_decimal(name, result, length, decimals));
 
1483
    break;
 
1484
  }
 
1485
  default:
 
1486
    assert(0);
 
1487
  }
1592
1488
  if (new_item)
1593
1489
    session->change_item_tree(ref, new_item);
1594
1490
}
1607
1503
    item_result=item->val_str(&item_tmp);
1608
1504
    if (item->null_value)
1609
1505
      return 1;                                 // This must be true
1610
 
    field->val_str_internal(&field_tmp);
1611
 
    return not stringcmp(&field_tmp,item_result);
 
1506
    field->val_str(&field_tmp);
 
1507
    return !stringcmp(&field_tmp,item_result);
1612
1508
  }
1613
 
 
1614
1509
  if (res_type == INT_RESULT)
1615
1510
    return 1;                                   // Both where of type int
1616
 
 
1617
1511
  if (res_type == DECIMAL_RESULT)
1618
1512
  {
1619
 
    type::Decimal item_buf, *item_val,
 
1513
    my_decimal item_buf, *item_val,
1620
1514
               field_buf, *field_val;
1621
1515
    item_val= item->val_decimal(&item_buf);
1622
1516
    if (item->null_value)
1623
1517
      return 1;                                 // This must be true
1624
1518
    field_val= field->val_decimal(&field_buf);
1625
 
    return !class_decimal_cmp(item_val, field_val);
 
1519
    return !my_decimal_cmp(item_val, field_val);
1626
1520
  }
1627
 
 
1628
1521
  double result= item->val_real();
1629
1522
  if (item->null_value)
1630
1523
    return 1;
1631
 
 
1632
1524
  return result == field->val_real();
1633
1525
}
1634
1526
 
1664
1556
                                         uint32_t convert_blob_length)
1665
1557
{
1666
1558
  bool maybe_null= item->maybe_null;
1667
 
  Field *new_field= NULL;
 
1559
  Field *new_field;
1668
1560
 
1669
1561
  switch (item->result_type()) {
1670
1562
  case REAL_RESULT:
1671
1563
    new_field= new Field_double(item->max_length, maybe_null,
1672
1564
                                item->name, item->decimals, true);
1673
1565
    break;
1674
 
 
1675
1566
  case INT_RESULT:
1676
1567
    /*
1677
1568
      Select an integer type with the minimal fit precision.
1678
1569
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1679
1570
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1680
 
      Int32 -> make them field::Int64.
 
1571
      Field_long : make them Field_int64_t.
1681
1572
    */
1682
 
    if (item->unsigned_flag)
1683
 
    {
1684
 
      new_field= new field::Size(item->max_length, maybe_null,
1685
 
                                  item->name, item->unsigned_flag);
1686
 
    }
1687
 
    else if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
1688
 
    {
1689
 
      new_field= new field::Int64(item->max_length, maybe_null,
1690
 
                                  item->name, item->unsigned_flag);
1691
 
    }
 
1573
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1574
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
1575
                                   item->name, item->unsigned_flag);
1692
1576
    else
1693
 
    {
1694
 
      new_field= new field::Int32(item->max_length, maybe_null,
1695
 
                                  item->name, item->unsigned_flag);
1696
 
    }
1697
 
 
 
1577
      new_field=new Field_long(item->max_length, maybe_null,
 
1578
                               item->name, item->unsigned_flag);
1698
1579
    break;
1699
 
 
1700
1580
  case STRING_RESULT:
1701
1581
    assert(item->collation.collation);
1702
1582
 
1706
1586
      To preserve type they needed to be handled separately.
1707
1587
    */
1708
1588
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1709
 
        type == DRIZZLE_TYPE_TIME ||
1710
 
        type == DRIZZLE_TYPE_MICROTIME ||
1711
1589
        type == DRIZZLE_TYPE_DATE ||
1712
1590
        type == DRIZZLE_TYPE_TIMESTAMP)
1713
 
    {
1714
1591
      new_field= item->tmp_table_field_from_field_type(table, 1);
1715
 
      /*
1716
 
        Make sure that the blob fits into a Field_varstring which has
1717
 
        2-byte lenght.
1718
 
      */
1719
 
    }
 
1592
    /*
 
1593
      Make sure that the blob fits into a Field_varstring which has
 
1594
      2-byte lenght.
 
1595
    */
1720
1596
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1721
1597
             convert_blob_length <= Field_varstring::MAX_SIZE &&
1722
1598
             convert_blob_length)
1723
 
    {
1724
 
      table->setVariableWidth();
1725
1599
      new_field= new Field_varstring(convert_blob_length, maybe_null,
1726
 
                                     item->name, item->collation.collation);
1727
 
    }
 
1600
                                     item->name, table->getMutableShare(),
 
1601
                                     item->collation.collation);
1728
1602
    else
1729
 
    {
1730
1603
      new_field= item->make_string_field(table);
1731
 
    }
1732
1604
    new_field->set_derivation(item->collation.derivation);
1733
1605
    break;
1734
 
 
1735
1606
  case DECIMAL_RESULT:
 
1607
  {
 
1608
    uint8_t dec= item->decimals;
 
1609
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
1610
    uint32_t len= item->max_length;
 
1611
 
 
1612
    /*
 
1613
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
1614
      will always throw a warning. We must limit dec to
 
1615
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
1616
    */
 
1617
 
 
1618
    if (dec > 0)
1736
1619
    {
1737
 
      uint8_t dec= item->decimals;
1738
 
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1739
 
      uint32_t len= item->max_length;
 
1620
      signed int overflow;
 
1621
 
 
1622
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1740
1623
 
1741
1624
      /*
1742
 
        Trying to put too many digits overall in a DECIMAL(prec,dec)
1743
 
        will always throw a warning. We must limit dec to
1744
 
        DECIMAL_MAX_SCALE however to prevent an assert() later.
 
1625
        If the value still overflows the field with the corrected dec,
 
1626
        we'll throw out decimals rather than integers. This is still
 
1627
        bad and of course throws a truncation warning.
 
1628
        +1: for decimal point
1745
1629
      */
1746
1630
 
1747
 
      if (dec > 0)
1748
 
      {
1749
 
        signed int overflow;
1750
 
 
1751
 
        dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1752
 
 
1753
 
        /*
1754
 
          If the value still overflows the field with the corrected dec,
1755
 
          we'll throw out decimals rather than integers. This is still
1756
 
          bad and of course throws a truncation warning.
1757
 
          +1: for decimal point
1758
 
        */
1759
 
 
1760
 
        overflow= class_decimal_precision_to_length(intg + dec, dec,
1761
 
                                                 item->unsigned_flag) - len;
1762
 
 
1763
 
        if (overflow > 0)
1764
 
          dec= max(0, dec - overflow);            // too long, discard fract
1765
 
        else
1766
 
          len-= item->decimals - dec;             // corrected value fits
1767
 
      }
1768
 
 
1769
 
      new_field= new Field_decimal(len,
1770
 
                                   maybe_null,
1771
 
                                   item->name,
1772
 
                                   dec,
1773
 
                                   item->unsigned_flag);
1774
 
      break;
 
1631
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
1632
                                               item->unsigned_flag) - len;
 
1633
 
 
1634
      if (overflow > 0)
 
1635
        dec= max(0, dec - overflow);            // too long, discard fract
 
1636
      else
 
1637
        len-= item->decimals - dec;             // corrected value fits
1775
1638
    }
1776
1639
 
 
1640
    new_field= new Field_decimal(len,
 
1641
                                 maybe_null,
 
1642
                                 item->name,
 
1643
                                 dec,
 
1644
                                 item->unsigned_flag);
 
1645
    break;
 
1646
  }
1777
1647
  case ROW_RESULT:
 
1648
  default:
1778
1649
    // This case should never be choosen
1779
1650
    assert(0);
1780
 
    abort();
 
1651
    new_field= 0;
 
1652
    break;
1781
1653
  }
1782
 
 
1783
1654
  if (new_field)
1784
1655
    new_field->init(table);
1785
1656
 
1786
1657
  if (copy_func && item->is_result_field())
1787
1658
    *((*copy_func)++) = item;                   // Save for copy_funcs
1788
 
 
1789
1659
  if (modify_item)
1790
1660
    item->set_result_field(new_field);
1791
 
 
1792
1661
  if (item->type() == Item::NULL_ITEM)
1793
1662
    new_field->is_created_from_null_item= true;
1794
 
 
1795
1663
  return new_field;
1796
1664
}
1797
1665
 
1848
1716
        field->result_field= result;
1849
1717
    }
1850
1718
    else
1851
 
    {
1852
1719
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1853
1720
                                          orig_item ? orig_item->name :
1854
1721
                                          item->name,
1856
1723
                                          modify_item ? field :
1857
1724
                                          NULL,
1858
1725
                                          convert_blob_length);
1859
 
    }
1860
1726
    if (orig_type == Item::REF_ITEM && orig_modify)
1861
1727
      ((Item_ref*)orig_item)->set_result_field(result);
1862
1728
    if (field->field->eq_def(result))
1896
1762
  }
1897
1763
}
1898
1764
 
1899
 
std::ostream& operator<<(std::ostream& output, const Item &item)
1900
 
{
1901
 
  switch (item.type())
1902
 
  {
1903
 
  case drizzled::Item::SUBSELECT_ITEM :
1904
 
  case drizzled::Item::FIELD_ITEM :
1905
 
  case drizzled::Item::SUM_FUNC_ITEM :
1906
 
  case drizzled::Item::STRING_ITEM :
1907
 
  case drizzled::Item::INT_ITEM :
1908
 
  case drizzled::Item::REAL_ITEM :
1909
 
  case drizzled::Item::NULL_ITEM :
1910
 
  case drizzled::Item::VARBIN_ITEM :
1911
 
  case drizzled::Item::COPY_STR_ITEM :
1912
 
  case drizzled::Item::FIELD_AVG_ITEM :
1913
 
  case drizzled::Item::DEFAULT_VALUE_ITEM :
1914
 
  case drizzled::Item::PROC_ITEM :
1915
 
  case drizzled::Item::COND_ITEM :
1916
 
  case drizzled::Item::REF_ITEM :
1917
 
  case drizzled::Item::FIELD_STD_ITEM :
1918
 
  case drizzled::Item::FIELD_VARIANCE_ITEM :
1919
 
  case drizzled::Item::INSERT_VALUE_ITEM :
1920
 
  case drizzled::Item::ROW_ITEM:
1921
 
  case drizzled::Item::CACHE_ITEM :
1922
 
  case drizzled::Item::TYPE_HOLDER :
1923
 
  case drizzled::Item::PARAM_ITEM :
1924
 
  case drizzled::Item::DECIMAL_ITEM :
1925
 
  case drizzled::Item::FUNC_ITEM :
1926
 
  case drizzled::Item::BOOLEAN_ITEM :
1927
 
    {
1928
 
      output << "Item:(";
1929
 
      output <<  item.full_name();
1930
 
      output << ", ";
1931
 
      output << drizzled::display::type(item.type());
1932
 
      output << ")";
1933
 
    }
1934
 
    break;
1935
 
  }
1936
 
 
1937
 
  return output;  // for multiple << operators.
1938
 
}
1939
 
 
1940
1765
} /* namespace drizzled */