My Project
field.h
00001 #ifndef FIELD_INCLUDED
00002 #define FIELD_INCLUDED
00003 
00004 /* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; version 2 of the License.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
00018 
00019 #include "mysqld.h"                             /* system_charset_info */
00020 #include "table.h"                              /* TABLE */
00021 #include "sql_string.h"                         /* String */
00022 #include "my_decimal.h"                         /* my_decimal */
00023 #include "sql_error.h"                          /* Sql_condition */
00024 #include "mysql_version.h"                      /* FRM_VER */
00025 
00026 /*
00027 
00028 Field class hierarchy
00029 
00030 
00031 Field (abstract)
00032 |
00033 +--Field_bit
00034 |  +--Field_bit_as_char
00035 |  
00036 +--Field_num (abstract)
00037 |  |  +--Field_real (asbstract)
00038 |  |     +--Field_decimal
00039 |  |     +--Field_float
00040 |  |     +--Field_double
00041 |  |
00042 |  +--Field_new_decimal
00043 |  +--Field_short
00044 |  +--Field_medium
00045 |  +--Field_long
00046 |  +--Field_longlong
00047 |  +--Field_tiny
00048 |     +--Field_year
00049 |
00050 +--Field_str (abstract)
00051 |  +--Field_longstr
00052 |  |  +--Field_string
00053 |  |  +--Field_varstring
00054 |  |  +--Field_blob
00055 |  |     +--Field_geom
00056 |  |
00057 |  +--Field_null
00058 |  +--Field_enum
00059 |     +--Field_set
00060 |
00061 +--Field_temporal (abstract)
00062    +--Field_time_common (abstract)
00063    |  +--Field_time
00064    |  +--Field_timef
00065    |
00066    +--Field_temporal_with_date (abstract)
00067       +--Field_newdate
00068       +--Field_temporal_with_date_and_time (abstract)
00069          +--Field_timestamp
00070          +--Field_datetime
00071          +--Field_temporal_with_date_and_timef (abstract)
00072             +--Field_timestampf
00073             +--Field_datetimef
00074 */
00075 
00076 
00077 class Send_field;
00078 class Protocol;
00079 class Create_field;
00080 class Relay_log_info;
00081 class Field;
00082 
00083 enum enum_check_fields
00084 {
00085   CHECK_FIELD_IGNORE,
00086   CHECK_FIELD_WARN,
00087   CHECK_FIELD_ERROR_FOR_NULL
00088 };
00089 
00090 
00091 enum Derivation
00092 {
00093   DERIVATION_IGNORABLE= 6,
00094   DERIVATION_NUMERIC= 5,
00095   DERIVATION_COERCIBLE= 4,
00096   DERIVATION_SYSCONST= 3,
00097   DERIVATION_IMPLICIT= 2,
00098   DERIVATION_NONE= 1,
00099   DERIVATION_EXPLICIT= 0
00100 };
00101 
00108 enum type_conversion_status
00109 {
00111   TYPE_OK= 0,
00116   TYPE_NOTE_TIME_TRUNCATED,
00121   TYPE_WARN_OUT_OF_RANGE,
00131   TYPE_NOTE_TRUNCATED,
00140   TYPE_WARN_TRUNCATED,
00142   TYPE_ERR_NULL_CONSTRAINT_VIOLATION,
00147   TYPE_ERR_BAD_VALUE,
00149   TYPE_ERR_OOM
00150 };
00151 
00152 
00153 #define STORAGE_TYPE_MASK 7
00154 #define COLUMN_FORMAT_MASK 7
00155 #define COLUMN_FORMAT_SHIFT 3
00156 
00157 #define my_charset_numeric      my_charset_latin1
00158 #define MY_REPERTOIRE_NUMERIC   MY_REPERTOIRE_ASCII
00159 
00160 struct st_cache_field;
00161 type_conversion_status field_conv(Field *to,Field *from);
00162 
00163 inline uint get_enum_pack_length(int elements)
00164 {
00165   return elements < 256 ? 1 : 2;
00166 }
00167 
00168 inline uint get_set_pack_length(int elements)
00169 {
00170   uint len= (elements + 7) / 8;
00171   return len > 4 ? 8 : len;
00172 }
00173 
00174 inline type_conversion_status
00175 decimal_err_to_type_conv_status(int dec_error)
00176 {
00177   if (dec_error & E_DEC_OOM)
00178     return TYPE_ERR_OOM;
00179 
00180   if (dec_error & (E_DEC_DIV_ZERO | E_DEC_BAD_NUM))
00181     return TYPE_ERR_BAD_VALUE;
00182 
00183   if (dec_error & E_DEC_TRUNCATED)
00184     return TYPE_NOTE_TRUNCATED;
00185 
00186   if (dec_error & E_DEC_OVERFLOW)
00187     return TYPE_WARN_OUT_OF_RANGE;
00188 
00189   if (dec_error == E_DEC_OK)
00190     return TYPE_OK;
00191 
00192   // impossible
00193   DBUG_ASSERT(false);
00194   return TYPE_ERR_BAD_VALUE;
00195 }
00196 
00201 inline type_conversion_status
00202 time_warning_to_type_conversion_status(const int warn)
00203 {
00204   if (warn & MYSQL_TIME_NOTE_TRUNCATED)
00205     return TYPE_NOTE_TIME_TRUNCATED;
00206 
00207   if (warn & MYSQL_TIME_WARN_OUT_OF_RANGE)
00208     return TYPE_WARN_OUT_OF_RANGE;
00209 
00210   if (warn & MYSQL_TIME_WARN_TRUNCATED)
00211     return TYPE_NOTE_TRUNCATED;
00212 
00213   if (warn & (MYSQL_TIME_WARN_ZERO_DATE | MYSQL_TIME_WARN_ZERO_IN_DATE))
00214     return TYPE_ERR_BAD_VALUE;
00215 
00216   if (warn & MYSQL_TIME_WARN_INVALID_TIMESTAMP)
00217     // date was fine but pointed to daylight saving time switch gap
00218     return TYPE_OK;
00219 
00220   DBUG_ASSERT(!warn);
00221   return TYPE_OK;
00222 }
00223 
00224 #define ASSERT_COLUMN_MARKED_FOR_READ \
00225 DBUG_ASSERT(!table || (!table->read_set || \
00226                        bitmap_is_set(table->read_set, field_index)))
00227 #define ASSERT_COLUMN_MARKED_FOR_WRITE \
00228 DBUG_ASSERT(!table || (!table->write_set || \
00229                        bitmap_is_set(table->write_set, field_index)))
00230 
00231 
00240 inline bool is_temporal_type(enum_field_types type)
00241 {
00242   switch (type)
00243   {
00244   case MYSQL_TYPE_TIME:
00245   case MYSQL_TYPE_DATETIME:
00246   case MYSQL_TYPE_TIMESTAMP:
00247   case MYSQL_TYPE_DATE:
00248   case MYSQL_TYPE_NEWDATE:
00249     return true;
00250   default:
00251     return false;
00252   }
00253 }
00254 
00255 
00265 inline bool is_temporal_real_type(enum_field_types type)
00266 {
00267   switch (type)
00268   {
00269   case MYSQL_TYPE_TIME2:
00270   case MYSQL_TYPE_TIMESTAMP2:
00271   case MYSQL_TYPE_DATETIME2:
00272     return true;
00273   default:
00274     return is_temporal_type(type);
00275   }
00276 }
00277 
00278 
00287 inline bool is_temporal_type_with_time(enum_field_types type)
00288 {
00289   switch (type)
00290   {
00291   case MYSQL_TYPE_TIME:
00292   case MYSQL_TYPE_DATETIME:
00293   case MYSQL_TYPE_TIMESTAMP:
00294     return true;
00295   default:
00296     return false;
00297   }
00298 }
00299 
00300 
00309 inline bool is_temporal_type_with_date(enum_field_types type)
00310 {
00311   switch (type)
00312   {
00313   case MYSQL_TYPE_DATE:
00314   case MYSQL_TYPE_DATETIME:
00315   case MYSQL_TYPE_TIMESTAMP:
00316     return true;
00317   default:
00318     return false;
00319   }
00320 }
00321 
00322 
00331 inline bool is_temporal_type_with_date_and_time(enum_field_types type)
00332 {
00333   switch (type)
00334   {
00335   case MYSQL_TYPE_DATETIME:
00336   case MYSQL_TYPE_TIMESTAMP:
00337     return true;
00338   default:
00339     return false;
00340   }
00341 }
00342 
00343 
00352 inline bool real_type_with_now_as_default(enum_field_types type)
00353 {
00354   return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
00355     type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
00356 }
00357 
00358 
00367 inline bool real_type_with_now_on_update(enum_field_types type)
00368 {
00369   return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
00370     type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
00371 }
00372 
00373 
00378 inline bool is_timestamp_type(enum_field_types type)
00379 {
00380   return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2;
00381 }
00382 
00383 
00391 inline enum_field_types real_type_to_type(enum_field_types real_type)
00392 {
00393   switch (real_type)
00394   {
00395   case MYSQL_TYPE_TIME2:
00396     return MYSQL_TYPE_TIME;
00397   case MYSQL_TYPE_DATETIME2:
00398     return MYSQL_TYPE_DATETIME;
00399   case MYSQL_TYPE_TIMESTAMP2:
00400     return MYSQL_TYPE_TIMESTAMP;
00401   case MYSQL_TYPE_NEWDATE:
00402     return MYSQL_TYPE_DATE;
00403   /* Note: NEWDECIMAL is a type, not only a real_type */
00404   default: return real_type;
00405   }
00406 }
00407 
00408 
00427 template<bool Is_big_endian>
00428 void copy_integer(uchar *to, int to_length,
00429                   const uchar* from, int from_length,
00430                   bool is_unsigned)
00431 {
00432   if (Is_big_endian)
00433   {
00434     if (is_unsigned)
00435       to[0]= from[0];
00436     else
00437       to[0]= (char)(from[0] ^ 128); // Reverse the sign bit.
00438     memcpy(to + 1, from + 1, to_length - 1);
00439   }
00440   else
00441   {
00442     const int sign_byte= from[from_length - 1];
00443     if (is_unsigned)
00444       to[0]= sign_byte;
00445     else
00446       to[0]= static_cast<char>(sign_byte ^ 128); // Reverse the sign bit.
00447     for (int i= 1, j= from_length - 2; i < to_length; ++i, --j)
00448       to[i]= from[j];
00449   }
00450 }
00451 
00452 
00453 class Field
00454 {
00455   Field(const Item &);                          /* Prevent use of these */
00456   void operator=(Field &);
00457 public:
00458 
00459   bool has_insert_default_function() const
00460   {
00461     return unireg_check == TIMESTAMP_DN_FIELD ||
00462       unireg_check == TIMESTAMP_DNUN_FIELD;
00463   }
00464 
00465   bool has_update_default_function() const
00466   {
00467     return unireg_check == TIMESTAMP_UN_FIELD ||
00468       unireg_check == TIMESTAMP_DNUN_FIELD;
00469   }
00470 
00471   /* To do: inherit Sql_alloc and get these for free */
00472   static void *operator new(size_t size) throw ()
00473   { return sql_alloc(size); }
00474   static void *operator new(size_t size, MEM_ROOT *mem_root) throw () {
00475     return alloc_root(mem_root, size);
00476   }
00477   static void operator delete(void *ptr, MEM_ROOT *mem_root)
00478   { DBUG_ASSERT(false); /* never called */ }
00479 
00480   static void operator delete(void *ptr_arg, size_t size) throw()
00481   { TRASH(ptr_arg, size); }
00482 
00483   uchar         *ptr;                   // Position to field in record
00484 
00485 protected:
00490   uchar         *null_ptr;
00491 
00492 public:
00493   /*
00494     Note that you can use table->in_use as replacement for current_thd member 
00495     only inside of val_*() and store() members (e.g. you can't use it in cons)
00496   */
00497   TABLE *table;                                 // Pointer for table
00498   TABLE *orig_table;                            // Pointer to original table
00499   const char    **table_name, *field_name;
00500   LEX_STRING    comment;
00501   /* Field is part of the following keys */
00502   key_map key_start;                /* Keys that starts with this field */
00503   key_map part_of_key;              /* All keys that includes this field */
00504   key_map part_of_key_not_clustered;/* ^ but only for non-clustered keys */
00505   key_map part_of_sortkey;          /* ^ but only keys usable for sorting */
00506   /* 
00507     We use three additional unireg types for TIMESTAMP to overcome limitation 
00508     of current binary format of .frm file. We'd like to be able to support 
00509     NOW() as default and on update value for such fields but unable to hold 
00510     this info anywhere except unireg_check field. This issue will be resolved
00511     in more clean way with transition to new text based .frm format.
00512     See also comment for Field_timestamp::Field_timestamp().
00513   */
00514   enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
00515                 CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
00516                 BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
00517                 TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
00518   enum geometry_type
00519   {
00520     GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
00521     GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
00522     GEOM_GEOMETRYCOLLECTION = 7
00523   };
00524   enum imagetype { itRAW, itMBR};
00525 
00526   utype         unireg_check;
00527   uint32        field_length;           // Length of field
00528   uint32        flags;
00529   uint16        field_index;            // field number in fields array
00530   uchar         null_bit;               // Bit used to test null bit
00540   bool is_created_from_null_item;
00541 
00542   Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
00543         uchar null_bit_arg, utype unireg_check_arg,
00544         const char *field_name_arg);
00545   virtual ~Field() {}
00546 
00547   /* Store functions returns 1 on overflow and -1 on fatal error */
00548   virtual type_conversion_status store(const char *to, uint length,
00549                                        const CHARSET_INFO *cs)=0;
00550   virtual type_conversion_status store(double nr)=0;
00551   virtual type_conversion_status store(longlong nr, bool unsigned_val)=0;
00563   virtual type_conversion_status store_packed(longlong nr)
00564   {
00565     return store(nr, 0);
00566   }
00567   virtual type_conversion_status store_decimal(const my_decimal *d)=0;
00583   virtual type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
00592   type_conversion_status store_time(MYSQL_TIME *ltime)
00593   {
00594     return store_time(ltime, 0);
00595   }
00596   type_conversion_status store(const char *to, uint length,
00597                                const CHARSET_INFO *cs,
00598                                enum_check_fields check_level);
00599   virtual double val_real(void)=0;
00600   virtual longlong val_int(void)=0;
00606   virtual longlong val_time_temporal()
00607   {
00608     DBUG_ASSERT(0);
00609     return 0;
00610   }
00616   virtual longlong val_date_temporal()
00617   {
00618     DBUG_ASSERT(0);
00619     return 0;
00620   }
00625   longlong val_temporal_by_field_type()
00626   {
00627     // Return longlong TIME or DATETIME representation, depending on field type
00628     if (type() == MYSQL_TYPE_TIME)
00629       return val_time_temporal();
00630     DBUG_ASSERT(is_temporal_with_date());
00631     return val_date_temporal();
00632   }
00633   virtual my_decimal *val_decimal(my_decimal *)= 0;
00634   inline String *val_str(String *str) { return val_str(str, str); }
00635   /*
00636      val_str(buf1, buf2) gets two buffers and should use them as follows:
00637      if it needs a temp buffer to convert result to string - use buf1
00638        example Field_tiny::val_str()
00639      if the value exists as a string already - use buf2
00640        example Field_string::val_str()
00641      consequently, buf2 may be created as 'String buf;' - no memory
00642      will be allocated for it. buf1 will be allocated to hold a
00643      value if it's too small. Using allocated buffer for buf2 may result in
00644      an unnecessary free (and later, may be an alloc).
00645      This trickery is used to decrease a number of malloc calls.
00646   */
00647   virtual String *val_str(String*,String *)=0;
00648   String *val_int_as_str(String *val_buffer, my_bool unsigned_flag);
00649   /*
00650    str_needs_quotes() returns TRUE if the value returned by val_str() needs
00651    to be quoted when used in constructing an SQL query.
00652   */
00653   virtual bool str_needs_quotes() { return FALSE; }
00654   virtual Item_result result_type () const=0;
00665   virtual Item_result numeric_context_result_type() const
00666   {
00667     return result_type();
00668   }
00669   virtual Item_result cmp_type () const { return result_type(); }
00670   virtual Item_result cast_to_int_type () const { return result_type(); }
00671   static bool type_can_have_key_part(enum_field_types);
00672   static enum_field_types field_type_merge(enum_field_types, enum_field_types);
00673   static Item_result result_merge_type(enum_field_types);
00674   virtual bool eq(Field *field)
00675   {
00676     return (ptr == field->ptr && null_ptr == field->null_ptr &&
00677             null_bit == field->null_bit && field->type() == type());
00678   }
00679   virtual bool eq_def(Field *field);
00680   
00681   /*
00682     pack_length() returns size (in bytes) used to store field data in memory
00683     (i.e. it returns the maximum size of the field in a row of the table,
00684     which is located in RAM).
00685   */
00686   virtual uint32 pack_length() const { return (uint32) field_length; }
00687 
00688   /*
00689     pack_length_in_rec() returns size (in bytes) used to store field data on
00690     storage (i.e. it returns the maximal size of the field in a row of the
00691     table, which is located on disk).
00692   */
00693   virtual uint32 pack_length_in_rec() const { return pack_length(); }
00694   virtual bool compatible_field_size(uint metadata, Relay_log_info *rli,
00695                                      uint16 mflags, int *order);
00696   virtual uint pack_length_from_metadata(uint field_metadata)
00697   {
00698     DBUG_ENTER("Field::pack_length_from_metadata");
00699     DBUG_RETURN(field_metadata);
00700   }
00701   virtual uint row_pack_length() const { return 0; }
00702   virtual int save_field_metadata(uchar *first_byte)
00703   { return do_save_field_metadata(first_byte); }
00704 
00705   /*
00706     data_length() return the "real size" of the data in memory.
00707   */
00708   virtual uint32 data_length() { return pack_length(); }
00709   virtual uint32 sort_length() const { return pack_length(); }
00710 
00717   virtual uint32 max_data_length() const {
00718     return pack_length();
00719   };
00720 
00721   virtual type_conversion_status reset(void)
00722   {
00723     memset(ptr, 0, pack_length());
00724     return TYPE_OK;
00725   }
00726   virtual void reset_fields() {}
00732   virtual bool get_timestamp(struct timeval *tm, int *warnings);
00764   virtual void store_timestamp(const timeval *tm) { DBUG_ASSERT(false); }
00765 
00772   void store_timestamp(my_time_t sec)
00773   {
00774     struct timeval tm;
00775     tm.tv_sec= sec;
00776     tm.tv_usec= 0;
00777     store_timestamp(&tm);
00778   }
00779   virtual void set_default()
00780   {
00781     if (has_insert_default_function())
00782     {
00783       evaluate_insert_default_function();
00784       return;
00785     }
00786 
00787     my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
00788                                           table->record[0]);
00789     memcpy(ptr, ptr + l_offset, pack_length());
00790     if (real_maybe_null())
00791       *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
00792                   (null_ptr[l_offset] & null_bit));
00793   }
00794 
00795 
00801   void evaluate_insert_default_function();
00802 
00803 
00810   void evaluate_update_default_function();
00811   virtual bool binary() const { return 1; }
00812   virtual bool zero_pack() const { return 1; }
00813   virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
00814   virtual uint32 key_length() const { return pack_length(); }
00815   virtual enum_field_types type() const =0;
00816   virtual enum_field_types real_type() const { return type(); }
00817   virtual enum_field_types binlog_type() const
00818   {
00819     /*
00820       Binlog stores field->type() as type code by default.
00821       This puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
00822       with extra data type details put into metadata.
00823 
00824       We cannot store field->type() in case of temporal types with
00825       fractional seconds: TIME(n), DATETIME(n) and TIMESTAMP(n),
00826       because binlog records with MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
00827       type codes do not have metadata.
00828       So for temporal data types with fractional seconds we'll store
00829       real_type() type codes instead, i.e.
00830       MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2,
00831       and put precision into metatada.
00832 
00833       Note: perhaps binlog should eventually be modified to store
00834       real_type() instead of type() for all column types.
00835     */
00836     return type();
00837   }
00838   inline  int cmp(const uchar *str) { return cmp(ptr,str); }
00839   virtual int cmp_max(const uchar *a, const uchar *b, uint max_len)
00840     { return cmp(a, b); }
00841   virtual int cmp(const uchar *,const uchar *)=0;
00842   virtual int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L)
00843   { return memcmp(a,b,pack_length()); }
00844   virtual int cmp_offset(uint row_offset)
00845   { return cmp(ptr,ptr+row_offset); }
00846   virtual int cmp_binary_offset(uint row_offset)
00847   { return cmp_binary(ptr, ptr+row_offset); };
00848   virtual int key_cmp(const uchar *a,const uchar *b)
00849   { return cmp(a, b); }
00850   virtual int key_cmp(const uchar *str, uint length)
00851   { return cmp(ptr,str); }
00852   virtual uint decimals() const { return 0; }
00853   /*
00854     Caller beware: sql_type can change str.Ptr, so check
00855     ptr() to see if it changed if you are using your own buffer
00856     in str and restore it with set() if needed
00857   */
00858   virtual void sql_type(String &str) const =0;
00859 
00860   bool is_temporal() const
00861   { return is_temporal_type(type()); }
00862 
00863   bool is_temporal_with_date() const
00864   { return is_temporal_type_with_date(type()); }
00865 
00866   bool is_temporal_with_time() const
00867   { return is_temporal_type_with_time(type()); }
00868 
00869   bool is_temporal_with_date_and_time() const
00870   { return is_temporal_type_with_date_and_time(type()); }
00871 
00872   bool is_null(my_ptrdiff_t row_offset= 0) const
00873   {
00874     /*
00875       if the field is NULLable, it returns NULLity based
00876       on null_ptr[row_offset] value. Otherwise it returns
00877       NULL flag depending on TABLE::null_row value.
00878 
00879       The table may have been marked as containing only NULL values
00880       for all fields if it is a NULL-complemented row of an OUTER JOIN
00881       or if the query is an implicitly grouped query (has aggregate
00882       functions but no GROUP BY clause) with no qualifying rows. If
00883       this is the case (in which TABLE::null_row is true) and the
00884       field is not nullable, the field is considered to be NULL.
00885 
00886       Do not change the order of testing. Fields may be associated
00887       with a TABLE object without being part of the current row.
00888       For NULL value check to work for these fields, they must
00889       have a valid null_ptr, and this pointer must be checked before
00890       TABLE::null_row. 
00891 
00892     */
00893     return real_maybe_null() ?
00894       MY_TEST(null_ptr[row_offset] & null_bit) : table->null_row;
00895   }
00896 
00897   bool is_real_null(my_ptrdiff_t row_offset= 0) const
00898   { return real_maybe_null() ? MY_TEST(null_ptr[row_offset] & null_bit) : false; }
00899 
00900   bool is_null_in_record(const uchar *record) const
00901   { return real_maybe_null() ? MY_TEST(record[null_offset()] & null_bit) : false; }
00902 
00903   void set_null(my_ptrdiff_t row_offset= 0)
00904   {
00905     if (real_maybe_null())
00906       null_ptr[row_offset]|= null_bit;
00907   }
00908 
00909   void set_notnull(my_ptrdiff_t row_offset= 0)
00910   {
00911     if (real_maybe_null())
00912       null_ptr[row_offset]&= (uchar) ~null_bit;
00913   }
00914 
00915   bool maybe_null(void) const
00916   { return real_maybe_null() || table->maybe_null; }
00917 
00919   bool real_maybe_null(void) const
00920   { return null_ptr != 0; }
00921 
00922   uint null_offset(const uchar *record) const
00923   { return (uint) (null_ptr - record); }
00924 
00925   uint null_offset() const
00926   { return null_offset(table->record[0]); }
00927 
00928   void set_null_ptr(uchar *p_null_ptr, uint p_null_bit)
00929   {
00930     null_ptr= p_null_ptr;
00931     null_bit= p_null_bit;
00932   }
00933 
00934   enum {
00935     LAST_NULL_BYTE_UNDEF= 0
00936   };
00937 
00938   /*
00939     Find the position of the last null byte for the field.
00940 
00941     SYNOPSIS
00942       last_null_byte()
00943 
00944     DESCRIPTION
00945       Return a pointer to the last byte of the null bytes where the
00946       field conceptually is placed.
00947 
00948     RETURN VALUE
00949       The position of the last null byte relative to the beginning of
00950       the record. If the field does not use any bits of the null
00951       bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
00952    */
00953   size_t last_null_byte() const {
00954     size_t bytes= do_last_null_byte();
00955     DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
00956     DBUG_ASSERT(bytes <= table->s->null_bytes);
00957     return bytes;
00958   }
00959 
00960   virtual void make_field(Send_field *);
00961 
00972   virtual void make_sort_key(uchar *buff, uint length) = 0;
00973   virtual bool optimize_range(uint idx, uint part);
00974   /*
00975     This should be true for fields which, when compared with constant
00976     items, can be casted to longlong. In this case we will at 'fix_fields'
00977     stage cast the constant items to longlongs and at the execution stage
00978     use field->val_int() for comparison.  Used to optimize clauses like
00979     'a_column BETWEEN date_const, date_const'.
00980   */
00981   virtual bool can_be_compared_as_longlong() const { return false; }
00982   virtual void free() {}
00983   virtual Field *new_field(MEM_ROOT *root, TABLE *new_table,
00984                            bool keep_type);
00985   virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
00986                                uchar *new_ptr, uchar *new_null_ptr,
00987                                uint new_null_bit);
00988 
00989   Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr)
00990   { return new_key_field(root, new_table, new_ptr, null_ptr, null_bit); }
00991 
01001   virtual Field *clone() const =0;
01002 
01013   virtual Field *clone(MEM_ROOT *mem_root) const =0;
01014   inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
01015   {
01016     ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
01017   }
01018   inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
01019   virtual void move_field_offset(my_ptrdiff_t ptr_diff)
01020   {
01021     ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
01022     if (null_ptr)
01023       null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
01024   }
01025   virtual void get_image(uchar *buff, uint length, const CHARSET_INFO *cs)
01026     { memcpy(buff,ptr,length); }
01027   virtual void set_image(const uchar *buff,uint length,
01028                          const CHARSET_INFO *cs)
01029     { memcpy(ptr,buff,length); }
01030 
01031 
01032   /*
01033     Copy a field part into an output buffer.
01034 
01035     SYNOPSIS
01036       Field::get_key_image()
01037       buff   [out] output buffer
01038       length       output buffer size
01039       type         itMBR for geometry blobs, otherwise itRAW
01040 
01041     DESCRIPTION
01042       This function makes a copy of field part of size equal to or
01043       less than "length" parameter value.
01044       For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
01045       is padded by zero byte.
01046 
01047     NOTES
01048       For variable length character fields (i.e. UTF-8) the "length"
01049       parameter means a number of output buffer bytes as if all field
01050       characters have maximal possible size (mbmaxlen). In the other words,
01051       "length" parameter is a number of characters multiplied by
01052       field_charset->mbmaxlen.
01053 
01054     RETURN
01055       Number of copied bytes (excluding padded zero bytes -- see above).
01056   */
01057 
01058   virtual uint get_key_image(uchar *buff, uint length, imagetype type)
01059   {
01060     get_image(buff, length, &my_charset_bin);
01061     return length;
01062   }
01063   virtual void set_key_image(const uchar *buff,uint length)
01064     { set_image(buff,length, &my_charset_bin); }
01065   inline longlong val_int_offset(uint row_offset)
01066     {
01067       ptr+=row_offset;
01068       longlong tmp=val_int();
01069       ptr-=row_offset;
01070       return tmp;
01071     }
01072   inline longlong val_int(const uchar *new_ptr)
01073   {
01074     uchar *old_ptr= ptr;
01075     longlong return_value;
01076     ptr= (uchar*) new_ptr;
01077     return_value= val_int();
01078     ptr= old_ptr;
01079     return return_value;
01080   }
01081   inline String *val_str(String *str, const uchar *new_ptr)
01082   {
01083     uchar *old_ptr= ptr;
01084     ptr= (uchar*) new_ptr;
01085     val_str(str);
01086     ptr= old_ptr;
01087     return str;
01088   }
01089   virtual bool send_binary(Protocol *protocol);
01090 
01091   virtual uchar *pack(uchar *to, const uchar *from,
01092                       uint max_length, bool low_byte_first);
01096   uchar *pack(uchar *to, const uchar *from)
01097   {
01098     DBUG_ENTER("Field::pack");
01099     uchar *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
01100     DBUG_RETURN(result);
01101   }
01102 
01103   virtual const uchar *unpack(uchar* to, const uchar *from,
01104                               uint param_data, bool low_byte_first);
01108   const uchar *unpack(uchar* to, const uchar *from)
01109   {
01110     DBUG_ENTER("Field::unpack");
01111     const uchar *result= unpack(to, from, 0U, table->s->db_low_byte_first);
01112     DBUG_RETURN(result);
01113   }
01114 
01115   virtual uint packed_col_length(const uchar *to, uint length)
01116   { return length;}
01117   virtual uint max_packed_col_length(uint max_length)
01118   { return max_length;}
01119 
01120   uint offset(uchar *record)
01121   {
01122     return (uint) (ptr - record);
01123   }
01124   void copy_from_tmp(int offset);
01125   uint fill_cache_field(struct st_cache_field *copy);
01126   virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
01127   virtual bool get_time(MYSQL_TIME *ltime);
01128   virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
01129   virtual const CHARSET_INFO *charset_for_protocol(void) const
01130   { return binary() ? &my_charset_bin : charset(); }
01131   virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
01132   virtual bool has_charset(void) const { return FALSE; }
01133   /*
01134     match_collation_to_optimize_range() is to distinguish in
01135     range optimizer (see opt_range.cc) between real string types:
01136       CHAR, VARCHAR, TEXT
01137     and the other string-alike types with result_type() == STRING_RESULT:
01138       DATE, TIME, DATETIME, TIMESTAMP
01139     We need it to decide whether to test if collation of the operation
01140     matches collation of the field (needed only for real string types).
01141     QQ: shouldn't DATE/TIME types have their own XXX_RESULT types eventually?
01142   */
01143   virtual bool match_collation_to_optimize_range() const { return false; };
01144   virtual enum Derivation derivation(void) const
01145   { return DERIVATION_IMPLICIT; }
01146   virtual uint repertoire(void) const { return MY_REPERTOIRE_UNICODE30; }
01147   virtual void set_derivation(enum Derivation derivation_arg) { }
01148   bool set_warning(Sql_condition::enum_warning_level, unsigned int code,
01149                    int cuted_increment) const;
01150   inline bool check_overflow(int op_result)
01151   {
01152     return (op_result == E_DEC_OVERFLOW);
01153   }
01154   inline bool check_truncated(int op_result)
01155   {
01156     return (op_result == E_DEC_TRUNCATED);
01157   }
01158   bool warn_if_overflow(int op_result);
01159   void init(TABLE *table_arg)
01160   {
01161     orig_table= table= table_arg;
01162     table_name= &table_arg->alias;
01163   }
01164 
01165   /* maximum possible display length */
01166   virtual uint32 max_display_length()= 0;
01167 
01175   virtual uint is_equal(Create_field *new_field);
01176   /* convert decimal to longlong with overflow check */
01177   longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
01178                                     bool *has_overflow);
01179   /* The max. number of characters */
01180   virtual uint32 char_length()
01181   {
01182     return field_length / charset()->mbmaxlen;
01183   }
01184 
01185   virtual geometry_type get_geometry_type()
01186   {
01187     /* shouldn't get here. */
01188     DBUG_ASSERT(0);
01189     return GEOM_GEOMETRY;
01190   }
01191 #ifndef DBUG_OFF
01192   /* Print field value into debug trace, in NULL-aware way. */
01193   void dbug_print()
01194   {
01195     if (is_real_null())
01196       fprintf(DBUG_FILE, "NULL");
01197     else
01198     {
01199       char buf[256];
01200       String str(buf, sizeof(buf), &my_charset_bin);
01201       str.length(0);
01202       String *pstr;
01203       pstr= val_str(&str);
01204       fprintf(DBUG_FILE, "'%s'", pstr->c_ptr_safe());
01205     }
01206   }
01207 #endif
01208 
01209   ha_storage_media field_storage_type() const
01210   {
01211     return (ha_storage_media)
01212       ((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
01213   }
01214 
01215   void set_storage_type(ha_storage_media storage_type_arg)
01216   {
01217     DBUG_ASSERT(field_storage_type() == HA_SM_DEFAULT);
01218     flags |= (storage_type_arg << FIELD_FLAGS_STORAGE_MEDIA);
01219   }
01220 
01221   column_format_type column_format() const
01222   {
01223     return (column_format_type)
01224       ((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
01225   }
01226 
01227   void set_column_format(column_format_type column_format_arg)
01228   {
01229     DBUG_ASSERT(column_format() == COLUMN_FORMAT_TYPE_DEFAULT);
01230     flags |= (column_format_arg << FIELD_FLAGS_COLUMN_FORMAT);
01231   }
01232 
01233   /* Validate the value stored in a field */
01234   virtual type_conversion_status validate_stored_val(THD *thd)
01235   { return TYPE_OK; }
01236 
01237   /* Hash value */
01238   virtual void hash(ulong *nr, ulong *nr2);
01239   friend int cre_myisam(char * name, register TABLE *form, uint options,
01240                         ulonglong auto_increment_value);
01241   friend class Copy_field;
01242   friend class Item_avg_field;
01243   friend class Item_std_field;
01244   friend class Item_sum_num;
01245   friend class Item_sum_sum;
01246   friend class Item_sum_str;
01247   friend class Item_sum_count;
01248   friend class Item_sum_avg;
01249   friend class Item_sum_std;
01250   friend class Item_sum_min;
01251   friend class Item_sum_max;
01252   friend class Item_func_group_concat;
01253 
01254 private:
01255   /*
01256     Primitive for implementing last_null_byte().
01257 
01258     SYNOPSIS
01259       do_last_null_byte()
01260 
01261     DESCRIPTION
01262       Primitive for the implementation of the last_null_byte()
01263       function. This represents the inheritance interface and can be
01264       overridden by subclasses.
01265    */
01266   virtual size_t do_last_null_byte() const;
01267 
01278   virtual int do_save_field_metadata(uchar *metadata_ptr)
01279   { return 0; }
01280 
01281 protected:
01282   static void handle_int16(uchar *to, const uchar *from,
01283                            bool low_byte_first_from, bool low_byte_first_to)
01284   {
01285     int16 val;
01286 #ifdef WORDS_BIGENDIAN
01287     if (low_byte_first_from)
01288       val = sint2korr(from);
01289     else
01290 #endif
01291       shortget(val, from);
01292 
01293 #ifdef WORDS_BIGENDIAN
01294     if (low_byte_first_to)
01295       int2store(to, val);
01296     else
01297 #endif
01298       shortstore(to, val);
01299   }
01300 
01301   static void handle_int24(uchar *to, const uchar *from,
01302                            bool low_byte_first_from, bool low_byte_first_to)
01303   {
01304     int32 val;
01305 #ifdef WORDS_BIGENDIAN
01306     if (low_byte_first_from)
01307       val = sint3korr(from);
01308     else
01309 #endif
01310       val= (from[0] << 16) + (from[1] << 8) + from[2];
01311 
01312 #ifdef WORDS_BIGENDIAN
01313     if (low_byte_first_to)
01314       int2store(to, val);
01315     else
01316 #endif
01317     {
01318       to[0]= 0xFF & (val >> 16);
01319       to[1]= 0xFF & (val >> 8);
01320       to[2]= 0xFF & val;
01321     }
01322   }
01323 
01324   /*
01325     Helper function to pack()/unpack() int32 values
01326   */
01327   static void handle_int32(uchar *to, const uchar *from,
01328                            bool low_byte_first_from, bool low_byte_first_to)
01329   {
01330     int32 val;
01331 #ifdef WORDS_BIGENDIAN
01332     if (low_byte_first_from)
01333       val = sint4korr(from);
01334     else
01335 #endif
01336       longget(val, from);
01337 
01338 #ifdef WORDS_BIGENDIAN
01339     if (low_byte_first_to)
01340       int4store(to, val);
01341     else
01342 #endif
01343       longstore(to, val);
01344   }
01345 
01346   /*
01347     Helper function to pack()/unpack() int64 values
01348   */
01349   static void handle_int64(uchar* to, const uchar *from,
01350                            bool low_byte_first_from, bool low_byte_first_to)
01351   {
01352     int64 val;
01353 #ifdef WORDS_BIGENDIAN
01354     if (low_byte_first_from)
01355       val = sint8korr(from);
01356     else
01357 #endif
01358       longlongget(val, from);
01359 
01360 #ifdef WORDS_BIGENDIAN
01361     if (low_byte_first_to)
01362       int8store(to, val);
01363     else
01364 #endif
01365       longlongstore(to, val);
01366   }
01367 
01368   uchar *pack_int16(uchar *to, const uchar *from, bool low_byte_first_to)
01369   {
01370     handle_int16(to, from, table->s->db_low_byte_first, low_byte_first_to);
01371     return to  + sizeof(int16);
01372   }
01373 
01374   const uchar *unpack_int16(uchar* to, const uchar *from,
01375                             bool low_byte_first_from)
01376   {
01377     handle_int16(to, from, low_byte_first_from, table->s->db_low_byte_first);
01378     return from + sizeof(int16);
01379   }
01380 
01381   uchar *pack_int24(uchar *to, const uchar *from, bool low_byte_first_to)
01382   {
01383     handle_int24(to, from, table->s->db_low_byte_first, low_byte_first_to);
01384     return to + 3;
01385   }
01386 
01387   const uchar *unpack_int24(uchar* to, const uchar *from,
01388                             bool low_byte_first_from)
01389   {
01390     handle_int24(to, from, low_byte_first_from, table->s->db_low_byte_first);
01391     return from + 3;
01392   }
01393 
01394   uchar *pack_int32(uchar *to, const uchar *from, bool low_byte_first_to)
01395   {
01396     handle_int32(to, from, table->s->db_low_byte_first, low_byte_first_to);
01397     return to  + sizeof(int32);
01398   }
01399 
01400   const uchar *unpack_int32(uchar* to, const uchar *from,
01401                             bool low_byte_first_from)
01402   {
01403     handle_int32(to, from, low_byte_first_from, table->s->db_low_byte_first);
01404     return from + sizeof(int32);
01405   }
01406 
01407   uchar *pack_int64(uchar* to, const uchar *from, bool low_byte_first_to)
01408   {
01409     handle_int64(to, from, table->s->db_low_byte_first, low_byte_first_to);
01410     return to + sizeof(int64);
01411   }
01412 
01413   const uchar *unpack_int64(uchar* to, const uchar *from,
01414                             bool low_byte_first_from)
01415   {
01416     handle_int64(to, from, low_byte_first_from, table->s->db_low_byte_first);
01417     return from + sizeof(int64);
01418   }
01419 
01420 };
01421 
01422 
01423 class Field_num :public Field {
01424 public:
01425   const uint8 dec;
01426   bool zerofill,unsigned_flag;  // Purify cannot handle bit fields
01427   Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
01428             uchar null_bit_arg, utype unireg_check_arg,
01429             const char *field_name_arg,
01430             uint8 dec_arg, bool zero_arg, bool unsigned_arg);
01431   Item_result result_type () const { return REAL_RESULT; }
01432   enum Derivation derivation(void) const { return DERIVATION_NUMERIC; }
01433   uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; }
01434   const CHARSET_INFO *charset(void) const { return &my_charset_numeric; }
01435   void prepend_zeros(String *value);
01436   void add_zerofill_and_unsigned(String &res) const;
01437   friend class Create_field;
01438   uint decimals() const { return (uint) dec; }
01439   bool eq_def(Field *field);
01440   type_conversion_status store_decimal(const my_decimal *);
01441   type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
01442   my_decimal *val_decimal(my_decimal *);
01443   bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
01444   bool get_time(MYSQL_TIME *ltime);
01445   uint is_equal(Create_field *new_field);
01446   uint row_pack_length() const { return pack_length(); }
01447   uint32 pack_length_from_metadata(uint field_metadata) {
01448     uint32 length= pack_length();
01449     DBUG_PRINT("result", ("pack_length_from_metadata(%d): %u",
01450                           field_metadata, length));
01451     return length;
01452   }
01453   type_conversion_status check_int(const CHARSET_INFO *cs,
01454                                    const char *str, int length,
01455                                    const char *int_end, int error);
01456   type_conversion_status get_int(const CHARSET_INFO *cs,
01457                                  const char *from, uint len,
01458                                  longlong *rnd, ulonglong unsigned_max,
01459                                  longlong signed_min, longlong signed_max);
01460 };
01461 
01462 
01463 class Field_str :public Field {
01464 protected:
01465   const CHARSET_INFO *field_charset;
01466   enum Derivation field_derivation;
01467 public:
01468   Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
01469             uchar null_bit_arg, utype unireg_check_arg,
01470             const char *field_name_arg, const CHARSET_INFO *charset);
01471   Item_result result_type () const { return STRING_RESULT; }
01472   Item_result numeric_context_result_type() const
01473   { 
01474     return REAL_RESULT; 
01475   }
01476   uint decimals() const { return NOT_FIXED_DEC; }
01477   void make_field(Send_field *field);
01478   type_conversion_status store(double nr);
01479   type_conversion_status store(longlong nr, bool unsigned_val)=0;
01480   type_conversion_status store_decimal(const my_decimal *);
01481   type_conversion_status store(const char *to, uint length,
01482                                const CHARSET_INFO *cs)=0;
01483   uint repertoire(void) const
01484   {
01485     return my_charset_repertoire(field_charset);
01486   }
01487   const CHARSET_INFO *charset(void) const { return field_charset; }
01488   void set_charset(const CHARSET_INFO *charset_arg)
01489   { field_charset= charset_arg; }
01490   enum Derivation derivation(void) const { return field_derivation; }
01491   virtual void set_derivation(enum Derivation derivation_arg)
01492   { field_derivation= derivation_arg; }
01493   bool binary() const { return field_charset == &my_charset_bin; }
01494   uint32 max_display_length() { return field_length; }
01495   friend class Create_field;
01496   virtual bool str_needs_quotes() { return TRUE; }
01497   uint is_equal(Create_field *new_field);
01498 };
01499 
01500 
01501 /* base class for Field_string, Field_varstring and Field_blob */
01502 
01503 class Field_longstr :public Field_str
01504 {
01505 protected:
01506   type_conversion_status report_if_important_data(const char *ptr,
01507                                                   const char *end,
01508                                                   bool count_spaces) const;
01509   type_conversion_status
01510     check_string_copy_error(const char *well_formed_error_pos,
01511                             const char *cannot_convert_error_pos,
01512                             const char *from_end_pos,
01513                             const char *end,
01514                             bool count_spaces,
01515                             const CHARSET_INFO *cs) const;
01516 public:
01517   Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01518                 uchar null_bit_arg, utype unireg_check_arg,
01519                 const char *field_name_arg, const CHARSET_INFO *charset_arg)
01520     :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
01521                field_name_arg, charset_arg)
01522     {}
01523 
01524   type_conversion_status store_decimal(const my_decimal *d);
01525   uint32 max_data_length() const;
01526 };
01527 
01528 /* base class for float and double and decimal (old one) */
01529 class Field_real :public Field_num {
01530 public:
01531   my_bool not_fixed;
01532 
01533   Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01534              uchar null_bit_arg, utype unireg_check_arg,
01535              const char *field_name_arg,
01536              uint8 dec_arg, bool zero_arg, bool unsigned_arg)
01537     :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
01538                field_name_arg, dec_arg, zero_arg, unsigned_arg),
01539     not_fixed(dec_arg >= NOT_FIXED_DEC)
01540     {}
01541   type_conversion_status store_decimal(const my_decimal *);
01542   type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
01543   my_decimal *val_decimal(my_decimal *);
01544   bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
01545   bool get_time(MYSQL_TIME *ltime);
01546   bool truncate(double *nr, double max_length);
01547   uint32 max_display_length() { return field_length; }
01548   virtual const uchar *unpack(uchar* to, const uchar *from,
01549                               uint param_data, bool low_byte_first);
01550   virtual uchar *pack(uchar* to, const uchar *from,
01551                       uint max_length, bool low_byte_first);
01552 };
01553 
01554 
01555 class Field_decimal :public Field_real {
01556 public:
01557   Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01558                 uchar null_bit_arg,
01559                 enum utype unireg_check_arg, const char *field_name_arg,
01560                 uint8 dec_arg,bool zero_arg,bool unsigned_arg)
01561     :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
01562                 unireg_check_arg, field_name_arg,
01563                 dec_arg, zero_arg, unsigned_arg)
01564     {}
01565   enum_field_types type() const { return MYSQL_TYPE_DECIMAL;}
01566   enum ha_base_keytype key_type() const
01567   { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
01568   type_conversion_status reset(void);
01569   type_conversion_status store(const char *to, uint length,
01570                                const CHARSET_INFO *charset);
01571   type_conversion_status store(double nr);
01572   type_conversion_status store(longlong nr, bool unsigned_val);
01573   double val_real(void);
01574   longlong val_int(void);
01575   String *val_str(String*,String *);
01576   int cmp(const uchar *,const uchar *);
01577   void make_sort_key(uchar *buff, uint length);
01578   void overflow(bool negative);
01579   bool zero_pack() const { return 0; }
01580   void sql_type(String &str) const;
01581   Field_decimal *clone(MEM_ROOT *mem_root) const {
01582     DBUG_ASSERT(type() == MYSQL_TYPE_DECIMAL);
01583     return new (mem_root) Field_decimal(*this);
01584   }
01585   Field_decimal *clone() const {
01586     DBUG_ASSERT(type() == MYSQL_TYPE_DECIMAL);
01587     return new Field_decimal(*this);
01588   }
01589   virtual const uchar *unpack(uchar* to, const uchar *from,
01590                               uint param_data, bool low_byte_first)
01591   {
01592     return Field::unpack(to, from, param_data, low_byte_first);
01593   }
01594   virtual uchar *pack(uchar* to, const uchar *from,
01595                       uint max_length, bool low_byte_first)
01596   {
01597     return Field::pack(to, from, max_length, low_byte_first);
01598   }
01599 };
01600 
01601 
01602 /* New decimal/numeric field which use fixed point arithmetic */
01603 class Field_new_decimal :public Field_num {
01604 private:
01605   int do_save_field_metadata(uchar *first_byte);
01606 public:
01607 
01608   /* The maximum number of decimal digits can be stored */
01609   uint precision;
01610   uint bin_size;
01611   /*
01612     Constructors take max_length of the field as a parameter - not the
01613     precision as the number of decimal digits allowed.
01614     So for example we need to count length from precision handling
01615     CREATE TABLE ( DECIMAL(x,y)) 
01616   */
01617   Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01618                     uchar null_bit_arg,
01619                     enum utype unireg_check_arg, const char *field_name_arg,
01620                     uint8 dec_arg, bool zero_arg, bool unsigned_arg);
01621   Field_new_decimal(uint32 len_arg, bool maybe_null_arg,
01622                     const char *field_name_arg, uint8 dec_arg,
01623                     bool unsigned_arg);
01624   enum_field_types type() const { return MYSQL_TYPE_NEWDECIMAL;}
01625   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
01626   Item_result result_type () const { return DECIMAL_RESULT; }
01627   type_conversion_status reset(void);
01628   type_conversion_status store_value(const my_decimal *decimal_value);
01629   void set_value_on_overflow(my_decimal *decimal_value, bool sign);
01630   type_conversion_status store(const char *to, uint length,
01631                                const CHARSET_INFO *charset);
01632   type_conversion_status store(double nr);
01633   type_conversion_status store(longlong nr, bool unsigned_val);
01634   type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
01635   type_conversion_status store_decimal(const my_decimal *);
01636   double val_real(void);
01637   longlong val_int(void);
01638   my_decimal *val_decimal(my_decimal *);
01639   bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
01640   bool get_time(MYSQL_TIME *ltime);
01641   String *val_str(String*, String *);
01642   int cmp(const uchar *, const uchar *);
01643   void make_sort_key(uchar *buff, uint length);
01644   bool zero_pack() const { return 0; }
01645   void sql_type(String &str) const;
01646   uint32 max_display_length() { return field_length; }
01647   uint32 pack_length() const { return (uint32) bin_size; }
01648   uint pack_length_from_metadata(uint field_metadata);
01649   uint row_pack_length() const { return pack_length(); }
01650   bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
01651                              uint16 mflags, int *order_var);
01652   uint is_equal(Create_field *new_field);
01653   Field_new_decimal *clone(MEM_ROOT *mem_root) const { 
01654     DBUG_ASSERT(type() == MYSQL_TYPE_NEWDECIMAL);
01655     return new (mem_root) Field_new_decimal(*this);
01656   }
01657   Field_new_decimal *clone() const {
01658     DBUG_ASSERT(type() == MYSQL_TYPE_NEWDECIMAL);
01659     return new Field_new_decimal(*this);
01660   }
01661   virtual const uchar *unpack(uchar* to, const uchar *from,
01662                               uint param_data, bool low_byte_first);
01663   static Field *create_from_item (Item *);
01664 };
01665 
01666 
01667 class Field_tiny :public Field_num {
01668 public:
01669   Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01670              uchar null_bit_arg,
01671              enum utype unireg_check_arg, const char *field_name_arg,
01672              bool zero_arg, bool unsigned_arg)
01673     :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
01674                unireg_check_arg, field_name_arg,
01675                0, zero_arg,unsigned_arg)
01676     {}
01677   enum Item_result result_type () const { return INT_RESULT; }
01678   enum_field_types type() const { return MYSQL_TYPE_TINY;}
01679   enum ha_base_keytype key_type() const
01680     { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
01681   type_conversion_status store(const char *to, uint length,
01682                                const CHARSET_INFO *charset);
01683   type_conversion_status store(double nr);
01684   type_conversion_status store(longlong nr, bool unsigned_val);
01685   type_conversion_status reset(void) { ptr[0]=0; return TYPE_OK; }
01686   double val_real(void);
01687   longlong val_int(void);
01688   String *val_str(String*,String *);
01689   bool send_binary(Protocol *protocol);
01690   int cmp(const uchar *,const uchar *);
01691   void make_sort_key(uchar *buff, uint length);
01692   uint32 pack_length() const { return 1; }
01693   void sql_type(String &str) const;
01694   uint32 max_display_length() { return 4; }
01695   Field_tiny *clone(MEM_ROOT *mem_root) const { 
01696     DBUG_ASSERT(type() == MYSQL_TYPE_TINY);
01697     return new (mem_root) Field_tiny(*this);
01698   }
01699   Field_tiny *clone() const {
01700     DBUG_ASSERT(type() == MYSQL_TYPE_TINY);
01701     return new Field_tiny(*this);
01702   }
01703   virtual uchar *pack(uchar* to, const uchar *from,
01704                       uint max_length, bool low_byte_first)
01705   {
01706     *to= *from;
01707     return to + 1;
01708   }
01709 
01710   virtual const uchar *unpack(uchar* to, const uchar *from,
01711                               uint param_data, bool low_byte_first)
01712   {
01713     *to= *from;
01714     return from + 1;
01715   }
01716 };
01717 
01718 
01719 class Field_short :public Field_num {
01720 public:
01721   Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01722               uchar null_bit_arg,
01723               enum utype unireg_check_arg, const char *field_name_arg,
01724               bool zero_arg, bool unsigned_arg)
01725     :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
01726                unireg_check_arg, field_name_arg,
01727                0, zero_arg,unsigned_arg)
01728     {}
01729   Field_short(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
01730               bool unsigned_arg)
01731     :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
01732                NONE, field_name_arg, 0, 0, unsigned_arg)
01733     {}
01734   enum Item_result result_type () const { return INT_RESULT; }
01735   enum_field_types type() const { return MYSQL_TYPE_SHORT;}
01736   enum ha_base_keytype key_type() const
01737     { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
01738   type_conversion_status store(const char *to, uint length,
01739                                const CHARSET_INFO *charset);
01740   type_conversion_status store(double nr);
01741   type_conversion_status store(longlong nr, bool unsigned_val);
01742   type_conversion_status reset(void) { ptr[0]=ptr[1]=0; return TYPE_OK; }
01743   double val_real(void);
01744   longlong val_int(void);
01745   String *val_str(String*,String *);
01746   bool send_binary(Protocol *protocol);
01747   int cmp(const uchar *,const uchar *);
01748   void make_sort_key(uchar *buff, uint length);
01749   uint32 pack_length() const { return 2; }
01750   void sql_type(String &str) const;
01751   uint32 max_display_length() { return 6; }
01752   Field_short *clone(MEM_ROOT *mem_root) const {
01753     DBUG_ASSERT(type() == MYSQL_TYPE_SHORT);
01754     return new (mem_root) Field_short(*this);
01755   }
01756   Field_short *clone() const {
01757     DBUG_ASSERT(type() == MYSQL_TYPE_SHORT);
01758     return new Field_short(*this);
01759   }
01760   virtual uchar *pack(uchar* to, const uchar *from,
01761                       uint max_length, bool low_byte_first)
01762   {
01763     return pack_int16(to, from, low_byte_first);
01764   }
01765 
01766   virtual const uchar *unpack(uchar* to, const uchar *from,
01767                               uint param_data, bool low_byte_first)
01768   {
01769     return unpack_int16(to, from, low_byte_first);
01770   }
01771 };
01772 
01773 class Field_medium :public Field_num {
01774 public:
01775   Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01776               uchar null_bit_arg,
01777               enum utype unireg_check_arg, const char *field_name_arg,
01778               bool zero_arg, bool unsigned_arg)
01779     :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
01780                unireg_check_arg, field_name_arg,
01781                0, zero_arg,unsigned_arg)
01782     {}
01783   enum Item_result result_type () const { return INT_RESULT; }
01784   enum_field_types type() const { return MYSQL_TYPE_INT24;}
01785   enum ha_base_keytype key_type() const
01786     { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
01787   type_conversion_status store(const char *to, uint length,
01788                                const CHARSET_INFO *charset);
01789   type_conversion_status store(double nr);
01790   type_conversion_status store(longlong nr, bool unsigned_val);
01791   type_conversion_status reset(void)
01792   {
01793     ptr[0]=ptr[1]=ptr[2]=0;
01794     return TYPE_OK;
01795   }
01796   double val_real(void);
01797   longlong val_int(void);
01798   String *val_str(String*,String *);
01799   bool send_binary(Protocol *protocol);
01800   int cmp(const uchar *,const uchar *);
01801   void make_sort_key(uchar *buff, uint length);
01802   uint32 pack_length() const { return 3; }
01803   void sql_type(String &str) const;
01804   uint32 max_display_length() { return 8; }
01805   Field_medium *clone(MEM_ROOT *mem_root) const {
01806     DBUG_ASSERT(type() == MYSQL_TYPE_INT24);
01807     return new (mem_root) Field_medium(*this);
01808   }
01809   Field_medium *clone() const {
01810     DBUG_ASSERT(type() == MYSQL_TYPE_INT24);
01811     return new Field_medium(*this);
01812   }
01813   virtual uchar *pack(uchar* to, const uchar *from,
01814                       uint max_length, bool low_byte_first)
01815   {
01816     return Field::pack(to, from, max_length, low_byte_first);
01817   }
01818 
01819   virtual const uchar *unpack(uchar* to, const uchar *from,
01820                               uint param_data, bool low_byte_first)
01821   {
01822     return Field::unpack(to, from, param_data, low_byte_first);
01823   }
01824 };
01825 
01826 
01827 class Field_long :public Field_num {
01828 public:
01829 
01830   static const int PACK_LENGTH= 4;
01831 
01832   Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01833              uchar null_bit_arg,
01834              enum utype unireg_check_arg, const char *field_name_arg,
01835              bool zero_arg, bool unsigned_arg)
01836     :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
01837                unireg_check_arg, field_name_arg,
01838                0, zero_arg,unsigned_arg)
01839     {}
01840   Field_long(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
01841              bool unsigned_arg)
01842     :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
01843                NONE, field_name_arg,0,0,unsigned_arg)
01844     {}
01845   enum Item_result result_type () const { return INT_RESULT; }
01846   enum_field_types type() const { return MYSQL_TYPE_LONG;}
01847   enum ha_base_keytype key_type() const
01848     { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
01849   type_conversion_status store(const char *to, uint length,
01850                                const CHARSET_INFO *charset);
01851   type_conversion_status store(double nr);
01852   type_conversion_status store(longlong nr, bool unsigned_val);
01853   type_conversion_status reset(void)
01854   {
01855     ptr[0]=ptr[1]=ptr[2]=ptr[3]=0;
01856     return TYPE_OK;
01857   }
01858   double val_real(void);
01859   longlong val_int(void);
01860   bool send_binary(Protocol *protocol);
01861   String *val_str(String*,String *);
01862   int cmp(const uchar *,const uchar *);
01863   void make_sort_key(uchar *buff, uint length);
01864   uint32 pack_length() const { return PACK_LENGTH; }
01865   void sql_type(String &str) const;
01866   uint32 max_display_length() { return MY_INT32_NUM_DECIMAL_DIGITS; }
01867   Field_long *clone(MEM_ROOT *mem_root) const {
01868     DBUG_ASSERT(type() == MYSQL_TYPE_LONG);
01869     return new (mem_root) Field_long(*this);
01870   }
01871   Field_long *clone() const {
01872     DBUG_ASSERT(type() == MYSQL_TYPE_LONG);
01873     return new Field_long(*this);
01874   }
01875   virtual uchar *pack(uchar* to, const uchar *from,
01876                       uint max_length __attribute__((unused)),
01877                       bool low_byte_first)
01878   {
01879     return pack_int32(to, from, low_byte_first);
01880   }
01881   virtual const uchar *unpack(uchar* to, const uchar *from,
01882                               uint param_data __attribute__((unused)),
01883                               bool low_byte_first)
01884   {
01885     return unpack_int32(to, from, low_byte_first);
01886   }
01887 };
01888 
01889 
01890 #ifdef HAVE_LONG_LONG
01891 class Field_longlong :public Field_num {
01892 public:
01893   static const int PACK_LENGTH= 8;
01894 
01895   Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01896               uchar null_bit_arg,
01897               enum utype unireg_check_arg, const char *field_name_arg,
01898               bool zero_arg, bool unsigned_arg)
01899     :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
01900                unireg_check_arg, field_name_arg,
01901                0, zero_arg,unsigned_arg)
01902     {}
01903   Field_longlong(uint32 len_arg,bool maybe_null_arg,
01904                  const char *field_name_arg,
01905                   bool unsigned_arg)
01906     :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
01907                NONE, field_name_arg,0,0,unsigned_arg)
01908     {}
01909   enum Item_result result_type () const { return INT_RESULT; }
01910   enum_field_types type() const { return MYSQL_TYPE_LONGLONG;}
01911   enum ha_base_keytype key_type() const
01912     { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
01913   type_conversion_status store(const char *to, uint length,
01914                                const CHARSET_INFO *charset);
01915   type_conversion_status store(double nr);
01916   type_conversion_status store(longlong nr, bool unsigned_val);
01917   type_conversion_status reset(void)
01918   {
01919     ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
01920     return TYPE_OK;
01921   }
01922   double val_real(void);
01923   longlong val_int(void);
01924   String *val_str(String*,String *);
01925   bool send_binary(Protocol *protocol);
01926   int cmp(const uchar *,const uchar *);
01927   void make_sort_key(uchar *buff, uint length);
01928   uint32 pack_length() const { return PACK_LENGTH; }
01929   void sql_type(String &str) const;
01930   bool can_be_compared_as_longlong() const { return true; }
01931   uint32 max_display_length() { return 20; }
01932   Field_longlong *clone(MEM_ROOT *mem_root) const { 
01933     DBUG_ASSERT(type() == MYSQL_TYPE_LONGLONG);
01934     return new (mem_root) Field_longlong(*this);
01935   }
01936   Field_longlong *clone() const {
01937     DBUG_ASSERT(type() == MYSQL_TYPE_LONGLONG);
01938     return new Field_longlong(*this);
01939   }
01940   virtual uchar *pack(uchar* to, const uchar *from,
01941                       uint max_length  __attribute__((unused)),
01942                       bool low_byte_first)
01943   {
01944     return pack_int64(to, from, low_byte_first);
01945   }
01946   virtual const uchar *unpack(uchar* to, const uchar *from,
01947                               uint param_data __attribute__((unused)),
01948                               bool low_byte_first)
01949   {
01950     return unpack_int64(to, from, low_byte_first);
01951   }
01952 };
01953 #endif
01954 
01955 
01956 class Field_float :public Field_real {
01957 public:
01958   Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
01959               uchar null_bit_arg,
01960               enum utype unireg_check_arg, const char *field_name_arg,
01961               uint8 dec_arg,bool zero_arg,bool unsigned_arg)
01962     :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
01963                 unireg_check_arg, field_name_arg,
01964                 dec_arg, zero_arg, unsigned_arg)
01965     {}
01966   Field_float(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
01967               uint8 dec_arg)
01968     :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
01969                 NONE, field_name_arg, dec_arg, 0, 0)
01970     {}
01971   enum_field_types type() const { return MYSQL_TYPE_FLOAT;}
01972   enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
01973   type_conversion_status store(const char *to, uint length,
01974                                const CHARSET_INFO *charset);
01975   type_conversion_status store(double nr);
01976   type_conversion_status store(longlong nr, bool unsigned_val);
01977   type_conversion_status reset(void)
01978   {
01979     memset(ptr, 0, sizeof(float));
01980     return TYPE_OK;
01981   }
01982   double val_real(void);
01983   longlong val_int(void);
01984   String *val_str(String*,String *);
01985   bool send_binary(Protocol *protocol);
01986   int cmp(const uchar *,const uchar *);
01987   void make_sort_key(uchar *buff, uint length);
01988   uint32 pack_length() const { return sizeof(float); }
01989   uint row_pack_length() const { return pack_length(); }
01990   void sql_type(String &str) const;
01991   Field_float *clone(MEM_ROOT *mem_root) const { 
01992     DBUG_ASSERT(type() == MYSQL_TYPE_FLOAT);
01993     return new (mem_root) Field_float(*this);
01994   }
01995   Field_float *clone() const {
01996     DBUG_ASSERT(type() == MYSQL_TYPE_FLOAT);
01997     return new Field_float(*this);
01998   }
01999 private:
02000   int do_save_field_metadata(uchar *first_byte);
02001 };
02002 
02003 
02004 class Field_double :public Field_real {
02005 public:
02006   Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
02007                uchar null_bit_arg,
02008                enum utype unireg_check_arg, const char *field_name_arg,
02009                uint8 dec_arg,bool zero_arg,bool unsigned_arg)
02010     :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
02011                 unireg_check_arg, field_name_arg,
02012                 dec_arg, zero_arg, unsigned_arg)
02013     {}
02014   Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
02015                uint8 dec_arg)
02016     :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
02017                 NONE, field_name_arg, dec_arg, 0, 0)
02018     {}
02019   Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
02020                uint8 dec_arg, my_bool not_fixed_arg)
02021     :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
02022                 NONE, field_name_arg, dec_arg, 0, 0)
02023     {not_fixed= not_fixed_arg; }
02024   enum_field_types type() const { return MYSQL_TYPE_DOUBLE;}
02025   enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
02026   type_conversion_status store(const char *to, uint length,
02027                                const CHARSET_INFO *charset);
02028   type_conversion_status store(double nr);
02029   type_conversion_status store(longlong nr, bool unsigned_val);
02030   type_conversion_status reset(void)
02031   {
02032     memset(ptr, 0, sizeof(double));
02033     return TYPE_OK;
02034   }
02035   double val_real(void);
02036   longlong val_int(void);
02037   String *val_str(String*,String *);
02038   bool send_binary(Protocol *protocol);
02039   int cmp(const uchar *,const uchar *);
02040   void make_sort_key(uchar *buff, uint length);
02041   uint32 pack_length() const { return sizeof(double); }
02042   uint row_pack_length() const { return pack_length(); }
02043   void sql_type(String &str) const;
02044   Field_double *clone(MEM_ROOT *mem_root) const {
02045     DBUG_ASSERT(type() == MYSQL_TYPE_DOUBLE);
02046     return new (mem_root) Field_double(*this);
02047   }
02048   Field_double *clone() const {
02049     DBUG_ASSERT(type() == MYSQL_TYPE_DOUBLE);
02050     return new Field_double(*this);
02051   }
02052 private:
02053   int do_save_field_metadata(uchar *first_byte);
02054 };
02055 
02056 
02057 /* Everything saved in this will disappear. It will always return NULL */
02058 
02059 class Field_null :public Field_str {
02060   static uchar null[1];
02061 public:
02062   Field_null(uchar *ptr_arg, uint32 len_arg,
02063              enum utype unireg_check_arg, const char *field_name_arg,
02064              const CHARSET_INFO *cs)
02065     :Field_str(ptr_arg, len_arg, null, 1,
02066                unireg_check_arg, field_name_arg, cs)
02067     {}
02068   enum_field_types type() const { return MYSQL_TYPE_NULL;}
02069   type_conversion_status store(const char *to, uint length,
02070                                const CHARSET_INFO *cs)
02071   {
02072     null[0]= 1;
02073     return TYPE_OK;
02074   }
02075   type_conversion_status store(double nr)   { null[0]=1; return TYPE_OK; }
02076   type_conversion_status store(longlong nr, bool unsigned_val)
02077   {
02078     null[0]=1;
02079     return TYPE_OK;
02080   }
02081   type_conversion_status store_decimal(const my_decimal *d)
02082   {
02083     null[0]=1;
02084     return TYPE_OK;
02085   }
02086   type_conversion_status reset(void)       { return TYPE_OK; }
02087   double val_real(void)         { return 0.0;}
02088   longlong val_int(void)        { return 0;}
02089   my_decimal *val_decimal(my_decimal *) { return 0; }
02090   String *val_str(String *value,String *value2)
02091   { value2->length(0); return value2;}
02092   int cmp(const uchar *a, const uchar *b) { return 0;}
02093   void make_sort_key(uchar *buff, uint length)  {}
02094   uint32 pack_length() const { return 0; }
02095   void sql_type(String &str) const;
02096   uint32 max_display_length() { return 4; }
02097   Field_null *clone(MEM_ROOT *mem_root) const {
02098     DBUG_ASSERT(type() == MYSQL_TYPE_NULL);
02099     return new (mem_root) Field_null(*this);
02100   }
02101   Field_null *clone() const {
02102     DBUG_ASSERT(type() == MYSQL_TYPE_NULL);
02103     return new Field_null(*this);
02104   }
02105 };
02106 
02107 
02108 /*
02109   Abstract class for TIME, DATE, DATETIME, TIMESTAMP
02110   with and without fractional part.
02111 */
02112 class Field_temporal :public Field {
02113 protected:
02114   uint8 dec; // Number of fractional digits
02115 
02119   uint8 normalize_dec(uint8 dec_arg)
02120   { return dec_arg == NOT_FIXED_DEC ? DATETIME_MAX_DECIMALS : dec_arg; }
02121 
02132   virtual type_conversion_status store_internal(const MYSQL_TIME *ltime,
02133                                                 int *error)= 0;
02134 
02144   virtual type_conversion_status store_internal_with_round(MYSQL_TIME *ltime,
02145                                                            int *warnings)= 0;
02146 
02156   type_conversion_status store_lldiv_t(const lldiv_t *lld, int *warning);
02157 
02169   virtual bool convert_str_to_TIME(const char *str, uint len,
02170                                    const CHARSET_INFO *cs,
02171                                    MYSQL_TIME *ltime, 
02172                                    MYSQL_TIME_STATUS *status)= 0;
02186   virtual type_conversion_status convert_number_to_TIME(longlong nr,
02187                                                         bool unsigned_val,
02188                                                         int nanoseconds,
02189                                                         MYSQL_TIME *ltime,
02190                                                         int *warning)= 0;
02191 
02201   longlong convert_number_to_datetime(longlong nr, bool unsigned_val,
02202                                       MYSQL_TIME *ltime, int *warning);
02203 
02213   void set_warnings(ErrConvString str, int warnings);
02214 
02228   virtual ulonglong date_flags(const THD *thd)
02229   {
02230     return 0;
02231   }
02237   inline ulonglong date_flags()
02238   {
02239     return date_flags(table ? table->in_use : current_thd);
02240   }
02241 
02251   void set_datetime_warning(Sql_condition::enum_warning_level level, uint code,
02252                             ErrConvString str,
02253                             timestamp_type ts_type, int cuted_increment);
02254 public:
02265   Field_temporal(uchar *ptr_arg,
02266                  uchar *null_ptr_arg, uchar null_bit_arg,
02267                  enum utype unireg_check_arg, const char *field_name_arg,
02268                  uint32 len_arg, uint8 dec_arg)
02269     :Field(ptr_arg,
02270            len_arg + ((dec= normalize_dec(dec_arg)) ? dec + 1 : 0),
02271            null_ptr_arg, null_bit_arg,
02272            unireg_check_arg, field_name_arg)
02273     { flags|= BINARY_FLAG; }
02281   Field_temporal(bool maybe_null_arg, const char *field_name_arg,
02282                  uint32 len_arg, uint8 dec_arg)
02283     :Field((uchar *) 0, 
02284            len_arg + ((dec= normalize_dec(dec_arg)) ? dec + 1 : 0),
02285            maybe_null_arg ? (uchar *) "" : 0, 0,
02286            NONE, field_name_arg)
02287     { flags|= BINARY_FLAG; }
02288   virtual Item_result result_type() const { return STRING_RESULT; }
02289   virtual uint32 max_display_length() { return field_length; }
02290   virtual bool str_needs_quotes() { return TRUE; }
02291   virtual uint is_equal(Create_field *new_field);
02292   Item_result numeric_context_result_type() const
02293   {
02294     return dec ? DECIMAL_RESULT : INT_RESULT;
02295   }
02296   enum Item_result cmp_type() const { return INT_RESULT; }
02297   enum Derivation derivation() const { return DERIVATION_NUMERIC; }
02298   uint repertoire() const { return MY_REPERTOIRE_NUMERIC; }
02299   const CHARSET_INFO *charset() const { return &my_charset_numeric; }
02300   bool can_be_compared_as_longlong() const { return true; }
02301   bool binary() const { return true; }
02302   type_conversion_status store(const char *str, uint len,
02303                                const CHARSET_INFO *cs);
02304   type_conversion_status store_decimal(const my_decimal *decimal);
02305   type_conversion_status store(longlong nr, bool unsigned_val);
02306   type_conversion_status store(double nr);
02307   double val_real() // FSP-enable types redefine it.
02308   {
02309     return (double) val_int();
02310   }
02311   my_decimal *val_decimal(my_decimal *decimal_value); // FSP types redefine it
02312 };
02313 
02314 
02320 class Field_temporal_with_date :public Field_temporal {
02321 protected:
02326   virtual bool get_date_internal(MYSQL_TIME *ltime)= 0;
02327 
02333   bool get_internal_check_zero(MYSQL_TIME *ltime, uint fuzzydate);
02334   
02335   type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
02336                                                 int nanoseconds,
02337                                                 MYSQL_TIME *ltime,
02338                                                 int *warning);
02339   bool convert_str_to_TIME(const char *str, uint len, const CHARSET_INFO *cs,
02340                            MYSQL_TIME *ltime, MYSQL_TIME_STATUS *status);
02341   type_conversion_status store_internal_with_round(MYSQL_TIME *ltime,
02342                                                    int *warnings);
02343 public:
02354   Field_temporal_with_date(uchar *ptr_arg, uchar *null_ptr_arg,
02355                            uchar null_bit_arg,
02356                            enum utype unireg_check_arg,
02357                            const char *field_name_arg,
02358                            uint8 int_length_arg, uint8 dec_arg)
02359     :Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg,
02360                     unireg_check_arg, field_name_arg,
02361                     int_length_arg, dec_arg)
02362     { }
02370   Field_temporal_with_date(bool maybe_null_arg, const char *field_name_arg,
02371                            uint int_length_arg, uint8 dec_arg)
02372     :Field_temporal((uchar*) 0, maybe_null_arg ? (uchar*) "": 0, 0,
02373                     NONE, field_name_arg, int_length_arg, dec_arg)
02374     { }
02375   bool send_binary(Protocol *protocol);
02376   type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
02377   String *val_str(String *, String *);
02378   longlong val_time_temporal();
02379   longlong val_date_temporal();
02380   bool get_time(MYSQL_TIME *ltime)
02381   {
02382     return get_date(ltime, TIME_FUZZY_DATE);
02383   }
02384   /* Validate the value stored in a field */
02385   virtual type_conversion_status validate_stored_val(THD *thd);
02386 };
02387 
02388 
02394 class Field_temporal_with_date_and_time :public Field_temporal_with_date {
02395 private:
02396   int do_save_field_metadata(uchar *metadata_ptr)
02397   {
02398     if (decimals())
02399     {
02400       *metadata_ptr= decimals();
02401       return 1;
02402     }
02403     return 0;
02404   }
02405 protected:
02412   void init_timestamp_flags();
02418   virtual void store_timestamp_internal(const struct timeval *tm)= 0;
02419   bool convert_TIME_to_timestamp(THD *thd, const MYSQL_TIME *ltime,
02420                                  struct timeval *tm, int *error);
02421 
02422 public:
02432   Field_temporal_with_date_and_time(uchar *ptr_arg, uchar *null_ptr_arg,
02433                                     uchar null_bit_arg,
02434                                     enum utype unireg_check_arg,
02435                                     const char *field_name_arg,
02436                                     uint8 dec_arg)
02437     :Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
02438                               unireg_check_arg, field_name_arg,
02439                               MAX_DATETIME_WIDTH, dec_arg)
02440     { }
02441   void store_timestamp(const struct timeval *tm);
02442 };
02443 
02444 
02449 class Field_temporal_with_date_and_timef :
02450   public Field_temporal_with_date_and_time {
02451 private:
02452   int do_save_field_metadata(uchar *metadata_ptr)
02453   {
02454     *metadata_ptr= decimals();
02455     return 1;
02456   }
02457 public:
02467   Field_temporal_with_date_and_timef(uchar *ptr_arg, uchar *null_ptr_arg,
02468                                      uchar null_bit_arg,
02469                                      enum utype unireg_check_arg,
02470                                      const char *field_name_arg,
02471                                      uint8 dec_arg)
02472     :Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
02473                                        unireg_check_arg, field_name_arg,
02474                                        dec_arg)
02475     { }
02482   Field_temporal_with_date_and_timef(bool maybe_null_arg,
02483                                      const char *field_name_arg,
02484                                      uint8 dec_arg)
02485     :Field_temporal_with_date_and_time((uchar *) 0,
02486                                        maybe_null_arg ? (uchar*) "" : 0, 0,
02487                                        NONE, field_name_arg, dec_arg)
02488     { }
02489 
02490   uint decimals() const { return dec; }
02491   const CHARSET_INFO *sort_charset() const { return &my_charset_bin; }
02492   void make_sort_key(uchar *to, uint length) { memcpy(to, ptr, length); }
02493   int cmp(const uchar *a_ptr, const uchar *b_ptr)
02494   {
02495     return memcmp(a_ptr, b_ptr, pack_length());
02496   }
02497   uint row_pack_length() const { return pack_length(); }
02498   double val_real();
02499   longlong val_int();
02500   my_decimal *val_decimal(my_decimal *decimal_value);
02501 };
02502 
02503 
02504 /*
02505   Field implementing TIMESTAMP data type without fractional seconds.
02506   We will be removed eventually.
02507 */
02508 class Field_timestamp :public Field_temporal_with_date_and_time {
02509 protected:
02510   ulonglong date_flags(const THD *thd);
02511   type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
02512   bool get_date_internal(MYSQL_TIME *ltime);
02513   void store_timestamp_internal(const struct timeval *tm);
02514 public:
02515   static const int PACK_LENGTH= 4;
02516   Field_timestamp(uchar *ptr_arg, uint32 len_arg,
02517                   uchar *null_ptr_arg, uchar null_bit_arg,
02518                   enum utype unireg_check_arg, const char *field_name_arg);
02519   Field_timestamp(bool maybe_null_arg, const char *field_name_arg);
02520   enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP;}
02521   enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
02522   type_conversion_status store_packed(longlong nr);
02523   type_conversion_status reset(void)
02524   {
02525     ptr[0]=ptr[1]=ptr[2]=ptr[3]=0;
02526     return TYPE_OK;
02527   }
02528   longlong val_int(void);
02529   int cmp(const uchar *,const uchar *);
02530   void make_sort_key(uchar *buff, uint length);
02531   uint32 pack_length() const { return PACK_LENGTH; }
02532   void sql_type(String &str) const;
02533   bool zero_pack() const { return 0; }
02534   /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
02535   bool get_timestamp(struct timeval *tm, int *warnings);
02536   bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
02537   Field_timestamp *clone(MEM_ROOT *mem_root) const {
02538     DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
02539     return new (mem_root) Field_timestamp(*this);
02540   }
02541   Field_timestamp *clone() const
02542   {
02543     DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
02544     return new Field_timestamp(*this);
02545   }
02546   uchar *pack(uchar *to, const uchar *from,
02547               uint max_length __attribute__((unused)), bool low_byte_first)
02548   {
02549     return pack_int32(to, from, low_byte_first);
02550   }
02551   const uchar *unpack(uchar* to, const uchar *from,
02552                       uint param_data __attribute__((unused)),
02553                       bool low_byte_first)
02554   {
02555     return unpack_int32(to, from, low_byte_first);
02556   }
02557   /* Validate the value stored in a field */
02558   virtual type_conversion_status validate_stored_val(THD *thd);
02559 };
02560 
02561 
02562 /*
02563   Field implementing TIMESTAMP(N) data type, where N=0..6.
02564 */
02565 class Field_timestampf :public Field_temporal_with_date_and_timef {
02566 protected:
02567   bool get_date_internal(MYSQL_TIME *ltime);
02568   type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
02569   ulonglong date_flags(const THD *thd);
02570   void store_timestamp_internal(const struct timeval *tm);
02571 public:
02572   static const int PACK_LENGTH= 8;
02583   Field_timestampf(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
02584                    enum utype unireg_check_arg, const char *field_name_arg,
02585                    uint8 dec_arg);
02592   Field_timestampf(bool maybe_null_arg, const char *field_name_arg,
02593                    uint8 dec_arg);
02594   Field_timestampf *clone(MEM_ROOT *mem_root) const
02595   {
02596     DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
02597     return new (mem_root) Field_timestampf(*this);
02598   }
02599   Field_timestampf *clone() const
02600   {
02601     DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
02602     return new Field_timestampf(*this);
02603   }
02604 
02605   enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP; }
02606   enum_field_types real_type() const { return MYSQL_TYPE_TIMESTAMP2; }
02607   enum_field_types binlog_type() const { return MYSQL_TYPE_TIMESTAMP2; }
02608   bool zero_pack() const { return 0; }
02609 
02610   uint32 pack_length() const
02611   {
02612     return my_timestamp_binary_length(dec);
02613   }
02614   virtual uint pack_length_from_metadata(uint field_metadata)
02615   {
02616     DBUG_ENTER("Field_timestampf::pack_length_from_metadata");
02617     uint tmp= my_timestamp_binary_length(field_metadata);
02618     DBUG_RETURN(tmp);
02619   }
02620 
02621   type_conversion_status reset();
02622   type_conversion_status store_packed(longlong nr);
02623   bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
02624   void sql_type(String &str) const;
02625 
02626   bool get_timestamp(struct timeval *tm, int *warnings);
02627   /* Validate the value stored in a field */
02628   virtual type_conversion_status validate_stored_val(THD *thd);
02629 };
02630 
02631 
02632 class Field_year :public Field_tiny {
02633 public:
02634   Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
02635              uchar null_bit_arg,
02636              enum utype unireg_check_arg, const char *field_name_arg)
02637     :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
02638                 unireg_check_arg, field_name_arg, 1, 1)
02639     {}
02640   enum_field_types type() const { return MYSQL_TYPE_YEAR;}
02641   type_conversion_status store(const char *to,uint length,
02642                                const CHARSET_INFO *charset);
02643   type_conversion_status store(double nr);
02644   type_conversion_status store(longlong nr, bool unsigned_val);
02645   type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
02646   double val_real(void);
02647   longlong val_int(void);
02648   String *val_str(String*,String *);
02649   bool send_binary(Protocol *protocol);
02650   void sql_type(String &str) const;
02651   bool can_be_compared_as_longlong() const { return true; }
02652   Field_year *clone(MEM_ROOT *mem_root) const {
02653     DBUG_ASSERT(type() == MYSQL_TYPE_YEAR);
02654     return new (mem_root) Field_year(*this);
02655   }
02656   Field_year *clone() const {
02657     DBUG_ASSERT(type() == MYSQL_TYPE_YEAR);
02658     return new Field_year(*this);
02659   }
02660 };
02661 
02662 
02663 class Field_newdate :public Field_temporal_with_date {
02664 protected:
02665   static const int PACK_LENGTH= 3;
02666   ulonglong date_flags(const THD *thd);
02667   bool get_date_internal(MYSQL_TIME *ltime);
02668   type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
02669 
02670 public:
02671   Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
02672                 enum utype unireg_check_arg, const char *field_name_arg)
02673     :Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
02674                               unireg_check_arg, field_name_arg,
02675                               MAX_DATE_WIDTH, 0)
02676     { }
02677   Field_newdate(bool maybe_null_arg, const char *field_name_arg)
02678     :Field_temporal_with_date((uchar *) 0, maybe_null_arg ? (uchar *) "" : 0,
02679                               0, NONE, field_name_arg, MAX_DATE_WIDTH, 0)
02680     { }
02681   enum_field_types type() const { return MYSQL_TYPE_DATE;}
02682   enum_field_types real_type() const { return MYSQL_TYPE_NEWDATE; }
02683   enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
02684   type_conversion_status reset(void)
02685   {
02686     ptr[0]=ptr[1]=ptr[2]=0;
02687     return TYPE_OK;
02688   }
02689   type_conversion_status store_packed(longlong nr);
02690   longlong val_int(void);
02691   longlong val_time_temporal();
02692   longlong val_date_temporal();
02693   String *val_str(String*,String *);
02694   bool send_binary(Protocol *protocol);
02695   int cmp(const uchar *,const uchar *);
02696   void make_sort_key(uchar *buff, uint length);
02697   uint32 pack_length() const { return PACK_LENGTH; }
02698   void sql_type(String &str) const;
02699   bool zero_pack() const { return 1; }
02700   bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
02701   Field_newdate *clone(MEM_ROOT *mem_root) const
02702   {
02703     DBUG_ASSERT(type() == MYSQL_TYPE_DATE);
02704     DBUG_ASSERT(real_type() == MYSQL_TYPE_NEWDATE);
02705     return new (mem_root) Field_newdate(*this);
02706   }
02707   Field_newdate *clone() const
02708   {
02709     DBUG_ASSERT(type() == MYSQL_TYPE_DATE);
02710     DBUG_ASSERT(real_type() == MYSQL_TYPE_NEWDATE);
02711     return new Field_newdate(*this);
02712   }
02713 };
02714 
02715 
02719 class Field_time_common :public Field_temporal {
02720 protected:
02721   bool convert_str_to_TIME(const char *str, uint len, const CHARSET_INFO *cs,
02722                            MYSQL_TIME *ltime, MYSQL_TIME_STATUS *status);
02728   type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
02729                                                 int nanoseconds,
02730                                                 MYSQL_TIME *ltime,
02731                                                 int *warning);
02736   virtual type_conversion_status store_internal(const MYSQL_TIME *ltime,
02737                                                 int *error)= 0;
02742   virtual type_conversion_status store_internal_with_round(MYSQL_TIME *ltime,
02743                                                            int *warnings);
02744 public:
02754   Field_time_common(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
02755                     enum utype unireg_check_arg, const char *field_name_arg,
02756                     uint8 dec_arg)
02757     :Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg,
02758                     unireg_check_arg, field_name_arg,
02759                     MAX_TIME_WIDTH, dec_arg)
02760     { }
02767   Field_time_common(bool maybe_null_arg, const char *field_name_arg,
02768                     uint8 dec_arg)
02769     :Field_temporal((uchar *) 0, maybe_null_arg ? (uchar *) "" : 0, 0,
02770                     NONE, field_name_arg, MAX_TIME_WIDTH, dec_arg)
02771     { }
02772   type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
02773   String *val_str(String*, String *);
02774   bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
02775   longlong val_date_temporal();
02776   bool send_binary(Protocol *protocol);
02777 };
02778 
02779 
02780 /*
02781   Field implementing TIME data type without fractional seconds.
02782   We will be removed eventually.
02783 */
02784 class Field_time :public Field_time_common {
02785 protected:
02786   type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
02787 public:
02788   Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
02789              enum utype unireg_check_arg, const char *field_name_arg)
02790     :Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg,
02791                        unireg_check_arg, field_name_arg, 0)
02792     { }
02793   Field_time(bool maybe_null_arg, const char *field_name_arg)
02794     :Field_time_common((uchar *) 0, maybe_null_arg ? (uchar *) "" : 0, 0,
02795                        NONE, field_name_arg, 0)
02796     { }
02797   enum_field_types type() const { return MYSQL_TYPE_TIME;}
02798   enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
02799   type_conversion_status store_packed(longlong nr);
02800   type_conversion_status reset(void)
02801   {
02802     ptr[0]=ptr[1]=ptr[2]=0;
02803     return TYPE_OK;
02804   }
02805   longlong val_int(void);
02806   longlong val_time_temporal();
02807   bool get_time(MYSQL_TIME *ltime);
02808   int cmp(const uchar *,const uchar *);
02809   void make_sort_key(uchar *buff, uint length);
02810   uint32 pack_length() const { return 3; }
02811   void sql_type(String &str) const;
02812   bool zero_pack() const { return 1; }
02813   Field_time *clone(MEM_ROOT *mem_root) const {
02814     DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
02815     return new (mem_root) Field_time(*this);
02816   }
02817   Field_time *clone() const {
02818     DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
02819     return new Field_time(*this);
02820   }
02821 };
02822 
02823 
02824 /*
02825   Field implementing TIME(N) data type, where N=0..6.
02826 */
02827 class Field_timef :public Field_time_common {
02828 private:
02829   int do_save_field_metadata(uchar *metadata_ptr)
02830   {
02831     *metadata_ptr= decimals();
02832     return 1;
02833   }
02834 protected:
02835   type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
02836 public:
02846   Field_timef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
02847               enum utype unireg_check_arg, const char *field_name_arg,
02848               uint8 dec_arg)
02849     :Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg,
02850                        unireg_check_arg, field_name_arg, dec_arg)
02851   { }
02858   Field_timef(bool maybe_null_arg, const char *field_name_arg, uint8 dec_arg)
02859     :Field_time_common((uchar *) 0, maybe_null_arg ? (uchar *) "" : 0, 0,
02860                        NONE, field_name_arg, dec_arg)
02861   { }
02862   Field_timef *clone(MEM_ROOT *mem_root) const
02863   {
02864     DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
02865     return new (mem_root) Field_timef(*this);
02866   }
02867   Field_timef *clone() const
02868   {
02869     DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
02870     return new Field_timef(*this);
02871   }
02872   uint decimals() const { return dec; }
02873   enum_field_types type() const { return MYSQL_TYPE_TIME;}
02874   enum_field_types real_type() const { return MYSQL_TYPE_TIME2; }
02875   enum_field_types binlog_type() const { return MYSQL_TYPE_TIME2; }
02876   type_conversion_status store_packed(longlong nr);
02877   type_conversion_status reset();
02878   double val_real();
02879   longlong val_int();
02880   longlong val_time_temporal();
02881   bool get_time(MYSQL_TIME *ltime);
02882   my_decimal *val_decimal(my_decimal *);
02883   uint32 pack_length() const
02884   {
02885     return my_time_binary_length(dec);
02886   }
02887   virtual uint pack_length_from_metadata(uint field_metadata)
02888   {
02889     DBUG_ENTER("Field_timef::pack_length_from_metadata");
02890     uint tmp= my_time_binary_length(field_metadata);
02891     DBUG_RETURN(tmp);
02892   }
02893   uint row_pack_length() const { return pack_length(); }
02894   void sql_type(String &str) const;
02895   bool zero_pack() const { return 1; }
02896   const CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
02897   void make_sort_key(uchar *to, uint length) { memcpy(to, ptr, length); }
02898   int cmp(const uchar *a_ptr, const uchar *b_ptr)
02899   {
02900     return memcmp(a_ptr, b_ptr, pack_length());
02901   }
02902 };
02903 
02904 
02905 /*
02906   Field implementing DATETIME data type without fractional seconds.
02907   We will be removed eventually.
02908 */
02909 class Field_datetime :public Field_temporal_with_date_and_time {
02910 protected:
02911   type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
02912   bool get_date_internal(MYSQL_TIME *ltime);
02913   ulonglong date_flags(const THD *thd);
02914   void store_timestamp_internal(const struct timeval *tm);
02915 
02916 public:
02917   static const int PACK_LENGTH= 8;
02918 
02944   Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
02945                  enum utype unireg_check_arg, const char *field_name_arg)
02946     :Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
02947                                        unireg_check_arg, field_name_arg, 0)
02948     {}
02949   Field_datetime(bool maybe_null_arg, const char *field_name_arg)
02950     :Field_temporal_with_date_and_time((uchar *) 0,
02951                                        maybe_null_arg ? (uchar *) "" : 0,
02952                                        0, NONE, field_name_arg, 0)
02953     {}
02954   enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
02955 #ifdef HAVE_LONG_LONG
02956   enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; }
02957 #endif
02958   using Field_temporal_with_date_and_time::store; // Make -Woverloaded-virtual
02959   type_conversion_status store(longlong nr, bool unsigned_val);
02960   type_conversion_status store_packed(longlong nr);
02961   type_conversion_status reset(void)
02962   {
02963     ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
02964     return TYPE_OK;
02965   }
02966   longlong val_int(void);
02967   String *val_str(String*,String *);
02968   int cmp(const uchar *,const uchar *);
02969   void make_sort_key(uchar *buff, uint length);
02970   uint32 pack_length() const { return PACK_LENGTH; }
02971   void sql_type(String &str) const;
02972   bool zero_pack() const { return 1; }
02973   bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
02974   Field_datetime *clone(MEM_ROOT *mem_root) const
02975   {
02976     DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
02977     return new (mem_root) Field_datetime(*this);
02978   }
02979   Field_datetime *clone() const
02980   {
02981     DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
02982     return new Field_datetime(*this);
02983   }
02984   uchar *pack(uchar* to, const uchar *from,
02985               uint max_length __attribute__((unused)), bool low_byte_first)
02986   {
02987     return pack_int64(to, from, low_byte_first);
02988   }
02989   const uchar *unpack(uchar* to, const uchar *from,
02990                       uint param_data __attribute__((unused)),
02991                       bool low_byte_first)
02992   {
02993     return unpack_int64(to, from, low_byte_first);
02994   }
02995 };
02996 
02997 
02998 /*
02999   Field implementing DATETIME(N) data type, where N=0..6.
03000 */
03001 class Field_datetimef :public Field_temporal_with_date_and_timef {
03002 protected:
03003   bool get_date_internal(MYSQL_TIME *ltime);
03004   type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
03005   ulonglong date_flags(const THD *thd);
03006   void store_timestamp_internal(const struct timeval *tm);
03007 
03008 public:
03018   Field_datetimef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
03019                  enum utype unireg_check_arg, const char *field_name_arg,
03020                  uint8 dec_arg)
03021     :Field_temporal_with_date_and_timef(ptr_arg, null_ptr_arg, null_bit_arg,
03022                                         unireg_check_arg, field_name_arg,
03023                                         dec_arg)
03024     {}
03032   Field_datetimef(bool maybe_null_arg, const char *field_name_arg,
03033                   uint8 dec_arg)
03034     :Field_temporal_with_date_and_timef((uchar *) 0,
03035                                         maybe_null_arg ? (uchar *) "" : 0, 0,
03036                                         NONE, field_name_arg, dec_arg)
03037     {}
03038   Field_datetimef *clone(MEM_ROOT *mem_root) const
03039   {
03040     DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
03041     return new (mem_root) Field_datetimef(*this);
03042   }
03043   Field_datetimef *clone() const
03044   {
03045     DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
03046     return new Field_datetimef(*this);
03047   }
03048 
03049   enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
03050   enum_field_types real_type() const { return MYSQL_TYPE_DATETIME2; }
03051   enum_field_types binlog_type() const { return MYSQL_TYPE_DATETIME2; }
03052   uint32 pack_length() const
03053   {
03054     return my_datetime_binary_length(dec);
03055   }
03056   virtual uint pack_length_from_metadata(uint field_metadata)
03057   {
03058     DBUG_ENTER("Field_datetimef::pack_length_from_metadata");
03059     uint tmp= my_datetime_binary_length(field_metadata);
03060     DBUG_RETURN(tmp);
03061   }
03062   bool zero_pack() const { return 1; }
03063 
03064   type_conversion_status store_packed(longlong nr);
03065   type_conversion_status reset();
03066   longlong val_date_temporal();
03067   bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
03068   void sql_type(String &str) const;
03069 };
03070 
03071 
03072 class Field_string :public Field_longstr {
03073 public:
03074   bool can_alter_field_type;
03075   Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
03076                uchar null_bit_arg,
03077                enum utype unireg_check_arg, const char *field_name_arg,
03078                const CHARSET_INFO *cs)
03079     :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
03080                    unireg_check_arg, field_name_arg, cs),
03081      can_alter_field_type(1) {};
03082   Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
03083                const CHARSET_INFO *cs)
03084     :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
03085                    NONE, field_name_arg, cs),
03086      can_alter_field_type(1) {};
03087 
03088   enum_field_types type() const
03089   {
03090     return ((can_alter_field_type && orig_table &&
03091              orig_table->s->db_create_options & HA_OPTION_PACK_RECORD &&
03092              field_length >= 4) &&
03093             orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR ?
03094             MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING);
03095   }
03096   bool match_collation_to_optimize_range() const { return true; }
03097   enum ha_base_keytype key_type() const
03098     { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
03099   bool zero_pack() const { return 0; }
03100   type_conversion_status reset(void)
03101   {
03102     charset()->cset->fill(charset(),(char*) ptr, field_length,
03103                           (has_charset() ? ' ' : 0));
03104     return TYPE_OK;
03105   }
03106   type_conversion_status store(const char *to,uint length,
03107                                const CHARSET_INFO *charset);
03108   type_conversion_status store(longlong nr, bool unsigned_val);
03109   /* QQ: To be deleted */
03110   type_conversion_status store(double nr) { return Field_str::store(nr); }
03111   double val_real(void);
03112   longlong val_int(void);
03113   String *val_str(String*,String *);
03114   my_decimal *val_decimal(my_decimal *);
03115   int cmp(const uchar *,const uchar *);
03116   void make_sort_key(uchar *buff, uint length);
03117   void sql_type(String &str) const;
03118   virtual uchar *pack(uchar *to, const uchar *from,
03119                       uint max_length, bool low_byte_first);
03120   virtual const uchar *unpack(uchar* to, const uchar *from,
03121                               uint param_data, bool low_byte_first);
03122   uint pack_length_from_metadata(uint field_metadata)
03123   {
03124     DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
03125     if (field_metadata == 0)
03126       return row_pack_length();
03127     return (((field_metadata >> 4) & 0x300) ^ 0x300) + (field_metadata & 0x00ff);
03128   }
03129   bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
03130                              uint16 mflags, int *order_var);
03131   uint row_pack_length() const { return field_length; }
03132   int pack_cmp(const uchar *a,const uchar *b,uint key_length,
03133                my_bool insert_or_update);
03134   int pack_cmp(const uchar *b,uint key_length,my_bool insert_or_update);
03135   uint packed_col_length(const uchar *to, uint length);
03136   uint max_packed_col_length(uint max_length);
03137   enum_field_types real_type() const { return MYSQL_TYPE_STRING; }
03138   bool has_charset(void) const
03139   { return charset() == &my_charset_bin ? FALSE : TRUE; }
03140   Field *new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
03141   Field_string *clone(MEM_ROOT *mem_root) const {
03142     DBUG_ASSERT(real_type() == MYSQL_TYPE_STRING);
03143     return new (mem_root) Field_string(*this);
03144   }
03145   Field_string *clone() const {
03146     DBUG_ASSERT(real_type() == MYSQL_TYPE_STRING);
03147     return new Field_string(*this);
03148   }
03149   virtual uint get_key_image(uchar *buff,uint length, imagetype type);
03150 private:
03151   int do_save_field_metadata(uchar *first_byte);
03152 };
03153 
03154 
03155 class Field_varstring :public Field_longstr {
03156 public:
03157   /*
03158     The maximum space available in a Field_varstring, in bytes. See
03159     length_bytes.
03160   */
03161   static const uint MAX_SIZE;
03162   /* Store number of bytes used to store length (1 or 2) */
03163   uint32 length_bytes;
03164   Field_varstring(uchar *ptr_arg,
03165                   uint32 len_arg, uint length_bytes_arg,
03166                   uchar *null_ptr_arg, uchar null_bit_arg,
03167                   enum utype unireg_check_arg, const char *field_name_arg,
03168                   TABLE_SHARE *share, const CHARSET_INFO *cs)
03169     :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
03170                    unireg_check_arg, field_name_arg, cs),
03171      length_bytes(length_bytes_arg)
03172   {
03173     share->varchar_fields++;
03174   }
03175   Field_varstring(uint32 len_arg,bool maybe_null_arg,
03176                   const char *field_name_arg,
03177                   TABLE_SHARE *share, const CHARSET_INFO *cs)
03178     :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
03179                    NONE, field_name_arg, cs),
03180      length_bytes(len_arg < 256 ? 1 :2)
03181   {
03182     share->varchar_fields++;
03183   }
03184 
03185   enum_field_types type() const { return MYSQL_TYPE_VARCHAR; }
03186   bool match_collation_to_optimize_range() const { return true; }
03187   enum ha_base_keytype key_type() const;
03188   uint row_pack_length() const { return field_length; }
03189   bool zero_pack() const { return 0; }
03190   type_conversion_status reset(void)
03191   {
03192     memset(ptr, 0, field_length+length_bytes);
03193     return TYPE_OK;
03194   }
03195   uint32 pack_length() const { return (uint32) field_length+length_bytes; }
03196   uint32 key_length() const { return (uint32) field_length; }
03197   uint32 sort_length() const
03198   {
03199     return (uint32) field_length + (field_charset == &my_charset_bin ?
03200                                     length_bytes : 0);
03201   }
03202   type_conversion_status store(const char *to,uint length,
03203                                const CHARSET_INFO *charset);
03204   type_conversion_status store(longlong nr, bool unsigned_val);
03205   /* QQ: To be deleted */
03206   type_conversion_status store(double nr) { return Field_str::store(nr); }
03207   double val_real(void);
03208   longlong val_int(void);
03209   String *val_str(String*,String *);
03210   my_decimal *val_decimal(my_decimal *);
03211   int cmp_max(const uchar *, const uchar *, uint max_length);
03212   int cmp(const uchar *a,const uchar *b)
03213   {
03214     return cmp_max(a, b, ~0L);
03215   }
03216   void make_sort_key(uchar *buff, uint length);
03217   uint get_key_image(uchar *buff,uint length, imagetype type);
03218   void set_key_image(const uchar *buff,uint length);
03219   void sql_type(String &str) const;
03220   virtual uchar *pack(uchar *to, const uchar *from,
03221                       uint max_length, bool low_byte_first);
03222   virtual const uchar *unpack(uchar* to, const uchar *from,
03223                               uint param_data, bool low_byte_first);
03224   int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
03225   int key_cmp(const uchar *,const uchar*);
03226   int key_cmp(const uchar *str, uint length);
03227   uint packed_col_length(const uchar *to, uint length);
03228   uint max_packed_col_length(uint max_length);
03229   uint32 data_length();
03230   enum_field_types real_type() const { return MYSQL_TYPE_VARCHAR; }
03231   bool has_charset(void) const
03232   { return charset() == &my_charset_bin ? FALSE : TRUE; }
03233   Field *new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
03234   Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
03235                        uchar *new_ptr, uchar *new_null_ptr,
03236                        uint new_null_bit);
03237   Field_varstring *clone(MEM_ROOT *mem_root) const { 
03238     DBUG_ASSERT(type() == MYSQL_TYPE_VARCHAR);
03239     DBUG_ASSERT(real_type() == MYSQL_TYPE_VARCHAR);
03240     return new (mem_root) Field_varstring(*this);
03241   }
03242   Field_varstring *clone() const {
03243     DBUG_ASSERT(type() == MYSQL_TYPE_VARCHAR);
03244     DBUG_ASSERT(real_type() == MYSQL_TYPE_VARCHAR);
03245     return new Field_varstring(*this);
03246   }
03247   uint is_equal(Create_field *new_field);
03248   void hash(ulong *nr, ulong *nr2);
03249 private:
03250   int do_save_field_metadata(uchar *first_byte);
03251 };
03252 
03253 
03254 class Field_blob :public Field_longstr {
03255   virtual type_conversion_status store_internal(const char *from, uint length,
03256                                                 const CHARSET_INFO *cs);
03260   type_conversion_status store_to_mem(const char *from, uint length,
03261                                       const CHARSET_INFO *cs,
03262                                       uint max_length,
03263                                       Blob_mem_storage *blob_storage);
03264 protected:
03268   uint packlength;
03269   
03273   String value;
03274 
03278   void store_ptr_and_length(const char *from, uint32 length)
03279   {
03280     store_length(length);
03281     bmove(ptr + packlength, &from, sizeof(char *));
03282   }
03283   
03284 public:
03285   Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
03286              enum utype unireg_check_arg, const char *field_name_arg,
03287              TABLE_SHARE *share, uint blob_pack_length, const CHARSET_INFO *cs);
03288   Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
03289              const CHARSET_INFO *cs)
03290     :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
03291                    NONE, field_name_arg, cs),
03292     packlength(4)
03293   {
03294     flags|= BLOB_FLAG;
03295   }
03296   Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
03297              const CHARSET_INFO *cs, bool set_packlength)
03298     :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
03299                    NONE, field_name_arg, cs)
03300   {
03301     flags|= BLOB_FLAG;
03302     packlength= 4;
03303     if (set_packlength)
03304     {
03305       uint32 l_char_length= len_arg/cs->mbmaxlen;
03306       packlength= l_char_length <= 255 ? 1 :
03307                   l_char_length <= 65535 ? 2 :
03308                   l_char_length <= 16777215 ? 3 : 4;
03309     }
03310   }
03311   Field_blob(uint32 packlength_arg)
03312     :Field_longstr((uchar*) 0, 0, (uchar*) "", 0, NONE, "temp", system_charset_info),
03313     packlength(packlength_arg) {}
03314   /* Note that the default copy constructor is used, in clone() */
03315   enum_field_types type() const { return MYSQL_TYPE_BLOB;}
03316   bool match_collation_to_optimize_range() const { return true; }
03317   enum ha_base_keytype key_type() const
03318     { return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2; }
03319   type_conversion_status store(const char *to, uint length,
03320                                const CHARSET_INFO *charset);
03321   type_conversion_status store(double nr);
03322   type_conversion_status store(longlong nr, bool unsigned_val);
03323   double val_real(void);
03324   longlong val_int(void);
03325   String *val_str(String*,String *);
03326   my_decimal *val_decimal(my_decimal *);
03327   int cmp_max(const uchar *, const uchar *, uint max_length);
03328   int cmp(const uchar *a,const uchar *b)
03329     { return cmp_max(a, b, ~0L); }
03330   int cmp(const uchar *a, uint32 a_length, const uchar *b, uint32 b_length);
03331   int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
03332   int key_cmp(const uchar *,const uchar*);
03333   int key_cmp(const uchar *str, uint length);
03334   uint32 key_length() const { return 0; }
03335   void make_sort_key(uchar *buff, uint length);
03336   uint32 pack_length() const
03337   { return (uint32) (packlength + portable_sizeof_char_ptr); }
03338 
03347   uint32 pack_length_no_ptr() const
03348   { return (uint32) (packlength); }
03349   uint row_pack_length() const { return pack_length_no_ptr(); }
03350   uint32 sort_length() const;
03351   virtual uint32 max_data_length() const
03352   {
03353     return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
03354   }
03355   type_conversion_status reset(void)
03356   {
03357     memset(ptr, 0, packlength+sizeof(uchar*));
03358     return TYPE_OK;
03359   }
03360   void reset_fields() { memset(&value, 0, sizeof(value)); }
03361   uint32 get_field_buffer_size(void) { return value.alloced_length(); }
03362 #ifndef WORDS_BIGENDIAN
03363   static
03364 #endif
03365   void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number, bool low_byte_first);
03366   void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number)
03367   {
03368     store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
03369   }
03370   inline void store_length(uint32 number)
03371   {
03372     store_length(ptr, packlength, number);
03373   }
03374   inline uint32 get_length(uint row_offset= 0)
03375   { return get_length(ptr+row_offset, this->packlength, table->s->db_low_byte_first); }
03376   uint32 get_length(const uchar *ptr, uint packlength, bool low_byte_first);
03377   uint32 get_length(const uchar *ptr_arg)
03378   { return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first); }
03379   void put_length(uchar *pos, uint32 length);
03380   inline void get_ptr(uchar **str)
03381     {
03382       memcpy(str, ptr+packlength, sizeof(uchar*));
03383     }
03384   inline void get_ptr(uchar **str, uint row_offset)
03385     {
03386       memcpy(str, ptr+packlength+row_offset, sizeof(char*));
03387     }
03388   inline void set_ptr(uchar *length, uchar *data)
03389     {
03390       memcpy(ptr,length,packlength);
03391       memcpy(ptr+packlength, &data,sizeof(char*));
03392     }
03393   void set_ptr_offset(my_ptrdiff_t ptr_diff, uint32 length, uchar *data)
03394     {
03395       uchar *ptr_ofs= ADD_TO_PTR(ptr,ptr_diff,uchar*);
03396       store_length(ptr_ofs, packlength, length);
03397       memcpy(ptr_ofs+packlength, &data, sizeof(char*));
03398     }
03399   inline void set_ptr(uint32 length, uchar *data)
03400     {
03401       set_ptr_offset(0, length, data);
03402     }
03403   uint get_key_image(uchar *buff,uint length, imagetype type);
03404   void set_key_image(const uchar *buff,uint length);
03405   void sql_type(String &str) const;
03406   inline bool copy()
03407   {
03408     uchar *tmp;
03409     get_ptr(&tmp);
03410     if (value.copy((char*) tmp, get_length(), charset()))
03411     {
03412       Field_blob::reset();
03413       return 1;
03414     }
03415     tmp=(uchar*) value.ptr();
03416     memcpy(ptr+packlength, &tmp, sizeof(char*));
03417     return 0;
03418   }
03419   Field_blob *clone(MEM_ROOT *mem_root) const { 
03420     DBUG_ASSERT(type() == MYSQL_TYPE_BLOB);
03421     return new (mem_root) Field_blob(*this);
03422   }
03423   Field_blob *clone() const {
03424     DBUG_ASSERT(type() == MYSQL_TYPE_BLOB);
03425     return new Field_blob(*this);
03426   }
03427   virtual uchar *pack(uchar *to, const uchar *from,
03428                       uint max_length, bool low_byte_first);
03429   virtual const uchar *unpack(uchar *to, const uchar *from,
03430                               uint param_data, bool low_byte_first);
03431   uint packed_col_length(const uchar *col_ptr, uint length);
03432   uint max_packed_col_length(uint max_length);
03433   void free() { value.free(); }
03434   inline void clear_temporary() { memset(&value, 0, sizeof(value)); }
03435   friend type_conversion_status field_conv(Field *to,Field *from);
03436   bool has_charset(void) const
03437   { return charset() == &my_charset_bin ? FALSE : TRUE; }
03438   uint32 max_display_length();
03439   uint32 char_length();
03440   uint is_equal(Create_field *new_field);
03441   inline bool in_read_set() { return bitmap_is_set(table->read_set, field_index); }
03442   inline bool in_write_set() { return bitmap_is_set(table->write_set, field_index); }
03443 private:
03444   int do_save_field_metadata(uchar *first_byte);
03445 };
03446 
03447 
03448 #ifdef HAVE_SPATIAL
03449 class Field_geom :public Field_blob {
03450   virtual type_conversion_status store_internal(const char *from, uint length,
03451                                                 const CHARSET_INFO *cs);
03452 public:
03453   enum geometry_type geom_type;
03454 
03455   Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
03456              enum utype unireg_check_arg, const char *field_name_arg,
03457              TABLE_SHARE *share, uint blob_pack_length,
03458              enum geometry_type geom_type_arg)
03459      :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, 
03460                  field_name_arg, share, blob_pack_length, &my_charset_bin)
03461   { geom_type= geom_type_arg; }
03462   Field_geom(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
03463              TABLE_SHARE *share, enum geometry_type geom_type_arg)
03464     :Field_blob(len_arg, maybe_null_arg, field_name_arg, &my_charset_bin)
03465   { geom_type= geom_type_arg; }
03466   enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY2; }
03467   enum_field_types type() const { return MYSQL_TYPE_GEOMETRY; }
03468   bool match_collation_to_optimize_range() const { return false; }
03469   void sql_type(String &str) const;
03470   using Field_blob::store;
03471   type_conversion_status store(double nr);
03472   type_conversion_status store(longlong nr, bool unsigned_val);
03473   type_conversion_status store_decimal(const my_decimal *);
03474 
03479   type_conversion_status reset(void)
03480   {
03481     type_conversion_status res= Field_blob::reset();
03482     if (res != TYPE_OK)
03483       return res;
03484     return maybe_null() ? TYPE_OK : TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
03485   }
03486 
03487   geometry_type get_geometry_type() { return geom_type; };
03488   Field_geom *clone(MEM_ROOT *mem_root) const {
03489     DBUG_ASSERT(type() == MYSQL_TYPE_GEOMETRY);
03490     return new (mem_root) Field_geom(*this);
03491   }
03492   Field_geom *clone() const { 
03493     DBUG_ASSERT(type() == MYSQL_TYPE_GEOMETRY);
03494     return new Field_geom(*this);
03495   }
03496   uint is_equal(Create_field *new_field);
03497 };
03498 #endif /*HAVE_SPATIAL*/
03499 
03500 
03501 class Field_enum :public Field_str {
03502 protected:
03503   uint packlength;
03504 public:
03505   TYPELIB *typelib;
03506   Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
03507              uchar null_bit_arg,
03508              enum utype unireg_check_arg, const char *field_name_arg,
03509              uint packlength_arg,
03510              TYPELIB *typelib_arg,
03511              const CHARSET_INFO *charset_arg)
03512     :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
03513                unireg_check_arg, field_name_arg, charset_arg),
03514     packlength(packlength_arg),typelib(typelib_arg)
03515   {
03516     flags|=ENUM_FLAG;
03517   }
03518   Field *new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
03519   enum_field_types type() const { return MYSQL_TYPE_STRING; }
03520   bool match_collation_to_optimize_range() const { return false; }
03521   enum Item_result cmp_type () const { return INT_RESULT; }
03522   enum Item_result cast_to_int_type () const { return INT_RESULT; }
03523   enum ha_base_keytype key_type() const;
03524   type_conversion_status store(const char *to,uint length,
03525                                const CHARSET_INFO *charset);
03526   type_conversion_status store(double nr);
03527   type_conversion_status store(longlong nr, bool unsigned_val);
03528   double val_real(void);
03529   my_decimal *val_decimal(my_decimal *decimal_value);
03530   longlong val_int(void);
03531   String *val_str(String*,String *);
03532   int cmp(const uchar *,const uchar *);
03533   void make_sort_key(uchar *buff, uint length);
03534   uint32 pack_length() const { return (uint32) packlength; }
03535   void store_type(ulonglong value);
03536   void sql_type(String &str) const;
03537   enum_field_types real_type() const { return MYSQL_TYPE_ENUM; }
03538   uint pack_length_from_metadata(uint field_metadata)
03539   { return (field_metadata & 0x00ff); }
03540   uint row_pack_length() const { return pack_length(); }
03541   virtual bool zero_pack() const { return 0; }
03542   bool optimize_range(uint idx, uint part) { return 0; }
03543   bool eq_def(Field *field);
03544   bool has_charset(void) const { return TRUE; }
03545   /* enum and set are sorted as integers */
03546   CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
03547   Field_enum *clone(MEM_ROOT *mem_root) const {
03548     DBUG_ASSERT(real_type() == MYSQL_TYPE_ENUM);
03549     return new (mem_root) Field_enum(*this);
03550   }
03551   Field_enum *clone() const { 
03552     DBUG_ASSERT(real_type() == MYSQL_TYPE_ENUM);
03553     return new Field_enum(*this);
03554   }
03555   virtual uchar *pack(uchar *to, const uchar *from,
03556                       uint max_length, bool low_byte_first);
03557   virtual const uchar *unpack(uchar *to, const uchar *from,
03558                               uint param_data, bool low_byte_first);
03559 
03560 private:
03561   int do_save_field_metadata(uchar *first_byte);
03562   uint is_equal(Create_field *new_field);
03563 };
03564 
03565 
03566 class Field_set :public Field_enum {
03567 public:
03568   Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
03569             uchar null_bit_arg,
03570             enum utype unireg_check_arg, const char *field_name_arg,
03571             uint32 packlength_arg,
03572             TYPELIB *typelib_arg, const CHARSET_INFO *charset_arg)
03573     :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
03574                     unireg_check_arg, field_name_arg,
03575                 packlength_arg,
03576                 typelib_arg,charset_arg),
03577       empty_set_string("", 0, charset_arg)
03578     {
03579       flags= (flags & ~ENUM_FLAG) | SET_FLAG;
03580     }
03581   type_conversion_status store(const char *to, uint length,
03582                                const CHARSET_INFO *charset);
03583   type_conversion_status store(double nr)
03584   {
03585     return Field_set::store((longlong) nr, FALSE);
03586   }
03587   type_conversion_status store(longlong nr, bool unsigned_val);
03588   virtual bool zero_pack() const { return 1; }
03589   String *val_str(String*,String *);
03590   void sql_type(String &str) const;
03591   enum_field_types real_type() const { return MYSQL_TYPE_SET; }
03592   bool has_charset(void) const { return TRUE; }
03593   Field_set *clone(MEM_ROOT *mem_root) const { 
03594     DBUG_ASSERT(real_type() == MYSQL_TYPE_SET);
03595     return new (mem_root) Field_set(*this);
03596   }
03597   Field_set *clone() const {
03598     DBUG_ASSERT(real_type() == MYSQL_TYPE_SET);
03599     return new Field_set(*this);
03600   }
03601 private:
03602   const String empty_set_string;
03603 };
03604 
03605 
03606 /*
03607   Note:
03608     To use Field_bit::cmp_binary() you need to copy the bits stored in
03609     the beginning of the record (the NULL bytes) to each memory you
03610     want to compare (where the arguments point).
03611 
03612     This is the reason:
03613     - Field_bit::cmp_binary() is only implemented in the base class
03614       (Field::cmp_binary()).
03615     - Field::cmp_binary() currenly use pack_length() to calculate how
03616       long the data is.
03617     - pack_length() includes size of the bits stored in the NULL bytes
03618       of the record.
03619 */
03620 class Field_bit :public Field {
03621 public:
03622   uchar *bit_ptr;     // position in record where 'uneven' bits store
03623   uchar bit_ofs;      // offset to 'uneven' high bits
03624   uint bit_len;       // number of 'uneven' high bits
03625   uint bytes_in_rec;
03626   Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
03627             uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
03628             enum utype unireg_check_arg, const char *field_name_arg);
03629   enum_field_types type() const { return MYSQL_TYPE_BIT; }
03630   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BIT; }
03631   uint32 key_length() const { return (uint32) (field_length + 7) / 8; }
03632   uint32 max_data_length() const { return (field_length + 7) / 8; }
03633   uint32 max_display_length() { return field_length; }
03634   Item_result result_type () const { return INT_RESULT; }
03635   type_conversion_status reset(void)
03636   {
03637     memset(ptr, 0, bytes_in_rec);
03638     if (bit_ptr && (bit_len > 0))  // reset odd bits among null bits
03639       clr_rec_bits(bit_ptr, bit_ofs, bit_len);
03640     return TYPE_OK;
03641   }
03642   type_conversion_status store(const char *to, uint length,
03643                                const CHARSET_INFO *charset);
03644   type_conversion_status store(double nr);
03645   type_conversion_status store(longlong nr, bool unsigned_val);
03646   type_conversion_status store_decimal(const my_decimal *);
03647   double val_real(void);
03648   longlong val_int(void);
03649   String *val_str(String*, String *);
03650   virtual bool str_needs_quotes() { return TRUE; }
03651   my_decimal *val_decimal(my_decimal *);
03652   int cmp(const uchar *a, const uchar *b)
03653   {
03654     DBUG_ASSERT(ptr == a || ptr == b);
03655     if (ptr == a)
03656       return Field_bit::key_cmp(b, bytes_in_rec+MY_TEST(bit_len));
03657     else
03658       return Field_bit::key_cmp(a, bytes_in_rec+MY_TEST(bit_len)) * -1;
03659   }
03660   int cmp_binary_offset(uint row_offset)
03661   { return cmp_offset(row_offset); }
03662   int cmp_max(const uchar *a, const uchar *b, uint max_length);
03663   int key_cmp(const uchar *a, const uchar *b)
03664   { return cmp_binary((uchar *) a, (uchar *) b); }
03665   int key_cmp(const uchar *str, uint length);
03666   int cmp_offset(uint row_offset);
03667   void get_image(uchar *buff, uint length, const CHARSET_INFO *cs)
03668   { get_key_image(buff, length, itRAW); }   
03669   void set_image(const uchar *buff,uint length, const CHARSET_INFO *cs)
03670   { Field_bit::store((char *) buff, length, cs); }
03671   uint get_key_image(uchar *buff, uint length, imagetype type);
03672   void set_key_image(const uchar *buff, uint length)
03673   { Field_bit::store((char*) buff, length, &my_charset_bin); }
03674   void make_sort_key(uchar *buff, uint length)
03675   { get_key_image(buff, length, itRAW); }
03676   uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
03677   uint32 pack_length_in_rec() const { return bytes_in_rec; }
03678   uint pack_length_from_metadata(uint field_metadata);
03679   uint row_pack_length() const
03680   { return (bytes_in_rec + ((bit_len > 0) ? 1 : 0)); }
03681   bool compatible_field_size(uint metadata, Relay_log_info *rli,
03682                              uint16 mflags, int *order_var);
03683   void sql_type(String &str) const;
03684   virtual uchar *pack(uchar *to, const uchar *from,
03685                       uint max_length, bool low_byte_first);
03686   virtual const uchar *unpack(uchar *to, const uchar *from,
03687                               uint param_data, bool low_byte_first);
03688   virtual void set_default();
03689 
03690   Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
03691                        uchar *new_ptr, uchar *new_null_ptr,
03692                        uint new_null_bit);
03693   void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg)
03694   {
03695     bit_ptr= bit_ptr_arg;
03696     bit_ofs= bit_ofs_arg;
03697   }
03698   bool eq(Field *field)
03699   {
03700     return (Field::eq(field) &&
03701             bit_ptr == ((Field_bit *)field)->bit_ptr &&
03702             bit_ofs == ((Field_bit *)field)->bit_ofs);
03703   }
03704   uint is_equal(Create_field *new_field);
03705   void move_field_offset(my_ptrdiff_t ptr_diff)
03706   {
03707     Field::move_field_offset(ptr_diff);
03708     bit_ptr= ADD_TO_PTR(bit_ptr, ptr_diff, uchar*);
03709   }
03710   void hash(ulong *nr, ulong *nr2);
03711   Field_bit *clone(MEM_ROOT *mem_root) const { 
03712     DBUG_ASSERT(type() == MYSQL_TYPE_BIT);
03713     return new (mem_root) Field_bit(*this);
03714   }
03715   Field_bit *clone() const {
03716     DBUG_ASSERT(type() == MYSQL_TYPE_BIT);
03717     return new Field_bit(*this);
03718   }
03719 private:
03720   virtual size_t do_last_null_byte() const;
03721   int do_save_field_metadata(uchar *first_byte);
03722 };
03723 
03724 
03732 class Field_bit_as_char: public Field_bit {
03733 public:
03734   Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
03735                     uchar null_bit_arg,
03736                     enum utype unireg_check_arg, const char *field_name_arg);
03737   enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
03738   type_conversion_status store(const char *to, uint length,
03739                                const CHARSET_INFO *charset);
03740   type_conversion_status store(double nr) { return Field_bit::store(nr); }
03741   type_conversion_status store(longlong nr, bool unsigned_val)
03742   { return Field_bit::store(nr, unsigned_val); }
03743   void sql_type(String &str) const;
03744   Field_bit_as_char *clone(MEM_ROOT *mem_root) const { 
03745     return new (mem_root) Field_bit_as_char(*this);
03746   }
03747   Field_bit_as_char *clone() const { return new Field_bit_as_char(*this); }
03748 };
03749 
03750 
03751 /*
03752   Create field class for CREATE TABLE
03753 */
03754 
03755 class Create_field :public Sql_alloc
03756 {
03757 public:
03758   const char *field_name;
03759   const char *change;                   // If done with alter table
03760   const char *after;                    // Put column after this one
03761   LEX_STRING comment;                   // Comment for field
03762 
03771   Item *def;
03772   enum  enum_field_types sql_type;
03773   /*
03774     At various stages in execution this can be length of field in bytes or
03775     max number of characters. 
03776   */
03777   ulong length;
03778   /*
03779     The value of `length' as set by parser: is the number of characters
03780     for most of the types, or of bytes for BLOBs or numeric types.
03781   */
03782   uint32 char_length;
03783   uint  decimals, flags, pack_length, key_length;
03784   Field::utype unireg_check;
03785   TYPELIB *interval;                    // Which interval to use
03786   TYPELIB *save_interval;               // Temporary copy for the above
03787                                         // Used only for UCS2 intervals
03788   List<String> interval_list;
03789   const CHARSET_INFO *charset;
03790   Field::geometry_type geom_type;
03791   Field *field;                         // For alter table
03792 
03793   uint8 row,col,sc_length,interval_id;  // For rea_create_table
03794   uint  offset,pack_flag;
03795   Create_field() :after(NULL) {}
03796   Create_field(Field *field, Field *orig_field);
03797   /* Used to make a clone of this object for ALTER/CREATE TABLE */
03798   Create_field *clone(MEM_ROOT *mem_root) const
03799     { return new (mem_root) Create_field(*this); }
03800   void create_length_to_internal_length(void);
03801 
03802   /* Init for a tmp table field. To be extended if need be. */
03803   void init_for_tmp_table(enum_field_types sql_type_arg,
03804                           uint32 max_length, uint32 decimals,
03805                           bool maybe_null, bool is_unsigned,
03806                           uint pack_length = ~0U);
03807 
03808   bool init(THD *thd, const char *field_name, enum_field_types type,
03809             const char *length, const char *decimals, uint type_modifier,
03810             Item *default_value, Item *on_update_value, LEX_STRING *comment,
03811             const char *change, List<String> *interval_list,
03812             const CHARSET_INFO *cs, uint uint_geom_type);
03813 
03814   ha_storage_media field_storage_type() const
03815   {
03816     return (ha_storage_media)
03817       ((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
03818   }
03819 
03820   column_format_type column_format() const
03821   {
03822     return (column_format_type)
03823       ((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
03824   }
03825 };
03826 
03827 
03828 /*
03829   A class for sending info to the client
03830 */
03831 
03832 class Send_field :public Sql_alloc {
03833  public:
03834   const char *db_name;
03835   const char *table_name,*org_table_name;
03836   const char *col_name,*org_col_name;
03837   ulong length;
03838   uint charsetnr, flags, decimals;
03839   enum_field_types type;
03840   Send_field() {}
03841 };
03842 
03843 
03844 /*
03845   A class for quick copying data to fields
03846 */
03847 
03848 class Copy_field :public Sql_alloc {
03853   typedef void Copy_func(Copy_field*);
03854   Copy_func *get_copy_func(Field *to, Field *from);
03855 public:
03856   uchar *from_ptr,*to_ptr;
03857   uchar *from_null_ptr,*to_null_ptr;
03858   my_bool *null_row;
03859   uint  from_bit,to_bit;
03877   uint from_length,to_length;
03878   Field *from_field,*to_field;
03879   String tmp;                                   // For items
03880 
03881   Copy_field() {}
03882   ~Copy_field() {}
03883   void set(Field *to,Field *from,bool save);    // Field to field 
03884   void set(uchar *to,Field *from);              // Field to string
03885   void (*do_copy)(Copy_field *);
03886   void (*do_copy2)(Copy_field *);               // Used to handle null values
03887 };
03888 
03889 
03890 Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
03891                   uchar *null_pos, uchar null_bit,
03892                   uint pack_flag, enum_field_types field_type,
03893                   const CHARSET_INFO *cs,
03894                   Field::geometry_type geom_type,
03895                   Field::utype unireg_check,
03896                   TYPELIB *interval, const char *field_name);
03897 uint pack_length_to_packflag(uint type);
03898 enum_field_types get_blob_type_from_length(ulong length);
03899 uint32 calc_pack_length(enum_field_types type,uint32 length);
03900 type_conversion_status set_field_to_null(Field *field);
03901 type_conversion_status set_field_to_null_with_conversions(Field *field,
03902                                                           bool no_conversions);
03903 
03904 /*
03905   The following are for the interface with the .frm file
03906 */
03907 
03908 #define FIELDFLAG_DECIMAL               1
03909 #define FIELDFLAG_BINARY                1       // Shares same flag
03910 #define FIELDFLAG_NUMBER                2
03911 #define FIELDFLAG_ZEROFILL              4
03912 #define FIELDFLAG_PACK                  120     // Bits used for packing
03913 #define FIELDFLAG_INTERVAL              256     // mangled with decimals!
03914 #define FIELDFLAG_BITFIELD              512     // mangled with decimals!
03915 #define FIELDFLAG_BLOB                  1024    // mangled with decimals!
03916 #define FIELDFLAG_GEOM                  2048    // mangled with decimals!
03917 
03918 #define FIELDFLAG_TREAT_BIT_AS_CHAR     4096    /* use Field_bit_as_char */
03919 
03920 #define FIELDFLAG_LEFT_FULLSCREEN       8192
03921 #define FIELDFLAG_RIGHT_FULLSCREEN      16384
03922 #define FIELDFLAG_FORMAT_NUMBER         16384   // predit: ###,,## in output
03923 #define FIELDFLAG_NO_DEFAULT            16384   /* sql */
03924 #define FIELDFLAG_SUM                   ((uint) 32768)// predit: +#fieldflag
03925 #define FIELDFLAG_MAYBE_NULL            ((uint) 32768)// sql
03926 #define FIELDFLAG_HEX_ESCAPE            ((uint) 0x10000)
03927 #define FIELDFLAG_PACK_SHIFT            3
03928 #define FIELDFLAG_DEC_SHIFT             8
03929 #define FIELDFLAG_MAX_DEC               31
03930 #define FIELDFLAG_NUM_SCREEN_TYPE       0x7F01
03931 #define FIELDFLAG_ALFA_SCREEN_TYPE      0x7800
03932 
03933 #define MTYP_TYPENR(type) (type & 127)  /* Remove bits from type */
03934 
03935 #define f_is_dec(x)             ((x) & FIELDFLAG_DECIMAL)
03936 #define f_is_num(x)             ((x) & FIELDFLAG_NUMBER)
03937 #define f_is_zerofill(x)        ((x) & FIELDFLAG_ZEROFILL)
03938 #define f_is_packed(x)          ((x) & FIELDFLAG_PACK)
03939 #define f_packtype(x)           (((x) >> FIELDFLAG_PACK_SHIFT) & 15)
03940 #define f_decimals(x)           ((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
03941 #define f_is_alpha(x)           (!f_is_num(x))
03942 #define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
03943 #define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
03944 #define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
03945 #define f_is_blob(x)            (((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
03946 #define f_is_geom(x)            (((x) & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM)
03947 #define f_is_equ(x)             ((x) & (1+2+FIELDFLAG_PACK+31*256))
03948 #define f_settype(x)            (((int) x) << FIELDFLAG_PACK_SHIFT)
03949 #define f_maybe_null(x)         (x & FIELDFLAG_MAYBE_NULL)
03950 #define f_no_default(x)         (x & FIELDFLAG_NO_DEFAULT)
03951 #define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
03952 #define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)
03953 
03954 #endif /* FIELD_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines