~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
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
 
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"
38
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"
39
48
#include "drizzled/field/null.h"
40
 
#include "drizzled/field/date.h"
41
 
#include "drizzled/field/decimal.h"
42
49
#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"
 
50
#include "drizzled/field/size.h"
 
51
#include "drizzled/field/time.h"
49
52
#include "drizzled/field/varstring.h"
 
53
 
50
54
#include "drizzled/internal/m_string.h"
51
55
 
52
56
#include <cstdio>
92
96
  {
93
97
    case INT_RESULT:
94
98
      return val_int() != 0;
 
99
 
95
100
    case DECIMAL_RESULT:
96
101
    {
97
 
      my_decimal decimal_value;
98
 
      my_decimal *val= val_decimal(&decimal_value);
 
102
      type::Decimal decimal_value;
 
103
      type::Decimal *val= val_decimal(&decimal_value);
99
104
      if (val)
100
 
        return !my_decimal_is_zero(val);
 
105
        return not val->isZero();
101
106
      return false;
102
107
    }
 
108
 
103
109
    case REAL_RESULT:
104
110
    case STRING_RESULT:
105
111
      return val_real() != 0.0;
 
112
 
106
113
    case ROW_RESULT:
107
 
    default:
108
114
      assert(0);
109
 
      return false;
 
115
      abort();
110
116
  }
 
117
 
 
118
  assert(0);
 
119
  abort();
111
120
}
112
121
 
113
122
String *Item::val_string_from_real(String *str)
132
141
 
133
142
String *Item::val_string_from_decimal(String *str)
134
143
{
135
 
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
 
144
  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
136
145
  if (null_value)
137
146
    return NULL;
138
147
 
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);
 
148
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
149
  class_decimal2string(&dec_buf, 0, str);
141
150
  return str;
142
151
}
143
152
 
144
 
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
 
153
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
145
154
{
146
155
  double nr= val_real();
147
156
  if (null_value)
148
157
    return NULL;
149
158
 
150
 
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
159
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
151
160
  return (decimal_value);
152
161
}
153
162
 
154
 
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
 
163
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
155
164
{
156
165
  int64_t nr= val_int();
157
166
  if (null_value)
158
167
    return NULL;
159
168
 
160
 
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
169
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
161
170
  return decimal_value;
162
171
}
163
172
 
164
 
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
 
173
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
165
174
{
166
175
  String *res;
167
176
  char *end_ptr;
169
178
    return NULL;
170
179
 
171
180
  end_ptr= (char*) res->ptr()+ res->length();
172
 
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
 
181
  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
173
182
                     res->ptr(), 
174
183
                     res->length(), 
175
 
                     res->charset(),
176
 
                     decimal_value) & E_DEC_BAD_NUM)
 
184
                     res->charset()) & E_DEC_BAD_NUM)
177
185
  {
178
 
    push_warning_printf(current_session, 
 
186
    push_warning_printf(&getSession(), 
179
187
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
180
188
                        ER_TRUNCATED_WRONG_VALUE,
181
189
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
184
192
  return decimal_value;
185
193
}
186
194
 
187
 
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
 
195
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
188
196
{
189
197
  assert(fixed);
190
 
  DRIZZLE_TIME ltime;
191
 
  if (get_date(&ltime, TIME_FUZZY_DATE))
 
198
  type::Time ltime;
 
199
  if (get_date(ltime, TIME_FUZZY_DATE))
192
200
  {
193
 
    my_decimal_set_zero(decimal_value);
 
201
    decimal_value->set_zero();
194
202
    null_value= 1;                               // set NULL, stop processing
195
203
    return NULL;
196
204
  }
197
 
  return date2my_decimal(&ltime, decimal_value);
 
205
  return date2_class_decimal(&ltime, decimal_value);
198
206
}
199
207
 
200
 
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
 
208
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
201
209
{
202
210
  assert(fixed);
203
 
  DRIZZLE_TIME ltime;
204
 
  if (get_time(&ltime))
 
211
  type::Time ltime;
 
212
  if (get_time(ltime))
205
213
  {
206
 
    my_decimal_set_zero(decimal_value);
 
214
    decimal_value->set_zero();
207
215
    return NULL;
208
216
  }
209
 
  return date2my_decimal(&ltime, decimal_value);
 
217
  return date2_class_decimal(&ltime, decimal_value);
210
218
}
211
219
 
212
220
double Item::val_real_from_decimal()
213
221
{
214
222
  /* Note that fix_fields may not be called for Item_avg_field items */
215
223
  double result;
216
 
  my_decimal value_buff, *dec_val= val_decimal(&value_buff);
 
224
  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
217
225
  if (null_value)
218
226
    return 0.0;
219
 
  my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
 
227
  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
220
228
  return result;
221
229
}
222
230
 
224
232
{
225
233
  /* Note that fix_fields may not be called for Item_avg_field items */
226
234
  int64_t result;
227
 
  my_decimal value, *dec_val= val_decimal(&value);
 
235
  type::Decimal value, *dec_val= val_decimal(&value);
 
236
 
228
237
  if (null_value)
229
238
    return 0;
230
 
  my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
 
239
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
240
 
231
241
  return result;
232
242
}
233
243
 
234
 
int Item::save_time_in_field(Field *field)
 
244
bool Item::save_time_in_field(Field *field)
235
245
{
236
 
  DRIZZLE_TIME ltime;
237
 
  if (get_time(&ltime))
 
246
  type::Time ltime;
 
247
 
 
248
  if (get_time(ltime))
238
249
    return set_field_to_null(field);
 
250
 
239
251
  field->set_notnull();
240
 
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
 
252
 
 
253
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
241
254
}
242
255
 
243
 
int Item::save_date_in_field(Field *field)
 
256
bool Item::save_date_in_field(Field *field)
244
257
{
245
 
  DRIZZLE_TIME ltime;
246
 
  if (get_date(&ltime, TIME_FUZZY_DATE))
 
258
  type::Time ltime;
 
259
 
 
260
  if (get_date(ltime, TIME_FUZZY_DATE))
247
261
    return set_field_to_null(field);
 
262
 
248
263
  field->set_notnull();
249
 
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
 
264
 
 
265
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
250
266
}
251
267
 
252
268
/**
257
273
{
258
274
  if (null_value)
259
275
    return set_field_to_null(field);
 
276
 
260
277
  field->set_notnull();
 
278
 
261
279
  return field->store(result->ptr(), result->length(), collation.collation);
262
280
}
263
281
 
276
294
  with_sum_func(false),
277
295
  is_autogenerated_name(true),
278
296
  with_subselect(false),
279
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
 
297
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
 
298
  _session(*current_session)
280
299
{
281
300
  cmp_context= (Item_result)-1;
282
301
 
283
302
  /* Put item in free list so that we can free all items at end */
284
 
  Session *session= current_session;
285
 
  next= session->free_list;
286
 
  session->free_list= this;
 
303
  next= getSession().free_list;
 
304
  getSession().free_list= this;
287
305
 
288
306
  /*
289
307
    Item constructor can be called during execution other then SQL_COM
290
308
    command => we should check session->lex->current_select on zero (session->lex
291
309
    can be uninitialised)
292
310
  */
293
 
  if (session->lex->current_select)
 
311
  if (getSession().lex->current_select)
294
312
  {
295
 
    enum_parsing_place place= session->lex->current_select->parsing_place;
 
313
    enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
296
314
    if (place == SELECT_LIST || place == IN_HAVING)
297
 
      session->lex->current_select->select_n_having_items++;
 
315
      getSession().getLex()->current_select->select_n_having_items++;
298
316
  }
299
317
}
300
318
 
315
333
  is_autogenerated_name(item->is_autogenerated_name),
316
334
  with_subselect(item->with_subselect),
317
335
  collation(item->collation),
318
 
  cmp_context(item->cmp_context)
 
336
  cmp_context(item->cmp_context),
 
337
  _session(*session)
319
338
{
320
339
  /* Put this item in the session's free list */
321
 
  next= session->free_list;
322
 
  session->free_list= this;
 
340
  next= getSession().free_list;
 
341
  getSession().free_list= this;
323
342
}
324
343
 
325
344
uint32_t Item::float_length(uint32_t decimals_par) const
332
351
  Item_result restype= result_type();
333
352
 
334
353
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
335
 
    return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
354
    return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
336
355
               (uint32_t) DECIMAL_MAX_PRECISION);
337
356
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
338
357
}
339
358
 
340
359
int Item::decimal_int_part() const
341
360
{
342
 
  return my_decimal_int_part(decimal_precision(), decimals);
 
361
  return class_decimal_int_part(decimal_precision(), decimals);
343
362
}
344
363
 
345
364
void Item::print(String *str, enum_query_type)
417
436
    if (orig_len != length && ! is_autogenerated_name)
418
437
    {
419
438
      if (length == 0)
420
 
        push_warning_printf(current_session, 
 
439
        push_warning_printf(&getSession(), 
421
440
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
422
441
                            ER_NAME_BECOMES_EMPTY, 
423
442
                            ER(ER_NAME_BECOMES_EMPTY),
424
443
                            str + length - orig_len);
425
444
      else
426
 
        push_warning_printf(current_session, 
 
445
        push_warning_printf(&getSession(),
427
446
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
428
447
                            ER_REMOVED_SPACES, 
429
448
                            ER(ER_REMOVED_SPACES),
452
471
  return conv->safe ? conv : NULL;
453
472
}
454
473
 
455
 
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
474
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
456
475
{
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));
 
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
 
484
515
  return true;
485
516
}
486
517
 
487
 
bool Item::get_time(DRIZZLE_TIME *ltime)
 
518
bool Item::get_time(type::Time &ltime)
488
519
{
489
 
  char buff[40];
 
520
  char buff[type::Time::MAX_STRING_LENGTH];
490
521
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
491
 
  if (!(res=val_str(&tmp)) ||
492
 
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
 
522
  if (!(res=val_str(&tmp)) or
 
523
      str_to_time_with_warn(&getSession(), res->ptr(), res->length(), &ltime))
493
524
  {
494
 
    memset(ltime, 0, sizeof(*ltime));
 
525
    ltime.reset();
 
526
 
495
527
    return true;
496
528
  }
 
529
 
497
530
  return false;
498
531
}
499
532
 
500
 
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
533
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
501
534
{
502
 
  return get_date(ltime,fuzzydate);
 
535
  return get_date(ltime, fuzzydate);
503
536
}
504
537
 
505
538
bool Item::is_null()
816
849
  {
817
850
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
818
851
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
819
 
    prev_subselect_item->const_item_cache= 0;
 
852
    prev_subselect_item->const_item_cache= false;
820
853
  }
821
854
  {
822
855
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
831
864
    }
832
865
    else
833
866
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
834
 
    prev_subselect_item->const_item_cache= 0;
 
867
    prev_subselect_item->const_item_cache= false;
835
868
    mark_as_dependent(session, last_select, current_sel, resolved_item,
836
869
                      dependent);
837
870
  }
851
884
    - the found item on success
852
885
    - NULL if find_item is not in group_list
853
886
*/
854
 
static Item** find_field_in_group_list(Item *find_item, Order *group_list)
 
887
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
855
888
{
856
889
  const char *db_name;
857
890
  const char *table_name;
907
940
        if (cur_field->db_name && db_name)
908
941
        {
909
942
          /* If field_name is also qualified by a database name. */
910
 
          if (strcasecmp(cur_field->db_name, db_name))
 
943
          if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
 
944
          {
911
945
            /* Same field names, different databases. */
912
946
            return NULL;
 
947
          }
913
948
          ++cur_match_degree;
914
949
        }
915
950
      }
928
963
          best match, they must reference the same column, otherwise the field
929
964
          is ambiguous.
930
965
        */
931
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), current_session->where);
 
966
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
932
967
        return NULL;
933
968
      }
934
969
    }
936
971
 
937
972
  if (found_group)
938
973
    return found_group->item;
939
 
  else
940
 
    return NULL;
 
974
 
 
975
  return NULL;
941
976
}
942
977
 
943
978
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
964
999
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
965
1000
  if (select->having_fix_field && !ref->with_sum_func && group_list)
966
1001
  {
967
 
    group_by_ref= find_field_in_group_list(ref, group_list);
 
1002
    group_by_ref= find_field_in_group_list(session, ref, group_list);
968
1003
 
969
1004
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
970
1005
    if (group_by_ref && (select_ref != not_found_item) &&
973
1008
      ambiguous_fields= true;
974
1009
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
975
1010
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
976
 
                          current_session->where);
 
1011
                          session->where());
977
1012
 
978
1013
    }
979
1014
  }
1002
1037
}
1003
1038
 
1004
1039
void Item::init_make_field(SendField *tmp_field,
1005
 
                           enum enum_field_types field_type_arg)
 
1040
                           enum enum_field_types field_type_arg)
1006
1041
{
1007
1042
  char *empty_name= (char*) "";
1008
1043
  tmp_field->db_name=   empty_name;
1044
1079
  case REAL_RESULT:    
1045
1080
    return DRIZZLE_TYPE_DOUBLE;
1046
1081
  case ROW_RESULT:
1047
 
  default:
1048
1082
    assert(0);
1049
 
    return DRIZZLE_TYPE_VARCHAR;
1050
1083
  }
 
1084
 
 
1085
  abort();
1051
1086
}
1052
1087
 
1053
1088
bool Item::is_datetime()
1054
1089
{
1055
1090
  switch (field_type())
1056
1091
  {
 
1092
    case DRIZZLE_TYPE_TIME:
1057
1093
    case DRIZZLE_TYPE_DATE:
1058
1094
    case DRIZZLE_TYPE_DATETIME:
1059
1095
    case DRIZZLE_TYPE_TIMESTAMP:
 
1096
    case DRIZZLE_TYPE_MICROTIME:
1060
1097
      return true;
1061
 
    default:
1062
 
      break;
 
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;
1063
1109
  }
1064
 
  return false;
 
1110
 
 
1111
  assert(0);
 
1112
  abort();
1065
1113
}
1066
1114
 
1067
1115
String *Item::check_well_formed_result(String *str, bool send_error)
1074
1122
                                       str->length(), &well_formed_error);
1075
1123
  if (wlen < str->length())
1076
1124
  {
1077
 
    Session *session= current_session;
1078
1125
    char hexbuf[7];
1079
1126
    enum DRIZZLE_ERROR::enum_warning_level level;
1080
1127
    uint32_t diff= str->length() - wlen;
1091
1138
      null_value= 1;
1092
1139
      str= 0;
1093
1140
    }
1094
 
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
 
1141
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
1095
1142
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1096
1143
  }
1097
1144
  return str;
1146
1193
    The field functions defines a field to be not null if null_ptr is not 0
1147
1194
  */
1148
1195
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1149
 
  Field *field;
 
1196
  Field *field= NULL;
1150
1197
 
1151
1198
  switch (field_type()) {
1152
1199
  case DRIZZLE_TYPE_DECIMAL:
1156
1203
                                 0,
1157
1204
                                 Field::NONE,
1158
1205
                                 name,
1159
 
                                 decimals,
1160
 
                                 0,
1161
 
                                 unsigned_flag);
 
1206
                                 decimals);
1162
1207
    break;
1163
1208
  case DRIZZLE_TYPE_LONG:
1164
 
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1165
 
                          name, 0, unsigned_flag);
 
1209
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1166
1210
    break;
1167
1211
  case DRIZZLE_TYPE_LONGLONG:
1168
 
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1169
 
                              name, 0, unsigned_flag);
 
1212
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1170
1213
    break;
1171
1214
  case DRIZZLE_TYPE_DOUBLE:
1172
1215
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1173
 
                            name, decimals, 0, unsigned_flag);
 
1216
                            name, decimals, 0, unsigned_flag);
1174
1217
    break;
1175
1218
  case DRIZZLE_TYPE_NULL:
1176
1219
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1178
1221
  case DRIZZLE_TYPE_DATE:
1179
1222
    field= new Field_date(maybe_null, name, &my_charset_bin);
1180
1223
    break;
 
1224
 
 
1225
  case DRIZZLE_TYPE_MICROTIME:
 
1226
    field= new field::Microtime(maybe_null, name);
 
1227
    break;
 
1228
 
1181
1229
  case DRIZZLE_TYPE_TIMESTAMP:
1182
 
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
 
1230
    field= new field::Epoch(maybe_null, name);
1183
1231
    break;
1184
1232
  case DRIZZLE_TYPE_DATETIME:
1185
1233
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1186
1234
    break;
1187
 
  default:
1188
 
    /* This case should never be chosen */
1189
 
    assert(0);
1190
 
    /* Fall through to make_string_field() */
 
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:
1191
1240
  case DRIZZLE_TYPE_ENUM:
1192
1241
  case DRIZZLE_TYPE_VARCHAR:
1193
1242
    return make_string_field(table);
1195
1244
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1196
1245
    break;                                      // Blob handled outside of case
1197
1246
  }
 
1247
  assert(field);
 
1248
 
1198
1249
  if (field)
1199
1250
    field->init(table);
1200
1251
  return field;
1246
1297
  }
1247
1298
  else if (result_type() == DECIMAL_RESULT)
1248
1299
  {
1249
 
    my_decimal decimal_value;
1250
 
    my_decimal *value= val_decimal(&decimal_value);
 
1300
    type::Decimal decimal_value;
 
1301
    type::Decimal *value= val_decimal(&decimal_value);
1251
1302
    if (null_value)
1252
1303
      return set_field_to_null_with_conversions(field, no_conversions);
1253
1304
    field->set_notnull();
1329
1380
  enum_field_types f_type;
1330
1381
 
1331
1382
  switch ((f_type=field_type())) {
1332
 
  default:
 
1383
  case DRIZZLE_TYPE_DATE:
1333
1384
  case DRIZZLE_TYPE_NULL:
1334
1385
  case DRIZZLE_TYPE_ENUM:
1335
1386
  case DRIZZLE_TYPE_BLOB:
1336
1387
  case DRIZZLE_TYPE_VARCHAR:
 
1388
  case DRIZZLE_TYPE_BOOLEAN:
 
1389
  case DRIZZLE_TYPE_UUID:
1337
1390
  case DRIZZLE_TYPE_DECIMAL:
1338
 
  {
1339
 
    String *res;
1340
 
    if ((res=val_str(buffer)))
1341
 
      result= client->store(res->ptr(),res->length());
1342
 
    break;
1343
 
  }
 
1391
    {
 
1392
      String *res;
 
1393
      if ((res=val_str(buffer)))
 
1394
        result= client->store(res->ptr(),res->length());
 
1395
      break;
 
1396
    }
1344
1397
  case DRIZZLE_TYPE_LONG:
1345
 
  {
1346
 
    int64_t nr;
1347
 
    nr= val_int();
1348
 
    if (!null_value)
1349
 
      result= client->store((int32_t)nr);
1350
 
    break;
1351
 
  }
 
1398
    {
 
1399
      int64_t nr;
 
1400
      nr= val_int();
 
1401
      if (!null_value)
 
1402
        result= client->store((int32_t)nr);
 
1403
      break;
 
1404
    }
1352
1405
  case DRIZZLE_TYPE_LONGLONG:
1353
 
  {
1354
 
    int64_t nr;
1355
 
    nr= val_int();
1356
 
    if (!null_value)
1357
1406
    {
1358
 
      if (unsigned_flag)
1359
 
        result= client->store((uint64_t)nr);
1360
 
      else
1361
 
        result= client->store((int64_t)nr);
 
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
1417
    }
1363
 
    break;
1364
 
  }
1365
1418
  case DRIZZLE_TYPE_DOUBLE:
1366
 
  {
1367
 
    double nr= val_real();
1368
 
    if (!null_value)
1369
 
      result= client->store(nr, decimals, buffer);
1370
 
    break;
1371
 
  }
 
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
    }
1372
1433
  case DRIZZLE_TYPE_DATETIME:
 
1434
  case DRIZZLE_TYPE_MICROTIME:
1373
1435
  case DRIZZLE_TYPE_TIMESTAMP:
1374
 
  {
1375
 
    DRIZZLE_TIME tm;
1376
 
    get_date(&tm, TIME_FUZZY_DATE);
1377
 
    if (!null_value)
1378
 
      result= client->store(&tm);
1379
 
    break;
1380
 
  }
 
1436
    {
 
1437
      type::Time tm;
 
1438
      get_date(tm, TIME_FUZZY_DATE);
 
1439
      if (!null_value)
 
1440
        result= client->store(&tm);
 
1441
      break;
 
1442
    }
1381
1443
  }
1382
1444
  if (null_value)
1383
1445
    result= client->store();
 
1446
 
1384
1447
  return result;
1385
1448
}
1386
1449
 
 
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
 
1387
1488
Item_result item_cmp_type(Item_result a,Item_result b)
1388
1489
{
1389
1490
  if (a == STRING_RESULT && b == STRING_RESULT)
1390
1491
    return STRING_RESULT;
 
1492
 
1391
1493
  if (a == INT_RESULT && b == INT_RESULT)
1392
1494
    return INT_RESULT;
1393
1495
  else if (a == ROW_RESULT || b == ROW_RESULT)
1394
1496
    return ROW_RESULT;
 
1497
 
1395
1498
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1396
1499
      (b == INT_RESULT || b == DECIMAL_RESULT))
1397
1500
    return DECIMAL_RESULT;
 
1501
 
1398
1502
  return REAL_RESULT;
1399
1503
}
1400
1504
 
1410
1514
 
1411
1515
  switch (res_type) {
1412
1516
  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
1420
1517
    {
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());
 
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;
1424
1530
    }
1425
 
    break;
1426
 
  }
1427
1531
  case INT_RESULT:
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
 
  }
 
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
    }
1436
1540
  case ROW_RESULT:
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).
 
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).
1442
1546
 
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 */
 
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 */
1466
1570
  case REAL_RESULT:
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
 
  }
 
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
    }
1475
1579
  case DECIMAL_RESULT:
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
 
  }
 
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
 
1489
1592
  if (new_item)
1490
1593
    session->change_item_tree(ref, new_item);
1491
1594
}
1504
1607
    item_result=item->val_str(&item_tmp);
1505
1608
    if (item->null_value)
1506
1609
      return 1;                                 // This must be true
1507
 
    field->val_str(&field_tmp);
1508
 
    return !stringcmp(&field_tmp,item_result);
 
1610
    field->val_str_internal(&field_tmp);
 
1611
    return not stringcmp(&field_tmp,item_result);
1509
1612
  }
 
1613
 
1510
1614
  if (res_type == INT_RESULT)
1511
1615
    return 1;                                   // Both where of type int
 
1616
 
1512
1617
  if (res_type == DECIMAL_RESULT)
1513
1618
  {
1514
 
    my_decimal item_buf, *item_val,
 
1619
    type::Decimal item_buf, *item_val,
1515
1620
               field_buf, *field_val;
1516
1621
    item_val= item->val_decimal(&item_buf);
1517
1622
    if (item->null_value)
1518
1623
      return 1;                                 // This must be true
1519
1624
    field_val= field->val_decimal(&field_buf);
1520
 
    return !my_decimal_cmp(item_val, field_val);
 
1625
    return !class_decimal_cmp(item_val, field_val);
1521
1626
  }
 
1627
 
1522
1628
  double result= item->val_real();
1523
1629
  if (item->null_value)
1524
1630
    return 1;
 
1631
 
1525
1632
  return result == field->val_real();
1526
1633
}
1527
1634
 
1557
1664
                                         uint32_t convert_blob_length)
1558
1665
{
1559
1666
  bool maybe_null= item->maybe_null;
1560
 
  Field *new_field;
 
1667
  Field *new_field= NULL;
1561
1668
 
1562
1669
  switch (item->result_type()) {
1563
1670
  case REAL_RESULT:
1564
1671
    new_field= new Field_double(item->max_length, maybe_null,
1565
1672
                                item->name, item->decimals, true);
1566
1673
    break;
 
1674
 
1567
1675
  case INT_RESULT:
1568
1676
    /*
1569
1677
      Select an integer type with the minimal fit precision.
1570
1678
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1571
1679
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1572
 
      Field_long : make them Field_int64_t.
 
1680
      Int32 -> make them field::Int64.
1573
1681
    */
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);
 
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
    }
1577
1692
    else
1578
 
      new_field=new Field_long(item->max_length, maybe_null,
1579
 
                               item->name, item->unsigned_flag);
 
1693
    {
 
1694
      new_field= new field::Int32(item->max_length, maybe_null,
 
1695
                                  item->name, item->unsigned_flag);
 
1696
    }
 
1697
 
1580
1698
    break;
 
1699
 
1581
1700
  case STRING_RESULT:
1582
1701
    assert(item->collation.collation);
1583
1702
 
1587
1706
      To preserve type they needed to be handled separately.
1588
1707
    */
1589
1708
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
 
1709
        type == DRIZZLE_TYPE_TIME ||
 
1710
        type == DRIZZLE_TYPE_MICROTIME ||
1590
1711
        type == DRIZZLE_TYPE_DATE ||
1591
1712
        type == DRIZZLE_TYPE_TIMESTAMP)
1592
1713
    {
1610
1731
    }
1611
1732
    new_field->set_derivation(item->collation.derivation);
1612
1733
    break;
 
1734
 
1613
1735
  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)
1626
1736
    {
1627
 
      signed int overflow;
1628
 
 
1629
 
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
 
1737
      uint8_t dec= item->decimals;
 
1738
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
1739
      uint32_t len= item->max_length;
1630
1740
 
1631
1741
      /*
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
 
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
1745
      */
1637
1746
 
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
 
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;
1645
1775
    }
1646
1776
 
1647
 
    new_field= new Field_decimal(len,
1648
 
                                 maybe_null,
1649
 
                                 item->name,
1650
 
                                 dec,
1651
 
                                 item->unsigned_flag);
1652
 
    break;
1653
 
  }
1654
1777
  case ROW_RESULT:
1655
 
  default:
1656
1778
    // This case should never be choosen
1657
1779
    assert(0);
1658
 
    new_field= 0;
1659
 
    break;
 
1780
    abort();
1660
1781
  }
 
1782
 
1661
1783
  if (new_field)
1662
1784
    new_field->init(table);
1663
1785
 
1664
1786
  if (copy_func && item->is_result_field())
1665
1787
    *((*copy_func)++) = item;                   // Save for copy_funcs
 
1788
 
1666
1789
  if (modify_item)
1667
1790
    item->set_result_field(new_field);
 
1791
 
1668
1792
  if (item->type() == Item::NULL_ITEM)
1669
1793
    new_field->is_created_from_null_item= true;
 
1794
 
1670
1795
  return new_field;
1671
1796
}
1672
1797
 
1723
1848
        field->result_field= result;
1724
1849
    }
1725
1850
    else
 
1851
    {
1726
1852
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1727
1853
                                          orig_item ? orig_item->name :
1728
1854
                                          item->name,
1730
1856
                                          modify_item ? field :
1731
1857
                                          NULL,
1732
1858
                                          convert_blob_length);
 
1859
    }
1733
1860
    if (orig_type == Item::REF_ITEM && orig_modify)
1734
1861
      ((Item_ref*)orig_item)->set_result_field(result);
1735
1862
    if (field->field->eq_def(result))
1769
1896
  }
1770
1897
}
1771
1898
 
 
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
 
1772
1940
} /* namespace drizzled */