~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Andrew Hutchings
  • Date: 2010-10-20 15:31:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1907.
  • Revision ID: andrew@linuxjedi.co.uk-20101020153127-w9djuz9omzezg2kz
Add error message for global sort buffer constraint

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;
1193
1146
    The field functions defines a field to be not null if null_ptr is not 0
1194
1147
  */
1195
1148
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1196
 
  Field *field= NULL;
 
1149
  Field *field;
1197
1150
 
1198
1151
  switch (field_type()) {
1199
1152
  case DRIZZLE_TYPE_DECIMAL:
1203
1156
                                 0,
1204
1157
                                 Field::NONE,
1205
1158
                                 name,
1206
 
                                 decimals);
 
1159
                                 decimals,
 
1160
                                 0,
 
1161
                                 unsigned_flag);
1207
1162
    break;
1208
1163
  case DRIZZLE_TYPE_LONG:
1209
 
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1164
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1165
                          name, 0, unsigned_flag);
1210
1166
    break;
1211
1167
  case DRIZZLE_TYPE_LONGLONG:
1212
 
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1168
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1169
                              name, 0, unsigned_flag);
1213
1170
    break;
1214
1171
  case DRIZZLE_TYPE_DOUBLE:
1215
1172
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1216
 
                            name, decimals, 0, unsigned_flag);
 
1173
                            name, decimals, 0, unsigned_flag);
1217
1174
    break;
1218
1175
  case DRIZZLE_TYPE_NULL:
1219
1176
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1221
1178
  case DRIZZLE_TYPE_DATE:
1222
1179
    field= new Field_date(maybe_null, name, &my_charset_bin);
1223
1180
    break;
1224
 
 
1225
 
  case DRIZZLE_TYPE_MICROTIME:
1226
 
    field= new field::Microtime(maybe_null, name);
1227
 
    break;
1228
 
 
1229
1181
  case DRIZZLE_TYPE_TIMESTAMP:
1230
 
    field= new field::Epoch(maybe_null, name);
 
1182
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1231
1183
    break;
1232
1184
  case DRIZZLE_TYPE_DATETIME:
1233
1185
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1234
1186
    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:
 
1187
  default:
 
1188
    /* This case should never be chosen */
 
1189
    assert(0);
 
1190
    /* Fall through to make_string_field() */
1240
1191
  case DRIZZLE_TYPE_ENUM:
1241
1192
  case DRIZZLE_TYPE_VARCHAR:
1242
1193
    return make_string_field(table);
1243
1194
  case DRIZZLE_TYPE_BLOB:
 
1195
    if (this->type() == Item::TYPE_HOLDER)
 
1196
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
 
1197
                            1);
 
1198
    else
1244
1199
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1245
1200
    break;                                      // Blob handled outside of case
1246
1201
  }
1247
 
  assert(field);
1248
 
 
1249
1202
  if (field)
1250
1203
    field->init(table);
1251
1204
  return field;
1297
1250
  }
1298
1251
  else if (result_type() == DECIMAL_RESULT)
1299
1252
  {
1300
 
    type::Decimal decimal_value;
1301
 
    type::Decimal *value= val_decimal(&decimal_value);
 
1253
    my_decimal decimal_value;
 
1254
    my_decimal *value= val_decimal(&decimal_value);
1302
1255
    if (null_value)
1303
1256
      return set_field_to_null_with_conversions(field, no_conversions);
1304
1257
    field->set_notnull();
1380
1333
  enum_field_types f_type;
1381
1334
 
1382
1335
  switch ((f_type=field_type())) {
1383
 
  case DRIZZLE_TYPE_DATE:
 
1336
  default:
1384
1337
  case DRIZZLE_TYPE_NULL:
1385
1338
  case DRIZZLE_TYPE_ENUM:
1386
1339
  case DRIZZLE_TYPE_BLOB:
1387
1340
  case DRIZZLE_TYPE_VARCHAR:
1388
 
  case DRIZZLE_TYPE_BOOLEAN:
1389
 
  case DRIZZLE_TYPE_UUID:
1390
1341
  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
 
    }
 
1342
  {
 
1343
    String *res;
 
1344
    if ((res=val_str(buffer)))
 
1345
      result= client->store(res->ptr(),res->length());
 
1346
    break;
 
1347
  }
1397
1348
  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
 
    }
 
1349
  {
 
1350
    int64_t nr;
 
1351
    nr= val_int();
 
1352
    if (!null_value)
 
1353
      result= client->store((int32_t)nr);
 
1354
    break;
 
1355
  }
1405
1356
  case DRIZZLE_TYPE_LONGLONG:
 
1357
  {
 
1358
    int64_t nr;
 
1359
    nr= val_int();
 
1360
    if (!null_value)
1406
1361
    {
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;
 
1362
      if (unsigned_flag)
 
1363
        result= client->store((uint64_t)nr);
 
1364
      else
 
1365
        result= client->store((int64_t)nr);
1417
1366
    }
 
1367
    break;
 
1368
  }
1418
1369
  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
 
    }
 
1370
  {
 
1371
    double nr= val_real();
 
1372
    if (!null_value)
 
1373
      result= client->store(nr, decimals, buffer);
 
1374
    break;
 
1375
  }
1433
1376
  case DRIZZLE_TYPE_DATETIME:
1434
 
  case DRIZZLE_TYPE_MICROTIME:
1435
1377
  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
 
    }
 
1378
  {
 
1379
    DRIZZLE_TIME tm;
 
1380
    get_date(&tm, TIME_FUZZY_DATE);
 
1381
    if (!null_value)
 
1382
      result= client->store(&tm);
 
1383
    break;
 
1384
  }
1443
1385
  }
1444
1386
  if (null_value)
1445
1387
    result= client->store();
1446
 
 
1447
1388
  return result;
1448
1389
}
1449
1390
 
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
1391
Item_result item_cmp_type(Item_result a,Item_result b)
1489
1392
{
1490
1393
  if (a == STRING_RESULT && b == STRING_RESULT)
1491
1394
    return STRING_RESULT;
1492
 
 
1493
1395
  if (a == INT_RESULT && b == INT_RESULT)
1494
1396
    return INT_RESULT;
1495
1397
  else if (a == ROW_RESULT || b == ROW_RESULT)
1496
1398
    return ROW_RESULT;
1497
 
 
1498
1399
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1499
1400
      (b == INT_RESULT || b == DECIMAL_RESULT))
1500
1401
    return DECIMAL_RESULT;
1501
 
 
1502
1402
  return REAL_RESULT;
1503
1403
}
1504
1404
 
1514
1414
 
1515
1415
  switch (res_type) {
1516
1416
  case STRING_RESULT:
 
1417
  {
 
1418
    char buff[MAX_FIELD_WIDTH];
 
1419
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
 
1420
    result=item->val_str(&tmp);
 
1421
    if (item->null_value)
 
1422
      new_item= new Item_null(name);
 
1423
    else
1517
1424
    {
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;
 
1425
      uint32_t length= result->length();
 
1426
      char *tmp_str= memory::sql_strmake(result->ptr(), length);
 
1427
      new_item= new Item_string(name, tmp_str, length, result->charset());
1530
1428
    }
 
1429
    break;
 
1430
  }
1531
1431
  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
 
    }
 
1432
  {
 
1433
    int64_t result=item->val_int();
 
1434
    uint32_t length=item->max_length;
 
1435
    bool null_value=item->null_value;
 
1436
    new_item= (null_value ? (Item*) new Item_null(name) :
 
1437
               (Item*) new Item_int(name, result, length));
 
1438
    break;
 
1439
  }
1540
1440
  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).
 
1441
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
 
1442
  {
 
1443
    /*
 
1444
      Substitute constants only in Item_rows. Don't affect other Items
 
1445
      with ROW_RESULT (eg Item_singlerow_subselect).
1546
1446
 
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 */
 
1447
      For such Items more optimal is to detect if it is constant and replace
 
1448
      it with Item_row. This would optimize queries like this:
 
1449
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
 
1450
    */
 
1451
    Item_row *item_row= (Item_row*) item;
 
1452
    Item_row *comp_item_row= (Item_row*) comp_item;
 
1453
    uint32_t col;
 
1454
    new_item= 0;
 
1455
    /*
 
1456
      If item and comp_item are both Item_rows and have same number of cols
 
1457
      then process items in Item_row one by one.
 
1458
      We can't ignore NULL values here as this item may be used with <=>, in
 
1459
      which case NULL's are significant.
 
1460
    */
 
1461
    assert(item->result_type() == comp_item->result_type());
 
1462
    assert(item_row->cols() == comp_item_row->cols());
 
1463
    col= item_row->cols();
 
1464
    while (col-- > 0)
 
1465
      resolve_const_item(session, item_row->addr(col),
 
1466
                         comp_item_row->element_index(col));
 
1467
    break;
 
1468
  }
 
1469
  /* Fallthrough */
1570
1470
  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
 
    }
 
1471
  {                                             // It must REAL_RESULT
 
1472
    double result= item->val_real();
 
1473
    uint32_t length=item->max_length,decimals=item->decimals;
 
1474
    bool null_value=item->null_value;
 
1475
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
 
1476
               new Item_float(name, result, decimals, length));
 
1477
    break;
 
1478
  }
1579
1479
  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
 
 
 
1480
  {
 
1481
    my_decimal decimal_value;
 
1482
    my_decimal *result= item->val_decimal(&decimal_value);
 
1483
    uint32_t length= item->max_length, decimals= item->decimals;
 
1484
    bool null_value= item->null_value;
 
1485
    new_item= (null_value ?
 
1486
               (Item*) new Item_null(name) :
 
1487
               (Item*) new Item_decimal(name, result, length, decimals));
 
1488
    break;
 
1489
  }
 
1490
  default:
 
1491
    assert(0);
 
1492
  }
1592
1493
  if (new_item)
1593
1494
    session->change_item_tree(ref, new_item);
1594
1495
}
1607
1508
    item_result=item->val_str(&item_tmp);
1608
1509
    if (item->null_value)
1609
1510
      return 1;                                 // This must be true
1610
 
    field->val_str_internal(&field_tmp);
1611
 
    return not stringcmp(&field_tmp,item_result);
 
1511
    field->val_str(&field_tmp);
 
1512
    return !stringcmp(&field_tmp,item_result);
1612
1513
  }
1613
 
 
1614
1514
  if (res_type == INT_RESULT)
1615
1515
    return 1;                                   // Both where of type int
1616
 
 
1617
1516
  if (res_type == DECIMAL_RESULT)
1618
1517
  {
1619
 
    type::Decimal item_buf, *item_val,
 
1518
    my_decimal item_buf, *item_val,
1620
1519
               field_buf, *field_val;
1621
1520
    item_val= item->val_decimal(&item_buf);
1622
1521
    if (item->null_value)
1623
1522
      return 1;                                 // This must be true
1624
1523
    field_val= field->val_decimal(&field_buf);
1625
 
    return !class_decimal_cmp(item_val, field_val);
 
1524
    return !my_decimal_cmp(item_val, field_val);
1626
1525
  }
1627
 
 
1628
1526
  double result= item->val_real();
1629
1527
  if (item->null_value)
1630
1528
    return 1;
1631
 
 
1632
1529
  return result == field->val_real();
1633
1530
}
1634
1531
 
1664
1561
                                         uint32_t convert_blob_length)
1665
1562
{
1666
1563
  bool maybe_null= item->maybe_null;
1667
 
  Field *new_field= NULL;
 
1564
  Field *new_field;
1668
1565
 
1669
1566
  switch (item->result_type()) {
1670
1567
  case REAL_RESULT:
1671
1568
    new_field= new Field_double(item->max_length, maybe_null,
1672
1569
                                item->name, item->decimals, true);
1673
1570
    break;
1674
 
 
1675
1571
  case INT_RESULT:
1676
1572
    /*
1677
1573
      Select an integer type with the minimal fit precision.
1678
1574
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1679
1575
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1680
 
      Int32 -> make them field::Int64.
 
1576
      Field_long : make them Field_int64_t.
1681
1577
    */
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
 
    }
 
1578
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1579
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
1580
                                   item->name, item->unsigned_flag);
1692
1581
    else
1693
 
    {
1694
 
      new_field= new field::Int32(item->max_length, maybe_null,
1695
 
                                  item->name, item->unsigned_flag);
1696
 
    }
1697
 
 
 
1582
      new_field=new Field_long(item->max_length, maybe_null,
 
1583
                               item->name, item->unsigned_flag);
1698
1584
    break;
1699
 
 
1700
1585
  case STRING_RESULT:
1701
1586
    assert(item->collation.collation);
1702
1587
 
1706
1591
      To preserve type they needed to be handled separately.
1707
1592
    */
1708
1593
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1709
 
        type == DRIZZLE_TYPE_TIME ||
1710
 
        type == DRIZZLE_TYPE_MICROTIME ||
1711
1594
        type == DRIZZLE_TYPE_DATE ||
1712
1595
        type == DRIZZLE_TYPE_TIMESTAMP)
1713
1596
    {
1731
1614
    }
1732
1615
    new_field->set_derivation(item->collation.derivation);
1733
1616
    break;
1734
 
 
1735
1617
  case DECIMAL_RESULT:
 
1618
  {
 
1619
    uint8_t dec= item->decimals;
 
1620
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
1621
    uint32_t len= item->max_length;
 
1622
 
 
1623
    /*
 
1624
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
1625
      will always throw a warning. We must limit dec to
 
1626
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
1627
    */
 
1628
 
 
1629
    if (dec > 0)
1736
1630
    {
1737
 
      uint8_t dec= item->decimals;
1738
 
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1739
 
      uint32_t len= item->max_length;
 
1631
      signed int overflow;
 
1632
 
 
1633
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1740
1634
 
1741
1635
      /*
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.
 
1636
        If the value still overflows the field with the corrected dec,
 
1637
        we'll throw out decimals rather than integers. This is still
 
1638
        bad and of course throws a truncation warning.
 
1639
        +1: for decimal point
1745
1640
      */
1746
1641
 
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;
 
1642
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
1643
                                               item->unsigned_flag) - len;
 
1644
 
 
1645
      if (overflow > 0)
 
1646
        dec= max(0, dec - overflow);            // too long, discard fract
 
1647
      else
 
1648
        len-= item->decimals - dec;             // corrected value fits
1775
1649
    }
1776
1650
 
 
1651
    new_field= new Field_decimal(len,
 
1652
                                 maybe_null,
 
1653
                                 item->name,
 
1654
                                 dec,
 
1655
                                 item->unsigned_flag);
 
1656
    break;
 
1657
  }
1777
1658
  case ROW_RESULT:
 
1659
  default:
1778
1660
    // This case should never be choosen
1779
1661
    assert(0);
1780
 
    abort();
 
1662
    new_field= 0;
 
1663
    break;
1781
1664
  }
1782
 
 
1783
1665
  if (new_field)
1784
1666
    new_field->init(table);
1785
1667
 
1786
1668
  if (copy_func && item->is_result_field())
1787
1669
    *((*copy_func)++) = item;                   // Save for copy_funcs
1788
 
 
1789
1670
  if (modify_item)
1790
1671
    item->set_result_field(new_field);
1791
 
 
1792
1672
  if (item->type() == Item::NULL_ITEM)
1793
1673
    new_field->is_created_from_null_item= true;
1794
 
 
1795
1674
  return new_field;
1796
1675
}
1797
1676
 
1848
1727
        field->result_field= result;
1849
1728
    }
1850
1729
    else
1851
 
    {
1852
1730
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1853
1731
                                          orig_item ? orig_item->name :
1854
1732
                                          item->name,
1856
1734
                                          modify_item ? field :
1857
1735
                                          NULL,
1858
1736
                                          convert_blob_length);
1859
 
    }
1860
1737
    if (orig_type == Item::REF_ITEM && orig_modify)
1861
1738
      ((Item_ref*)orig_item)->set_result_field(result);
1862
1739
    if (field->field->eq_def(result))
1896
1773
  }
1897
1774
}
1898
1775
 
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
1776
} /* namespace drizzled */