~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Mark Atwood
  • Date: 2010-06-24 03:15:21 UTC
  • mto: (1637.2.4 build)
  • mto: This revision was merged to the branch mainline in revision 1639.
  • Revision ID: me@mark.atwood.name-20100624031521-gafmppfbf5afm68w
new syslog module, with plugins for query log, error message, and SYSLOG() function

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;
866
830
                  0);
867
831
    }
868
832
    else
869
 
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
870
 
    prev_subselect_item->const_item_cache= false;
 
833
      prev_subselect_item->used_tables_cache|= found_field->table->map;
 
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_st *group_list)
891
855
{
892
856
  const char *db_name;
893
857
  const char *table_name;
894
858
  const char *field_name;
895
 
  Order *found_group= NULL;
 
859
  order_st *found_group= NULL;
896
860
  int found_match_degree= 0;
897
861
  Item_ident *cur_field;
898
862
  int cur_match_degree= 0;
918
882
 
919
883
  assert(field_name != 0);
920
884
 
921
 
  for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
885
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
922
886
  {
923
887
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
924
888
    {
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)
982
944
{
983
945
  Item **group_by_ref= NULL;
984
946
  Item **select_ref= NULL;
985
 
  Order *group_list= (Order*) select->group_list.first;
 
947
  order_st *group_list= (order_st*) select->group_list.first;
986
948
  bool ambiguous_fields= false;
987
949
  uint32_t counter;
988
950
  enum_resolution_type resolution;
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;
1152
1124
  Field *field;
1153
1125
  assert(collation.collation);
1154
1126
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1155
 
  {
1156
1127
    field= new Field_blob(max_length, maybe_null, name,
1157
1128
                          collation.collation);
1158
 
  }
1159
1129
  else
1160
 
  {
1161
 
    table->setVariableWidth();
1162
 
    field= new Field_varstring(max_length, maybe_null, name,
 
1130
    field= new Field_varstring(max_length, maybe_null, name, table->getMutableShare(),
1163
1131
                               collation.collation);
1164
 
  }
1165
1132
 
1166
1133
  if (field)
1167
1134
    field->init(table);
1174
1141
    The field functions defines a field to be not null if null_ptr is not 0
1175
1142
  */
1176
1143
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1177
 
  Field *field= NULL;
 
1144
  Field *field;
1178
1145
 
1179
1146
  switch (field_type()) {
1180
1147
  case DRIZZLE_TYPE_DECIMAL:
1184
1151
                                 0,
1185
1152
                                 Field::NONE,
1186
1153
                                 name,
1187
 
                                 decimals);
 
1154
                                 decimals,
 
1155
                                 0,
 
1156
                                 unsigned_flag);
1188
1157
    break;
1189
1158
  case DRIZZLE_TYPE_LONG:
1190
 
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1159
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1160
                          name, 0, unsigned_flag);
1191
1161
    break;
1192
1162
  case DRIZZLE_TYPE_LONGLONG:
1193
 
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1163
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1164
                              name, 0, unsigned_flag);
1194
1165
    break;
1195
1166
  case DRIZZLE_TYPE_DOUBLE:
1196
1167
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1197
 
                            name, decimals, 0, unsigned_flag);
 
1168
                            name, decimals, 0, unsigned_flag);
1198
1169
    break;
1199
1170
  case DRIZZLE_TYPE_NULL:
1200
 
    field= new Field_null((unsigned char*) 0, max_length, name);
 
1171
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1201
1172
    break;
1202
1173
  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
 
 
 
1174
    field= new Field_date(maybe_null, name, &my_charset_bin);
 
1175
    break;
1210
1176
  case DRIZZLE_TYPE_TIMESTAMP:
1211
 
    field= new field::Epoch(maybe_null, name);
 
1177
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1212
1178
    break;
1213
1179
  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:
 
1180
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
 
1181
    break;
 
1182
  default:
 
1183
    /* This case should never be chosen */
 
1184
    assert(0);
 
1185
    /* Fall through to make_string_field() */
1221
1186
  case DRIZZLE_TYPE_ENUM:
1222
1187
  case DRIZZLE_TYPE_VARCHAR:
1223
1188
    return make_string_field(table);
1224
1189
  case DRIZZLE_TYPE_BLOB:
 
1190
    if (this->type() == Item::TYPE_HOLDER)
 
1191
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
 
1192
                            1);
 
1193
    else
1225
1194
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1226
1195
    break;                                      // Blob handled outside of case
1227
1196
  }
1228
 
  assert(field);
1229
 
 
1230
1197
  if (field)
1231
1198
    field->init(table);
1232
1199
  return field;
1278
1245
  }
1279
1246
  else if (result_type() == DECIMAL_RESULT)
1280
1247
  {
1281
 
    type::Decimal decimal_value;
1282
 
    type::Decimal *value= val_decimal(&decimal_value);
 
1248
    my_decimal decimal_value;
 
1249
    my_decimal *value= val_decimal(&decimal_value);
1283
1250
    if (null_value)
1284
1251
      return set_field_to_null_with_conversions(field, no_conversions);
1285
1252
    field->set_notnull();
1361
1328
  enum_field_types f_type;
1362
1329
 
1363
1330
  switch ((f_type=field_type())) {
1364
 
  case DRIZZLE_TYPE_DATE:
 
1331
  default:
1365
1332
  case DRIZZLE_TYPE_NULL:
1366
1333
  case DRIZZLE_TYPE_ENUM:
1367
1334
  case DRIZZLE_TYPE_BLOB:
1368
1335
  case DRIZZLE_TYPE_VARCHAR:
1369
 
  case DRIZZLE_TYPE_BOOLEAN:
1370
 
  case DRIZZLE_TYPE_UUID:
1371
1336
  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
 
    }
 
1337
  {
 
1338
    String *res;
 
1339
    if ((res=val_str(buffer)))
 
1340
      result= client->store(res->ptr(),res->length());
 
1341
    break;
 
1342
  }
1378
1343
  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
 
    }
 
1344
  {
 
1345
    int64_t nr;
 
1346
    nr= val_int();
 
1347
    if (!null_value)
 
1348
      result= client->store((int32_t)nr);
 
1349
    break;
 
1350
  }
1386
1351
  case DRIZZLE_TYPE_LONGLONG:
 
1352
  {
 
1353
    int64_t nr;
 
1354
    nr= val_int();
 
1355
    if (!null_value)
1387
1356
    {
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;
 
1357
      if (unsigned_flag)
 
1358
        result= client->store((uint64_t)nr);
 
1359
      else
 
1360
        result= client->store((int64_t)nr);
1398
1361
    }
 
1362
    break;
 
1363
  }
1399
1364
  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
 
    }
 
1365
  {
 
1366
    double nr= val_real();
 
1367
    if (!null_value)
 
1368
      result= client->store(nr, decimals, buffer);
 
1369
    break;
 
1370
  }
1414
1371
  case DRIZZLE_TYPE_DATETIME:
1415
 
  case DRIZZLE_TYPE_MICROTIME:
1416
1372
  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
 
    }
 
1373
  {
 
1374
    DRIZZLE_TIME tm;
 
1375
    get_date(&tm, TIME_FUZZY_DATE);
 
1376
    if (!null_value)
 
1377
      result= client->store(&tm);
 
1378
    break;
 
1379
  }
1424
1380
  }
1425
1381
  if (null_value)
1426
1382
    result= client->store();
1427
 
 
1428
1383
  return result;
1429
1384
}
1430
1385
 
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
1386
Item_result item_cmp_type(Item_result a,Item_result b)
1470
1387
{
1471
1388
  if (a == STRING_RESULT && b == STRING_RESULT)
1472
1389
    return STRING_RESULT;
1473
 
 
1474
1390
  if (a == INT_RESULT && b == INT_RESULT)
1475
1391
    return INT_RESULT;
1476
1392
  else if (a == ROW_RESULT || b == ROW_RESULT)
1477
1393
    return ROW_RESULT;
1478
 
 
1479
1394
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1480
1395
      (b == INT_RESULT || b == DECIMAL_RESULT))
1481
1396
    return DECIMAL_RESULT;
1482
 
 
1483
1397
  return REAL_RESULT;
1484
1398
}
1485
1399
 
1495
1409
 
1496
1410
  switch (res_type) {
1497
1411
  case STRING_RESULT:
 
1412
  {
 
1413
    char buff[MAX_FIELD_WIDTH];
 
1414
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
 
1415
    result=item->val_str(&tmp);
 
1416
    if (item->null_value)
 
1417
      new_item= new Item_null(name);
 
1418
    else
1498
1419
    {
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;
 
1420
      uint32_t length= result->length();
 
1421
      char *tmp_str= memory::sql_strmake(result->ptr(), length);
 
1422
      new_item= new Item_string(name, tmp_str, length, result->charset());
1511
1423
    }
 
1424
    break;
 
1425
  }
1512
1426
  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
 
    }
 
1427
  {
 
1428
    int64_t result=item->val_int();
 
1429
    uint32_t length=item->max_length;
 
1430
    bool null_value=item->null_value;
 
1431
    new_item= (null_value ? (Item*) new Item_null(name) :
 
1432
               (Item*) new Item_int(name, result, length));
 
1433
    break;
 
1434
  }
1521
1435
  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).
 
1436
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
 
1437
  {
 
1438
    /*
 
1439
      Substitute constants only in Item_rows. Don't affect other Items
 
1440
      with ROW_RESULT (eg Item_singlerow_subselect).
1527
1441
 
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 */
 
1442
      For such Items more optimal is to detect if it is constant and replace
 
1443
      it with Item_row. This would optimize queries like this:
 
1444
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
 
1445
    */
 
1446
    Item_row *item_row= (Item_row*) item;
 
1447
    Item_row *comp_item_row= (Item_row*) comp_item;
 
1448
    uint32_t col;
 
1449
    new_item= 0;
 
1450
    /*
 
1451
      If item and comp_item are both Item_rows and have same number of cols
 
1452
      then process items in Item_row one by one.
 
1453
      We can't ignore NULL values here as this item may be used with <=>, in
 
1454
      which case NULL's are significant.
 
1455
    */
 
1456
    assert(item->result_type() == comp_item->result_type());
 
1457
    assert(item_row->cols() == comp_item_row->cols());
 
1458
    col= item_row->cols();
 
1459
    while (col-- > 0)
 
1460
      resolve_const_item(session, item_row->addr(col),
 
1461
                         comp_item_row->element_index(col));
 
1462
    break;
 
1463
  }
 
1464
  /* Fallthrough */
1551
1465
  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
 
    }
 
1466
  {                                             // It must REAL_RESULT
 
1467
    double result= item->val_real();
 
1468
    uint32_t length=item->max_length,decimals=item->decimals;
 
1469
    bool null_value=item->null_value;
 
1470
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
 
1471
               new Item_float(name, result, decimals, length));
 
1472
    break;
 
1473
  }
1560
1474
  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
 
 
 
1475
  {
 
1476
    my_decimal decimal_value;
 
1477
    my_decimal *result= item->val_decimal(&decimal_value);
 
1478
    uint32_t length= item->max_length, decimals= item->decimals;
 
1479
    bool null_value= item->null_value;
 
1480
    new_item= (null_value ?
 
1481
               (Item*) new Item_null(name) :
 
1482
               (Item*) new Item_decimal(name, result, length, decimals));
 
1483
    break;
 
1484
  }
 
1485
  default:
 
1486
    assert(0);
 
1487
  }
1573
1488
  if (new_item)
1574
1489
    session->change_item_tree(ref, new_item);
1575
1490
}
1588
1503
    item_result=item->val_str(&item_tmp);
1589
1504
    if (item->null_value)
1590
1505
      return 1;                                 // This must be true
1591
 
    field->val_str_internal(&field_tmp);
1592
 
    return not stringcmp(&field_tmp,item_result);
 
1506
    field->val_str(&field_tmp);
 
1507
    return !stringcmp(&field_tmp,item_result);
1593
1508
  }
1594
 
 
1595
1509
  if (res_type == INT_RESULT)
1596
1510
    return 1;                                   // Both where of type int
1597
 
 
1598
1511
  if (res_type == DECIMAL_RESULT)
1599
1512
  {
1600
 
    type::Decimal item_buf, *item_val,
 
1513
    my_decimal item_buf, *item_val,
1601
1514
               field_buf, *field_val;
1602
1515
    item_val= item->val_decimal(&item_buf);
1603
1516
    if (item->null_value)
1604
1517
      return 1;                                 // This must be true
1605
1518
    field_val= field->val_decimal(&field_buf);
1606
 
    return !class_decimal_cmp(item_val, field_val);
 
1519
    return !my_decimal_cmp(item_val, field_val);
1607
1520
  }
1608
 
 
1609
1521
  double result= item->val_real();
1610
1522
  if (item->null_value)
1611
1523
    return 1;
1612
 
 
1613
1524
  return result == field->val_real();
1614
1525
}
1615
1526
 
1645
1556
                                         uint32_t convert_blob_length)
1646
1557
{
1647
1558
  bool maybe_null= item->maybe_null;
1648
 
  Field *new_field= NULL;
 
1559
  Field *new_field;
1649
1560
 
1650
1561
  switch (item->result_type()) {
1651
1562
  case REAL_RESULT:
1652
1563
    new_field= new Field_double(item->max_length, maybe_null,
1653
1564
                                item->name, item->decimals, true);
1654
1565
    break;
1655
 
 
1656
1566
  case INT_RESULT:
1657
1567
    /*
1658
1568
      Select an integer type with the minimal fit precision.
1659
1569
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1660
1570
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1661
 
      Int32 -> make them field::Int64.
 
1571
      Field_long : make them Field_int64_t.
1662
1572
    */
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
 
    }
 
1573
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1574
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
1575
                                   item->name, item->unsigned_flag);
1673
1576
    else
1674
 
    {
1675
 
      new_field= new field::Int32(item->max_length, maybe_null,
1676
 
                                  item->name, item->unsigned_flag);
1677
 
    }
1678
 
 
 
1577
      new_field=new Field_long(item->max_length, maybe_null,
 
1578
                               item->name, item->unsigned_flag);
1679
1579
    break;
1680
 
 
1681
1580
  case STRING_RESULT:
1682
1581
    assert(item->collation.collation);
1683
1582
 
 
1583
    enum enum_field_types type;
1684
1584
    /*
1685
1585
      DATE/TIME fields have STRING_RESULT result type.
1686
1586
      To preserve type they needed to be handled separately.
1687
1587
    */
1688
 
    if (field::isDateTime(item->field_type()))
1689
 
    {
 
1588
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
 
1589
        type == DRIZZLE_TYPE_DATE ||
 
1590
        type == DRIZZLE_TYPE_TIMESTAMP)
1690
1591
      new_field= item->tmp_table_field_from_field_type(table, 1);
1691
 
      /*
1692
 
        Make sure that the blob fits into a Field_varstring which has
1693
 
        2-byte lenght.
1694
 
      */
1695
 
    }
 
1592
    /*
 
1593
      Make sure that the blob fits into a Field_varstring which has
 
1594
      2-byte lenght.
 
1595
    */
1696
1596
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1697
1597
             convert_blob_length <= Field_varstring::MAX_SIZE &&
1698
1598
             convert_blob_length)
1699
 
    {
1700
 
      table->setVariableWidth();
1701
1599
      new_field= new Field_varstring(convert_blob_length, maybe_null,
1702
 
                                     item->name, item->collation.collation);
1703
 
    }
 
1600
                                     item->name, table->getMutableShare(),
 
1601
                                     item->collation.collation);
1704
1602
    else
1705
 
    {
1706
1603
      new_field= item->make_string_field(table);
1707
 
    }
1708
1604
    new_field->set_derivation(item->collation.derivation);
1709
1605
    break;
1710
 
 
1711
1606
  case DECIMAL_RESULT:
 
1607
  {
 
1608
    uint8_t dec= item->decimals;
 
1609
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
1610
    uint32_t len= item->max_length;
 
1611
 
 
1612
    /*
 
1613
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
1614
      will always throw a warning. We must limit dec to
 
1615
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
1616
    */
 
1617
 
 
1618
    if (dec > 0)
1712
1619
    {
1713
 
      uint8_t dec= item->decimals;
1714
 
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1715
 
      uint32_t len= item->max_length;
 
1620
      signed int overflow;
 
1621
 
 
1622
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1716
1623
 
1717
1624
      /*
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.
 
1625
        If the value still overflows the field with the corrected dec,
 
1626
        we'll throw out decimals rather than integers. This is still
 
1627
        bad and of course throws a truncation warning.
 
1628
        +1: for decimal point
1721
1629
      */
1722
1630
 
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;
 
1631
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
1632
                                               item->unsigned_flag) - len;
 
1633
 
 
1634
      if (overflow > 0)
 
1635
        dec= max(0, dec - overflow);            // too long, discard fract
 
1636
      else
 
1637
        len-= item->decimals - dec;             // corrected value fits
1751
1638
    }
1752
1639
 
 
1640
    new_field= new Field_decimal(len,
 
1641
                                 maybe_null,
 
1642
                                 item->name,
 
1643
                                 dec,
 
1644
                                 item->unsigned_flag);
 
1645
    break;
 
1646
  }
1753
1647
  case ROW_RESULT:
 
1648
  default:
1754
1649
    // This case should never be choosen
1755
1650
    assert(0);
1756
 
    abort();
 
1651
    new_field= 0;
 
1652
    break;
1757
1653
  }
1758
 
 
1759
1654
  if (new_field)
1760
1655
    new_field->init(table);
1761
1656
 
1762
1657
  if (copy_func && item->is_result_field())
1763
1658
    *((*copy_func)++) = item;                   // Save for copy_funcs
1764
 
 
1765
1659
  if (modify_item)
1766
1660
    item->set_result_field(new_field);
1767
 
 
1768
1661
  if (item->type() == Item::NULL_ITEM)
1769
1662
    new_field->is_created_from_null_item= true;
1770
 
 
1771
1663
  return new_field;
1772
1664
}
1773
1665
 
1824
1716
        field->result_field= result;
1825
1717
    }
1826
1718
    else
1827
 
    {
1828
1719
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1829
1720
                                          orig_item ? orig_item->name :
1830
1721
                                          item->name,
1832
1723
                                          modify_item ? field :
1833
1724
                                          NULL,
1834
1725
                                          convert_blob_length);
1835
 
    }
1836
1726
    if (orig_type == Item::REF_ITEM && orig_modify)
1837
1727
      ((Item_ref*)orig_item)->set_result_field(result);
1838
1728
    if (field->field->eq_def(result))
1872
1762
  }
1873
1763
}
1874
1764
 
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
1765
} /* namespace drizzled */