1 #ifndef FIELD_INCLUDED
2 #define FIELD_INCLUDED
3 /* Copyright (c) 2000, 2015, Oracle and/or its affiliates.
4    Copyright (c) 2008, 2020, MariaDB Corporation.
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-1335  USA */
18 
19 /*
20   Because of the function make_new_field() all field classes that have static
21   variables must declare the size_of() member function.
22 */
23 
24 #ifdef USE_PRAGMA_INTERFACE
25 #pragma interface			/* gcc class implementation */
26 #endif
27 
28 #include "mysqld.h"                             /* system_charset_info */
29 #include "table.h"                              /* TABLE */
30 #include "sql_string.h"                         /* String */
31 #include "my_decimal.h"                         /* my_decimal */
32 #include "sql_error.h"                          /* Sql_condition */
33 #include "compat56.h"
34 #include "sql_type.h"                           /* Type_std_attributes */
35 #include "field_comp.h"
36 
37 class Send_field;
38 class Copy_field;
39 class Protocol;
40 class Create_field;
41 class Relay_log_info;
42 class Field;
43 class Column_statistics;
44 class Column_statistics_collected;
45 class Item_func;
46 class Item_bool_func;
47 class Item_equal;
48 class Virtual_tmp_table;
49 class Qualified_column_ident;
50 class Table_ident;
51 
52 enum enum_check_fields
53 {
54   CHECK_FIELD_IGNORE,
55   CHECK_FIELD_EXPRESSION,
56   CHECK_FIELD_WARN,
57   CHECK_FIELD_ERROR_FOR_NULL,
58 };
59 
60 /*
61   Common declarations for Field and Item
62 */
63 class Value_source
64 {
65 protected:
66 
67   // Parameters for warning and note generation
68   class Warn_filter
69   {
70     bool m_want_warning_edom;
71     bool m_want_note_truncated_spaces;
72   public:
Warn_filter(bool want_warning_edom,bool want_note_truncated_spaces)73     Warn_filter(bool want_warning_edom, bool want_note_truncated_spaces) :
74      m_want_warning_edom(want_warning_edom),
75      m_want_note_truncated_spaces(want_note_truncated_spaces)
76     { }
77     Warn_filter(const THD *thd);
want_warning_edom()78     bool want_warning_edom() const
79     { return m_want_warning_edom; }
want_note_truncated_spaces()80     bool want_note_truncated_spaces() const
81     { return m_want_note_truncated_spaces; }
82   };
83   class Warn_filter_all: public Warn_filter
84   {
85   public:
Warn_filter_all()86     Warn_filter_all() :Warn_filter(true, true) { }
87   };
88 
89   class Converter_double_to_longlong
90   {
91   protected:
92     bool m_error;
93     longlong m_result;
94   public:
95     Converter_double_to_longlong(double nr, bool unsigned_flag);
result()96     longlong result() const { return m_result; }
error()97     bool error() const { return m_error; }
98     void push_warning(THD *thd, double nr, bool unsigned_flag);
99   };
100   class Converter_double_to_longlong_with_warn:
101     public Converter_double_to_longlong
102   {
103   public:
Converter_double_to_longlong_with_warn(THD * thd,double nr,bool unsigned_flag)104     Converter_double_to_longlong_with_warn(THD *thd, double nr,
105                                            bool unsigned_flag)
106       :Converter_double_to_longlong(nr, unsigned_flag)
107     {
108       if (m_error)
109         push_warning(thd, nr, unsigned_flag);
110     }
Converter_double_to_longlong_with_warn(double nr,bool unsigned_flag)111     Converter_double_to_longlong_with_warn(double nr, bool unsigned_flag)
112       :Converter_double_to_longlong(nr, unsigned_flag)
113     {
114       if (m_error)
115         push_warning(current_thd, nr, unsigned_flag);
116     }
117   };
118 
119   // String-to-number converters
120   class Converter_string_to_number
121   {
122   protected:
123     char *m_end_of_num; // Where the low-level conversion routine stopped
124     int m_error;        // The error code returned by the low-level routine
125     bool m_edom;        // If EDOM-alike error happened during conversion
126     /**
127       Check string-to-number conversion and produce a warning if
128       - could not convert any digits (EDOM-alike error)
129       - found garbage at the end of the string
130       - found extra spaces at the end (a note)
131       See also Field_num::check_edom_and_truncation() for a similar function.
132 
133       @param thd         - the thread that will be used to generate warnings.
134                            Can be NULL (which means current_thd will be used
135                            if a warning is really necessary).
136       @param type        - name of the data type
137                            (e.g. "INTEGER", "DECIMAL", "DOUBLE")
138       @param cs          - character set of the original string
139       @param str         - the original string
140       @param end         - the end of the string
141       @param allow_notes - tells if trailing space notes should be displayed
142                            or suppressed.
143 
144       Unlike Field_num::check_edom_and_truncation(), this function does not
145       distinguish between EDOM and truncation and reports the same warning for
146       both cases. Perhaps we should eventually print different warnings,
147       to make the explicit CAST work closer to the implicit cast in
148       Field_xxx::store().
149     */
150     void check_edom_and_truncation(THD *thd, Warn_filter filter,
151                                    const char *type,
152                                    CHARSET_INFO *cs,
153                                    const char *str,
154                                    size_t length) const;
155   public:
error()156     int error() const { return m_error; }
157   };
158 
159   class Converter_strntod: public Converter_string_to_number
160   {
161     double m_result;
162   public:
Converter_strntod(CHARSET_INFO * cs,const char * str,size_t length)163     Converter_strntod(CHARSET_INFO *cs, const char *str, size_t length)
164     {
165       m_result= my_strntod(cs, (char *) str, length, &m_end_of_num, &m_error);
166       // strntod() does not set an error if the input string was empty
167       m_edom= m_error !=0 || str == m_end_of_num;
168     }
result()169     double result() const { return m_result; }
170   };
171 
172   class Converter_string_to_longlong: public Converter_string_to_number
173   {
174   protected:
175     longlong m_result;
176   public:
result()177     longlong result() const { return m_result; }
178   };
179 
180   class Converter_strntoll: public Converter_string_to_longlong
181   {
182   public:
Converter_strntoll(CHARSET_INFO * cs,const char * str,size_t length)183     Converter_strntoll(CHARSET_INFO *cs, const char *str, size_t length)
184     {
185       m_result= my_strntoll(cs, str, length, 10, &m_end_of_num, &m_error);
186       /*
187          All non-zero errors means EDOM error.
188          strntoll() does not set an error if the input string was empty.
189          Check it here.
190          Notice the different with the same condition in Converter_strntoll10.
191       */
192       m_edom= m_error != 0 || str == m_end_of_num;
193     }
194   };
195 
196   class Converter_strtoll10: public Converter_string_to_longlong
197   {
198   public:
Converter_strtoll10(CHARSET_INFO * cs,const char * str,size_t length)199     Converter_strtoll10(CHARSET_INFO *cs, const char *str, size_t length)
200     {
201       m_end_of_num= (char *) str + length;
202       m_result= (*(cs->cset->strtoll10))(cs, str, &m_end_of_num, &m_error);
203       /*
204         Negative error means "good negative number".
205         Only a positive m_error value means a real error.
206         strtoll10() sets error to MY_ERRNO_EDOM in case of an empty string,
207         so we don't have to additionally catch empty strings here.
208       */
209       m_edom= m_error > 0;
210     }
211   };
212 
213   class Converter_str2my_decimal: public Converter_string_to_number
214   {
215   public:
Converter_str2my_decimal(uint mask,CHARSET_INFO * cs,const char * str,size_t length,my_decimal * buf)216     Converter_str2my_decimal(uint mask,
217                              CHARSET_INFO *cs, const char *str, size_t length,
218                              my_decimal *buf)
219     {
220       DBUG_ASSERT(length < UINT_MAX32);
221       m_error= str2my_decimal(mask, str, length, cs,
222                               buf, (const char **) &m_end_of_num);
223       // E_DEC_TRUNCATED means a very minor truncation: '1e-100' -> 0
224       m_edom= m_error && m_error != E_DEC_TRUNCATED;
225     }
226   };
227 
228 
229   // String-to-number converters with automatic warning generation
230   class Converter_strntod_with_warn: public Converter_strntod
231   {
232   public:
Converter_strntod_with_warn(THD * thd,Warn_filter filter,CHARSET_INFO * cs,const char * str,size_t length)233     Converter_strntod_with_warn(THD *thd, Warn_filter filter,
234                                 CHARSET_INFO *cs,
235                                 const char *str, size_t length)
236       :Converter_strntod(cs, str, length)
237     {
238       check_edom_and_truncation(thd, filter, "DOUBLE", cs, str, length);
239     }
240   };
241 
242   class Converter_strntoll_with_warn: public Converter_strntoll
243   {
244   public:
Converter_strntoll_with_warn(THD * thd,Warn_filter filter,CHARSET_INFO * cs,const char * str,size_t length)245     Converter_strntoll_with_warn(THD *thd, Warn_filter filter,
246                                  CHARSET_INFO *cs,
247                                  const char *str, size_t length)
248       :Converter_strntoll(cs, str, length)
249     {
250       check_edom_and_truncation(thd, filter, "INTEGER", cs, str, length);
251     }
252   };
253 
254   class Converter_strtoll10_with_warn: public Converter_strtoll10
255   {
256   public:
Converter_strtoll10_with_warn(THD * thd,Warn_filter filter,CHARSET_INFO * cs,const char * str,size_t length)257     Converter_strtoll10_with_warn(THD *thd, Warn_filter filter,
258                                  CHARSET_INFO *cs,
259                                  const char *str, size_t length)
260       :Converter_strtoll10(cs, str, length)
261     {
262       check_edom_and_truncation(thd, filter, "INTEGER", cs, str, length);
263     }
264   };
265 
266   class Converter_str2my_decimal_with_warn: public Converter_str2my_decimal
267   {
268   public:
Converter_str2my_decimal_with_warn(THD * thd,Warn_filter filter,uint mask,CHARSET_INFO * cs,const char * str,size_t length,my_decimal * buf)269     Converter_str2my_decimal_with_warn(THD *thd, Warn_filter filter,
270                                        uint mask, CHARSET_INFO *cs,
271                                        const char *str, size_t length,
272                                        my_decimal *buf)
273      :Converter_str2my_decimal(mask, cs, str, length, buf)
274     {
275       check_edom_and_truncation(thd, filter, "DECIMAL", cs, str, length);
276     }
277   };
278 
279 
280   // String-to-number conversion methods for the old code compatibility
longlong_from_string_with_check(CHARSET_INFO * cs,const char * cptr,const char * end)281   longlong longlong_from_string_with_check(CHARSET_INFO *cs, const char *cptr,
282                                            const char *end) const
283   {
284     /*
285       TODO: Give error if we wanted a signed integer and we got an unsigned
286       one
287 
288       Notice, longlong_from_string_with_check() honors thd->no_error, because
289       it's used to handle queries like this:
290         SELECT COUNT(@@basedir);
291       and is called when Item_func_get_system_var::update_null_value()
292       suppresses warnings and then calls val_int().
293       The other methods {double|decimal}_from_string_with_check() ignore
294       thd->no_errors, because they are not used for update_null_value()
295       and they always allow all kind of warnings.
296     */
297     THD *thd= current_thd;
298     return Converter_strtoll10_with_warn(thd, Warn_filter(thd),
299                                          cs, cptr, end - cptr).result();
300   }
301 
double_from_string_with_check(CHARSET_INFO * cs,const char * cptr,const char * end)302   double double_from_string_with_check(CHARSET_INFO *cs, const char *cptr,
303                                        const char *end) const
304   {
305     return Converter_strntod_with_warn(NULL, Warn_filter_all(),
306                                        cs, cptr, end - cptr).result();
307   }
decimal_from_string_with_check(my_decimal * decimal_value,CHARSET_INFO * cs,const char * cptr,const char * end)308   my_decimal *decimal_from_string_with_check(my_decimal *decimal_value,
309                                              CHARSET_INFO *cs,
310                                              const char *cptr,
311                                              const char *end)
312   {
313     Converter_str2my_decimal_with_warn(NULL, Warn_filter_all(),
314                                        E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
315                                        cs, cptr, end - cptr, decimal_value);
316     return decimal_value;
317   }
318 
longlong_from_hex_hybrid(const char * str,size_t length)319   longlong longlong_from_hex_hybrid(const char *str, size_t length)
320   {
321     const char *end= str + length;
322     const char *ptr= end - MY_MIN(length, sizeof(longlong));
323     ulonglong value= 0;
324     for ( ; ptr != end ; ptr++)
325       value= (value << 8) + (ulonglong) (uchar) *ptr;
326     return (longlong) value;
327   }
328 
longlong_from_string_with_check(const String * str)329   longlong longlong_from_string_with_check(const String *str) const
330   {
331     return longlong_from_string_with_check(str->charset(),
332                                            str->ptr(), str->end());
333   }
double_from_string_with_check(const String * str)334   double double_from_string_with_check(const String *str) const
335   {
336     return double_from_string_with_check(str->charset(),
337                                          str->ptr(), str->end());
338   }
decimal_from_string_with_check(my_decimal * decimal_value,const String * str)339   my_decimal *decimal_from_string_with_check(my_decimal *decimal_value,
340                                              const String *str)
341   {
342     return decimal_from_string_with_check(decimal_value, str->charset(),
343                                           str->ptr(), str->end());
344   }
345   // End of String-to-number conversion methods
346 
347 public:
348   /*
349     The enumeration Subst_constraint is currently used only in implementations
350     of the virtual function subst_argument_checker.
351   */
352   enum Subst_constraint
353   {
354     ANY_SUBST,           /* Any substitution for a field is allowed  */
355     IDENTITY_SUBST       /* Substitution for a field is allowed if any two
356                             different values of the field type are not equal */
357   };
358   /*
359     Item context attributes.
360     Comparison functions pass their attributes to propagate_equal_fields().
361     For example, for string comparison, the collation of the comparison
362     operation is important inside propagate_equal_fields().
363   */
364   class Context
365   {
366     /*
367       Which type of propagation is allowed:
368       - ANY_SUBST (loose equality, according to the collation), or
369       - IDENTITY_SUBST (strict binary equality).
370     */
371     Subst_constraint m_subst_constraint;
372     /*
373       Comparison type.
374       Important only when ANY_SUBSTS.
375     */
376     const Type_handler *m_compare_handler;
377     /*
378       Collation of the comparison operation.
379       Important only when ANY_SUBST.
380     */
381     CHARSET_INFO *m_compare_collation;
382   public:
Context(Subst_constraint subst,const Type_handler * h,CHARSET_INFO * cs)383     Context(Subst_constraint subst, const Type_handler *h, CHARSET_INFO *cs)
384       :m_subst_constraint(subst),
385        m_compare_handler(h),
386        m_compare_collation(cs)
387     { DBUG_ASSERT(h == h->type_handler_for_comparison()); }
subst_constraint()388     Subst_constraint subst_constraint() const { return m_subst_constraint; }
compare_type_handler()389     const Type_handler *compare_type_handler() const
390     {
391       DBUG_ASSERT(m_subst_constraint == ANY_SUBST);
392       return m_compare_handler;
393     }
compare_collation()394     CHARSET_INFO *compare_collation() const
395     {
396       DBUG_ASSERT(m_subst_constraint == ANY_SUBST);
397       return m_compare_collation;
398     }
399   };
400   class Context_identity: public Context
401   { // Use this to request only exact value, no invariants.
402   public:
Context_identity()403      Context_identity()
404       :Context(IDENTITY_SUBST, &type_handler_long_blob, &my_charset_bin) { }
405   };
406   class Context_boolean: public Context
407   { // Use this when an item is [a part of] a boolean expression
408   public:
Context_boolean()409     Context_boolean()
410       :Context(ANY_SUBST, &type_handler_longlong, &my_charset_bin) { }
411   };
412 };
413 
414 
415 #define STORAGE_TYPE_MASK 7
416 #define COLUMN_FORMAT_MASK 7
417 #define COLUMN_FORMAT_SHIFT 3
418 
419 /* The length of the header part for each virtual column in the .frm file */
420 #define FRM_VCOL_OLD_HEADER_SIZE(b) (3 + MY_TEST(b))
421 #define FRM_VCOL_NEW_BASE_SIZE 16
422 #define FRM_VCOL_NEW_HEADER_SIZE 6
423 
424 class Count_distinct_field;
425 
426 struct ha_field_option_struct;
427 
428 struct st_cache_field;
429 int field_conv(Field *to,Field *from);
430 int truncate_double(double *nr, uint field_length, uint dec,
431                     bool unsigned_flag, double max_value);
432 
get_enum_pack_length(int elements)433 inline uint get_enum_pack_length(int elements)
434 {
435   return elements < 256 ? 1 : 2;
436 }
437 
get_set_pack_length(int elements)438 inline uint get_set_pack_length(int elements)
439 {
440   uint len= (elements + 7) / 8;
441   return len > 4 ? 8 : len;
442 }
443 
444 
445 /**
446   Tests if field type is temporal and has date part,
447   i.e. represents DATE, DATETIME or TIMESTAMP types in SQL.
448 
449   @param type    Field type, as returned by field->type().
450   @retval true   If field type is temporal type with date part.
451   @retval false  If field type is not temporal type with date part.
452 */
is_temporal_type_with_date(enum_field_types type)453 inline bool is_temporal_type_with_date(enum_field_types type)
454 {
455   switch (type)
456   {
457   case MYSQL_TYPE_DATE:
458   case MYSQL_TYPE_DATETIME:
459   case MYSQL_TYPE_TIMESTAMP:
460     return true;
461   case MYSQL_TYPE_DATETIME2:
462   case MYSQL_TYPE_TIMESTAMP2:
463     DBUG_ASSERT(0); // field->real_type() should not get to here.
464     /* fall through */
465   default:
466     return false;
467   }
468 }
469 
470 
471 /**
472   Convert temporal real types as returned by field->real_type()
473   to field type as returned by field->type().
474 
475   @param real_type  Real type.
476   @retval           Field type.
477 */
real_type_to_type(enum_field_types real_type)478 inline enum_field_types real_type_to_type(enum_field_types real_type)
479 {
480   switch (real_type)
481   {
482   case MYSQL_TYPE_TIME2:
483     return MYSQL_TYPE_TIME;
484   case MYSQL_TYPE_DATETIME2:
485     return MYSQL_TYPE_DATETIME;
486   case MYSQL_TYPE_TIMESTAMP2:
487     return MYSQL_TYPE_TIMESTAMP;
488   case MYSQL_TYPE_NEWDATE:
489     return MYSQL_TYPE_DATE;
490   /* Note: NEWDECIMAL is a type, not only a real_type */
491   default: return real_type;
492   }
493 }
494 
495 
496 enum enum_vcol_info_type
497 {
498   VCOL_GENERATED_VIRTUAL, VCOL_GENERATED_STORED,
499   VCOL_DEFAULT, VCOL_CHECK_FIELD, VCOL_CHECK_TABLE,
500   /* Additional types should be added here */
501   /* Following is the highest value last   */
502   VCOL_TYPE_NONE = 127 // Since the 0 value is already in use
503 };
504 
vcol_type_name(enum_vcol_info_type type)505 static inline const char *vcol_type_name(enum_vcol_info_type type)
506 {
507   switch (type)
508   {
509   case VCOL_GENERATED_VIRTUAL:
510   case VCOL_GENERATED_STORED:
511     return "GENERATED ALWAYS AS";
512   case VCOL_DEFAULT:
513     return "DEFAULT";
514   case VCOL_CHECK_FIELD:
515   case VCOL_CHECK_TABLE:
516     return "CHECK";
517   case VCOL_TYPE_NONE:
518     return "UNTYPED";
519   }
520   return 0;
521 }
522 
523 /*
524   Flags for Virtual_column_info. If none is set, the expression must be
525   a constant with no side-effects, so it's calculated at CREATE TABLE time,
526   stored in table->record[2], and not recalculated for every statement.
527 */
528 #define VCOL_FIELD_REF         1
529 #define VCOL_NON_DETERMINISTIC 2
530 #define VCOL_SESSION_FUNC      4  /* uses session data, e.g. USER or DAYNAME */
531 #define VCOL_TIME_FUNC         8
532 #define VCOL_AUTO_INC         16
533 #define VCOL_IMPOSSIBLE       32
534 #define VCOL_NOT_VIRTUAL      64  /* Function can't be virtual */
535 
536 #define VCOL_NOT_STRICTLY_DETERMINISTIC                       \
537   (VCOL_NON_DETERMINISTIC | VCOL_TIME_FUNC | VCOL_SESSION_FUNC)
538 
539 /*
540   Virtual_column_info is the class to contain additional
541   characteristics that is specific for a virtual/computed
542   field such as:
543    - the defining expression that is evaluated to compute the value
544   of the field
545   - whether the field is to be stored in the database
546   - whether the field is used in a partitioning expression
547 */
548 
549 class Virtual_column_info: public Sql_alloc
550 {
551 private:
552   enum_vcol_info_type vcol_type; /* Virtual column expression type */
553   /*
554     The following data is only updated by the parser and read
555     when a Create_field object is created/initialized.
556   */
557   enum_field_types field_type;   /* Real field type*/
558   /* Flag indicating that the field used in a partitioning expression */
559   bool in_partitioning_expr;
560 
561 public:
562   /* Flag indicating  that the field is physically stored in the database */
563   bool stored_in_db;
564   bool utf8;                                    /* Already in utf8 */
565   bool automatic_name;
566   Item *expr;
567   LEX_CSTRING name;                             /* Name of constraint */
568   /* see VCOL_* (VCOL_FIELD_REF, ...) */
569   uint flags;
570 
Virtual_column_info()571   Virtual_column_info()
572   : vcol_type((enum_vcol_info_type)VCOL_TYPE_NONE),
573     field_type((enum enum_field_types)MYSQL_TYPE_VIRTUAL),
574     in_partitioning_expr(FALSE), stored_in_db(FALSE),
575     utf8(TRUE), automatic_name(FALSE), expr(NULL), flags(0)
576   {
577     name.str= NULL;
578     name.length= 0;
579   };
~Virtual_column_info()580   ~Virtual_column_info() {}
get_vcol_type()581   enum_vcol_info_type get_vcol_type() const
582   {
583     return vcol_type;
584   }
set_vcol_type(enum_vcol_info_type v_type)585   void set_vcol_type(enum_vcol_info_type v_type)
586   {
587     vcol_type= v_type;
588   }
get_vcol_type_name()589   const char *get_vcol_type_name() const
590   {
591     DBUG_ASSERT(vcol_type != VCOL_TYPE_NONE);
592     return vcol_type_name(vcol_type);
593   }
get_real_type()594   enum_field_types get_real_type() const
595   {
596     return field_type;
597   }
set_field_type(enum_field_types fld_type)598   void set_field_type(enum_field_types fld_type)
599   {
600     /* Calling this function can only be done once. */
601     field_type= fld_type;
602   }
is_stored()603   bool is_stored() const
604   {
605     return stored_in_db;
606   }
set_stored_in_db_flag(bool stored)607   void set_stored_in_db_flag(bool stored)
608   {
609     stored_in_db= stored;
610   }
is_in_partitioning_expr()611   bool is_in_partitioning_expr() const
612   {
613     return in_partitioning_expr;
614   }
mark_as_in_partitioning_expr()615   void mark_as_in_partitioning_expr()
616   {
617     in_partitioning_expr= TRUE;
618   }
619   inline bool is_equal(const Virtual_column_info* vcol) const;
620   inline void print(String*);
621 };
622 
623 class Field: public Value_source
624 {
625   Field(const Item &);				/* Prevent use of these */
626   void operator=(Field &);
627 protected:
save_in_field_str(Field * to)628   int save_in_field_str(Field *to)
629   {
630     StringBuffer<MAX_FIELD_WIDTH> result(charset());
631     val_str(&result);
632     return to->store(result.ptr(), result.length(), charset());
633   }
634   void error_generated_column_function_is_not_allowed(THD *thd, bool error)
635                                                       const;
636   static void do_field_int(Copy_field *copy);
637   static void do_field_real(Copy_field *copy);
638   static void do_field_string(Copy_field *copy);
639   static void do_field_temporal(Copy_field *copy);
640   static void do_field_timestamp(Copy_field *copy);
641   static void do_field_decimal(Copy_field *copy);
642 public:
new(size_t size,MEM_ROOT * mem_root)643   static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
644   { return alloc_root(mem_root, size); }
new(size_t size)645   static void *operator new(size_t size) throw ()
646   {
647     DBUG_ASSERT(size < UINT_MAX32);
648     return thd_alloc(current_thd, (uint) size);
649   }
delete(void * ptr_arg,size_t size)650   static void operator delete(void *ptr_arg, size_t size) { TRASH_FREE(ptr_arg, size); }
delete(void * ptr,MEM_ROOT * mem_root)651   static void operator delete(void *ptr, MEM_ROOT *mem_root)
652   { DBUG_ASSERT(0); }
653 
654   /**
655      Used by System Versioning.
656    */
set_max()657   virtual void set_max()
658   { DBUG_ASSERT(0); }
is_max()659   virtual bool is_max()
660   { DBUG_ASSERT(0); return false; }
661 
662   uchar		*ptr;			// Position to field in record
663 
664   field_visibility_t invisible;
665   /**
666      Byte where the @c NULL bit is stored inside a record. If this Field is a
667      @c NOT @c NULL field, this member is @c NULL.
668   */
669   uchar		*null_ptr;
670   /*
671     Note that you can use table->in_use as replacement for current_thd member
672     only inside of val_*() and store() members (e.g. you can't use it in cons)
673   */
674   TABLE *table;                                 // Pointer for table
675   TABLE *orig_table;                            // Pointer to original table
676   const char * const *table_name;               // Pointer to alias in TABLE
677   LEX_CSTRING field_name;
678   LEX_CSTRING comment;
679   /** reference to the list of options or NULL */
680   engine_option_value *option_list;
681   ha_field_option_struct *option_struct;   /* structure with parsed options */
682   /* Field is part of the following keys */
683   key_map	key_start, part_of_key, part_of_key_not_clustered;
684 
685   /*
686     Bitmap of indexes that have records ordered by col1, ... this_field, ...
687 
688     For example, INDEX (col(prefix_n)) is not present in col.part_of_sortkey.
689   */
690   key_map       part_of_sortkey;
691   /*
692     We use three additional unireg types for TIMESTAMP to overcome limitation
693     of current binary format of .frm file. We'd like to be able to support
694     NOW() as default and on update value for such fields but unable to hold
695     this info anywhere except unireg_check field. This issue will be resolved
696     in more clean way with transition to new text based .frm format.
697     See also comment for Field_timestamp::Field_timestamp().
698   */
699   enum utype  {
700     NONE=0,
701     NEXT_NUMBER=15,             // AUTO_INCREMENT
702     TIMESTAMP_OLD_FIELD=18,     // TIMESTAMP created before 4.1.3
703     TIMESTAMP_DN_FIELD=21,      // TIMESTAMP DEFAULT NOW()
704     TIMESTAMP_UN_FIELD=22,      // TIMESTAMP ON UPDATE NOW()
705     TIMESTAMP_DNUN_FIELD=23,    // TIMESTAMP DEFAULT NOW() ON UPDATE NOW()
706     TMYSQL_COMPRESSED= 24,      // Compatibility with TMySQL
707     };
708   enum geometry_type
709   {
710     GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
711     GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
712     GEOM_GEOMETRYCOLLECTION = 7
713   };
714   enum imagetype { itRAW, itMBR};
715 
716   utype		unireg_check;
717   uint32	field_length;		// Length of field
718   uint32	flags;
719   uint16        field_index;            // field number in fields array
720   uchar		null_bit;		// Bit used to test null bit
721   /**
722      If true, this field was created in create_tmp_field_from_item from a NULL
723      value. This means that the type of the field is just a guess, and the type
724      may be freely coerced to another type.
725 
726      @see create_tmp_field_from_item
727      @see Item_type_holder::get_real_type
728 
729    */
730   bool is_created_from_null_item;
731 
732   /*
733     Selectivity of the range condition over this field.
734     When calculating this selectivity a range predicate
735     is taken into account only if:
736     - it is extracted from the WHERE clause
737     - it depends only on the table the field belongs to
738   */
739   double cond_selectivity;
740 
741   /*
742     The next field in the class of equal fields at the top AND level
743     of the WHERE clause
744   */
745   Field *next_equal_field;
746 
747   /*
748     This structure is used for statistical data on the column
749     that has been read from the statistical table column_stat
750   */
751   Column_statistics *read_stats;
752   /*
753     This structure is used for statistical data on the column that
754     is collected by the function collect_statistics_for_table
755   */
756   Column_statistics_collected *collected_stats;
757 
758   /*
759     This is additional data provided for any computed(virtual) field,
760     default function or check constraint.
761     In particular it includes a pointer to the item by which this field
762     can be computed from other fields.
763   */
764   Virtual_column_info *vcol_info, *check_constraint, *default_value;
765 
766   Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
767         uchar null_bit_arg, utype unireg_check_arg,
768         const LEX_CSTRING *field_name_arg);
~Field()769   virtual ~Field() {}
770 
dtcollation()771   DTCollation dtcollation() const
772   {
773     return DTCollation(charset(), derivation(), repertoire());
774   }
type_std_attributes()775   virtual Type_std_attributes type_std_attributes() const
776   {
777     return Type_std_attributes(field_length, decimals(),
778                                MY_TEST(flags & UNSIGNED_FLAG),
779                                dtcollation());
780   }
781 
is_unsigned()782   bool is_unsigned() const { return flags & UNSIGNED_FLAG; }
783 
784   /**
785     Convenience definition of a copy function returned by
786     Field::get_copy_func()
787   */
788   typedef void Copy_func(Copy_field*);
789   virtual Copy_func *get_copy_func(const Field *from) const= 0;
790   /* Store functions returns 1 on overflow and -1 on fatal error */
store_field(Field * from)791   virtual int  store_field(Field *from) { return from->save_in_field(this); }
792   virtual int  save_in_field(Field *to)= 0;
793   /**
794     Check if it is possible just copy the value
795     of the field 'from' to the field 'this', e.g. for
796       INSERT INTO t1 (field1) SELECT field2 FROM t2;
797     @param from   - The field to copy from
798     @retval true  - it is possible to just copy value of 'from' to 'this'
799     @retval false - conversion is needed
800   */
801   virtual bool memcpy_field_possible(const Field *from) const= 0;
802   virtual int  store(const char *to, size_t length,CHARSET_INFO *cs)=0;
803   virtual int  store_hex_hybrid(const char *str, size_t length);
804   virtual int  store(double nr)=0;
805   virtual int  store(longlong nr, bool unsigned_val)=0;
806   virtual int  store_decimal(const my_decimal *d)=0;
807   virtual int  store_time_dec(const MYSQL_TIME *ltime, uint dec);
808   virtual int  store_timestamp(my_time_t timestamp, ulong sec_part);
store_time(const MYSQL_TIME * ltime)809   int store_time(const MYSQL_TIME *ltime)
810   { return store_time_dec(ltime, TIME_SECOND_PART_DIGITS); }
811   int store(const char *to, size_t length, CHARSET_INFO *cs,
812             enum_check_fields check_level);
store(const LEX_STRING * ls,CHARSET_INFO * cs)813   int store(const LEX_STRING *ls, CHARSET_INFO *cs)
814   {
815     DBUG_ASSERT(ls->length < UINT_MAX32);
816     return store(ls->str, (uint) ls->length, cs);
817   }
store(const LEX_CSTRING * ls,CHARSET_INFO * cs)818   int store(const LEX_CSTRING *ls, CHARSET_INFO *cs)
819   {
820     DBUG_ASSERT(ls->length < UINT_MAX32);
821     return store(ls->str, (uint) ls->length, cs);
822   }
store(const LEX_CSTRING & ls,CHARSET_INFO * cs)823   int store(const LEX_CSTRING &ls, CHARSET_INFO *cs)
824   {
825     DBUG_ASSERT(ls.length < UINT_MAX32);
826     return store(ls.str, (uint) ls.length, cs);
827   }
828 
829 #ifdef HAVE_valgrind
830   /**
831     Mark unused memory in the field as defined. Mainly used to ensure
832     that if we write full field to disk (for example in
833     Count_distinct_field::add(), we don't write unitalized data to
834     disk which would confuse valgrind or MSAN.
835   */
mark_unused_memory_as_defined()836   virtual void mark_unused_memory_as_defined() {}
837 #else
mark_unused_memory_as_defined()838   void mark_unused_memory_as_defined() {}
839 #endif
840 
841   virtual double val_real(void)=0;
842   virtual longlong val_int(void)=0;
843   /*
844     Get ulonglong representation.
845     Negative values are truncated to 0.
846   */
val_uint(void)847   virtual ulonglong val_uint(void)
848   {
849     longlong nr= val_int();
850     return nr < 0 ? 0 : (ulonglong) nr;
851   }
852   virtual bool val_bool(void)= 0;
853   virtual my_decimal *val_decimal(my_decimal *);
val_str(String * str)854   inline String *val_str(String *str) { return val_str(str, str); }
855   /*
856      val_str(buf1, buf2) gets two buffers and should use them as follows:
857      if it needs a temp buffer to convert result to string - use buf1
858        example Field_tiny::val_str()
859      if the value exists as a string already - use buf2
860        example Field_string::val_str()
861      consequently, buf2 may be created as 'String buf;' - no memory
862      will be allocated for it. buf1 will be allocated to hold a
863      value if it's too small. Using allocated buffer for buf2 may result in
864      an unnecessary free (and later, may be an alloc).
865      This trickery is used to decrease a number of malloc calls.
866   */
867   virtual String *val_str(String*,String *)=0;
868   String *val_int_as_str(String *val_buffer, bool unsigned_flag);
869   /*
870     Return the field value as a LEX_CSTRING, without padding to full length
871     (MODE_PAD_CHAR_TO_FULL_LENGTH is temporarily suppressed during the call).
872 
873     In case of an empty value, to[0] is assigned to empty_clex_string,
874     memory is not allocated.
875     In case of a non-empty value, the memory is allocated on mem_root.
876     In case of a memory allocation failure, to[0] is assigned to {NULL,0}.
877 
878     @param  [IN] mem_root  store non-empty values here
879     @param  [OUT to        return the string here
880     @retval                false (success)
881     @retval                true  (EOM)
882   */
883   bool val_str_nopad(MEM_ROOT *mem_root, LEX_CSTRING *to);
884   fast_field_copier get_fast_field_copier(const Field *from);
885   /*
886    str_needs_quotes() returns TRUE if the value returned by val_str() needs
887    to be quoted when used in constructing an SQL query.
888   */
str_needs_quotes()889   virtual bool str_needs_quotes() { return FALSE; }
result_type()890   Item_result result_type () const
891   {
892     return type_handler()->result_type();
893   }
cmp_type()894   Item_result cmp_type () const
895   {
896     return type_handler()->cmp_type();
897   }
898   static enum_field_types field_type_merge(enum_field_types, enum_field_types);
eq(Field * field)899   virtual bool eq(Field *field)
900   {
901     return (ptr == field->ptr && null_ptr == field->null_ptr &&
902             null_bit == field->null_bit && field->type() == type());
903   }
904   virtual bool eq_def(const Field *field) const;
905 
906   /*
907     pack_length() returns size (in bytes) used to store field data in memory
908     (i.e. it returns the maximum size of the field in a row of the table,
909     which is located in RAM).
910   */
pack_length()911   virtual uint32 pack_length() const { return (uint32) field_length; }
912 
913   /*
914     pack_length_in_rec() returns size (in bytes) used to store field data on
915     storage (i.e. it returns the maximal size of the field in a row of the
916     table, which is located on disk).
917   */
pack_length_in_rec()918   virtual uint32 pack_length_in_rec() const { return pack_length(); }
919   virtual bool compatible_field_size(uint metadata, Relay_log_info *rli,
920                                      uint16 mflags, int *order);
pack_length_from_metadata(uint field_metadata)921   virtual uint pack_length_from_metadata(uint field_metadata)
922   {
923     DBUG_ENTER("Field::pack_length_from_metadata");
924     DBUG_RETURN(field_metadata);
925   }
row_pack_length()926   virtual uint row_pack_length() const { return 0; }
927 
928 
929   /**
930      Retrieve the field metadata for fields.
931 
932      This default implementation returns 0 and saves 0 in the first_byte value.
933 
934      @param   first_byte   First byte of field metadata
935 
936      @returns 0 no bytes written.
937   */
938 
save_field_metadata(uchar * first_byte)939   virtual int save_field_metadata(uchar *first_byte)
940   { return 0; }
941 
942 
943   /*
944     data_length() return the "real size" of the data in memory.
945   */
data_length()946   virtual uint32 data_length() { return pack_length(); }
sort_length()947   virtual uint32 sort_length() const { return pack_length(); }
948 
949   /*
950     Get the number bytes occupied by the value in the field.
951     CHAR values are stripped of trailing spaces.
952     Flexible values are stripped of their length.
953   */
value_length()954   virtual uint32 value_length()
955   {
956     uint len;
957     if (!zero_pack() &&
958 	(type() == MYSQL_TYPE_STRING &&
959         (len= pack_length()) >= 4 && len < 256))
960     {
961       uchar *str, *end;
962       for (str= ptr, end= str+len; end > str && end[-1] == ' '; end--) {}
963       len=(uint) (end-str);
964       return len;
965     }
966     return data_length();
967   }
968 
969   /**
970      Get the maximum size of the data in packed format.
971 
972      @return Maximum data length of the field when packed using the
973      Field::pack() function.
974    */
max_data_length()975   virtual uint32 max_data_length() const {
976     return pack_length();
977   };
978 
reset(void)979   virtual int reset(void) { bzero(ptr,pack_length()); return 0; }
reset_fields()980   virtual void reset_fields() {}
ptr_in_record(const uchar * record)981   const uchar *ptr_in_record(const uchar *record) const
982   {
983     my_ptrdiff_t l_offset= (my_ptrdiff_t) (ptr -  table->record[0]);
984     DBUG_ASSERT(l_offset >= 0 && table->s->rec_buff_length - l_offset > 0);
985     return record + l_offset;
986   }
987   virtual int set_default();
988 
has_update_default_function()989   bool has_update_default_function() const
990   {
991     return flags & ON_UPDATE_NOW_FLAG;
992   }
has_default_now_unireg_check()993   bool has_default_now_unireg_check() const
994   {
995     return unireg_check == TIMESTAMP_DN_FIELD
996         || unireg_check == TIMESTAMP_DNUN_FIELD;
997   }
998 
999   /*
1000     Mark the field as having a value supplied by the client, thus it should
1001     not be auto-updated.
1002   */
set_has_explicit_value()1003   void set_has_explicit_value()
1004   {
1005     bitmap_set_bit(&table->has_value_set, field_index);
1006   }
has_explicit_value()1007   bool has_explicit_value()
1008   {
1009     return bitmap_is_set(&table->has_value_set, field_index);
1010   }
clear_has_explicit_value()1011   void clear_has_explicit_value()
1012   {
1013     bitmap_clear_bit(&table->has_value_set, field_index);
1014   }
1015 
get_timestamp(const uchar * pos,ulong * sec_part)1016   virtual my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const
1017   { DBUG_ASSERT(0); return 0; }
get_timestamp(ulong * sec_part)1018   my_time_t get_timestamp(ulong *sec_part) const
1019   {
1020     return get_timestamp(ptr, sec_part);
1021   }
1022 
binary()1023   virtual bool binary() const { return 1; }
zero_pack()1024   virtual bool zero_pack() const { return 1; }
key_type()1025   virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
key_length()1026   virtual uint32 key_length() const { return pack_length(); }
1027   virtual const Type_handler *type_handler() const= 0;
type()1028   virtual enum_field_types type() const
1029   {
1030     return type_handler()->field_type();
1031   }
real_type()1032   virtual enum_field_types real_type() const
1033   {
1034     return type_handler()->real_field_type();
1035   }
binlog_type()1036   virtual enum_field_types binlog_type() const
1037   {
1038     /*
1039       Binlog stores field->type() as type code by default. For example,
1040       it puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
1041       with extra data type details put into metadata.
1042 
1043       Binlog behaviour slightly differs between various MySQL and MariaDB
1044       versions for the temporal data types TIME, DATETIME and TIMESTAMP.
1045 
1046       MySQL prior to 5.6 uses MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
1047       and MYSQL_TYPE_TIMESTAMP type codes in binlog and stores no
1048       additional metadata.
1049 
1050       MariaDB-5.3 implements new versions for TIME, DATATIME, TIMESTAMP
1051       with fractional second precision, but uses the old format for the
1052       types TIME(0), DATETIME(0), TIMESTAMP(0), and it still stores
1053       MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME and MYSQL_TYPE_TIMESTAMP in binlog,
1054       with no additional metadata.
1055       So row-based replication between temporal data types of
1056       different precision is not possible in MariaDB.
1057 
1058       MySQL-5.6 also implements a new version of TIME, DATETIME, TIMESTAMP
1059       which support fractional second precision 0..6, and use the new
1060       format even for the types TIME(0), DATETIME(0), TIMESTAMP(0).
1061       For these new data types, MySQL-5.6 stores new type codes
1062       MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2 in binlog,
1063       with fractional precision 0..6 put into metadata.
1064       This makes it in theory possible to do row-based replication between
1065       columns of different fractional precision (e.g. from TIME(1) on master
1066       to TIME(6) on slave). However, it's not currently fully implemented yet.
1067       MySQL-5.6 can only do row-based replication from the old types
1068       TIME, DATETIME, TIMESTAMP (represented by MYSQL_TYPE_TIME,
1069       MYSQL_TYPE_DATETIME and MYSQL_TYPE_TIMESTAMP type codes in binlog)
1070       to the new corresponding types TIME(0), DATETIME(0), TIMESTAMP(0).
1071 
1072       Note: MariaDB starting from the version 10.0 understands the new
1073       MySQL-5.6 type codes MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2,
1074       MYSQL_TYPE_TIMESTAMP2. When started over MySQL-5.6 tables both on
1075       master and on slave, MariaDB-10.0 can also do row-based replication
1076       from the old types TIME, DATETIME, TIMESTAMP to the new MySQL-5.6
1077       types TIME(0), DATETIME(0), TIMESTAMP(0).
1078 
1079       Note: perhaps binlog should eventually be modified to store
1080       real_type() instead of type() for all column types.
1081     */
1082     return type();
1083   }
cmp(const uchar * str)1084   inline  int cmp(const uchar *str) { return cmp(ptr,str); }
1085   virtual int cmp(const uchar *,const uchar *)=0;
1086   /*
1087     The following method is used for comparing prefix keys.
1088     Currently it's only used in partitioning.
1089   */
cmp_prefix(const uchar * a,const uchar * b,size_t prefix_len)1090   virtual int cmp_prefix(const uchar *a, const uchar *b, size_t prefix_len)
1091   { return cmp(a, b); }
1092   virtual int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0U)
1093   { return memcmp(a,b,pack_length()); }
cmp_offset(uint row_offset)1094   virtual int cmp_offset(uint row_offset)
1095   { return cmp(ptr,ptr+row_offset); }
cmp_binary_offset(uint row_offset)1096   virtual int cmp_binary_offset(uint row_offset)
1097   { return cmp_binary(ptr, ptr+row_offset); };
key_cmp(const uchar * a,const uchar * b)1098   virtual int key_cmp(const uchar *a,const uchar *b)
1099   { return cmp(a, b); }
key_cmp(const uchar * str,uint length)1100   virtual int key_cmp(const uchar *str, uint length)
1101   { return cmp(ptr,str); }
1102   /*
1103     Update the value m of the 'min_val' field with the current value v
1104     of this field if force_update is set to TRUE or if v < m.
1105     Return TRUE if the value has been updated.
1106   */
update_min(Field * min_val,bool force_update)1107   virtual bool update_min(Field *min_val, bool force_update)
1108   {
1109     bool update_fl= force_update || cmp(ptr, min_val->ptr) < 0;
1110     if (update_fl)
1111     {
1112       min_val->set_notnull();
1113       memcpy(min_val->ptr, ptr, pack_length());
1114     }
1115     return update_fl;
1116   }
1117   /*
1118     Update the value m of the 'max_val' field with the current value v
1119     of this field if force_update is set to TRUE or if v > m.
1120     Return TRUE if the value has been updated.
1121   */
update_max(Field * max_val,bool force_update)1122   virtual bool update_max(Field *max_val, bool force_update)
1123   {
1124     bool update_fl= force_update || cmp(ptr, max_val->ptr) > 0;
1125     if (update_fl)
1126     {
1127       max_val->set_notnull();
1128       memcpy(max_val->ptr, ptr, pack_length());
1129     }
1130     return update_fl;
1131   }
store_field_value(uchar * val,uint len)1132   virtual void store_field_value(uchar *val, uint len)
1133   {
1134      memcpy(ptr, val, len);
1135   }
decimals()1136   virtual uint decimals() const { return 0; }
1137   virtual Information_schema_numeric_attributes
information_schema_numeric_attributes()1138             information_schema_numeric_attributes() const
1139   {
1140     return Information_schema_numeric_attributes();
1141   }
1142   virtual Information_schema_character_attributes
information_schema_character_attributes()1143             information_schema_character_attributes() const
1144   {
1145     return Information_schema_character_attributes();
1146   }
1147   /*
1148     Caller beware: sql_type can change str.Ptr, so check
1149     ptr() to see if it changed if you are using your own buffer
1150     in str and restore it with set() if needed
1151   */
1152   virtual void sql_type(String &str) const =0;
sql_rpl_type(String * str)1153   virtual void sql_rpl_type(String *str) const { sql_type(*str); }
1154   virtual uint size_of() const =0;		// For new field
1155   inline bool is_null(my_ptrdiff_t row_offset= 0) const
1156   {
1157     /*
1158       The table may have been marked as containing only NULL values
1159       for all fields if it is a NULL-complemented row of an OUTER JOIN
1160       or if the query is an implicitly grouped query (has aggregate
1161       functions but no GROUP BY clause) with no qualifying rows. If
1162       this is the case (in which TABLE::null_row is true), the field
1163       is considered to be NULL.
1164 
1165       Note that if a table->null_row is set then also all null_bits are
1166       set for the row.
1167 
1168       In the case of the 'result_field' for GROUP BY, table->null_row might
1169       refer to the *next* row in the table (when the algorithm is: read the
1170       next row, see if any of group column values have changed, send the
1171       result - grouped - row to the client if yes). So, table->null_row might
1172       be wrong, but such a result_field is always nullable (that's defined by
1173       original_field->maybe_null()) and we trust its null bit.
1174     */
1175     return null_ptr ? null_ptr[row_offset] & null_bit : table->null_row;
1176   }
1177   inline bool is_real_null(my_ptrdiff_t row_offset= 0) const
1178     { return null_ptr && (null_ptr[row_offset] & null_bit); }
is_null_in_record(const uchar * record)1179   inline bool is_null_in_record(const uchar *record) const
1180   {
1181     if (maybe_null_in_table())
1182       return record[(uint) (null_ptr - table->record[0])] & null_bit;
1183     return 0;
1184   }
1185   inline void set_null(my_ptrdiff_t row_offset= 0)
1186     { if (null_ptr) null_ptr[row_offset]|= null_bit; }
1187   inline void set_notnull(my_ptrdiff_t row_offset= 0)
1188     { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
maybe_null(void)1189   inline bool maybe_null(void) const
1190   { return null_ptr != 0 || table->maybe_null; }
1191   // Set to NULL on LOAD DATA or LOAD XML
1192   virtual bool load_data_set_null(THD *thd);
1193   // Reset when a LOAD DATA file ended unexpectedly
1194   virtual bool load_data_set_no_data(THD *thd, bool fixed_format);
1195   void load_data_set_value(const char *pos, uint length, CHARSET_INFO *cs);
1196 
1197   /* @return true if this field is NULL-able (even if temporarily) */
real_maybe_null(void)1198   inline bool real_maybe_null(void) const { return null_ptr != 0; }
null_offset(const uchar * record)1199   uint null_offset(const uchar *record) const
1200   { return (uint) (null_ptr - record); }
1201   /*
1202     For a NULL-able field (that can actually store a NULL value in a table)
1203     null_ptr points to the "null bitmap" in the table->record[0] header. For
1204     NOT NULL fields it is either 0 or points outside table->record[0] into the
1205     table->triggers->extra_null_bitmap (so that the field can store a NULL
1206     value temporarily, only in memory)
1207   */
maybe_null_in_table()1208   bool maybe_null_in_table() const
1209   { return null_ptr >= table->record[0] && null_ptr <= ptr; }
1210 
null_offset()1211   uint null_offset() const
1212   { return null_offset(table->record[0]); }
set_null_ptr(uchar * p_null_ptr,uint p_null_bit)1213   void set_null_ptr(uchar *p_null_ptr, uint p_null_bit)
1214   {
1215     null_ptr= p_null_ptr;
1216     null_bit= p_null_bit;
1217   }
1218 
stored_in_db()1219   bool stored_in_db() const { return !vcol_info || vcol_info->stored_in_db; }
1220   bool check_vcol_sql_mode_dependency(THD *, vcol_init_mode mode) const;
1221 
value_depends_on_sql_mode()1222   virtual sql_mode_t value_depends_on_sql_mode() const
1223   {
1224     return 0;
1225   }
can_handle_sql_mode_dependency_on_store()1226   virtual sql_mode_t can_handle_sql_mode_dependency_on_store() const
1227   {
1228     return 0;
1229   }
1230 
get_thd()1231   inline THD *get_thd() const
1232   { return likely(table) ? table->in_use : current_thd; }
1233 
1234   enum {
1235     LAST_NULL_BYTE_UNDEF= 0
1236   };
1237 
1238   /*
1239     Find the position of the last null byte for the field.
1240 
1241     SYNOPSIS
1242       last_null_byte()
1243 
1244     DESCRIPTION
1245       Return a pointer to the last byte of the null bytes where the
1246       field conceptually is placed.
1247 
1248     RETURN VALUE
1249       The position of the last null byte relative to the beginning of
1250       the record. If the field does not use any bits of the null
1251       bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
1252    */
last_null_byte()1253   size_t last_null_byte() const {
1254     size_t bytes= do_last_null_byte();
1255     DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
1256     DBUG_ASSERT(bytes <= table->s->null_bytes);
1257     return bytes;
1258   }
1259 
1260   void make_sort_key(uchar *buff, uint length);
1261   virtual void make_send_field(Send_field *);
1262 
1263   /*
1264     Some implementations actually may write up to 8 bytes regardless of what
1265     size was requested. This is due to the minimum value of the system variable
1266     max_sort_length.
1267   */
1268 
1269   virtual void sort_string(uchar *buff,uint length)=0;
1270   virtual bool optimize_range(uint idx, uint part) const;
free()1271   virtual void free() {}
1272   virtual Field *make_new_field(MEM_ROOT *root, TABLE *new_table,
1273                                 bool keep_type);
1274   virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
1275                                uchar *new_ptr, uint32 length,
1276                                uchar *new_null_ptr, uint new_null_bit);
1277   Field *clone(MEM_ROOT *mem_root, TABLE *new_table);
1278   Field *clone(MEM_ROOT *mem_root, TABLE *new_table, my_ptrdiff_t diff);
move_field(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg)1279   inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
1280   {
1281     ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
1282   }
move_field(uchar * ptr_arg)1283   inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
record_ptr()1284   inline uchar *record_ptr() // record[0] or wherever the field was moved to
1285   {
1286     my_ptrdiff_t offset= table->s->field[field_index]->ptr - table->s->default_values;
1287     return ptr - offset;
1288   }
move_field_offset(my_ptrdiff_t ptr_diff)1289   virtual void move_field_offset(my_ptrdiff_t ptr_diff)
1290   {
1291     ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
1292     if (null_ptr)
1293       null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
1294   }
get_image(uchar * buff,uint length,CHARSET_INFO * cs)1295   virtual void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
1296     { memcpy(buff,ptr,length); }
set_image(const uchar * buff,uint length,CHARSET_INFO * cs)1297   virtual void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
1298     { memcpy(ptr,buff,length); }
1299 
1300 
1301   /*
1302     Copy a field part into an output buffer.
1303 
1304     SYNOPSIS
1305       Field::get_key_image()
1306       buff   [out] output buffer
1307       length       output buffer size
1308       type         itMBR for geometry blobs, otherwise itRAW
1309 
1310     DESCRIPTION
1311       This function makes a copy of field part of size equal to or
1312       less than "length" parameter value.
1313       For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
1314       is padded by zero byte.
1315 
1316     NOTES
1317       For variable length character fields (i.e. UTF-8) the "length"
1318       parameter means a number of output buffer bytes as if all field
1319       characters have maximal possible size (mbmaxlen). In the other words,
1320       "length" parameter is a number of characters multiplied by
1321       field_charset->mbmaxlen.
1322 
1323     RETURN
1324       Number of copied bytes (excluding padded zero bytes -- see above).
1325   */
1326 
get_key_image(uchar * buff,uint length,imagetype type_arg)1327   virtual uint get_key_image(uchar *buff, uint length, imagetype type_arg)
1328   {
1329     get_image(buff, length, &my_charset_bin);
1330     return length;
1331   }
set_key_image(const uchar * buff,uint length)1332   virtual void set_key_image(const uchar *buff,uint length)
1333     { set_image(buff,length, &my_charset_bin); }
val_int_offset(uint row_offset)1334   inline longlong val_int_offset(uint row_offset)
1335     {
1336       ptr+=row_offset;
1337       longlong tmp=val_int();
1338       ptr-=row_offset;
1339       return tmp;
1340     }
val_int(const uchar * new_ptr)1341   inline longlong val_int(const uchar *new_ptr)
1342   {
1343     uchar *old_ptr= ptr;
1344     longlong return_value;
1345     ptr= (uchar*) new_ptr;
1346     return_value= val_int();
1347     ptr= old_ptr;
1348     return return_value;
1349   }
val_str(String * str,const uchar * new_ptr)1350   inline String *val_str(String *str, const uchar *new_ptr)
1351   {
1352     uchar *old_ptr= ptr;
1353     ptr= (uchar*) new_ptr;
1354     val_str(str);
1355     ptr= old_ptr;
1356     return str;
1357   }
1358   virtual bool send_binary(Protocol *protocol);
1359 
1360   virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
1361   /**
1362      @overload Field::pack(uchar*, const uchar*, uint, bool)
1363   */
pack(uchar * to,const uchar * from)1364   uchar *pack(uchar *to, const uchar *from)
1365   {
1366     DBUG_ENTER("Field::pack");
1367     uchar *result= this->pack(to, from, UINT_MAX);
1368     DBUG_RETURN(result);
1369   }
1370 
1371   virtual const uchar *unpack(uchar* to, const uchar *from,
1372                               const uchar *from_end, uint param_data=0);
1373 
packed_col_length(const uchar * to,uint length)1374   virtual uint packed_col_length(const uchar *to, uint length)
1375   { return length;}
max_packed_col_length(uint max_length)1376   virtual uint max_packed_col_length(uint max_length)
1377   { return max_length;}
1378 
offset(uchar * record)1379   uint offset(uchar *record) const
1380   {
1381     return (uint) (ptr - record);
1382   }
1383   void copy_from_tmp(int offset);
1384   uint fill_cache_field(struct st_cache_field *copy);
1385   virtual bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
get_time(MYSQL_TIME * ltime)1386   bool get_time(MYSQL_TIME *ltime) { return get_date(ltime, TIME_TIME_ONLY); }
get_typelib()1387   virtual TYPELIB *get_typelib() const { return NULL; }
charset(void)1388   virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
charset_for_protocol(void)1389   virtual CHARSET_INFO *charset_for_protocol(void) const
1390   { return binary() ? &my_charset_bin : charset(); }
sort_charset(void)1391   virtual CHARSET_INFO *sort_charset(void) const { return charset(); }
has_charset(void)1392   virtual bool has_charset(void) const { return FALSE; }
derivation(void)1393   virtual enum Derivation derivation(void) const
1394   { return DERIVATION_IMPLICIT; }
repertoire(void)1395   virtual uint repertoire(void) const { return MY_REPERTOIRE_UNICODE30; }
set_time()1396   virtual int set_time() { return 1; }
1397   bool set_warning(Sql_condition::enum_warning_level, unsigned int code,
1398                    int cuted_increment, ulong current_row=0) const;
1399 protected:
set_warning(unsigned int code,int cuted_increment)1400   bool set_warning(unsigned int code, int cuted_increment) const
1401   {
1402     return set_warning(Sql_condition::WARN_LEVEL_WARN, code, cuted_increment);
1403   }
set_note(unsigned int code,int cuted_increment)1404   bool set_note(unsigned int code, int cuted_increment) const
1405   {
1406     return set_warning(Sql_condition::WARN_LEVEL_NOTE, code, cuted_increment);
1407   }
1408   void set_datetime_warning(Sql_condition::enum_warning_level, uint code,
1409                             const ErrConv *str, timestamp_type ts_type,
1410                             int cuted_increment) const;
set_datetime_warning(uint code,const ErrConv * str,timestamp_type ts_type,int cuted_increment)1411   void set_datetime_warning(uint code,
1412                             const ErrConv *str, timestamp_type ts_type,
1413                             int cuted_increment) const
1414   {
1415     set_datetime_warning(Sql_condition::WARN_LEVEL_WARN, code, str, ts_type,
1416                          cuted_increment);
1417   }
1418   void set_warning_truncated_wrong_value(const char *type, const char *value);
check_overflow(int op_result)1419   inline bool check_overflow(int op_result)
1420   {
1421     return (op_result == E_DEC_OVERFLOW);
1422   }
1423   int warn_if_overflow(int op_result);
1424   Copy_func *get_identical_copy_func() const;
1425 public:
set_table_name(String * alias)1426   void set_table_name(String *alias)
1427   {
1428     table_name= &alias->Ptr;
1429   }
init(TABLE * table_arg)1430   void init(TABLE *table_arg)
1431   {
1432     orig_table= table= table_arg;
1433     set_table_name(&table_arg->alias);
1434   }
init_for_make_new_field(TABLE * new_table_arg,TABLE * orig_table_arg)1435   void init_for_make_new_field(TABLE *new_table_arg, TABLE *orig_table_arg)
1436   {
1437     init(new_table_arg);
1438     /*
1439       Normally orig_table is different from table only if field was
1440       created via ::make_new_field.  Here we alter the type of field,
1441       so ::make_new_field is not applicable. But we still need to
1442       preserve the original field metadata for the client-server
1443       protocol.
1444     */
1445     orig_table= orig_table_arg;
1446   }
1447 
1448   /* maximum possible display length */
1449   virtual uint32 max_display_length() const= 0;
1450   /**
1451     Whether a field being created is compatible with a existing one.
1452 
1453     Used by the ALTER TABLE code to evaluate whether the new definition
1454     of a table is compatible with the old definition so that it can
1455     determine if data needs to be copied over (table data change).
1456   */
1457   virtual uint is_equal(Create_field *new_field);
1458   /* convert decimal to longlong with overflow check */
1459   longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
1460                                     int *err);
1461   /* The max. number of characters */
char_length()1462   virtual uint32 char_length() const
1463   {
1464     return field_length / charset()->mbmaxlen;
1465   }
1466 
get_geometry_type()1467   virtual geometry_type get_geometry_type()
1468   {
1469     /* shouldn't get here. */
1470     DBUG_ASSERT(0);
1471     return GEOM_GEOMETRY;
1472   }
1473 
field_storage_type()1474   ha_storage_media field_storage_type() const
1475   {
1476     return (ha_storage_media)
1477       ((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
1478   }
1479 
set_storage_type(ha_storage_media storage_type_arg)1480   void set_storage_type(ha_storage_media storage_type_arg)
1481   {
1482     DBUG_ASSERT(field_storage_type() == HA_SM_DEFAULT);
1483     flags |= static_cast<uint32>(storage_type_arg) <<
1484       FIELD_FLAGS_STORAGE_MEDIA;
1485   }
1486 
column_format()1487   column_format_type column_format() const
1488   {
1489     return (column_format_type)
1490       ((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
1491   }
1492 
set_column_format(column_format_type column_format_arg)1493   void set_column_format(column_format_type column_format_arg)
1494   {
1495     DBUG_ASSERT(column_format() == COLUMN_FORMAT_TYPE_DEFAULT);
1496     flags |= static_cast<uint32>(column_format_arg) <<
1497       FIELD_FLAGS_COLUMN_FORMAT;
1498   }
1499 
vers_sys_field()1500   bool vers_sys_field() const
1501   {
1502     return flags & (VERS_ROW_START | VERS_ROW_END);
1503   }
1504 
vers_update_unversioned()1505   bool vers_update_unversioned() const
1506   {
1507     return flags & VERS_UPDATE_UNVERSIONED_FLAG;
1508   }
1509 
1510   /*
1511     Validate a non-null field value stored in the given record
1512     according to the current thread settings, e.g. sql_mode.
1513     @param thd     - the thread
1514     @param record  - the record to check in
1515   */
validate_value_in_record(THD * thd,const uchar * record)1516   virtual bool validate_value_in_record(THD *thd, const uchar *record) const
1517   { return false; }
1518   bool validate_value_in_record_with_warn(THD *thd, const uchar *record);
1519   key_map get_possible_keys();
1520 
1521   /* Hash value */
1522   virtual void hash(ulong *nr, ulong *nr2);
1523 
1524   /**
1525     Get the upper limit of the MySQL integral and floating-point type.
1526 
1527     @return maximum allowed value for the field
1528   */
get_max_int_value()1529   virtual ulonglong get_max_int_value() const
1530   {
1531     DBUG_ASSERT(false);
1532     return 0ULL;
1533   }
1534 
1535 /**
1536   Checks whether a string field is part of write_set.
1537 
1538   @return
1539     FALSE  - If field is not char/varchar/....
1540            - If field is char/varchar/.. and is not part of write set.
1541     TRUE   - If field is char/varchar/.. and is part of write set.
1542 */
is_varchar_and_in_write_set()1543   virtual bool is_varchar_and_in_write_set() const { return FALSE; }
1544 
1545   /* Check whether the field can be used as a join attribute in hash join */
hash_join_is_possible()1546   virtual bool hash_join_is_possible() { return TRUE; }
eq_cmp_as_binary()1547   virtual bool eq_cmp_as_binary() { return TRUE; }
1548 
1549   /* Position of the field value within the interval of [min, max] */
pos_in_interval(Field * min,Field * max)1550   virtual double pos_in_interval(Field *min, Field *max)
1551   {
1552     return (double) 0.5;
1553   }
1554 
1555   /*
1556     Check if comparison between the field and an item unambiguously
1557     identifies a distinct field value.
1558 
1559     Example1: SELECT * FROM t1 WHERE int_column=10;
1560               This example returns distinct integer value of 10.
1561 
1562     Example2: SELECT * FROM t1 WHERE varchar_column=DATE'2001-01-01'
1563               This example returns non-distinct values.
1564               Comparison as DATE will return '2001-01-01' and '2001-01-01x',
1565               but these two values are not equal to each other as VARCHARs.
1566     See also the function with the same name in sql_select.cc.
1567   */
1568   virtual bool test_if_equality_guarantees_uniqueness(const Item *const_item)
1569                                                       const;
1570   virtual bool can_be_substituted_to_equal_item(const Context &ctx,
1571                                         const Item_equal *item);
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)1572   virtual Item *get_equal_const_item(THD *thd, const Context &ctx,
1573                                      Item *const_item)
1574   {
1575     return const_item;
1576   }
1577   virtual bool can_optimize_keypart_ref(const Item_bool_func *cond,
1578                                         const Item *item) const;
can_optimize_hash_join(const Item_bool_func * cond,const Item * item)1579   virtual bool can_optimize_hash_join(const Item_bool_func *cond,
1580                                       const Item *item) const
1581   {
1582     return can_optimize_keypart_ref(cond, item);
1583   }
1584   virtual bool can_optimize_group_min_max(const Item_bool_func *cond,
1585                                           const Item *const_item) const;
1586   /**
1587     Test if Field can use range optimizer for a standard comparison operation:
1588       <=, <, =, <=>, >, >=
1589     Note, this method does not cover spatial operations.
1590   */
1591   virtual bool can_optimize_range(const Item_bool_func *cond,
1592                                   const Item *item,
1593                                   bool is_eq_func) const;
1594 
can_optimize_outer_join_table_elimination(const Item_bool_func * cond,const Item * item)1595   bool can_optimize_outer_join_table_elimination(const Item_bool_func *cond,
1596                                                  const Item *item) const
1597   {
1598     // Exactly the same rules with REF access
1599     return can_optimize_keypart_ref(cond, item);
1600   }
1601 
1602   bool save_in_field_default_value(bool view_eror_processing);
1603   bool save_in_field_ignore_value(bool view_error_processing);
1604 
1605   /* Mark field in read map. Updates also virtual fields */
1606   void register_field_in_read_map();
1607 
compression_method()1608   virtual Compression_method *compression_method() const { return 0; }
1609 
virtual_tmp_table_addr()1610   virtual Virtual_tmp_table **virtual_tmp_table_addr()
1611   {
1612     return NULL;
1613   }
1614   virtual bool sp_prepare_and_store_item(THD *thd, Item **value);
1615 
1616   friend int cre_myisam(char * name, TABLE *form, uint options,
1617 			ulonglong auto_increment_value);
1618   friend class Copy_field;
1619   friend class Item_avg_field;
1620   friend class Item_std_field;
1621   friend class Item_sum_num;
1622   friend class Item_sum_sum;
1623   friend class Item_sum_count;
1624   friend class Item_sum_avg;
1625   friend class Item_sum_std;
1626   friend class Item_sum_min;
1627   friend class Item_sum_max;
1628   friend class Item_func_group_concat;
1629 
1630 private:
1631   /*
1632     Primitive for implementing last_null_byte().
1633 
1634     SYNOPSIS
1635       do_last_null_byte()
1636 
1637     DESCRIPTION
1638       Primitive for the implementation of the last_null_byte()
1639       function. This represents the inheritance interface and can be
1640       overridden by subclasses.
1641    */
1642   virtual size_t do_last_null_byte() const;
1643 
1644 protected:
pack_int(uchar * to,const uchar * from,size_t size)1645   uchar *pack_int(uchar *to, const uchar *from, size_t size)
1646   {
1647     memcpy(to, from, size);
1648     return to + size;
1649   }
1650 
unpack_int(uchar * to,const uchar * from,const uchar * from_end,size_t size)1651   const uchar *unpack_int(uchar* to, const uchar *from,
1652                           const uchar *from_end, size_t size)
1653   {
1654     if (from + size > from_end)
1655       return 0;
1656     memcpy(to, from, size);
1657     return from + size;
1658   }
1659 
pack_int16(uchar * to,const uchar * from)1660   uchar *pack_int16(uchar *to, const uchar *from)
1661   { return pack_int(to, from, 2); }
unpack_int16(uchar * to,const uchar * from,const uchar * from_end)1662   const uchar *unpack_int16(uchar* to, const uchar *from, const uchar *from_end)
1663   { return unpack_int(to, from, from_end, 2); }
pack_int24(uchar * to,const uchar * from)1664   uchar *pack_int24(uchar *to, const uchar *from)
1665   { return pack_int(to, from, 3); }
unpack_int24(uchar * to,const uchar * from,const uchar * from_end)1666   const uchar *unpack_int24(uchar* to, const uchar *from, const uchar *from_end)
1667   { return unpack_int(to, from, from_end, 3); }
pack_int32(uchar * to,const uchar * from)1668   uchar *pack_int32(uchar *to, const uchar *from)
1669   { return pack_int(to, from, 4); }
unpack_int32(uchar * to,const uchar * from,const uchar * from_end)1670   const uchar *unpack_int32(uchar* to, const uchar *from, const uchar *from_end)
1671   { return unpack_int(to, from, from_end, 4); }
pack_int64(uchar * to,const uchar * from)1672   uchar *pack_int64(uchar* to, const uchar *from)
1673   { return pack_int(to, from, 8); }
unpack_int64(uchar * to,const uchar * from,const uchar * from_end)1674   const uchar *unpack_int64(uchar* to, const uchar *from,  const uchar *from_end)
1675   { return unpack_int(to, from, from_end, 8); }
1676 
1677   double pos_in_interval_val_real(Field *min, Field *max);
1678   double pos_in_interval_val_str(Field *min, Field *max, uint data_offset);
1679 };
1680 
1681 
1682 class Field_num :public Field {
1683 protected:
1684   int check_edom_and_important_data_truncation(const char *type, bool edom,
1685                                                CHARSET_INFO *cs,
1686                                                const char *str, size_t length,
1687                                                const char *end_of_num);
1688   int check_edom_and_truncation(const char *type, bool edom,
1689                                 CHARSET_INFO *cs,
1690                                 const char *str, size_t length,
1691                                 const char *end_of_num);
check_int(CHARSET_INFO * cs,const char * str,size_t length,const char * int_end,int error)1692   int check_int(CHARSET_INFO *cs, const char *str, size_t length,
1693                 const char *int_end, int error)
1694   {
1695     return check_edom_and_truncation("integer",
1696                                      error == MY_ERRNO_EDOM || str == int_end,
1697                                      cs, str, length, int_end);
1698   }
1699   bool get_int(CHARSET_INFO *cs, const char *from, size_t len,
1700                longlong *rnd, ulonglong unsigned_max,
1701                longlong signed_min, longlong signed_max);
1702   void prepend_zeros(String *value) const;
1703   Item *get_equal_zerofill_const_item(THD *thd, const Context &ctx,
1704                                       Item *const_item);
1705 public:
1706   const uint8 dec;
1707   bool zerofill,unsigned_flag;	// Purify cannot handle bit fields
1708   Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1709 	    uchar null_bit_arg, utype unireg_check_arg,
1710 	    const LEX_CSTRING *field_name_arg,
1711             uint8 dec_arg, bool zero_arg, bool unsigned_arg);
derivation(void)1712   enum Derivation derivation(void) const { return DERIVATION_NUMERIC; }
repertoire(void)1713   uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; }
charset(void)1714   CHARSET_INFO *charset(void) const { return &my_charset_numeric; }
1715   sql_mode_t can_handle_sql_mode_dependency_on_store() const;
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)1716   Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item)
1717   {
1718     return (flags & ZEROFILL_FLAG) ?
1719            get_equal_zerofill_const_item(thd, ctx, const_item) :
1720            const_item;
1721   }
1722   void add_zerofill_and_unsigned(String &res) const;
1723   friend class Create_field;
1724   void make_send_field(Send_field *);
decimals()1725   uint decimals() const { return (uint) dec; }
size_of()1726   uint size_of() const { return sizeof(*this); }
1727   bool eq_def(const Field *field) const;
get_copy_func(const Field * from)1728   Copy_func *get_copy_func(const Field *from) const
1729   {
1730     if (unsigned_flag && from->cmp_type() == DECIMAL_RESULT)
1731       return do_field_decimal;
1732     return do_field_int;
1733   }
save_in_field(Field * to)1734   int save_in_field(Field *to)
1735   {
1736     return to->store(val_int(), MY_TEST(flags & UNSIGNED_FLAG));
1737   }
1738   uint is_equal(Create_field *new_field);
row_pack_length()1739   uint row_pack_length() const { return pack_length(); }
pack_length_from_metadata(uint field_metadata)1740   uint32 pack_length_from_metadata(uint field_metadata) {
1741     uint32 length= pack_length();
1742     DBUG_PRINT("result", ("pack_length_from_metadata(%d): %u",
1743                           field_metadata, length));
1744     return length;
1745   }
pos_in_interval(Field * min,Field * max)1746   double pos_in_interval(Field *min, Field *max)
1747   {
1748     return pos_in_interval_val_real(min, max);
1749   }
1750 };
1751 
1752 
1753 class Field_str :public Field {
1754 protected:
1755   // TODO-10.2: Reuse DTCollation instead of these three members
1756   CHARSET_INFO *field_charset;
1757   enum Derivation field_derivation;
1758   uint field_repertoire;
1759 public:
1760   bool can_be_substituted_to_equal_item(const Context &ctx,
1761                                         const Item_equal *item_equal);
1762   Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1763 	    uchar null_bit_arg, utype unireg_check_arg,
1764 	    const LEX_CSTRING *field_name_arg,
1765 	    const DTCollation &collation);
decimals()1766   uint decimals() const { return is_created_from_null_item ? 0 : NOT_FIXED_DEC; }
save_in_field(Field * to)1767   int  save_in_field(Field *to) { return save_in_field_str(to); }
memcpy_field_possible(const Field * from)1768   bool memcpy_field_possible(const Field *from) const
1769   {
1770     return real_type() == from->real_type() &&
1771            pack_length() == from->pack_length() &&
1772            charset() == from->charset();
1773   }
1774   int  store(double nr);
1775   int  store(longlong nr, bool unsigned_val);
1776   int  store_decimal(const my_decimal *);
1777   int  store(const char *to,size_t length,CHARSET_INFO *cs)=0;
store_hex_hybrid(const char * str,size_t length)1778   int  store_hex_hybrid(const char *str, size_t length)
1779   {
1780     return store(str, length, &my_charset_bin);
1781   }
repertoire(void)1782   uint repertoire(void) const { return field_repertoire; }
charset(void)1783   CHARSET_INFO *charset(void) const { return field_charset; }
derivation(void)1784   enum Derivation derivation(void) const { return field_derivation; }
binary()1785   bool binary() const { return field_charset == &my_charset_bin; }
max_display_length()1786   uint32 max_display_length() const { return field_length; }
char_length()1787   uint32 char_length() const { return field_length / field_charset->mbmaxlen; }
1788   Information_schema_character_attributes
information_schema_character_attributes()1789     information_schema_character_attributes() const
1790   {
1791     return Information_schema_character_attributes(max_display_length(),
1792                                                    char_length());
1793   }
1794   friend class Create_field;
1795   my_decimal *val_decimal(my_decimal *);
val_bool()1796   bool val_bool() { return val_real() != 0e0; }
str_needs_quotes()1797   virtual bool str_needs_quotes() { return TRUE; }
1798   uint is_equal(Create_field *new_field);
eq_cmp_as_binary()1799   bool eq_cmp_as_binary() { return MY_TEST(flags & BINARY_FLAG); }
length_size()1800   virtual uint length_size() { return 0; }
pos_in_interval(Field * min,Field * max)1801   double pos_in_interval(Field *min, Field *max)
1802   {
1803     return pos_in_interval_val_str(min, max, length_size());
1804   }
1805   bool test_if_equality_guarantees_uniqueness(const Item *const_item) const;
1806 };
1807 
1808 /* base class for Field_string, Field_varstring and Field_blob */
1809 
1810 class Field_longstr :public Field_str
1811 {
1812 protected:
1813   int report_if_important_data(const char *ptr, const char *end,
1814                                bool count_spaces);
1815   bool check_string_copy_error(const String_copier *copier,
1816                                const char *end, CHARSET_INFO *cs);
check_conversion_status(const String_copier * copier,const char * end,CHARSET_INFO * cs,bool count_spaces)1817   int check_conversion_status(const String_copier *copier,
1818                               const char *end, CHARSET_INFO *cs,
1819                               bool count_spaces)
1820   {
1821     if (check_string_copy_error(copier, end, cs))
1822       return 2;
1823     return report_if_important_data(copier->source_end_pos(),
1824                                     end, count_spaces);
1825   }
well_formed_copy_with_check(char * to,size_t to_length,CHARSET_INFO * from_cs,const char * from,size_t from_length,size_t nchars,bool count_spaces,uint * copy_length)1826   int well_formed_copy_with_check(char *to, size_t to_length,
1827                                   CHARSET_INFO *from_cs,
1828                                   const char *from, size_t from_length,
1829                                   size_t nchars, bool count_spaces,
1830                                   uint *copy_length)
1831   {
1832     String_copier copier;
1833 
1834     *copy_length= copier.well_formed_copy(field_charset, to, to_length,
1835                                           from_cs, from, from_length,
1836                                           nchars);
1837 
1838     return check_conversion_status(&copier, from + from_length, from_cs, count_spaces);
1839   }
1840   bool cmp_to_string_with_same_collation(const Item_bool_func *cond,
1841                                          const Item *item) const;
1842   bool cmp_to_string_with_stricter_collation(const Item_bool_func *cond,
1843                                              const Item *item) const;
1844   int compress(char *to, uint to_length,
1845                const char *from, uint length,
1846                uint max_length,
1847                uint *out_length,
1848                CHARSET_INFO *cs, size_t nchars);
1849   String *uncompress(String *val_buffer, String *val_ptr,
1850                      const uchar *from, uint from_length);
1851 public:
Field_longstr(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)1852   Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1853                 uchar null_bit_arg, utype unireg_check_arg,
1854                 const LEX_CSTRING *field_name_arg,
1855                 const DTCollation &collation)
1856     :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
1857                field_name_arg, collation)
1858     {}
1859 
1860   int store_decimal(const my_decimal *d);
1861   uint32 max_data_length() const;
1862 
is_varchar_and_in_write_set()1863   bool is_varchar_and_in_write_set() const
1864   {
1865     DBUG_ASSERT(table && table->write_set);
1866     return bitmap_is_set(table->write_set, field_index);
1867   }
match_collation_to_optimize_range()1868   bool match_collation_to_optimize_range() const { return true; }
1869 
1870   bool can_optimize_keypart_ref(const Item_bool_func *cond,
1871                                 const Item *item) const;
1872   bool can_optimize_hash_join(const Item_bool_func *cond,
1873                               const Item *item) const;
1874   bool can_optimize_group_min_max(const Item_bool_func *cond,
1875                                   const Item *const_item) const;
1876   bool can_optimize_range(const Item_bool_func *cond,
1877                           const Item *item,
1878                           bool is_eq_func) const;
1879 };
1880 
1881 /* base class for float and double and decimal (old one) */
1882 class Field_real :public Field_num {
1883 protected:
1884   double get_double(const char *str, size_t length, CHARSET_INFO *cs, int *err);
1885 public:
1886   bool not_fixed;
1887 
Field_real(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)1888   Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1889              uchar null_bit_arg, utype unireg_check_arg,
1890              const LEX_CSTRING *field_name_arg,
1891              uint8 dec_arg, bool zero_arg, bool unsigned_arg)
1892     :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
1893                field_name_arg, dec_arg, zero_arg, unsigned_arg),
1894     not_fixed(dec_arg >= FLOATING_POINT_DECIMALS)
1895     {}
get_copy_func(const Field * from)1896   Copy_func *get_copy_func(const Field *from) const
1897   {
1898     return do_field_real;
1899   }
1900   Information_schema_numeric_attributes
information_schema_numeric_attributes()1901     information_schema_numeric_attributes() const
1902   {
1903     return dec == NOT_FIXED_DEC ?
1904                   Information_schema_numeric_attributes(field_length) :
1905                   Information_schema_numeric_attributes(field_length, dec);
1906   }
save_in_field(Field * to)1907   int save_in_field(Field *to) { return to->store(val_real()); }
memcpy_field_possible(const Field * from)1908   bool memcpy_field_possible(const Field *from) const
1909   {
1910     /*
1911       Cannot do memcpy from a longer field to a shorter field,
1912       e.g. a DOUBLE(53,10) into a DOUBLE(10,10).
1913       But it should be OK the other way around.
1914     */
1915     return real_type() == from->real_type() &&
1916            pack_length() == from->pack_length() &&
1917            is_unsigned() <= from->is_unsigned() &&
1918            decimals() == from->decimals() &&
1919            field_length >= from->field_length;
1920   }
1921   int store_decimal(const my_decimal *);
1922   int  store_time_dec(const MYSQL_TIME *ltime, uint dec);
1923   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
1924   my_decimal *val_decimal(my_decimal *);
val_bool()1925   bool val_bool() { return val_real() != 0e0; }
max_display_length()1926   uint32 max_display_length() const { return field_length; }
size_of()1927   uint size_of() const { return sizeof(*this); }
1928   Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item);
1929 };
1930 
1931 
1932 class Field_decimal :public Field_real {
1933 public:
Field_decimal(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)1934   Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1935 		uchar null_bit_arg,
1936 		enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
1937 		uint8 dec_arg,bool zero_arg,bool unsigned_arg)
1938     :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1939                 unireg_check_arg, field_name_arg,
1940                 dec_arg, zero_arg, unsigned_arg)
1941     {}
1942   Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
type_handler()1943   const Type_handler *type_handler() const { return &type_handler_olddecimal; }
key_type()1944   enum ha_base_keytype key_type() const
1945   { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
1946   Information_schema_numeric_attributes
information_schema_numeric_attributes()1947     information_schema_numeric_attributes() const
1948   {
1949     uint tmp= dec ? 2 : 1; // The sign and the decimal point
1950     return Information_schema_numeric_attributes(field_length - tmp, dec);
1951   }
get_copy_func(const Field * from)1952   Copy_func *get_copy_func(const Field *from) const
1953   {
1954     return eq_def(from) ? get_identical_copy_func() : do_field_string;
1955   }
1956   int reset(void);
1957   int store(const char *to,size_t length,CHARSET_INFO *charset);
1958   int store(double nr);
1959   int store(longlong nr, bool unsigned_val);
1960   double val_real(void);
1961   longlong val_int(void);
1962   String *val_str(String*,String *);
1963   int cmp(const uchar *,const uchar *);
1964   void sort_string(uchar *buff,uint length);
1965   void overflow(bool negative);
zero_pack()1966   bool zero_pack() const { return 0; }
1967   void sql_type(String &str) const;
pack(uchar * to,const uchar * from,uint max_length)1968   virtual uchar *pack(uchar* to, const uchar *from, uint max_length)
1969   {
1970     return Field::pack(to, from, max_length);
1971   }
1972 };
1973 
1974 
1975 /* New decimal/numeric field which use fixed point arithmetic */
1976 class Field_new_decimal :public Field_num {
1977 private:
1978   int save_field_metadata(uchar *first_byte);
1979 public:
1980   /* The maximum number of decimal digits can be stored */
1981   uint precision;
1982   uint bin_size;
1983   /*
1984     Constructors take max_length of the field as a parameter - not the
1985     precision as the number of decimal digits allowed.
1986     So for example we need to count length from precision handling
1987     CREATE TABLE ( DECIMAL(x,y))
1988   */
1989   Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1990                     uchar null_bit_arg,
1991                     enum utype unireg_check_arg,
1992                     const LEX_CSTRING *field_name_arg,
1993                     uint8 dec_arg, bool zero_arg, bool unsigned_arg);
type_handler()1994   const Type_handler *type_handler() const { return &type_handler_newdecimal; }
key_type()1995   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
get_copy_func(const Field * from)1996   Copy_func *get_copy_func(const Field *from) const
1997   {
1998     //  if (from->real_type() == MYSQL_TYPE_BIT) // QQ: why?
1999     //    return do_field_int;
2000     return do_field_decimal;
2001   }
save_in_field(Field * to)2002   int save_in_field(Field *to)
2003   {
2004     my_decimal buff;
2005     return to->store_decimal(val_decimal(&buff));
2006   }
memcpy_field_possible(const Field * from)2007   bool memcpy_field_possible(const Field *from) const
2008   {
2009     return real_type() == from->real_type() &&
2010            pack_length() == from->pack_length() &&
2011            is_unsigned() <= from->is_unsigned() &&
2012            decimals() == from->decimals() &&
2013            field_length == from->field_length;
2014   }
2015   int  reset(void);
2016   bool store_value(const my_decimal *decimal_value);
2017   bool store_value(const my_decimal *decimal_value, int *native_error);
2018   void set_value_on_overflow(my_decimal *decimal_value, bool sign);
2019   int  store(const char *to, size_t length, CHARSET_INFO *charset);
2020   int  store(double nr);
2021   int  store(longlong nr, bool unsigned_val);
2022   int  store_time_dec(const MYSQL_TIME *ltime, uint dec);
2023   int  store_decimal(const my_decimal *);
2024   double val_real(void);
2025   longlong val_int(void);
2026   ulonglong val_uint(void);
2027   my_decimal *val_decimal(my_decimal *);
2028   String *val_str(String*, String *);
2029   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
val_bool()2030   bool val_bool()
2031   {
2032     my_decimal decimal_value;
2033     my_decimal *val= val_decimal(&decimal_value);
2034     return val ? !my_decimal_is_zero(val) : 0;
2035   }
2036   int cmp(const uchar *, const uchar *);
2037   void sort_string(uchar *buff, uint length);
zero_pack()2038   bool zero_pack() const { return 0; }
2039   void sql_type(String &str) const;
max_display_length()2040   uint32 max_display_length() const { return field_length; }
2041   Information_schema_numeric_attributes
information_schema_numeric_attributes()2042     information_schema_numeric_attributes() const
2043   {
2044     return Information_schema_numeric_attributes(precision, dec);
2045   }
size_of()2046   uint size_of() const { return sizeof(*this); }
pack_length()2047   uint32 pack_length() const { return (uint32) bin_size; }
2048   uint pack_length_from_metadata(uint field_metadata);
row_pack_length()2049   uint row_pack_length() const { return pack_length(); }
2050   bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
2051                              uint16 mflags, int *order_var);
2052   uint is_equal(Create_field *new_field);
2053   virtual const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end, uint param_data);
2054   Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item);
2055 };
2056 
2057 
2058 class Field_int :public Field_num
2059 {
2060 protected:
2061   String *val_str_from_long(String *val_buffer, uint max_char_length,
2062                             int radix, long nr);
2063 public:
Field_int(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2064   Field_int(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2065             uchar null_bit_arg, enum utype unireg_check_arg,
2066             const LEX_CSTRING *field_name_arg, bool zero_arg, bool unsigned_arg)
2067     :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2068                unireg_check_arg, field_name_arg, 0, zero_arg, unsigned_arg)
2069     {}
memcpy_field_possible(const Field * from)2070   bool memcpy_field_possible(const Field *from) const
2071   {
2072     return real_type() == from->real_type() &&
2073            pack_length() == from->pack_length() &&
2074            is_unsigned() == from->is_unsigned();
2075   }
2076   int store_decimal(const my_decimal *);
2077   my_decimal *val_decimal(my_decimal *);
val_bool()2078   bool val_bool() { return val_int() != 0; }
val_uint()2079   ulonglong val_uint()
2080   {
2081     longlong nr= val_int();
2082     return nr < 0 && !unsigned_flag ? 0 : (ulonglong) nr;
2083   }
2084   int  store_time_dec(const MYSQL_TIME *ltime, uint dec);
2085   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
2086   virtual const Type_limits_int *type_limits_int() const= 0;
max_display_length()2087   uint32 max_display_length() const
2088   {
2089     return type_limits_int()->char_length();
2090   }
type_std_attributes()2091   Type_std_attributes type_std_attributes() const
2092   {
2093     /*
2094       For integer data types, the user-specified length does not constrain the
2095       supported range, so e.g. a column of the INT(1) data type supports the
2096       full integer range anyway.
2097       Choose the maximum from the user-specified length and the maximum
2098       possible length determined by the data type capacity:
2099         INT(1)  -> 11
2100         INT(10) -> 11
2101         INT(40) -> 40
2102     */
2103     uint32 length1= max_display_length();
2104     uint32 length2= field_length;
2105     return Type_std_attributes(MY_MAX(length1, length2), decimals(),
2106                                MY_TEST(flags & UNSIGNED_FLAG),
2107                                dtcollation());
2108   }
2109   Information_schema_numeric_attributes
information_schema_numeric_attributes()2110     information_schema_numeric_attributes() const
2111   {
2112     uint32 prec= type_limits_int()->precision();
2113     return Information_schema_numeric_attributes(prec, 0);
2114   }
2115 };
2116 
2117 
2118 class Field_tiny :public Field_int
2119 {
2120 public:
Field_tiny(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2121   Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2122 	     uchar null_bit_arg,
2123 	     enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2124 	     bool zero_arg, bool unsigned_arg)
2125     :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2126                unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2127     {}
type_handler()2128   const Type_handler *type_handler() const { return &type_handler_tiny; }
key_type()2129   enum ha_base_keytype key_type() const
2130     { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
2131   int store(const char *to,size_t length,CHARSET_INFO *charset);
2132   int store(double nr);
2133   int store(longlong nr, bool unsigned_val);
reset(void)2134   int reset(void) { ptr[0]=0; return 0; }
2135   double val_real(void);
2136   longlong val_int(void);
2137   String *val_str(String*,String *);
2138   bool send_binary(Protocol *protocol);
2139   int cmp(const uchar *,const uchar *);
2140   void sort_string(uchar *buff,uint length);
pack_length()2141   uint32 pack_length() const { return 1; }
2142   void sql_type(String &str) const;
type_limits_int()2143   const Type_limits_int *type_limits_int() const
2144   {
2145     return type_handler_tiny.type_limits_int_by_unsigned_flag(is_unsigned());
2146   }
2147 
pack(uchar * to,const uchar * from,uint max_length)2148   virtual uchar *pack(uchar* to, const uchar *from, uint max_length)
2149   {
2150     *to= *from;
2151     return to + 1;
2152   }
2153 
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2154   virtual const uchar *unpack(uchar* to, const uchar *from,
2155                               const uchar *from_end, uint param_data)
2156   {
2157     if (from == from_end)
2158       return 0;
2159     *to= *from;
2160     return from + 1;
2161   }
get_max_int_value()2162   virtual ulonglong get_max_int_value() const
2163   {
2164     return unsigned_flag ? 0xFFULL : 0x7FULL;
2165   }
2166 };
2167 
2168 
2169 class Field_short :public Field_int
2170 {
2171 public:
Field_short(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2172   Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2173 	      uchar null_bit_arg,
2174 	      enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2175 	      bool zero_arg, bool unsigned_arg)
2176     :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2177                unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2178     {}
Field_short(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,bool unsigned_arg)2179   Field_short(uint32 len_arg,bool maybe_null_arg,
2180               const LEX_CSTRING *field_name_arg,
2181 	      bool unsigned_arg)
2182     :Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2183                NONE, field_name_arg, 0, unsigned_arg)
2184     {}
type_handler()2185   const Type_handler *type_handler() const { return &type_handler_short; }
key_type()2186   enum ha_base_keytype key_type() const
2187     { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
2188   int store(const char *to,size_t length,CHARSET_INFO *charset);
2189   int store(double nr);
2190   int store(longlong nr, bool unsigned_val);
reset(void)2191   int reset(void) { ptr[0]=ptr[1]=0; return 0; }
2192   double val_real(void);
2193   longlong val_int(void);
2194   String *val_str(String*,String *);
2195   bool send_binary(Protocol *protocol);
2196   int cmp(const uchar *,const uchar *);
2197   void sort_string(uchar *buff,uint length);
pack_length()2198   uint32 pack_length() const { return 2; }
2199   void sql_type(String &str) const;
type_limits_int()2200   const Type_limits_int *type_limits_int() const
2201   {
2202     return type_handler_short.type_limits_int_by_unsigned_flag(is_unsigned());
2203   }
pack(uchar * to,const uchar * from,uint max_length)2204   virtual uchar *pack(uchar* to, const uchar *from, uint max_length)
2205   { return pack_int16(to, from); }
2206 
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2207   virtual const uchar *unpack(uchar* to, const uchar *from,
2208                               const uchar *from_end, uint param_data)
2209   { return unpack_int16(to, from, from_end); }
get_max_int_value()2210   virtual ulonglong get_max_int_value() const
2211   {
2212     return unsigned_flag ? 0xFFFFULL : 0x7FFFULL;
2213   }
2214 };
2215 
2216 class Field_medium :public Field_int
2217 {
2218 public:
Field_medium(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2219   Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2220 	      uchar null_bit_arg,
2221 	      enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2222 	      bool zero_arg, bool unsigned_arg)
2223     :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2224                unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2225     {}
type_handler()2226   const Type_handler *type_handler() const { return &type_handler_int24; }
key_type()2227   enum ha_base_keytype key_type() const
2228     { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
2229   int store(const char *to,size_t length,CHARSET_INFO *charset);
2230   int store(double nr);
2231   int store(longlong nr, bool unsigned_val);
reset(void)2232   int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
2233   double val_real(void);
2234   longlong val_int(void);
2235   String *val_str(String*,String *);
2236   bool send_binary(Protocol *protocol);
2237   int cmp(const uchar *,const uchar *);
2238   void sort_string(uchar *buff,uint length);
pack_length()2239   uint32 pack_length() const { return 3; }
2240   void sql_type(String &str) const;
type_limits_int()2241   const Type_limits_int *type_limits_int() const
2242   {
2243     return type_handler_int24.type_limits_int_by_unsigned_flag(is_unsigned());
2244   }
pack(uchar * to,const uchar * from,uint max_length)2245   virtual uchar *pack(uchar* to, const uchar *from, uint max_length)
2246   {
2247     return Field::pack(to, from, max_length);
2248   }
get_max_int_value()2249   virtual ulonglong get_max_int_value() const
2250   {
2251     return unsigned_flag ? 0xFFFFFFULL : 0x7FFFFFULL;
2252   }
2253 };
2254 
2255 
2256 class Field_long :public Field_int
2257 {
2258 public:
Field_long(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2259   Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2260 	     uchar null_bit_arg,
2261 	     enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2262 	     bool zero_arg, bool unsigned_arg)
2263     :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2264                unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2265     {}
Field_long(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,bool unsigned_arg)2266   Field_long(uint32 len_arg,bool maybe_null_arg,
2267              const LEX_CSTRING *field_name_arg,
2268 	     bool unsigned_arg)
2269     :Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2270                NONE, field_name_arg, 0, unsigned_arg)
2271     {}
type_handler()2272   const Type_handler *type_handler() const { return &type_handler_long; }
key_type()2273   enum ha_base_keytype key_type() const
2274     { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
2275   int store(const char *to,size_t length,CHARSET_INFO *charset);
2276   int store(double nr);
2277   int store(longlong nr, bool unsigned_val);
reset(void)2278   int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
2279   double val_real(void);
2280   longlong val_int(void);
2281   bool send_binary(Protocol *protocol);
2282   String *val_str(String*,String *);
2283   int cmp(const uchar *,const uchar *);
2284   void sort_string(uchar *buff,uint length);
pack_length()2285   uint32 pack_length() const { return 4; }
2286   void sql_type(String &str) const;
type_limits_int()2287   const Type_limits_int *type_limits_int() const
2288   {
2289     return type_handler_long.type_limits_int_by_unsigned_flag(is_unsigned());
2290   }
pack(uchar * to,const uchar * from,uint max_length)2291   virtual uchar *pack(uchar* to, const uchar *from,
2292                       uint max_length __attribute__((unused)))
2293   {
2294     return pack_int32(to, from);
2295   }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2296   virtual const uchar *unpack(uchar* to, const uchar *from,
2297                               const uchar *from_end,
2298                               uint param_data __attribute__((unused)))
2299   {
2300     return unpack_int32(to, from, from_end);
2301   }
get_max_int_value()2302   virtual ulonglong get_max_int_value() const
2303   {
2304     return unsigned_flag ? 0xFFFFFFFFULL : 0x7FFFFFFFULL;
2305   }
2306 };
2307 
2308 
2309 class Field_longlong :public Field_int
2310 {
2311 public:
Field_longlong(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2312   Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2313 	      uchar null_bit_arg,
2314 	      enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2315 	      bool zero_arg, bool unsigned_arg)
2316     :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2317                unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2318     {}
Field_longlong(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,bool unsigned_arg)2319   Field_longlong(uint32 len_arg,bool maybe_null_arg,
2320 		 const LEX_CSTRING *field_name_arg,
2321                  bool unsigned_arg)
2322     :Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2323                 NONE, field_name_arg, 0, unsigned_arg)
2324     {}
type_handler()2325   const Type_handler *type_handler() const { return &type_handler_longlong; }
key_type()2326   enum ha_base_keytype key_type() const
2327     { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
2328   int store(const char *to,size_t length,CHARSET_INFO *charset);
2329   int store(double nr);
2330   int store(longlong nr, bool unsigned_val);
reset(void)2331   int reset(void)
2332   {
2333     ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
2334     return 0;
2335   }
2336   double val_real(void);
2337   longlong val_int(void);
2338   String *val_str(String*,String *);
2339   bool send_binary(Protocol *protocol);
2340   int cmp(const uchar *,const uchar *);
2341   void sort_string(uchar *buff,uint length);
pack_length()2342   uint32 pack_length() const { return 8; }
2343   void sql_type(String &str) const;
type_limits_int()2344   const Type_limits_int *type_limits_int() const
2345   {
2346     return type_handler_longlong.type_limits_int_by_unsigned_flag(is_unsigned());
2347   }
pack(uchar * to,const uchar * from,uint max_length)2348   virtual uchar *pack(uchar* to, const uchar *from,
2349                       uint max_length  __attribute__((unused)))
2350   {
2351     return pack_int64(to, from);
2352   }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2353   const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
2354                       uint param_data __attribute__((unused)))
2355   {
2356     return unpack_int64(to, from, from_end);
2357   }
2358   void set_max();
2359   bool is_max();
get_max_int_value()2360   virtual ulonglong get_max_int_value() const
2361   {
2362     return unsigned_flag ? 0xFFFFFFFFFFFFFFFFULL : 0x7FFFFFFFFFFFFFFFULL;
2363   }
2364 };
2365 
2366 
2367 class Field_vers_trx_id :public Field_longlong {
2368   MYSQL_TIME cache;
2369   ulonglong cached;
2370 public:
Field_vers_trx_id(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2371   Field_vers_trx_id(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2372                     uchar null_bit_arg, enum utype unireg_check_arg,
2373                     const LEX_CSTRING *field_name_arg, bool zero_arg,
2374                     bool unsigned_arg)
2375       : Field_longlong(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2376                        unireg_check_arg, field_name_arg, zero_arg,
2377                        unsigned_arg),
2378         cached(0)
2379   {}
type_handler()2380   const Type_handler *type_handler() const { return &type_handler_vers_trx_id; }
size_of()2381   uint size_of() const { return sizeof(*this); }
2382   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate, ulonglong trx_id);
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2383   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2384   {
2385     return get_date(ltime, fuzzydate, (ulonglong) val_int());
2386   }
2387   bool test_if_equality_guarantees_uniqueness(const Item *item) const;
can_optimize_keypart_ref(const Item_bool_func * cond,const Item * item)2388   bool can_optimize_keypart_ref(const Item_bool_func *cond,
2389                                       const Item *item) const
2390   {
2391     return true;
2392   }
2393 
can_optimize_group_min_max(const Item_bool_func * cond,const Item * const_item)2394   bool can_optimize_group_min_max(const Item_bool_func *cond,
2395                                         const Item *const_item) const
2396   {
2397     return true;
2398   }
can_optimize_range(const Item_bool_func * cond,const Item * item,bool is_eq_func)2399   bool can_optimize_range(const Item_bool_func *cond,
2400                                   const Item *item,
2401                                   bool is_eq_func) const
2402   {
2403     return true;
2404   }
2405   /* cmp_type() cannot be TIME_RESULT, because we want to compare this field against
2406      integers. But in all other cases we treat it as TIME_RESULT! */
2407 };
2408 
2409 
2410 class Field_float :public Field_real {
2411 public:
Field_float(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2412   Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2413 	      uchar null_bit_arg,
2414 	      enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2415               uint8 dec_arg,bool zero_arg,bool unsigned_arg)
2416     :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2417                 unireg_check_arg, field_name_arg,
2418                 dec_arg, zero_arg, unsigned_arg)
2419     {
2420       if (dec_arg >= FLOATING_POINT_DECIMALS)
2421         dec_arg= NOT_FIXED_DEC;
2422     }
Field_float(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg)2423   Field_float(uint32 len_arg, bool maybe_null_arg,
2424               const LEX_CSTRING *field_name_arg, uint8 dec_arg)
2425     :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
2426                 NONE, field_name_arg, dec_arg, 0, 0)
2427     {
2428       if (dec_arg >= FLOATING_POINT_DECIMALS)
2429         dec_arg= NOT_FIXED_DEC;
2430     }
type_handler()2431   const Type_handler *type_handler() const { return &type_handler_float; }
key_type()2432   enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
2433   int store(const char *to,size_t length,CHARSET_INFO *charset);
2434   int store(double nr);
2435   int store(longlong nr, bool unsigned_val);
reset(void)2436   int reset(void) { bzero(ptr,sizeof(float)); return 0; }
2437   double val_real(void);
2438   longlong val_int(void);
2439   String *val_str(String*,String *);
2440   bool send_binary(Protocol *protocol);
2441   int cmp(const uchar *,const uchar *);
2442   void sort_string(uchar *buff,uint length);
pack_length()2443   uint32 pack_length() const { return sizeof(float); }
row_pack_length()2444   uint row_pack_length() const { return pack_length(); }
2445   void sql_type(String &str) const;
get_max_int_value()2446   virtual ulonglong get_max_int_value() const
2447   {
2448     /*
2449       We use the maximum as per IEEE754-2008 standard, 2^24
2450     */
2451     return 0x1000000ULL;
2452   }
2453 private:
2454   int save_field_metadata(uchar *first_byte);
2455 };
2456 
2457 
2458 class Field_double :public Field_real {
2459   longlong val_int_from_real(bool want_unsigned_result);
2460 public:
Field_double(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2461   Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2462 	       uchar null_bit_arg,
2463 	       enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2464 	       uint8 dec_arg,bool zero_arg,bool unsigned_arg)
2465     :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2466                 unireg_check_arg, field_name_arg,
2467                 dec_arg, zero_arg, unsigned_arg)
2468     {
2469       if (dec_arg >= FLOATING_POINT_DECIMALS)
2470         dec_arg= NOT_FIXED_DEC;
2471     }
Field_double(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg)2472   Field_double(uint32 len_arg, bool maybe_null_arg,
2473                const LEX_CSTRING *field_name_arg, uint8 dec_arg)
2474     :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
2475                 NONE, field_name_arg, dec_arg, 0, 0)
2476     {
2477       if (dec_arg >= FLOATING_POINT_DECIMALS)
2478         dec_arg= NOT_FIXED_DEC;
2479     }
Field_double(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool not_fixed_arg)2480   Field_double(uint32 len_arg, bool maybe_null_arg,
2481                const LEX_CSTRING *field_name_arg,
2482 	       uint8 dec_arg, bool not_fixed_arg)
2483     :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
2484                 NONE, field_name_arg, dec_arg, 0, 0)
2485     {
2486       not_fixed= not_fixed_arg;
2487       if (dec_arg >= FLOATING_POINT_DECIMALS)
2488         dec_arg= NOT_FIXED_DEC;
2489     }
type_handler()2490   const Type_handler *type_handler() const { return &type_handler_double; }
key_type()2491   enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
2492   int  store(const char *to,size_t length,CHARSET_INFO *charset);
2493   int  store(double nr);
2494   int  store(longlong nr, bool unsigned_val);
reset(void)2495   int reset(void) { bzero(ptr,sizeof(double)); return 0; }
2496   double val_real(void);
val_int(void)2497   longlong val_int(void) { return val_int_from_real(false); }
val_uint(void)2498   ulonglong val_uint(void) { return (ulonglong) val_int_from_real(true); }
2499   String *val_str(String*,String *);
2500   bool send_binary(Protocol *protocol);
2501   int cmp(const uchar *,const uchar *);
2502   void sort_string(uchar *buff,uint length);
pack_length()2503   uint32 pack_length() const { return sizeof(double); }
row_pack_length()2504   uint row_pack_length() const { return pack_length(); }
2505   void sql_type(String &str) const;
get_max_int_value()2506   virtual ulonglong get_max_int_value() const
2507   {
2508     /*
2509       We use the maximum as per IEEE754-2008 standard, 2^53
2510     */
2511     return 0x20000000000000ULL;
2512   }
2513 private:
2514   int save_field_metadata(uchar *first_byte);
2515 };
2516 
2517 
2518 /* Everything saved in this will disappear. It will always return NULL */
2519 
2520 class Field_null :public Field_str {
2521   static uchar null[1];
2522 public:
Field_null(uchar * ptr_arg,uint32 len_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)2523   Field_null(uchar *ptr_arg, uint32 len_arg,
2524 	     enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2525 	     const DTCollation &collation)
2526     :Field_str(ptr_arg, len_arg, null, 1,
2527 	       unireg_check_arg, field_name_arg, collation)
2528     {}
type_handler()2529   const Type_handler *type_handler() const { return &type_handler_null; }
2530   Information_schema_character_attributes
information_schema_character_attributes()2531     information_schema_character_attributes() const
2532   {
2533     return Information_schema_character_attributes();
2534   }
get_copy_func(const Field * from)2535   Copy_func *get_copy_func(const Field *from) const
2536   {
2537     return do_field_string;
2538   }
store(const char * to,size_t length,CHARSET_INFO * cs)2539   int  store(const char *to, size_t length, CHARSET_INFO *cs)
2540   { null[0]=1; return 0; }
store(double nr)2541   int store(double nr)   { null[0]=1; return 0; }
store(longlong nr,bool unsigned_val)2542   int store(longlong nr, bool unsigned_val) { null[0]=1; return 0; }
store_decimal(const my_decimal * d)2543   int store_decimal(const my_decimal *d)  { null[0]=1; return 0; }
reset(void)2544   int reset(void)	  { return 0; }
val_real(void)2545   double val_real(void)		{ return 0.0;}
val_int(void)2546   longlong val_int(void)	{ return 0;}
val_bool(void)2547   bool val_bool(void) { return false; }
val_decimal(my_decimal *)2548   my_decimal *val_decimal(my_decimal *) { return 0; }
val_str(String * value,String * value2)2549   String *val_str(String *value,String *value2)
2550   { value2->length(0); return value2;}
cmp(const uchar * a,const uchar * b)2551   int cmp(const uchar *a, const uchar *b) { return 0;}
sort_string(uchar * buff,uint length)2552   void sort_string(uchar *buff, uint length)  {}
pack_length()2553   uint32 pack_length() const { return 0; }
2554   void sql_type(String &str) const;
size_of()2555   uint size_of() const { return sizeof(*this); }
max_display_length()2556   uint32 max_display_length() const { return 4; }
move_field_offset(my_ptrdiff_t ptr_diff)2557   void move_field_offset(my_ptrdiff_t ptr_diff) {}
can_optimize_keypart_ref(const Item_bool_func * cond,const Item * item)2558   bool can_optimize_keypart_ref(const Item_bool_func *cond,
2559                                 const Item *item) const
2560   {
2561     return false;
2562   }
can_optimize_group_min_max(const Item_bool_func * cond,const Item * const_item)2563   bool can_optimize_group_min_max(const Item_bool_func *cond,
2564                                   const Item *const_item) const
2565   {
2566     return false;
2567   }
2568 };
2569 
2570 
2571 class Field_temporal: public Field {
2572 protected:
2573   Item *get_equal_const_item_datetime(THD *thd, const Context &ctx,
2574                                       Item *const_item);
2575 public:
Field_temporal(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2576   Field_temporal(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
2577                  uchar null_bit_arg, utype unireg_check_arg,
2578                  const LEX_CSTRING *field_name_arg)
2579     :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
2580                field_name_arg)
2581     { flags|= BINARY_FLAG; }
store_hex_hybrid(const char * str,size_t length)2582   int  store_hex_hybrid(const char *str, size_t length)
2583   {
2584     return store(str, length, &my_charset_bin);
2585   }
2586   sql_mode_t can_handle_sql_mode_dependency_on_store() const;
2587   Copy_func *get_copy_func(const Field *from) const;
save_in_field(Field * to)2588   int save_in_field(Field *to)
2589   {
2590     MYSQL_TIME ltime;
2591     if (get_date(&ltime, 0))
2592       return to->reset();
2593     return to->store_time_dec(&ltime, decimals());
2594   }
2595   bool memcpy_field_possible(const Field *from) const;
max_display_length()2596   uint32 max_display_length() const { return field_length; }
str_needs_quotes()2597   bool str_needs_quotes() { return TRUE; }
derivation(void)2598   enum Derivation derivation(void) const { return DERIVATION_NUMERIC; }
repertoire(void)2599   uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; }
charset(void)2600   CHARSET_INFO *charset(void) const { return &my_charset_numeric; }
sort_charset(void)2601   CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
binary()2602   bool binary() const { return true; }
val_bool()2603   bool val_bool() { return val_real() != 0e0; }
2604   uint is_equal(Create_field *new_field);
eq_def(const Field * field)2605   bool eq_def(const Field *field) const
2606   {
2607     return (Field::eq_def(field) && decimals() == field->decimals());
2608   }
2609   my_decimal *val_decimal(my_decimal*);
2610   void set_warnings(Sql_condition::enum_warning_level trunc_level,
2611                     const ErrConv *str, int was_cut, timestamp_type ts_type);
pos_in_interval(Field * min,Field * max)2612   double pos_in_interval(Field *min, Field *max)
2613   {
2614     return pos_in_interval_val_real(min, max);
2615   }
2616   bool can_optimize_keypart_ref(const Item_bool_func *cond,
2617                                 const Item *item) const;
2618   bool can_optimize_group_min_max(const Item_bool_func *cond,
2619                                   const Item *const_item) const;
can_optimize_range(const Item_bool_func * cond,const Item * item,bool is_eq_func)2620   bool can_optimize_range(const Item_bool_func *cond,
2621                                   const Item *item,
2622                                   bool is_eq_func) const
2623   {
2624     return true;
2625   }
2626 };
2627 
2628 
2629 /**
2630   Abstract class for:
2631   - DATE
2632   - DATETIME
2633   - DATETIME(1..6)
2634   - DATETIME(0..6) - MySQL56 version
2635 */
2636 class Field_temporal_with_date: public Field_temporal {
2637 protected:
2638   int store_TIME_with_warning(MYSQL_TIME *ltime, const ErrConv *str,
2639                               int was_cut, int have_smth_to_conv);
2640   virtual void store_TIME(MYSQL_TIME *ltime) = 0;
2641   virtual bool get_TIME(MYSQL_TIME *ltime, const uchar *pos,
2642                         ulonglong fuzzydate) const = 0;
validate_MMDD(bool not_zero_date,uint month,uint day,ulonglong fuzzydate)2643   bool validate_MMDD(bool not_zero_date, uint month, uint day,
2644                      ulonglong fuzzydate) const
2645   {
2646     if (!not_zero_date)
2647       return fuzzydate & TIME_NO_ZERO_DATE;
2648     if (!month || !day)
2649       return fuzzydate & TIME_NO_ZERO_IN_DATE;
2650     return false;
2651   }
2652 public:
Field_temporal_with_date(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2653   Field_temporal_with_date(uchar *ptr_arg, uint32 len_arg,
2654                            uchar *null_ptr_arg, uchar null_bit_arg,
2655                            utype unireg_check_arg,
2656                            const LEX_CSTRING *field_name_arg)
2657     :Field_temporal(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2658                     unireg_check_arg, field_name_arg)
2659     {}
2660   int  store(const char *to, size_t length, CHARSET_INFO *charset);
2661   int  store(double nr);
2662   int  store(longlong nr, bool unsigned_val);
2663   int  store_time_dec(const MYSQL_TIME *ltime, uint dec);
2664   int  store_decimal(const my_decimal *);
2665   bool validate_value_in_record(THD *thd, const uchar *record) const;
2666 };
2667 
2668 
2669 class Field_timestamp :public Field_temporal {
2670 protected:
2671   sql_mode_t sql_mode_for_timestamp(THD *thd) const;
2672   int store_TIME_with_warning(THD *, MYSQL_TIME *, const ErrConv *,
2673                               int warnings, bool have_smth_to_conv);
2674 public:
2675   Field_timestamp(uchar *ptr_arg, uint32 len_arg,
2676                   uchar *null_ptr_arg, uchar null_bit_arg,
2677 		  enum utype unireg_check_arg,
2678                   const LEX_CSTRING *field_name_arg,
2679 		  TABLE_SHARE *share);
type_handler()2680   const Type_handler *type_handler() const { return &type_handler_timestamp; }
key_type()2681   enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
2682   Copy_func *get_copy_func(const Field *from) const;
2683   int  store(const char *to,size_t length,CHARSET_INFO *charset);
2684   int  store(double nr);
2685   int  store(longlong nr, bool unsigned_val);
2686   int  store_time_dec(const MYSQL_TIME *ltime, uint dec);
2687   int  store_decimal(const my_decimal *);
2688   int  store_timestamp(my_time_t timestamp, ulong sec_part);
2689   int  save_in_field(Field *to);
2690   double val_real(void);
2691   longlong val_int(void);
2692   String *val_str(String*,String *);
2693   bool send_binary(Protocol *protocol);
2694   int cmp(const uchar *,const uchar *);
2695   void sort_string(uchar *buff,uint length);
pack_length()2696   uint32 pack_length() const { return 4; }
2697   void sql_type(String &str) const;
zero_pack()2698   bool zero_pack() const { return 0; }
2699   int set_time();
2700   /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
2701   my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const;
get_timestamp(ulong * sec_part)2702   my_time_t get_timestamp(ulong *sec_part) const
2703   {
2704     return get_timestamp(ptr, sec_part);
2705   }
store_TIME(my_time_t timestamp,ulong sec_part)2706   virtual void store_TIME(my_time_t timestamp, ulong sec_part)
2707   {
2708     int4store(ptr,timestamp);
2709   }
2710   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
pack(uchar * to,const uchar * from,uint max_length)2711   uchar *pack(uchar *to, const uchar *from,
2712               uint max_length __attribute__((unused)))
2713   {
2714     return pack_int32(to, from);
2715   }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2716   const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
2717                       uint param_data __attribute__((unused)))
2718   {
2719     return unpack_int32(to, from, from_end);
2720   }
2721   bool validate_value_in_record(THD *thd, const uchar *record) const;
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)2722   Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item)
2723   {
2724     return get_equal_const_item_datetime(thd, ctx, const_item);
2725   }
2726   bool load_data_set_null(THD *thd);
2727   bool load_data_set_no_data(THD *thd, bool fixed_format);
size_of()2728   uint size_of() const { return sizeof(*this); }
2729 };
2730 
2731 
2732 /**
2733   Abstract class for:
2734   - TIMESTAMP(1..6)
2735   - TIMESTAMP(0..6) - MySQL56 version
2736 */
2737 class Field_timestamp_with_dec :public Field_timestamp {
2738 protected:
2739   uint dec;
2740 public:
Field_timestamp_with_dec(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint dec_arg)2741   Field_timestamp_with_dec(uchar *ptr_arg,
2742                            uchar *null_ptr_arg, uchar null_bit_arg,
2743                            enum utype unireg_check_arg,
2744                            const LEX_CSTRING *field_name_arg,
2745                            TABLE_SHARE *share, uint dec_arg) :
2746   Field_timestamp(ptr_arg,
2747                   MAX_DATETIME_WIDTH + dec_arg + MY_TEST(dec_arg), null_ptr_arg,
2748                   null_bit_arg, unireg_check_arg, field_name_arg, share),
2749   dec(dec_arg)
2750   {
2751     DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
2752   }
decimals()2753   uint decimals() const { return dec; }
key_type()2754   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
pack(uchar * to,const uchar * from,uint max_length)2755   uchar *pack(uchar *to, const uchar *from, uint max_length)
2756   { return Field::pack(to, from, max_length); }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2757   const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
2758                       uint param_data)
2759   { return Field::unpack(to, from, from_end, param_data); }
2760   void make_send_field(Send_field *field);
sort_string(uchar * to,uint length)2761   void sort_string(uchar *to, uint length)
2762   {
2763     DBUG_ASSERT(length == pack_length());
2764     memcpy(to, ptr, length);
2765   }
2766   bool send_binary(Protocol *protocol);
2767   double val_real(void);
2768   my_decimal* val_decimal(my_decimal*);
2769   int set_time();
2770 };
2771 
2772 
2773 class Field_timestamp_hires :public Field_timestamp_with_dec {
sec_part_bytes(uint dec)2774   uint sec_part_bytes(uint dec) const
2775   {
2776     return Type_handler_timestamp::sec_part_bytes(dec);
2777   }
2778 public:
Field_timestamp_hires(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint dec_arg)2779   Field_timestamp_hires(uchar *ptr_arg,
2780                         uchar *null_ptr_arg, uchar null_bit_arg,
2781                         enum utype unireg_check_arg,
2782                         const LEX_CSTRING *field_name_arg,
2783                         TABLE_SHARE *share, uint dec_arg) :
2784   Field_timestamp_with_dec(ptr_arg, null_ptr_arg, null_bit_arg,
2785                            unireg_check_arg, field_name_arg, share, dec_arg)
2786   {
2787     DBUG_ASSERT(dec);
2788   }
2789   my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const;
2790   void store_TIME(my_time_t timestamp, ulong sec_part);
2791   int cmp(const uchar *,const uchar *);
pack_length()2792   uint32 pack_length() const { return 4 + sec_part_bytes(dec); }
size_of()2793   uint size_of() const { return sizeof(*this); }
2794 };
2795 
2796 
2797 /**
2798   TIMESTAMP(0..6) - MySQL56 version
2799 */
2800 class Field_timestampf :public Field_timestamp_with_dec {
save_field_metadata(uchar * metadata_ptr)2801   int save_field_metadata(uchar *metadata_ptr)
2802   {
2803     *metadata_ptr= (uchar) decimals();
2804     return 1;
2805   }
2806 public:
Field_timestampf(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint dec_arg)2807   Field_timestampf(uchar *ptr_arg,
2808                    uchar *null_ptr_arg, uchar null_bit_arg,
2809                    enum utype unireg_check_arg,
2810                    const LEX_CSTRING *field_name_arg,
2811                    TABLE_SHARE *share, uint dec_arg) :
2812     Field_timestamp_with_dec(ptr_arg, null_ptr_arg, null_bit_arg,
2813                              unireg_check_arg, field_name_arg, share, dec_arg)
2814     {}
type_handler()2815   const Type_handler *type_handler() const { return &type_handler_timestamp2; }
binlog_type()2816   enum_field_types binlog_type() const { return MYSQL_TYPE_TIMESTAMP2; }
pack_length()2817   uint32 pack_length() const
2818   {
2819     return my_timestamp_binary_length(dec);
2820   }
row_pack_length()2821   uint row_pack_length() const { return pack_length(); }
pack_length_from_metadata(uint field_metadata)2822   uint pack_length_from_metadata(uint field_metadata)
2823   {
2824     DBUG_ENTER("Field_timestampf::pack_length_from_metadata");
2825     uint tmp= my_timestamp_binary_length(field_metadata);
2826     DBUG_RETURN(tmp);
2827   }
cmp(const uchar * a_ptr,const uchar * b_ptr)2828   int cmp(const uchar *a_ptr,const uchar *b_ptr)
2829   {
2830     return memcmp(a_ptr, b_ptr, pack_length());
2831   }
2832   void set_max();
2833   bool is_max();
2834   void store_TIME(my_time_t timestamp, ulong sec_part);
2835   my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const;
get_timestamp(ulong * sec_part)2836   my_time_t get_timestamp(ulong *sec_part) const
2837   {
2838     return get_timestamp(ptr, sec_part);
2839   }
size_of()2840   uint size_of() const { return sizeof(*this); }
2841 };
2842 
2843 
2844 class Field_year :public Field_tiny {
2845 public:
Field_year(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2846   Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2847 	     uchar null_bit_arg,
2848 	     enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg)
2849     :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2850 		unireg_check_arg, field_name_arg, 1, 1)
2851     {}
type_handler()2852   const Type_handler *type_handler() const { return &type_handler_year; }
get_copy_func(const Field * from)2853   Copy_func *get_copy_func(const Field *from) const
2854   {
2855     if (eq_def(from))
2856       return get_identical_copy_func();
2857     switch (from->cmp_type()) {
2858     case STRING_RESULT:
2859     {
2860       const Type_handler *handler= from->type_handler();
2861       if (handler == &type_handler_enum || handler == &type_handler_set)
2862         return do_field_int;
2863       return do_field_string;
2864     }
2865     case TIME_RESULT:
2866       return do_field_temporal;
2867     case DECIMAL_RESULT:
2868       return do_field_decimal;
2869     case REAL_RESULT:
2870       return do_field_real;
2871     case INT_RESULT:
2872       break;
2873     case ROW_RESULT:
2874     default:
2875       DBUG_ASSERT(0);
2876       break;
2877     }
2878     return do_field_int;
2879   }
2880   int  store(const char *to,size_t length,CHARSET_INFO *charset);
2881   int  store(double nr);
2882   int  store(longlong nr, bool unsigned_val);
2883   int  store_time_dec(const MYSQL_TIME *ltime, uint dec);
2884   double val_real(void);
2885   longlong val_int(void);
2886   String *val_str(String*,String *);
2887   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
2888   bool send_binary(Protocol *protocol);
2889   Information_schema_numeric_attributes
information_schema_numeric_attributes()2890     information_schema_numeric_attributes() const
2891   {
2892     return Information_schema_numeric_attributes();
2893   }
max_display_length()2894   uint32 max_display_length() const { return field_length; }
2895   void sql_type(String &str) const;
2896 };
2897 
2898 
2899 class Field_date :public Field_temporal_with_date {
2900   void store_TIME(MYSQL_TIME *ltime);
2901   bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
2902 public:
Field_date(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2903   Field_date(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2904 	     enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg)
2905     :Field_temporal_with_date(ptr_arg, MAX_DATE_WIDTH, null_ptr_arg, null_bit_arg,
2906                               unireg_check_arg, field_name_arg) {}
type_handler()2907   const Type_handler *type_handler() const { return &type_handler_date; }
key_type()2908   enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
reset(void)2909   int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2910   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2911   { return Field_date::get_TIME(ltime, ptr, fuzzydate); }
2912   double val_real(void);
2913   longlong val_int(void);
2914   String *val_str(String*,String *);
2915   bool send_binary(Protocol *protocol);
2916   int cmp(const uchar *,const uchar *);
2917   void sort_string(uchar *buff,uint length);
pack_length()2918   uint32 pack_length() const { return 4; }
2919   void sql_type(String &str) const;
pack(uchar * to,const uchar * from,uint max_length)2920   uchar *pack(uchar* to, const uchar *from,
2921               uint max_length __attribute__((unused)))
2922   {
2923     return pack_int32(to, from);
2924   }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2925   const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
2926                       uint param_data __attribute__((unused)))
2927   {
2928     return unpack_int32(to, from, from_end);
2929   }
size_of()2930   uint size_of() const { return sizeof(*this); }
2931 };
2932 
2933 
2934 class Field_newdate :public Field_temporal_with_date {
2935   void store_TIME(MYSQL_TIME *ltime);
2936   bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
2937 public:
Field_newdate(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2938   Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2939 		enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg)
2940     :Field_temporal_with_date(ptr_arg, MAX_DATE_WIDTH, null_ptr_arg, null_bit_arg,
2941                               unireg_check_arg, field_name_arg)
2942     {}
type_handler()2943   const Type_handler *type_handler() const { return &type_handler_newdate; }
key_type()2944   enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
reset(void)2945   int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
2946   double val_real(void);
2947   longlong val_int(void);
2948   String *val_str(String*,String *);
2949   bool send_binary(Protocol *protocol);
2950   int cmp(const uchar *,const uchar *);
2951   void sort_string(uchar *buff,uint length);
pack_length()2952   uint32 pack_length() const { return 3; }
2953   void sql_type(String &str) const;
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2954   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2955   { return Field_newdate::get_TIME(ltime, ptr, fuzzydate); }
size_of()2956   uint size_of() const { return sizeof(*this); }
2957   Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item);
2958 };
2959 
2960 
2961 class Field_time :public Field_temporal {
2962   /*
2963     when this Field_time instance is used for storing values for index lookups
2964     (see class store_key, Field::new_key_field(), etc), the following
2965     might be set to TO_DAYS(CURDATE()). See also Field_time::store_time_dec()
2966   */
2967   long curdays;
2968 protected:
2969   virtual void store_TIME(const MYSQL_TIME *ltime);
2970   int store_TIME_with_warning(MYSQL_TIME *ltime, const ErrConv *str,
2971                               int was_cut, int have_smth_to_conv);
set_warnings(Sql_condition::enum_warning_level level,const ErrConv * str,int was_cut)2972   void set_warnings(Sql_condition::enum_warning_level level,
2973                     const ErrConv *str, int was_cut)
2974   {
2975     Field_temporal::set_warnings(level, str, was_cut, MYSQL_TIMESTAMP_TIME);
2976   }
2977   bool check_zero_in_date_with_warn(ulonglong fuzzydate);
2978   static void do_field_time(Copy_field *copy);
2979 public:
Field_time(uchar * ptr_arg,uint length_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2980   Field_time(uchar *ptr_arg, uint length_arg, uchar *null_ptr_arg,
2981              uchar null_bit_arg, enum utype unireg_check_arg,
2982              const LEX_CSTRING *field_name_arg)
2983     :Field_temporal(ptr_arg, length_arg, null_ptr_arg, null_bit_arg,
2984                     unireg_check_arg, field_name_arg), curdays(0)
2985     {}
2986   bool can_be_substituted_to_equal_item(const Context &ctx,
2987                                         const Item_equal *item_equal);
type_handler()2988   const Type_handler *type_handler() const { return &type_handler_time; }
key_type()2989   enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
get_copy_func(const Field * from)2990   Copy_func *get_copy_func(const Field *from) const
2991   {
2992     return from->cmp_type() == REAL_RESULT ? do_field_string : // MDEV-9344
2993            from->type() == MYSQL_TYPE_YEAR ? do_field_int :
2994            from->type() == MYSQL_TYPE_BIT  ? do_field_int :
2995            eq_def(from)                    ? get_identical_copy_func() :
2996                                              do_field_time;
2997   }
memcpy_field_possible(const Field * from)2998   bool memcpy_field_possible(const Field *from) const
2999   {
3000     return real_type() == from->real_type() &&
3001            decimals() == from->decimals();
3002   }
3003   int store_time_dec(const MYSQL_TIME *ltime, uint dec);
3004   int store(const char *to,size_t length,CHARSET_INFO *charset);
3005   int store(double nr);
3006   int store(longlong nr, bool unsigned_val);
3007   int  store_decimal(const my_decimal *);
3008   double val_real(void);
3009   longlong val_int(void);
3010   String *val_str(String*,String *);
3011   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
3012   bool send_binary(Protocol *protocol);
3013   int cmp(const uchar *,const uchar *);
3014   void sort_string(uchar *buff,uint length);
pack_length()3015   uint32 pack_length() const { return 3; }
3016   void sql_type(String &str) const;
size_of()3017   uint size_of() const { return sizeof(*this); }
3018   void set_curdays(THD *thd);
3019   Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
3020                        uchar *new_ptr, uint32 length,
3021                        uchar *new_null_ptr, uint new_null_bit);
3022   Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item);
3023 };
3024 
3025 
3026 /**
3027   Abstract class for:
3028   - TIME(1..6)
3029   - TIME(0..6) - MySQL56 version
3030 */
3031 class Field_time_with_dec :public Field_time {
3032 protected:
3033   uint dec;
3034 public:
Field_time_with_dec(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3035   Field_time_with_dec(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3036                       enum utype unireg_check_arg,
3037                       const LEX_CSTRING *field_name_arg,
3038                       uint dec_arg)
3039     :Field_time(ptr_arg, MIN_TIME_WIDTH + dec_arg + MY_TEST(dec_arg),
3040                 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg),
3041      dec(dec_arg)
3042   {
3043     DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
3044   }
decimals()3045   uint decimals() const { return dec; }
key_type()3046   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
3047   longlong val_int(void);
3048   double val_real(void);
3049   void make_send_field(Send_field *);
3050 };
3051 
3052 
3053 /**
3054   TIME(1..6)
3055 */
3056 class Field_time_hires :public Field_time_with_dec {
3057   longlong zero_point;
3058   void store_TIME(const MYSQL_TIME *);
3059 public:
Field_time_hires(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3060   Field_time_hires(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3061              enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3062              uint dec_arg)
3063     :Field_time_with_dec(ptr_arg, null_ptr_arg,
3064                          null_bit_arg, unireg_check_arg, field_name_arg,
3065                          dec_arg)
3066   {
3067     DBUG_ASSERT(dec);
3068     zero_point= sec_part_shift(
3069                    ((TIME_MAX_VALUE_SECONDS+1LL)*TIME_SECOND_PART_FACTOR), dec);
3070   }
3071   int reset(void);
3072   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
3073   int cmp(const uchar *,const uchar *);
3074   void sort_string(uchar *buff,uint length);
pack_length()3075   uint32 pack_length() const { return Type_handler_time::hires_bytes(dec); }
size_of()3076   uint size_of() const { return sizeof(*this); }
3077 };
3078 
3079 
3080 /**
3081   TIME(0..6) - MySQL56 version
3082 */
3083 class Field_timef :public Field_time_with_dec {
3084   void store_TIME(const MYSQL_TIME *ltime);
save_field_metadata(uchar * metadata_ptr)3085   int save_field_metadata(uchar *metadata_ptr)
3086   {
3087     *metadata_ptr= (uchar) decimals();
3088     return 1;
3089   }
3090 public:
Field_timef(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3091   Field_timef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3092              enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3093              uint dec_arg)
3094     :Field_time_with_dec(ptr_arg, null_ptr_arg,
3095                          null_bit_arg, unireg_check_arg, field_name_arg,
3096                          dec_arg)
3097   {
3098     DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
3099   }
type_handler()3100   const Type_handler *type_handler() const { return &type_handler_time2; }
binlog_type()3101   enum_field_types binlog_type() const { return MYSQL_TYPE_TIME2; }
pack_length()3102   uint32 pack_length() const
3103   {
3104     return my_time_binary_length(dec);
3105   }
row_pack_length()3106   uint row_pack_length() const { return pack_length(); }
pack_length_from_metadata(uint field_metadata)3107   uint pack_length_from_metadata(uint field_metadata)
3108   {
3109     DBUG_ENTER("Field_timef::pack_length_from_metadata");
3110     uint tmp= my_time_binary_length(field_metadata);
3111     DBUG_RETURN(tmp);
3112   }
sort_string(uchar * to,uint length)3113   void sort_string(uchar *to, uint length)
3114   {
3115     DBUG_ASSERT(length == Field_timef::pack_length());
3116     memcpy(to, ptr, length);
3117   }
cmp(const uchar * a_ptr,const uchar * b_ptr)3118   int cmp(const uchar *a_ptr, const uchar *b_ptr)
3119   {
3120     return memcmp(a_ptr, b_ptr, pack_length());
3121   }
3122   int reset();
3123   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
size_of()3124   uint size_of() const { return sizeof(*this); }
3125 };
3126 
3127 
3128 class Field_datetime :public Field_temporal_with_date {
3129   void store_TIME(MYSQL_TIME *ltime);
3130   bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
3131 public:
Field_datetime(uchar * ptr_arg,uint length_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)3132   Field_datetime(uchar *ptr_arg, uint length_arg, uchar *null_ptr_arg,
3133                  uchar null_bit_arg, enum utype unireg_check_arg,
3134                  const LEX_CSTRING *field_name_arg)
3135     :Field_temporal_with_date(ptr_arg, length_arg, null_ptr_arg, null_bit_arg,
3136                               unireg_check_arg, field_name_arg)
3137     {
3138       if (unireg_check == TIMESTAMP_UN_FIELD ||
3139           unireg_check == TIMESTAMP_DNUN_FIELD)
3140         flags|= ON_UPDATE_NOW_FLAG;
3141     }
type_handler()3142   const Type_handler *type_handler() const { return &type_handler_datetime; }
key_type()3143   enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; }
3144   double val_real(void);
3145   longlong val_int(void);
3146   String *val_str(String*,String *);
3147   bool send_binary(Protocol *protocol);
3148   int cmp(const uchar *,const uchar *);
3149   void sort_string(uchar *buff,uint length);
pack_length()3150   uint32 pack_length() const { return 8; }
3151   void sql_type(String &str) const;
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)3152   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
3153   { return Field_datetime::get_TIME(ltime, ptr, fuzzydate); }
3154   int set_time();
pack(uchar * to,const uchar * from,uint max_length)3155   uchar *pack(uchar* to, const uchar *from,
3156               uint max_length __attribute__((unused)))
3157   {
3158     return pack_int64(to, from);
3159   }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)3160   const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
3161                       uint param_data __attribute__((unused)))
3162   {
3163     return unpack_int64(to, from, from_end);
3164   }
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)3165   Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item)
3166   {
3167     return get_equal_const_item_datetime(thd, ctx, const_item);
3168   }
size_of()3169   uint size_of() const { return sizeof(*this); }
3170 };
3171 
3172 
3173 /**
3174   Abstract class for:
3175   - DATETIME(1..6)
3176   - DATETIME(0..6) - MySQL56 version
3177 */
3178 class Field_datetime_with_dec :public Field_datetime {
3179 protected:
3180   uint dec;
3181 public:
Field_datetime_with_dec(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3182   Field_datetime_with_dec(uchar *ptr_arg, uchar *null_ptr_arg,
3183                           uchar null_bit_arg, enum utype unireg_check_arg,
3184                           const LEX_CSTRING *field_name_arg, uint dec_arg)
3185     :Field_datetime(ptr_arg, MAX_DATETIME_WIDTH + dec_arg + MY_TEST(dec_arg),
3186                     null_ptr_arg, null_bit_arg, unireg_check_arg,
3187                     field_name_arg), dec(dec_arg)
3188   {
3189     DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
3190   }
decimals()3191   uint decimals() const { return dec; }
key_type()3192   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
3193   void make_send_field(Send_field *field);
3194   bool send_binary(Protocol *protocol);
pack(uchar * to,const uchar * from,uint max_length)3195   uchar *pack(uchar *to, const uchar *from, uint max_length)
3196   { return Field::pack(to, from, max_length); }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)3197   const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
3198                       uint param_data)
3199   { return Field::unpack(to, from, from_end, param_data); }
sort_string(uchar * to,uint length)3200   void sort_string(uchar *to, uint length)
3201   {
3202     DBUG_ASSERT(length == pack_length());
3203     memcpy(to, ptr, length);
3204   }
3205   double val_real(void);
3206   longlong val_int(void);
3207   String *val_str(String*,String *);
3208 };
3209 
3210 
3211 /**
3212   DATETIME(1..6)
3213 */
3214 class Field_datetime_hires :public Field_datetime_with_dec {
3215   void store_TIME(MYSQL_TIME *ltime);
3216   bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
3217 public:
Field_datetime_hires(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3218   Field_datetime_hires(uchar *ptr_arg, uchar *null_ptr_arg,
3219                        uchar null_bit_arg, enum utype unireg_check_arg,
3220                        const LEX_CSTRING *field_name_arg, uint dec_arg)
3221     :Field_datetime_with_dec(ptr_arg, null_ptr_arg, null_bit_arg,
3222                              unireg_check_arg, field_name_arg, dec_arg)
3223   {
3224     DBUG_ASSERT(dec);
3225   }
3226   int cmp(const uchar *,const uchar *);
pack_length()3227   uint32 pack_length() const { return Type_handler_datetime::hires_bytes(dec); }
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)3228   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
3229   { return Field_datetime_hires::get_TIME(ltime, ptr, fuzzydate); }
size_of()3230   uint size_of() const { return sizeof(*this); }
3231 };
3232 
3233 
3234 /**
3235   DATETIME(0..6) - MySQL56 version
3236 */
3237 class Field_datetimef :public Field_datetime_with_dec {
3238   void store_TIME(MYSQL_TIME *ltime);
3239   bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
save_field_metadata(uchar * metadata_ptr)3240   int save_field_metadata(uchar *metadata_ptr)
3241   {
3242     *metadata_ptr= (uchar) decimals();
3243     return 1;
3244   }
3245 public:
Field_datetimef(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3246   Field_datetimef(uchar *ptr_arg, uchar *null_ptr_arg,
3247                   uchar null_bit_arg, enum utype unireg_check_arg,
3248                   const LEX_CSTRING *field_name_arg, uint dec_arg)
3249     :Field_datetime_with_dec(ptr_arg, null_ptr_arg, null_bit_arg,
3250                              unireg_check_arg, field_name_arg, dec_arg)
3251   {}
type_handler()3252   const Type_handler *type_handler() const { return &type_handler_datetime2; }
binlog_type()3253   enum_field_types binlog_type() const { return MYSQL_TYPE_DATETIME2; }
pack_length()3254   uint32 pack_length() const
3255   {
3256     return my_datetime_binary_length(dec);
3257   }
row_pack_length()3258   uint row_pack_length() const { return pack_length(); }
pack_length_from_metadata(uint field_metadata)3259   uint pack_length_from_metadata(uint field_metadata)
3260   {
3261     DBUG_ENTER("Field_datetimef::pack_length_from_metadata");
3262     uint tmp= my_datetime_binary_length(field_metadata);
3263     DBUG_RETURN(tmp);
3264   }
cmp(const uchar * a_ptr,const uchar * b_ptr)3265   int cmp(const uchar *a_ptr, const uchar *b_ptr)
3266   {
3267     return memcmp(a_ptr, b_ptr, pack_length());
3268   }
3269   int reset();
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)3270   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
3271   { return Field_datetimef::get_TIME(ltime, ptr, fuzzydate); }
size_of()3272   uint size_of() const { return sizeof(*this); }
3273 };
3274 
3275 
3276 static inline Field_timestamp *
new_Field_timestamp(MEM_ROOT * root,uchar * ptr,uchar * null_ptr,uchar null_bit,enum Field::utype unireg_check,const LEX_CSTRING * field_name,TABLE_SHARE * share,uint dec)3277 new_Field_timestamp(MEM_ROOT *root,uchar *ptr, uchar *null_ptr, uchar null_bit,
3278                     enum Field::utype unireg_check,
3279                     const LEX_CSTRING *field_name,
3280                     TABLE_SHARE *share, uint dec)
3281 {
3282   if (dec==0)
3283     return new (root)
3284       Field_timestamp(ptr, MAX_DATETIME_WIDTH, null_ptr,
3285                       null_bit, unireg_check, field_name, share);
3286   if (dec >= FLOATING_POINT_DECIMALS)
3287     dec= MAX_DATETIME_PRECISION;
3288   return new (root)
3289     Field_timestamp_hires(ptr, null_ptr, null_bit, unireg_check,
3290                           field_name, share, dec);
3291 }
3292 
3293 static inline Field_time *
new_Field_time(MEM_ROOT * root,uchar * ptr,uchar * null_ptr,uchar null_bit,enum Field::utype unireg_check,const LEX_CSTRING * field_name,uint dec)3294 new_Field_time(MEM_ROOT *root, uchar *ptr, uchar *null_ptr, uchar null_bit,
3295                enum Field::utype unireg_check, const LEX_CSTRING *field_name,
3296                uint dec)
3297 {
3298   if (dec == 0)
3299     return new (root)
3300       Field_time(ptr, MIN_TIME_WIDTH, null_ptr, null_bit, unireg_check,
3301                  field_name);
3302   if (dec >= FLOATING_POINT_DECIMALS)
3303     dec= MAX_DATETIME_PRECISION;
3304   return new (root)
3305     Field_time_hires(ptr, null_ptr, null_bit, unireg_check, field_name, dec);
3306 }
3307 
3308 static inline Field_datetime *
new_Field_datetime(MEM_ROOT * root,uchar * ptr,uchar * null_ptr,uchar null_bit,enum Field::utype unireg_check,const LEX_CSTRING * field_name,uint dec)3309 new_Field_datetime(MEM_ROOT *root, uchar *ptr, uchar *null_ptr, uchar null_bit,
3310                    enum Field::utype unireg_check,
3311                    const LEX_CSTRING *field_name, uint dec)
3312 {
3313   if (dec == 0)
3314     return new (root)
3315       Field_datetime(ptr, MAX_DATETIME_WIDTH, null_ptr, null_bit,
3316                      unireg_check, field_name);
3317   if (dec >= FLOATING_POINT_DECIMALS)
3318     dec= MAX_DATETIME_PRECISION;
3319   return new (root)
3320     Field_datetime_hires(ptr, null_ptr, null_bit,
3321                          unireg_check, field_name, dec);
3322 }
3323 
3324 class Field_string :public Field_longstr {
3325   class Warn_filter_string: public Warn_filter
3326   {
3327   public:
3328     Warn_filter_string(const THD *thd, const Field_string *field);
3329   };
is_var_string()3330   bool is_var_string() const
3331   {
3332     return can_alter_field_type &&
3333            orig_table &&
3334            (orig_table->s->db_create_options & HA_OPTION_PACK_RECORD) &&
3335            field_length >= 4 &&
3336            orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR;
3337   }
3338 public:
3339   bool can_alter_field_type;
Field_string(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)3340   Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
3341 	       uchar null_bit_arg,
3342 	       enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3343 	       const DTCollation &collation)
3344     :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3345                    unireg_check_arg, field_name_arg, collation),
3346      can_alter_field_type(1) {};
Field_string(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)3347   Field_string(uint32 len_arg,bool maybe_null_arg,
3348                const LEX_CSTRING *field_name_arg,
3349                const DTCollation &collation)
3350     :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3351                    NONE, field_name_arg, collation),
3352      can_alter_field_type(1) {};
3353 
type_handler()3354   const Type_handler *type_handler() const
3355   {
3356     if (is_var_string())
3357       return &type_handler_var_string;
3358     return &type_handler_string;
3359   }
key_type()3360   enum ha_base_keytype key_type() const
3361     { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
zero_pack()3362   bool zero_pack() const { return 0; }
3363   Copy_func *get_copy_func(const Field *from) const;
reset(void)3364   int reset(void)
3365   {
3366     charset()->cset->fill(charset(),(char*) ptr, field_length,
3367                           (has_charset() ? ' ' : 0));
3368     return 0;
3369   }
3370   int store(const char *to,size_t length,CHARSET_INFO *charset);
3371   using Field_str::store;
3372   double val_real(void);
3373   longlong val_int(void);
3374   String *val_str(String*,String *);
3375   my_decimal *val_decimal(my_decimal *);
3376   int cmp(const uchar *,const uchar *);
3377   void sort_string(uchar *buff,uint length);
3378   void sql_type(String &str) const;
3379   void sql_rpl_type(String*) const;
3380   virtual uchar *pack(uchar *to, const uchar *from,
3381                       uint max_length);
3382   virtual const uchar *unpack(uchar* to, const uchar *from,
3383                               const uchar *from_end,uint param_data);
pack_length_from_metadata(uint field_metadata)3384   uint pack_length_from_metadata(uint field_metadata)
3385   {
3386     DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
3387     if (field_metadata == 0)
3388       return row_pack_length();
3389     return (((field_metadata >> 4) & 0x300) ^ 0x300) + (field_metadata & 0x00ff);
3390   }
3391   bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
3392                              uint16 mflags, int *order_var);
row_pack_length()3393   uint row_pack_length() const { return field_length; }
3394   int pack_cmp(const uchar *a,const uchar *b,uint key_length,
3395                bool insert_or_update);
3396   int pack_cmp(const uchar *b,uint key_length,bool insert_or_update);
3397   uint packed_col_length(const uchar *to, uint length);
3398   uint max_packed_col_length(uint max_length);
size_of()3399   uint size_of() const { return sizeof(*this); }
has_charset(void)3400   bool has_charset(void) const
3401   { return charset() == &my_charset_bin ? FALSE : TRUE; }
3402   Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
3403   virtual uint get_key_image(uchar *buff,uint length, imagetype type);
3404   sql_mode_t value_depends_on_sql_mode() const;
3405   sql_mode_t can_handle_sql_mode_dependency_on_store() const;
3406 private:
3407   int save_field_metadata(uchar *first_byte);
3408 };
3409 
3410 
3411 class Field_varstring :public Field_longstr {
3412 public:
get_data()3413   uchar *get_data() const
3414   {
3415     return ptr + length_bytes;
3416   }
get_length()3417   uint get_length() const
3418   {
3419     return length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
3420   }
3421 protected:
store_length(uint32 number)3422   void store_length(uint32 number)
3423   {
3424     if (length_bytes == 1)
3425       *ptr= (uchar) number;
3426     else
3427       int2store(ptr, number);
3428   }
3429 public:
3430   /*
3431     The maximum space available in a Field_varstring, in bytes. See
3432     length_bytes.
3433   */
3434   static const uint MAX_SIZE;
3435   /* Store number of bytes used to store length (1 or 2) */
3436   uint32 length_bytes;
Field_varstring(uchar * ptr_arg,uint32 len_arg,uint length_bytes_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,const DTCollation & collation)3437   Field_varstring(uchar *ptr_arg,
3438                   uint32 len_arg, uint length_bytes_arg,
3439                   uchar *null_ptr_arg, uchar null_bit_arg,
3440 		  enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3441 		  TABLE_SHARE *share, const DTCollation &collation)
3442     :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3443                    unireg_check_arg, field_name_arg, collation),
3444      length_bytes(length_bytes_arg)
3445   {
3446     share->varchar_fields++;
3447   }
Field_varstring(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,const DTCollation & collation)3448   Field_varstring(uint32 len_arg,bool maybe_null_arg,
3449                   const LEX_CSTRING *field_name_arg,
3450                   TABLE_SHARE *share, const DTCollation &collation)
3451     :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3452                    NONE, field_name_arg, collation),
3453      length_bytes(len_arg < 256 ? 1 :2)
3454   {
3455     share->varchar_fields++;
3456   }
3457 
type_handler()3458   const Type_handler *type_handler() const { return &type_handler_varchar; }
3459   enum ha_base_keytype key_type() const;
row_pack_length()3460   uint row_pack_length() const { return field_length; }
zero_pack()3461   bool zero_pack() const { return 0; }
reset(void)3462   int  reset(void) { bzero(ptr,field_length+length_bytes); return 0; }
pack_length()3463   uint32 pack_length() const { return (uint32) field_length+length_bytes; }
key_length()3464   uint32 key_length() const { return (uint32) field_length; }
sort_length()3465   uint32 sort_length() const
3466   {
3467     return (uint32) field_length + (field_charset == &my_charset_bin ?
3468                                     length_bytes : 0);
3469   }
3470   Copy_func *get_copy_func(const Field *from) const;
3471   bool memcpy_field_possible(const Field *from) const;
3472   int  store(const char *to,size_t length,CHARSET_INFO *charset);
3473   using Field_str::store;
3474 #ifdef HAVE_valgrind
3475   void mark_unused_memory_as_defined();
3476 #endif
3477   double val_real(void);
3478   longlong val_int(void);
3479   String *val_str(String*,String *);
3480   my_decimal *val_decimal(my_decimal *);
3481   int cmp(const uchar *a,const uchar *b);
3482   int cmp_prefix(const uchar *a, const uchar *b, size_t prefix_len);
3483   void sort_string(uchar *buff,uint length);
3484   uint get_key_image(uchar *buff,uint length, imagetype type);
3485   void set_key_image(const uchar *buff,uint length);
3486   void sql_type(String &str) const;
3487   void sql_rpl_type(String*) const;
3488   virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
3489   virtual const uchar *unpack(uchar* to, const uchar *from,
3490                               const uchar *from_end, uint param_data);
3491   int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0U);
3492   int key_cmp(const uchar *,const uchar*);
3493   int key_cmp(const uchar *str, uint length);
3494   uint packed_col_length(const uchar *to, uint length);
3495   uint max_packed_col_length(uint max_length);
3496   uint32 data_length();
size_of()3497   uint size_of() const { return sizeof(*this); }
has_charset(void)3498   bool has_charset(void) const
3499   { return charset() == &my_charset_bin ? FALSE : TRUE; }
3500   Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
3501   Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
3502                        uchar *new_ptr, uint32 length,
3503                        uchar *new_null_ptr, uint new_null_bit);
3504   uint is_equal(Create_field *new_field);
3505   void hash(ulong *nr, ulong *nr2);
length_size()3506   uint length_size() { return length_bytes; }
3507 private:
3508   int save_field_metadata(uchar *first_byte);
3509 };
3510 
3511 
3512 class Field_varstring_compressed: public Field_varstring {
3513 public:
Field_varstring_compressed(uchar * ptr_arg,uint32 len_arg,uint length_bytes_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,const DTCollation & collation,Compression_method * compression_method_arg)3514   Field_varstring_compressed(uchar *ptr_arg,
3515                              uint32 len_arg, uint length_bytes_arg,
3516                              uchar *null_ptr_arg, uchar null_bit_arg,
3517                              enum utype unireg_check_arg,
3518                              const LEX_CSTRING *field_name_arg,
3519                              TABLE_SHARE *share, const DTCollation &collation,
3520                              Compression_method *compression_method_arg):
3521     Field_varstring(ptr_arg, len_arg, length_bytes_arg, null_ptr_arg,
3522                     null_bit_arg, unireg_check_arg, field_name_arg,
3523                     share, collation),
3524     compression_method_ptr(compression_method_arg) { DBUG_ASSERT(len_arg > 0); }
compression_method()3525   Compression_method *compression_method() const
3526   { return compression_method_ptr; }
3527 private:
3528   Compression_method *compression_method_ptr;
3529   int store(const char *to, size_t length, CHARSET_INFO *charset);
3530   using Field_str::store;
3531   String *val_str(String *, String *);
3532   double val_real(void);
3533   longlong val_int(void);
size_of()3534   uint size_of() const { return sizeof(*this); }
binlog_type()3535   enum_field_types binlog_type() const { return MYSQL_TYPE_VARCHAR_COMPRESSED; }
sql_type(String & str)3536   void sql_type(String &str) const
3537   {
3538     Field_varstring::sql_type(str);
3539     str.append(STRING_WITH_LEN(" /*!100301 COMPRESSED*/"));
3540   }
max_display_length()3541   uint32 max_display_length() const { return field_length - 1; }
char_length()3542   uint32 char_length() const
3543   {
3544     return (field_length - 1) / field_charset->mbmaxlen;
3545   }
3546   int cmp(const uchar *a_ptr, const uchar *b_ptr);
3547 
3548   /*
3549     Compressed fields can't have keys as two rows may have different
3550     compression methods or compression levels.
3551   */
3552 
key_cmp(const uchar * str,uint length)3553   int key_cmp(const uchar *str, uint length)
3554   { DBUG_ASSERT(0); return 0; }
3555   using Field_varstring::key_cmp;
3556 };
3557 
3558 
number_storage_requirement(uint32 n)3559 static inline uint8 number_storage_requirement(uint32 n)
3560 {
3561   return n < 256 ? 1 : n < 65536 ? 2 : n < 16777216 ? 3 : 4;
3562 }
3563 
3564 
store_bigendian(ulonglong num,uchar * to,uint bytes)3565 static inline void store_bigendian(ulonglong num, uchar *to, uint bytes)
3566 {
3567   switch(bytes) {
3568   case 1: mi_int1store(to, num); break;
3569   case 2: mi_int2store(to, num); break;
3570   case 3: mi_int3store(to, num); break;
3571   case 4: mi_int4store(to, num); break;
3572   case 5: mi_int5store(to, num); break;
3573   case 6: mi_int6store(to, num); break;
3574   case 7: mi_int7store(to, num); break;
3575   case 8: mi_int8store(to, num); break;
3576   default: DBUG_ASSERT(0);
3577   }
3578 }
3579 
3580 
read_bigendian(const uchar * from,uint bytes)3581 static inline longlong read_bigendian(const uchar *from, uint bytes)
3582 {
3583   switch(bytes) {
3584   case 1: return mi_uint1korr(from);
3585   case 2: return mi_uint2korr(from);
3586   case 3: return mi_uint3korr(from);
3587   case 4: return mi_uint4korr(from);
3588   case 5: return mi_uint5korr(from);
3589   case 6: return mi_uint6korr(from);
3590   case 7: return mi_uint7korr(from);
3591   case 8: return mi_sint8korr(from);
3592   default: DBUG_ASSERT(0); return 0;
3593   }
3594 }
3595 
3596 
3597 extern LEX_CSTRING temp_lex_str;
3598 
3599 class Field_blob :public Field_longstr {
3600 protected:
3601   /**
3602     The number of bytes used to represent the length of the blob.
3603   */
3604   uint packlength;
3605 
3606   /**
3607     The 'value'-object is a cache fronting the storage engine.
3608   */
3609   String value;
3610   /**
3611      Cache for blob values when reading a row with a virtual blob
3612      field. This is needed to not destroy the old cached value when
3613      updating the blob with a new value when creating the new row.
3614   */
3615   String read_value;
3616 
3617   static void do_copy_blob(Copy_field *copy);
3618   static void do_conv_blob(Copy_field *copy);
3619 public:
3620   Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3621 	     enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3622 	     TABLE_SHARE *share, uint blob_pack_length,
3623 	     const DTCollation &collation);
Field_blob(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)3624   Field_blob(uint32 len_arg,bool maybe_null_arg, const LEX_CSTRING *field_name_arg,
3625              const DTCollation &collation)
3626     :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3627                    NONE, field_name_arg, collation),
3628     packlength(4)
3629   {
3630     flags|= BLOB_FLAG;
3631   }
Field_blob(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation,bool set_packlength)3632   Field_blob(uint32 len_arg,bool maybe_null_arg,
3633              const LEX_CSTRING *field_name_arg,
3634              const DTCollation &collation, bool set_packlength)
3635     :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3636                    NONE, field_name_arg, collation)
3637   {
3638     flags|= BLOB_FLAG;
3639     packlength= set_packlength ? number_storage_requirement(len_arg) : 4;
3640   }
Field_blob(uint32 packlength_arg)3641   Field_blob(uint32 packlength_arg)
3642     :Field_longstr((uchar*) 0, 0, (uchar*) "", 0, NONE, &temp_lex_str,
3643                    system_charset_info),
3644     packlength(packlength_arg) {}
3645   const Type_handler *type_handler() const;
3646   /* Note that the default copy constructor is used, in clone() */
type()3647   enum_field_types type() const
3648   {
3649     /*
3650       We cannot return type_handler()->field_type() here.
3651       Some pieces of the code (e.g. in engines) rely on the fact
3652       that Field::type(), Field::real_type() and Item_field::field_type()
3653       return MYSQL_TYPE_BLOB for all blob variants.
3654       We should eventually fix all such code pieces to expect
3655       all BLOB type codes.
3656     */
3657     return MYSQL_TYPE_BLOB;
3658   }
real_type()3659   enum_field_types real_type() const
3660   {
3661     return MYSQL_TYPE_BLOB;
3662   }
key_type()3663   enum ha_base_keytype key_type() const
3664     { return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2; }
type_std_attributes()3665   Type_std_attributes type_std_attributes() const
3666   {
3667     return Type_std_attributes(Field_blob::max_display_length(), decimals(),
3668                                MY_TEST(flags & UNSIGNED_FLAG),
3669                                dtcollation());
3670   }
3671   Information_schema_character_attributes
information_schema_character_attributes()3672     information_schema_character_attributes() const
3673   {
3674     uint32 octets= Field_blob::octet_length();
3675     uint32 chars= octets / field_charset->mbminlen;
3676     return Information_schema_character_attributes(octets, chars);
3677   }
get_copy_func(const Field * from)3678   Copy_func *get_copy_func(const Field *from) const
3679   {
3680     /*
3681     TODO: MDEV-9331
3682     if (from->type() == MYSQL_TYPE_BIT)
3683       return do_field_int;
3684     */
3685     if (!(from->flags & BLOB_FLAG) || from->charset() != charset() ||
3686         !from->compression_method() != !compression_method())
3687       return do_conv_blob;
3688     if (from->pack_length() != Field_blob::pack_length())
3689       return do_copy_blob;
3690     return get_identical_copy_func();
3691   }
store_field(Field * from)3692   int  store_field(Field *from)
3693   {                                             // Be sure the value is stored
3694     from->val_str(&value);
3695     if (table->copy_blobs ||
3696         (!value.is_alloced() && from->is_varchar_and_in_write_set()))
3697       value.copy();
3698     return store(value.ptr(), value.length(), from->charset());
3699   }
memcpy_field_possible(const Field * from)3700   bool memcpy_field_possible(const Field *from) const
3701   {
3702     return Field_str::memcpy_field_possible(from) &&
3703            !compression_method() == !from->compression_method() &&
3704            !table->copy_blobs;
3705   }
3706   int store(const char *to, size_t length, CHARSET_INFO *charset);
3707   using Field_str::store;
3708   double val_real(void);
3709   longlong val_int(void);
3710   String *val_str(String*,String *);
3711   my_decimal *val_decimal(my_decimal *);
3712   int cmp(const uchar *a,const uchar *b);
3713   int cmp_prefix(const uchar *a, const uchar *b, size_t prefix_len);
3714   int cmp(const uchar *a, uint32 a_length, const uchar *b, uint32 b_length);
3715   int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0U);
3716   int key_cmp(const uchar *,const uchar*);
3717   int key_cmp(const uchar *str, uint length);
3718   /* Never update the value of min_val for a blob field */
update_min(Field * min_val,bool force_update)3719   bool update_min(Field *min_val, bool force_update) { return FALSE; }
3720   /* Never update the value of max_val for a blob field */
update_max(Field * max_val,bool force_update)3721   bool update_max(Field *max_val, bool force_update) { return FALSE; }
key_length()3722   uint32 key_length() const { return 0; }
3723   void sort_string(uchar *buff,uint length);
pack_length()3724   uint32 pack_length() const
3725   { return (uint32) (packlength + portable_sizeof_char_ptr); }
3726 
3727   /**
3728      Return the packed length without the pointer size added.
3729 
3730      This is used to determine the size of the actual data in the row
3731      buffer.
3732 
3733      @returns The length of the raw data itself without the pointer.
3734   */
pack_length_no_ptr()3735   uint32 pack_length_no_ptr() const
3736   { return (uint32) (packlength); }
row_pack_length()3737   uint row_pack_length() const { return pack_length_no_ptr(); }
3738   uint32 sort_length() const;
value_length()3739   uint32 value_length() { return get_length(); }
max_data_length()3740   virtual uint32 max_data_length() const
3741   {
3742     return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
3743   }
reset(void)3744   int reset(void) { bzero(ptr, packlength+sizeof(uchar*)); return 0; }
reset_fields()3745   void reset_fields() { bzero((uchar*) &value,sizeof(value)); bzero((uchar*) &read_value,sizeof(read_value)); }
get_field_buffer_size(void)3746   uint32 get_field_buffer_size(void) { return value.alloced_length(); }
3747   void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number);
store_length(size_t number)3748   inline void store_length(size_t number)
3749   {
3750     DBUG_ASSERT(number < UINT_MAX32);
3751     store_length(ptr, packlength, (uint32)number);
3752   }
3753   inline uint32 get_length(my_ptrdiff_t row_offset= 0) const
3754   { return get_length(ptr+row_offset, this->packlength); }
3755   uint32 get_length(const uchar *ptr, uint packlength) const;
get_length(const uchar * ptr_arg)3756   uint32 get_length(const uchar *ptr_arg) const
3757   { return get_length(ptr_arg, this->packlength); }
get_ptr()3758   inline uchar *get_ptr() const { return get_ptr(0); }
get_ptr(my_ptrdiff_t row_offset)3759   inline uchar *get_ptr(my_ptrdiff_t row_offset) const
3760   {
3761     uchar *s;
3762     memcpy(&s, ptr + packlength + row_offset, sizeof(uchar*));
3763     return s;
3764   }
set_ptr(uchar * length,uchar * data)3765   inline void set_ptr(uchar *length, uchar *data)
3766   {
3767     memcpy(ptr,length,packlength);
3768     memcpy(ptr+packlength, &data,sizeof(char*));
3769   }
set_ptr_offset(my_ptrdiff_t ptr_diff,uint32 length,const uchar * data)3770   void set_ptr_offset(my_ptrdiff_t ptr_diff, uint32 length, const uchar *data)
3771   {
3772     uchar *ptr_ofs= ADD_TO_PTR(ptr,ptr_diff,uchar*);
3773     store_length(ptr_ofs, packlength, length);
3774     memcpy(ptr_ofs+packlength, &data, sizeof(char*));
3775   }
set_ptr(uint32 length,uchar * data)3776   inline void set_ptr(uint32 length, uchar *data)
3777   {
3778     set_ptr_offset(0, length, data);
3779   }
3780   int copy_value(Field_blob *from);
3781   uint get_key_image(uchar *buff,uint length, imagetype type);
3782   void set_key_image(const uchar *buff,uint length);
3783   Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
3784                        uchar *new_ptr, uint32 length,
3785                        uchar *new_null_ptr, uint new_null_bit);
3786   void sql_type(String &str) const;
3787   /**
3788      Copy blob buffer into internal storage "value" and update record pointer.
3789 
3790      @retval true     Memory allocation error
3791      @retval false    Success
3792   */
copy()3793   inline bool copy()
3794   {
3795     uchar *tmp= get_ptr();
3796     if (value.copy((char*) tmp, get_length(), charset()))
3797     {
3798       Field_blob::reset();
3799       return 1;
3800     }
3801     tmp=(uchar*) value.ptr();
3802     memcpy(ptr+packlength, &tmp, sizeof(char*));
3803     return 0;
3804   }
swap(String & inout,bool set_read_value)3805   void swap(String &inout, bool set_read_value)
3806   {
3807     if (set_read_value)
3808       read_value.swap(inout);
3809     else
3810       value.swap(inout);
3811   }
3812   /**
3813      Return pointer to blob cache or NULL if not cached.
3814   */
cached(bool * set_read_value)3815   String * cached(bool *set_read_value)
3816   {
3817     char *tmp= (char *) get_ptr();
3818     if (!value.is_empty() && tmp == value.ptr())
3819     {
3820       *set_read_value= false;
3821       return &value;
3822     }
3823 
3824     if (!read_value.is_empty() && tmp == read_value.ptr())
3825     {
3826       *set_read_value= true;
3827       return &read_value;
3828     }
3829 
3830     return NULL;
3831   }
3832   /* store value for the duration of the current read record */
swap_value_and_read_value()3833   inline void swap_value_and_read_value()
3834   {
3835     read_value.swap(value);
3836   }
set_value(uchar * data)3837   inline void set_value(uchar *data)
3838   {
3839     /* Set value pointer. Lengths are not important */
3840     value.reset((char*) data, 1, 1, &my_charset_bin);
3841   }
3842   virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
3843   virtual const uchar *unpack(uchar *to, const uchar *from,
3844                               const uchar *from_end, uint param_data);
3845   uint packed_col_length(const uchar *col_ptr, uint length);
3846   uint max_packed_col_length(uint max_length);
free()3847   void free()
3848   {
3849     value.free();
3850     read_value.free();
3851   }
clear_temporary()3852   inline void clear_temporary()
3853   {
3854     uchar *tmp= get_ptr();
3855     if (likely(value.ptr() == (char*) tmp))
3856       bzero((uchar*) &value, sizeof(value));
3857     else
3858     {
3859       /*
3860         Currently read_value should never point to tmp, the following code
3861         is mainly here to make things future proof.
3862       */
3863       if (unlikely(read_value.ptr() == (char*) tmp))
3864         bzero((uchar*) &read_value, sizeof(read_value));
3865     }
3866   }
size_of()3867   uint size_of() const { return sizeof(*this); }
has_charset(void)3868   bool has_charset(void) const
3869   { return charset() == &my_charset_bin ? FALSE : TRUE; }
3870   uint32 max_display_length() const;
3871   uint32 char_length() const;
3872   uint32 octet_length() const;
3873   uint is_equal(Create_field *new_field);
3874 
3875   friend void TABLE::remember_blob_values(String *blob_storage);
3876   friend void TABLE::restore_blob_values(String *blob_storage);
3877 
3878 private:
3879   int save_field_metadata(uchar *first_byte);
3880 };
3881 
3882 
3883 class Field_blob_compressed: public Field_blob {
3884 public:
Field_blob_compressed(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint blob_pack_length,const DTCollation & collation,Compression_method * compression_method_arg)3885   Field_blob_compressed(uchar *ptr_arg, uchar *null_ptr_arg,
3886                         uchar null_bit_arg, enum utype unireg_check_arg,
3887                         const LEX_CSTRING *field_name_arg, TABLE_SHARE *share,
3888                         uint blob_pack_length, const DTCollation &collation,
3889                         Compression_method *compression_method_arg):
3890     Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
3891                field_name_arg, share, blob_pack_length, collation),
3892     compression_method_ptr(compression_method_arg) {}
compression_method()3893   Compression_method *compression_method() const
3894   { return compression_method_ptr; }
3895 private:
3896   Compression_method *compression_method_ptr;
3897   int store(const char *to, size_t length, CHARSET_INFO *charset);
3898   using Field_str::store;
3899   String *val_str(String *, String *);
3900   double val_real(void);
3901   longlong val_int(void);
size_of()3902   uint size_of() const { return sizeof(*this); }
binlog_type()3903   enum_field_types binlog_type() const { return MYSQL_TYPE_BLOB_COMPRESSED; }
sql_type(String & str)3904   void sql_type(String &str) const
3905   {
3906     Field_blob::sql_type(str);
3907     str.append(STRING_WITH_LEN(" /*!100301 COMPRESSED*/"));
3908   }
3909 
3910   /*
3911     Compressed fields can't have keys as two rows may have different
3912     compression methods or compression levels.
3913   */
3914 
get_key_image(uchar * buff,uint length,imagetype type_arg)3915   uint get_key_image(uchar *buff, uint length, imagetype type_arg)
3916   { DBUG_ASSERT(0); return 0; }
set_key_image(const uchar * buff,uint length)3917   void set_key_image(const uchar *buff, uint length)
3918   { DBUG_ASSERT(0); }
key_cmp(const uchar * a,const uchar * b)3919   int key_cmp(const uchar *a, const uchar *b)
3920   { DBUG_ASSERT(0); return 0; }
key_cmp(const uchar * str,uint length)3921   int key_cmp(const uchar *str, uint length)
3922   { DBUG_ASSERT(0); return 0; }
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uint32 length,uchar * new_null_ptr,uint new_null_bit)3923   Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
3924                        uchar *new_ptr, uint32 length,
3925                        uchar *new_null_ptr, uint new_null_bit)
3926   { DBUG_ASSERT(0); return 0; }
3927 };
3928 
3929 
3930 #ifdef HAVE_SPATIAL
3931 class Field_geom :public Field_blob {
3932 public:
3933   enum geometry_type geom_type;
3934   uint srid;
3935   uint precision;
3936   enum storage_type { GEOM_STORAGE_WKB= 0, GEOM_STORAGE_BINARY= 1};
3937   enum storage_type storage;
3938 
Field_geom(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint blob_pack_length,enum geometry_type geom_type_arg,uint field_srid)3939   Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3940 	     enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3941 	     TABLE_SHARE *share, uint blob_pack_length,
3942 	     enum geometry_type geom_type_arg, uint field_srid)
3943      :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
3944                  field_name_arg, share, blob_pack_length, &my_charset_bin)
3945   { geom_type= geom_type_arg; srid= field_srid; }
key_type()3946   enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY2; }
type_handler()3947   const Type_handler *type_handler() const
3948   {
3949     return &type_handler_geometry;
3950   }
type()3951   enum_field_types type() const
3952   {
3953     return MYSQL_TYPE_GEOMETRY;
3954   }
real_type()3955   enum_field_types real_type() const
3956   {
3957     return MYSQL_TYPE_GEOMETRY;
3958   }
3959   Information_schema_character_attributes
information_schema_character_attributes()3960     information_schema_character_attributes() const
3961   {
3962     return Information_schema_character_attributes();
3963   }
3964   bool can_optimize_range(const Item_bool_func *cond,
3965                                   const Item *item,
3966                                   bool is_eq_func) const;
3967   void sql_type(String &str) const;
3968   uint is_equal(Create_field *new_field);
3969   int  store(const char *to, size_t length, CHARSET_INFO *charset);
3970   int  store(double nr);
3971   int  store(longlong nr, bool unsigned_val);
3972   int  store_decimal(const my_decimal *);
size_of()3973   uint size_of() const { return sizeof(*this); }
3974   /**
3975    Key length is provided only to support hash joins. (compared byte for byte)
3976    Ex: SELECT .. FROM t1,t2 WHERE t1.field_geom1=t2.field_geom2.
3977 
3978    The comparison is not very relevant, as identical geometry might be
3979    represented differently, but we need to support it either way.
3980   */
key_length()3981   uint32 key_length() const { return packlength; }
3982 
3983   /**
3984     Non-nullable GEOMETRY types cannot have defaults,
3985     but the underlying blob must still be reset.
3986    */
reset(void)3987   int reset(void) { return Field_blob::reset() || !maybe_null(); }
3988   bool load_data_set_null(THD *thd);
3989   bool load_data_set_no_data(THD *thd, bool fixed_format);
3990 
get_geometry_type()3991   geometry_type get_geometry_type() { return geom_type; };
3992   static geometry_type geometry_type_merge(geometry_type, geometry_type);
get_srid()3993   uint get_srid() { return srid; }
3994 };
3995 
3996 uint gis_field_options_image(uchar *buff, List<Create_field> &create_fields);
3997 uint gis_field_options_read(const uchar *buf, size_t buf_len,
3998       Field_geom::storage_type *st_type,uint *precision, uint *scale, uint *srid);
3999 
4000 #endif /*HAVE_SPATIAL*/
4001 
4002 
4003 class Field_enum :public Field_str {
4004   static void do_field_enum(Copy_field *copy_field);
4005   bool can_optimize_range_or_keypart_ref(const Item_bool_func *cond,
4006                                          const Item *item) const;
4007 protected:
4008   uint packlength;
4009 public:
4010   TYPELIB *typelib;
Field_enum(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint packlength_arg,TYPELIB * typelib_arg,const DTCollation & collation)4011   Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
4012              uchar null_bit_arg,
4013              enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
4014              uint packlength_arg,
4015              TYPELIB *typelib_arg,
4016              const DTCollation &collation)
4017     :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
4018 	       unireg_check_arg, field_name_arg, collation),
4019     packlength(packlength_arg),typelib(typelib_arg)
4020   {
4021       flags|=ENUM_FLAG;
4022   }
4023   Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
type_handler()4024   const Type_handler *type_handler() const { return &type_handler_enum; }
4025   enum ha_base_keytype key_type() const;
4026   sql_mode_t can_handle_sql_mode_dependency_on_store() const;
get_copy_func(const Field * from)4027   Copy_func *get_copy_func(const Field *from) const
4028   {
4029     if (eq_def(from))
4030       return get_identical_copy_func();
4031     if (real_type() == MYSQL_TYPE_ENUM &&
4032         from->real_type() == MYSQL_TYPE_ENUM)
4033       return do_field_enum;
4034     if (from->result_type() == STRING_RESULT)
4035       return do_field_string;
4036     return do_field_int;
4037   }
store_field(Field * from)4038   int store_field(Field *from)
4039   {
4040     if (from->real_type() == MYSQL_TYPE_ENUM && from->val_int() == 0)
4041     {
4042       store_type(0);
4043       return 0;
4044     }
4045     return from->save_in_field(this);
4046   }
save_in_field(Field * to)4047   int save_in_field(Field *to)
4048   {
4049     if (to->result_type() != STRING_RESULT)
4050       return to->store(val_int(), 0);
4051     return save_in_field_str(to);
4052   }
memcpy_field_possible(const Field * from)4053   bool memcpy_field_possible(const Field *from) const { return false; }
4054   int  store(const char *to,size_t length,CHARSET_INFO *charset);
4055   int  store(double nr);
4056   int  store(longlong nr, bool unsigned_val);
4057   double val_real(void);
4058   longlong val_int(void);
4059   String *val_str(String*,String *);
4060   int cmp(const uchar *,const uchar *);
4061   void sort_string(uchar *buff,uint length);
pack_length()4062   uint32 pack_length() const { return (uint32) packlength; }
4063   void store_type(ulonglong value);
4064   void sql_type(String &str) const;
size_of()4065   uint size_of() const { return sizeof(*this); }
pack_length_from_metadata(uint field_metadata)4066   uint pack_length_from_metadata(uint field_metadata)
4067   { return (field_metadata & 0x00ff); }
row_pack_length()4068   uint row_pack_length() const { return pack_length(); }
zero_pack()4069   virtual bool zero_pack() const { return 0; }
optimize_range(uint idx,uint part)4070   bool optimize_range(uint idx, uint part) const { return 0; }
4071   bool eq_def(const Field *field) const;
has_charset(void)4072   bool has_charset(void) const { return TRUE; }
4073   /* enum and set are sorted as integers */
sort_charset(void)4074   CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
decimals()4075   uint decimals() const { return 0; }
get_typelib()4076   TYPELIB *get_typelib() const { return typelib; }
4077 
4078   virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
4079   virtual const uchar *unpack(uchar *to, const uchar *from,
4080                               const uchar *from_end, uint param_data);
4081 
can_optimize_keypart_ref(const Item_bool_func * cond,const Item * item)4082   bool can_optimize_keypart_ref(const Item_bool_func *cond,
4083                                 const Item *item) const
4084   {
4085     return can_optimize_range_or_keypart_ref(cond, item);
4086   }
can_optimize_group_min_max(const Item_bool_func * cond,const Item * const_item)4087   bool can_optimize_group_min_max(const Item_bool_func *cond,
4088                                   const Item *const_item) const
4089   {
4090     /*
4091       Can't use GROUP_MIN_MAX optimization for ENUM and SET,
4092       because the values are stored as numbers in index,
4093       while MIN() and MAX() work as strings.
4094       It would return the records with min and max enum numeric indexes.
4095      "Bug#45300 MAX() and ENUM type" should be fixed first.
4096     */
4097     return false;
4098   }
can_optimize_range(const Item_bool_func * cond,const Item * item,bool is_eq_func)4099   bool can_optimize_range(const Item_bool_func *cond,
4100                           const Item *item,
4101                           bool is_eq_func) const
4102   {
4103     return can_optimize_range_or_keypart_ref(cond, item);
4104   }
4105 private:
4106   int save_field_metadata(uchar *first_byte);
4107   uint is_equal(Create_field *new_field);
4108 };
4109 
4110 
4111 class Field_set :public Field_enum {
4112 public:
Field_set(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint32 packlength_arg,TYPELIB * typelib_arg,const DTCollation & collation)4113   Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
4114 	    uchar null_bit_arg,
4115 	    enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
4116 	    uint32 packlength_arg,
4117 	    TYPELIB *typelib_arg, const DTCollation &collation)
4118     :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
4119 		    unireg_check_arg, field_name_arg,
4120                 packlength_arg,
4121                 typelib_arg, collation),
4122       empty_set_string("", 0, collation.collation)
4123     {
4124       flags=(flags & ~ENUM_FLAG) | SET_FLAG;
4125     }
store_field(Field * from)4126   int  store_field(Field *from) { return from->save_in_field(this); }
4127   int  store(const char *to,size_t length,CHARSET_INFO *charset);
store(double nr)4128   int  store(double nr) { return Field_set::store((longlong) nr, FALSE); }
4129   int  store(longlong nr, bool unsigned_val);
4130 
zero_pack()4131   virtual bool zero_pack() const { return 1; }
4132   String *val_str(String*,String *);
4133   void sql_type(String &str) const;
size_of()4134   uint size_of() const { return sizeof(*this); }
type_handler()4135   const Type_handler *type_handler() const { return &type_handler_set; }
has_charset(void)4136   bool has_charset(void) const { return TRUE; }
4137 private:
4138   const String empty_set_string;
4139 };
4140 
4141 
4142 /*
4143   Note:
4144     To use Field_bit::cmp_binary() you need to copy the bits stored in
4145     the beginning of the record (the NULL bytes) to each memory you
4146     want to compare (where the arguments point).
4147 
4148     This is the reason:
4149     - Field_bit::cmp_binary() is only implemented in the base class
4150       (Field::cmp_binary()).
4151     - Field::cmp_binary() currently uses pack_length() to calculate how
4152       long the data is.
4153     - pack_length() includes size of the bits stored in the NULL bytes
4154       of the record.
4155 */
4156 class Field_bit :public Field {
4157 public:
4158   uchar *bit_ptr;     // position in record where 'uneven' bits store
4159   uchar bit_ofs;      // offset to 'uneven' high bits
4160   uint bit_len;       // number of 'uneven' high bits
4161   uint bytes_in_rec;
4162   Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
4163             uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
4164             enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg);
type_handler()4165   const Type_handler *type_handler() const { return &type_handler_bit; }
key_type()4166   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BIT; }
key_length()4167   uint32 key_length() const { return (uint32) (field_length + 7) / 8; }
max_data_length()4168   uint32 max_data_length() const { return (field_length + 7) / 8; }
max_display_length()4169   uint32 max_display_length() const { return field_length; }
4170   Information_schema_numeric_attributes
information_schema_numeric_attributes()4171     information_schema_numeric_attributes() const
4172   {
4173     return Information_schema_numeric_attributes(field_length);
4174   }
size_of()4175   uint size_of() const { return sizeof(*this); }
reset(void)4176   int reset(void) {
4177     bzero(ptr, bytes_in_rec);
4178     if (bit_ptr && (bit_len > 0))  // reset odd bits among null bits
4179       clr_rec_bits(bit_ptr, bit_ofs, bit_len);
4180     return 0;
4181   }
get_copy_func(const Field * from)4182   Copy_func *get_copy_func(const Field *from) const
4183   {
4184     if (from->cmp_type() == DECIMAL_RESULT)
4185       return do_field_decimal;
4186     return do_field_int;
4187   }
save_in_field(Field * to)4188   int save_in_field(Field *to) { return to->store(val_int(), true); }
memcpy_field_possible(const Field * from)4189   bool memcpy_field_possible(const Field *from) const { return false; }
4190   int store(const char *to, size_t length, CHARSET_INFO *charset);
4191   int store(double nr);
4192   int store(longlong nr, bool unsigned_val);
4193   int store_decimal(const my_decimal *);
4194   double val_real(void);
4195   longlong val_int(void);
4196   String *val_str(String*, String *);
str_needs_quotes()4197   virtual bool str_needs_quotes() { return TRUE; }
4198   my_decimal *val_decimal(my_decimal *);
val_bool()4199   bool val_bool() { return val_int() != 0; }
cmp(const uchar * a,const uchar * b)4200   int cmp(const uchar *a, const uchar *b)
4201   {
4202     DBUG_ASSERT(ptr == a || ptr == b);
4203     if (ptr == a)
4204       return Field_bit::key_cmp(b, bytes_in_rec + MY_TEST(bit_len));
4205     else
4206       return Field_bit::key_cmp(a, bytes_in_rec + MY_TEST(bit_len)) * -1;
4207   }
cmp_binary_offset(uint row_offset)4208   int cmp_binary_offset(uint row_offset)
4209   { return cmp_offset(row_offset); }
4210   int cmp_prefix(const uchar *a, const uchar *b, size_t prefix_len);
key_cmp(const uchar * a,const uchar * b)4211   int key_cmp(const uchar *a, const uchar *b)
4212   { return cmp_binary((uchar *) a, (uchar *) b); }
4213   int key_cmp(const uchar *str, uint length);
4214   int cmp_offset(uint row_offset);
update_min(Field * min_val,bool force_update)4215   bool update_min(Field *min_val, bool force_update)
4216   {
4217     longlong val= val_int();
4218     bool update_fl= force_update || val < min_val->val_int();
4219     if (update_fl)
4220     {
4221       min_val->set_notnull();
4222       min_val->store(val, FALSE);
4223     }
4224     return update_fl;
4225   }
update_max(Field * max_val,bool force_update)4226   bool update_max(Field *max_val, bool force_update)
4227   {
4228     longlong val= val_int();
4229     bool update_fl= force_update || val > max_val->val_int();
4230     if (update_fl)
4231     {
4232       max_val->set_notnull();
4233       max_val->store(val, FALSE);
4234     }
4235     return update_fl;
4236   }
store_field_value(uchar * val,uint len)4237   void store_field_value(uchar *val, uint len)
4238   {
4239     store(*((longlong *)val), TRUE);
4240   }
pos_in_interval(Field * min,Field * max)4241   double pos_in_interval(Field *min, Field *max)
4242   {
4243     return pos_in_interval_val_real(min, max);
4244   }
get_image(uchar * buff,uint length,CHARSET_INFO * cs)4245   void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
4246   { get_key_image(buff, length, itRAW); }
set_image(const uchar * buff,uint length,CHARSET_INFO * cs)4247   void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
4248   { Field_bit::store((char *) buff, length, cs); }
4249   uint get_key_image(uchar *buff, uint length, imagetype type);
set_key_image(const uchar * buff,uint length)4250   void set_key_image(const uchar *buff, uint length)
4251   { Field_bit::store((char*) buff, length, &my_charset_bin); }
sort_string(uchar * buff,uint length)4252   void sort_string(uchar *buff, uint length)
4253   { get_key_image(buff, length, itRAW); }
pack_length()4254   uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
pack_length_in_rec()4255   uint32 pack_length_in_rec() const { return bytes_in_rec; }
4256   uint pack_length_from_metadata(uint field_metadata);
row_pack_length()4257   uint row_pack_length() const
4258   { return (bytes_in_rec + ((bit_len > 0) ? 1 : 0)); }
4259   bool compatible_field_size(uint metadata, Relay_log_info *rli,
4260                              uint16 mflags, int *order_var);
4261   void sql_type(String &str) const;
4262   virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
4263   virtual const uchar *unpack(uchar *to, const uchar *from,
4264                               const uchar *from_end, uint param_data);
4265   virtual int set_default();
4266 
4267   Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
4268                        uchar *new_ptr, uint32 length,
4269                        uchar *new_null_ptr, uint new_null_bit);
set_bit_ptr(uchar * bit_ptr_arg,uchar bit_ofs_arg)4270   void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg)
4271   {
4272     bit_ptr= bit_ptr_arg;
4273     bit_ofs= bit_ofs_arg;
4274   }
eq(Field * field)4275   bool eq(Field *field)
4276   {
4277     return (Field::eq(field) &&
4278             bit_ptr == ((Field_bit *)field)->bit_ptr &&
4279             bit_ofs == ((Field_bit *)field)->bit_ofs);
4280   }
4281   uint is_equal(Create_field *new_field);
move_field_offset(my_ptrdiff_t ptr_diff)4282   void move_field_offset(my_ptrdiff_t ptr_diff)
4283   {
4284     Field::move_field_offset(ptr_diff);
4285     bit_ptr= ADD_TO_PTR(bit_ptr, ptr_diff, uchar*);
4286   }
4287   void hash(ulong *nr, ulong *nr2);
4288 
4289 private:
4290   virtual size_t do_last_null_byte() const;
4291   int save_field_metadata(uchar *first_byte);
4292 };
4293 
4294 
4295 /**
4296   BIT field represented as chars for non-MyISAM tables.
4297 
4298   @todo The inheritance relationship is backwards since Field_bit is
4299   an extended version of Field_bit_as_char and not the other way
4300   around. Hence, we should refactor it to fix the hierarchy order.
4301  */
4302 class Field_bit_as_char: public Field_bit {
4303 public:
4304   Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
4305                     uchar null_bit_arg,
4306                     enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg);
key_type()4307   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
size_of()4308   uint size_of() const { return sizeof(*this); }
4309   int store(const char *to, size_t length, CHARSET_INFO *charset);
store(double nr)4310   int store(double nr) { return Field_bit::store(nr); }
store(longlong nr,bool unsigned_val)4311   int store(longlong nr, bool unsigned_val)
4312   { return Field_bit::store(nr, unsigned_val); }
4313   void sql_type(String &str) const;
4314 };
4315 
4316 
4317 class Field_row: public Field_null
4318 {
4319   class Virtual_tmp_table *m_table;
4320 public:
Field_row(uchar * ptr_arg,const LEX_CSTRING * field_name_arg)4321   Field_row(uchar *ptr_arg, const LEX_CSTRING *field_name_arg)
4322     :Field_null(ptr_arg, 0, Field::NONE, field_name_arg, &my_charset_bin),
4323      m_table(NULL)
4324     {}
4325   ~Field_row();
virtual_tmp_table_addr()4326   Virtual_tmp_table **virtual_tmp_table_addr() { return &m_table; }
4327   bool sp_prepare_and_store_item(THD *thd, Item **value);
4328 };
4329 
4330 
4331 extern const LEX_CSTRING null_clex_str;
4332 
4333 Field *make_field(TABLE_SHARE *share, MEM_ROOT *mem_root,
4334                   uchar *ptr, uint32 field_length,
4335                   uchar *null_pos, uchar null_bit,
4336                   uint pack_flag, const Type_handler *handler,
4337                   CHARSET_INFO *cs,
4338                   Field::geometry_type geom_type, uint srid,
4339                   Field::utype unireg_check,
4340                   TYPELIB *interval, const LEX_CSTRING *field_name,
4341                   uint32 flags);
4342 
4343 /*
4344   Create field class for CREATE TABLE
4345 */
4346 class Column_definition: public Sql_alloc,
4347                          public Type_handler_hybrid_field_type
4348 {
4349   /**
4350     Create "interval" from "interval_list".
4351     @param mem_root                   - memory root to create the TYPELIB
4352                                         instance and its values on
4353     @param reuse_interval_list_values - determines if TYPELIB can reuse strings
4354                                         from interval_list, or should always
4355                                         allocate a copy on mem_root, even if
4356                                         character set conversion is not needed
4357     @retval false on success
4358     @retval true  on error (bad values, or EOM)
4359   */
4360   bool create_interval_from_interval_list(MEM_ROOT *mem_root,
4361                                           bool reuse_interval_list_values);
4362 
4363   /*
4364     Calculate TYPELIB (set or enum) max and total lengths
4365 
4366     @param  cs            charset+collation pair of the interval
4367     @param  max_length    length of the longest item
4368     @param  tot_length    sum of the item lengths
4369 
4370     After this method call:
4371     - ENUM uses max_length
4372     - SET uses tot_length.
4373   */
calculate_interval_lengths(uint32 * max_length,uint32 * tot_length)4374   void calculate_interval_lengths(uint32 *max_length, uint32 *tot_length)
4375   {
4376     const char **pos;
4377     uint *len;
4378     *max_length= *tot_length= 0;
4379     for (pos= interval->type_names, len= interval->type_lengths;
4380          *pos ; pos++, len++)
4381     {
4382       size_t length= charset->cset->numchars(charset, *pos, *pos + *len);
4383       DBUG_ASSERT(length < UINT_MAX32);
4384       *tot_length+= (uint) length;
4385       set_if_bigger(*max_length, (uint32)length);
4386     }
4387   }
4388   bool prepare_stage1_check_typelib_default();
4389   bool prepare_stage1_convert_default(THD *, MEM_ROOT *, CHARSET_INFO *to);
4390   const Type_handler *field_type() const; // Prevent using this
4391   Compression_method *compression_method_ptr;
4392 public:
4393   LEX_CSTRING field_name;
4394   LEX_CSTRING comment;			// Comment for field
4395   enum enum_column_versioning
4396   {
4397     VERSIONING_NOT_SET,
4398     WITH_VERSIONING,
4399     WITHOUT_VERSIONING
4400   };
4401   Item *on_update;		        // ON UPDATE NOW()
4402   /*
4403     At various stages in execution this can be length of field in bytes or
4404     max number of characters.
4405   */
4406   ulonglong length;
4407   field_visibility_t invisible;
4408   /*
4409     The value of `length' as set by parser: is the number of characters
4410     for most of the types, or of bytes for BLOBs or numeric types.
4411   */
4412   uint32 char_length;
4413   uint  decimals, flags, pack_length, key_length;
4414   Field::utype unireg_check;
4415   TYPELIB *interval;			// Which interval to use
4416   List<String> interval_list;
4417   CHARSET_INFO *charset;
4418   uint32 srid;
4419   Field::geometry_type geom_type;
4420   engine_option_value *option_list;
4421 
4422   uint pack_flag;
4423 
4424   /*
4425     This is additinal data provided for any computed(virtual) field.
4426     In particular it includes a pointer to the item by  which this field
4427     can be computed from other fields.
4428   */
4429   Virtual_column_info
4430     *vcol_info,                      // Virtual field
4431     *default_value,                  // Default value
4432     *check_constraint;               // Check constraint
4433 
4434   enum_column_versioning versioning;
4435 
Column_definition()4436   Column_definition()
4437    :Type_handler_hybrid_field_type(&type_handler_null),
4438     compression_method_ptr(0),
4439     comment(null_clex_str),
4440     on_update(NULL), length(0), invisible(VISIBLE), char_length(0),
4441     decimals(0),
4442     flags(0), pack_length(0), key_length(0), unireg_check(Field::NONE),
4443     interval(0), charset(&my_charset_bin),
4444     srid(0), geom_type(Field::GEOM_GEOMETRY),
4445     option_list(NULL), pack_flag(0),
4446     vcol_info(0), default_value(0), check_constraint(0),
4447     versioning(VERSIONING_NOT_SET)
4448   {
4449     interval_list.empty();
4450   }
4451 
4452   Column_definition(THD *thd, Field *field, Field *orig_field);
4453   void set_attributes(const Lex_field_type_st &type, CHARSET_INFO *cs);
create_length_to_internal_length_null()4454   void create_length_to_internal_length_null()
4455   {
4456     DBUG_ASSERT(length == 0);
4457     key_length= pack_length= 0;
4458   }
create_length_to_internal_length_simple()4459   void create_length_to_internal_length_simple()
4460   {
4461     key_length= pack_length= type_handler()->calc_pack_length((uint32) length);
4462   }
create_length_to_internal_length_string()4463   void create_length_to_internal_length_string()
4464   {
4465     length*= charset->mbmaxlen;
4466     if (real_field_type() == MYSQL_TYPE_VARCHAR && compression_method())
4467       length++;
4468     set_if_smaller(length, UINT_MAX32);
4469     key_length= (uint) length;
4470     pack_length= type_handler()->calc_pack_length((uint32) length);
4471   }
create_length_to_internal_length_typelib()4472   void create_length_to_internal_length_typelib()
4473   {
4474     /* Pack_length already calculated in sql_parse.cc */
4475     length*= charset->mbmaxlen;
4476     key_length= pack_length;
4477   }
vers_sys_field()4478   bool vers_sys_field() const
4479   {
4480     return flags & (VERS_ROW_START | VERS_ROW_END);
4481   }
4482   void create_length_to_internal_length_bit();
4483   void create_length_to_internal_length_newdecimal();
4484 
4485   /**
4486     Prepare a SET/ENUM field.
4487     Create "interval" from "interval_list" if needed, and adjust "length".
4488     @param mem_root                   - Memory root to allocate TYPELIB and
4489                                         its values on
4490     @param reuse_interval_list_values - determines if TYPELIB can reuse value
4491                                         buffers from interval_list, or should
4492                                         always allocate a copy on mem_root,
4493                                         even if character set conversion
4494                                         is not needed
4495   */
4496   bool prepare_interval_field(MEM_ROOT *mem_root,
4497                               bool reuse_interval_list_values);
4498 
prepare_interval_field_calc_length()4499   void prepare_interval_field_calc_length()
4500   {
4501     uint32 field_length, dummy;
4502     if (real_field_type() == MYSQL_TYPE_SET)
4503     {
4504       calculate_interval_lengths(&dummy, &field_length);
4505       length= field_length + (interval->count - 1);
4506     }
4507     else /* MYSQL_TYPE_ENUM */
4508     {
4509       calculate_interval_lengths(&field_length, &dummy);
4510       length= field_length;
4511     }
4512     set_if_smaller(length, MAX_FIELD_WIDTH - 1);
4513   }
4514 
4515   bool prepare_blob_field(THD *thd);
4516 
4517   bool sp_prepare_create_field(THD *thd, MEM_ROOT *mem_root);
4518 
4519   bool prepare_stage1(THD *thd, MEM_ROOT *mem_root,
4520                       handler *file, ulonglong table_flags);
4521   bool prepare_stage1_typelib(THD *thd, MEM_ROOT *mem_root,
4522                               handler *file, ulonglong table_flags);
4523   bool prepare_stage1_string(THD *thd, MEM_ROOT *mem_root,
4524                              handler *file, ulonglong table_flags);
4525   bool prepare_stage1_bit(THD *thd, MEM_ROOT *mem_root,
4526                           handler *file, ulonglong table_flags);
4527 
4528   void redefine_stage1_common(const Column_definition *dup_field,
4529                               const handler *file,
4530                               const Schema_specification_st *schema);
redefine_stage1(const Column_definition * dup_field,const handler * file,const Schema_specification_st * schema)4531   bool redefine_stage1(const Column_definition *dup_field, const handler *file,
4532                        const Schema_specification_st *schema)
4533   {
4534     const Type_handler *handler= dup_field->type_handler();
4535     return handler->Column_definition_redefine_stage1(this, dup_field,
4536                                                       file, schema);
4537   }
4538   bool prepare_stage2(handler *handler, ulonglong table_flags);
4539   bool prepare_stage2_blob(handler *handler,
4540                            ulonglong table_flags, uint field_flags);
4541   bool prepare_stage2_varchar(ulonglong table_flags);
4542   bool prepare_stage2_typelib(const char *type_name, uint field_flags,
4543                               uint *dup_val_count);
4544   uint pack_flag_numeric(uint dec) const;
sign_length()4545   uint sign_length() const { return flags & UNSIGNED_FLAG ? 0 : 1; }
4546   bool check_length(uint mysql_errno, uint max_allowed_length) const;
4547   bool fix_attributes_real(uint default_length);
4548   bool fix_attributes_int(uint default_length);
4549   bool fix_attributes_decimal();
4550   bool fix_attributes_temporal_with_time(uint int_part_length);
4551   bool fix_attributes_bit();
4552 
4553   bool check(THD *thd);
4554 
stored_in_db()4555   bool stored_in_db() const { return !vcol_info || vcol_info->stored_in_db; }
4556 
field_storage_type()4557   ha_storage_media field_storage_type() const
4558   {
4559     return (ha_storage_media)
4560       ((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
4561   }
4562 
column_format()4563   column_format_type column_format() const
4564   {
4565     return (column_format_type)
4566       ((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
4567   }
4568 
has_default_function()4569   bool has_default_function() const
4570   {
4571     return unireg_check != Field::NONE;
4572   }
4573 
make_field(TABLE_SHARE * share,MEM_ROOT * mem_root,uchar * ptr,uchar * null_pos,uchar null_bit,const LEX_CSTRING * field_name_arg)4574   Field *make_field(TABLE_SHARE *share, MEM_ROOT *mem_root,
4575                     uchar *ptr, uchar *null_pos, uchar null_bit,
4576                     const LEX_CSTRING *field_name_arg) const
4577   {
4578     return ::make_field(share, mem_root, ptr,
4579                         (uint32)length, null_pos, null_bit,
4580                         pack_flag, type_handler(), charset,
4581                         geom_type, srid, unireg_check, interval,
4582                         field_name_arg, flags);
4583   }
make_field(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * field_name_arg)4584   Field *make_field(TABLE_SHARE *share, MEM_ROOT *mem_root,
4585                     const LEX_CSTRING *field_name_arg) const
4586   {
4587     return make_field(share, mem_root, (uchar *) 0, (uchar *) "", 0,
4588                       field_name_arg);
4589   }
4590   /* Return true if default is an expression that must be saved explicitely */
4591   bool has_default_expression();
4592 
has_default_now_unireg_check()4593   bool has_default_now_unireg_check() const
4594   {
4595     return unireg_check == Field::TIMESTAMP_DN_FIELD
4596         || unireg_check == Field::TIMESTAMP_DNUN_FIELD;
4597   }
4598 
set_type(const Column_definition & other)4599   void set_type(const Column_definition &other)
4600   {
4601     set_handler(other.type_handler());
4602     length= other.length;
4603     char_length= other.char_length;
4604     decimals= other.decimals;
4605     flags= other.flags;
4606     pack_length= other.pack_length;
4607     key_length= other.key_length;
4608     unireg_check= other.unireg_check;
4609     interval= other.interval;
4610     charset= other.charset;
4611     srid= other.srid;
4612     geom_type= other.geom_type;
4613     pack_flag= other.pack_flag;
4614   }
4615 
4616   // Replace the entire value by another definition
set_column_definition(const Column_definition * def)4617   void set_column_definition(const Column_definition *def)
4618   {
4619     *this= *def;
4620   }
4621   bool set_compressed(const char *method);
4622   bool set_compressed_deprecated(THD *thd, const char *method);
4623   bool set_compressed_deprecated_column_attribute(THD *thd,
4624                                                   const char *pos,
4625                                                   const char *method);
set_compression_method(Compression_method * compression_method_arg)4626   void set_compression_method(Compression_method *compression_method_arg)
4627   { compression_method_ptr= compression_method_arg; }
compression_method()4628   Compression_method *compression_method() const
4629   { return compression_method_ptr; }
4630 };
4631 
4632 
4633 /**
4634   List of ROW element definitions, e.g.:
4635     DECLARE a ROW(a INT,b VARCHAR(10))
4636 */
4637 class Row_definition_list: public List<class Spvar_definition>
4638 {
4639 public:
4640   inline bool eq_name(const Spvar_definition *def, const LEX_CSTRING *name) const;
4641   /**
4642     Find a ROW field by name.
4643     @param [IN]  name   - the name
4644     @param [OUT] offset - if the ROW field found, its offset it returned here
4645     @retval NULL        - the ROW field was not found
4646     @retval !NULL       - the pointer to the found ROW field
4647   */
find_row_field_by_name(const LEX_CSTRING * name,uint * offset)4648   Spvar_definition *find_row_field_by_name(const LEX_CSTRING *name, uint *offset) const
4649   {
4650     // Cast-off the "const" qualifier
4651     List_iterator<Spvar_definition> it(*((List<Spvar_definition>*)this));
4652     Spvar_definition *def;
4653     for (*offset= 0; (def= it++); (*offset)++)
4654     {
4655       if (eq_name(def, name))
4656         return def;
4657     }
4658     return 0;
4659   }
4660   bool adjust_formal_params_to_actual_params(THD *thd, List<Item> *args);
4661   bool adjust_formal_params_to_actual_params(THD *thd,
4662                                              Item **args, uint arg_count);
4663   bool resolve_type_refs(THD *);
4664 };
4665 
4666 /**
4667   This class is used during a stored routine or a trigger execution,
4668   at sp_rcontext::create() time.
4669   Currently it can represent:
4670   - variables with explicit data types:   DECLARE a INT;
4671   - variables with data type references:  DECLARE a t1.a%TYPE;
4672   - ROW type variables
4673 
4674   Notes:
4675   - Scalar variables have m_field_definitions==NULL.
4676   - ROW variables are defined as having MYSQL_TYPE_NULL,
4677     with a non-empty m_field_definitions.
4678 
4679   Data type references to other object types will be added soon, e.g.:
4680   - DECLARE a table_name%ROWTYPE;
4681   - DECLARE a cursor_name%ROWTYPE;
4682   - DECLARE a record_name%TYPE;
4683   - DECLARE a variable_name%TYPE;
4684 */
4685 class Spvar_definition: public Column_definition
4686 {
4687   Qualified_column_ident *m_column_type_ref; // for %TYPE
4688   Table_ident *m_table_rowtype_ref;          // for table%ROWTYPE
4689   bool m_cursor_rowtype_ref;                       // for cursor%ROWTYPE
4690   uint m_cursor_rowtype_offset;                    // for cursor%ROWTYPE
4691   Row_definition_list *m_row_field_definitions;    // for ROW
4692 public:
Spvar_definition()4693   Spvar_definition()
4694    :m_column_type_ref(NULL),
4695     m_table_rowtype_ref(NULL),
4696     m_cursor_rowtype_ref(false),
4697     m_cursor_rowtype_offset(0),
4698     m_row_field_definitions(NULL)
4699   { }
Spvar_definition(THD * thd,Field * field)4700   Spvar_definition(THD *thd, Field *field)
4701    :Column_definition(thd, field, NULL),
4702     m_column_type_ref(NULL),
4703     m_table_rowtype_ref(NULL),
4704     m_cursor_rowtype_ref(false),
4705     m_cursor_rowtype_offset(0),
4706     m_row_field_definitions(NULL)
4707   { }
type_handler()4708   const Type_handler *type_handler() const
4709   {
4710     return Type_handler_hybrid_field_type::type_handler();
4711   }
is_column_type_ref()4712   bool is_column_type_ref() const { return m_column_type_ref != 0; }
is_table_rowtype_ref()4713   bool is_table_rowtype_ref() const { return m_table_rowtype_ref != 0; }
is_cursor_rowtype_ref()4714   bool is_cursor_rowtype_ref() const { return m_cursor_rowtype_ref; }
is_explicit_data_type()4715   bool is_explicit_data_type() const
4716   {
4717     return !is_column_type_ref() &&
4718            !is_table_rowtype_ref() &&
4719            !is_cursor_rowtype_ref();
4720   }
column_type_ref()4721   Qualified_column_ident *column_type_ref() const
4722   {
4723     return m_column_type_ref;
4724   }
set_column_type_ref(Qualified_column_ident * ref)4725   void set_column_type_ref(Qualified_column_ident *ref)
4726   {
4727     m_column_type_ref= ref;
4728   }
4729 
table_rowtype_ref()4730   Table_ident *table_rowtype_ref() const
4731   {
4732     return m_table_rowtype_ref;
4733   }
set_table_rowtype_ref(Table_ident * ref)4734   void set_table_rowtype_ref(Table_ident *ref)
4735   {
4736     DBUG_ASSERT(ref);
4737     set_handler(&type_handler_row);
4738     m_table_rowtype_ref= ref;
4739   }
4740 
cursor_rowtype_offset()4741   uint cursor_rowtype_offset() const
4742   {
4743     return m_cursor_rowtype_offset;
4744   }
set_cursor_rowtype_ref(uint offset)4745   void set_cursor_rowtype_ref(uint offset)
4746   {
4747     set_handler(&type_handler_row);
4748     m_cursor_rowtype_ref= true;
4749     m_cursor_rowtype_offset= offset;
4750   }
4751 
4752   /*
4753     Find a ROW field by name.
4754     See Row_field_list::find_row_field_by_name() for details.
4755   */
find_row_field_by_name(const LEX_CSTRING * name,uint * offset)4756   Spvar_definition *find_row_field_by_name(const LEX_CSTRING *name, uint *offset) const
4757   {
4758     DBUG_ASSERT(m_row_field_definitions);
4759     return m_row_field_definitions->find_row_field_by_name(name, offset);
4760   }
is_row()4761   uint is_row() const
4762   {
4763     return m_row_field_definitions != NULL;
4764   }
4765   // Check if "this" defines a ROW variable with n elements
is_row(uint n)4766   uint is_row(uint n) const
4767   {
4768     return m_row_field_definitions != NULL &&
4769            m_row_field_definitions->elements == n;
4770   }
row_field_definitions()4771   Row_definition_list *row_field_definitions() const
4772   {
4773     return m_row_field_definitions;
4774   }
set_row_field_definitions(Row_definition_list * list)4775   void set_row_field_definitions(Row_definition_list *list)
4776   {
4777     DBUG_ASSERT(list);
4778     set_handler(&type_handler_row);
4779     m_row_field_definitions= list;
4780   }
4781 
4782 };
4783 
4784 
eq_name(const Spvar_definition * def,const LEX_CSTRING * name)4785 inline bool Row_definition_list::eq_name(const Spvar_definition *def,
4786                                          const LEX_CSTRING *name) const
4787 {
4788   return def->field_name.length == name->length && my_strcasecmp(system_charset_info, def->field_name.str, name->str) == 0;
4789 }
4790 
4791 
4792 class Create_field :public Column_definition
4793 {
4794 public:
4795   LEX_CSTRING change;			// If done with alter table
4796   LEX_CSTRING after;			// Put column after this one
4797   Field *field;				// For alter table
4798   TYPELIB *save_interval;               // Temporary copy for the above
4799                                         // Used only for UCS2 intervals
4800 
4801   /** structure with parsed options (for comparing fields in ALTER TABLE) */
4802   ha_field_option_struct *option_struct;
4803   uint	offset;
4804   uint8 interval_id;                    // For rea_create_table
4805   bool create_if_not_exists;            // Used in ALTER TABLE IF NOT EXISTS
4806 
Create_field()4807   Create_field():
4808     Column_definition(),
4809     field(0), option_struct(NULL),
4810     create_if_not_exists(false)
4811   {
4812     change= after= null_clex_str;
4813   }
Create_field(THD * thd,Field * old_field,Field * orig_field)4814   Create_field(THD *thd, Field *old_field, Field *orig_field):
4815     Column_definition(thd, old_field, orig_field),
4816     change(old_field->field_name),
4817     field(old_field), option_struct(old_field->option_struct),
4818     create_if_not_exists(false)
4819   {
4820     after= null_clex_str;
4821   }
4822   /* Used to make a clone of this object for ALTER/CREATE TABLE */
4823   Create_field *clone(MEM_ROOT *mem_root) const;
4824 
is_some_bigint()4825   bool is_some_bigint() const
4826   {
4827     return type_handler() == &type_handler_longlong ||
4828            type_handler() == &type_handler_vers_trx_id;
4829   }
4830 
4831   bool vers_check_timestamp(const Lex_table_name &table_name) const;
4832   bool vers_check_bigint(const Lex_table_name &table_name) const;
4833 };
4834 
4835 
4836 /*
4837   A class for sending info to the client
4838 */
4839 
4840 class Send_field :public Sql_alloc {
4841  public:
4842   const char *db_name;
4843   const char *table_name,*org_table_name;
4844   LEX_CSTRING col_name, org_col_name;
4845   ulong length;
4846   uint flags, decimals;
4847   enum_field_types type;
Send_field()4848   Send_field() {}
4849 };
4850 
4851 
4852 /*
4853   A class for quick copying data to fields
4854 */
4855 
4856 class Copy_field :public Sql_alloc {
4857 public:
4858   uchar *from_ptr,*to_ptr;
4859   uchar *from_null_ptr,*to_null_ptr;
4860   bool *null_row;
4861   uint	from_bit,to_bit;
4862   /**
4863     Number of bytes in the fields pointed to by 'from_ptr' and
4864     'to_ptr'. Usually this is the number of bytes that are copied from
4865     'from_ptr' to 'to_ptr'.
4866 
4867     For variable-length fields (VARCHAR), the first byte(s) describe
4868     the actual length of the text. For VARCHARs with length
4869        < 256 there is 1 length byte
4870        >= 256 there is 2 length bytes
4871     Thus, if from_field is VARCHAR(10), from_length (and in most cases
4872     to_length) is 11. For VARCHAR(1024), the length is 1026. @see
4873     Field_varstring::length_bytes
4874 
4875     Note that for VARCHARs, do_copy() will be do_varstring*() which
4876     only copies the length-bytes (1 or 2) + the actual length of the
4877     text instead of from/to_length bytes.
4878   */
4879   uint from_length,to_length;
4880   Field *from_field,*to_field;
4881   String tmp;					// For items
4882 
Copy_field()4883   Copy_field() {}
~Copy_field()4884   ~Copy_field() {}
4885   void set(Field *to,Field *from,bool save);	// Field to field
4886   void set(uchar *to,Field *from);		// Field to string
4887   void (*do_copy)(Copy_field *);
4888   void (*do_copy2)(Copy_field *);		// Used to handle null values
4889 };
4890 
4891 
4892 uint pack_length_to_packflag(uint type);
4893 enum_field_types get_blob_type_from_length(ulong length);
4894 int set_field_to_null(Field *field);
4895 int set_field_to_null_with_conversions(Field *field, bool no_conversions);
4896 int convert_null_to_field_value_or_error(Field *field);
4897 bool check_expression(Virtual_column_info *vcol, LEX_CSTRING *name,
4898                       enum_vcol_info_type type);
4899 
4900 /*
4901   The following are for the interface with the .frm file
4902 */
4903 
4904 #define FIELDFLAG_DECIMAL		1U
4905 #define FIELDFLAG_BINARY		1U	// Shares same flag
4906 #define FIELDFLAG_NUMBER		2U
4907 #define FIELDFLAG_ZEROFILL		4U
4908 #define FIELDFLAG_PACK			120U	// Bits used for packing
4909 #define FIELDFLAG_INTERVAL		256U    // mangled with decimals!
4910 #define FIELDFLAG_BITFIELD		512U	// mangled with decimals!
4911 #define FIELDFLAG_BLOB			1024U	// mangled with decimals!
4912 #define FIELDFLAG_GEOM			2048U   // mangled with decimals!
4913 
4914 #define FIELDFLAG_TREAT_BIT_AS_CHAR     4096U   /* use Field_bit_as_char */
4915 #define FIELDFLAG_LONG_DECIMAL          8192U
4916 #define FIELDFLAG_NO_DEFAULT		16384U  /* sql */
4917 #define FIELDFLAG_MAYBE_NULL		32768U	// sql
4918 #define FIELDFLAG_HEX_ESCAPE		0x10000U
4919 #define FIELDFLAG_PACK_SHIFT		3
4920 #define FIELDFLAG_DEC_SHIFT		8
4921 #define FIELDFLAG_MAX_DEC               63U
4922 
4923 #define MTYP_TYPENR(type) (type & 127U)	/* Remove bits from type */
4924 
4925 #define f_is_dec(x)		((x) & FIELDFLAG_DECIMAL)
4926 #define f_is_num(x)		((x) & FIELDFLAG_NUMBER)
4927 #define f_is_zerofill(x)	((x) & FIELDFLAG_ZEROFILL)
4928 #define f_is_packed(x)		((x) & FIELDFLAG_PACK)
4929 #define f_packtype(x)		(((x) >> FIELDFLAG_PACK_SHIFT) & 15)
4930 #define f_decimals(x)		((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
4931 #define f_is_alpha(x)		(!f_is_num(x))
4932 #define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
4933 #define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
4934 #define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
4935 #define f_is_blob(x)		(((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
4936 #define f_is_geom(x)		(((x) & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM)
4937 #define f_settype(x)		(((uint) (x)) << FIELDFLAG_PACK_SHIFT)
4938 #define f_maybe_null(x)		((x) & FIELDFLAG_MAYBE_NULL)
4939 #define f_no_default(x)		((x) & FIELDFLAG_NO_DEFAULT)
4940 #define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
4941 #define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)
4942 #define f_visibility(x)         (static_cast<field_visibility_t> ((x) & INVISIBLE_MAX_BITS))
4943 
4944 inline
vers_end_id()4945 ulonglong TABLE::vers_end_id() const
4946 {
4947   DBUG_ASSERT(versioned(VERS_TRX_ID));
4948   return static_cast<ulonglong>(vers_end_field()->val_int());
4949 }
4950 
4951 inline
vers_start_id()4952 ulonglong TABLE::vers_start_id() const
4953 {
4954   DBUG_ASSERT(versioned(VERS_TRX_ID));
4955   return static_cast<ulonglong>(vers_start_field()->val_int());
4956 }
4957 
4958 
4959 #endif /* FIELD_INCLUDED */
4960