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