~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item.h

Renamed more stuff to drizzle.

Show diffs side-by-side

added added

removed removed

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