~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.h

  • Committer: Brian Aker
  • Date: 2008-09-12 06:42:41 UTC
  • mto: (322.2.8 stdize-code)
  • mto: This revision was merged to the branch mainline in revision 386.
  • Revision ID: brian@tangent.org-20080912064241-520utxq2078904l2
Remove 3 byte UTF8 collation

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 <mysys/drizzle_time.h>
25
 
#include <drizzled/my_decimal.h>
26
 
#include <drizzled/sql_bitmap.h>
27
 
#include <drizzled/sql_list.h>
28
 
#include <drizzled/sql_alloc.h>
 
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 */
29
15
 
30
16
class Protocol;
31
 
class TableList;
 
17
struct TableList;
 
18
void item_init(void);                   /* Init item functions */
32
19
class Item_field;
33
 
class Name_resolution_context;
34
 
class st_select_lex;
35
 
class Item_equal;
36
 
class user_var_entry;
37
 
class Item_sum;
38
 
class Item_in_subselect;
39
 
class Send_field;
40
 
class Field;
41
 
 
42
 
void item_init(void);                   /* Init item functions */
43
 
 
44
 
 
45
 
void dummy_error_processor(Session *session, void *data);
46
 
void view_error_processor(Session *session, void *data);
47
 
 
48
 
 
49
 
/*************************************************************************/
 
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);
50
411
/*
51
412
  Analyzer function
52
413
    SYNOPSIS
53
414
      argp   in/out IN:  Analysis parameter
54
415
                    OUT: Parameter to be passed to the transformer
55
416
 
56
 
     RETURN
 
417
    RETURN 
57
418
      true   Invoke the transformer
58
419
      false  Don't do it
59
420
 
60
421
*/
61
 
typedef bool (Item::*Item_analyzer) (unsigned char **argp);
62
 
typedef Item* (Item::*Item_transformer) (unsigned char *arg);
 
422
typedef bool (Item::*Item_analyzer) (uchar **argp);
 
423
typedef Item* (Item::*Item_transformer) (uchar *arg);
63
424
typedef void (*Cond_traverser) (const Item *item, void *arg);
64
 
typedef bool (Item::*Item_processor) (unsigned char *arg);
65
 
 
66
 
 
67
 
class Item: public Sql_alloc
 
425
 
 
426
 
 
427
class Item
68
428
{
69
 
  /* Prevent use of these */
70
 
  Item(const Item &);
 
429
  Item(const Item &);                   /* Prevent use of these */
71
430
  void operator=(Item &);
72
 
 
73
431
  /* Cache of the result of is_expensive(). */
74
432
  int8_t is_expensive_cache;
75
 
  virtual bool is_expensive_processor(unsigned char *arg);
 
433
  virtual bool is_expensive_processor(uchar *arg __attribute__((unused)))
 
434
  { return 0; }
76
435
 
77
436
public:
 
437
  static void *operator new(size_t size)
 
438
  { return sql_alloc(size); }
 
439
  static void *operator new(size_t size, MEM_ROOT *mem_root)
 
440
  { return alloc_root(mem_root, size); }
 
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
  {}
78
447
 
79
 
  enum Type {FIELD_ITEM= 0,
80
 
    FUNC_ITEM,
81
 
    SUM_FUNC_ITEM,
82
 
    STRING_ITEM,
83
 
    INT_ITEM,
84
 
    REAL_ITEM,
85
 
    NULL_ITEM,
86
 
    VARBIN_ITEM,
87
 
    COPY_STR_ITEM,
88
 
    FIELD_AVG_ITEM,
89
 
    DEFAULT_VALUE_ITEM,
90
 
    PROC_ITEM,
91
 
    COND_ITEM,
92
 
    REF_ITEM,
93
 
    FIELD_STD_ITEM,
94
 
    FIELD_VARIANCE_ITEM,
95
 
    INSERT_VALUE_ITEM,
96
 
    SUBSELECT_ITEM,
97
 
    ROW_ITEM, CACHE_ITEM,
98
 
    TYPE_HOLDER,
99
 
    PARAM_ITEM,
100
 
    DECIMAL_ITEM
101
 
  };
 
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};
102
456
 
103
457
  enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
104
458
 
105
459
  enum traverse_order { POSTFIX, PREFIX };
 
460
  
 
461
  /* Reuse size, only used by SP local variable assignment, otherwize 0 */
 
462
  uint rsize;
106
463
 
107
464
  /*
108
465
    str_values's main purpose is to be used to cache the value in
109
466
    save_in_field
110
467
  */
111
468
  String str_value;
112
 
 
113
 
  /* Name from select */
114
 
  char * name;
115
 
 
 
469
  char * name;                  /* Name from select */
116
470
  /* Original item name (if it was renamed)*/
117
471
  char * orig_name;
118
472
  Item *next;
119
473
  uint32_t max_length;
120
 
 
121
 
  /* Length of name */
122
 
  uint32_t name_length;
123
 
 
 
474
  uint name_length;                     /* Length of name */
124
475
  int8_t marker;
125
476
  uint8_t decimals;
126
477
  bool maybe_null;                      /* If item may be null */
145
496
     top AND/OR structure of WHERE clause to protect it of
146
497
     optimisation changes in prepared statements
147
498
  */
148
 
  Item(Session *session, Item *item);
 
499
  Item(THD *thd, Item *item);
149
500
  virtual ~Item()
150
501
  {
151
502
#ifdef EXTRA_DEBUG
152
503
    name=0;
153
504
#endif
154
 
  }
155
 
  void set_name(const char *str, uint32_t length,
156
 
                const CHARSET_INFO * const cs);
 
505
  }             /*lint -e1509 */
 
506
  void set_name(const char *str, uint length, const CHARSET_INFO * const cs);
157
507
  void rename(char *new_name);
158
508
  void init_make_field(Send_field *tmp_field,enum enum_field_types type);
159
509
  virtual void cleanup();
160
510
  virtual void make_field(Send_field *field);
161
511
  Field *make_string_field(Table *table);
162
 
  virtual bool fix_fields(Session *, Item **);
163
 
 
 
512
  virtual bool fix_fields(THD *, Item **);
164
513
  /*
165
514
    Fix after some tables has been pulled out. Basically re-calculate all
166
515
    attributes that are dependent on the tables.
167
516
  */
168
 
  virtual void fix_after_pullout(st_select_lex *new_parent, Item **ref);
 
517
  virtual void fix_after_pullout(st_select_lex *new_parent __attribute__((unused)),
 
518
                                 Item **ref __attribute__((unused))) {};
169
519
 
170
520
  /*
171
521
    should be used in case where we are sure that we do not need
172
 
    complete fix_fields() procedure.  */
 
522
    complete fix_fields() procedure.
 
523
  */
173
524
  inline void quick_fix_field() { fixed= 1; }
174
 
 
175
 
  /*
176
 
  Save value in field, but don't give any warnings
177
 
 
178
 
  NOTES
179
 
   This is used to temporary store and retrieve a value in a column,
180
 
   for example in opt_range to adjust the key value to fit the column.
181
 
  Return: Function returns 1 on overflow and -1 on fatal errors
182
 
  */
 
525
  /* Function returns 1 on overflow and -1 on fatal errors */
183
526
  int save_in_field_no_warnings(Field *field, bool no_conversions);
184
 
 
185
527
  virtual int save_in_field(Field *field, bool no_conversions);
186
528
  virtual void save_org_in_field(Field *field)
187
529
  { (void) save_in_field(field, 1); }
194
536
  virtual enum_field_types string_field_type() const;
195
537
  virtual enum_field_types field_type() const;
196
538
  virtual enum Type type() const =0;
197
 
 
 
539
  
198
540
  /*
199
541
    Return information about function monotonicity. See comment for
200
542
    enum_monotonicity_info for details. This function can only be called
204
546
  { return NON_MONOTONIC; }
205
547
 
206
548
  /*
207
 
    Convert:
208
 
      "func_arg $CMP$ const" half-interval
209
 
    into:
210
 
      "FUNC(func_arg) $CMP2$ const2"
 
549
    Convert "func_arg $CMP$ const" half-interval into "FUNC(func_arg) $CMP2$ const2"
211
550
 
212
551
    SYNOPSIS
213
552
      val_int_endpoint()
227
566
      The value of const is supplied implicitly as the value this item's
228
567
      argument, the form of $CMP$ comparison is specified through the
229
568
      function's arguments. The calle returns the result interval
230
 
 
 
569
         
231
570
         F(x) $CMP2$ F(const)
232
 
 
233
 
      passing back F(const) as the return value, and the form of $CMP2$
 
571
      
 
572
      passing back F(const) as the return value, and the form of $CMP2$ 
234
573
      through the out parameter. NULL values are assumed to be comparable and
235
574
      be less than any non-NULL values.
236
575
 
239
578
        - If the value of the function is NULL then the bound is the
240
579
          smallest possible value of INT64_MIN
241
580
  */
242
 
  virtual int64_t val_int_endpoint(bool left_endp, bool *incl_endp);
 
581
  virtual int64_t val_int_endpoint(bool left_endp __attribute__((unused)),
 
582
                                    bool *incl_endp __attribute__((unused)))
 
583
  { assert(0); return 0; }
243
584
 
244
585
 
245
586
  /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
351
692
 
352
693
  virtual Field *get_tmp_table_field(void) { return 0; }
353
694
  /* This is also used to create fields in CREATE ... SELECT: */
354
 
  virtual Field *tmp_table_field(Table *t_arg);
355
 
  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 : "???"; }
356
698
 
357
699
  /*
358
700
    *result* family of methods is analog of *val* family (see above) but
389
731
  /* cloning of constant items (0 if it is not const) */
390
732
  virtual Item *clone_item() { return 0; }
391
733
  virtual cond_result eq_cmp_result() const { return COND_OK; }
392
 
  inline uint32_t float_length(uint32_t decimals_par) const
 
734
  inline uint float_length(uint decimals_par) const
393
735
  { return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;}
394
 
  virtual uint32_t decimal_precision() const;
395
 
  int decimal_int_part() const;
396
 
 
397
 
  /*
 
736
  virtual uint decimal_precision() const;
 
737
  inline int decimal_int_part() const
 
738
  { return my_decimal_int_part(decimal_precision(), decimals); }
 
739
  /* 
398
740
    Returns true if this is constant (during query execution, i.e. its value
399
741
    will not change until next fix_fields) and its value is known.
400
742
  */
401
743
  virtual bool const_item() const { return used_tables() == 0; }
402
 
  /*
 
744
  /* 
403
745
    Returns true if this is constant but its value may be not known yet.
404
746
    (Can be used for parameters of prep. stmts or of stored procedures.)
405
747
  */
406
 
  virtual bool const_during_execution() const
 
748
  virtual bool const_during_execution() const 
407
749
  { return (used_tables() & ~PARAM_TABLE_BIT) == 0; }
408
750
 
409
751
  /**
417
759
    query and why they should be generated from the Item-tree, @see
418
760
    mysql_register_view().
419
761
  */
420
 
  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
  }
421
767
 
422
768
  void print_item_w_name(String *, enum_query_type query_type);
423
769
  virtual void update_used_tables() {}
424
 
  virtual void split_sum_func(Session *session, Item **ref_pointer_array,
425
 
                              List<Item> &fields);
426
 
 
 
770
  virtual void split_sum_func(THD *thd __attribute__((unused)),
 
771
                              Item **ref_pointer_array __attribute__((unused)),
 
772
                              List<Item> &fields __attribute__((unused))) {}
427
773
  /* Called for items that really have to be split */
428
 
  void split_sum_func(Session *session, Item **ref_pointer_array,
429
 
                      List<Item> &fields,
430
 
                      Item **ref, bool skip_registered);
431
 
 
432
 
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
 
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);
433
777
  virtual bool get_time(DRIZZLE_TIME *ltime);
434
 
  virtual bool get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
435
 
 
 
778
  virtual bool get_date_result(DRIZZLE_TIME *ltime,uint fuzzydate)
 
779
  { return get_date(ltime,fuzzydate); }
436
780
  /*
437
 
    The method allows to determine nullness of a complex expression
438
 
    without fully evaluating it, instead of calling val/result*() then
 
781
    The method allows to determine nullness of a complex expression 
 
782
    without fully evaluating it, instead of calling val/result*() then 
439
783
    checking null_value. Used in Item_func_isnull/Item_func_isnotnull
440
784
    and Item_sum_count/Item_sum_count_distinct.
441
785
    Any new item which can be NULL must implement this method.
442
786
  */
443
 
  virtual bool is_null();
 
787
  virtual bool is_null() { return 0; }
444
788
 
445
789
  /*
446
790
   Make sure the null_value member has a correct value.
447
791
  */
448
 
  virtual void update_null_value ();
 
792
  virtual void update_null_value () { (void) val_int(); }
449
793
 
450
794
  /*
451
795
    Inform the item that there will be no distinction between its result
457
801
      Item_cond_and and subquery-related item) enable special optimizations
458
802
      when they are "top level".
459
803
  */
460
 
  virtual void top_level_item(void);
 
804
  virtual void top_level_item(void) {}
461
805
  /*
462
806
    set field of temporary table for Item which can be switched on temporary
463
807
    table during query processing (grouping and so on)
464
808
  */
465
 
  virtual void set_result_field(Field *field);
466
 
  virtual bool is_result_field(void);
467
 
  virtual bool is_bool_func(void);
468
 
  virtual void save_in_result_field(bool no_conversions);
469
 
 
 
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
  {}
470
814
  /*
471
815
    set value of aggregate function in case of no rows for grouping were found
472
816
  */
473
 
  virtual void no_rows_in_result(void);
474
 
  virtual Item *copy_or_same(Session *session);
475
 
 
476
 
  virtual Item *copy_andor_structure(Session *session);
477
 
 
478
 
  virtual Item *real_item(void);
479
 
  virtual Item *get_tmp_table_item(Session *session);
 
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); }
480
824
 
481
825
  static const CHARSET_INFO *default_charset();
482
 
  virtual const CHARSET_INFO *compare_collation();
483
 
 
484
 
  virtual bool walk(Item_processor processor,
485
 
                    bool walk_subquery,
486
 
                    unsigned char *arg);
487
 
 
488
 
  virtual Item* transform(Item_transformer transformer, unsigned char *arg);
 
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);
489
836
 
490
837
  /*
491
838
    This function performs a generic "compilation" of the Item tree.
492
 
    The process of compilation is assumed to go as follows:
493
 
 
 
839
    The process of compilation is assumed to go as follows: 
 
840
    
494
841
    compile()
495
 
    {
 
842
    { 
496
843
      if (this->*some_analyzer(...))
497
844
      {
498
845
        compile children if any;
501
848
    }
502
849
 
503
850
    i.e. analysis is performed top-down while transformation is done
504
 
    bottom-up.
505
 
  */
506
 
  virtual Item* compile(Item_analyzer analyzer, unsigned char **arg_p,
507
 
                        Item_transformer transformer, unsigned char *arg_t);
508
 
 
509
 
  virtual void traverse_cond(Cond_traverser traverser,
510
 
                             void *arg,
511
 
                             traverse_order order);
512
 
 
513
 
  virtual bool remove_dependence_processor(unsigned char * arg);
514
 
  virtual bool remove_fixed(unsigned char * arg);
515
 
  virtual bool cleanup_processor(unsigned char *arg);
516
 
  virtual bool collect_item_field_processor(unsigned char * arg);
517
 
  virtual bool find_item_in_field_list_processor(unsigned char *arg);
518
 
  virtual bool change_context_processor(unsigned char *context);
519
 
  virtual bool reset_query_id_processor(unsigned char *query_id_arg);
520
 
  virtual bool register_field_in_read_map(unsigned char *arg);
521
 
 
522
 
  /*
523
 
    The next function differs from the previous one that a bitmap to be updated
524
 
    is passed as unsigned char *arg.
525
 
  */
526
 
  virtual bool register_field_in_bitmap(unsigned char *arg);
527
 
  virtual bool subst_argument_checker(unsigned char **arg);
528
 
 
529
 
  /*
530
 
    Check if an expression/function is allowed for a virtual column
531
 
    SYNOPSIS
532
 
      check_vcol_func_processor()
533
 
      arg is just ignored
534
 
    RETURN VALUE
535
 
      TRUE                           Function not accepted
536
 
      FALSE                          Function accepted
537
 
  */
538
 
  virtual bool check_vcol_func_processor(unsigned char *arg);
539
 
  virtual Item *equal_fields_propagator(unsigned char * arg);
540
 
  virtual bool set_no_const_sub(unsigned char *arg);
541
 
  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; }
542
909
 
543
910
  // Row emulation
544
 
  virtual uint32_t cols();
545
 
  virtual Item* element_index(uint32_t i);
546
 
  virtual Item** addr(uint32_t i);
547
 
  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);
548
915
  // It is not row => null inside is impossible
549
 
  virtual bool null_inside();
 
916
  virtual bool null_inside() { return 0; }
550
917
  // used in row subselects to get value of elements
551
 
  virtual void bring_value();
 
918
  virtual void bring_value() {}
552
919
 
553
920
  Field *tmp_table_field_from_field_type(Table *table, bool fixed_length);
554
 
  virtual Item_field *filed_for_view_update();
 
921
  virtual Item_field *filed_for_view_update() { return 0; }
555
922
 
556
 
  virtual Item *neg_transformer(Session *session);
557
 
  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; }
558
925
  virtual Item *safe_charset_converter(const CHARSET_INFO * const tocs);
559
 
  void delete_self();
 
926
  void delete_self()
 
927
  {
 
928
    cleanup();
 
929
    delete this;
 
930
  }
560
931
 
561
932
  /*
562
933
    result_as_int64_t() must return true for Items representing DATE/TIME
565
936
    their values should be compared as integers (because the integer
566
937
    representation is more precise than the string one).
567
938
  */
568
 
  virtual bool result_as_int64_t();
 
939
  virtual bool result_as_int64_t() { return false; }
569
940
  bool is_datetime();
570
941
 
571
942
  /*
579
950
    where 'cost' is either 'double' or some structure of various cost
580
951
    parameters.
581
952
  */
582
 
  virtual bool is_expensive();
583
 
 
 
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
  }
584
959
  String *check_well_formed_result(String *str, bool send_error= 0);
585
 
  bool eq_by_collation(Item *item, bool binary_cmp,
586
 
                       const CHARSET_INFO * const cs);
587
 
 
588
 
};
589
 
 
590
 
#include <drizzled/item/basic_constant.h>
591
 
#include <drizzled/item/bin_string.h>
592
 
#include <drizzled/item/blob.h>
593
 
#include <drizzled/item/copy_string.h>
594
 
#include <drizzled/item/ident.h>
595
 
#include <drizzled/item/field.h>
596
 
 
597
 
void mark_as_dependent(Session *session,
598
 
                       st_select_lex *last,
599
 
                       st_select_lex *current,
600
 
                       Item_ident *resolved_item,
601
 
                       Item_ident *mark_item);
602
 
 
603
 
Item** resolve_ref_in_select_and_group(Session *session,
604
 
                                       Item_ident *ref,
605
 
                                       st_select_lex *select);
606
 
 
607
 
 
608
 
void mark_select_range_as_dependent(Session *session,
 
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,
609
2509
                                    st_select_lex *last_select,
610
2510
                                    st_select_lex *current_sel,
611
2511
                                    Field *found_field, Item *found_item,
612
2512
                                    Item_ident *resolved_item);
613
2513
 
614
 
extern void resolve_const_item(Session *session, Item **ref, Item *cmp_item);
 
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);
615
2517
extern bool field_is_equal_to_item(Field *field,Item *item);
616
 
 
617
 
/**
618
 
  Create field for temporary table.
619
 
 
620
 
  @param session                Thread handler
621
 
  @param table          Temporary table
622
 
  @param item           Item to create a field for
623
 
  @param type           Type of item (normally item->type)
624
 
  @param copy_func      If set and item is a function, store copy of item
625
 
                       in this array
626
 
  @param from_field    if field will be created using other field as example,
627
 
                       pointer example field will be written here
628
 
  @param default_field  If field has a default value field, store it here
629
 
  @param group          1 if we are going to do a relative group by on result
630
 
  @param modify_item    1 if item->result_field should point to new item.
631
 
                       This is relevent for how fill_record() is going to
632
 
                       work:
633
 
                       If modify_item is 1 then fill_record() will update
634
 
                       the record in the original table.
635
 
                       If modify_item is 0 then fill_record() will update
636
 
                       the temporary table
637
 
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
638
 
                             field instead of blob.
639
 
 
640
 
  @retval
641
 
    0                   on error
642
 
  @retval
643
 
    new_created field
644
 
*/
645
 
 
646
 
/* TODO: This is here for now because it needs the Item::Type. It should live
647
 
   in Field or Table once item.h is clean enough to actually include */
648
 
Field *create_tmp_field(Session *session, Table *table, Item *item,
649
 
                        Item::Type type,
650
 
                        Item ***copy_func, Field **from_field,
651
 
                        Field **def_field,
652
 
                        bool group, bool modify_item,
653
 
                        bool table_cant_handle_bit_fields,
654
 
                        bool make_copy_field,
655
 
                        uint32_t convert_blob_length);
656
 
 
657
 
#endif /* DRIZZLED_ITEM_H */