~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.h

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:59 UTC
  • mfrom: (518 drizzle)
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063259-fwbqogq7lnezct0l
Merged with trunk.

Show diffs side-by-side

added added

removed removed

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