1 /* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 
24 /**
25   @file
26 
27   @brief
28   Functions to copy data to or from fields
29 
30     This could be done with a single short function but opencoding this
31     gives much more speed.
32 */
33 
34 #include "sql_priv.h"
35 #include "sql_class.h"                          // THD
36 #include "sql_time.h"
37 #include <m_ctype.h>
38 
do_field_eq(Copy_field * copy)39 static void do_field_eq(Copy_field *copy)
40 {
41   memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
42 }
43 
do_field_1(Copy_field * copy)44 static void do_field_1(Copy_field *copy)
45 {
46   copy->to_ptr[0]=copy->from_ptr[0];
47 }
48 
do_field_2(Copy_field * copy)49 static void do_field_2(Copy_field *copy)
50 {
51   copy->to_ptr[0]=copy->from_ptr[0];
52   copy->to_ptr[1]=copy->from_ptr[1];
53 }
54 
do_field_3(Copy_field * copy)55 static void do_field_3(Copy_field *copy)
56 {
57   copy->to_ptr[0]=copy->from_ptr[0];
58   copy->to_ptr[1]=copy->from_ptr[1];
59   copy->to_ptr[2]=copy->from_ptr[2];
60 }
61 
do_field_4(Copy_field * copy)62 static void do_field_4(Copy_field *copy)
63 {
64   copy->to_ptr[0]=copy->from_ptr[0];
65   copy->to_ptr[1]=copy->from_ptr[1];
66   copy->to_ptr[2]=copy->from_ptr[2];
67   copy->to_ptr[3]=copy->from_ptr[3];
68 }
69 
do_field_6(Copy_field * copy)70 static void do_field_6(Copy_field *copy)
71 {						// For blob field
72   copy->to_ptr[0]=copy->from_ptr[0];
73   copy->to_ptr[1]=copy->from_ptr[1];
74   copy->to_ptr[2]=copy->from_ptr[2];
75   copy->to_ptr[3]=copy->from_ptr[3];
76   copy->to_ptr[4]=copy->from_ptr[4];
77   copy->to_ptr[5]=copy->from_ptr[5];
78 }
79 
do_field_8(Copy_field * copy)80 static void do_field_8(Copy_field *copy)
81 {
82   copy->to_ptr[0]=copy->from_ptr[0];
83   copy->to_ptr[1]=copy->from_ptr[1];
84   copy->to_ptr[2]=copy->from_ptr[2];
85   copy->to_ptr[3]=copy->from_ptr[3];
86   copy->to_ptr[4]=copy->from_ptr[4];
87   copy->to_ptr[5]=copy->from_ptr[5];
88   copy->to_ptr[6]=copy->from_ptr[6];
89   copy->to_ptr[7]=copy->from_ptr[7];
90 }
91 
do_field_to_null_str(Copy_field * copy)92 static void do_field_to_null_str(Copy_field *copy)
93 {
94   if (*copy->null_row ||
95       (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
96   {
97     memset(copy->to_ptr, 0, copy->from_length);
98     copy->to_null_ptr[0]=1;			// Always bit 1
99   }
100   else
101   {
102     copy->to_null_ptr[0]=0;
103     memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
104   }
105 }
106 
107 
set_field_to_null(Field * field)108 type_conversion_status set_field_to_null(Field *field)
109 {
110   if (field->real_maybe_null())
111   {
112     field->set_null();
113     field->reset();
114     return TYPE_OK;
115   }
116   field->reset();
117   switch (field->table->in_use->count_cuted_fields) {
118   case CHECK_FIELD_WARN:
119     field->set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
120     /* fall through */
121   case CHECK_FIELD_IGNORE:
122     return TYPE_OK;
123   case CHECK_FIELD_ERROR_FOR_NULL:
124     if (!field->table->in_use->no_errors)
125       my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
126     return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
127   }
128   DBUG_ASSERT(false); // impossible
129   return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
130 }
131 
132 
133 /**
134   Set field to NULL or TIMESTAMP or to next auto_increment number.
135 
136   @param field           Field to update
137   @param no_conversions  Set to 1 if we should return 1 if field can't
138                          take null values.
139                          If set to 0 we will do store the 'default value'
140                          if the field is a special field. If not we will
141                          give an error.
142 
143   @retval
144     0    Field could take 0 or an automatic conversion was used
145   @retval
146     -1   Field could not take NULL and no conversion was used.
147     If no_conversion was not set, an error message is printed
148 */
149 
150 type_conversion_status
set_field_to_null_with_conversions(Field * field,bool no_conversions)151 set_field_to_null_with_conversions(Field *field, bool no_conversions)
152 {
153   if (field->real_maybe_null())
154   {
155     field->set_null();
156     field->reset();
157     return TYPE_OK;
158   }
159   if (no_conversions)
160     return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
161 
162   /*
163     Check if this is a special type, which will get a special walue
164     when set to NULL (TIMESTAMP fields which allow setting to NULL
165     are handled by first check).
166 
167     From the manual:
168 
169     TIMESTAMP columns [...] assigning NULL assigns the current timestamp.
170 
171     But if explicit_defaults_for_timestamp, use standard-compliant behaviour:
172     no special value.
173   */
174   if (field->type() == MYSQL_TYPE_TIMESTAMP &&
175       !field->table->in_use->variables.explicit_defaults_for_timestamp)
176   {
177     Item_func_now_local::store_in(field);
178     return TYPE_OK;			// Ok to set time to NULL
179   }
180 
181   // Note: we ignore any potential failure of reset() here.
182   field->reset();
183 
184   if (field == field->table->next_number_field)
185   {
186     field->table->auto_increment_field_not_null= FALSE;
187     return TYPE_OK;		        // field is set in fill_record()
188   }
189   switch (field->table->in_use->count_cuted_fields) {
190   case CHECK_FIELD_WARN:
191     field->set_warning(Sql_condition::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
192     /* fall through */
193   case CHECK_FIELD_IGNORE:
194     return TYPE_OK;
195   case CHECK_FIELD_ERROR_FOR_NULL:
196     if (!field->table->in_use->no_errors)
197       my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
198     return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
199   }
200   DBUG_ASSERT(false); // impossible
201   return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
202 }
203 
204 
do_skip(Copy_field * copy MY_ATTRIBUTE ((unused)))205 static void do_skip(Copy_field *copy MY_ATTRIBUTE((unused)))
206 {
207 }
208 
209 
do_copy_null(Copy_field * copy)210 static void do_copy_null(Copy_field *copy)
211 {
212   if (*copy->null_row ||
213       (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
214   {
215     *copy->to_null_ptr|=copy->to_bit;
216     copy->to_field->reset();
217   }
218   else
219   {
220     *copy->to_null_ptr&= ~copy->to_bit;
221     (copy->do_copy2)(copy);
222   }
223 }
224 
225 
do_copy_not_null(Copy_field * copy)226 static void do_copy_not_null(Copy_field *copy)
227 {
228   if (*copy->null_row ||
229       (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
230   {
231     copy->to_field->set_warning(Sql_condition::WARN_LEVEL_WARN,
232                                 WARN_DATA_TRUNCATED, 1);
233     copy->to_field->reset();
234   }
235   else
236     (copy->do_copy2)(copy);
237 }
238 
239 
do_copy_maybe_null(Copy_field * copy)240 static void do_copy_maybe_null(Copy_field *copy)
241 {
242   *copy->to_null_ptr&= ~copy->to_bit;
243   (copy->do_copy2)(copy);
244 }
245 
246 /* timestamp and next_number has special handling in case of NULL values */
247 
do_copy_timestamp(Copy_field * copy)248 static void do_copy_timestamp(Copy_field *copy)
249 {
250   if (*copy->null_row || (*copy->from_null_ptr & copy->from_bit))
251   {
252     /* Same as in set_field_to_null_with_conversions() */
253     Item_func_now_local::store_in(copy->to_field);
254   }
255   else
256     (copy->do_copy2)(copy);
257 }
258 
259 
do_copy_next_number(Copy_field * copy)260 static void do_copy_next_number(Copy_field *copy)
261 {
262   if (*copy->null_row || (*copy->from_null_ptr & copy->from_bit))
263   {
264     /* Same as in set_field_to_null_with_conversions() */
265     copy->to_field->table->auto_increment_field_not_null= FALSE;
266     copy->to_field->reset();
267   }
268   else
269     (copy->do_copy2)(copy);
270 }
271 
272 
do_copy_blob(Copy_field * copy)273 static void do_copy_blob(Copy_field *copy)
274 {
275   ulong from_length=((Field_blob*) copy->from_field)->get_length();
276   ((Field_blob*) copy->to_field)->store_length(from_length);
277   memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
278   ulong to_length=((Field_blob*) copy->to_field)->get_length();
279   if (to_length < from_length)
280   {
281     if (copy->to_field->table->in_use->abort_on_warning)
282     {
283       copy->to_field->set_warning(Sql_condition::WARN_LEVEL_WARN,
284                                   ER_DATA_TOO_LONG, 1);
285     }
286     else
287     {
288       copy->to_field->set_warning(Sql_condition::WARN_LEVEL_WARN,
289                                   WARN_DATA_TRUNCATED, 1);
290     }
291   }
292 }
293 
do_conv_blob(Copy_field * copy)294 static void do_conv_blob(Copy_field *copy)
295 {
296   copy->from_field->val_str(&copy->tmp);
297   ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
298 					 copy->tmp.length(),
299 					 copy->tmp.charset());
300 }
301 
302 /** Save blob in copy->tmp for GROUP BY. */
303 
do_save_blob(Copy_field * copy)304 static void do_save_blob(Copy_field *copy)
305 {
306   char buff[MAX_FIELD_WIDTH];
307   String res(buff,sizeof(buff),copy->tmp.charset());
308   copy->from_field->val_str(&res);
309   copy->tmp.copy(res);
310   ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
311 					 copy->tmp.length(),
312 					 copy->tmp.charset());
313 }
314 
315 
do_field_string(Copy_field * copy)316 static void do_field_string(Copy_field *copy)
317 {
318   char buff[MAX_FIELD_WIDTH];
319   String res(buff, sizeof(buff), copy->from_field->charset());
320   res.length(0U);
321 
322   copy->from_field->val_str(&res);
323   copy->to_field->store(res.c_ptr_quick(), res.length(), res.charset());
324 }
325 
326 
do_field_enum(Copy_field * copy)327 static void do_field_enum(Copy_field *copy)
328 {
329   if (copy->from_field->val_int() == 0)
330     ((Field_enum *) copy->to_field)->store_type((ulonglong) 0);
331   else
332     do_field_string(copy);
333 }
334 
335 
do_field_varbinary_pre50(Copy_field * copy)336 static void do_field_varbinary_pre50(Copy_field *copy)
337 {
338   char buff[MAX_FIELD_WIDTH];
339   copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
340   copy->from_field->val_str(&copy->tmp);
341 
342   /* Use the same function as in 4.1 to trim trailing spaces */
343   uint length= my_lengthsp_8bit(&my_charset_bin, copy->tmp.c_ptr_quick(),
344                                 copy->from_field->field_length);
345 
346   copy->to_field->store(copy->tmp.c_ptr_quick(), length,
347                         copy->tmp.charset());
348 }
349 
350 
do_field_int(Copy_field * copy)351 static void do_field_int(Copy_field *copy)
352 {
353   longlong value= copy->from_field->val_int();
354   copy->to_field->store(value,
355                         MY_TEST(copy->from_field->flags & UNSIGNED_FLAG));
356 }
357 
do_field_real(Copy_field * copy)358 static void do_field_real(Copy_field *copy)
359 {
360   double value=copy->from_field->val_real();
361   copy->to_field->store(value);
362 }
363 
364 
do_field_decimal(Copy_field * copy)365 static void do_field_decimal(Copy_field *copy)
366 {
367   my_decimal value;
368   copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
369 }
370 
371 
copy_time_to_time(Field * from,Field * to)372 inline type_conversion_status copy_time_to_time(Field *from, Field *to)
373 {
374   MYSQL_TIME ltime;
375   from->get_time(&ltime);
376   return to->store_time(&ltime);
377 }
378 
379 /**
380   Convert between fields using time representation.
381 */
do_field_time(Copy_field * copy)382 static void do_field_time(Copy_field *copy)
383 {
384   (void) copy_time_to_time(copy->from_field, copy->to_field);
385 }
386 
387 
388 /**
389   string copy for single byte characters set when to string is shorter than
390   from string.
391 */
392 
do_cut_string(Copy_field * copy)393 static void do_cut_string(Copy_field *copy)
394 {
395   const CHARSET_INFO *cs= copy->from_field->charset();
396   memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
397 
398   /* Check if we loosed any important characters */
399   if (cs->cset->scan(cs,
400                      (char*) copy->from_ptr + copy->to_length,
401                      (char*) copy->from_ptr + copy->from_length,
402                      MY_SEQ_SPACES) < copy->from_length - copy->to_length)
403   {
404     copy->to_field->set_warning(Sql_condition::WARN_LEVEL_WARN,
405                                 WARN_DATA_TRUNCATED, 1);
406   }
407 }
408 
409 
410 /**
411   string copy for multi byte characters set when to string is shorter than
412   from string.
413 */
414 
do_cut_string_complex(Copy_field * copy)415 static void do_cut_string_complex(Copy_field *copy)
416 {						// Shorter string field
417   int well_formed_error;
418   const CHARSET_INFO *cs= copy->from_field->charset();
419   const uchar *from_end= copy->from_ptr + copy->from_length;
420   uint copy_length= cs->cset->well_formed_len(cs,
421                                               (char*) copy->from_ptr,
422                                               (char*) from_end,
423                                               copy->to_length / cs->mbmaxlen,
424                                               &well_formed_error);
425   if (copy->to_length < copy_length)
426     copy_length= copy->to_length;
427   memcpy(copy->to_ptr, copy->from_ptr, copy_length);
428 
429   /* Check if we lost any important characters */
430   if (well_formed_error ||
431       cs->cset->scan(cs, (char*) copy->from_ptr + copy_length,
432                      (char*) from_end,
433                      MY_SEQ_SPACES) < (copy->from_length - copy_length))
434   {
435     copy->to_field->set_warning(Sql_condition::WARN_LEVEL_WARN,
436                                 WARN_DATA_TRUNCATED, 1);
437   }
438 
439   if (copy_length < copy->to_length)
440     cs->cset->fill(cs, (char*) copy->to_ptr + copy_length,
441                    copy->to_length - copy_length, ' ');
442 }
443 
444 
445 
446 
do_expand_binary(Copy_field * copy)447 static void do_expand_binary(Copy_field *copy)
448 {
449   const CHARSET_INFO *cs= copy->from_field->charset();
450   memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
451   cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
452                      copy->to_length-copy->from_length, '\0');
453 }
454 
455 
456 
do_expand_string(Copy_field * copy)457 static void do_expand_string(Copy_field *copy)
458 {
459   const CHARSET_INFO *cs= copy->from_field->charset();
460   memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
461   cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
462                      copy->to_length-copy->from_length, ' ');
463 }
464 
465 /**
466   Find how many bytes should be copied between Field_varstring fields
467   so that only the bytes in use in the 'from' field are copied.
468   Handles single and multi-byte charsets. Adds warning if not all
469   bytes in 'from' will fit into 'to'.
470 
471   @param to   Variable length field we're copying to
472   @param from Variable length field we're copying from
473 
474   @return Number of bytes that should be copied from 'from' to 'to'.
475 */
get_varstring_copy_length(Field_varstring * to,const Field_varstring * from)476 static uint get_varstring_copy_length(Field_varstring *to,
477                                       const Field_varstring *from)
478 {
479   const CHARSET_INFO * const cs= from->charset();
480   const bool is_multibyte_charset= (cs->mbmaxlen != 1);
481   const uint to_byte_length= to->row_pack_length();
482 
483   uint bytes_to_copy;
484   if (from->length_bytes == 1)
485     bytes_to_copy= *from->ptr;
486   else
487     bytes_to_copy= uint2korr(from->ptr);
488 
489   if (is_multibyte_charset)
490   {
491     int well_formed_error;
492     const char *from_beg= reinterpret_cast<char*>(from->ptr + from->length_bytes);
493     const uint to_char_length= (to_byte_length) / cs->mbmaxlen;
494     const uint from_byte_length= bytes_to_copy;
495     bytes_to_copy=
496       cs->cset->well_formed_len(cs, from_beg,
497                                 from_beg + from_byte_length,
498                                 to_char_length,
499                                 &well_formed_error);
500     if (bytes_to_copy < from_byte_length)
501     {
502       if (from->table->in_use->count_cuted_fields)
503         to->set_warning(Sql_condition::WARN_LEVEL_WARN,
504                         WARN_DATA_TRUNCATED, 1);
505     }
506   }
507   else
508   {
509     if (bytes_to_copy > (to_byte_length))
510     {
511       bytes_to_copy= to_byte_length;
512       if (from->table->in_use->count_cuted_fields)
513         to->set_warning(Sql_condition::WARN_LEVEL_WARN,
514                         WARN_DATA_TRUNCATED, 1);
515     }
516   }
517   return bytes_to_copy;
518 }
519 
520 /**
521   A variable length string field consists of:
522    (a) 1 or 2 length bytes, depending on the VARCHAR column definition
523    (b) as many relevant character bytes, as defined in the length byte(s)
524    (c) unused padding up to the full length of the column
525 
526   This function only copies (a) and (b)
527 
528   Condition for using this function: to and from must use the same
529   number of bytes for length, i.e: to->length_bytes==from->length_bytes
530 
531   @param to   Variable length field we're copying to
532   @param from Variable length field we're copying from
533 */
copy_field_varstring(Field_varstring * const to,const Field_varstring * const from)534 static void copy_field_varstring(Field_varstring * const to,
535                                  const Field_varstring * const from)
536 {
537   const uint length_bytes= from->length_bytes;
538   DBUG_ASSERT(length_bytes == to->length_bytes);
539   DBUG_ASSERT(length_bytes == 1 || length_bytes == 2);
540 
541   const uint bytes_to_copy= get_varstring_copy_length(to, from);
542   if (length_bytes == 1)
543     *to->ptr= static_cast<uchar>(bytes_to_copy);
544   else
545     int2store(to->ptr, bytes_to_copy);
546 
547   // memcpy should not be used for overlaping memory blocks
548   DBUG_ASSERT(to->ptr != from->ptr);
549   memcpy(to->ptr + length_bytes, from->ptr + length_bytes, bytes_to_copy);
550 }
551 
do_varstring(Copy_field * copy)552 static void do_varstring(Copy_field *copy)
553 {
554   copy_field_varstring(static_cast<Field_varstring*>(copy->to_field),
555                        static_cast<Field_varstring*>(copy->from_field));
556 }
557 
558 
559 /***************************************************************************
560 ** The different functions that fills in a Copy_field class
561 ***************************************************************************/
562 
563 /**
564   copy of field to maybe null string.
565   If field is null then the all bytes are set to 0.
566   if field is not null then the first byte is set to 1 and the rest of the
567   string is the field value.
568   The 'to' buffer should have a size of field->pack_length()+1
569 */
570 
set(uchar * to,Field * from)571 void Copy_field::set(uchar *to,Field *from)
572 {
573   from_ptr=from->ptr;
574   to_ptr=to;
575   from_length=from->pack_length();
576   null_row= &from->table->null_row;
577   if (from->maybe_null())
578   {
579     from_null_ptr=from->null_ptr;
580     from_bit=	  from->null_bit;
581     to_ptr[0]=	  1;				// Null as default value
582     to_null_ptr=  (uchar*) to_ptr++;
583     to_bit=	  1;
584     do_copy=	  do_field_to_null_str;
585   }
586   else
587   {
588     to_null_ptr=  0;				// For easy debugging
589     do_copy=	  do_field_eq;
590   }
591 }
592 
593 
594 /*
595   To do:
596 
597   If 'save' is set to true and the 'from' is a blob field, do_copy is set to
598   do_save_blob rather than do_conv_blob.  The only differences between them
599   appears to be:
600 
601   - do_save_blob allocates and uses an intermediate buffer before calling
602     Field_blob::store. Is this in order to trigger the call to
603     well_formed_copy_nchars, by changing the pointer copy->tmp.ptr()?
604     That call will take place anyway in all known cases.
605  */
set(Field * to,Field * from,bool save)606 void Copy_field::set(Field *to,Field *from,bool save)
607 {
608   if (to->type() == MYSQL_TYPE_NULL)
609   {
610     to_null_ptr=0;				// For easy debugging
611     to_ptr=0;
612     do_copy=do_skip;
613     return;
614   }
615   from_field=from;
616   to_field=to;
617   from_ptr=from->ptr;
618   from_length=from->pack_length();
619   to_ptr=  to->ptr;
620   to_length=to_field->pack_length();
621 
622   // set up null handling
623   from_null_ptr=to_null_ptr=0;
624   null_row= &from->table->null_row;
625   if (from->maybe_null())
626   {
627     from_null_ptr=	from->null_ptr;
628     from_bit=		from->null_bit;
629     if (to_field->real_maybe_null())
630     {
631       to_null_ptr=	to->null_ptr;
632       to_bit=		to->null_bit;
633       do_copy=	do_copy_null;
634     }
635     else
636     {
637       if (to_field->type() == MYSQL_TYPE_TIMESTAMP)
638         do_copy= do_copy_timestamp;               // Automatic timestamp
639       else if (to_field == to_field->table->next_number_field)
640         do_copy= do_copy_next_number;
641       else
642         do_copy= do_copy_not_null;
643     }
644   }
645   else if (to_field->real_maybe_null())
646   {
647     to_null_ptr=	to->null_ptr;
648     to_bit=		to->null_bit;
649     do_copy= do_copy_maybe_null;
650   }
651   else
652    do_copy=0;
653 
654   if ((to->flags & BLOB_FLAG) && save)
655     do_copy2= do_save_blob;
656   else
657     do_copy2= get_copy_func(to,from);
658   if (!do_copy)					// Not null
659     do_copy=do_copy2;
660 }
661 
662 
663 Copy_field::Copy_func *
get_copy_func(Field * to,Field * from)664 Copy_field::get_copy_func(Field *to,Field *from)
665 {
666   bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
667                                      from->table->s->db_low_byte_first);
668   if (to->flags & BLOB_FLAG)
669   {
670     if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
671       return do_conv_blob;
672     if (from_length != to_length || !compatible_db_low_byte_first)
673     {
674       // Correct pointer to point at char pointer
675       to_ptr+=   to_length - portable_sizeof_char_ptr;
676       from_ptr+= from_length - portable_sizeof_char_ptr;
677       return do_copy_blob;
678     }
679   }
680   else
681   {
682     if (to->real_type() == MYSQL_TYPE_BIT ||
683         from->real_type() == MYSQL_TYPE_BIT)
684       return do_field_int;
685     if (to->result_type() == DECIMAL_RESULT)
686       return do_field_decimal;
687     // Check if identical fields
688     if (from->result_type() == STRING_RESULT)
689     {
690       if (from->is_temporal())
691       {
692         if (to->is_temporal())
693         {
694           return do_field_time;
695         }
696         else
697         {
698           if (to->result_type() == INT_RESULT)
699             return do_field_int;
700           if (to->result_type() == REAL_RESULT)
701             return do_field_real;
702           /* Note: conversion from any to DECIMAL_RESULT is handled earlier */
703         }
704       }
705       /*
706         Detect copy from pre 5.0 varbinary to varbinary as of 5.0 and
707         use special copy function that removes trailing spaces and thus
708         repairs data.
709       */
710       if (from->type() == MYSQL_TYPE_VAR_STRING && !from->has_charset() &&
711           to->type() == MYSQL_TYPE_VARCHAR && !to->has_charset())
712         return do_field_varbinary_pre50;
713 
714       /*
715         If we are copying date or datetime's we have to check the dates
716         if we don't allow 'all' dates.
717       */
718       if (to->real_type() != from->real_type() ||
719           to->decimals() != from->decimals() /* e.g. TIME vs TIME(6) */ ||
720           !compatible_db_low_byte_first ||
721           (((to->table->in_use->variables.sql_mode &
722             (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) &&
723            to->type() == MYSQL_TYPE_DATE) ||
724            to->type() == MYSQL_TYPE_DATETIME))
725       {
726 	if (from->real_type() == MYSQL_TYPE_ENUM ||
727 	    from->real_type() == MYSQL_TYPE_SET)
728 	  if (to->result_type() != STRING_RESULT)
729 	    return do_field_int;		// Convert SET to number
730 	return do_field_string;
731       }
732       if (to->real_type() == MYSQL_TYPE_ENUM ||
733 	  to->real_type() == MYSQL_TYPE_SET)
734       {
735 	if (!to->eq_def(from))
736         {
737           if (from->real_type() == MYSQL_TYPE_ENUM &&
738               to->real_type() == MYSQL_TYPE_ENUM)
739             return do_field_enum;
740           else
741             return do_field_string;
742         }
743       }
744       else if (to->charset() != from->charset())
745 	return do_field_string;
746       else if (to->real_type() == MYSQL_TYPE_VARCHAR)
747       {
748         if (((Field_varstring*) to)->length_bytes !=
749             ((Field_varstring*) from)->length_bytes)
750           return do_field_string;
751         else
752           return do_varstring;
753       }
754       else if (to_length < from_length)
755 	return (from->charset()->mbmaxlen == 1 ?
756                 do_cut_string : do_cut_string_complex);
757       else if (to_length > from_length)
758       {
759         if (to->charset() == &my_charset_bin)
760           return do_expand_binary;
761         else
762           return do_expand_string;
763       }
764 
765     }
766     else if (to->real_type() != from->real_type() ||
767 	     to_length != from_length ||
768              !compatible_db_low_byte_first)
769     {
770       if (to->real_type() == MYSQL_TYPE_DECIMAL ||
771 	  to->result_type() == STRING_RESULT)
772 	return do_field_string;
773       if (to->result_type() == INT_RESULT)
774 	return do_field_int;
775       return do_field_real;
776     }
777     else
778     {
779       if (!to->eq_def(from) || !compatible_db_low_byte_first)
780       {
781 	if (to->real_type() == MYSQL_TYPE_DECIMAL)
782 	  return do_field_string;
783 	if (to->result_type() == INT_RESULT)
784 	  return do_field_int;
785 	else
786 	  return do_field_real;
787       }
788     }
789   }
790     /* Eq fields */
791   switch (to_length) {
792   case 1: return do_field_1;
793   case 2: return do_field_2;
794   case 3: return do_field_3;
795   case 4: return do_field_4;
796   case 6: return do_field_6;
797   case 8: return do_field_8;
798   }
799   return do_field_eq;
800 }
801 
802 
803 /** Simple quick field convert that is called on insert. */
804 
field_conv(Field * to,Field * from)805 type_conversion_status field_conv(Field *to,Field *from)
806 {
807   if (to->real_type() == from->real_type() &&
808       !(to->type() == MYSQL_TYPE_BLOB && to->table->copy_blobs) &&
809       to->charset() == from->charset())
810   {
811     if (to->real_type() == MYSQL_TYPE_VARCHAR &&
812         from->real_type() == MYSQL_TYPE_VARCHAR)
813     {
814       Field_varstring *to_vc= static_cast<Field_varstring*>(to);
815       const Field_varstring *from_vc= static_cast<Field_varstring*>(from);
816       if (to_vc->length_bytes == from_vc->length_bytes)
817       {
818         copy_field_varstring(to_vc, from_vc);
819         return TYPE_OK;
820       }
821     }
822     if (to->pack_length() == from->pack_length() &&
823         !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
824 	to->real_type() != MYSQL_TYPE_ENUM &&
825 	to->real_type() != MYSQL_TYPE_SET &&
826         to->real_type() != MYSQL_TYPE_BIT &&
827         (!to->is_temporal_with_time() ||
828          to->decimals() == from->decimals()) &&
829         (to->real_type() != MYSQL_TYPE_NEWDECIMAL ||
830          (to->field_length == from->field_length &&
831           (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
832 	to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
833         (!(to->table->in_use->variables.sql_mode &
834            (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) ||
835          (to->type() != MYSQL_TYPE_DATE &&
836           to->type() != MYSQL_TYPE_DATETIME &&
837           (!to->table->in_use->variables.explicit_defaults_for_timestamp ||
838            to->type() != MYSQL_TYPE_TIMESTAMP))) &&
839          (from->real_type() != MYSQL_TYPE_VARCHAR))
840     {						// Identical fields
841       // to->ptr==from->ptr may happen if one does 'UPDATE ... SET x=x'
842       memmove(to->ptr, from->ptr, to->pack_length());
843       return TYPE_OK;
844     }
845   }
846   if (to->type() == MYSQL_TYPE_BLOB)
847   {						// Be sure the value is stored
848     Field_blob *blob=(Field_blob*) to;
849     from->val_str(&blob->value);
850 
851     /*
852       Copy value if copy_blobs is set, or source is part of the table's
853       writeset.
854     */
855     if (to->table->copy_blobs ||
856         (!blob->value.is_alloced() && from->is_updatable()))
857       blob->value.copy();
858 
859     return blob->store(blob->value.ptr(),blob->value.length(),from->charset());
860   }
861   if (from->real_type() == MYSQL_TYPE_ENUM &&
862       to->real_type() == MYSQL_TYPE_ENUM &&
863       from->val_int() == 0)
864   {
865     ((Field_enum *)(to))->store_type(0);
866     return TYPE_OK;
867   }
868   else if (from->is_temporal() && to->result_type() == INT_RESULT)
869   {
870     MYSQL_TIME ltime;
871     longlong nr;
872     if (from->type() == MYSQL_TYPE_TIME)
873     {
874       from->get_time(&ltime);
875       nr= TIME_to_ulonglong_time_round(&ltime);
876     }
877     else
878     {
879       from->get_date(&ltime, TIME_FUZZY_DATE);
880       nr= TIME_to_ulonglong_datetime_round(&ltime);
881     }
882     return to->store(ltime.neg ? -nr : nr, 0);
883   }
884   else if (from->is_temporal() &&
885            (to->result_type() == REAL_RESULT ||
886             to->result_type() == DECIMAL_RESULT ||
887             to->result_type() == INT_RESULT))
888   {
889     my_decimal tmp;
890     /*
891       We prefer DECIMAL as the safest precise type:
892       double supports only 15 digits, which is not enough for DATETIME(6).
893     */
894     return to->store_decimal(from->val_decimal(&tmp));
895   }
896   else if (from->is_temporal() && to->is_temporal())
897   {
898     return copy_time_to_time(from, to);
899   }
900   else if ((from->result_type() == STRING_RESULT &&
901             (to->result_type() == STRING_RESULT ||
902              (from->real_type() != MYSQL_TYPE_ENUM &&
903               from->real_type() != MYSQL_TYPE_SET))) ||
904            to->type() == MYSQL_TYPE_DECIMAL)
905   {
906     char buff[MAX_FIELD_WIDTH];
907     String result(buff,sizeof(buff),from->charset());
908     from->val_str(&result);
909     /*
910       We use c_ptr_quick() here to make it easier if to is a float/double
911       as the conversion routines will do a copy of the result doesn't
912       end with \0. Can be replaced with .ptr() when we have our own
913       string->double conversion.
914     */
915     return to->store(result.c_ptr_quick(),result.length(),from->charset());
916   }
917   else if (from->result_type() == REAL_RESULT)
918     return to->store(from->val_real());
919   else if (from->result_type() == DECIMAL_RESULT)
920   {
921     my_decimal buff;
922     return to->store_decimal(from->val_decimal(&buff));
923   }
924   else
925     return to->store(from->val_int(), MY_TEST(from->flags & UNSIGNED_FLAG));
926 }
927