~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to sql/item.h

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef ITEM_INCLUDED
 
2
#define ITEM_INCLUDED
 
3
 
 
4
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 
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
 
 
19
 
 
20
#include "sql_priv.h"                /* STRING_BUFFER_USUAL_SIZE */
 
21
#include "unireg.h"
 
22
#include "sql_const.h"                 /* RAND_TABLE_BIT, MAX_FIELD_NAME */
 
23
#include "unireg.h"                    // REQUIRED: for other includes
 
24
#include "thr_malloc.h"                         /* sql_calloc */
 
25
#include "field.h"                              /* Derivation */
 
26
#include "sql_array.h"
 
27
 
 
28
class Protocol;
 
29
struct TABLE_LIST;
 
30
void item_init(void);                   /* Init item functions */
 
31
class Item_field;
 
32
class user_var_entry;
 
33
 
 
34
typedef Bounds_checked_array<Item*> Ref_ptr_array;
 
35
 
 
36
static inline uint32
 
37
char_to_byte_length_safe(uint32 char_length_arg, uint32 mbmaxlen_arg)
 
38
{
 
39
   ulonglong tmp= ((ulonglong) char_length_arg) * mbmaxlen_arg;
 
40
   return (tmp > UINT_MAX32) ? (uint32) UINT_MAX32 : (uint32) tmp;
 
41
}
 
42
 
 
43
 
 
44
/*
 
45
   "Declared Type Collation"
 
46
   A combination of collation and its derivation.
 
47
 
 
48
  Flags for collation aggregation modes:
 
49
  MY_COLL_ALLOW_SUPERSET_CONV  - allow conversion to a superset
 
50
  MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
 
51
                                 (i.e. constant).
 
52
  MY_COLL_ALLOW_CONV           - allow any kind of conversion
 
53
                                 (combination of the above two)
 
54
  MY_COLL_ALLOW_NUMERIC_CONV   - if all items were numbers, convert to
 
55
                                 @@character_set_connection
 
56
  MY_COLL_DISALLOW_NONE        - don't allow return DERIVATION_NONE
 
57
                                 (e.g. when aggregating for comparison)
 
58
  MY_COLL_CMP_CONV             - combination of MY_COLL_ALLOW_CONV
 
59
                                 and MY_COLL_DISALLOW_NONE
 
60
*/
 
61
 
 
62
#define MY_COLL_ALLOW_SUPERSET_CONV   1
 
63
#define MY_COLL_ALLOW_COERCIBLE_CONV  2
 
64
#define MY_COLL_DISALLOW_NONE         4
 
65
#define MY_COLL_ALLOW_NUMERIC_CONV    8
 
66
 
 
67
#define MY_COLL_ALLOW_CONV (MY_COLL_ALLOW_SUPERSET_CONV | MY_COLL_ALLOW_COERCIBLE_CONV)
 
68
#define MY_COLL_CMP_CONV   (MY_COLL_ALLOW_CONV | MY_COLL_DISALLOW_NONE)
 
69
 
 
70
class DTCollation {
 
71
public:
 
72
  const CHARSET_INFO *collation;
 
73
  enum Derivation derivation;
 
74
  uint repertoire;
 
75
  
 
76
  void set_repertoire_from_charset(const CHARSET_INFO *cs)
 
77
  {
 
78
    repertoire= cs->state & MY_CS_PUREASCII ?
 
79
                MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
 
80
  }
 
81
  DTCollation()
 
82
  {
 
83
    collation= &my_charset_bin;
 
84
    derivation= DERIVATION_NONE;
 
85
    repertoire= MY_REPERTOIRE_UNICODE30;
 
86
  }
 
87
  DTCollation(const CHARSET_INFO *collation_arg, Derivation derivation_arg)
 
88
  {
 
89
    collation= collation_arg;
 
90
    derivation= derivation_arg;
 
91
    set_repertoire_from_charset(collation_arg);
 
92
  }
 
93
  void set(DTCollation &dt)
 
94
  { 
 
95
    collation= dt.collation;
 
96
    derivation= dt.derivation;
 
97
    repertoire= dt.repertoire;
 
98
  }
 
99
  void set(const CHARSET_INFO *collation_arg, Derivation derivation_arg)
 
100
  {
 
101
    collation= collation_arg;
 
102
    derivation= derivation_arg;
 
103
    set_repertoire_from_charset(collation_arg);
 
104
  }
 
105
  void set(const CHARSET_INFO *collation_arg,
 
106
           Derivation derivation_arg,
 
107
           uint repertoire_arg)
 
108
  {
 
109
    collation= collation_arg;
 
110
    derivation= derivation_arg;
 
111
    repertoire= repertoire_arg;
 
112
  }
 
113
  void set_numeric()
 
114
  {
 
115
    collation= &my_charset_numeric;
 
116
    derivation= DERIVATION_NUMERIC;
 
117
    repertoire= MY_REPERTOIRE_NUMERIC;
 
118
  }
 
119
  void set(const CHARSET_INFO *collation_arg)
 
120
  {
 
121
    collation= collation_arg;
 
122
    set_repertoire_from_charset(collation_arg);
 
123
  }
 
124
  void set(Derivation derivation_arg)
 
125
  { derivation= derivation_arg; }
 
126
  void set_repertoire(uint repertoire_arg)
 
127
  { repertoire= repertoire_arg; }
 
128
  bool aggregate(DTCollation &dt, uint flags= 0);
 
129
  bool set(DTCollation &dt1, DTCollation &dt2, uint flags= 0)
 
130
  { set(dt1); return aggregate(dt2, flags); }
 
131
  const char *derivation_name() const
 
132
  {
 
133
    switch(derivation)
 
134
    {
 
135
      case DERIVATION_NUMERIC:   return "NUMERIC";
 
136
      case DERIVATION_IGNORABLE: return "IGNORABLE";
 
137
      case DERIVATION_COERCIBLE: return "COERCIBLE";
 
138
      case DERIVATION_IMPLICIT:  return "IMPLICIT";
 
139
      case DERIVATION_SYSCONST:  return "SYSCONST";
 
140
      case DERIVATION_EXPLICIT:  return "EXPLICIT";
 
141
      case DERIVATION_NONE:      return "NONE";
 
142
      default: return "UNKNOWN";
 
143
    }
 
144
  }
 
145
};
 
146
 
 
147
/*************************************************************************/
 
148
 
 
149
/**
 
150
  Storage for name strings.
 
151
  Enpowers Simple_cstring with allocation routines from the sql_strmake family.
 
152
 
 
153
  This class must stay as small as possible as we often 
 
154
  pass it into functions using call-by-value evaluation.
 
155
 
 
156
  Don't add new members or virual methods into this class!
 
157
*/
 
158
class Name_string: public Simple_cstring
 
159
{
 
160
private:
 
161
  void set_or_copy(const char *str, size_t length, bool is_null_terminated)
 
162
  {
 
163
    if (is_null_terminated)
 
164
      set(str, length);
 
165
    else
 
166
      copy(str, length);  
 
167
  }
 
168
public:
 
169
  Name_string(): Simple_cstring() {}
 
170
  /*
 
171
    Please do NOT add constructor Name_string(const char *str) !
 
172
    It will involve hidden strlen() call, which can affect
 
173
    performance negatively. Use Name_string(str, len) instead.
 
174
  */
 
175
  Name_string(const char *str, size_t length):
 
176
    Simple_cstring(str, length) {}
 
177
  Name_string(const LEX_STRING str): Simple_cstring(str) {}
 
178
  Name_string(const char *str, size_t length, bool is_null_terminated):
 
179
    Simple_cstring()
 
180
  {
 
181
    set_or_copy(str, length, is_null_terminated);
 
182
  }
 
183
  Name_string(const LEX_STRING str, bool is_null_terminated):
 
184
    Simple_cstring()
 
185
  {
 
186
    set_or_copy(str.str, str.length, is_null_terminated);
 
187
  }
 
188
  /**
 
189
    Allocate space using sql_strmake() or sql_strmake_with_convert().
 
190
  */
 
191
  void copy(const char *str, size_t length, const CHARSET_INFO *cs);
 
192
  /**
 
193
    Variants for copy(), for various argument combinations.
 
194
  */
 
195
  void copy(const char *str, size_t length)
 
196
  {
 
197
    copy(str, length, system_charset_info);
 
198
  }
 
199
  void copy(const char *str)
 
200
  {
 
201
    copy(str, (str ? strlen(str) : 0), system_charset_info);
 
202
  }
 
203
  void copy(const LEX_STRING lex)
 
204
  {
 
205
    copy(lex.str, lex.length);
 
206
  }
 
207
  void copy(const LEX_STRING *lex)
 
208
  {
 
209
    copy(lex->str, lex->length);
 
210
  }
 
211
  void copy(const Name_string str)
 
212
  {
 
213
    copy(str.ptr(), str.length());
 
214
  }
 
215
  /**
 
216
    Compare name to another name in C string, case insensitively.
 
217
  */
 
218
  bool eq(const char *str) const
 
219
  {
 
220
    DBUG_ASSERT(str && ptr());
 
221
    return my_strcasecmp(system_charset_info, ptr(), str) == 0;
 
222
  }
 
223
  bool eq_safe(const char *str) const
 
224
  {
 
225
    return is_set() && str && eq(str);
 
226
  }
 
227
  /**
 
228
    Compare name to another name in Name_string, case insensitively.
 
229
  */
 
230
  bool eq(const Name_string name) const
 
231
  {
 
232
    return eq(name.ptr());
 
233
  }
 
234
  bool eq_safe(const Name_string name) const
 
235
  {
 
236
    return is_set() && name.is_set() && eq(name);
 
237
  }
 
238
};
 
239
 
 
240
 
 
241
#define NAME_STRING(x)  Name_string(C_STRING_WITH_LEN(x))
 
242
 
 
243
 
 
244
extern const Name_string null_name_string;
 
245
 
 
246
 
 
247
/**
 
248
  Storage for Item names.
 
249
  Adds "autogenerated" flag and warning functionality to Name_string.
 
250
*/
 
251
class Item_name_string: public Name_string
 
252
{
 
253
private:
 
254
  bool m_is_autogenerated; /* indicates if name of this Item
 
255
                              was autogenerated or set by user */
 
256
public:
 
257
  Item_name_string(): Name_string(), m_is_autogenerated(true)
 
258
  { }
 
259
  Item_name_string(const Name_string name)
 
260
    :Name_string(name), m_is_autogenerated(true)
 
261
  { }
 
262
  /**
 
263
    Set m_is_autogenerated flag to the given value.
 
264
  */
 
265
  void set_autogenerated(bool is_autogenerated)
 
266
  {
 
267
    m_is_autogenerated= is_autogenerated;
 
268
  }
 
269
  /**
 
270
    Return the auto-generated flag.
 
271
  */
 
272
  bool is_autogenerated() const { return m_is_autogenerated; }
 
273
  using Name_string::copy;
 
274
  /**
 
275
    Copy name together with autogenerated flag.
 
276
    Produce a warning if name was cut.
 
277
  */
 
278
  void copy(const char *str_arg, size_t length_arg, const CHARSET_INFO *cs_arg,
 
279
           bool is_autogenerated_arg);
 
280
};
 
281
 
 
282
 
 
283
 
 
284
/*************************************************************************/
 
285
/*
 
286
  A framework to easily handle different return types for hybrid items
 
287
  (hybrid item is an item whose operand can be of any type, e.g. integer,
 
288
  real, decimal).
 
289
*/
 
290
 
 
291
struct Hybrid_type_traits;
 
292
 
 
293
struct Hybrid_type
 
294
{
 
295
  longlong integer;
 
296
 
 
297
  double real;
 
298
  /*
 
299
    Use two decimal buffers interchangeably to speed up += operation
 
300
    which has no native support in decimal library.
 
301
    Hybrid_type+= arg is implemented as dec_buf[1]= dec_buf[0] + arg.
 
302
    The third decimal is used as a handy temporary storage.
 
303
  */
 
304
  my_decimal dec_buf[3];
 
305
  int used_dec_buf_no;
 
306
 
 
307
  /*
 
308
    Traits moved to a separate class to
 
309
      a) be able to easily change object traits in runtime
 
310
      b) they work as a differentiator for the union above
 
311
  */
 
312
  const Hybrid_type_traits *traits;
 
313
 
 
314
  Hybrid_type() {}
 
315
  /* XXX: add traits->copy() when needed */
 
316
  Hybrid_type(const Hybrid_type &rhs) :traits(rhs.traits) {}
 
317
};
 
318
 
 
319
 
 
320
/* Hybryd_type_traits interface + default implementation for REAL_RESULT */
 
321
 
 
322
struct Hybrid_type_traits
 
323
{
 
324
  virtual Item_result type() const { return REAL_RESULT; }
 
325
 
 
326
  virtual void
 
327
  fix_length_and_dec(Item *item, Item *arg) const;
 
328
 
 
329
  /* Hybrid_type operations. */
 
330
  virtual void set_zero(Hybrid_type *val) const { val->real= 0.0; }
 
331
  virtual void add(Hybrid_type *val, Field *f) const
 
332
  { val->real+= f->val_real(); }
 
333
  virtual void div(Hybrid_type *val, ulonglong u) const
 
334
  { val->real/= ulonglong2double(u); }
 
335
 
 
336
  virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const
 
337
  { return (longlong) rint(val->real); }
 
338
  virtual double val_real(Hybrid_type *val) const { return val->real; }
 
339
  virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const;
 
340
  virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const;
 
341
  static const Hybrid_type_traits *instance();
 
342
  Hybrid_type_traits() {}
 
343
  virtual ~Hybrid_type_traits() {}
 
344
};
 
345
 
 
346
 
 
347
struct Hybrid_type_traits_decimal: public Hybrid_type_traits
 
348
{
 
349
  virtual Item_result type() const { return DECIMAL_RESULT; }
 
350
 
 
351
  virtual void
 
352
  fix_length_and_dec(Item *arg, Item *item) const;
 
353
 
 
354
  /* Hybrid_type operations. */
 
355
  virtual void set_zero(Hybrid_type *val) const;
 
356
  virtual void add(Hybrid_type *val, Field *f) const;
 
357
  virtual void div(Hybrid_type *val, ulonglong u) const;
 
358
 
 
359
  virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const;
 
360
  virtual double val_real(Hybrid_type *val) const;
 
361
  virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const
 
362
  { return &val->dec_buf[val->used_dec_buf_no]; }
 
363
  virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const;
 
364
  static const Hybrid_type_traits_decimal *instance();
 
365
  Hybrid_type_traits_decimal() {};
 
366
};
 
367
 
 
368
 
 
369
struct Hybrid_type_traits_integer: public Hybrid_type_traits
 
370
{
 
371
  virtual Item_result type() const { return INT_RESULT; }
 
372
 
 
373
  virtual void
 
374
  fix_length_and_dec(Item *arg, Item *item) const;
 
375
 
 
376
  /* Hybrid_type operations. */
 
377
  virtual void set_zero(Hybrid_type *val) const
 
378
  { val->integer= 0; }
 
379
  virtual void add(Hybrid_type *val, Field *f) const
 
380
  { val->integer+= f->val_int(); }
 
381
  virtual void div(Hybrid_type *val, ulonglong u) const
 
382
  { val->integer/= (longlong) u; }
 
383
 
 
384
  virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const
 
385
  { return val->integer; }
 
386
  virtual double val_real(Hybrid_type *val) const
 
387
  { return (double) val->integer; }
 
388
  virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const
 
389
  {
 
390
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, &val->dec_buf[2]);
 
391
    return &val->dec_buf[2];
 
392
  }
 
393
  virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const
 
394
  { buf->set(val->integer, &my_charset_bin); return buf;}
 
395
  static const Hybrid_type_traits_integer *instance();
 
396
  Hybrid_type_traits_integer() {};
 
397
};
 
398
 
 
399
 
 
400
void dummy_error_processor(THD *thd, void *data);
 
401
 
 
402
void view_error_processor(THD *thd, void *data);
 
403
 
 
404
/*
 
405
  Instances of Name_resolution_context store the information necesary for
 
406
  name resolution of Items and other context analysis of a query made in
 
407
  fix_fields().
 
408
 
 
409
  This structure is a part of SELECT_LEX, a pointer to this structure is
 
410
  assigned when an item is created (which happens mostly during  parsing
 
411
  (sql_yacc.yy)), but the structure itself will be initialized after parsing
 
412
  is complete
 
413
 
 
414
  TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
 
415
  separate SELECT_LEX which allow to remove tricks of changing this
 
416
  structure before and after INSERT/CREATE and its SELECT to make correct
 
417
  field name resolution.
 
418
*/
 
419
struct Name_resolution_context: Sql_alloc
 
420
{
 
421
  /*
 
422
    The name resolution context to search in when an Item cannot be
 
423
    resolved in this context (the context of an outer select)
 
424
  */
 
425
  Name_resolution_context *outer_context;
 
426
 
 
427
  /*
 
428
    List of tables used to resolve the items of this context.  Usually these
 
429
    are tables from the FROM clause of SELECT statement.  The exceptions are
 
430
    INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
 
431
    subquery is not moved to a separate SELECT_LEX.  For these types of
 
432
    statements we have to change this member dynamically to ensure correct
 
433
    name resolution of different parts of the statement.
 
434
  */
 
435
  TABLE_LIST *table_list;
 
436
  /*
 
437
    In most cases the two table references below replace 'table_list' above
 
438
    for the purpose of name resolution. The first and last name resolution
 
439
    table references allow us to search only in a sub-tree of the nested
 
440
    join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
 
441
    and JOIN ... ON. 
 
442
  */
 
443
  TABLE_LIST *first_name_resolution_table;
 
444
  /*
 
445
    Last table to search in the list of leaf table references that begins
 
446
    with first_name_resolution_table.
 
447
  */
 
448
  TABLE_LIST *last_name_resolution_table;
 
449
 
 
450
  /*
 
451
    SELECT_LEX item belong to, in case of merged VIEW it can differ from
 
452
    SELECT_LEX where item was created, so we can't use table_list/field_list
 
453
    from there
 
454
  */
 
455
  st_select_lex *select_lex;
 
456
 
 
457
  /*
 
458
    Processor of errors caused during Item name resolving, now used only to
 
459
    hide underlying tables in errors about views (i.e. it substitute some
 
460
    errors for views)
 
461
  */
 
462
  void (*error_processor)(THD *, void *);
 
463
  void *error_processor_data;
 
464
 
 
465
  /**
 
466
    When TRUE, items are resolved in this context against
 
467
    SELECT_LEX::item_list, SELECT_lex::group_list and
 
468
    this->table_list. If FALSE, items are resolved only against
 
469
    this->table_list.
 
470
 
 
471
    @see st_select_lex::item_list, st_select_lex::group_list
 
472
  */
 
473
  bool resolve_in_select_list;
 
474
 
 
475
  /*
 
476
    Security context of this name resolution context. It's used for views
 
477
    and is non-zero only if the view is defined with SQL SECURITY DEFINER.
 
478
  */
 
479
  Security_context *security_ctx;
 
480
 
 
481
  Name_resolution_context()
 
482
    :outer_context(0), table_list(0), select_lex(0),
 
483
    error_processor_data(0),
 
484
    security_ctx(0)
 
485
    {}
 
486
 
 
487
  void init()
 
488
  {
 
489
    resolve_in_select_list= FALSE;
 
490
    error_processor= &dummy_error_processor;
 
491
    first_name_resolution_table= NULL;
 
492
    last_name_resolution_table= NULL;
 
493
  }
 
494
 
 
495
  void resolve_in_table_list_only(TABLE_LIST *tables)
 
496
  {
 
497
    table_list= first_name_resolution_table= tables;
 
498
    resolve_in_select_list= FALSE;
 
499
  }
 
500
 
 
501
  void process_error(THD *thd)
 
502
  {
 
503
    (*error_processor)(thd, error_processor_data);
 
504
  }
 
505
};
 
506
 
 
507
 
 
508
/*
 
509
  Store and restore the current state of a name resolution context.
 
510
*/
 
511
 
 
512
class Name_resolution_context_state
 
513
{
 
514
private:
 
515
  TABLE_LIST *save_table_list;
 
516
  TABLE_LIST *save_first_name_resolution_table;
 
517
  TABLE_LIST *save_next_name_resolution_table;
 
518
  bool        save_resolve_in_select_list;
 
519
  TABLE_LIST *save_next_local;
 
520
 
 
521
public:
 
522
  Name_resolution_context_state() {}          /* Remove gcc warning */
 
523
 
 
524
public:
 
525
  /* Save the state of a name resolution context. */
 
526
  void save_state(Name_resolution_context *context, TABLE_LIST *table_list)
 
527
  {
 
528
    save_table_list=                  context->table_list;
 
529
    save_first_name_resolution_table= context->first_name_resolution_table;
 
530
    save_resolve_in_select_list=      context->resolve_in_select_list;
 
531
    save_next_local=                  table_list->next_local;
 
532
    save_next_name_resolution_table=  table_list->next_name_resolution_table;
 
533
  }
 
534
 
 
535
  /* Restore a name resolution context from saved state. */
 
536
  void restore_state(Name_resolution_context *context, TABLE_LIST *table_list)
 
537
  {
 
538
    table_list->next_local=                save_next_local;
 
539
    table_list->next_name_resolution_table= save_next_name_resolution_table;
 
540
    context->table_list=                   save_table_list;
 
541
    context->first_name_resolution_table=  save_first_name_resolution_table;
 
542
    context->resolve_in_select_list=       save_resolve_in_select_list;
 
543
  }
 
544
 
 
545
  TABLE_LIST *get_first_name_resolution_table()
 
546
  {
 
547
    return save_first_name_resolution_table;
 
548
  }
 
549
};
 
550
 
 
551
 
 
552
/*
 
553
  This enum is used to report information about monotonicity of function
 
554
  represented by Item* tree.
 
555
  Monotonicity is defined only for Item* trees that represent table
 
556
  partitioning expressions (i.e. have no subselects/user vars/PS parameters
 
557
  etc etc). An Item* tree is assumed to have the same monotonicity properties
 
558
  as its correspoinding function F:
 
559
 
 
560
  [signed] longlong F(field1, field2, ...) {
 
561
    put values of field_i into table record buffer;
 
562
    return item->val_int(); 
 
563
  }
 
564
 
 
565
  NOTE
 
566
  At the moment function monotonicity is not well defined (and so may be
 
567
  incorrect) for Item trees with parameters/return types that are different
 
568
  from INT_RESULT, may be NULL, or are unsigned.
 
569
  It will be possible to address this issue once the related partitioning bugs
 
570
  (BUG#16002, BUG#15447, BUG#13436) are fixed.
 
571
 
 
572
  The NOT_NULL enums are used in TO_DAYS, since TO_DAYS('2001-00-00') returns
 
573
  NULL which puts those rows into the NULL partition, but
 
574
  '2000-12-31' < '2001-00-00' < '2001-01-01'. So special handling is needed
 
575
  for this (see Bug#20577).
 
576
*/
 
577
 
 
578
typedef enum monotonicity_info 
 
579
{
 
580
   NON_MONOTONIC,              /* none of the below holds */
 
581
   MONOTONIC_INCREASING,       /* F() is unary and (x < y) => (F(x) <= F(y)) */
 
582
   MONOTONIC_INCREASING_NOT_NULL,  /* But only for valid/real x and y */
 
583
   MONOTONIC_STRICT_INCREASING,/* F() is unary and (x < y) => (F(x) <  F(y)) */
 
584
   MONOTONIC_STRICT_INCREASING_NOT_NULL  /* But only for valid/real x and y */
 
585
} enum_monotonicity_info;
 
586
 
 
587
/*************************************************************************/
 
588
 
 
589
class sp_rcontext;
 
590
 
 
591
 
 
592
class Settable_routine_parameter
 
593
{
 
594
public:
 
595
  /*
 
596
    Set required privileges for accessing the parameter.
 
597
 
 
598
    SYNOPSIS
 
599
      set_required_privilege()
 
600
        rw        if 'rw' is true then we are going to read and set the
 
601
                  parameter, so SELECT and UPDATE privileges might be
 
602
                  required, otherwise we only reading it and SELECT
 
603
                  privilege might be required.
 
604
  */
 
605
  Settable_routine_parameter() {}
 
606
  virtual ~Settable_routine_parameter() {}
 
607
  virtual void set_required_privilege(bool rw) {};
 
608
 
 
609
  /*
 
610
    Set parameter value.
 
611
 
 
612
    SYNOPSIS
 
613
      set_value()
 
614
        thd       thread handle
 
615
        ctx       context to which parameter belongs (if it is local
 
616
                  variable).
 
617
        it        item which represents new value
 
618
 
 
619
    RETURN
 
620
      FALSE if parameter value has been set,
 
621
      TRUE if error has occured.
 
622
  */
 
623
  virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it)= 0;
 
624
 
 
625
  virtual void set_out_param_info(Send_field *info) {}
 
626
 
 
627
  virtual const Send_field *get_out_param_info() const
 
628
  { return NULL; }
 
629
};
 
630
 
 
631
 
 
632
typedef bool (Item::*Item_processor) (uchar *arg);
 
633
/*
 
634
  Analyzer function
 
635
    SYNOPSIS
 
636
      argp   in/out IN:  Analysis parameter
 
637
                    OUT: Parameter to be passed to the transformer
 
638
 
 
639
    RETURN 
 
640
      TRUE   Invoke the transformer
 
641
      FALSE  Don't do it
 
642
 
 
643
*/
 
644
typedef bool (Item::*Item_analyzer) (uchar **argp);
 
645
typedef Item* (Item::*Item_transformer) (uchar *arg);
 
646
typedef void (*Cond_traverser) (const Item *item, void *arg);
 
647
 
 
648
 
 
649
class Item
 
650
{
 
651
  Item(const Item &);                   /* Prevent use of these */
 
652
  void operator=(Item &);
 
653
  /* Cache of the result of is_expensive(). */
 
654
  int8 is_expensive_cache;
 
655
  virtual bool is_expensive_processor(uchar *arg) { return 0; }
 
656
 
 
657
public:
 
658
  static void *operator new(size_t size) throw ()
 
659
  { return sql_alloc(size); }
 
660
  static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
 
661
  { return alloc_root(mem_root, size); }
 
662
  static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
 
663
  static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
 
664
 
 
665
  enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM,
 
666
             INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM,
 
667
             COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
 
668
             PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM,
 
669
             FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM,
 
670
             SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER,
 
671
             PARAM_ITEM, TRIGGER_FIELD_ITEM, DECIMAL_ITEM,
 
672
             XPATH_NODESET, XPATH_NODESET_CMP,
 
673
             VIEW_FIXER_ITEM};
 
674
 
 
675
  enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
 
676
 
 
677
  enum traverse_order { POSTFIX, PREFIX };
 
678
  
 
679
  /* Reuse size, only used by SP local variable assignment, otherwize 0 */
 
680
  uint rsize;
 
681
 
 
682
  /*
 
683
    str_values's main purpose is to be used to cache the value in
 
684
    save_in_field
 
685
  */
 
686
  String str_value;
 
687
 
 
688
  Item_name_string item_name;  /* Name from select */
 
689
  Item_name_string orig_name;  /* Original item name (if it was renamed)*/
 
690
 
 
691
  /**
 
692
     Intrusive list pointer for free list. If not null, points to the next
 
693
     Item on some Query_arena's free list. For instance, stored procedures
 
694
     have their own Query_arena's.
 
695
 
 
696
     @see Query_arena::free_list
 
697
   */
 
698
  Item *next;
 
699
  uint32 max_length;                    /* Maximum length, in bytes */
 
700
  /**
 
701
     This member has several successive meanings, depending on the phase we're
 
702
     in:
 
703
     - during field resolution: it contains the index, in the "all_fields"
 
704
     list, of the expression to which this field belongs; or a special
 
705
     constant UNDEF_POS; see st_select_lex::cur_pos_in_all_fields and
 
706
     match_exprs_for_only_full_group_by().
 
707
     - when attaching conditions to tables: it says whether some condition
 
708
     needs to be attached or can be omitted (for example because it is already
 
709
     implemented by 'ref' access)
 
710
     - when pushing index conditions: it says whether a condition uses only
 
711
     indexed columns
 
712
     - when creating an internal temporary table: it says how to store BIT
 
713
     fields
 
714
     - when we change DISTINCT to GROUP BY: it is used for book-keeping of
 
715
     fields.
 
716
  */
 
717
  int marker;
 
718
  uint8 decimals;
 
719
  my_bool maybe_null;                   /* If item may be null */
 
720
  my_bool null_value;                   /* if item is null */
 
721
  my_bool unsigned_flag;
 
722
  my_bool with_sum_func;
 
723
  my_bool fixed;                        /* If item fixed with fix_fields */
 
724
  DTCollation collation;
 
725
  Item_result cmp_context;              /* Comparison context */
 
726
 protected:
 
727
  my_bool with_subselect;               /* If this item is a subselect or some
 
728
                                           of its arguments is or contains a
 
729
                                           subselect. Computed by fix_fields
 
730
                                           and updated by update_used_tables. */
 
731
  my_bool with_stored_program;          /* If this item is a stored program
 
732
                                           or some of its arguments is or
 
733
                                           contains a stored program.
 
734
                                           Computed by fix_fields and updated
 
735
                                           by update_used_tables. */
 
736
 
 
737
  /**
 
738
    This variable is a cache of 'Needed tables are locked'. True if either
 
739
    'No tables locks is needed' or 'Needed tables are locked'.
 
740
    If tables are used, then it will be set to
 
741
    current_thd->lex->is_query_tables_locked().
 
742
 
 
743
    It is used when checking const_item()/can_be_evaluated_now().
 
744
  */
 
745
  bool tables_locked_cache;
 
746
 public:
 
747
  // alloc & destruct is done as start of select using sql_alloc
 
748
  Item();
 
749
  /*
 
750
     Constructor used by Item_field, Item_ref & aggregate (sum) functions.
 
751
     Used for duplicating lists in processing queries with temporary
 
752
     tables
 
753
     Also it used for Item_cond_and/Item_cond_or for creating
 
754
     top AND/OR structure of WHERE clause to protect it of
 
755
     optimisation changes in prepared statements
 
756
  */
 
757
  Item(THD *thd, Item *item);
 
758
  virtual ~Item()
 
759
  {
 
760
#ifdef EXTRA_DEBUG
 
761
    item_name.set(0);
 
762
#endif
 
763
  }             /*lint -e1509 */
 
764
  void rename(char *new_name);
 
765
  void init_make_field(Send_field *tmp_field,enum enum_field_types type);
 
766
  virtual void cleanup();
 
767
  virtual void make_field(Send_field *field);
 
768
  virtual Field *make_string_field(TABLE *table);
 
769
  virtual bool fix_fields(THD *, Item **);
 
770
  /**
 
771
    Fix after tables have been moved from one select_lex level to the parent
 
772
    level, e.g by semijoin conversion.
 
773
    Basically re-calculate all attributes dependent on the tables.
 
774
 
 
775
    @param parent_select  select_lex that tables are moved to.
 
776
    @param removed_select select_lex that tables are moved away from,
 
777
                          child of parent_select.
 
778
  */
 
779
  virtual void fix_after_pullout(st_select_lex *parent_select,
 
780
                                 st_select_lex *removed_select)
 
781
  {};
 
782
  /*
 
783
    should be used in case where we are sure that we do not need
 
784
    complete fix_fields() procedure.
 
785
  */
 
786
  inline void quick_fix_field() { fixed= 1; }
 
787
  /* Function returns 1 on overflow and -1 on fatal errors */
 
788
  type_conversion_status save_in_field_no_warnings(Field *field,
 
789
                                                   bool no_conversions);
 
790
  /**
 
791
    Save a temporal value in packed longlong format into a Field.
 
792
    Used in optimizer.
 
793
    @param OUT field  The field to set the value to.
 
794
    @retval 0         On success.
 
795
    @retval >0        In error.
 
796
  */
 
797
  virtual type_conversion_status save_in_field(Field *field,
 
798
                                               bool no_conversions);
 
799
  virtual void save_org_in_field(Field *field)
 
800
  { (void) save_in_field(field, 1); }
 
801
  virtual type_conversion_status save_safe_in_field(Field *field)
 
802
  { return save_in_field(field, 1); }
 
803
  virtual bool send(Protocol *protocol, String *str);
 
804
  virtual bool eq(const Item *, bool binary_cmp) const;
 
805
  virtual Item_result result_type() const { return REAL_RESULT; }
 
806
  /**
 
807
    Result type when an item appear in a numeric context.
 
808
    See Field::numeric_context_result_type() for more comments.
 
809
  */
 
810
  virtual enum Item_result numeric_context_result_type() const
 
811
  {
 
812
    if (is_temporal())
 
813
      return decimals ? DECIMAL_RESULT : INT_RESULT;
 
814
    if (result_type() == STRING_RESULT)
 
815
      return REAL_RESULT; 
 
816
    return result_type();
 
817
  }
 
818
  /**
 
819
    Similar to result_type() but makes DATE, DATETIME, TIMESTAMP
 
820
    pretend to be numbers rather than strings.
 
821
  */
 
822
  inline enum Item_result temporal_with_date_as_number_result_type() const
 
823
  {
 
824
    return is_temporal_with_date() ? 
 
825
           (decimals ? DECIMAL_RESULT : INT_RESULT) : result_type();
 
826
  }
 
827
  virtual Item_result cast_to_int_type() const { return result_type(); }
 
828
  virtual enum_field_types string_field_type() const;
 
829
  virtual enum_field_types field_type() const;
 
830
  virtual enum Type type() const =0;
 
831
  
 
832
  /*
 
833
    Return information about function monotonicity. See comment for
 
834
    enum_monotonicity_info for details. This function can only be called
 
835
    after fix_fields() call.
 
836
  */
 
837
  virtual enum_monotonicity_info get_monotonicity_info() const
 
838
  { return NON_MONOTONIC; }
 
839
 
 
840
  /*
 
841
    Convert "func_arg $CMP$ const" half-interval into "FUNC(func_arg) $CMP2$ const2"
 
842
 
 
843
    SYNOPSIS
 
844
      val_int_endpoint()
 
845
        left_endp  FALSE  <=> The interval is "x < const" or "x <= const"
 
846
                   TRUE   <=> The interval is "x > const" or "x >= const"
 
847
 
 
848
        incl_endp  IN   FALSE <=> the comparison is '<' or '>'
 
849
                        TRUE  <=> the comparison is '<=' or '>='
 
850
                   OUT  The same but for the "F(x) $CMP$ F(const)" comparison
 
851
 
 
852
    DESCRIPTION
 
853
      This function is defined only for unary monotonic functions. The caller
 
854
      supplies the source half-interval
 
855
 
 
856
         x $CMP$ const
 
857
 
 
858
      The value of const is supplied implicitly as the value this item's
 
859
      argument, the form of $CMP$ comparison is specified through the
 
860
      function's arguments. The calle returns the result interval
 
861
         
 
862
         F(x) $CMP2$ F(const)
 
863
      
 
864
      passing back F(const) as the return value, and the form of $CMP2$ 
 
865
      through the out parameter. NULL values are assumed to be comparable and
 
866
      be less than any non-NULL values.
 
867
 
 
868
    RETURN
 
869
      The output range bound, which equal to the value of val_int()
 
870
        - If the value of the function is NULL then the bound is the 
 
871
          smallest possible value of LONGLONG_MIN 
 
872
  */
 
873
  virtual longlong val_int_endpoint(bool left_endp, bool *incl_endp)
 
874
  { DBUG_ASSERT(0); return 0; }
 
875
 
 
876
 
 
877
  /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
 
878
  /*
 
879
    Return double precision floating point representation of item.
 
880
 
 
881
    SYNOPSIS
 
882
      val_real()
 
883
 
 
884
    RETURN
 
885
      In case of NULL value return 0.0 and set null_value flag to TRUE.
 
886
      If value is not null null_value flag will be reset to FALSE.
 
887
  */
 
888
  virtual double val_real()=0;
 
889
  /*
 
890
    Return integer representation of item.
 
891
 
 
892
    SYNOPSIS
 
893
      val_int()
 
894
 
 
895
    RETURN
 
896
      In case of NULL value return 0 and set null_value flag to TRUE.
 
897
      If value is not null null_value flag will be reset to FALSE.
 
898
  */
 
899
  virtual longlong val_int()=0;
 
900
  /**
 
901
    Return date value of item in packed longlong format.
 
902
  */
 
903
  virtual longlong val_date_temporal();
 
904
  /**
 
905
    Return time value of item in packed longlong format.
 
906
  */
 
907
  virtual longlong val_time_temporal();
 
908
  /**
 
909
    Return date or time value of item in packed longlong format,
 
910
    depending on item field type.
 
911
  */
 
912
  longlong val_temporal_by_field_type()
 
913
  {
 
914
    if (field_type() == MYSQL_TYPE_TIME)
 
915
      return val_time_temporal();
 
916
    DBUG_ASSERT(is_temporal_with_date());
 
917
    return val_date_temporal();
 
918
  }
 
919
  /**
 
920
    Get date or time value in packed longlong format.
 
921
    Before conversion from MYSQL_TIME to packed format,
 
922
    the MYSQL_TIME value is rounded to "dec" fractional digits.
 
923
  */
 
924
  longlong val_temporal_with_round(enum_field_types type, uint8 dec);
 
925
 
 
926
  /*
 
927
    This is just a shortcut to avoid the cast. You should still use
 
928
    unsigned_flag to check the sign of the item.
 
929
  */
 
930
  inline ulonglong val_uint() { return (ulonglong) val_int(); }
 
931
  /*
 
932
    Return string representation of this item object.
 
933
 
 
934
    SYNOPSIS
 
935
      val_str()
 
936
      str   an allocated buffer this or any nested Item object can use to
 
937
            store return value of this method.
 
938
 
 
939
    NOTE
 
940
      Buffer passed via argument  should only be used if the item itself
 
941
      doesn't have an own String buffer. In case when the item maintains
 
942
      it's own string buffer, it's preferable to return it instead to
 
943
      minimize number of mallocs/memcpys.
 
944
      The caller of this method can modify returned string, but only in case
 
945
      when it was allocated on heap, (is_alloced() is true).  This allows
 
946
      the caller to efficiently use a buffer allocated by a child without
 
947
      having to allocate a buffer of it's own. The buffer, given to
 
948
      val_str() as argument, belongs to the caller and is later used by the
 
949
      caller at it's own choosing.
 
950
      A few implications from the above:
 
951
      - unless you return a string object which only points to your buffer
 
952
        but doesn't manages it you should be ready that it will be
 
953
        modified.
 
954
      - even for not allocated strings (is_alloced() == false) the caller
 
955
        can change charset (see Item_func_{typecast/binary}. XXX: is this
 
956
        a bug?
 
957
      - still you should try to minimize data copying and return internal
 
958
        object whenever possible.
 
959
 
 
960
    RETURN
 
961
      In case of NULL value return 0 (NULL pointer) and set null_value flag
 
962
      to TRUE.
 
963
      If value is not null null_value flag will be reset to FALSE.
 
964
  */
 
965
  virtual String *val_str(String *str)=0;
 
966
 
 
967
  /*
 
968
    Returns string representation of this item in ASCII format.
 
969
 
 
970
    SYNOPSIS
 
971
      val_str_ascii()
 
972
      str - similar to val_str();
 
973
 
 
974
    NOTE
 
975
      This method is introduced for performance optimization purposes.
 
976
 
 
977
      1. val_str() result of some Items in string context
 
978
      depends on @@character_set_results.
 
979
      @@character_set_results can be set to a "real multibyte" character
 
980
      set like UCS2, UTF16, UTF32. (We'll use only UTF32 in the examples
 
981
      below for convenience.)
 
982
 
 
983
      So the default string result of such functions
 
984
      in these circumstances is real multi-byte character set, like UTF32.
 
985
 
 
986
      For example, all numbers in string context
 
987
      return result in @@character_set_results:
 
988
 
 
989
      SELECT CONCAT(20010101); -> UTF32
 
990
 
 
991
      We do sprintf() first (to get ASCII representation)
 
992
      and then convert to UTF32;
 
993
      
 
994
      So these kind "data sources" can use ASCII representation
 
995
      internally, but return multi-byte data only because
 
996
      @@character_set_results wants so.
 
997
      Therefore, conversion from ASCII to UTF32 is applied internally.
 
998
 
 
999
 
 
1000
      2. Some other functions need in fact ASCII input.
 
1001
 
 
1002
      For example,
 
1003
        inet_aton(), GeometryFromText(), Convert_TZ(), GET_FORMAT().
 
1004
 
 
1005
      Similar, fields of certain type, like DATE, TIME,
 
1006
      when you insert string data into them, expect in fact ASCII input.
 
1007
      If they get non-ASCII input, for example UTF32, they
 
1008
      convert input from UTF32 to ASCII, and then use ASCII
 
1009
      representation to do further processing.
 
1010
 
 
1011
 
 
1012
      3. Now imagine we pass result of a data source of the first type
 
1013
         to a data destination of the second type.
 
1014
 
 
1015
      What happens:
 
1016
        a. data source converts data from ASCII to UTF32, because
 
1017
           @@character_set_results wants so and passes the result to
 
1018
           data destination.
 
1019
        b. data destination gets UTF32 string.
 
1020
        c. data destination converts UTF32 string to ASCII,
 
1021
           because it needs ASCII representation to be able to handle data
 
1022
           correctly.
 
1023
 
 
1024
      As a result we get two steps of unnecessary conversion:
 
1025
      From ASCII to UTF32, then from UTF32 to ASCII.
 
1026
 
 
1027
      A better way to handle these situations is to pass ASCII
 
1028
      representation directly from the source to the destination.
 
1029
 
 
1030
      This is why val_str_ascii() introduced.
 
1031
 
 
1032
    RETURN
 
1033
      Similar to val_str()
 
1034
  */
 
1035
  virtual String *val_str_ascii(String *str);
 
1036
  
 
1037
  /*
 
1038
    Return decimal representation of item with fixed point.
 
1039
 
 
1040
    SYNOPSIS
 
1041
      val_decimal()
 
1042
      decimal_buffer  buffer which can be used by Item for returning value
 
1043
                      (but can be not)
 
1044
 
 
1045
    NOTE
 
1046
      Returned value should not be changed if it is not the same which was
 
1047
      passed via argument.
 
1048
 
 
1049
    RETURN
 
1050
      Return pointer on my_decimal (it can be other then passed via argument)
 
1051
        if value is not NULL (null_value flag will be reset to FALSE).
 
1052
      In case of NULL value it return 0 pointer and set null_value flag
 
1053
        to TRUE.
 
1054
  */
 
1055
  virtual my_decimal *val_decimal(my_decimal *decimal_buffer)= 0;
 
1056
  /*
 
1057
    Return boolean value of item.
 
1058
 
 
1059
    RETURN
 
1060
      FALSE value is false or NULL
 
1061
      TRUE value is true (not equal to 0)
 
1062
  */
 
1063
  virtual bool val_bool();
 
1064
  virtual String *val_nodeset(String*) { return 0; }
 
1065
 
 
1066
protected:
 
1067
  /* Helper functions, see item_sum.cc */
 
1068
  String *val_string_from_real(String *str);
 
1069
  String *val_string_from_int(String *str);
 
1070
  String *val_string_from_decimal(String *str);
 
1071
  String *val_string_from_date(String *str);
 
1072
  String *val_string_from_datetime(String *str);
 
1073
  String *val_string_from_time(String *str);
 
1074
  my_decimal *val_decimal_from_real(my_decimal *decimal_value);
 
1075
  my_decimal *val_decimal_from_int(my_decimal *decimal_value);
 
1076
  my_decimal *val_decimal_from_string(my_decimal *decimal_value);
 
1077
  my_decimal *val_decimal_from_date(my_decimal *decimal_value);
 
1078
  my_decimal *val_decimal_from_time(my_decimal *decimal_value);
 
1079
  longlong val_int_from_decimal();
 
1080
  longlong val_int_from_date();
 
1081
  longlong val_int_from_time();
 
1082
  longlong val_int_from_datetime();
 
1083
  double val_real_from_decimal();
 
1084
 
 
1085
  /**
 
1086
    Convert val_str() to date in MYSQL_TIME
 
1087
  */
 
1088
  bool get_date_from_string(MYSQL_TIME *ltime, uint flags);
 
1089
  /**
 
1090
    Convert val_real() to date in MYSQL_TIME
 
1091
  */
 
1092
  bool get_date_from_real(MYSQL_TIME *ltime, uint flags);
 
1093
  /**
 
1094
    Convert val_decimal() to date in MYSQL_TIME
 
1095
  */
 
1096
  bool get_date_from_decimal(MYSQL_TIME *ltime, uint flags);
 
1097
  /**
 
1098
    Convert val_int() to date in MYSQL_TIME
 
1099
  */
 
1100
  bool get_date_from_int(MYSQL_TIME *ltime, uint flags);
 
1101
  /**
 
1102
    Convert get_time() from time to date in MYSQL_TIME
 
1103
  */
 
1104
  bool get_date_from_time(MYSQL_TIME *ltime);
 
1105
 
 
1106
  /**
 
1107
    Convert a numeric type to date
 
1108
  */
 
1109
  bool get_date_from_numeric(MYSQL_TIME *ltime, uint fuzzydate);
 
1110
 
 
1111
  /**
 
1112
    Convert a non-temporal type to date
 
1113
  */
 
1114
  bool get_date_from_non_temporal(MYSQL_TIME *ltime, uint fuzzydate);
 
1115
 
 
1116
  /**
 
1117
    Convert val_str() to time in MYSQL_TIME
 
1118
  */
 
1119
  bool get_time_from_string(MYSQL_TIME *ltime);
 
1120
  /**
 
1121
    Convert val_real() to time in MYSQL_TIME
 
1122
  */
 
1123
  bool get_time_from_real(MYSQL_TIME *ltime);
 
1124
  /**
 
1125
    Convert val_decimal() to time in MYSQL_TIME
 
1126
  */
 
1127
  bool get_time_from_decimal(MYSQL_TIME *ltime);
 
1128
  /**
 
1129
    Convert val_int() to time in MYSQL_TIME
 
1130
  */
 
1131
  bool get_time_from_int(MYSQL_TIME *ltime);
 
1132
  /**
 
1133
    Convert date to time
 
1134
  */
 
1135
  bool get_time_from_date(MYSQL_TIME *ltime);
 
1136
  /**
 
1137
    Convert datetime to time
 
1138
  */
 
1139
  bool get_time_from_datetime(MYSQL_TIME *ltime);
 
1140
 
 
1141
  /**
 
1142
    Convert a numeric type to time
 
1143
  */
 
1144
  bool get_time_from_numeric(MYSQL_TIME *ltime);
 
1145
 
 
1146
  /**
 
1147
    Convert a non-temporal type to time
 
1148
  */
 
1149
  bool get_time_from_non_temporal(MYSQL_TIME *ltime);
 
1150
 
 
1151
 
 
1152
public:
 
1153
 
 
1154
  type_conversion_status save_time_in_field(Field *field);
 
1155
  type_conversion_status save_date_in_field(Field *field);
 
1156
  type_conversion_status save_str_value_in_field(Field *field, String *result);
 
1157
 
 
1158
  virtual Field *get_tmp_table_field() { return 0; }
 
1159
  /* This is also used to create fields in CREATE ... SELECT: */
 
1160
  virtual Field *tmp_table_field(TABLE *t_arg) { return 0; }
 
1161
  virtual const char *full_name() const
 
1162
  {
 
1163
    return item_name.is_set() ? item_name.ptr() : "???";
 
1164
  }
 
1165
 
 
1166
  /*
 
1167
    *result* family of methods is analog of *val* family (see above) but
 
1168
    return value of result_field of item if it is present. If Item have not
 
1169
    result field, it return val(). This methods set null_value flag in same
 
1170
    way as *val* methods do it.
 
1171
  */
 
1172
  virtual double  val_result() { return val_real(); }
 
1173
  virtual longlong val_int_result() { return val_int(); }
 
1174
  /**
 
1175
    Get time value in packed longlong format. NULL is converted to 0.
 
1176
  */
 
1177
  virtual longlong val_time_temporal_result() { return val_time_temporal(); }
 
1178
  /**
 
1179
    Get date value in packed longlong format. NULL is converted to 0.
 
1180
  */
 
1181
  virtual longlong val_date_temporal_result() { return val_date_temporal(); }
 
1182
  virtual String *str_result(String* tmp) { return val_str(tmp); }
 
1183
  virtual my_decimal *val_decimal_result(my_decimal *val)
 
1184
  { return val_decimal(val); }
 
1185
  virtual bool val_bool_result() { return val_bool(); }
 
1186
  virtual bool is_null_result() { return is_null(); }
 
1187
 
 
1188
  /* bit map of tables used by item */
 
1189
  virtual table_map used_tables() const { return (table_map) 0L; }
 
1190
  /**
 
1191
    Return used table information for the level this item is resolved on.
 
1192
     - For fields, this returns the table the item is resolved from.
 
1193
     - For all other items, this behaves like used_tables().
 
1194
 
 
1195
    @note: Use this function with caution. External calls to this function
 
1196
           should only be made for class objects derived from Item_ident.
 
1197
           Item::resolved_used_tables is for internal use only, in order to
 
1198
           process fields underlying a view column reference.
 
1199
  */
 
1200
  virtual table_map resolved_used_tables() const
 
1201
  {
 
1202
    // As this is the level this item was resolved on, it cannot be outer:
 
1203
    DBUG_ASSERT(!(used_tables() & OUTER_REF_TABLE_BIT));
 
1204
 
 
1205
    return used_tables();
 
1206
  }
 
1207
  /*
 
1208
    Return table map of tables that can't be NULL tables (tables that are
 
1209
    used in a context where if they would contain a NULL row generated
 
1210
    by a LEFT or RIGHT join, the item would not be true).
 
1211
    This expression is used on WHERE item to determinate if a LEFT JOIN can be
 
1212
    converted to a normal join.
 
1213
    Generally this function should return used_tables() if the function
 
1214
    would return null if any of the arguments are null
 
1215
    As this is only used in the beginning of optimization, the value don't
 
1216
    have to be updated in update_used_tables()
 
1217
  */
 
1218
  virtual table_map not_null_tables() const { return used_tables(); }
 
1219
  /*
 
1220
    Returns true if this is a simple constant item like an integer, not
 
1221
    a constant expression. Used in the optimizer to propagate basic constants.
 
1222
  */
 
1223
  virtual bool basic_const_item() const { return 0; }
 
1224
  /* cloning of constant items (0 if it is not const) */
 
1225
  virtual Item *clone_item() { return 0; }
 
1226
  virtual cond_result eq_cmp_result() const { return COND_OK; }
 
1227
  inline uint float_length(uint decimals_par) const
 
1228
  { return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;}
 
1229
  virtual uint decimal_precision() const;
 
1230
  inline int decimal_int_part() const
 
1231
  { return my_decimal_int_part(decimal_precision(), decimals); }
 
1232
  /**
 
1233
    TIME precision of the item: 0..6
 
1234
  */
 
1235
  virtual uint time_precision();
 
1236
  /**
 
1237
    DATETIME precision of the item: 0..6
 
1238
  */
 
1239
  virtual uint datetime_precision();
 
1240
  /* 
 
1241
    Returns true if this is constant (during query execution, i.e. its value
 
1242
    will not change until next fix_fields) and its value is known.
 
1243
    When the default implementation of used_tables() is effective, this
 
1244
    function will always return true (because used_tables() is empty).
 
1245
  */
 
1246
  virtual bool const_item() const
 
1247
  {
 
1248
    if (used_tables() == 0)
 
1249
      return can_be_evaluated_now();
 
1250
    return false;
 
1251
  }
 
1252
  /* 
 
1253
    Returns true if this is constant but its value may be not known yet.
 
1254
    (Can be used for parameters of prep. stmts or of stored procedures.)
 
1255
  */
 
1256
  virtual bool const_during_execution() const 
 
1257
  { return (used_tables() & ~PARAM_TABLE_BIT) == 0; }
 
1258
 
 
1259
  /**
 
1260
    This method is used for to:
 
1261
      - to generate a view definition query (SELECT-statement);
 
1262
      - to generate a SQL-query for EXPLAIN EXTENDED;
 
1263
      - to generate a SQL-query to be shown in INFORMATION_SCHEMA;
 
1264
      - debug.
 
1265
 
 
1266
    For more information about view definition query, INFORMATION_SCHEMA
 
1267
    query and why they should be generated from the Item-tree, @see
 
1268
    mysql_register_view().
 
1269
  */
 
1270
  virtual inline void print(String *str, enum_query_type query_type)
 
1271
  {
 
1272
    str->append(full_name());
 
1273
  }
 
1274
 
 
1275
  void print_item_w_name(String *, enum_query_type query_type);
 
1276
  /**
 
1277
     Prints the item when it's part of ORDER BY and GROUP BY.
 
1278
     @param  str            String to print to
 
1279
     @param  query_type     How to format the item
 
1280
     @param  used_alias     Whether item was referenced with alias.
 
1281
  */
 
1282
  void print_for_order(String *str, enum_query_type query_type,
 
1283
                       bool used_alias);
 
1284
 
 
1285
  virtual void update_used_tables() {}
 
1286
  virtual void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
 
1287
                              List<Item> &fields) {}
 
1288
  /* Called for items that really have to be split */
 
1289
  void split_sum_func2(THD *thd, Ref_ptr_array ref_pointer_array,
 
1290
                       List<Item> &fields,
 
1291
                       Item **ref, bool skip_registered);
 
1292
  virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate)= 0;
 
1293
  virtual bool get_time(MYSQL_TIME *ltime)= 0;
 
1294
  /**
 
1295
    Get timestamp in "struct timeval" format.
 
1296
    @retval  false on success
 
1297
    @retval  true  on error
 
1298
  */
 
1299
  virtual bool get_timeval(struct timeval *tm, int *warnings);
 
1300
  virtual bool get_date_result(MYSQL_TIME *ltime,uint fuzzydate)
 
1301
  { return get_date(ltime,fuzzydate); }
 
1302
  /*
 
1303
    The method allows to determine nullness of a complex expression 
 
1304
    without fully evaluating it, instead of calling val/result*() then 
 
1305
    checking null_value. Used in Item_func_isnull/Item_func_isnotnull
 
1306
    and Item_sum_count/Item_sum_count_distinct.
 
1307
    Any new item which can be NULL must implement this method.
 
1308
  */
 
1309
  virtual bool is_null() { return 0; }
 
1310
 
 
1311
  /*
 
1312
   Make sure the null_value member has a correct value.
 
1313
  */
 
1314
  virtual void update_null_value () { (void) val_int(); }
 
1315
 
 
1316
  /*
 
1317
    Inform the item that there will be no distinction between its result
 
1318
    being FALSE or NULL.
 
1319
 
 
1320
    NOTE
 
1321
      This function will be called for eg. Items that are top-level AND-parts
 
1322
      of the WHERE clause. Items implementing this function (currently
 
1323
      Item_cond_and and subquery-related item) enable special optimizations
 
1324
      when they are "top level".
 
1325
  */
 
1326
  virtual void top_level_item() {}
 
1327
  /*
 
1328
    set field of temporary table for Item which can be switched on temporary
 
1329
    table during query processing (grouping and so on)
 
1330
  */
 
1331
  virtual void set_result_field(Field *field) {}
 
1332
  virtual bool is_result_field() { return 0; }
 
1333
  virtual bool is_bool_func() { return 0; }
 
1334
  virtual void save_in_result_field(bool no_conversions) {}
 
1335
  /*
 
1336
    Set value of aggregate function in case of no rows for grouping were found.
 
1337
    Also used for subqueries with outer references in SELECT list.
 
1338
  */
 
1339
  virtual void no_rows_in_result() {}
 
1340
  virtual Item *copy_or_same(THD *thd) { return this; }
 
1341
  /**
 
1342
     @param real_items  True <=> in the copy, replace any Item_ref with its
 
1343
     real_item()
 
1344
     @todo this argument should be always false and removed in WL#7082.
 
1345
  */
 
1346
  virtual Item *copy_andor_structure(THD *thd, bool real_items= false)
 
1347
  { return real_items ? real_item() : this; }
 
1348
  virtual Item *real_item() { return this; }
 
1349
  virtual Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
 
1350
 
 
1351
  static const CHARSET_INFO *default_charset();
 
1352
  virtual const CHARSET_INFO *compare_collation() { return NULL; }
 
1353
 
 
1354
  /*
 
1355
    For backward compatibility, to make numeric
 
1356
    data types return "binary" charset in client-side metadata.
 
1357
  */
 
1358
  virtual const CHARSET_INFO *charset_for_protocol(void) const
 
1359
  {
 
1360
    return result_type() == STRING_RESULT ? collation.collation :
 
1361
                                            &my_charset_bin;
 
1362
  };
 
1363
 
 
1364
  virtual bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
 
1365
  {
 
1366
    return (this->*processor)(arg);
 
1367
  }
 
1368
 
 
1369
  virtual Item* transform(Item_transformer transformer, uchar *arg);
 
1370
 
 
1371
  /*
 
1372
    This function performs a generic "compilation" of the Item tree.
 
1373
    The process of compilation is assumed to go as follows: 
 
1374
    
 
1375
    compile()
 
1376
    { 
 
1377
      if (this->*some_analyzer(...))
 
1378
      {
 
1379
        compile children if any;
 
1380
        return this->*some_transformer(...);
 
1381
      }
 
1382
      else
 
1383
        return this;
 
1384
    }
 
1385
 
 
1386
    i.e. analysis is performed top-down while transformation is done
 
1387
    bottom-up. If no transformation is applied, the item is returned unchanged.
 
1388
    A transformation error is indicated by returning a NULL pointer. Notice
 
1389
    that the analyzer function should never cause an error.
 
1390
  */
 
1391
  virtual Item* compile(Item_analyzer analyzer, uchar **arg_p,
 
1392
                        Item_transformer transformer, uchar *arg_t)
 
1393
  {
 
1394
    if ((this->*analyzer) (arg_p))
 
1395
      return ((this->*transformer) (arg_t));
 
1396
    return this;
 
1397
  }
 
1398
 
 
1399
   virtual void traverse_cond(Cond_traverser traverser,
 
1400
                              void *arg, traverse_order order)
 
1401
   {
 
1402
     (*traverser)(this, arg);
 
1403
   }
 
1404
 
 
1405
  /*
 
1406
    This is used to get the most recent version of any function in
 
1407
    an item tree. The version is the version where a MySQL function
 
1408
    was introduced in. So any function which is added should use
 
1409
    this function and set the int_arg to maximum of the input data
 
1410
    and their own version info.
 
1411
  */
 
1412
  virtual bool intro_version(uchar *int_arg) { return 0; }
 
1413
 
 
1414
  virtual bool remove_dependence_processor(uchar * arg) { return 0; }
 
1415
  virtual bool remove_fixed(uchar * arg) { fixed= 0; return 0; }
 
1416
  virtual bool cleanup_processor(uchar *arg);
 
1417
  virtual bool collect_item_field_processor(uchar * arg) { return 0; }
 
1418
  virtual bool add_field_to_set_processor(uchar * arg) { return 0; }
 
1419
 
 
1420
  /**
 
1421
     Visitor interface for removing all column expressions (Item_field) in
 
1422
     this expression tree from a bitmap. @See walk()
 
1423
 
 
1424
     @param arg  A MY_BITMAP* cast to unsigned char*, where the bits represent
 
1425
                 Field::field_index values.
 
1426
   */
 
1427
  virtual bool remove_column_from_bitmap(uchar *arg) { return false; }
 
1428
  virtual bool find_item_in_field_list_processor(uchar *arg) { return 0; }
 
1429
  virtual bool change_context_processor(uchar *context) { return 0; }
 
1430
  virtual bool reset_query_id_processor(uchar *query_id_arg) { return 0; }
 
1431
  virtual bool find_item_processor(uchar *arg) { return this == (void *) arg; }
 
1432
  virtual bool register_field_in_read_map(uchar *arg) { return 0; }
 
1433
  virtual bool inform_item_in_cond_of_tab(uchar *join_tab_index) { return false; }
 
1434
  /**
 
1435
     Clean up after removing the item from the item tree.
 
1436
 
 
1437
     @param arg Pointer to the st_select_lex from which the walk started, i.e.,
 
1438
                the st_select_lex that contained the clause that was removed.
 
1439
  */
 
1440
  virtual bool clean_up_after_removal(uchar *arg) { return false; }
 
1441
 
 
1442
  virtual bool cache_const_expr_analyzer(uchar **arg);
 
1443
  virtual Item* cache_const_expr_transformer(uchar *arg);
 
1444
 
 
1445
  /**
 
1446
     Analyzer for finding Item_field by name
 
1447
     
 
1448
     @param arg  Field name to search for
 
1449
     
 
1450
     @return TRUE Go deeper in item tree.  (Found Item or not an Item_field)
 
1451
     @return FALSE Don't go deeper in item tree. (Item_field with other name)
 
1452
  */
 
1453
  virtual bool item_field_by_name_analyzer(uchar **arg) { return true; };
 
1454
 
 
1455
  /**
 
1456
     Simple transformer that returns the argument if this is an Item_field.
 
1457
     The new item will inherit it's name to maintain aliases.
 
1458
 
 
1459
     @param arg Item to replace Item_field
 
1460
 
 
1461
     @return argument if this is an Item_field
 
1462
     @return this otherwise.
 
1463
  */
 
1464
  virtual Item* item_field_by_name_transformer(uchar *arg) { return this; }
 
1465
 
 
1466
  virtual bool equality_substitution_analyzer(uchar **arg) { return false; }
 
1467
 
 
1468
  virtual Item* equality_substitution_transformer(uchar *arg) { return this; }
 
1469
 
 
1470
  /*
 
1471
    Check if a partition function is allowed
 
1472
    SYNOPSIS
 
1473
      check_partition_func_processor()
 
1474
      int_arg                        Ignored
 
1475
    RETURN VALUE
 
1476
      TRUE                           Partition function not accepted
 
1477
      FALSE                          Partition function accepted
 
1478
 
 
1479
    DESCRIPTION
 
1480
    check_partition_func_processor is used to check if a partition function
 
1481
    uses an allowed function. An allowed function will always ensure that
 
1482
    X=Y guarantees that also part_function(X)=part_function(Y) where X is
 
1483
    a set of partition fields and so is Y. The problems comes mainly from
 
1484
    character sets where two equal strings can be quite unequal. E.g. the
 
1485
    german character for double s is equal to 2 s.
 
1486
 
 
1487
    The default is that an item is not allowed
 
1488
    in a partition function. Allowed functions
 
1489
    can never depend on server version, they cannot depend on anything
 
1490
    related to the environment. They can also only depend on a set of
 
1491
    fields in the table itself. They cannot depend on other tables and
 
1492
    cannot contain any queries and cannot contain udf's or similar.
 
1493
    If a new Item class is defined and it inherits from a class that is
 
1494
    allowed in a partition function then it is very important to consider
 
1495
    whether this should be inherited to the new class. If not the function
 
1496
    below should be defined in the new Item class.
 
1497
 
 
1498
    The general behaviour is that most integer functions are allowed.
 
1499
    If the partition function contains any multi-byte collations then
 
1500
    the function check_part_func_fields will report an error on the
 
1501
    partition function independent of what functions are used. So the
 
1502
    only character sets allowed are single character collation and
 
1503
    even for those only a limited set of functions are allowed. The
 
1504
    problem with multi-byte collations is that almost every string
 
1505
    function has the ability to change things such that two strings
 
1506
    that are equal will not be equal after manipulated by a string
 
1507
    function. E.g. two strings one contains a double s, there is a
 
1508
    special german character that is equal to two s. Now assume a
 
1509
    string function removes one character at this place, then in
 
1510
    one the double s will be removed and in the other there will
 
1511
    still be one s remaining and the strings are no longer equal
 
1512
    and thus the partition function will not sort equal strings into
 
1513
    the same partitions.
 
1514
 
 
1515
    So the check if a partition function is valid is two steps. First
 
1516
    check that the field types are valid, next check that the partition
 
1517
    function is valid. The current set of partition functions valid
 
1518
    assumes that there are no multi-byte collations amongst the partition
 
1519
    fields.
 
1520
  */
 
1521
  virtual bool check_partition_func_processor(uchar *bool_arg) { return TRUE;}
 
1522
  virtual bool subst_argument_checker(uchar **arg)
 
1523
  { 
 
1524
    if (*arg)
 
1525
      *arg= NULL; 
 
1526
    return TRUE;     
 
1527
  }
 
1528
  virtual bool explain_subquery_checker(uchar **arg) { return true; }
 
1529
  virtual Item *explain_subquery_propagator(uchar *arg) { return this; }
 
1530
 
 
1531
  virtual Item *equal_fields_propagator(uchar * arg) { return this; }
 
1532
  virtual bool set_no_const_sub(uchar *arg) { return FALSE; }
 
1533
  virtual Item *replace_equal_field(uchar * arg) { return this; }
 
1534
  /*
 
1535
    Check if an expression value has allowed arguments, like DATE/DATETIME
 
1536
    for date functions. Also used by partitioning code to reject
 
1537
    timezone-dependent expressions in a (sub)partitioning function.
 
1538
  */
 
1539
  virtual bool check_valid_arguments_processor(uchar *bool_arg)
 
1540
  {
 
1541
    return FALSE;
 
1542
  }
 
1543
 
 
1544
  /**
 
1545
    Find a function of a given type
 
1546
 
 
1547
    @param   arg     the function type to search (enum Item_func::Functype)
 
1548
    @return
 
1549
      @retval TRUE   the function type we're searching for is found
 
1550
      @retval FALSE  the function type wasn't found
 
1551
 
 
1552
    @description
 
1553
      This function can be used (together with Item::walk()) to find functions
 
1554
      in an item tree fragment.
 
1555
  */
 
1556
  virtual bool find_function_processor (uchar *arg)
 
1557
  {
 
1558
    return FALSE;
 
1559
  }
 
1560
 
 
1561
  /*
 
1562
    For SP local variable returns pointer to Item representing its
 
1563
    current value and pointer to current Item otherwise.
 
1564
  */
 
1565
  virtual Item *this_item() { return this; }
 
1566
  virtual const Item *this_item() const { return this; }
 
1567
 
 
1568
  /*
 
1569
    For SP local variable returns address of pointer to Item representing its
 
1570
    current value and pointer passed via parameter otherwise.
 
1571
  */
 
1572
  virtual Item **this_item_addr(THD *thd, Item **addr_arg) { return addr_arg; }
 
1573
 
 
1574
  // Row emulation
 
1575
  virtual uint cols() { return 1; }
 
1576
  virtual Item* element_index(uint i) { return this; }
 
1577
  virtual Item** addr(uint i) { return 0; }
 
1578
  virtual bool check_cols(uint c);
 
1579
  // It is not row => null inside is impossible
 
1580
  virtual bool null_inside() { return 0; }
 
1581
  // used in row subselects to get value of elements
 
1582
  virtual void bring_value() {}
 
1583
 
 
1584
  Field *tmp_table_field_from_field_type(TABLE *table, bool fixed_length);
 
1585
  virtual Item_field *field_for_view_update() { return 0; }
 
1586
 
 
1587
  virtual Item *neg_transformer(THD *thd) { return NULL; }
 
1588
  virtual Item *update_value_transformer(uchar *select_arg) { return this; }
 
1589
  virtual Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
1590
  void delete_self()
 
1591
  {
 
1592
    cleanup();
 
1593
    delete this;
 
1594
  }
 
1595
 
 
1596
  virtual bool is_splocal() { return 0; } /* Needed for error checking */
 
1597
 
 
1598
  /*
 
1599
    Return Settable_routine_parameter interface of the Item.  Return 0
 
1600
    if this Item is not Settable_routine_parameter.
 
1601
  */
 
1602
  virtual Settable_routine_parameter *get_settable_routine_parameter()
 
1603
  {
 
1604
    return 0;
 
1605
  }
 
1606
  inline bool is_temporal_with_date() const
 
1607
  {
 
1608
    return is_temporal_type_with_date(field_type());
 
1609
  }
 
1610
  inline bool is_temporal_with_date_and_time() const
 
1611
  {
 
1612
    return is_temporal_type_with_date_and_time(field_type());
 
1613
  }
 
1614
  inline bool is_temporal_with_time() const
 
1615
  {
 
1616
    return is_temporal_type_with_time(field_type());
 
1617
  }
 
1618
  inline bool is_temporal() const
 
1619
  {
 
1620
    return is_temporal_type(field_type());
 
1621
  }
 
1622
  /**
 
1623
    Check whether this and the given item has compatible comparison context.
 
1624
    Used by the equality propagation. See Item_field::equal_fields_propagator.
 
1625
 
 
1626
    @return
 
1627
      TRUE  if the context is the same or if fields could be
 
1628
            compared as DATETIME values by the Arg_comparator.
 
1629
      FALSE otherwise.
 
1630
  */
 
1631
  inline bool has_compatible_context(Item *item) const
 
1632
  {
 
1633
    /* Same context. */
 
1634
    if (cmp_context == (Item_result)-1 || item->cmp_context == cmp_context)
 
1635
      return TRUE;
 
1636
    /* DATETIME comparison context. */
 
1637
    if (is_temporal_with_date())
 
1638
      return item->is_temporal_with_date() ||
 
1639
             item->cmp_context == STRING_RESULT;
 
1640
    if (item->is_temporal_with_date())
 
1641
      return is_temporal_with_date() || cmp_context == STRING_RESULT;
 
1642
    return FALSE;
 
1643
  }
 
1644
  virtual Field::geometry_type get_geometry_type() const
 
1645
    { return Field::GEOM_GEOMETRY; };
 
1646
  String *check_well_formed_result(String *str, bool send_error= 0);
 
1647
  bool eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO *cs); 
 
1648
 
 
1649
  /*
 
1650
    Test whether an expression is expensive to compute. Used during
 
1651
    optimization to avoid computing expensive expressions during this
 
1652
    phase. Also used to force temp tables when sorting on expensive
 
1653
    functions.
 
1654
    TODO:
 
1655
    Normally we should have a method:
 
1656
      cost Item::execution_cost(),
 
1657
    where 'cost' is either 'double' or some structure of various cost
 
1658
    parameters.
 
1659
  */
 
1660
  virtual bool is_expensive()
 
1661
  {
 
1662
    if (is_expensive_cache < 0)
 
1663
      is_expensive_cache= walk(&Item::is_expensive_processor, 0, (uchar*)0);
 
1664
    return MY_TEST(is_expensive_cache);
 
1665
  }
 
1666
  virtual bool can_be_evaluated_now() const;
 
1667
  uint32 max_char_length() const
 
1668
  { return max_length / collation.collation->mbmaxlen; }
 
1669
  void fix_length_and_charset(uint32 max_char_length_arg,
 
1670
                              const CHARSET_INFO *cs)
 
1671
  {
 
1672
    max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
 
1673
    collation.collation= cs;
 
1674
  }
 
1675
  void fix_char_length(uint32 max_char_length_arg)
 
1676
  {
 
1677
    max_length= char_to_byte_length_safe(max_char_length_arg,
 
1678
                                         collation.collation->mbmaxlen);
 
1679
  }
 
1680
  void fix_char_length_ulonglong(ulonglong max_char_length_arg)
 
1681
  {
 
1682
    ulonglong max_result_length= max_char_length_arg *
 
1683
                                 collation.collation->mbmaxlen;
 
1684
    if (max_result_length >= MAX_BLOB_WIDTH)
 
1685
    {
 
1686
      max_length= MAX_BLOB_WIDTH;
 
1687
      maybe_null= 1;
 
1688
    }
 
1689
    else
 
1690
      max_length= (uint32) max_result_length;
 
1691
  }
 
1692
  void fix_length_and_charset_datetime(uint32 max_char_length_arg)
 
1693
  {
 
1694
    collation.set(&my_charset_numeric, DERIVATION_NUMERIC, MY_REPERTOIRE_ASCII);
 
1695
    fix_char_length(max_char_length_arg);
 
1696
  }
 
1697
  void fix_length_and_dec_and_charset_datetime(uint32 max_char_length_arg,
 
1698
                                               uint8 dec_arg)
 
1699
  {
 
1700
    decimals= dec_arg;
 
1701
    fix_length_and_charset_datetime(max_char_length_arg +
 
1702
                                    (dec_arg ? dec_arg + 1 : 0));
 
1703
  }
 
1704
  /*
 
1705
    Return TRUE if the item points to a column of an outer-joined table.
 
1706
  */
 
1707
  virtual bool is_outer_field() const { DBUG_ASSERT(fixed); return FALSE; }
 
1708
 
 
1709
  /**
 
1710
     Check if an item either is a blob field, or will be represented as a BLOB
 
1711
     field if a field is created based on this item.
 
1712
 
 
1713
     @retval TRUE  If a field based on this item will be a BLOB field,
 
1714
     @retval FALSE Otherwise.
 
1715
  */
 
1716
  bool is_blob_field() const;
 
1717
 
 
1718
  /**
 
1719
    Checks if this item or any of its decendents contains a subquery.
 
1720
  */
 
1721
  virtual bool has_subquery() const { return with_subselect; }
 
1722
  virtual bool has_stored_program() const { return with_stored_program; }
 
1723
  /// Whether this Item was created by the IN->EXISTS subquery transformation
 
1724
  virtual bool created_by_in2exists() const { return false; }
 
1725
};
 
1726
 
 
1727
 
 
1728
class sp_head;
 
1729
 
 
1730
 
 
1731
class Item_basic_constant :public Item
 
1732
{
 
1733
  table_map used_table_map;
 
1734
public:
 
1735
  Item_basic_constant(): Item(), used_table_map(0) {};
 
1736
  void set_used_tables(table_map map) { used_table_map= map; }
 
1737
  table_map used_tables() const { return used_table_map; }
 
1738
  /* to prevent drop fixed flag (no need parent cleanup call) */
 
1739
  void cleanup()
 
1740
  {
 
1741
    /*
 
1742
      Restore the original field name as it might not have been allocated
 
1743
      in the statement memory. If the name is auto generated, it must be
 
1744
      done again between subsequent executions of a prepared statement.
 
1745
    */
 
1746
    if (orig_name.is_set())
 
1747
      item_name= orig_name;
 
1748
  }
 
1749
};
 
1750
 
 
1751
 
 
1752
/*****************************************************************************
 
1753
  The class is a base class for representation of stored routine variables in
 
1754
  the Item-hierarchy. There are the following kinds of SP-vars:
 
1755
    - local variables (Item_splocal);
 
1756
    - CASE expression (Item_case_expr);
 
1757
*****************************************************************************/
 
1758
 
 
1759
class Item_sp_variable :public Item
 
1760
{
 
1761
protected:
 
1762
  /*
 
1763
    THD, which is stored in fix_fields() and is used in this_item() to avoid
 
1764
    current_thd use.
 
1765
  */
 
1766
  THD *m_thd;
 
1767
 
 
1768
public:
 
1769
  Name_string m_name;
 
1770
 
 
1771
public:
 
1772
#ifndef DBUG_OFF
 
1773
  /*
 
1774
    Routine to which this Item_splocal belongs. Used for checking if correct
 
1775
    runtime context is used for variable handling.
 
1776
  */
 
1777
  sp_head *m_sp;
 
1778
#endif
 
1779
 
 
1780
public:
 
1781
  Item_sp_variable(const Name_string sp_var_name);
 
1782
 
 
1783
public:
 
1784
  bool fix_fields(THD *thd, Item **);
 
1785
 
 
1786
  double val_real();
 
1787
  longlong val_int();
 
1788
  String *val_str(String *sp);
 
1789
  my_decimal *val_decimal(my_decimal *decimal_value);
 
1790
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
 
1791
  bool get_time(MYSQL_TIME *ltime);
 
1792
  bool is_null();
 
1793
 
 
1794
public:
 
1795
  inline void make_field(Send_field *field);  
 
1796
  inline type_conversion_status save_in_field(Field *field,
 
1797
                                              bool no_conversions);
 
1798
  inline bool send(Protocol *protocol, String *str);
 
1799
}; 
 
1800
 
 
1801
/*****************************************************************************
 
1802
  Item_sp_variable inline implementation.
 
1803
*****************************************************************************/
 
1804
 
 
1805
inline void Item_sp_variable::make_field(Send_field *field)
 
1806
{
 
1807
  Item *it= this_item();
 
1808
  it->item_name.copy(item_name.is_set() ? item_name : m_name);
 
1809
  it->make_field(field);
 
1810
}
 
1811
 
 
1812
inline type_conversion_status
 
1813
Item_sp_variable::save_in_field(Field *field, bool no_conversions)
 
1814
{
 
1815
  return this_item()->save_in_field(field, no_conversions);
 
1816
}
 
1817
 
 
1818
inline bool Item_sp_variable::send(Protocol *protocol, String *str)
 
1819
{
 
1820
  return this_item()->send(protocol, str);
 
1821
}
 
1822
 
 
1823
 
 
1824
/*****************************************************************************
 
1825
  A reference to local SP variable (incl. reference to SP parameter), used in
 
1826
  runtime.
 
1827
*****************************************************************************/
 
1828
 
 
1829
class Item_splocal :public Item_sp_variable,
 
1830
                    private Settable_routine_parameter
 
1831
{
 
1832
  uint m_var_idx;
 
1833
 
 
1834
  Type m_type;
 
1835
  Item_result m_result_type;
 
1836
  enum_field_types m_field_type;
 
1837
public:
 
1838
  /*
 
1839
    If this variable is a parameter in LIMIT clause.
 
1840
    Used only during NAME_CONST substitution, to not append
 
1841
    NAME_CONST to the resulting query and thus not break
 
1842
    the slave.
 
1843
  */
 
1844
  bool limit_clause_param;
 
1845
  /* 
 
1846
    Position of this reference to SP variable in the statement (the
 
1847
    statement itself is in sp_instr_stmt::m_query).
 
1848
    This is valid only for references to SP variables in statements,
 
1849
    excluding DECLARE CURSOR statement. It is used to replace references to SP
 
1850
    variables with NAME_CONST calls when putting statements into the binary
 
1851
    log.
 
1852
    Value of 0 means that this object doesn't corresponding to reference to
 
1853
    SP variable in query text.
 
1854
  */
 
1855
  uint pos_in_query;
 
1856
  /*
 
1857
    Byte length of SP variable name in the statement (see pos_in_query).
 
1858
    The value of this field may differ from the name_length value because
 
1859
    name_length contains byte length of UTF8-encoded item name, but
 
1860
    the query string (see sp_instr_stmt::m_query) is currently stored with
 
1861
    a charset from the SET NAMES statement.
 
1862
  */
 
1863
  uint len_in_query;
 
1864
 
 
1865
  Item_splocal(const Name_string sp_var_name, uint sp_var_idx,
 
1866
               enum_field_types sp_var_type,
 
1867
               uint pos_in_q= 0, uint len_in_q= 0);
 
1868
 
 
1869
  bool is_splocal() { return 1; } /* Needed for error checking */
 
1870
 
 
1871
  Item *this_item();
 
1872
  const Item *this_item() const;
 
1873
  Item **this_item_addr(THD *thd, Item **);
 
1874
 
 
1875
  virtual void print(String *str, enum_query_type query_type);
 
1876
 
 
1877
public:
 
1878
  inline uint get_var_idx() const;
 
1879
 
 
1880
  inline enum Type type() const;
 
1881
  inline Item_result result_type() const;
 
1882
  inline enum_field_types field_type() const { return m_field_type; }
 
1883
 
 
1884
private:
 
1885
  bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
 
1886
 
 
1887
public:
 
1888
  Settable_routine_parameter *get_settable_routine_parameter()
 
1889
  {
 
1890
    return this;
 
1891
  }
 
1892
};
 
1893
 
 
1894
/*****************************************************************************
 
1895
  Item_splocal inline implementation.
 
1896
*****************************************************************************/
 
1897
 
 
1898
inline uint Item_splocal::get_var_idx() const
 
1899
{
 
1900
  return m_var_idx;
 
1901
}
 
1902
 
 
1903
inline enum Item::Type Item_splocal::type() const
 
1904
{
 
1905
  return m_type;
 
1906
}
 
1907
 
 
1908
inline Item_result Item_splocal::result_type() const
 
1909
{
 
1910
  return m_result_type;
 
1911
}
 
1912
 
 
1913
 
 
1914
/*****************************************************************************
 
1915
  A reference to case expression in SP, used in runtime.
 
1916
*****************************************************************************/
 
1917
 
 
1918
class Item_case_expr :public Item_sp_variable
 
1919
{
 
1920
public:
 
1921
  Item_case_expr(uint case_expr_id);
 
1922
 
 
1923
public:
 
1924
  Item *this_item();
 
1925
  const Item *this_item() const;
 
1926
  Item **this_item_addr(THD *thd, Item **);
 
1927
 
 
1928
  inline enum Type type() const;
 
1929
  inline Item_result result_type() const;
 
1930
 
 
1931
public:
 
1932
  /*
 
1933
    NOTE: print() is intended to be used from views and for debug.
 
1934
    Item_case_expr can not occur in views, so here it is only for debug
 
1935
    purposes.
 
1936
  */
 
1937
  virtual void print(String *str, enum_query_type query_type);
 
1938
 
 
1939
private:
 
1940
  uint m_case_expr_id;
 
1941
};
 
1942
 
 
1943
/*****************************************************************************
 
1944
  Item_case_expr inline implementation.
 
1945
*****************************************************************************/
 
1946
 
 
1947
inline enum Item::Type Item_case_expr::type() const
 
1948
{
 
1949
  return this_item()->type();
 
1950
}
 
1951
 
 
1952
inline Item_result Item_case_expr::result_type() const
 
1953
{
 
1954
  return this_item()->result_type();
 
1955
}
 
1956
 
 
1957
 
 
1958
/*
 
1959
  NAME_CONST(given_name, const_value). 
 
1960
  This 'function' has all properties of the supplied const_value (which is 
 
1961
  assumed to be a literal constant), and the name given_name. 
 
1962
 
 
1963
  This is used to replace references to SP variables when we write PROCEDURE
 
1964
  statements into the binary log.
 
1965
 
 
1966
  TODO
 
1967
    Together with Item_splocal and Item::this_item() we can actually extract
 
1968
    common a base of this class and Item_splocal. Maybe it is possible to
 
1969
    extract a common base with class Item_ref, too.
 
1970
*/
 
1971
 
 
1972
class Item_name_const : public Item
 
1973
{
 
1974
  Item *value_item;
 
1975
  Item *name_item;
 
1976
  bool valid_args;
 
1977
public:
 
1978
  Item_name_const(Item *name_arg, Item *val);
 
1979
 
 
1980
  bool fix_fields(THD *, Item **);
 
1981
 
 
1982
  enum Type type() const;
 
1983
  double val_real();
 
1984
  longlong val_int();
 
1985
  String *val_str(String *sp);
 
1986
  my_decimal *val_decimal(my_decimal *);
 
1987
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
 
1988
  bool get_time(MYSQL_TIME *ltime);
 
1989
  bool is_null();
 
1990
  virtual void print(String *str, enum_query_type query_type);
 
1991
 
 
1992
  Item_result result_type() const
 
1993
  {
 
1994
    return value_item->result_type();
 
1995
  }
 
1996
 
 
1997
  type_conversion_status save_in_field(Field *field, bool no_conversions)
 
1998
  {
 
1999
    return  value_item->save_in_field(field, no_conversions);
 
2000
  }
 
2001
 
 
2002
  bool send(Protocol *protocol, String *str)
 
2003
  {
 
2004
    return value_item->send(protocol, str);
 
2005
  }
 
2006
};
 
2007
 
 
2008
bool agg_item_collations(DTCollation &c, const char *name,
 
2009
                         Item **items, uint nitems, uint flags, int item_sep);
 
2010
bool agg_item_collations_for_comparison(DTCollation &c, const char *name,
 
2011
                                        Item **items, uint nitems, uint flags);
 
2012
bool agg_item_set_converter(DTCollation &coll, const char *fname,
 
2013
                            Item **args, uint nargs, uint flags, int item_sep);
 
2014
bool agg_item_charsets(DTCollation &c, const char *name,
 
2015
                       Item **items, uint nitems, uint flags, int item_sep);
 
2016
inline bool
 
2017
agg_item_charsets_for_string_result(DTCollation &c, const char *name,
 
2018
                                    Item **items, uint nitems,
 
2019
                                    int item_sep= 1)
 
2020
{
 
2021
  uint flags= MY_COLL_ALLOW_SUPERSET_CONV |
 
2022
              MY_COLL_ALLOW_COERCIBLE_CONV |
 
2023
              MY_COLL_ALLOW_NUMERIC_CONV;
 
2024
  return agg_item_charsets(c, name, items, nitems, flags, item_sep);
 
2025
}
 
2026
inline bool
 
2027
agg_item_charsets_for_comparison(DTCollation &c, const char *name,
 
2028
                                 Item **items, uint nitems,
 
2029
                                 int item_sep= 1)
 
2030
{
 
2031
  uint flags= MY_COLL_ALLOW_SUPERSET_CONV |
 
2032
              MY_COLL_ALLOW_COERCIBLE_CONV |
 
2033
              MY_COLL_DISALLOW_NONE;
 
2034
  return agg_item_charsets(c, name, items, nitems, flags, item_sep);
 
2035
}
 
2036
inline bool
 
2037
agg_item_charsets_for_string_result_with_comparison(DTCollation &c,
 
2038
                                                    const char *name,
 
2039
                                                    Item **items, uint nitems,
 
2040
                                                    int item_sep= 1)
 
2041
{
 
2042
  uint flags= MY_COLL_ALLOW_SUPERSET_CONV |
 
2043
              MY_COLL_ALLOW_COERCIBLE_CONV |
 
2044
              MY_COLL_ALLOW_NUMERIC_CONV |
 
2045
              MY_COLL_DISALLOW_NONE;
 
2046
  return agg_item_charsets(c, name, items, nitems, flags, item_sep);
 
2047
}
 
2048
 
 
2049
 
 
2050
class Item_num: public Item_basic_constant
 
2051
{
 
2052
public:
 
2053
  Item_num() { collation.set_numeric(); } /* Remove gcc warning */
 
2054
  virtual Item_num *neg()= 0;
 
2055
  Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
2056
  bool check_partition_func_processor(uchar *int_arg) { return FALSE;}
 
2057
};
 
2058
 
 
2059
#define NO_CACHED_FIELD_INDEX ((uint)(-1))
 
2060
 
 
2061
class st_select_lex;
 
2062
class Item_ident :public Item
 
2063
{
 
2064
protected:
 
2065
  /* 
 
2066
    We have to store initial values of db_name, table_name and field_name
 
2067
    to be able to restore them during cleanup() because they can be 
 
2068
    updated during fix_fields() to values from Field object and life-time 
 
2069
    of those is shorter than life-time of Item_field.
 
2070
  */
 
2071
  const char *orig_db_name;
 
2072
  const char *orig_table_name;
 
2073
  const char *orig_field_name;
 
2074
 
 
2075
public:
 
2076
  Name_resolution_context *context;
 
2077
  const char *db_name;
 
2078
  const char *table_name;
 
2079
  const char *field_name;
 
2080
  bool alias_name_used; /* true if item was resolved against alias */
 
2081
  /* 
 
2082
    Cached value of index for this field in table->field array, used by prep. 
 
2083
    stmts for speeding up their re-execution. Holds NO_CACHED_FIELD_INDEX 
 
2084
    if index value is not known.
 
2085
  */
 
2086
  uint cached_field_index;
 
2087
  /*
 
2088
    Cached pointer to table which contains this field, used for the same reason
 
2089
    by prep. stmt. too in case then we have not-fully qualified field.
 
2090
    0 - means no cached value.
 
2091
  */
 
2092
  TABLE_LIST *cached_table;
 
2093
  st_select_lex *depended_from;
 
2094
  Item_ident(Name_resolution_context *context_arg,
 
2095
             const char *db_name_arg, const char *table_name_arg,
 
2096
             const char *field_name_arg);
 
2097
  Item_ident(THD *thd, Item_ident *item);
 
2098
  const char *full_name() const;
 
2099
  virtual void fix_after_pullout(st_select_lex *parent_select,
 
2100
                                 st_select_lex *removed_select);
 
2101
  void cleanup();
 
2102
  bool remove_dependence_processor(uchar * arg);
 
2103
  virtual void print(String *str, enum_query_type query_type);
 
2104
  virtual bool change_context_processor(uchar *cntx)
 
2105
    { context= (Name_resolution_context *)cntx; return FALSE; }
 
2106
  friend bool insert_fields(THD *thd, Name_resolution_context *context,
 
2107
                            const char *db_name,
 
2108
                            const char *table_name, List_iterator<Item> *it,
 
2109
                            bool any_privileges);
 
2110
};
 
2111
 
 
2112
 
 
2113
class Item_ident_for_show :public Item
 
2114
{
 
2115
public:
 
2116
  Field *field;
 
2117
  const char *db_name;
 
2118
  const char *table_name;
 
2119
 
 
2120
  Item_ident_for_show(Field *par_field, const char *db_arg,
 
2121
                      const char *table_name_arg)
 
2122
    :field(par_field), db_name(db_arg), table_name(table_name_arg)
 
2123
  {}
 
2124
 
 
2125
  enum Type type() const { return FIELD_ITEM; }
 
2126
  double val_real() { return field->val_real(); }
 
2127
  longlong val_int() { return field->val_int(); }
 
2128
  String *val_str(String *str) { return field->val_str(str); }
 
2129
  my_decimal *val_decimal(my_decimal *dec) { return field->val_decimal(dec); }
 
2130
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
2131
  {
 
2132
    return field->get_date(ltime, fuzzydate);
 
2133
  }
 
2134
  bool get_time(MYSQL_TIME *ltime)
 
2135
  {
 
2136
    return field->get_time(ltime);
 
2137
  }
 
2138
  void make_field(Send_field *tmp_field);
 
2139
  CHARSET_INFO *charset_for_protocol(void) const
 
2140
  { return (CHARSET_INFO *)field->charset_for_protocol(); }
 
2141
};
 
2142
 
 
2143
 
 
2144
class Item_equal;
 
2145
class COND_EQUAL;
 
2146
 
 
2147
class Item_field :public Item_ident
 
2148
{
 
2149
protected:
 
2150
  void set_field(Field *field);
 
2151
public:
 
2152
  Field *field,*result_field;
 
2153
  Item_equal *item_equal;
 
2154
  bool no_const_subst;
 
2155
  /*
 
2156
    if any_privileges set to TRUE then here real effective privileges will
 
2157
    be stored
 
2158
  */
 
2159
  uint have_privileges;
 
2160
  /* field need any privileges (for VIEW creation) */
 
2161
  bool any_privileges;
 
2162
  Item_field(Name_resolution_context *context_arg,
 
2163
             const char *db_arg,const char *table_name_arg,
 
2164
             const char *field_name_arg);
 
2165
  /*
 
2166
    Constructor needed to process subquery with temporary tables (see Item).
 
2167
    Notice that it will have no name resolution context.
 
2168
  */
 
2169
  Item_field(THD *thd, Item_field *item);
 
2170
  /*
 
2171
    Ensures that field, table, and database names will live as long as
 
2172
    Item_field (this is important in prepared statements).
 
2173
  */
 
2174
  Item_field(THD *thd, Name_resolution_context *context_arg, Field *field);
 
2175
  /*
 
2176
    If this constructor is used, fix_fields() won't work, because
 
2177
    db_name, table_name and column_name are unknown. It's necessary to call
 
2178
    reset_field() before fix_fields() for all fields created this way.
 
2179
  */
 
2180
  Item_field(Field *field);
 
2181
  enum Type type() const { return FIELD_ITEM; }
 
2182
  bool eq(const Item *item, bool binary_cmp) const;
 
2183
  double val_real();
 
2184
  longlong val_int();
 
2185
  longlong val_time_temporal();
 
2186
  longlong val_date_temporal();
 
2187
  my_decimal *val_decimal(my_decimal *);
 
2188
  String *val_str(String*);
 
2189
  double val_result();
 
2190
  longlong val_int_result();
 
2191
  longlong val_time_temporal_result();
 
2192
  longlong val_date_temporal_result();
 
2193
  String *str_result(String* tmp);
 
2194
  my_decimal *val_decimal_result(my_decimal *);
 
2195
  bool val_bool_result();
 
2196
  bool is_null_result();
 
2197
  bool send(Protocol *protocol, String *str_arg);
 
2198
  void reset_field(Field *f);
 
2199
  bool fix_fields(THD *, Item **);
 
2200
  void make_field(Send_field *tmp_field);
 
2201
  type_conversion_status save_in_field(Field *field,bool no_conversions);
 
2202
  void save_org_in_field(Field *field);
 
2203
  table_map used_tables() const;
 
2204
  virtual table_map resolved_used_tables() const;
 
2205
  enum Item_result result_type () const
 
2206
  {
 
2207
    return field->result_type();
 
2208
  }
 
2209
  enum Item_result numeric_context_result_type() const
 
2210
  {
 
2211
    return field->numeric_context_result_type();
 
2212
  }
 
2213
  Item_result cast_to_int_type() const
 
2214
  {
 
2215
    return field->cast_to_int_type();
 
2216
  }
 
2217
  enum_field_types field_type() const
 
2218
  {
 
2219
    return field->type();
 
2220
  }
 
2221
  enum_monotonicity_info get_monotonicity_info() const
 
2222
  {
 
2223
    return MONOTONIC_STRICT_INCREASING;
 
2224
  }
 
2225
  longlong val_int_endpoint(bool left_endp, bool *incl_endp);
 
2226
  Field *get_tmp_table_field() { return result_field; }
 
2227
  Field *tmp_table_field(TABLE *t_arg) { return result_field; }
 
2228
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
2229
  bool get_date_result(MYSQL_TIME *ltime,uint fuzzydate);
 
2230
  bool get_time(MYSQL_TIME *ltime);
 
2231
  bool get_timeval(struct timeval *tm, int *warnings);
 
2232
  bool is_null() { return field->is_null(); }
 
2233
  void update_null_value();
 
2234
  Item *get_tmp_table_item(THD *thd);
 
2235
  bool collect_item_field_processor(uchar * arg);
 
2236
  bool add_field_to_set_processor(uchar * arg);
 
2237
  bool remove_column_from_bitmap(uchar * arg);
 
2238
  bool find_item_in_field_list_processor(uchar *arg);
 
2239
  bool register_field_in_read_map(uchar *arg);
 
2240
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
2241
  void cleanup();
 
2242
  Item_equal *find_item_equal(COND_EQUAL *cond_equal);
 
2243
  bool subst_argument_checker(uchar **arg);
 
2244
  Item *equal_fields_propagator(uchar *arg);
 
2245
  bool set_no_const_sub(uchar *arg);
 
2246
  Item *replace_equal_field(uchar *arg);
 
2247
  inline uint32 max_disp_length() { return field->max_display_length(); }
 
2248
  Item_field *field_for_view_update() { return this; }
 
2249
  Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
2250
  int fix_outer_field(THD *thd, Field **field, Item **reference);
 
2251
  virtual Item *update_value_transformer(uchar *select_arg);
 
2252
  virtual bool item_field_by_name_analyzer(uchar **arg);
 
2253
  virtual Item* item_field_by_name_transformer(uchar *arg);
 
2254
  virtual void print(String *str, enum_query_type query_type);
 
2255
  bool is_outer_field() const
 
2256
  {
 
2257
    DBUG_ASSERT(fixed);
 
2258
    return field->table->pos_in_table_list->outer_join ||
 
2259
           field->table->pos_in_table_list->outer_join_nest();
 
2260
  }
 
2261
  Field::geometry_type get_geometry_type() const
 
2262
  {
 
2263
    DBUG_ASSERT(field_type() == MYSQL_TYPE_GEOMETRY);
 
2264
    return field->get_geometry_type();
 
2265
  }
 
2266
  const CHARSET_INFO *charset_for_protocol(void) const
 
2267
  { return field->charset_for_protocol(); }
 
2268
 
 
2269
#ifndef DBUG_OFF
 
2270
  void dbug_print()
 
2271
  {
 
2272
    fprintf(DBUG_FILE, "<field ");
 
2273
    if (field)
 
2274
    {
 
2275
      fprintf(DBUG_FILE, "'%s.%s': ", field->table->alias, field->field_name);
 
2276
      field->dbug_print();
 
2277
    }
 
2278
    else
 
2279
      fprintf(DBUG_FILE, "NULL");
 
2280
 
 
2281
    fprintf(DBUG_FILE, ", result_field: ");
 
2282
    if (result_field)
 
2283
    {
 
2284
      fprintf(DBUG_FILE, "'%s.%s': ",
 
2285
              result_field->table->alias, result_field->field_name);
 
2286
      result_field->dbug_print();
 
2287
    }
 
2288
    else
 
2289
      fprintf(DBUG_FILE, "NULL");
 
2290
    fprintf(DBUG_FILE, ">\n");
 
2291
  }
 
2292
#endif
 
2293
 
 
2294
  /// Pushes the item to select_lex.non_agg_fields() and updates its marker.
 
2295
  bool push_to_non_agg_fields(st_select_lex *select_lex);
 
2296
 
 
2297
  friend class Item_default_value;
 
2298
  friend class Item_insert_value;
 
2299
  friend class st_select_lex_unit;
 
2300
};
 
2301
 
 
2302
class Item_null :public Item_basic_constant
 
2303
{
 
2304
  void init()
 
2305
  {
 
2306
    maybe_null= null_value= TRUE;
 
2307
    max_length= 0;
 
2308
    fixed= 1;
 
2309
    collation.set(&my_charset_bin, DERIVATION_IGNORABLE);
 
2310
  }
 
2311
public:
 
2312
  Item_null()
 
2313
  {
 
2314
    init();
 
2315
    item_name= NAME_STRING("NULL");
 
2316
  }
 
2317
  Item_null(const Name_string &name_par)
 
2318
  {
 
2319
    init();
 
2320
    item_name= name_par;
 
2321
  }
 
2322
  enum Type type() const { return NULL_ITEM; }
 
2323
  bool eq(const Item *item, bool binary_cmp) const;
 
2324
  double val_real();
 
2325
  longlong val_int();
 
2326
  longlong val_time_temporal() { return val_int(); }
 
2327
  longlong val_date_temporal() { return val_int(); }
 
2328
  String *val_str(String *str);
 
2329
  my_decimal *val_decimal(my_decimal *);
 
2330
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
2331
  {
 
2332
    return true;
 
2333
  }
 
2334
  bool get_time(MYSQL_TIME *ltime)
 
2335
  {
 
2336
    return true;
 
2337
  }
 
2338
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
2339
  type_conversion_status save_safe_in_field(Field *field);
 
2340
  bool send(Protocol *protocol, String *str);
 
2341
  enum Item_result result_type () const { return STRING_RESULT; }
 
2342
  enum_field_types field_type() const   { return MYSQL_TYPE_NULL; }
 
2343
  bool basic_const_item() const { return 1; }
 
2344
  Item *clone_item() { return new Item_null(item_name); }
 
2345
  bool is_null() { return 1; }
 
2346
 
 
2347
  virtual inline void print(String *str, enum_query_type query_type)
 
2348
  {
 
2349
    str->append(STRING_WITH_LEN("NULL"));
 
2350
  }
 
2351
 
 
2352
  Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
2353
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
2354
};
 
2355
 
 
2356
/**
 
2357
  An item representing NULL values for use with ROLLUP.
 
2358
 
 
2359
  When grouping WITH ROLLUP, Item_null_result items are created to
 
2360
  represent NULL values in the grouping columns of the ROLLUP rows. To
 
2361
  avoid type problems during execution, these objects are created with
 
2362
  the same field and result types as the fields of the columns they
 
2363
  belong to.
 
2364
 */
 
2365
class Item_null_result :public Item_null
 
2366
{
 
2367
  /** Field type for this NULL value */
 
2368
  enum_field_types fld_type;
 
2369
  /** Result type for this NULL value */
 
2370
  Item_result res_type;
 
2371
 
 
2372
public:
 
2373
  Field *result_field;
 
2374
  Item_null_result(enum_field_types fld_type, Item_result res_type)
 
2375
    : Item_null(), fld_type(fld_type), res_type(res_type), result_field(0) {}
 
2376
  bool is_result_field() { return result_field != 0; }
 
2377
  void save_in_result_field(bool no_conversions)
 
2378
  {
 
2379
    save_in_field(result_field, no_conversions);
 
2380
  }
 
2381
  bool check_partition_func_processor(uchar *int_arg) {return TRUE;}
 
2382
  enum_field_types field_type() const { return fld_type; }
 
2383
  Item_result result_type() const { return res_type; }
 
2384
};  
 
2385
 
 
2386
/* Item represents one placeholder ('?') of prepared statement */
 
2387
 
 
2388
class Item_param :public Item,
 
2389
                  private Settable_routine_parameter
 
2390
{
 
2391
  char cnvbuf[MAX_FIELD_WIDTH];
 
2392
  String cnvstr;
 
2393
  Item *cnvitem;
 
2394
 
 
2395
public:
 
2396
  enum enum_item_param_state
 
2397
  {
 
2398
    NO_VALUE, NULL_VALUE, INT_VALUE, REAL_VALUE,
 
2399
    STRING_VALUE, TIME_VALUE, LONG_DATA_VALUE,
 
2400
    DECIMAL_VALUE
 
2401
  } state;
 
2402
 
 
2403
  /*
 
2404
    A buffer for string and long data values. Historically all allocated
 
2405
    values returned from val_str() were treated as eligible to
 
2406
    modification. I. e. in some cases Item_func_concat can append it's
 
2407
    second argument to return value of the first one. Because of that we
 
2408
    can't return the original buffer holding string data from val_str(),
 
2409
    and have to have one buffer for data and another just pointing to
 
2410
    the data. This is the latter one and it's returned from val_str().
 
2411
    Can not be declared inside the union as it's not a POD type.
 
2412
  */
 
2413
  String str_value_ptr;
 
2414
  my_decimal decimal_value;
 
2415
  union
 
2416
  {
 
2417
    longlong integer;
 
2418
    double   real;
 
2419
    /*
 
2420
      Character sets conversion info for string values.
 
2421
      Character sets of client and connection defined at bind time are used
 
2422
      for all conversions, even if one of them is later changed (i.e.
 
2423
      between subsequent calls to mysql_stmt_execute).
 
2424
    */
 
2425
    struct CONVERSION_INFO
 
2426
    {
 
2427
      const CHARSET_INFO *character_set_client;
 
2428
      const CHARSET_INFO *character_set_of_placeholder;
 
2429
      /*
 
2430
        This points at character set of connection if conversion
 
2431
        to it is required (i. e. if placeholder typecode is not BLOB).
 
2432
        Otherwise it's equal to character_set_client (to simplify
 
2433
        check in convert_str_value()).
 
2434
      */
 
2435
      const CHARSET_INFO *final_character_set_of_str_value;
 
2436
    } cs_info;
 
2437
    MYSQL_TIME     time;
 
2438
  } value;
 
2439
 
 
2440
  /* Cached values for virtual methods to save us one switch.  */
 
2441
  enum Item_result item_result_type;
 
2442
  enum Type item_type;
 
2443
 
 
2444
  /*
 
2445
    Used when this item is used in a temporary table.
 
2446
    This is NOT placeholder metadata sent to client, as this value
 
2447
    is assigned after sending metadata (in setup_one_conversion_function).
 
2448
    For example in case of 'SELECT ?' you'll get MYSQL_TYPE_STRING both
 
2449
    in result set and placeholders metadata, no matter what type you will
 
2450
    supply for this placeholder in mysql_stmt_execute.
 
2451
  */
 
2452
  enum enum_field_types param_type;
 
2453
  /*
 
2454
    Offset of placeholder inside statement text. Used to create
 
2455
    no-placeholders version of this statement for the binary log.
 
2456
  */
 
2457
  uint pos_in_query;
 
2458
 
 
2459
  Item_param(uint pos_in_query_arg);
 
2460
 
 
2461
  enum Item_result result_type () const { return item_result_type; }
 
2462
  enum Type type() const { return item_type; }
 
2463
  enum_field_types field_type() const { return param_type; }
 
2464
 
 
2465
  double val_real();
 
2466
  longlong val_int();
 
2467
  my_decimal *val_decimal(my_decimal*);
 
2468
  String *val_str(String*);
 
2469
  bool get_time(MYSQL_TIME *tm);
 
2470
  bool get_date(MYSQL_TIME *tm, uint fuzzydate);
 
2471
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
2472
 
 
2473
  void set_null();
 
2474
  void set_int(longlong i, uint32 max_length_arg);
 
2475
  void set_double(double i);
 
2476
  void set_decimal(const char *str, ulong length);
 
2477
  void set_decimal(const my_decimal *dv);
 
2478
  bool set_str(const char *str, ulong length);
 
2479
  bool set_longdata(const char *str, ulong length);
 
2480
  void set_time(MYSQL_TIME *tm, timestamp_type type, uint32 max_length_arg);
 
2481
  bool set_from_user_var(THD *thd, const user_var_entry *entry);
 
2482
  void reset();
 
2483
  /*
 
2484
    Assign placeholder value from bind data.
 
2485
    Note, that 'len' has different semantics in embedded library (as we
 
2486
    don't need to check that packet is not broken there). See
 
2487
    sql_prepare.cc for details.
 
2488
  */
 
2489
  void (*set_param_func)(Item_param *param, uchar **pos, ulong len);
 
2490
 
 
2491
  const String *query_val_str(THD *thd, String *str) const;
 
2492
 
 
2493
  bool convert_str_value(THD *thd);
 
2494
 
 
2495
  /*
 
2496
    If value for parameter was not set we treat it as non-const
 
2497
    so noone will use parameters value in fix_fields still
 
2498
    parameter is constant during execution.
 
2499
  */
 
2500
  virtual table_map used_tables() const
 
2501
  { return state != NO_VALUE ? (table_map)0 : PARAM_TABLE_BIT; }
 
2502
  virtual void print(String *str, enum_query_type query_type);
 
2503
  bool is_null()
 
2504
  { DBUG_ASSERT(state != NO_VALUE); return state == NULL_VALUE; }
 
2505
  bool basic_const_item() const;
 
2506
  /*
 
2507
    This method is used to make a copy of a basic constant item when
 
2508
    propagating constants in the optimizer. The reason to create a new
 
2509
    item and not use the existing one is not precisely known (2005/04/16).
 
2510
    Probably we are trying to preserve tree structure of items, in other
 
2511
    words, avoid pointing at one item from two different nodes of the tree.
 
2512
    Return a new basic constant item if parameter value is a basic
 
2513
    constant, assert otherwise. This method is called only if
 
2514
    basic_const_item returned TRUE.
 
2515
  */
 
2516
  Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
2517
  Item *clone_item();
 
2518
  /*
 
2519
    Implement by-value equality evaluation if parameter value
 
2520
    is set and is a basic constant (integer, real or string).
 
2521
    Otherwise return FALSE.
 
2522
  */
 
2523
  bool eq(const Item *item, bool binary_cmp) const;
 
2524
  /** Item is a argument to a limit clause. */
 
2525
  bool limit_clause_param;
 
2526
  void set_param_type_and_swap_value(Item_param *from);
 
2527
 
 
2528
private:
 
2529
  virtual inline Settable_routine_parameter *
 
2530
    get_settable_routine_parameter()
 
2531
  {
 
2532
    return this;
 
2533
  }
 
2534
 
 
2535
  virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
 
2536
 
 
2537
  virtual void set_out_param_info(Send_field *info);
 
2538
 
 
2539
public:
 
2540
  virtual const Send_field *get_out_param_info() const;
 
2541
 
 
2542
  virtual void make_field(Send_field *field);
 
2543
 
 
2544
private:
 
2545
  Send_field *m_out_param_info;
 
2546
};
 
2547
 
 
2548
 
 
2549
class Item_int :public Item_num
 
2550
{
 
2551
public:
 
2552
  longlong value;
 
2553
  Item_int(int32 i,uint length= MY_INT32_NUM_DECIMAL_DIGITS)
 
2554
    :value((longlong) i)
 
2555
    { max_length=length; fixed= 1; }
 
2556
  Item_int(longlong i,uint length= MY_INT64_NUM_DECIMAL_DIGITS)
 
2557
    :value(i)
 
2558
    { max_length=length; fixed= 1; }
 
2559
  Item_int(ulonglong i, uint length= MY_INT64_NUM_DECIMAL_DIGITS)
 
2560
    :value((longlong)i)
 
2561
    { max_length=length; fixed= 1; unsigned_flag= 1; }
 
2562
  Item_int(Item_int *item_arg)
 
2563
  {
 
2564
    value= item_arg->value;
 
2565
    item_name= item_arg->item_name;
 
2566
    max_length= item_arg->max_length;
 
2567
    fixed= 1;
 
2568
  }
 
2569
  Item_int(const Name_string &name_arg, longlong i, uint length) :value(i)
 
2570
  {
 
2571
    max_length= length;
 
2572
    item_name= name_arg;
 
2573
    fixed= 1;
 
2574
  }
 
2575
  Item_int(const char *str_arg, uint length);
 
2576
  enum Type type() const { return INT_ITEM; }
 
2577
  enum Item_result result_type () const { return INT_RESULT; }
 
2578
  enum_field_types field_type() const { return MYSQL_TYPE_LONGLONG; }
 
2579
  longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
 
2580
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; }
 
2581
  my_decimal *val_decimal(my_decimal *);
 
2582
  String *val_str(String*);
 
2583
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
2584
  {
 
2585
    return get_date_from_int(ltime, fuzzydate);
 
2586
  }
 
2587
  bool get_time(MYSQL_TIME *ltime)
 
2588
  {
 
2589
    return get_time_from_int(ltime);
 
2590
  }
 
2591
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
2592
  bool basic_const_item() const { return 1; }
 
2593
  Item *clone_item() { return new Item_int(this); }
 
2594
  virtual void print(String *str, enum_query_type query_type);
 
2595
  Item_num *neg() { value= -value; return this; }
 
2596
  uint decimal_precision() const
 
2597
  { return (uint)(max_length - MY_TEST(value < 0)); }
 
2598
  bool eq(const Item *, bool binary_cmp) const;
 
2599
  bool check_partition_func_processor(uchar *bool_arg) { return FALSE;}
 
2600
};
 
2601
 
 
2602
 
 
2603
/**
 
2604
  Item_int with value==0 and length==1
 
2605
*/
 
2606
class Item_int_0 :public Item_int
 
2607
{
 
2608
public:
 
2609
  Item_int_0() :Item_int(NAME_STRING("0"), 0, 1) {}
 
2610
};
 
2611
 
 
2612
 
 
2613
/*
 
2614
  Item_temporal is used to store numeric representation
 
2615
  of time/date/datetime values for queries like:
 
2616
 
 
2617
     WHERE datetime_column NOT IN
 
2618
     ('2006-04-25 10:00:00','2006-04-25 10:02:00', ...);
 
2619
 
 
2620
  and for SHOW/INFORMATION_SCHEMA purposes (see sql_show.cc)
 
2621
 
 
2622
  TS-TODO: Can't we use Item_time_literal, Item_date_literal,
 
2623
  TS-TODO: and Item_datetime_literal for this purpose?
 
2624
*/
 
2625
class Item_temporal :public Item_int
 
2626
{
 
2627
  enum_field_types cached_field_type;
 
2628
public:
 
2629
  Item_temporal(enum_field_types field_type_arg, longlong i): Item_int(i),
 
2630
    cached_field_type(field_type_arg)
 
2631
  {
 
2632
    DBUG_ASSERT(is_temporal_type(field_type_arg));
 
2633
  }
 
2634
  Item_temporal(enum_field_types field_type_arg, const Name_string &name_arg,
 
2635
                longlong i, uint length): Item_int(i),
 
2636
    cached_field_type(field_type_arg)
 
2637
  {
 
2638
    DBUG_ASSERT(is_temporal_type(field_type_arg));
 
2639
    max_length= length;
 
2640
    item_name= name_arg;
 
2641
    fixed= 1;
 
2642
  }
 
2643
  Item *clone_item() { return new Item_temporal(field_type(), value); }
 
2644
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
2645
  longlong val_time_temporal() { return val_int(); }
 
2646
  longlong val_date_temporal() { return val_int(); }
 
2647
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
2648
  {
 
2649
    DBUG_ASSERT(0);
 
2650
    return false;
 
2651
  }
 
2652
  bool get_time(MYSQL_TIME *ltime)
 
2653
  {
 
2654
    DBUG_ASSERT(0);
 
2655
    return false;
 
2656
  }
 
2657
  enum_field_types field_type() const
 
2658
  {
 
2659
    return cached_field_type;
 
2660
  }
 
2661
};
 
2662
 
 
2663
 
 
2664
class Item_uint :public Item_int
 
2665
{
 
2666
public:
 
2667
  Item_uint(const char *str_arg, uint length)
 
2668
    :Item_int(str_arg, length) { unsigned_flag= 1; }
 
2669
  Item_uint(ulonglong i) :Item_int((ulonglong) i, 10) {}
 
2670
  Item_uint(const Name_string &name_arg, longlong i, uint length)
 
2671
    :Item_int(name_arg, i, length) { unsigned_flag= 1; }
 
2672
  double val_real()
 
2673
    { DBUG_ASSERT(fixed == 1); return ulonglong2double((ulonglong)value); }
 
2674
  String *val_str(String*);
 
2675
 
 
2676
  Item *clone_item() { return new Item_uint(item_name, value, max_length); }
 
2677
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
2678
  virtual void print(String *str, enum_query_type query_type);
 
2679
  Item_num *neg ();
 
2680
  uint decimal_precision() const { return max_length; }
 
2681
  bool check_partition_func_processor(uchar *bool_arg) { return FALSE;}
 
2682
};
 
2683
 
 
2684
 
 
2685
/* decimal (fixed point) constant */
 
2686
class Item_decimal :public Item_num
 
2687
{
 
2688
protected:
 
2689
  my_decimal decimal_value;
 
2690
public:
 
2691
  Item_decimal(const char *str_arg, uint length, const CHARSET_INFO *charset);
 
2692
  Item_decimal(const Name_string &name_arg,
 
2693
               const my_decimal *val_arg, uint decimal_par, uint length);
 
2694
  Item_decimal(my_decimal *value_par);
 
2695
  Item_decimal(longlong val, bool unsig);
 
2696
  Item_decimal(double val, int precision, int scale);
 
2697
  Item_decimal(const uchar *bin, int precision, int scale);
 
2698
 
 
2699
  enum Type type() const { return DECIMAL_ITEM; }
 
2700
  enum Item_result result_type () const { return DECIMAL_RESULT; }
 
2701
  enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
 
2702
  longlong val_int();
 
2703
  double val_real();
 
2704
  String *val_str(String*);
 
2705
  my_decimal *val_decimal(my_decimal *val) { return &decimal_value; }
 
2706
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
2707
  {
 
2708
    return get_date_from_decimal(ltime, fuzzydate);
 
2709
  }
 
2710
  bool get_time(MYSQL_TIME *ltime)
 
2711
  {
 
2712
    return get_time_from_decimal(ltime);
 
2713
  }
 
2714
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
2715
  bool basic_const_item() const { return 1; }
 
2716
  Item *clone_item()
 
2717
  {
 
2718
    return new Item_decimal(item_name, &decimal_value, decimals, max_length);
 
2719
  }
 
2720
  virtual void print(String *str, enum_query_type query_type);
 
2721
  Item_num *neg()
 
2722
  {
 
2723
    my_decimal_neg(&decimal_value);
 
2724
    unsigned_flag= !decimal_value.sign();
 
2725
    return this;
 
2726
  }
 
2727
  uint decimal_precision() const { return decimal_value.precision(); }
 
2728
  bool eq(const Item *, bool binary_cmp) const;
 
2729
  void set_decimal_value(my_decimal *value_par);
 
2730
  bool check_partition_func_processor(uchar *bool_arg) { return FALSE;}
 
2731
};
 
2732
 
 
2733
 
 
2734
class Item_float :public Item_num
 
2735
{
 
2736
  Name_string presentation;
 
2737
public:
 
2738
  double value;
 
2739
  // Item_real() :value(0) {}
 
2740
  Item_float(const char *str_arg, uint length);
 
2741
  Item_float(const Name_string name_arg,
 
2742
             double val_arg, uint decimal_par, uint length)
 
2743
    :value(val_arg)
 
2744
  {
 
2745
    presentation= name_arg;
 
2746
    item_name= name_arg;
 
2747
    decimals= (uint8) decimal_par;
 
2748
    max_length= length;
 
2749
    fixed= 1;
 
2750
  }
 
2751
  Item_float(double value_par, uint decimal_par) :value(value_par)
 
2752
  {
 
2753
    decimals= (uint8) decimal_par;
 
2754
    fixed= 1;
 
2755
  }
 
2756
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
2757
  enum Type type() const { return REAL_ITEM; }
 
2758
  enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE; }
 
2759
  double val_real() { DBUG_ASSERT(fixed == 1); return value; }
 
2760
  longlong val_int()
 
2761
  {
 
2762
    DBUG_ASSERT(fixed == 1);
 
2763
    if (value <= (double) LONGLONG_MIN)
 
2764
    {
 
2765
       return LONGLONG_MIN;
 
2766
    }
 
2767
    else if (value >= (double) (ulonglong) LONGLONG_MAX)
 
2768
    {
 
2769
      return LONGLONG_MAX;
 
2770
    }
 
2771
    return (longlong) rint(value);
 
2772
  }
 
2773
  String *val_str(String*);
 
2774
  my_decimal *val_decimal(my_decimal *);
 
2775
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
2776
  {
 
2777
    return get_date_from_real(ltime, fuzzydate);
 
2778
  }
 
2779
  bool get_time(MYSQL_TIME *ltime)
 
2780
  {
 
2781
    return get_time_from_real(ltime);
 
2782
  }
 
2783
  bool basic_const_item() const { return 1; }
 
2784
  Item *clone_item()
 
2785
  { return new Item_float(item_name, value, decimals, max_length); }
 
2786
  Item_num *neg() { value= -value; return this; }
 
2787
  virtual void print(String *str, enum_query_type query_type);
 
2788
  bool eq(const Item *, bool binary_cmp) const;
 
2789
};
 
2790
 
 
2791
 
 
2792
class Item_static_float_func :public Item_float
 
2793
{
 
2794
  const Name_string func_name;
 
2795
public:
 
2796
  Item_static_float_func(const Name_string &name_arg,
 
2797
                         double val_arg, uint decimal_par, uint length)
 
2798
    :Item_float(null_name_string,
 
2799
                val_arg, decimal_par, length), func_name(name_arg)
 
2800
  {}
 
2801
 
 
2802
  virtual inline void print(String *str, enum_query_type query_type)
 
2803
  {
 
2804
    str->append(func_name);
 
2805
  }
 
2806
 
 
2807
  Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
2808
};
 
2809
 
 
2810
 
 
2811
class Item_string :public Item_basic_constant
 
2812
{
 
2813
public:
 
2814
  /* Create from a string, set name from the string itself. */
 
2815
  Item_string(const char *str,uint length,
 
2816
              const CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE,
 
2817
              uint repertoire= MY_REPERTOIRE_UNICODE30)
 
2818
    : m_cs_specified(FALSE)
 
2819
  {
 
2820
    str_value.set_or_copy_aligned(str, length, cs);
 
2821
    collation.set(cs, dv, repertoire);
 
2822
    /*
 
2823
      We have to have a different max_length than 'length' here to
 
2824
      ensure that we get the right length if we do use the item
 
2825
      to create a new table. In this case max_length must be the maximum
 
2826
      number of chars for a string of this type because we in Create_field::
 
2827
      divide the max_length with mbmaxlen).
 
2828
    */
 
2829
    max_length= str_value.numchars()*cs->mbmaxlen;
 
2830
    item_name.copy(str, length, cs);
 
2831
    decimals=NOT_FIXED_DEC;
 
2832
    // it is constant => can be used without fix_fields (and frequently used)
 
2833
    fixed= 1;
 
2834
  }
 
2835
  /* Just create an item and do not fill string representation */
 
2836
  Item_string(const CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE)
 
2837
    : m_cs_specified(FALSE)
 
2838
  {
 
2839
    collation.set(cs, dv);
 
2840
    max_length= 0;
 
2841
    decimals= NOT_FIXED_DEC;
 
2842
    fixed= 1;
 
2843
  }
 
2844
  /* Create from the given name and string. */
 
2845
  Item_string(const Name_string name_par, const char *str, uint length,
 
2846
              const CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE,
 
2847
              uint repertoire= MY_REPERTOIRE_UNICODE30)
 
2848
    : m_cs_specified(FALSE)
 
2849
  {
 
2850
    str_value.set_or_copy_aligned(str, length, cs);
 
2851
    collation.set(cs, dv, repertoire);
 
2852
    max_length= str_value.numchars()*cs->mbmaxlen;
 
2853
    item_name= name_par;
 
2854
    decimals=NOT_FIXED_DEC;
 
2855
    // it is constant => can be used without fix_fields (and frequently used)
 
2856
    fixed= 1;
 
2857
  }
 
2858
  /*
 
2859
    This is used in stored procedures to avoid memory leaks and
 
2860
    does a deep copy of its argument.
 
2861
  */
 
2862
  void set_str_with_copy(const char *str_arg, uint length_arg)
 
2863
  {
 
2864
    str_value.copy(str_arg, length_arg, collation.collation);
 
2865
    max_length= str_value.numchars() * collation.collation->mbmaxlen;
 
2866
  }
 
2867
  void set_repertoire_from_value()
 
2868
  {
 
2869
    collation.repertoire= my_string_repertoire(str_value.charset(),
 
2870
                                               str_value.ptr(),
 
2871
                                               str_value.length());
 
2872
  }
 
2873
  enum Type type() const { return STRING_ITEM; }
 
2874
  double val_real();
 
2875
  longlong val_int();
 
2876
  String *val_str(String*)
 
2877
  {
 
2878
    DBUG_ASSERT(fixed == 1);
 
2879
    return (String*) &str_value;
 
2880
  }
 
2881
  my_decimal *val_decimal(my_decimal *);
 
2882
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
2883
  {
 
2884
    return get_date_from_string(ltime, fuzzydate);
 
2885
  }
 
2886
  bool get_time(MYSQL_TIME *ltime)
 
2887
  {
 
2888
    return get_time_from_string(ltime);
 
2889
  }
 
2890
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
2891
  enum Item_result result_type () const { return STRING_RESULT; }
 
2892
  enum_field_types field_type() const { return MYSQL_TYPE_VARCHAR; }
 
2893
  bool basic_const_item() const { return 1; }
 
2894
  bool eq(const Item *item, bool binary_cmp) const;
 
2895
  Item *clone_item() 
 
2896
  {
 
2897
    return new Item_string(static_cast<Name_string>(item_name), str_value.ptr(), 
 
2898
                           str_value.length(), collation.collation);
 
2899
  }
 
2900
  Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
2901
  Item *charset_converter(const CHARSET_INFO *tocs, bool lossless);
 
2902
  inline void append(char *str, uint length)
 
2903
  {
 
2904
    str_value.append(str, length);
 
2905
    max_length= str_value.numchars() * collation.collation->mbmaxlen;
 
2906
  }
 
2907
  virtual void print(String *str, enum_query_type query_type);
 
2908
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
2909
 
 
2910
  /**
 
2911
    Return TRUE if character-set-introducer was explicitly specified in the
 
2912
    original query for this item (text literal).
 
2913
 
 
2914
    This operation is to be called from Item_string::print(). The idea is
 
2915
    that when a query is generated (re-constructed) from the Item-tree,
 
2916
    character-set-introducers should appear only for those literals, where
 
2917
    they were explicitly specified by the user. Otherwise, that may lead to
 
2918
    loss collation information (character set introducers implies default
 
2919
    collation for the literal).
 
2920
 
 
2921
    Basically, that makes sense only for views and hopefully will be gone
 
2922
    one day when we start using original query as a view definition.
 
2923
 
 
2924
    @return This operation returns the value of m_cs_specified attribute.
 
2925
      @retval TRUE if character set introducer was explicitly specified in
 
2926
      the original query.
 
2927
      @retval FALSE otherwise.
 
2928
  */
 
2929
  inline bool is_cs_specified() const
 
2930
  {
 
2931
    return m_cs_specified;
 
2932
  }
 
2933
 
 
2934
  /**
 
2935
    Set the value of m_cs_specified attribute.
 
2936
 
 
2937
    m_cs_specified attribute shows whether character-set-introducer was
 
2938
    explicitly specified in the original query for this text literal or
 
2939
    not. The attribute makes sense (is used) only for views.
 
2940
 
 
2941
    This operation is to be called from the parser during parsing an input
 
2942
    query.
 
2943
  */
 
2944
  inline void set_cs_specified(bool cs_specified)
 
2945
  {
 
2946
    m_cs_specified= cs_specified;
 
2947
  }
 
2948
 
 
2949
private:
 
2950
  bool m_cs_specified;
 
2951
};
 
2952
 
 
2953
 
 
2954
longlong 
 
2955
longlong_from_string_with_check (const CHARSET_INFO *cs,
 
2956
                                 const char *cptr, char *end);
 
2957
double 
 
2958
double_from_string_with_check (const CHARSET_INFO *cs,
 
2959
                               const char *cptr, char *end);
 
2960
 
 
2961
class Item_static_string_func :public Item_string
 
2962
{
 
2963
  const Name_string func_name;
 
2964
public:
 
2965
  Item_static_string_func(const Name_string &name_par,
 
2966
                          const char *str, uint length, const CHARSET_INFO *cs,
 
2967
                          Derivation dv= DERIVATION_COERCIBLE)
 
2968
    :Item_string(null_name_string, str, length, cs, dv), func_name(name_par)
 
2969
  {}
 
2970
  Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
2971
 
 
2972
  virtual inline void print(String *str, enum_query_type query_type)
 
2973
  {
 
2974
    str->append(func_name);
 
2975
  }
 
2976
 
 
2977
  bool check_partition_func_processor(uchar *int_arg) {return TRUE;}
 
2978
};
 
2979
 
 
2980
 
 
2981
/* for show tables */
 
2982
class Item_partition_func_safe_string: public Item_string
 
2983
{
 
2984
public:
 
2985
  Item_partition_func_safe_string(const Name_string name, uint length,
 
2986
                                  const CHARSET_INFO *cs= NULL):
 
2987
    Item_string(name, NullS, 0, cs)
 
2988
  {
 
2989
    max_length= length;
 
2990
  }
 
2991
};
 
2992
 
 
2993
 
 
2994
class Item_blob :public Item_partition_func_safe_string
 
2995
{
 
2996
public:
 
2997
  Item_blob(const char *name, uint length) :
 
2998
    Item_partition_func_safe_string(Name_string(name, strlen(name)),
 
2999
                                    length, &my_charset_bin)
 
3000
  { }
 
3001
  enum Type type() const { return TYPE_HOLDER; }
 
3002
  enum_field_types field_type() const { return MYSQL_TYPE_BLOB; }
 
3003
};
 
3004
 
 
3005
 
 
3006
/**
 
3007
  Item_empty_string -- is a utility class to put an item into List<Item>
 
3008
  which is then used in protocol.send_result_set_metadata() when sending SHOW output to
 
3009
  the client.
 
3010
*/
 
3011
 
 
3012
class Item_empty_string :public Item_partition_func_safe_string
 
3013
{
 
3014
public:
 
3015
  Item_empty_string(const char *header, uint length,
 
3016
                    const CHARSET_INFO *cs= NULL) :
 
3017
    Item_partition_func_safe_string(Name_string(header, strlen(header)),
 
3018
                                    0, cs ? cs : &my_charset_utf8_general_ci)
 
3019
    {
 
3020
      max_length= length * collation.collation->mbmaxlen;
 
3021
    }
 
3022
  void make_field(Send_field *field);
 
3023
};
 
3024
 
 
3025
 
 
3026
class Item_return_int :public Item_int
 
3027
{
 
3028
  enum_field_types int_field_type;
 
3029
public:
 
3030
  Item_return_int(const char *name_arg, uint length,
 
3031
                  enum_field_types field_type_arg, longlong value= 0)
 
3032
    :Item_int(Name_string(name_arg, name_arg ? strlen(name_arg) : 0),
 
3033
              value, length), int_field_type(field_type_arg)
 
3034
  {
 
3035
    unsigned_flag=1;
 
3036
  }
 
3037
  enum_field_types field_type() const { return int_field_type; }
 
3038
};
 
3039
 
 
3040
 
 
3041
class Item_hex_string: public Item_basic_constant
 
3042
{
 
3043
public:
 
3044
  Item_hex_string();
 
3045
  Item_hex_string(const char *str,uint str_length);
 
3046
  enum Type type() const { return VARBIN_ITEM; }
 
3047
  double val_real()
 
3048
  { 
 
3049
    DBUG_ASSERT(fixed == 1); 
 
3050
    return (double) (ulonglong) Item_hex_string::val_int();
 
3051
  }
 
3052
  longlong val_int();
 
3053
  bool basic_const_item() const { return 1; }
 
3054
  String *val_str(String*) { DBUG_ASSERT(fixed == 1); return &str_value; }
 
3055
  my_decimal *val_decimal(my_decimal *);
 
3056
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
3057
  {
 
3058
    return get_date_from_string(ltime, fuzzydate);
 
3059
  }
 
3060
  bool get_time(MYSQL_TIME *ltime)
 
3061
  {
 
3062
    return get_time_from_string(ltime);
 
3063
  }
 
3064
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
3065
  enum Item_result result_type () const { return STRING_RESULT; }
 
3066
  enum Item_result cast_to_int_type() const { return INT_RESULT; }
 
3067
  enum_field_types field_type() const { return MYSQL_TYPE_VARCHAR; }
 
3068
  virtual void print(String *str, enum_query_type query_type);
 
3069
  bool eq(const Item *item, bool binary_cmp) const;
 
3070
  virtual Item *safe_charset_converter(const CHARSET_INFO *tocs);
 
3071
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
3072
private:
 
3073
  void hex_string_init(const char *str, uint str_length);
 
3074
};
 
3075
 
 
3076
 
 
3077
class Item_bin_string: public Item_hex_string
 
3078
{
 
3079
public:
 
3080
  Item_bin_string(const char *str,uint str_length);
 
3081
};
 
3082
 
 
3083
class Item_result_field :public Item    /* Item with result field */
 
3084
{
 
3085
public:
 
3086
  Field *result_field;                          /* Save result here */
 
3087
  Item_result_field() :result_field(0) {}
 
3088
  // Constructor used for Item_sum/Item_cond_and/or (see Item comment)
 
3089
  Item_result_field(THD *thd, Item_result_field *item):
 
3090
    Item(thd, item), result_field(item->result_field)
 
3091
  {}
 
3092
  ~Item_result_field() {}                       /* Required with gcc 2.95 */
 
3093
  Field *get_tmp_table_field() { return result_field; }
 
3094
  Field *tmp_table_field(TABLE *t_arg) { return result_field; }
 
3095
  table_map used_tables() const { return 1; }
 
3096
  virtual void fix_length_and_dec()=0;
 
3097
  void set_result_field(Field *field) { result_field= field; }
 
3098
  bool is_result_field() { return 1; }
 
3099
  void save_in_result_field(bool no_conversions)
 
3100
  {
 
3101
    save_in_field(result_field, no_conversions);
 
3102
  }
 
3103
  void cleanup();
 
3104
  /*
 
3105
    This method is used for debug purposes to print the name of an
 
3106
    item to the debug log. The second use of this method is as
 
3107
    a helper function of print() and error messages, where it is
 
3108
    applicable. To suit both goals it should return a meaningful,
 
3109
    distinguishable and sintactically correct string. This method
 
3110
    should not be used for runtime type identification, use enum
 
3111
    {Sum}Functype and Item_func::functype()/Item_sum::sum_func()
 
3112
    instead.
 
3113
    Added here, to the parent class of both Item_func and Item_sum_func.
 
3114
 
 
3115
    NOTE: for Items inherited from Item_sum, func_name() return part of
 
3116
    function name till first argument (including '(') to make difference in
 
3117
    names for functions with 'distinct' clause and without 'distinct' and
 
3118
    also to make printing of items inherited from Item_sum uniform.
 
3119
  */
 
3120
  virtual const char *func_name() const= 0;
 
3121
};
 
3122
 
 
3123
 
 
3124
class Item_ref :public Item_ident
 
3125
{
 
3126
protected:
 
3127
  void set_properties();
 
3128
public:
 
3129
  enum Ref_Type { REF, DIRECT_REF, VIEW_REF, OUTER_REF, AGGREGATE_REF };
 
3130
  Field *result_field;                   /* Save result here */
 
3131
  Item **ref;
 
3132
  Item_ref(Name_resolution_context *context_arg,
 
3133
           const char *db_arg, const char *table_name_arg,
 
3134
           const char *field_name_arg)
 
3135
    :Item_ident(context_arg, db_arg, table_name_arg, field_name_arg),
 
3136
     result_field(0), ref(0) {}
 
3137
  /*
 
3138
    This constructor is used in two scenarios:
 
3139
    A) *item = NULL
 
3140
      No initialization is performed, fix_fields() call will be necessary.
 
3141
      
 
3142
    B) *item points to an Item this Item_ref will refer to. This is 
 
3143
      used for GROUP BY. fix_fields() will not be called in this case,
 
3144
      so we call set_properties to make this item "fixed". set_properties
 
3145
      performs a subset of action Item_ref::fix_fields does, and this subset
 
3146
      is enough for Item_ref's used in GROUP BY.
 
3147
    
 
3148
    TODO we probably fix a superset of problems like in BUG#6658. Check this 
 
3149
         with Bar, and if we have a more broader set of problems like this.
 
3150
  */
 
3151
  Item_ref(Name_resolution_context *context_arg, Item **item,
 
3152
           const char *table_name_arg, const char *field_name_arg,
 
3153
           bool alias_name_used_arg= FALSE);
 
3154
 
 
3155
  /* Constructor need to process subselect with temporary tables (see Item) */
 
3156
  Item_ref(THD *thd, Item_ref *item)
 
3157
    :Item_ident(thd, item), result_field(item->result_field), ref(item->ref) {}
 
3158
  enum Type type() const                { return REF_ITEM; }
 
3159
  bool eq(const Item *item, bool binary_cmp) const
 
3160
  { 
 
3161
    Item *it= ((Item *) item)->real_item();
 
3162
    return ref && (*ref)->eq(it, binary_cmp);
 
3163
  }
 
3164
  double val_real();
 
3165
  longlong val_int();
 
3166
  longlong val_time_temporal();
 
3167
  longlong val_date_temporal();
 
3168
  my_decimal *val_decimal(my_decimal *);
 
3169
  bool val_bool();
 
3170
  String *val_str(String* tmp);
 
3171
  bool is_null();
 
3172
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
3173
  double val_result();
 
3174
  longlong val_int_result();
 
3175
  String *str_result(String* tmp);
 
3176
  my_decimal *val_decimal_result(my_decimal *);
 
3177
  bool val_bool_result();
 
3178
  bool is_null_result();
 
3179
  bool send(Protocol *prot, String *tmp);
 
3180
  void make_field(Send_field *field);
 
3181
  bool fix_fields(THD *, Item **);
 
3182
  void fix_after_pullout(st_select_lex *parent_select,
 
3183
                         st_select_lex *removed_select);
 
3184
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
3185
  void save_org_in_field(Field *field);
 
3186
  enum Item_result result_type () const { return (*ref)->result_type(); }
 
3187
  enum_field_types field_type() const   { return (*ref)->field_type(); }
 
3188
  Field *get_tmp_table_field()
 
3189
  { return result_field ? result_field : (*ref)->get_tmp_table_field(); }
 
3190
  Item *get_tmp_table_item(THD *thd);
 
3191
  bool const_item() const
 
3192
  {
 
3193
    return (*ref)->const_item() && (used_tables() == 0);
 
3194
  }
 
3195
  table_map used_tables() const         
 
3196
  {
 
3197
    return depended_from ? OUTER_REF_TABLE_BIT : (*ref)->used_tables(); 
 
3198
  }
 
3199
  void update_used_tables() 
 
3200
  { 
 
3201
    if (!depended_from) 
 
3202
      (*ref)->update_used_tables(); 
 
3203
  }
 
3204
 
 
3205
  virtual table_map resolved_used_tables() const
 
3206
  { return (*ref)->resolved_used_tables(); }
 
3207
 
 
3208
  table_map not_null_tables() const
 
3209
  {
 
3210
    /*
 
3211
      It can happen that our 'depended_from' member is set but the
 
3212
      'depended_from' member of the referenced item is not (example: if a
 
3213
      field in a subquery belongs to an outer merged view), so we first test
 
3214
      ours:
 
3215
    */
 
3216
    return depended_from ? OUTER_REF_TABLE_BIT : (*ref)->not_null_tables();
 
3217
  }
 
3218
  void set_result_field(Field *field)   { result_field= field; }
 
3219
  bool is_result_field() { return 1; }
 
3220
  void save_in_result_field(bool no_conversions)
 
3221
  {
 
3222
    (*ref)->save_in_field(result_field, no_conversions);
 
3223
  }
 
3224
  Item *real_item()
 
3225
  {
 
3226
    return ref ? (*ref)->real_item() : this;
 
3227
  }
 
3228
  bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
 
3229
  {
 
3230
    return (*ref)->walk(processor, walk_subquery, arg) ||
 
3231
           (this->*processor)(arg);
 
3232
  }
 
3233
  virtual Item* transform(Item_transformer, uchar *arg);
 
3234
  virtual Item* compile(Item_analyzer analyzer, uchar **arg_p,
 
3235
                        Item_transformer transformer, uchar *arg_t);
 
3236
  virtual bool explain_subquery_checker(uchar **arg)
 
3237
  {
 
3238
    /*
 
3239
      Always return false: we don't need to go deeper into referenced
 
3240
      expression tree since we have to mark aliased subqueries at
 
3241
      their original places (select list, derived tables), not by
 
3242
      references from other expression (order by etc).
 
3243
    */
 
3244
    return false;
 
3245
  }
 
3246
  virtual void print(String *str, enum_query_type query_type);
 
3247
  void cleanup();
 
3248
  Item_field *field_for_view_update()
 
3249
    { return (*ref)->field_for_view_update(); }
 
3250
  virtual Ref_Type ref_type() { return REF; }
 
3251
 
 
3252
  // Row emulation: forwarding of ROW-related calls to ref
 
3253
  uint cols()
 
3254
  {
 
3255
    return ref && result_type() == ROW_RESULT ? (*ref)->cols() : 1;
 
3256
  }
 
3257
  Item* element_index(uint i)
 
3258
  {
 
3259
    return ref && result_type() == ROW_RESULT ? (*ref)->element_index(i) : this;
 
3260
  }
 
3261
  Item** addr(uint i)
 
3262
  {
 
3263
    return ref && result_type() == ROW_RESULT ? (*ref)->addr(i) : 0;
 
3264
  }
 
3265
  bool check_cols(uint c)
 
3266
  {
 
3267
    return ref && result_type() == ROW_RESULT ? (*ref)->check_cols(c) 
 
3268
                                              : Item::check_cols(c);
 
3269
  }
 
3270
  bool null_inside()
 
3271
  {
 
3272
    return ref && result_type() == ROW_RESULT ? (*ref)->null_inside() : 0;
 
3273
  }
 
3274
  void bring_value()
 
3275
  { 
 
3276
    if (ref && result_type() == ROW_RESULT)
 
3277
      (*ref)->bring_value();
 
3278
  }
 
3279
  bool get_time(MYSQL_TIME *ltime)
 
3280
  {
 
3281
    DBUG_ASSERT(fixed);
 
3282
    return (*ref)->get_time(ltime);
 
3283
  }
 
3284
  virtual bool basic_const_item() const { return ref && (*ref)->basic_const_item(); }
 
3285
  bool is_outer_field() const
 
3286
  {
 
3287
    DBUG_ASSERT(fixed);
 
3288
    DBUG_ASSERT(ref);
 
3289
    return (*ref)->is_outer_field();
 
3290
  }
 
3291
 
 
3292
  /**
 
3293
    Checks if the item tree that ref points to contains a subquery.
 
3294
  */
 
3295
  virtual bool has_subquery() const 
 
3296
  { 
 
3297
    DBUG_ASSERT(ref);
 
3298
    return (*ref)->has_subquery();
 
3299
  }
 
3300
 
 
3301
  /**
 
3302
    Checks if the item tree that ref points to contains a stored program.
 
3303
  */
 
3304
  virtual bool has_stored_program() const 
 
3305
  { 
 
3306
    DBUG_ASSERT(ref);
 
3307
    return (*ref)->has_stored_program();
 
3308
  }
 
3309
 
 
3310
  virtual bool created_by_in2exists() const
 
3311
  {
 
3312
    return (*ref)->created_by_in2exists();
 
3313
  }
 
3314
};
 
3315
 
 
3316
 
 
3317
/*
 
3318
  The same as Item_ref, but get value from val_* family of method to get
 
3319
  value of item on which it referred instead of result* family.
 
3320
*/
 
3321
class Item_direct_ref :public Item_ref
 
3322
{
 
3323
public:
 
3324
  Item_direct_ref(Name_resolution_context *context_arg, Item **item,
 
3325
                  const char *table_name_arg,
 
3326
                  const char *field_name_arg,
 
3327
                  bool alias_name_used_arg= FALSE)
 
3328
    :Item_ref(context_arg, item, table_name_arg,
 
3329
              field_name_arg, alias_name_used_arg)
 
3330
  {}
 
3331
  /* Constructor need to process subselect with temporary tables (see Item) */
 
3332
  Item_direct_ref(THD *thd, Item_direct_ref *item) : Item_ref(thd, item) {}
 
3333
 
 
3334
  double val_real();
 
3335
  longlong val_int();
 
3336
  longlong val_time_temporal();
 
3337
  longlong val_date_temporal();
 
3338
  String *val_str(String* tmp);
 
3339
  my_decimal *val_decimal(my_decimal *);
 
3340
  bool val_bool();
 
3341
  bool is_null();
 
3342
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
3343
  virtual Ref_Type ref_type() { return DIRECT_REF; }
 
3344
};
 
3345
 
 
3346
/*
 
3347
  Class for view fields, the same as Item_direct_ref, but call fix_fields
 
3348
  of reference if it is not called yet
 
3349
*/
 
3350
class Item_direct_view_ref :public Item_direct_ref
 
3351
{
 
3352
public:
 
3353
  Item_direct_view_ref(Name_resolution_context *context_arg,
 
3354
                       Item **item,
 
3355
                       const char *alias_name_arg,
 
3356
                       const char *table_name_arg,
 
3357
                       const char *field_name_arg)
 
3358
    : Item_direct_ref(context_arg, item, alias_name_arg, field_name_arg)
 
3359
  {
 
3360
    orig_table_name= table_name_arg;
 
3361
  }
 
3362
 
 
3363
  /* Constructor need to process subselect with temporary tables (see Item) */
 
3364
  Item_direct_view_ref(THD *thd, Item_direct_ref *item)
 
3365
    :Item_direct_ref(thd, item) {}
 
3366
 
 
3367
  /*
 
3368
    We share one underlying Item_field, so we have to disable
 
3369
    build_equal_items_for_cond().
 
3370
    TODO: Implement multiple equality optimization for views.
 
3371
  */
 
3372
  virtual bool subst_argument_checker(uchar **arg)
 
3373
  {
 
3374
    return false;
 
3375
  }
 
3376
 
 
3377
  bool fix_fields(THD *, Item **);
 
3378
  bool eq(const Item *item, bool binary_cmp) const;
 
3379
  Item *get_tmp_table_item(THD *thd)
 
3380
  {
 
3381
    Item *item= Item_ref::get_tmp_table_item(thd);
 
3382
    item->item_name= item_name;
 
3383
    return item;
 
3384
  }
 
3385
  virtual Ref_Type ref_type() { return VIEW_REF; }
 
3386
};
 
3387
 
 
3388
 
 
3389
/*
 
3390
  Class for outer fields.
 
3391
  An object of this class is created when the select where the outer field was
 
3392
  resolved is a grouping one. After it has been fixed the ref field will point
 
3393
  to either an Item_ref or an Item_direct_ref object which will be used to
 
3394
  access the field.
 
3395
  The ref field may also point to an Item_field instance.
 
3396
  See also comments for the fix_inner_refs() and the
 
3397
  Item_field::fix_outer_field() functions.
 
3398
*/
 
3399
 
 
3400
class Item_sum;
 
3401
class Item_outer_ref :public Item_direct_ref
 
3402
{
 
3403
public:
 
3404
  Item *outer_ref;
 
3405
  /* The aggregate function under which this outer ref is used, if any. */
 
3406
  Item_sum *in_sum_func;
 
3407
  /*
 
3408
    TRUE <=> that the outer_ref is already present in the select list
 
3409
    of the outer select.
 
3410
  */
 
3411
  bool found_in_select_list;
 
3412
  Item_outer_ref(Name_resolution_context *context_arg,
 
3413
                 Item_field *outer_field_arg)
 
3414
    :Item_direct_ref(context_arg, 0, outer_field_arg->table_name,
 
3415
                     outer_field_arg->field_name),
 
3416
    outer_ref(outer_field_arg), in_sum_func(0),
 
3417
    found_in_select_list(0)
 
3418
  {
 
3419
    ref= &outer_ref;
 
3420
    set_properties();
 
3421
    fixed= 0;
 
3422
  }
 
3423
  Item_outer_ref(Name_resolution_context *context_arg, Item **item,
 
3424
                 const char *table_name_arg, const char *field_name_arg,
 
3425
                 bool alias_name_used_arg)
 
3426
    :Item_direct_ref(context_arg, item, table_name_arg, field_name_arg,
 
3427
                     alias_name_used_arg),
 
3428
    outer_ref(0), in_sum_func(0), found_in_select_list(1)
 
3429
  {}
 
3430
  void save_in_result_field(bool no_conversions)
 
3431
  {
 
3432
    outer_ref->save_org_in_field(result_field);
 
3433
  }
 
3434
  bool fix_fields(THD *, Item **);
 
3435
  void fix_after_pullout(st_select_lex *parent_select,
 
3436
                         st_select_lex *removed_select);
 
3437
  table_map used_tables() const
 
3438
  {
 
3439
    return (*ref)->const_item() ? 0 : OUTER_REF_TABLE_BIT;
 
3440
  }
 
3441
  table_map not_null_tables() const { return 0; }
 
3442
 
 
3443
  virtual Ref_Type ref_type() { return OUTER_REF; }
 
3444
};
 
3445
 
 
3446
 
 
3447
class Item_in_subselect;
 
3448
 
 
3449
 
 
3450
/*
 
3451
  An object of this class:
 
3452
   - Converts val_XXX() calls to ref->val_XXX_result() calls, like Item_ref.
 
3453
   - Sets owner->was_null=TRUE if it has returned a NULL value from any
 
3454
     val_XXX() function. This allows to inject an Item_ref_null_helper
 
3455
     object into subquery and then check if the subquery has produced a row
 
3456
     with NULL value.
 
3457
*/
 
3458
 
 
3459
class Item_ref_null_helper: public Item_ref
 
3460
{
 
3461
protected:
 
3462
  Item_in_subselect* owner;
 
3463
public:
 
3464
  Item_ref_null_helper(Name_resolution_context *context_arg,
 
3465
                       Item_in_subselect* master, Item **item,
 
3466
                       const char *table_name_arg, const char *field_name_arg)
 
3467
    :Item_ref(context_arg, item, table_name_arg, field_name_arg),
 
3468
     owner(master) {}
 
3469
  double val_real();
 
3470
  longlong val_int();
 
3471
  longlong val_time_temporal();
 
3472
  longlong val_date_temporal();
 
3473
  String* val_str(String* s);
 
3474
  my_decimal *val_decimal(my_decimal *);
 
3475
  bool val_bool();
 
3476
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
 
3477
  virtual void print(String *str, enum_query_type query_type);
 
3478
  /*
 
3479
    we add RAND_TABLE_BIT to prevent moving this item from HAVING to WHERE
 
3480
  */
 
3481
  table_map used_tables() const
 
3482
  {
 
3483
    return (depended_from ?
 
3484
            OUTER_REF_TABLE_BIT :
 
3485
            (*ref)->used_tables() | RAND_TABLE_BIT);
 
3486
  }
 
3487
};
 
3488
 
 
3489
/*
 
3490
  The following class is used to optimize comparing of bigint columns.
 
3491
  We need to save the original item ('ref') to be able to call
 
3492
  ref->save_in_field(). This is used to create index search keys.
 
3493
  
 
3494
  An instance of Item_int_with_ref may have signed or unsigned integer value.
 
3495
  
 
3496
*/
 
3497
 
 
3498
class Item_int_with_ref :public Item_int
 
3499
{
 
3500
protected:
 
3501
  Item *ref;
 
3502
public:
 
3503
  Item_int_with_ref(longlong i, Item *ref_arg, my_bool unsigned_arg) :
 
3504
    Item_int(i), ref(ref_arg)
 
3505
  {
 
3506
    unsigned_flag= unsigned_arg;
 
3507
  }
 
3508
  type_conversion_status save_in_field(Field *field, bool no_conversions)
 
3509
  {
 
3510
    return ref->save_in_field(field, no_conversions);
 
3511
  }
 
3512
  Item *clone_item();
 
3513
  virtual Item *real_item() { return ref; }
 
3514
};
 
3515
 
 
3516
 
 
3517
/*
 
3518
  Similar to Item_int_with_ref, but to optimize comparing of temporal columns.
 
3519
*/
 
3520
class Item_temporal_with_ref :public Item_int_with_ref
 
3521
{
 
3522
private:
 
3523
  enum_field_types cached_field_type;
 
3524
public:
 
3525
  Item_temporal_with_ref(enum_field_types field_type_arg,
 
3526
                         uint8 decimals_arg, longlong i, Item *ref_arg,
 
3527
                         bool unsigned_flag):
 
3528
    Item_int_with_ref(i, ref_arg, unsigned_flag),
 
3529
    cached_field_type(field_type_arg)
 
3530
  {
 
3531
    decimals= decimals_arg;
 
3532
  }
 
3533
  enum_field_types field_type() const { return cached_field_type; }
 
3534
  void print(String *str, enum_query_type query_type);
 
3535
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
3536
  {
 
3537
    DBUG_ASSERT(0);
 
3538
    return true;
 
3539
  }
 
3540
  bool get_time(MYSQL_TIME *ltime)
 
3541
  {
 
3542
    DBUG_ASSERT(0);
 
3543
    return true;
 
3544
  }
 
3545
 
 
3546
};
 
3547
 
 
3548
 
 
3549
/*
 
3550
  Item_datetime_with_ref is used to optimize queries like:
 
3551
    SELECT ... FROM t1 WHERE date_or_datetime_column = 20110101101010;
 
3552
  The numeric constant is replaced to Item_datetime_with_ref
 
3553
  by convert_constant_item().
 
3554
*/
 
3555
class Item_datetime_with_ref :public Item_temporal_with_ref
 
3556
{
 
3557
private:
 
3558
  enum_field_types cached_field_type;
 
3559
public:
 
3560
  /**
 
3561
    Constructor for Item_datetime_with_ref.
 
3562
    @param    field_type_arg Data type: MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME
 
3563
    @param    decimals_arg   Number of fractional digits.
 
3564
    @param    i              Temporal value in packed format.
 
3565
    @param    ref_arg        Pointer to the original numeric Item.
 
3566
  */
 
3567
  Item_datetime_with_ref(enum_field_types field_type_arg,
 
3568
                         uint8 decimals_arg, longlong i, Item *ref_arg):
 
3569
    Item_temporal_with_ref(field_type_arg, decimals_arg, i, ref_arg, true),
 
3570
    cached_field_type(field_type_arg)
 
3571
  {
 
3572
  }
 
3573
  Item *clone_item();
 
3574
  longlong val_date_temporal() { return val_int(); }
 
3575
  longlong val_time_temporal()
 
3576
  {
 
3577
    DBUG_ASSERT(0);
 
3578
    return val_int();
 
3579
  }
 
3580
};
 
3581
 
 
3582
 
 
3583
/*
 
3584
  Item_time_with_ref is used to optimize queries like:
 
3585
    SELECT ... FROM t1 WHERE time_column = 20110101101010;
 
3586
  The numeric constant is replaced to Item_time_with_ref
 
3587
  by convert_constant_item().
 
3588
*/
 
3589
class Item_time_with_ref :public Item_temporal_with_ref
 
3590
{
 
3591
public:
 
3592
  /**
 
3593
    Constructor for Item_time_with_ref.
 
3594
    @param    decimals_arg   Number of fractional digits.
 
3595
    @param    i              Temporal value in packed format.
 
3596
    @param    ref_arg        Pointer to the original numeric Item.
 
3597
  */
 
3598
  Item_time_with_ref(uint8 decimals_arg, longlong i, Item *ref_arg):
 
3599
    Item_temporal_with_ref(MYSQL_TYPE_TIME, decimals_arg, i, ref_arg, 0)
 
3600
  {
 
3601
  }
 
3602
  Item *clone_item();
 
3603
  longlong val_time_temporal() { return val_int(); }
 
3604
  longlong val_date_temporal()
 
3605
  {
 
3606
    DBUG_ASSERT(0);
 
3607
    return val_int();
 
3608
  }
 
3609
};
 
3610
 
 
3611
 
 
3612
#ifdef MYSQL_SERVER
 
3613
#include "gstream.h"
 
3614
#include "spatial.h"
 
3615
#include "item_sum.h"
 
3616
#include "item_func.h"
 
3617
#include "item_row.h"
 
3618
#include "item_cmpfunc.h"
 
3619
#include "item_strfunc.h"
 
3620
#include "item_geofunc.h"
 
3621
#include "item_timefunc.h"
 
3622
#include "item_subselect.h"
 
3623
#include "item_xmlfunc.h"
 
3624
#include "item_create.h"
 
3625
#endif
 
3626
 
 
3627
/**
 
3628
  Base class to implement typed value caching Item classes
 
3629
 
 
3630
  Item_copy_ classes are very similar to the corresponding Item_
 
3631
  classes (e.g. Item_copy_int is similar to Item_int) but they add
 
3632
  the following additional functionality to Item_ :
 
3633
    1. Nullability
 
3634
    2. Possibility to store the value not only on instantiation time,
 
3635
       but also later.
 
3636
  Item_copy_ classes are a functionality subset of Item_cache_ 
 
3637
  classes, as e.g. they don't support comparisons with the original Item
 
3638
  as Item_cache_ classes do.
 
3639
  Item_copy_ classes are used in GROUP BY calculation.
 
3640
  TODO: Item_copy should be made an abstract interface and Item_copy_
 
3641
  classes should inherit both the respective Item_ class and the interface.
 
3642
  Ideally we should drop Item_copy_ classes altogether and merge 
 
3643
  their functionality to Item_cache_ (and these should be made to inherit
 
3644
  from Item_).
 
3645
*/
 
3646
 
 
3647
class Item_copy :public Item
 
3648
{
 
3649
protected:  
 
3650
 
 
3651
  /**
 
3652
    Stores the type of the resulting field that would be used to store the data
 
3653
    in the cache. This is to avoid calls to the original item.
 
3654
  */
 
3655
  enum enum_field_types cached_field_type;
 
3656
 
 
3657
  /** The original item that is copied */
 
3658
  Item *item;
 
3659
 
 
3660
  /**
 
3661
    Stores the result type of the original item, so it can be returned
 
3662
    without calling the original item's method
 
3663
  */
 
3664
  Item_result cached_result_type;
 
3665
 
 
3666
  /**
 
3667
    Constructor of the Item_copy class
 
3668
 
 
3669
    stores metadata information about the original class as well as a 
 
3670
    pointer to it.
 
3671
  */
 
3672
  Item_copy(Item *i)
 
3673
  {
 
3674
    item= i;
 
3675
    null_value=maybe_null=item->maybe_null;
 
3676
    decimals=item->decimals;
 
3677
    max_length=item->max_length;
 
3678
    item_name= item->item_name;
 
3679
    cached_field_type= item->field_type();
 
3680
    cached_result_type= item->result_type();
 
3681
    unsigned_flag= item->unsigned_flag;
 
3682
    fixed= item->fixed;
 
3683
    collation.set(item->collation);
 
3684
  }
 
3685
 
 
3686
public:
 
3687
  /** 
 
3688
    Factory method to create the appropriate subclass dependent on the type of 
 
3689
    the original item.
 
3690
 
 
3691
    @param item      the original item.
 
3692
  */  
 
3693
  static Item_copy *create (Item *item);
 
3694
 
 
3695
  /** 
 
3696
    Update the cache with the value of the original item
 
3697
   
 
3698
    This is the method that updates the cached value.
 
3699
    It must be explicitly called by the user of this class to store the value 
 
3700
    of the orginal item in the cache.
 
3701
  */  
 
3702
  virtual void copy() = 0;
 
3703
 
 
3704
  Item *get_item() { return item; }
 
3705
  /** All of the subclasses should have the same type tag */
 
3706
  enum Type type() const { return COPY_STR_ITEM; }
 
3707
  enum_field_types field_type() const { return cached_field_type; }
 
3708
  enum Item_result result_type () const { return cached_result_type; }
 
3709
 
 
3710
  void make_field(Send_field *field) { item->make_field(field); }
 
3711
  table_map used_tables() const { return (table_map) 1L; }
 
3712
  bool const_item() const { return 0; }
 
3713
  bool is_null() { return null_value; }
 
3714
 
 
3715
  virtual void no_rows_in_result()
 
3716
  {
 
3717
    item->no_rows_in_result();
 
3718
  }
 
3719
 
 
3720
  /*  
 
3721
    Override the methods below as pure virtual to make sure all the 
 
3722
    sub-classes implement them.
 
3723
  */  
 
3724
 
 
3725
  virtual String *val_str(String*) = 0;
 
3726
  virtual my_decimal *val_decimal(my_decimal *) = 0;
 
3727
  virtual double val_real() = 0;
 
3728
  virtual longlong val_int() = 0;
 
3729
  virtual bool get_date(MYSQL_TIME *ltime, uint fuzzydate)= 0;
 
3730
  virtual bool get_time(MYSQL_TIME *ltime)= 0;
 
3731
  virtual type_conversion_status save_in_field(Field *field,
 
3732
                                               bool no_conversions) = 0;
 
3733
};
 
3734
 
 
3735
/**
 
3736
 Implementation of a string cache.
 
3737
 
 
3738
 Uses Item::str_value for storage
 
3739
*/ 
 
3740
class Item_copy_string : public Item_copy
 
3741
{
 
3742
public:
 
3743
  Item_copy_string (Item *item) : Item_copy(item) {}
 
3744
 
 
3745
  String *val_str(String*);
 
3746
  my_decimal *val_decimal(my_decimal *);
 
3747
  double val_real();
 
3748
  longlong val_int();
 
3749
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
 
3750
  bool get_time(MYSQL_TIME *ltime);
 
3751
  void copy();
 
3752
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
3753
};
 
3754
 
 
3755
 
 
3756
class Item_copy_int : public Item_copy
 
3757
{
 
3758
protected:  
 
3759
  longlong cached_value; 
 
3760
public:
 
3761
  Item_copy_int (Item *i) : Item_copy(i) {}
 
3762
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
3763
 
 
3764
  virtual String *val_str(String*);
 
3765
  virtual my_decimal *val_decimal(my_decimal *);
 
3766
  virtual double val_real()
 
3767
  {
 
3768
    return null_value ? 0.0 : (double) cached_value;
 
3769
  }
 
3770
  virtual longlong val_int()
 
3771
  {
 
3772
    return null_value ? LL(0) : cached_value;
 
3773
  }
 
3774
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
3775
  {
 
3776
    return get_date_from_int(ltime, fuzzydate);
 
3777
  }
 
3778
  bool get_time(MYSQL_TIME *ltime)
 
3779
  {
 
3780
    return get_time_from_int(ltime);
 
3781
  }
 
3782
  virtual void copy();
 
3783
};
 
3784
 
 
3785
 
 
3786
class Item_copy_uint : public Item_copy_int
 
3787
{
 
3788
public:
 
3789
  Item_copy_uint (Item *item) : Item_copy_int(item) 
 
3790
  {
 
3791
    unsigned_flag= 1;
 
3792
  }
 
3793
 
 
3794
  String *val_str(String*);
 
3795
  double val_real()
 
3796
  {
 
3797
    return null_value ? 0.0 : (double) (ulonglong) cached_value;
 
3798
  }
 
3799
};
 
3800
 
 
3801
 
 
3802
class Item_copy_float : public Item_copy
 
3803
{
 
3804
protected:  
 
3805
  double cached_value; 
 
3806
public:
 
3807
  Item_copy_float (Item *i) : Item_copy(i) {}
 
3808
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
3809
 
 
3810
  String *val_str(String*);
 
3811
  my_decimal *val_decimal(my_decimal *);
 
3812
  double val_real()
 
3813
  {
 
3814
    return null_value ? 0.0 : cached_value;
 
3815
  }
 
3816
  longlong val_int()
 
3817
  {
 
3818
    return (longlong) rint(val_real());
 
3819
  }
 
3820
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
3821
  {
 
3822
    return get_date_from_real(ltime, fuzzydate);
 
3823
  }
 
3824
  bool get_time(MYSQL_TIME *ltime)
 
3825
  {
 
3826
    return get_time_from_real(ltime);
 
3827
  }
 
3828
  void copy()
 
3829
  {
 
3830
    cached_value= item->val_real();
 
3831
    null_value= item->null_value;
 
3832
  }
 
3833
};
 
3834
 
 
3835
 
 
3836
class Item_copy_decimal : public Item_copy
 
3837
{
 
3838
protected:  
 
3839
  my_decimal cached_value;
 
3840
public:
 
3841
  Item_copy_decimal (Item *i) : Item_copy(i) {}
 
3842
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
3843
 
 
3844
  String *val_str(String*);
 
3845
  my_decimal *val_decimal(my_decimal *) 
 
3846
  { 
 
3847
    return null_value ? NULL: &cached_value; 
 
3848
  }
 
3849
  double val_real();
 
3850
  longlong val_int();
 
3851
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
3852
  {
 
3853
    return get_date_from_decimal(ltime, fuzzydate);
 
3854
  }
 
3855
  bool get_time(MYSQL_TIME *ltime)
 
3856
  {
 
3857
    return get_time_from_decimal(ltime);
 
3858
  }
 
3859
  void copy();
 
3860
};
 
3861
 
 
3862
 
 
3863
class Cached_item :public Sql_alloc
 
3864
{
 
3865
public:
 
3866
  my_bool null_value;
 
3867
  Cached_item() :null_value(0) {}
 
3868
  virtual bool cmp(void)=0;
 
3869
  virtual ~Cached_item(); /*line -e1509 */
 
3870
};
 
3871
 
 
3872
class Cached_item_str :public Cached_item
 
3873
{
 
3874
  Item *item;
 
3875
  uint32 value_max_length;
 
3876
  String value,tmp_value;
 
3877
public:
 
3878
  Cached_item_str(THD *thd, Item *arg);
 
3879
  bool cmp(void);
 
3880
  ~Cached_item_str();                           // Deallocate String:s
 
3881
};
 
3882
 
 
3883
 
 
3884
class Cached_item_real :public Cached_item
 
3885
{
 
3886
  Item *item;
 
3887
  double value;
 
3888
public:
 
3889
  Cached_item_real(Item *item_par) :item(item_par),value(0.0) {}
 
3890
  bool cmp(void);
 
3891
};
 
3892
 
 
3893
class Cached_item_int :public Cached_item
 
3894
{
 
3895
  Item *item;
 
3896
  longlong value;
 
3897
public:
 
3898
  Cached_item_int(Item *item_par) :item(item_par),value(0) {}
 
3899
  bool cmp(void);
 
3900
};
 
3901
 
 
3902
class Cached_item_temporal :public Cached_item
 
3903
{
 
3904
  Item *item;
 
3905
  longlong value;
 
3906
public:
 
3907
  Cached_item_temporal(Item *item_par) :item(item_par), value(0) {}
 
3908
  bool cmp(void);
 
3909
};
 
3910
 
 
3911
 
 
3912
class Cached_item_decimal :public Cached_item
 
3913
{
 
3914
  Item *item;
 
3915
  my_decimal value;
 
3916
public:
 
3917
  Cached_item_decimal(Item *item_par);
 
3918
  bool cmp(void);
 
3919
};
 
3920
 
 
3921
class Cached_item_field :public Cached_item
 
3922
{
 
3923
  uchar *buff;
 
3924
  Field *field;
 
3925
  uint length;
 
3926
 
 
3927
public:
 
3928
#ifndef DBUG_OFF
 
3929
  void dbug_print()
 
3930
  {
 
3931
    uchar *org_ptr;
 
3932
    org_ptr= field->ptr;
 
3933
    fprintf(DBUG_FILE, "new: ");
 
3934
    field->dbug_print();
 
3935
    field->ptr= buff;
 
3936
    fprintf(DBUG_FILE, ", old: ");
 
3937
    field->dbug_print();
 
3938
    field->ptr= org_ptr;
 
3939
    fprintf(DBUG_FILE, "\n");
 
3940
  }
 
3941
#endif
 
3942
  Cached_item_field(Field *arg_field) : field(arg_field)
 
3943
  {
 
3944
    field= arg_field;
 
3945
    /* TODO: take the memory allocation below out of the constructor. */
 
3946
    buff= (uchar*) sql_calloc(length=field->pack_length());
 
3947
  }
 
3948
  bool cmp(void);
 
3949
};
 
3950
 
 
3951
class Item_default_value : public Item_field
 
3952
{
 
3953
public:
 
3954
  Item *arg;
 
3955
  Item_default_value(Name_resolution_context *context_arg)
 
3956
    :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
 
3957
               (const char *)NULL),
 
3958
     arg(NULL) {}
 
3959
  Item_default_value(Name_resolution_context *context_arg, Item *a)
 
3960
    :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
 
3961
                (const char *)NULL),
 
3962
     arg(a) {}
 
3963
  enum Type type() const { return DEFAULT_VALUE_ITEM; }
 
3964
  bool eq(const Item *item, bool binary_cmp) const;
 
3965
  bool fix_fields(THD *, Item **);
 
3966
  virtual void print(String *str, enum_query_type query_type);
 
3967
  type_conversion_status save_in_field(Field *field_arg, bool no_conversions);
 
3968
  table_map used_tables() const { return (table_map)0L; }
 
3969
  Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
 
3970
 
 
3971
  bool walk(Item_processor processor, bool walk_subquery, uchar *args)
 
3972
  {
 
3973
    if (arg && arg->walk(processor, walk_subquery, args))
 
3974
      return true;
 
3975
 
 
3976
    return (this->*processor)(args);
 
3977
  }
 
3978
 
 
3979
  Item *transform(Item_transformer transformer, uchar *args);
 
3980
};
 
3981
 
 
3982
/*
 
3983
  Item_insert_value -- an implementation of VALUES() function.
 
3984
  You can use the VALUES(col_name) function in the UPDATE clause
 
3985
  to refer to column values from the INSERT portion of the INSERT
 
3986
  ... UPDATE statement. In other words, VALUES(col_name) in the
 
3987
  UPDATE clause refers to the value of col_name that would be
 
3988
  inserted, had no duplicate-key conflict occurred.
 
3989
  In all other places this function returns NULL.
 
3990
*/
 
3991
 
 
3992
class Item_insert_value : public Item_field
 
3993
{
 
3994
public:
 
3995
  Item *arg;
 
3996
  Item_insert_value(Name_resolution_context *context_arg, Item *a)
 
3997
    :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
 
3998
               (const char *)NULL),
 
3999
     arg(a) {}
 
4000
  bool eq(const Item *item, bool binary_cmp) const;
 
4001
  bool fix_fields(THD *, Item **);
 
4002
  virtual void print(String *str, enum_query_type query_type);
 
4003
  type_conversion_status save_in_field(Field *field_arg, bool no_conversions)
 
4004
  {
 
4005
    return Item_field::save_in_field(field_arg, no_conversions);
 
4006
  }
 
4007
  /* 
 
4008
   We use RAND_TABLE_BIT to prevent Item_insert_value from
 
4009
   being treated as a constant and precalculated before execution
 
4010
  */
 
4011
  table_map used_tables() const { return RAND_TABLE_BIT; }
 
4012
 
 
4013
  bool walk(Item_processor processor, bool walk_subquery, uchar *args)
 
4014
  {
 
4015
    return arg->walk(processor, walk_subquery, args) ||
 
4016
            (this->*processor)(args);
 
4017
  }
 
4018
};
 
4019
 
 
4020
 
 
4021
class Table_triggers_list;
 
4022
 
 
4023
/*
 
4024
  Represents NEW/OLD version of field of row which is
 
4025
  changed/read in trigger.
 
4026
 
 
4027
  Note: For this item main part of actual binding to Field object happens
 
4028
        not during fix_fields() call (like for Item_field) but right after
 
4029
        parsing of trigger definition, when table is opened, with special
 
4030
        setup_field() call. On fix_fields() stage we simply choose one of
 
4031
        two Field instances representing either OLD or NEW version of this
 
4032
        field.
 
4033
*/
 
4034
class Item_trigger_field : public Item_field,
 
4035
                           private Settable_routine_parameter
 
4036
{
 
4037
public:
 
4038
  /* Is this item represents row from NEW or OLD row ? */
 
4039
  enum row_version_type {OLD_ROW, NEW_ROW};
 
4040
  row_version_type row_version;
 
4041
  /* Next in list of all Item_trigger_field's in trigger */
 
4042
  Item_trigger_field *next_trg_field;
 
4043
  /* Index of the field in the TABLE::field array */
 
4044
  uint field_idx;
 
4045
  /* Pointer to Table_trigger_list object for table of this trigger */
 
4046
  Table_triggers_list *triggers;
 
4047
 
 
4048
  Item_trigger_field(Name_resolution_context *context_arg,
 
4049
                     row_version_type row_ver_arg,
 
4050
                     const char *field_name_arg,
 
4051
                     ulong priv, const bool ro)
 
4052
    :Item_field(context_arg,
 
4053
               (const char *)NULL, (const char *)NULL, field_name_arg),
 
4054
     row_version(row_ver_arg), field_idx((uint)-1), original_privilege(priv),
 
4055
     want_privilege(priv), table_grants(NULL), read_only (ro)
 
4056
  {}
 
4057
  void setup_field(THD *thd, TABLE *table, GRANT_INFO *table_grant_info);
 
4058
  enum Type type() const { return TRIGGER_FIELD_ITEM; }
 
4059
  bool eq(const Item *item, bool binary_cmp) const;
 
4060
  bool fix_fields(THD *, Item **);
 
4061
  virtual void print(String *str, enum_query_type query_type);
 
4062
  table_map used_tables() const { return (table_map)0L; }
 
4063
  Field *get_tmp_table_field() { return 0; }
 
4064
  Item *copy_or_same(THD *thd) { return this; }
 
4065
  Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
 
4066
  void cleanup();
 
4067
 
 
4068
private:
 
4069
  void set_required_privilege(bool rw);
 
4070
  bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
 
4071
 
 
4072
public:
 
4073
  Settable_routine_parameter *get_settable_routine_parameter()
 
4074
  {
 
4075
    return (read_only ? 0 : this);
 
4076
  }
 
4077
 
 
4078
  bool set_value(THD *thd, Item **it)
 
4079
  {
 
4080
    return set_value(thd, NULL, it);
 
4081
  }
 
4082
 
 
4083
private:
 
4084
  /*
 
4085
    'want_privilege' holds privileges required to perform operation on
 
4086
    this trigger field (SELECT_ACL if we are going to read it and
 
4087
    UPDATE_ACL if we are going to update it).  It is initialized at
 
4088
    parse time but can be updated later if this trigger field is used
 
4089
    as OUT or INOUT parameter of stored routine (in this case
 
4090
    set_required_privilege() is called to appropriately update
 
4091
    want_privilege and cleanup() is responsible for restoring of
 
4092
    original want_privilege once parameter's value is updated).
 
4093
  */
 
4094
  ulong original_privilege;
 
4095
  ulong want_privilege;
 
4096
  GRANT_INFO *table_grants;
 
4097
  /*
 
4098
    Trigger field is read-only unless it belongs to the NEW row in a
 
4099
    BEFORE INSERT of BEFORE UPDATE trigger.
 
4100
  */
 
4101
  bool read_only;
 
4102
};
 
4103
 
 
4104
 
 
4105
class Item_cache: public Item_basic_constant
 
4106
{
 
4107
protected:
 
4108
  Item *example;
 
4109
  table_map used_table_map;
 
4110
  /**
 
4111
    Field that this object will get value from. This is used by 
 
4112
    index-based subquery engines to detect and remove the equality injected 
 
4113
    by IN->EXISTS transformation.
 
4114
  */  
 
4115
  Field *cached_field;
 
4116
  enum enum_field_types cached_field_type;
 
4117
  /*
 
4118
    TRUE <=> cache holds value of the last stored item (i.e actual value).
 
4119
    store() stores item to be cached and sets this flag to FALSE.
 
4120
    On the first call of val_xxx function if this flag is set to FALSE the 
 
4121
    cache_value() will be called to actually cache value of saved item.
 
4122
    cache_value() will set this flag to TRUE.
 
4123
  */
 
4124
  bool value_cached;
 
4125
public:
 
4126
  Item_cache():
 
4127
    example(0), used_table_map(0), cached_field(0),
 
4128
    cached_field_type(MYSQL_TYPE_STRING),
 
4129
    value_cached(0)
 
4130
  {
 
4131
    fixed= 1; 
 
4132
    null_value= 1;
 
4133
  }
 
4134
  Item_cache(enum_field_types field_type_arg):
 
4135
    example(0), used_table_map(0), cached_field(0),
 
4136
    cached_field_type(field_type_arg),
 
4137
    value_cached(0)
 
4138
  {
 
4139
    fixed= 1;
 
4140
    null_value= 1;
 
4141
  }
 
4142
 
 
4143
  void set_used_tables(table_map map) { used_table_map= map; }
 
4144
 
 
4145
  virtual table_map resolved_used_tables() const
 
4146
  {
 
4147
    return example ? example->resolved_used_tables() : used_table_map;
 
4148
  }
 
4149
 
 
4150
  virtual bool allocate(uint i) { return 0; }
 
4151
  virtual bool setup(Item *item)
 
4152
  {
 
4153
    example= item;
 
4154
    max_length= item->max_length;
 
4155
    decimals= item->decimals;
 
4156
    collation.set(item->collation);
 
4157
    unsigned_flag= item->unsigned_flag;
 
4158
    if (item->type() == FIELD_ITEM)
 
4159
      cached_field= ((Item_field *)item)->field;
 
4160
    return 0;
 
4161
  };
 
4162
  enum Type type() const { return CACHE_ITEM; }
 
4163
  enum_field_types field_type() const { return cached_field_type; }
 
4164
  static Item_cache* get_cache(const Item *item);
 
4165
  static Item_cache* get_cache(const Item* item, const Item_result type);
 
4166
  table_map used_tables() const { return used_table_map; }
 
4167
  virtual void keep_array() {}
 
4168
  virtual void print(String *str, enum_query_type query_type);
 
4169
  bool eq_def(Field *field) 
 
4170
  { 
 
4171
    return cached_field ? cached_field->eq_def (field) : FALSE;
 
4172
  }
 
4173
  bool eq(const Item *item, bool binary_cmp) const
 
4174
  {
 
4175
    return this == item;
 
4176
  }
 
4177
  /**
 
4178
     Check if saved item has a non-NULL value.
 
4179
     Will cache value of saved item if not already done. 
 
4180
     @return TRUE if cached value is non-NULL.
 
4181
   */
 
4182
  bool has_value()
 
4183
  {
 
4184
    return (value_cached || cache_value()) && !null_value;
 
4185
  }
 
4186
 
 
4187
  /** 
 
4188
    If this item caches a field value, return pointer to underlying field.
 
4189
 
 
4190
    @return Pointer to field, or NULL if this is not a cache for a field value.
 
4191
  */
 
4192
  Field* field() { return cached_field; }
 
4193
 
 
4194
  virtual void store(Item *item);
 
4195
  virtual bool cache_value()= 0;
 
4196
  bool basic_const_item() const
 
4197
  { return MY_TEST(example && example->basic_const_item());}
 
4198
  bool walk (Item_processor processor, bool walk_subquery, uchar *argument);
 
4199
  virtual void clear() { null_value= TRUE; value_cached= FALSE; }
 
4200
  bool is_null() { return value_cached ? null_value : example->is_null(); }
 
4201
  Item_result result_type() const
 
4202
  {
 
4203
    if (!example)
 
4204
      return INT_RESULT;
 
4205
    return Field::result_merge_type(example->field_type());
 
4206
  }
 
4207
};
 
4208
 
 
4209
 
 
4210
class Item_cache_int: public Item_cache
 
4211
{
 
4212
protected:
 
4213
  longlong value;
 
4214
public:
 
4215
  Item_cache_int(): Item_cache(),
 
4216
    value(0) {}
 
4217
  Item_cache_int(enum_field_types field_type_arg):
 
4218
    Item_cache(field_type_arg), value(0) {}
 
4219
 
 
4220
  virtual void store(Item *item){ Item_cache::store(item); }
 
4221
  void store(Item *item, longlong val_arg);
 
4222
  double val_real();
 
4223
  longlong val_int();
 
4224
  longlong val_time_temporal() { return val_int(); }
 
4225
  longlong val_date_temporal() { return val_int(); }
 
4226
  String* val_str(String *str);
 
4227
  my_decimal *val_decimal(my_decimal *);
 
4228
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4229
  {
 
4230
    return get_date_from_int(ltime, fuzzydate);
 
4231
  }
 
4232
  bool get_time(MYSQL_TIME *ltime)
 
4233
  {
 
4234
    return get_time_from_int(ltime);
 
4235
  }
 
4236
  enum Item_result result_type() const { return INT_RESULT; }
 
4237
  bool cache_value();
 
4238
};
 
4239
 
 
4240
 
 
4241
class Item_cache_real: public Item_cache
 
4242
{
 
4243
  double value;
 
4244
public:
 
4245
  Item_cache_real(): Item_cache(),
 
4246
    value(0) {}
 
4247
 
 
4248
  double val_real();
 
4249
  longlong val_int();
 
4250
  String* val_str(String *str);
 
4251
  my_decimal *val_decimal(my_decimal *);
 
4252
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4253
  {
 
4254
    return get_date_from_real(ltime, fuzzydate);
 
4255
  }
 
4256
  bool get_time(MYSQL_TIME *ltime)
 
4257
  {
 
4258
    return get_time_from_real(ltime);
 
4259
  }
 
4260
  enum Item_result result_type() const { return REAL_RESULT; }
 
4261
  bool cache_value();
 
4262
};
 
4263
 
 
4264
 
 
4265
class Item_cache_decimal: public Item_cache
 
4266
{
 
4267
protected:
 
4268
  my_decimal decimal_value;
 
4269
public:
 
4270
  Item_cache_decimal(): Item_cache() {}
 
4271
 
 
4272
  double val_real();
 
4273
  longlong val_int();
 
4274
  String* val_str(String *str);
 
4275
  my_decimal *val_decimal(my_decimal *);
 
4276
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4277
  {
 
4278
    return get_date_from_decimal(ltime, fuzzydate);
 
4279
  }
 
4280
  bool get_time(MYSQL_TIME *ltime)
 
4281
  {
 
4282
    return get_time_from_decimal(ltime);
 
4283
  }
 
4284
  enum Item_result result_type() const { return DECIMAL_RESULT; }
 
4285
  bool cache_value();
 
4286
};
 
4287
 
 
4288
 
 
4289
class Item_cache_str: public Item_cache
 
4290
{
 
4291
  char buffer[STRING_BUFFER_USUAL_SIZE];
 
4292
  String *value, value_buff;
 
4293
  bool is_varbinary;
 
4294
  
 
4295
public:
 
4296
  Item_cache_str(const Item *item) :
 
4297
    Item_cache(item->field_type()), value(0),
 
4298
    is_varbinary(item->type() == FIELD_ITEM &&
 
4299
                 cached_field_type == MYSQL_TYPE_VARCHAR &&
 
4300
                 !((const Item_field *) item)->field->has_charset())
 
4301
  {
 
4302
    collation.set(const_cast<DTCollation&>(item->collation));
 
4303
  }
 
4304
  double val_real();
 
4305
  longlong val_int();
 
4306
  String* val_str(String *);
 
4307
  my_decimal *val_decimal(my_decimal *);
 
4308
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4309
  {
 
4310
    return get_date_from_string(ltime, fuzzydate);
 
4311
  }
 
4312
  bool get_time(MYSQL_TIME *ltime)
 
4313
  {
 
4314
    return get_time_from_string(ltime);
 
4315
  }
 
4316
  enum Item_result result_type() const { return STRING_RESULT; }
 
4317
  const CHARSET_INFO *charset() const { return value->charset(); };
 
4318
  type_conversion_status save_in_field(Field *field, bool no_conversions);
 
4319
  bool cache_value();
 
4320
};
 
4321
 
 
4322
class Item_cache_row: public Item_cache
 
4323
{
 
4324
  Item_cache  **values;
 
4325
  uint item_count;
 
4326
  bool save_array;
 
4327
public:
 
4328
  Item_cache_row()
 
4329
    :Item_cache(), values(0), item_count(2),
 
4330
    save_array(0) {}
 
4331
  
 
4332
  /*
 
4333
    'allocate' used only in row transformer, to preallocate space for row 
 
4334
    cache.
 
4335
  */
 
4336
  bool allocate(uint num);
 
4337
  /*
 
4338
    'setup' is needed only by row => it not called by simple row subselect
 
4339
    (only by IN subselect (in subselect optimizer))
 
4340
  */
 
4341
  bool setup(Item *item);
 
4342
  void store(Item *item);
 
4343
  void illegal_method_call(const char *);
 
4344
  void make_field(Send_field *)
 
4345
  {
 
4346
    illegal_method_call((const char*)"make_field");
 
4347
  };
 
4348
  double val_real()
 
4349
  {
 
4350
    illegal_method_call((const char*)"val");
 
4351
    return 0;
 
4352
  };
 
4353
  longlong val_int()
 
4354
  {
 
4355
    illegal_method_call((const char*)"val_int");
 
4356
    return 0;
 
4357
  };
 
4358
  String *val_str(String *)
 
4359
  {
 
4360
    illegal_method_call((const char*)"val_str");
 
4361
    return 0;
 
4362
  };
 
4363
  my_decimal *val_decimal(my_decimal *val)
 
4364
  {
 
4365
    illegal_method_call((const char*)"val_decimal");
 
4366
    return 0;
 
4367
  };
 
4368
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4369
  {
 
4370
    illegal_method_call((const char *) "get_date");
 
4371
    return true;
 
4372
  }
 
4373
  bool get_time(MYSQL_TIME *ltime)
 
4374
  {
 
4375
    illegal_method_call((const char *) "get_time");
 
4376
    return true;
 
4377
  }
 
4378
 
 
4379
  enum Item_result result_type() const { return ROW_RESULT; }
 
4380
  
 
4381
  uint cols() { return item_count; }
 
4382
  Item *element_index(uint i) { return values[i]; }
 
4383
  Item **addr(uint i) { return (Item **) (values + i); }
 
4384
  bool check_cols(uint c);
 
4385
  bool null_inside();
 
4386
  void bring_value();
 
4387
  void keep_array() { save_array= 1; }
 
4388
  void cleanup()
 
4389
  {
 
4390
    DBUG_ENTER("Item_cache_row::cleanup");
 
4391
    Item_cache::cleanup();
 
4392
    if (save_array)
 
4393
      memset(values, 0, item_count*sizeof(Item**));
 
4394
    else
 
4395
      values= 0;
 
4396
    DBUG_VOID_RETURN;
 
4397
  }
 
4398
  bool cache_value();
 
4399
};
 
4400
 
 
4401
 
 
4402
class Item_cache_datetime: public Item_cache
 
4403
{
 
4404
protected:
 
4405
  String str_value;
 
4406
  longlong int_value;
 
4407
  bool str_value_cached;
 
4408
public:
 
4409
  Item_cache_datetime(enum_field_types field_type_arg):
 
4410
    Item_cache(field_type_arg), int_value(0), str_value_cached(0)
 
4411
  {
 
4412
    cmp_context= STRING_RESULT;
 
4413
  }
 
4414
 
 
4415
  void store(Item *item, longlong val_arg);
 
4416
  void store(Item *item);
 
4417
  double val_real();
 
4418
  longlong val_int();
 
4419
  longlong val_time_temporal();
 
4420
  longlong val_date_temporal();
 
4421
  String* val_str(String *str);
 
4422
  my_decimal *val_decimal(my_decimal *);
 
4423
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
 
4424
  bool get_time(MYSQL_TIME *ltime);
 
4425
  enum Item_result result_type() const { return STRING_RESULT; }
 
4426
  /*
 
4427
    In order to avoid INT <-> STRING conversion of a DATETIME value
 
4428
    two cache_value functions are introduced. One (cache_value) caches STRING
 
4429
    value, another (cache_value_int) - INT value. Thus this cache item
 
4430
    completely relies on the ability of the underlying item to do the
 
4431
    correct conversion.
 
4432
  */
 
4433
  bool cache_value_int();
 
4434
  bool cache_value();
 
4435
  void clear() { Item_cache::clear(); str_value_cached= FALSE; }
 
4436
};
 
4437
 
 
4438
 
 
4439
/*
 
4440
  Item_type_holder used to store type. name, length of Item for UNIONS &
 
4441
  derived tables.
 
4442
 
 
4443
  Item_type_holder do not need cleanup() because its time of live limited by
 
4444
  single SP/PS execution.
 
4445
*/
 
4446
class Item_type_holder: public Item
 
4447
{
 
4448
protected:
 
4449
  TYPELIB *enum_set_typelib;
 
4450
  enum_field_types fld_type;
 
4451
  Field::geometry_type geometry_type;
 
4452
 
 
4453
  void get_full_info(Item *item);
 
4454
 
 
4455
  /* It is used to count decimal precision in join_types */
 
4456
  int prev_decimal_int_part;
 
4457
public:
 
4458
  Item_type_holder(THD*, Item*);
 
4459
 
 
4460
  Item_result result_type() const;
 
4461
  enum_field_types field_type() const { return fld_type; };
 
4462
  enum Type type() const { return TYPE_HOLDER; }
 
4463
  double val_real();
 
4464
  longlong val_int();
 
4465
  my_decimal *val_decimal(my_decimal *);
 
4466
  String *val_str(String*);
 
4467
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4468
  {
 
4469
    DBUG_ASSERT(0);
 
4470
    return true;
 
4471
  }
 
4472
  bool get_time(MYSQL_TIME *ltime)
 
4473
  {
 
4474
    DBUG_ASSERT(0);
 
4475
    return true;
 
4476
  }
 
4477
  bool join_types(THD *thd, Item *);
 
4478
  Field *make_field_by_type(TABLE *table);
 
4479
  static uint32 display_length(Item *item);
 
4480
  static enum_field_types get_real_type(Item *);
 
4481
  Field::geometry_type get_geometry_type() const { return geometry_type; };
 
4482
};
 
4483
 
 
4484
 
 
4485
class st_select_lex;
 
4486
void mark_select_range_as_dependent(THD *thd,
 
4487
                                    st_select_lex *last_select,
 
4488
                                    st_select_lex *current_sel,
 
4489
                                    Field *found_field, Item *found_item,
 
4490
                                    Item_ident *resolved_item);
 
4491
 
 
4492
extern Cached_item *new_Cached_item(THD *thd, Item *item,
 
4493
                                    bool use_result_field);
 
4494
extern Item_result item_cmp_type(Item_result a,Item_result b);
 
4495
extern void resolve_const_item(THD *thd, Item **ref, Item *cmp_item);
 
4496
extern int stored_field_cmp_to_item(THD *thd, Field *field, Item *item);
 
4497
 
 
4498
extern const String my_null_string;
 
4499
 
 
4500
#endif /* ITEM_INCLUDED */