~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2010-11-19 00:00:46 UTC
  • mto: (1945.2.1 quick)
  • mto: This revision was merged to the branch mainline in revision 1944.
  • Revision ID: brian@tangent.org-20101119000046-iajnd847tmo595ts
Fix style issue around table for message (though this is imperfect,...)

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
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
21
 
#include <drizzled/sql_select.h>
22
 
#include <drizzled/error.h>
23
 
#include <drizzled/show.h>
24
 
#include <drizzled/item/cmpfunc.h>
25
 
#include <drizzled/item/cache_row.h>
26
 
#include <drizzled/item/type_holder.h>
27
 
#include <drizzled/item/sum.h>
28
 
#include <drizzled/item/copy_string.h>
29
 
#include <drizzled/function/str/conv_charset.h>
30
 
#include <drizzled/sql_base.h>
31
 
#include <drizzled/util/convert.h>
32
 
#include <drizzled/plugin/client.h>
33
 
#include <drizzled/time_functions.h>
34
 
 
35
 
#include <drizzled/field/str.h>
36
 
#include <drizzled/field/num.h>
37
 
 
38
 
#include <drizzled/field/blob.h>
39
 
#include <drizzled/field/date.h>
40
 
#include <drizzled/field/datetime.h>
41
 
#include <drizzled/field/decimal.h>
42
 
#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>
52
 
#include <drizzled/field/varstring.h>
53
 
 
54
 
#include <drizzled/current_session.h>
55
 
#include <drizzled/session.h>
56
 
 
57
 
#include <drizzled/internal/m_string.h>
 
20
#include "config.h"
 
21
#include "drizzled/sql_select.h"
 
22
#include "drizzled/error.h"
 
23
#include "drizzled/show.h"
 
24
#include "drizzled/item/cmpfunc.h"
 
25
#include "drizzled/item/cache_row.h"
 
26
#include "drizzled/item/type_holder.h"
 
27
#include "drizzled/item/sum.h"
 
28
#include "drizzled/item/copy_string.h"
 
29
#include "drizzled/function/str/conv_charset.h"
 
30
#include "drizzled/sql_base.h"
 
31
#include "drizzled/util/convert.h"
 
32
#include "drizzled/plugin/client.h"
 
33
#include "drizzled/time_functions.h"
 
34
 
 
35
#include "drizzled/field/str.h"
 
36
#include "drizzled/field/num.h"
 
37
#include "drizzled/field/blob.h"
 
38
#include "drizzled/field/enum.h"
 
39
#include "drizzled/field/null.h"
 
40
#include "drizzled/field/date.h"
 
41
#include "drizzled/field/decimal.h"
 
42
#include "drizzled/field/real.h"
 
43
#include "drizzled/field/double.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"
 
49
#include "drizzled/field/varstring.h"
 
50
#include "drizzled/internal/m_string.h"
58
51
 
59
52
#include <cstdio>
60
53
#include <math.h>
99
92
  {
100
93
    case INT_RESULT:
101
94
      return val_int() != 0;
102
 
 
103
95
    case DECIMAL_RESULT:
104
96
    {
105
 
      type::Decimal decimal_value;
106
 
      type::Decimal *val= val_decimal(&decimal_value);
 
97
      my_decimal decimal_value;
 
98
      my_decimal *val= val_decimal(&decimal_value);
107
99
      if (val)
108
 
        return not val->isZero();
 
100
        return !my_decimal_is_zero(val);
109
101
      return false;
110
102
    }
111
 
 
112
103
    case REAL_RESULT:
113
104
    case STRING_RESULT:
114
105
      return val_real() != 0.0;
115
 
 
116
106
    case ROW_RESULT:
 
107
    default:
117
108
      assert(0);
118
 
      abort();
 
109
      return false;
119
110
  }
120
 
 
121
 
  assert(0);
122
 
  abort();
123
111
}
124
112
 
125
113
String *Item::val_string_from_real(String *str)
144
132
 
145
133
String *Item::val_string_from_decimal(String *str)
146
134
{
147
 
  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
 
135
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
148
136
  if (null_value)
149
137
    return NULL;
150
138
 
151
 
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
152
 
  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);
153
141
  return str;
154
142
}
155
143
 
156
 
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
 
144
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
157
145
{
158
146
  double nr= val_real();
159
147
  if (null_value)
160
148
    return NULL;
161
149
 
162
 
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
150
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
163
151
  return (decimal_value);
164
152
}
165
153
 
166
 
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
 
154
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
167
155
{
168
156
  int64_t nr= val_int();
169
157
  if (null_value)
170
158
    return NULL;
171
159
 
172
 
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
160
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
173
161
  return decimal_value;
174
162
}
175
163
 
176
 
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
 
164
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
177
165
{
178
166
  String *res;
179
167
  char *end_ptr;
181
169
    return NULL;
182
170
 
183
171
  end_ptr= (char*) res->ptr()+ res->length();
184
 
  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
 
172
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
185
173
                     res->ptr(), 
186
174
                     res->length(), 
187
 
                     res->charset()) & E_DEC_BAD_NUM)
 
175
                     res->charset(),
 
176
                     decimal_value) & E_DEC_BAD_NUM)
188
177
  {
189
 
    push_warning_printf(&getSession(), 
 
178
    push_warning_printf(current_session, 
190
179
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
191
180
                        ER_TRUNCATED_WRONG_VALUE,
192
181
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
195
184
  return decimal_value;
196
185
}
197
186
 
198
 
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
 
187
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
199
188
{
200
189
  assert(fixed);
201
 
  type::Time ltime;
202
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
190
  DRIZZLE_TIME ltime;
 
191
  if (get_date(&ltime, TIME_FUZZY_DATE))
203
192
  {
204
 
    decimal_value->set_zero();
 
193
    my_decimal_set_zero(decimal_value);
205
194
    null_value= 1;                               // set NULL, stop processing
206
195
    return NULL;
207
196
  }
208
 
  return date2_class_decimal(&ltime, decimal_value);
 
197
  return date2my_decimal(&ltime, decimal_value);
209
198
}
210
199
 
211
 
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
 
200
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
212
201
{
213
202
  assert(fixed);
214
 
  type::Time ltime;
215
 
  if (get_time(ltime))
 
203
  DRIZZLE_TIME ltime;
 
204
  if (get_time(&ltime))
216
205
  {
217
 
    decimal_value->set_zero();
 
206
    my_decimal_set_zero(decimal_value);
218
207
    return NULL;
219
208
  }
220
 
  return date2_class_decimal(&ltime, decimal_value);
 
209
  return date2my_decimal(&ltime, decimal_value);
221
210
}
222
211
 
223
212
double Item::val_real_from_decimal()
224
213
{
225
214
  /* Note that fix_fields may not be called for Item_avg_field items */
226
215
  double result;
227
 
  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
 
216
  my_decimal value_buff, *dec_val= val_decimal(&value_buff);
228
217
  if (null_value)
229
218
    return 0.0;
230
 
  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
 
219
  my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
231
220
  return result;
232
221
}
233
222
 
235
224
{
236
225
  /* Note that fix_fields may not be called for Item_avg_field items */
237
226
  int64_t result;
238
 
  type::Decimal value, *dec_val= val_decimal(&value);
239
 
 
 
227
  my_decimal value, *dec_val= val_decimal(&value);
240
228
  if (null_value)
241
229
    return 0;
242
 
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
243
 
 
 
230
  my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
244
231
  return result;
245
232
}
246
233
 
247
 
bool Item::save_time_in_field(Field *field)
 
234
int Item::save_time_in_field(Field *field)
248
235
{
249
 
  type::Time ltime;
250
 
 
251
 
  if (get_time(ltime))
 
236
  DRIZZLE_TIME ltime;
 
237
  if (get_time(&ltime))
252
238
    return set_field_to_null(field);
253
 
 
254
239
  field->set_notnull();
255
 
 
256
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
 
240
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
257
241
}
258
242
 
259
 
bool Item::save_date_in_field(Field *field)
 
243
int Item::save_date_in_field(Field *field)
260
244
{
261
 
  type::Time ltime;
262
 
 
263
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
245
  DRIZZLE_TIME ltime;
 
246
  if (get_date(&ltime, TIME_FUZZY_DATE))
264
247
    return set_field_to_null(field);
265
 
 
266
248
  field->set_notnull();
267
 
 
268
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
 
249
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
269
250
}
270
251
 
271
252
/**
276
257
{
277
258
  if (null_value)
278
259
    return set_field_to_null(field);
279
 
 
280
260
  field->set_notnull();
281
 
 
282
261
  return field->store(result->ptr(), result->length(), collation.collation);
283
262
}
284
263
 
297
276
  with_sum_func(false),
298
277
  is_autogenerated_name(true),
299
278
  with_subselect(false),
300
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
301
 
  _session(*current_session)
 
279
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
302
280
{
303
281
  cmp_context= (Item_result)-1;
304
282
 
305
283
  /* Put item in free list so that we can free all items at end */
306
 
  next= getSession().free_list;
307
 
  getSession().free_list= this;
 
284
  Session *session= current_session;
 
285
  next= session->free_list;
 
286
  session->free_list= this;
308
287
 
309
288
  /*
310
289
    Item constructor can be called during execution other then SQL_COM
311
 
    command => we should check session->getLex()->current_select on zero (session->lex
 
290
    command => we should check session->lex->current_select on zero (session->lex
312
291
    can be uninitialised)
313
292
  */
314
 
  if (getSession().getLex()->current_select)
 
293
  if (session->lex->current_select)
315
294
  {
316
 
    enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
 
295
    enum_parsing_place place= session->lex->current_select->parsing_place;
317
296
    if (place == SELECT_LIST || place == IN_HAVING)
318
 
      getSession().getLex()->current_select->select_n_having_items++;
 
297
      session->lex->current_select->select_n_having_items++;
319
298
  }
320
299
}
321
300
 
336
315
  is_autogenerated_name(item->is_autogenerated_name),
337
316
  with_subselect(item->with_subselect),
338
317
  collation(item->collation),
339
 
  cmp_context(item->cmp_context),
340
 
  _session(*session)
 
318
  cmp_context(item->cmp_context)
341
319
{
342
320
  /* Put this item in the session's free list */
343
 
  next= getSession().free_list;
344
 
  getSession().free_list= this;
 
321
  next= session->free_list;
 
322
  session->free_list= this;
345
323
}
346
324
 
347
325
uint32_t Item::float_length(uint32_t decimals_par) const
354
332
  Item_result restype= result_type();
355
333
 
356
334
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
357
 
    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),
358
336
               (uint32_t) DECIMAL_MAX_PRECISION);
359
337
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
360
338
}
361
339
 
362
340
int Item::decimal_int_part() const
363
341
{
364
 
  return class_decimal_int_part(decimal_precision(), decimals);
 
342
  return my_decimal_int_part(decimal_precision(), decimals);
365
343
}
366
344
 
367
345
void Item::print(String *str, enum_query_type)
439
417
    if (orig_len != length && ! is_autogenerated_name)
440
418
    {
441
419
      if (length == 0)
442
 
        push_warning_printf(&getSession(), 
 
420
        push_warning_printf(current_session, 
443
421
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
444
422
                            ER_NAME_BECOMES_EMPTY, 
445
423
                            ER(ER_NAME_BECOMES_EMPTY),
446
424
                            str + length - orig_len);
447
425
      else
448
 
        push_warning_printf(&getSession(),
 
426
        push_warning_printf(current_session, 
449
427
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
450
428
                            ER_REMOVED_SPACES, 
451
429
                            ER(ER_REMOVED_SPACES),
474
452
  return conv->safe ? conv : NULL;
475
453
}
476
454
 
477
 
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
 
455
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
478
456
{
479
 
  do
480
 
  {
481
 
    if (is_null())
482
 
    {
483
 
      break;
484
 
    }
485
 
    else if (result_type() == STRING_RESULT)
486
 
    {
487
 
      char buff[type::Time::MAX_STRING_LENGTH];
488
 
      String tmp(buff,sizeof(buff), &my_charset_bin),*res;
489
 
      if (!(res=val_str(&tmp)) ||
490
 
          str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
491
 
                                    &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
492
 
      {
493
 
        break;
494
 
      }
495
 
    }
496
 
    else
497
 
    {
498
 
      int64_t value= val_int();
499
 
      type::datetime_t date_value;
500
 
 
501
 
      ltime.convert(date_value, value, fuzzydate);
502
 
 
503
 
      if (not type::is_valid(date_value))
504
 
      {
505
 
        char buff[DECIMAL_LONGLONG_DIGITS], *end;
506
 
        end= internal::int64_t10_to_str(value, buff, -10);
507
 
        make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
508
 
                                     buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
509
 
        break;
510
 
      }
511
 
    }
512
 
 
513
 
    return false;
514
 
  } while (0);
515
 
 
516
 
  ltime.reset();
517
 
 
 
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));
518
484
  return true;
519
485
}
520
486
 
521
 
bool Item::get_time(type::Time &ltime)
 
487
bool Item::get_time(DRIZZLE_TIME *ltime)
522
488
{
523
 
  char buff[type::Time::MAX_STRING_LENGTH];
 
489
  char buff[40];
524
490
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
525
 
  if (!(res=val_str(&tmp)) or
526
 
      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))
527
493
  {
528
 
    ltime.reset();
529
 
 
 
494
    memset(ltime, 0, sizeof(*ltime));
530
495
    return true;
531
496
  }
532
 
 
533
497
  return false;
534
498
}
535
499
 
536
 
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
 
500
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
537
501
{
538
 
  return get_date(ltime, fuzzydate);
 
502
  return get_date(ltime,fuzzydate);
539
503
}
540
504
 
541
505
bool Item::is_null()
788
752
    Item *real_itm= real_item();
789
753
 
790
754
    ref_pointer_array[el]= real_itm;
791
 
    if (!(item_ref= new Item_aggregate_ref(&session->getLex()->current_select->context,
 
755
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
792
756
                                           ref_pointer_array + el, 0, name)))
793
757
      return; /* fatal_error is set */
794
758
    if (type() == SUM_FUNC_ITEM)
821
785
  if (mark_item)
822
786
    mark_item->depended_from= last;
823
787
  current->mark_as_dependent(last);
824
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
788
  if (session->lex->describe & DESCRIBE_EXTENDED)
825
789
  {
826
790
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
827
791
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
852
816
  {
853
817
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
854
818
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
855
 
    prev_subselect_item->const_item_cache= false;
 
819
    prev_subselect_item->const_item_cache= 0;
856
820
  }
857
821
  {
858
822
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
867
831
    }
868
832
    else
869
833
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
870
 
    prev_subselect_item->const_item_cache= false;
 
834
    prev_subselect_item->const_item_cache= 0;
871
835
    mark_as_dependent(session, last_select, current_sel, resolved_item,
872
836
                      dependent);
873
837
  }
887
851
    - the found item on success
888
852
    - NULL if find_item is not in group_list
889
853
*/
890
 
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 *group_list)
891
855
{
892
856
  const char *db_name;
893
857
  const char *table_name;
943
907
        if (cur_field->db_name && db_name)
944
908
        {
945
909
          /* If field_name is also qualified by a database name. */
946
 
          if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
947
 
          {
 
910
          if (strcasecmp(cur_field->db_name, db_name))
948
911
            /* Same field names, different databases. */
949
912
            return NULL;
950
 
          }
951
913
          ++cur_match_degree;
952
914
        }
953
915
      }
966
928
          best match, they must reference the same column, otherwise the field
967
929
          is ambiguous.
968
930
        */
969
 
        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);
970
932
        return NULL;
971
933
      }
972
934
    }
974
936
 
975
937
  if (found_group)
976
938
    return found_group->item;
977
 
 
978
 
  return NULL;
 
939
  else
 
940
    return NULL;
979
941
}
980
942
 
981
943
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
1002
964
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
1003
965
  if (select->having_fix_field && !ref->with_sum_func && group_list)
1004
966
  {
1005
 
    group_by_ref= find_field_in_group_list(session, ref, group_list);
 
967
    group_by_ref= find_field_in_group_list(ref, group_list);
1006
968
 
1007
969
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
1008
970
    if (group_by_ref && (select_ref != not_found_item) &&
1011
973
      ambiguous_fields= true;
1012
974
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
1013
975
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
1014
 
                          session->where());
 
976
                          current_session->where);
1015
977
 
1016
978
    }
1017
979
  }
1040
1002
}
1041
1003
 
1042
1004
void Item::init_make_field(SendField *tmp_field,
1043
 
                           enum enum_field_types field_type_arg)
 
1005
                           enum enum_field_types field_type_arg)
1044
1006
{
1045
1007
  char *empty_name= (char*) "";
1046
1008
  tmp_field->db_name=   empty_name;
1082
1044
  case REAL_RESULT:    
1083
1045
    return DRIZZLE_TYPE_DOUBLE;
1084
1046
  case ROW_RESULT:
 
1047
  default:
1085
1048
    assert(0);
 
1049
    return DRIZZLE_TYPE_VARCHAR;
1086
1050
  }
1087
 
 
1088
 
  abort();
1089
1051
}
1090
1052
 
1091
1053
bool Item::is_datetime()
1092
1054
{
1093
 
  return field::isDateTime(field_type());
 
1055
  switch (field_type())
 
1056
  {
 
1057
    case DRIZZLE_TYPE_DATE:
 
1058
    case DRIZZLE_TYPE_DATETIME:
 
1059
    case DRIZZLE_TYPE_TIMESTAMP:
 
1060
      return true;
 
1061
    default:
 
1062
      break;
 
1063
  }
 
1064
  return false;
1094
1065
}
1095
1066
 
1096
1067
String *Item::check_well_formed_result(String *str, bool send_error)
1103
1074
                                       str->length(), &well_formed_error);
1104
1075
  if (wlen < str->length())
1105
1076
  {
 
1077
    Session *session= current_session;
1106
1078
    char hexbuf[7];
1107
1079
    enum DRIZZLE_ERROR::enum_warning_level level;
1108
1080
    uint32_t diff= str->length() - wlen;
1119
1091
      null_value= 1;
1120
1092
      str= 0;
1121
1093
    }
1122
 
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
 
1094
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1123
1095
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1124
1096
  }
1125
1097
  return str;
1174
1146
    The field functions defines a field to be not null if null_ptr is not 0
1175
1147
  */
1176
1148
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1177
 
  Field *field= NULL;
 
1149
  Field *field;
1178
1150
 
1179
1151
  switch (field_type()) {
1180
1152
  case DRIZZLE_TYPE_DECIMAL:
1184
1156
                                 0,
1185
1157
                                 Field::NONE,
1186
1158
                                 name,
1187
 
                                 decimals);
 
1159
                                 decimals,
 
1160
                                 0,
 
1161
                                 unsigned_flag);
1188
1162
    break;
1189
1163
  case DRIZZLE_TYPE_LONG:
1190
 
    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);
1191
1166
    break;
1192
1167
  case DRIZZLE_TYPE_LONGLONG:
1193
 
    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);
1194
1170
    break;
1195
1171
  case DRIZZLE_TYPE_DOUBLE:
1196
1172
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1197
 
                            name, decimals, 0, unsigned_flag);
 
1173
                            name, decimals, 0, unsigned_flag);
1198
1174
    break;
1199
1175
  case DRIZZLE_TYPE_NULL:
1200
 
    field= new Field_null((unsigned char*) 0, max_length, name);
 
1176
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1201
1177
    break;
1202
1178
  case DRIZZLE_TYPE_DATE:
1203
 
    field= new Field_date(maybe_null, name);
1204
 
    break;
1205
 
 
1206
 
  case DRIZZLE_TYPE_MICROTIME:
1207
 
    field= new field::Microtime(maybe_null, name);
1208
 
    break;
1209
 
 
 
1179
    field= new Field_date(maybe_null, name, &my_charset_bin);
 
1180
    break;
1210
1181
  case DRIZZLE_TYPE_TIMESTAMP:
1211
 
    field= new field::Epoch(maybe_null, name);
 
1182
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1212
1183
    break;
1213
1184
  case DRIZZLE_TYPE_DATETIME:
1214
 
    field= new Field_datetime(maybe_null, name);
1215
 
    break;
1216
 
  case DRIZZLE_TYPE_TIME:
1217
 
    field= new field::Time(maybe_null, name);
1218
 
    break;
1219
 
  case DRIZZLE_TYPE_BOOLEAN:
1220
 
  case DRIZZLE_TYPE_UUID:
 
1185
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
 
1186
    break;
 
1187
  default:
 
1188
    /* This case should never be chosen */
 
1189
    assert(0);
 
1190
    /* Fall through to make_string_field() */
1221
1191
  case DRIZZLE_TYPE_ENUM:
1222
1192
  case DRIZZLE_TYPE_VARCHAR:
1223
1193
    return make_string_field(table);
1225
1195
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1226
1196
    break;                                      // Blob handled outside of case
1227
1197
  }
1228
 
  assert(field);
1229
 
 
1230
1198
  if (field)
1231
1199
    field->init(table);
1232
1200
  return field;
1278
1246
  }
1279
1247
  else if (result_type() == DECIMAL_RESULT)
1280
1248
  {
1281
 
    type::Decimal decimal_value;
1282
 
    type::Decimal *value= val_decimal(&decimal_value);
 
1249
    my_decimal decimal_value;
 
1250
    my_decimal *value= val_decimal(&decimal_value);
1283
1251
    if (null_value)
1284
1252
      return set_field_to_null_with_conversions(field, no_conversions);
1285
1253
    field->set_notnull();
1361
1329
  enum_field_types f_type;
1362
1330
 
1363
1331
  switch ((f_type=field_type())) {
1364
 
  case DRIZZLE_TYPE_DATE:
 
1332
  default:
1365
1333
  case DRIZZLE_TYPE_NULL:
1366
1334
  case DRIZZLE_TYPE_ENUM:
1367
1335
  case DRIZZLE_TYPE_BLOB:
1368
1336
  case DRIZZLE_TYPE_VARCHAR:
1369
 
  case DRIZZLE_TYPE_BOOLEAN:
1370
 
  case DRIZZLE_TYPE_UUID:
1371
1337
  case DRIZZLE_TYPE_DECIMAL:
1372
 
    {
1373
 
      String *res;
1374
 
      if ((res=val_str(buffer)))
1375
 
        result= client->store(res->ptr(),res->length());
1376
 
      break;
1377
 
    }
 
1338
  {
 
1339
    String *res;
 
1340
    if ((res=val_str(buffer)))
 
1341
      result= client->store(res->ptr(),res->length());
 
1342
    break;
 
1343
  }
1378
1344
  case DRIZZLE_TYPE_LONG:
1379
 
    {
1380
 
      int64_t nr;
1381
 
      nr= val_int();
1382
 
      if (!null_value)
1383
 
        result= client->store((int32_t)nr);
1384
 
      break;
1385
 
    }
 
1345
  {
 
1346
    int64_t nr;
 
1347
    nr= val_int();
 
1348
    if (!null_value)
 
1349
      result= client->store((int32_t)nr);
 
1350
    break;
 
1351
  }
1386
1352
  case DRIZZLE_TYPE_LONGLONG:
 
1353
  {
 
1354
    int64_t nr;
 
1355
    nr= val_int();
 
1356
    if (!null_value)
1387
1357
    {
1388
 
      int64_t nr;
1389
 
      nr= val_int();
1390
 
      if (!null_value)
1391
 
      {
1392
 
        if (unsigned_flag)
1393
 
          result= client->store((uint64_t)nr);
1394
 
        else
1395
 
          result= client->store((int64_t)nr);
1396
 
      }
1397
 
      break;
 
1358
      if (unsigned_flag)
 
1359
        result= client->store((uint64_t)nr);
 
1360
      else
 
1361
        result= client->store((int64_t)nr);
1398
1362
    }
 
1363
    break;
 
1364
  }
1399
1365
  case DRIZZLE_TYPE_DOUBLE:
1400
 
    {
1401
 
      double nr= val_real();
1402
 
      if (!null_value)
1403
 
        result= client->store(nr, decimals, buffer);
1404
 
      break;
1405
 
    }
1406
 
  case DRIZZLE_TYPE_TIME:
1407
 
    {
1408
 
      type::Time tm;
1409
 
      get_time(tm);
1410
 
      if (not null_value)
1411
 
        result= client->store(&tm);
1412
 
      break;
1413
 
    }
 
1366
  {
 
1367
    double nr= val_real();
 
1368
    if (!null_value)
 
1369
      result= client->store(nr, decimals, buffer);
 
1370
    break;
 
1371
  }
1414
1372
  case DRIZZLE_TYPE_DATETIME:
1415
 
  case DRIZZLE_TYPE_MICROTIME:
1416
1373
  case DRIZZLE_TYPE_TIMESTAMP:
1417
 
    {
1418
 
      type::Time tm;
1419
 
      get_date(tm, TIME_FUZZY_DATE);
1420
 
      if (!null_value)
1421
 
        result= client->store(&tm);
1422
 
      break;
1423
 
    }
 
1374
  {
 
1375
    DRIZZLE_TIME tm;
 
1376
    get_date(&tm, TIME_FUZZY_DATE);
 
1377
    if (!null_value)
 
1378
      result= client->store(&tm);
 
1379
    break;
 
1380
  }
1424
1381
  }
1425
1382
  if (null_value)
1426
1383
    result= client->store();
1427
 
 
1428
1384
  return result;
1429
1385
}
1430
1386
 
1431
 
uint32_t Item::max_char_length() const
1432
 
{
1433
 
  return max_length / collation.collation->mbmaxlen;
1434
 
}
1435
 
 
1436
 
void Item::fix_length_and_charset(uint32_t max_char_length_arg, CHARSET_INFO *cs)
1437
 
1438
 
  max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1439
 
  collation.collation= cs;
1440
 
}
1441
 
 
1442
 
void Item::fix_char_length(uint32_t max_char_length_arg)
1443
 
1444
 
  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1445
 
}
1446
 
 
1447
 
void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
1448
 
1449
 
  uint64_t max_result_length= max_char_length_arg *
1450
 
    collation.collation->mbmaxlen;
1451
 
 
1452
 
  if (max_result_length >= MAX_BLOB_WIDTH)
1453
 
  { 
1454
 
    max_length= MAX_BLOB_WIDTH;
1455
 
    maybe_null= false;
1456
 
  }
1457
 
  else
1458
 
  {
1459
 
    max_length= max_result_length;
1460
 
  }
1461
 
}
1462
 
 
1463
 
void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
1464
 
1465
 
  collation.set(&my_charset_bin);
1466
 
  fix_char_length(max_char_length_arg);
1467
 
}
1468
 
 
1469
1387
Item_result item_cmp_type(Item_result a,Item_result b)
1470
1388
{
1471
1389
  if (a == STRING_RESULT && b == STRING_RESULT)
1472
1390
    return STRING_RESULT;
1473
 
 
1474
1391
  if (a == INT_RESULT && b == INT_RESULT)
1475
1392
    return INT_RESULT;
1476
1393
  else if (a == ROW_RESULT || b == ROW_RESULT)
1477
1394
    return ROW_RESULT;
1478
 
 
1479
1395
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1480
1396
      (b == INT_RESULT || b == DECIMAL_RESULT))
1481
1397
    return DECIMAL_RESULT;
1482
 
 
1483
1398
  return REAL_RESULT;
1484
1399
}
1485
1400
 
1495
1410
 
1496
1411
  switch (res_type) {
1497
1412
  case STRING_RESULT:
 
1413
  {
 
1414
    char buff[MAX_FIELD_WIDTH];
 
1415
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
 
1416
    result=item->val_str(&tmp);
 
1417
    if (item->null_value)
 
1418
      new_item= new Item_null(name);
 
1419
    else
1498
1420
    {
1499
 
      char buff[MAX_FIELD_WIDTH];
1500
 
      String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1501
 
      result=item->val_str(&tmp);
1502
 
      if (item->null_value)
1503
 
        new_item= new Item_null(name);
1504
 
      else
1505
 
      {
1506
 
        uint32_t length= result->length();
1507
 
        char *tmp_str= memory::sql_strmake(result->ptr(), length);
1508
 
        new_item= new Item_string(name, tmp_str, length, result->charset());
1509
 
      }
1510
 
      break;
 
1421
      uint32_t length= result->length();
 
1422
      char *tmp_str= memory::sql_strmake(result->ptr(), length);
 
1423
      new_item= new Item_string(name, tmp_str, length, result->charset());
1511
1424
    }
 
1425
    break;
 
1426
  }
1512
1427
  case INT_RESULT:
1513
 
    {
1514
 
      int64_t result=item->val_int();
1515
 
      uint32_t length=item->max_length;
1516
 
      bool null_value=item->null_value;
1517
 
      new_item= (null_value ? (Item*) new Item_null(name) :
1518
 
                 (Item*) new Item_int(name, result, length));
1519
 
      break;
1520
 
    }
 
1428
  {
 
1429
    int64_t result=item->val_int();
 
1430
    uint32_t length=item->max_length;
 
1431
    bool null_value=item->null_value;
 
1432
    new_item= (null_value ? (Item*) new Item_null(name) :
 
1433
               (Item*) new Item_int(name, result, length));
 
1434
    break;
 
1435
  }
1521
1436
  case ROW_RESULT:
1522
 
    if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1523
 
    {
1524
 
      /*
1525
 
        Substitute constants only in Item_rows. Don't affect other Items
1526
 
        with ROW_RESULT (eg Item_singlerow_subselect).
 
1437
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
 
1438
  {
 
1439
    /*
 
1440
      Substitute constants only in Item_rows. Don't affect other Items
 
1441
      with ROW_RESULT (eg Item_singlerow_subselect).
1527
1442
 
1528
 
        For such Items more optimal is to detect if it is constant and replace
1529
 
        it with Item_row. This would optimize queries like this:
1530
 
        SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1531
 
      */
1532
 
      Item_row *item_row= (Item_row*) item;
1533
 
      Item_row *comp_item_row= (Item_row*) comp_item;
1534
 
      uint32_t col;
1535
 
      new_item= 0;
1536
 
      /*
1537
 
        If item and comp_item are both Item_rows and have same number of cols
1538
 
        then process items in Item_row one by one.
1539
 
        We can't ignore NULL values here as this item may be used with <=>, in
1540
 
        which case NULL's are significant.
1541
 
      */
1542
 
      assert(item->result_type() == comp_item->result_type());
1543
 
      assert(item_row->cols() == comp_item_row->cols());
1544
 
      col= item_row->cols();
1545
 
      while (col-- > 0)
1546
 
        resolve_const_item(session, item_row->addr(col),
1547
 
                           comp_item_row->element_index(col));
1548
 
      break;
1549
 
    }
1550
 
    /* Fallthrough */
 
1443
      For such Items more optimal is to detect if it is constant and replace
 
1444
      it with Item_row. This would optimize queries like this:
 
1445
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
 
1446
    */
 
1447
    Item_row *item_row= (Item_row*) item;
 
1448
    Item_row *comp_item_row= (Item_row*) comp_item;
 
1449
    uint32_t col;
 
1450
    new_item= 0;
 
1451
    /*
 
1452
      If item and comp_item are both Item_rows and have same number of cols
 
1453
      then process items in Item_row one by one.
 
1454
      We can't ignore NULL values here as this item may be used with <=>, in
 
1455
      which case NULL's are significant.
 
1456
    */
 
1457
    assert(item->result_type() == comp_item->result_type());
 
1458
    assert(item_row->cols() == comp_item_row->cols());
 
1459
    col= item_row->cols();
 
1460
    while (col-- > 0)
 
1461
      resolve_const_item(session, item_row->addr(col),
 
1462
                         comp_item_row->element_index(col));
 
1463
    break;
 
1464
  }
 
1465
  /* Fallthrough */
1551
1466
  case REAL_RESULT:
1552
 
    {                                           // It must REAL_RESULT
1553
 
      double result= item->val_real();
1554
 
      uint32_t length=item->max_length,decimals=item->decimals;
1555
 
      bool null_value=item->null_value;
1556
 
      new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1557
 
                 new Item_float(name, result, decimals, length));
1558
 
      break;
1559
 
    }
 
1467
  {                                             // It must REAL_RESULT
 
1468
    double result= item->val_real();
 
1469
    uint32_t length=item->max_length,decimals=item->decimals;
 
1470
    bool null_value=item->null_value;
 
1471
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
 
1472
               new Item_float(name, result, decimals, length));
 
1473
    break;
 
1474
  }
1560
1475
  case DECIMAL_RESULT:
1561
 
    {
1562
 
      type::Decimal decimal_value;
1563
 
      type::Decimal *result= item->val_decimal(&decimal_value);
1564
 
      uint32_t length= item->max_length, decimals= item->decimals;
1565
 
      bool null_value= item->null_value;
1566
 
      new_item= (null_value ?
1567
 
                 (Item*) new Item_null(name) :
1568
 
                 (Item*) new Item_decimal(name, result, length, decimals));
1569
 
      break;
1570
 
    }
1571
 
  }
1572
 
 
 
1476
  {
 
1477
    my_decimal decimal_value;
 
1478
    my_decimal *result= item->val_decimal(&decimal_value);
 
1479
    uint32_t length= item->max_length, decimals= item->decimals;
 
1480
    bool null_value= item->null_value;
 
1481
    new_item= (null_value ?
 
1482
               (Item*) new Item_null(name) :
 
1483
               (Item*) new Item_decimal(name, result, length, decimals));
 
1484
    break;
 
1485
  }
 
1486
  default:
 
1487
    assert(0);
 
1488
  }
1573
1489
  if (new_item)
1574
1490
    session->change_item_tree(ref, new_item);
1575
1491
}
1588
1504
    item_result=item->val_str(&item_tmp);
1589
1505
    if (item->null_value)
1590
1506
      return 1;                                 // This must be true
1591
 
    field->val_str_internal(&field_tmp);
1592
 
    return not stringcmp(&field_tmp,item_result);
 
1507
    field->val_str(&field_tmp);
 
1508
    return !stringcmp(&field_tmp,item_result);
1593
1509
  }
1594
 
 
1595
1510
  if (res_type == INT_RESULT)
1596
1511
    return 1;                                   // Both where of type int
1597
 
 
1598
1512
  if (res_type == DECIMAL_RESULT)
1599
1513
  {
1600
 
    type::Decimal item_buf, *item_val,
 
1514
    my_decimal item_buf, *item_val,
1601
1515
               field_buf, *field_val;
1602
1516
    item_val= item->val_decimal(&item_buf);
1603
1517
    if (item->null_value)
1604
1518
      return 1;                                 // This must be true
1605
1519
    field_val= field->val_decimal(&field_buf);
1606
 
    return !class_decimal_cmp(item_val, field_val);
 
1520
    return !my_decimal_cmp(item_val, field_val);
1607
1521
  }
1608
 
 
1609
1522
  double result= item->val_real();
1610
1523
  if (item->null_value)
1611
1524
    return 1;
1612
 
 
1613
1525
  return result == field->val_real();
1614
1526
}
1615
1527
 
1645
1557
                                         uint32_t convert_blob_length)
1646
1558
{
1647
1559
  bool maybe_null= item->maybe_null;
1648
 
  Field *new_field= NULL;
 
1560
  Field *new_field;
1649
1561
 
1650
1562
  switch (item->result_type()) {
1651
1563
  case REAL_RESULT:
1652
1564
    new_field= new Field_double(item->max_length, maybe_null,
1653
1565
                                item->name, item->decimals, true);
1654
1566
    break;
1655
 
 
1656
1567
  case INT_RESULT:
1657
1568
    /*
1658
1569
      Select an integer type with the minimal fit precision.
1659
1570
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1660
1571
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1661
 
      Int32 -> make them field::Int64.
 
1572
      Field_long : make them Field_int64_t.
1662
1573
    */
1663
 
    if (item->unsigned_flag)
1664
 
    {
1665
 
      new_field= new field::Size(item->max_length, maybe_null,
1666
 
                                  item->name, item->unsigned_flag);
1667
 
    }
1668
 
    else if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
1669
 
    {
1670
 
      new_field= new field::Int64(item->max_length, maybe_null,
1671
 
                                  item->name, item->unsigned_flag);
1672
 
    }
 
1574
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1575
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
1576
                                   item->name, item->unsigned_flag);
1673
1577
    else
1674
 
    {
1675
 
      new_field= new field::Int32(item->max_length, maybe_null,
1676
 
                                  item->name, item->unsigned_flag);
1677
 
    }
1678
 
 
 
1578
      new_field=new Field_long(item->max_length, maybe_null,
 
1579
                               item->name, item->unsigned_flag);
1679
1580
    break;
1680
 
 
1681
1581
  case STRING_RESULT:
1682
1582
    assert(item->collation.collation);
1683
1583
 
 
1584
    enum enum_field_types type;
1684
1585
    /*
1685
1586
      DATE/TIME fields have STRING_RESULT result type.
1686
1587
      To preserve type they needed to be handled separately.
1687
1588
    */
1688
 
    if (field::isDateTime(item->field_type()))
 
1589
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
 
1590
        type == DRIZZLE_TYPE_DATE ||
 
1591
        type == DRIZZLE_TYPE_TIMESTAMP)
1689
1592
    {
1690
1593
      new_field= item->tmp_table_field_from_field_type(table, 1);
1691
1594
      /*
1707
1610
    }
1708
1611
    new_field->set_derivation(item->collation.derivation);
1709
1612
    break;
1710
 
 
1711
1613
  case DECIMAL_RESULT:
 
1614
  {
 
1615
    uint8_t dec= item->decimals;
 
1616
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
1617
    uint32_t len= item->max_length;
 
1618
 
 
1619
    /*
 
1620
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
1621
      will always throw a warning. We must limit dec to
 
1622
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
1623
    */
 
1624
 
 
1625
    if (dec > 0)
1712
1626
    {
1713
 
      uint8_t dec= item->decimals;
1714
 
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1715
 
      uint32_t len= item->max_length;
 
1627
      signed int overflow;
 
1628
 
 
1629
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1716
1630
 
1717
1631
      /*
1718
 
        Trying to put too many digits overall in a DECIMAL(prec,dec)
1719
 
        will always throw a warning. We must limit dec to
1720
 
        DECIMAL_MAX_SCALE however to prevent an assert() later.
 
1632
        If the value still overflows the field with the corrected dec,
 
1633
        we'll throw out decimals rather than integers. This is still
 
1634
        bad and of course throws a truncation warning.
 
1635
        +1: for decimal point
1721
1636
      */
1722
1637
 
1723
 
      if (dec > 0)
1724
 
      {
1725
 
        signed int overflow;
1726
 
 
1727
 
        dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1728
 
 
1729
 
        /*
1730
 
          If the value still overflows the field with the corrected dec,
1731
 
          we'll throw out decimals rather than integers. This is still
1732
 
          bad and of course throws a truncation warning.
1733
 
          +1: for decimal point
1734
 
        */
1735
 
 
1736
 
        overflow= class_decimal_precision_to_length(intg + dec, dec,
1737
 
                                                 item->unsigned_flag) - len;
1738
 
 
1739
 
        if (overflow > 0)
1740
 
          dec= max(0, dec - overflow);            // too long, discard fract
1741
 
        else
1742
 
          len-= item->decimals - dec;             // corrected value fits
1743
 
      }
1744
 
 
1745
 
      new_field= new Field_decimal(len,
1746
 
                                   maybe_null,
1747
 
                                   item->name,
1748
 
                                   dec,
1749
 
                                   item->unsigned_flag);
1750
 
      break;
 
1638
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
1639
                                               item->unsigned_flag) - len;
 
1640
 
 
1641
      if (overflow > 0)
 
1642
        dec= max(0, dec - overflow);            // too long, discard fract
 
1643
      else
 
1644
        len-= item->decimals - dec;             // corrected value fits
1751
1645
    }
1752
1646
 
 
1647
    new_field= new Field_decimal(len,
 
1648
                                 maybe_null,
 
1649
                                 item->name,
 
1650
                                 dec,
 
1651
                                 item->unsigned_flag);
 
1652
    break;
 
1653
  }
1753
1654
  case ROW_RESULT:
 
1655
  default:
1754
1656
    // This case should never be choosen
1755
1657
    assert(0);
1756
 
    abort();
 
1658
    new_field= 0;
 
1659
    break;
1757
1660
  }
1758
 
 
1759
1661
  if (new_field)
1760
1662
    new_field->init(table);
1761
1663
 
1762
1664
  if (copy_func && item->is_result_field())
1763
1665
    *((*copy_func)++) = item;                   // Save for copy_funcs
1764
 
 
1765
1666
  if (modify_item)
1766
1667
    item->set_result_field(new_field);
1767
 
 
1768
1668
  if (item->type() == Item::NULL_ITEM)
1769
1669
    new_field->is_created_from_null_item= true;
1770
 
 
1771
1670
  return new_field;
1772
1671
}
1773
1672
 
1824
1723
        field->result_field= result;
1825
1724
    }
1826
1725
    else
1827
 
    {
1828
1726
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1829
1727
                                          orig_item ? orig_item->name :
1830
1728
                                          item->name,
1832
1730
                                          modify_item ? field :
1833
1731
                                          NULL,
1834
1732
                                          convert_blob_length);
1835
 
    }
1836
1733
    if (orig_type == Item::REF_ITEM && orig_modify)
1837
1734
      ((Item_ref*)orig_item)->set_result_field(result);
1838
1735
    if (field->field->eq_def(result))
1872
1769
  }
1873
1770
}
1874
1771
 
1875
 
std::ostream& operator<<(std::ostream& output, const Item &item)
1876
 
{
1877
 
  switch (item.type())
1878
 
  {
1879
 
  case drizzled::Item::SUBSELECT_ITEM :
1880
 
  case drizzled::Item::FIELD_ITEM :
1881
 
  case drizzled::Item::SUM_FUNC_ITEM :
1882
 
  case drizzled::Item::STRING_ITEM :
1883
 
  case drizzled::Item::INT_ITEM :
1884
 
  case drizzled::Item::REAL_ITEM :
1885
 
  case drizzled::Item::NULL_ITEM :
1886
 
  case drizzled::Item::VARBIN_ITEM :
1887
 
  case drizzled::Item::COPY_STR_ITEM :
1888
 
  case drizzled::Item::FIELD_AVG_ITEM :
1889
 
  case drizzled::Item::DEFAULT_VALUE_ITEM :
1890
 
  case drizzled::Item::PROC_ITEM :
1891
 
  case drizzled::Item::COND_ITEM :
1892
 
  case drizzled::Item::REF_ITEM :
1893
 
  case drizzled::Item::FIELD_STD_ITEM :
1894
 
  case drizzled::Item::FIELD_VARIANCE_ITEM :
1895
 
  case drizzled::Item::INSERT_VALUE_ITEM :
1896
 
  case drizzled::Item::ROW_ITEM:
1897
 
  case drizzled::Item::CACHE_ITEM :
1898
 
  case drizzled::Item::TYPE_HOLDER :
1899
 
  case drizzled::Item::PARAM_ITEM :
1900
 
  case drizzled::Item::DECIMAL_ITEM :
1901
 
  case drizzled::Item::FUNC_ITEM :
1902
 
  case drizzled::Item::BOOLEAN_ITEM :
1903
 
    {
1904
 
      output << "Item:(";
1905
 
      output <<  item.full_name();
1906
 
      output << ", ";
1907
 
      output << drizzled::display::type(item.type());
1908
 
      output << ")";
1909
 
    }
1910
 
    break;
1911
 
  }
1912
 
 
1913
 
  return output;  // for multiple << operators.
1914
 
}
1915
 
 
1916
1772
} /* namespace drizzled */