~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
class Protocol;
17
struct TABLE_LIST;
18
void item_init(void);			/* Init item functions */
19
class Item_field;
20
21
/*
22
   "Declared Type Collation"
23
   A combination of collation and its derivation.
24
25
  Flags for collation aggregation modes:
26
  MY_COLL_ALLOW_SUPERSET_CONV  - allow conversion to a superset
27
  MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
28
                                 (i.e. constant).
29
  MY_COLL_ALLOW_CONV           - allow any kind of conversion
30
                                 (combination of the above two)
31
  MY_COLL_DISALLOW_NONE        - don't allow return DERIVATION_NONE
32
                                 (e.g. when aggregating for comparison)
33
  MY_COLL_CMP_CONV             - combination of MY_COLL_ALLOW_CONV
34
                                 and MY_COLL_DISALLOW_NONE
35
*/
36
37
#define MY_COLL_ALLOW_SUPERSET_CONV   1
38
#define MY_COLL_ALLOW_COERCIBLE_CONV  2
39
#define MY_COLL_ALLOW_CONV            3
40
#define MY_COLL_DISALLOW_NONE         4
41
#define MY_COLL_CMP_CONV              7
42
43
class DTCollation {
44
public:
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
45
  const CHARSET_INFO *collation;
1 by brian
clean slate
46
  enum Derivation derivation;
47
  uint repertoire;
48
  
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
49
  void set_repertoire_from_charset(const CHARSET_INFO * const cs)
1 by brian
clean slate
50
  {
51
    repertoire= cs->state & MY_CS_PUREASCII ?
52
                MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
53
  }
54
  DTCollation()
55
  {
56
    collation= &my_charset_bin;
57
    derivation= DERIVATION_NONE;
58
    repertoire= MY_REPERTOIRE_UNICODE30;
59
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
60
  DTCollation(const CHARSET_INFO * const collation_arg, Derivation derivation_arg)
1 by brian
clean slate
61
  {
62
    collation= collation_arg;
63
    derivation= derivation_arg;
64
    set_repertoire_from_charset(collation_arg);
65
  }
66
  void set(DTCollation &dt)
67
  { 
68
    collation= dt.collation;
69
    derivation= dt.derivation;
70
    repertoire= dt.repertoire;
71
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
72
  void set(const CHARSET_INFO * const collation_arg, Derivation derivation_arg)
1 by brian
clean slate
73
  {
74
    collation= collation_arg;
75
    derivation= derivation_arg;
76
    set_repertoire_from_charset(collation_arg);
77
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
78
  void set(const CHARSET_INFO * const collation_arg,
1 by brian
clean slate
79
           Derivation derivation_arg,
80
           uint repertoire_arg)
81
  {
82
    collation= collation_arg;
83
    derivation= derivation_arg;
84
    repertoire= repertoire_arg;
85
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
86
  void set(const CHARSET_INFO * const collation_arg)
1 by brian
clean slate
87
  {
88
    collation= collation_arg;
89
    set_repertoire_from_charset(collation_arg);
90
  }
91
  void set(Derivation derivation_arg)
92
  { derivation= derivation_arg; }
93
  bool aggregate(DTCollation &dt, uint flags= 0);
94
  bool set(DTCollation &dt1, DTCollation &dt2, uint flags= 0)
95
  { set(dt1); return aggregate(dt2, flags); }
96
  const char *derivation_name() const
97
  {
98
    switch(derivation)
99
    {
100
      case DERIVATION_IGNORABLE: return "IGNORABLE";
101
      case DERIVATION_COERCIBLE: return "COERCIBLE";
102
      case DERIVATION_IMPLICIT:  return "IMPLICIT";
103
      case DERIVATION_SYSCONST:  return "SYSCONST";
104
      case DERIVATION_EXPLICIT:  return "EXPLICIT";
105
      case DERIVATION_NONE:      return "NONE";
106
      default: return "UNKNOWN";
107
    }
108
  }
109
};
110
111
/*************************************************************************/
112
/*
113
  A framework to easily handle different return types for hybrid items
114
  (hybrid item is an item whose operand can be of any type, e.g. integer,
115
  real, decimal).
116
*/
117
118
struct Hybrid_type_traits;
119
120
struct Hybrid_type
121
{
152 by Brian Aker
longlong replacement
122
  int64_t integer;
1 by brian
clean slate
123
124
  double real;
125
  /*
126
    Use two decimal buffers interchangeably to speed up += operation
127
    which has no native support in decimal library.
128
    Hybrid_type+= arg is implemented as dec_buf[1]= dec_buf[0] + arg.
129
    The third decimal is used as a handy temporary storage.
130
  */
131
  my_decimal dec_buf[3];
132
  int used_dec_buf_no;
133
134
  /*
135
    Traits moved to a separate class to
136
      a) be able to easily change object traits in runtime
137
      b) they work as a differentiator for the union above
138
  */
139
  const Hybrid_type_traits *traits;
140
141
  Hybrid_type() {}
142
  /* XXX: add traits->copy() when needed */
143
  Hybrid_type(const Hybrid_type &rhs) :traits(rhs.traits) {}
144
};
145
146
147
/* Hybryd_type_traits interface + default implementation for REAL_RESULT */
148
149
struct Hybrid_type_traits
150
{
151
  virtual Item_result type() const { return REAL_RESULT; }
152
153
  virtual void
154
  fix_length_and_dec(Item *item, Item *arg) const;
155
156
  /* Hybrid_type operations. */
157
  virtual void set_zero(Hybrid_type *val) const { val->real= 0.0; }
158
  virtual void add(Hybrid_type *val, Field *f) const
159
  { val->real+= f->val_real(); }
151 by Brian Aker
Ulonglong to uint64_t
160
  virtual void div(Hybrid_type *val, uint64_t u) const
161
  { val->real/= uint64_t2double(u); }
1 by brian
clean slate
162
152 by Brian Aker
longlong replacement
163
  virtual int64_t val_int(Hybrid_type *val,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
164
                           bool unsigned_flag __attribute__((unused))) const
152 by Brian Aker
longlong replacement
165
  { return (int64_t) rint(val->real); }
1 by brian
clean slate
166
  virtual double val_real(Hybrid_type *val) const { return val->real; }
167
  virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const;
206 by Brian Aker
Removed final uint dead types.
168
  virtual String *val_str(Hybrid_type *val, String *buf, uint8_t decimals) const;
1 by brian
clean slate
169
  static const Hybrid_type_traits *instance();
170
  Hybrid_type_traits() {}
171
  virtual ~Hybrid_type_traits() {}
172
};
173
174
175
struct Hybrid_type_traits_decimal: public Hybrid_type_traits
176
{
177
  virtual Item_result type() const { return DECIMAL_RESULT; }
178
179
  virtual void
180
  fix_length_and_dec(Item *arg, Item *item) const;
181
182
  /* Hybrid_type operations. */
183
  virtual void set_zero(Hybrid_type *val) const;
184
  virtual void add(Hybrid_type *val, Field *f) const;
151 by Brian Aker
Ulonglong to uint64_t
185
  virtual void div(Hybrid_type *val, uint64_t u) const;
1 by brian
clean slate
186
152 by Brian Aker
longlong replacement
187
  virtual int64_t val_int(Hybrid_type *val, bool unsigned_flag) const;
1 by brian
clean slate
188
  virtual double val_real(Hybrid_type *val) const;
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
189
  virtual my_decimal *val_decimal(Hybrid_type *val,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
190
                                  my_decimal *buf __attribute__((unused))) const
1 by brian
clean slate
191
  { return &val->dec_buf[val->used_dec_buf_no]; }
206 by Brian Aker
Removed final uint dead types.
192
  virtual String *val_str(Hybrid_type *val, String *buf, uint8_t decimals) const;
1 by brian
clean slate
193
  static const Hybrid_type_traits_decimal *instance();
194
  Hybrid_type_traits_decimal() {};
195
};
196
197
198
struct Hybrid_type_traits_integer: public Hybrid_type_traits
199
{
200
  virtual Item_result type() const { return INT_RESULT; }
201
202
  virtual void
203
  fix_length_and_dec(Item *arg, Item *item) const;
204
205
  /* Hybrid_type operations. */
206
  virtual void set_zero(Hybrid_type *val) const
207
  { val->integer= 0; }
208
  virtual void add(Hybrid_type *val, Field *f) const
209
  { val->integer+= f->val_int(); }
151 by Brian Aker
Ulonglong to uint64_t
210
  virtual void div(Hybrid_type *val, uint64_t u) const
152 by Brian Aker
longlong replacement
211
  { val->integer/= (int64_t) u; }
1 by brian
clean slate
212
152 by Brian Aker
longlong replacement
213
  virtual int64_t val_int(Hybrid_type *val,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
214
                           bool unsigned_flag __attribute__((unused))) const
1 by brian
clean slate
215
  { return val->integer; }
216
  virtual double val_real(Hybrid_type *val) const
217
  { return (double) val->integer; }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
218
  virtual my_decimal *val_decimal(Hybrid_type *val,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
219
                                  my_decimal *buf __attribute__((unused))) const
1 by brian
clean slate
220
  {
221
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, &val->dec_buf[2]);
222
    return &val->dec_buf[2];
223
  }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
224
  virtual String *val_str(Hybrid_type *val, String *buf,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
225
                          uint8_t decimals __attribute__((unused))) const
1 by brian
clean slate
226
  { buf->set(val->integer, &my_charset_bin); return buf;}
227
  static const Hybrid_type_traits_integer *instance();
228
  Hybrid_type_traits_integer() {};
229
};
230
231
232
void dummy_error_processor(THD *thd, void *data);
233
234
void view_error_processor(THD *thd, void *data);
235
236
/*
237
  Instances of Name_resolution_context store the information necesary for
238
  name resolution of Items and other context analysis of a query made in
239
  fix_fields().
240
241
  This structure is a part of SELECT_LEX, a pointer to this structure is
242
  assigned when an item is created (which happens mostly during  parsing
243
  (sql_yacc.yy)), but the structure itself will be initialized after parsing
244
  is complete
245
246
  TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
247
  separate SELECT_LEX which allow to remove tricks of changing this
248
  structure before and after INSERT/CREATE and its SELECT to make correct
249
  field name resolution.
250
*/
251
struct Name_resolution_context: Sql_alloc
252
{
253
  /*
254
    The name resolution context to search in when an Item cannot be
255
    resolved in this context (the context of an outer select)
256
  */
257
  Name_resolution_context *outer_context;
258
259
  /*
260
    List of tables used to resolve the items of this context.  Usually these
261
    are tables from the FROM clause of SELECT statement.  The exceptions are
262
    INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
263
    subquery is not moved to a separate SELECT_LEX.  For these types of
264
    statements we have to change this member dynamically to ensure correct
265
    name resolution of different parts of the statement.
266
  */
267
  TABLE_LIST *table_list;
268
  /*
269
    In most cases the two table references below replace 'table_list' above
270
    for the purpose of name resolution. The first and last name resolution
271
    table references allow us to search only in a sub-tree of the nested
272
    join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
273
    and JOIN ... ON. 
274
  */
275
  TABLE_LIST *first_name_resolution_table;
276
  /*
277
    Last table to search in the list of leaf table references that begins
278
    with first_name_resolution_table.
279
  */
280
  TABLE_LIST *last_name_resolution_table;
281
282
  /*
283
    SELECT_LEX item belong to, in case of merged VIEW it can differ from
284
    SELECT_LEX where item was created, so we can't use table_list/field_list
285
    from there
286
  */
287
  st_select_lex *select_lex;
288
289
  /*
290
    Processor of errors caused during Item name resolving, now used only to
291
    hide underlying tables in errors about views (i.e. it substitute some
292
    errors for views)
293
  */
294
  void (*error_processor)(THD *, void *);
295
  void *error_processor_data;
296
297
  /*
163 by Brian Aker
Merge Monty's code.
298
    When true items are resolved in this context both against the
299
    SELECT list and this->table_list. If false, items are resolved
1 by brian
clean slate
300
    only against this->table_list.
301
  */
302
  bool resolve_in_select_list;
303
304
  /*
305
    Security context of this name resolution context. It's used for views
306
    and is non-zero only if the view is defined with SQL SECURITY DEFINER.
307
  */
308
  Security_context *security_ctx;
309
310
  Name_resolution_context()
311
    :outer_context(0), table_list(0), select_lex(0),
312
    error_processor_data(0),
313
    security_ctx(0)
314
    {}
315
316
  void init()
317
  {
163 by Brian Aker
Merge Monty's code.
318
    resolve_in_select_list= false;
1 by brian
clean slate
319
    error_processor= &dummy_error_processor;
320
    first_name_resolution_table= NULL;
321
    last_name_resolution_table= NULL;
322
  }
323
324
  void resolve_in_table_list_only(TABLE_LIST *tables)
325
  {
326
    table_list= first_name_resolution_table= tables;
163 by Brian Aker
Merge Monty's code.
327
    resolve_in_select_list= false;
1 by brian
clean slate
328
  }
329
330
  void process_error(THD *thd)
331
  {
332
    (*error_processor)(thd, error_processor_data);
333
  }
334
};
335
336
337
/*
338
  Store and restore the current state of a name resolution context.
339
*/
340
341
class Name_resolution_context_state
342
{
343
private:
344
  TABLE_LIST *save_table_list;
345
  TABLE_LIST *save_first_name_resolution_table;
346
  TABLE_LIST *save_next_name_resolution_table;
347
  bool        save_resolve_in_select_list;
348
  TABLE_LIST *save_next_local;
349
350
public:
351
  Name_resolution_context_state() {}          /* Remove gcc warning */
352
353
public:
354
  /* Save the state of a name resolution context. */
355
  void save_state(Name_resolution_context *context, TABLE_LIST *table_list)
356
  {
357
    save_table_list=                  context->table_list;
358
    save_first_name_resolution_table= context->first_name_resolution_table;
359
    save_resolve_in_select_list=      context->resolve_in_select_list;
360
    save_next_local=                  table_list->next_local;
361
    save_next_name_resolution_table=  table_list->next_name_resolution_table;
362
  }
363
364
  /* Restore a name resolution context from saved state. */
365
  void restore_state(Name_resolution_context *context, TABLE_LIST *table_list)
366
  {
367
    table_list->next_local=                save_next_local;
368
    table_list->next_name_resolution_table= save_next_name_resolution_table;
369
    context->table_list=                   save_table_list;
370
    context->first_name_resolution_table=  save_first_name_resolution_table;
371
    context->resolve_in_select_list=       save_resolve_in_select_list;
372
  }
373
374
  TABLE_LIST *get_first_name_resolution_table()
375
  {
376
    return save_first_name_resolution_table;
377
  }
378
};
379
380
381
/*
382
  This enum is used to report information about monotonicity of function
383
  represented by Item* tree.
384
  Monotonicity is defined only for Item* trees that represent table
385
  partitioning expressions (i.e. have no subselects/user vars/PS parameters
386
  etc etc). An Item* tree is assumed to have the same monotonicity properties
387
  as its correspoinding function F:
388
152 by Brian Aker
longlong replacement
389
  [signed] int64_t F(field1, field2, ...) {
1 by brian
clean slate
390
    put values of field_i into table record buffer;
391
    return item->val_int(); 
392
  }
393
394
  NOTE
395
  At the moment function monotonicity is not well defined (and so may be
396
  incorrect) for Item trees with parameters/return types that are different
397
  from INT_RESULT, may be NULL, or are unsigned.
398
  It will be possible to address this issue once the related partitioning bugs
399
  (BUG#16002, BUG#15447, BUG#13436) are fixed.
400
*/
401
402
typedef enum monotonicity_info 
403
{
404
   NON_MONOTONIC,              /* none of the below holds */
405
   MONOTONIC_INCREASING,       /* F() is unary and (x < y) => (F(x) <= F(y)) */
406
   MONOTONIC_STRICT_INCREASING /* F() is unary and (x < y) => (F(x) <  F(y)) */
407
} enum_monotonicity_info;
408
409
/*************************************************************************/
410
typedef bool (Item::*Item_processor) (uchar *arg);
411
/*
412
  Analyzer function
413
    SYNOPSIS
414
      argp   in/out IN:  Analysis parameter
415
                    OUT: Parameter to be passed to the transformer
416
417
    RETURN 
163 by Brian Aker
Merge Monty's code.
418
      true   Invoke the transformer
419
      false  Don't do it
1 by brian
clean slate
420
421
*/
422
typedef bool (Item::*Item_analyzer) (uchar **argp);
423
typedef Item* (Item::*Item_transformer) (uchar *arg);
424
typedef void (*Cond_traverser) (const Item *item, void *arg);
425
426
427
class Item
428
{
429
  Item(const Item &);			/* Prevent use of these */
430
  void operator=(Item &);
431
  /* Cache of the result of is_expensive(). */
206 by Brian Aker
Removed final uint dead types.
432
  int8_t is_expensive_cache;
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
433
  virtual bool is_expensive_processor(uchar *arg __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
434
  { return 0; }
1 by brian
clean slate
435
436
public:
437
  static void *operator new(size_t size)
438
  { return sql_alloc(size); }
439
  static void *operator new(size_t size, MEM_ROOT *mem_root)
440
  { return alloc_root(mem_root, size); }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
441
  static void operator delete(void *ptr __attribute__((unused)),
442
                              size_t size __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
443
  { TRASH(ptr, size); }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
444
  static void operator delete(void *ptr __attribute__((unused)),
445
                              MEM_ROOT *mem_root __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
446
  {}
1 by brian
clean slate
447
448
  enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM,
449
	     INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM,
450
	     COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
451
	     PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM,
452
	     FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM,
453
             SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER,
454
             PARAM_ITEM, TRIGGER_FIELD_ITEM, DECIMAL_ITEM,
455
             XPATH_NODESET, XPATH_NODESET_CMP,
456
             VIEW_FIXER_ITEM};
457
458
  enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
459
460
  enum traverse_order { POSTFIX, PREFIX };
461
  
462
  /* Reuse size, only used by SP local variable assignment, otherwize 0 */
463
  uint rsize;
464
465
  /*
466
    str_values's main purpose is to be used to cache the value in
467
    save_in_field
468
  */
469
  String str_value;
470
  char * name;			/* Name from select */
471
  /* Original item name (if it was renamed)*/
472
  char * orig_name;
473
  Item *next;
203 by Brian Aker
Small cleanup around uint32 types (need to merge).
474
  uint32_t max_length;
1 by brian
clean slate
475
  uint name_length;                     /* Length of name */
206 by Brian Aker
Removed final uint dead types.
476
  int8_t marker;
477
  uint8_t decimals;
1 by brian
clean slate
478
  my_bool maybe_null;			/* If item may be null */
479
  my_bool null_value;			/* if item is null */
480
  my_bool unsigned_flag;
481
  my_bool with_sum_func;
482
  my_bool fixed;                        /* If item fixed with fix_fields */
483
  my_bool is_autogenerated_name;        /* indicate was name of this Item
484
                                           autogenerated or set by user */
485
  DTCollation collation;
486
  my_bool with_subselect;               /* If this item is a subselect or some
487
                                           of its arguments is or contains a
488
                                           subselect. Computed by fix_fields. */
489
  Item_result cmp_context;              /* Comparison context */
490
  // alloc & destruct is done as start of select using sql_alloc
491
  Item();
492
  /*
493
     Constructor used by Item_field, Item_ref & aggregate (sum) functions.
494
     Used for duplicating lists in processing queries with temporary
495
     tables
496
     Also it used for Item_cond_and/Item_cond_or for creating
497
     top AND/OR structure of WHERE clause to protect it of
498
     optimisation changes in prepared statements
499
  */
500
  Item(THD *thd, Item *item);
501
  virtual ~Item()
502
  {
503
#ifdef EXTRA_DEBUG
504
    name=0;
505
#endif
506
  }		/*lint -e1509 */
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
507
  void set_name(const char *str, uint length, const CHARSET_INFO * const cs);
1 by brian
clean slate
508
  void rename(char *new_name);
509
  void init_make_field(Send_field *tmp_field,enum enum_field_types type);
510
  virtual void cleanup();
511
  virtual void make_field(Send_field *field);
512
  Field *make_string_field(TABLE *table);
513
  virtual bool fix_fields(THD *, Item **);
514
  /*
515
    Fix after some tables has been pulled out. Basically re-calculate all
516
    attributes that are dependent on the tables.
517
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
518
  virtual void fix_after_pullout(st_select_lex *new_parent __attribute__((unused)),
519
                                 Item **ref __attribute__((unused))) {};
1 by brian
clean slate
520
521
  /*
522
    should be used in case where we are sure that we do not need
523
    complete fix_fields() procedure.
524
  */
525
  inline void quick_fix_field() { fixed= 1; }
526
  /* Function returns 1 on overflow and -1 on fatal errors */
527
  int save_in_field_no_warnings(Field *field, bool no_conversions);
528
  virtual int save_in_field(Field *field, bool no_conversions);
529
  virtual void save_org_in_field(Field *field)
530
  { (void) save_in_field(field, 1); }
531
  virtual int save_safe_in_field(Field *field)
532
  { return save_in_field(field, 1); }
533
  virtual bool send(Protocol *protocol, String *str);
534
  virtual bool eq(const Item *, bool binary_cmp) const;
535
  virtual Item_result result_type() const { return REAL_RESULT; }
536
  virtual Item_result cast_to_int_type() const { return result_type(); }
537
  virtual enum_field_types string_field_type() const;
538
  virtual enum_field_types field_type() const;
539
  virtual enum Type type() const =0;
540
  
541
  /*
542
    Return information about function monotonicity. See comment for
543
    enum_monotonicity_info for details. This function can only be called
544
    after fix_fields() call.
545
  */
546
  virtual enum_monotonicity_info get_monotonicity_info() const
547
  { return NON_MONOTONIC; }
548
549
  /*
550
    Convert "func_arg $CMP$ const" half-interval into "FUNC(func_arg) $CMP2$ const2"
551
552
    SYNOPSIS
553
      val_int_endpoint()
163 by Brian Aker
Merge Monty's code.
554
        left_endp  false  <=> The interval is "x < const" or "x <= const"
555
                   true   <=> The interval is "x > const" or "x >= const"
1 by brian
clean slate
556
163 by Brian Aker
Merge Monty's code.
557
        incl_endp  IN   true <=> the comparison is '<' or '>'
558
                        false <=> the comparison is '<=' or '>='
1 by brian
clean slate
559
                   OUT  The same but for the "F(x) $CMP$ F(const)" comparison
560
561
    DESCRIPTION
562
      This function is defined only for unary monotonic functions. The caller
563
      supplies the source half-interval
564
565
         x $CMP$ const
566
567
      The value of const is supplied implicitly as the value this item's
568
      argument, the form of $CMP$ comparison is specified through the
569
      function's arguments. The calle returns the result interval
570
         
571
         F(x) $CMP2$ F(const)
572
      
573
      passing back F(const) as the return value, and the form of $CMP2$ 
574
      through the out parameter. NULL values are assumed to be comparable and
575
      be less than any non-NULL values.
576
577
    RETURN
578
      The output range bound, which equal to the value of val_int()
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
579
        - If the value of the function is NULL then the bound is the
163 by Brian Aker
Merge Monty's code.
580
          smallest possible value of INT64_MIN
1 by brian
clean slate
581
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
582
  virtual int64_t val_int_endpoint(bool left_endp __attribute__((unused)),
583
                                    bool *incl_endp __attribute__((unused)))
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
584
  { assert(0); return 0; }
1 by brian
clean slate
585
586
587
  /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
588
  /*
589
    Return double precision floating point representation of item.
590
591
    SYNOPSIS
592
      val_real()
593
594
    RETURN
163 by Brian Aker
Merge Monty's code.
595
      In case of NULL value return 0.0 and set null_value flag to true.
596
      If value is not null null_value flag will be reset to false.
1 by brian
clean slate
597
  */
598
  virtual double val_real()=0;
599
  /*
600
    Return integer representation of item.
601
602
    SYNOPSIS
603
      val_int()
604
605
    RETURN
163 by Brian Aker
Merge Monty's code.
606
      In case of NULL value return 0 and set null_value flag to true.
607
      If value is not null null_value flag will be reset to false.
1 by brian
clean slate
608
  */
152 by Brian Aker
longlong replacement
609
  virtual int64_t val_int()=0;
1 by brian
clean slate
610
  /*
611
    This is just a shortcut to avoid the cast. You should still use
612
    unsigned_flag to check the sign of the item.
613
  */
151 by Brian Aker
Ulonglong to uint64_t
614
  inline uint64_t val_uint() { return (uint64_t) val_int(); }
1 by brian
clean slate
615
  /*
616
    Return string representation of this item object.
617
618
    SYNOPSIS
619
      val_str()
620
      str   an allocated buffer this or any nested Item object can use to
621
            store return value of this method.
622
623
    NOTE
624
      Buffer passed via argument  should only be used if the item itself
625
      doesn't have an own String buffer. In case when the item maintains
626
      it's own string buffer, it's preferable to return it instead to
627
      minimize number of mallocs/memcpys.
628
      The caller of this method can modify returned string, but only in case
629
      when it was allocated on heap, (is_alloced() is true).  This allows
630
      the caller to efficiently use a buffer allocated by a child without
631
      having to allocate a buffer of it's own. The buffer, given to
632
      val_str() as argument, belongs to the caller and is later used by the
633
      caller at it's own choosing.
634
      A few implications from the above:
635
      - unless you return a string object which only points to your buffer
636
        but doesn't manages it you should be ready that it will be
637
        modified.
638
      - even for not allocated strings (is_alloced() == false) the caller
639
        can change charset (see Item_func_{typecast/binary}. XXX: is this
640
        a bug?
641
      - still you should try to minimize data copying and return internal
642
        object whenever possible.
643
644
    RETURN
645
      In case of NULL value return 0 (NULL pointer) and set null_value flag
163 by Brian Aker
Merge Monty's code.
646
      to true.
647
      If value is not null null_value flag will be reset to false.
1 by brian
clean slate
648
  */
649
  virtual String *val_str(String *str)=0;
650
  /*
651
    Return decimal representation of item with fixed point.
652
653
    SYNOPSIS
654
      val_decimal()
655
      decimal_buffer  buffer which can be used by Item for returning value
656
                      (but can be not)
657
658
    NOTE
659
      Returned value should not be changed if it is not the same which was
660
      passed via argument.
661
662
    RETURN
663
      Return pointer on my_decimal (it can be other then passed via argument)
163 by Brian Aker
Merge Monty's code.
664
        if value is not NULL (null_value flag will be reset to false).
1 by brian
clean slate
665
      In case of NULL value it return 0 pointer and set null_value flag
163 by Brian Aker
Merge Monty's code.
666
        to true.
1 by brian
clean slate
667
  */
668
  virtual my_decimal *val_decimal(my_decimal *decimal_buffer)= 0;
669
  /*
670
    Return boolean value of item.
671
672
    RETURN
163 by Brian Aker
Merge Monty's code.
673
      false value is false or NULL
674
      true value is true (not equal to 0)
1 by brian
clean slate
675
  */
676
  virtual bool val_bool();
677
  virtual String *val_nodeset(String*) { return 0; }
678
  /* Helper functions, see item_sum.cc */
679
  String *val_string_from_real(String *str);
680
  String *val_string_from_int(String *str);
681
  String *val_string_from_decimal(String *str);
682
  my_decimal *val_decimal_from_real(my_decimal *decimal_value);
683
  my_decimal *val_decimal_from_int(my_decimal *decimal_value);
684
  my_decimal *val_decimal_from_string(my_decimal *decimal_value);
685
  my_decimal *val_decimal_from_date(my_decimal *decimal_value);
686
  my_decimal *val_decimal_from_time(my_decimal *decimal_value);
152 by Brian Aker
longlong replacement
687
  int64_t val_int_from_decimal();
1 by brian
clean slate
688
  double val_real_from_decimal();
689
690
  int save_time_in_field(Field *field);
691
  int save_date_in_field(Field *field);
692
  int save_str_value_in_field(Field *field, String *result);
693
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
694
  virtual Field *get_tmp_table_field(void) { return 0; }
1 by brian
clean slate
695
  /* This is also used to create fields in CREATE ... SELECT: */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
696
  virtual Field *tmp_table_field(TABLE *t_arg __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
697
  { return 0; }
698
  virtual const char *full_name(void) const { return name ? name : "???"; }
1 by brian
clean slate
699
700
  /*
701
    *result* family of methods is analog of *val* family (see above) but
702
    return value of result_field of item if it is present. If Item have not
703
    result field, it return val(). This methods set null_value flag in same
704
    way as *val* methods do it.
705
  */
706
  virtual double  val_result() { return val_real(); }
152 by Brian Aker
longlong replacement
707
  virtual int64_t val_int_result() { return val_int(); }
1 by brian
clean slate
708
  virtual String *str_result(String* tmp) { return val_str(tmp); }
709
  virtual my_decimal *val_decimal_result(my_decimal *val)
710
  { return val_decimal(val); }
711
  virtual bool val_bool_result() { return val_bool(); }
712
713
  /* bit map of tables used by item */
714
  virtual table_map used_tables() const { return (table_map) 0L; }
715
  /*
716
    Return table map of tables that can't be NULL tables (tables that are
717
    used in a context where if they would contain a NULL row generated
718
    by a LEFT or RIGHT join, the item would not be true).
719
    This expression is used on WHERE item to determinate if a LEFT JOIN can be
720
    converted to a normal join.
721
    Generally this function should return used_tables() if the function
722
    would return null if any of the arguments are null
723
    As this is only used in the beginning of optimization, the value don't
724
    have to be updated in update_used_tables()
725
  */
726
  virtual table_map not_null_tables() const { return used_tables(); }
727
  /*
728
    Returns true if this is a simple constant item like an integer, not
729
    a constant expression. Used in the optimizer to propagate basic constants.
730
  */
731
  virtual bool basic_const_item() const { return 0; }
732
  /* cloning of constant items (0 if it is not const) */
733
  virtual Item *clone_item() { return 0; }
734
  virtual cond_result eq_cmp_result() const { return COND_OK; }
735
  inline uint float_length(uint decimals_par) const
736
  { return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;}
737
  virtual uint decimal_precision() const;
738
  inline int decimal_int_part() const
739
  { return my_decimal_int_part(decimal_precision(), decimals); }
740
  /* 
741
    Returns true if this is constant (during query execution, i.e. its value
742
    will not change until next fix_fields) and its value is known.
743
  */
744
  virtual bool const_item() const { return used_tables() == 0; }
745
  /* 
746
    Returns true if this is constant but its value may be not known yet.
747
    (Can be used for parameters of prep. stmts or of stored procedures.)
748
  */
749
  virtual bool const_during_execution() const 
750
  { return (used_tables() & ~PARAM_TABLE_BIT) == 0; }
751
752
  /**
753
    This method is used for to:
754
      - to generate a view definition query (SELECT-statement);
755
      - to generate a SQL-query for EXPLAIN EXTENDED;
756
      - to generate a SQL-query to be shown in INFORMATION_SCHEMA;
757
      - debug.
758
759
    For more information about view definition query, INFORMATION_SCHEMA
760
    query and why they should be generated from the Item-tree, @see
761
    mysql_register_view().
762
  */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
763
  virtual inline void print(String *str,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
764
                            enum_query_type query_type __attribute__((unused)))
1 by brian
clean slate
765
  {
766
    str->append(full_name());
767
  }
768
769
  void print_item_w_name(String *, enum_query_type query_type);
770
  virtual void update_used_tables() {}
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
771
  virtual void split_sum_func(THD *thd __attribute__((unused)),
772
                              Item **ref_pointer_array __attribute__((unused)),
773
                              List<Item> &fields __attribute__((unused))) {}
1 by brian
clean slate
774
  /* Called for items that really have to be split */
775
  void split_sum_func2(THD *thd, Item **ref_pointer_array, List<Item> &fields,
776
                       Item **ref, bool skip_registered);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
777
  virtual bool get_date(DRIZZLE_TIME *ltime,uint fuzzydate);
778
  virtual bool get_time(DRIZZLE_TIME *ltime);
779
  virtual bool get_date_result(DRIZZLE_TIME *ltime,uint fuzzydate)
1 by brian
clean slate
780
  { return get_date(ltime,fuzzydate); }
781
  /*
782
    The method allows to determine nullness of a complex expression 
783
    without fully evaluating it, instead of calling val/result*() then 
784
    checking null_value. Used in Item_func_isnull/Item_func_isnotnull
785
    and Item_sum_count/Item_sum_count_distinct.
786
    Any new item which can be NULL must implement this method.
787
  */
788
  virtual bool is_null() { return 0; }
789
790
  /*
791
   Make sure the null_value member has a correct value.
792
  */
793
  virtual void update_null_value () { (void) val_int(); }
794
795
  /*
796
    Inform the item that there will be no distinction between its result
163 by Brian Aker
Merge Monty's code.
797
    being false or NULL.
1 by brian
clean slate
798
799
    NOTE
800
      This function will be called for eg. Items that are top-level AND-parts
801
      of the WHERE clause. Items implementing this function (currently
802
      Item_cond_and and subquery-related item) enable special optimizations
803
      when they are "top level".
804
  */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
805
  virtual void top_level_item(void) {}
1 by brian
clean slate
806
  /*
807
    set field of temporary table for Item which can be switched on temporary
808
    table during query processing (grouping and so on)
809
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
810
  virtual void set_result_field(Field *field __attribute__((unused))) {}
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
811
  virtual bool is_result_field(void) { return 0; }
812
  virtual bool is_bool_func(void) { return 0; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
813
  virtual void save_in_result_field(bool no_conversions __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
814
  {}
1 by brian
clean slate
815
  /*
816
    set value of aggregate function in case of no rows for grouping were found
817
  */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
818
  virtual void no_rows_in_result(void) {}
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
819
  virtual Item *copy_or_same(THD *thd __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
820
  { return this; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
821
  virtual Item *copy_andor_structure(THD *thd  __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
822
  { return this; }
823
  virtual Item *real_item(void) { return this; }
1 by brian
clean slate
824
  virtual Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
825
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
826
  static const CHARSET_INFO *default_charset();
827
  virtual const CHARSET_INFO *compare_collation() { return NULL; }
1 by brian
clean slate
828
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
829
  virtual bool walk(Item_processor processor __attribute__((unused)),
830
                    bool walk_subquery __attribute__((unused)),
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
831
                    uchar *arg)
1 by brian
clean slate
832
  {
833
    return (this->*processor)(arg);
834
  }
835
836
  virtual Item* transform(Item_transformer transformer, uchar *arg);
837
838
  /*
839
    This function performs a generic "compilation" of the Item tree.
840
    The process of compilation is assumed to go as follows: 
841
    
842
    compile()
843
    { 
844
      if (this->*some_analyzer(...))
845
      {
846
        compile children if any;
847
        this->*some_transformer(...);
848
      }
849
    }
850
851
    i.e. analysis is performed top-down while transformation is done
852
    bottom-up.      
853
  */
854
  virtual Item* compile(Item_analyzer analyzer, uchar **arg_p,
855
                        Item_transformer transformer, uchar *arg_t)
856
  {
857
    if ((this->*analyzer) (arg_p))
858
      return ((this->*transformer) (arg_t));
859
    return 0;
860
  }
861
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
862
   virtual void traverse_cond(Cond_traverser traverser __attribute__((unused)),
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
863
                              void *arg,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
864
                              traverse_order order __attribute__((unused)))
1 by brian
clean slate
865
   {
866
     (*traverser)(this, arg);
867
   }
868
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
869
  virtual bool remove_dependence_processor(uchar * arg __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
870
  { return 0; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
871
  virtual bool remove_fixed(uchar * arg __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
872
  {
873
    fixed= 0;
874
    return 0;
875
  }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
876
  virtual bool cleanup_processor(uchar *arg __attribute__((unused)));
877
  virtual bool collect_item_field_processor(uchar * arg __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
878
  { return 0; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
879
  virtual bool find_item_in_field_list_processor(uchar *arg __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
880
 { return 0; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
881
  virtual bool change_context_processor(uchar *context __attribute__((unused)))
882
  { return 0; }
883
  virtual bool reset_query_id_processor(uchar *query_id_arg __attribute__((unused)))
884
  { return 0; }
885
  virtual bool register_field_in_read_map(uchar *arg __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
886
  { return 0; }
1 by brian
clean slate
887
  virtual bool subst_argument_checker(uchar **arg)
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
888
  {
1 by brian
clean slate
889
    if (*arg)
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
890
      *arg= NULL;
891
    return true;
1 by brian
clean slate
892
  }
893
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
894
  virtual Item *equal_fields_propagator(uchar * arg __attribute__((unused))) { return this; }
895
  virtual bool set_no_const_sub(uchar *arg __attribute__((unused))) { return false; }
896
  virtual Item *replace_equal_field(uchar * arg __attribute__((unused))) { return this; }
1 by brian
clean slate
897
898
  /*
899
    For SP local variable returns pointer to Item representing its
900
    current value and pointer to current Item otherwise.
901
  */
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
902
  virtual Item *this_item(void) { return this; }
903
  virtual const Item *this_item(void) const { return this; }
1 by brian
clean slate
904
905
  /*
906
    For SP local variable returns address of pointer to Item representing its
907
    current value and pointer passed via parameter otherwise.
908
  */
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
909
  virtual Item **this_item_addr(THD *thd __attribute__((unused)), Item **addr_arg) { return addr_arg; }
1 by brian
clean slate
910
911
  // Row emulation
912
  virtual uint cols() { return 1; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
913
  virtual Item* element_index(uint i __attribute__((unused))) { return this; }
914
  virtual Item** addr(uint i __attribute__((unused))) { return 0; }
1 by brian
clean slate
915
  virtual bool check_cols(uint c);
916
  // It is not row => null inside is impossible
917
  virtual bool null_inside() { return 0; }
918
  // used in row subselects to get value of elements
919
  virtual void bring_value() {}
920
921
  Field *tmp_table_field_from_field_type(TABLE *table, bool fixed_length);
922
  virtual Item_field *filed_for_view_update() { return 0; }
923
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
924
  virtual Item *neg_transformer(THD *thd __attribute__((unused))) { return NULL; }
925
  virtual Item *update_value_transformer(uchar *select_arg __attribute__((unused))) { return this; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
926
  virtual Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
927
  void delete_self()
928
  {
929
    cleanup();
930
    delete this;
931
  }
932
933
  /*
163 by Brian Aker
Merge Monty's code.
934
    result_as_int64_t() must return true for Items representing DATE/TIME
1 by brian
clean slate
935
    functions and DATE/TIME table fields.
936
    Those Items have result_type()==STRING_RESULT (and not INT_RESULT), but
937
    their values should be compared as integers (because the integer
938
    representation is more precise than the string one).
939
  */
163 by Brian Aker
Merge Monty's code.
940
  virtual bool result_as_int64_t() { return false; }
1 by brian
clean slate
941
  bool is_datetime();
942
943
  /*
944
    Test whether an expression is expensive to compute. Used during
945
    optimization to avoid computing expensive expressions during this
946
    phase. Also used to force temp tables when sorting on expensive
947
    functions.
948
    TODO:
949
    Normally we should have a method:
950
      cost Item::execution_cost(),
951
    where 'cost' is either 'double' or some structure of various cost
952
    parameters.
953
  */
954
  virtual bool is_expensive()
955
  {
956
    if (is_expensive_cache < 0)
957
      is_expensive_cache= walk(&Item::is_expensive_processor, 0, (uchar*)0);
958
    return test(is_expensive_cache);
959
  }
960
  String *check_well_formed_result(String *str, bool send_error= 0);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
961
  bool eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs); 
1 by brian
clean slate
962
};
963
964
965
class Item_basic_constant :public Item
966
{
967
public:
968
  /* to prevent drop fixed flag (no need parent cleanup call) */
969
  void cleanup()
970
  {
971
    /*
972
      Restore the original field name as it might not have been allocated
973
      in the statement memory. If the name is auto generated, it must be
974
      done again between subsequent executions of a prepared statement.
975
    */
976
    if (orig_name)
977
      name= orig_name;
978
  }
979
};
980
981
bool agg_item_collations(DTCollation &c, const char *name,
982
                         Item **items, uint nitems, uint flags, int item_sep);
983
bool agg_item_collations_for_comparison(DTCollation &c, const char *name,
984
                                        Item **items, uint nitems, uint flags);
985
bool agg_item_charsets(DTCollation &c, const char *name,
986
                       Item **items, uint nitems, uint flags, int item_sep);
987
988
989
class Item_num: public Item_basic_constant
990
{
991
public:
992
  Item_num() {}                               /* Remove gcc warning */
993
  virtual Item_num *neg()= 0;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
994
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
995
};
996
997
#define NO_CACHED_FIELD_INDEX ((uint)(-1))
998
999
class st_select_lex;
1000
class Item_ident :public Item
1001
{
1002
protected:
1003
  /* 
1004
    We have to store initial values of db_name, table_name and field_name
1005
    to be able to restore them during cleanup() because they can be 
1006
    updated during fix_fields() to values from Field object and life-time 
1007
    of those is shorter than life-time of Item_field.
1008
  */
1009
  const char *orig_db_name;
1010
  const char *orig_table_name;
1011
  const char *orig_field_name;
1012
1013
public:
1014
  Name_resolution_context *context;
1015
  const char *db_name;
1016
  const char *table_name;
1017
  const char *field_name;
1018
  bool alias_name_used; /* true if item was resolved against alias */
1019
  /* 
1020
    Cached value of index for this field in table->field array, used by prep. 
1021
    stmts for speeding up their re-execution. Holds NO_CACHED_FIELD_INDEX 
1022
    if index value is not known.
1023
  */
1024
  uint cached_field_index;
1025
  /*
1026
    Cached pointer to table which contains this field, used for the same reason
1027
    by prep. stmt. too in case then we have not-fully qualified field.
1028
    0 - means no cached value.
1029
  */
1030
  TABLE_LIST *cached_table;
1031
  st_select_lex *depended_from;
1032
  Item_ident(Name_resolution_context *context_arg,
1033
             const char *db_name_arg, const char *table_name_arg,
1034
             const char *field_name_arg);
1035
  Item_ident(THD *thd, Item_ident *item);
1036
  const char *full_name() const;
1037
  void cleanup();
1038
  bool remove_dependence_processor(uchar * arg);
1039
  virtual void print(String *str, enum_query_type query_type);
1040
  virtual bool change_context_processor(uchar *cntx)
163 by Brian Aker
Merge Monty's code.
1041
    { context= (Name_resolution_context *)cntx; return false; }
1 by brian
clean slate
1042
  friend bool insert_fields(THD *thd, Name_resolution_context *context,
1043
                            const char *db_name,
1044
                            const char *table_name, List_iterator<Item> *it,
1045
                            bool any_privileges);
1046
};
1047
1048
1049
class Item_ident_for_show :public Item
1050
{
1051
public:
1052
  Field *field;
1053
  const char *db_name;
1054
  const char *table_name;
1055
1056
  Item_ident_for_show(Field *par_field, const char *db_arg,
1057
                      const char *table_name_arg)
1058
    :field(par_field), db_name(db_arg), table_name(table_name_arg)
1059
  {}
1060
1061
  enum Type type() const { return FIELD_ITEM; }
1062
  double val_real() { return field->val_real(); }
152 by Brian Aker
longlong replacement
1063
  int64_t val_int() { return field->val_int(); }
1 by brian
clean slate
1064
  String *val_str(String *str) { return field->val_str(str); }
1065
  my_decimal *val_decimal(my_decimal *dec) { return field->val_decimal(dec); }
1066
  void make_field(Send_field *tmp_field);
1067
};
1068
1069
1070
class Item_equal;
1071
class COND_EQUAL;
1072
1073
class Item_field :public Item_ident
1074
{
1075
protected:
1076
  void set_field(Field *field);
1077
public:
1078
  Field *field,*result_field;
1079
  Item_equal *item_equal;
1080
  bool no_const_subst;
1081
  /*
163 by Brian Aker
Merge Monty's code.
1082
    if any_privileges set to true then here real effective privileges will
1 by brian
clean slate
1083
    be stored
1084
  */
1085
  uint have_privileges;
1086
  /* field need any privileges (for VIEW creation) */
1087
  bool any_privileges;
1088
  Item_field(Name_resolution_context *context_arg,
1089
             const char *db_arg,const char *table_name_arg,
1090
	     const char *field_name_arg);
1091
  /*
1092
    Constructor needed to process subselect with temporary tables (see Item)
1093
  */
1094
  Item_field(THD *thd, Item_field *item);
1095
  /*
1096
    Constructor used inside setup_wild(), ensures that field, table,
1097
    and database names will live as long as Item_field (this is important
1098
    in prepared statements).
1099
  */
1100
  Item_field(THD *thd, Name_resolution_context *context_arg, Field *field);
1101
  /*
1102
    If this constructor is used, fix_fields() won't work, because
1103
    db_name, table_name and column_name are unknown. It's necessary to call
1104
    reset_field() before fix_fields() for all fields created this way.
1105
  */
1106
  Item_field(Field *field);
1107
  enum Type type() const { return FIELD_ITEM; }
1108
  bool eq(const Item *item, bool binary_cmp) const;
1109
  double val_real();
152 by Brian Aker
longlong replacement
1110
  int64_t val_int();
1 by brian
clean slate
1111
  my_decimal *val_decimal(my_decimal *);
1112
  String *val_str(String*);
1113
  double val_result();
152 by Brian Aker
longlong replacement
1114
  int64_t val_int_result();
1 by brian
clean slate
1115
  String *str_result(String* tmp);
1116
  my_decimal *val_decimal_result(my_decimal *);
1117
  bool val_bool_result();
1118
  bool send(Protocol *protocol, String *str_arg);
1119
  void reset_field(Field *f);
1120
  bool fix_fields(THD *, Item **);
1121
  void fix_after_pullout(st_select_lex *new_parent, Item **ref);
1122
  void make_field(Send_field *tmp_field);
1123
  int save_in_field(Field *field,bool no_conversions);
1124
  void save_org_in_field(Field *field);
1125
  table_map used_tables() const;
1126
  enum Item_result result_type () const
1127
  {
1128
    return field->result_type();
1129
  }
1130
  Item_result cast_to_int_type() const
1131
  {
1132
    return field->cast_to_int_type();
1133
  }
1134
  enum_field_types field_type() const
1135
  {
1136
    return field->type();
1137
  }
1138
  enum_monotonicity_info get_monotonicity_info() const
1139
  {
1140
    return MONOTONIC_STRICT_INCREASING;
1141
  }
152 by Brian Aker
longlong replacement
1142
  int64_t val_int_endpoint(bool left_endp, bool *incl_endp);
1 by brian
clean slate
1143
  Field *get_tmp_table_field() { return result_field; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1144
  Field *tmp_table_field(TABLE *t_arg __attribute__((unused))) { return result_field; }
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1145
  bool get_date(DRIZZLE_TIME *ltime,uint fuzzydate);
1146
  bool get_date_result(DRIZZLE_TIME *ltime,uint fuzzydate);
1147
  bool get_time(DRIZZLE_TIME *ltime);
1 by brian
clean slate
1148
  bool is_null() { return field->is_null(); }
1149
  void update_null_value();
1150
  Item *get_tmp_table_item(THD *thd);
1151
  bool collect_item_field_processor(uchar * arg);
1152
  bool find_item_in_field_list_processor(uchar *arg);
1153
  bool register_field_in_read_map(uchar *arg);
1154
  void cleanup();
152 by Brian Aker
longlong replacement
1155
  bool result_as_int64_t()
1 by brian
clean slate
1156
  {
152 by Brian Aker
longlong replacement
1157
    return field->can_be_compared_as_int64_t();
1 by brian
clean slate
1158
  }
1159
  Item_equal *find_item_equal(COND_EQUAL *cond_equal);
1160
  bool subst_argument_checker(uchar **arg);
1161
  Item *equal_fields_propagator(uchar *arg);
1162
  bool set_no_const_sub(uchar *arg);
1163
  Item *replace_equal_field(uchar *arg);
203 by Brian Aker
Small cleanup around uint32 types (need to merge).
1164
  inline uint32_t max_disp_length() { return field->max_display_length(); }
1 by brian
clean slate
1165
  Item_field *filed_for_view_update() { return this; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1166
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
1167
  int fix_outer_field(THD *thd, Field **field, Item **reference);
1168
  virtual Item *update_value_transformer(uchar *select_arg);
1169
  virtual void print(String *str, enum_query_type query_type);
1170
1171
  friend class Item_default_value;
1172
  friend class Item_insert_value;
1173
  friend class st_select_lex_unit;
1174
};
1175
1176
class Item_null :public Item_basic_constant
1177
{
1178
public:
1179
  Item_null(char *name_par=0)
1180
  {
163 by Brian Aker
Merge Monty's code.
1181
    maybe_null= null_value= true;
1 by brian
clean slate
1182
    max_length= 0;
1183
    name= name_par ? name_par : (char*) "NULL";
1184
    fixed= 1;
1185
    collation.set(&my_charset_bin, DERIVATION_IGNORABLE);
1186
  }
1187
  enum Type type() const { return NULL_ITEM; }
1188
  bool eq(const Item *item, bool binary_cmp) const;
1189
  double val_real();
152 by Brian Aker
longlong replacement
1190
  int64_t val_int();
1 by brian
clean slate
1191
  String *val_str(String *str);
1192
  my_decimal *val_decimal(my_decimal *);
1193
  int save_in_field(Field *field, bool no_conversions);
1194
  int save_safe_in_field(Field *field);
1195
  bool send(Protocol *protocol, String *str);
1196
  enum Item_result result_type () const { return STRING_RESULT; }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1197
  enum_field_types field_type() const   { return DRIZZLE_TYPE_NULL; }
1 by brian
clean slate
1198
  bool basic_const_item() const { return 1; }
1199
  Item *clone_item() { return new Item_null(name); }
1200
  bool is_null() { return 1; }
1201
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1202
  virtual inline void print(String *str,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1203
                            enum_query_type query_type __attribute__((unused)))
1 by brian
clean slate
1204
  {
1205
    str->append(STRING_WITH_LEN("NULL"));
1206
  }
1207
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1208
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
1209
};
1210
1211
class Item_null_result :public Item_null
1212
{
1213
public:
1214
  Field *result_field;
1215
  Item_null_result() : Item_null(), result_field(0) {}
1216
  bool is_result_field() { return result_field != 0; }
1217
  void save_in_result_field(bool no_conversions)
1218
  {
1219
    save_in_field(result_field, no_conversions);
1220
  }
1221
};  
1222
1223
/* Item represents one placeholder ('?') of prepared statement */
1224
1225
class Item_param :public Item
1226
{
1227
  char cnvbuf[MAX_FIELD_WIDTH];
1228
  String cnvstr;
1229
  Item *cnvitem;
1230
1231
public:
1232
  enum enum_item_param_state
1233
  {
1234
    NO_VALUE, NULL_VALUE, INT_VALUE, REAL_VALUE,
1235
    STRING_VALUE, TIME_VALUE, LONG_DATA_VALUE,
1236
    DECIMAL_VALUE
1237
  } state;
1238
1239
  /*
1240
    A buffer for string and long data values. Historically all allocated
1241
    values returned from val_str() were treated as eligible to
1242
    modification. I. e. in some cases Item_func_concat can append it's
1243
    second argument to return value of the first one. Because of that we
1244
    can't return the original buffer holding string data from val_str(),
1245
    and have to have one buffer for data and another just pointing to
1246
    the data. This is the latter one and it's returned from val_str().
1247
    Can not be declared inside the union as it's not a POD type.
1248
  */
1249
  String str_value_ptr;
1250
  my_decimal decimal_value;
1251
  union
1252
  {
152 by Brian Aker
longlong replacement
1253
    int64_t integer;
1 by brian
clean slate
1254
    double   real;
1255
    /*
1256
      Character sets conversion info for string values.
1257
      Character sets of client and connection defined at bind time are used
1258
      for all conversions, even if one of them is later changed (i.e.
1259
      between subsequent calls to mysql_stmt_execute).
1260
    */
1261
    struct CONVERSION_INFO
1262
    {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1263
      const CHARSET_INFO *character_set_client;
1264
      const CHARSET_INFO *character_set_of_placeholder;
1 by brian
clean slate
1265
      /*
1266
        This points at character set of connection if conversion
1267
        to it is required (i. e. if placeholder typecode is not BLOB).
1268
        Otherwise it's equal to character_set_client (to simplify
1269
        check in convert_str_value()).
1270
      */
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1271
      const CHARSET_INFO *final_character_set_of_str_value;
1 by brian
clean slate
1272
    } cs_info;
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1273
    DRIZZLE_TIME     time;
1 by brian
clean slate
1274
  } value;
1275
1276
  /* Cached values for virtual methods to save us one switch.  */
1277
  enum Item_result item_result_type;
1278
  enum Type item_type;
1279
1280
  /*
1281
    Used when this item is used in a temporary table.
1282
    This is NOT placeholder metadata sent to client, as this value
1283
    is assigned after sending metadata (in setup_one_conversion_function).
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1284
    For example in case of 'SELECT ?' you'll get DRIZZLE_TYPE_STRING both
1 by brian
clean slate
1285
    in result set and placeholders metadata, no matter what type you will
1286
    supply for this placeholder in mysql_stmt_execute.
1287
  */
1288
  enum enum_field_types param_type;
1289
  /*
1290
    Offset of placeholder inside statement text. Used to create
1291
    no-placeholders version of this statement for the binary log.
1292
  */
1293
  uint pos_in_query;
1294
1295
  Item_param(uint pos_in_query_arg);
1296
1297
  enum Item_result result_type () const { return item_result_type; }
1298
  enum Type type() const { return item_type; }
1299
  enum_field_types field_type() const { return param_type; }
1300
1301
  double val_real();
152 by Brian Aker
longlong replacement
1302
  int64_t val_int();
1 by brian
clean slate
1303
  my_decimal *val_decimal(my_decimal*);
1304
  String *val_str(String*);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1305
  bool get_time(DRIZZLE_TIME *tm);
1306
  bool get_date(DRIZZLE_TIME *tm, uint fuzzydate);
1 by brian
clean slate
1307
  int  save_in_field(Field *field, bool no_conversions);
1308
1309
  void set_null();
203 by Brian Aker
Small cleanup around uint32 types (need to merge).
1310
  void set_int(int64_t i, uint32_t max_length_arg);
1 by brian
clean slate
1311
  void set_double(double i);
1312
  void set_decimal(const char *str, ulong length);
1313
  bool set_str(const char *str, ulong length);
1314
  bool set_longdata(const char *str, ulong length);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1315
  void set_time(DRIZZLE_TIME *tm, timestamp_type type, uint32_t max_length_arg);
1 by brian
clean slate
1316
  bool set_from_user_var(THD *thd, const user_var_entry *entry);
1317
  void reset();
1318
  /*
1319
    Assign placeholder value from bind data.
1320
    Note, that 'len' has different semantics in embedded library (as we
1321
    don't need to check that packet is not broken there). See
1322
    sql_prepare.cc for details.
1323
  */
1324
  void (*set_param_func)(Item_param *param, uchar **pos, ulong len);
1325
1326
  const String *query_val_str(String *str) const;
1327
1328
  bool convert_str_value(THD *thd);
1329
1330
  /*
1331
    If value for parameter was not set we treat it as non-const
1332
    so noone will use parameters value in fix_fields still
1333
    parameter is constant during execution.
1334
  */
1335
  virtual table_map used_tables() const
1336
  { return state != NO_VALUE ? (table_map)0 : PARAM_TABLE_BIT; }
1337
  virtual void print(String *str, enum_query_type query_type);
1338
  bool is_null()
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1339
  { assert(state != NO_VALUE); return state == NULL_VALUE; }
1 by brian
clean slate
1340
  bool basic_const_item() const;
1341
  /*
1342
    This method is used to make a copy of a basic constant item when
1343
    propagating constants in the optimizer. The reason to create a new
1344
    item and not use the existing one is not precisely known (2005/04/16).
1345
    Probably we are trying to preserve tree structure of items, in other
1346
    words, avoid pointing at one item from two different nodes of the tree.
1347
    Return a new basic constant item if parameter value is a basic
1348
    constant, assert otherwise. This method is called only if
163 by Brian Aker
Merge Monty's code.
1349
    basic_const_item returned true.
1 by brian
clean slate
1350
  */
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1351
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
1352
  Item *clone_item();
1353
  /*
1354
    Implement by-value equality evaluation if parameter value
1355
    is set and is a basic constant (integer, real or string).
163 by Brian Aker
Merge Monty's code.
1356
    Otherwise return false.
1 by brian
clean slate
1357
  */
1358
  bool eq(const Item *item, bool binary_cmp) const;
1359
  /** Item is a argument to a limit clause. */
1360
  bool limit_clause_param;
1361
};
1362
1363
1364
class Item_int :public Item_num
1365
{
1366
public:
152 by Brian Aker
longlong replacement
1367
  int64_t value;
205 by Brian Aker
uint32 -> uin32_t
1368
  Item_int(int32_t i,uint length= MY_INT32_NUM_DECIMAL_DIGITS)
152 by Brian Aker
longlong replacement
1369
    :value((int64_t) i)
1 by brian
clean slate
1370
    { max_length=length; fixed= 1; }
152 by Brian Aker
longlong replacement
1371
  Item_int(int64_t i,uint length= MY_INT64_NUM_DECIMAL_DIGITS)
1 by brian
clean slate
1372
    :value(i)
1373
    { max_length=length; fixed= 1; }
151 by Brian Aker
Ulonglong to uint64_t
1374
  Item_int(uint64_t i, uint length= MY_INT64_NUM_DECIMAL_DIGITS)
152 by Brian Aker
longlong replacement
1375
    :value((int64_t)i)
1 by brian
clean slate
1376
    { max_length=length; fixed= 1; unsigned_flag= 1; }
152 by Brian Aker
longlong replacement
1377
  Item_int(const char *str_arg,int64_t i,uint length) :value(i)
1 by brian
clean slate
1378
    { max_length=length; name=(char*) str_arg; fixed= 1; }
1379
  Item_int(const char *str_arg, uint length=64);
1380
  enum Type type() const { return INT_ITEM; }
1381
  enum Item_result result_type () const { return INT_RESULT; }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1382
  enum_field_types field_type() const { return DRIZZLE_TYPE_LONGLONG; }
152 by Brian Aker
longlong replacement
1383
  int64_t val_int() { assert(fixed == 1); return value; }
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1384
  double val_real() { assert(fixed == 1); return (double) value; }
1 by brian
clean slate
1385
  my_decimal *val_decimal(my_decimal *);
1386
  String *val_str(String*);
1387
  int save_in_field(Field *field, bool no_conversions);
1388
  bool basic_const_item() const { return 1; }
1389
  Item *clone_item() { return new Item_int(name,value,max_length); }
1390
  virtual void print(String *str, enum_query_type query_type);
1391
  Item_num *neg() { value= -value; return this; }
1392
  uint decimal_precision() const
1393
  { return (uint)(max_length - test(value < 0)); }
1394
  bool eq(const Item *, bool binary_cmp) const;
1395
};
1396
1397
1398
class Item_uint :public Item_int
1399
{
1400
public:
1401
  Item_uint(const char *str_arg, uint length);
151 by Brian Aker
Ulonglong to uint64_t
1402
  Item_uint(uint64_t i) :Item_int((uint64_t) i, 10) {}
152 by Brian Aker
longlong replacement
1403
  Item_uint(const char *str_arg, int64_t i, uint length);
1 by brian
clean slate
1404
  double val_real()
151 by Brian Aker
Ulonglong to uint64_t
1405
    { assert(fixed == 1); return uint64_t2double((uint64_t)value); }
1 by brian
clean slate
1406
  String *val_str(String*);
1407
  Item *clone_item() { return new Item_uint(name, value, max_length); }
1408
  int save_in_field(Field *field, bool no_conversions);
1409
  virtual void print(String *str, enum_query_type query_type);
1410
  Item_num *neg ();
1411
  uint decimal_precision() const { return max_length; }
1412
};
1413
1414
1415
/* decimal (fixed point) constant */
1416
class Item_decimal :public Item_num
1417
{
1418
protected:
1419
  my_decimal decimal_value;
1420
public:
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1421
  Item_decimal(const char *str_arg, uint length, const CHARSET_INFO * const charset);
1 by brian
clean slate
1422
  Item_decimal(const char *str, const my_decimal *val_arg,
1423
               uint decimal_par, uint length);
1424
  Item_decimal(my_decimal *value_par);
152 by Brian Aker
longlong replacement
1425
  Item_decimal(int64_t val, bool unsig);
1 by brian
clean slate
1426
  Item_decimal(double val, int precision, int scale);
1427
  Item_decimal(const uchar *bin, int precision, int scale);
1428
1429
  enum Type type() const { return DECIMAL_ITEM; }
1430
  enum Item_result result_type () const { return DECIMAL_RESULT; }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1431
  enum_field_types field_type() const { return DRIZZLE_TYPE_NEWDECIMAL; }
152 by Brian Aker
longlong replacement
1432
  int64_t val_int();
1 by brian
clean slate
1433
  double val_real();
1434
  String *val_str(String*);
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1435
  my_decimal *val_decimal(my_decimal *val __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1436
  { return &decimal_value; }
1 by brian
clean slate
1437
  int save_in_field(Field *field, bool no_conversions);
1438
  bool basic_const_item() const { return 1; }
1439
  Item *clone_item()
1440
  {
1441
    return new Item_decimal(name, &decimal_value, decimals, max_length);
1442
  }
1443
  virtual void print(String *str, enum_query_type query_type);
1444
  Item_num *neg()
1445
  {
1446
    my_decimal_neg(&decimal_value);
1447
    unsigned_flag= !decimal_value.sign();
1448
    return this;
1449
  }
1450
  uint decimal_precision() const { return decimal_value.precision(); }
1451
  bool eq(const Item *, bool binary_cmp) const;
1452
  void set_decimal_value(my_decimal *value_par);
1453
};
1454
1455
1456
class Item_float :public Item_num
1457
{
1458
  char *presentation;
1459
public:
1460
  double value;
1461
  // Item_real() :value(0) {}
1462
  Item_float(const char *str_arg, uint length);
1463
  Item_float(const char *str,double val_arg,uint decimal_par,uint length)
1464
    :value(val_arg)
1465
  {
1466
    presentation= name=(char*) str;
206 by Brian Aker
Removed final uint dead types.
1467
    decimals=(uint8_t) decimal_par;
1 by brian
clean slate
1468
    max_length=length;
1469
    fixed= 1;
1470
  }
1471
  Item_float(double value_par, uint decimal_par) :presentation(0), value(value_par)
1472
  {
206 by Brian Aker
Removed final uint dead types.
1473
    decimals= (uint8_t) decimal_par;
1 by brian
clean slate
1474
    fixed= 1;
1475
  }
1476
  int save_in_field(Field *field, bool no_conversions);
1477
  enum Type type() const { return REAL_ITEM; }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1478
  enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE; }
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1479
  double val_real() { assert(fixed == 1); return value; }
152 by Brian Aker
longlong replacement
1480
  int64_t val_int()
1 by brian
clean slate
1481
  {
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1482
    assert(fixed == 1);
163 by Brian Aker
Merge Monty's code.
1483
    if (value <= (double) INT64_MIN)
1 by brian
clean slate
1484
    {
163 by Brian Aker
Merge Monty's code.
1485
       return INT64_MIN;
1 by brian
clean slate
1486
    }
163 by Brian Aker
Merge Monty's code.
1487
    else if (value >= (double) (uint64_t) INT64_MAX)
1 by brian
clean slate
1488
    {
163 by Brian Aker
Merge Monty's code.
1489
      return INT64_MAX;
1 by brian
clean slate
1490
    }
152 by Brian Aker
longlong replacement
1491
    return (int64_t) rint(value);
1 by brian
clean slate
1492
  }
1493
  String *val_str(String*);
1494
  my_decimal *val_decimal(my_decimal *);
1495
  bool basic_const_item() const { return 1; }
1496
  Item *clone_item()
1497
  { return new Item_float(name, value, decimals, max_length); }
1498
  Item_num *neg() { value= -value; return this; }
1499
  virtual void print(String *str, enum_query_type query_type);
1500
  bool eq(const Item *, bool binary_cmp) const;
1501
};
1502
1503
1504
class Item_static_float_func :public Item_float
1505
{
1506
  const char *func_name;
1507
public:
1508
  Item_static_float_func(const char *str, double val_arg, uint decimal_par,
1509
                        uint length)
1510
    :Item_float(NullS, val_arg, decimal_par, length), func_name(str)
1511
  {}
1512
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1513
  virtual inline void print(String *str,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1514
                            enum_query_type query_type __attribute__((unused)))
1 by brian
clean slate
1515
  {
1516
    str->append(func_name);
1517
  }
1518
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1519
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
1520
};
1521
1522
1523
class Item_string :public Item_basic_constant
1524
{
1525
public:
1526
  Item_string(const char *str,uint length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1527
              const CHARSET_INFO * const cs, Derivation dv= DERIVATION_COERCIBLE,
1 by brian
clean slate
1528
              uint repertoire= MY_REPERTOIRE_UNICODE30)
163 by Brian Aker
Merge Monty's code.
1529
    : m_cs_specified(false)
1 by brian
clean slate
1530
  {
1531
    str_value.set_or_copy_aligned(str, length, cs);
1532
    collation.set(cs, dv, repertoire);
1533
    /*
1534
      We have to have a different max_length than 'length' here to
1535
      ensure that we get the right length if we do use the item
1536
      to create a new table. In this case max_length must be the maximum
1537
      number of chars for a string of this type because we in Create_field::
1538
      divide the max_length with mbmaxlen).
1539
    */
1540
    max_length= str_value.numchars()*cs->mbmaxlen;
1541
    set_name(str, length, cs);
1542
    decimals=NOT_FIXED_DEC;
1543
    // it is constant => can be used without fix_fields (and frequently used)
1544
    fixed= 1;
1545
  }
1546
  /* Just create an item and do not fill string representation */
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1547
  Item_string(const CHARSET_INFO * const cs, Derivation dv= DERIVATION_COERCIBLE)
163 by Brian Aker
Merge Monty's code.
1548
    : m_cs_specified(false)
1 by brian
clean slate
1549
  {
1550
    collation.set(cs, dv);
1551
    max_length= 0;
1552
    set_name(NULL, 0, cs);
1553
    decimals= NOT_FIXED_DEC;
1554
    fixed= 1;
1555
  }
1556
  Item_string(const char *name_par, const char *str, uint length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1557
              const CHARSET_INFO * const cs, Derivation dv= DERIVATION_COERCIBLE,
1 by brian
clean slate
1558
              uint repertoire= MY_REPERTOIRE_UNICODE30)
163 by Brian Aker
Merge Monty's code.
1559
    : m_cs_specified(false)
1 by brian
clean slate
1560
  {
1561
    str_value.set_or_copy_aligned(str, length, cs);
1562
    collation.set(cs, dv, repertoire);
1563
    max_length= str_value.numchars()*cs->mbmaxlen;
1564
    set_name(name_par, 0, cs);
1565
    decimals=NOT_FIXED_DEC;
1566
    // it is constant => can be used without fix_fields (and frequently used)
1567
    fixed= 1;
1568
  }
1569
  /*
1570
    This is used in stored procedures to avoid memory leaks and
1571
    does a deep copy of its argument.
1572
  */
1573
  void set_str_with_copy(const char *str_arg, uint length_arg)
1574
  {
1575
    str_value.copy(str_arg, length_arg, collation.collation);
1576
    max_length= str_value.numchars() * collation.collation->mbmaxlen;
1577
  }
1578
  void set_repertoire_from_value()
1579
  {
1580
    collation.repertoire= my_string_repertoire(str_value.charset(),
1581
                                               str_value.ptr(),
1582
                                               str_value.length());
1583
  }
1584
  enum Type type() const { return STRING_ITEM; }
1585
  double val_real();
152 by Brian Aker
longlong replacement
1586
  int64_t val_int();
1 by brian
clean slate
1587
  String *val_str(String*)
1588
  {
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1589
    assert(fixed == 1);
1 by brian
clean slate
1590
    return (String*) &str_value;
1591
  }
1592
  my_decimal *val_decimal(my_decimal *);
1593
  int save_in_field(Field *field, bool no_conversions);
1594
  enum Item_result result_type () const { return STRING_RESULT; }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1595
  enum_field_types field_type() const { return DRIZZLE_TYPE_VARCHAR; }
1 by brian
clean slate
1596
  bool basic_const_item() const { return 1; }
1597
  bool eq(const Item *item, bool binary_cmp) const;
1598
  Item *clone_item() 
1599
  {
1600
    return new Item_string(name, str_value.ptr(), 
1601
    			   str_value.length(), collation.collation);
1602
  }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1603
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
1604
  inline void append(char *str, uint length)
1605
  {
1606
    str_value.append(str, length);
1607
    max_length= str_value.numchars() * collation.collation->mbmaxlen;
1608
  }
1609
  virtual void print(String *str, enum_query_type query_type);
1610
1611
  /**
163 by Brian Aker
Merge Monty's code.
1612
    Return true if character-set-introducer was explicitly specified in the
1 by brian
clean slate
1613
    original query for this item (text literal).
1614
1615
    This operation is to be called from Item_string::print(). The idea is
1616
    that when a query is generated (re-constructed) from the Item-tree,
1617
    character-set-introducers should appear only for those literals, where
1618
    they were explicitly specified by the user. Otherwise, that may lead to
1619
    loss collation information (character set introducers implies default
1620
    collation for the literal).
1621
1622
    Basically, that makes sense only for views and hopefully will be gone
1623
    one day when we start using original query as a view definition.
1624
1625
    @return This operation returns the value of m_cs_specified attribute.
163 by Brian Aker
Merge Monty's code.
1626
      @retval true if character set introducer was explicitly specified in
1 by brian
clean slate
1627
      the original query.
163 by Brian Aker
Merge Monty's code.
1628
      @retval false otherwise.
1 by brian
clean slate
1629
  */
1630
  inline bool is_cs_specified() const
1631
  {
1632
    return m_cs_specified;
1633
  }
1634
1635
  /**
1636
    Set the value of m_cs_specified attribute.
1637
1638
    m_cs_specified attribute shows whether character-set-introducer was
1639
    explicitly specified in the original query for this text literal or
1640
    not. The attribute makes sense (is used) only for views.
1641
1642
    This operation is to be called from the parser during parsing an input
1643
    query.
1644
  */
1645
  inline void set_cs_specified(bool cs_specified)
1646
  {
1647
    m_cs_specified= cs_specified;
1648
  }
1649
1650
private:
1651
  bool m_cs_specified;
1652
};
1653
1654
1655
class Item_static_string_func :public Item_string
1656
{
1657
  const char *func_name;
1658
public:
1659
  Item_static_string_func(const char *name_par, const char *str, uint length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1660
                          const CHARSET_INFO * const cs,
1 by brian
clean slate
1661
                          Derivation dv= DERIVATION_COERCIBLE)
1662
    :Item_string(NullS, str, length, cs, dv), func_name(name_par)
1663
  {}
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1664
  Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
1665
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1666
  virtual inline void print(String *str,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1667
                            enum_query_type query_type __attribute__((unused)))
1 by brian
clean slate
1668
  {
1669
    str->append(func_name);
1670
  }
1671
};
1672
1673
1674
/* for show tables */
1675
class Item_return_date_time :public Item_string
1676
{
1677
  enum_field_types date_time_field_type;
1678
public:
1679
  Item_return_date_time(const char *name_arg, enum_field_types field_type_arg)
1680
    :Item_string(name_arg, 0, &my_charset_bin),
1681
     date_time_field_type(field_type_arg)
1682
  { }
1683
  enum_field_types field_type() const { return date_time_field_type; }
1684
};
1685
1686
1687
class Item_blob :public Item_string
1688
{
1689
public:
1690
  Item_blob(const char *name, uint length) :
1691
    Item_string(name, length, &my_charset_bin)
1692
  { max_length= length; }
1693
  enum Type type() const { return TYPE_HOLDER; }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1694
  enum_field_types field_type() const { return DRIZZLE_TYPE_BLOB; }
1 by brian
clean slate
1695
};
1696
1697
1698
/**
1699
  Item_empty_string -- is a utility class to put an item into List<Item>
1700
  which is then used in protocol.send_fields() when sending SHOW output to
1701
  the client.
1702
*/
1703
1704
class Item_empty_string :public Item_string
1705
{
1706
public:
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1707
  Item_empty_string(const char *header,uint length, const CHARSET_INFO * cs= NULL) :
1 by brian
clean slate
1708
    Item_string("",0, cs ? cs : &my_charset_utf8_general_ci)
1709
    { name=(char*) header; max_length= cs ? length * cs->mbmaxlen : length; }
1710
  void make_field(Send_field *field);
1711
};
1712
1713
1714
class Item_return_int :public Item_int
1715
{
1716
  enum_field_types int_field_type;
1717
public:
1718
  Item_return_int(const char *name_arg, uint length,
152 by Brian Aker
longlong replacement
1719
		  enum_field_types field_type_arg, int64_t value= 0)
1 by brian
clean slate
1720
    :Item_int(name_arg, value, length), int_field_type(field_type_arg)
1721
  {
1722
    unsigned_flag=1;
1723
  }
1724
  enum_field_types field_type() const { return int_field_type; }
1725
};
1726
1727
1728
class Item_hex_string: public Item_basic_constant
1729
{
1730
public:
1731
  Item_hex_string() {}
1732
  Item_hex_string(const char *str,uint str_length);
1733
  enum Type type() const { return VARBIN_ITEM; }
1734
  double val_real()
1735
  { 
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1736
    assert(fixed == 1); 
151 by Brian Aker
Ulonglong to uint64_t
1737
    return (double) (uint64_t) Item_hex_string::val_int();
1 by brian
clean slate
1738
  }
152 by Brian Aker
longlong replacement
1739
  int64_t val_int();
1 by brian
clean slate
1740
  bool basic_const_item() const { return 1; }
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1741
  String *val_str(String*) { assert(fixed == 1); return &str_value; }
1 by brian
clean slate
1742
  my_decimal *val_decimal(my_decimal *);
1743
  int save_in_field(Field *field, bool no_conversions);
1744
  enum Item_result result_type () const { return STRING_RESULT; }
1745
  enum Item_result cast_to_int_type() const { return INT_RESULT; }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1746
  enum_field_types field_type() const { return DRIZZLE_TYPE_VARCHAR; }
1 by brian
clean slate
1747
  virtual void print(String *str, enum_query_type query_type);
1748
  bool eq(const Item *item, bool binary_cmp) const;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1749
  virtual Item *safe_charset_converter(const CHARSET_INFO * const tocs);
1 by brian
clean slate
1750
};
1751
1752
1753
class Item_bin_string: public Item_hex_string
1754
{
1755
public:
1756
  Item_bin_string(const char *str,uint str_length);
1757
};
1758
1759
class Item_result_field :public Item	/* Item with result field */
1760
{
1761
public:
1762
  Field *result_field;				/* Save result here */
1763
  Item_result_field() :result_field(0) {}
1764
  // Constructor used for Item_sum/Item_cond_and/or (see Item comment)
1765
  Item_result_field(THD *thd, Item_result_field *item):
1766
    Item(thd, item), result_field(item->result_field)
1767
  {}
1768
  ~Item_result_field() {}			/* Required with gcc 2.95 */
1769
  Field *get_tmp_table_field() { return result_field; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
1770
  Field *tmp_table_field(TABLE *t_arg __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
1771
  { return result_field; }
1 by brian
clean slate
1772
  table_map used_tables() const { return 1; }
1773
  virtual void fix_length_and_dec()=0;
1774
  void set_result_field(Field *field) { result_field= field; }
1775
  bool is_result_field() { return 1; }
1776
  void save_in_result_field(bool no_conversions)
1777
  {
1778
    save_in_field(result_field, no_conversions);
1779
  }
1780
  void cleanup();
1781
};
1782
1783
1784
class Item_ref :public Item_ident
1785
{
1786
protected:
1787
  void set_properties();
1788
public:
1789
  enum Ref_Type { REF, DIRECT_REF, VIEW_REF, OUTER_REF };
1790
  Field *result_field;			 /* Save result here */
1791
  Item **ref;
1792
  Item_ref(Name_resolution_context *context_arg,
1793
           const char *db_arg, const char *table_name_arg,
1794
           const char *field_name_arg)
1795
    :Item_ident(context_arg, db_arg, table_name_arg, field_name_arg),
1796
     result_field(0), ref(0) {}
1797
  /*
1798
    This constructor is used in two scenarios:
1799
    A) *item = NULL
1800
      No initialization is performed, fix_fields() call will be necessary.
1801
      
1802
    B) *item points to an Item this Item_ref will refer to. This is 
1803
      used for GROUP BY. fix_fields() will not be called in this case,
1804
      so we call set_properties to make this item "fixed". set_properties
1805
      performs a subset of action Item_ref::fix_fields does, and this subset
1806
      is enough for Item_ref's used in GROUP BY.
1807
    
1808
    TODO we probably fix a superset of problems like in BUG#6658. Check this 
1809
         with Bar, and if we have a more broader set of problems like this.
1810
  */
1811
  Item_ref(Name_resolution_context *context_arg, Item **item,
1812
           const char *table_name_arg, const char *field_name_arg,
163 by Brian Aker
Merge Monty's code.
1813
           bool alias_name_used_arg= false);
1 by brian
clean slate
1814
1815
  /* Constructor need to process subselect with temporary tables (see Item) */
1816
  Item_ref(THD *thd, Item_ref *item)
1817
    :Item_ident(thd, item), result_field(item->result_field), ref(item->ref) {}
1818
  enum Type type() const		{ return REF_ITEM; }
1819
  bool eq(const Item *item, bool binary_cmp) const
1820
  { 
1821
    Item *it= ((Item *) item)->real_item();
1822
    return ref && (*ref)->eq(it, binary_cmp);
1823
  }
1824
  double val_real();
152 by Brian Aker
longlong replacement
1825
  int64_t val_int();
1 by brian
clean slate
1826
  my_decimal *val_decimal(my_decimal *);
1827
  bool val_bool();
1828
  String *val_str(String* tmp);
1829
  bool is_null();
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1830
  bool get_date(DRIZZLE_TIME *ltime,uint fuzzydate);
1 by brian
clean slate
1831
  double val_result();
152 by Brian Aker
longlong replacement
1832
  int64_t val_int_result();
1 by brian
clean slate
1833
  String *str_result(String* tmp);
1834
  my_decimal *val_decimal_result(my_decimal *);
1835
  bool val_bool_result();
1836
  bool send(Protocol *prot, String *tmp);
1837
  void make_field(Send_field *field);
1838
  bool fix_fields(THD *, Item **);
1839
  void fix_after_pullout(st_select_lex *new_parent, Item **ref);
1840
  int save_in_field(Field *field, bool no_conversions);
1841
  void save_org_in_field(Field *field);
1842
  enum Item_result result_type () const { return (*ref)->result_type(); }
1843
  enum_field_types field_type() const   { return (*ref)->field_type(); }
1844
  Field *get_tmp_table_field()
1845
  { return result_field ? result_field : (*ref)->get_tmp_table_field(); }
1846
  Item *get_tmp_table_item(THD *thd);
1847
  table_map used_tables() const		
1848
  {
1849
    return depended_from ? OUTER_REF_TABLE_BIT : (*ref)->used_tables(); 
1850
  }
1851
  void update_used_tables() 
1852
  { 
1853
    if (!depended_from) 
1854
      (*ref)->update_used_tables(); 
1855
  }
1856
  table_map not_null_tables() const { return (*ref)->not_null_tables(); }
1857
  void set_result_field(Field *field)	{ result_field= field; }
1858
  bool is_result_field() { return 1; }
1859
  void save_in_result_field(bool no_conversions)
1860
  {
1861
    (*ref)->save_in_field(result_field, no_conversions);
1862
  }
1863
  Item *real_item()
1864
  {
1865
    return ref ? (*ref)->real_item() : this;
1866
  }
1867
  bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
1868
  { return (*ref)->walk(processor, walk_subquery, arg); }
1869
  virtual void print(String *str, enum_query_type query_type);
152 by Brian Aker
longlong replacement
1870
  bool result_as_int64_t()
1 by brian
clean slate
1871
  {
152 by Brian Aker
longlong replacement
1872
    return (*ref)->result_as_int64_t();
1 by brian
clean slate
1873
  }
1874
  void cleanup();
1875
  Item_field *filed_for_view_update()
1876
    { return (*ref)->filed_for_view_update(); }
1877
  virtual Ref_Type ref_type() { return REF; }
1878
1879
  // Row emulation: forwarding of ROW-related calls to ref
1880
  uint cols()
1881
  {
1882
    return ref && result_type() == ROW_RESULT ? (*ref)->cols() : 1;
1883
  }
1884
  Item* element_index(uint i)
1885
  {
1886
    return ref && result_type() == ROW_RESULT ? (*ref)->element_index(i) : this;
1887
  }
1888
  Item** addr(uint i)
1889
  {
1890
    return ref && result_type() == ROW_RESULT ? (*ref)->addr(i) : 0;
1891
  }
1892
  bool check_cols(uint c)
1893
  {
1894
    return ref && result_type() == ROW_RESULT ? (*ref)->check_cols(c) 
1895
                                              : Item::check_cols(c);
1896
  }
1897
  bool null_inside()
1898
  {
1899
    return ref && result_type() == ROW_RESULT ? (*ref)->null_inside() : 0;
1900
  }
1901
  void bring_value()
1902
  { 
1903
    if (ref && result_type() == ROW_RESULT)
1904
      (*ref)->bring_value();
1905
  }
1906
1907
};
1908
1909
1910
/*
1911
  The same as Item_ref, but get value from val_* family of method to get
1912
  value of item on which it referred instead of result* family.
1913
*/
1914
class Item_direct_ref :public Item_ref
1915
{
1916
public:
1917
  Item_direct_ref(Name_resolution_context *context_arg, Item **item,
1918
                  const char *table_name_arg,
1919
                  const char *field_name_arg,
163 by Brian Aker
Merge Monty's code.
1920
                  bool alias_name_used_arg= false)
1 by brian
clean slate
1921
    :Item_ref(context_arg, item, table_name_arg,
1922
              field_name_arg, alias_name_used_arg)
1923
  {}
1924
  /* Constructor need to process subselect with temporary tables (see Item) */
1925
  Item_direct_ref(THD *thd, Item_direct_ref *item) : Item_ref(thd, item) {}
1926
1927
  double val_real();
152 by Brian Aker
longlong replacement
1928
  int64_t val_int();
1 by brian
clean slate
1929
  String *val_str(String* tmp);
1930
  my_decimal *val_decimal(my_decimal *);
1931
  bool val_bool();
1932
  bool is_null();
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1933
  bool get_date(DRIZZLE_TIME *ltime,uint fuzzydate);
1 by brian
clean slate
1934
  virtual Ref_Type ref_type() { return DIRECT_REF; }
1935
};
1936
1937
/*
1938
  Class for view fields, the same as Item_direct_ref, but call fix_fields
1939
  of reference if it is not called yet
1940
*/
1941
class Item_direct_view_ref :public Item_direct_ref
1942
{
1943
public:
1944
  Item_direct_view_ref(Name_resolution_context *context_arg, Item **item,
1945
                  const char *table_name_arg,
1946
                  const char *field_name_arg)
1947
    :Item_direct_ref(context_arg, item, table_name_arg, field_name_arg) {}
1948
  /* Constructor need to process subselect with temporary tables (see Item) */
1949
  Item_direct_view_ref(THD *thd, Item_direct_ref *item)
1950
    :Item_direct_ref(thd, item) {}
1951
1952
  bool fix_fields(THD *, Item **);
1953
  bool eq(const Item *item, bool binary_cmp) const;
1954
  Item *get_tmp_table_item(THD *thd)
1955
  {
1956
    Item *item= Item_ref::get_tmp_table_item(thd);
1957
    item->name= name;
1958
    return item;
1959
  }
1960
  virtual Ref_Type ref_type() { return VIEW_REF; }
1961
};
1962
1963
1964
/*
1965
  Class for outer fields.
1966
  An object of this class is created when the select where the outer field was
1967
  resolved is a grouping one. After it has been fixed the ref field will point
1968
  to either an Item_ref or an Item_direct_ref object which will be used to
1969
  access the field.
1970
  See also comments for the fix_inner_refs() and the
1971
  Item_field::fix_outer_field() functions.
1972
*/
1973
1974
class Item_sum;
1975
class Item_outer_ref :public Item_direct_ref
1976
{
1977
public:
1978
  Item *outer_ref;
1979
  /* The aggregate function under which this outer ref is used, if any. */
1980
  Item_sum *in_sum_func;
1981
  /*
163 by Brian Aker
Merge Monty's code.
1982
    true <=> that the outer_ref is already present in the select list
1 by brian
clean slate
1983
    of the outer select.
1984
  */
1985
  bool found_in_select_list;
1986
  Item_outer_ref(Name_resolution_context *context_arg,
1987
                 Item_field *outer_field_arg)
1988
    :Item_direct_ref(context_arg, 0, outer_field_arg->table_name,
1989
                     outer_field_arg->field_name),
1990
    outer_ref(outer_field_arg), in_sum_func(0),
1991
    found_in_select_list(0)
1992
  {
1993
    ref= &outer_ref;
1994
    set_properties();
1995
    fixed= 0;
1996
  }
1997
  Item_outer_ref(Name_resolution_context *context_arg, Item **item,
1998
                 const char *table_name_arg, const char *field_name_arg,
1999
                 bool alias_name_used_arg)
2000
    :Item_direct_ref(context_arg, item, table_name_arg, field_name_arg,
2001
                     alias_name_used_arg),
2002
    outer_ref(0), in_sum_func(0), found_in_select_list(1)
2003
  {}
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2004
  void save_in_result_field(bool no_conversions __attribute__((unused)))
1 by brian
clean slate
2005
  {
2006
    outer_ref->save_org_in_field(result_field);
2007
  }
2008
  bool fix_fields(THD *, Item **);
2009
  void fix_after_pullout(st_select_lex *new_parent, Item **ref);
2010
  table_map used_tables() const
2011
  {
2012
    return (*ref)->const_item() ? 0 : OUTER_REF_TABLE_BIT;
2013
  }
2014
  virtual Ref_Type ref_type() { return OUTER_REF; }
2015
};
2016
2017
2018
class Item_in_subselect;
2019
2020
2021
/*
2022
  An object of this class:
2023
   - Converts val_XXX() calls to ref->val_XXX_result() calls, like Item_ref.
163 by Brian Aker
Merge Monty's code.
2024
   - Sets owner->was_null=true if it has returned a NULL value from any
1 by brian
clean slate
2025
     val_XXX() function. This allows to inject an Item_ref_null_helper
2026
     object into subquery and then check if the subquery has produced a row
2027
     with NULL value.
2028
*/
2029
2030
class Item_ref_null_helper: public Item_ref
2031
{
2032
protected:
2033
  Item_in_subselect* owner;
2034
public:
2035
  Item_ref_null_helper(Name_resolution_context *context_arg,
2036
                       Item_in_subselect* master, Item **item,
2037
		       const char *table_name_arg, const char *field_name_arg)
2038
    :Item_ref(context_arg, item, table_name_arg, field_name_arg),
2039
     owner(master) {}
2040
  double val_real();
152 by Brian Aker
longlong replacement
2041
  int64_t val_int();
1 by brian
clean slate
2042
  String* val_str(String* s);
2043
  my_decimal *val_decimal(my_decimal *);
2044
  bool val_bool();
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
2045
  bool get_date(DRIZZLE_TIME *ltime, uint fuzzydate);
1 by brian
clean slate
2046
  virtual void print(String *str, enum_query_type query_type);
2047
  /*
2048
    we add RAND_TABLE_BIT to prevent moving this item from HAVING to WHERE
2049
  */
2050
  table_map used_tables() const
2051
  {
2052
    return (depended_from ?
2053
            OUTER_REF_TABLE_BIT :
2054
            (*ref)->used_tables() | RAND_TABLE_BIT);
2055
  }
2056
};
2057
2058
/*
2059
  The following class is used to optimize comparing of date and bigint columns
2060
  We need to save the original item ('ref') to be able to call
2061
  ref->save_in_field(). This is used to create index search keys.
2062
  
2063
  An instance of Item_int_with_ref may have signed or unsigned integer value.
2064
  
2065
*/
2066
2067
class Item_int_with_ref :public Item_int
2068
{
2069
  Item *ref;
2070
public:
152 by Brian Aker
longlong replacement
2071
  Item_int_with_ref(int64_t i, Item *ref_arg, my_bool unsigned_arg) :
1 by brian
clean slate
2072
    Item_int(i), ref(ref_arg)
2073
  {
2074
    unsigned_flag= unsigned_arg;
2075
  }
2076
  int save_in_field(Field *field, bool no_conversions)
2077
  {
2078
    return ref->save_in_field(field, no_conversions);
2079
  }
2080
  Item *clone_item();
2081
  virtual Item *real_item() { return ref; }
2082
};
2083
2084
#ifdef MYSQL_SERVER
2085
#include "item_sum.h"
2086
#include "item_func.h"
2087
#include "item_row.h"
2088
#include "item_cmpfunc.h"
2089
#include "item_strfunc.h"
2090
#include "item_timefunc.h"
2091
#include "item_subselect.h"
2092
#endif
2093
2094
class Item_copy_string :public Item
2095
{
2096
  enum enum_field_types cached_field_type;
2097
public:
2098
  Item *item;
2099
  Item_copy_string(Item *i) :item(i)
2100
  {
2101
    null_value=maybe_null=item->maybe_null;
2102
    decimals=item->decimals;
2103
    max_length=item->max_length;
2104
    name=item->name;
2105
    cached_field_type= item->field_type();
2106
  }
2107
  enum Type type() const { return COPY_STR_ITEM; }
2108
  enum Item_result result_type () const { return STRING_RESULT; }
2109
  enum_field_types field_type() const { return cached_field_type; }
2110
  double val_real()
2111
  {
2112
    int err_not_used;
2113
    char *end_not_used;
2114
    return (null_value ? 0.0 :
2115
	    my_strntod(str_value.charset(), (char*) str_value.ptr(),
2116
		       str_value.length(), &end_not_used, &err_not_used));
2117
  }
152 by Brian Aker
longlong replacement
2118
  int64_t val_int()
1 by brian
clean slate
2119
  {
2120
    int err;
80.1.1 by Brian Aker
LL() cleanup
2121
    return null_value ? 0LL : my_strntoll(str_value.charset(),str_value.ptr(),
1 by brian
clean slate
2122
                                            str_value.length(),10, (char**) 0,
2123
                                            &err); 
2124
  }
2125
  String *val_str(String*);
2126
  my_decimal *val_decimal(my_decimal *);
2127
  void make_field(Send_field *field) { item->make_field(field); }
2128
  void copy();
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
2129
  int save_in_field(Field *field,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2130
                    bool no_conversions __attribute__((unused)))
1 by brian
clean slate
2131
  {
2132
    return save_str_value_in_field(field, &str_value);
2133
  }
2134
  table_map used_tables() const { return (table_map) 1L; }
2135
  bool const_item() const { return 0; }
2136
  bool is_null() { return null_value; }
2137
};
2138
2139
2140
class Cached_item :public Sql_alloc
2141
{
2142
public:
2143
  my_bool null_value;
2144
  Cached_item() :null_value(0) {}
2145
  virtual bool cmp(void)=0;
2146
  virtual ~Cached_item(); /*line -e1509 */
2147
};
2148
2149
class Cached_item_str :public Cached_item
2150
{
2151
  Item *item;
2152
  String value,tmp_value;
2153
public:
2154
  Cached_item_str(THD *thd, Item *arg);
2155
  bool cmp(void);
2156
  ~Cached_item_str();                           // Deallocate String:s
2157
};
2158
2159
2160
class Cached_item_real :public Cached_item
2161
{
2162
  Item *item;
2163
  double value;
2164
public:
2165
  Cached_item_real(Item *item_par) :item(item_par),value(0.0) {}
2166
  bool cmp(void);
2167
};
2168
2169
class Cached_item_int :public Cached_item
2170
{
2171
  Item *item;
152 by Brian Aker
longlong replacement
2172
  int64_t value;
1 by brian
clean slate
2173
public:
2174
  Cached_item_int(Item *item_par) :item(item_par),value(0) {}
2175
  bool cmp(void);
2176
};
2177
2178
2179
class Cached_item_decimal :public Cached_item
2180
{
2181
  Item *item;
2182
  my_decimal value;
2183
public:
2184
  Cached_item_decimal(Item *item_par);
2185
  bool cmp(void);
2186
};
2187
2188
class Cached_item_field :public Cached_item
2189
{
2190
  uchar *buff;
2191
  Field *field;
2192
  uint length;
2193
2194
public:
2195
  Cached_item_field(Field *arg_field) : field(arg_field)
2196
  {
2197
    field= arg_field;
2198
    /* TODO: take the memory allocation below out of the constructor. */
2199
    buff= (uchar*) sql_calloc(length=field->pack_length());
2200
  }
2201
  bool cmp(void);
2202
};
2203
2204
class Item_default_value : public Item_field
2205
{
2206
public:
2207
  Item *arg;
2208
  Item_default_value(Name_resolution_context *context_arg)
2209
    :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
2210
               (const char *)NULL),
2211
     arg(NULL) {}
2212
  Item_default_value(Name_resolution_context *context_arg, Item *a)
2213
    :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
2214
                (const char *)NULL),
2215
     arg(a) {}
2216
  enum Type type() const { return DEFAULT_VALUE_ITEM; }
2217
  bool eq(const Item *item, bool binary_cmp) const;
2218
  bool fix_fields(THD *, Item **);
2219
  virtual void print(String *str, enum_query_type query_type);
2220
  int save_in_field(Field *field_arg, bool no_conversions);
2221
  table_map used_tables() const { return (table_map)0L; }
2222
2223
  bool walk(Item_processor processor, bool walk_subquery, uchar *args)
2224
  {
2225
    return arg->walk(processor, walk_subquery, args) ||
2226
      (this->*processor)(args);
2227
  }
2228
2229
  Item *transform(Item_transformer transformer, uchar *args);
2230
};
2231
2232
/*
2233
  Item_insert_value -- an implementation of VALUES() function.
2234
  You can use the VALUES(col_name) function in the UPDATE clause
2235
  to refer to column values from the INSERT portion of the INSERT
2236
  ... UPDATE statement. In other words, VALUES(col_name) in the
2237
  UPDATE clause refers to the value of col_name that would be
2238
  inserted, had no duplicate-key conflict occurred.
2239
  In all other places this function returns NULL.
2240
*/
2241
2242
class Item_insert_value : public Item_field
2243
{
2244
public:
2245
  Item *arg;
2246
  Item_insert_value(Name_resolution_context *context_arg, Item *a)
2247
    :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
2248
               (const char *)NULL),
2249
     arg(a) {}
2250
  bool eq(const Item *item, bool binary_cmp) const;
2251
  bool fix_fields(THD *, Item **);
2252
  virtual void print(String *str, enum_query_type query_type);
2253
  int save_in_field(Field *field_arg, bool no_conversions)
2254
  {
2255
    return Item_field::save_in_field(field_arg, no_conversions);
2256
  }
2257
  /* 
2258
   We use RAND_TABLE_BIT to prevent Item_insert_value from
2259
   being treated as a constant and precalculated before execution
2260
  */
2261
  table_map used_tables() const { return RAND_TABLE_BIT; }
2262
2263
  bool walk(Item_processor processor, bool walk_subquery, uchar *args)
2264
  {
2265
    return arg->walk(processor, walk_subquery, args) ||
2266
	    (this->*processor)(args);
2267
  }
2268
};
2269
2270
2271
class Item_cache: public Item_basic_constant
2272
{
2273
protected:
2274
  Item *example;
2275
  table_map used_table_map;
2276
  /*
2277
    Field that this object will get value from. This is set/used by 
2278
    index-based subquery engines to detect and remove the equality injected 
2279
    by IN->EXISTS transformation.
2280
    For all other uses of Item_cache, cached_field doesn't matter.
2281
  */  
2282
  Field *cached_field;
2283
  enum enum_field_types cached_field_type;
2284
public:
2285
  Item_cache(): 
241 by Brian Aker
First pass of CHAR removal.
2286
    example(0), used_table_map(0), cached_field(0), cached_field_type(DRIZZLE_TYPE_VARCHAR) 
1 by brian
clean slate
2287
  {
2288
    fixed= 1; 
2289
    null_value= 1;
2290
  }
2291
  Item_cache(enum_field_types field_type_arg):
2292
    example(0), used_table_map(0), cached_field(0), cached_field_type(field_type_arg)
2293
  {
2294
    fixed= 1;
2295
    null_value= 1;
2296
  }
2297
2298
  void set_used_tables(table_map map) { used_table_map= map; }
2299
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2300
  virtual bool allocate(uint i __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
2301
  { return 0; }
1 by brian
clean slate
2302
  virtual bool setup(Item *item)
2303
  {
2304
    example= item;
2305
    max_length= item->max_length;
2306
    decimals= item->decimals;
2307
    collation.set(item->collation);
2308
    unsigned_flag= item->unsigned_flag;
2309
    if (item->type() == FIELD_ITEM)
2310
      cached_field= ((Item_field *)item)->field;
2311
    return 0;
2312
  };
2313
  virtual void store(Item *)= 0;
2314
  enum Type type() const { return CACHE_ITEM; }
2315
  enum_field_types field_type() const { return cached_field_type; }
2316
  static Item_cache* get_cache(const Item *item);
2317
  table_map used_tables() const { return used_table_map; }
2318
  virtual void keep_array() {}
2319
  virtual void print(String *str, enum_query_type query_type);
2320
  bool eq_def(Field *field) 
2321
  { 
163 by Brian Aker
Merge Monty's code.
2322
    return cached_field ? cached_field->eq_def (field) : false;
1 by brian
clean slate
2323
  }
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
2324
  bool eq(const Item *item,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2325
          bool binary_cmp __attribute__((unused))) const
1 by brian
clean slate
2326
  {
2327
    return this == item;
2328
  }
2329
};
2330
2331
2332
class Item_cache_int: public Item_cache
2333
{
2334
protected:
152 by Brian Aker
longlong replacement
2335
  int64_t value;
1 by brian
clean slate
2336
public:
2337
  Item_cache_int(): Item_cache(), value(0) {}
2338
  Item_cache_int(enum_field_types field_type_arg):
2339
    Item_cache(field_type_arg), value(0) {}
2340
2341
  void store(Item *item);
152 by Brian Aker
longlong replacement
2342
  void store(Item *item, int64_t val_arg);
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
2343
  double val_real() { assert(fixed == 1); return (double) value; }
152 by Brian Aker
longlong replacement
2344
  int64_t val_int() { assert(fixed == 1); return value; }
1 by brian
clean slate
2345
  String* val_str(String *str);
2346
  my_decimal *val_decimal(my_decimal *);
2347
  enum Item_result result_type() const { return INT_RESULT; }
163 by Brian Aker
Merge Monty's code.
2348
  bool result_as_int64_t() { return true; }
1 by brian
clean slate
2349
};
2350
2351
2352
class Item_cache_real: public Item_cache
2353
{
2354
  double value;
2355
public:
2356
  Item_cache_real(): Item_cache(), value(0) {}
2357
2358
  void store(Item *item);
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
2359
  double val_real() { assert(fixed == 1); return value; }
152 by Brian Aker
longlong replacement
2360
  int64_t val_int();
1 by brian
clean slate
2361
  String* val_str(String *str);
2362
  my_decimal *val_decimal(my_decimal *);
2363
  enum Item_result result_type() const { return REAL_RESULT; }
2364
};
2365
2366
2367
class Item_cache_decimal: public Item_cache
2368
{
2369
protected:
2370
  my_decimal decimal_value;
2371
public:
2372
  Item_cache_decimal(): Item_cache() {}
2373
2374
  void store(Item *item);
2375
  double val_real();
152 by Brian Aker
longlong replacement
2376
  int64_t val_int();
1 by brian
clean slate
2377
  String* val_str(String *str);
2378
  my_decimal *val_decimal(my_decimal *);
2379
  enum Item_result result_type() const { return DECIMAL_RESULT; }
2380
};
2381
2382
2383
class Item_cache_str: public Item_cache
2384
{
2385
  char buffer[STRING_BUFFER_USUAL_SIZE];
2386
  String *value, value_buff;
2387
  bool is_varbinary;
2388
  
2389
public:
2390
  Item_cache_str(const Item *item) :
2391
    Item_cache(), value(0),
2392
    is_varbinary(item->type() == FIELD_ITEM &&
2393
                 ((const Item_field *) item)->field->type() ==
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
2394
                   DRIZZLE_TYPE_VARCHAR &&
1 by brian
clean slate
2395
                 !((const Item_field *) item)->field->has_charset())
2396
  {}
2397
  void store(Item *item);
2398
  double val_real();
152 by Brian Aker
longlong replacement
2399
  int64_t val_int();
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
2400
  String* val_str(String *) { assert(fixed == 1); return value; }
1 by brian
clean slate
2401
  my_decimal *val_decimal(my_decimal *);
2402
  enum Item_result result_type() const { return STRING_RESULT; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
2403
  const CHARSET_INFO *charset() const { return value->charset(); };
1 by brian
clean slate
2404
  int save_in_field(Field *field, bool no_conversions);
2405
};
2406
2407
class Item_cache_row: public Item_cache
2408
{
2409
  Item_cache  **values;
2410
  uint item_count;
2411
  bool save_array;
2412
public:
2413
  Item_cache_row()
2414
    :Item_cache(), values(0), item_count(2), save_array(0) {}
2415
  
2416
  /*
2417
    'allocate' used only in row transformer, to preallocate space for row 
2418
    cache.
2419
  */
2420
  bool allocate(uint num);
2421
  /*
2422
    'setup' is needed only by row => it not called by simple row subselect
2423
    (only by IN subselect (in subselect optimizer))
2424
  */
2425
  bool setup(Item *item);
2426
  void store(Item *item);
2427
  void illegal_method_call(const char *);
2428
  void make_field(Send_field *)
2429
  {
2430
    illegal_method_call((const char*)"make_field");
2431
  };
2432
  double val_real()
2433
  {
2434
    illegal_method_call((const char*)"val");
2435
    return 0;
2436
  };
152 by Brian Aker
longlong replacement
2437
  int64_t val_int()
1 by brian
clean slate
2438
  {
2439
    illegal_method_call((const char*)"val_int");
2440
    return 0;
2441
  };
2442
  String *val_str(String *)
2443
  {
2444
    illegal_method_call((const char*)"val_str");
2445
    return 0;
2446
  };
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
2447
  my_decimal *val_decimal(my_decimal *val __attribute__((unused)))
1 by brian
clean slate
2448
  {
2449
    illegal_method_call((const char*)"val_decimal");
2450
    return 0;
2451
  };
2452
2453
  enum Item_result result_type() const { return ROW_RESULT; }
2454
  
2455
  uint cols() { return item_count; }
2456
  Item *element_index(uint i) { return values[i]; }
2457
  Item **addr(uint i) { return (Item **) (values + i); }
2458
  bool check_cols(uint c);
2459
  bool null_inside();
2460
  void bring_value();
2461
  void keep_array() { save_array= 1; }
2462
  void cleanup()
2463
  {
2464
    Item_cache::cleanup();
2465
    if (save_array)
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
2466
      memset(values, 0, item_count*sizeof(Item**));
1 by brian
clean slate
2467
    else
2468
      values= 0;
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
2469
    return;
1 by brian
clean slate
2470
  }
2471
};
2472
2473
2474
/*
2475
  Item_type_holder used to store type. name, length of Item for UNIONS &
2476
  derived tables.
2477
2478
  Item_type_holder do not need cleanup() because its time of live limited by
2479
  single SP/PS execution.
2480
*/
2481
class Item_type_holder: public Item
2482
{
2483
protected:
2484
  TYPELIB *enum_set_typelib;
2485
  enum_field_types fld_type;
2486
2487
  void get_full_info(Item *item);
2488
2489
  /* It is used to count decimal precision in join_types */
2490
  int prev_decimal_int_part;
2491
public:
2492
  Item_type_holder(THD*, Item*);
2493
2494
  Item_result result_type() const;
2495
  enum_field_types field_type() const { return fld_type; };
2496
  enum Type type() const { return TYPE_HOLDER; }
2497
  double val_real();
152 by Brian Aker
longlong replacement
2498
  int64_t val_int();
1 by brian
clean slate
2499
  my_decimal *val_decimal(my_decimal *);
2500
  String *val_str(String*);
2501
  bool join_types(THD *thd, Item *);
2502
  Field *make_field_by_type(TABLE *table);
203 by Brian Aker
Small cleanup around uint32 types (need to merge).
2503
  static uint32_t display_length(Item *item);
1 by brian
clean slate
2504
  static enum_field_types get_real_type(Item *);
2505
};
2506
2507
2508
class st_select_lex;
2509
void mark_select_range_as_dependent(THD *thd,
2510
                                    st_select_lex *last_select,
2511
                                    st_select_lex *current_sel,
2512
                                    Field *found_field, Item *found_item,
2513
                                    Item_ident *resolved_item);
2514
2515
extern Cached_item *new_Cached_item(THD *thd, Item *item,
2516
                                    bool use_result_field);
2517
extern void resolve_const_item(THD *thd, Item **ref, Item *cmp_item);
2518
extern bool field_is_equal_to_item(Field *field,Item *item);