MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
item.h
1 #ifndef ITEM_INCLUDED
2 #define ITEM_INCLUDED
3 
4 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18 
19 
20 #include "sql_priv.h" /* STRING_BUFFER_USUAL_SIZE */
21 #include "unireg.h"
22 #include "sql_const.h" /* RAND_TABLE_BIT, MAX_FIELD_NAME */
23 #include "unireg.h" // REQUIRED: for other includes
24 #include "thr_malloc.h" /* sql_calloc */
25 #include "field.h" /* Derivation */
26 #include "sql_array.h"
27 
28 class Protocol;
29 struct TABLE_LIST;
30 void item_init(void); /* Init item functions */
31 class Item_field;
32 class user_var_entry;
33 
35 
36 static inline uint32
37 char_to_byte_length_safe(uint32 char_length_arg, uint32 mbmaxlen_arg)
38 {
39  ulonglong tmp= ((ulonglong) char_length_arg) * mbmaxlen_arg;
40  return (tmp > UINT_MAX32) ? (uint32) UINT_MAX32 : (uint32) tmp;
41 }
42 
43 
44 /*
45  "Declared Type Collation"
46  A combination of collation and its derivation.
47 
48  Flags for collation aggregation modes:
49  MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
50  MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
51  (i.e. constant).
52  MY_COLL_ALLOW_CONV - allow any kind of conversion
53  (combination of the above two)
54  MY_COLL_ALLOW_NUMERIC_CONV - if all items were numbers, convert to
55  @@character_set_connection
56  MY_COLL_DISALLOW_NONE - don't allow return DERIVATION_NONE
57  (e.g. when aggregating for comparison)
58  MY_COLL_CMP_CONV - combination of MY_COLL_ALLOW_CONV
59  and MY_COLL_DISALLOW_NONE
60 */
61 
62 #define MY_COLL_ALLOW_SUPERSET_CONV 1
63 #define MY_COLL_ALLOW_COERCIBLE_CONV 2
64 #define MY_COLL_DISALLOW_NONE 4
65 #define MY_COLL_ALLOW_NUMERIC_CONV 8
66 
67 #define MY_COLL_ALLOW_CONV (MY_COLL_ALLOW_SUPERSET_CONV | MY_COLL_ALLOW_COERCIBLE_CONV)
68 #define MY_COLL_CMP_CONV (MY_COLL_ALLOW_CONV | MY_COLL_DISALLOW_NONE)
69 
70 class DTCollation {
71 public:
72  const CHARSET_INFO *collation;
73  enum Derivation derivation;
74  uint repertoire;
75 
76  void set_repertoire_from_charset(const CHARSET_INFO *cs)
77  {
78  repertoire= cs->state & MY_CS_PUREASCII ?
79  MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
80  }
81  DTCollation()
82  {
83  collation= &my_charset_bin;
84  derivation= DERIVATION_NONE;
85  repertoire= MY_REPERTOIRE_UNICODE30;
86  }
87  DTCollation(const CHARSET_INFO *collation_arg, Derivation derivation_arg)
88  {
89  collation= collation_arg;
90  derivation= derivation_arg;
91  set_repertoire_from_charset(collation_arg);
92  }
93  void set(DTCollation &dt)
94  {
95  collation= dt.collation;
96  derivation= dt.derivation;
97  repertoire= dt.repertoire;
98  }
99  void set(const CHARSET_INFO *collation_arg, Derivation derivation_arg)
100  {
101  collation= collation_arg;
102  derivation= derivation_arg;
103  set_repertoire_from_charset(collation_arg);
104  }
105  void set(const CHARSET_INFO *collation_arg,
106  Derivation derivation_arg,
107  uint repertoire_arg)
108  {
109  collation= collation_arg;
110  derivation= derivation_arg;
111  repertoire= repertoire_arg;
112  }
113  void set_numeric()
114  {
115  collation= &my_charset_numeric;
116  derivation= DERIVATION_NUMERIC;
117  repertoire= MY_REPERTOIRE_NUMERIC;
118  }
119  void set(const CHARSET_INFO *collation_arg)
120  {
121  collation= collation_arg;
122  set_repertoire_from_charset(collation_arg);
123  }
124  void set(Derivation derivation_arg)
125  { derivation= derivation_arg; }
126  void set_repertoire(uint repertoire_arg)
127  { repertoire= repertoire_arg; }
128  bool aggregate(DTCollation &dt, uint flags= 0);
129  bool set(DTCollation &dt1, DTCollation &dt2, uint flags= 0)
130  { set(dt1); return aggregate(dt2, flags); }
131  const char *derivation_name() const
132  {
133  switch(derivation)
134  {
135  case DERIVATION_NUMERIC: return "NUMERIC";
136  case DERIVATION_IGNORABLE: return "IGNORABLE";
137  case DERIVATION_COERCIBLE: return "COERCIBLE";
138  case DERIVATION_IMPLICIT: return "IMPLICIT";
139  case DERIVATION_SYSCONST: return "SYSCONST";
140  case DERIVATION_EXPLICIT: return "EXPLICIT";
141  case DERIVATION_NONE: return "NONE";
142  default: return "UNKNOWN";
143  }
144  }
145 };
146 
147 /*************************************************************************/
148 
159 {
160 private:
161  void set_or_copy(const char *str, size_t length, bool is_null_terminated)
162  {
163  if (is_null_terminated)
164  set(str, length);
165  else
166  copy(str, length);
167  }
168 public:
170  /*
171  Please do NOT add constructor Name_string(const char *str) !
172  It will involve hidden strlen() call, which can affect
173  performance negatively. Use Name_string(str, len) instead.
174  */
175  Name_string(const char *str, size_t length):
176  Simple_cstring(str, length) {}
177  Name_string(const LEX_STRING str): Simple_cstring(str) {}
178  Name_string(const char *str, size_t length, bool is_null_terminated):
180  {
181  set_or_copy(str, length, is_null_terminated);
182  }
183  Name_string(const LEX_STRING str, bool is_null_terminated):
185  {
186  set_or_copy(str.str, str.length, is_null_terminated);
187  }
191  void copy(const char *str, size_t length, const CHARSET_INFO *cs);
195  void copy(const char *str, size_t length)
196  {
197  copy(str, length, system_charset_info);
198  }
199  void copy(const char *str)
200  {
201  copy(str, (str ? strlen(str) : 0), system_charset_info);
202  }
203  void copy(const LEX_STRING lex)
204  {
205  copy(lex.str, lex.length);
206  }
207  void copy(const LEX_STRING *lex)
208  {
209  copy(lex->str, lex->length);
210  }
211  void copy(const Name_string str)
212  {
213  copy(str.ptr(), str.length());
214  }
218  bool eq(const char *str) const
219  {
220  DBUG_ASSERT(str && ptr());
221  return my_strcasecmp(system_charset_info, ptr(), str) == 0;
222  }
223  bool eq_safe(const char *str) const
224  {
225  return is_set() && str && eq(str);
226  }
230  bool eq(const Name_string name) const
231  {
232  return eq(name.ptr());
233  }
234  bool eq_safe(const Name_string name) const
235  {
236  return is_set() && name.is_set() && eq(name);
237  }
238 };
239 
240 
241 #define NAME_STRING(x) Name_string(C_STRING_WITH_LEN(x))
242 
243 
244 extern const Name_string null_name_string;
245 
246 
252 {
253 private:
254  bool m_is_autogenerated; /* indicates if name of this Item
255  was autogenerated or set by user */
256 public:
257  Item_name_string(): Name_string(), m_is_autogenerated(true)
258  { }
260  :Name_string(name), m_is_autogenerated(true)
261  { }
266  {
267  m_is_autogenerated= is_autogenerated;
268  }
272  bool is_autogenerated() const { return m_is_autogenerated; }
273  using Name_string::copy;
278  void copy(const char *str_arg, size_t length_arg, const CHARSET_INFO *cs_arg,
279  bool is_autogenerated_arg);
280 };
281 
282 
283 
284 /*************************************************************************/
285 /*
286  A framework to easily handle different return types for hybrid items
287  (hybrid item is an item whose operand can be of any type, e.g. integer,
288  real, decimal).
289 */
290 
291 struct Hybrid_type_traits;
292 
294 {
295  longlong integer;
296 
297  double real;
298  /*
299  Use two decimal buffers interchangeably to speed up += operation
300  which has no native support in decimal library.
301  Hybrid_type+= arg is implemented as dec_buf[1]= dec_buf[0] + arg.
302  The third decimal is used as a handy temporary storage.
303  */
304  my_decimal dec_buf[3];
305  int used_dec_buf_no;
306 
307  /*
308  Traits moved to a separate class to
309  a) be able to easily change object traits in runtime
310  b) they work as a differentiator for the union above
311  */
312  const Hybrid_type_traits *traits;
313 
314  Hybrid_type() {}
315  /* XXX: add traits->copy() when needed */
316  Hybrid_type(const Hybrid_type &rhs) :traits(rhs.traits) {}
317 };
318 
319 
320 /* Hybryd_type_traits interface + default implementation for REAL_RESULT */
321 
323 {
324  virtual Item_result type() const { return REAL_RESULT; }
325 
326  virtual void
327  fix_length_and_dec(Item *item, Item *arg) const;
328 
329  /* Hybrid_type operations. */
330  virtual void set_zero(Hybrid_type *val) const { val->real= 0.0; }
331  virtual void add(Hybrid_type *val, Field *f) const
332  { val->real+= f->val_real(); }
333  virtual void div(Hybrid_type *val, ulonglong u) const
334  { val->real/= ulonglong2double(u); }
335 
336  virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const
337  { return (longlong) rint(val->real); }
338  virtual double val_real(Hybrid_type *val) const { return val->real; }
339  virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const;
340  virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const;
341  static const Hybrid_type_traits *instance();
342  Hybrid_type_traits() {}
343  virtual ~Hybrid_type_traits() {}
344 };
345 
346 
348 {
349  virtual Item_result type() const { return DECIMAL_RESULT; }
350 
351  virtual void
352  fix_length_and_dec(Item *arg, Item *item) const;
353 
354  /* Hybrid_type operations. */
355  virtual void set_zero(Hybrid_type *val) const;
356  virtual void add(Hybrid_type *val, Field *f) const;
357  virtual void div(Hybrid_type *val, ulonglong u) const;
358 
359  virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const;
360  virtual double val_real(Hybrid_type *val) const;
361  virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const
362  { return &val->dec_buf[val->used_dec_buf_no]; }
363  virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const;
364  static const Hybrid_type_traits_decimal *instance();
366 };
367 
368 
370 {
371  virtual Item_result type() const { return INT_RESULT; }
372 
373  virtual void
374  fix_length_and_dec(Item *arg, Item *item) const;
375 
376  /* Hybrid_type operations. */
377  virtual void set_zero(Hybrid_type *val) const
378  { val->integer= 0; }
379  virtual void add(Hybrid_type *val, Field *f) const
380  { val->integer+= f->val_int(); }
381  virtual void div(Hybrid_type *val, ulonglong u) const
382  { val->integer/= (longlong) u; }
383 
384  virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const
385  { return val->integer; }
386  virtual double val_real(Hybrid_type *val) const
387  { return (double) val->integer; }
388  virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const
389  {
390  int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, &val->dec_buf[2]);
391  return &val->dec_buf[2];
392  }
393  virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const
394  { buf->set(val->integer, &my_charset_bin); return buf;}
395  static const Hybrid_type_traits_integer *instance();
397 };
398 
399 
400 void dummy_error_processor(THD *thd, void *data);
401 
402 void view_error_processor(THD *thd, void *data);
403 
404 /*
405  Instances of Name_resolution_context store the information necesary for
406  name resolution of Items and other context analysis of a query made in
407  fix_fields().
408 
409  This structure is a part of SELECT_LEX, a pointer to this structure is
410  assigned when an item is created (which happens mostly during parsing
411  (sql_yacc.yy)), but the structure itself will be initialized after parsing
412  is complete
413 
414  TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
415  separate SELECT_LEX which allow to remove tricks of changing this
416  structure before and after INSERT/CREATE and its SELECT to make correct
417  field name resolution.
418 */
420 {
421  /*
422  The name resolution context to search in when an Item cannot be
423  resolved in this context (the context of an outer select)
424  */
425  Name_resolution_context *outer_context;
426 
427  /*
428  List of tables used to resolve the items of this context. Usually these
429  are tables from the FROM clause of SELECT statement. The exceptions are
430  INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
431  subquery is not moved to a separate SELECT_LEX. For these types of
432  statements we have to change this member dynamically to ensure correct
433  name resolution of different parts of the statement.
434  */
435  TABLE_LIST *table_list;
436  /*
437  In most cases the two table references below replace 'table_list' above
438  for the purpose of name resolution. The first and last name resolution
439  table references allow us to search only in a sub-tree of the nested
440  join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
441  and JOIN ... ON.
442  */
443  TABLE_LIST *first_name_resolution_table;
444  /*
445  Last table to search in the list of leaf table references that begins
446  with first_name_resolution_table.
447  */
448  TABLE_LIST *last_name_resolution_table;
449 
450  /*
451  SELECT_LEX item belong to, in case of merged VIEW it can differ from
452  SELECT_LEX where item was created, so we can't use table_list/field_list
453  from there
454  */
455  st_select_lex *select_lex;
456 
457  /*
458  Processor of errors caused during Item name resolving, now used only to
459  hide underlying tables in errors about views (i.e. it substitute some
460  errors for views)
461  */
462  void (*error_processor)(THD *, void *);
463  void *error_processor_data;
464 
474 
475  /*
476  Security context of this name resolution context. It's used for views
477  and is non-zero only if the view is defined with SQL SECURITY DEFINER.
478  */
479  Security_context *security_ctx;
480 
482  :outer_context(0), table_list(0), select_lex(0),
483  error_processor_data(0),
484  security_ctx(0)
485  {}
486 
487  void init()
488  {
489  resolve_in_select_list= FALSE;
490  error_processor= &dummy_error_processor;
491  first_name_resolution_table= NULL;
492  last_name_resolution_table= NULL;
493  }
494 
495  void resolve_in_table_list_only(TABLE_LIST *tables)
496  {
497  table_list= first_name_resolution_table= tables;
498  resolve_in_select_list= FALSE;
499  }
500 
501  void process_error(THD *thd)
502  {
503  (*error_processor)(thd, error_processor_data);
504  }
505 };
506 
507 
508 /*
509  Store and restore the current state of a name resolution context.
510 */
511 
513 {
514 private:
515  TABLE_LIST *save_table_list;
516  TABLE_LIST *save_first_name_resolution_table;
517  TABLE_LIST *save_next_name_resolution_table;
518  bool save_resolve_in_select_list;
519  TABLE_LIST *save_next_local;
520 
521 public:
522  Name_resolution_context_state() {} /* Remove gcc warning */
523 
524 public:
525  /* Save the state of a name resolution context. */
526  void save_state(Name_resolution_context *context, TABLE_LIST *table_list)
527  {
528  save_table_list= context->table_list;
529  save_first_name_resolution_table= context->first_name_resolution_table;
530  save_resolve_in_select_list= context->resolve_in_select_list;
531  save_next_local= table_list->next_local;
532  save_next_name_resolution_table= table_list->next_name_resolution_table;
533  }
534 
535  /* Restore a name resolution context from saved state. */
536  void restore_state(Name_resolution_context *context, TABLE_LIST *table_list)
537  {
538  table_list->next_local= save_next_local;
539  table_list->next_name_resolution_table= save_next_name_resolution_table;
540  context->table_list= save_table_list;
541  context->first_name_resolution_table= save_first_name_resolution_table;
542  context->resolve_in_select_list= save_resolve_in_select_list;
543  }
544 
545  TABLE_LIST *get_first_name_resolution_table()
546  {
547  return save_first_name_resolution_table;
548  }
549 };
550 
551 
552 /*
553  This enum is used to report information about monotonicity of function
554  represented by Item* tree.
555  Monotonicity is defined only for Item* trees that represent table
556  partitioning expressions (i.e. have no subselects/user vars/PS parameters
557  etc etc). An Item* tree is assumed to have the same monotonicity properties
558  as its correspoinding function F:
559 
560  [signed] longlong F(field1, field2, ...) {
561  put values of field_i into table record buffer;
562  return item->val_int();
563  }
564 
565  NOTE
566  At the moment function monotonicity is not well defined (and so may be
567  incorrect) for Item trees with parameters/return types that are different
568  from INT_RESULT, may be NULL, or are unsigned.
569  It will be possible to address this issue once the related partitioning bugs
570  (BUG#16002, BUG#15447, BUG#13436) are fixed.
571 
572  The NOT_NULL enums are used in TO_DAYS, since TO_DAYS('2001-00-00') returns
573  NULL which puts those rows into the NULL partition, but
574  '2000-12-31' < '2001-00-00' < '2001-01-01'. So special handling is needed
575  for this (see Bug#20577).
576 */
577 
578 typedef enum monotonicity_info
579 {
580  NON_MONOTONIC, /* none of the below holds */
581  MONOTONIC_INCREASING, /* F() is unary and (x < y) => (F(x) <= F(y)) */
582  MONOTONIC_INCREASING_NOT_NULL, /* But only for valid/real x and y */
583  MONOTONIC_STRICT_INCREASING,/* F() is unary and (x < y) => (F(x) < F(y)) */
584  MONOTONIC_STRICT_INCREASING_NOT_NULL /* But only for valid/real x and y */
585 } enum_monotonicity_info;
586 
587 /*************************************************************************/
588 
589 class sp_rcontext;
590 
591 
593 {
594 public:
595  /*
596  Set required privileges for accessing the parameter.
597 
598  SYNOPSIS
599  set_required_privilege()
600  rw if 'rw' is true then we are going to read and set the
601  parameter, so SELECT and UPDATE privileges might be
602  required, otherwise we only reading it and SELECT
603  privilege might be required.
604  */
606  virtual ~Settable_routine_parameter() {}
607  virtual void set_required_privilege(bool rw) {};
608 
609  /*
610  Set parameter value.
611 
612  SYNOPSIS
613  set_value()
614  thd thread handle
615  ctx context to which parameter belongs (if it is local
616  variable).
617  it item which represents new value
618 
619  RETURN
620  FALSE if parameter value has been set,
621  TRUE if error has occured.
622  */
623  virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it)= 0;
624 
625  virtual void set_out_param_info(Send_field *info) {}
626 
627  virtual const Send_field *get_out_param_info() const
628  { return NULL; }
629 };
630 
631 
632 typedef bool (Item::*Item_processor) (uchar *arg);
633 /*
634  Analyzer function
635  SYNOPSIS
636  argp in/out IN: Analysis parameter
637  OUT: Parameter to be passed to the transformer
638 
639  RETURN
640  TRUE Invoke the transformer
641  FALSE Don't do it
642 
643 */
644 typedef bool (Item::*Item_analyzer) (uchar **argp);
645 typedef Item* (Item::*Item_transformer) (uchar *arg);
646 typedef void (*Cond_traverser) (const Item *item, void *arg);
647 
648 
649 class Item
650 {
651  Item(const Item &); /* Prevent use of these */
652  void operator=(Item &);
653  /* Cache of the result of is_expensive(). */
654  int8 is_expensive_cache;
655  virtual bool is_expensive_processor(uchar *arg) { return 0; }
656 
657 public:
658  static void *operator new(size_t size) throw ()
659  { return sql_alloc(size); }
660  static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
661  { return alloc_root(mem_root, size); }
662  static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
663  static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
664 
665  enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM,
666  INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM,
667  COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
668  PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM,
669  FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM,
670  SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER,
671  PARAM_ITEM, TRIGGER_FIELD_ITEM, DECIMAL_ITEM,
672  XPATH_NODESET, XPATH_NODESET_CMP,
673  VIEW_FIXER_ITEM};
674 
675  enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
676 
677  enum traverse_order { POSTFIX, PREFIX };
678 
679  /* Reuse size, only used by SP local variable assignment, otherwize 0 */
680  uint rsize;
681 
682  /*
683  str_values's main purpose is to be used to cache the value in
684  save_in_field
685  */
686  String str_value;
687 
688  Item_name_string item_name; /* Name from select */
689  Item_name_string orig_name; /* Original item name (if it was renamed)*/
690 
699  uint32 max_length; /* Maximum length, in bytes */
717  int marker;
718  uint8 decimals;
719  my_bool maybe_null; /* If item may be null */
720  my_bool null_value; /* if item is null */
721  my_bool unsigned_flag;
722  my_bool with_sum_func;
723  my_bool fixed; /* If item fixed with fix_fields */
724  DTCollation collation;
725  Item_result cmp_context; /* Comparison context */
726  protected:
727  my_bool with_subselect; /* If this item is a subselect or some
728  of its arguments is or contains a
729  subselect. Computed by fix_fields
730  and updated by update_used_tables. */
731  my_bool with_stored_program; /* If this item is a stored program
732  or some of its arguments is or
733  contains a stored program.
734  Computed by fix_fields and updated
735  by update_used_tables. */
736 
746  public:
747  // alloc & destruct is done as start of select using sql_alloc
748  Item();
749  /*
750  Constructor used by Item_field, Item_ref & aggregate (sum) functions.
751  Used for duplicating lists in processing queries with temporary
752  tables
753  Also it used for Item_cond_and/Item_cond_or for creating
754  top AND/OR structure of WHERE clause to protect it of
755  optimisation changes in prepared statements
756  */
757  Item(THD *thd, Item *item);
758  virtual ~Item()
759  {
760 #ifdef EXTRA_DEBUG
761  item_name.set(0);
762 #endif
763  } /*lint -e1509 */
764  void rename(char *new_name);
765  void init_make_field(Send_field *tmp_field,enum enum_field_types type);
766  virtual void cleanup();
767  virtual void make_field(Send_field *field);
768  virtual Field *make_string_field(TABLE *table);
769  virtual bool fix_fields(THD *, Item **);
779  virtual void fix_after_pullout(st_select_lex *parent_select,
780  st_select_lex *removed_select)
781  {};
782  /*
783  should be used in case where we are sure that we do not need
784  complete fix_fields() procedure.
785  */
786  inline void quick_fix_field() { fixed= 1; }
787  /* Function returns 1 on overflow and -1 on fatal errors */
788  type_conversion_status save_in_field_no_warnings(Field *field,
789  bool no_conversions);
797  virtual type_conversion_status save_in_field(Field *field,
798  bool no_conversions);
799  virtual void save_org_in_field(Field *field)
800  { (void) save_in_field(field, 1); }
801  virtual type_conversion_status save_safe_in_field(Field *field)
802  { return save_in_field(field, 1); }
803  virtual bool send(Protocol *protocol, String *str);
804  virtual bool eq(const Item *, bool binary_cmp) const;
805  virtual Item_result result_type() const { return REAL_RESULT; }
810  virtual enum Item_result numeric_context_result_type() const
811  {
812  if (is_temporal())
813  return decimals ? DECIMAL_RESULT : INT_RESULT;
814  if (result_type() == STRING_RESULT)
815  return REAL_RESULT;
816  return result_type();
817  }
822  inline enum Item_result temporal_with_date_as_number_result_type() const
823  {
824  return is_temporal_with_date() ?
825  (decimals ? DECIMAL_RESULT : INT_RESULT) : result_type();
826  }
827  virtual Item_result cast_to_int_type() const { return result_type(); }
828  virtual enum_field_types string_field_type() const;
829  virtual enum_field_types field_type() const;
830  virtual enum Type type() const =0;
831 
832  /*
833  Return information about function monotonicity. See comment for
834  enum_monotonicity_info for details. This function can only be called
835  after fix_fields() call.
836  */
837  virtual enum_monotonicity_info get_monotonicity_info() const
838  { return NON_MONOTONIC; }
839 
840  /*
841  Convert "func_arg $CMP$ const" half-interval into "FUNC(func_arg) $CMP2$ const2"
842 
843  SYNOPSIS
844  val_int_endpoint()
845  left_endp FALSE <=> The interval is "x < const" or "x <= const"
846  TRUE <=> The interval is "x > const" or "x >= const"
847 
848  incl_endp IN FALSE <=> the comparison is '<' or '>'
849  TRUE <=> the comparison is '<=' or '>='
850  OUT The same but for the "F(x) $CMP$ F(const)" comparison
851 
852  DESCRIPTION
853  This function is defined only for unary monotonic functions. The caller
854  supplies the source half-interval
855 
856  x $CMP$ const
857 
858  The value of const is supplied implicitly as the value this item's
859  argument, the form of $CMP$ comparison is specified through the
860  function's arguments. The calle returns the result interval
861 
862  F(x) $CMP2$ F(const)
863 
864  passing back F(const) as the return value, and the form of $CMP2$
865  through the out parameter. NULL values are assumed to be comparable and
866  be less than any non-NULL values.
867 
868  RETURN
869  The output range bound, which equal to the value of val_int()
870  - If the value of the function is NULL then the bound is the
871  smallest possible value of LONGLONG_MIN
872  */
873  virtual longlong val_int_endpoint(bool left_endp, bool *incl_endp)
874  { DBUG_ASSERT(0); return 0; }
875 
876 
877  /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
878  /*
879  Return double precision floating point representation of item.
880 
881  SYNOPSIS
882  val_real()
883 
884  RETURN
885  In case of NULL value return 0.0 and set null_value flag to TRUE.
886  If value is not null null_value flag will be reset to FALSE.
887  */
888  virtual double val_real()=0;
889  /*
890  Return integer representation of item.
891 
892  SYNOPSIS
893  val_int()
894 
895  RETURN
896  In case of NULL value return 0 and set null_value flag to TRUE.
897  If value is not null null_value flag will be reset to FALSE.
898  */
899  virtual longlong val_int()=0;
903  virtual longlong val_date_temporal();
907  virtual longlong val_time_temporal();
913  {
914  if (field_type() == MYSQL_TYPE_TIME)
915  return val_time_temporal();
916  DBUG_ASSERT(is_temporal_with_date());
917  return val_date_temporal();
918  }
924  longlong val_temporal_with_round(enum_field_types type, uint8 dec);
925 
926  /*
927  This is just a shortcut to avoid the cast. You should still use
928  unsigned_flag to check the sign of the item.
929  */
930  inline ulonglong val_uint() { return (ulonglong) val_int(); }
931  /*
932  Return string representation of this item object.
933 
934  SYNOPSIS
935  val_str()
936  str an allocated buffer this or any nested Item object can use to
937  store return value of this method.
938 
939  NOTE
940  Buffer passed via argument should only be used if the item itself
941  doesn't have an own String buffer. In case when the item maintains
942  it's own string buffer, it's preferable to return it instead to
943  minimize number of mallocs/memcpys.
944  The caller of this method can modify returned string, but only in case
945  when it was allocated on heap, (is_alloced() is true). This allows
946  the caller to efficiently use a buffer allocated by a child without
947  having to allocate a buffer of it's own. The buffer, given to
948  val_str() as argument, belongs to the caller and is later used by the
949  caller at it's own choosing.
950  A few implications from the above:
951  - unless you return a string object which only points to your buffer
952  but doesn't manages it you should be ready that it will be
953  modified.
954  - even for not allocated strings (is_alloced() == false) the caller
955  can change charset (see Item_func_{typecast/binary}. XXX: is this
956  a bug?
957  - still you should try to minimize data copying and return internal
958  object whenever possible.
959 
960  RETURN
961  In case of NULL value return 0 (NULL pointer) and set null_value flag
962  to TRUE.
963  If value is not null null_value flag will be reset to FALSE.
964  */
965  virtual String *val_str(String *str)=0;
966 
967  /*
968  Returns string representation of this item in ASCII format.
969 
970  SYNOPSIS
971  val_str_ascii()
972  str - similar to val_str();
973 
974  NOTE
975  This method is introduced for performance optimization purposes.
976 
977  1. val_str() result of some Items in string context
978  depends on @@character_set_results.
979  @@character_set_results can be set to a "real multibyte" character
980  set like UCS2, UTF16, UTF32. (We'll use only UTF32 in the examples
981  below for convenience.)
982 
983  So the default string result of such functions
984  in these circumstances is real multi-byte character set, like UTF32.
985 
986  For example, all numbers in string context
987  return result in @@character_set_results:
988 
989  SELECT CONCAT(20010101); -> UTF32
990 
991  We do sprintf() first (to get ASCII representation)
992  and then convert to UTF32;
993 
994  So these kind "data sources" can use ASCII representation
995  internally, but return multi-byte data only because
996  @@character_set_results wants so.
997  Therefore, conversion from ASCII to UTF32 is applied internally.
998 
999 
1000  2. Some other functions need in fact ASCII input.
1001 
1002  For example,
1003  inet_aton(), GeometryFromText(), Convert_TZ(), GET_FORMAT().
1004 
1005  Similar, fields of certain type, like DATE, TIME,
1006  when you insert string data into them, expect in fact ASCII input.
1007  If they get non-ASCII input, for example UTF32, they
1008  convert input from UTF32 to ASCII, and then use ASCII
1009  representation to do further processing.
1010 
1011 
1012  3. Now imagine we pass result of a data source of the first type
1013  to a data destination of the second type.
1014 
1015  What happens:
1016  a. data source converts data from ASCII to UTF32, because
1017  @@character_set_results wants so and passes the result to
1018  data destination.
1019  b. data destination gets UTF32 string.
1020  c. data destination converts UTF32 string to ASCII,
1021  because it needs ASCII representation to be able to handle data
1022  correctly.
1023 
1024  As a result we get two steps of unnecessary conversion:
1025  From ASCII to UTF32, then from UTF32 to ASCII.
1026 
1027  A better way to handle these situations is to pass ASCII
1028  representation directly from the source to the destination.
1029 
1030  This is why val_str_ascii() introduced.
1031 
1032  RETURN
1033  Similar to val_str()
1034  */
1035  virtual String *val_str_ascii(String *str);
1036 
1037  /*
1038  Return decimal representation of item with fixed point.
1039 
1040  SYNOPSIS
1041  val_decimal()
1042  decimal_buffer buffer which can be used by Item for returning value
1043  (but can be not)
1044 
1045  NOTE
1046  Returned value should not be changed if it is not the same which was
1047  passed via argument.
1048 
1049  RETURN
1050  Return pointer on my_decimal (it can be other then passed via argument)
1051  if value is not NULL (null_value flag will be reset to FALSE).
1052  In case of NULL value it return 0 pointer and set null_value flag
1053  to TRUE.
1054  */
1055  virtual my_decimal *val_decimal(my_decimal *decimal_buffer)= 0;
1056  /*
1057  Return boolean value of item.
1058 
1059  RETURN
1060  FALSE value is false or NULL
1061  TRUE value is true (not equal to 0)
1062  */
1063  virtual bool val_bool();
1064  virtual String *val_nodeset(String*) { return 0; }
1065 
1066 protected:
1067  /* Helper functions, see item_sum.cc */
1068  String *val_string_from_real(String *str);
1069  String *val_string_from_int(String *str);
1070  String *val_string_from_decimal(String *str);
1071  String *val_string_from_date(String *str);
1072  String *val_string_from_datetime(String *str);
1073  String *val_string_from_time(String *str);
1074  my_decimal *val_decimal_from_real(my_decimal *decimal_value);
1075  my_decimal *val_decimal_from_int(my_decimal *decimal_value);
1076  my_decimal *val_decimal_from_string(my_decimal *decimal_value);
1077  my_decimal *val_decimal_from_date(my_decimal *decimal_value);
1078  my_decimal *val_decimal_from_time(my_decimal *decimal_value);
1079  longlong val_int_from_decimal();
1080  longlong val_int_from_date();
1081  longlong val_int_from_time();
1082  longlong val_int_from_datetime();
1083  double val_real_from_decimal();
1084 
1088  bool get_date_from_string(MYSQL_TIME *ltime, uint flags);
1092  bool get_date_from_real(MYSQL_TIME *ltime, uint flags);
1096  bool get_date_from_decimal(MYSQL_TIME *ltime, uint flags);
1100  bool get_date_from_int(MYSQL_TIME *ltime, uint flags);
1104  bool get_date_from_time(MYSQL_TIME *ltime);
1105 
1109  bool get_date_from_numeric(MYSQL_TIME *ltime, uint fuzzydate);
1110 
1114  bool get_date_from_non_temporal(MYSQL_TIME *ltime, uint fuzzydate);
1115 
1119  bool get_time_from_string(MYSQL_TIME *ltime);
1123  bool get_time_from_real(MYSQL_TIME *ltime);
1127  bool get_time_from_decimal(MYSQL_TIME *ltime);
1131  bool get_time_from_int(MYSQL_TIME *ltime);
1135  bool get_time_from_date(MYSQL_TIME *ltime);
1139  bool get_time_from_datetime(MYSQL_TIME *ltime);
1140 
1144  bool get_time_from_numeric(MYSQL_TIME *ltime);
1145 
1150 
1151 
1152 public:
1153 
1154  type_conversion_status save_time_in_field(Field *field);
1155  type_conversion_status save_date_in_field(Field *field);
1156  type_conversion_status save_str_value_in_field(Field *field, String *result);
1157 
1158  virtual Field *get_tmp_table_field() { return 0; }
1159  /* This is also used to create fields in CREATE ... SELECT: */
1160  virtual Field *tmp_table_field(TABLE *t_arg) { return 0; }
1161  virtual const char *full_name() const
1162  {
1163  return item_name.is_set() ? item_name.ptr() : "???";
1164  }
1165 
1166  /*
1167  *result* family of methods is analog of *val* family (see above) but
1168  return value of result_field of item if it is present. If Item have not
1169  result field, it return val(). This methods set null_value flag in same
1170  way as *val* methods do it.
1171  */
1172  virtual double val_result() { return val_real(); }
1173  virtual longlong val_int_result() { return val_int(); }
1177  virtual longlong val_time_temporal_result() { return val_time_temporal(); }
1181  virtual longlong val_date_temporal_result() { return val_date_temporal(); }
1182  virtual String *str_result(String* tmp) { return val_str(tmp); }
1183  virtual my_decimal *val_decimal_result(my_decimal *val)
1184  { return val_decimal(val); }
1185  virtual bool val_bool_result() { return val_bool(); }
1186  virtual bool is_null_result() { return is_null(); }
1187 
1188  /* bit map of tables used by item */
1189  virtual table_map used_tables() const { return (table_map) 0L; }
1200  virtual table_map resolved_used_tables() const
1201  {
1202  // As this is the level this item was resolved on, it cannot be outer:
1203  DBUG_ASSERT(!(used_tables() & OUTER_REF_TABLE_BIT));
1204 
1205  return used_tables();
1206  }
1207  /*
1208  Return table map of tables that can't be NULL tables (tables that are
1209  used in a context where if they would contain a NULL row generated
1210  by a LEFT or RIGHT join, the item would not be true).
1211  This expression is used on WHERE item to determinate if a LEFT JOIN can be
1212  converted to a normal join.
1213  Generally this function should return used_tables() if the function
1214  would return null if any of the arguments are null
1215  As this is only used in the beginning of optimization, the value don't
1216  have to be updated in update_used_tables()
1217  */
1218  virtual table_map not_null_tables() const { return used_tables(); }
1219  /*
1220  Returns true if this is a simple constant item like an integer, not
1221  a constant expression. Used in the optimizer to propagate basic constants.
1222  */
1223  virtual bool basic_const_item() const { return 0; }
1224  /* cloning of constant items (0 if it is not const) */
1225  virtual Item *clone_item() { return 0; }
1226  virtual cond_result eq_cmp_result() const { return COND_OK; }
1227  inline uint float_length(uint decimals_par) const
1228  { return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;}
1229  virtual uint decimal_precision() const;
1230  inline int decimal_int_part() const
1231  { return my_decimal_int_part(decimal_precision(), decimals); }
1235  virtual uint time_precision();
1239  virtual uint datetime_precision();
1240  /*
1241  Returns true if this is constant (during query execution, i.e. its value
1242  will not change until next fix_fields) and its value is known.
1243  When the default implementation of used_tables() is effective, this
1244  function will always return true (because used_tables() is empty).
1245  */
1246  virtual bool const_item() const
1247  {
1248  if (used_tables() == 0)
1249  return can_be_evaluated_now();
1250  return false;
1251  }
1252  /*
1253  Returns true if this is constant but its value may be not known yet.
1254  (Can be used for parameters of prep. stmts or of stored procedures.)
1255  */
1256  virtual bool const_during_execution() const
1257  { return (used_tables() & ~PARAM_TABLE_BIT) == 0; }
1258 
1270  virtual inline void print(String *str, enum_query_type query_type)
1271  {
1272  str->append(full_name());
1273  }
1274 
1275  void print_item_w_name(String *, enum_query_type query_type);
1282  void print_for_order(String *str, enum_query_type query_type,
1283  bool used_alias);
1284 
1285  virtual void update_used_tables() {}
1286  virtual void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
1287  List<Item> &fields) {}
1288  /* Called for items that really have to be split */
1289  void split_sum_func2(THD *thd, Ref_ptr_array ref_pointer_array,
1290  List<Item> &fields,
1291  Item **ref, bool skip_registered);
1292  virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate)= 0;
1293  virtual bool get_time(MYSQL_TIME *ltime)= 0;
1299  virtual bool get_timeval(struct timeval *tm, int *warnings);
1300  virtual bool get_date_result(MYSQL_TIME *ltime,uint fuzzydate)
1301  { return get_date(ltime,fuzzydate); }
1302  /*
1303  The method allows to determine nullness of a complex expression
1304  without fully evaluating it, instead of calling val/result*() then
1305  checking null_value. Used in Item_func_isnull/Item_func_isnotnull
1306  and Item_sum_count/Item_sum_count_distinct.
1307  Any new item which can be NULL must implement this method.
1308  */
1309  virtual bool is_null() { return 0; }
1310 
1311  /*
1312  Make sure the null_value member has a correct value.
1313  */
1314  virtual void update_null_value () { (void) val_int(); }
1315 
1316  /*
1317  Inform the item that there will be no distinction between its result
1318  being FALSE or NULL.
1319 
1320  NOTE
1321  This function will be called for eg. Items that are top-level AND-parts
1322  of the WHERE clause. Items implementing this function (currently
1323  Item_cond_and and subquery-related item) enable special optimizations
1324  when they are "top level".
1325  */
1326  virtual void top_level_item() {}
1327  /*
1328  set field of temporary table for Item which can be switched on temporary
1329  table during query processing (grouping and so on)
1330  */
1331  virtual void set_result_field(Field *field) {}
1332  virtual bool is_result_field() { return 0; }
1333  virtual bool is_bool_func() { return 0; }
1334  virtual void save_in_result_field(bool no_conversions) {}
1335  /*
1336  Set value of aggregate function in case of no rows for grouping were found.
1337  Also used for subqueries with outer references in SELECT list.
1338  */
1339  virtual void no_rows_in_result() {}
1340  virtual Item *copy_or_same(THD *thd) { return this; }
1346  virtual Item *copy_andor_structure(THD *thd, bool real_items= false)
1347  { return real_items ? real_item() : this; }
1348  virtual Item *real_item() { return this; }
1349  virtual Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
1350 
1351  static const CHARSET_INFO *default_charset();
1352  virtual const CHARSET_INFO *compare_collation() { return NULL; }
1353 
1354  /*
1355  For backward compatibility, to make numeric
1356  data types return "binary" charset in client-side metadata.
1357  */
1358  virtual const CHARSET_INFO *charset_for_protocol(void) const
1359  {
1360  return result_type() == STRING_RESULT ? collation.collation :
1361  &my_charset_bin;
1362  };
1363 
1364  virtual bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
1365  {
1366  return (this->*processor)(arg);
1367  }
1368 
1369  virtual Item* transform(Item_transformer transformer, uchar *arg);
1370 
1371  /*
1372  This function performs a generic "compilation" of the Item tree.
1373  The process of compilation is assumed to go as follows:
1374 
1375  compile()
1376  {
1377  if (this->*some_analyzer(...))
1378  {
1379  compile children if any;
1380  return this->*some_transformer(...);
1381  }
1382  else
1383  return this;
1384  }
1385 
1386  i.e. analysis is performed top-down while transformation is done
1387  bottom-up. If no transformation is applied, the item is returned unchanged.
1388  A transformation error is indicated by returning a NULL pointer. Notice
1389  that the analyzer function should never cause an error.
1390  */
1391  virtual Item* compile(Item_analyzer analyzer, uchar **arg_p,
1392  Item_transformer transformer, uchar *arg_t)
1393  {
1394  if ((this->*analyzer) (arg_p))
1395  return ((this->*transformer) (arg_t));
1396  return this;
1397  }
1398 
1399  virtual void traverse_cond(Cond_traverser traverser,
1400  void *arg, traverse_order order)
1401  {
1402  (*traverser)(this, arg);
1403  }
1404 
1405  /*
1406  This is used to get the most recent version of any function in
1407  an item tree. The version is the version where a MySQL function
1408  was introduced in. So any function which is added should use
1409  this function and set the int_arg to maximum of the input data
1410  and their own version info.
1411  */
1412  virtual bool intro_version(uchar *int_arg) { return 0; }
1413 
1414  virtual bool remove_dependence_processor(uchar * arg) { return 0; }
1415  virtual bool remove_fixed(uchar * arg) { fixed= 0; return 0; }
1416  virtual bool cleanup_processor(uchar *arg);
1417  virtual bool collect_item_field_processor(uchar * arg) { return 0; }
1418  virtual bool add_field_to_set_processor(uchar * arg) { return 0; }
1419 
1427  virtual bool remove_column_from_bitmap(uchar *arg) { return false; }
1428  virtual bool find_item_in_field_list_processor(uchar *arg) { return 0; }
1429  virtual bool change_context_processor(uchar *context) { return 0; }
1430  virtual bool reset_query_id_processor(uchar *query_id_arg) { return 0; }
1431  virtual bool find_item_processor(uchar *arg) { return this == (void *) arg; }
1432  virtual bool register_field_in_read_map(uchar *arg) { return 0; }
1433  virtual bool inform_item_in_cond_of_tab(uchar *join_tab_index) { return false; }
1440  virtual bool clean_up_after_removal(uchar *arg) { return false; }
1441 
1442  virtual bool cache_const_expr_analyzer(uchar **arg);
1443  virtual Item* cache_const_expr_transformer(uchar *arg);
1444 
1453  virtual bool item_field_by_name_analyzer(uchar **arg) { return true; };
1454 
1464  virtual Item* item_field_by_name_transformer(uchar *arg) { return this; }
1465 
1466  virtual bool equality_substitution_analyzer(uchar **arg) { return false; }
1467 
1468  virtual Item* equality_substitution_transformer(uchar *arg) { return this; }
1469 
1470  /*
1471  Check if a partition function is allowed
1472  SYNOPSIS
1473  check_partition_func_processor()
1474  int_arg Ignored
1475  RETURN VALUE
1476  TRUE Partition function not accepted
1477  FALSE Partition function accepted
1478 
1479  DESCRIPTION
1480  check_partition_func_processor is used to check if a partition function
1481  uses an allowed function. An allowed function will always ensure that
1482  X=Y guarantees that also part_function(X)=part_function(Y) where X is
1483  a set of partition fields and so is Y. The problems comes mainly from
1484  character sets where two equal strings can be quite unequal. E.g. the
1485  german character for double s is equal to 2 s.
1486 
1487  The default is that an item is not allowed
1488  in a partition function. Allowed functions
1489  can never depend on server version, they cannot depend on anything
1490  related to the environment. They can also only depend on a set of
1491  fields in the table itself. They cannot depend on other tables and
1492  cannot contain any queries and cannot contain udf's or similar.
1493  If a new Item class is defined and it inherits from a class that is
1494  allowed in a partition function then it is very important to consider
1495  whether this should be inherited to the new class. If not the function
1496  below should be defined in the new Item class.
1497 
1498  The general behaviour is that most integer functions are allowed.
1499  If the partition function contains any multi-byte collations then
1500  the function check_part_func_fields will report an error on the
1501  partition function independent of what functions are used. So the
1502  only character sets allowed are single character collation and
1503  even for those only a limited set of functions are allowed. The
1504  problem with multi-byte collations is that almost every string
1505  function has the ability to change things such that two strings
1506  that are equal will not be equal after manipulated by a string
1507  function. E.g. two strings one contains a double s, there is a
1508  special german character that is equal to two s. Now assume a
1509  string function removes one character at this place, then in
1510  one the double s will be removed and in the other there will
1511  still be one s remaining and the strings are no longer equal
1512  and thus the partition function will not sort equal strings into
1513  the same partitions.
1514 
1515  So the check if a partition function is valid is two steps. First
1516  check that the field types are valid, next check that the partition
1517  function is valid. The current set of partition functions valid
1518  assumes that there are no multi-byte collations amongst the partition
1519  fields.
1520  */
1521  virtual bool check_partition_func_processor(uchar *bool_arg) { return TRUE;}
1522  virtual bool subst_argument_checker(uchar **arg)
1523  {
1524  if (*arg)
1525  *arg= NULL;
1526  return TRUE;
1527  }
1528  virtual bool explain_subquery_checker(uchar **arg) { return true; }
1529  virtual Item *explain_subquery_propagator(uchar *arg) { return this; }
1530 
1531  virtual Item *equal_fields_propagator(uchar * arg) { return this; }
1532  virtual bool set_no_const_sub(uchar *arg) { return FALSE; }
1533  virtual Item *replace_equal_field(uchar * arg) { return this; }
1534  /*
1535  Check if an expression value has allowed arguments, like DATE/DATETIME
1536  for date functions. Also used by partitioning code to reject
1537  timezone-dependent expressions in a (sub)partitioning function.
1538  */
1539  virtual bool check_valid_arguments_processor(uchar *bool_arg)
1540  {
1541  return FALSE;
1542  }
1543 
1556  virtual bool find_function_processor (uchar *arg)
1557  {
1558  return FALSE;
1559  }
1560 
1561  /*
1562  For SP local variable returns pointer to Item representing its
1563  current value and pointer to current Item otherwise.
1564  */
1565  virtual Item *this_item() { return this; }
1566  virtual const Item *this_item() const { return this; }
1567 
1568  /*
1569  For SP local variable returns address of pointer to Item representing its
1570  current value and pointer passed via parameter otherwise.
1571  */
1572  virtual Item **this_item_addr(THD *thd, Item **addr_arg) { return addr_arg; }
1573 
1574  // Row emulation
1575  virtual uint cols() { return 1; }
1576  virtual Item* element_index(uint i) { return this; }
1577  virtual Item** addr(uint i) { return 0; }
1578  virtual bool check_cols(uint c);
1579  // It is not row => null inside is impossible
1580  virtual bool null_inside() { return 0; }
1581  // used in row subselects to get value of elements
1582  virtual void bring_value() {}
1583 
1584  Field *tmp_table_field_from_field_type(TABLE *table, bool fixed_length);
1585  virtual Item_field *field_for_view_update() { return 0; }
1586 
1587  virtual Item *neg_transformer(THD *thd) { return NULL; }
1588  virtual Item *update_value_transformer(uchar *select_arg) { return this; }
1589  virtual Item *safe_charset_converter(const CHARSET_INFO *tocs);
1590  void delete_self()
1591  {
1592  cleanup();
1593  delete this;
1594  }
1595 
1596  virtual bool is_splocal() { return 0; } /* Needed for error checking */
1597 
1598  /*
1599  Return Settable_routine_parameter interface of the Item. Return 0
1600  if this Item is not Settable_routine_parameter.
1601  */
1602  virtual Settable_routine_parameter *get_settable_routine_parameter()
1603  {
1604  return 0;
1605  }
1606  inline bool is_temporal_with_date() const
1607  {
1608  return is_temporal_type_with_date(field_type());
1609  }
1610  inline bool is_temporal_with_date_and_time() const
1611  {
1612  return is_temporal_type_with_date_and_time(field_type());
1613  }
1614  inline bool is_temporal_with_time() const
1615  {
1616  return is_temporal_type_with_time(field_type());
1617  }
1618  inline bool is_temporal() const
1619  {
1620  return is_temporal_type(field_type());
1621  }
1631  inline bool has_compatible_context(Item *item) const
1632  {
1633  /* Same context. */
1634  if (cmp_context == (Item_result)-1 || item->cmp_context == cmp_context)
1635  return TRUE;
1636  /* DATETIME comparison context. */
1637  if (is_temporal_with_date())
1638  return item->is_temporal_with_date() ||
1639  item->cmp_context == STRING_RESULT;
1640  if (item->is_temporal_with_date())
1641  return is_temporal_with_date() || cmp_context == STRING_RESULT;
1642  return FALSE;
1643  }
1644  virtual Field::geometry_type get_geometry_type() const
1645  { return Field::GEOM_GEOMETRY; };
1646  String *check_well_formed_result(String *str, bool send_error= 0);
1647  bool eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO *cs);
1648 
1649  /*
1650  Test whether an expression is expensive to compute. Used during
1651  optimization to avoid computing expensive expressions during this
1652  phase. Also used to force temp tables when sorting on expensive
1653  functions.
1654  TODO:
1655  Normally we should have a method:
1656  cost Item::execution_cost(),
1657  where 'cost' is either 'double' or some structure of various cost
1658  parameters.
1659  */
1660  virtual bool is_expensive()
1661  {
1662  if (is_expensive_cache < 0)
1663  is_expensive_cache= walk(&Item::is_expensive_processor, 0, (uchar*)0);
1664  return test(is_expensive_cache);
1665  }
1666  virtual bool can_be_evaluated_now() const;
1667  uint32 max_char_length() const
1668  { return max_length / collation.collation->mbmaxlen; }
1669  void fix_length_and_charset(uint32 max_char_length_arg,
1670  const CHARSET_INFO *cs)
1671  {
1672  max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1673  collation.collation= cs;
1674  }
1675  void fix_char_length(uint32 max_char_length_arg)
1676  {
1677  max_length= char_to_byte_length_safe(max_char_length_arg,
1678  collation.collation->mbmaxlen);
1679  }
1680  void fix_char_length_ulonglong(ulonglong max_char_length_arg)
1681  {
1682  ulonglong max_result_length= max_char_length_arg *
1683  collation.collation->mbmaxlen;
1684  if (max_result_length >= MAX_BLOB_WIDTH)
1685  {
1686  max_length= MAX_BLOB_WIDTH;
1687  maybe_null= 1;
1688  }
1689  else
1690  max_length= (uint32) max_result_length;
1691  }
1692  void fix_length_and_charset_datetime(uint32 max_char_length_arg)
1693  {
1694  collation.set(&my_charset_numeric, DERIVATION_NUMERIC, MY_REPERTOIRE_ASCII);
1695  fix_char_length(max_char_length_arg);
1696  }
1697  void fix_length_and_dec_and_charset_datetime(uint32 max_char_length_arg,
1698  uint8 dec_arg)
1699  {
1700  decimals= dec_arg;
1701  fix_length_and_charset_datetime(max_char_length_arg +
1702  (dec_arg ? dec_arg + 1 : 0));
1703  }
1704  /*
1705  Return TRUE if the item points to a column of an outer-joined table.
1706  */
1707  virtual bool is_outer_field() const { DBUG_ASSERT(fixed); return FALSE; }
1708 
1720  bool is_blob_field() const;
1721 
1725  virtual bool has_subquery() const { return with_subselect; }
1726  virtual bool has_stored_program() const { return with_stored_program; }
1728  virtual bool created_by_in2exists() const { return false; }
1729 };
1730 
1731 
1732 class sp_head;
1733 
1734 
1736 {
1737  table_map used_table_map;
1738 public:
1739  Item_basic_constant(): Item(), used_table_map(0) {};
1740  void set_used_tables(table_map map) { used_table_map= map; }
1741  table_map used_tables() const { return used_table_map; }
1742  /* to prevent drop fixed flag (no need parent cleanup call) */
1743  void cleanup()
1744  {
1745  /*
1746  Restore the original field name as it might not have been allocated
1747  in the statement memory. If the name is auto generated, it must be
1748  done again between subsequent executions of a prepared statement.
1749  */
1750  if (orig_name.is_set())
1751  item_name= orig_name;
1752  }
1753 };
1754 
1755 
1756 /*****************************************************************************
1757  The class is a base class for representation of stored routine variables in
1758  the Item-hierarchy. There are the following kinds of SP-vars:
1759  - local variables (Item_splocal);
1760  - CASE expression (Item_case_expr);
1761 *****************************************************************************/
1762 
1763 class Item_sp_variable :public Item
1764 {
1765 protected:
1766  /*
1767  THD, which is stored in fix_fields() and is used in this_item() to avoid
1768  current_thd use.
1769  */
1770  THD *m_thd;
1771 
1772 public:
1773  Name_string m_name;
1774 
1775 public:
1776 #ifndef DBUG_OFF
1777  /*
1778  Routine to which this Item_splocal belongs. Used for checking if correct
1779  runtime context is used for variable handling.
1780  */
1781  sp_head *m_sp;
1782 #endif
1783 
1784 public:
1785  Item_sp_variable(const Name_string sp_var_name);
1786 
1787 public:
1788  bool fix_fields(THD *thd, Item **);
1789 
1790  double val_real();
1791  longlong val_int();
1792  String *val_str(String *sp);
1793  my_decimal *val_decimal(my_decimal *decimal_value);
1794  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
1795  bool get_time(MYSQL_TIME *ltime);
1796  bool is_null();
1797 
1798 public:
1799  inline void make_field(Send_field *field);
1800  inline type_conversion_status save_in_field(Field *field,
1801  bool no_conversions);
1802  inline bool send(Protocol *protocol, String *str);
1803 };
1804 
1805 /*****************************************************************************
1806  Item_sp_variable inline implementation.
1807 *****************************************************************************/
1808 
1809 inline void Item_sp_variable::make_field(Send_field *field)
1810 {
1811  Item *it= this_item();
1812  it->item_name.copy(item_name.is_set() ? item_name : m_name);
1813  it->make_field(field);
1814 }
1815 
1816 inline type_conversion_status
1817 Item_sp_variable::save_in_field(Field *field, bool no_conversions)
1818 {
1819  return this_item()->save_in_field(field, no_conversions);
1820 }
1821 
1822 inline bool Item_sp_variable::send(Protocol *protocol, String *str)
1823 {
1824  return this_item()->send(protocol, str);
1825 }
1826 
1827 
1828 /*****************************************************************************
1829  A reference to local SP variable (incl. reference to SP parameter), used in
1830  runtime.
1831 *****************************************************************************/
1832 
1835 {
1836  uint m_var_idx;
1837 
1838  Type m_type;
1839  Item_result m_result_type;
1840  enum_field_types m_field_type;
1841 public:
1842  /*
1843  If this variable is a parameter in LIMIT clause.
1844  Used only during NAME_CONST substitution, to not append
1845  NAME_CONST to the resulting query and thus not break
1846  the slave.
1847  */
1848  bool limit_clause_param;
1849  /*
1850  Position of this reference to SP variable in the statement (the
1851  statement itself is in sp_instr_stmt::m_query).
1852  This is valid only for references to SP variables in statements,
1853  excluding DECLARE CURSOR statement. It is used to replace references to SP
1854  variables with NAME_CONST calls when putting statements into the binary
1855  log.
1856  Value of 0 means that this object doesn't corresponding to reference to
1857  SP variable in query text.
1858  */
1859  uint pos_in_query;
1860  /*
1861  Byte length of SP variable name in the statement (see pos_in_query).
1862  The value of this field may differ from the name_length value because
1863  name_length contains byte length of UTF8-encoded item name, but
1864  the query string (see sp_instr_stmt::m_query) is currently stored with
1865  a charset from the SET NAMES statement.
1866  */
1867  uint len_in_query;
1868 
1869  Item_splocal(const Name_string sp_var_name, uint sp_var_idx,
1870  enum_field_types sp_var_type,
1871  uint pos_in_q= 0, uint len_in_q= 0);
1872 
1873  bool is_splocal() { return 1; } /* Needed for error checking */
1874 
1875  Item *this_item();
1876  const Item *this_item() const;
1877  Item **this_item_addr(THD *thd, Item **);
1878 
1879  virtual void print(String *str, enum_query_type query_type);
1880 
1881 public:
1882  inline uint get_var_idx() const;
1883 
1884  inline enum Type type() const;
1885  inline Item_result result_type() const;
1886  inline enum_field_types field_type() const { return m_field_type; }
1887 
1888 private:
1889  bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
1890 
1891 public:
1892  Settable_routine_parameter *get_settable_routine_parameter()
1893  {
1894  return this;
1895  }
1896 };
1897 
1898 /*****************************************************************************
1899  Item_splocal inline implementation.
1900 *****************************************************************************/
1901 
1902 inline uint Item_splocal::get_var_idx() const
1903 {
1904  return m_var_idx;
1905 }
1906 
1907 inline enum Item::Type Item_splocal::type() const
1908 {
1909  return m_type;
1910 }
1911 
1912 inline Item_result Item_splocal::result_type() const
1913 {
1914  return m_result_type;
1915 }
1916 
1917 
1918 /*****************************************************************************
1919  A reference to case expression in SP, used in runtime.
1920 *****************************************************************************/
1921 
1923 {
1924 public:
1925  Item_case_expr(uint case_expr_id);
1926 
1927 public:
1928  Item *this_item();
1929  const Item *this_item() const;
1930  Item **this_item_addr(THD *thd, Item **);
1931 
1932  inline enum Type type() const;
1933  inline Item_result result_type() const;
1934 
1935 public:
1936  /*
1937  NOTE: print() is intended to be used from views and for debug.
1938  Item_case_expr can not occur in views, so here it is only for debug
1939  purposes.
1940  */
1941  virtual void print(String *str, enum_query_type query_type);
1942 
1943 private:
1944  uint m_case_expr_id;
1945 };
1946 
1947 /*****************************************************************************
1948  Item_case_expr inline implementation.
1949 *****************************************************************************/
1950 
1951 inline enum Item::Type Item_case_expr::type() const
1952 {
1953  return this_item()->type();
1954 }
1955 
1956 inline Item_result Item_case_expr::result_type() const
1957 {
1958  return this_item()->result_type();
1959 }
1960 
1961 
1962 /*
1963  NAME_CONST(given_name, const_value).
1964  This 'function' has all properties of the supplied const_value (which is
1965  assumed to be a literal constant), and the name given_name.
1966 
1967  This is used to replace references to SP variables when we write PROCEDURE
1968  statements into the binary log.
1969 
1970  TODO
1971  Together with Item_splocal and Item::this_item() we can actually extract
1972  common a base of this class and Item_splocal. Maybe it is possible to
1973  extract a common base with class Item_ref, too.
1974 */
1975 
1976 class Item_name_const : public Item
1977 {
1978  Item *value_item;
1979  Item *name_item;
1980  bool valid_args;
1981 public:
1982  Item_name_const(Item *name_arg, Item *val);
1983 
1984  bool fix_fields(THD *, Item **);
1985 
1986  enum Type type() const;
1987  double val_real();
1988  longlong val_int();
1989  String *val_str(String *sp);
1990  my_decimal *val_decimal(my_decimal *);
1991  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
1992  bool get_time(MYSQL_TIME *ltime);
1993  bool is_null();
1994  virtual void print(String *str, enum_query_type query_type);
1995 
1996  Item_result result_type() const
1997  {
1998  return value_item->result_type();
1999  }
2000 
2001  type_conversion_status save_in_field(Field *field, bool no_conversions)
2002  {
2003  return value_item->save_in_field(field, no_conversions);
2004  }
2005 
2006  bool send(Protocol *protocol, String *str)
2007  {
2008  return value_item->send(protocol, str);
2009  }
2010 };
2011 
2012 bool agg_item_collations(DTCollation &c, const char *name,
2013  Item **items, uint nitems, uint flags, int item_sep);
2014 bool agg_item_collations_for_comparison(DTCollation &c, const char *name,
2015  Item **items, uint nitems, uint flags);
2016 bool agg_item_set_converter(DTCollation &coll, const char *fname,
2017  Item **args, uint nargs, uint flags, int item_sep);
2018 bool agg_item_charsets(DTCollation &c, const char *name,
2019  Item **items, uint nitems, uint flags, int item_sep);
2020 inline bool
2021 agg_item_charsets_for_string_result(DTCollation &c, const char *name,
2022  Item **items, uint nitems,
2023  int item_sep= 1)
2024 {
2025  uint flags= MY_COLL_ALLOW_SUPERSET_CONV |
2026  MY_COLL_ALLOW_COERCIBLE_CONV |
2027  MY_COLL_ALLOW_NUMERIC_CONV;
2028  return agg_item_charsets(c, name, items, nitems, flags, item_sep);
2029 }
2030 inline bool
2031 agg_item_charsets_for_comparison(DTCollation &c, const char *name,
2032  Item **items, uint nitems,
2033  int item_sep= 1)
2034 {
2035  uint flags= MY_COLL_ALLOW_SUPERSET_CONV |
2036  MY_COLL_ALLOW_COERCIBLE_CONV |
2037  MY_COLL_DISALLOW_NONE;
2038  return agg_item_charsets(c, name, items, nitems, flags, item_sep);
2039 }
2040 inline bool
2041 agg_item_charsets_for_string_result_with_comparison(DTCollation &c,
2042  const char *name,
2043  Item **items, uint nitems,
2044  int item_sep= 1)
2045 {
2046  uint flags= MY_COLL_ALLOW_SUPERSET_CONV |
2047  MY_COLL_ALLOW_COERCIBLE_CONV |
2048  MY_COLL_ALLOW_NUMERIC_CONV |
2049  MY_COLL_DISALLOW_NONE;
2050  return agg_item_charsets(c, name, items, nitems, flags, item_sep);
2051 }
2052 
2053 
2055 {
2056 public:
2057  Item_num() { collation.set_numeric(); } /* Remove gcc warning */
2058  virtual Item_num *neg()= 0;
2060  bool check_partition_func_processor(uchar *int_arg) { return FALSE;}
2061 };
2062 
2063 #define NO_CACHED_FIELD_INDEX ((uint)(-1))
2064 
2065 class st_select_lex;
2066 class Item_ident :public Item
2067 {
2068 protected:
2069  /*
2070  We have to store initial values of db_name, table_name and field_name
2071  to be able to restore them during cleanup() because they can be
2072  updated during fix_fields() to values from Field object and life-time
2073  of those is shorter than life-time of Item_field.
2074  */
2075  const char *orig_db_name;
2076  const char *orig_table_name;
2077  const char *orig_field_name;
2078 
2079 public:
2080  Name_resolution_context *context;
2081  const char *db_name;
2082  const char *table_name;
2083  const char *field_name;
2084  bool alias_name_used; /* true if item was resolved against alias */
2085  /*
2086  Cached value of index for this field in table->field array, used by prep.
2087  stmts for speeding up their re-execution. Holds NO_CACHED_FIELD_INDEX
2088  if index value is not known.
2089  */
2090  uint cached_field_index;
2091  /*
2092  Cached pointer to table which contains this field, used for the same reason
2093  by prep. stmt. too in case then we have not-fully qualified field.
2094  0 - means no cached value.
2095  */
2096  TABLE_LIST *cached_table;
2097  st_select_lex *depended_from;
2098  Item_ident(Name_resolution_context *context_arg,
2099  const char *db_name_arg, const char *table_name_arg,
2100  const char *field_name_arg);
2101  Item_ident(THD *thd, Item_ident *item);
2102  const char *full_name() const;
2103  virtual void fix_after_pullout(st_select_lex *parent_select,
2104  st_select_lex *removed_select);
2105  void cleanup();
2106  bool remove_dependence_processor(uchar * arg);
2107  virtual void print(String *str, enum_query_type query_type);
2108  virtual bool change_context_processor(uchar *cntx)
2109  { context= (Name_resolution_context *)cntx; return FALSE; }
2110  friend bool insert_fields(THD *thd, Name_resolution_context *context,
2111  const char *db_name,
2112  const char *table_name, List_iterator<Item> *it,
2113  bool any_privileges);
2114 };
2115 
2116 
2118 {
2119 public:
2120  Field *field;
2121  const char *db_name;
2122  const char *table_name;
2123 
2124  Item_ident_for_show(Field *par_field, const char *db_arg,
2125  const char *table_name_arg)
2126  :field(par_field), db_name(db_arg), table_name(table_name_arg)
2127  {}
2128 
2129  enum Type type() const { return FIELD_ITEM; }
2130  double val_real() { return field->val_real(); }
2131  longlong val_int() { return field->val_int(); }
2132  String *val_str(String *str) { return field->val_str(str); }
2133  my_decimal *val_decimal(my_decimal *dec) { return field->val_decimal(dec); }
2134  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
2135  {
2136  return field->get_date(ltime, fuzzydate);
2137  }
2138  bool get_time(MYSQL_TIME *ltime)
2139  {
2140  return field->get_time(ltime);
2141  }
2142  void make_field(Send_field *tmp_field);
2143  CHARSET_INFO *charset_for_protocol(void) const
2144  { return (CHARSET_INFO *)field->charset_for_protocol(); }
2145 };
2146 
2147 
2148 class Item_equal;
2149 class COND_EQUAL;
2150 
2151 class Item_field :public Item_ident
2152 {
2153 protected:
2154  void set_field(Field *field);
2155 public:
2156  Field *field,*result_field;
2157  Item_equal *item_equal;
2158  bool no_const_subst;
2159  /*
2160  if any_privileges set to TRUE then here real effective privileges will
2161  be stored
2162  */
2163  uint have_privileges;
2164  /* field need any privileges (for VIEW creation) */
2165  bool any_privileges;
2166  Item_field(Name_resolution_context *context_arg,
2167  const char *db_arg,const char *table_name_arg,
2168  const char *field_name_arg);
2169  /*
2170  Constructor needed to process subquery with temporary tables (see Item).
2171  Notice that it will have no name resolution context.
2172  */
2173  Item_field(THD *thd, Item_field *item);
2174  /*
2175  Ensures that field, table, and database names will live as long as
2176  Item_field (this is important in prepared statements).
2177  */
2178  Item_field(THD *thd, Name_resolution_context *context_arg, Field *field);
2179  /*
2180  If this constructor is used, fix_fields() won't work, because
2181  db_name, table_name and column_name are unknown. It's necessary to call
2182  reset_field() before fix_fields() for all fields created this way.
2183  */
2184  Item_field(Field *field);
2185  enum Type type() const { return FIELD_ITEM; }
2186  bool eq(const Item *item, bool binary_cmp) const;
2187  double val_real();
2188  longlong val_int();
2189  longlong val_time_temporal();
2190  longlong val_date_temporal();
2191  my_decimal *val_decimal(my_decimal *);
2192  String *val_str(String*);
2193  double val_result();
2194  longlong val_int_result();
2195  longlong val_time_temporal_result();
2196  longlong val_date_temporal_result();
2197  String *str_result(String* tmp);
2198  my_decimal *val_decimal_result(my_decimal *);
2199  bool val_bool_result();
2200  bool is_null_result();
2201  bool send(Protocol *protocol, String *str_arg);
2202  void reset_field(Field *f);
2203  bool fix_fields(THD *, Item **);
2204  void make_field(Send_field *tmp_field);
2205  type_conversion_status save_in_field(Field *field,bool no_conversions);
2206  void save_org_in_field(Field *field);
2207  table_map used_tables() const;
2208  virtual table_map resolved_used_tables() const;
2209  enum Item_result result_type () const
2210  {
2211  return field->result_type();
2212  }
2213  enum Item_result numeric_context_result_type() const
2214  {
2215  return field->numeric_context_result_type();
2216  }
2217  Item_result cast_to_int_type() const
2218  {
2219  return field->cast_to_int_type();
2220  }
2221  enum_field_types field_type() const
2222  {
2223  return field->type();
2224  }
2225  enum_monotonicity_info get_monotonicity_info() const
2226  {
2227  return MONOTONIC_STRICT_INCREASING;
2228  }
2229  longlong val_int_endpoint(bool left_endp, bool *incl_endp);
2230  Field *get_tmp_table_field() { return result_field; }
2231  Field *tmp_table_field(TABLE *t_arg) { return result_field; }
2232  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
2233  bool get_date_result(MYSQL_TIME *ltime,uint fuzzydate);
2234  bool get_time(MYSQL_TIME *ltime);
2235  bool get_timeval(struct timeval *tm, int *warnings);
2236  bool is_null() { return field->is_null(); }
2237  void update_null_value();
2238  Item *get_tmp_table_item(THD *thd);
2239  bool collect_item_field_processor(uchar * arg);
2240  bool add_field_to_set_processor(uchar * arg);
2241  bool remove_column_from_bitmap(uchar * arg);
2242  bool find_item_in_field_list_processor(uchar *arg);
2243  bool register_field_in_read_map(uchar *arg);
2244  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
2245  void cleanup();
2246  Item_equal *find_item_equal(COND_EQUAL *cond_equal);
2247  bool subst_argument_checker(uchar **arg);
2248  Item *equal_fields_propagator(uchar *arg);
2249  bool set_no_const_sub(uchar *arg);
2250  Item *replace_equal_field(uchar *arg);
2251  inline uint32 max_disp_length() { return field->max_display_length(); }
2252  Item_field *field_for_view_update() { return this; }
2253  Item *safe_charset_converter(const CHARSET_INFO *tocs);
2254  int fix_outer_field(THD *thd, Field **field, Item **reference);
2255  virtual Item *update_value_transformer(uchar *select_arg);
2256  virtual bool item_field_by_name_analyzer(uchar **arg);
2257  virtual Item* item_field_by_name_transformer(uchar *arg);
2258  virtual void print(String *str, enum_query_type query_type);
2259  bool is_outer_field() const
2260  {
2261  DBUG_ASSERT(fixed);
2262  return field->table->pos_in_table_list->outer_join ||
2263  field->table->pos_in_table_list->outer_join_nest();
2264  }
2265  Field::geometry_type get_geometry_type() const
2266  {
2267  DBUG_ASSERT(field_type() == MYSQL_TYPE_GEOMETRY);
2268  return field->get_geometry_type();
2269  }
2270  const CHARSET_INFO *charset_for_protocol(void) const
2271  { return field->charset_for_protocol(); }
2272 
2273 #ifndef DBUG_OFF
2274  void dbug_print()
2275  {
2276  fprintf(DBUG_FILE, "<field ");
2277  if (field)
2278  {
2279  fprintf(DBUG_FILE, "'%s.%s': ", field->table->alias, field->field_name);
2280  field->dbug_print();
2281  }
2282  else
2283  fprintf(DBUG_FILE, "NULL");
2284 
2285  fprintf(DBUG_FILE, ", result_field: ");
2286  if (result_field)
2287  {
2288  fprintf(DBUG_FILE, "'%s.%s': ",
2289  result_field->table->alias, result_field->field_name);
2290  result_field->dbug_print();
2291  }
2292  else
2293  fprintf(DBUG_FILE, "NULL");
2294  fprintf(DBUG_FILE, ">\n");
2295  }
2296 #endif
2297 
2299  bool push_to_non_agg_fields(st_select_lex *select_lex);
2300 
2301  friend class Item_default_value;
2302  friend class Item_insert_value;
2303  friend class st_select_lex_unit;
2304 };
2305 
2307 {
2308  void init()
2309  {
2310  maybe_null= null_value= TRUE;
2311  max_length= 0;
2312  fixed= 1;
2313  collation.set(&my_charset_bin, DERIVATION_IGNORABLE);
2314  }
2315 public:
2316  Item_null()
2317  {
2318  init();
2319  item_name= NAME_STRING("NULL");
2320  }
2321  Item_null(const Name_string &name_par)
2322  {
2323  init();
2324  item_name= name_par;
2325  }
2326  enum Type type() const { return NULL_ITEM; }
2327  bool eq(const Item *item, bool binary_cmp) const;
2328  double val_real();
2329  longlong val_int();
2330  longlong val_time_temporal() { return val_int(); }
2331  longlong val_date_temporal() { return val_int(); }
2332  String *val_str(String *str);
2333  my_decimal *val_decimal(my_decimal *);
2334  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
2335  {
2336  return true;
2337  }
2338  bool get_time(MYSQL_TIME *ltime)
2339  {
2340  return true;
2341  }
2342  type_conversion_status save_in_field(Field *field, bool no_conversions);
2343  type_conversion_status save_safe_in_field(Field *field);
2344  bool send(Protocol *protocol, String *str);
2345  enum Item_result result_type () const { return STRING_RESULT; }
2346  enum_field_types field_type() const { return MYSQL_TYPE_NULL; }
2347  bool basic_const_item() const { return 1; }
2348  Item *clone_item() { return new Item_null(item_name); }
2349  bool is_null() { return 1; }
2350 
2351  virtual inline void print(String *str, enum_query_type query_type)
2352  {
2353  str->append(STRING_WITH_LEN("NULL"));
2354  }
2355 
2356  Item *safe_charset_converter(const CHARSET_INFO *tocs);
2357  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
2358 };
2359 
2370 {
2372  enum_field_types fld_type;
2374  Item_result res_type;
2375 
2376 public:
2377  Field *result_field;
2378  Item_null_result(enum_field_types fld_type, Item_result res_type)
2379  : Item_null(), fld_type(fld_type), res_type(res_type), result_field(0) {}
2380  bool is_result_field() { return result_field != 0; }
2381  void save_in_result_field(bool no_conversions)
2382  {
2383  save_in_field(result_field, no_conversions);
2384  }
2385  bool check_partition_func_processor(uchar *int_arg) {return TRUE;}
2386  enum_field_types field_type() const { return fld_type; }
2387  Item_result result_type() const { return res_type; }
2388 };
2389 
2390 /* Item represents one placeholder ('?') of prepared statement */
2391 
2392 class Item_param :public Item,
2394 {
2395  char cnvbuf[MAX_FIELD_WIDTH];
2396  String cnvstr;
2397  Item *cnvitem;
2398 
2399 public:
2400  enum enum_item_param_state
2401  {
2402  NO_VALUE, NULL_VALUE, INT_VALUE, REAL_VALUE,
2403  STRING_VALUE, TIME_VALUE, LONG_DATA_VALUE,
2404  DECIMAL_VALUE
2405  } state;
2406 
2407  /*
2408  A buffer for string and long data values. Historically all allocated
2409  values returned from val_str() were treated as eligible to
2410  modification. I. e. in some cases Item_func_concat can append it's
2411  second argument to return value of the first one. Because of that we
2412  can't return the original buffer holding string data from val_str(),
2413  and have to have one buffer for data and another just pointing to
2414  the data. This is the latter one and it's returned from val_str().
2415  Can not be declared inside the union as it's not a POD type.
2416  */
2417  String str_value_ptr;
2418  my_decimal decimal_value;
2419  union
2420  {
2421  longlong integer;
2422  double real;
2423  /*
2424  Character sets conversion info for string values.
2425  Character sets of client and connection defined at bind time are used
2426  for all conversions, even if one of them is later changed (i.e.
2427  between subsequent calls to mysql_stmt_execute).
2428  */
2429  struct CONVERSION_INFO
2430  {
2431  const CHARSET_INFO *character_set_client;
2432  const CHARSET_INFO *character_set_of_placeholder;
2433  /*
2434  This points at character set of connection if conversion
2435  to it is required (i. e. if placeholder typecode is not BLOB).
2436  Otherwise it's equal to character_set_client (to simplify
2437  check in convert_str_value()).
2438  */
2439  const CHARSET_INFO *final_character_set_of_str_value;
2440  } cs_info;
2441  MYSQL_TIME time;
2442  } value;
2443 
2444  /* Cached values for virtual methods to save us one switch. */
2445  enum Item_result item_result_type;
2446  enum Type item_type;
2447 
2448  /*
2449  Used when this item is used in a temporary table.
2450  This is NOT placeholder metadata sent to client, as this value
2451  is assigned after sending metadata (in setup_one_conversion_function).
2452  For example in case of 'SELECT ?' you'll get MYSQL_TYPE_STRING both
2453  in result set and placeholders metadata, no matter what type you will
2454  supply for this placeholder in mysql_stmt_execute.
2455  */
2456  enum enum_field_types param_type;
2457  /*
2458  Offset of placeholder inside statement text. Used to create
2459  no-placeholders version of this statement for the binary log.
2460  */
2461  uint pos_in_query;
2462 
2463  Item_param(uint pos_in_query_arg);
2464 
2465  enum Item_result result_type () const { return item_result_type; }
2466  enum Type type() const { return item_type; }
2467  enum_field_types field_type() const { return param_type; }
2468 
2469  double val_real();
2470  longlong val_int();
2471  my_decimal *val_decimal(my_decimal*);
2472  String *val_str(String*);
2473  bool get_time(MYSQL_TIME *tm);
2474  bool get_date(MYSQL_TIME *tm, uint fuzzydate);
2475  type_conversion_status save_in_field(Field *field, bool no_conversions);
2476 
2477  void set_null();
2478  void set_int(longlong i, uint32 max_length_arg);
2479  void set_double(double i);
2480  void set_decimal(const char *str, ulong length);
2481  void set_decimal(const my_decimal *dv);
2482  bool set_str(const char *str, ulong length);
2483  bool set_longdata(const char *str, ulong length);
2484  void set_time(MYSQL_TIME *tm, timestamp_type type, uint32 max_length_arg);
2485  bool set_from_user_var(THD *thd, const user_var_entry *entry);
2486  void reset();
2487  /*
2488  Assign placeholder value from bind data.
2489  Note, that 'len' has different semantics in embedded library (as we
2490  don't need to check that packet is not broken there). See
2491  sql_prepare.cc for details.
2492  */
2493  void (*set_param_func)(Item_param *param, uchar **pos, ulong len);
2494 
2495  const String *query_val_str(THD *thd, String *str) const;
2496 
2497  bool convert_str_value(THD *thd);
2498 
2499  /*
2500  If value for parameter was not set we treat it as non-const
2501  so noone will use parameters value in fix_fields still
2502  parameter is constant during execution.
2503  */
2504  virtual table_map used_tables() const
2505  { return state != NO_VALUE ? (table_map)0 : PARAM_TABLE_BIT; }
2506  virtual void print(String *str, enum_query_type query_type);
2507  bool is_null()
2508  { DBUG_ASSERT(state != NO_VALUE); return state == NULL_VALUE; }
2509  bool basic_const_item() const;
2510  /*
2511  This method is used to make a copy of a basic constant item when
2512  propagating constants in the optimizer. The reason to create a new
2513  item and not use the existing one is not precisely known (2005/04/16).
2514  Probably we are trying to preserve tree structure of items, in other
2515  words, avoid pointing at one item from two different nodes of the tree.
2516  Return a new basic constant item if parameter value is a basic
2517  constant, assert otherwise. This method is called only if
2518  basic_const_item returned TRUE.
2519  */
2520  Item *safe_charset_converter(const CHARSET_INFO *tocs);
2521  Item *clone_item();
2522  /*
2523  Implement by-value equality evaluation if parameter value
2524  is set and is a basic constant (integer, real or string).
2525  Otherwise return FALSE.
2526  */
2527  bool eq(const Item *item, bool binary_cmp) const;
2531 
2532 private:
2533  virtual inline Settable_routine_parameter *
2534  get_settable_routine_parameter()
2535  {
2536  return this;
2537  }
2538 
2539  virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
2540 
2541  virtual void set_out_param_info(Send_field *info);
2542 
2543 public:
2544  virtual const Send_field *get_out_param_info() const;
2545 
2546  virtual void make_field(Send_field *field);
2547 
2548 private:
2549  Send_field *m_out_param_info;
2550 };
2551 
2552 
2553 class Item_int :public Item_num
2554 {
2555 public:
2556  longlong value;
2557  Item_int(int32 i,uint length= MY_INT32_NUM_DECIMAL_DIGITS)
2558  :value((longlong) i)
2559  { max_length=length; fixed= 1; }
2560  Item_int(longlong i,uint length= MY_INT64_NUM_DECIMAL_DIGITS)
2561  :value(i)
2562  { max_length=length; fixed= 1; }
2563  Item_int(ulonglong i, uint length= MY_INT64_NUM_DECIMAL_DIGITS)
2564  :value((longlong)i)
2565  { max_length=length; fixed= 1; unsigned_flag= 1; }
2566  Item_int(Item_int *item_arg)
2567  {
2568  value= item_arg->value;
2569  item_name= item_arg->item_name;
2570  max_length= item_arg->max_length;
2571  fixed= 1;
2572  }
2573  Item_int(const Name_string &name_arg, longlong i, uint length) :value(i)
2574  {
2575  max_length= length;
2576  item_name= name_arg;
2577  fixed= 1;
2578  }
2579  Item_int(const char *str_arg, uint length);
2580  enum Type type() const { return INT_ITEM; }
2581  enum Item_result result_type () const { return INT_RESULT; }
2582  enum_field_types field_type() const { return MYSQL_TYPE_LONGLONG; }
2583  longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
2584  double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; }
2585  my_decimal *val_decimal(my_decimal *);
2586  String *val_str(String*);
2587  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
2588  {
2589  return get_date_from_int(ltime, fuzzydate);
2590  }
2591  bool get_time(MYSQL_TIME *ltime)
2592  {
2593  return get_time_from_int(ltime);
2594  }
2595  type_conversion_status save_in_field(Field *field, bool no_conversions);
2596  bool basic_const_item() const { return 1; }
2597  Item *clone_item() { return new Item_int(this); }
2598  virtual void print(String *str, enum_query_type query_type);
2599  Item_num *neg() { value= -value; return this; }
2600  uint decimal_precision() const
2601  { return (uint)(max_length - test(value < 0)); }
2602  bool eq(const Item *, bool binary_cmp) const;
2603  bool check_partition_func_processor(uchar *bool_arg) { return FALSE;}
2604 };
2605 
2606 
2610 class Item_int_0 :public Item_int
2611 {
2612 public:
2613  Item_int_0() :Item_int(NAME_STRING("0"), 0, 1) {}
2614 };
2615 
2616 
2617 /*
2618  Item_temporal is used to store numeric representation
2619  of time/date/datetime values for queries like:
2620 
2621  WHERE datetime_column NOT IN
2622  ('2006-04-25 10:00:00','2006-04-25 10:02:00', ...);
2623 
2624  and for SHOW/INFORMATION_SCHEMA purposes (see sql_show.cc)
2625 
2626  TS-TODO: Can't we use Item_time_literal, Item_date_literal,
2627  TS-TODO: and Item_datetime_literal for this purpose?
2628 */
2630 {
2631  enum_field_types cached_field_type;
2632 public:
2633  Item_temporal(enum_field_types field_type_arg, longlong i): Item_int(i),
2634  cached_field_type(field_type_arg)
2635  {
2636  DBUG_ASSERT(is_temporal_type(field_type_arg));
2637  }
2638  Item_temporal(enum_field_types field_type_arg, const Name_string &name_arg,
2639  longlong i, uint length): Item_int(i),
2640  cached_field_type(field_type_arg)
2641  {
2642  DBUG_ASSERT(is_temporal_type(field_type_arg));
2643  max_length= length;
2644  item_name= name_arg;
2645  fixed= 1;
2646  }
2647  Item *clone_item() { return new Item_temporal(field_type(), value); }
2648  type_conversion_status save_in_field(Field *field, bool no_conversions);
2649  longlong val_time_temporal() { return val_int(); }
2650  longlong val_date_temporal() { return val_int(); }
2651  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
2652  {
2653  DBUG_ASSERT(0);
2654  return false;
2655  }
2656  bool get_time(MYSQL_TIME *ltime)
2657  {
2658  DBUG_ASSERT(0);
2659  return false;
2660  }
2661  enum_field_types field_type() const
2662  {
2663  return cached_field_type;
2664  }
2665 };
2666 
2667 
2668 class Item_uint :public Item_int
2669 {
2670 public:
2671  Item_uint(const char *str_arg, uint length)
2672  :Item_int(str_arg, length) { unsigned_flag= 1; }
2673  Item_uint(ulonglong i) :Item_int((ulonglong) i, 10) {}
2674  Item_uint(const Name_string &name_arg, longlong i, uint length)
2675  :Item_int(name_arg, i, length) { unsigned_flag= 1; }
2676  double val_real()
2677  { DBUG_ASSERT(fixed == 1); return ulonglong2double((ulonglong)value); }
2678  String *val_str(String*);
2679 
2680  Item *clone_item() { return new Item_uint(item_name, value, max_length); }
2681  type_conversion_status save_in_field(Field *field, bool no_conversions);
2682  virtual void print(String *str, enum_query_type query_type);
2683  Item_num *neg ();
2684  uint decimal_precision() const { return max_length; }
2685  bool check_partition_func_processor(uchar *bool_arg) { return FALSE;}
2686 };
2687 
2688 
2689 /* decimal (fixed point) constant */
2690 class Item_decimal :public Item_num
2691 {
2692 protected:
2693  my_decimal decimal_value;
2694 public:
2695  Item_decimal(const char *str_arg, uint length, const CHARSET_INFO *charset);
2696  Item_decimal(const Name_string &name_arg,
2697  const my_decimal *val_arg, uint decimal_par, uint length);
2698  Item_decimal(my_decimal *value_par);
2699  Item_decimal(longlong val, bool unsig);
2700  Item_decimal(double val, int precision, int scale);
2701  Item_decimal(const uchar *bin, int precision, int scale);
2702 
2703  enum Type type() const { return DECIMAL_ITEM; }
2704  enum Item_result result_type () const { return DECIMAL_RESULT; }
2705  enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
2706  longlong val_int();
2707  double val_real();
2708  String *val_str(String*);
2709  my_decimal *val_decimal(my_decimal *val) { return &decimal_value; }
2710  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
2711  {
2712  return get_date_from_decimal(ltime, fuzzydate);
2713  }
2714  bool get_time(MYSQL_TIME *ltime)
2715  {
2716  return get_time_from_decimal(ltime);
2717  }
2718  type_conversion_status save_in_field(Field *field, bool no_conversions);
2719  bool basic_const_item() const { return 1; }
2720  Item *clone_item()
2721  {
2722  return new Item_decimal(item_name, &decimal_value, decimals, max_length);
2723  }
2724  virtual void print(String *str, enum_query_type query_type);
2725  Item_num *neg()
2726  {
2727  my_decimal_neg(&decimal_value);
2728  unsigned_flag= !decimal_value.sign();
2729  return this;
2730  }
2731  uint decimal_precision() const { return decimal_value.precision(); }
2732  bool eq(const Item *, bool binary_cmp) const;
2733  void set_decimal_value(my_decimal *value_par);
2734  bool check_partition_func_processor(uchar *bool_arg) { return FALSE;}
2735 };
2736 
2737 
2738 class Item_float :public Item_num
2739 {
2740  Name_string presentation;
2741 public:
2742  double value;
2743  // Item_real() :value(0) {}
2744  Item_float(const char *str_arg, uint length);
2745  Item_float(const Name_string name_arg,
2746  double val_arg, uint decimal_par, uint length)
2747  :value(val_arg)
2748  {
2749  presentation= name_arg;
2750  item_name= name_arg;
2751  decimals= (uint8) decimal_par;
2752  max_length= length;
2753  fixed= 1;
2754  }
2755  Item_float(double value_par, uint decimal_par) :value(value_par)
2756  {
2757  decimals= (uint8) decimal_par;
2758  fixed= 1;
2759  }
2760  type_conversion_status save_in_field(Field *field, bool no_conversions);
2761  enum Type type() const { return REAL_ITEM; }
2762  enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE; }
2763  double val_real() { DBUG_ASSERT(fixed == 1); return value; }
2764  longlong val_int()
2765  {
2766  DBUG_ASSERT(fixed == 1);
2767  if (value <= (double) LONGLONG_MIN)
2768  {
2769  return LONGLONG_MIN;
2770  }
2771  else if (value >= (double) (ulonglong) LONGLONG_MAX)
2772  {
2773  return LONGLONG_MAX;
2774  }
2775  return (longlong) rint(value);
2776  }
2777  String *val_str(String*);
2778  my_decimal *val_decimal(my_decimal *);
2779  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
2780  {
2781  return get_date_from_real(ltime, fuzzydate);
2782  }
2783  bool get_time(MYSQL_TIME *ltime)
2784  {
2785  return get_time_from_real(ltime);
2786  }
2787  bool basic_const_item() const { return 1; }
2788  Item *clone_item()
2789  { return new Item_float(item_name, value, decimals, max_length); }
2790  Item_num *neg() { value= -value; return this; }
2791  virtual void print(String *str, enum_query_type query_type);
2792  bool eq(const Item *, bool binary_cmp) const;
2793 };
2794 
2795 
2797 {
2798  const Name_string func_name;
2799 public:
2800  Item_static_float_func(const Name_string &name_arg,
2801  double val_arg, uint decimal_par, uint length)
2802  :Item_float(null_name_string,
2803  val_arg, decimal_par, length), func_name(name_arg)
2804  {}
2805 
2806  virtual inline void print(String *str, enum_query_type query_type)
2807  {
2808  str->append(func_name);
2809  }
2810 
2812 };
2813 
2814 
2816 {
2817 public:
2818  /* Create from a string, set name from the string itself. */
2819  Item_string(const char *str,uint length,
2820  const CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE,
2821  uint repertoire= MY_REPERTOIRE_UNICODE30)
2822  : m_cs_specified(FALSE)
2823  {
2824  str_value.set_or_copy_aligned(str, length, cs);
2825  collation.set(cs, dv, repertoire);
2826  /*
2827  We have to have a different max_length than 'length' here to
2828  ensure that we get the right length if we do use the item
2829  to create a new table. In this case max_length must be the maximum
2830  number of chars for a string of this type because we in Create_field::
2831  divide the max_length with mbmaxlen).
2832  */
2833  max_length= str_value.numchars()*cs->mbmaxlen;
2834  item_name.copy(str, length, cs);
2835  decimals=NOT_FIXED_DEC;
2836  // it is constant => can be used without fix_fields (and frequently used)
2837  fixed= 1;
2838  }
2839  /* Just create an item and do not fill string representation */
2840  Item_string(const CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE)
2841  : m_cs_specified(FALSE)
2842  {
2843  collation.set(cs, dv);
2844  max_length= 0;
2845  decimals= NOT_FIXED_DEC;
2846  fixed= 1;
2847  }
2848  /* Create from the given name and string. */
2849  Item_string(const Name_string name_par, const char *str, uint length,
2850  const CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE,
2851  uint repertoire= MY_REPERTOIRE_UNICODE30)
2852  : m_cs_specified(FALSE)
2853  {
2854  str_value.set_or_copy_aligned(str, length, cs);
2855  collation.set(cs, dv, repertoire);
2856  max_length= str_value.numchars()*cs->mbmaxlen;
2857  item_name= name_par;
2858  decimals=NOT_FIXED_DEC;
2859  // it is constant => can be used without fix_fields (and frequently used)
2860  fixed= 1;
2861  }
2862  /*
2863  This is used in stored procedures to avoid memory leaks and
2864  does a deep copy of its argument.
2865  */
2866  void set_str_with_copy(const char *str_arg, uint length_arg)
2867  {
2868  str_value.copy(str_arg, length_arg, collation.collation);
2869  max_length= str_value.numchars() * collation.collation->mbmaxlen;
2870  }
2871  void set_repertoire_from_value()
2872  {
2873  collation.repertoire= my_string_repertoire(str_value.charset(),
2874  str_value.ptr(),
2875  str_value.length());
2876  }
2877  enum Type type() const { return STRING_ITEM; }
2878  double val_real();
2879  longlong val_int();
2880  String *val_str(String*)
2881  {
2882  DBUG_ASSERT(fixed == 1);
2883  return (String*) &str_value;
2884  }
2885  my_decimal *val_decimal(my_decimal *);
2886  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
2887  {
2888  return get_date_from_string(ltime, fuzzydate);
2889  }
2890  bool get_time(MYSQL_TIME *ltime)
2891  {
2892  return get_time_from_string(ltime);
2893  }
2894  type_conversion_status save_in_field(Field *field, bool no_conversions);
2895  enum Item_result result_type () const { return STRING_RESULT; }
2896  enum_field_types field_type() const { return MYSQL_TYPE_VARCHAR; }
2897  bool basic_const_item() const { return 1; }
2898  bool eq(const Item *item, bool binary_cmp) const;
2899  Item *clone_item()
2900  {
2901  return new Item_string(static_cast<Name_string>(item_name), str_value.ptr(),
2902  str_value.length(), collation.collation);
2903  }
2904  Item *safe_charset_converter(const CHARSET_INFO *tocs);
2905  Item *charset_converter(const CHARSET_INFO *tocs, bool lossless);
2906  inline void append(char *str, uint length)
2907  {
2908  str_value.append(str, length);
2909  max_length= str_value.numchars() * collation.collation->mbmaxlen;
2910  }
2911  virtual void print(String *str, enum_query_type query_type);
2912  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
2913 
2933  inline bool is_cs_specified() const
2934  {
2935  return m_cs_specified;
2936  }
2937 
2948  inline void set_cs_specified(bool cs_specified)
2949  {
2950  m_cs_specified= cs_specified;
2951  }
2952 
2953 private:
2954  bool m_cs_specified;
2955 };
2956 
2957 
2958 longlong
2959 longlong_from_string_with_check (const CHARSET_INFO *cs,
2960  const char *cptr, char *end);
2961 double
2962 double_from_string_with_check (const CHARSET_INFO *cs,
2963  const char *cptr, char *end);
2964 
2966 {
2967  const Name_string func_name;
2968 public:
2969  Item_static_string_func(const Name_string &name_par,
2970  const char *str, uint length, const CHARSET_INFO *cs,
2971  Derivation dv= DERIVATION_COERCIBLE)
2972  :Item_string(null_name_string, str, length, cs, dv), func_name(name_par)
2973  {}
2974  Item *safe_charset_converter(const CHARSET_INFO *tocs);
2975 
2976  virtual inline void print(String *str, enum_query_type query_type)
2977  {
2978  str->append(func_name);
2979  }
2980 
2981  bool check_partition_func_processor(uchar *int_arg) {return TRUE;}
2982 };
2983 
2984 
2985 /* for show tables */
2987 {
2988 public:
2990  const CHARSET_INFO *cs= NULL):
2991  Item_string(name, NullS, 0, cs)
2992  {
2993  max_length= length;
2994  }
2995 };
2996 
2997 
2999 {
3000 public:
3001  Item_blob(const char *name, uint length) :
3002  Item_partition_func_safe_string(Name_string(name, strlen(name)),
3003  length, &my_charset_bin)
3004  { }
3005  enum Type type() const { return TYPE_HOLDER; }
3006  enum_field_types field_type() const { return MYSQL_TYPE_BLOB; }
3007 };
3008 
3009 
3017 {
3018 public:
3019  Item_empty_string(const char *header, uint length,
3020  const CHARSET_INFO *cs= NULL) :
3021  Item_partition_func_safe_string(Name_string(header, strlen(header)),
3022  0, cs ? cs : &my_charset_utf8_general_ci)
3023  {
3024  max_length= length * collation.collation->mbmaxlen;
3025  }
3026  void make_field(Send_field *field);
3027 };
3028 
3029 
3031 {
3032  enum_field_types int_field_type;
3033 public:
3034  Item_return_int(const char *name_arg, uint length,
3035  enum_field_types field_type_arg, longlong value= 0)
3036  :Item_int(Name_string(name_arg, name_arg ? strlen(name_arg) : 0),
3037  value, length), int_field_type(field_type_arg)
3038  {
3039  unsigned_flag=1;
3040  }
3041  enum_field_types field_type() const { return int_field_type; }
3042 };
3043 
3044 
3046 {
3047 public:
3048  Item_hex_string();
3049  Item_hex_string(const char *str,uint str_length);
3050  enum Type type() const { return VARBIN_ITEM; }
3051  double val_real()
3052  {
3053  DBUG_ASSERT(fixed == 1);
3054  return (double) (ulonglong) Item_hex_string::val_int();
3055  }
3056  longlong val_int();
3057  bool basic_const_item() const { return 1; }
3058  String *val_str(String*) { DBUG_ASSERT(fixed == 1); return &str_value; }
3059  my_decimal *val_decimal(my_decimal *);
3060  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
3061  {
3062  return get_date_from_string(ltime, fuzzydate);
3063  }
3064  bool get_time(MYSQL_TIME *ltime)
3065  {
3066  return get_time_from_string(ltime);
3067  }
3068  type_conversion_status save_in_field(Field *field, bool no_conversions);
3069  enum Item_result result_type () const { return STRING_RESULT; }
3070  enum Item_result cast_to_int_type() const { return INT_RESULT; }
3071  enum_field_types field_type() const { return MYSQL_TYPE_VARCHAR; }
3072  virtual void print(String *str, enum_query_type query_type);
3073  bool eq(const Item *item, bool binary_cmp) const;
3074  virtual Item *safe_charset_converter(const CHARSET_INFO *tocs);
3075  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
3076 private:
3077  void hex_string_init(const char *str, uint str_length);
3078 };
3079 
3080 
3082 {
3083 public:
3084  Item_bin_string(const char *str,uint str_length);
3085 };
3086 
3087 class Item_result_field :public Item /* Item with result field */
3088 {
3089 public:
3090  Field *result_field; /* Save result here */
3091  Item_result_field() :result_field(0) {}
3092  // Constructor used for Item_sum/Item_cond_and/or (see Item comment)
3093  Item_result_field(THD *thd, Item_result_field *item):
3094  Item(thd, item), result_field(item->result_field)
3095  {}
3096  ~Item_result_field() {} /* Required with gcc 2.95 */
3097  Field *get_tmp_table_field() { return result_field; }
3098  Field *tmp_table_field(TABLE *t_arg) { return result_field; }
3099  table_map used_tables() const { return 1; }
3100  virtual void fix_length_and_dec()=0;
3101  void set_result_field(Field *field) { result_field= field; }
3102  bool is_result_field() { return 1; }
3103  void save_in_result_field(bool no_conversions)
3104  {
3105  save_in_field(result_field, no_conversions);
3106  }
3107  void cleanup();
3108  /*
3109  This method is used for debug purposes to print the name of an
3110  item to the debug log. The second use of this method is as
3111  a helper function of print() and error messages, where it is
3112  applicable. To suit both goals it should return a meaningful,
3113  distinguishable and sintactically correct string. This method
3114  should not be used for runtime type identification, use enum
3115  {Sum}Functype and Item_func::functype()/Item_sum::sum_func()
3116  instead.
3117  Added here, to the parent class of both Item_func and Item_sum_func.
3118 
3119  NOTE: for Items inherited from Item_sum, func_name() return part of
3120  function name till first argument (including '(') to make difference in
3121  names for functions with 'distinct' clause and without 'distinct' and
3122  also to make printing of items inherited from Item_sum uniform.
3123  */
3124  virtual const char *func_name() const= 0;
3125 };
3126 
3127 
3128 class Item_ref :public Item_ident
3129 {
3130 protected:
3131  void set_properties();
3132 public:
3133  enum Ref_Type { REF, DIRECT_REF, VIEW_REF, OUTER_REF, AGGREGATE_REF };
3134  Field *result_field; /* Save result here */
3135  Item **ref;
3136  Item_ref(Name_resolution_context *context_arg,
3137  const char *db_arg, const char *table_name_arg,
3138  const char *field_name_arg)
3139  :Item_ident(context_arg, db_arg, table_name_arg, field_name_arg),
3140  result_field(0), ref(0) {}
3141  /*
3142  This constructor is used in two scenarios:
3143  A) *item = NULL
3144  No initialization is performed, fix_fields() call will be necessary.
3145 
3146  B) *item points to an Item this Item_ref will refer to. This is
3147  used for GROUP BY. fix_fields() will not be called in this case,
3148  so we call set_properties to make this item "fixed". set_properties
3149  performs a subset of action Item_ref::fix_fields does, and this subset
3150  is enough for Item_ref's used in GROUP BY.
3151 
3152  TODO we probably fix a superset of problems like in BUG#6658. Check this
3153  with Bar, and if we have a more broader set of problems like this.
3154  */
3155  Item_ref(Name_resolution_context *context_arg, Item **item,
3156  const char *table_name_arg, const char *field_name_arg,
3157  bool alias_name_used_arg= FALSE);
3158 
3159  /* Constructor need to process subselect with temporary tables (see Item) */
3160  Item_ref(THD *thd, Item_ref *item)
3161  :Item_ident(thd, item), result_field(item->result_field), ref(item->ref) {}
3162  enum Type type() const { return REF_ITEM; }
3163  bool eq(const Item *item, bool binary_cmp) const
3164  {
3165  Item *it= ((Item *) item)->real_item();
3166  return ref && (*ref)->eq(it, binary_cmp);
3167  }
3168  double val_real();
3169  longlong val_int();
3170  longlong val_time_temporal();
3171  longlong val_date_temporal();
3172  my_decimal *val_decimal(my_decimal *);
3173  bool val_bool();
3174  String *val_str(String* tmp);
3175  bool is_null();
3176  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
3177  double val_result();
3178  longlong val_int_result();
3179  String *str_result(String* tmp);
3180  my_decimal *val_decimal_result(my_decimal *);
3181  bool val_bool_result();
3182  bool is_null_result();
3183  bool send(Protocol *prot, String *tmp);
3184  void make_field(Send_field *field);
3185  bool fix_fields(THD *, Item **);
3186  void fix_after_pullout(st_select_lex *parent_select,
3187  st_select_lex *removed_select);
3188  type_conversion_status save_in_field(Field *field, bool no_conversions);
3189  void save_org_in_field(Field *field);
3190  enum Item_result result_type () const { return (*ref)->result_type(); }
3191  enum_field_types field_type() const { return (*ref)->field_type(); }
3192  Field *get_tmp_table_field()
3193  { return result_field ? result_field : (*ref)->get_tmp_table_field(); }
3194  Item *get_tmp_table_item(THD *thd);
3195  bool const_item() const
3196  {
3197  return (*ref)->const_item() && (used_tables() == 0);
3198  }
3199  table_map used_tables() const
3200  {
3201  return depended_from ? OUTER_REF_TABLE_BIT : (*ref)->used_tables();
3202  }
3203  void update_used_tables()
3204  {
3205  if (!depended_from)
3206  (*ref)->update_used_tables();
3207  }
3208 
3209  virtual table_map resolved_used_tables() const
3210  { return (*ref)->resolved_used_tables(); }
3211 
3212  table_map not_null_tables() const
3213  {
3214  /*
3215  It can happen that our 'depended_from' member is set but the
3216  'depended_from' member of the referenced item is not (example: if a
3217  field in a subquery belongs to an outer merged view), so we first test
3218  ours:
3219  */
3220  return depended_from ? OUTER_REF_TABLE_BIT : (*ref)->not_null_tables();
3221  }
3222  void set_result_field(Field *field) { result_field= field; }
3223  bool is_result_field() { return 1; }
3224  void save_in_result_field(bool no_conversions)
3225  {
3226  (*ref)->save_in_field(result_field, no_conversions);
3227  }
3228  Item *real_item()
3229  {
3230  return ref ? (*ref)->real_item() : this;
3231  }
3232  bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
3233  {
3234  return (*ref)->walk(processor, walk_subquery, arg) ||
3235  (this->*processor)(arg);
3236  }
3237  virtual Item* transform(Item_transformer, uchar *arg);
3238  virtual Item* compile(Item_analyzer analyzer, uchar **arg_p,
3239  Item_transformer transformer, uchar *arg_t);
3240  virtual bool explain_subquery_checker(uchar **arg)
3241  {
3242  /*
3243  Always return false: we don't need to go deeper into referenced
3244  expression tree since we have to mark aliased subqueries at
3245  their original places (select list, derived tables), not by
3246  references from other expression (order by etc).
3247  */
3248  return false;
3249  }
3250  virtual void print(String *str, enum_query_type query_type);
3251  void cleanup();
3252  Item_field *field_for_view_update()
3253  { return (*ref)->field_for_view_update(); }
3254  virtual Ref_Type ref_type() { return REF; }
3255 
3256  // Row emulation: forwarding of ROW-related calls to ref
3257  uint cols()
3258  {
3259  return ref && result_type() == ROW_RESULT ? (*ref)->cols() : 1;
3260  }
3261  Item* element_index(uint i)
3262  {
3263  return ref && result_type() == ROW_RESULT ? (*ref)->element_index(i) : this;
3264  }
3265  Item** addr(uint i)
3266  {
3267  return ref && result_type() == ROW_RESULT ? (*ref)->addr(i) : 0;
3268  }
3269  bool check_cols(uint c)
3270  {
3271  return ref && result_type() == ROW_RESULT ? (*ref)->check_cols(c)
3272  : Item::check_cols(c);
3273  }
3274  bool null_inside()
3275  {
3276  return ref && result_type() == ROW_RESULT ? (*ref)->null_inside() : 0;
3277  }
3278  void bring_value()
3279  {
3280  if (ref && result_type() == ROW_RESULT)
3281  (*ref)->bring_value();
3282  }
3283  bool get_time(MYSQL_TIME *ltime)
3284  {
3285  DBUG_ASSERT(fixed);
3286  return (*ref)->get_time(ltime);
3287  }
3288  virtual bool basic_const_item() const { return ref && (*ref)->basic_const_item(); }
3289  bool is_outer_field() const
3290  {
3291  DBUG_ASSERT(fixed);
3292  DBUG_ASSERT(ref);
3293  return (*ref)->is_outer_field();
3294  }
3295 
3299  virtual bool has_subquery() const
3300  {
3301  DBUG_ASSERT(ref);
3302  return (*ref)->has_subquery();
3303  }
3304 
3308  virtual bool has_stored_program() const
3309  {
3310  DBUG_ASSERT(ref);
3311  return (*ref)->has_stored_program();
3312  }
3313 
3314  virtual bool created_by_in2exists() const
3315  {
3316  return (*ref)->created_by_in2exists();
3317  }
3318 };
3319 
3320 
3321 /*
3322  The same as Item_ref, but get value from val_* family of method to get
3323  value of item on which it referred instead of result* family.
3324 */
3326 {
3327 public:
3328  Item_direct_ref(Name_resolution_context *context_arg, Item **item,
3329  const char *table_name_arg,
3330  const char *field_name_arg,
3331  bool alias_name_used_arg= FALSE)
3332  :Item_ref(context_arg, item, table_name_arg,
3333  field_name_arg, alias_name_used_arg)
3334  {}
3335  /* Constructor need to process subselect with temporary tables (see Item) */
3336  Item_direct_ref(THD *thd, Item_direct_ref *item) : Item_ref(thd, item) {}
3337 
3338  double val_real();
3339  longlong val_int();
3340  longlong val_time_temporal();
3341  longlong val_date_temporal();
3342  String *val_str(String* tmp);
3343  my_decimal *val_decimal(my_decimal *);
3344  bool val_bool();
3345  bool is_null();
3346  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
3347  virtual Ref_Type ref_type() { return DIRECT_REF; }
3348 };
3349 
3350 /*
3351  Class for view fields, the same as Item_direct_ref, but call fix_fields
3352  of reference if it is not called yet
3353 */
3355 {
3356 public:
3358  Item **item,
3359  const char *alias_name_arg,
3360  const char *table_name_arg,
3361  const char *field_name_arg)
3362  : Item_direct_ref(context_arg, item, alias_name_arg, field_name_arg)
3363  {
3364  orig_table_name= table_name_arg;
3365  }
3366 
3367  /* Constructor need to process subselect with temporary tables (see Item) */
3368  Item_direct_view_ref(THD *thd, Item_direct_ref *item)
3369  :Item_direct_ref(thd, item) {}
3370 
3371  /*
3372  We share one underlying Item_field, so we have to disable
3373  build_equal_items_for_cond().
3374  TODO: Implement multiple equality optimization for views.
3375  */
3376  virtual bool subst_argument_checker(uchar **arg)
3377  {
3378  return false;
3379  }
3380 
3381  bool fix_fields(THD *, Item **);
3382  bool eq(const Item *item, bool binary_cmp) const;
3383  Item *get_tmp_table_item(THD *thd)
3384  {
3385  Item *item= Item_ref::get_tmp_table_item(thd);
3386  item->item_name= item_name;
3387  return item;
3388  }
3389  virtual Ref_Type ref_type() { return VIEW_REF; }
3390 };
3391 
3392 
3393 /*
3394  Class for outer fields.
3395  An object of this class is created when the select where the outer field was
3396  resolved is a grouping one. After it has been fixed the ref field will point
3397  to either an Item_ref or an Item_direct_ref object which will be used to
3398  access the field.
3399  The ref field may also point to an Item_field instance.
3400  See also comments for the fix_inner_refs() and the
3401  Item_field::fix_outer_field() functions.
3402 */
3403 
3404 class Item_sum;
3406 {
3407 public:
3408  Item *outer_ref;
3409  /* The aggregate function under which this outer ref is used, if any. */
3410  Item_sum *in_sum_func;
3411  /*
3412  TRUE <=> that the outer_ref is already present in the select list
3413  of the outer select.
3414  */
3415  bool found_in_select_list;
3417  Item_field *outer_field_arg)
3418  :Item_direct_ref(context_arg, 0, outer_field_arg->table_name,
3419  outer_field_arg->field_name),
3420  outer_ref(outer_field_arg), in_sum_func(0),
3421  found_in_select_list(0)
3422  {
3423  ref= &outer_ref;
3424  set_properties();
3425  fixed= 0;
3426  }
3427  Item_outer_ref(Name_resolution_context *context_arg, Item **item,
3428  const char *table_name_arg, const char *field_name_arg,
3429  bool alias_name_used_arg)
3430  :Item_direct_ref(context_arg, item, table_name_arg, field_name_arg,
3431  alias_name_used_arg),
3432  outer_ref(0), in_sum_func(0), found_in_select_list(1)
3433  {}
3434  void save_in_result_field(bool no_conversions)
3435  {
3436  outer_ref->save_org_in_field(result_field);
3437  }
3438  bool fix_fields(THD *, Item **);
3439  void fix_after_pullout(st_select_lex *parent_select,
3440  st_select_lex *removed_select);
3441  table_map used_tables() const
3442  {
3443  return (*ref)->const_item() ? 0 : OUTER_REF_TABLE_BIT;
3444  }
3445  table_map not_null_tables() const { return 0; }
3446 
3447  virtual Ref_Type ref_type() { return OUTER_REF; }
3448 };
3449 
3450 
3451 class Item_in_subselect;
3452 
3453 
3454 /*
3455  An object of this class:
3456  - Converts val_XXX() calls to ref->val_XXX_result() calls, like Item_ref.
3457  - Sets owner->was_null=TRUE if it has returned a NULL value from any
3458  val_XXX() function. This allows to inject an Item_ref_null_helper
3459  object into subquery and then check if the subquery has produced a row
3460  with NULL value.
3461 */
3462 
3464 {
3465 protected:
3466  Item_in_subselect* owner;
3467 public:
3469  Item_in_subselect* master, Item **item,
3470  const char *table_name_arg, const char *field_name_arg)
3471  :Item_ref(context_arg, item, table_name_arg, field_name_arg),
3472  owner(master) {}
3473  double val_real();
3474  longlong val_int();
3475  longlong val_time_temporal();
3476  longlong val_date_temporal();
3477  String* val_str(String* s);
3478  my_decimal *val_decimal(my_decimal *);
3479  bool val_bool();
3480  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
3481  virtual void print(String *str, enum_query_type query_type);
3482  /*
3483  we add RAND_TABLE_BIT to prevent moving this item from HAVING to WHERE
3484  */
3485  table_map used_tables() const
3486  {
3487  return (depended_from ?
3488  OUTER_REF_TABLE_BIT :
3489  (*ref)->used_tables() | RAND_TABLE_BIT);
3490  }
3491 };
3492 
3493 /*
3494  The following class is used to optimize comparing of bigint columns.
3495  We need to save the original item ('ref') to be able to call
3496  ref->save_in_field(). This is used to create index search keys.
3497 
3498  An instance of Item_int_with_ref may have signed or unsigned integer value.
3499 
3500 */
3501 
3503 {
3504 protected:
3505  Item *ref;
3506 public:
3507  Item_int_with_ref(longlong i, Item *ref_arg, my_bool unsigned_arg) :
3508  Item_int(i), ref(ref_arg)
3509  {
3510  unsigned_flag= unsigned_arg;
3511  }
3512  type_conversion_status save_in_field(Field *field, bool no_conversions)
3513  {
3514  return ref->save_in_field(field, no_conversions);
3515  }
3516  Item *clone_item();
3517  virtual Item *real_item() { return ref; }
3518 };
3519 
3520 
3521 /*
3522  Similar to Item_int_with_ref, but to optimize comparing of temporal columns.
3523 */
3525 {
3526 private:
3527  enum_field_types cached_field_type;
3528 public:
3529  Item_temporal_with_ref(enum_field_types field_type_arg,
3530  uint8 decimals_arg, longlong i, Item *ref_arg,
3531  bool unsigned_flag):
3532  Item_int_with_ref(i, ref_arg, unsigned_flag),
3533  cached_field_type(field_type_arg)
3534  {
3535  decimals= decimals_arg;
3536  }
3537  enum_field_types field_type() const { return cached_field_type; }
3538  void print(String *str, enum_query_type query_type);
3539  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
3540  {
3541  DBUG_ASSERT(0);
3542  return true;
3543  }
3544  bool get_time(MYSQL_TIME *ltime)
3545  {
3546  DBUG_ASSERT(0);
3547  return true;
3548  }
3549 
3550 };
3551 
3552 
3553 /*
3554  Item_datetime_with_ref is used to optimize queries like:
3555  SELECT ... FROM t1 WHERE date_or_datetime_column = 20110101101010;
3556  The numeric constant is replaced to Item_datetime_with_ref
3557  by convert_constant_item().
3558 */
3560 {
3561 private:
3562  enum_field_types cached_field_type;
3563 public:
3571  Item_datetime_with_ref(enum_field_types field_type_arg,
3572  uint8 decimals_arg, longlong i, Item *ref_arg):
3573  Item_temporal_with_ref(field_type_arg, decimals_arg, i, ref_arg, true),
3574  cached_field_type(field_type_arg)
3575  {
3576  }
3577  Item *clone_item();
3578  longlong val_date_temporal() { return val_int(); }
3580  {
3581  DBUG_ASSERT(0);
3582  return val_int();
3583  }
3584 };
3585 
3586 
3587 /*
3588  Item_time_with_ref is used to optimize queries like:
3589  SELECT ... FROM t1 WHERE time_column = 20110101101010;
3590  The numeric constant is replaced to Item_time_with_ref
3591  by convert_constant_item().
3592 */
3594 {
3595 public:
3602  Item_time_with_ref(uint8 decimals_arg, longlong i, Item *ref_arg):
3603  Item_temporal_with_ref(MYSQL_TYPE_TIME, decimals_arg, i, ref_arg, 0)
3604  {
3605  }
3606  Item *clone_item();
3607  longlong val_time_temporal() { return val_int(); }
3609  {
3610  DBUG_ASSERT(0);
3611  return val_int();
3612  }
3613 };
3614 
3615 
3616 #ifdef MYSQL_SERVER
3617 #include "gstream.h"
3618 #include "spatial.h"
3619 #include "item_sum.h"
3620 #include "item_func.h"
3621 #include "item_row.h"
3622 #include "item_cmpfunc.h"
3623 #include "item_strfunc.h"
3624 #include "item_geofunc.h"
3625 #include "item_timefunc.h"
3626 #include "item_subselect.h"
3627 #include "item_xmlfunc.h"
3628 #include "item_create.h"
3629 #endif
3630 
3651 class Item_copy :public Item
3652 {
3653 protected:
3654 
3659  enum enum_field_types cached_field_type;
3660 
3663 
3668  Item_result cached_result_type;
3669 
3677  {
3678  item= i;
3679  null_value=maybe_null=item->maybe_null;
3680  decimals=item->decimals;
3681  max_length=item->max_length;
3682  item_name= item->item_name;
3683  cached_field_type= item->field_type();
3684  cached_result_type= item->result_type();
3685  unsigned_flag= item->unsigned_flag;
3686  fixed= item->fixed;
3687  collation.set(item->collation);
3688  }
3689 
3690 public:
3697  static Item_copy *create (Item *item);
3698 
3706  virtual void copy() = 0;
3707 
3708  Item *get_item() { return item; }
3710  enum Type type() const { return COPY_STR_ITEM; }
3711  enum_field_types field_type() const { return cached_field_type; }
3712  enum Item_result result_type () const { return cached_result_type; }
3713 
3714  void make_field(Send_field *field) { item->make_field(field); }
3715  table_map used_tables() const { return (table_map) 1L; }
3716  bool const_item() const { return 0; }
3717  bool is_null() { return null_value; }
3718 
3719  virtual void no_rows_in_result()
3720  {
3721  item->no_rows_in_result();
3722  }
3723 
3724  /*
3725  Override the methods below as pure virtual to make sure all the
3726  sub-classes implement them.
3727  */
3728 
3729  virtual String *val_str(String*) = 0;
3730  virtual my_decimal *val_decimal(my_decimal *) = 0;
3731  virtual double val_real() = 0;
3732  virtual longlong val_int() = 0;
3733  virtual bool get_date(MYSQL_TIME *ltime, uint fuzzydate)= 0;
3734  virtual bool get_time(MYSQL_TIME *ltime)= 0;
3735  virtual type_conversion_status save_in_field(Field *field,
3736  bool no_conversions) = 0;
3737 };
3738 
3745 {
3746 public:
3747  Item_copy_string (Item *item) : Item_copy(item) {}
3748 
3749  String *val_str(String*);
3750  my_decimal *val_decimal(my_decimal *);
3751  double val_real();
3752  longlong val_int();
3753  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
3754  bool get_time(MYSQL_TIME *ltime);
3755  void copy();
3756  type_conversion_status save_in_field(Field *field, bool no_conversions);
3757 };
3758 
3759 
3760 class Item_copy_int : public Item_copy
3761 {
3762 protected:
3763  longlong cached_value;
3764 public:
3765  Item_copy_int (Item *i) : Item_copy(i) {}
3766  type_conversion_status save_in_field(Field *field, bool no_conversions);
3767 
3768  virtual String *val_str(String*);
3769  virtual my_decimal *val_decimal(my_decimal *);
3770  virtual double val_real()
3771  {
3772  return null_value ? 0.0 : (double) cached_value;
3773  }
3774  virtual longlong val_int()
3775  {
3776  return null_value ? LL(0) : cached_value;
3777  }
3778  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
3779  {
3780  return get_date_from_int(ltime, fuzzydate);
3781  }
3782  bool get_time(MYSQL_TIME *ltime)
3783  {
3784  return get_time_from_int(ltime);
3785  }
3786  virtual void copy();
3787 };
3788 
3789 
3791 {
3792 public:
3793  Item_copy_uint (Item *item) : Item_copy_int(item)
3794  {
3795  unsigned_flag= 1;
3796  }
3797 
3798  String *val_str(String*);
3799  double val_real()
3800  {
3801  return null_value ? 0.0 : (double) (ulonglong) cached_value;
3802  }
3803 };
3804 
3805 
3807 {
3808 protected:
3809  double cached_value;
3810 public:
3811  Item_copy_float (Item *i) : Item_copy(i) {}
3812  type_conversion_status save_in_field(Field *field, bool no_conversions);
3813 
3814  String *val_str(String*);
3815  my_decimal *val_decimal(my_decimal *);
3816  double val_real()
3817  {
3818  return null_value ? 0.0 : cached_value;
3819  }
3820  longlong val_int()
3821  {
3822  return (longlong) rint(val_real());
3823  }
3824  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
3825  {
3826  return get_date_from_real(ltime, fuzzydate);
3827  }
3828  bool get_time(MYSQL_TIME *ltime)
3829  {
3830  return get_time_from_real(ltime);
3831  }
3832  void copy()
3833  {
3834  cached_value= item->val_real();
3835  null_value= item->null_value;
3836  }
3837 };
3838 
3839 
3841 {
3842 protected:
3843  my_decimal cached_value;
3844 public:
3845  Item_copy_decimal (Item *i) : Item_copy(i) {}
3846  type_conversion_status save_in_field(Field *field, bool no_conversions);
3847 
3848  String *val_str(String*);
3849  my_decimal *val_decimal(my_decimal *)
3850  {
3851  return null_value ? NULL: &cached_value;
3852  }
3853  double val_real();
3854  longlong val_int();
3855  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
3856  {
3857  return get_date_from_decimal(ltime, fuzzydate);
3858  }
3859  bool get_time(MYSQL_TIME *ltime)
3860  {
3861  return get_time_from_decimal(ltime);
3862  }
3863  void copy();
3864 };
3865 
3866 
3867 class Cached_item :public Sql_alloc
3868 {
3869 public:
3870  my_bool null_value;
3871  Cached_item() :null_value(0) {}
3872  virtual bool cmp(void)=0;
3873  virtual ~Cached_item(); /*line -e1509 */
3874 };
3875 
3877 {
3878  Item *item;
3879  uint32 value_max_length;
3880  String value,tmp_value;
3881 public:
3882  Cached_item_str(THD *thd, Item *arg);
3883  bool cmp(void);
3884  ~Cached_item_str(); // Deallocate String:s
3885 };
3886 
3887 
3889 {
3890  Item *item;
3891  double value;
3892 public:
3893  Cached_item_real(Item *item_par) :item(item_par),value(0.0) {}
3894  bool cmp(void);
3895 };
3896 
3898 {
3899  Item *item;
3900  longlong value;
3901 public:
3902  Cached_item_int(Item *item_par) :item(item_par),value(0) {}
3903  bool cmp(void);
3904 };
3905 
3907 {
3908  Item *item;
3909  longlong value;
3910 public:
3911  Cached_item_temporal(Item *item_par) :item(item_par), value(0) {}
3912  bool cmp(void);
3913 };
3914 
3915 
3917 {
3918  Item *item;
3919  my_decimal value;
3920 public:
3921  Cached_item_decimal(Item *item_par);
3922  bool cmp(void);
3923 };
3924 
3926 {
3927  uchar *buff;
3928  Field *field;
3929  uint length;
3930 
3931 public:
3932 #ifndef DBUG_OFF
3933  void dbug_print()
3934  {
3935  uchar *org_ptr;
3936  org_ptr= field->ptr;
3937  fprintf(DBUG_FILE, "new: ");
3938  field->dbug_print();
3939  field->ptr= buff;
3940  fprintf(DBUG_FILE, ", old: ");
3941  field->dbug_print();
3942  field->ptr= org_ptr;
3943  fprintf(DBUG_FILE, "\n");
3944  }
3945 #endif
3946  Cached_item_field(Field *arg_field) : field(arg_field)
3947  {
3948  field= arg_field;
3949  /* TODO: take the memory allocation below out of the constructor. */
3950  buff= (uchar*) sql_calloc(length=field->pack_length());
3951  }
3952  bool cmp(void);
3953 };
3954 
3956 {
3957 public:
3958  Item *arg;
3960  :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
3961  (const char *)NULL),
3962  arg(NULL) {}
3964  :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
3965  (const char *)NULL),
3966  arg(a) {}
3967  enum Type type() const { return DEFAULT_VALUE_ITEM; }
3968  bool eq(const Item *item, bool binary_cmp) const;
3969  bool fix_fields(THD *, Item **);
3970  virtual void print(String *str, enum_query_type query_type);
3971  type_conversion_status save_in_field(Field *field_arg, bool no_conversions);
3972  table_map used_tables() const { return (table_map)0L; }
3973  Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
3974 
3975  bool walk(Item_processor processor, bool walk_subquery, uchar *args)
3976  {
3977  if (arg && arg->walk(processor, walk_subquery, args))
3978  return true;
3979 
3980  return (this->*processor)(args);
3981  }
3982 
3983  Item *transform(Item_transformer transformer, uchar *args);
3984 };
3985 
3986 /*
3987  Item_insert_value -- an implementation of VALUES() function.
3988  You can use the VALUES(col_name) function in the UPDATE clause
3989  to refer to column values from the INSERT portion of the INSERT
3990  ... UPDATE statement. In other words, VALUES(col_name) in the
3991  UPDATE clause refers to the value of col_name that would be
3992  inserted, had no duplicate-key conflict occurred.
3993  In all other places this function returns NULL.
3994 */
3995 
3997 {
3998 public:
3999  Item *arg;
4001  :Item_field(context_arg, (const char *)NULL, (const char *)NULL,
4002  (const char *)NULL),
4003  arg(a) {}
4004  bool eq(const Item *item, bool binary_cmp) const;
4005  bool fix_fields(THD *, Item **);
4006  virtual void print(String *str, enum_query_type query_type);
4007  type_conversion_status save_in_field(Field *field_arg, bool no_conversions)
4008  {
4009  return Item_field::save_in_field(field_arg, no_conversions);
4010  }
4011  /*
4012  We use RAND_TABLE_BIT to prevent Item_insert_value from
4013  being treated as a constant and precalculated before execution
4014  */
4015  table_map used_tables() const { return RAND_TABLE_BIT; }
4016 
4017  bool walk(Item_processor processor, bool walk_subquery, uchar *args)
4018  {
4019  return arg->walk(processor, walk_subquery, args) ||
4020  (this->*processor)(args);
4021  }
4022 };
4023 
4024 
4025 class Table_triggers_list;
4026 
4027 /*
4028  Represents NEW/OLD version of field of row which is
4029  changed/read in trigger.
4030 
4031  Note: For this item main part of actual binding to Field object happens
4032  not during fix_fields() call (like for Item_field) but right after
4033  parsing of trigger definition, when table is opened, with special
4034  setup_field() call. On fix_fields() stage we simply choose one of
4035  two Field instances representing either OLD or NEW version of this
4036  field.
4037 */
4040 {
4041 public:
4042  /* Is this item represents row from NEW or OLD row ? */
4043  enum row_version_type {OLD_ROW, NEW_ROW};
4044  row_version_type row_version;
4045  /* Next in list of all Item_trigger_field's in trigger */
4046  Item_trigger_field *next_trg_field;
4047  /* Index of the field in the TABLE::field array */
4048  uint field_idx;
4049  /* Pointer to Table_trigger_list object for table of this trigger */
4050  Table_triggers_list *triggers;
4051 
4053  row_version_type row_ver_arg,
4054  const char *field_name_arg,
4055  ulong priv, const bool ro)
4056  :Item_field(context_arg,
4057  (const char *)NULL, (const char *)NULL, field_name_arg),
4058  row_version(row_ver_arg), field_idx((uint)-1), original_privilege(priv),
4059  want_privilege(priv), table_grants(NULL), read_only (ro)
4060  {}
4061  void setup_field(THD *thd, TABLE *table, GRANT_INFO *table_grant_info);
4062  enum Type type() const { return TRIGGER_FIELD_ITEM; }
4063  bool eq(const Item *item, bool binary_cmp) const;
4064  bool fix_fields(THD *, Item **);
4065  virtual void print(String *str, enum_query_type query_type);
4066  table_map used_tables() const { return (table_map)0L; }
4067  Field *get_tmp_table_field() { return 0; }
4068  Item *copy_or_same(THD *thd) { return this; }
4069  Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
4070  void cleanup();
4071 
4072 private:
4073  void set_required_privilege(bool rw);
4074  bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
4075 
4076 public:
4077  Settable_routine_parameter *get_settable_routine_parameter()
4078  {
4079  return (read_only ? 0 : this);
4080  }
4081 
4082  bool set_value(THD *thd, Item **it)
4083  {
4084  return set_value(thd, NULL, it);
4085  }
4086 
4087 private:
4088  /*
4089  'want_privilege' holds privileges required to perform operation on
4090  this trigger field (SELECT_ACL if we are going to read it and
4091  UPDATE_ACL if we are going to update it). It is initialized at
4092  parse time but can be updated later if this trigger field is used
4093  as OUT or INOUT parameter of stored routine (in this case
4094  set_required_privilege() is called to appropriately update
4095  want_privilege and cleanup() is responsible for restoring of
4096  original want_privilege once parameter's value is updated).
4097  */
4098  ulong original_privilege;
4099  ulong want_privilege;
4100  GRANT_INFO *table_grants;
4101  /*
4102  Trigger field is read-only unless it belongs to the NEW row in a
4103  BEFORE INSERT of BEFORE UPDATE trigger.
4104  */
4105  bool read_only;
4106 };
4107 
4108 
4110 {
4111 protected:
4112  Item *example;
4113  table_map used_table_map;
4120  enum enum_field_types cached_field_type;
4121  /*
4122  TRUE <=> cache holds value of the last stored item (i.e actual value).
4123  store() stores item to be cached and sets this flag to FALSE.
4124  On the first call of val_xxx function if this flag is set to FALSE the
4125  cache_value() will be called to actually cache value of saved item.
4126  cache_value() will set this flag to TRUE.
4127  */
4128  bool value_cached;
4129 public:
4130  Item_cache():
4131  example(0), used_table_map(0), cached_field(0),
4132  cached_field_type(MYSQL_TYPE_STRING),
4133  value_cached(0)
4134  {
4135  fixed= 1;
4136  null_value= 1;
4137  }
4138  Item_cache(enum_field_types field_type_arg):
4139  example(0), used_table_map(0), cached_field(0),
4140  cached_field_type(field_type_arg),
4141  value_cached(0)
4142  {
4143  fixed= 1;
4144  null_value= 1;
4145  }
4146 
4147  void set_used_tables(table_map map) { used_table_map= map; }
4148 
4149  virtual table_map resolved_used_tables() const
4150  {
4151  return example ? example->resolved_used_tables() : used_table_map;
4152  }
4153 
4154  virtual bool allocate(uint i) { return 0; }
4155  virtual bool setup(Item *item)
4156  {
4157  example= item;
4158  max_length= item->max_length;
4159  decimals= item->decimals;
4160  collation.set(item->collation);
4161  unsigned_flag= item->unsigned_flag;
4162  if (item->type() == FIELD_ITEM)
4163  cached_field= ((Item_field *)item)->field;
4164  return 0;
4165  };
4166  enum Type type() const { return CACHE_ITEM; }
4167  enum_field_types field_type() const { return cached_field_type; }
4168  static Item_cache* get_cache(const Item *item);
4169  static Item_cache* get_cache(const Item* item, const Item_result type);
4170  table_map used_tables() const { return used_table_map; }
4171  virtual void keep_array() {}
4172  virtual void print(String *str, enum_query_type query_type);
4173  bool eq_def(Field *field)
4174  {
4175  return cached_field ? cached_field->eq_def (field) : FALSE;
4176  }
4177  bool eq(const Item *item, bool binary_cmp) const
4178  {
4179  return this == item;
4180  }
4186  bool has_value()
4187  {
4188  return (value_cached || cache_value()) && !null_value;
4189  }
4190 
4196  Field* field() { return cached_field; }
4197 
4198  virtual void store(Item *item);
4199  virtual bool cache_value()= 0;
4200  bool basic_const_item() const
4201  { return test(example && example->basic_const_item());}
4202  bool walk (Item_processor processor, bool walk_subquery, uchar *argument);
4203  virtual void clear() { null_value= TRUE; value_cached= FALSE; }
4204  bool is_null() { return value_cached ? null_value : example->is_null(); }
4205  Item_result result_type() const
4206  {
4207  if (!example)
4208  return INT_RESULT;
4209  return Field::result_merge_type(example->field_type());
4210  }
4211 };
4212 
4213 
4215 {
4216 protected:
4217  longlong value;
4218 public:
4220  value(0) {}
4221  Item_cache_int(enum_field_types field_type_arg):
4222  Item_cache(field_type_arg), value(0) {}
4223 
4224  virtual void store(Item *item){ Item_cache::store(item); }
4225  void store(Item *item, longlong val_arg);
4226  double val_real();
4227  longlong val_int();
4228  longlong val_time_temporal() { return val_int(); }
4229  longlong val_date_temporal() { return val_int(); }
4230  String* val_str(String *str);
4231  my_decimal *val_decimal(my_decimal *);
4232  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
4233  {
4234  return get_date_from_int(ltime, fuzzydate);
4235  }
4236  bool get_time(MYSQL_TIME *ltime)
4237  {
4238  return get_time_from_int(ltime);
4239  }
4240  enum Item_result result_type() const { return INT_RESULT; }
4241  bool cache_value();
4242 };
4243 
4244 
4246 {
4247  double value;
4248 public:
4250  value(0) {}
4251 
4252  double val_real();
4253  longlong val_int();
4254  String* val_str(String *str);
4255  my_decimal *val_decimal(my_decimal *);
4256  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
4257  {
4258  return get_date_from_real(ltime, fuzzydate);
4259  }
4260  bool get_time(MYSQL_TIME *ltime)
4261  {
4262  return get_time_from_real(ltime);
4263  }
4264  enum Item_result result_type() const { return REAL_RESULT; }
4265  bool cache_value();
4266 };
4267 
4268 
4270 {
4271 protected:
4272  my_decimal decimal_value;
4273 public:
4275 
4276  double val_real();
4277  longlong val_int();
4278  String* val_str(String *str);
4279  my_decimal *val_decimal(my_decimal *);
4280  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
4281  {
4282  return get_date_from_decimal(ltime, fuzzydate);
4283  }
4284  bool get_time(MYSQL_TIME *ltime)
4285  {
4286  return get_time_from_decimal(ltime);
4287  }
4288  enum Item_result result_type() const { return DECIMAL_RESULT; }
4289  bool cache_value();
4290 };
4291 
4292 
4294 {
4295  char buffer[STRING_BUFFER_USUAL_SIZE];
4296  String *value, value_buff;
4297  bool is_varbinary;
4298 
4299 public:
4300  Item_cache_str(const Item *item) :
4301  Item_cache(item->field_type()), value(0),
4302  is_varbinary(item->type() == FIELD_ITEM &&
4303  cached_field_type == MYSQL_TYPE_VARCHAR &&
4304  !((const Item_field *) item)->field->has_charset())
4305  {
4306  collation.set(const_cast<DTCollation&>(item->collation));
4307  }
4308  double val_real();
4309  longlong val_int();
4310  String* val_str(String *);
4311  my_decimal *val_decimal(my_decimal *);
4312  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
4313  {
4314  return get_date_from_string(ltime, fuzzydate);
4315  }
4316  bool get_time(MYSQL_TIME *ltime)
4317  {
4318  return get_time_from_string(ltime);
4319  }
4320  enum Item_result result_type() const { return STRING_RESULT; }
4321  const CHARSET_INFO *charset() const { return value->charset(); };
4322  type_conversion_status save_in_field(Field *field, bool no_conversions);
4323  bool cache_value();
4324 };
4325 
4327 {
4328  Item_cache **values;
4329  uint item_count;
4330  bool save_array;
4331 public:
4332  Item_cache_row()
4333  :Item_cache(), values(0), item_count(2),
4334  save_array(0) {}
4335 
4336  /*
4337  'allocate' used only in row transformer, to preallocate space for row
4338  cache.
4339  */
4340  bool allocate(uint num);
4341  /*
4342  'setup' is needed only by row => it not called by simple row subselect
4343  (only by IN subselect (in subselect optimizer))
4344  */
4345  bool setup(Item *item);
4346  void store(Item *item);
4347  void illegal_method_call(const char *);
4348  void make_field(Send_field *)
4349  {
4350  illegal_method_call((const char*)"make_field");
4351  };
4352  double val_real()
4353  {
4354  illegal_method_call((const char*)"val");
4355  return 0;
4356  };
4357  longlong val_int()
4358  {
4359  illegal_method_call((const char*)"val_int");
4360  return 0;
4361  };
4362  String *val_str(String *)
4363  {
4364  illegal_method_call((const char*)"val_str");
4365  return 0;
4366  };
4367  my_decimal *val_decimal(my_decimal *val)
4368  {
4369  illegal_method_call((const char*)"val_decimal");
4370  return 0;
4371  };
4372  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
4373  {
4374  illegal_method_call((const char *) "get_date");
4375  return true;
4376  }
4377  bool get_time(MYSQL_TIME *ltime)
4378  {
4379  illegal_method_call((const char *) "get_time");
4380  return true;
4381  }
4382 
4383  enum Item_result result_type() const { return ROW_RESULT; }
4384 
4385  uint cols() { return item_count; }
4386  Item *element_index(uint i) { return values[i]; }
4387  Item **addr(uint i) { return (Item **) (values + i); }
4388  bool check_cols(uint c);
4389  bool null_inside();
4390  void bring_value();
4391  void keep_array() { save_array= 1; }
4392  void cleanup()
4393  {
4394  DBUG_ENTER("Item_cache_row::cleanup");
4395  Item_cache::cleanup();
4396  if (save_array)
4397  memset(values, 0, item_count*sizeof(Item**));
4398  else
4399  values= 0;
4400  DBUG_VOID_RETURN;
4401  }
4402  bool cache_value();
4403 };
4404 
4405 
4407 {
4408 protected:
4409  String str_value;
4410  longlong int_value;
4411  bool str_value_cached;
4412 public:
4413  Item_cache_datetime(enum_field_types field_type_arg):
4414  Item_cache(field_type_arg), int_value(0), str_value_cached(0)
4415  {
4416  cmp_context= STRING_RESULT;
4417  }
4418 
4419  void store(Item *item, longlong val_arg);
4420  void store(Item *item);
4421  double val_real();
4422  longlong val_int();
4423  longlong val_time_temporal();
4424  longlong val_date_temporal();
4425  String* val_str(String *str);
4426  my_decimal *val_decimal(my_decimal *);
4427  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
4428  bool get_time(MYSQL_TIME *ltime);
4429  enum Item_result result_type() const { return STRING_RESULT; }
4430  /*
4431  In order to avoid INT <-> STRING conversion of a DATETIME value
4432  two cache_value functions are introduced. One (cache_value) caches STRING
4433  value, another (cache_value_int) - INT value. Thus this cache item
4434  completely relies on the ability of the underlying item to do the
4435  correct conversion.
4436  */
4437  bool cache_value_int();
4438  bool cache_value();
4439  void clear() { Item_cache::clear(); str_value_cached= FALSE; }
4440 };
4441 
4442 
4443 /*
4444  Item_type_holder used to store type. name, length of Item for UNIONS &
4445  derived tables.
4446 
4447  Item_type_holder do not need cleanup() because its time of live limited by
4448  single SP/PS execution.
4449 */
4450 class Item_type_holder: public Item
4451 {
4452 protected:
4453  TYPELIB *enum_set_typelib;
4454  enum_field_types fld_type;
4455  Field::geometry_type geometry_type;
4456 
4457  void get_full_info(Item *item);
4458 
4459  /* It is used to count decimal precision in join_types */
4460  int prev_decimal_int_part;
4461 public:
4462  Item_type_holder(THD*, Item*);
4463 
4464  Item_result result_type() const;
4465  enum_field_types field_type() const { return fld_type; };
4466  enum Type type() const { return TYPE_HOLDER; }
4467  double val_real();
4468  longlong val_int();
4469  my_decimal *val_decimal(my_decimal *);
4470  String *val_str(String*);
4471  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
4472  {
4473  DBUG_ASSERT(0);
4474  return true;
4475  }
4476  bool get_time(MYSQL_TIME *ltime)
4477  {
4478  DBUG_ASSERT(0);
4479  return true;
4480  }
4481  bool join_types(THD *thd, Item *);
4483  static uint32 display_length(Item *item);
4484  static enum_field_types get_real_type(Item *);
4485  Field::geometry_type get_geometry_type() const { return geometry_type; };
4486 };
4487 
4488 
4489 class st_select_lex;
4490 void mark_select_range_as_dependent(THD *thd,
4491  st_select_lex *last_select,
4492  st_select_lex *current_sel,
4493  Field *found_field, Item *found_item,
4494  Item_ident *resolved_item);
4495 
4496 extern Cached_item *new_Cached_item(THD *thd, Item *item,
4497  bool use_result_field);
4498 extern Item_result item_cmp_type(Item_result a,Item_result b);
4499 extern void resolve_const_item(THD *thd, Item **ref, Item *cmp_item);
4500 extern int stored_field_cmp_to_item(THD *thd, Field *field, Item *item);
4501 
4502 extern const String my_null_string;
4503 
4504 #endif /* ITEM_INCLUDED */