~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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 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/internal/m_string.h"
55
 
 
56
 
#include <cstdio>
57
 
#include <math.h>
58
 
#include <algorithm>
59
 
#include <float.h>
60
 
 
61
 
using namespace std;
62
 
 
63
 
namespace drizzled
64
 
{
 
20
 
 
21
#include <drizzled/server_includes.h>
 
22
#include CMATH_H
 
23
 
 
24
#include <drizzled/sql_select.h>
 
25
#include <drizzled/error.h>
 
26
#include <drizzled/show.h>
 
27
#include <drizzled/item/cmpfunc.h>
 
28
#include <drizzled/item/cache_row.h>
 
29
#include <drizzled/item/type_holder.h>
 
30
#include <drizzled/item/sum.h>
 
31
#include <drizzled/item/copy_string.h>
 
32
#include <drizzled/function/str/conv_charset.h>
 
33
#include <drizzled/virtual_column_info.h>
 
34
#include <drizzled/sql_base.h>
 
35
 
 
36
 
 
37
#include <drizzled/field/str.h>
 
38
#include <drizzled/field/longstr.h>
 
39
#include <drizzled/field/num.h>
 
40
#include <drizzled/field/blob.h>
 
41
#include <drizzled/field/enum.h>
 
42
#include <drizzled/field/null.h>
 
43
#include <drizzled/field/date.h>
 
44
#include <drizzled/field/decimal.h>
 
45
#include <drizzled/field/real.h>
 
46
#include <drizzled/field/double.h>
 
47
#include <drizzled/field/long.h>
 
48
#include <drizzled/field/int64_t.h>
 
49
#include <drizzled/field/num.h>
 
50
#include <drizzled/field/timetype.h>
 
51
#include <drizzled/field/timestamp.h>
 
52
#include <drizzled/field/datetime.h>
 
53
#include <drizzled/field/varstring.h>
 
54
 
 
55
 
 
56
#if defined(CMATH_NAMESPACE)
 
57
using namespace CMATH_NAMESPACE;
 
58
#endif
65
59
 
66
60
const String my_null_string("NULL", 4, default_charset_info);
67
61
 
 
62
/*****************************************************************************
 
63
** Item functions
 
64
*****************************************************************************/
 
65
 
68
66
bool Item::is_expensive_processor(unsigned char *)
69
67
{
70
68
  return false;
73
71
void Item::fix_after_pullout(Select_Lex *, Item **)
74
72
{}
75
73
 
 
74
 
76
75
Field *Item::tmp_table_field(Table *)
77
76
{
78
77
  return NULL;
79
78
}
80
79
 
 
80
 
81
81
const char *Item::full_name(void) const
82
82
{
83
83
  return name ? name : "???";
84
84
}
85
85
 
 
86
 
86
87
int64_t Item::val_int_endpoint(bool, bool *)
87
88
{
88
89
  assert(0);
89
90
  return 0;
90
91
}
91
92
 
92
 
/** @todo Make this functions class dependent */
 
93
 
 
94
/**
 
95
  @todo
 
96
    Make this functions class dependent
 
97
*/
 
98
 
93
99
bool Item::val_bool()
94
100
{
95
 
  switch(result_type()) 
 
101
  switch(result_type()) {
 
102
  case INT_RESULT:
 
103
    return val_int() != 0;
 
104
  case DECIMAL_RESULT:
96
105
  {
97
 
    case INT_RESULT:
98
 
      return val_int() != 0;
99
 
 
100
 
    case DECIMAL_RESULT:
101
 
    {
102
 
      type::Decimal decimal_value;
103
 
      type::Decimal *val= val_decimal(&decimal_value);
104
 
      if (val)
105
 
        return not val->isZero();
106
 
      return false;
107
 
    }
108
 
 
109
 
    case REAL_RESULT:
110
 
    case STRING_RESULT:
111
 
      return val_real() != 0.0;
112
 
 
113
 
    case ROW_RESULT:
114
 
      assert(0);
115
 
      abort();
116
 
  }
117
 
 
118
 
  assert(0);
119
 
  abort();
 
106
    my_decimal decimal_value;
 
107
    my_decimal *val= val_decimal(&decimal_value);
 
108
    if (val)
 
109
      return !my_decimal_is_zero(val);
 
110
    return false;
 
111
  }
 
112
  case REAL_RESULT:
 
113
  case STRING_RESULT:
 
114
    return val_real() != 0.0;
 
115
  case ROW_RESULT:
 
116
  default:
 
117
    assert(0);
 
118
    return false;                                   // Wrong (but safe)
 
119
  }
120
120
}
121
121
 
 
122
 
122
123
String *Item::val_string_from_real(String *str)
123
124
{
124
125
  double nr= val_real();
125
126
  if (null_value)
126
 
    return NULL;
 
127
    return NULL;                                        /* purecov: inspected */
127
128
 
128
 
  str->set_real(nr, decimals, &my_charset_bin);
 
129
  str->set_real(nr,decimals, &my_charset_bin);
129
130
  return str;
130
131
}
131
132
 
 
133
 
132
134
String *Item::val_string_from_int(String *str)
133
135
{
134
136
  int64_t nr= val_int();
139
141
  return str;
140
142
}
141
143
 
 
144
 
142
145
String *Item::val_string_from_decimal(String *str)
143
146
{
144
 
  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
 
147
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
145
148
  if (null_value)
146
149
    return NULL;
147
150
 
148
 
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
149
 
  class_decimal2string(&dec_buf, 0, str);
 
151
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
152
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
150
153
  return str;
151
154
}
152
155
 
153
 
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
 
156
 
 
157
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
154
158
{
155
159
  double nr= val_real();
156
160
  if (null_value)
157
161
    return NULL;
158
162
 
159
 
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
163
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
160
164
  return (decimal_value);
161
165
}
162
166
 
163
 
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
 
167
 
 
168
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
164
169
{
165
170
  int64_t nr= val_int();
166
171
  if (null_value)
167
172
    return NULL;
168
173
 
169
 
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
174
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
170
175
  return decimal_value;
171
176
}
172
177
 
173
 
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
 
178
 
 
179
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
174
180
{
175
181
  String *res;
176
182
  char *end_ptr;
177
183
  if (!(res= val_str(&str_value)))
178
 
    return NULL;
 
184
    return NULL;                                   // NULL or EOM
179
185
 
180
186
  end_ptr= (char*) res->ptr()+ res->length();
181
 
  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
182
 
                     res->ptr(), 
183
 
                     res->length(), 
184
 
                     res->charset()) & E_DEC_BAD_NUM)
 
187
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
 
188
                     res->ptr(), res->length(), res->charset(),
 
189
                     decimal_value) & E_DEC_BAD_NUM)
185
190
  {
186
 
    push_warning_printf(&getSession(), 
187
 
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
191
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
188
192
                        ER_TRUNCATED_WRONG_VALUE,
189
193
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
190
194
                        str_value.c_ptr());
192
196
  return decimal_value;
193
197
}
194
198
 
195
 
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
 
199
 
 
200
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
196
201
{
197
 
  assert(fixed);
198
 
  type::Time ltime;
199
 
  if (get_date(ltime, TIME_FUZZY_DATE))
 
202
  assert(fixed == 1);
 
203
  DRIZZLE_TIME ltime;
 
204
  if (get_date(&ltime, TIME_FUZZY_DATE))
200
205
  {
201
 
    decimal_value->set_zero();
 
206
    my_decimal_set_zero(decimal_value);
202
207
    null_value= 1;                               // set NULL, stop processing
203
208
    return NULL;
204
209
  }
205
 
  return date2_class_decimal(&ltime, decimal_value);
 
210
  return date2my_decimal(&ltime, decimal_value);
206
211
}
207
212
 
208
 
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
 
213
 
 
214
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
209
215
{
210
 
  assert(fixed);
211
 
  type::Time ltime;
212
 
  if (get_time(ltime))
 
216
  assert(fixed == 1);
 
217
  DRIZZLE_TIME ltime;
 
218
  if (get_time(&ltime))
213
219
  {
214
 
    decimal_value->set_zero();
 
220
    my_decimal_set_zero(decimal_value);
215
221
    return NULL;
216
222
  }
217
 
  return date2_class_decimal(&ltime, decimal_value);
 
223
  return date2my_decimal(&ltime, decimal_value);
218
224
}
219
225
 
 
226
 
220
227
double Item::val_real_from_decimal()
221
228
{
222
229
  /* Note that fix_fields may not be called for Item_avg_field items */
223
230
  double result;
224
 
  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
 
231
  my_decimal value_buff, *dec_val= val_decimal(&value_buff);
225
232
  if (null_value)
226
233
    return 0.0;
227
 
  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
 
234
  my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
228
235
  return result;
229
236
}
230
237
 
 
238
 
231
239
int64_t Item::val_int_from_decimal()
232
240
{
233
241
  /* Note that fix_fields may not be called for Item_avg_field items */
234
242
  int64_t result;
235
 
  type::Decimal value, *dec_val= val_decimal(&value);
236
 
 
 
243
  my_decimal value, *dec_val= val_decimal(&value);
237
244
  if (null_value)
238
245
    return 0;
239
 
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
240
 
 
 
246
  my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
241
247
  return result;
242
248
}
243
249
 
244
 
bool Item::save_time_in_field(Field *field)
245
 
{
246
 
  type::Time ltime;
247
 
 
248
 
  if (get_time(ltime))
249
 
    return set_field_to_null(field);
250
 
 
251
 
  field->set_notnull();
252
 
 
253
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
254
 
}
255
 
 
256
 
bool Item::save_date_in_field(Field *field)
257
 
{
258
 
  type::Time ltime;
259
 
 
260
 
  if (get_date(ltime, TIME_FUZZY_DATE))
261
 
    return set_field_to_null(field);
262
 
 
263
 
  field->set_notnull();
264
 
 
265
 
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
266
 
}
267
 
 
268
 
/**
269
 
 * Check if the Item is null and stores the NULL or the
270
 
 * result value in the field accordingly.
271
 
 */
 
250
int Item::save_time_in_field(Field *field)
 
251
{
 
252
  DRIZZLE_TIME ltime;
 
253
  if (get_time(&ltime))
 
254
    return set_field_to_null(field);
 
255
  field->set_notnull();
 
256
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
 
257
}
 
258
 
 
259
 
 
260
int Item::save_date_in_field(Field *field)
 
261
{
 
262
  DRIZZLE_TIME ltime;
 
263
  if (get_date(&ltime, TIME_FUZZY_DATE))
 
264
    return set_field_to_null(field);
 
265
  field->set_notnull();
 
266
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
 
267
}
 
268
 
 
269
 
 
270
/*
 
271
  Store the string value in field directly
 
272
 
 
273
  SYNOPSIS
 
274
    Item::save_str_value_in_field()
 
275
    field   a pointer to field where to store
 
276
    result  the pointer to the string value to be stored
 
277
 
 
278
  DESCRIPTION
 
279
    The method is used by Item_*::save_in_field implementations
 
280
    when we don't need to calculate the value to store
 
281
    See Item_string::save_in_field() implementation for example
 
282
 
 
283
  IMPLEMENTATION
 
284
    Check if the Item is null and stores the NULL or the
 
285
    result value in the field accordingly.
 
286
 
 
287
  RETURN
 
288
    Nonzero value if error
 
289
*/
 
290
 
272
291
int Item::save_str_value_in_field(Field *field, String *result)
273
292
{
274
293
  if (null_value)
275
294
    return set_field_to_null(field);
276
 
 
277
295
  field->set_notnull();
278
 
 
279
 
  return field->store(result->ptr(), result->length(), collation.collation);
 
296
  return field->store(result->ptr(), result->length(),
 
297
                      collation.collation);
280
298
}
281
299
 
 
300
 
282
301
Item::Item():
283
 
  is_expensive_cache(-1),
284
 
  name(0), 
285
 
  name_length(0),
286
 
  orig_name(0), 
287
 
  max_length(0), 
288
 
  marker(0),
289
 
  decimals(0),
290
 
  fixed(false),
291
 
  maybe_null(false),
292
 
  null_value(false),
293
 
  unsigned_flag(false), 
294
 
  with_sum_func(false),
295
 
  is_autogenerated_name(true),
296
 
  with_subselect(false),
297
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
298
 
  _session(*current_session)
 
302
  is_expensive_cache(-1), name(0), orig_name(0), max_length(0), name_length(0),
 
303
  unsigned_flag(false), fixed(0), is_autogenerated_name(true),
 
304
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
299
305
{
 
306
  marker= 0;
 
307
  maybe_null= false;
 
308
  null_value= false;
 
309
  with_sum_func= false;
 
310
  unsigned_flag= false;
 
311
  decimals= 0;
 
312
  with_subselect= 0;
300
313
  cmp_context= (Item_result)-1;
301
314
 
302
315
  /* Put item in free list so that we can free all items at end */
303
 
  next= getSession().free_list;
304
 
  getSession().free_list= this;
305
 
 
 
316
  Session *session= current_session;
 
317
  next= session->free_list;
 
318
  session->free_list= this;
306
319
  /*
307
320
    Item constructor can be called during execution other then SQL_COM
308
321
    command => we should check session->lex->current_select on zero (session->lex
309
322
    can be uninitialised)
310
323
  */
311
 
  if (getSession().lex->current_select)
 
324
  if (session->lex->current_select)
312
325
  {
313
 
    enum_parsing_place place= getSession().getLex()->current_select->parsing_place;
314
 
    if (place == SELECT_LIST || place == IN_HAVING)
315
 
      getSession().getLex()->current_select->select_n_having_items++;
 
326
    enum_parsing_place place=
 
327
      session->lex->current_select->parsing_place;
 
328
    if (place == SELECT_LIST ||
 
329
        place == IN_HAVING)
 
330
      session->lex->current_select->select_n_having_items++;
316
331
  }
317
332
}
318
333
 
 
334
/**
 
335
  Constructor used by Item_field, Item_ref & aggregate (sum)
 
336
  functions.
 
337
 
 
338
  Used for duplicating lists in processing queries with temporary
 
339
  tables.
 
340
*/
319
341
Item::Item(Session *session, Item *item):
320
342
  is_expensive_cache(-1),
321
343
  str_value(item->str_value),
322
344
  name(item->name),
323
 
  name_length(item->name_length),
324
345
  orig_name(item->orig_name),
325
346
  max_length(item->max_length),
326
347
  marker(item->marker),
327
348
  decimals(item->decimals),
328
 
  fixed(item->fixed),
329
349
  maybe_null(item->maybe_null),
330
350
  null_value(item->null_value),
331
351
  unsigned_flag(item->unsigned_flag),
332
352
  with_sum_func(item->with_sum_func),
333
 
  is_autogenerated_name(item->is_autogenerated_name),
334
 
  with_subselect(item->with_subselect),
 
353
  fixed(item->fixed),
335
354
  collation(item->collation),
336
 
  cmp_context(item->cmp_context),
337
 
  _session(*session)
 
355
  cmp_context(item->cmp_context)
338
356
{
339
 
  /* Put this item in the session's free list */
340
 
  next= getSession().free_list;
341
 
  getSession().free_list= this;
 
357
  next= session->free_list;                             // Put in free list
 
358
  session->free_list= this;
342
359
}
343
360
 
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;
347
 
}
348
361
 
349
362
uint32_t Item::decimal_precision() const
350
363
{
351
364
  Item_result restype= result_type();
352
365
 
353
366
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
354
 
    return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
355
 
               (uint32_t) DECIMAL_MAX_PRECISION);
356
 
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
 
367
    return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
368
               (unsigned int)DECIMAL_MAX_PRECISION);
 
369
  return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
357
370
}
358
371
 
 
372
 
359
373
int Item::decimal_int_part() const
360
374
{
361
 
  return class_decimal_int_part(decimal_precision(), decimals);
 
375
  return my_decimal_int_part(decimal_precision(), decimals);
362
376
}
363
377
 
 
378
 
364
379
void Item::print(String *str, enum_query_type)
365
380
{
366
381
  str->append(full_name());
367
382
}
368
383
 
 
384
 
369
385
void Item::print_item_w_name(String *str, enum_query_type query_type)
370
386
{
371
387
  print(str, query_type);
377
393
  }
378
394
}
379
395
 
 
396
 
380
397
void Item::split_sum_func(Session *, Item **, List<Item> &)
381
398
{}
382
399
 
 
400
 
383
401
void Item::cleanup()
384
402
{
385
 
  fixed= false;
 
403
  fixed=0;
386
404
  marker= 0;
387
405
  if (orig_name)
388
406
    name= orig_name;
389
407
  return;
390
408
}
391
409
 
 
410
 
 
411
/**
 
412
  cleanup() item if it is 'fixed'.
 
413
 
 
414
  @param arg   a dummy parameter, is not used here
 
415
*/
 
416
 
 
417
bool Item::cleanup_processor(unsigned char *)
 
418
{
 
419
  if (fixed)
 
420
    cleanup();
 
421
  return false;
 
422
}
 
423
 
 
424
 
 
425
/**
 
426
  rename item (used for views, cleanup() return original name).
 
427
 
 
428
  @param new_name       new name of item;
 
429
*/
 
430
 
392
431
void Item::rename(char *new_name)
393
432
{
394
433
  /*
395
434
    we can compare pointers to names here, because if name was not changed,
396
435
    pointer will be same
397
436
  */
398
 
  if (! orig_name && new_name != name)
 
437
  if (!orig_name && new_name != name)
399
438
    orig_name= name;
400
439
  name= new_name;
401
440
}
402
441
 
 
442
 
 
443
/**
 
444
  Traverse item tree possibly transforming it (replacing items).
 
445
 
 
446
  This function is designed to ease transformation of Item trees.
 
447
  Re-execution note: every such transformation is registered for
 
448
  rollback by Session::change_item_tree() and is rolled back at the end
 
449
  of execution by Session::rollback_item_tree_changes().
 
450
 
 
451
  Therefore:
 
452
  - this function can not be used at prepared statement prepare
 
453
  (in particular, in fix_fields!), as only permanent
 
454
  transformation of Item trees are allowed at prepare.
 
455
  - the transformer function shall allocate new Items in execution
 
456
  memory root (session->mem_root) and not anywhere else: allocated
 
457
  items will be gone in the end of execution.
 
458
 
 
459
  If you don't need to transform an item tree, but only traverse
 
460
  it, please use Item::walk() instead.
 
461
 
 
462
 
 
463
  @param transformer    functor that performs transformation of a subtree
 
464
  @param arg            opaque argument passed to the functor
 
465
 
 
466
  @return
 
467
    Returns pointer to the new subtree root.  Session::change_item_tree()
 
468
    should be called for it if transformation took place, i.e. if a
 
469
    pointer to newly allocated item is returned.
 
470
*/
 
471
 
403
472
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
404
473
{
405
474
  return (this->*transformer)(arg);
406
475
}
407
476
 
 
477
 
408
478
bool Item::check_cols(uint32_t c)
409
479
{
410
480
  if (c != 1)
411
481
  {
412
482
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
413
 
    return true;
 
483
    return 1;
414
484
  }
415
485
  return false;
416
486
}
417
487
 
 
488
 
418
489
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
419
490
{
420
491
  if (!length)
427
498
  if (cs->ctype)
428
499
  {
429
500
    uint32_t orig_len= length;
430
 
    while (length && ! my_isgraph(cs, *str))
431
 
    {
432
 
      /* Fix problem with yacc */
 
501
    /*
 
502
      This will probably need a better implementation in the future:
 
503
      a function in CHARSET_INFO structure.
 
504
    */
 
505
    while (length && !my_isgraph(cs,*str))
 
506
    {                                           // Fix problem with yacc
433
507
      length--;
434
508
      str++;
435
509
    }
436
 
    if (orig_len != length && ! is_autogenerated_name)
 
510
    if (orig_len != length && !is_autogenerated_name)
437
511
    {
438
512
      if (length == 0)
439
 
        push_warning_printf(&getSession(), 
440
 
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
441
 
                            ER_NAME_BECOMES_EMPTY, 
442
 
                            ER(ER_NAME_BECOMES_EMPTY),
 
513
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
514
                            ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
443
515
                            str + length - orig_len);
444
516
      else
445
 
        push_warning_printf(&getSession(),
446
 
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
447
 
                            ER_REMOVED_SPACES, 
448
 
                            ER(ER_REMOVED_SPACES),
 
517
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
518
                            ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
449
519
                            str + length - orig_len);
450
520
    }
451
521
  }
452
 
  name= memory::sql_strmake(str, length);
 
522
  if (!my_charset_same(cs, system_charset_info))
 
523
  {
 
524
    size_t res_length;
 
525
    name= sql_strmake_with_convert(str, name_length= length, cs,
 
526
                                   length, system_charset_info,
 
527
                                   &res_length);
 
528
  }
 
529
  else
 
530
      name= sql_strmake(str, length);
453
531
}
454
532
 
 
533
 
 
534
/**
 
535
  @details
 
536
  This function is called when:
 
537
  - Comparing items in the WHERE clause (when doing where optimization)
 
538
  - When trying to find an order_st BY/GROUP BY item in the SELECT part
 
539
*/
 
540
 
455
541
bool Item::eq(const Item *item, bool) const
456
542
{
457
543
  /*
459
545
    for all basic constants we have special checks, and Item_param's
460
546
    type() can be only among basic constant types.
461
547
  */
462
 
  return type() == item->type() && 
463
 
         name && 
464
 
         item->name &&
465
 
         ! my_strcasecmp(system_charset_info, name, item->name);
 
548
  return type() == item->type() && name && item->name &&
 
549
    !my_strcasecmp(system_charset_info,name,item->name);
466
550
}
467
551
 
 
552
 
468
553
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
469
554
{
470
555
  Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
471
556
  return conv->safe ? conv : NULL;
472
557
}
473
558
 
474
 
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
 
559
 
 
560
/**
 
561
  Get the value of the function as a DRIZZLE_TIME structure.
 
562
  As a extra convenience the time structure is reset on error!
 
563
*/
 
564
 
 
565
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
475
566
{
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
 
 
 
567
  if (result_type() == STRING_RESULT)
 
568
  {
 
569
    char buff[40];
 
570
    String tmp(buff,sizeof(buff), &my_charset_bin),*res;
 
571
    if (!(res=val_str(&tmp)) ||
 
572
        str_to_datetime_with_warn(res->ptr(), res->length(),
 
573
                                  ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
574
      goto err;
 
575
  }
 
576
  else
 
577
  {
 
578
    int64_t value= val_int();
 
579
    int was_cut;
 
580
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
 
581
    {
 
582
      char buff[22], *end;
 
583
      end= int64_t10_to_str(value, buff, -10);
 
584
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
585
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
 
586
                                   NULL);
 
587
      goto err;
 
588
    }
 
589
  }
 
590
  return false;
 
591
 
 
592
err:
 
593
  memset(ltime, 0, sizeof(*ltime));
515
594
  return true;
516
595
}
517
596
 
518
 
bool Item::get_time(type::Time &ltime)
 
597
/**
 
598
  Get time of first argument.\
 
599
 
 
600
  As a extra convenience the time structure is reset on error!
 
601
*/
 
602
 
 
603
bool Item::get_time(DRIZZLE_TIME *ltime)
519
604
{
520
 
  char buff[type::Time::MAX_STRING_LENGTH];
 
605
  char buff[40];
521
606
  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))
 
607
  if (!(res=val_str(&tmp)) ||
 
608
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
524
609
  {
525
 
    ltime.reset();
526
 
 
 
610
    memset(ltime, 0, sizeof(*ltime));
527
611
    return true;
528
612
  }
529
 
 
530
613
  return false;
531
614
}
532
615
 
533
 
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
 
616
 
 
617
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
534
618
{
535
 
  return get_date(ltime, fuzzydate);
 
619
  return get_date(ltime,fuzzydate);
536
620
}
537
621
 
 
622
 
538
623
bool Item::is_null()
539
624
{
540
625
  return false;
541
626
}
542
627
 
 
628
 
543
629
void Item::update_null_value ()
544
630
{
545
631
  (void) val_int();
546
632
}
547
633
 
 
634
 
548
635
void Item::top_level_item(void)
549
636
{}
550
637
 
 
638
 
551
639
void Item::set_result_field(Field *)
552
640
{}
553
641
 
 
642
 
554
643
bool Item::is_result_field(void)
555
644
{
556
645
  return false;
557
646
}
558
647
 
 
648
 
559
649
bool Item::is_bool_func(void)
560
650
{
561
651
  return false;
562
652
}
563
653
 
 
654
 
564
655
void Item::save_in_result_field(bool)
565
656
{}
566
657
 
 
658
 
567
659
void Item::no_rows_in_result(void)
568
660
{}
569
661
 
 
662
 
570
663
Item *Item::copy_or_same(Session *)
571
664
{
572
665
  return this;
573
666
}
574
667
 
 
668
 
575
669
Item *Item::copy_andor_structure(Session *)
576
670
{
577
671
  return this;
578
672
}
579
673
 
 
674
 
580
675
Item *Item::real_item(void)
581
676
{
582
677
  return this;
583
678
}
584
679
 
 
680
 
585
681
const Item *Item::real_item(void) const
586
682
{
587
683
  return this;
588
684
}
589
685
 
 
686
 
590
687
Item *Item::get_tmp_table_item(Session *session)
591
688
{
592
689
  return copy_or_same(session);
593
690
}
594
691
 
 
692
 
595
693
const CHARSET_INFO *Item::default_charset()
596
694
{
597
695
  return current_session->variables.getCollation();
598
696
}
599
697
 
 
698
 
600
699
const CHARSET_INFO *Item::compare_collation()
601
700
{
602
701
  return NULL;
603
702
}
604
703
 
 
704
 
605
705
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
606
706
{
607
707
  return (this->*processor)(arg);
608
708
}
609
709
 
610
 
Item* Item::compile(Item_analyzer analyzer, 
611
 
                    unsigned char **arg_p,
612
 
                    Item_transformer transformer, 
613
 
                    unsigned char *arg_t)
 
710
 
 
711
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
 
712
                    Item_transformer transformer, unsigned char *arg_t)
614
713
{
615
714
  if ((this->*analyzer) (arg_p))
616
715
    return ((this->*transformer) (arg_t));
617
716
  return NULL;
618
717
}
619
718
 
 
719
 
620
720
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
621
721
{
622
722
  (*traverser)(this, arg);
623
723
}
624
724
 
 
725
 
625
726
bool Item::remove_dependence_processor(unsigned char *)
626
727
{
627
728
  return false;
628
729
}
629
730
 
 
731
 
630
732
bool Item::remove_fixed(unsigned char *)
631
733
{
632
 
  fixed= false;
 
734
  fixed= 0;
633
735
  return false;
634
736
}
635
737
 
 
738
 
636
739
bool Item::collect_item_field_processor(unsigned char *)
637
740
{
638
741
  return false;
639
742
}
640
743
 
 
744
 
641
745
bool Item::find_item_in_field_list_processor(unsigned char *)
642
746
{
643
747
  return false;
644
748
}
645
749
 
 
750
 
646
751
bool Item::change_context_processor(unsigned char *)
647
752
{
648
753
  return false;
649
754
}
650
755
 
 
756
bool Item::reset_query_id_processor(unsigned char *)
 
757
{
 
758
  return false;
 
759
}
 
760
 
 
761
 
651
762
bool Item::register_field_in_read_map(unsigned char *)
652
763
{
653
764
  return false;
654
765
}
655
766
 
 
767
 
 
768
bool Item::register_field_in_bitmap(unsigned char *)
 
769
{
 
770
  return false;
 
771
}
 
772
 
 
773
 
656
774
bool Item::subst_argument_checker(unsigned char **arg)
657
775
{
658
776
  if (*arg)
660
778
  return true;
661
779
}
662
780
 
 
781
 
 
782
bool Item::check_vcol_func_processor(unsigned char *)
 
783
{
 
784
  return true;
 
785
}
 
786
 
 
787
 
663
788
Item *Item::equal_fields_propagator(unsigned char *)
664
789
{
665
790
  return this;
666
791
}
667
792
 
 
793
 
668
794
bool Item::set_no_const_sub(unsigned char *)
669
795
{
670
796
  return false;
671
797
}
672
798
 
 
799
 
673
800
Item *Item::replace_equal_field(unsigned char *)
674
801
{
675
802
  return this;
676
803
}
677
804
 
 
805
 
678
806
uint32_t Item::cols()
679
807
{
680
808
  return 1;
681
809
}
682
810
 
 
811
 
683
812
Item* Item::element_index(uint32_t)
684
813
{
685
814
  return this;
686
815
}
687
816
 
 
817
 
688
818
Item** Item::addr(uint32_t)
689
819
{
690
820
  return NULL;
691
821
}
692
822
 
 
823
 
693
824
bool Item::null_inside()
694
825
{
695
826
  return false;
696
827
}
697
828
 
 
829
 
698
830
void Item::bring_value()
699
831
{}
700
832
 
 
833
 
701
834
Item *Item::neg_transformer(Session *)
702
835
{
703
836
  return NULL;
704
837
}
705
838
 
 
839
 
706
840
Item *Item::update_value_transformer(unsigned char *)
707
841
{
708
842
  return this;
709
843
}
710
844
 
 
845
 
711
846
void Item::delete_self()
712
847
{
713
848
  cleanup();
719
854
  return false;
720
855
}
721
856
 
 
857
 
722
858
bool Item::is_expensive()
723
859
{
724
860
  if (is_expensive_cache < 0)
727
863
  return test(is_expensive_cache);
728
864
}
729
865
 
 
866
 
 
867
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
 
868
{
 
869
  int res;
 
870
  Table *table= field->table;
 
871
  Session *session= table->in_use;
 
872
  enum_check_fields tmp= session->count_cuted_fields;
 
873
  ulong sql_mode= session->variables.sql_mode;
 
874
  session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
 
875
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
876
  res= save_in_field(field, no_conversions);
 
877
  session->count_cuted_fields= tmp;
 
878
  session->variables.sql_mode= sql_mode;
 
879
  return res;
 
880
}
 
881
 
 
882
 
730
883
/*
731
884
 need a special class to adjust printing : references to aggregate functions
732
885
 must not be printed as refs because the aggregate functions that are added to
748
901
  }
749
902
};
750
903
 
 
904
 
 
905
/**
 
906
  Move SUM items out from item tree and replace with reference.
 
907
 
 
908
  @param session                        Thread handler
 
909
  @param ref_pointer_array      Pointer to array of reference fields
 
910
  @param fields         All fields in select
 
911
  @param ref                    Pointer to item
 
912
  @param skip_registered       <=> function be must skipped for registered
 
913
                               SUM items
 
914
 
 
915
  @note
 
916
    This is from split_sum_func() for items that should be split
 
917
 
 
918
    All found SUM items are added FIRST in the fields list and
 
919
    we replace the item with a reference.
 
920
 
 
921
    session->fatal_error() may be called if we are out of memory
 
922
*/
 
923
 
751
924
void Item::split_sum_func(Session *session, Item **ref_pointer_array,
752
925
                          List<Item> &fields, Item **ref,
753
926
                          bool skip_registered)
754
927
{
755
928
  /* An item of type Item_sum  is registered <=> ref_by != 0 */
756
 
  if (type() == SUM_FUNC_ITEM && 
757
 
      skip_registered &&
 
929
  if (type() == SUM_FUNC_ITEM && skip_registered &&
758
930
      ((Item_sum *) this)->ref_by)
759
931
    return;
760
 
 
761
932
  if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
762
933
      (type() == FUNC_ITEM &&
763
934
       (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
787
958
    ref_pointer_array[el]= real_itm;
788
959
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
789
960
                                           ref_pointer_array + el, 0, name)))
790
 
      return; /* fatal_error is set */
 
961
      return;                                   // fatal_error is set
791
962
    if (type() == SUM_FUNC_ITEM)
792
963
      item_ref->depended_from= ((Item_sum *) this)->depended_from();
793
964
    fields.push_front(real_itm);
798
969
/*
799
970
  Functions to convert item to field (for send_fields)
800
971
*/
 
972
 
 
973
/* ARGSUSED */
801
974
bool Item::fix_fields(Session *, Item **)
802
975
{
803
 
  /* We do not check fields which are fixed during construction */
804
 
  assert(! fixed || basic_const_item());
805
 
  fixed= true;
 
976
 
 
977
  // We do not check fields which are fixed during construction
 
978
  assert(fixed == 0 || basic_const_item());
 
979
  fixed= 1;
806
980
  return false;
807
981
}
808
982
 
 
983
 
 
984
/**
 
985
  Mark item and Select_Lexs as dependent if item was resolved in
 
986
  outer SELECT.
 
987
 
 
988
  @param session             thread handler
 
989
  @param last            select from which current item depend
 
990
  @param current         current select
 
991
  @param resolved_item   item which was resolved in outer SELECT(for warning)
 
992
  @param mark_item       item which should be marked (can be differ in case of
 
993
                         substitution)
 
994
*/
 
995
 
809
996
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current,
810
997
                              Item_ident *resolved_item,
811
998
                              Item_ident *mark_item)
821
1008
  if (session->lex->describe & DESCRIBE_EXTENDED)
822
1009
  {
823
1010
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
824
 
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
 
1011
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
825
1012
            db_name, (db_name[0] ? "." : ""),
826
1013
            table_name, (table_name [0] ? "." : ""),
827
1014
            resolved_item->field_name,
831
1018
  }
832
1019
}
833
1020
 
 
1021
 
 
1022
/**
 
1023
  Mark range of selects and resolved identifier (field/reference)
 
1024
  item as dependent.
 
1025
 
 
1026
  @param session             thread handler
 
1027
  @param last_select     select where resolved_item was resolved
 
1028
  @param current_sel     current select (select where resolved_item was placed)
 
1029
  @param found_field     field which was found during resolving
 
1030
  @param found_item      Item which was found during resolving (if resolved
 
1031
                         identifier belongs to VIEW)
 
1032
  @param resolved_item   Identifier which was resolved
 
1033
 
 
1034
  @note
 
1035
    We have to mark all items between current_sel (including) and
 
1036
    last_select (excluding) as dependend (select before last_select should
 
1037
    be marked with actual table mask used by resolved item, all other with
 
1038
    OUTER_REF_TABLE_BIT) and also write dependence information to Item of
 
1039
    resolved identifier.
 
1040
*/
 
1041
 
834
1042
void mark_select_range_as_dependent(Session *session,
835
1043
                                    Select_Lex *last_select,
836
1044
                                    Select_Lex *current_sel,
847
1055
  for (; previous_select->outer_select() != last_select;
848
1056
       previous_select= previous_select->outer_select())
849
1057
  {
850
 
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
 
1058
    Item_subselect *prev_subselect_item=
 
1059
      previous_select->master_unit()->item;
851
1060
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
852
 
    prev_subselect_item->const_item_cache= false;
 
1061
    prev_subselect_item->const_item_cache= 0;
853
1062
  }
854
1063
  {
855
 
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
 
1064
    Item_subselect *prev_subselect_item=
 
1065
      previous_select->master_unit()->item;
856
1066
    Item_ident *dependent= resolved_item;
857
1067
    if (found_field == view_ref_found)
858
1068
    {
859
1069
      Item::Type type= found_item->type();
860
 
      prev_subselect_item->used_tables_cache|= found_item->used_tables();
 
1070
      prev_subselect_item->used_tables_cache|=
 
1071
        found_item->used_tables();
861
1072
      dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ?
862
1073
                  (Item_ident*) found_item :
863
1074
                  0);
864
1075
    }
865
1076
    else
866
 
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
867
 
    prev_subselect_item->const_item_cache= false;
 
1077
      prev_subselect_item->used_tables_cache|=
 
1078
        found_field->table->map;
 
1079
    prev_subselect_item->const_item_cache= 0;
868
1080
    mark_as_dependent(session, last_select, current_sel, resolved_item,
869
1081
                      dependent);
870
1082
  }
871
1083
}
872
1084
 
 
1085
 
873
1086
/**
874
1087
  Search a GROUP BY clause for a field with a certain name.
875
1088
 
884
1097
    - the found item on success
885
1098
    - NULL if find_item is not in group_list
886
1099
*/
887
 
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
 
1100
 
 
1101
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
888
1102
{
889
1103
  const char *db_name;
890
1104
  const char *table_name;
891
1105
  const char *field_name;
892
 
  Order *found_group= NULL;
893
 
  int found_match_degree= 0;
 
1106
  order_st      *found_group= NULL;
 
1107
  int         found_match_degree= 0;
894
1108
  Item_ident *cur_field;
895
 
  int cur_match_degree= 0;
896
 
  char name_buff[NAME_LEN+1];
 
1109
  int         cur_match_degree= 0;
 
1110
  char        name_buff[NAME_LEN+1];
897
1111
 
898
1112
  if (find_item->type() == Item::FIELD_ITEM ||
899
1113
      find_item->type() == Item::REF_ITEM)
900
1114
  {
901
 
    db_name= ((Item_ident*) find_item)->db_name;
 
1115
    db_name=    ((Item_ident*) find_item)->db_name;
902
1116
    table_name= ((Item_ident*) find_item)->table_name;
903
1117
    field_name= ((Item_ident*) find_item)->field_name;
904
1118
  }
905
1119
  else
906
1120
    return NULL;
907
1121
 
908
 
  if (db_name)
 
1122
  if (db_name && lower_case_table_names)
909
1123
  {
910
1124
    /* Convert database to lower case for comparison */
911
1125
    strncpy(name_buff, db_name, sizeof(name_buff)-1);
915
1129
 
916
1130
  assert(field_name != 0);
917
1131
 
918
 
  for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
1132
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
919
1133
  {
920
1134
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
921
1135
    {
924
1138
 
925
1139
      assert(cur_field->field_name != 0);
926
1140
 
927
 
      if (! my_strcasecmp(system_charset_info, cur_field->field_name, field_name))
 
1141
      if (!my_strcasecmp(system_charset_info,
 
1142
                         cur_field->field_name, field_name))
928
1143
        ++cur_match_degree;
929
1144
      else
930
1145
        continue;
940
1155
        if (cur_field->db_name && db_name)
941
1156
        {
942
1157
          /* If field_name is also qualified by a database name. */
943
 
          if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
944
 
          {
 
1158
          if (strcmp(cur_field->db_name, db_name))
945
1159
            /* Same field names, different databases. */
946
1160
            return NULL;
947
 
          }
948
1161
          ++cur_match_degree;
949
1162
        }
950
1163
      }
954
1167
        found_match_degree= cur_match_degree;
955
1168
        found_group= cur_group;
956
1169
      }
957
 
      else if (found_group &&
958
 
               (cur_match_degree == found_match_degree) &&
 
1170
      else if (found_group && (cur_match_degree == found_match_degree) &&
959
1171
               ! (*(found_group->item))->eq(cur_field, 0))
960
1172
      {
961
1173
        /*
963
1175
          best match, they must reference the same column, otherwise the field
964
1176
          is ambiguous.
965
1177
        */
966
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
 
1178
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
 
1179
                 find_item->full_name(), current_session->where);
967
1180
        return NULL;
968
1181
      }
969
1182
    }
971
1184
 
972
1185
  if (found_group)
973
1186
    return found_group->item;
974
 
 
975
 
  return NULL;
 
1187
  else
 
1188
    return NULL;
976
1189
}
977
1190
 
978
 
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
 
1191
 
 
1192
/**
 
1193
  Resolve a column reference in a sub-select.
 
1194
 
 
1195
  Resolve a column reference (usually inside a HAVING clause) against the
 
1196
  SELECT and GROUP BY clauses of the query described by 'select'. The name
 
1197
  resolution algorithm searches both the SELECT and GROUP BY clauses, and in
 
1198
  case of a name conflict prefers GROUP BY column names over SELECT names. If
 
1199
  both clauses contain different fields with the same names, a warning is
 
1200
  issued that name of 'ref' is ambiguous. We extend ANSI SQL in that when no
 
1201
  GROUP BY column is found, then a HAVING name is resolved as a possibly
 
1202
  derived SELECT column. This extension is allowed only if the
 
1203
  MODE_ONLY_FULL_GROUP_BY sql mode isn't enabled.
 
1204
 
 
1205
  @param session     current thread
 
1206
  @param ref     column reference being resolved
 
1207
  @param select  the select that ref is resolved against
 
1208
 
 
1209
  @note
 
1210
    The resolution procedure is:
 
1211
    - Search for a column or derived column named col_ref_i [in table T_j]
 
1212
    in the SELECT clause of Q.
 
1213
    - Search for a column named col_ref_i [in table T_j]
 
1214
    in the GROUP BY clause of Q.
 
1215
    - If found different columns with the same name in GROUP BY and SELECT
 
1216
    - issue a warning and return the GROUP BY column,
 
1217
    - otherwise
 
1218
    - if the MODE_ONLY_FULL_GROUP_BY mode is enabled return error
 
1219
    - else return the found SELECT column.
 
1220
 
 
1221
 
 
1222
  @return
 
1223
    - NULL - there was an error, and the error was already reported
 
1224
    - not_found_item - the item was not resolved, no error was reported
 
1225
    - resolved item - if the item was resolved
 
1226
*/
 
1227
 
 
1228
Item**
 
1229
resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
979
1230
{
980
1231
  Item **group_by_ref= NULL;
981
1232
  Item **select_ref= NULL;
982
 
  Order *group_list= (Order*) select->group_list.first;
 
1233
  order_st *group_list= (order_st*) select->group_list.first;
983
1234
  bool ambiguous_fields= false;
984
1235
  uint32_t counter;
985
1236
  enum_resolution_type resolution;
988
1239
    Search for a column or derived column named as 'ref' in the SELECT
989
1240
    clause of the current select.
990
1241
  */
991
 
  if (!(select_ref= find_item_in_list(session,
992
 
                                      ref, *(select->get_item_list()),
 
1242
  if (!(select_ref= find_item_in_list(ref, *(select->get_item_list()),
993
1243
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
994
1244
                                      &resolution)))
995
1245
    return NULL; /* Some error occurred. */
999
1249
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
1000
1250
  if (select->having_fix_field && !ref->with_sum_func && group_list)
1001
1251
  {
1002
 
    group_by_ref= find_field_in_group_list(session, ref, group_list);
 
1252
    group_by_ref= find_field_in_group_list(ref, group_list);
1003
1253
 
1004
1254
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
1005
1255
    if (group_by_ref && (select_ref != not_found_item) &&
1008
1258
      ambiguous_fields= true;
1009
1259
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
1010
1260
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
1011
 
                          session->where());
 
1261
                          current_session->where);
1012
1262
 
1013
1263
    }
1014
1264
  }
1036
1286
  return (Item**) not_found_item;
1037
1287
}
1038
1288
 
1039
 
void Item::init_make_field(SendField *tmp_field,
1040
 
                           enum enum_field_types field_type_arg)
 
1289
void Item::init_make_field(Send_field *tmp_field,
 
1290
                           enum enum_field_types field_type_arg)
1041
1291
{
1042
1292
  char *empty_name= (char*) "";
1043
 
  tmp_field->db_name=   empty_name;
1044
 
  tmp_field->org_table_name= empty_name;
1045
 
  tmp_field->org_col_name= empty_name;
1046
 
  tmp_field->table_name= empty_name;
1047
 
  tmp_field->col_name= name;
1048
 
  tmp_field->charsetnr= collation.collation->number;
1049
 
  tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) |
1050
 
                    (my_binary_compare(collation.collation) ?
1051
 
                      BINARY_FLAG : 0);
1052
 
  tmp_field->type= field_type_arg;
1053
 
  tmp_field->length= max_length;
1054
 
  tmp_field->decimals= decimals;
 
1293
  tmp_field->db_name=           empty_name;
 
1294
  tmp_field->org_table_name=    empty_name;
 
1295
  tmp_field->org_col_name=      empty_name;
 
1296
  tmp_field->table_name=        empty_name;
 
1297
  tmp_field->col_name=          name;
 
1298
  tmp_field->charsetnr=         collation.collation->number;
 
1299
  tmp_field->flags=             (maybe_null ? 0 : NOT_NULL_FLAG) |
 
1300
                                (my_binary_compare(collation.collation) ?
 
1301
                                 BINARY_FLAG : 0);
 
1302
  tmp_field->type=              field_type_arg;
 
1303
  tmp_field->length=max_length;
 
1304
  tmp_field->decimals=decimals;
1055
1305
}
1056
1306
 
1057
 
void Item::make_field(SendField *tmp_field)
 
1307
void Item::make_field(Send_field *tmp_field)
1058
1308
{
1059
1309
  init_make_field(tmp_field, field_type());
1060
1310
}
1061
1311
 
 
1312
 
1062
1313
enum_field_types Item::string_field_type() const
1063
1314
{
1064
1315
  enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
1070
1321
enum_field_types Item::field_type() const
1071
1322
{
1072
1323
  switch (result_type()) {
1073
 
  case STRING_RESULT:  
1074
 
    return string_field_type();
1075
 
  case INT_RESULT:     
1076
 
    return DRIZZLE_TYPE_LONGLONG;
1077
 
  case DECIMAL_RESULT: 
1078
 
    return DRIZZLE_TYPE_DECIMAL;
1079
 
  case REAL_RESULT:    
1080
 
    return DRIZZLE_TYPE_DOUBLE;
 
1324
  case STRING_RESULT:  return string_field_type();
 
1325
  case INT_RESULT:     return DRIZZLE_TYPE_LONGLONG;
 
1326
  case DECIMAL_RESULT: return DRIZZLE_TYPE_NEWDECIMAL;
 
1327
  case REAL_RESULT:    return DRIZZLE_TYPE_DOUBLE;
1081
1328
  case ROW_RESULT:
 
1329
  default:
1082
1330
    assert(0);
 
1331
    return DRIZZLE_TYPE_VARCHAR;
1083
1332
  }
1084
 
 
1085
 
  abort();
1086
1333
}
1087
1334
 
 
1335
 
1088
1336
bool Item::is_datetime()
1089
1337
{
1090
1338
  switch (field_type())
1091
1339
  {
1092
 
    case DRIZZLE_TYPE_TIME:
1093
1340
    case DRIZZLE_TYPE_DATE:
1094
1341
    case DRIZZLE_TYPE_DATETIME:
1095
1342
    case DRIZZLE_TYPE_TIMESTAMP:
1096
 
    case DRIZZLE_TYPE_MICROTIME:
1097
1343
      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;
 
1344
    default:
 
1345
      break;
1109
1346
  }
1110
 
 
1111
 
  assert(0);
1112
 
  abort();
 
1347
  return false;
1113
1348
}
1114
1349
 
 
1350
 
1115
1351
String *Item::check_well_formed_result(String *str, bool send_error)
1116
1352
{
1117
1353
  /* Check whether we got a well-formed string */
1122
1358
                                       str->length(), &well_formed_error);
1123
1359
  if (wlen < str->length())
1124
1360
  {
 
1361
    Session *session= current_session;
1125
1362
    char hexbuf[7];
1126
1363
    enum DRIZZLE_ERROR::enum_warning_level level;
1127
1364
    uint32_t diff= str->length() - wlen;
1128
 
    set_if_smaller(diff, 3U);
1129
 
    (void) drizzled_string_to_hex(hexbuf, str->ptr() + wlen, diff);
 
1365
    set_if_smaller(diff, 3);
 
1366
    drizzleclient_drizzleclient_octet2hex(hexbuf, str->ptr() + wlen, diff);
1130
1367
    if (send_error)
1131
1368
    {
1132
1369
      my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
1138
1375
      null_value= 1;
1139
1376
      str= 0;
1140
1377
    }
1141
 
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
 
1378
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1142
1379
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1143
1380
  }
1144
1381
  return str;
1145
1382
}
1146
1383
 
 
1384
/*
 
1385
  Compare two items using a given collation
 
1386
 
 
1387
  SYNOPSIS
 
1388
    eq_by_collation()
 
1389
    item               item to compare with
 
1390
    binary_cmp         true <-> compare as binaries
 
1391
    cs                 collation to use when comparing strings
 
1392
 
 
1393
  DESCRIPTION
 
1394
    This method works exactly as Item::eq if the collation cs coincides with
 
1395
    the collation of the compared objects. Otherwise, first the collations that
 
1396
    differ from cs are replaced for cs and then the items are compared by
 
1397
    Item::eq. After the comparison the original collations of items are
 
1398
    restored.
 
1399
 
 
1400
  RETURN
 
1401
    1    compared items has been detected as equal
 
1402
    0    otherwise
 
1403
*/
 
1404
 
1147
1405
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
1148
1406
{
1149
1407
  const CHARSET_INFO *save_cs= 0;
1166
1424
  return res;
1167
1425
}
1168
1426
 
 
1427
 
 
1428
/**
 
1429
  Create a field to hold a string value from an item.
 
1430
 
 
1431
  If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n
 
1432
  If max_length > 0 create a varchar @n
 
1433
  If max_length == 0 create a CHAR(0)
 
1434
 
 
1435
  @param table          Table for which the field is created
 
1436
*/
 
1437
 
1169
1438
Field *Item::make_string_field(Table *table)
1170
1439
{
1171
1440
  Field *field;
1172
1441
  assert(collation.collation);
1173
1442
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1174
 
  {
1175
1443
    field= new Field_blob(max_length, maybe_null, name,
1176
1444
                          collation.collation);
1177
 
  }
1178
1445
  else
1179
 
  {
1180
 
    table->setVariableWidth();
1181
 
    field= new Field_varstring(max_length, maybe_null, name,
 
1446
    field= new Field_varstring(max_length, maybe_null, name, table->s,
1182
1447
                               collation.collation);
1183
 
  }
1184
1448
 
1185
1449
  if (field)
1186
1450
    field->init(table);
1187
1451
  return field;
1188
1452
}
1189
1453
 
 
1454
 
 
1455
/**
 
1456
  Create a field based on field_type of argument.
 
1457
 
 
1458
  For now, this is only used to create a field for
 
1459
  IFNULL(x,something) and time functions
 
1460
 
 
1461
  @retval
 
1462
    NULL  error
 
1463
  @retval
 
1464
    \#    Created field
 
1465
*/
 
1466
 
1190
1467
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
1191
1468
{
1192
1469
  /*
1193
1470
    The field functions defines a field to be not null if null_ptr is not 0
1194
1471
  */
1195
1472
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1196
 
  Field *field= NULL;
 
1473
  Field *field;
1197
1474
 
1198
1475
  switch (field_type()) {
1199
 
  case DRIZZLE_TYPE_DECIMAL:
1200
 
    field= new Field_decimal((unsigned char*) 0,
1201
 
                                 max_length,
1202
 
                                 null_ptr,
1203
 
                                 0,
1204
 
                                 Field::NONE,
1205
 
                                 name,
1206
 
                                 decimals);
 
1476
  case DRIZZLE_TYPE_NEWDECIMAL:
 
1477
    field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
 
1478
                                 Field::NONE, name, decimals, 0,
 
1479
                                 unsigned_flag);
1207
1480
    break;
1208
1481
  case DRIZZLE_TYPE_LONG:
1209
 
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1482
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1483
                          name, 0, unsigned_flag);
1210
1484
    break;
1211
1485
  case DRIZZLE_TYPE_LONGLONG:
1212
 
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1486
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1487
                              name, 0, unsigned_flag);
1213
1488
    break;
1214
1489
  case DRIZZLE_TYPE_DOUBLE:
1215
1490
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1216
 
                            name, decimals, 0, unsigned_flag);
 
1491
                            name, decimals, 0, unsigned_flag);
1217
1492
    break;
1218
1493
  case DRIZZLE_TYPE_NULL:
1219
 
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
 
1494
    field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
 
1495
                          name, &my_charset_bin);
1220
1496
    break;
1221
1497
  case DRIZZLE_TYPE_DATE:
1222
1498
    field= new Field_date(maybe_null, name, &my_charset_bin);
1223
1499
    break;
1224
 
 
1225
 
  case DRIZZLE_TYPE_MICROTIME:
1226
 
    field= new field::Microtime(maybe_null, name);
 
1500
  case DRIZZLE_TYPE_TIME:
 
1501
    field= new Field_time(maybe_null, name, &my_charset_bin);
1227
1502
    break;
1228
 
 
1229
1503
  case DRIZZLE_TYPE_TIMESTAMP:
1230
 
    field= new field::Epoch(maybe_null, name);
 
1504
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1231
1505
    break;
1232
1506
  case DRIZZLE_TYPE_DATETIME:
1233
1507
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1234
1508
    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:
 
1509
  default:
 
1510
    /* This case should never be chosen */
 
1511
    assert(0);
 
1512
    /* Fall through to make_string_field() */
1240
1513
  case DRIZZLE_TYPE_ENUM:
1241
1514
  case DRIZZLE_TYPE_VARCHAR:
1242
1515
    return make_string_field(table);
1243
1516
  case DRIZZLE_TYPE_BLOB:
 
1517
    if (this->type() == Item::TYPE_HOLDER)
 
1518
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
 
1519
                            1);
 
1520
    else
1244
1521
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1245
1522
    break;                                      // Blob handled outside of case
1246
1523
  }
1247
 
  assert(field);
1248
 
 
1249
1524
  if (field)
1250
1525
    field->init(table);
1251
1526
  return field;
1252
1527
}
1253
1528
 
 
1529
 
1254
1530
/*
1255
1531
  This implementation can lose str_value content, so if the
1256
1532
  Item uses str_value to store something, it should
1257
1533
  reimplement it's ::save_in_field() as Item_string, for example, does
1258
1534
*/
 
1535
 
1259
1536
int Item::save_in_field(Field *field, bool no_conversions)
1260
1537
{
1261
1538
  int error;
1297
1574
  }
1298
1575
  else if (result_type() == DECIMAL_RESULT)
1299
1576
  {
1300
 
    type::Decimal decimal_value;
1301
 
    type::Decimal *value= val_decimal(&decimal_value);
 
1577
    my_decimal decimal_value;
 
1578
    my_decimal *value= val_decimal(&decimal_value);
1302
1579
    if (null_value)
1303
1580
      return set_field_to_null_with_conversions(field, no_conversions);
1304
1581
    field->set_notnull();
1315
1592
  return error;
1316
1593
}
1317
1594
 
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
 
bool Item::send(plugin::Client *client, String *buffer)
 
1595
 
 
1596
/**
 
1597
  This is only called from items that is not of type item_field.
 
1598
*/
 
1599
 
 
1600
bool Item::send(Protocol *protocol, String *buffer)
1378
1601
{
1379
1602
  bool result= false;
1380
1603
  enum_field_types f_type;
1381
1604
 
1382
1605
  switch ((f_type=field_type())) {
1383
 
  case DRIZZLE_TYPE_DATE:
 
1606
  default:
1384
1607
  case DRIZZLE_TYPE_NULL:
1385
1608
  case DRIZZLE_TYPE_ENUM:
1386
1609
  case DRIZZLE_TYPE_BLOB:
1387
1610
  case DRIZZLE_TYPE_VARCHAR:
1388
 
  case DRIZZLE_TYPE_BOOLEAN:
1389
 
  case DRIZZLE_TYPE_UUID:
1390
 
  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
 
    }
 
1611
  case DRIZZLE_TYPE_NEWDECIMAL:
 
1612
  {
 
1613
    String *res;
 
1614
    if ((res=val_str(buffer)))
 
1615
      result= protocol->store(res->ptr(),res->length(),res->charset());
 
1616
    break;
 
1617
  }
1397
1618
  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
 
    }
 
1619
  {
 
1620
    int64_t nr;
 
1621
    nr= val_int();
 
1622
    if (!null_value)
 
1623
      result= protocol->store_long(nr);
 
1624
    break;
 
1625
  }
1405
1626
  case DRIZZLE_TYPE_LONGLONG:
1406
 
    {
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;
1417
 
    }
 
1627
  {
 
1628
    int64_t nr;
 
1629
    nr= val_int();
 
1630
    if (!null_value)
 
1631
      result= protocol->store_int64_t(nr, unsigned_flag);
 
1632
    break;
 
1633
  }
1418
1634
  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
 
    }
 
1635
  {
 
1636
    double nr= val_real();
 
1637
    if (!null_value)
 
1638
      result= protocol->store(nr, decimals, buffer);
 
1639
    break;
 
1640
  }
1433
1641
  case DRIZZLE_TYPE_DATETIME:
1434
 
  case DRIZZLE_TYPE_MICROTIME:
1435
1642
  case DRIZZLE_TYPE_TIMESTAMP:
 
1643
  {
 
1644
    DRIZZLE_TIME tm;
 
1645
    get_date(&tm, TIME_FUZZY_DATE);
 
1646
    if (!null_value)
1436
1647
    {
1437
 
      type::Time tm;
1438
 
      get_date(tm, TIME_FUZZY_DATE);
1439
 
      if (!null_value)
1440
 
        result= client->store(&tm);
1441
 
      break;
 
1648
      if (f_type == DRIZZLE_TYPE_DATE)
 
1649
        return protocol->store_date(&tm);
 
1650
      else
 
1651
        result= protocol->store(&tm);
1442
1652
    }
 
1653
    break;
 
1654
  }
 
1655
  case DRIZZLE_TYPE_TIME:
 
1656
  {
 
1657
    DRIZZLE_TIME tm;
 
1658
    get_time(&tm);
 
1659
    if (!null_value)
 
1660
      result= protocol->store_time(&tm);
 
1661
    break;
 
1662
  }
1443
1663
  }
1444
1664
  if (null_value)
1445
 
    result= client->store();
1446
 
 
 
1665
    result= protocol->store_null();
1447
1666
  return result;
1448
1667
}
1449
1668
 
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);
 
1669
 
 
1670
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
 
1671
{
 
1672
  return item->type() == DEFAULT_VALUE_ITEM &&
 
1673
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
 
1674
}
 
1675
 
 
1676
 
 
1677
bool Item_default_value::fix_fields(Session *session, Item **)
 
1678
{
 
1679
  Item *real_arg;
 
1680
  Item_field *field_arg;
 
1681
  Field *def_field;
 
1682
  assert(fixed == 0);
 
1683
 
 
1684
  if (!arg)
 
1685
  {
 
1686
    fixed= 1;
 
1687
    return false;
 
1688
  }
 
1689
  if (!arg->fixed && arg->fix_fields(session, &arg))
 
1690
    goto error;
 
1691
 
 
1692
 
 
1693
  real_arg= arg->real_item();
 
1694
  if (real_arg->type() != FIELD_ITEM)
 
1695
  {
 
1696
    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
 
1697
    goto error;
 
1698
  }
 
1699
 
 
1700
  field_arg= (Item_field *)real_arg;
 
1701
  if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
 
1702
  {
 
1703
    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
 
1704
    goto error;
 
1705
  }
 
1706
  if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
 
1707
    goto error;
 
1708
  memcpy(def_field, field_arg->field, field_arg->field->size_of());
 
1709
  def_field->move_field_offset((my_ptrdiff_t)
 
1710
                               (def_field->table->s->default_values -
 
1711
                                def_field->table->record[0]));
 
1712
  set_field(def_field);
 
1713
  return false;
 
1714
 
 
1715
error:
 
1716
  context->process_error(session);
 
1717
  return true;
 
1718
}
 
1719
 
 
1720
 
 
1721
void Item_default_value::print(String *str, enum_query_type query_type)
 
1722
{
 
1723
  if (!arg)
 
1724
  {
 
1725
    str->append(STRING_WITH_LEN("default"));
 
1726
    return;
 
1727
  }
 
1728
  str->append(STRING_WITH_LEN("default("));
 
1729
  arg->print(str, query_type);
 
1730
  str->append(')');
 
1731
}
 
1732
 
 
1733
 
 
1734
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
 
1735
{
 
1736
  if (!arg)
 
1737
  {
 
1738
    if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
 
1739
    {
 
1740
      if (field_arg->reset())
 
1741
      {
 
1742
        my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
 
1743
                   ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
 
1744
        return -1;
 
1745
      }
 
1746
 
 
1747
      {
 
1748
        push_warning_printf(field_arg->table->in_use,
 
1749
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1750
                            ER_NO_DEFAULT_FOR_FIELD,
 
1751
                            ER(ER_NO_DEFAULT_FOR_FIELD),
 
1752
                            field_arg->field_name);
 
1753
      }
 
1754
      return 1;
 
1755
    }
 
1756
    field_arg->set_default();
 
1757
    return 0;
 
1758
  }
 
1759
  return Item_field::save_in_field(field_arg, no_conversions);
 
1760
}
 
1761
 
 
1762
 
 
1763
/**
 
1764
  This method like the walk method traverses the item tree, but at the
 
1765
  same time it can replace some nodes in the tree.
 
1766
*/
 
1767
 
 
1768
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
 
1769
{
 
1770
  Item *new_item= arg->transform(transformer, args);
 
1771
  if (!new_item)
 
1772
    return NULL;
 
1773
 
 
1774
  /*
 
1775
    Session::change_item_tree() should be called only if the tree was
 
1776
    really transformed, i.e. when a new item has been created.
 
1777
    Otherwise we'll be allocating a lot of unnecessary memory for
 
1778
    change records at each execution.
 
1779
  */
 
1780
  if (arg != new_item)
 
1781
    current_session->change_item_tree(&arg, new_item);
 
1782
  return (this->*transformer)(args);
1486
1783
}
1487
1784
 
1488
1785
Item_result item_cmp_type(Item_result a,Item_result b)
1489
1786
{
1490
1787
  if (a == STRING_RESULT && b == STRING_RESULT)
1491
1788
    return STRING_RESULT;
1492
 
 
1493
1789
  if (a == INT_RESULT && b == INT_RESULT)
1494
1790
    return INT_RESULT;
1495
1791
  else if (a == ROW_RESULT || b == ROW_RESULT)
1496
1792
    return ROW_RESULT;
1497
 
 
1498
1793
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1499
1794
      (b == INT_RESULT || b == DECIMAL_RESULT))
1500
1795
    return DECIMAL_RESULT;
1501
 
 
1502
1796
  return REAL_RESULT;
1503
1797
}
1504
1798
 
 
1799
 
1505
1800
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
1506
1801
{
1507
1802
  Item *item= *ref;
1508
1803
  Item *new_item= NULL;
1509
1804
  if (item->basic_const_item())
1510
 
    return; /* Can't be better */
 
1805
    return;                                     // Can't be better
1511
1806
  Item_result res_type=item_cmp_type(comp_item->result_type(),
1512
1807
                                     item->result_type());
1513
 
  char *name=item->name; /* Alloced by memory::sql_alloc */
 
1808
  char *name=item->name;                        // Alloced by sql_alloc
1514
1809
 
1515
1810
  switch (res_type) {
1516
1811
  case STRING_RESULT:
 
1812
  {
 
1813
    char buff[MAX_FIELD_WIDTH];
 
1814
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
 
1815
    result=item->val_str(&tmp);
 
1816
    if (item->null_value)
 
1817
      new_item= new Item_null(name);
 
1818
    else
1517
1819
    {
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;
 
1820
      uint32_t length= result->length();
 
1821
      char *tmp_str= sql_strmake(result->ptr(), length);
 
1822
      new_item= new Item_string(name, tmp_str, length, result->charset());
1530
1823
    }
 
1824
    break;
 
1825
  }
1531
1826
  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
 
    }
 
1827
  {
 
1828
    int64_t result=item->val_int();
 
1829
    uint32_t length=item->max_length;
 
1830
    bool null_value=item->null_value;
 
1831
    new_item= (null_value ? (Item*) new Item_null(name) :
 
1832
               (Item*) new Item_int(name, result, length));
 
1833
    break;
 
1834
  }
1540
1835
  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).
 
1836
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
 
1837
  {
 
1838
    /*
 
1839
      Substitute constants only in Item_rows. Don't affect other Items
 
1840
      with ROW_RESULT (eg Item_singlerow_subselect).
1546
1841
 
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 */
 
1842
      For such Items more optimal is to detect if it is constant and replace
 
1843
      it with Item_row. This would optimize queries like this:
 
1844
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
 
1845
    */
 
1846
    Item_row *item_row= (Item_row*) item;
 
1847
    Item_row *comp_item_row= (Item_row*) comp_item;
 
1848
    uint32_t col;
 
1849
    new_item= 0;
 
1850
    /*
 
1851
      If item and comp_item are both Item_rows and have same number of cols
 
1852
      then process items in Item_row one by one.
 
1853
      We can't ignore NULL values here as this item may be used with <=>, in
 
1854
      which case NULL's are significant.
 
1855
    */
 
1856
    assert(item->result_type() == comp_item->result_type());
 
1857
    assert(item_row->cols() == comp_item_row->cols());
 
1858
    col= item_row->cols();
 
1859
    while (col-- > 0)
 
1860
      resolve_const_item(session, item_row->addr(col),
 
1861
                         comp_item_row->element_index(col));
 
1862
    break;
 
1863
  }
 
1864
  /* Fallthrough */
1570
1865
  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
 
    }
 
1866
  {                                             // It must REAL_RESULT
 
1867
    double result= item->val_real();
 
1868
    uint32_t length=item->max_length,decimals=item->decimals;
 
1869
    bool null_value=item->null_value;
 
1870
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
 
1871
               new Item_float(name, result, decimals, length));
 
1872
    break;
 
1873
  }
1579
1874
  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
 
 
 
1875
  {
 
1876
    my_decimal decimal_value;
 
1877
    my_decimal *result= item->val_decimal(&decimal_value);
 
1878
    uint32_t length= item->max_length, decimals= item->decimals;
 
1879
    bool null_value= item->null_value;
 
1880
    new_item= (null_value ?
 
1881
               (Item*) new Item_null(name) :
 
1882
               (Item*) new Item_decimal(name, result, length, decimals));
 
1883
    break;
 
1884
  }
 
1885
  default:
 
1886
    assert(0);
 
1887
  }
1592
1888
  if (new_item)
1593
1889
    session->change_item_tree(ref, new_item);
1594
1890
}
1595
1891
 
 
1892
/**
 
1893
  Return true if the value stored in the field is equal to the const
 
1894
  item.
 
1895
 
 
1896
  We need to use this on the range optimizer because in some cases
 
1897
  we can't store the value in the field without some precision/character loss.
 
1898
*/
 
1899
 
1596
1900
bool field_is_equal_to_item(Field *field,Item *item)
1597
1901
{
1598
1902
 
1607
1911
    item_result=item->val_str(&item_tmp);
1608
1912
    if (item->null_value)
1609
1913
      return 1;                                 // This must be true
1610
 
    field->val_str_internal(&field_tmp);
1611
 
    return not stringcmp(&field_tmp,item_result);
 
1914
    field->val_str(&field_tmp);
 
1915
    return !stringcmp(&field_tmp,item_result);
1612
1916
  }
1613
 
 
1614
1917
  if (res_type == INT_RESULT)
1615
1918
    return 1;                                   // Both where of type int
1616
 
 
1617
1919
  if (res_type == DECIMAL_RESULT)
1618
1920
  {
1619
 
    type::Decimal item_buf, *item_val,
 
1921
    my_decimal item_buf, *item_val,
1620
1922
               field_buf, *field_val;
1621
1923
    item_val= item->val_decimal(&item_buf);
1622
1924
    if (item->null_value)
1623
1925
      return 1;                                 // This must be true
1624
1926
    field_val= field->val_decimal(&field_buf);
1625
 
    return !class_decimal_cmp(item_val, field_val);
 
1927
    return !my_decimal_cmp(item_val, field_val);
1626
1928
  }
1627
 
 
1628
1929
  double result= item->val_real();
1629
1930
  if (item->null_value)
1630
1931
    return 1;
1631
 
 
1632
1932
  return result == field->val_real();
1633
1933
}
1634
1934
 
 
1935
/**
 
1936
  Dummy error processor used by default by Name_resolution_context.
 
1937
 
 
1938
  @note
 
1939
    do nothing
 
1940
*/
 
1941
 
1635
1942
void dummy_error_processor(Session *, void *)
1636
1943
{}
1637
1944
 
1658
1965
  @retval
1659
1966
    new_created field
1660
1967
*/
 
1968
 
1661
1969
static Field *create_tmp_field_from_item(Session *,
1662
1970
                                         Item *item, Table *table,
1663
1971
                                         Item ***copy_func, bool modify_item,
1664
1972
                                         uint32_t convert_blob_length)
1665
1973
{
1666
1974
  bool maybe_null= item->maybe_null;
1667
 
  Field *new_field= NULL;
 
1975
  Field *new_field;
1668
1976
 
1669
1977
  switch (item->result_type()) {
1670
1978
  case REAL_RESULT:
1671
1979
    new_field= new Field_double(item->max_length, maybe_null,
1672
1980
                                item->name, item->decimals, true);
1673
1981
    break;
1674
 
 
1675
1982
  case INT_RESULT:
1676
1983
    /*
1677
1984
      Select an integer type with the minimal fit precision.
1678
1985
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1679
1986
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1680
 
      Int32 -> make them field::Int64.
 
1987
      Field_long : make them Field_int64_t.
1681
1988
    */
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
 
    }
 
1989
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1990
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
1991
                                   item->name, item->unsigned_flag);
1692
1992
    else
1693
 
    {
1694
 
      new_field= new field::Int32(item->max_length, maybe_null,
1695
 
                                  item->name, item->unsigned_flag);
1696
 
    }
1697
 
 
 
1993
      new_field=new Field_long(item->max_length, maybe_null,
 
1994
                               item->name, item->unsigned_flag);
1698
1995
    break;
1699
 
 
1700
1996
  case STRING_RESULT:
1701
1997
    assert(item->collation.collation);
1702
1998
 
1706
2002
      To preserve type they needed to be handled separately.
1707
2003
    */
1708
2004
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1709
 
        type == DRIZZLE_TYPE_TIME ||
1710
 
        type == DRIZZLE_TYPE_MICROTIME ||
1711
 
        type == DRIZZLE_TYPE_DATE ||
 
2005
        type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_DATE ||
1712
2006
        type == DRIZZLE_TYPE_TIMESTAMP)
1713
 
    {
1714
2007
      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
 
    }
 
2008
    /*
 
2009
      Make sure that the blob fits into a Field_varstring which has
 
2010
      2-byte lenght.
 
2011
    */
1720
2012
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1721
2013
             convert_blob_length <= Field_varstring::MAX_SIZE &&
1722
2014
             convert_blob_length)
1723
 
    {
1724
 
      table->setVariableWidth();
1725
2015
      new_field= new Field_varstring(convert_blob_length, maybe_null,
1726
 
                                     item->name, item->collation.collation);
1727
 
    }
 
2016
                                     item->name, table->s,
 
2017
                                     item->collation.collation);
1728
2018
    else
1729
 
    {
1730
2019
      new_field= item->make_string_field(table);
1731
 
    }
1732
2020
    new_field->set_derivation(item->collation.derivation);
1733
2021
    break;
1734
 
 
1735
2022
  case DECIMAL_RESULT:
 
2023
  {
 
2024
    uint8_t dec= item->decimals;
 
2025
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
2026
    uint32_t len= item->max_length;
 
2027
 
 
2028
    /*
 
2029
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
2030
      will always throw a warning. We must limit dec to
 
2031
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
2032
    */
 
2033
 
 
2034
    if (dec > 0)
1736
2035
    {
1737
 
      uint8_t dec= item->decimals;
1738
 
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1739
 
      uint32_t len= item->max_length;
 
2036
      signed int overflow;
 
2037
 
 
2038
      dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
1740
2039
 
1741
2040
      /*
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.
 
2041
        If the value still overflows the field with the corrected dec,
 
2042
        we'll throw out decimals rather than integers. This is still
 
2043
        bad and of course throws a truncation warning.
 
2044
        +1: for decimal point
1745
2045
      */
1746
2046
 
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;
 
2047
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
2048
                                               item->unsigned_flag) - len;
 
2049
 
 
2050
      if (overflow > 0)
 
2051
        dec= cmax(0, dec - overflow);            // too long, discard fract
 
2052
      else
 
2053
        len -= item->decimals - dec;            // corrected value fits
1775
2054
    }
1776
2055
 
 
2056
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
2057
                                     dec, item->unsigned_flag);
 
2058
    break;
 
2059
  }
1777
2060
  case ROW_RESULT:
 
2061
  default:
1778
2062
    // This case should never be choosen
1779
2063
    assert(0);
1780
 
    abort();
 
2064
    new_field= 0;
 
2065
    break;
1781
2066
  }
1782
 
 
1783
2067
  if (new_field)
1784
2068
    new_field->init(table);
1785
2069
 
1786
2070
  if (copy_func && item->is_result_field())
1787
2071
    *((*copy_func)++) = item;                   // Save for copy_funcs
1788
 
 
1789
2072
  if (modify_item)
1790
2073
    item->set_result_field(new_field);
1791
 
 
1792
2074
  if (item->type() == Item::NULL_ITEM)
1793
2075
    new_field->is_created_from_null_item= true;
1794
 
 
1795
2076
  return new_field;
1796
2077
}
1797
2078
 
1798
 
Field *create_tmp_field(Session *session,
1799
 
                        Table *table,
1800
 
                        Item *item,
1801
 
                        Item::Type type,
1802
 
                        Item ***copy_func,
1803
 
                        Field **from_field,
1804
 
                        Field **default_field,
1805
 
                        bool group,
1806
 
                        bool modify_item,
1807
 
                        bool make_copy_field,
 
2079
Field *create_tmp_field(Session *session, Table *table,Item *item,
 
2080
                        Item::Type type, Item ***copy_func, Field **from_field,
 
2081
                        Field **default_field, bool group, bool modify_item,
 
2082
                        bool, bool make_copy_field,
1808
2083
                        uint32_t convert_blob_length)
1809
2084
{
1810
2085
  Field *result;
1848
2123
        field->result_field= result;
1849
2124
    }
1850
2125
    else
1851
 
    {
1852
2126
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1853
2127
                                          orig_item ? orig_item->name :
1854
2128
                                          item->name,
1856
2130
                                          modify_item ? field :
1857
2131
                                          NULL,
1858
2132
                                          convert_blob_length);
1859
 
    }
1860
2133
    if (orig_type == Item::REF_ITEM && orig_modify)
1861
2134
      ((Item_ref*)orig_item)->set_result_field(result);
1862
2135
    if (field->field->eq_def(result))
1896
2169
  }
1897
2170
}
1898
2171
 
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 */
 
2172
 
 
2173
/**
 
2174
  Wrapper of hide_view_error call for Name_resolution_context error
 
2175
  processor.
 
2176
 
 
2177
  @note
 
2178
    hide view underlying tables details in error messages
 
2179
*/
 
2180
 
 
2181
/*****************************************************************************
 
2182
** Instantiate templates
 
2183
*****************************************************************************/
 
2184
 
 
2185
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
2186
template class List<Item>;
 
2187
template class List_iterator<Item>;
 
2188
template class List_iterator_fast<Item>;
 
2189
template class List_iterator_fast<Item_field>;
 
2190
template class List<List_item>;
 
2191
#endif