~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

fix variables_c test, remove have_community checks, no longer valid

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