~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2009-11-18 22:58:22 UTC
  • mto: (1223.1.1 push) (1226.1.2 push)
  • mto: This revision was merged to the branch mainline in revision 1224.
  • Revision ID: brian@gaz-20091118225822-4ryr9rviir23o0kr
Second pass through bugs related to CREATE TABLE LIKE

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"
 
20
#include "drizzled/server_includes.h"
21
21
#include "drizzled/sql_select.h"
22
22
#include "drizzled/error.h"
23
23
#include "drizzled/show.h"
30
30
#include "drizzled/sql_base.h"
31
31
#include "drizzled/util/convert.h"
32
32
#include "drizzled/plugin/client.h"
33
 
#include "drizzled/time_functions.h"
34
33
 
35
34
#include "drizzled/field/str.h"
36
35
#include "drizzled/field/num.h"
37
 
 
38
36
#include "drizzled/field/blob.h"
 
37
#include "drizzled/field/enum.h"
 
38
#include "drizzled/field/null.h"
39
39
#include "drizzled/field/date.h"
40
 
#include "drizzled/field/datetime.h"
41
40
#include "drizzled/field/decimal.h"
 
41
#include "drizzled/field/real.h"
42
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"
 
43
#include "drizzled/field/long.h"
 
44
#include "drizzled/field/int64_t.h"
 
45
#include "drizzled/field/num.h"
 
46
#include "drizzled/field/timestamp.h"
 
47
#include "drizzled/field/datetime.h"
52
48
#include "drizzled/field/varstring.h"
53
49
 
54
 
#include "drizzled/internal/m_string.h"
55
 
 
56
 
#include <cstdio>
57
50
#include <math.h>
58
51
#include <algorithm>
59
 
#include <float.h>
60
52
 
61
53
using namespace std;
62
 
 
63
 
namespace drizzled
64
 
{
 
54
using namespace drizzled;
65
55
 
66
56
const String my_null_string("NULL", 4, default_charset_info);
67
57
 
96
86
  {
97
87
    case INT_RESULT:
98
88
      return val_int() != 0;
99
 
 
100
89
    case DECIMAL_RESULT:
101
90
    {
102
 
      type::Decimal decimal_value;
103
 
      type::Decimal *val= val_decimal(&decimal_value);
 
91
      my_decimal decimal_value;
 
92
      my_decimal *val= val_decimal(&decimal_value);
104
93
      if (val)
105
 
        return not val->isZero();
 
94
        return !my_decimal_is_zero(val);
106
95
      return false;
107
96
    }
108
 
 
109
97
    case REAL_RESULT:
110
98
    case STRING_RESULT:
111
99
      return val_real() != 0.0;
112
 
 
113
100
    case ROW_RESULT:
 
101
    default:
114
102
      assert(0);
115
 
      abort();
 
103
      return false;
116
104
  }
117
 
 
118
 
  assert(0);
119
 
  abort();
120
105
}
121
106
 
122
107
String *Item::val_string_from_real(String *str)
141
126
 
142
127
String *Item::val_string_from_decimal(String *str)
143
128
{
144
 
  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
 
129
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
145
130
  if (null_value)
146
131
    return NULL;
147
132
 
148
 
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
149
 
  class_decimal2string(&dec_buf, 0, str);
 
133
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
134
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
150
135
  return str;
151
136
}
152
137
 
153
 
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
 
138
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
154
139
{
155
140
  double nr= val_real();
156
141
  if (null_value)
157
142
    return NULL;
158
143
 
159
 
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
144
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
160
145
  return (decimal_value);
161
146
}
162
147
 
163
 
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
 
148
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
164
149
{
165
150
  int64_t nr= val_int();
166
151
  if (null_value)
167
152
    return NULL;
168
153
 
169
 
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
154
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
170
155
  return decimal_value;
171
156
}
172
157
 
173
 
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
 
158
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
174
159
{
175
160
  String *res;
176
161
  char *end_ptr;
178
163
    return NULL;
179
164
 
180
165
  end_ptr= (char*) res->ptr()+ res->length();
181
 
  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
 
166
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
182
167
                     res->ptr(), 
183
168
                     res->length(), 
184
 
                     res->charset()) & E_DEC_BAD_NUM)
 
169
                     res->charset(),
 
170
                     decimal_value) & E_DEC_BAD_NUM)
185
171
  {
186
 
    push_warning_printf(&getSession(), 
 
172
    push_warning_printf(current_session, 
187
173
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
188
174
                        ER_TRUNCATED_WRONG_VALUE,
189
175
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
192
178
  return decimal_value;
193
179
}
194
180
 
195
 
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
 
181
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
196
182
{
197
183
  assert(fixed);
198
 
  type::Time ltime;
199
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
184
  DRIZZLE_TIME ltime;
 
185
  if (get_date(&ltime, TIME_FUZZY_DATE))
200
186
  {
201
 
    decimal_value->set_zero();
 
187
    my_decimal_set_zero(decimal_value);
202
188
    null_value= 1;                               // set NULL, stop processing
203
189
    return NULL;
204
190
  }
205
 
  return date2_class_decimal(&ltime, decimal_value);
 
191
  return date2my_decimal(&ltime, decimal_value);
206
192
}
207
193
 
208
 
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
 
194
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
209
195
{
210
196
  assert(fixed);
211
 
  type::Time ltime;
212
 
  if (get_time(ltime))
 
197
  DRIZZLE_TIME ltime;
 
198
  if (get_time(&ltime))
213
199
  {
214
 
    decimal_value->set_zero();
 
200
    my_decimal_set_zero(decimal_value);
215
201
    return NULL;
216
202
  }
217
 
  return date2_class_decimal(&ltime, decimal_value);
 
203
  return date2my_decimal(&ltime, decimal_value);
218
204
}
219
205
 
220
206
double Item::val_real_from_decimal()
221
207
{
222
208
  /* Note that fix_fields may not be called for Item_avg_field items */
223
209
  double result;
224
 
  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
 
210
  my_decimal value_buff, *dec_val= val_decimal(&value_buff);
225
211
  if (null_value)
226
212
    return 0.0;
227
 
  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
 
213
  my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
228
214
  return result;
229
215
}
230
216
 
232
218
{
233
219
  /* Note that fix_fields may not be called for Item_avg_field items */
234
220
  int64_t result;
235
 
  type::Decimal value, *dec_val= val_decimal(&value);
236
 
 
 
221
  my_decimal value, *dec_val= val_decimal(&value);
237
222
  if (null_value)
238
223
    return 0;
239
 
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
240
 
 
 
224
  my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
241
225
  return result;
242
226
}
243
227
 
244
 
bool Item::save_time_in_field(Field *field)
 
228
int Item::save_time_in_field(Field *field)
245
229
{
246
 
  type::Time ltime;
247
 
 
248
 
  if (get_time(ltime))
 
230
  DRIZZLE_TIME ltime;
 
231
  if (get_time(&ltime))
249
232
    return set_field_to_null(field);
250
 
 
251
233
  field->set_notnull();
252
 
 
253
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
 
234
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
254
235
}
255
236
 
256
 
bool Item::save_date_in_field(Field *field)
 
237
int Item::save_date_in_field(Field *field)
257
238
{
258
 
  type::Time ltime;
259
 
 
260
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
239
  DRIZZLE_TIME ltime;
 
240
  if (get_date(&ltime, TIME_FUZZY_DATE))
261
241
    return set_field_to_null(field);
262
 
 
263
242
  field->set_notnull();
264
 
 
265
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
 
243
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
266
244
}
267
245
 
268
246
/**
273
251
{
274
252
  if (null_value)
275
253
    return set_field_to_null(field);
276
 
 
277
254
  field->set_notnull();
278
 
 
279
255
  return field->store(result->ptr(), result->length(), collation.collation);
280
256
}
281
257
 
294
270
  with_sum_func(false),
295
271
  is_autogenerated_name(true),
296
272
  with_subselect(false),
297
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
298
 
  _session(*current_session)
 
273
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
299
274
{
300
275
  cmp_context= (Item_result)-1;
301
276
 
302
277
  /* Put item in free list so that we can free all items at end */
303
 
  next= getSession().free_list;
304
 
  getSession().free_list= this;
 
278
  Session *session= current_session;
 
279
  next= session->free_list;
 
280
  session->free_list= this;
305
281
 
306
282
  /*
307
283
    Item constructor can be called during execution other then SQL_COM
308
284
    command => we should check session->lex->current_select on zero (session->lex
309
285
    can be uninitialised)
310
286
  */
311
 
  if (getSession().lex->current_select)
 
287
  if (session->lex->current_select)
312
288
  {
313
 
    enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
 
289
    enum_parsing_place place= session->lex->current_select->parsing_place;
314
290
    if (place == SELECT_LIST || place == IN_HAVING)
315
 
      getSession().getLex()->current_select->select_n_having_items++;
 
291
      session->lex->current_select->select_n_having_items++;
316
292
  }
317
293
}
318
294
 
333
309
  is_autogenerated_name(item->is_autogenerated_name),
334
310
  with_subselect(item->with_subselect),
335
311
  collation(item->collation),
336
 
  cmp_context(item->cmp_context),
337
 
  _session(*session)
 
312
  cmp_context(item->cmp_context)
338
313
{
339
314
  /* Put this item in the session's free list */
340
 
  next= getSession().free_list;
341
 
  getSession().free_list= this;
342
 
}
343
 
 
344
 
uint32_t Item::float_length(uint32_t decimals_par) const
345
 
{
346
 
  return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
 
315
  next= session->free_list;
 
316
  session->free_list= this;
347
317
}
348
318
 
349
319
uint32_t Item::decimal_precision() const
351
321
  Item_result restype= result_type();
352
322
 
353
323
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
354
 
    return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
324
    return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
355
325
               (uint32_t) DECIMAL_MAX_PRECISION);
356
326
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
357
327
}
358
328
 
359
329
int Item::decimal_int_part() const
360
330
{
361
 
  return class_decimal_int_part(decimal_precision(), decimals);
 
331
  return my_decimal_int_part(decimal_precision(), decimals);
362
332
}
363
333
 
364
334
void Item::print(String *str, enum_query_type)
436
406
    if (orig_len != length && ! is_autogenerated_name)
437
407
    {
438
408
      if (length == 0)
439
 
        push_warning_printf(&getSession(), 
 
409
        push_warning_printf(current_session, 
440
410
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
441
411
                            ER_NAME_BECOMES_EMPTY, 
442
412
                            ER(ER_NAME_BECOMES_EMPTY),
443
413
                            str + length - orig_len);
444
414
      else
445
 
        push_warning_printf(&getSession(),
 
415
        push_warning_printf(current_session, 
446
416
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
447
417
                            ER_REMOVED_SPACES, 
448
418
                            ER(ER_REMOVED_SPACES),
449
419
                            str + length - orig_len);
450
420
    }
451
421
  }
452
 
  name= memory::sql_strmake(str, length);
 
422
  name= sql_strmake(str, length);
453
423
}
454
424
 
455
425
bool Item::eq(const Item *item, bool) const
471
441
  return conv->safe ? conv : NULL;
472
442
}
473
443
 
474
 
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
 
444
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
475
445
{
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
 
 
 
446
  if (result_type() == STRING_RESULT)
 
447
  {
 
448
    char buff[40];
 
449
    String tmp(buff,sizeof(buff), &my_charset_bin),*res;
 
450
    if (!(res=val_str(&tmp)) ||
 
451
        str_to_datetime_with_warn(res->ptr(), res->length(),
 
452
                                  ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
453
      goto err;
 
454
  }
 
455
  else
 
456
  {
 
457
    int64_t value= val_int();
 
458
    int was_cut;
 
459
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
 
460
    {
 
461
      char buff[22], *end;
 
462
      end= int64_t10_to_str(value, buff, -10);
 
463
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
464
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
 
465
                                   NULL);
 
466
      goto err;
 
467
    }
 
468
  }
 
469
  return false;
 
470
 
 
471
err:
 
472
  memset(ltime, 0, sizeof(*ltime));
515
473
  return true;
516
474
}
517
475
 
518
 
bool Item::get_time(type::Time &ltime)
 
476
bool Item::get_time(DRIZZLE_TIME *ltime)
519
477
{
520
 
  char buff[type::Time::MAX_STRING_LENGTH];
 
478
  char buff[40];
521
479
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
522
 
  if (!(res=val_str(&tmp)) or
523
 
      str_to_time_with_warn(&getSession(), res->ptr(), res->length(), &ltime))
 
480
  if (!(res=val_str(&tmp)) ||
 
481
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
524
482
  {
525
 
    ltime.reset();
526
 
 
 
483
    memset(ltime, 0, sizeof(*ltime));
527
484
    return true;
528
485
  }
529
 
 
530
486
  return false;
531
487
}
532
488
 
533
 
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
 
489
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
534
490
{
535
 
  return get_date(ltime, fuzzydate);
 
491
  return get_date(ltime,fuzzydate);
536
492
}
537
493
 
538
494
bool Item::is_null()
648
604
  return false;
649
605
}
650
606
 
 
607
bool Item::reset_query_id_processor(unsigned char *)
 
608
{
 
609
  return false;
 
610
}
 
611
 
651
612
bool Item::register_field_in_read_map(unsigned char *)
652
613
{
653
614
  return false;
727
688
  return test(is_expensive_cache);
728
689
}
729
690
 
 
691
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
 
692
{
 
693
  int res;
 
694
  Table *table= field->table;
 
695
  Session *session= table->in_use;
 
696
  enum_check_fields tmp= session->count_cuted_fields;
 
697
  ulong sql_mode= session->variables.sql_mode;
 
698
  session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
 
699
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
700
  res= save_in_field(field, no_conversions);
 
701
  session->count_cuted_fields= tmp;
 
702
  session->variables.sql_mode= sql_mode;
 
703
  return res;
 
704
}
 
705
 
730
706
/*
731
707
 need a special class to adjust printing : references to aggregate functions
732
708
 must not be printed as refs because the aggregate functions that are added to
821
797
  if (session->lex->describe & DESCRIBE_EXTENDED)
822
798
  {
823
799
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
824
 
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
 
800
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
825
801
            db_name, (db_name[0] ? "." : ""),
826
802
            table_name, (table_name [0] ? "." : ""),
827
803
            resolved_item->field_name,
849
825
  {
850
826
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
851
827
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
852
 
    prev_subselect_item->const_item_cache= false;
 
828
    prev_subselect_item->const_item_cache= 0;
853
829
  }
854
830
  {
855
831
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
863
839
                  0);
864
840
    }
865
841
    else
866
 
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
867
 
    prev_subselect_item->const_item_cache= false;
 
842
      prev_subselect_item->used_tables_cache|= found_field->table->map;
 
843
    prev_subselect_item->const_item_cache= 0;
868
844
    mark_as_dependent(session, last_select, current_sel, resolved_item,
869
845
                      dependent);
870
846
  }
884
860
    - the found item on success
885
861
    - NULL if find_item is not in group_list
886
862
*/
887
 
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
 
863
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
888
864
{
889
865
  const char *db_name;
890
866
  const char *table_name;
891
867
  const char *field_name;
892
 
  Order *found_group= NULL;
 
868
  order_st *found_group= NULL;
893
869
  int found_match_degree= 0;
894
870
  Item_ident *cur_field;
895
871
  int cur_match_degree= 0;
915
891
 
916
892
  assert(field_name != 0);
917
893
 
918
 
  for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
894
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
919
895
  {
920
896
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
921
897
    {
940
916
        if (cur_field->db_name && db_name)
941
917
        {
942
918
          /* If field_name is also qualified by a database name. */
943
 
          if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
944
 
          {
 
919
          if (strcmp(cur_field->db_name, db_name))
945
920
            /* Same field names, different databases. */
946
921
            return NULL;
947
 
          }
948
922
          ++cur_match_degree;
949
923
        }
950
924
      }
963
937
          best match, they must reference the same column, otherwise the field
964
938
          is ambiguous.
965
939
        */
966
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
 
940
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), current_session->where);
967
941
        return NULL;
968
942
      }
969
943
    }
971
945
 
972
946
  if (found_group)
973
947
    return found_group->item;
974
 
 
975
 
  return NULL;
 
948
  else
 
949
    return NULL;
976
950
}
977
951
 
978
952
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
979
953
{
980
954
  Item **group_by_ref= NULL;
981
955
  Item **select_ref= NULL;
982
 
  Order *group_list= (Order*) select->group_list.first;
 
956
  order_st *group_list= (order_st*) select->group_list.first;
983
957
  bool ambiguous_fields= false;
984
958
  uint32_t counter;
985
959
  enum_resolution_type resolution;
988
962
    Search for a column or derived column named as 'ref' in the SELECT
989
963
    clause of the current select.
990
964
  */
991
 
  if (!(select_ref= find_item_in_list(session,
992
 
                                      ref, *(select->get_item_list()),
 
965
  if (!(select_ref= find_item_in_list(ref, *(select->get_item_list()),
993
966
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
994
967
                                      &resolution)))
995
968
    return NULL; /* Some error occurred. */
999
972
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
1000
973
  if (select->having_fix_field && !ref->with_sum_func && group_list)
1001
974
  {
1002
 
    group_by_ref= find_field_in_group_list(session, ref, group_list);
 
975
    group_by_ref= find_field_in_group_list(ref, group_list);
1003
976
 
1004
977
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
1005
978
    if (group_by_ref && (select_ref != not_found_item) &&
1008
981
      ambiguous_fields= true;
1009
982
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
1010
983
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
1011
 
                          session->where());
 
984
                          current_session->where);
1012
985
 
1013
986
    }
1014
987
  }
1037
1010
}
1038
1011
 
1039
1012
void Item::init_make_field(SendField *tmp_field,
1040
 
                           enum enum_field_types field_type_arg)
 
1013
                           enum enum_field_types field_type_arg)
1041
1014
{
1042
1015
  char *empty_name= (char*) "";
1043
1016
  tmp_field->db_name=   empty_name;
1079
1052
  case REAL_RESULT:    
1080
1053
    return DRIZZLE_TYPE_DOUBLE;
1081
1054
  case ROW_RESULT:
 
1055
  default:
1082
1056
    assert(0);
 
1057
    return DRIZZLE_TYPE_VARCHAR;
1083
1058
  }
1084
 
 
1085
 
  abort();
1086
1059
}
1087
1060
 
1088
1061
bool Item::is_datetime()
1089
1062
{
1090
1063
  switch (field_type())
1091
1064
  {
1092
 
    case DRIZZLE_TYPE_TIME:
1093
1065
    case DRIZZLE_TYPE_DATE:
1094
1066
    case DRIZZLE_TYPE_DATETIME:
1095
1067
    case DRIZZLE_TYPE_TIMESTAMP:
1096
 
    case DRIZZLE_TYPE_MICROTIME:
1097
1068
      return true;
1098
 
    case DRIZZLE_TYPE_BLOB:
1099
 
    case DRIZZLE_TYPE_VARCHAR:
1100
 
    case DRIZZLE_TYPE_DOUBLE:
1101
 
    case DRIZZLE_TYPE_DECIMAL:
1102
 
    case DRIZZLE_TYPE_ENUM:
1103
 
    case DRIZZLE_TYPE_LONG:
1104
 
    case DRIZZLE_TYPE_LONGLONG:
1105
 
    case DRIZZLE_TYPE_NULL:
1106
 
    case DRIZZLE_TYPE_UUID:
1107
 
    case DRIZZLE_TYPE_BOOLEAN:
1108
 
      return false;
 
1069
    default:
 
1070
      break;
1109
1071
  }
1110
 
 
1111
 
  assert(0);
1112
 
  abort();
 
1072
  return false;
1113
1073
}
1114
1074
 
1115
1075
String *Item::check_well_formed_result(String *str, bool send_error)
1122
1082
                                       str->length(), &well_formed_error);
1123
1083
  if (wlen < str->length())
1124
1084
  {
 
1085
    Session *session= current_session;
1125
1086
    char hexbuf[7];
1126
1087
    enum DRIZZLE_ERROR::enum_warning_level level;
1127
1088
    uint32_t diff= str->length() - wlen;
1138
1099
      null_value= 1;
1139
1100
      str= 0;
1140
1101
    }
1141
 
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
 
1102
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1142
1103
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1143
1104
  }
1144
1105
  return str;
1171
1132
  Field *field;
1172
1133
  assert(collation.collation);
1173
1134
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1174
 
  {
1175
1135
    field= new Field_blob(max_length, maybe_null, name,
1176
1136
                          collation.collation);
1177
 
  }
1178
1137
  else
1179
 
  {
1180
 
    table->setVariableWidth();
1181
 
    field= new Field_varstring(max_length, maybe_null, name,
 
1138
    field= new Field_varstring(max_length, maybe_null, name, table->s,
1182
1139
                               collation.collation);
1183
 
  }
1184
1140
 
1185
1141
  if (field)
1186
1142
    field->init(table);
1193
1149
    The field functions defines a field to be not null if null_ptr is not 0
1194
1150
  */
1195
1151
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1196
 
  Field *field= NULL;
 
1152
  Field *field;
1197
1153
 
1198
1154
  switch (field_type()) {
1199
1155
  case DRIZZLE_TYPE_DECIMAL:
1203
1159
                                 0,
1204
1160
                                 Field::NONE,
1205
1161
                                 name,
1206
 
                                 decimals);
 
1162
                                 decimals,
 
1163
                                 0,
 
1164
                                 unsigned_flag);
1207
1165
    break;
1208
1166
  case DRIZZLE_TYPE_LONG:
1209
 
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1167
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1168
                          name, 0, unsigned_flag);
1210
1169
    break;
1211
1170
  case DRIZZLE_TYPE_LONGLONG:
1212
 
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1171
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1172
                              name, 0, unsigned_flag);
1213
1173
    break;
1214
1174
  case DRIZZLE_TYPE_DOUBLE:
1215
1175
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1216
 
                            name, decimals, 0, unsigned_flag);
 
1176
                            name, decimals, 0, unsigned_flag);
1217
1177
    break;
1218
1178
  case DRIZZLE_TYPE_NULL:
1219
1179
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1221
1181
  case DRIZZLE_TYPE_DATE:
1222
1182
    field= new Field_date(maybe_null, name, &my_charset_bin);
1223
1183
    break;
1224
 
 
1225
 
  case DRIZZLE_TYPE_MICROTIME:
1226
 
    field= new field::Microtime(maybe_null, name);
1227
 
    break;
1228
 
 
1229
1184
  case DRIZZLE_TYPE_TIMESTAMP:
1230
 
    field= new field::Epoch(maybe_null, name);
 
1185
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1231
1186
    break;
1232
1187
  case DRIZZLE_TYPE_DATETIME:
1233
1188
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1234
1189
    break;
1235
 
  case DRIZZLE_TYPE_TIME:
1236
 
    field= new field::Time(maybe_null, name, &my_charset_bin);
1237
 
    break;
1238
 
  case DRIZZLE_TYPE_BOOLEAN:
1239
 
  case DRIZZLE_TYPE_UUID:
 
1190
  default:
 
1191
    /* This case should never be chosen */
 
1192
    assert(0);
 
1193
    /* Fall through to make_string_field() */
1240
1194
  case DRIZZLE_TYPE_ENUM:
1241
1195
  case DRIZZLE_TYPE_VARCHAR:
1242
1196
    return make_string_field(table);
1243
1197
  case DRIZZLE_TYPE_BLOB:
 
1198
    if (this->type() == Item::TYPE_HOLDER)
 
1199
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
 
1200
                            1);
 
1201
    else
1244
1202
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1245
1203
    break;                                      // Blob handled outside of case
1246
1204
  }
1247
 
  assert(field);
1248
 
 
1249
1205
  if (field)
1250
1206
    field->init(table);
1251
1207
  return field;
1297
1253
  }
1298
1254
  else if (result_type() == DECIMAL_RESULT)
1299
1255
  {
1300
 
    type::Decimal decimal_value;
1301
 
    type::Decimal *value= val_decimal(&decimal_value);
 
1256
    my_decimal decimal_value;
 
1257
    my_decimal *value= val_decimal(&decimal_value);
1302
1258
    if (null_value)
1303
1259
      return set_field_to_null_with_conversions(field, no_conversions);
1304
1260
    field->set_notnull();
1315
1271
  return error;
1316
1272
}
1317
1273
 
1318
 
/**
1319
 
  Check if an item is a constant one and can be cached.
1320
 
 
1321
 
  @param arg [out] TRUE <=> Cache this item.
1322
 
 
1323
 
  @return TRUE  Go deeper in item tree.
1324
 
  @return FALSE Don't go deeper in item tree.
1325
 
*/
1326
 
 
1327
 
bool Item::cache_const_expr_analyzer(unsigned char **arg)
1328
 
{
1329
 
  bool *cache_flag= (bool*)*arg;
1330
 
  if (!*cache_flag)
1331
 
  {
1332
 
    Item *item= real_item();
1333
 
    /*
1334
 
      Cache constant items unless it's a basic constant, constant field or
1335
 
      a subselect (they use their own cache).
1336
 
    */
1337
 
    if (const_item() &&
1338
 
        !(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
1339
 
          item->type() == SUBSELECT_ITEM ||
1340
 
           /*
1341
 
             Do not cache GET_USER_VAR() function as its const_item() may
1342
 
             return TRUE for the current thread but it still may change
1343
 
             during the execution.
1344
 
           */
1345
 
          (item->type() == Item::FUNC_ITEM &&
1346
 
           ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1347
 
      *cache_flag= true;
1348
 
    return true;
1349
 
  }
1350
 
  return false;
1351
 
}
1352
 
 
1353
 
/**
1354
 
  Cache item if needed.
1355
 
 
1356
 
  @param arg   TRUE <=> Cache this item.
1357
 
 
1358
 
  @return cache if cache needed.
1359
 
  @return this otherwise.
1360
 
*/
1361
 
 
1362
 
Item* Item::cache_const_expr_transformer(unsigned char *arg)
1363
 
{
1364
 
  if (*(bool*)arg)
1365
 
  {
1366
 
    *((bool*)arg)= false;
1367
 
    Item_cache *cache= Item_cache::get_cache(this);
1368
 
    if (!cache)
1369
 
      return NULL;
1370
 
    cache->setup(this);
1371
 
    cache->store(this);
1372
 
    return cache;
1373
 
  }
1374
 
  return this;
1375
 
}
1376
 
 
1377
1274
bool Item::send(plugin::Client *client, String *buffer)
1378
1275
{
1379
1276
  bool result= false;
1380
1277
  enum_field_types f_type;
1381
1278
 
1382
1279
  switch ((f_type=field_type())) {
1383
 
  case DRIZZLE_TYPE_DATE:
 
1280
  default:
1384
1281
  case DRIZZLE_TYPE_NULL:
1385
1282
  case DRIZZLE_TYPE_ENUM:
1386
1283
  case DRIZZLE_TYPE_BLOB:
1387
1284
  case DRIZZLE_TYPE_VARCHAR:
1388
 
  case DRIZZLE_TYPE_BOOLEAN:
1389
 
  case DRIZZLE_TYPE_UUID:
1390
1285
  case DRIZZLE_TYPE_DECIMAL:
1391
 
    {
1392
 
      String *res;
1393
 
      if ((res=val_str(buffer)))
1394
 
        result= client->store(res->ptr(),res->length());
1395
 
      break;
1396
 
    }
 
1286
  {
 
1287
    String *res;
 
1288
    if ((res=val_str(buffer)))
 
1289
      result= client->store(res->ptr(),res->length());
 
1290
    break;
 
1291
  }
1397
1292
  case DRIZZLE_TYPE_LONG:
1398
 
    {
1399
 
      int64_t nr;
1400
 
      nr= val_int();
1401
 
      if (!null_value)
1402
 
        result= client->store((int32_t)nr);
1403
 
      break;
1404
 
    }
 
1293
  {
 
1294
    int64_t nr;
 
1295
    nr= val_int();
 
1296
    if (!null_value)
 
1297
      result= client->store((int32_t)nr);
 
1298
    break;
 
1299
  }
1405
1300
  case DRIZZLE_TYPE_LONGLONG:
 
1301
  {
 
1302
    int64_t nr;
 
1303
    nr= val_int();
 
1304
    if (!null_value)
1406
1305
    {
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;
 
1306
      if (unsigned_flag)
 
1307
        result= client->store((uint64_t)nr);
 
1308
      else
 
1309
        result= client->store((int64_t)nr);
1417
1310
    }
 
1311
    break;
 
1312
  }
1418
1313
  case DRIZZLE_TYPE_DOUBLE:
1419
 
    {
1420
 
      double nr= val_real();
1421
 
      if (!null_value)
1422
 
        result= client->store(nr, decimals, buffer);
1423
 
      break;
1424
 
    }
1425
 
  case DRIZZLE_TYPE_TIME:
1426
 
    {
1427
 
      type::Time tm;
1428
 
      get_time(tm);
1429
 
      if (not null_value)
1430
 
        result= client->store(&tm);
1431
 
      break;
1432
 
    }
 
1314
  {
 
1315
    double nr= val_real();
 
1316
    if (!null_value)
 
1317
      result= client->store(nr, decimals, buffer);
 
1318
    break;
 
1319
  }
1433
1320
  case DRIZZLE_TYPE_DATETIME:
1434
 
  case DRIZZLE_TYPE_MICROTIME:
1435
1321
  case DRIZZLE_TYPE_TIMESTAMP:
1436
 
    {
1437
 
      type::Time tm;
1438
 
      get_date(tm, TIME_FUZZY_DATE);
1439
 
      if (!null_value)
1440
 
        result= client->store(&tm);
1441
 
      break;
1442
 
    }
 
1322
  {
 
1323
    DRIZZLE_TIME tm;
 
1324
    get_date(&tm, TIME_FUZZY_DATE);
 
1325
    if (!null_value)
 
1326
      result= client->store(&tm);
 
1327
    break;
 
1328
  }
1443
1329
  }
1444
1330
  if (null_value)
1445
1331
    result= client->store();
1446
 
 
1447
1332
  return result;
1448
1333
}
1449
1334
 
1450
 
uint32_t Item::max_char_length() const
1451
 
{
1452
 
  return max_length / collation.collation->mbmaxlen;
1453
 
}
1454
 
 
1455
 
void Item::fix_length_and_charset(uint32_t max_char_length_arg, CHARSET_INFO *cs)
1456
 
1457
 
  max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1458
 
  collation.collation= cs;
1459
 
}
1460
 
 
1461
 
void Item::fix_char_length(uint32_t max_char_length_arg)
1462
 
1463
 
  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1464
 
}
1465
 
 
1466
 
void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
1467
 
1468
 
  uint64_t max_result_length= max_char_length_arg *
1469
 
    collation.collation->mbmaxlen;
1470
 
 
1471
 
  if (max_result_length >= MAX_BLOB_WIDTH)
1472
 
  { 
1473
 
    max_length= MAX_BLOB_WIDTH;
1474
 
    maybe_null= false;
1475
 
  }
1476
 
  else
1477
 
  {
1478
 
    max_length= max_result_length;
1479
 
  }
1480
 
}
1481
 
 
1482
 
void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
1483
 
1484
 
  collation.set(&my_charset_bin);
1485
 
  fix_char_length(max_char_length_arg);
1486
 
}
1487
 
 
1488
1335
Item_result item_cmp_type(Item_result a,Item_result b)
1489
1336
{
1490
1337
  if (a == STRING_RESULT && b == STRING_RESULT)
1491
1338
    return STRING_RESULT;
1492
 
 
1493
1339
  if (a == INT_RESULT && b == INT_RESULT)
1494
1340
    return INT_RESULT;
1495
1341
  else if (a == ROW_RESULT || b == ROW_RESULT)
1496
1342
    return ROW_RESULT;
1497
 
 
1498
1343
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1499
1344
      (b == INT_RESULT || b == DECIMAL_RESULT))
1500
1345
    return DECIMAL_RESULT;
1501
 
 
1502
1346
  return REAL_RESULT;
1503
1347
}
1504
1348
 
1510
1354
    return; /* Can't be better */
1511
1355
  Item_result res_type=item_cmp_type(comp_item->result_type(),
1512
1356
                                     item->result_type());
1513
 
  char *name=item->name; /* Alloced by memory::sql_alloc */
 
1357
  char *name=item->name; /* Alloced by sql_alloc */
1514
1358
 
1515
1359
  switch (res_type) {
1516
1360
  case STRING_RESULT:
 
1361
  {
 
1362
    char buff[MAX_FIELD_WIDTH];
 
1363
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
 
1364
    result=item->val_str(&tmp);
 
1365
    if (item->null_value)
 
1366
      new_item= new Item_null(name);
 
1367
    else
1517
1368
    {
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;
 
1369
      uint32_t length= result->length();
 
1370
      char *tmp_str= sql_strmake(result->ptr(), length);
 
1371
      new_item= new Item_string(name, tmp_str, length, result->charset());
1530
1372
    }
 
1373
    break;
 
1374
  }
1531
1375
  case INT_RESULT:
1532
 
    {
1533
 
      int64_t result=item->val_int();
1534
 
      uint32_t length=item->max_length;
1535
 
      bool null_value=item->null_value;
1536
 
      new_item= (null_value ? (Item*) new Item_null(name) :
1537
 
                 (Item*) new Item_int(name, result, length));
1538
 
      break;
1539
 
    }
 
1376
  {
 
1377
    int64_t result=item->val_int();
 
1378
    uint32_t length=item->max_length;
 
1379
    bool null_value=item->null_value;
 
1380
    new_item= (null_value ? (Item*) new Item_null(name) :
 
1381
               (Item*) new Item_int(name, result, length));
 
1382
    break;
 
1383
  }
1540
1384
  case ROW_RESULT:
1541
 
    if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1542
 
    {
1543
 
      /*
1544
 
        Substitute constants only in Item_rows. Don't affect other Items
1545
 
        with ROW_RESULT (eg Item_singlerow_subselect).
 
1385
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
 
1386
  {
 
1387
    /*
 
1388
      Substitute constants only in Item_rows. Don't affect other Items
 
1389
      with ROW_RESULT (eg Item_singlerow_subselect).
1546
1390
 
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 */
 
1391
      For such Items more optimal is to detect if it is constant and replace
 
1392
      it with Item_row. This would optimize queries like this:
 
1393
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
 
1394
    */
 
1395
    Item_row *item_row= (Item_row*) item;
 
1396
    Item_row *comp_item_row= (Item_row*) comp_item;
 
1397
    uint32_t col;
 
1398
    new_item= 0;
 
1399
    /*
 
1400
      If item and comp_item are both Item_rows and have same number of cols
 
1401
      then process items in Item_row one by one.
 
1402
      We can't ignore NULL values here as this item may be used with <=>, in
 
1403
      which case NULL's are significant.
 
1404
    */
 
1405
    assert(item->result_type() == comp_item->result_type());
 
1406
    assert(item_row->cols() == comp_item_row->cols());
 
1407
    col= item_row->cols();
 
1408
    while (col-- > 0)
 
1409
      resolve_const_item(session, item_row->addr(col),
 
1410
                         comp_item_row->element_index(col));
 
1411
    break;
 
1412
  }
 
1413
  /* Fallthrough */
1570
1414
  case REAL_RESULT:
1571
 
    {                                           // It must REAL_RESULT
1572
 
      double result= item->val_real();
1573
 
      uint32_t length=item->max_length,decimals=item->decimals;
1574
 
      bool null_value=item->null_value;
1575
 
      new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1576
 
                 new Item_float(name, result, decimals, length));
1577
 
      break;
1578
 
    }
 
1415
  {                                             // It must REAL_RESULT
 
1416
    double result= item->val_real();
 
1417
    uint32_t length=item->max_length,decimals=item->decimals;
 
1418
    bool null_value=item->null_value;
 
1419
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
 
1420
               new Item_float(name, result, decimals, length));
 
1421
    break;
 
1422
  }
1579
1423
  case DECIMAL_RESULT:
1580
 
    {
1581
 
      type::Decimal decimal_value;
1582
 
      type::Decimal *result= item->val_decimal(&decimal_value);
1583
 
      uint32_t length= item->max_length, decimals= item->decimals;
1584
 
      bool null_value= item->null_value;
1585
 
      new_item= (null_value ?
1586
 
                 (Item*) new Item_null(name) :
1587
 
                 (Item*) new Item_decimal(name, result, length, decimals));
1588
 
      break;
1589
 
    }
1590
 
  }
1591
 
 
 
1424
  {
 
1425
    my_decimal decimal_value;
 
1426
    my_decimal *result= item->val_decimal(&decimal_value);
 
1427
    uint32_t length= item->max_length, decimals= item->decimals;
 
1428
    bool null_value= item->null_value;
 
1429
    new_item= (null_value ?
 
1430
               (Item*) new Item_null(name) :
 
1431
               (Item*) new Item_decimal(name, result, length, decimals));
 
1432
    break;
 
1433
  }
 
1434
  default:
 
1435
    assert(0);
 
1436
  }
1592
1437
  if (new_item)
1593
1438
    session->change_item_tree(ref, new_item);
1594
1439
}
1607
1452
    item_result=item->val_str(&item_tmp);
1608
1453
    if (item->null_value)
1609
1454
      return 1;                                 // This must be true
1610
 
    field->val_str_internal(&field_tmp);
1611
 
    return not stringcmp(&field_tmp,item_result);
 
1455
    field->val_str(&field_tmp);
 
1456
    return !stringcmp(&field_tmp,item_result);
1612
1457
  }
1613
 
 
1614
1458
  if (res_type == INT_RESULT)
1615
1459
    return 1;                                   // Both where of type int
1616
 
 
1617
1460
  if (res_type == DECIMAL_RESULT)
1618
1461
  {
1619
 
    type::Decimal item_buf, *item_val,
 
1462
    my_decimal item_buf, *item_val,
1620
1463
               field_buf, *field_val;
1621
1464
    item_val= item->val_decimal(&item_buf);
1622
1465
    if (item->null_value)
1623
1466
      return 1;                                 // This must be true
1624
1467
    field_val= field->val_decimal(&field_buf);
1625
 
    return !class_decimal_cmp(item_val, field_val);
 
1468
    return !my_decimal_cmp(item_val, field_val);
1626
1469
  }
1627
 
 
1628
1470
  double result= item->val_real();
1629
1471
  if (item->null_value)
1630
1472
    return 1;
1631
 
 
1632
1473
  return result == field->val_real();
1633
1474
}
1634
1475
 
1664
1505
                                         uint32_t convert_blob_length)
1665
1506
{
1666
1507
  bool maybe_null= item->maybe_null;
1667
 
  Field *new_field= NULL;
 
1508
  Field *new_field;
1668
1509
 
1669
1510
  switch (item->result_type()) {
1670
1511
  case REAL_RESULT:
1671
1512
    new_field= new Field_double(item->max_length, maybe_null,
1672
1513
                                item->name, item->decimals, true);
1673
1514
    break;
1674
 
 
1675
1515
  case INT_RESULT:
1676
1516
    /*
1677
1517
      Select an integer type with the minimal fit precision.
1678
1518
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1679
1519
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1680
 
      Int32 -> make them field::Int64.
 
1520
      Field_long : make them Field_int64_t.
1681
1521
    */
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
 
    }
 
1522
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1523
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
1524
                                   item->name, item->unsigned_flag);
1692
1525
    else
1693
 
    {
1694
 
      new_field= new field::Int32(item->max_length, maybe_null,
1695
 
                                  item->name, item->unsigned_flag);
1696
 
    }
1697
 
 
 
1526
      new_field=new Field_long(item->max_length, maybe_null,
 
1527
                               item->name, item->unsigned_flag);
1698
1528
    break;
1699
 
 
1700
1529
  case STRING_RESULT:
1701
1530
    assert(item->collation.collation);
1702
1531
 
1706
1535
      To preserve type they needed to be handled separately.
1707
1536
    */
1708
1537
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1709
 
        type == DRIZZLE_TYPE_TIME ||
1710
 
        type == DRIZZLE_TYPE_MICROTIME ||
1711
1538
        type == DRIZZLE_TYPE_DATE ||
1712
1539
        type == DRIZZLE_TYPE_TIMESTAMP)
1713
 
    {
1714
1540
      new_field= item->tmp_table_field_from_field_type(table, 1);
1715
 
      /*
1716
 
        Make sure that the blob fits into a Field_varstring which has
1717
 
        2-byte lenght.
1718
 
      */
1719
 
    }
 
1541
    /*
 
1542
      Make sure that the blob fits into a Field_varstring which has
 
1543
      2-byte lenght.
 
1544
    */
1720
1545
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1721
1546
             convert_blob_length <= Field_varstring::MAX_SIZE &&
1722
1547
             convert_blob_length)
1723
 
    {
1724
 
      table->setVariableWidth();
1725
1548
      new_field= new Field_varstring(convert_blob_length, maybe_null,
1726
 
                                     item->name, item->collation.collation);
1727
 
    }
 
1549
                                     item->name, table->s,
 
1550
                                     item->collation.collation);
1728
1551
    else
1729
 
    {
1730
1552
      new_field= item->make_string_field(table);
1731
 
    }
1732
1553
    new_field->set_derivation(item->collation.derivation);
1733
1554
    break;
1734
 
 
1735
1555
  case DECIMAL_RESULT:
 
1556
  {
 
1557
    uint8_t dec= item->decimals;
 
1558
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
1559
    uint32_t len= item->max_length;
 
1560
 
 
1561
    /*
 
1562
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
1563
      will always throw a warning. We must limit dec to
 
1564
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
1565
    */
 
1566
 
 
1567
    if (dec > 0)
1736
1568
    {
1737
 
      uint8_t dec= item->decimals;
1738
 
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1739
 
      uint32_t len= item->max_length;
 
1569
      signed int overflow;
 
1570
 
 
1571
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1740
1572
 
1741
1573
      /*
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.
 
1574
        If the value still overflows the field with the corrected dec,
 
1575
        we'll throw out decimals rather than integers. This is still
 
1576
        bad and of course throws a truncation warning.
 
1577
        +1: for decimal point
1745
1578
      */
1746
1579
 
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;
 
1580
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
1581
                                               item->unsigned_flag) - len;
 
1582
 
 
1583
      if (overflow > 0)
 
1584
        dec= max(0, dec - overflow);            // too long, discard fract
 
1585
      else
 
1586
        len-= item->decimals - dec;             // corrected value fits
1775
1587
    }
1776
1588
 
 
1589
    new_field= new Field_decimal(len,
 
1590
                                 maybe_null,
 
1591
                                 item->name,
 
1592
                                 dec,
 
1593
                                 item->unsigned_flag);
 
1594
    break;
 
1595
  }
1777
1596
  case ROW_RESULT:
 
1597
  default:
1778
1598
    // This case should never be choosen
1779
1599
    assert(0);
1780
 
    abort();
 
1600
    new_field= 0;
 
1601
    break;
1781
1602
  }
1782
 
 
1783
1603
  if (new_field)
1784
1604
    new_field->init(table);
1785
1605
 
1786
1606
  if (copy_func && item->is_result_field())
1787
1607
    *((*copy_func)++) = item;                   // Save for copy_funcs
1788
 
 
1789
1608
  if (modify_item)
1790
1609
    item->set_result_field(new_field);
1791
 
 
1792
1610
  if (item->type() == Item::NULL_ITEM)
1793
1611
    new_field->is_created_from_null_item= true;
1794
 
 
1795
1612
  return new_field;
1796
1613
}
1797
1614
 
1798
1615
Field *create_tmp_field(Session *session,
1799
1616
                        Table *table,
1800
1617
                        Item *item,
1801
 
                        Item::Type type,
1802
 
                        Item ***copy_func,
 
1618
                        Item::Type type, 
 
1619
                        Item ***copy_func, 
1803
1620
                        Field **from_field,
1804
 
                        Field **default_field,
1805
 
                        bool group,
 
1621
                        Field **default_field, 
 
1622
                        bool group, 
1806
1623
                        bool modify_item,
 
1624
                        bool, 
1807
1625
                        bool make_copy_field,
1808
1626
                        uint32_t convert_blob_length)
1809
1627
{
1848
1666
        field->result_field= result;
1849
1667
    }
1850
1668
    else
1851
 
    {
1852
1669
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1853
1670
                                          orig_item ? orig_item->name :
1854
1671
                                          item->name,
1856
1673
                                          modify_item ? field :
1857
1674
                                          NULL,
1858
1675
                                          convert_blob_length);
1859
 
    }
1860
1676
    if (orig_type == Item::REF_ITEM && orig_modify)
1861
1677
      ((Item_ref*)orig_item)->set_result_field(result);
1862
1678
    if (field->field->eq_def(result))
1896
1712
  }
1897
1713
}
1898
1714
 
1899
 
std::ostream& operator<<(std::ostream& output, const Item &item)
1900
 
{
1901
 
  switch (item.type())
1902
 
  {
1903
 
  case drizzled::Item::SUBSELECT_ITEM :
1904
 
  case drizzled::Item::FIELD_ITEM :
1905
 
  case drizzled::Item::SUM_FUNC_ITEM :
1906
 
  case drizzled::Item::STRING_ITEM :
1907
 
  case drizzled::Item::INT_ITEM :
1908
 
  case drizzled::Item::REAL_ITEM :
1909
 
  case drizzled::Item::NULL_ITEM :
1910
 
  case drizzled::Item::VARBIN_ITEM :
1911
 
  case drizzled::Item::COPY_STR_ITEM :
1912
 
  case drizzled::Item::FIELD_AVG_ITEM :
1913
 
  case drizzled::Item::DEFAULT_VALUE_ITEM :
1914
 
  case drizzled::Item::PROC_ITEM :
1915
 
  case drizzled::Item::COND_ITEM :
1916
 
  case drizzled::Item::REF_ITEM :
1917
 
  case drizzled::Item::FIELD_STD_ITEM :
1918
 
  case drizzled::Item::FIELD_VARIANCE_ITEM :
1919
 
  case drizzled::Item::INSERT_VALUE_ITEM :
1920
 
  case drizzled::Item::ROW_ITEM:
1921
 
  case drizzled::Item::CACHE_ITEM :
1922
 
  case drizzled::Item::TYPE_HOLDER :
1923
 
  case drizzled::Item::PARAM_ITEM :
1924
 
  case drizzled::Item::DECIMAL_ITEM :
1925
 
  case drizzled::Item::FUNC_ITEM :
1926
 
  case drizzled::Item::BOOLEAN_ITEM :
1927
 
    {
1928
 
      output << "Item:(";
1929
 
      output <<  item.full_name();
1930
 
      output << ", ";
1931
 
      output << drizzled::display::type(item.type());
1932
 
      output << ")";
1933
 
    }
1934
 
    break;
1935
 
  }
1936
 
 
1937
 
  return output;  // for multiple << operators.
1938
 
}
1939
 
 
1940
 
} /* namespace drizzled */
 
1715
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
1716
template class List<Item>;
 
1717
template class List_iterator<Item>;
 
1718
template class List_iterator_fast<Item>;
 
1719
template class List_iterator_fast<Item_field>;
 
1720
template class List<List_item>;
 
1721
#endif