~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.h

Cleanup around SAFEMALLOC

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