~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2009-02-20 22:48:37 UTC
  • Revision ID: brian@tangent.org-20090220224837-fw5wrf46n4ru3e6a
First pass of stripping uint

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
15
19
 
16
20
 
17
21
#include <drizzled/server_includes.h>
 
22
#include CMATH_H
 
23
 
18
24
#include <drizzled/sql_select.h>
19
 
#include <drizzled/drizzled_error_messages.h>
 
25
#include <drizzled/error.h>
 
26
#include <drizzled/show.h>
 
27
#include <drizzled/item/cmpfunc.h>
 
28
#include <drizzled/item/cache_row.h>
 
29
#include <drizzled/item/type_holder.h>
 
30
#include <drizzled/item/sum.h>
 
31
#include <drizzled/item/copy_string.h>
 
32
#include <drizzled/function/str/conv_charset.h>
 
33
#include <drizzled/virtual_column_info.h>
 
34
#include <drizzled/sql_base.h>
 
35
 
 
36
 
 
37
#include <drizzled/field/str.h>
 
38
#include <drizzled/field/longstr.h>
 
39
#include <drizzled/field/num.h>
 
40
#include <drizzled/field/blob.h>
 
41
#include <drizzled/field/enum.h>
 
42
#include <drizzled/field/null.h>
 
43
#include <drizzled/field/date.h>
 
44
#include <drizzled/field/decimal.h>
 
45
#include <drizzled/field/real.h>
 
46
#include <drizzled/field/double.h>
 
47
#include <drizzled/field/long.h>
 
48
#include <drizzled/field/int64_t.h>
 
49
#include <drizzled/field/num.h>
 
50
#include <drizzled/field/timetype.h>
 
51
#include <drizzled/field/timestamp.h>
 
52
#include <drizzled/field/datetime.h>
 
53
#include <drizzled/field/varstring.h>
 
54
 
 
55
 
 
56
#if defined(CMATH_NAMESPACE)
 
57
using namespace CMATH_NAMESPACE;
 
58
#endif
20
59
 
21
60
const String my_null_string("NULL", 4, default_charset_info);
22
61
 
23
 
/****************************************************************************/
24
 
 
25
 
/* Hybrid_type_traits {_real} */
26
 
 
27
 
void Hybrid_type_traits::fix_length_and_dec(Item *item, Item *arg) const
28
 
{
29
 
  item->decimals= NOT_FIXED_DEC;
30
 
  item->max_length= item->float_length(arg->decimals);
31
 
}
32
 
 
33
 
static const Hybrid_type_traits real_traits_instance;
34
 
 
35
 
const Hybrid_type_traits *Hybrid_type_traits::instance()
36
 
{
37
 
  return &real_traits_instance;
38
 
}
39
 
 
40
 
 
41
 
my_decimal *
42
 
Hybrid_type_traits::val_decimal(Hybrid_type *val,
43
 
                                my_decimal *to __attribute__((unused))) const
44
 
{
45
 
  double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
46
 
  return val->dec_buf;
47
 
}
48
 
 
49
 
 
50
 
String *
51
 
Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8_t decimals) const
52
 
{
53
 
  to->set_real(val->real, decimals, &my_charset_bin);
54
 
  return to;
55
 
}
56
 
 
57
 
/* Hybrid_type_traits_decimal */
58
 
static const Hybrid_type_traits_decimal decimal_traits_instance;
59
 
 
60
 
const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
61
 
{
62
 
  return &decimal_traits_instance;
63
 
}
64
 
 
65
 
 
66
 
void
67
 
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
68
 
{
69
 
  item->decimals= arg->decimals;
70
 
  item->max_length= cmin(arg->max_length + DECIMAL_LONGLONG_DIGITS,
71
 
                        (unsigned int)DECIMAL_MAX_STR_LENGTH);
72
 
}
73
 
 
74
 
 
75
 
void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
76
 
{
77
 
  my_decimal_set_zero(&val->dec_buf[0]);
78
 
  val->used_dec_buf_no= 0;
79
 
}
80
 
 
81
 
 
82
 
void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
83
 
{
84
 
  my_decimal_add(E_DEC_FATAL_ERROR,
85
 
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
86
 
                 &val->dec_buf[val->used_dec_buf_no],
87
 
                 f->val_decimal(&val->dec_buf[2]));
88
 
  val->used_dec_buf_no^= 1;
89
 
}
90
 
 
91
 
 
92
 
/**
93
 
  @todo
94
 
  what is '4' for scale?
95
 
*/
96
 
void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
97
 
{
98
 
  int2my_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
99
 
  /* XXX: what is '4' for scale? */
100
 
  my_decimal_div(E_DEC_FATAL_ERROR,
101
 
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
102
 
                 &val->dec_buf[val->used_dec_buf_no],
103
 
                 &val->dec_buf[2], 4);
104
 
  val->used_dec_buf_no^= 1;
105
 
}
106
 
 
107
 
 
108
 
int64_t
109
 
Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
110
 
{
111
 
  int64_t result;
112
 
  my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
113
 
                 unsigned_flag, &result);
114
 
  return result;
115
 
}
116
 
 
117
 
 
118
 
double
119
 
Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
120
 
{
121
 
  my_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
122
 
                    &val->real);
123
 
  return val->real;
124
 
}
125
 
 
126
 
 
127
 
String *
128
 
Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
129
 
                                    uint8_t decimals) const
130
 
{
131
 
  my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
132
 
                   decimals, false, &val->dec_buf[2]);
133
 
  my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to);
134
 
  return to;
135
 
}
136
 
 
137
 
/* Hybrid_type_traits_integer */
138
 
static const Hybrid_type_traits_integer integer_traits_instance;
139
 
 
140
 
const Hybrid_type_traits_integer *Hybrid_type_traits_integer::instance()
141
 
{
142
 
  return &integer_traits_instance;
143
 
}
144
 
 
145
 
void
146
 
Hybrid_type_traits_integer::fix_length_and_dec(Item *item,
147
 
                                               Item *arg __attribute__((unused))) const
148
 
{
149
 
  item->decimals= 0;
150
 
  item->max_length= MY_INT64_NUM_DECIMAL_DIGITS;
151
 
  item->unsigned_flag= 0;
152
 
}
153
 
 
154
62
/*****************************************************************************
155
63
** Item functions
156
64
*****************************************************************************/
157
65
 
158
 
/**
159
 
  Init all special items.
160
 
*/
161
 
 
162
 
void item_init(void)
163
 
{
 
66
bool Item::is_expensive_processor(unsigned char *)
 
67
{
 
68
  return false;
 
69
}
 
70
 
 
71
void Item::fix_after_pullout(Select_Lex *, Item **)
 
72
{}
 
73
 
 
74
 
 
75
Field *Item::tmp_table_field(Table *)
 
76
{
 
77
  return NULL;
 
78
}
 
79
 
 
80
 
 
81
const char *Item::full_name(void) const
 
82
{
 
83
  return name ? name : "???";
 
84
}
 
85
 
 
86
 
 
87
int64_t Item::val_int_endpoint(bool, bool *)
 
88
{
 
89
  assert(0);
 
90
  return 0;
164
91
}
165
92
 
166
93
 
180
107
    my_decimal *val= val_decimal(&decimal_value);
181
108
    if (val)
182
109
      return !my_decimal_is_zero(val);
183
 
    return 0;
 
110
    return false;
184
111
  }
185
112
  case REAL_RESULT:
186
113
  case STRING_RESULT:
188
115
  case ROW_RESULT:
189
116
  default:
190
117
    assert(0);
191
 
    return 0;                                   // Wrong (but safe)
 
118
    return false;                                   // Wrong (but safe)
192
119
  }
193
120
}
194
121
 
197
124
{
198
125
  double nr= val_real();
199
126
  if (null_value)
200
 
    return 0;                                   /* purecov: inspected */
 
127
    return NULL;                                        /* purecov: inspected */
 
128
 
201
129
  str->set_real(nr,decimals, &my_charset_bin);
202
130
  return str;
203
131
}
207
135
{
208
136
  int64_t nr= val_int();
209
137
  if (null_value)
210
 
    return 0;
 
138
    return NULL;
 
139
 
211
140
  str->set_int(nr, unsigned_flag, &my_charset_bin);
212
141
  return str;
213
142
}
217
146
{
218
147
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
219
148
  if (null_value)
220
 
    return 0;
 
149
    return NULL;
 
150
 
221
151
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
222
152
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
223
153
  return str;
228
158
{
229
159
  double nr= val_real();
230
160
  if (null_value)
231
 
    return 0;
 
161
    return NULL;
 
162
 
232
163
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
233
164
  return (decimal_value);
234
165
}
238
169
{
239
170
  int64_t nr= val_int();
240
171
  if (null_value)
241
 
    return 0;
 
172
    return NULL;
 
173
 
242
174
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
243
175
  return decimal_value;
244
176
}
249
181
  String *res;
250
182
  char *end_ptr;
251
183
  if (!(res= val_str(&str_value)))
252
 
    return 0;                                   // NULL or EOM
 
184
    return NULL;                                   // NULL or EOM
253
185
 
254
186
  end_ptr= (char*) res->ptr()+ res->length();
255
187
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
256
188
                     res->ptr(), res->length(), res->charset(),
257
189
                     decimal_value) & E_DEC_BAD_NUM)
258
190
  {
259
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
191
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
260
192
                        ER_TRUNCATED_WRONG_VALUE,
261
193
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
262
194
                        str_value.c_ptr());
273
205
  {
274
206
    my_decimal_set_zero(decimal_value);
275
207
    null_value= 1;                               // set NULL, stop processing
276
 
    return 0;
 
208
    return NULL;
277
209
  }
278
210
  return date2my_decimal(&ltime, decimal_value);
279
211
}
286
218
  if (get_time(&ltime))
287
219
  {
288
220
    my_decimal_set_zero(decimal_value);
289
 
    return 0;
 
221
    return NULL;
290
222
  }
291
223
  return date2my_decimal(&ltime, decimal_value);
292
224
}
367
299
 
368
300
 
369
301
Item::Item():
370
 
  is_expensive_cache(-1), rsize(0), name(0), orig_name(0), name_length(0),
371
 
  fixed(0), is_autogenerated_name(true),
 
302
  is_expensive_cache(-1), name(0), orig_name(0), max_length(0), name_length(0),
 
303
  unsigned_flag(false), fixed(0), is_autogenerated_name(true),
372
304
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
373
305
{
374
306
  marker= 0;
376
308
  null_value= false;
377
309
  with_sum_func= false;
378
310
  unsigned_flag= false;
379
 
  decimals= 0; 
380
 
  max_length= 0;
 
311
  decimals= 0;
381
312
  with_subselect= 0;
382
313
  cmp_context= (Item_result)-1;
383
314
 
384
315
  /* Put item in free list so that we can free all items at end */
385
 
  THD *thd= current_thd;
386
 
  next= thd->free_list;
387
 
  thd->free_list= this;
 
316
  Session *session= current_session;
 
317
  next= session->free_list;
 
318
  session->free_list= this;
388
319
  /*
389
320
    Item constructor can be called during execution other then SQL_COM
390
 
    command => we should check thd->lex->current_select on zero (thd->lex
 
321
    command => we should check session->lex->current_select on zero (session->lex
391
322
    can be uninitialised)
392
323
  */
393
 
  if (thd->lex->current_select)
 
324
  if (session->lex->current_select)
394
325
  {
395
 
    enum_parsing_place place= 
396
 
      thd->lex->current_select->parsing_place;
 
326
    enum_parsing_place place=
 
327
      session->lex->current_select->parsing_place;
397
328
    if (place == SELECT_LIST ||
398
329
        place == IN_HAVING)
399
 
      thd->lex->current_select->select_n_having_items++;
 
330
      session->lex->current_select->select_n_having_items++;
400
331
  }
401
332
}
402
333
 
407
338
  Used for duplicating lists in processing queries with temporary
408
339
  tables.
409
340
*/
410
 
Item::Item(THD *thd, Item *item):
 
341
Item::Item(Session *session, Item *item):
411
342
  is_expensive_cache(-1),
412
 
  rsize(0),
413
343
  str_value(item->str_value),
414
344
  name(item->name),
415
345
  orig_name(item->orig_name),
424
354
  collation(item->collation),
425
355
  cmp_context(item->cmp_context)
426
356
{
427
 
  next= thd->free_list;                         // Put in free list
428
 
  thd->free_list= this;
 
357
  next= session->free_list;                             // Put in free list
 
358
  session->free_list= this;
429
359
}
430
360
 
431
361
 
440
370
}
441
371
 
442
372
 
 
373
int Item::decimal_int_part() const
 
374
{
 
375
  return my_decimal_int_part(decimal_precision(), decimals);
 
376
}
 
377
 
 
378
 
 
379
void Item::print(String *str, enum_query_type)
 
380
{
 
381
  str->append(full_name());
 
382
}
 
383
 
 
384
 
443
385
void Item::print_item_w_name(String *str, enum_query_type query_type)
444
386
{
445
387
  print(str, query_type);
446
388
 
447
389
  if (name)
448
390
  {
449
 
    THD *thd= current_thd;
450
391
    str->append(STRING_WITH_LEN(" AS "));
451
 
    append_identifier(thd, str, name, (uint) strlen(name));
 
392
    str->append_identifier(name, (uint) strlen(name));
452
393
  }
453
394
}
454
395
 
455
396
 
 
397
void Item::split_sum_func(Session *, Item **, List<Item> &)
 
398
{}
 
399
 
 
400
 
456
401
void Item::cleanup()
457
402
{
458
403
  fixed=0;
469
414
  @param arg   a dummy parameter, is not used here
470
415
*/
471
416
 
472
 
bool Item::cleanup_processor(unsigned char *arg __attribute__((unused)))
 
417
bool Item::cleanup_processor(unsigned char *)
473
418
{
474
419
  if (fixed)
475
420
    cleanup();
500
445
 
501
446
  This function is designed to ease transformation of Item trees.
502
447
  Re-execution note: every such transformation is registered for
503
 
  rollback by THD::change_item_tree() and is rolled back at the end
504
 
  of execution by THD::rollback_item_tree_changes().
 
448
  rollback by Session::change_item_tree() and is rolled back at the end
 
449
  of execution by Session::rollback_item_tree_changes().
505
450
 
506
451
  Therefore:
507
452
  - this function can not be used at prepared statement prepare
508
453
  (in particular, in fix_fields!), as only permanent
509
454
  transformation of Item trees are allowed at prepare.
510
455
  - the transformer function shall allocate new Items in execution
511
 
  memory root (thd->mem_root) and not anywhere else: allocated
 
456
  memory root (session->mem_root) and not anywhere else: allocated
512
457
  items will be gone in the end of execution.
513
458
 
514
459
  If you don't need to transform an item tree, but only traverse
519
464
  @param arg            opaque argument passed to the functor
520
465
 
521
466
  @return
522
 
    Returns pointer to the new subtree root.  THD::change_item_tree()
 
467
    Returns pointer to the new subtree root.  Session::change_item_tree()
523
468
    should be called for it if transformation took place, i.e. if a
524
469
    pointer to newly allocated item is returned.
525
470
*/
530
475
}
531
476
 
532
477
 
533
 
Item_ident::Item_ident(Name_resolution_context *context_arg,
534
 
                       const char *db_name_arg,const char *table_name_arg,
535
 
                       const char *field_name_arg)
536
 
  :orig_db_name(db_name_arg), orig_table_name(table_name_arg),
537
 
   orig_field_name(field_name_arg), context(context_arg),
538
 
   db_name(db_name_arg), table_name(table_name_arg),
539
 
   field_name(field_name_arg),
540
 
   alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
541
 
   cached_table(0), depended_from(0)
542
 
{
543
 
  name = (char*) field_name_arg;
544
 
}
545
 
 
546
 
 
547
 
/**
548
 
  Constructor used by Item_field & Item_*_ref (see Item comment)
549
 
*/
550
 
 
551
 
Item_ident::Item_ident(THD *thd, Item_ident *item)
552
 
  :Item(thd, item),
553
 
   orig_db_name(item->orig_db_name),
554
 
   orig_table_name(item->orig_table_name), 
555
 
   orig_field_name(item->orig_field_name),
556
 
   context(item->context),
557
 
   db_name(item->db_name),
558
 
   table_name(item->table_name),
559
 
   field_name(item->field_name),
560
 
   alias_name_used(item->alias_name_used),
561
 
   cached_field_index(item->cached_field_index),
562
 
   cached_table(item->cached_table),
563
 
   depended_from(item->depended_from)
564
 
{}
565
 
 
566
 
void Item_ident::cleanup()
567
 
{
568
 
#ifdef CANT_BE_USED_AS_MEMORY_IS_FREED
569
 
                       db_name ? db_name : "(null)",
570
 
                       orig_db_name ? orig_db_name : "(null)",
571
 
                       table_name ? table_name : "(null)",
572
 
                       orig_table_name ? orig_table_name : "(null)",
573
 
                       field_name ? field_name : "(null)",
574
 
                       orig_field_name ? orig_field_name : "(null)"));
575
 
#endif
576
 
  Item::cleanup();
577
 
  db_name= orig_db_name; 
578
 
  table_name= orig_table_name;
579
 
  field_name= orig_field_name;
580
 
  depended_from= 0;
581
 
  return;
582
 
}
583
 
 
584
 
bool Item_ident::remove_dependence_processor(unsigned char * arg)
585
 
{
586
 
  if (depended_from == (st_select_lex *) arg)
587
 
    depended_from= 0;
588
 
  return(0);
589
 
}
590
 
 
591
 
 
592
 
/**
593
 
  Store the pointer to this item field into a list if not already there.
594
 
 
595
 
  The method is used by Item::walk to collect all unique Item_field objects
596
 
  from a tree of Items into a set of items represented as a list.
597
 
 
598
 
  Item_cond::walk() and Item_func::walk() stop the evaluation of the
599
 
  processor function for its arguments once the processor returns
600
 
  true.Therefore in order to force this method being called for all item
601
 
  arguments in a condition the method must return false.
602
 
 
603
 
  @param arg  pointer to a List<Item_field>
604
 
 
605
 
  @return
606
 
    false to force the evaluation of collect_item_field_processor
607
 
    for the subsequent items.
608
 
*/
609
 
 
610
 
bool Item_field::collect_item_field_processor(unsigned char *arg)
611
 
{
612
 
  List<Item_field> *item_list= (List<Item_field>*) arg;
613
 
  List_iterator<Item_field> item_list_it(*item_list);
614
 
  Item_field *curr_item;
615
 
  while ((curr_item= item_list_it++))
616
 
  {
617
 
    if (curr_item->eq(this, 1))
618
 
      return(false); /* Already in the set. */
619
 
  }
620
 
  item_list->push_back(this);
621
 
  return(false);
622
 
}
623
 
 
624
 
 
625
 
/**
626
 
  Check if an Item_field references some field from a list of fields.
627
 
 
628
 
  Check whether the Item_field represented by 'this' references any
629
 
  of the fields in the keyparts passed via 'arg'. Used with the
630
 
  method Item::walk() to test whether any keypart in a sequence of
631
 
  keyparts is referenced in an expression.
632
 
 
633
 
  @param arg   Field being compared, arg must be of type Field
634
 
 
635
 
  @retval
636
 
    true  if 'this' references the field 'arg'
637
 
  @retval
638
 
    false otherwise
639
 
*/
640
 
 
641
 
bool Item_field::find_item_in_field_list_processor(unsigned char *arg)
642
 
{
643
 
  KEY_PART_INFO *first_non_group_part= *((KEY_PART_INFO **) arg);
644
 
  KEY_PART_INFO *last_part= *(((KEY_PART_INFO **) arg) + 1);
645
 
  KEY_PART_INFO *cur_part;
646
 
 
647
 
  for (cur_part= first_non_group_part; cur_part != last_part; cur_part++)
648
 
  {
649
 
    if (field->eq(cur_part->field))
650
 
      return true;
651
 
  }
652
 
  return false;
653
 
}
654
 
 
655
 
 
656
 
/*
657
 
  Mark field in read_map
658
 
 
659
 
  NOTES
660
 
    This is used by filesort to register used fields in a a temporary
661
 
    column read set or to register used fields in a view
662
 
*/
663
 
 
664
 
bool Item_field::register_field_in_read_map(unsigned char *arg)
665
 
{
666
 
  Table *table= (Table *) arg;
667
 
  if (field->table == table || !table)
668
 
    bitmap_set_bit(field->table->read_set, field->field_index);
669
 
  return 0;
670
 
}
671
 
 
672
 
 
673
478
bool Item::check_cols(uint32_t c)
674
479
{
675
480
  if (c != 1)
677
482
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
678
483
    return 1;
679
484
  }
680
 
  return 0;
 
485
  return false;
681
486
}
682
487
 
683
488
 
705
510
    if (orig_len != length && !is_autogenerated_name)
706
511
    {
707
512
      if (length == 0)
708
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
513
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
709
514
                            ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
710
515
                            str + length - orig_len);
711
516
      else
712
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
517
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
713
518
                            ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
714
519
                            str + length - orig_len);
715
520
    }
718
523
  {
719
524
    size_t res_length;
720
525
    name= sql_strmake_with_convert(str, name_length= length, cs,
721
 
                                   MAX_ALIAS_NAME, system_charset_info,
 
526
                                   length, system_charset_info,
722
527
                                   &res_length);
723
528
  }
724
529
  else
725
 
    name= sql_strmake(str, (name_length= cmin(length,(unsigned int)MAX_ALIAS_NAME)));
 
530
      name= sql_strmake(str, length);
726
531
}
727
532
 
728
533
 
733
538
  - When trying to find an order_st BY/GROUP BY item in the SELECT part
734
539
*/
735
540
 
736
 
bool Item::eq(const Item *item, bool binary_cmp __attribute__((unused))) const
 
541
bool Item::eq(const Item *item, bool) const
737
542
{
738
543
  /*
739
544
    Note, that this is never true if item is a Item_param:
753
558
 
754
559
 
755
560
/**
756
 
  @details
757
 
  Created mostly for mysql_prepare_table(). Important
758
 
  when a string ENUM/SET column is described with a numeric default value:
759
 
 
760
 
  CREATE TABLE t1(a SET('a') DEFAULT 1);
761
 
 
762
 
  We cannot use generic Item::safe_charset_converter(), because
763
 
  the latter returns a non-fixed Item, so val_str() crashes afterwards.
764
 
  Override Item_num method, to return a fixed item.
765
 
*/
766
 
Item *Item_num::safe_charset_converter(const CHARSET_INFO * const tocs __attribute__((unused)))
767
 
{
768
 
  Item_string *conv;
769
 
  char buf[64];
770
 
  String *s, tmp(buf, sizeof(buf), &my_charset_bin);
771
 
  s= val_str(&tmp);
772
 
  if ((conv= new Item_string(s->ptr(), s->length(), s->charset())))
773
 
  {
774
 
    conv->str_value.copy();
775
 
    conv->str_value.mark_as_const();
776
 
  }
777
 
  return conv;
778
 
}
779
 
 
780
 
 
781
 
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const tocs __attribute__((unused)))
782
 
{
783
 
  Item_string *conv;
784
 
  char buf[64];
785
 
  String *s, tmp(buf, sizeof(buf), &my_charset_bin);
786
 
  s= val_str(&tmp);
787
 
  if ((conv= new Item_static_string_func(func_name, s->ptr(), s->length(),
788
 
                                         s->charset())))
789
 
  {
790
 
    conv->str_value.copy();
791
 
    conv->str_value.mark_as_const();
792
 
  }
793
 
  return conv;
794
 
}
795
 
 
796
 
 
797
 
Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
798
 
{
799
 
  Item_string *conv;
800
 
  uint32_t conv_errors;
801
 
  char *ptr;
802
 
  String tmp, cstr, *ostr= val_str(&tmp);
803
 
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
804
 
  if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(),
805
 
                                             cstr.charset(),
806
 
                                             collation.derivation)))
807
 
  {
808
 
    /*
809
 
      Safe conversion is not possible (or EOM).
810
 
      We could not convert a string into the requested character set
811
 
      without data loss. The target charset does not cover all the
812
 
      characters from the string. Operation cannot be done correctly.
813
 
    */
814
 
    return NULL;
815
 
  }
816
 
  if (!(ptr= current_thd->strmake(cstr.ptr(), cstr.length())))
817
 
    return NULL;
818
 
  conv->str_value.set(ptr, cstr.length(), cstr.charset());
819
 
  /* Ensure that no one is going to change the result string */
820
 
  conv->str_value.mark_as_const();
821
 
  return conv;
822
 
}
823
 
 
824
 
 
825
 
Item *Item_param::safe_charset_converter(const CHARSET_INFO * const tocs)
826
 
{
827
 
  if (const_item())
828
 
  {
829
 
    uint32_t cnv_errors;
830
 
    String *ostr= val_str(&cnvstr);
831
 
    cnvitem->str_value.copy(ostr->ptr(), ostr->length(),
832
 
                            ostr->charset(), tocs, &cnv_errors);
833
 
    if (cnv_errors)
834
 
       return NULL;
835
 
    cnvitem->str_value.mark_as_const();
836
 
    cnvitem->max_length= cnvitem->str_value.numchars() * tocs->mbmaxlen;
837
 
    return cnvitem;
838
 
  }
839
 
  return NULL;
840
 
}
841
 
 
842
 
 
843
 
Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
844
 
{
845
 
  Item_string *conv;
846
 
  uint32_t conv_errors;
847
 
  String tmp, cstr, *ostr= val_str(&tmp);
848
 
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
849
 
  if (conv_errors ||
850
 
      !(conv= new Item_static_string_func(func_name,
851
 
                                          cstr.ptr(), cstr.length(),
852
 
                                          cstr.charset(),
853
 
                                          collation.derivation)))
854
 
  {
855
 
    /*
856
 
      Safe conversion is not possible (or EOM).
857
 
      We could not convert a string into the requested character set
858
 
      without data loss. The target charset does not cover all the
859
 
      characters from the string. Operation cannot be done correctly.
860
 
    */
861
 
    return NULL;
862
 
  }
863
 
  conv->str_value.copy();
864
 
  /* Ensure that no one is going to change the result string */
865
 
  conv->str_value.mark_as_const();
866
 
  return conv;
867
 
}
868
 
 
869
 
 
870
 
bool Item_string::eq(const Item *item, bool binary_cmp) const
871
 
{
872
 
  if (type() == item->type() && item->basic_const_item())
873
 
  {
874
 
    if (binary_cmp)
875
 
      return !stringcmp(&str_value, &item->str_value);
876
 
    return (collation.collation == item->collation.collation &&
877
 
            !sortcmp(&str_value, &item->str_value, collation.collation));
878
 
  }
879
 
  return 0;
880
 
}
881
 
 
882
 
 
883
 
/**
884
561
  Get the value of the function as a DRIZZLE_TIME structure.
885
562
  As a extra convenience the time structure is reset on error!
886
563
*/
904
581
    {
905
582
      char buff[22], *end;
906
583
      end= int64_t10_to_str(value, buff, -10);
907
 
      make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
584
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
908
585
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
909
586
                                   NULL);
910
587
      goto err;
911
588
    }
912
589
  }
913
 
  return 0;
 
590
  return false;
914
591
 
915
592
err:
916
593
  memset(ltime, 0, sizeof(*ltime));
917
 
  return 1;
 
594
  return true;
918
595
}
919
596
 
920
597
/**
931
608
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
932
609
  {
933
610
    memset(ltime, 0, sizeof(*ltime));
934
 
    return 1;
 
611
    return true;
935
612
  }
936
 
  return 0;
937
 
}
 
613
  return false;
 
614
}
 
615
 
 
616
 
 
617
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
618
{
 
619
  return get_date(ltime,fuzzydate);
 
620
}
 
621
 
 
622
 
 
623
bool Item::is_null()
 
624
{
 
625
  return false;
 
626
}
 
627
 
 
628
 
 
629
void Item::update_null_value ()
 
630
{
 
631
  (void) val_int();
 
632
}
 
633
 
 
634
 
 
635
void Item::top_level_item(void)
 
636
{}
 
637
 
 
638
 
 
639
void Item::set_result_field(Field *)
 
640
{}
 
641
 
 
642
 
 
643
bool Item::is_result_field(void)
 
644
{
 
645
  return false;
 
646
}
 
647
 
 
648
 
 
649
bool Item::is_bool_func(void)
 
650
{
 
651
  return false;
 
652
}
 
653
 
 
654
 
 
655
void Item::save_in_result_field(bool)
 
656
{}
 
657
 
 
658
 
 
659
void Item::no_rows_in_result(void)
 
660
{}
 
661
 
 
662
 
 
663
Item *Item::copy_or_same(Session *)
 
664
{
 
665
  return this;
 
666
}
 
667
 
 
668
 
 
669
Item *Item::copy_andor_structure(Session *)
 
670
{
 
671
  return this;
 
672
}
 
673
 
 
674
 
 
675
Item *Item::real_item(void)
 
676
{
 
677
  return this;
 
678
}
 
679
 
 
680
 
 
681
const Item *Item::real_item(void) const
 
682
{
 
683
  return this;
 
684
}
 
685
 
 
686
 
 
687
Item *Item::get_tmp_table_item(Session *session)
 
688
{
 
689
  return copy_or_same(session);
 
690
}
 
691
 
938
692
 
939
693
const CHARSET_INFO *Item::default_charset()
940
694
{
941
 
  return current_thd->variables.collation_connection;
942
 
}
943
 
 
944
 
 
945
 
/*
946
 
  Save value in field, but don't give any warnings
947
 
 
948
 
  NOTES
949
 
   This is used to temporary store and retrieve a value in a column,
950
 
   for example in opt_range to adjust the key value to fit the column.
951
 
*/
 
695
  return current_session->variables.getCollation();
 
696
}
 
697
 
 
698
 
 
699
const CHARSET_INFO *Item::compare_collation()
 
700
{
 
701
  return NULL;
 
702
}
 
703
 
 
704
 
 
705
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
 
706
{
 
707
  return (this->*processor)(arg);
 
708
}
 
709
 
 
710
 
 
711
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
 
712
                    Item_transformer transformer, unsigned char *arg_t)
 
713
{
 
714
  if ((this->*analyzer) (arg_p))
 
715
    return ((this->*transformer) (arg_t));
 
716
  return NULL;
 
717
}
 
718
 
 
719
 
 
720
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
 
721
{
 
722
  (*traverser)(this, arg);
 
723
}
 
724
 
 
725
 
 
726
bool Item::remove_dependence_processor(unsigned char *)
 
727
{
 
728
  return false;
 
729
}
 
730
 
 
731
 
 
732
bool Item::remove_fixed(unsigned char *)
 
733
{
 
734
  fixed= 0;
 
735
  return false;
 
736
}
 
737
 
 
738
 
 
739
bool Item::collect_item_field_processor(unsigned char *)
 
740
{
 
741
  return false;
 
742
}
 
743
 
 
744
 
 
745
bool Item::find_item_in_field_list_processor(unsigned char *)
 
746
{
 
747
  return false;
 
748
}
 
749
 
 
750
 
 
751
bool Item::change_context_processor(unsigned char *)
 
752
{
 
753
  return false;
 
754
}
 
755
 
 
756
bool Item::reset_query_id_processor(unsigned char *)
 
757
{
 
758
  return false;
 
759
}
 
760
 
 
761
 
 
762
bool Item::register_field_in_read_map(unsigned char *)
 
763
{
 
764
  return false;
 
765
}
 
766
 
 
767
 
 
768
bool Item::register_field_in_bitmap(unsigned char *)
 
769
{
 
770
  return false;
 
771
}
 
772
 
 
773
 
 
774
bool Item::subst_argument_checker(unsigned char **arg)
 
775
{
 
776
  if (*arg)
 
777
    *arg= NULL;
 
778
  return true;
 
779
}
 
780
 
 
781
 
 
782
bool Item::check_vcol_func_processor(unsigned char *)
 
783
{
 
784
  return true;
 
785
}
 
786
 
 
787
 
 
788
Item *Item::equal_fields_propagator(unsigned char *)
 
789
{
 
790
  return this;
 
791
}
 
792
 
 
793
 
 
794
bool Item::set_no_const_sub(unsigned char *)
 
795
{
 
796
  return false;
 
797
}
 
798
 
 
799
 
 
800
Item *Item::replace_equal_field(unsigned char *)
 
801
{
 
802
  return this;
 
803
}
 
804
 
 
805
 
 
806
uint32_t Item::cols()
 
807
{
 
808
  return 1;
 
809
}
 
810
 
 
811
 
 
812
Item* Item::element_index(uint32_t)
 
813
{
 
814
  return this;
 
815
}
 
816
 
 
817
 
 
818
Item** Item::addr(uint32_t)
 
819
{
 
820
  return NULL;
 
821
}
 
822
 
 
823
 
 
824
bool Item::null_inside()
 
825
{
 
826
  return false;
 
827
}
 
828
 
 
829
 
 
830
void Item::bring_value()
 
831
{}
 
832
 
 
833
 
 
834
Item *Item::neg_transformer(Session *)
 
835
{
 
836
  return NULL;
 
837
}
 
838
 
 
839
 
 
840
Item *Item::update_value_transformer(unsigned char *)
 
841
{
 
842
  return this;
 
843
}
 
844
 
 
845
 
 
846
void Item::delete_self()
 
847
{
 
848
  cleanup();
 
849
  delete this;
 
850
}
 
851
 
 
852
bool Item::result_as_int64_t()
 
853
{
 
854
  return false;
 
855
}
 
856
 
 
857
 
 
858
bool Item::is_expensive()
 
859
{
 
860
  if (is_expensive_cache < 0)
 
861
    is_expensive_cache= walk(&Item::is_expensive_processor, 0,
 
862
                             (unsigned char*)0);
 
863
  return test(is_expensive_cache);
 
864
}
 
865
 
952
866
 
953
867
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
954
868
{
955
869
  int res;
956
870
  Table *table= field->table;
957
 
  THD *thd= table->in_use;
958
 
  enum_check_fields tmp= thd->count_cuted_fields;
959
 
  ulong sql_mode= thd->variables.sql_mode;
960
 
  thd->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
961
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
871
  Session *session= table->in_use;
 
872
  enum_check_fields tmp= session->count_cuted_fields;
 
873
  ulong sql_mode= session->variables.sql_mode;
 
874
  session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
 
875
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
962
876
  res= save_in_field(field, no_conversions);
963
 
  thd->count_cuted_fields= tmp;
964
 
  thd->variables.sql_mode= sql_mode;
 
877
  session->count_cuted_fields= tmp;
 
878
  session->variables.sql_mode= sql_mode;
965
879
  return res;
966
880
}
967
881
 
968
882
 
969
883
/*
970
 
 need a special class to adjust printing : references to aggregate functions 
 
884
 need a special class to adjust printing : references to aggregate functions
971
885
 must not be printed as refs because the aggregate functions that are added to
972
886
 the front of select list are not printed as well.
973
887
*/
991
905
/**
992
906
  Move SUM items out from item tree and replace with reference.
993
907
 
994
 
  @param thd                    Thread handler
 
908
  @param session                        Thread handler
995
909
  @param ref_pointer_array      Pointer to array of reference fields
996
910
  @param fields         All fields in select
997
911
  @param ref                    Pointer to item
999
913
                               SUM items
1000
914
 
1001
915
  @note
1002
 
    This is from split_sum_func2() for items that should be split
 
916
    This is from split_sum_func() for items that should be split
1003
917
 
1004
918
    All found SUM items are added FIRST in the fields list and
1005
919
    we replace the item with a reference.
1006
920
 
1007
 
    thd->fatal_error() may be called if we are out of memory
 
921
    session->fatal_error() may be called if we are out of memory
1008
922
*/
1009
923
 
1010
 
void Item::split_sum_func2(THD *thd, Item **ref_pointer_array,
1011
 
                           List<Item> &fields, Item **ref, 
1012
 
                           bool skip_registered)
 
924
void Item::split_sum_func(Session *session, Item **ref_pointer_array,
 
925
                          List<Item> &fields, Item **ref,
 
926
                          bool skip_registered)
1013
927
{
1014
 
  /* An item of type Item_sum  is registered <=> ref_by != 0 */ 
1015
 
  if (type() == SUM_FUNC_ITEM && skip_registered && 
 
928
  /* An item of type Item_sum  is registered <=> ref_by != 0 */
 
929
  if (type() == SUM_FUNC_ITEM && skip_registered &&
1016
930
      ((Item_sum *) this)->ref_by)
1017
 
    return;                                                 
 
931
    return;
1018
932
  if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
1019
933
      (type() == FUNC_ITEM &&
1020
934
       (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
1021
935
        ((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC)))
1022
936
  {
1023
937
    /* Will split complicated items and ignore simple ones */
1024
 
    split_sum_func(thd, ref_pointer_array, fields);
 
938
    split_sum_func(session, ref_pointer_array, fields);
1025
939
  }
1026
940
  else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) &&
1027
941
           type() != SUBSELECT_ITEM &&
1028
 
           (type() != REF_ITEM ||
1029
 
           ((Item_ref*)this)->ref_type() == Item_ref::VIEW_REF))
 
942
           type() != REF_ITEM)
1030
943
  {
1031
944
    /*
1032
945
      Replace item with a reference so that we can easily calculate
1043
956
    Item *real_itm= real_item();
1044
957
 
1045
958
    ref_pointer_array[el]= real_itm;
1046
 
    if (!(item_ref= new Item_aggregate_ref(&thd->lex->current_select->context,
 
959
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
1047
960
                                           ref_pointer_array + el, 0, name)))
1048
961
      return;                                   // fatal_error is set
1049
962
    if (type() == SUM_FUNC_ITEM)
1050
 
      item_ref->depended_from= ((Item_sum *) this)->depended_from(); 
 
963
      item_ref->depended_from= ((Item_sum *) this)->depended_from();
1051
964
    fields.push_front(real_itm);
1052
 
    thd->change_item_tree(ref, item_ref);
1053
 
  }
1054
 
}
1055
 
 
1056
 
 
1057
 
static bool
1058
 
left_is_superset(DTCollation *left, DTCollation *right)
1059
 
{
1060
 
  /* Allow convert to Unicode */
1061
 
  if (left->collation->state & MY_CS_UNICODE &&
1062
 
      (left->derivation < right->derivation ||
1063
 
       (left->derivation == right->derivation &&
1064
 
        !(right->collation->state & MY_CS_UNICODE))))
1065
 
    return true;
1066
 
  /* Allow convert from ASCII */
1067
 
  if (right->repertoire == MY_REPERTOIRE_ASCII &&
1068
 
      (left->derivation < right->derivation ||
1069
 
       (left->derivation == right->derivation &&
1070
 
        !(left->repertoire == MY_REPERTOIRE_ASCII))))
1071
 
    return true;
1072
 
  /* Disallow conversion otherwise */
1073
 
  return false;
1074
 
}
1075
 
 
1076
 
/**
1077
 
  Aggregate two collations together taking
1078
 
  into account their coercibility (aka derivation):.
1079
 
 
1080
 
  0 == DERIVATION_EXPLICIT  - an explicitly written COLLATE clause @n
1081
 
  1 == DERIVATION_NONE      - a mix of two different collations @n
1082
 
  2 == DERIVATION_IMPLICIT  - a column @n
1083
 
  3 == DERIVATION_COERCIBLE - a string constant.
1084
 
 
1085
 
  The most important rules are:
1086
 
  -# If collations are the same:
1087
 
  chose this collation, and the strongest derivation.
1088
 
  -# If collations are different:
1089
 
  - Character sets may differ, but only if conversion without
1090
 
  data loss is possible. The caller provides flags whether
1091
 
  character set conversion attempts should be done. If no
1092
 
  flags are substituted, then the character sets must be the same.
1093
 
  Currently processed flags are:
1094
 
  MY_COLL_ALLOW_SUPERSET_CONV  - allow conversion to a superset
1095
 
  MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
1096
 
  - two EXPLICIT collations produce an error, e.g. this is wrong:
1097
 
  CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
1098
 
  - the side with smaller derivation value wins,
1099
 
  i.e. a column is stronger than a string constant,
1100
 
  an explicit COLLATE clause is stronger than a column.
1101
 
  - if derivations are the same, we have DERIVATION_NONE,
1102
 
  we'll wait for an explicit COLLATE clause which possibly can
1103
 
  come from another argument later: for example, this is valid,
1104
 
  but we don't know yet when collecting the first two arguments:
1105
 
     @code
1106
 
       CONCAT(latin1_swedish_ci_column,
1107
 
              latin1_german1_ci_column,
1108
 
              expr COLLATE latin1_german2_ci)
1109
 
  @endcode
1110
 
*/
1111
 
 
1112
 
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
1113
 
{
1114
 
  if (!my_charset_same(collation, dt.collation))
1115
 
  {
1116
 
    /* 
1117
 
       We do allow to use binary strings (like BLOBS)
1118
 
       together with character strings.
1119
 
       Binaries have more precedence than a character
1120
 
       string of the same derivation.
1121
 
    */
1122
 
    if (collation == &my_charset_bin)
1123
 
    {
1124
 
      if (derivation <= dt.derivation)
1125
 
        ; // Do nothing
1126
 
      else
1127
 
      {
1128
 
        set(dt); 
1129
 
      }
1130
 
    }
1131
 
    else if (dt.collation == &my_charset_bin)
1132
 
    {
1133
 
      if (dt.derivation <= derivation)
1134
 
      {
1135
 
        set(dt);
1136
 
      }
1137
 
      else
1138
 
      {
1139
 
        // Do nothing
1140
 
      }
1141
 
    }
1142
 
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1143
 
             left_is_superset(this, &dt))
1144
 
    {
1145
 
      // Do nothing
1146
 
    }
1147
 
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1148
 
             left_is_superset(&dt, this))
1149
 
    {
1150
 
      set(dt);
1151
 
    }
1152
 
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1153
 
             derivation < dt.derivation &&
1154
 
             dt.derivation >= DERIVATION_SYSCONST)
1155
 
    {
1156
 
      // Do nothing;
1157
 
    }
1158
 
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1159
 
             dt.derivation < derivation &&
1160
 
             derivation >= DERIVATION_SYSCONST)
1161
 
    {
1162
 
      set(dt);
1163
 
    }
1164
 
    else
1165
 
    {
1166
 
      // Cannot apply conversion
1167
 
      set(0, DERIVATION_NONE, 0);
1168
 
      return 1;
1169
 
    }
1170
 
  }
1171
 
  else if (derivation < dt.derivation)
1172
 
  {
1173
 
    // Do nothing
1174
 
  }
1175
 
  else if (dt.derivation < derivation)
1176
 
  {
1177
 
    set(dt);
1178
 
  }
1179
 
  else
1180
 
  { 
1181
 
    if (collation == dt.collation)
1182
 
    {
1183
 
      // Do nothing
1184
 
    }
1185
 
    else 
1186
 
    {
1187
 
      if (derivation == DERIVATION_EXPLICIT)
1188
 
      {
1189
 
        set(0, DERIVATION_NONE, 0);
1190
 
        return 1;
1191
 
      }
1192
 
      if (collation->state & MY_CS_BINSORT)
1193
 
        return 0;
1194
 
      if (dt.collation->state & MY_CS_BINSORT)
1195
 
      {
1196
 
        set(dt);
1197
 
        return 0;
1198
 
      }
1199
 
      const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, 
1200
 
                                                            MY_CS_BINSORT,MYF(0));
1201
 
      set(bin, DERIVATION_NONE);
1202
 
    }
1203
 
  }
1204
 
  repertoire|= dt.repertoire;
1205
 
  return 0;
1206
 
}
1207
 
 
1208
 
/******************************/
1209
 
static
1210
 
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname)
1211
 
{
1212
 
  my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
1213
 
           c1.collation->name,c1.derivation_name(),
1214
 
           c2.collation->name,c2.derivation_name(),
1215
 
           fname);
1216
 
}
1217
 
 
1218
 
 
1219
 
static
1220
 
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
1221
 
                       const char *fname)
1222
 
{
1223
 
  my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
1224
 
           c1.collation->name,c1.derivation_name(),
1225
 
           c2.collation->name,c2.derivation_name(),
1226
 
           c3.collation->name,c3.derivation_name(),
1227
 
           fname);
1228
 
}
1229
 
 
1230
 
 
1231
 
static
1232
 
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
1233
 
                       int item_sep)
1234
 
{
1235
 
  if (count == 2)
1236
 
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
1237
 
  else if (count == 3)
1238
 
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
1239
 
                      args[2*item_sep]->collation, fname);
1240
 
  else
1241
 
    my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
1242
 
}
1243
 
 
1244
 
 
1245
 
bool agg_item_collations(DTCollation &c, const char *fname,
1246
 
                         Item **av, uint32_t count, uint32_t flags, int item_sep)
1247
 
{
1248
 
  uint32_t i;
1249
 
  Item **arg;
1250
 
  c.set(av[0]->collation);
1251
 
  for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1252
 
  {
1253
 
    if (c.aggregate((*arg)->collation, flags))
1254
 
    {
1255
 
      my_coll_agg_error(av, count, fname, item_sep);
1256
 
      return true;
1257
 
    }
1258
 
  }
1259
 
  if ((flags & MY_COLL_DISALLOW_NONE) &&
1260
 
      c.derivation == DERIVATION_NONE)
1261
 
  {
1262
 
    my_coll_agg_error(av, count, fname, item_sep);
1263
 
    return true;
1264
 
  }
1265
 
  return false;
1266
 
}
1267
 
 
1268
 
 
1269
 
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1270
 
                                        Item **av, uint32_t count, uint32_t flags)
1271
 
{
1272
 
  return (agg_item_collations(c, fname, av, count,
1273
 
                              flags | MY_COLL_DISALLOW_NONE, 1));
1274
 
}
1275
 
 
1276
 
 
1277
 
/**
1278
 
  Collect arguments' character sets together.
1279
 
 
1280
 
  We allow to apply automatic character set conversion in some cases.
1281
 
  The conditions when conversion is possible are:
1282
 
  - arguments A and B have different charsets
1283
 
  - A wins according to coercibility rules
1284
 
    (i.e. a column is stronger than a string constant,
1285
 
     an explicit COLLATE clause is stronger than a column)
1286
 
  - character set of A is either superset for character set of B,
1287
 
    or B is a string constant which can be converted into the
1288
 
    character set of A without data loss.
1289
 
    
1290
 
  If all of the above is true, then it's possible to convert
1291
 
  B into the character set of A, and then compare according
1292
 
  to the collation of A.
1293
 
  
1294
 
  For functions with more than two arguments:
1295
 
  @code
1296
 
    collect(A,B,C) ::= collect(collect(A,B),C)
1297
 
  @endcode
1298
 
  Since this function calls THD::change_item_tree() on the passed Item **
1299
 
  pointers, it is necessary to pass the original Item **'s, not copies.
1300
 
  Otherwise their values will not be properly restored (see BUG#20769).
1301
 
  If the items are not consecutive (eg. args[2] and args[5]), use the
1302
 
  item_sep argument, ie.
1303
 
  @code
1304
 
    agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
1305
 
  @endcode
1306
 
*/
1307
 
 
1308
 
bool agg_item_charsets(DTCollation &coll, const char *fname,
1309
 
                       Item **args, uint32_t nargs, uint32_t flags, int item_sep)
1310
 
{
1311
 
  Item **arg, *safe_args[2];
1312
 
 
1313
 
  memset(safe_args, 0, sizeof(safe_args));
1314
 
 
1315
 
  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
1316
 
    return true;
1317
 
 
1318
 
  /*
1319
 
    For better error reporting: save the first and the second argument.
1320
 
    We need this only if the the number of args is 3 or 2:
1321
 
    - for a longer argument list, "Illegal mix of collations"
1322
 
      doesn't display each argument's characteristics.
1323
 
    - if nargs is 1, then this error cannot happen.
1324
 
  */
1325
 
  if (nargs >=2 && nargs <= 3)
1326
 
  {
1327
 
    safe_args[0]= args[0];
1328
 
    safe_args[1]= args[item_sep];
1329
 
  }
1330
 
 
1331
 
  THD *thd= current_thd;
1332
 
  bool res= false;
1333
 
  uint32_t i;
1334
 
 
1335
 
  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1336
 
  {
1337
 
    Item* conv;
1338
 
    uint32_t dummy_offset;
1339
 
    if (!String::needs_conversion(0, (*arg)->collation.collation,
1340
 
                                  coll.collation,
1341
 
                                  &dummy_offset))
1342
 
      continue;
1343
 
 
1344
 
    if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
1345
 
        ((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
1346
 
      conv= new Item_func_conv_charset(*arg, coll.collation, 1);
1347
 
 
1348
 
    if (!conv)
1349
 
    {
1350
 
      if (nargs >=2 && nargs <= 3)
1351
 
      {
1352
 
        /* restore the original arguments for better error message */
1353
 
        args[0]= safe_args[0];
1354
 
        args[item_sep]= safe_args[1];
1355
 
      }
1356
 
      my_coll_agg_error(args, nargs, fname, item_sep);
1357
 
      res= true;
1358
 
      break; // we cannot return here, we need to restore "arena".
1359
 
    }
1360
 
    if ((*arg)->type() == Item::FIELD_ITEM)
1361
 
      ((Item_field *)(*arg))->no_const_subst= 1;
1362
 
    /*
1363
 
      If in statement prepare, then we create a converter for two
1364
 
      constant items, do it once and then reuse it.
1365
 
      If we're in execution of a prepared statement, arena is NULL,
1366
 
      and the conv was created in runtime memory. This can be
1367
 
      the case only if the argument is a parameter marker ('?'),
1368
 
      because for all true constants the charset converter has already
1369
 
      been created in prepare. In this case register the change for
1370
 
      rollback.
1371
 
    */
1372
 
    thd->change_item_tree(arg, conv);
1373
 
    /*
1374
 
      We do not check conv->fixed, because Item_func_conv_charset which can
1375
 
      be return by safe_charset_converter can't be fixed at creation
1376
 
    */
1377
 
    conv->fix_fields(thd, arg);
1378
 
  }
1379
 
 
1380
 
  return res;
1381
 
}
1382
 
 
1383
 
 
1384
 
void Item_ident_for_show::make_field(Send_field *tmp_field)
1385
 
{
1386
 
  tmp_field->table_name= tmp_field->org_table_name= table_name;
1387
 
  tmp_field->db_name= db_name;
1388
 
  tmp_field->col_name= tmp_field->org_col_name= field->field_name;
1389
 
  tmp_field->charsetnr= field->charset()->number;
1390
 
  tmp_field->length=field->field_length;
1391
 
  tmp_field->type=field->type();
1392
 
  tmp_field->flags= field->table->maybe_null ? 
1393
 
    (field->flags & ~NOT_NULL_FLAG) : field->flags;
1394
 
  tmp_field->decimals= field->decimals();
1395
 
}
1396
 
 
1397
 
/**********************************************/
1398
 
 
1399
 
Item_field::Item_field(Field *f)
1400
 
  :Item_ident(0, NULL, *f->table_name, f->field_name),
1401
 
   item_equal(0), no_const_subst(0),
1402
 
   have_privileges(0), any_privileges(0)
1403
 
{
1404
 
  set_field(f);
1405
 
  /*
1406
 
    field_name and table_name should not point to garbage
1407
 
    if this item is to be reused
1408
 
  */
1409
 
  orig_table_name= orig_field_name= "";
1410
 
}
1411
 
 
1412
 
 
1413
 
/**
1414
 
  Constructor used inside setup_wild().
1415
 
 
1416
 
  Ensures that field, table, and database names will live as long as
1417
 
  Item_field (this is important in prepared statements).
1418
 
*/
1419
 
 
1420
 
Item_field::Item_field(THD *thd __attribute__((unused)),
1421
 
                       Name_resolution_context *context_arg,
1422
 
                       Field *f)
1423
 
  :Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1424
 
   item_equal(0), no_const_subst(0),
1425
 
   have_privileges(0), any_privileges(0)
1426
 
{
1427
 
  set_field(f);
1428
 
}
1429
 
 
1430
 
 
1431
 
Item_field::Item_field(Name_resolution_context *context_arg,
1432
 
                       const char *db_arg,const char *table_name_arg,
1433
 
                       const char *field_name_arg)
1434
 
  :Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
1435
 
   field(0), result_field(0), item_equal(0), no_const_subst(0),
1436
 
   have_privileges(0), any_privileges(0)
1437
 
{
1438
 
  SELECT_LEX *select= current_thd->lex->current_select;
1439
 
  collation.set(DERIVATION_IMPLICIT);
1440
 
  if (select && select->parsing_place != IN_HAVING)
1441
 
      select->select_n_where_fields++;
1442
 
}
1443
 
 
1444
 
/**
1445
 
  Constructor need to process subselect with temporary tables (see Item)
1446
 
*/
1447
 
 
1448
 
Item_field::Item_field(THD *thd, Item_field *item)
1449
 
  :Item_ident(thd, item),
1450
 
   field(item->field),
1451
 
   result_field(item->result_field),
1452
 
   item_equal(item->item_equal),
1453
 
   no_const_subst(item->no_const_subst),
1454
 
   have_privileges(item->have_privileges),
1455
 
   any_privileges(item->any_privileges)
1456
 
{
1457
 
  collation.set(DERIVATION_IMPLICIT);
1458
 
}
1459
 
 
1460
 
void Item_field::set_field(Field *field_par)
1461
 
{
1462
 
  field=result_field=field_par;                 // for easy coding with fields
1463
 
  maybe_null=field->maybe_null();
1464
 
  decimals= field->decimals();
1465
 
  max_length= field_par->max_display_length();
1466
 
  table_name= *field_par->table_name;
1467
 
  field_name= field_par->field_name;
1468
 
  db_name= field_par->table->s->db.str;
1469
 
  alias_name_used= field_par->table->alias_name_used;
1470
 
  unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
1471
 
  collation.set(field_par->charset(), field_par->derivation());
1472
 
  fixed= 1;
1473
 
  if (field->table->s->tmp_table == SYSTEM_TMP_TABLE)
1474
 
    any_privileges= 0;
1475
 
}
1476
 
 
1477
 
 
1478
 
/**
1479
 
  Reset this item to point to a field from the new temporary table.
1480
 
  This is used when we create a new temporary table for each execution
1481
 
  of prepared statement.
1482
 
*/
1483
 
 
1484
 
void Item_field::reset_field(Field *f)
1485
 
{
1486
 
  set_field(f);
1487
 
  /* 'name' is pointing at field->field_name of old field */
1488
 
  name= (char*) f->field_name;
1489
 
}
1490
 
 
1491
 
const char *Item_ident::full_name() const
1492
 
{
1493
 
  char *tmp;
1494
 
  if (!table_name || !field_name)
1495
 
    return field_name ? field_name : name ? name : "tmp_field";
1496
 
  if (db_name && db_name[0])
1497
 
  {
1498
 
    tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1499
 
                          (uint) strlen(field_name)+3);
1500
 
    strxmov(tmp,db_name,".",table_name,".",field_name,NULL);
1501
 
  }
1502
 
  else
1503
 
  {
1504
 
    if (table_name[0])
1505
 
    {
1506
 
      tmp= (char*) sql_alloc((uint) strlen(table_name) +
1507
 
                             (uint) strlen(field_name) + 2);
1508
 
      strxmov(tmp, table_name, ".", field_name, NULL);
1509
 
    }
1510
 
    else
1511
 
      tmp= (char*) field_name;
1512
 
  }
1513
 
  return tmp;
1514
 
}
1515
 
 
1516
 
void Item_ident::print(String *str,
1517
 
                       enum_query_type query_type __attribute__((unused)))
1518
 
{
1519
 
  THD *thd= current_thd;
1520
 
  char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1521
 
  const char *d_name= db_name, *t_name= table_name;
1522
 
  if (lower_case_table_names== 1 ||
1523
 
      (lower_case_table_names == 2 && !alias_name_used))
1524
 
  {
1525
 
    if (table_name && table_name[0])
1526
 
    {
1527
 
      my_stpcpy(t_name_buff, table_name);
1528
 
      my_casedn_str(files_charset_info, t_name_buff);
1529
 
      t_name= t_name_buff;
1530
 
    }
1531
 
    if (db_name && db_name[0])
1532
 
    {
1533
 
      my_stpcpy(d_name_buff, db_name);
1534
 
      my_casedn_str(files_charset_info, d_name_buff);
1535
 
      d_name= d_name_buff;
1536
 
    }
1537
 
  }
1538
 
 
1539
 
  if (!table_name || !field_name || !field_name[0])
1540
 
  {
1541
 
    const char *nm= (field_name && field_name[0]) ?
1542
 
                      field_name : name ? name : "tmp_field";
1543
 
    append_identifier(thd, str, nm, (uint) strlen(nm));
1544
 
    return;
1545
 
  }
1546
 
  if (db_name && db_name[0] && !alias_name_used)
1547
 
  {
1548
 
    {
1549
 
      append_identifier(thd, str, d_name, (uint)strlen(d_name));
1550
 
      str->append('.');
1551
 
    }
1552
 
    append_identifier(thd, str, t_name, (uint)strlen(t_name));
1553
 
    str->append('.');
1554
 
    append_identifier(thd, str, field_name, (uint)strlen(field_name));
1555
 
  }
1556
 
  else
1557
 
  {
1558
 
    if (table_name[0])
1559
 
    {
1560
 
      append_identifier(thd, str, t_name, (uint) strlen(t_name));
1561
 
      str->append('.');
1562
 
      append_identifier(thd, str, field_name, (uint) strlen(field_name));
1563
 
    }
1564
 
    else
1565
 
      append_identifier(thd, str, field_name, (uint) strlen(field_name));
1566
 
  }
1567
 
}
1568
 
 
1569
 
/* ARGSUSED */
1570
 
String *Item_field::val_str(String *str)
1571
 
{
1572
 
  assert(fixed == 1);
1573
 
  if ((null_value=field->is_null()))
1574
 
    return 0;
1575
 
  str->set_charset(str_value.charset());
1576
 
  return field->val_str(str,&str_value);
1577
 
}
1578
 
 
1579
 
 
1580
 
double Item_field::val_real()
1581
 
{
1582
 
  assert(fixed == 1);
1583
 
  if ((null_value=field->is_null()))
1584
 
    return 0.0;
1585
 
  return field->val_real();
1586
 
}
1587
 
 
1588
 
 
1589
 
int64_t Item_field::val_int()
1590
 
{
1591
 
  assert(fixed == 1);
1592
 
  if ((null_value=field->is_null()))
1593
 
    return 0;
1594
 
  return field->val_int();
1595
 
}
1596
 
 
1597
 
 
1598
 
my_decimal *Item_field::val_decimal(my_decimal *decimal_value)
1599
 
{
1600
 
  if ((null_value= field->is_null()))
1601
 
    return 0;
1602
 
  return field->val_decimal(decimal_value);
1603
 
}
1604
 
 
1605
 
 
1606
 
String *Item_field::str_result(String *str)
1607
 
{
1608
 
  if ((null_value=result_field->is_null()))
1609
 
    return 0;
1610
 
  str->set_charset(str_value.charset());
1611
 
  return result_field->val_str(str,&str_value);
1612
 
}
1613
 
 
1614
 
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1615
 
{
1616
 
  if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1617
 
  {
1618
 
    memset(ltime, 0, sizeof(*ltime));
1619
 
    return 1;
1620
 
  }
1621
 
  return 0;
1622
 
}
1623
 
 
1624
 
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1625
 
{
1626
 
  if ((null_value=result_field->is_null()) ||
1627
 
      result_field->get_date(ltime,fuzzydate))
1628
 
  {
1629
 
    memset(ltime, 0, sizeof(*ltime));
1630
 
    return 1;
1631
 
  }
1632
 
  return 0;
1633
 
}
1634
 
 
1635
 
bool Item_field::get_time(DRIZZLE_TIME *ltime)
1636
 
{
1637
 
  if ((null_value=field->is_null()) || field->get_time(ltime))
1638
 
  {
1639
 
    memset(ltime, 0, sizeof(*ltime));
1640
 
    return 1;
1641
 
  }
1642
 
  return 0;
1643
 
}
1644
 
 
1645
 
double Item_field::val_result()
1646
 
{
1647
 
  if ((null_value=result_field->is_null()))
1648
 
    return 0.0;
1649
 
  return result_field->val_real();
1650
 
}
1651
 
 
1652
 
int64_t Item_field::val_int_result()
1653
 
{
1654
 
  if ((null_value=result_field->is_null()))
1655
 
    return 0;
1656
 
  return result_field->val_int();
1657
 
}
1658
 
 
1659
 
 
1660
 
my_decimal *Item_field::val_decimal_result(my_decimal *decimal_value)
1661
 
{
1662
 
  if ((null_value= result_field->is_null()))
1663
 
    return 0;
1664
 
  return result_field->val_decimal(decimal_value);
1665
 
}
1666
 
 
1667
 
 
1668
 
bool Item_field::val_bool_result()
1669
 
{
1670
 
  if ((null_value= result_field->is_null()))
1671
 
    return false;
1672
 
  switch (result_field->result_type()) {
1673
 
  case INT_RESULT:
1674
 
    return result_field->val_int() != 0;
1675
 
  case DECIMAL_RESULT:
1676
 
  {
1677
 
    my_decimal decimal_value;
1678
 
    my_decimal *val= result_field->val_decimal(&decimal_value);
1679
 
    if (val)
1680
 
      return !my_decimal_is_zero(val);
1681
 
    return 0;
1682
 
  }
1683
 
  case REAL_RESULT:
1684
 
  case STRING_RESULT:
1685
 
    return result_field->val_real() != 0.0;
1686
 
  case ROW_RESULT:
1687
 
  default:
1688
 
    assert(0);
1689
 
    return 0;                                   // Shut up compiler
1690
 
  }
1691
 
}
1692
 
 
1693
 
 
1694
 
bool Item_field::eq(const Item *item,
1695
 
                    bool binary_cmp __attribute__((unused))) const
1696
 
{
1697
 
  Item *real_item= ((Item *) item)->real_item();
1698
 
  if (real_item->type() != FIELD_ITEM)
1699
 
    return 0;
1700
 
 
1701
 
  Item_field *item_field= (Item_field*) real_item;
1702
 
  if (item_field->field && field)
1703
 
    return item_field->field == field;
1704
 
  /*
1705
 
    We may come here when we are trying to find a function in a GROUP BY
1706
 
    clause from the select list.
1707
 
    In this case the '100 % correct' way to do this would be to first
1708
 
    run fix_fields() on the GROUP BY item and then retry this function, but
1709
 
    I think it's better to relax the checking a bit as we will in
1710
 
    most cases do the correct thing by just checking the field name.
1711
 
    (In cases where we would choose wrong we would have to generate a
1712
 
    ER_NON_UNIQ_ERROR).
1713
 
  */
1714
 
  return (!my_strcasecmp(system_charset_info, item_field->name,
1715
 
                         field_name) &&
1716
 
          (!item_field->table_name || !table_name ||
1717
 
           (!my_strcasecmp(table_alias_charset, item_field->table_name,
1718
 
                           table_name) &&
1719
 
            (!item_field->db_name || !db_name ||
1720
 
             (item_field->db_name && !strcmp(item_field->db_name,
1721
 
                                             db_name))))));
1722
 
}
1723
 
 
1724
 
 
1725
 
table_map Item_field::used_tables() const
1726
 
{
1727
 
  if (field->table->const_table)
1728
 
    return 0;                                   // const item
1729
 
  return (depended_from ? OUTER_REF_TABLE_BIT : field->table->map);
1730
 
}
1731
 
 
1732
 
 
1733
 
void Item_field::fix_after_pullout(st_select_lex *new_parent,
1734
 
                                   Item **ref __attribute__((unused)))
1735
 
{
1736
 
  if (new_parent == depended_from)
1737
 
    depended_from= NULL;
1738
 
  Name_resolution_context *ctx= new Name_resolution_context();
1739
 
  ctx->outer_context= NULL; // We don't build a complete name resolver
1740
 
  ctx->select_lex= new_parent;
1741
 
  ctx->first_name_resolution_table= context->first_name_resolution_table;
1742
 
  ctx->last_name_resolution_table=  context->last_name_resolution_table;
1743
 
  this->context=ctx;
1744
 
}
1745
 
 
1746
 
 
1747
 
Item *Item_field::get_tmp_table_item(THD *thd)
1748
 
{
1749
 
  Item_field *new_item= new Item_field(thd, this);
1750
 
  if (new_item)
1751
 
    new_item->field= new_item->result_field;
1752
 
  return new_item;
1753
 
}
1754
 
 
1755
 
int64_t Item_field::val_int_endpoint(bool left_endp __attribute__((unused)),
1756
 
                                      bool *incl_endp __attribute__((unused)))
1757
 
{
1758
 
  int64_t res= val_int();
1759
 
  return null_value? INT64_MIN : res;
1760
 
}
1761
 
 
1762
 
/**
1763
 
  Create an item from a string we KNOW points to a valid int64_t
1764
 
  end \\0 terminated number string.
1765
 
  This is always 'signed'. Unsigned values are created with Item_uint()
1766
 
*/
1767
 
 
1768
 
Item_int::Item_int(const char *str_arg, uint32_t length)
1769
 
{
1770
 
  char *end_ptr= (char*) str_arg + length;
1771
 
  int error;
1772
 
  value= my_strtoll10(str_arg, &end_ptr, &error);
1773
 
  max_length= (uint) (end_ptr - str_arg);
1774
 
  name= (char*) str_arg;
1775
 
  fixed= 1;
1776
 
}
1777
 
 
1778
 
 
1779
 
my_decimal *Item_int::val_decimal(my_decimal *decimal_value)
1780
 
{
1781
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value);
1782
 
  return decimal_value;
1783
 
}
1784
 
 
1785
 
String *Item_int::val_str(String *str)
1786
 
{
1787
 
  // following assert is redundant, because fixed=1 assigned in constructor
1788
 
  assert(fixed == 1);
1789
 
  str->set(value, &my_charset_bin);
1790
 
  return str;
1791
 
}
1792
 
 
1793
 
void Item_int::print(String *str,
1794
 
                     enum_query_type query_type __attribute__((unused)))
1795
 
{
1796
 
  // my_charset_bin is good enough for numbers
1797
 
  str_value.set(value, &my_charset_bin);
1798
 
  str->append(str_value);
1799
 
}
1800
 
 
1801
 
 
1802
 
Item_uint::Item_uint(const char *str_arg, uint32_t length):
1803
 
  Item_int(str_arg, length)
1804
 
{
1805
 
  unsigned_flag= 1;
1806
 
}
1807
 
 
1808
 
 
1809
 
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
1810
 
  Item_int(str_arg, i, length)
1811
 
{
1812
 
  unsigned_flag= 1;
1813
 
}
1814
 
 
1815
 
 
1816
 
String *Item_uint::val_str(String *str)
1817
 
{
1818
 
  // following assert is redundant, because fixed=1 assigned in constructor
1819
 
  assert(fixed == 1);
1820
 
  str->set((uint64_t) value, &my_charset_bin);
1821
 
  return str;
1822
 
}
1823
 
 
1824
 
 
1825
 
void Item_uint::print(String *str,
1826
 
                      enum_query_type query_type __attribute__((unused)))
1827
 
{
1828
 
  // latin1 is good enough for numbers
1829
 
  str_value.set((uint64_t) value, default_charset());
1830
 
  str->append(str_value);
1831
 
}
1832
 
 
1833
 
 
1834
 
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1835
 
                           const CHARSET_INFO * const charset)
1836
 
{
1837
 
  str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1838
 
  name= (char*) str_arg;
1839
 
  decimals= (uint8_t) decimal_value.frac;
1840
 
  fixed= 1;
1841
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1842
 
                                             decimals, unsigned_flag);
1843
 
}
1844
 
 
1845
 
Item_decimal::Item_decimal(int64_t val, bool unsig)
1846
 
{
1847
 
  int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1848
 
  decimals= (uint8_t) decimal_value.frac;
1849
 
  fixed= 1;
1850
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1851
 
                                             decimals, unsigned_flag);
1852
 
}
1853
 
 
1854
 
 
1855
 
Item_decimal::Item_decimal(double val,
1856
 
                           int precision __attribute__((unused)),
1857
 
                           int scale __attribute__((unused)))
1858
 
{
1859
 
  double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1860
 
  decimals= (uint8_t) decimal_value.frac;
1861
 
  fixed= 1;
1862
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1863
 
                                             decimals, unsigned_flag);
1864
 
}
1865
 
 
1866
 
 
1867
 
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1868
 
                           uint32_t decimal_par, uint32_t length)
1869
 
{
1870
 
  my_decimal2decimal(val_arg, &decimal_value);
1871
 
  name= (char*) str;
1872
 
  decimals= (uint8_t) decimal_par;
1873
 
  max_length= length;
1874
 
  fixed= 1;
1875
 
}
1876
 
 
1877
 
 
1878
 
Item_decimal::Item_decimal(my_decimal *value_par)
1879
 
{
1880
 
  my_decimal2decimal(value_par, &decimal_value);
1881
 
  decimals= (uint8_t) decimal_value.frac;
1882
 
  fixed= 1;
1883
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1884
 
                                             decimals, unsigned_flag);
1885
 
}
1886
 
 
1887
 
 
1888
 
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
1889
 
{
1890
 
  binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1891
 
                    &decimal_value, precision, scale);
1892
 
  decimals= (uint8_t) decimal_value.frac;
1893
 
  fixed= 1;
1894
 
  max_length= my_decimal_precision_to_length(precision, decimals,
1895
 
                                             unsigned_flag);
1896
 
}
1897
 
 
1898
 
 
1899
 
int64_t Item_decimal::val_int()
1900
 
{
1901
 
  int64_t result;
1902
 
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1903
 
  return result;
1904
 
}
1905
 
 
1906
 
double Item_decimal::val_real()
1907
 
{
1908
 
  double result;
1909
 
  my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1910
 
  return result;
1911
 
}
1912
 
 
1913
 
String *Item_decimal::val_str(String *result)
1914
 
{
1915
 
  result->set_charset(&my_charset_bin);
1916
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
1917
 
  return result;
1918
 
}
1919
 
 
1920
 
void Item_decimal::print(String *str,
1921
 
                         enum_query_type query_type __attribute__((unused)))
1922
 
{
1923
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1924
 
  str->append(str_value);
1925
 
}
1926
 
 
1927
 
 
1928
 
bool Item_decimal::eq(const Item *item,
1929
 
                      bool binary_cmp __attribute__((unused))) const
1930
 
{
1931
 
  if (type() == item->type() && item->basic_const_item())
1932
 
  {
1933
 
    /*
1934
 
      We need to cast off const to call val_decimal(). This should
1935
 
      be OK for a basic constant. Additionally, we can pass 0 as
1936
 
      a true decimal constant will return its internal decimal
1937
 
      storage and ignore the argument.
1938
 
    */
1939
 
    Item *arg= (Item*) item;
1940
 
    my_decimal *value= arg->val_decimal(0);
1941
 
    return !my_decimal_cmp(&decimal_value, value);
1942
 
  }
1943
 
  return 0;
1944
 
}
1945
 
 
1946
 
 
1947
 
void Item_decimal::set_decimal_value(my_decimal *value_par)
1948
 
{
1949
 
  my_decimal2decimal(value_par, &decimal_value);
1950
 
  decimals= (uint8_t) decimal_value.frac;
1951
 
  unsigned_flag= !decimal_value.sign();
1952
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1953
 
                                             decimals, unsigned_flag);
1954
 
}
1955
 
 
1956
 
 
1957
 
String *Item_float::val_str(String *str)
1958
 
{
1959
 
  // following assert is redundant, because fixed=1 assigned in constructor
1960
 
  assert(fixed == 1);
1961
 
  str->set_real(value,decimals,&my_charset_bin);
1962
 
  return str;
1963
 
}
1964
 
 
1965
 
 
1966
 
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1967
 
{
1968
 
  // following assert is redundant, because fixed=1 assigned in constructor
1969
 
  assert(fixed == 1);
1970
 
  double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1971
 
  return (decimal_value);
1972
 
}
1973
 
 
1974
 
 
1975
 
void Item_string::print(String *str, enum_query_type query_type)
1976
 
{
1977
 
  if (query_type == QT_ORDINARY && is_cs_specified())
1978
 
  {
1979
 
    str->append('_');
1980
 
    str->append(collation.collation->csname);
1981
 
  }
1982
 
 
1983
 
  str->append('\'');
1984
 
 
1985
 
  if (query_type == QT_ORDINARY ||
1986
 
      my_charset_same(str_value.charset(), system_charset_info))
1987
 
  {
1988
 
    str_value.print(str);
1989
 
  }
1990
 
  else
1991
 
  {
1992
 
    THD *thd= current_thd;
1993
 
    LEX_STRING utf8_lex_str;
1994
 
 
1995
 
    thd->convert_string(&utf8_lex_str,
1996
 
                        system_charset_info,
1997
 
                        str_value.c_ptr_safe(),
1998
 
                        str_value.length(),
1999
 
                        str_value.charset());
2000
 
 
2001
 
    String utf8_str(utf8_lex_str.str,
2002
 
                    utf8_lex_str.length,
2003
 
                    system_charset_info);
2004
 
 
2005
 
    utf8_str.print(str);
2006
 
  }
2007
 
 
2008
 
  str->append('\'');
2009
 
}
2010
 
 
2011
 
 
2012
 
double Item_string::val_real()
2013
 
{
2014
 
  assert(fixed == 1);
2015
 
  int error;
2016
 
  char *end, *org_end;
2017
 
  double tmp;
2018
 
  const CHARSET_INFO * const cs= str_value.charset();
2019
 
 
2020
 
  org_end= (char*) str_value.ptr() + str_value.length();
2021
 
  tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
2022
 
                  &error);
2023
 
  if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2024
 
  {
2025
 
    /*
2026
 
      We can use str_value.ptr() here as Item_string is gurantee to put an
2027
 
      end \0 here.
2028
 
    */
2029
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2030
 
                        ER_TRUNCATED_WRONG_VALUE,
2031
 
                        ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
2032
 
                        str_value.ptr());
2033
 
  }
2034
 
  return tmp;
2035
 
}
2036
 
 
2037
 
 
2038
 
/**
2039
 
  @todo
2040
 
  Give error if we wanted a signed integer and we got an unsigned one
2041
 
*/
2042
 
int64_t Item_string::val_int()
2043
 
{
2044
 
  assert(fixed == 1);
2045
 
  int err;
2046
 
  int64_t tmp;
2047
 
  char *end= (char*) str_value.ptr()+ str_value.length();
2048
 
  char *org_end= end;
2049
 
  const CHARSET_INFO * const cs= str_value.charset();
2050
 
 
2051
 
  tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
2052
 
  /*
2053
 
    TODO: Give error if we wanted a signed integer and we got an unsigned
2054
 
    one
2055
 
  */
2056
 
  if (err > 0 ||
2057
 
      (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2058
 
  {
2059
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2060
 
                        ER_TRUNCATED_WRONG_VALUE,
2061
 
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2062
 
                        str_value.ptr());
2063
 
  }
2064
 
  return tmp;
2065
 
}
2066
 
 
2067
 
 
2068
 
my_decimal *Item_string::val_decimal(my_decimal *decimal_value)
2069
 
{
2070
 
  return val_decimal_from_string(decimal_value);
2071
 
}
2072
 
 
2073
 
 
2074
 
bool Item_null::eq(const Item *item,
2075
 
                   bool binary_cmp __attribute__((unused))) const
2076
 
{ return item->type() == type(); }
2077
 
 
2078
 
 
2079
 
double Item_null::val_real()
2080
 
{
2081
 
  // following assert is redundant, because fixed=1 assigned in constructor
2082
 
  assert(fixed == 1);
2083
 
  null_value=1;
2084
 
  return 0.0;
2085
 
}
2086
 
int64_t Item_null::val_int()
2087
 
{
2088
 
  // following assert is redundant, because fixed=1 assigned in constructor
2089
 
  assert(fixed == 1);
2090
 
  null_value=1;
2091
 
  return 0;
2092
 
}
2093
 
/* ARGSUSED */
2094
 
String *Item_null::val_str(String *str __attribute__((unused)))
2095
 
{
2096
 
  // following assert is redundant, because fixed=1 assigned in constructor
2097
 
  assert(fixed == 1);
2098
 
  null_value=1;
2099
 
  return 0;
2100
 
}
2101
 
 
2102
 
my_decimal *Item_null::val_decimal(my_decimal *decimal_value __attribute__((unused)))
2103
 
{
2104
 
  return 0;
2105
 
}
2106
 
 
2107
 
 
2108
 
Item *Item_null::safe_charset_converter(const CHARSET_INFO * const tocs)
2109
 
{
2110
 
  collation.set(tocs);
2111
 
  return this;
2112
 
}
2113
 
 
2114
 
/*********************** Item_param related ******************************/
2115
 
 
2116
 
/** 
2117
 
  Default function of Item_param::set_param_func, so in case
2118
 
  of malformed packet the server won't SIGSEGV.
2119
 
*/
2120
 
 
2121
 
static void
2122
 
default_set_param_func(Item_param *param,
2123
 
                       unsigned char **pos __attribute__((unused)),
2124
 
                       ulong len __attribute__((unused)))
2125
 
{
2126
 
  param->set_null();
2127
 
}
2128
 
 
2129
 
 
2130
 
Item_param::Item_param(uint32_t pos_in_query_arg) :
2131
 
  state(NO_VALUE),
2132
 
  item_result_type(STRING_RESULT),
2133
 
  /* Don't pretend to be a literal unless value for this item is set. */
2134
 
  item_type(PARAM_ITEM),
2135
 
  param_type(DRIZZLE_TYPE_VARCHAR),
2136
 
  pos_in_query(pos_in_query_arg),
2137
 
  set_param_func(default_set_param_func),
2138
 
  limit_clause_param(false)
2139
 
{
2140
 
  name= (char*) "?";
2141
 
  /* 
2142
 
    Since we can't say whenever this item can be NULL or cannot be NULL
2143
 
    before mysql_stmt_execute(), so we assuming that it can be NULL until
2144
 
    value is set.
2145
 
  */
2146
 
  maybe_null= 1;
2147
 
  cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE);
2148
 
  cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin);
2149
 
}
2150
 
 
2151
 
 
2152
 
void Item_param::set_null()
2153
 
{
2154
 
  /* These are cleared after each execution by reset() method */
2155
 
  null_value= 1;
2156
 
  /* 
2157
 
    Because of NULL and string values we need to set max_length for each new
2158
 
    placeholder value: user can submit NULL for any placeholder type, and 
2159
 
    string length can be different in each execution.
2160
 
  */
2161
 
  max_length= 0;
2162
 
  decimals= 0;
2163
 
  state= NULL_VALUE;
2164
 
  item_type= Item::NULL_ITEM;
2165
 
  return;
2166
 
}
2167
 
 
2168
 
void Item_param::set_int(int64_t i, uint32_t max_length_arg)
2169
 
{
2170
 
  value.integer= (int64_t) i;
2171
 
  state= INT_VALUE;
2172
 
  max_length= max_length_arg;
2173
 
  decimals= 0;
2174
 
  maybe_null= 0;
2175
 
  return;
2176
 
}
2177
 
 
2178
 
void Item_param::set_double(double d)
2179
 
{
2180
 
  value.real= d;
2181
 
  state= REAL_VALUE;
2182
 
  max_length= DBL_DIG + 8;
2183
 
  decimals= NOT_FIXED_DEC;
2184
 
  maybe_null= 0;
2185
 
  return;
2186
 
}
2187
 
 
2188
 
 
2189
 
/**
2190
 
  Set decimal parameter value from string.
2191
 
 
2192
 
  @param str      character string
2193
 
  @param length   string length
2194
 
 
2195
 
  @note
2196
 
    As we use character strings to send decimal values in
2197
 
    binary protocol, we use str2my_decimal to convert it to
2198
 
    internal decimal value.
2199
 
*/
2200
 
 
2201
 
void Item_param::set_decimal(char *str, ulong length)
2202
 
{
2203
 
  char *end;
2204
 
 
2205
 
  end= str+length;
2206
 
  str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2207
 
  state= DECIMAL_VALUE;
2208
 
  decimals= decimal_value.frac;
2209
 
  max_length= my_decimal_precision_to_length(decimal_value.precision(),
2210
 
                                             decimals, unsigned_flag);
2211
 
  maybe_null= 0;
2212
 
  return;
2213
 
}
2214
 
 
2215
 
 
2216
 
/**
2217
 
  Set parameter value from DRIZZLE_TIME value.
2218
 
 
2219
 
  @param tm              datetime value to set (time_type is ignored)
2220
 
  @param type            type of datetime value
2221
 
  @param max_length_arg  max length of datetime value as string
2222
 
 
2223
 
  @note
2224
 
    If we value to be stored is not normalized, zero value will be stored
2225
 
    instead and proper warning will be produced. This function relies on
2226
 
    the fact that even wrong value sent over binary protocol fits into
2227
 
    MAX_DATE_STRING_REP_LENGTH buffer.
2228
 
*/
2229
 
void Item_param::set_time(DRIZZLE_TIME *tm,
2230
 
                          enum enum_drizzle_timestamp_type time_type,
2231
 
                          uint32_t max_length_arg)
2232
 
2233
 
  value.time= *tm;
2234
 
  value.time.time_type= time_type;
2235
 
 
2236
 
  if (value.time.year > 9999 || value.time.month > 12 ||
2237
 
      value.time.day > 31 ||
2238
 
      ((time_type != DRIZZLE_TIMESTAMP_TIME) && value.time.hour > 23) ||
2239
 
      value.time.minute > 59 || value.time.second > 59)
2240
 
  {
2241
 
    char buff[MAX_DATE_STRING_REP_LENGTH];
2242
 
    uint32_t length= my_TIME_to_str(&value.time, buff);
2243
 
    make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2244
 
                                 buff, length, time_type, 0);
2245
 
    set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
2246
 
  }
2247
 
 
2248
 
  state= TIME_VALUE;
2249
 
  maybe_null= 0;
2250
 
  max_length= max_length_arg;
2251
 
  decimals= 0;
2252
 
  return;
2253
 
}
2254
 
 
2255
 
 
2256
 
bool Item_param::set_str(const char *str, ulong length)
2257
 
{
2258
 
  /*
2259
 
    Assign string with no conversion: data is converted only after it's
2260
 
    been written to the binary log.
2261
 
  */
2262
 
  uint32_t dummy_errors;
2263
 
  if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
2264
 
                     &dummy_errors))
2265
 
    return(true);
2266
 
  state= STRING_VALUE;
2267
 
  max_length= length;
2268
 
  maybe_null= 0;
2269
 
  /* max_length and decimals are set after charset conversion */
2270
 
  /* sic: str may be not null-terminated */
2271
 
  return(false);
2272
 
}
2273
 
 
2274
 
 
2275
 
bool Item_param::set_longdata(const char *str, ulong length)
2276
 
{
2277
 
  /*
2278
 
    If client character set is multibyte, end of long data packet
2279
 
    may hit at the middle of a multibyte character.  Additionally,
2280
 
    if binary log is open we must write long data value to the
2281
 
    binary log in character set of client. This is why we can't
2282
 
    convert long data to connection character set as it comes
2283
 
    (here), and first have to concatenate all pieces together,
2284
 
    write query to the binary log and only then perform conversion.
2285
 
  */
2286
 
  if (str_value.append(str, length, &my_charset_bin))
2287
 
    return(true);
2288
 
  state= LONG_DATA_VALUE;
2289
 
  maybe_null= 0;
2290
 
 
2291
 
  return(false);
2292
 
}
2293
 
 
2294
 
 
2295
 
/**
2296
 
  Set parameter value from user variable value.
2297
 
 
2298
 
  @param thd   Current thread
2299
 
  @param entry User variable structure (NULL means use NULL value)
2300
 
 
2301
 
  @retval
2302
 
    0 OK
2303
 
  @retval
2304
 
    1 Out of memory
2305
 
*/
2306
 
 
2307
 
bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry)
2308
 
{
2309
 
  if (entry && entry->value)
2310
 
  {
2311
 
    item_result_type= entry->type;
2312
 
    unsigned_flag= entry->unsigned_flag;
2313
 
    if (limit_clause_param)
2314
 
    {
2315
 
      bool unused;
2316
 
      set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
2317
 
      item_type= Item::INT_ITEM;
2318
 
      return(!unsigned_flag && value.integer < 0 ? 1 : 0);
2319
 
    }
2320
 
    switch (item_result_type) {
2321
 
    case REAL_RESULT:
2322
 
      set_double(*(double*)entry->value);
2323
 
      item_type= Item::REAL_ITEM;
2324
 
      break;
2325
 
    case INT_RESULT:
2326
 
      set_int(*(int64_t*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
2327
 
      item_type= Item::INT_ITEM;
2328
 
      break;
2329
 
    case STRING_RESULT:
2330
 
    {
2331
 
      const CHARSET_INFO * const fromcs= entry->collation.collation;
2332
 
      const CHARSET_INFO * const tocs= thd->variables.collation_connection;
2333
 
      uint32_t dummy_offset;
2334
 
 
2335
 
      value.cs_info.character_set_of_placeholder= 
2336
 
        value.cs_info.character_set_client= fromcs;
2337
 
      /*
2338
 
        Setup source and destination character sets so that they
2339
 
        are different only if conversion is necessary: this will
2340
 
        make later checks easier.
2341
 
      */
2342
 
      value.cs_info.final_character_set_of_str_value=
2343
 
        String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
2344
 
        tocs : fromcs;
2345
 
      /*
2346
 
        Exact value of max_length is not known unless data is converted to
2347
 
        charset of connection, so we have to set it later.
2348
 
      */
2349
 
      item_type= Item::STRING_ITEM;
2350
 
 
2351
 
      if (set_str((const char *)entry->value, entry->length))
2352
 
        return(1);
2353
 
      break;
2354
 
    }
2355
 
    case DECIMAL_RESULT:
2356
 
    {
2357
 
      const my_decimal *ent_value= (const my_decimal *)entry->value;
2358
 
      my_decimal2decimal(ent_value, &decimal_value);
2359
 
      state= DECIMAL_VALUE;
2360
 
      decimals= ent_value->frac;
2361
 
      max_length= my_decimal_precision_to_length(ent_value->precision(),
2362
 
                                                 decimals, unsigned_flag);
2363
 
      item_type= Item::DECIMAL_ITEM;
2364
 
      break;
2365
 
    }
2366
 
    default:
2367
 
      assert(0);
2368
 
      set_null();
2369
 
    }
2370
 
  }
2371
 
  else
2372
 
    set_null();
2373
 
 
2374
 
  return(0);
2375
 
}
2376
 
 
2377
 
/**
2378
 
  Resets parameter after execution.
2379
 
 
2380
 
  @note
2381
 
    We clear null_value here instead of setting it in set_* methods,
2382
 
    because we want more easily handle case for long data.
2383
 
*/
2384
 
 
2385
 
void Item_param::reset()
2386
 
{
2387
 
  /* Shrink string buffer if it's bigger than max possible CHAR column */
2388
 
  if (str_value.alloced_length() > MAX_CHAR_WIDTH)
2389
 
    str_value.free();
2390
 
  else
2391
 
    str_value.length(0);
2392
 
  str_value_ptr.length(0);
2393
 
  /*
2394
 
    We must prevent all charset conversions until data has been written
2395
 
    to the binary log.
2396
 
  */
2397
 
  str_value.set_charset(&my_charset_bin);
2398
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
2399
 
  state= NO_VALUE;
2400
 
  maybe_null= 1;
2401
 
  null_value= 0;
2402
 
  /*
2403
 
    Don't reset item_type to PARAM_ITEM: it's only needed to guard
2404
 
    us from item optimizations at prepare stage, when item doesn't yet
2405
 
    contain a literal of some kind.
2406
 
    In all other cases when this object is accessed its value is
2407
 
    set (this assumption is guarded by 'state' and
2408
 
    assertS(state != NO_VALUE) in all Item_param::get_*
2409
 
    methods).
2410
 
  */
2411
 
  return;
2412
 
}
2413
 
 
2414
 
 
2415
 
int Item_param::save_in_field(Field *field, bool no_conversions)
2416
 
{
2417
 
  field->set_notnull();
2418
 
 
2419
 
  switch (state) {
2420
 
  case INT_VALUE:
2421
 
    return field->store(value.integer, unsigned_flag);
2422
 
  case REAL_VALUE:
2423
 
    return field->store(value.real);
2424
 
  case DECIMAL_VALUE:
2425
 
    return field->store_decimal(&decimal_value);
2426
 
  case TIME_VALUE:
2427
 
    field->store_time(&value.time, value.time.time_type);
2428
 
    return 0;
2429
 
  case STRING_VALUE:
2430
 
  case LONG_DATA_VALUE:
2431
 
    return field->store(str_value.ptr(), str_value.length(),
2432
 
                        str_value.charset());
2433
 
  case NULL_VALUE:
2434
 
    return set_field_to_null_with_conversions(field, no_conversions);
2435
 
  case NO_VALUE:
2436
 
  default:
2437
 
    assert(0);
2438
 
  }
2439
 
  return 1;
2440
 
}
2441
 
 
2442
 
 
2443
 
bool Item_param::get_time(DRIZZLE_TIME *res)
2444
 
{
2445
 
  if (state == TIME_VALUE)
2446
 
  {
2447
 
    *res= value.time;
2448
 
    return 0;
2449
 
  }
2450
 
  /*
2451
 
    If parameter value isn't supplied assertion will fire in val_str()
2452
 
    which is called from Item::get_time().
2453
 
  */
2454
 
  return Item::get_time(res);
2455
 
}
2456
 
 
2457
 
 
2458
 
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
2459
 
{
2460
 
  if (state == TIME_VALUE)
2461
 
  {
2462
 
    *res= value.time;
2463
 
    return 0;
2464
 
  }
2465
 
  return Item::get_date(res, fuzzydate);
2466
 
}
2467
 
 
2468
 
 
2469
 
double Item_param::val_real()
2470
 
{
2471
 
  switch (state) {
2472
 
  case REAL_VALUE:
2473
 
    return value.real;
2474
 
  case INT_VALUE:
2475
 
    return (double) value.integer;
2476
 
  case DECIMAL_VALUE:
2477
 
  {
2478
 
    double result;
2479
 
    my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
2480
 
    return result;
2481
 
  }
2482
 
  case STRING_VALUE:
2483
 
  case LONG_DATA_VALUE:
2484
 
  {
2485
 
    int dummy_err;
2486
 
    char *end_not_used;
2487
 
    return my_strntod(str_value.charset(), (char*) str_value.ptr(),
2488
 
                      str_value.length(), &end_not_used, &dummy_err);
2489
 
  }
2490
 
  case TIME_VALUE:
2491
 
    /*
2492
 
      This works for example when user says SELECT ?+0.0 and supplies
2493
 
      time value for the placeholder.
2494
 
    */
2495
 
    return uint64_t2double(TIME_to_uint64_t(&value.time));
2496
 
  case NULL_VALUE:
2497
 
    return 0.0;
2498
 
  default:
2499
 
    assert(0);
2500
 
  }
2501
 
  return 0.0;
2502
 
2503
 
 
2504
 
 
2505
 
int64_t Item_param::val_int() 
2506
 
2507
 
  switch (state) {
2508
 
  case REAL_VALUE:
2509
 
    return (int64_t) rint(value.real);
2510
 
  case INT_VALUE:
2511
 
    return value.integer;
2512
 
  case DECIMAL_VALUE:
2513
 
  {
2514
 
    int64_t i;
2515
 
    my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
2516
 
    return i;
2517
 
  }
2518
 
  case STRING_VALUE:
2519
 
  case LONG_DATA_VALUE:
2520
 
    {
2521
 
      int dummy_err;
2522
 
      return my_strntoll(str_value.charset(), str_value.ptr(),
2523
 
                         str_value.length(), 10, (char**) 0, &dummy_err);
2524
 
    }
2525
 
  case TIME_VALUE:
2526
 
    return (int64_t) TIME_to_uint64_t(&value.time);
2527
 
  case NULL_VALUE:
2528
 
    return 0; 
2529
 
  default:
2530
 
    assert(0);
2531
 
  }
2532
 
  return 0;
2533
 
}
2534
 
 
2535
 
 
2536
 
my_decimal *Item_param::val_decimal(my_decimal *dec)
2537
 
{
2538
 
  switch (state) {
2539
 
  case DECIMAL_VALUE:
2540
 
    return &decimal_value;
2541
 
  case REAL_VALUE:
2542
 
    double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
2543
 
    return dec;
2544
 
  case INT_VALUE:
2545
 
    int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec);
2546
 
    return dec;
2547
 
  case STRING_VALUE:
2548
 
  case LONG_DATA_VALUE:
2549
 
    string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec);
2550
 
    return dec;
2551
 
  case TIME_VALUE:
2552
 
  {
2553
 
    int64_t i= (int64_t) TIME_to_uint64_t(&value.time);
2554
 
    int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
2555
 
    return dec;
2556
 
  }
2557
 
  case NULL_VALUE:
2558
 
    return 0; 
2559
 
  default:
2560
 
    assert(0);
2561
 
  }
2562
 
  return 0;
2563
 
}
2564
 
 
2565
 
 
2566
 
String *Item_param::val_str(String* str) 
2567
 
2568
 
  switch (state) {
2569
 
  case STRING_VALUE:
2570
 
  case LONG_DATA_VALUE:
2571
 
    return &str_value_ptr;
2572
 
  case REAL_VALUE:
2573
 
    str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2574
 
    return str;
2575
 
  case INT_VALUE:
2576
 
    str->set(value.integer, &my_charset_bin);
2577
 
    return str;
2578
 
  case DECIMAL_VALUE:
2579
 
    if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2580
 
                          0, 0, 0, str) <= 1)
2581
 
      return str;
2582
 
    return NULL;
2583
 
  case TIME_VALUE:
2584
 
  {
2585
 
    if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
2586
 
      break;
2587
 
    str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
2588
 
    str->set_charset(&my_charset_bin);
2589
 
    return str;
2590
 
  }
2591
 
  case NULL_VALUE:
2592
 
    return NULL; 
2593
 
  default:
2594
 
    assert(0);
2595
 
  }
2596
 
  return str;
2597
 
}
2598
 
 
2599
 
/**
2600
 
  Return Param item values in string format, for generating the dynamic 
2601
 
  query used in update/binary logs.
2602
 
 
2603
 
  @todo
2604
 
    - Change interface and implementation to fill log data in place
2605
 
    and avoid one more memcpy/alloc between str and log string.
2606
 
    - In case of error we need to notify replication
2607
 
    that binary log contains wrong statement 
2608
 
*/
2609
 
 
2610
 
const String *Item_param::query_val_str(String* str) const
2611
 
{
2612
 
  switch (state) {
2613
 
  case INT_VALUE:
2614
 
    str->set_int(value.integer, unsigned_flag, &my_charset_bin);
2615
 
    break;
2616
 
  case REAL_VALUE:
2617
 
    str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2618
 
    break;
2619
 
  case DECIMAL_VALUE:
2620
 
    if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2621
 
                          0, 0, 0, str) > 1)
2622
 
      return &my_null_string;
2623
 
    break;
2624
 
  case TIME_VALUE:
2625
 
    {
2626
 
      char *buf, *ptr;
2627
 
      str->length(0);
2628
 
      /*
2629
 
        TODO: in case of error we need to notify replication
2630
 
        that binary log contains wrong statement 
2631
 
      */
2632
 
      if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3))
2633
 
        break; 
2634
 
 
2635
 
      /* Create date string inplace */
2636
 
      buf= str->c_ptr_quick();
2637
 
      ptr= buf;
2638
 
      *ptr++= '\'';
2639
 
      ptr+= (uint) my_TIME_to_str(&value.time, ptr);
2640
 
      *ptr++= '\'';
2641
 
      str->length((uint32_t) (ptr - buf));
2642
 
      break;
2643
 
    }
2644
 
  case STRING_VALUE:
2645
 
  case LONG_DATA_VALUE:
2646
 
    {
2647
 
      str->length(0);
2648
 
      append_query_string(value.cs_info.character_set_client, &str_value, str);
2649
 
      break;
2650
 
    }
2651
 
  case NULL_VALUE:
2652
 
    return &my_null_string;
2653
 
  default:
2654
 
    assert(0);
2655
 
  }
2656
 
  return str;
2657
 
}
2658
 
 
2659
 
 
2660
 
/**
2661
 
  Convert string from client character set to the character set of
2662
 
  connection.
2663
 
*/
2664
 
 
2665
 
bool Item_param::convert_str_value(THD *thd)
2666
 
{
2667
 
  bool rc= false;
2668
 
  if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2669
 
  {
2670
 
    /*
2671
 
      Check is so simple because all charsets were set up properly
2672
 
      in setup_one_conversion_function, where typecode of
2673
 
      placeholder was also taken into account: the variables are different
2674
 
      here only if conversion is really necessary.
2675
 
    */
2676
 
    if (value.cs_info.final_character_set_of_str_value !=
2677
 
        value.cs_info.character_set_of_placeholder)
2678
 
    {
2679
 
      rc= thd->convert_string(&str_value,
2680
 
                              value.cs_info.character_set_of_placeholder,
2681
 
                              value.cs_info.final_character_set_of_str_value);
2682
 
    }
2683
 
    else
2684
 
      str_value.set_charset(value.cs_info.final_character_set_of_str_value);
2685
 
    /* Here str_value is guaranteed to be in final_character_set_of_str_value */
2686
 
 
2687
 
    max_length= str_value.length();
2688
 
    decimals= 0;
2689
 
    /*
2690
 
      str_value_ptr is returned from val_str(). It must be not alloced
2691
 
      to prevent it's modification by val_str() invoker.
2692
 
    */
2693
 
    str_value_ptr.set(str_value.ptr(), str_value.length(),
2694
 
                      str_value.charset());
2695
 
    /* Synchronize item charset with value charset */
2696
 
    collation.set(str_value.charset(), DERIVATION_COERCIBLE);
2697
 
  }
2698
 
  return rc;
2699
 
}
2700
 
 
2701
 
 
2702
 
bool Item_param::basic_const_item() const
2703
 
{
2704
 
  if (state == NO_VALUE || state == TIME_VALUE)
2705
 
    return false;
2706
 
  return true;
2707
 
}
2708
 
 
2709
 
 
2710
 
Item *
2711
 
Item_param::clone_item()
2712
 
{
2713
 
  /* see comments in the header file */
2714
 
  switch (state) {
2715
 
  case NULL_VALUE:
2716
 
    return new Item_null(name);
2717
 
  case INT_VALUE:
2718
 
    return (unsigned_flag ?
2719
 
            new Item_uint(name, value.integer, max_length) :
2720
 
            new Item_int(name, value.integer, max_length));
2721
 
  case REAL_VALUE:
2722
 
    return new Item_float(name, value.real, decimals, max_length);
2723
 
  case STRING_VALUE:
2724
 
  case LONG_DATA_VALUE:
2725
 
    return new Item_string(name, str_value.c_ptr_quick(), str_value.length(),
2726
 
                           str_value.charset());
2727
 
  case TIME_VALUE:
2728
 
    break;
2729
 
  case NO_VALUE:
2730
 
  default:
2731
 
    assert(0);
2732
 
  };
2733
 
  return 0;
2734
 
}
2735
 
 
2736
 
 
2737
 
bool
2738
 
Item_param::eq(const Item *arg, bool binary_cmp) const
2739
 
{
2740
 
  Item *item;
2741
 
  if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2742
 
    return false;
2743
 
  /*
2744
 
    We need to cast off const to call val_int(). This should be OK for
2745
 
    a basic constant.
2746
 
  */
2747
 
  item= (Item*) arg;
2748
 
 
2749
 
  switch (state) {
2750
 
  case NULL_VALUE:
2751
 
    return true;
2752
 
  case INT_VALUE:
2753
 
    return value.integer == item->val_int() &&
2754
 
           unsigned_flag == item->unsigned_flag;
2755
 
  case REAL_VALUE:
2756
 
    return value.real == item->val_real();
2757
 
  case STRING_VALUE:
2758
 
  case LONG_DATA_VALUE:
2759
 
    if (binary_cmp)
2760
 
      return !stringcmp(&str_value, &item->str_value);
2761
 
    return !sortcmp(&str_value, &item->str_value, collation.collation);
2762
 
  default:
2763
 
    break;
2764
 
  }
2765
 
  return false;
2766
 
}
2767
 
 
2768
 
/* End of Item_param related */
2769
 
 
2770
 
void Item_param::print(String *str,
2771
 
                       enum_query_type query_type __attribute__((unused)))
2772
 
{
2773
 
  if (state == NO_VALUE)
2774
 
  {
2775
 
    str->append('?');
2776
 
  }
2777
 
  else
2778
 
  {
2779
 
    char buffer[STRING_BUFFER_USUAL_SIZE];
2780
 
    String tmp(buffer, sizeof(buffer), &my_charset_bin);
2781
 
    const String *res;
2782
 
    res= query_val_str(&tmp);
2783
 
    str->append(*res);
2784
 
  }
2785
 
}
2786
 
 
2787
 
 
2788
 
/****************************************************************************
2789
 
  Item_copy_string
2790
 
****************************************************************************/
2791
 
 
2792
 
void Item_copy_string::copy()
2793
 
{
2794
 
  String *res=item->val_str(&str_value);
2795
 
  if (res && res != &str_value)
2796
 
    str_value.copy(*res);
2797
 
  null_value=item->null_value;
2798
 
}
2799
 
 
2800
 
/* ARGSUSED */
2801
 
String *Item_copy_string::val_str(String *str __attribute__((unused)))
2802
 
{
2803
 
  // Item_copy_string is used without fix_fields call
2804
 
  if (null_value)
2805
 
    return (String*) 0;
2806
 
  return &str_value;
2807
 
}
2808
 
 
2809
 
 
2810
 
my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value)
2811
 
{
2812
 
  // Item_copy_string is used without fix_fields call
2813
 
  if (null_value)
2814
 
    return 0;
2815
 
  string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value);
2816
 
  return (decimal_value);
2817
 
}
2818
 
 
 
965
    session->change_item_tree(ref, item_ref);
 
966
  }
 
967
}
2819
968
 
2820
969
/*
2821
970
  Functions to convert item to field (for send_fields)
2822
971
*/
2823
972
 
2824
973
/* ARGSUSED */
2825
 
bool Item::fix_fields(THD *thd __attribute__((unused)),
2826
 
                      Item **ref __attribute__((unused)))
 
974
bool Item::fix_fields(Session *, Item **)
2827
975
{
2828
976
 
2829
977
  // We do not check fields which are fixed during construction
2832
980
  return false;
2833
981
}
2834
982
 
2835
 
double Item_ref_null_helper::val_real()
2836
 
{
2837
 
  assert(fixed == 1);
2838
 
  double tmp= (*ref)->val_result();
2839
 
  owner->was_null|= null_value= (*ref)->null_value;
2840
 
  return tmp;
2841
 
}
2842
 
 
2843
 
 
2844
 
int64_t Item_ref_null_helper::val_int()
2845
 
{
2846
 
  assert(fixed == 1);
2847
 
  int64_t tmp= (*ref)->val_int_result();
2848
 
  owner->was_null|= null_value= (*ref)->null_value;
2849
 
  return tmp;
2850
 
}
2851
 
 
2852
 
 
2853
 
my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value)
2854
 
{
2855
 
  assert(fixed == 1);
2856
 
  my_decimal *val= (*ref)->val_decimal_result(decimal_value);
2857
 
  owner->was_null|= null_value= (*ref)->null_value;
2858
 
  return val;
2859
 
}
2860
 
 
2861
 
 
2862
 
bool Item_ref_null_helper::val_bool()
2863
 
{
2864
 
  assert(fixed == 1);
2865
 
  bool val= (*ref)->val_bool_result();
2866
 
  owner->was_null|= null_value= (*ref)->null_value;
2867
 
  return val;
2868
 
}
2869
 
 
2870
 
 
2871
 
String* Item_ref_null_helper::val_str(String* s)
2872
 
{
2873
 
  assert(fixed == 1);
2874
 
  String* tmp= (*ref)->str_result(s);
2875
 
  owner->was_null|= null_value= (*ref)->null_value;
2876
 
  return tmp;
2877
 
}
2878
 
 
2879
 
 
2880
 
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
2881
 
{  
2882
 
  return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
2883
 
}
2884
 
 
2885
983
 
2886
984
/**
2887
 
  Mark item and SELECT_LEXs as dependent if item was resolved in
 
985
  Mark item and Select_Lexs as dependent if item was resolved in
2888
986
  outer SELECT.
2889
987
 
2890
 
  @param thd             thread handler
 
988
  @param session             thread handler
2891
989
  @param last            select from which current item depend
2892
990
  @param current         current select
2893
991
  @param resolved_item   item which was resolved in outer SELECT(for warning)
2895
993
                         substitution)
2896
994
*/
2897
995
 
2898
 
static void mark_as_dependent(THD *thd, SELECT_LEX *last, SELECT_LEX *current,
 
996
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current,
2899
997
                              Item_ident *resolved_item,
2900
998
                              Item_ident *mark_item)
2901
999
{
2903
1001
                        resolved_item->db_name : "");
2904
1002
  const char *table_name= (resolved_item->table_name ?
2905
1003
                           resolved_item->table_name : "");
2906
 
  /* store pointer on SELECT_LEX from which item is dependent */
 
1004
  /* store pointer on Select_Lex from which item is dependent */
2907
1005
  if (mark_item)
2908
1006
    mark_item->depended_from= last;
2909
1007
  current->mark_as_dependent(last);
2910
 
  if (thd->lex->describe & DESCRIBE_EXTENDED)
 
1008
  if (session->lex->describe & DESCRIBE_EXTENDED)
2911
1009
  {
2912
1010
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
2913
1011
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
2915
1013
            table_name, (table_name [0] ? "." : ""),
2916
1014
            resolved_item->field_name,
2917
1015
            current->select_number, last->select_number);
2918
 
    push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1016
    push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2919
1017
                 ER_WARN_FIELD_RESOLVED, warn_buff);
2920
1018
  }
2921
1019
}
2925
1023
  Mark range of selects and resolved identifier (field/reference)
2926
1024
  item as dependent.
2927
1025
 
2928
 
  @param thd             thread handler
 
1026
  @param session             thread handler
2929
1027
  @param last_select     select where resolved_item was resolved
2930
1028
  @param current_sel     current select (select where resolved_item was placed)
2931
1029
  @param found_field     field which was found during resolving
2941
1039
    resolved identifier.
2942
1040
*/
2943
1041
 
2944
 
void mark_select_range_as_dependent(THD *thd,
2945
 
                                    SELECT_LEX *last_select,
2946
 
                                    SELECT_LEX *current_sel,
 
1042
void mark_select_range_as_dependent(Session *session,
 
1043
                                    Select_Lex *last_select,
 
1044
                                    Select_Lex *current_sel,
2947
1045
                                    Field *found_field, Item *found_item,
2948
1046
                                    Item_ident *resolved_item)
2949
1047
{
2953
1051
    done once when we resolved this field and cached result of
2954
1052
    resolving)
2955
1053
  */
2956
 
  SELECT_LEX *previous_select= current_sel;
 
1054
  Select_Lex *previous_select= current_sel;
2957
1055
  for (; previous_select->outer_select() != last_select;
2958
1056
       previous_select= previous_select->outer_select())
2959
1057
  {
2979
1077
      prev_subselect_item->used_tables_cache|=
2980
1078
        found_field->table->map;
2981
1079
    prev_subselect_item->const_item_cache= 0;
2982
 
    mark_as_dependent(thd, last_select, current_sel, resolved_item,
 
1080
    mark_as_dependent(session, last_select, current_sel, resolved_item,
2983
1081
                      dependent);
2984
1082
  }
2985
1083
}
3024
1122
  if (db_name && lower_case_table_names)
3025
1123
  {
3026
1124
    /* Convert database to lower case for comparison */
3027
 
    strmake(name_buff, db_name, sizeof(name_buff)-1);
 
1125
    strncpy(name_buff, db_name, sizeof(name_buff)-1);
3028
1126
    my_casedn_str(files_charset_info, name_buff);
3029
1127
    db_name= name_buff;
3030
1128
  }
3037
1135
    {
3038
1136
      cur_field= (Item_ident*) *cur_group->item;
3039
1137
      cur_match_degree= 0;
3040
 
      
 
1138
 
3041
1139
      assert(cur_field->field_name != 0);
3042
1140
 
3043
1141
      if (!my_strcasecmp(system_charset_info,
3078
1176
          is ambiguous.
3079
1177
        */
3080
1178
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
3081
 
                 find_item->full_name(), current_thd->where);
 
1179
                 find_item->full_name(), current_session->where);
3082
1180
        return NULL;
3083
1181
      }
3084
1182
    }
3104
1202
  derived SELECT column. This extension is allowed only if the
3105
1203
  MODE_ONLY_FULL_GROUP_BY sql mode isn't enabled.
3106
1204
 
3107
 
  @param thd     current thread
 
1205
  @param session     current thread
3108
1206
  @param ref     column reference being resolved
3109
1207
  @param select  the select that ref is resolved against
3110
1208
 
3127
1225
    - resolved item - if the item was resolved
3128
1226
*/
3129
1227
 
3130
 
static Item**
3131
 
resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select)
 
1228
Item**
 
1229
resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
3132
1230
{
3133
1231
  Item **group_by_ref= NULL;
3134
1232
  Item **select_ref= NULL;
3152
1250
  if (select->having_fix_field && !ref->with_sum_func && group_list)
3153
1251
  {
3154
1252
    group_by_ref= find_field_in_group_list(ref, group_list);
3155
 
    
 
1253
 
3156
1254
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
3157
1255
    if (group_by_ref && (select_ref != not_found_item) &&
3158
1256
        !((*group_by_ref)->eq(*select_ref, 0)))
3159
1257
    {
3160
1258
      ambiguous_fields= true;
3161
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
1259
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
3162
1260
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
3163
 
                          current_thd->where);
 
1261
                          current_session->where);
3164
1262
 
3165
1263
    }
3166
1264
  }
3188
1286
  return (Item**) not_found_item;
3189
1287
}
3190
1288
 
3191
 
 
3192
 
/**
3193
 
  Resolve the name of an outer select column reference.
3194
 
 
3195
 
  The method resolves the column reference represented by 'this' as a column
3196
 
  present in outer selects that contain current select.
3197
 
 
3198
 
  In prepared statements, because of cache, find_field_in_tables()
3199
 
  can resolve fields even if they don't belong to current context.
3200
 
  In this case this method only finds appropriate context and marks
3201
 
  current select as dependent. The found reference of field should be
3202
 
  provided in 'from_field'.
3203
 
 
3204
 
  @param[in] thd             current thread
3205
 
  @param[in,out] from_field  found field reference or (Field*)not_found_field
3206
 
  @param[in,out] reference   view column if this item was resolved to a
3207
 
    view column
3208
 
 
3209
 
  @note
3210
 
    This is the inner loop of Item_field::fix_fields:
3211
 
  @code
3212
 
        for each outer query Q_k beginning from the inner-most one
3213
 
        {
3214
 
          search for a column or derived column named col_ref_i
3215
 
          [in table T_j] in the FROM clause of Q_k;
3216
 
 
3217
 
          if such a column is not found
3218
 
            Search for a column or derived column named col_ref_i
3219
 
            [in table T_j] in the SELECT and GROUP clauses of Q_k.
3220
 
        }
3221
 
  @endcode
3222
 
 
3223
 
  @retval
3224
 
    1   column succefully resolved and fix_fields() should continue.
3225
 
  @retval
3226
 
    0   column fully fixed and fix_fields() should return false
3227
 
  @retval
3228
 
    -1  error occured
3229
 
*/
3230
 
 
3231
 
int
3232
 
Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
3233
 
{
3234
 
  enum_parsing_place place= NO_MATTER;
3235
 
  bool field_found= (*from_field != not_found_field);
3236
 
  bool upward_lookup= false;
3237
 
 
3238
 
  /*
3239
 
    If there are outer contexts (outer selects, but current select is
3240
 
    not derived table or view) try to resolve this reference in the
3241
 
    outer contexts.
3242
 
 
3243
 
    We treat each subselect as a separate namespace, so that different
3244
 
    subselects may contain columns with the same names. The subselects
3245
 
    are searched starting from the innermost.
3246
 
  */
3247
 
  Name_resolution_context *last_checked_context= context;
3248
 
  Item **ref= (Item **) not_found_item;
3249
 
  SELECT_LEX *current_sel= (SELECT_LEX *) thd->lex->current_select;
3250
 
  Name_resolution_context *outer_context= 0;
3251
 
  SELECT_LEX *select= 0;
3252
 
  /* Currently derived tables cannot be correlated */
3253
 
  if (current_sel->master_unit()->first_select()->linkage !=
3254
 
      DERIVED_TABLE_TYPE)
3255
 
    outer_context= context->outer_context;
3256
 
  for (;
3257
 
       outer_context;
3258
 
       outer_context= outer_context->outer_context)
3259
 
  {
3260
 
    select= outer_context->select_lex;
3261
 
    Item_subselect *prev_subselect_item=
3262
 
      last_checked_context->select_lex->master_unit()->item;
3263
 
    last_checked_context= outer_context;
3264
 
    upward_lookup= true;
3265
 
 
3266
 
    place= prev_subselect_item->parsing_place;
3267
 
    /*
3268
 
      If outer_field is set, field was already found by first call
3269
 
      to find_field_in_tables(). Only need to find appropriate context.
3270
 
    */
3271
 
    if (field_found && outer_context->select_lex !=
3272
 
        cached_table->select_lex)
3273
 
      continue;
3274
 
    /*
3275
 
      In case of a view, find_field_in_tables() writes the pointer to
3276
 
      the found view field into '*reference', in other words, it
3277
 
      substitutes this Item_field with the found expression.
3278
 
    */
3279
 
    if (field_found || (*from_field= find_field_in_tables(thd, this,
3280
 
                                          outer_context->
3281
 
                                            first_name_resolution_table,
3282
 
                                          outer_context->
3283
 
                                            last_name_resolution_table,
3284
 
                                          reference,
3285
 
                                          IGNORE_EXCEPT_NON_UNIQUE,
3286
 
                                          true, true)) !=
3287
 
        not_found_field)
3288
 
    {
3289
 
      if (*from_field)
3290
 
      {
3291
 
        if (*from_field != view_ref_found)
3292
 
        {
3293
 
          prev_subselect_item->used_tables_cache|= (*from_field)->table->map;
3294
 
          prev_subselect_item->const_item_cache= 0;
3295
 
          set_field(*from_field);
3296
 
          if (!last_checked_context->select_lex->having_fix_field &&
3297
 
              select->group_list.elements &&
3298
 
              (place == SELECT_LIST || place == IN_HAVING))
3299
 
          {
3300
 
            Item_outer_ref *rf;
3301
 
            /*
3302
 
              If an outer field is resolved in a grouping select then it
3303
 
              is replaced for an Item_outer_ref object. Otherwise an
3304
 
              Item_field object is used.
3305
 
              The new Item_outer_ref object is saved in the inner_refs_list of
3306
 
              the outer select. Here it is only created. It can be fixed only
3307
 
              after the original field has been fixed and this is done in the
3308
 
              fix_inner_refs() function.
3309
 
            */
3310
 
            ;
3311
 
            if (!(rf= new Item_outer_ref(context, this)))
3312
 
              return -1;
3313
 
            thd->change_item_tree(reference, rf);
3314
 
            select->inner_refs_list.push_back(rf);
3315
 
            rf->in_sum_func= thd->lex->in_sum_func;
3316
 
          }
3317
 
          /*
3318
 
            A reference is resolved to a nest level that's outer or the same as
3319
 
            the nest level of the enclosing set function : adjust the value of
3320
 
            max_arg_level for the function if it's needed.
3321
 
          */
3322
 
          if (thd->lex->in_sum_func &&
3323
 
              thd->lex->in_sum_func->nest_level >= select->nest_level)
3324
 
          {
3325
 
            Item::Type ref_type= (*reference)->type();
3326
 
            set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3327
 
                          select->nest_level);
3328
 
            set_field(*from_field);
3329
 
            fixed= 1;
3330
 
            mark_as_dependent(thd, last_checked_context->select_lex,
3331
 
                              context->select_lex, this,
3332
 
                              ((ref_type == REF_ITEM ||
3333
 
                                ref_type == FIELD_ITEM) ?
3334
 
                               (Item_ident*) (*reference) : 0));
3335
 
            return 0;
3336
 
          }
3337
 
        }
3338
 
        else
3339
 
        {
3340
 
          Item::Type ref_type= (*reference)->type();
3341
 
          prev_subselect_item->used_tables_cache|=
3342
 
            (*reference)->used_tables();
3343
 
          prev_subselect_item->const_item_cache&=
3344
 
            (*reference)->const_item();
3345
 
          mark_as_dependent(thd, last_checked_context->select_lex,
3346
 
                            context->select_lex, this,
3347
 
                            ((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3348
 
                             (Item_ident*) (*reference) :
3349
 
                             0));
3350
 
          /*
3351
 
            A reference to a view field had been found and we
3352
 
            substituted it instead of this Item (find_field_in_tables
3353
 
            does it by assigning the new value to *reference), so now
3354
 
            we can return from this function.
3355
 
          */
3356
 
          return 0;
3357
 
        }
3358
 
      }
3359
 
      break;
3360
 
    }
3361
 
 
3362
 
    /* Search in SELECT and GROUP lists of the outer select. */
3363
 
    if (place != IN_WHERE && place != IN_ON)
3364
 
    {
3365
 
      if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
3366
 
        return -1; /* Some error occurred (e.g. ambiguous names). */
3367
 
      if (ref != not_found_item)
3368
 
      {
3369
 
        assert(*ref && (*ref)->fixed);
3370
 
        prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
3371
 
        prev_subselect_item->const_item_cache&= (*ref)->const_item();
3372
 
        break;
3373
 
      }
3374
 
    }
3375
 
 
3376
 
    /*
3377
 
      Reference is not found in this select => this subquery depend on
3378
 
      outer select (or we just trying to find wrong identifier, in this
3379
 
      case it does not matter which used tables bits we set)
3380
 
    */
3381
 
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
3382
 
    prev_subselect_item->const_item_cache= 0;
3383
 
  }
3384
 
 
3385
 
  assert(ref != 0);
3386
 
  if (!*from_field)
3387
 
    return -1;
3388
 
  if (ref == not_found_item && *from_field == not_found_field)
3389
 
  {
3390
 
    if (upward_lookup)
3391
 
    {
3392
 
      // We can't say exactly what absent table or field
3393
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where);
3394
 
    }
3395
 
    else
3396
 
    {
3397
 
      /* Call find_field_in_tables only to report the error */
3398
 
      find_field_in_tables(thd, this,
3399
 
                           context->first_name_resolution_table,
3400
 
                           context->last_name_resolution_table,
3401
 
                           reference, REPORT_ALL_ERRORS,
3402
 
                           !any_privileges &&
3403
 
                           true, true);
3404
 
    }
3405
 
    return -1;
3406
 
  }
3407
 
  else if (ref != not_found_item)
3408
 
  {
3409
 
    Item *save;
3410
 
    Item_ref *rf;
3411
 
 
3412
 
    /* Should have been checked in resolve_ref_in_select_and_group(). */
3413
 
    assert(*ref && (*ref)->fixed);
3414
 
    /*
3415
 
      Here, a subset of actions performed by Item_ref::set_properties
3416
 
      is not enough. So we pass ptr to NULL into Item_[direct]_ref
3417
 
      constructor, so no initialization is performed, and call 
3418
 
      fix_fields() below.
3419
 
    */
3420
 
    save= *ref;
3421
 
    *ref= NULL;                             // Don't call set_properties()
3422
 
    rf= (place == IN_HAVING ?
3423
 
         new Item_ref(context, ref, (char*) table_name,
3424
 
                      (char*) field_name, alias_name_used) :
3425
 
         (!select->group_list.elements ?
3426
 
         new Item_direct_ref(context, ref, (char*) table_name,
3427
 
                             (char*) field_name, alias_name_used) :
3428
 
         new Item_outer_ref(context, ref, (char*) table_name,
3429
 
                            (char*) field_name, alias_name_used)));
3430
 
    *ref= save;
3431
 
    if (!rf)
3432
 
      return -1;
3433
 
 
3434
 
    if (place != IN_HAVING && select->group_list.elements)
3435
 
    {
3436
 
      outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3437
 
      ((Item_outer_ref*)rf)->in_sum_func= thd->lex->in_sum_func;
3438
 
    }
3439
 
    thd->change_item_tree(reference, rf);
3440
 
    /*
3441
 
      rf is Item_ref => never substitute other items (in this case)
3442
 
      during fix_fields() => we can use rf after fix_fields()
3443
 
    */
3444
 
    assert(!rf->fixed);                // Assured by Item_ref()
3445
 
    if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3446
 
      return -1;
3447
 
 
3448
 
    mark_as_dependent(thd, last_checked_context->select_lex,
3449
 
                      context->select_lex, this,
3450
 
                      rf);
3451
 
    return 0;
3452
 
  }
3453
 
  else
3454
 
  {
3455
 
    mark_as_dependent(thd, last_checked_context->select_lex,
3456
 
                      context->select_lex,
3457
 
                      this, (Item_ident*)*reference);
3458
 
    if (last_checked_context->select_lex->having_fix_field)
3459
 
    {
3460
 
      Item_ref *rf;
3461
 
      rf= new Item_ref(context,
3462
 
                       (cached_table->db[0] ? cached_table->db : 0),
3463
 
                       (char*) cached_table->alias, (char*) field_name);
3464
 
      if (!rf)
3465
 
        return -1;
3466
 
      thd->change_item_tree(reference, rf);
3467
 
      /*
3468
 
        rf is Item_ref => never substitute other items (in this case)
3469
 
        during fix_fields() => we can use rf after fix_fields()
3470
 
      */
3471
 
      assert(!rf->fixed);                // Assured by Item_ref()
3472
 
      if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3473
 
        return -1;
3474
 
      return 0;
3475
 
    }
3476
 
  }
3477
 
  return 1;
3478
 
}
3479
 
 
3480
 
 
3481
 
/**
3482
 
  Resolve the name of a column reference.
3483
 
 
3484
 
  The method resolves the column reference represented by 'this' as a column
3485
 
  present in one of: FROM clause, SELECT clause, GROUP BY clause of a query
3486
 
  Q, or in outer queries that contain Q.
3487
 
 
3488
 
  The name resolution algorithm used is (where [T_j] is an optional table
3489
 
  name that qualifies the column name):
3490
 
 
3491
 
  @code
3492
 
    resolve_column_reference([T_j].col_ref_i)
3493
 
    {
3494
 
      search for a column or derived column named col_ref_i
3495
 
      [in table T_j] in the FROM clause of Q;
3496
 
 
3497
 
      if such a column is NOT found AND    // Lookup in outer queries.
3498
 
         there are outer queries
3499
 
      {
3500
 
        for each outer query Q_k beginning from the inner-most one
3501
 
        {
3502
 
          search for a column or derived column named col_ref_i
3503
 
          [in table T_j] in the FROM clause of Q_k;
3504
 
 
3505
 
          if such a column is not found
3506
 
            Search for a column or derived column named col_ref_i
3507
 
            [in table T_j] in the SELECT and GROUP clauses of Q_k.
3508
 
        }
3509
 
      }
3510
 
    }
3511
 
  @endcode
3512
 
 
3513
 
    Notice that compared to Item_ref::fix_fields, here we first search the FROM
3514
 
    clause, and then we search the SELECT and GROUP BY clauses.
3515
 
 
3516
 
  @param[in]     thd        current thread
3517
 
  @param[in,out] reference  view column if this item was resolved to a
3518
 
    view column
3519
 
 
3520
 
  @retval
3521
 
    true  if error
3522
 
  @retval
3523
 
    false on success
3524
 
*/
3525
 
 
3526
 
bool Item_field::fix_fields(THD *thd, Item **reference)
3527
 
{
3528
 
  assert(fixed == 0);
3529
 
  Field *from_field= (Field *)not_found_field;
3530
 
  bool outer_fixed= false;
3531
 
 
3532
 
  if (!field)                                   // If field is not checked
3533
 
  {
3534
 
    /*
3535
 
      In case of view, find_field_in_tables() write pointer to view field
3536
 
      expression to 'reference', i.e. it substitute that expression instead
3537
 
      of this Item_field
3538
 
    */
3539
 
    if ((from_field= find_field_in_tables(thd, this,
3540
 
                                          context->first_name_resolution_table,
3541
 
                                          context->last_name_resolution_table,
3542
 
                                          reference,
3543
 
                                          thd->lex->use_only_table_context ?
3544
 
                                            REPORT_ALL_ERRORS : 
3545
 
                                            IGNORE_EXCEPT_NON_UNIQUE,
3546
 
                                          !any_privileges,
3547
 
                                          true)) ==
3548
 
        not_found_field)
3549
 
    {
3550
 
      int ret;
3551
 
      /* Look up in current select's item_list to find aliased fields */
3552
 
      if (thd->lex->current_select->is_item_list_lookup)
3553
 
      {
3554
 
        uint32_t counter;
3555
 
        enum_resolution_type resolution;
3556
 
        Item** res= find_item_in_list(this, thd->lex->current_select->item_list,
3557
 
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
3558
 
                                      &resolution);
3559
 
        if (!res)
3560
 
          return 1;
3561
 
        if (resolution == RESOLVED_AGAINST_ALIAS)
3562
 
          alias_name_used= true;
3563
 
        if (res != (Item **)not_found_item)
3564
 
        {
3565
 
          if ((*res)->type() == Item::FIELD_ITEM)
3566
 
          {
3567
 
            /*
3568
 
              It's an Item_field referencing another Item_field in the select
3569
 
              list.
3570
 
              Use the field from the Item_field in the select list and leave
3571
 
              the Item_field instance in place.
3572
 
            */
3573
 
 
3574
 
            Field *new_field= (*((Item_field**)res))->field;
3575
 
 
3576
 
            if (new_field == NULL)
3577
 
            {
3578
 
              /* The column to which we link isn't valid. */
3579
 
              my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name, 
3580
 
                       current_thd->where);
3581
 
              return(1);
3582
 
            }
3583
 
 
3584
 
            set_field(new_field);
3585
 
            return 0;
3586
 
          }
3587
 
          else
3588
 
          {
3589
 
            /*
3590
 
              It's not an Item_field in the select list so we must make a new
3591
 
              Item_ref to point to the Item in the select list and replace the
3592
 
              Item_field created by the parser with the new Item_ref.
3593
 
            */
3594
 
            Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3595
 
            if (!rf)
3596
 
              return 1;
3597
 
            thd->change_item_tree(reference, rf);
3598
 
            /*
3599
 
              Because Item_ref never substitutes itself with other items 
3600
 
              in Item_ref::fix_fields(), we can safely use the original 
3601
 
              pointer to it even after fix_fields()
3602
 
             */
3603
 
            return rf->fix_fields(thd, reference) ||  rf->check_cols(1);
3604
 
          }
3605
 
        }
3606
 
      }
3607
 
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3608
 
        goto error;
3609
 
      outer_fixed= true;
3610
 
      if (!ret)
3611
 
        goto mark_non_agg_field;
3612
 
    }
3613
 
    else if (!from_field)
3614
 
      goto error;
3615
 
 
3616
 
    if (!outer_fixed && cached_table && cached_table->select_lex &&
3617
 
        context->select_lex &&
3618
 
        cached_table->select_lex != context->select_lex)
3619
 
    {
3620
 
      int ret;
3621
 
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3622
 
        goto error;
3623
 
      outer_fixed= 1;
3624
 
      if (!ret)
3625
 
        goto mark_non_agg_field;
3626
 
    }
3627
 
 
3628
 
    /*
3629
 
      if it is not expression from merged VIEW we will set this field.
3630
 
 
3631
 
      We can leave expression substituted from view for next PS/SP rexecution
3632
 
      (i.e. do not register this substitution for reverting on cleanup()
3633
 
      (register_item_tree_changing())), because this subtree will be
3634
 
      fix_field'ed during setup_tables()->setup_underlying() (i.e. before
3635
 
      all other expressions of query, and references on tables which do
3636
 
      not present in query will not make problems.
3637
 
 
3638
 
      Also we suppose that view can't be changed during PS/SP life.
3639
 
    */
3640
 
    if (from_field == view_ref_found)
3641
 
      return false;
3642
 
 
3643
 
    set_field(from_field);
3644
 
    if (thd->lex->in_sum_func &&
3645
 
        thd->lex->in_sum_func->nest_level == 
3646
 
        thd->lex->current_select->nest_level)
3647
 
      set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3648
 
                    thd->lex->current_select->nest_level);
3649
 
  }
3650
 
  else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3651
 
  {
3652
 
    Table *table= field->table;
3653
 
    MY_BITMAP *current_bitmap, *other_bitmap;
3654
 
    if (thd->mark_used_columns == MARK_COLUMNS_READ)
3655
 
    {
3656
 
      current_bitmap= table->read_set;
3657
 
      other_bitmap=   table->write_set;
3658
 
    }
3659
 
    else
3660
 
    {
3661
 
      current_bitmap= table->write_set;
3662
 
      other_bitmap=   table->read_set;
3663
 
    }
3664
 
    if (!bitmap_fast_test_and_set(current_bitmap, field->field_index))
3665
 
    {
3666
 
      if (!bitmap_is_set(other_bitmap, field->field_index))
3667
 
      {
3668
 
        /* First usage of column */
3669
 
        table->used_fields++;                     // Used to optimize loops
3670
 
        /* purecov: begin inspected */
3671
 
        table->covering_keys.intersect(field->part_of_key);
3672
 
        /* purecov: end */
3673
 
      }
3674
 
    }
3675
 
  }
3676
 
  fixed= 1;
3677
 
mark_non_agg_field:
3678
 
  return false;
3679
 
 
3680
 
error:
3681
 
  context->process_error(thd);
3682
 
  return true;
3683
 
}
3684
 
 
3685
 
Item *Item_field::safe_charset_converter(const CHARSET_INFO * const tocs)
3686
 
{
3687
 
  no_const_subst= 1;
3688
 
  return Item::safe_charset_converter(tocs);
3689
 
}
3690
 
 
3691
 
 
3692
 
void Item_field::cleanup()
3693
 
{
3694
 
  Item_ident::cleanup();
3695
 
  /*
3696
 
    Even if this object was created by direct link to field in setup_wild()
3697
 
    it will be linked correctly next time by name of field and table alias.
3698
 
    I.e. we can drop 'field'.
3699
 
   */
3700
 
  field= result_field= 0;
3701
 
  null_value= false;
3702
 
  return;
3703
 
}
3704
 
 
3705
 
/**
3706
 
  Find a field among specified multiple equalities.
3707
 
 
3708
 
  The function first searches the field among multiple equalities
3709
 
  of the current level (in the cond_equal->current_level list).
3710
 
  If it fails, it continues searching in upper levels accessed
3711
 
  through a pointer cond_equal->upper_levels.
3712
 
  The search terminates as soon as a multiple equality containing 
3713
 
  the field is found. 
3714
 
 
3715
 
  @param cond_equal   reference to list of multiple equalities where
3716
 
                      the field (this object) is to be looked for
3717
 
 
3718
 
  @return
3719
 
    - First Item_equal containing the field, if success
3720
 
    - 0, otherwise
3721
 
*/
3722
 
 
3723
 
Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal)
3724
 
{
3725
 
  Item_equal *item= 0;
3726
 
  while (cond_equal)
3727
 
  {
3728
 
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
3729
 
    while ((item= li++))
3730
 
    {
3731
 
      if (item->contains(field))
3732
 
        return item;
3733
 
    }
3734
 
    /* 
3735
 
      The field is not found in any of the multiple equalities
3736
 
      of the current level. Look for it in upper levels
3737
 
    */
3738
 
    cond_equal= cond_equal->upper_levels;
3739
 
  }
3740
 
  return 0;
3741
 
}
3742
 
 
3743
 
 
3744
 
/**
3745
 
  Check whether a field can be substituted by an equal item.
3746
 
 
3747
 
  The function checks whether a substitution of the field
3748
 
  occurrence for an equal item is valid.
3749
 
 
3750
 
  @param arg   *arg != NULL <-> the field is in the context where
3751
 
               substitution for an equal item is valid
3752
 
 
3753
 
  @note
3754
 
    The following statement is not always true:
3755
 
  @n
3756
 
    x=y => F(x)=F(x/y).
3757
 
  @n
3758
 
    This means substitution of an item for an equal item not always
3759
 
    yields an equavalent condition. Here's an example:
3760
 
    @code
3761
 
    'a'='a '
3762
 
    (LENGTH('a')=1) != (LENGTH('a ')=2)
3763
 
  @endcode
3764
 
    Such a substitution is surely valid if either the substituted
3765
 
    field is not of a STRING type or if it is an argument of
3766
 
    a comparison predicate.
3767
 
 
3768
 
  @retval
3769
 
    true   substitution is valid
3770
 
  @retval
3771
 
    false  otherwise
3772
 
*/
3773
 
 
3774
 
bool Item_field::subst_argument_checker(unsigned char **arg)
3775
 
{
3776
 
  return (result_type() != STRING_RESULT) || (*arg);
3777
 
}
3778
 
 
3779
 
 
3780
 
/**
3781
 
  Set a pointer to the multiple equality the field reference belongs to
3782
 
  (if any).
3783
 
 
3784
 
  The function looks for a multiple equality containing the field item
3785
 
  among those referenced by arg.
3786
 
  In the case such equality exists the function does the following.
3787
 
  If the found multiple equality contains a constant, then the field
3788
 
  reference is substituted for this constant, otherwise it sets a pointer
3789
 
  to the multiple equality in the field item.
3790
 
 
3791
 
 
3792
 
  @param arg    reference to list of multiple equalities where
3793
 
                the field (this object) is to be looked for
3794
 
 
3795
 
  @note
3796
 
    This function is supposed to be called as a callback parameter in calls
3797
 
    of the compile method.
3798
 
 
3799
 
  @return
3800
 
    - pointer to the replacing constant item, if the field item was substituted
3801
 
    - pointer to the field item, otherwise.
3802
 
*/
3803
 
 
3804
 
Item *Item_field::equal_fields_propagator(unsigned char *arg)
3805
 
{
3806
 
  if (no_const_subst)
3807
 
    return this;
3808
 
  item_equal= find_item_equal((COND_EQUAL *) arg);
3809
 
  Item *item= 0;
3810
 
  if (item_equal)
3811
 
    item= item_equal->get_const();
3812
 
  /*
3813
 
    Disable const propagation for items used in different comparison contexts.
3814
 
    This must be done because, for example, Item_hex_string->val_int() is not
3815
 
    the same as (Item_hex_string->val_str() in BINARY column)->val_int().
3816
 
    We cannot simply disable the replacement in a particular context (
3817
 
    e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
3818
 
    Items don't know the context they are in and there are functions like 
3819
 
    IF (<hex_string>, 'yes', 'no').
3820
 
    The same problem occurs when comparing a DATE/TIME field with a
3821
 
    DATE/TIME represented as an int and as a string.
3822
 
  */
3823
 
  if (!item ||
3824
 
      (cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
3825
 
    item= this;
3826
 
 
3827
 
  return item;
3828
 
}
3829
 
 
3830
 
 
3831
 
/**
3832
 
  Mark the item to not be part of substitution if it's not a binary item.
3833
 
 
3834
 
  See comments in Arg_comparator::set_compare_func() for details.
3835
 
*/
3836
 
 
3837
 
bool Item_field::set_no_const_sub(unsigned char *arg __attribute__((unused)))
3838
 
{
3839
 
  if (field->charset() != &my_charset_bin)
3840
 
    no_const_subst=1;
3841
 
  return false;
3842
 
}
3843
 
 
3844
 
 
3845
 
/**
3846
 
  Replace an Item_field for an equal Item_field that evaluated earlier
3847
 
  (if any).
3848
 
 
3849
 
  The function returns a pointer to an item that is taken from
3850
 
  the very beginning of the item_equal list which the Item_field
3851
 
  object refers to (belongs to) unless item_equal contains  a constant
3852
 
  item. In this case the function returns this constant item, 
3853
 
  (if the substitution does not require conversion).   
3854
 
  If the Item_field object does not refer any Item_equal object
3855
 
  'this' is returned .
3856
 
 
3857
 
  @param arg   a dummy parameter, is not used here
3858
 
 
3859
 
 
3860
 
  @note
3861
 
    This function is supposed to be called as a callback parameter in calls
3862
 
    of the thransformer method.
3863
 
 
3864
 
  @return
3865
 
    - pointer to a replacement Item_field if there is a better equal item or
3866
 
      a pointer to a constant equal item;
3867
 
    - this - otherwise.
3868
 
*/
3869
 
 
3870
 
Item *Item_field::replace_equal_field(unsigned char *arg __attribute__((unused)))
3871
 
{
3872
 
  if (item_equal)
3873
 
  {
3874
 
    Item *const_item= item_equal->get_const();
3875
 
    if (const_item)
3876
 
    {
3877
 
      if (cmp_context != (Item_result)-1 &&
3878
 
          const_item->cmp_context != cmp_context)
3879
 
        return this;
3880
 
      return const_item;
3881
 
    }
3882
 
    Item_field *subst= item_equal->get_first();
3883
 
    if (subst && !field->eq(subst->field))
3884
 
      return subst;
3885
 
  }
3886
 
  return this;
3887
 
}
3888
 
 
3889
 
 
3890
1289
void Item::init_make_field(Send_field *tmp_field,
3891
1290
                           enum enum_field_types field_type_arg)
3892
1291
{
3897
1296
  tmp_field->table_name=        empty_name;
3898
1297
  tmp_field->col_name=          name;
3899
1298
  tmp_field->charsetnr=         collation.collation->number;
3900
 
  tmp_field->flags=             (maybe_null ? 0 : NOT_NULL_FLAG) | 
 
1299
  tmp_field->flags=             (maybe_null ? 0 : NOT_NULL_FLAG) |
3901
1300
                                (my_binary_compare(collation.collation) ?
3902
1301
                                 BINARY_FLAG : 0);
3903
1302
  tmp_field->type=              field_type_arg;
3904
1303
  tmp_field->length=max_length;
3905
1304
  tmp_field->decimals=decimals;
3906
 
  if (unsigned_flag)
3907
 
    tmp_field->flags |= UNSIGNED_FLAG;
3908
1305
}
3909
1306
 
3910
1307
void Item::make_field(Send_field *tmp_field)
3921
1318
  return f_type;
3922
1319
}
3923
1320
 
3924
 
 
3925
 
void Item_empty_string::make_field(Send_field *tmp_field)
3926
 
{
3927
 
  init_make_field(tmp_field, string_field_type());
3928
 
}
3929
 
 
3930
 
 
3931
1321
enum_field_types Item::field_type() const
3932
1322
{
3933
1323
  switch (result_type()) {
3947
1337
{
3948
1338
  switch (field_type())
3949
1339
  {
3950
 
    case DRIZZLE_TYPE_NEWDATE:
 
1340
    case DRIZZLE_TYPE_DATE:
3951
1341
    case DRIZZLE_TYPE_DATETIME:
3952
1342
    case DRIZZLE_TYPE_TIMESTAMP:
3953
1343
      return true;
3968
1358
                                       str->length(), &well_formed_error);
3969
1359
  if (wlen < str->length())
3970
1360
  {
3971
 
    THD *thd= current_thd;
 
1361
    Session *session= current_session;
3972
1362
    char hexbuf[7];
3973
1363
    enum DRIZZLE_ERROR::enum_warning_level level;
3974
1364
    uint32_t diff= str->length() - wlen;
3975
1365
    set_if_smaller(diff, 3);
3976
 
    octet2hex(hexbuf, str->ptr() + wlen, diff);
 
1366
    drizzleclient_drizzleclient_octet2hex(hexbuf, str->ptr() + wlen, diff);
3977
1367
    if (send_error)
3978
1368
    {
3979
1369
      my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
3980
1370
               cs->csname,  hexbuf);
3981
 
      return 0;
 
1371
      return NULL;
3982
1372
    }
3983
1373
    {
3984
1374
      level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
3985
1375
      null_value= 1;
3986
1376
      str= 0;
3987
1377
    }
3988
 
    push_warning_printf(thd, level, ER_INVALID_CHARACTER_STRING,
 
1378
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
3989
1379
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
3990
1380
  }
3991
1381
  return str;
3993
1383
 
3994
1384
/*
3995
1385
  Compare two items using a given collation
3996
 
  
 
1386
 
3997
1387
  SYNOPSIS
3998
1388
    eq_by_collation()
3999
1389
    item               item to compare with
4008
1398
    restored.
4009
1399
 
4010
1400
  RETURN
4011
 
    1    compared items has been detected as equal   
 
1401
    1    compared items has been detected as equal
4012
1402
    0    otherwise
4013
1403
*/
4014
1404
 
4032
1422
  if (save_item_cs)
4033
1423
    item->collation.collation= save_item_cs;
4034
1424
  return res;
4035
 
}  
 
1425
}
4036
1426
 
4037
1427
 
4038
1428
/**
4040
1430
 
4041
1431
  If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n
4042
1432
  If max_length > 0 create a varchar @n
4043
 
  If max_length == 0 create a CHAR(0) 
 
1433
  If max_length == 0 create a CHAR(0)
4044
1434
 
4045
1435
  @param table          Table for which the field is created
4046
1436
*/
4074
1464
    \#    Created field
4075
1465
*/
4076
1466
 
4077
 
Field *Item::tmp_table_field_from_field_type(Table *table, bool fixed_length __attribute__((unused)))
 
1467
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
4078
1468
{
4079
1469
  /*
4080
1470
    The field functions defines a field to be not null if null_ptr is not 0
4088
1478
                                 Field::NONE, name, decimals, 0,
4089
1479
                                 unsigned_flag);
4090
1480
    break;
4091
 
  case DRIZZLE_TYPE_TINY:
4092
 
    field= new Field_tiny((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4093
 
                          name, 0, unsigned_flag);
4094
 
    break;
4095
1481
  case DRIZZLE_TYPE_LONG:
4096
1482
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4097
1483
                          name, 0, unsigned_flag);
4108
1494
    field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
4109
1495
                          name, &my_charset_bin);
4110
1496
    break;
4111
 
  case DRIZZLE_TYPE_NEWDATE:
4112
 
    field= new Field_newdate(maybe_null, name, &my_charset_bin);
 
1497
  case DRIZZLE_TYPE_DATE:
 
1498
    field= new Field_date(maybe_null, name, &my_charset_bin);
4113
1499
    break;
4114
1500
  case DRIZZLE_TYPE_TIME:
4115
1501
    field= new Field_time(maybe_null, name, &my_charset_bin);
4141
1527
}
4142
1528
 
4143
1529
 
4144
 
/* ARGSUSED */
4145
 
void Item_field::make_field(Send_field *tmp_field)
4146
 
{
4147
 
  field->make_field(tmp_field);
4148
 
  assert(tmp_field->table_name != 0);
4149
 
  if (name)
4150
 
    tmp_field->col_name=name;                   // Use user supplied name
4151
 
  if (table_name)
4152
 
    tmp_field->table_name= table_name;
4153
 
  if (db_name)
4154
 
    tmp_field->db_name= db_name;
4155
 
}
4156
 
 
4157
 
 
4158
 
/**
4159
 
  Set a field's value from a item.
4160
 
*/
4161
 
 
4162
 
void Item_field::save_org_in_field(Field *to)
4163
 
{
4164
 
  if (field->is_null())
4165
 
  {
4166
 
    null_value=1;
4167
 
    set_field_to_null_with_conversions(to, 1);
4168
 
  }
4169
 
  else
4170
 
  {
4171
 
    to->set_notnull();
4172
 
    field_conv(to,field);
4173
 
    null_value=0;
4174
 
  }
4175
 
}
4176
 
 
4177
 
int Item_field::save_in_field(Field *to, bool no_conversions)
4178
 
{
4179
 
  int res;
4180
 
  if (result_field->is_null())
4181
 
  {
4182
 
    null_value=1;
4183
 
    res= set_field_to_null_with_conversions(to, no_conversions);
4184
 
  }
4185
 
  else
4186
 
  {
4187
 
    to->set_notnull();
4188
 
    res= field_conv(to,result_field);
4189
 
    null_value=0;
4190
 
  }
4191
 
  return(res);
4192
 
}
4193
 
 
4194
 
 
4195
 
/**
4196
 
  Store null in field.
4197
 
 
4198
 
  This is used on INSERT.
4199
 
  Allow NULL to be inserted in timestamp and auto_increment values.
4200
 
 
4201
 
  @param field          Field where we want to store NULL
4202
 
 
4203
 
  @retval
4204
 
    0   ok
4205
 
  @retval
4206
 
    1   Field doesn't support NULL values and can't handle 'field = NULL'
4207
 
*/
4208
 
 
4209
 
int Item_null::save_in_field(Field *field, bool no_conversions)
4210
 
{
4211
 
  return set_field_to_null_with_conversions(field, no_conversions);
4212
 
}
4213
 
 
4214
 
 
4215
 
/**
4216
 
  Store null in field.
4217
 
 
4218
 
  @param field          Field where we want to store NULL
4219
 
 
4220
 
  @retval
4221
 
    0    OK
4222
 
  @retval
4223
 
    1    Field doesn't support NULL values
4224
 
*/
4225
 
 
4226
 
int Item_null::save_safe_in_field(Field *field)
4227
 
{
4228
 
  return set_field_to_null(field);
4229
 
}
4230
 
 
4231
 
 
4232
1530
/*
4233
1531
  This implementation can lose str_value content, so if the
4234
1532
  Item uses str_value to store something, it should
4295
1593
}
4296
1594
 
4297
1595
 
4298
 
int Item_string::save_in_field(Field *field,
4299
 
                               bool no_conversions __attribute__((unused)))
4300
 
{
4301
 
  String *result;
4302
 
  result=val_str(&str_value);
4303
 
  return save_str_value_in_field(field, result);
4304
 
}
4305
 
 
4306
 
 
4307
 
int Item_uint::save_in_field(Field *field, bool no_conversions)
4308
 
{
4309
 
  /* Item_int::save_in_field handles both signed and unsigned. */
4310
 
  return Item_int::save_in_field(field, no_conversions);
4311
 
}
4312
 
 
4313
 
 
4314
 
int Item_int::save_in_field(Field *field,
4315
 
                            bool no_conversions __attribute__((unused)))
4316
 
{
4317
 
  int64_t nr=val_int();
4318
 
  if (null_value)
4319
 
    return set_field_to_null(field);
4320
 
  field->set_notnull();
4321
 
  return field->store(nr, unsigned_flag);
4322
 
}
4323
 
 
4324
 
 
4325
 
int Item_decimal::save_in_field(Field *field,
4326
 
                                bool no_conversions __attribute__((unused)))
4327
 
{
4328
 
  field->set_notnull();
4329
 
  return field->store_decimal(&decimal_value);
4330
 
}
4331
 
 
4332
 
 
4333
 
bool Item_int::eq(const Item *arg,
4334
 
                  bool binary_cmp __attribute__((unused))) const
4335
 
{
4336
 
  /* No need to check for null value as basic constant can't be NULL */
4337
 
  if (arg->basic_const_item() && arg->type() == type())
4338
 
  {
4339
 
    /*
4340
 
      We need to cast off const to call val_int(). This should be OK for
4341
 
      a basic constant.
4342
 
    */
4343
 
    Item *item= (Item*) arg;
4344
 
    return item->val_int() == value && item->unsigned_flag == unsigned_flag;
4345
 
  }
4346
 
  return false;
4347
 
}
4348
 
 
4349
 
 
4350
 
Item *Item_int_with_ref::clone_item()
4351
 
{
4352
 
  assert(ref->const_item());
4353
 
  /*
4354
 
    We need to evaluate the constant to make sure it works with
4355
 
    parameter markers.
4356
 
  */
4357
 
  return (ref->unsigned_flag ?
4358
 
          new Item_uint(ref->name, ref->val_int(), ref->max_length) :
4359
 
          new Item_int(ref->name, ref->val_int(), ref->max_length));
4360
 
}
4361
 
 
4362
 
 
4363
 
Item_num *Item_uint::neg()
4364
 
{
4365
 
  Item_decimal *item= new Item_decimal(value, 1);
4366
 
  return item->neg();
4367
 
}
4368
 
 
4369
 
 
4370
 
static uint32_t nr_of_decimals(const char *str, const char *end)
4371
 
{
4372
 
  const char *decimal_point;
4373
 
 
4374
 
  /* Find position for '.' */
4375
 
  for (;;)
4376
 
  {
4377
 
    if (str == end)
4378
 
      return 0;
4379
 
    if (*str == 'e' || *str == 'E')
4380
 
      return NOT_FIXED_DEC;    
4381
 
    if (*str++ == '.')
4382
 
      break;
4383
 
  }
4384
 
  decimal_point= str;
4385
 
  for (; my_isdigit(system_charset_info, *str) ; str++)
4386
 
    ;
4387
 
  if (*str == 'e' || *str == 'E')
4388
 
    return NOT_FIXED_DEC;
4389
 
  return (uint) (str - decimal_point);
4390
 
}
4391
 
 
4392
 
 
4393
 
/**
4394
 
  This function is only called during parsing. We will signal an error if
4395
 
  value is not a true double value (overflow)
4396
 
*/
4397
 
 
4398
 
Item_float::Item_float(const char *str_arg, uint32_t length)
4399
 
{
4400
 
  int error;
4401
 
  char *end_not_used;
4402
 
  value= my_strntod(&my_charset_bin, (char*) str_arg, length, &end_not_used,
4403
 
                    &error);
4404
 
  if (error)
4405
 
  {
4406
 
    /*
4407
 
      Note that we depend on that str_arg is null terminated, which is true
4408
 
      when we are in the parser
4409
 
    */
4410
 
    assert(str_arg[length] == 0);
4411
 
    my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", (char*) str_arg);
4412
 
  }
4413
 
  presentation= name=(char*) str_arg;
4414
 
  decimals=(uint8_t) nr_of_decimals(str_arg, str_arg+length);
4415
 
  max_length=length;
4416
 
  fixed= 1;
4417
 
}
4418
 
 
4419
 
 
4420
 
int Item_float::save_in_field(Field *field,
4421
 
                              bool no_conversions __attribute__((unused)))
4422
 
{
4423
 
  double nr= val_real();
4424
 
  if (null_value)
4425
 
    return set_field_to_null(field);
4426
 
  field->set_notnull();
4427
 
  return field->store(nr);
4428
 
}
4429
 
 
4430
 
 
4431
 
void Item_float::print(String *str,
4432
 
                       enum_query_type query_type __attribute__((unused)))
4433
 
{
4434
 
  if (presentation)
4435
 
  {
4436
 
    str->append(presentation);
4437
 
    return;
4438
 
  }
4439
 
  char buffer[20];
4440
 
  String num(buffer, sizeof(buffer), &my_charset_bin);
4441
 
  num.set_real(value, decimals, &my_charset_bin);
4442
 
  str->append(num);
4443
 
}
4444
 
 
4445
 
 
4446
 
/*
4447
 
  hex item
4448
 
  In string context this is a binary string.
4449
 
  In number context this is a int64_t value.
4450
 
*/
4451
 
 
4452
 
bool Item_float::eq(const Item *arg,
4453
 
                    bool binary_cmp __attribute__((unused))) const
4454
 
{
4455
 
  if (arg->basic_const_item() && arg->type() == type())
4456
 
  {
4457
 
    /*
4458
 
      We need to cast off const to call val_int(). This should be OK for
4459
 
      a basic constant.
4460
 
    */
4461
 
    Item *item= (Item*) arg;
4462
 
    return item->val_real() == value;
4463
 
  }
4464
 
  return false;
4465
 
}
4466
 
 
4467
 
 
4468
 
inline uint32_t char_val(char X)
4469
 
{
4470
 
  return (uint) (X >= '0' && X <= '9' ? X-'0' :
4471
 
                 X >= 'A' && X <= 'Z' ? X-'A'+10 :
4472
 
                 X-'a'+10);
4473
 
}
4474
 
 
4475
 
 
4476
 
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
4477
 
{
4478
 
  max_length=(str_length+1)/2;
4479
 
  char *ptr=(char*) sql_alloc(max_length+1);
4480
 
  if (!ptr)
4481
 
    return;
4482
 
  str_value.set(ptr,max_length,&my_charset_bin);
4483
 
  char *end=ptr+max_length;
4484
 
  if (max_length*2 != str_length)
4485
 
    *ptr++=char_val(*str++);                    // Not even, assume 0 prefix
4486
 
  while (ptr != end)
4487
 
  {
4488
 
    *ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
4489
 
    str+=2;
4490
 
  }
4491
 
  *ptr=0;                                       // Keep purify happy
4492
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
4493
 
  fixed= 1;
4494
 
  unsigned_flag= 1;
4495
 
}
4496
 
 
4497
 
int64_t Item_hex_string::val_int()
4498
 
{
4499
 
  // following assert is redundant, because fixed=1 assigned in constructor
4500
 
  assert(fixed == 1);
4501
 
  char *end=(char*) str_value.ptr()+str_value.length(),
4502
 
       *ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
4503
 
 
4504
 
  uint64_t value=0;
4505
 
  for (; ptr != end ; ptr++)
4506
 
    value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
4507
 
  return (int64_t) value;
4508
 
}
4509
 
 
4510
 
 
4511
 
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
4512
 
{
4513
 
  // following assert is redundant, because fixed=1 assigned in constructor
4514
 
  assert(fixed == 1);
4515
 
  uint64_t value= (uint64_t)val_int();
4516
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
4517
 
  return (decimal_value);
4518
 
}
4519
 
 
4520
 
 
4521
 
int Item_hex_string::save_in_field(Field *field,
4522
 
                                   bool no_conversions __attribute__((unused)))
4523
 
{
4524
 
  field->set_notnull();
4525
 
  if (field->result_type() == STRING_RESULT)
4526
 
    return field->store(str_value.ptr(), str_value.length(), 
4527
 
                        collation.collation);
4528
 
 
4529
 
  uint64_t nr;
4530
 
  uint32_t length= str_value.length();
4531
 
  if (length > 8)
4532
 
  {
4533
 
    nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
4534
 
    goto warn;
4535
 
  }
4536
 
  nr= (uint64_t) val_int();
4537
 
  if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
4538
 
  {
4539
 
    nr= INT64_MAX;
4540
 
    goto warn;
4541
 
  }
4542
 
  return field->store((int64_t) nr, true);  // Assume hex numbers are unsigned
4543
 
 
4544
 
warn:
4545
 
  if (!field->store((int64_t) nr, true))
4546
 
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
4547
 
                       1);
4548
 
  return 1;
4549
 
}
4550
 
 
4551
 
 
4552
 
void Item_hex_string::print(String *str,
4553
 
                            enum_query_type query_type __attribute__((unused)))
4554
 
{
4555
 
  char *end= (char*) str_value.ptr() + str_value.length(),
4556
 
       *ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
4557
 
  str->append("0x");
4558
 
  for (; ptr != end ; ptr++)
4559
 
  {
4560
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
4561
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
4562
 
  }
4563
 
}
4564
 
 
4565
 
 
4566
 
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
4567
 
{
4568
 
  if (arg->basic_const_item() && arg->type() == type())
4569
 
  {
4570
 
    if (binary_cmp)
4571
 
      return !stringcmp(&str_value, &arg->str_value);
4572
 
    return !sortcmp(&str_value, &arg->str_value, collation.collation);
4573
 
  }
4574
 
  return false;
4575
 
}
4576
 
 
4577
 
 
4578
 
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
4579
 
{
4580
 
  Item_string *conv;
4581
 
  String tmp, *str= val_str(&tmp);
4582
 
 
4583
 
  if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
4584
 
    return NULL;
4585
 
  conv->str_value.copy();
4586
 
  conv->str_value.mark_as_const();
4587
 
  return conv;
4588
 
}
4589
 
 
4590
 
 
4591
 
/*
4592
 
  bin item.
4593
 
  In string context this is a binary string.
4594
 
  In number context this is a int64_t value.
4595
 
*/
4596
 
  
4597
 
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
4598
 
{
4599
 
  const char *end= str + str_length - 1;
4600
 
  unsigned char bits= 0;
4601
 
  uint32_t power= 1;
4602
 
 
4603
 
  max_length= (str_length + 7) >> 3;
4604
 
  char *ptr= (char*) sql_alloc(max_length + 1);
4605
 
  if (!ptr)
4606
 
    return;
4607
 
  str_value.set(ptr, max_length, &my_charset_bin);
4608
 
  ptr+= max_length - 1;
4609
 
  ptr[1]= 0;                     // Set end null for string
4610
 
  for (; end >= str; end--)
4611
 
  {
4612
 
    if (power == 256)
4613
 
    {
4614
 
      power= 1;
4615
 
      *ptr--= bits;
4616
 
      bits= 0;     
4617
 
    }
4618
 
    if (*end == '1')
4619
 
      bits|= power; 
4620
 
    power<<= 1;
4621
 
  }
4622
 
  *ptr= (char) bits;
4623
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
4624
 
  fixed= 1;
4625
 
}
4626
 
 
4627
 
 
4628
 
/**
4629
 
  Pack data in buffer for sending.
4630
 
*/
4631
 
 
4632
 
bool Item_null::send(Protocol *protocol,
4633
 
                     String *packet __attribute__((unused)))
4634
 
{
4635
 
  return protocol->store_null();
4636
 
}
4637
 
 
4638
1596
/**
4639
1597
  This is only called from items that is not of type item_field.
4640
1598
*/
4657
1615
      result= protocol->store(res->ptr(),res->length(),res->charset());
4658
1616
    break;
4659
1617
  }
4660
 
  case DRIZZLE_TYPE_TINY:
4661
 
  {
4662
 
    int64_t nr;
4663
 
    nr= val_int();
4664
 
    if (!null_value)
4665
 
      result= protocol->store_tiny(nr);
4666
 
    break;
4667
 
  }
4668
1618
  case DRIZZLE_TYPE_LONG:
4669
1619
  {
4670
1620
    int64_t nr;
4695
1645
    get_date(&tm, TIME_FUZZY_DATE);
4696
1646
    if (!null_value)
4697
1647
    {
4698
 
      if (f_type == DRIZZLE_TYPE_NEWDATE)
 
1648
      if (f_type == DRIZZLE_TYPE_DATE)
4699
1649
        return protocol->store_date(&tm);
4700
1650
      else
4701
1651
        result= protocol->store(&tm);
4717
1667
}
4718
1668
 
4719
1669
 
4720
 
bool Item_field::send(Protocol *protocol,
4721
 
                      String *buffer __attribute__((unused)))
4722
 
{
4723
 
  return protocol->store(result_field);
4724
 
}
4725
 
 
4726
 
 
4727
 
void Item_field::update_null_value()
4728
 
{
4729
 
  /*
4730
 
    need to set no_errors to prevent warnings about type conversion
4731
 
    popping up.
4732
 
  */
4733
 
  THD *thd= field->table->in_use;
4734
 
  int no_errors;
4735
 
 
4736
 
  no_errors= thd->no_errors;
4737
 
  thd->no_errors= 1;
4738
 
  Item::update_null_value();
4739
 
  thd->no_errors= no_errors;
4740
 
}
4741
 
 
4742
 
 
4743
 
/*
4744
 
  Add the field to the select list and substitute it for the reference to
4745
 
  the field.
4746
 
 
4747
 
  SYNOPSIS
4748
 
    Item_field::update_value_transformer()
4749
 
    select_arg      current select
4750
 
 
4751
 
  DESCRIPTION
4752
 
    If the field doesn't belong to the table being inserted into then it is
4753
 
    added to the select list, pointer to it is stored in the ref_pointer_array
4754
 
    of the select and the field itself is substituted for the Item_ref object.
4755
 
    This is done in order to get correct values from update fields that
4756
 
    belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
4757
 
    UPDATE statement.
4758
 
 
4759
 
  RETURN
4760
 
    0             if error occured
4761
 
    ref           if all conditions are met
4762
 
    this field    otherwise
4763
 
*/
4764
 
 
4765
 
Item *Item_field::update_value_transformer(unsigned char *select_arg)
4766
 
{
4767
 
  SELECT_LEX *select= (SELECT_LEX*)select_arg;
4768
 
  assert(fixed);
4769
 
 
4770
 
  if (field->table != select->context.table_list->table)
4771
 
  {
4772
 
    List<Item> *all_fields= &select->join->all_fields;
4773
 
    Item **ref_pointer_array= select->ref_pointer_array;
4774
 
    int el= all_fields->elements;
4775
 
    Item_ref *ref;
4776
 
 
4777
 
    ref_pointer_array[el]= (Item*)this;
4778
 
    all_fields->push_front((Item*)this);
4779
 
    ref= new Item_ref(&select->context, ref_pointer_array + el,
4780
 
                      table_name, field_name);
4781
 
    return ref;
4782
 
  }
4783
 
  return this;
4784
 
}
4785
 
 
4786
 
 
4787
 
void Item_field::print(String *str, enum_query_type query_type)
4788
 
{
4789
 
  if (field && field->table->const_table)
4790
 
  {
4791
 
    char buff[MAX_FIELD_WIDTH];
4792
 
    String tmp(buff,sizeof(buff),str->charset());
4793
 
    field->val_str(&tmp);
4794
 
    str->append('\'');
4795
 
    str->append(tmp);
4796
 
    str->append('\'');
4797
 
    return;
4798
 
  }
4799
 
  Item_ident::print(str, query_type);
4800
 
}
4801
 
 
4802
 
 
4803
 
Item_ref::Item_ref(Name_resolution_context *context_arg,
4804
 
                   Item **item, const char *table_name_arg,
4805
 
                   const char *field_name_arg,
4806
 
                   bool alias_name_used_arg)
4807
 
  :Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
4808
 
   result_field(0), ref(item)
4809
 
{
4810
 
  alias_name_used= alias_name_used_arg;
4811
 
  /*
4812
 
    This constructor used to create some internals references over fixed items
4813
 
  */
4814
 
  if (ref && *ref && (*ref)->fixed)
4815
 
    set_properties();
4816
 
}
4817
 
 
4818
 
 
4819
 
/**
4820
 
  Resolve the name of a reference to a column reference.
4821
 
 
4822
 
  The method resolves the column reference represented by 'this' as a column
4823
 
  present in one of: GROUP BY clause, SELECT clause, outer queries. It is
4824
 
  used typically for columns in the HAVING clause which are not under
4825
 
  aggregate functions.
4826
 
 
4827
 
  POSTCONDITION @n
4828
 
  Item_ref::ref is 0 or points to a valid item.
4829
 
 
4830
 
  @note
4831
 
    The name resolution algorithm used is (where [T_j] is an optional table
4832
 
    name that qualifies the column name):
4833
 
 
4834
 
  @code
4835
 
        resolve_extended([T_j].col_ref_i)
4836
 
        {
4837
 
          Search for a column or derived column named col_ref_i [in table T_j]
4838
 
          in the SELECT and GROUP clauses of Q.
4839
 
 
4840
 
          if such a column is NOT found AND    // Lookup in outer queries.
4841
 
             there are outer queries
4842
 
          {
4843
 
            for each outer query Q_k beginning from the inner-most one
4844
 
           {
4845
 
              Search for a column or derived column named col_ref_i
4846
 
              [in table T_j] in the SELECT and GROUP clauses of Q_k.
4847
 
 
4848
 
              if such a column is not found AND
4849
 
                 - Q_k is not a group query AND
4850
 
                 - Q_k is not inside an aggregate function
4851
 
                 OR
4852
 
                 - Q_(k-1) is not in a HAVING or SELECT clause of Q_k
4853
 
              {
4854
 
                search for a column or derived column named col_ref_i
4855
 
                [in table T_j] in the FROM clause of Q_k;
4856
 
              }
4857
 
            }
4858
 
          }
4859
 
        }
4860
 
  @endcode
4861
 
  @n
4862
 
    This procedure treats GROUP BY and SELECT clauses as one namespace for
4863
 
    column references in HAVING. Notice that compared to
4864
 
    Item_field::fix_fields, here we first search the SELECT and GROUP BY
4865
 
    clauses, and then we search the FROM clause.
4866
 
 
4867
 
  @param[in]     thd        current thread
4868
 
  @param[in,out] reference  view column if this item was resolved to a
4869
 
    view column
4870
 
 
4871
 
  @todo
4872
 
    Here we could first find the field anyway, and then test this
4873
 
    condition, so that we can give a better error message -
4874
 
    ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4875
 
    ER_BAD_FIELD_ERROR which we produce now.
4876
 
 
4877
 
  @retval
4878
 
    true  if error
4879
 
  @retval
4880
 
    false on success
4881
 
*/
4882
 
 
4883
 
bool Item_ref::fix_fields(THD *thd, Item **reference)
4884
 
{
4885
 
  enum_parsing_place place= NO_MATTER;
4886
 
  assert(fixed == 0);
4887
 
  SELECT_LEX *current_sel= thd->lex->current_select;
4888
 
 
4889
 
  if (!ref || ref == not_found_item)
4890
 
  {
4891
 
    if (!(ref= resolve_ref_in_select_and_group(thd, this,
4892
 
                                               context->select_lex)))
4893
 
      goto error;             /* Some error occurred (e.g. ambiguous names). */
4894
 
 
4895
 
    if (ref == not_found_item) /* This reference was not resolved. */
4896
 
    {
4897
 
      Name_resolution_context *last_checked_context= context;
4898
 
      Name_resolution_context *outer_context= context->outer_context;
4899
 
      Field *from_field;
4900
 
      ref= 0;
4901
 
 
4902
 
      if (!outer_context)
4903
 
      {
4904
 
        /* The current reference cannot be resolved in this query. */
4905
 
        my_error(ER_BAD_FIELD_ERROR,MYF(0),
4906
 
                 this->full_name(), current_thd->where);
4907
 
        goto error;
4908
 
      }
4909
 
 
4910
 
      /*
4911
 
        If there is an outer context (select), and it is not a derived table
4912
 
        (which do not support the use of outer fields for now), try to
4913
 
        resolve this reference in the outer select(s).
4914
 
 
4915
 
        We treat each subselect as a separate namespace, so that different
4916
 
        subselects may contain columns with the same names. The subselects are
4917
 
        searched starting from the innermost.
4918
 
      */
4919
 
      from_field= (Field*) not_found_field;
4920
 
 
4921
 
      do
4922
 
      {
4923
 
        SELECT_LEX *select= outer_context->select_lex;
4924
 
        Item_subselect *prev_subselect_item=
4925
 
          last_checked_context->select_lex->master_unit()->item;
4926
 
        last_checked_context= outer_context;
4927
 
 
4928
 
        /* Search in the SELECT and GROUP lists of the outer select. */
4929
 
        if (outer_context->resolve_in_select_list)
4930
 
        {
4931
 
          if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
4932
 
            goto error; /* Some error occurred (e.g. ambiguous names). */
4933
 
          if (ref != not_found_item)
4934
 
          {
4935
 
            assert(*ref && (*ref)->fixed);
4936
 
            prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
4937
 
            prev_subselect_item->const_item_cache&= (*ref)->const_item();
4938
 
            break;
4939
 
          }
4940
 
          /*
4941
 
            Set ref to 0 to ensure that we get an error in case we replaced
4942
 
            this item with another item and still use this item in some
4943
 
            other place of the parse tree.
4944
 
          */
4945
 
          ref= 0;
4946
 
        }
4947
 
 
4948
 
        place= prev_subselect_item->parsing_place;
4949
 
        /*
4950
 
          Check table fields only if the subquery is used somewhere out of
4951
 
          HAVING or the outer SELECT does not use grouping (i.e. tables are
4952
 
          accessible).
4953
 
          TODO:
4954
 
          Here we could first find the field anyway, and then test this
4955
 
          condition, so that we can give a better error message -
4956
 
          ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4957
 
          ER_BAD_FIELD_ERROR which we produce now.
4958
 
        */
4959
 
        if ((place != IN_HAVING ||
4960
 
             (!select->with_sum_func &&
4961
 
              select->group_list.elements == 0)))
4962
 
        {
4963
 
          /*
4964
 
            In case of view, find_field_in_tables() write pointer to view
4965
 
            field expression to 'reference', i.e. it substitute that
4966
 
            expression instead of this Item_ref
4967
 
          */
4968
 
          from_field= find_field_in_tables(thd, this,
4969
 
                                           outer_context->
4970
 
                                             first_name_resolution_table,
4971
 
                                           outer_context->
4972
 
                                             last_name_resolution_table,
4973
 
                                           reference,
4974
 
                                           IGNORE_EXCEPT_NON_UNIQUE,
4975
 
                                           true, true);
4976
 
          if (! from_field)
4977
 
            goto error;
4978
 
          if (from_field == view_ref_found)
4979
 
          {
4980
 
            Item::Type refer_type= (*reference)->type();
4981
 
            prev_subselect_item->used_tables_cache|=
4982
 
              (*reference)->used_tables();
4983
 
            prev_subselect_item->const_item_cache&=
4984
 
              (*reference)->const_item();
4985
 
            assert((*reference)->type() == REF_ITEM);
4986
 
            mark_as_dependent(thd, last_checked_context->select_lex,
4987
 
                              context->select_lex, this,
4988
 
                              ((refer_type == REF_ITEM ||
4989
 
                                refer_type == FIELD_ITEM) ?
4990
 
                               (Item_ident*) (*reference) :
4991
 
                               0));
4992
 
            /*
4993
 
              view reference found, we substituted it instead of this
4994
 
              Item, so can quit
4995
 
            */
4996
 
            return false;
4997
 
          }
4998
 
          if (from_field != not_found_field)
4999
 
          {
5000
 
            if (cached_table && cached_table->select_lex &&
5001
 
                outer_context->select_lex &&
5002
 
                cached_table->select_lex != outer_context->select_lex)
5003
 
            {
5004
 
              /*
5005
 
                Due to cache, find_field_in_tables() can return field which
5006
 
                doesn't belong to provided outer_context. In this case we have
5007
 
                to find proper field context in order to fix field correcly.
5008
 
              */
5009
 
              do
5010
 
              {
5011
 
                outer_context= outer_context->outer_context;
5012
 
                select= outer_context->select_lex;
5013
 
                prev_subselect_item=
5014
 
                  last_checked_context->select_lex->master_unit()->item;
5015
 
                last_checked_context= outer_context;
5016
 
              } while (outer_context && outer_context->select_lex &&
5017
 
                       cached_table->select_lex != outer_context->select_lex);
5018
 
            }
5019
 
            prev_subselect_item->used_tables_cache|= from_field->table->map;
5020
 
            prev_subselect_item->const_item_cache= 0;
5021
 
            break;
5022
 
          }
5023
 
        }
5024
 
        assert(from_field == not_found_field);
5025
 
 
5026
 
        /* Reference is not found => depend on outer (or just error). */
5027
 
        prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
5028
 
        prev_subselect_item->const_item_cache= 0;
5029
 
 
5030
 
        outer_context= outer_context->outer_context;
5031
 
      } while (outer_context);
5032
 
 
5033
 
      assert(from_field != 0 && from_field != view_ref_found);
5034
 
      if (from_field != not_found_field)
5035
 
      {
5036
 
        Item_field* fld;
5037
 
        if (!(fld= new Item_field(from_field)))
5038
 
          goto error;
5039
 
        thd->change_item_tree(reference, fld);
5040
 
        mark_as_dependent(thd, last_checked_context->select_lex,
5041
 
                          thd->lex->current_select, this, fld);
5042
 
        /*
5043
 
          A reference is resolved to a nest level that's outer or the same as
5044
 
          the nest level of the enclosing set function : adjust the value of
5045
 
          max_arg_level for the function if it's needed.
5046
 
        */
5047
 
        if (thd->lex->in_sum_func &&
5048
 
            thd->lex->in_sum_func->nest_level >= 
5049
 
            last_checked_context->select_lex->nest_level)
5050
 
          set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5051
 
                        last_checked_context->select_lex->nest_level);
5052
 
        return false;
5053
 
      }
5054
 
      if (ref == 0)
5055
 
      {
5056
 
        /* The item was not a table field and not a reference */
5057
 
        my_error(ER_BAD_FIELD_ERROR, MYF(0),
5058
 
                 this->full_name(), current_thd->where);
5059
 
        goto error;
5060
 
      }
5061
 
      /* Should be checked in resolve_ref_in_select_and_group(). */
5062
 
      assert(*ref && (*ref)->fixed);
5063
 
      mark_as_dependent(thd, last_checked_context->select_lex,
5064
 
                        context->select_lex, this, this);
5065
 
      /*
5066
 
        A reference is resolved to a nest level that's outer or the same as
5067
 
        the nest level of the enclosing set function : adjust the value of
5068
 
        max_arg_level for the function if it's needed.
5069
 
      */
5070
 
      if (thd->lex->in_sum_func &&
5071
 
          thd->lex->in_sum_func->nest_level >= 
5072
 
          last_checked_context->select_lex->nest_level)
5073
 
        set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5074
 
                      last_checked_context->select_lex->nest_level);
5075
 
    }
5076
 
  }
5077
 
 
5078
 
  assert(*ref);
5079
 
  /*
5080
 
    Check if this is an incorrect reference in a group function or forward
5081
 
    reference. Do not issue an error if this is:
5082
 
      1. outer reference (will be fixed later by the fix_inner_refs function);
5083
 
      2. an unnamed reference inside an aggregate function.
5084
 
  */
5085
 
  if (!((*ref)->type() == REF_ITEM &&
5086
 
       ((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
5087
 
      (((*ref)->with_sum_func && name &&
5088
 
        !(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
5089
 
          current_sel->having_fix_field)) ||
5090
 
       !(*ref)->fixed))
5091
 
  {
5092
 
    my_error(ER_ILLEGAL_REFERENCE, MYF(0),
5093
 
             name, ((*ref)->with_sum_func?
5094
 
                    "reference to group function":
5095
 
                    "forward reference in item list"));
5096
 
    goto error;
5097
 
  }
5098
 
 
5099
 
  set_properties();
5100
 
 
5101
 
  if ((*ref)->check_cols(1))
5102
 
    goto error;
5103
 
  return false;
5104
 
 
5105
 
error:
5106
 
  context->process_error(thd);
5107
 
  return true;
5108
 
}
5109
 
 
5110
 
 
5111
 
void Item_ref::set_properties()
5112
 
{
5113
 
  max_length= (*ref)->max_length;
5114
 
  maybe_null= (*ref)->maybe_null;
5115
 
  decimals=   (*ref)->decimals;
5116
 
  collation.set((*ref)->collation);
5117
 
  /*
5118
 
    We have to remember if we refer to a sum function, to ensure that
5119
 
    split_sum_func() doesn't try to change the reference.
5120
 
  */
5121
 
  with_sum_func= (*ref)->with_sum_func;
5122
 
  unsigned_flag= (*ref)->unsigned_flag;
5123
 
  fixed= 1;
5124
 
  if (alias_name_used)
5125
 
    return;
5126
 
  if ((*ref)->type() == FIELD_ITEM)
5127
 
    alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
5128
 
  else
5129
 
    alias_name_used= true; // it is not field, so it is was resolved by alias
5130
 
}
5131
 
 
5132
 
 
5133
 
void Item_ref::cleanup()
5134
 
{
5135
 
  Item_ident::cleanup();
5136
 
  result_field= 0;
5137
 
  return;
5138
 
}
5139
 
 
5140
 
 
5141
 
void Item_ref::print(String *str, enum_query_type query_type)
5142
 
{
5143
 
  if (ref)
5144
 
  {
5145
 
    if ((*ref)->type() != Item::CACHE_ITEM && ref_type() != VIEW_REF &&
5146
 
        !table_name && name && alias_name_used)
5147
 
    {
5148
 
      THD *thd= current_thd;
5149
 
      append_identifier(thd, str, name, (uint) strlen(name));
5150
 
    }
5151
 
    else
5152
 
      (*ref)->print(str, query_type);
5153
 
  }
5154
 
  else
5155
 
    Item_ident::print(str, query_type);
5156
 
}
5157
 
 
5158
 
 
5159
 
bool Item_ref::send(Protocol *prot, String *tmp)
5160
 
{
5161
 
  if (result_field)
5162
 
    return prot->store(result_field);
5163
 
  return (*ref)->send(prot, tmp);
5164
 
}
5165
 
 
5166
 
 
5167
 
double Item_ref::val_result()
5168
 
{
5169
 
  if (result_field)
5170
 
  {
5171
 
    if ((null_value= result_field->is_null()))
5172
 
      return 0.0;
5173
 
    return result_field->val_real();
5174
 
  }
5175
 
  return val_real();
5176
 
}
5177
 
 
5178
 
 
5179
 
int64_t Item_ref::val_int_result()
5180
 
{
5181
 
  if (result_field)
5182
 
  {
5183
 
    if ((null_value= result_field->is_null()))
5184
 
      return 0;
5185
 
    return result_field->val_int();
5186
 
  }
5187
 
  return val_int();
5188
 
}
5189
 
 
5190
 
 
5191
 
String *Item_ref::str_result(String* str)
5192
 
{
5193
 
  if (result_field)
5194
 
  {
5195
 
    if ((null_value= result_field->is_null()))
5196
 
      return 0;
5197
 
    str->set_charset(str_value.charset());
5198
 
    return result_field->val_str(str, &str_value);
5199
 
  }
5200
 
  return val_str(str);
5201
 
}
5202
 
 
5203
 
 
5204
 
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
5205
 
{
5206
 
  if (result_field)
5207
 
  {
5208
 
    if ((null_value= result_field->is_null()))
5209
 
      return 0;
5210
 
    return result_field->val_decimal(decimal_value);
5211
 
  }
5212
 
  return val_decimal(decimal_value);
5213
 
}
5214
 
 
5215
 
 
5216
 
bool Item_ref::val_bool_result()
5217
 
{
5218
 
  if (result_field)
5219
 
  {
5220
 
    if ((null_value= result_field->is_null()))
5221
 
      return 0;
5222
 
    switch (result_field->result_type()) {
5223
 
    case INT_RESULT:
5224
 
      return result_field->val_int() != 0;
5225
 
    case DECIMAL_RESULT:
5226
 
    {
5227
 
      my_decimal decimal_value;
5228
 
      my_decimal *val= result_field->val_decimal(&decimal_value);
5229
 
      if (val)
5230
 
        return !my_decimal_is_zero(val);
5231
 
      return 0;
5232
 
    }
5233
 
    case REAL_RESULT:
5234
 
    case STRING_RESULT:
5235
 
      return result_field->val_real() != 0.0;
5236
 
    case ROW_RESULT:
5237
 
    default:
5238
 
      assert(0);
5239
 
    }
5240
 
  }
5241
 
  return val_bool();
5242
 
}
5243
 
 
5244
 
 
5245
 
double Item_ref::val_real()
5246
 
{
5247
 
  assert(fixed);
5248
 
  double tmp=(*ref)->val_result();
5249
 
  null_value=(*ref)->null_value;
5250
 
  return tmp;
5251
 
}
5252
 
 
5253
 
 
5254
 
int64_t Item_ref::val_int()
5255
 
{
5256
 
  assert(fixed);
5257
 
  int64_t tmp=(*ref)->val_int_result();
5258
 
  null_value=(*ref)->null_value;
5259
 
  return tmp;
5260
 
}
5261
 
 
5262
 
 
5263
 
bool Item_ref::val_bool()
5264
 
{
5265
 
  assert(fixed);
5266
 
  bool tmp= (*ref)->val_bool_result();
5267
 
  null_value= (*ref)->null_value;
5268
 
  return tmp;
5269
 
}
5270
 
 
5271
 
 
5272
 
String *Item_ref::val_str(String* tmp)
5273
 
{
5274
 
  assert(fixed);
5275
 
  tmp=(*ref)->str_result(tmp);
5276
 
  null_value=(*ref)->null_value;
5277
 
  return tmp;
5278
 
}
5279
 
 
5280
 
 
5281
 
bool Item_ref::is_null()
5282
 
{
5283
 
  assert(fixed);
5284
 
  return (*ref)->is_null();
5285
 
}
5286
 
 
5287
 
 
5288
 
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
5289
 
{
5290
 
  return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5291
 
}
5292
 
 
5293
 
 
5294
 
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
5295
 
{
5296
 
  my_decimal *val= (*ref)->val_decimal_result(decimal_value);
5297
 
  null_value= (*ref)->null_value;
5298
 
  return val;
5299
 
}
5300
 
 
5301
 
int Item_ref::save_in_field(Field *to, bool no_conversions)
5302
 
{
5303
 
  int res;
5304
 
  assert(!result_field);
5305
 
  res= (*ref)->save_in_field(to, no_conversions);
5306
 
  null_value= (*ref)->null_value;
5307
 
  return res;
5308
 
}
5309
 
 
5310
 
 
5311
 
void Item_ref::save_org_in_field(Field *field)
5312
 
{
5313
 
  (*ref)->save_org_in_field(field);
5314
 
}
5315
 
 
5316
 
 
5317
 
void Item_ref::make_field(Send_field *field)
5318
 
{
5319
 
  (*ref)->make_field(field);
5320
 
  /* Non-zero in case of a view */
5321
 
  if (name)
5322
 
    field->col_name= name;
5323
 
  if (table_name)
5324
 
    field->table_name= table_name;
5325
 
  if (db_name)
5326
 
    field->db_name= db_name;
5327
 
}
5328
 
 
5329
 
 
5330
 
Item *Item_ref::get_tmp_table_item(THD *thd)
5331
 
{
5332
 
  if (!result_field)
5333
 
    return (*ref)->get_tmp_table_item(thd);
5334
 
 
5335
 
  Item_field *item= new Item_field(result_field);
5336
 
  if (item)
5337
 
  {
5338
 
    item->table_name= table_name;
5339
 
    item->db_name= db_name;
5340
 
  }
5341
 
  return item;
5342
 
}
5343
 
 
5344
 
 
5345
 
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
5346
 
{
5347
 
  str->append(STRING_WITH_LEN("<ref_null_helper>("));
5348
 
  if (ref)
5349
 
    (*ref)->print(str, query_type);
5350
 
  else
5351
 
    str->append('?');
5352
 
  str->append(')');
5353
 
}
5354
 
 
5355
 
 
5356
 
double Item_direct_ref::val_real()
5357
 
{
5358
 
  double tmp=(*ref)->val_real();
5359
 
  null_value=(*ref)->null_value;
5360
 
  return tmp;
5361
 
}
5362
 
 
5363
 
 
5364
 
int64_t Item_direct_ref::val_int()
5365
 
{
5366
 
  int64_t tmp=(*ref)->val_int();
5367
 
  null_value=(*ref)->null_value;
5368
 
  return tmp;
5369
 
}
5370
 
 
5371
 
 
5372
 
String *Item_direct_ref::val_str(String* tmp)
5373
 
{
5374
 
  tmp=(*ref)->val_str(tmp);
5375
 
  null_value=(*ref)->null_value;
5376
 
  return tmp;
5377
 
}
5378
 
 
5379
 
 
5380
 
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
5381
 
{
5382
 
  my_decimal *tmp= (*ref)->val_decimal(decimal_value);
5383
 
  null_value=(*ref)->null_value;
5384
 
  return tmp;
5385
 
}
5386
 
 
5387
 
 
5388
 
bool Item_direct_ref::val_bool()
5389
 
{
5390
 
  bool tmp= (*ref)->val_bool();
5391
 
  null_value=(*ref)->null_value;
5392
 
  return tmp;
5393
 
}
5394
 
 
5395
 
 
5396
 
bool Item_direct_ref::is_null()
5397
 
{
5398
 
  return (*ref)->is_null();
5399
 
}
5400
 
 
5401
 
 
5402
 
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
5403
 
{
5404
 
  return (null_value=(*ref)->get_date(ltime,fuzzydate));
5405
 
}
5406
 
 
5407
 
 
5408
 
/**
5409
 
  Prepare referenced field then call usual Item_direct_ref::fix_fields .
5410
 
 
5411
 
  @param thd         thread handler
5412
 
  @param reference   reference on reference where this item stored
5413
 
 
5414
 
  @retval
5415
 
    false   OK
5416
 
  @retval
5417
 
    true    Error
5418
 
*/
5419
 
 
5420
 
bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference)
5421
 
{
5422
 
  /* view fild reference must be defined */
5423
 
  assert(*ref);
5424
 
  /* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */
5425
 
  if (!(*ref)->fixed &&
5426
 
      ((*ref)->fix_fields(thd, ref)))
5427
 
    return true;
5428
 
  return Item_direct_ref::fix_fields(thd, reference);
5429
 
}
5430
 
 
5431
 
/*
5432
 
  Prepare referenced outer field then call usual Item_direct_ref::fix_fields
5433
 
 
5434
 
  SYNOPSIS
5435
 
    Item_outer_ref::fix_fields()
5436
 
    thd         thread handler
5437
 
    reference   reference on reference where this item stored
5438
 
 
5439
 
  RETURN
5440
 
    false   OK
5441
 
    true    Error
5442
 
*/
5443
 
 
5444
 
bool Item_outer_ref::fix_fields(THD *thd, Item **reference)
5445
 
{
5446
 
  bool err;
5447
 
  /* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5448
 
  if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
5449
 
    return true;
5450
 
  err= Item_direct_ref::fix_fields(thd, reference);
5451
 
  if (!outer_ref)
5452
 
    outer_ref= *ref;
5453
 
  if ((*ref)->type() == Item::FIELD_ITEM)
5454
 
    table_name= ((Item_field*)outer_ref)->table_name;
5455
 
  return err;
5456
 
}
5457
 
 
5458
 
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
5459
 
{
5460
 
  if (depended_from == new_parent)
5461
 
  {
5462
 
    *ref= outer_ref;
5463
 
    outer_ref->fix_after_pullout(new_parent, ref);
5464
 
  }
5465
 
}
5466
 
 
5467
 
void Item_ref::fix_after_pullout(st_select_lex *new_parent,
5468
 
                                 Item **refptr __attribute__((unused)))
5469
 
{
5470
 
  if (depended_from == new_parent)
5471
 
  {
5472
 
    (*ref)->fix_after_pullout(new_parent, ref);
5473
 
    depended_from= NULL;
5474
 
  }
5475
 
}
5476
 
 
5477
 
/**
5478
 
  Compare two view column references for equality.
5479
 
 
5480
 
  A view column reference is considered equal to another column
5481
 
  reference if the second one is a view column and if both column
5482
 
  references resolve to the same item. It is assumed that both
5483
 
  items are of the same type.
5484
 
 
5485
 
  @param item        item to compare with
5486
 
  @param binary_cmp  make binary comparison
5487
 
 
5488
 
  @retval
5489
 
    true    Referenced item is equal to given item
5490
 
  @retval
5491
 
    false   otherwise
5492
 
*/
5493
 
 
5494
 
bool Item_direct_view_ref::eq(const Item *item,
5495
 
                              bool binary_cmp __attribute__((unused))) const
5496
 
{
5497
 
  if (item->type() == REF_ITEM)
5498
 
  {
5499
 
    Item_ref *item_ref= (Item_ref*) item;
5500
 
    if (item_ref->ref_type() == VIEW_REF)
5501
 
    {
5502
 
      Item *item_ref_ref= *(item_ref->ref);
5503
 
      return ((*ref)->real_item() == item_ref_ref->real_item());
5504
 
    }
5505
 
  }
5506
 
  return false;
5507
 
}
5508
 
 
5509
1670
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
5510
1671
{
5511
1672
  return item->type() == DEFAULT_VALUE_ITEM &&
5513
1674
}
5514
1675
 
5515
1676
 
5516
 
bool Item_default_value::fix_fields(THD *thd,
5517
 
                                    Item **items __attribute__((unused)))
 
1677
bool Item_default_value::fix_fields(Session *session, Item **)
5518
1678
{
5519
1679
  Item *real_arg;
5520
1680
  Item_field *field_arg;
5526
1686
    fixed= 1;
5527
1687
    return false;
5528
1688
  }
5529
 
  if (!arg->fixed && arg->fix_fields(thd, &arg))
 
1689
  if (!arg->fixed && arg->fix_fields(session, &arg))
5530
1690
    goto error;
5531
1691
 
5532
1692
 
5553
1713
  return false;
5554
1714
 
5555
1715
error:
5556
 
  context->process_error(thd);
 
1716
  context->process_error(session);
5557
1717
  return true;
5558
1718
}
5559
1719
 
5603
1763
/**
5604
1764
  This method like the walk method traverses the item tree, but at the
5605
1765
  same time it can replace some nodes in the tree.
5606
 
*/ 
 
1766
*/
5607
1767
 
5608
1768
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
5609
1769
{
5610
1770
  Item *new_item= arg->transform(transformer, args);
5611
1771
  if (!new_item)
5612
 
    return 0;
 
1772
    return NULL;
5613
1773
 
5614
1774
  /*
5615
 
    THD::change_item_tree() should be called only if the tree was
 
1775
    Session::change_item_tree() should be called only if the tree was
5616
1776
    really transformed, i.e. when a new item has been created.
5617
1777
    Otherwise we'll be allocating a lot of unnecessary memory for
5618
1778
    change records at each execution.
5619
1779
  */
5620
1780
  if (arg != new_item)
5621
 
    current_thd->change_item_tree(&arg, new_item);
 
1781
    current_session->change_item_tree(&arg, new_item);
5622
1782
  return (this->*transformer)(args);
5623
1783
}
5624
1784
 
5625
 
 
5626
 
bool Item_insert_value::eq(const Item *item, bool binary_cmp) const
5627
 
{
5628
 
  return item->type() == INSERT_VALUE_ITEM &&
5629
 
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
5630
 
}
5631
 
 
5632
 
 
5633
 
bool Item_insert_value::fix_fields(THD *thd,
5634
 
                                   Item **items __attribute__((unused)))
5635
 
{
5636
 
  assert(fixed == 0);
5637
 
  /* We should only check that arg is in first table */
5638
 
  if (!arg->fixed)
5639
 
  {
5640
 
    bool res;
5641
 
    TableList *orig_next_table= context->last_name_resolution_table;
5642
 
    context->last_name_resolution_table= context->first_name_resolution_table;
5643
 
    res= arg->fix_fields(thd, &arg);
5644
 
    context->last_name_resolution_table= orig_next_table;
5645
 
    if (res)
5646
 
      return true;
5647
 
  }
5648
 
 
5649
 
  if (arg->type() == REF_ITEM)
5650
 
  {
5651
 
    Item_ref *ref= (Item_ref *)arg;
5652
 
    if (ref->ref[0]->type() != FIELD_ITEM)
5653
 
    {
5654
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
5655
 
      return true;
5656
 
    }
5657
 
    arg= ref->ref[0];
5658
 
  }
5659
 
  /*
5660
 
    According to our SQL grammar, VALUES() function can reference
5661
 
    only to a column.
5662
 
  */
5663
 
  assert(arg->type() == FIELD_ITEM);
5664
 
 
5665
 
  Item_field *field_arg= (Item_field *)arg;
5666
 
 
5667
 
  if (field_arg->field->table->insert_values)
5668
 
  {
5669
 
    Field *def_field= (Field*) sql_alloc(field_arg->field->size_of());
5670
 
    if (!def_field)
5671
 
      return true;
5672
 
    memcpy(def_field, field_arg->field, field_arg->field->size_of());
5673
 
    def_field->move_field_offset((my_ptrdiff_t)
5674
 
                                 (def_field->table->insert_values -
5675
 
                                  def_field->table->record[0]));
5676
 
    set_field(def_field);
5677
 
  }
5678
 
  else
5679
 
  {
5680
 
    Field *tmp_field= field_arg->field;
5681
 
    /* charset doesn't matter here, it's to avoid sigsegv only */
5682
 
    tmp_field= new Field_null(0, 0, Field::NONE, field_arg->field->field_name,
5683
 
                          &my_charset_bin);
5684
 
    if (tmp_field)
5685
 
    {
5686
 
      tmp_field->init(field_arg->field->table);
5687
 
      set_field(tmp_field);
5688
 
    }
5689
 
  }
5690
 
  return false;
5691
 
}
5692
 
 
5693
 
void Item_insert_value::print(String *str, enum_query_type query_type)
5694
 
{
5695
 
  str->append(STRING_WITH_LEN("values("));
5696
 
  arg->print(str, query_type);
5697
 
  str->append(')');
5698
 
}
5699
 
 
5700
 
 
5701
1785
Item_result item_cmp_type(Item_result a,Item_result b)
5702
1786
{
5703
1787
  if (a == STRING_RESULT && b == STRING_RESULT)
5713
1797
}
5714
1798
 
5715
1799
 
5716
 
void resolve_const_item(THD *thd, Item **ref, Item *comp_item)
 
1800
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
5717
1801
{
5718
1802
  Item *item= *ref;
5719
1803
  Item *new_item= NULL;
5773
1857
    assert(item_row->cols() == comp_item_row->cols());
5774
1858
    col= item_row->cols();
5775
1859
    while (col-- > 0)
5776
 
      resolve_const_item(thd, item_row->addr(col),
 
1860
      resolve_const_item(session, item_row->addr(col),
5777
1861
                         comp_item_row->element_index(col));
5778
1862
    break;
5779
1863
  }
5802
1886
    assert(0);
5803
1887
  }
5804
1888
  if (new_item)
5805
 
    thd->change_item_tree(ref, new_item);
 
1889
    session->change_item_tree(ref, new_item);
5806
1890
}
5807
1891
 
5808
1892
/**
5848
1932
  return result == field->val_real();
5849
1933
}
5850
1934
 
5851
 
Item_cache* Item_cache::get_cache(const Item *item)
5852
 
{
5853
 
  switch (item->result_type()) {
5854
 
  case INT_RESULT:
5855
 
    return new Item_cache_int();
5856
 
  case REAL_RESULT:
5857
 
    return new Item_cache_real();
5858
 
  case DECIMAL_RESULT:
5859
 
    return new Item_cache_decimal();
5860
 
  case STRING_RESULT:
5861
 
    return new Item_cache_str(item);
5862
 
  case ROW_RESULT:
5863
 
    return new Item_cache_row();
5864
 
  default:
5865
 
    // should never be in real life
5866
 
    assert(0);
5867
 
    return 0;
5868
 
  }
5869
 
}
5870
 
 
5871
 
 
5872
 
void Item_cache::print(String *str, enum_query_type query_type)
5873
 
{
5874
 
  str->append(STRING_WITH_LEN("<cache>("));
5875
 
  if (example)
5876
 
    example->print(str, query_type);
5877
 
  else
5878
 
    Item::print(str, query_type);
5879
 
  str->append(')');
5880
 
}
5881
 
 
5882
 
 
5883
 
void Item_cache_int::store(Item *item)
5884
 
{
5885
 
  value= item->val_int_result();
5886
 
  null_value= item->null_value;
5887
 
  unsigned_flag= item->unsigned_flag;
5888
 
}
5889
 
 
5890
 
 
5891
 
void Item_cache_int::store(Item *item, int64_t val_arg)
5892
 
{
5893
 
  value= val_arg;
5894
 
  null_value= item->null_value;
5895
 
  unsigned_flag= item->unsigned_flag;
5896
 
}
5897
 
 
5898
 
 
5899
 
String *Item_cache_int::val_str(String *str)
5900
 
{
5901
 
  assert(fixed == 1);
5902
 
  str->set(value, default_charset());
5903
 
  return str;
5904
 
}
5905
 
 
5906
 
 
5907
 
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
5908
 
{
5909
 
  assert(fixed == 1);
5910
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
5911
 
  return decimal_val;
5912
 
}
5913
 
 
5914
 
 
5915
 
void Item_cache_real::store(Item *item)
5916
 
{
5917
 
  value= item->val_result();
5918
 
  null_value= item->null_value;
5919
 
}
5920
 
 
5921
 
 
5922
 
int64_t Item_cache_real::val_int()
5923
 
{
5924
 
  assert(fixed == 1);
5925
 
  return (int64_t) rint(value);
5926
 
}
5927
 
 
5928
 
 
5929
 
String* Item_cache_real::val_str(String *str)
5930
 
{
5931
 
  assert(fixed == 1);
5932
 
  str->set_real(value, decimals, default_charset());
5933
 
  return str;
5934
 
}
5935
 
 
5936
 
 
5937
 
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
5938
 
{
5939
 
  assert(fixed == 1);
5940
 
  double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
5941
 
  return decimal_val;
5942
 
}
5943
 
 
5944
 
 
5945
 
void Item_cache_decimal::store(Item *item)
5946
 
{
5947
 
  my_decimal *val= item->val_decimal_result(&decimal_value);
5948
 
  if (!(null_value= item->null_value) && val != &decimal_value)
5949
 
    my_decimal2decimal(val, &decimal_value);
5950
 
}
5951
 
 
5952
 
double Item_cache_decimal::val_real()
5953
 
{
5954
 
  assert(fixed);
5955
 
  double res;
5956
 
  my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
5957
 
  return res;
5958
 
}
5959
 
 
5960
 
int64_t Item_cache_decimal::val_int()
5961
 
{
5962
 
  assert(fixed);
5963
 
  int64_t res;
5964
 
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
5965
 
  return res;
5966
 
}
5967
 
 
5968
 
String* Item_cache_decimal::val_str(String *str)
5969
 
{
5970
 
  assert(fixed);
5971
 
  my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
5972
 
                   &decimal_value);
5973
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
5974
 
  return str;
5975
 
}
5976
 
 
5977
 
my_decimal *Item_cache_decimal::val_decimal(my_decimal *val __attribute__((unused)))
5978
 
{
5979
 
  assert(fixed);
5980
 
  return &decimal_value;
5981
 
}
5982
 
 
5983
 
 
5984
 
void Item_cache_str::store(Item *item)
5985
 
{
5986
 
  value_buff.set(buffer, sizeof(buffer), item->collation.collation);
5987
 
  value= item->str_result(&value_buff);
5988
 
  if ((null_value= item->null_value))
5989
 
    value= 0;
5990
 
  else if (value != &value_buff)
5991
 
  {
5992
 
    /*
5993
 
      We copy string value to avoid changing value if 'item' is table field
5994
 
      in queries like following (where t1.c is varchar):
5995
 
      select a, 
5996
 
             (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
5997
 
             (select c from t1 where a=t2.a)
5998
 
        from t2;
5999
 
    */
6000
 
    value_buff.copy(*value);
6001
 
    value= &value_buff;
6002
 
  }
6003
 
}
6004
 
 
6005
 
double Item_cache_str::val_real()
6006
 
{
6007
 
  assert(fixed == 1);
6008
 
  int err_not_used;
6009
 
  char *end_not_used;
6010
 
  if (value)
6011
 
    return my_strntod(value->charset(), (char*) value->ptr(),
6012
 
                      value->length(), &end_not_used, &err_not_used);
6013
 
  return (double) 0;
6014
 
}
6015
 
 
6016
 
 
6017
 
int64_t Item_cache_str::val_int()
6018
 
{
6019
 
  assert(fixed == 1);
6020
 
  int err;
6021
 
  if (value)
6022
 
    return my_strntoll(value->charset(), value->ptr(),
6023
 
                       value->length(), 10, (char**) 0, &err);
6024
 
  else
6025
 
    return (int64_t)0;
6026
 
}
6027
 
 
6028
 
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
6029
 
{
6030
 
  assert(fixed == 1);
6031
 
  if (value)
6032
 
    string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
6033
 
  else
6034
 
    decimal_val= 0;
6035
 
  return decimal_val;
6036
 
}
6037
 
 
6038
 
 
6039
 
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
6040
 
{
6041
 
  int res= Item_cache::save_in_field(field, no_conversions);
6042
 
 
6043
 
  return res;
6044
 
}
6045
 
 
6046
 
 
6047
 
bool Item_cache_row::allocate(uint32_t num)
6048
 
{
6049
 
  item_count= num;
6050
 
  THD *thd= current_thd;
6051
 
  return (!(values= 
6052
 
            (Item_cache **) thd->calloc(sizeof(Item_cache *)*item_count)));
6053
 
}
6054
 
 
6055
 
 
6056
 
bool Item_cache_row::setup(Item * item)
6057
 
{
6058
 
  example= item;
6059
 
  if (!values && allocate(item->cols()))
6060
 
    return 1;
6061
 
  for (uint32_t i= 0; i < item_count; i++)
6062
 
  {
6063
 
    Item *el= item->element_index(i);
6064
 
    Item_cache *tmp;
6065
 
    if (!(tmp= values[i]= Item_cache::get_cache(el)))
6066
 
      return 1;
6067
 
    tmp->setup(el);
6068
 
  }
6069
 
  return 0;
6070
 
}
6071
 
 
6072
 
 
6073
 
void Item_cache_row::store(Item * item)
6074
 
{
6075
 
  null_value= 0;
6076
 
  item->bring_value();
6077
 
  for (uint32_t i= 0; i < item_count; i++)
6078
 
  {
6079
 
    values[i]->store(item->element_index(i));
6080
 
    null_value|= values[i]->null_value;
6081
 
  }
6082
 
}
6083
 
 
6084
 
 
6085
 
void Item_cache_row::illegal_method_call(const char *method __attribute__((unused)))
6086
 
{
6087
 
  assert(0);
6088
 
  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
6089
 
  return;
6090
 
}
6091
 
 
6092
 
 
6093
 
bool Item_cache_row::check_cols(uint32_t c)
6094
 
{
6095
 
  if (c != item_count)
6096
 
  {
6097
 
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
6098
 
    return 1;
6099
 
  }
6100
 
  return 0;
6101
 
}
6102
 
 
6103
 
 
6104
 
bool Item_cache_row::null_inside()
6105
 
{
6106
 
  for (uint32_t i= 0; i < item_count; i++)
6107
 
  {
6108
 
    if (values[i]->cols() > 1)
6109
 
    {
6110
 
      if (values[i]->null_inside())
6111
 
        return 1;
6112
 
    }
6113
 
    else
6114
 
    {
6115
 
      values[i]->update_null_value();
6116
 
      if (values[i]->null_value)
6117
 
        return 1;
6118
 
    }
6119
 
  }
6120
 
  return 0;
6121
 
}
6122
 
 
6123
 
 
6124
 
void Item_cache_row::bring_value()
6125
 
{
6126
 
  for (uint32_t i= 0; i < item_count; i++)
6127
 
    values[i]->bring_value();
6128
 
  return;
6129
 
}
6130
 
 
6131
 
 
6132
 
Item_type_holder::Item_type_holder(THD *thd, Item *item)
6133
 
  :Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
6134
 
{
6135
 
  assert(item->fixed);
6136
 
  maybe_null= item->maybe_null;
6137
 
  collation.set(item->collation);
6138
 
  get_full_info(item);
6139
 
  /* fix variable decimals which always is NOT_FIXED_DEC */
6140
 
  if (Field::result_merge_type(fld_type) == INT_RESULT)
6141
 
    decimals= 0;
6142
 
  prev_decimal_int_part= item->decimal_int_part();
6143
 
}
6144
 
 
6145
 
 
6146
 
/**
6147
 
  Return expression type of Item_type_holder.
6148
 
 
6149
 
  @return
6150
 
    Item_result (type of internal MySQL expression result)
6151
 
*/
6152
 
 
6153
 
Item_result Item_type_holder::result_type() const
6154
 
{
6155
 
  return Field::result_merge_type(fld_type);
6156
 
}
6157
 
 
6158
 
 
6159
 
/**
6160
 
  Find real field type of item.
6161
 
 
6162
 
  @return
6163
 
    type of field which should be created to store item value
6164
 
*/
6165
 
 
6166
 
enum_field_types Item_type_holder::get_real_type(Item *item)
6167
 
{
6168
 
  switch(item->type())
6169
 
  {
6170
 
  case FIELD_ITEM:
6171
 
  {
6172
 
    /*
6173
 
      Item_fields::field_type ask Field_type() but sometimes field return
6174
 
      a different type, like for enum/set, so we need to ask real type.
6175
 
    */
6176
 
    Field *field= ((Item_field *) item)->field;
6177
 
    enum_field_types type= field->real_type();
6178
 
    if (field->is_created_from_null_item)
6179
 
      return DRIZZLE_TYPE_NULL;
6180
 
    return type;
6181
 
  }
6182
 
  case SUM_FUNC_ITEM:
6183
 
  {
6184
 
    /*
6185
 
      Argument of aggregate function sometimes should be asked about field
6186
 
      type
6187
 
    */
6188
 
    Item_sum *item_sum= (Item_sum *) item;
6189
 
    if (item_sum->keep_field_type())
6190
 
      return get_real_type(item_sum->args[0]);
6191
 
    break;
6192
 
  }
6193
 
  case FUNC_ITEM:
6194
 
    if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC)
6195
 
    {
6196
 
      /*
6197
 
        There are work around of problem with changing variable type on the
6198
 
        fly and variable always report "string" as field type to get
6199
 
        acceptable information for client in send_field, so we make field
6200
 
        type from expression type.
6201
 
      */
6202
 
      switch (item->result_type()) {
6203
 
      case STRING_RESULT:
6204
 
        return DRIZZLE_TYPE_VARCHAR;
6205
 
      case INT_RESULT:
6206
 
        return DRIZZLE_TYPE_LONGLONG;
6207
 
      case REAL_RESULT:
6208
 
        return DRIZZLE_TYPE_DOUBLE;
6209
 
      case DECIMAL_RESULT:
6210
 
        return DRIZZLE_TYPE_NEWDECIMAL;
6211
 
      case ROW_RESULT:
6212
 
      default:
6213
 
        assert(0);
6214
 
        return DRIZZLE_TYPE_VARCHAR;
6215
 
      }
6216
 
    }
6217
 
    break;
6218
 
  default:
6219
 
    break;
6220
 
  }
6221
 
  return item->field_type();
6222
 
}
6223
 
 
6224
 
/**
6225
 
  Find field type which can carry current Item_type_holder type and
6226
 
  type of given Item.
6227
 
 
6228
 
  @param thd     thread handler
6229
 
  @param item    given item to join its parameters with this item ones
6230
 
 
6231
 
  @retval
6232
 
    true   error - types are incompatible
6233
 
  @retval
6234
 
    false  OK
6235
 
*/
6236
 
 
6237
 
bool Item_type_holder::join_types(THD *thd __attribute__((unused)),
6238
 
                                  Item *item)
6239
 
{
6240
 
  uint32_t max_length_orig= max_length;
6241
 
  uint32_t decimals_orig= decimals;
6242
 
  fld_type= Field::field_type_merge(fld_type, get_real_type(item));
6243
 
  {
6244
 
    int item_decimals= item->decimals;
6245
 
    /* fix variable decimals which always is NOT_FIXED_DEC */
6246
 
    if (Field::result_merge_type(fld_type) == INT_RESULT)
6247
 
      item_decimals= 0;
6248
 
    decimals= cmax((int)decimals, item_decimals);
6249
 
  }
6250
 
  if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
6251
 
  {
6252
 
    decimals= cmin((int)cmax(decimals, item->decimals), DECIMAL_MAX_SCALE);
6253
 
    int precision= cmin(cmax(prev_decimal_int_part, item->decimal_int_part())
6254
 
                       + decimals, DECIMAL_MAX_PRECISION);
6255
 
    unsigned_flag&= item->unsigned_flag;
6256
 
    max_length= my_decimal_precision_to_length(precision, decimals,
6257
 
                                               unsigned_flag);
6258
 
  }
6259
 
 
6260
 
  switch (Field::result_merge_type(fld_type))
6261
 
  {
6262
 
  case STRING_RESULT:
6263
 
  {
6264
 
    const char *old_cs, *old_derivation;
6265
 
    uint32_t old_max_chars= max_length / collation.collation->mbmaxlen;
6266
 
    old_cs= collation.collation->name;
6267
 
    old_derivation= collation.derivation_name();
6268
 
    if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
6269
 
    {
6270
 
      my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
6271
 
               old_cs, old_derivation,
6272
 
               item->collation.collation->name,
6273
 
               item->collation.derivation_name(),
6274
 
               "UNION");
6275
 
      return(true);
6276
 
    }
6277
 
    /*
6278
 
      To figure out max_length, we have to take into account possible
6279
 
      expansion of the size of the values because of character set
6280
 
      conversions.
6281
 
     */
6282
 
    if (collation.collation != &my_charset_bin)
6283
 
    {
6284
 
      max_length= cmax(old_max_chars * collation.collation->mbmaxlen,
6285
 
                      display_length(item) /
6286
 
                      item->collation.collation->mbmaxlen *
6287
 
                      collation.collation->mbmaxlen);
6288
 
    }
6289
 
    else
6290
 
      set_if_bigger(max_length, display_length(item));
6291
 
    break;
6292
 
  }
6293
 
  case REAL_RESULT:
6294
 
  {
6295
 
    if (decimals != NOT_FIXED_DEC)
6296
 
    {
6297
 
      int delta1= max_length_orig - decimals_orig;
6298
 
      int delta2= item->max_length - item->decimals;
6299
 
      max_length= cmax(delta1, delta2) + decimals;
6300
 
      if (fld_type == DRIZZLE_TYPE_DOUBLE && max_length > DBL_DIG + 2) 
6301
 
      {
6302
 
        max_length= DBL_DIG + 7;
6303
 
        decimals= NOT_FIXED_DEC;
6304
 
      }
6305
 
    }
6306
 
    else
6307
 
      max_length= DBL_DIG+7;
6308
 
    break;
6309
 
  }
6310
 
  default:
6311
 
    max_length= cmax(max_length, display_length(item));
6312
 
  };
6313
 
  maybe_null|= item->maybe_null;
6314
 
  get_full_info(item);
6315
 
 
6316
 
  /* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
6317
 
  prev_decimal_int_part= decimal_int_part();
6318
 
  return(false);
6319
 
}
6320
 
 
6321
 
/**
6322
 
  Calculate lenth for merging result for given Item type.
6323
 
 
6324
 
  @param item  Item for length detection
6325
 
 
6326
 
  @return
6327
 
    length
6328
 
*/
6329
 
 
6330
 
uint32_t Item_type_holder::display_length(Item *item)
6331
 
{
6332
 
  if (item->type() == Item::FIELD_ITEM)
6333
 
    return ((Item_field *)item)->max_disp_length();
6334
 
 
6335
 
  switch (item->field_type())
6336
 
  {
6337
 
  case DRIZZLE_TYPE_TIMESTAMP:
6338
 
  case DRIZZLE_TYPE_TIME:
6339
 
  case DRIZZLE_TYPE_DATETIME:
6340
 
  case DRIZZLE_TYPE_NEWDATE:
6341
 
  case DRIZZLE_TYPE_VARCHAR:
6342
 
  case DRIZZLE_TYPE_NEWDECIMAL:
6343
 
  case DRIZZLE_TYPE_ENUM:
6344
 
  case DRIZZLE_TYPE_BLOB:
6345
 
  case DRIZZLE_TYPE_TINY:
6346
 
    return 4;
6347
 
  case DRIZZLE_TYPE_LONG:
6348
 
    return MY_INT32_NUM_DECIMAL_DIGITS;
6349
 
  case DRIZZLE_TYPE_DOUBLE:
6350
 
    return 53;
6351
 
  case DRIZZLE_TYPE_NULL:
6352
 
    return 0;
6353
 
  case DRIZZLE_TYPE_LONGLONG:
6354
 
    return 20;
6355
 
  default:
6356
 
    assert(0); // we should never go there
6357
 
    return 0;
6358
 
  }
6359
 
}
6360
 
 
6361
 
 
6362
 
/**
6363
 
  Make temporary table field according collected information about type
6364
 
  of UNION result.
6365
 
 
6366
 
  @param table  temporary table for which we create fields
6367
 
 
6368
 
  @return
6369
 
    created field
6370
 
*/
6371
 
 
6372
 
Field *Item_type_holder::make_field_by_type(Table *table)
6373
 
{
6374
 
  /*
6375
 
    The field functions defines a field to be not null if null_ptr is not 0
6376
 
  */
6377
 
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
6378
 
  Field *field;
6379
 
 
6380
 
  switch (fld_type) {
6381
 
  case DRIZZLE_TYPE_ENUM:
6382
 
    assert(enum_set_typelib);
6383
 
    field= new Field_enum((unsigned char *) 0, max_length, null_ptr, 0,
6384
 
                          Field::NONE, name,
6385
 
                          get_enum_pack_length(enum_set_typelib->count),
6386
 
                          enum_set_typelib, collation.collation);
6387
 
    if (field)
6388
 
      field->init(table);
6389
 
    return field;
6390
 
  case DRIZZLE_TYPE_NULL:
6391
 
    return make_string_field(table);
6392
 
  default:
6393
 
    break;
6394
 
  }
6395
 
  return tmp_table_field_from_field_type(table, 0);
6396
 
}
6397
 
 
6398
 
 
6399
 
/**
6400
 
  Get full information from Item about enum/set fields to be able to create
6401
 
  them later.
6402
 
 
6403
 
  @param item    Item for information collection
6404
 
*/
6405
 
void Item_type_holder::get_full_info(Item *item)
6406
 
{
6407
 
  if (fld_type == DRIZZLE_TYPE_ENUM)
6408
 
  {
6409
 
    if (item->type() == Item::SUM_FUNC_ITEM &&
6410
 
        (((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
6411
 
         ((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC))
6412
 
      item = ((Item_sum*)item)->args[0];
6413
 
    /*
6414
 
      We can have enum/set type after merging only if we have one enum|set
6415
 
      field (or MIN|MAX(enum|set field)) and number of NULL fields
6416
 
    */
6417
 
    assert((enum_set_typelib &&
6418
 
                 get_real_type(item) == DRIZZLE_TYPE_NULL) ||
6419
 
                (!enum_set_typelib &&
6420
 
                 item->type() == Item::FIELD_ITEM &&
6421
 
                 (get_real_type(item) == DRIZZLE_TYPE_ENUM) &&
6422
 
                 ((Field_enum*)((Item_field *) item)->field)->typelib));
6423
 
    if (!enum_set_typelib)
6424
 
    {
6425
 
      enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib;
6426
 
    }
6427
 
  }
6428
 
}
6429
 
 
6430
 
 
6431
 
double Item_type_holder::val_real()
6432
 
{
6433
 
  assert(0); // should never be called
6434
 
  return 0.0;
6435
 
}
6436
 
 
6437
 
 
6438
 
int64_t Item_type_holder::val_int()
6439
 
{
6440
 
  assert(0); // should never be called
6441
 
  return 0;
6442
 
}
6443
 
 
6444
 
my_decimal *Item_type_holder::val_decimal(my_decimal *)
6445
 
{
6446
 
  assert(0); // should never be called
6447
 
  return 0;
6448
 
}
6449
 
 
6450
 
String *Item_type_holder::val_str(String*)
6451
 
{
6452
 
  assert(0); // should never be called
6453
 
  return 0;
6454
 
}
6455
 
 
6456
 
void Item_result_field::cleanup()
6457
 
{
6458
 
  Item::cleanup();
6459
 
  result_field= 0;
6460
 
  return;
6461
 
}
6462
 
 
6463
1935
/**
6464
1936
  Dummy error processor used by default by Name_resolution_context.
6465
1937
 
6467
1939
    do nothing
6468
1940
*/
6469
1941
 
6470
 
void dummy_error_processor(THD *thd __attribute__((unused)),
6471
 
                           void *data __attribute__((unused)))
 
1942
void dummy_error_processor(Session *, void *)
6472
1943
{}
6473
1944
 
6474
1945
/**
 
1946
  Create field for temporary table using type of given item.
 
1947
 
 
1948
  @param session                   Thread handler
 
1949
  @param item                  Item to create a field for
 
1950
  @param table                 Temporary table
 
1951
  @param copy_func             If set and item is a function, store copy of
 
1952
                               item in this array
 
1953
  @param modify_item           1 if item->result_field should point to new
 
1954
                               item. This is relevent for how fill_record()
 
1955
                               is going to work:
 
1956
                               If modify_item is 1 then fill_record() will
 
1957
                               update the record in the original table.
 
1958
                               If modify_item is 0 then fill_record() will
 
1959
                               update the temporary table
 
1960
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
1961
                               field instead of blob.
 
1962
 
 
1963
  @retval
 
1964
    0  on error
 
1965
  @retval
 
1966
    new_created field
 
1967
*/
 
1968
 
 
1969
static Field *create_tmp_field_from_item(Session *,
 
1970
                                         Item *item, Table *table,
 
1971
                                         Item ***copy_func, bool modify_item,
 
1972
                                         uint32_t convert_blob_length)
 
1973
{
 
1974
  bool maybe_null= item->maybe_null;
 
1975
  Field *new_field;
 
1976
 
 
1977
  switch (item->result_type()) {
 
1978
  case REAL_RESULT:
 
1979
    new_field= new Field_double(item->max_length, maybe_null,
 
1980
                                item->name, item->decimals, true);
 
1981
    break;
 
1982
  case INT_RESULT:
 
1983
    /*
 
1984
      Select an integer type with the minimal fit precision.
 
1985
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
 
1986
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
 
1987
      Field_long : make them Field_int64_t.
 
1988
    */
 
1989
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1990
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
1991
                                   item->name, item->unsigned_flag);
 
1992
    else
 
1993
      new_field=new Field_long(item->max_length, maybe_null,
 
1994
                               item->name, item->unsigned_flag);
 
1995
    break;
 
1996
  case STRING_RESULT:
 
1997
    assert(item->collation.collation);
 
1998
 
 
1999
    enum enum_field_types type;
 
2000
    /*
 
2001
      DATE/TIME fields have STRING_RESULT result type.
 
2002
      To preserve type they needed to be handled separately.
 
2003
    */
 
2004
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
 
2005
        type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_DATE ||
 
2006
        type == DRIZZLE_TYPE_TIMESTAMP)
 
2007
      new_field= item->tmp_table_field_from_field_type(table, 1);
 
2008
    /*
 
2009
      Make sure that the blob fits into a Field_varstring which has
 
2010
      2-byte lenght.
 
2011
    */
 
2012
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
 
2013
             convert_blob_length <= Field_varstring::MAX_SIZE &&
 
2014
             convert_blob_length)
 
2015
      new_field= new Field_varstring(convert_blob_length, maybe_null,
 
2016
                                     item->name, table->s,
 
2017
                                     item->collation.collation);
 
2018
    else
 
2019
      new_field= item->make_string_field(table);
 
2020
    new_field->set_derivation(item->collation.derivation);
 
2021
    break;
 
2022
  case DECIMAL_RESULT:
 
2023
  {
 
2024
    uint8_t dec= item->decimals;
 
2025
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
2026
    uint32_t len= item->max_length;
 
2027
 
 
2028
    /*
 
2029
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
2030
      will always throw a warning. We must limit dec to
 
2031
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
2032
    */
 
2033
 
 
2034
    if (dec > 0)
 
2035
    {
 
2036
      signed int overflow;
 
2037
 
 
2038
      dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
 
2039
 
 
2040
      /*
 
2041
        If the value still overflows the field with the corrected dec,
 
2042
        we'll throw out decimals rather than integers. This is still
 
2043
        bad and of course throws a truncation warning.
 
2044
        +1: for decimal point
 
2045
      */
 
2046
 
 
2047
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
2048
                                               item->unsigned_flag) - len;
 
2049
 
 
2050
      if (overflow > 0)
 
2051
        dec= cmax(0, dec - overflow);            // too long, discard fract
 
2052
      else
 
2053
        len -= item->decimals - dec;            // corrected value fits
 
2054
    }
 
2055
 
 
2056
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
2057
                                     dec, item->unsigned_flag);
 
2058
    break;
 
2059
  }
 
2060
  case ROW_RESULT:
 
2061
  default:
 
2062
    // This case should never be choosen
 
2063
    assert(0);
 
2064
    new_field= 0;
 
2065
    break;
 
2066
  }
 
2067
  if (new_field)
 
2068
    new_field->init(table);
 
2069
 
 
2070
  if (copy_func && item->is_result_field())
 
2071
    *((*copy_func)++) = item;                   // Save for copy_funcs
 
2072
  if (modify_item)
 
2073
    item->set_result_field(new_field);
 
2074
  if (item->type() == Item::NULL_ITEM)
 
2075
    new_field->is_created_from_null_item= true;
 
2076
  return new_field;
 
2077
}
 
2078
 
 
2079
Field *create_tmp_field(Session *session, Table *table,Item *item,
 
2080
                        Item::Type type, Item ***copy_func, Field **from_field,
 
2081
                        Field **default_field, bool group, bool modify_item,
 
2082
                        bool, bool make_copy_field,
 
2083
                        uint32_t convert_blob_length)
 
2084
{
 
2085
  Field *result;
 
2086
  Item::Type orig_type= type;
 
2087
  Item *orig_item= 0;
 
2088
 
 
2089
  if (type != Item::FIELD_ITEM &&
 
2090
      item->real_item()->type() == Item::FIELD_ITEM)
 
2091
  {
 
2092
    orig_item= item;
 
2093
    item= item->real_item();
 
2094
    type= Item::FIELD_ITEM;
 
2095
  }
 
2096
 
 
2097
  switch (type) {
 
2098
  case Item::SUM_FUNC_ITEM:
 
2099
  {
 
2100
    Item_sum *item_sum=(Item_sum*) item;
 
2101
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
 
2102
    if (!result)
 
2103
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
2104
    return result;
 
2105
  }
 
2106
  case Item::FIELD_ITEM:
 
2107
  case Item::DEFAULT_VALUE_ITEM:
 
2108
  {
 
2109
    Item_field *field= (Item_field*) item;
 
2110
    bool orig_modify= modify_item;
 
2111
    if (orig_type == Item::REF_ITEM)
 
2112
      modify_item= 0;
 
2113
    /*
 
2114
      If item have to be able to store NULLs but underlaid field can't do it,
 
2115
      create_tmp_field_from_field() can't be used for tmp field creation.
 
2116
    */
 
2117
    if (field->maybe_null && !field->field->maybe_null())
 
2118
    {
 
2119
      result= create_tmp_field_from_item(session, item, table, NULL,
 
2120
                                         modify_item, convert_blob_length);
 
2121
      *from_field= field->field;
 
2122
      if (result && modify_item)
 
2123
        field->result_field= result;
 
2124
    }
 
2125
    else
 
2126
      result= create_tmp_field_from_field(session, (*from_field= field->field),
 
2127
                                          orig_item ? orig_item->name :
 
2128
                                          item->name,
 
2129
                                          table,
 
2130
                                          modify_item ? field :
 
2131
                                          NULL,
 
2132
                                          convert_blob_length);
 
2133
    if (orig_type == Item::REF_ITEM && orig_modify)
 
2134
      ((Item_ref*)orig_item)->set_result_field(result);
 
2135
    if (field->field->eq_def(result))
 
2136
      *default_field= field->field;
 
2137
    return result;
 
2138
  }
 
2139
  /* Fall through */
 
2140
  case Item::FUNC_ITEM:
 
2141
    /* Fall through */
 
2142
  case Item::COND_ITEM:
 
2143
  case Item::FIELD_AVG_ITEM:
 
2144
  case Item::FIELD_STD_ITEM:
 
2145
  case Item::SUBSELECT_ITEM:
 
2146
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
 
2147
  case Item::PROC_ITEM:
 
2148
  case Item::INT_ITEM:
 
2149
  case Item::REAL_ITEM:
 
2150
  case Item::DECIMAL_ITEM:
 
2151
  case Item::STRING_ITEM:
 
2152
  case Item::REF_ITEM:
 
2153
  case Item::NULL_ITEM:
 
2154
  case Item::VARBIN_ITEM:
 
2155
    if (make_copy_field)
 
2156
    {
 
2157
      assert(((Item_result_field*)item)->result_field);
 
2158
      *from_field= ((Item_result_field*)item)->result_field;
 
2159
    }
 
2160
    return create_tmp_field_from_item(session, item, table,
 
2161
                                      (make_copy_field ? 0 : copy_func),
 
2162
                                       modify_item, convert_blob_length);
 
2163
  case Item::TYPE_HOLDER:
 
2164
    result= ((Item_type_holder *)item)->make_field_by_type(table);
 
2165
    result->set_derivation(item->collation.derivation);
 
2166
    return result;
 
2167
  default:                                      // Dosen't have to be stored
 
2168
    return NULL;
 
2169
  }
 
2170
}
 
2171
 
 
2172
 
 
2173
/**
6475
2174
  Wrapper of hide_view_error call for Name_resolution_context error
6476
2175
  processor.
6477
2176