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