MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
field.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2000, 2013 Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 
26 #include "sql_priv.h"
27 #include "sql_select.h"
28 #include "rpl_rli.h" // Pull in Relay_log_info
29 #include "rpl_slave.h" // Pull in rpl_master_has_bug()
30 #include "strfunc.h" // find_type2, find_set
31 #include "sql_time.h" // str_to_datetime_with_warn,
32  // str_to_time_with_warn,
33  // TIME_to_timestamp,
34  // make_time, make_date,
35  // make_truncated_value_warning
36 #include "tztime.h" // struct Time_zone
37 #include "filesort.h" // change_double_for_sort
38 #include "log_event.h" // class Table_map_log_event
39 #include <m_ctype.h>
40 #include <errno.h>
41 #include "sql_join_buffer.h" // CACHE_FIELD
42 
43 using std::max;
44 using std::min;
45 
46 // Maximum allowed exponent value for converting string to decimal
47 #define MAX_EXPONENT 1024
48 
52 uchar Field_null::null[1]={1};
53 const char field_separator=',';
54 
55 #define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
56 #define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
57 #define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
58 #define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
59 ((ulong) ((LL(1) << MY_MIN(arg, 4) * 8) - LL(1)))
60 
61 /*
62  Rules for merging different types of fields in UNION
63 
64  NOTE: to avoid 256*256 table, gap in table types numeration is skiped
65  following #defines describe that gap and how to canculate number of fields
66  and index of field in thia array.
67 */
68 #define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_BIT + 1)
69 #define FIELDTYPE_TEAR_TO (MYSQL_TYPE_NEWDECIMAL - 1)
70 #define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
71 
72 
73 inline int field_type2index (enum_field_types field_type)
74 {
75  field_type= real_type_to_type(field_type);
76  DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM ||
77  field_type > FIELDTYPE_TEAR_TO);
78  return (field_type < FIELDTYPE_TEAR_FROM ?
79  field_type :
80  ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
81 }
82 
83 
84 static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
85 {
86  /* MYSQL_TYPE_DECIMAL -> */
87  {
88  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
89  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
90  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
91  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
92  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
93  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
94  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
95  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
96  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
97  MYSQL_TYPE_DECIMAL, MYSQL_TYPE_DECIMAL,
98  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
99  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
100  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
101  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
102  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
103  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
104  //MYSQL_TYPE_BIT <16>-<245>
105  MYSQL_TYPE_VARCHAR,
106  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
107  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
108  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
109  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
110  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
111  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
112  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
113  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
114  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
115  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
116  },
117  /* MYSQL_TYPE_TINY -> */
118  {
119  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
120  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
121  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
122  MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
123  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
124  MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
125  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
126  MYSQL_TYPE_TINY, MYSQL_TYPE_VARCHAR,
127  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
128  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
129  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
130  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
131  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
132  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY,
133  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
134  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
135  //MYSQL_TYPE_BIT <16>-<245>
136  MYSQL_TYPE_VARCHAR,
137  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
138  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
139  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
140  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
141  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
142  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
143  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
144  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
145  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
146  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
147  },
148  /* MYSQL_TYPE_SHORT -> */
149  {
150  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
151  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_SHORT,
152  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
153  MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
154  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
155  MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
156  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
157  MYSQL_TYPE_SHORT, MYSQL_TYPE_VARCHAR,
158  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
159  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
160  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
161  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
162  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
163  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_SHORT,
164  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
165  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
166  //MYSQL_TYPE_BIT <16>-<245>
167  MYSQL_TYPE_VARCHAR,
168  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
169  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
170  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
171  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
172  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
173  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
174  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
175  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
176  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
177  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
178  },
179  /* MYSQL_TYPE_LONG -> */
180  {
181  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
182  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONG,
183  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
184  MYSQL_TYPE_LONG, MYSQL_TYPE_LONG,
185  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
186  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
187  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
188  MYSQL_TYPE_LONG, MYSQL_TYPE_VARCHAR,
189  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
190  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG,
191  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
192  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
193  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
194  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONG,
195  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
196  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
197  //MYSQL_TYPE_BIT <16>-<245>
198  MYSQL_TYPE_VARCHAR,
199  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
200  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
201  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
202  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
203  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
204  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
205  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
206  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
207  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
208  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
209  },
210  /* MYSQL_TYPE_FLOAT -> */
211  {
212  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
213  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_FLOAT,
214  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
215  MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
216  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
217  MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
218  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
219  MYSQL_TYPE_FLOAT, MYSQL_TYPE_VARCHAR,
220  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
221  MYSQL_TYPE_FLOAT, MYSQL_TYPE_FLOAT,
222  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
223  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
224  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
225  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_FLOAT,
226  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
227  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
228  //MYSQL_TYPE_BIT <16>-<245>
229  MYSQL_TYPE_VARCHAR,
230  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
231  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
232  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
233  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
234  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
235  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
236  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
237  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
238  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
239  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
240  },
241  /* MYSQL_TYPE_DOUBLE -> */
242  {
243  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
244  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
245  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
246  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
247  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
248  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
249  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
250  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
251  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
252  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
253  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
254  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
255  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
256  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_DOUBLE,
257  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
258  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
259  //MYSQL_TYPE_BIT <16>-<245>
260  MYSQL_TYPE_VARCHAR,
261  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
262  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
263  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
264  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
265  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
266  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
267  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
268  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
269  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
270  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
271  },
272  /* MYSQL_TYPE_NULL -> */
273  {
274  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
275  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
276  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
277  MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
278  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
279  MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
280  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
281  MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP,
282  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
283  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
284  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
285  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_TIME,
286  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
287  MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR,
288  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
289  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
290  //MYSQL_TYPE_BIT <16>-<245>
291  MYSQL_TYPE_BIT,
292  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
293  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_ENUM,
294  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
295  MYSQL_TYPE_SET, MYSQL_TYPE_TINY_BLOB,
296  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
297  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
298  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
299  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
300  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
301  MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY
302  },
303  /* MYSQL_TYPE_TIMESTAMP -> */
304  {
305  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
306  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
307  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
308  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
309  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
310  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
311  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
312  MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_TIMESTAMP,
313  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
314  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
315  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
316  MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
317  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
318  MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
319  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
320  MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
321  //MYSQL_TYPE_BIT <16>-<245>
322  MYSQL_TYPE_VARCHAR,
323  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
324  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
325  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
326  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
327  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
328  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
329  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
330  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
331  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
332  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
333  },
334  /* MYSQL_TYPE_LONGLONG -> */
335  {
336  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
337  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONGLONG,
338  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
339  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
340  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
341  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
342  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
343  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_VARCHAR,
344  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
345  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG,
346  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
347  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
348  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
349  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONGLONG,
350  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
351  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
352  //MYSQL_TYPE_BIT <16>-<245>
353  MYSQL_TYPE_VARCHAR,
354  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
355  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
356  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
357  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
358  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
359  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
360  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
361  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
362  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
363  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
364  },
365  /* MYSQL_TYPE_INT24 -> */
366  {
367  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
368  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_INT24,
369  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
370  MYSQL_TYPE_INT24, MYSQL_TYPE_LONG,
371  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
372  MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
373  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
374  MYSQL_TYPE_INT24, MYSQL_TYPE_VARCHAR,
375  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
376  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
377  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
378  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
379  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
380  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_INT24,
381  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
382  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
383  //MYSQL_TYPE_BIT <16>-<245>
384  MYSQL_TYPE_VARCHAR,
385  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
386  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
387  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
388  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
389  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
390  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
391  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
392  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
393  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
394  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
395  },
396  /* MYSQL_TYPE_DATE -> */
397  {
398  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
399  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
400  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
401  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
402  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
403  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
404  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
405  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
406  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
407  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
408  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
409  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
410  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
411  MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
412  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
413  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
414  //MYSQL_TYPE_BIT <16>-<245>
415  MYSQL_TYPE_VARCHAR,
416  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
417  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
418  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
419  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
420  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
421  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
422  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
423  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
424  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
425  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
426  },
427  /* MYSQL_TYPE_TIME -> */
428  {
429  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
430  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
431  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
432  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
433  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
434  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
435  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
436  MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME,
437  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
438  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
439  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
440  MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIME,
441  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
442  MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
443  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
444  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
445  //MYSQL_TYPE_BIT <16>-<245>
446  MYSQL_TYPE_VARCHAR,
447  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
448  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
449  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
450  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
451  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
452  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
453  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
454  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
455  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
456  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
457  },
458  /* MYSQL_TYPE_DATETIME -> */
459  {
460  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
461  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
462  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
463  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
464  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
465  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
466  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
467  MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
468  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
469  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
470  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
471  MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
472  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
473  MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
474  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
475  MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
476  //MYSQL_TYPE_BIT <16>-<245>
477  MYSQL_TYPE_VARCHAR,
478  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
479  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
480  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
481  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
482  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
483  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
484  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
485  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
486  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
487  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
488  },
489  /* MYSQL_TYPE_YEAR -> */
490  {
491  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
492  MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY,
493  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
494  MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
495  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
496  MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
497  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
498  MYSQL_TYPE_YEAR, MYSQL_TYPE_VARCHAR,
499  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
500  MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
501  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
502  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
503  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
504  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_YEAR,
505  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
506  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
507  //MYSQL_TYPE_BIT <16>-<245>
508  MYSQL_TYPE_VARCHAR,
509  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
510  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
511  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
512  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
513  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
514  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
515  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
516  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
517  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
518  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
519  },
520  /* MYSQL_TYPE_NEWDATE -> */
521  {
522  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
523  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
524  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
525  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
526  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
527  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
528  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
529  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
530  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
531  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
532  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
533  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
534  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
535  MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
536  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
537  MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
538  //MYSQL_TYPE_BIT <16>-<245>
539  MYSQL_TYPE_VARCHAR,
540  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
541  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
542  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
543  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
544  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
545  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
546  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
547  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
548  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
549  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
550  },
551  /* MYSQL_TYPE_VARCHAR -> */
552  {
553  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
554  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
555  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
556  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
557  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
558  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
559  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
560  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
561  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
562  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
563  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
564  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
565  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
566  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
567  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
568  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
569  //MYSQL_TYPE_BIT <16>-<245>
570  MYSQL_TYPE_VARCHAR,
571  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
572  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
573  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
574  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
575  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
576  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
577  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
578  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
579  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
580  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR
581  },
582  /* MYSQL_TYPE_BIT -> */
583  {
584  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
585  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
586  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
587  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
588  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
589  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
590  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
591  MYSQL_TYPE_BIT, MYSQL_TYPE_VARCHAR,
592  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
593  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
594  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
595  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
596  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
597  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
598  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
599  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
600  //MYSQL_TYPE_BIT <16>-<245>
601  MYSQL_TYPE_BIT,
602  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
603  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
604  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
605  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
606  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
607  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
608  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
609  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
610  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
611  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
612  },
613  /* MYSQL_TYPE_NEWDECIMAL -> */
614  {
615  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
616  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
617  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
618  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
619  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
620  MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
621  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
622  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
623  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
624  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
625  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
626  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
627  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
628  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_NEWDECIMAL,
629  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
630  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
631  //MYSQL_TYPE_BIT <16>-<245>
632  MYSQL_TYPE_VARCHAR,
633  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
634  MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
635  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
636  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
637  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
638  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
639  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
640  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
641  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
642  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
643  },
644  /* MYSQL_TYPE_ENUM -> */
645  {
646  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
647  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
648  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
649  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
650  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
651  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
652  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
653  MYSQL_TYPE_ENUM, MYSQL_TYPE_VARCHAR,
654  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
655  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
656  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
657  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
658  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
659  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
660  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
661  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
662  //MYSQL_TYPE_BIT <16>-<245>
663  MYSQL_TYPE_VARCHAR,
664  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
665  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
666  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
667  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
668  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
669  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
670  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
671  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
672  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
673  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
674  },
675  /* MYSQL_TYPE_SET -> */
676  {
677  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
678  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
679  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
680  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
681  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
682  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
683  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
684  MYSQL_TYPE_SET, MYSQL_TYPE_VARCHAR,
685  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
686  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
687  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
688  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
689  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
690  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
691  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
692  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
693  //MYSQL_TYPE_BIT <16>-<245>
694  MYSQL_TYPE_VARCHAR,
695  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
696  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
697  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
698  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
699  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
700  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
701  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
702  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
703  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
704  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
705  },
706  /* MYSQL_TYPE_TINY_BLOB -> */
707  {
708  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
709  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
710  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
711  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
712  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
713  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
714  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
715  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
716  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
717  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
718  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
719  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
720  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
721  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
722  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
723  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
724  //MYSQL_TYPE_BIT <16>-<245>
725  MYSQL_TYPE_TINY_BLOB,
726  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
727  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
728  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
729  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
730  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
731  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
732  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
733  MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB,
734  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
735  MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB
736  },
737  /* MYSQL_TYPE_MEDIUM_BLOB -> */
738  {
739  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
740  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
741  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
742  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
743  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
744  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
745  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
746  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
747  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
748  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
749  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
750  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
751  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
752  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
753  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
754  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
755  //MYSQL_TYPE_BIT <16>-<245>
756  MYSQL_TYPE_MEDIUM_BLOB,
757  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
758  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
759  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
760  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
761  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
762  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
763  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
764  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
765  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
766  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB
767  },
768  /* MYSQL_TYPE_LONG_BLOB -> */
769  {
770  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
771  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
772  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
773  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
774  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
775  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
776  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
777  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
778  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
779  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
780  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
781  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
782  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
783  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
784  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
785  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
786  //MYSQL_TYPE_BIT <16>-<245>
787  MYSQL_TYPE_LONG_BLOB,
788  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
789  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
790  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
791  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
792  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
793  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
794  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
795  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
796  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
797  MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB
798  },
799  /* MYSQL_TYPE_BLOB -> */
800  {
801  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
802  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
803  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
804  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
805  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
806  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
807  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
808  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
809  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
810  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
811  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
812  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
813  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
814  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
815  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
816  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
817  //MYSQL_TYPE_BIT <16>-<245>
818  MYSQL_TYPE_BLOB,
819  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
820  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
821  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
822  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
823  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
824  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
825  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
826  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
827  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
828  MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB
829  },
830  /* MYSQL_TYPE_VAR_STRING -> */
831  {
832  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
833  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
834  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
835  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
836  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
837  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
838  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
839  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
840  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
841  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
842  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
843  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
844  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
845  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
846  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
847  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
848  //MYSQL_TYPE_BIT <16>-<245>
849  MYSQL_TYPE_VARCHAR,
850  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
851  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
852  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
853  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
854  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
855  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
856  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
857  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
858  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
859  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR
860  },
861  /* MYSQL_TYPE_STRING -> */
862  {
863  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
864  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
865  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
866  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
867  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
868  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
869  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
870  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
871  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
872  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
873  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
874  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
875  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
876  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
877  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
878  MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR,
879  //MYSQL_TYPE_BIT <16>-<245>
880  MYSQL_TYPE_STRING,
881  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
882  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
883  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
884  MYSQL_TYPE_STRING, MYSQL_TYPE_TINY_BLOB,
885  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
886  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
887  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
888  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
889  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
890  MYSQL_TYPE_STRING, MYSQL_TYPE_STRING
891  },
892  /* MYSQL_TYPE_GEOMETRY -> */
893  {
894  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
895  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
896  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
897  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
898  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
899  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
900  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
901  MYSQL_TYPE_GEOMETRY, MYSQL_TYPE_VARCHAR,
902  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
903  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
904  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
905  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
906  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
907  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
908  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
909  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
910  //MYSQL_TYPE_BIT <16>-<245>
911  MYSQL_TYPE_VARCHAR,
912  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
913  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
914  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
915  MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
916  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
917  MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
918  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
919  MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
920  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
921  MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY
922  }
923 };
924 
925 
936 enum_field_types Field::field_type_merge(enum_field_types a,
937  enum_field_types b)
938 {
939  return field_types_merge_rules[field_type2index(a)]
940  [field_type2index(b)];
941 }
942 
943 
944 static Item_result field_types_result_type [FIELDTYPE_NUM]=
945 {
946  //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
947  DECIMAL_RESULT, INT_RESULT,
948  //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
949  INT_RESULT, INT_RESULT,
950  //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
951  REAL_RESULT, REAL_RESULT,
952  //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
953  STRING_RESULT, STRING_RESULT,
954  //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
955  INT_RESULT, INT_RESULT,
956  //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
957  STRING_RESULT, STRING_RESULT,
958  //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
959  STRING_RESULT, INT_RESULT,
960  //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
961  STRING_RESULT, STRING_RESULT,
962  //MYSQL_TYPE_BIT <16>-<245>
963  STRING_RESULT,
964  //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
965  DECIMAL_RESULT, STRING_RESULT,
966  //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
967  STRING_RESULT, STRING_RESULT,
968  //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
969  STRING_RESULT, STRING_RESULT,
970  //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
971  STRING_RESULT, STRING_RESULT,
972  //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
973  STRING_RESULT, STRING_RESULT
974 };
975 
976 
977 /*
978  Test if the given string contains important data:
979  not spaces for character string,
980  or any data for binary string.
981 
982  SYNOPSIS
983  test_if_important_data()
984  cs Character set
985  str String to test
986  strend String end
987 
988  RETURN
989  FALSE - If string does not have important data
990  TRUE - If string has some important data
991 */
992 
993 static bool
994 test_if_important_data(const CHARSET_INFO *cs, const char *str,
995  const char *strend)
996 {
997  if (cs != &my_charset_bin)
998  str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
999  return (str < strend);
1000 }
1001 
1002 
1009 CPP_UNNAMED_NS_START
1010 
1011 int compare(unsigned int a, unsigned int b)
1012 {
1013  if (a < b)
1014  return -1;
1015  if (b < a)
1016  return 1;
1017  return 0;
1018 }
1019 
1020 CPP_UNNAMED_NS_END
1021 
1031 Item_result Field::result_merge_type(enum_field_types field_type)
1032 {
1033  return field_types_result_type[field_type2index(field_type)];
1034 }
1035 
1036 /*****************************************************************************
1037  Static help functions
1038 *****************************************************************************/
1039 
1053 static void push_numerical_conversion_warning(THD* thd, const char* str,
1054  uint length,
1055  const CHARSET_INFO* cs,
1056  const char* typestr, int error,
1057  const char* field_name="UNKNOWN",
1058  ulong row_num=0)
1059 {
1060  char buf[MY_MAX(MY_MAX(DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE,
1061  LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE),
1062  DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE)];
1063 
1064  String tmp(buf, sizeof(buf), cs);
1065  tmp.copy(str, length, cs);
1066  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1067  error, ER(error), typestr, tmp.c_ptr(),
1068  field_name, row_num);
1069 }
1070 
1071 
1084 static void set_decimal_warning(Field_new_decimal *field,
1085  int dec_error,
1086  my_decimal *dec_value,
1087  const char *from,
1088  uint length,
1089  const CHARSET_INFO *charset_arg)
1090 {
1091  switch (dec_error) {
1092  case E_DEC_TRUNCATED:
1093  field->set_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1094  break;
1095  case E_DEC_OVERFLOW:
1096  field->set_warning(Sql_condition::WARN_LEVEL_WARN,
1097  ER_WARN_DATA_OUT_OF_RANGE, 1);
1098  field->set_value_on_overflow(dec_value, dec_value->sign());
1099  break;
1100  case E_DEC_BAD_NUM:
1101  ErrConvString errmsg(from, length, charset_arg);
1102  const Diagnostics_area *da= field->table->in_use->get_stmt_da();
1103  push_warning_printf(field->table->in_use, Sql_condition::WARN_LEVEL_WARN,
1104  ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1105  ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1106  "decimal", errmsg.ptr(), field->field_name,
1107  static_cast<ulong>(da->current_row_for_warning()));
1108  my_decimal_set_zero(dec_value);
1109  }
1110 }
1111 
1126 bool Field::type_can_have_key_part(enum enum_field_types type)
1127 {
1128  switch (type) {
1129  case MYSQL_TYPE_VARCHAR:
1130  case MYSQL_TYPE_TINY_BLOB:
1131  case MYSQL_TYPE_MEDIUM_BLOB:
1132  case MYSQL_TYPE_LONG_BLOB:
1133  case MYSQL_TYPE_BLOB:
1134  case MYSQL_TYPE_VAR_STRING:
1135  case MYSQL_TYPE_STRING:
1136  case MYSQL_TYPE_GEOMETRY:
1137  return TRUE;
1138  default:
1139  return FALSE;
1140  }
1141 }
1142 
1143 
1147 Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1148  uchar null_bit_arg, utype unireg_check_arg,
1149  const char *field_name_arg,
1150  uint8 dec_arg, bool zero_arg, bool unsigned_arg)
1151  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1152  unireg_check_arg, field_name_arg),
1153  dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
1154 {
1155  if (zerofill)
1156  flags|=ZEROFILL_FLAG;
1157  if (unsigned_flag)
1158  flags|=UNSIGNED_FLAG;
1159 }
1160 
1161 
1162 void Field_num::prepend_zeros(String *value)
1163 {
1164  int diff;
1165  if ((diff= (int) (field_length - value->length())) > 0)
1166  {
1167  bmove_upp((uchar*) value->ptr()+field_length,
1168  (uchar*) value->ptr()+value->length(),
1169  value->length());
1170  memset(const_cast<char*>(value->ptr()), '0', diff);
1171  value->length(field_length);
1172  (void) value->c_ptr_quick(); // Avoid warnings in purify
1173  }
1174 }
1175 
1193 type_conversion_status
1194 Field_num::check_int(const CHARSET_INFO *cs, const char *str, int length,
1195  const char *int_end, int error)
1196 {
1197  /* Test if we get an empty string or wrong integer */
1198  if (str == int_end || error == MY_ERRNO_EDOM)
1199  {
1200  ErrConvString err(str, length, cs);
1201  push_warning_printf(table->in_use, Sql_condition::WARN_LEVEL_WARN,
1202  ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1203  ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1204  "integer", err.ptr(), field_name,
1205  (ulong) table->in_use->get_stmt_da()->
1206  current_row_for_warning());
1207  return TYPE_ERR_BAD_VALUE;
1208  }
1209  /* Test if we have garbage at the end of the given string. */
1210  if (test_if_important_data(cs, int_end, str + length))
1211  {
1212  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1213  return TYPE_WARN_TRUNCATED;
1214  }
1215  return TYPE_OK;
1216 }
1217 
1218 
1219 /*
1220  Conver a string to an integer then check bounds.
1221 
1222  SYNOPSIS
1223  Field_num::get_int
1224  cs Character set
1225  from String to convert
1226  len Length of the string
1227  rnd OUT longlong value
1228  unsigned_max max unsigned value
1229  signed_min min signed value
1230  signed_max max signed value
1231 
1232  DESCRIPTION
1233  The function calls strntoull10rnd() to get an integer value then
1234  check bounds and errors returned. In case of any error a warning
1235  is raised.
1236 
1237  @return TYPE_OK, TYPE_WARN_OUT_OF_RANGE, TYPE_ERR_BAD_VALUE or
1238  TYPE_WARN_TRUNCATED
1239 */
1240 
1241 type_conversion_status
1242 Field_num::get_int(const CHARSET_INFO *cs, const char *from, uint len,
1243  longlong *rnd, ulonglong unsigned_max,
1244  longlong signed_min, longlong signed_max)
1245 {
1246  char *end;
1247  int error;
1248 
1249  *rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len,
1250  unsigned_flag, &end,
1251  &error);
1252  if (unsigned_flag)
1253  {
1254  if ((((ulonglong) *rnd > unsigned_max) &&
1255  (*rnd= (longlong) unsigned_max)) ||
1256  error == MY_ERRNO_ERANGE)
1257  goto out_of_range;
1258  }
1259  else
1260  {
1261  if (*rnd < signed_min)
1262  {
1263  *rnd= signed_min;
1264  goto out_of_range;
1265  }
1266  else if (*rnd > signed_max)
1267  {
1268  *rnd= signed_max;
1269  goto out_of_range;
1270  }
1271  }
1272  if (table->in_use->count_cuted_fields != 0)
1273  return check_int(cs, from, len, end, error);
1274 
1275  return TYPE_OK;
1276 
1277 out_of_range:
1278  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1279  return TYPE_WARN_OUT_OF_RANGE;
1280 }
1281 
1282 
1283 /*
1284  This is a generic method which is executed only for
1285  Field_short, Field_medium, Field_long, Field_longlong and Field_tiny.
1286 
1287  The other field types that come from Field_num override this method:
1288  Field_real (common parent for Field_decimal, Field_float, Field_double),
1289  Field_new_decimal, Field_year.
1290 */
1291 type_conversion_status
1293  uint8 dec_arg __attribute__((unused)))
1294 {
1295  longlong nr= TIME_to_ulonglong_round(ltime);
1296  return store(ltime->neg ? -nr : nr, 0);
1297 }
1298 
1299 
1310 bool Field::warn_if_overflow(int op_result)
1311 {
1312  if (op_result == E_DEC_OVERFLOW)
1313  {
1314  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1315  return true;
1316  }
1317  if (op_result == E_DEC_TRUNCATED)
1318  {
1319  set_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1320  /* We return 0 here as this is not a critical issue */
1321  }
1322  return false;
1323 }
1324 
1325 
1332 String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
1333 {
1334  ASSERT_COLUMN_MARKED_FOR_READ;
1335  const CHARSET_INFO *cs= &my_charset_bin;
1336  uint length;
1337  longlong value= val_int();
1338 
1339  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
1340  return 0;
1341  length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
1342  MY_INT64_NUM_DECIMAL_DIGITS,
1343  unsigned_val ? 10 : -10,
1344  value);
1345  val_buffer->length(length);
1346  return val_buffer;
1347 }
1348 
1349 
1351 Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
1352  uchar null_bit_arg,
1353  utype unireg_check_arg, const char *field_name_arg)
1354  :ptr(ptr_arg), null_ptr(null_ptr_arg),
1355  table(0), orig_table(0), table_name(0),
1356  field_name(field_name_arg),
1357  unireg_check(unireg_check_arg),
1358  field_length(length_arg), null_bit(null_bit_arg),
1359  is_created_from_null_item(FALSE)
1360 {
1361  flags=null_ptr ? 0: NOT_NULL_FLAG;
1362  comment.str= (char*) "";
1363  comment.length=0;
1364  field_index= 0;
1365 }
1366 
1367 
1368 void Field::hash(ulong *nr, ulong *nr2)
1369 {
1370  if (is_null())
1371  {
1372  *nr^= (*nr << 1) | 1;
1373  }
1374  else
1375  {
1376  uint len= pack_length();
1377  const CHARSET_INFO *cs= sort_charset();
1378  cs->coll->hash_sort(cs, ptr, len, nr, nr2);
1379  }
1380 }
1381 
1382 size_t
1383 Field::do_last_null_byte() const
1384 {
1385  DBUG_ASSERT(null_ptr == NULL || null_ptr >= table->record[0]);
1386  if (null_ptr)
1387  return null_offset() + 1;
1388  return LAST_NULL_BYTE_UNDEF;
1389 }
1390 
1391 
1392 void Field::copy_from_tmp(int row_offset)
1393 {
1394  memcpy(ptr,ptr+row_offset,pack_length());
1395  if (null_ptr)
1396  {
1397  *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
1398  (null_ptr[row_offset] & (uchar) null_bit));
1399  }
1400 }
1401 
1402 
1403 bool Field::send_binary(Protocol *protocol)
1404 {
1405  char buff[MAX_FIELD_WIDTH];
1406  String tmp(buff,sizeof(buff),charset());
1407  val_str(&tmp);
1408  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
1409 }
1410 
1411 
1446 bool Field::compatible_field_size(uint field_metadata,
1447  Relay_log_info *rli_arg __attribute__((unused)),
1448  uint16 mflags __attribute__((unused)),
1449  int *order_var)
1450 {
1451  uint const source_size= pack_length_from_metadata(field_metadata);
1452  uint const destination_size= row_pack_length();
1453  DBUG_PRINT("debug", ("real_type: %d, source_size: %u, destination_size: %u",
1454  real_type(), source_size, destination_size));
1455  *order_var = compare(source_size, destination_size);
1456  return true;
1457 }
1458 
1459 
1460 type_conversion_status
1461 Field::store(const char *to, uint length, const CHARSET_INFO *cs,
1462  enum_check_fields check_level)
1463 {
1464  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
1465  table->in_use->count_cuted_fields= check_level;
1466  const type_conversion_status res= store(to, length, cs);
1467  table->in_use->count_cuted_fields= old_check_level;
1468  return res;
1469 }
1470 
1471 
1509 uchar *
1510 Field::pack(uchar *to, const uchar *from, uint max_length,
1511  bool low_byte_first __attribute__((unused)))
1512 {
1513  uint32 length= pack_length();
1514  set_if_smaller(length, max_length);
1515  memcpy(to, from, length);
1516  return to+length;
1517 }
1518 
1549 const uchar *
1550 Field::unpack(uchar* to, const uchar *from, uint param_data,
1551  bool low_byte_first __attribute__((unused)))
1552 {
1553  uint length=pack_length();
1554  int from_type= 0;
1555  /*
1556  If from length is > 255, it has encoded data in the upper bits. Need
1557  to mask it out.
1558  */
1559  if (param_data > 255)
1560  {
1561  from_type= (param_data & 0xff00) >> 8U; // real_type.
1562  param_data= param_data & 0x00ff; // length.
1563  }
1564 
1565  if ((param_data == 0) ||
1566  (length == param_data) ||
1567  (from_type != real_type()))
1568  {
1569  memcpy(to, from, length);
1570  return from+length;
1571  }
1572 
1573  uint len= (param_data && (param_data < length)) ?
1574  param_data : length;
1575 
1576  memcpy(to, from, param_data > length ? length : len);
1577  return from+len;
1578 }
1579 
1580 
1581 void Field_num::add_zerofill_and_unsigned(String &res) const
1582 {
1583  if (unsigned_flag)
1584  res.append(STRING_WITH_LEN(" unsigned"));
1585  if (zerofill)
1586  res.append(STRING_WITH_LEN(" zerofill"));
1587 }
1588 
1589 
1590 void Field::make_field(Send_field *field)
1591 {
1592  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
1593  {
1594  field->db_name= orig_table->s->db.str;
1595  if (orig_table->pos_in_table_list &&
1596  orig_table->pos_in_table_list->schema_table)
1597  field->org_table_name= (orig_table->pos_in_table_list->
1598  schema_table->table_name);
1599  else
1600  field->org_table_name= orig_table->s->table_name.str;
1601  }
1602  else
1603  field->org_table_name= field->db_name= "";
1604  if (orig_table && orig_table->alias)
1605  {
1606  field->table_name= orig_table->alias;
1607  field->org_col_name= field_name;
1608  }
1609  else
1610  {
1611  field->table_name= "";
1612  field->org_col_name= "";
1613  }
1614  field->col_name= field_name;
1615  field->charsetnr= charset()->number;
1616  field->length=field_length;
1617  field->type=type();
1618  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1619  field->decimals= decimals();
1620 }
1621 
1622 
1635  bool unsigned_flag,
1636  bool *has_overflow)
1637 {
1638  if (unsigned_flag && val->sign())
1639  {
1640  // Converting a signed decimal to unsigned int
1641  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1642  *has_overflow= true;
1643  return 0;
1644  }
1645 
1646  longlong val_ll;
1647  int conversion_error= my_decimal2int(E_DEC_ERROR & ~E_DEC_OVERFLOW
1648  & ~E_DEC_TRUNCATED,
1649  val, unsigned_flag, &val_ll);
1650 
1651  if (warn_if_overflow(conversion_error))
1652  {
1653  *has_overflow= true;
1654  if (unsigned_flag)
1655  return ULONGLONG_MAX;
1656 
1657  return (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
1658  }
1659 
1660  return val_ll;
1661 }
1662 
1663 
1676 type_conversion_status Field_num::store_decimal(const my_decimal *val)
1677 {
1678  ASSERT_COLUMN_MARKED_FOR_WRITE;
1679  bool has_overflow= false;
1680  longlong i= convert_decimal2longlong(val, unsigned_flag, &has_overflow);
1681  const type_conversion_status res= store(i, unsigned_flag);
1682  return has_overflow ? TYPE_WARN_OUT_OF_RANGE : res;
1683 }
1684 
1685 
1701 {
1702  ASSERT_COLUMN_MARKED_FOR_READ;
1703  DBUG_ASSERT(result_type() == INT_RESULT);
1704  longlong nr= val_int();
1705  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
1706  return decimal_value;
1707 }
1708 
1709 
1710 bool Field_num::get_date(MYSQL_TIME *ltime, uint fuzzydate)
1711 {
1712  DBUG_ASSERT(result_type() == INT_RESULT);
1713  return my_longlong_to_datetime_with_warn(val_int(), ltime, fuzzydate);
1714 }
1715 
1716 
1717 bool Field_num::get_time(MYSQL_TIME *ltime)
1718 {
1719  DBUG_ASSERT(result_type() == INT_RESULT);
1720  return my_longlong_to_time_with_warn(val_int(), ltime);
1721 }
1722 
1723 
1724 Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1725  uchar null_bit_arg, utype unireg_check_arg,
1726  const char *field_name_arg,
1727  const CHARSET_INFO *charset_arg)
1728  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1729  unireg_check_arg, field_name_arg)
1730 {
1731  field_charset= charset_arg;
1732  if (charset_arg->state & MY_CS_BINSORT)
1733  flags|=BINARY_FLAG;
1734  field_derivation= DERIVATION_IMPLICIT;
1735 }
1736 
1737 
1738 void Field_str::make_field(Send_field *field)
1739 {
1740  Field::make_field(field);
1741  field->decimals= 0;
1742 }
1743 
1764 type_conversion_status Field_str::store_decimal(const my_decimal *d)
1765 {
1766  ASSERT_COLUMN_MARKED_FOR_WRITE;
1767  double val;
1768  /* TODO: use decimal2string? */
1769  int err= my_decimal2double(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, d, &val);
1770  warn_if_overflow(err);
1771  const type_conversion_status res= store(val);
1772 
1773  return (err != E_DEC_OK) ? decimal_err_to_type_conv_status(err) : res;
1774 }
1775 
1776 
1777 uint Field::fill_cache_field(CACHE_FIELD *copy)
1778 {
1779  uint store_length;
1780  copy->str= ptr;
1781  copy->length= pack_length();
1782  copy->field= this;
1783  if (flags & BLOB_FLAG)
1784  {
1785  copy->type= CACHE_BLOB;
1786  copy->length-= portable_sizeof_char_ptr;
1787  return copy->length;
1788  }
1789  else if (!zero_pack() &&
1790  (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
1791  copy->length < 256))
1792  {
1793  copy->type= CACHE_STRIPPED; /* Remove end space */
1794  store_length= 2;
1795  }
1796  else if (type() == MYSQL_TYPE_VARCHAR)
1797  {
1798  copy->type= pack_length()-row_pack_length() == 1 ? CACHE_VARSTR1:
1799  CACHE_VARSTR2;
1800  store_length= 0;
1801  }
1802  else
1803  {
1804  copy->type= 0;
1805  store_length= 0;
1806  }
1807  return copy->length + store_length;
1808 }
1809 
1810 
1811 bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1812 {
1813  char buff[MAX_DATE_STRING_REP_LENGTH];
1814  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1815  return !(res= val_str(&tmp)) ||
1816  str_to_datetime_with_warn(res, ltime, fuzzydate);
1817 }
1818 
1819 
1820 bool Field::get_time(MYSQL_TIME *ltime)
1821 {
1822  char buff[MAX_DATE_STRING_REP_LENGTH];
1823  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1824  return !(res= val_str(&tmp)) || str_to_time_with_warn(res, ltime);
1825 }
1826 
1827 
1828 bool Field::get_timestamp(struct timeval *tm, int *warnings)
1829 {
1830  MYSQL_TIME ltime;
1831  DBUG_ASSERT(!is_null());
1832  return get_date(&ltime, TIME_FUZZY_DATE) ||
1833  datetime_to_timeval(current_thd, &ltime, tm, warnings);
1834 }
1835 
1836 
1844 type_conversion_status Field::store_time(MYSQL_TIME *ltime, uint8 dec_arg)
1845 {
1846  ASSERT_COLUMN_MARKED_FOR_WRITE;
1847  char buff[MAX_DATE_STRING_REP_LENGTH];
1848  uint length= (uint) my_TIME_to_str(ltime, buff,
1849  MY_MIN(dec_arg, DATETIME_MAX_DECIMALS));
1850  /* Avoid conversion when field character set is ASCII compatible */
1851  return store(buff, length, (charset()->state & MY_CS_NONASCII) ?
1852  &my_charset_latin1 : charset());
1853 }
1854 
1855 
1856 bool Field::optimize_range(uint idx, uint part)
1857 {
1858  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1859 }
1860 
1861 
1862 Field *Field::new_field(MEM_ROOT *root, TABLE *new_table,
1863  bool keep_type __attribute__((unused)))
1864 {
1865  Field *tmp= clone(root);
1866  if (tmp == NULL)
1867  return 0;
1868 
1869  if (tmp->table->maybe_null)
1870  tmp->flags&= ~NOT_NULL_FLAG;
1871  tmp->table= new_table;
1872  tmp->key_start.init(0);
1873  tmp->part_of_key.init(0);
1874  tmp->part_of_sortkey.init(0);
1875  /*
1876  todo: We should never alter unireg_check after an object is constructed,
1877  and the member should be made const. But a lot of code depends upon this
1878  hack, and the different utype values are completely unrelated so we can
1879  never be quite sure which parts of the server will break.
1880  */
1881  tmp->unireg_check= Field::NONE;
1882  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
1883  ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1884  tmp->reset_fields();
1885  return tmp;
1886 }
1887 
1888 
1889 Field *Field::new_key_field(MEM_ROOT *root, TABLE *new_table,
1890  uchar *new_ptr, uchar *new_null_ptr,
1891  uint new_null_bit)
1892 {
1893  Field *tmp;
1894  if ((tmp= new_field(root, new_table, table == new_table)))
1895  {
1896  tmp->ptr= new_ptr;
1897  tmp->null_ptr= new_null_ptr;
1898  tmp->null_bit= new_null_bit;
1899  }
1900  return tmp;
1901 }
1902 
1904 {
1905  if (has_insert_default_function())
1907 }
1908 
1910 {
1911  if (has_update_default_function())
1913 }
1914 
1915 /****************************************************************************
1916  Field_null, a field that always return NULL
1917 ****************************************************************************/
1918 
1919 void Field_null::sql_type(String &res) const
1920 {
1921  res.set_ascii(STRING_WITH_LEN("null"));
1922 }
1923 
1924 
1925 /****************************************************************************
1926  Functions for the Field_decimal class
1927  This is an number stored as a pre-space (or pre-zero) string
1928 ****************************************************************************/
1929 
1930 type_conversion_status Field_decimal::reset(void)
1931 {
1932  Field_decimal::store(STRING_WITH_LEN("0"),&my_charset_bin);
1933  return TYPE_OK;
1934 }
1935 
1936 void Field_decimal::overflow(bool negative)
1937 {
1938  uint len=field_length;
1939  uchar *to=ptr, filler= '9';
1940 
1941  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1942  if (negative)
1943  {
1944  if (!unsigned_flag)
1945  {
1946  /* Put - sign as a first digit so we'll have -999..999 or 999..999 */
1947  *to++ = '-';
1948  len--;
1949  }
1950  else
1951  {
1952  filler= '0'; // Fill up with 0
1953  if (!zerofill)
1954  {
1955  /*
1956  Handle unsigned integer without zerofill, in which case
1957  the number should be of format ' 0' or ' 0.000'
1958  */
1959  uint whole_part=field_length- (dec ? dec+2 : 1);
1960  // Fill with spaces up to the first digit
1961  memset(to, ' ', whole_part);
1962  to+= whole_part;
1963  len-= whole_part;
1964  // The main code will also handle the 0 before the decimal point
1965  }
1966  }
1967  }
1968  memset(to, filler, len);
1969  if (dec)
1970  ptr[field_length-dec-1]='.';
1971  return;
1972 }
1973 
1974 
1975 type_conversion_status Field_decimal::store(const char *from_arg, uint len,
1976  const CHARSET_INFO *cs)
1977 {
1978  ASSERT_COLUMN_MARKED_FOR_WRITE;
1979  char buff[STRING_BUFFER_USUAL_SIZE];
1980  String tmp(buff,sizeof(buff), &my_charset_bin);
1981  const uchar *from= (uchar*) from_arg;
1982 
1983  /* Convert character set if the old one is multi uchar */
1984  if (cs->mbmaxlen > 1)
1985  {
1986  uint dummy_errors;
1987  tmp.copy((char*) from, len, cs, &my_charset_bin, &dummy_errors);
1988  from= (uchar*) tmp.ptr();
1989  len= tmp.length();
1990  }
1991 
1992  const uchar *end= from+len;
1993  /* The pointer where the field value starts (i.e., "where to write") */
1994  uchar *to= ptr;
1995  uint tmp_dec, tmp_uint;
1996  /*
1997  The sign of the number : will be 0 (means positive but sign not
1998  specified), '+' or '-'
1999  */
2000  uchar sign_char=0;
2001  /* The pointers where prezeros start and stop */
2002  const uchar *pre_zeros_from, *pre_zeros_end;
2003  /* The pointers where digits at the left of '.' start and stop */
2004  const uchar *int_digits_from, *int_digits_end;
2005  /* The pointers where digits at the right of '.' start and stop */
2006  const uchar *frac_digits_from, *frac_digits_end;
2007  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
2008  char expo_sign_char=0;
2009  uint exponent=0; // value of the exponent
2010  /*
2011  Pointers used when digits move from the left of the '.' to the
2012  right of the '.' (explained below)
2013  */
2014  const uchar *UNINIT_VAR(int_digits_tail_from);
2015  /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
2016  uint UNINIT_VAR(int_digits_added_zeros);
2017  /*
2018  Pointer used when digits move from the right of the '.' to the left
2019  of the '.'
2020  */
2021  const uchar *UNINIT_VAR(frac_digits_head_end);
2022  /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
2023  uint UNINIT_VAR(frac_digits_added_zeros);
2024  uchar *pos,*tmp_left_pos,*tmp_right_pos;
2025  /* Pointers that are used as limits (begin and end of the field buffer) */
2026  uchar *left_wall,*right_wall;
2027  uchar tmp_char;
2028  /*
2029  To remember if table->in_use->cuted_fields has already been incremented,
2030  to do that only once
2031  */
2032  bool is_cuted_fields_incr=0;
2033 
2034  /*
2035  There are three steps in this function :
2036  - parse the input string
2037  - modify the position of digits around the decimal dot '.'
2038  according to the exponent value (if specified)
2039  - write the formatted number
2040  */
2041 
2042  if ((tmp_dec=dec))
2043  tmp_dec++;
2044 
2045  /* skip pre-space */
2046  while (from != end && my_isspace(&my_charset_bin,*from))
2047  from++;
2048  if (from == end)
2049  {
2050  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
2051  is_cuted_fields_incr=1;
2052  }
2053  else if (*from == '+' || *from == '-') // Found some sign ?
2054  {
2055  sign_char= *from++;
2056  /*
2057  We allow "+" for unsigned decimal unless defined different
2058  Both options allowed as one may wish not to have "+" for unsigned numbers
2059  because of data processing issues
2060  */
2061  if (unsigned_flag)
2062  {
2063  if (sign_char=='-')
2064  {
2065  Field_decimal::overflow(1);
2066  return TYPE_WARN_OUT_OF_RANGE;
2067  }
2068  /*
2069  Defining this will not store "+" for unsigned decimal type even if
2070  it is passed in numeric string. This will make some tests to fail
2071  */
2072 #ifdef DONT_ALLOW_UNSIGNED_PLUS
2073  else
2074  sign_char=0;
2075 #endif
2076  }
2077  }
2078 
2079  pre_zeros_from= from;
2080  for (; from!=end && *from == '0'; from++) ; // Read prezeros
2081  pre_zeros_end=int_digits_from=from;
2082  /* Read non zero digits at the left of '.'*/
2083  for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
2084  int_digits_end=from;
2085  if (from!=end && *from == '.') // Some '.' ?
2086  from++;
2087  frac_digits_from= from;
2088  /* Read digits at the right of '.' */
2089  for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
2090  frac_digits_end=from;
2091  // Some exponentiation symbol ?
2092  if (from != end && (*from == 'e' || *from == 'E'))
2093  {
2094  from++;
2095  if (from != end && (*from == '+' || *from == '-')) // Some exponent sign ?
2096  expo_sign_char= *from++;
2097  else
2098  expo_sign_char= '+';
2099  /*
2100  Read digits of the exponent and compute its value. We must care about
2101  'exponent' overflow, because as unsigned arithmetic is "modulo", big
2102  exponents will become small (e.g. 1e4294967296 will become 1e0, and the
2103  field will finally contain 1 instead of its max possible value).
2104  */
2105  for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
2106  {
2107  exponent=10*exponent+(*from-'0');
2108  if (exponent>MAX_EXPONENT)
2109  break;
2110  }
2111  }
2112 
2113  /*
2114  We only have to generate warnings if count_cuted_fields is set.
2115  This is to avoid extra checks of the number when they are not needed.
2116  Even if this flag is not set, it's OK to increment warnings, if
2117  it makes the code easer to read.
2118  */
2119 
2120  if (table->in_use->count_cuted_fields)
2121  {
2122  // Skip end spaces
2123  for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
2124  if (from != end) // If still something left, warn
2125  {
2126  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
2127  is_cuted_fields_incr=1;
2128  }
2129  }
2130 
2131  /*
2132  Now "move" digits around the decimal dot according to the exponent value,
2133  and add necessary zeros.
2134  Examples :
2135  - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
2136  - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
2137  between '.' and '1'
2138  - 1234.5E-3 : '234' moves at the right of '.'
2139  These moves are implemented with pointers which point at the begin
2140  and end of each moved segment. Examples :
2141  - 1234.5E-3 : before the code below is executed, the int_digits part is
2142  from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2143  below, the int_digits part is from '1' to '1', the frac_digits_head
2144  part is from '2' to '4', and the frac_digits part from '5' to '5'.
2145  - 1234.5E3 : before the code below is executed, the int_digits part is
2146  from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2147  below, the int_digits part is from '1' to '4', the int_digits_tail
2148  part is from '5' to '5', the frac_digits part is empty, and
2149  int_digits_added_zeros=2 (to make 1234500).
2150  */
2151 
2152  /*
2153  Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
2154  as int_digits_added_zeros<=exponent<4G and
2155  (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
2156  (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
2157  */
2158 
2159  if (!expo_sign_char)
2160  tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
2161  else if (expo_sign_char == '-')
2162  {
2163  tmp_uint=min(exponent,(uint)(int_digits_end-int_digits_from));
2164  frac_digits_added_zeros=exponent-tmp_uint;
2165  int_digits_end -= tmp_uint;
2166  frac_digits_head_end=int_digits_end+tmp_uint;
2167  tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
2168  }
2169  else // (expo_sign_char=='+')
2170  {
2171  tmp_uint=min(exponent,(uint)(frac_digits_end-frac_digits_from));
2172  int_digits_added_zeros=exponent-tmp_uint;
2173  int_digits_tail_from=frac_digits_from;
2174  frac_digits_from=frac_digits_from+tmp_uint;
2175  /*
2176  We "eat" the heading zeros of the
2177  int_digits.int_digits_tail.int_digits_added_zeros concatenation
2178  (for example 0.003e3 must become 3 and not 0003)
2179  */
2180  if (int_digits_from == int_digits_end)
2181  {
2182  /*
2183  There was nothing in the int_digits part, so continue
2184  eating int_digits_tail zeros
2185  */
2186  for (; int_digits_tail_from != frac_digits_from &&
2187  *int_digits_tail_from == '0'; int_digits_tail_from++) ;
2188  if (int_digits_tail_from == frac_digits_from)
2189  {
2190  // there were only zeros in int_digits_tail too
2191  int_digits_added_zeros=0;
2192  }
2193  }
2194  tmp_uint= (uint) (tmp_dec+(int_digits_end-int_digits_from)+
2195  (uint)(frac_digits_from-int_digits_tail_from)+
2196  int_digits_added_zeros);
2197  }
2198 
2199  /*
2200  Now write the formated number
2201 
2202  First the digits of the int_% parts.
2203  Do we have enough room to write these digits ?
2204  If the sign is defined and '-', we need one position for it
2205  */
2206 
2207  if (field_length < tmp_uint + (int) (sign_char == '-'))
2208  {
2209  // too big number, change to max or min number
2210  Field_decimal::overflow(sign_char == '-');
2211  return TYPE_WARN_OUT_OF_RANGE;
2212  }
2213 
2214  /*
2215  Tmp_left_pos is the position where the leftmost digit of
2216  the int_% parts will be written
2217  */
2218  tmp_left_pos=pos=to+(uint)(field_length-tmp_uint);
2219 
2220  // Write all digits of the int_% parts
2221  while (int_digits_from != int_digits_end)
2222  *pos++ = *int_digits_from++ ;
2223 
2224  if (expo_sign_char == '+')
2225  {
2226  while (int_digits_tail_from != frac_digits_from)
2227  *pos++= *int_digits_tail_from++;
2228  while (int_digits_added_zeros-- >0)
2229  *pos++= '0';
2230  }
2231  /*
2232  Note the position where the rightmost digit of the int_% parts has been
2233  written (this is to later check if the int_% parts contained nothing,
2234  meaning an extra 0 is needed).
2235  */
2236  tmp_right_pos=pos;
2237 
2238  /*
2239  Step back to the position of the leftmost digit of the int_% parts,
2240  to write sign and fill with zeros or blanks or prezeros.
2241  */
2242  pos=tmp_left_pos-1;
2243  if (zerofill)
2244  {
2245  left_wall=to-1;
2246  while (pos > left_wall) // Fill with zeros
2247  *pos--='0';
2248  }
2249  else
2250  {
2251  left_wall=to+(sign_char != 0)-1;
2252  if (!expo_sign_char) // If exponent was specified, ignore prezeros
2253  {
2254  for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
2255  pre_zeros_from++)
2256  *pos--= '0';
2257  }
2258  if (pos == tmp_right_pos-1)
2259  *pos--= '0'; // no 0 has ever been written, so write one
2260  left_wall= to-1;
2261  if (sign_char && pos != left_wall)
2262  {
2263  /* Write sign if possible (it is if sign is '-') */
2264  *pos--= sign_char;
2265  }
2266  while (pos != left_wall)
2267  *pos--=' '; //fill with blanks
2268  }
2269 
2270  /*
2271  Write digits of the frac_% parts ;
2272  Depending on table->in_use->count_cutted_fields, we may also want
2273  to know if some non-zero tail of these parts will
2274  be truncated (for example, 0.002->0.00 will generate a warning,
2275  while 0.000->0.00 will not)
2276  (and 0E1000000000 will not, while 1E-1000000000 will)
2277  */
2278 
2279  pos=to+(uint)(field_length-tmp_dec); // Calculate post to '.'
2280  right_wall=to+field_length;
2281  if (pos != right_wall)
2282  *pos++='.';
2283 
2284  if (expo_sign_char == '-')
2285  {
2286  while (frac_digits_added_zeros-- > 0)
2287  {
2288  if (pos == right_wall)
2289  {
2290  if (table->in_use->count_cuted_fields && !is_cuted_fields_incr)
2291  break; // Go on below to see if we lose non zero digits
2292  return TYPE_OK;
2293  }
2294  *pos++='0';
2295  }
2296  while (int_digits_end != frac_digits_head_end)
2297  {
2298  tmp_char= *int_digits_end++;
2299  if (pos == right_wall)
2300  {
2301  if (tmp_char != '0') // Losing a non zero digit ?
2302  {
2303  if (!is_cuted_fields_incr)
2304  set_warning(Sql_condition::WARN_LEVEL_WARN,
2305  WARN_DATA_TRUNCATED, 1);
2306  return TYPE_OK;
2307  }
2308  continue;
2309  }
2310  *pos++= tmp_char;
2311  }
2312  }
2313 
2314  for (;frac_digits_from!=frac_digits_end;)
2315  {
2316  tmp_char= *frac_digits_from++;
2317  if (pos == right_wall)
2318  {
2319  if (tmp_char != '0') // Losing a non zero digit ?
2320  {
2321  if (!is_cuted_fields_incr)
2322  {
2323  /*
2324  This is a note, not a warning, as we don't want to abort
2325  when we cut decimals in strict mode
2326  */
2327  set_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
2328  }
2329  return TYPE_OK;
2330  }
2331  continue;
2332  }
2333  *pos++= tmp_char;
2334  }
2335 
2336  while (pos != right_wall)
2337  *pos++='0'; // Fill with zeros at right of '.'
2338  return TYPE_OK;
2339 }
2340 
2341 
2342 type_conversion_status Field_decimal::store(double nr)
2343 {
2344  ASSERT_COLUMN_MARKED_FOR_WRITE;
2345  if (unsigned_flag && nr < 0)
2346  {
2347  overflow(1);
2348  return TYPE_WARN_OUT_OF_RANGE;
2349  }
2350 
2351  if (!isfinite(nr)) // Handle infinity as special case
2352  {
2353  overflow(nr < 0.0);
2354  return TYPE_WARN_OUT_OF_RANGE;
2355  }
2356 
2357  reg4 uint i;
2358  size_t length;
2359  uchar fyllchar,*to;
2360  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
2361 
2362  fyllchar = zerofill ? (char) '0' : (char) ' ';
2363  length= my_fcvt(nr, dec, buff, NULL);
2364 
2365  if (length > field_length)
2366  {
2367  overflow(nr < 0.0);
2368  return TYPE_WARN_OUT_OF_RANGE;
2369  }
2370  else
2371  {
2372  to=ptr;
2373  for (i=field_length-length ; i-- > 0 ;)
2374  *to++ = fyllchar;
2375  memcpy(to,buff,length);
2376  return TYPE_OK;
2377  }
2378 }
2379 
2380 
2381 type_conversion_status Field_decimal::store(longlong nr, bool unsigned_val)
2382 {
2383  ASSERT_COLUMN_MARKED_FOR_WRITE;
2384  char buff[22];
2385  uint length, int_part;
2386  char fyllchar;
2387  uchar *to;
2388 
2389  if (nr < 0 && unsigned_flag && !unsigned_val)
2390  {
2391  overflow(1);
2392  return TYPE_WARN_OUT_OF_RANGE;
2393  }
2394  length= (uint) (longlong10_to_str(nr,buff,unsigned_val ? 10 : -10) - buff);
2395  int_part= field_length- (dec ? dec+1 : 0);
2396 
2397  if (length > int_part)
2398  {
2399  overflow(!unsigned_val && nr < 0L); /* purecov: inspected */
2400  return TYPE_WARN_OUT_OF_RANGE;
2401  }
2402 
2403  fyllchar = zerofill ? (char) '0' : (char) ' ';
2404  to= ptr;
2405  for (uint i=int_part-length ; i-- > 0 ;)
2406  *to++ = fyllchar;
2407  memcpy(to,buff,length);
2408  if (dec)
2409  {
2410  to[length]='.';
2411  memset(to + length + 1, '0', dec);
2412  }
2413  return TYPE_OK;
2414 }
2415 
2416 
2417 double Field_decimal::val_real(void)
2418 {
2419  ASSERT_COLUMN_MARKED_FOR_READ;
2420  int not_used;
2421  char *end_not_used;
2422  return my_strntod(&my_charset_bin, (char*) ptr, field_length, &end_not_used,
2423  &not_used);
2424 }
2425 
2426 longlong Field_decimal::val_int(void)
2427 {
2428  ASSERT_COLUMN_MARKED_FOR_READ;
2429  int not_used;
2430  if (unsigned_flag)
2431  return my_strntoull(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2432  &not_used);
2433  return my_strntoll(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2434  &not_used);
2435 }
2436 
2437 
2438 String *Field_decimal::val_str(String *val_buffer __attribute__((unused)),
2439  String *val_ptr)
2440 {
2441  ASSERT_COLUMN_MARKED_FOR_READ;
2442  uchar *str;
2443  size_t tmp_length;
2444 
2445  for (str=ptr ; *str == ' ' ; str++) ;
2446  val_ptr->set_charset(&my_charset_numeric);
2447  tmp_length= (size_t) (str-ptr);
2448  if (field_length < tmp_length) // Error in data
2449  val_ptr->length(0);
2450  else
2451  val_ptr->set_ascii((const char*) str, field_length-tmp_length);
2452  return val_ptr;
2453 }
2454 
2460 int Field_decimal::cmp(const uchar *a_ptr,const uchar *b_ptr)
2461 {
2462  const uchar *end;
2463  int swap=0;
2464  /* First remove prefixes '0', ' ', and '-' */
2465  for (end=a_ptr+field_length;
2466  a_ptr != end &&
2467  (*a_ptr == *b_ptr ||
2468  ((my_isspace(&my_charset_bin,*a_ptr) || *a_ptr == '+' ||
2469  *a_ptr == '0') &&
2470  (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' ||
2471  *b_ptr == '0')));
2472  a_ptr++,b_ptr++)
2473  {
2474  if (*a_ptr == '-') // If both numbers are negative
2475  swap= -1 ^ 1; // Swap result
2476  }
2477  if (a_ptr == end)
2478  return 0;
2479  if (*a_ptr == '-')
2480  return -1;
2481  if (*b_ptr == '-')
2482  return 1;
2483 
2484  while (a_ptr != end)
2485  {
2486  if (*a_ptr++ != *b_ptr++)
2487  return swap ^ (a_ptr[-1] < b_ptr[-1] ? -1 : 1); // compare digits
2488  }
2489  return 0;
2490 }
2491 
2492 
2493 void Field_decimal::make_sort_key(uchar *to, uint length)
2494 {
2495  uchar *str,*end;
2496  for (str=ptr,end=ptr+length;
2497  str != end &&
2498  ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
2499  *str == '0')) ;
2500  str++)
2501  *to++=' ';
2502  if (str == end)
2503  return; /* purecov: inspected */
2504 
2505  if (*str == '-')
2506  {
2507  *to++=1; // Smaller than any number
2508  str++;
2509  while (str != end)
2510  if (my_isdigit(&my_charset_bin,*str))
2511  *to++= (char) ('9' - *str++);
2512  else
2513  *to++= *str++;
2514  }
2515  else memcpy(to,str,(uint) (end-str));
2516 }
2517 
2518 
2519 void Field_decimal::sql_type(String &res) const
2520 {
2521  const CHARSET_INFO *cs=res.charset();
2522  uint tmp=field_length;
2523  if (!unsigned_flag)
2524  tmp--;
2525  if (dec)
2526  tmp--;
2527  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2528  "decimal(%d,%d)",tmp,dec));
2529  add_zerofill_and_unsigned(res);
2530 }
2531 
2532 
2533 /****************************************************************************
2534 ** Field_new_decimal
2535 ****************************************************************************/
2536 
2537 Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
2538  uint32 len_arg, uchar *null_ptr_arg,
2539  uchar null_bit_arg,
2540  enum utype unireg_check_arg,
2541  const char *field_name_arg,
2542  uint8 dec_arg,bool zero_arg,
2543  bool unsigned_arg)
2544  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2545  unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
2546 {
2547  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
2548  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
2549  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
2550  (dec <= DECIMAL_MAX_SCALE));
2551  bin_size= my_decimal_get_binary_size(precision, dec);
2552 }
2553 
2554 
2555 Field_new_decimal::Field_new_decimal(uint32 len_arg,
2556  bool maybe_null_arg,
2557  const char *name,
2558  uint8 dec_arg,
2559  bool unsigned_arg)
2560  :Field_num((uchar*) 0, len_arg,
2561  maybe_null_arg ? (uchar*) "": 0, 0,
2562  NONE, name, dec_arg, 0, unsigned_arg)
2563 {
2564  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
2565  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
2566  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
2567  (dec <= DECIMAL_MAX_SCALE));
2568  bin_size= my_decimal_get_binary_size(precision, dec);
2569 }
2570 
2571 
2572 Field *Field_new_decimal::create_from_item (Item *item)
2573 {
2574  uint8 dec= item->decimals;
2575  uint8 intg= item->decimal_precision() - dec;
2576  uint32 len= item->max_char_length();
2577 
2578  DBUG_ASSERT (item->result_type() == DECIMAL_RESULT);
2579 
2580  /*
2581  Trying to put too many digits overall in a DECIMAL(prec,dec)
2582  will always throw a warning. We must limit dec to
2583  DECIMAL_MAX_SCALE however to prevent an assert() later.
2584  */
2585 
2586  if (dec > 0)
2587  {
2588  signed int overflow;
2589 
2590  dec= min<int>(dec, DECIMAL_MAX_SCALE);
2591 
2592  /*
2593  If the value still overflows the field with the corrected dec,
2594  we'll throw out decimals rather than integers. This is still
2595  bad and of course throws a truncation warning.
2596  +1: for decimal point
2597  */
2598 
2599  const int required_length=
2600  my_decimal_precision_to_length(intg + dec, dec,
2601  item->unsigned_flag);
2602 
2603  overflow= required_length - len;
2604 
2605  if (overflow > 0)
2606  dec= max(0, dec - overflow); // too long, discard fract
2607  else
2608  /* Corrected value fits. */
2609  len= required_length;
2610  }
2611  return new Field_new_decimal(len, item->maybe_null, item->item_name.ptr(),
2612  dec, item->unsigned_flag);
2613 }
2614 
2615 
2616 type_conversion_status Field_new_decimal::reset(void)
2617 {
2618  store_value(&decimal_zero);
2619  return TYPE_OK;
2620 }
2621 
2622 
2631  bool sign)
2632 {
2633  DBUG_ENTER("Field_new_decimal::set_value_on_overflow");
2634  max_my_decimal(decimal_value, precision, decimals());
2635  if (sign)
2636  {
2637  if (unsigned_flag)
2638  my_decimal_set_zero(decimal_value);
2639  else
2640  decimal_value->sign(TRUE);
2641  }
2642  DBUG_VOID_RETURN;
2643 }
2644 
2645 
2660 type_conversion_status
2662 {
2663  ASSERT_COLUMN_MARKED_FOR_WRITE;
2664  type_conversion_status error= TYPE_OK;
2665  DBUG_ENTER("Field_new_decimal::store_value");
2666 #ifndef DBUG_OFF
2667  {
2668  char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
2669  DBUG_PRINT("enter", ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
2670  }
2671 #endif
2672 
2673  /* check that we do not try to write negative value in unsigned field */
2674  if (unsigned_flag && decimal_value->sign())
2675  {
2676  DBUG_PRINT("info", ("unsigned overflow"));
2677  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2678  error= TYPE_WARN_OUT_OF_RANGE;
2679  decimal_value= &decimal_zero;
2680  }
2681 #ifndef DBUG_OFF
2682  {
2683  char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
2684  DBUG_PRINT("info", ("saving with precision %d scale: %d value %s",
2685  (int)precision, (int)dec,
2686  dbug_decimal_as_string(dbug_buff, decimal_value)));
2687  }
2688 #endif
2689 
2690  int err= my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2691  decimal_value, ptr, precision, dec);
2692  if (warn_if_overflow(err))
2693  {
2694  my_decimal buff;
2695  DBUG_PRINT("info", ("overflow"));
2696  set_value_on_overflow(&buff, decimal_value->sign());
2697  my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
2698  }
2699  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2700  bin_size););
2701  DBUG_RETURN((err != E_DEC_OK) ? decimal_err_to_type_conv_status(err)
2702  : error);
2703 }
2704 
2705 
2706 type_conversion_status
2707 Field_new_decimal::store(const char *from, uint length,
2708  const CHARSET_INFO *charset_arg)
2709 {
2710  ASSERT_COLUMN_MARKED_FOR_WRITE;
2711  my_decimal decimal_value;
2712  DBUG_ENTER("Field_new_decimal::store(char*)");
2713 
2714  int err= str2my_decimal(E_DEC_FATAL_ERROR &
2715  ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
2716  from, length, charset_arg,
2717  &decimal_value);
2718 
2719  if (err != 0 && table->in_use->abort_on_warning)
2720  {
2721  ErrConvString errmsg(from, length, charset_arg);
2722  const Diagnostics_area *da= table->in_use->get_stmt_da();
2723  push_warning_printf(table->in_use, Sql_condition::WARN_LEVEL_WARN,
2724  ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
2725  ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
2726  "decimal", errmsg.ptr(), field_name,
2727  static_cast<ulong>(da->current_row_for_warning()));
2728  DBUG_RETURN(decimal_err_to_type_conv_status(err));
2729  }
2730 
2731  if (err != 0)
2732  set_decimal_warning(this, err, &decimal_value, from, length, charset_arg);
2733 
2734 #ifndef DBUG_OFF
2735  char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
2736  DBUG_PRINT("enter", ("value: %s",
2737  dbug_decimal_as_string(dbug_buff, &decimal_value)));
2738 #endif
2739 
2740  type_conversion_status store_stat= store_value(&decimal_value);
2741  DBUG_RETURN(err != 0 ? decimal_err_to_type_conv_status(err) : store_stat);
2742 }
2743 
2744 
2745 type_conversion_status
2746 store_internal_with_error_check(Field_new_decimal *field,
2747  int err, my_decimal *value)
2748 {
2749  type_conversion_status stat= TYPE_OK;
2750  if (err != 0)
2751  {
2752  if (field->check_overflow(err))
2753  {
2754  field->set_value_on_overflow(value, value->sign());
2755  stat= TYPE_WARN_OUT_OF_RANGE;
2756  }
2757  else if (field->check_truncated(err))
2758  stat= TYPE_NOTE_TRUNCATED;
2759  /* Only issue a warning if store_value doesn't issue an warning */
2760  field->table->in_use->got_warning= 0;
2761  }
2762  type_conversion_status store_stat= field->store_value(value);
2763  if (store_stat != TYPE_OK)
2764  return store_stat;
2765  else if (err != 0 && !field->table->in_use->got_warning)
2766  field->warn_if_overflow(err);
2767  return stat;
2768 }
2769 
2776 type_conversion_status Field_new_decimal::store(double nr)
2777 {
2778  ASSERT_COLUMN_MARKED_FOR_WRITE;
2779  DBUG_ENTER("Field_new_decimal::store(double)");
2780  my_decimal decimal_value;
2781 
2782  int conv_err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
2783  &decimal_value);
2784  DBUG_RETURN(store_internal_with_error_check(this, conv_err, &decimal_value));
2785 }
2786 
2787 type_conversion_status
2788 Field_new_decimal::store(longlong nr, bool unsigned_val)
2789 {
2790  ASSERT_COLUMN_MARKED_FOR_WRITE;
2791  DBUG_ENTER("Field_new_decimal::store(double, unsigned_val)");
2792  my_decimal decimal_value;
2793  int conv_err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2794  nr, unsigned_val, &decimal_value);
2795  DBUG_RETURN(store_internal_with_error_check(this, conv_err, &decimal_value));
2796 }
2797 
2798 
2799 type_conversion_status
2801 {
2802  ASSERT_COLUMN_MARKED_FOR_WRITE;
2803  return store_value(decimal_value);
2804 }
2805 
2806 
2807 type_conversion_status
2809  uint8 dec_arg __attribute__((unused)))
2810 {
2811  my_decimal decimal_value;
2812  return store_value(date2my_decimal(ltime, &decimal_value));
2813 }
2814 
2815 
2816 double Field_new_decimal::val_real(void)
2817 {
2818  ASSERT_COLUMN_MARKED_FOR_READ;
2819  double dbl;
2820  my_decimal decimal_value;
2821  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
2822  return dbl;
2823 }
2824 
2825 
2826 longlong Field_new_decimal::val_int(void)
2827 {
2828  ASSERT_COLUMN_MARKED_FOR_READ;
2829  longlong i;
2830  my_decimal decimal_value;
2831  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2832  unsigned_flag, &i);
2833  return i;
2834 }
2835 
2836 
2838 {
2839  ASSERT_COLUMN_MARKED_FOR_READ;
2840  DBUG_ENTER("Field_new_decimal::val_decimal");
2841  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
2842  precision, dec);
2843  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2844  bin_size););
2845  DBUG_RETURN(decimal_value);
2846 }
2847 
2848 
2849 String *Field_new_decimal::val_str(String *val_buffer,
2850  String *val_ptr __attribute__((unused)))
2851 {
2852  ASSERT_COLUMN_MARKED_FOR_READ;
2853  my_decimal decimal_value;
2854  uint fixed_precision= zerofill ? precision : 0;
2855  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2856  fixed_precision, dec, '0', val_buffer);
2857  val_buffer->set_charset(&my_charset_numeric);
2858  return val_buffer;
2859 }
2860 
2861 
2862 bool Field_new_decimal::get_date(MYSQL_TIME *ltime, uint fuzzydate)
2863 {
2864  my_decimal buf, *decimal_value= val_decimal(&buf);
2865  if (!decimal_value)
2866  {
2867  set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
2868  return true;
2869  }
2870  return my_decimal_to_datetime_with_warn(decimal_value, ltime, fuzzydate);
2871 }
2872 
2873 
2874 bool Field_new_decimal::get_time(MYSQL_TIME *ltime)
2875 {
2876  my_decimal buf, *decimal_value= val_decimal(&buf);
2877  if (!decimal_value)
2878  {
2879  set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
2880  return true;
2881  }
2882  return my_decimal_to_time_with_warn(decimal_value, ltime);
2883 }
2884 
2885 
2886 int Field_new_decimal::cmp(const uchar *a,const uchar*b)
2887 {
2888  return memcmp(a, b, bin_size);
2889 }
2890 
2891 
2892 void Field_new_decimal::make_sort_key(uchar *buff, uint length)
2893 {
2894  memcpy(buff, ptr, min<uint>(length, bin_size));
2895 }
2896 
2897 
2898 void Field_new_decimal::sql_type(String &str) const
2899 {
2900  const CHARSET_INFO *cs= str.charset();
2901  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
2902  "decimal(%d,%d)", precision, (int)dec));
2903  add_zerofill_and_unsigned(str);
2904 }
2905 
2906 
2918 int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
2919 {
2920  *metadata_ptr= precision;
2921  *(metadata_ptr + 1)= decimals();
2922  return 2;
2923 }
2924 
2925 
2939 {
2940  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
2941  uint const source_decimal= field_metadata & 0x00ff;
2942  uint const source_size= my_decimal_get_binary_size(source_precision,
2943  source_decimal);
2944  return (source_size);
2945 }
2946 
2963  Relay_log_info * __attribute__((unused)),
2964  uint16 mflags __attribute__((unused)),
2965  int *order_var)
2966 {
2967  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
2968  uint const source_decimal= field_metadata & 0x00ff;
2969  int order= compare(source_precision, precision);
2970  *order_var= order != 0 ? order : compare(source_decimal, dec);
2971  return true;
2972 }
2973 
2974 
2976 {
2977  return ((new_field->sql_type == real_type()) &&
2978  ((new_field->flags & UNSIGNED_FLAG) ==
2979  (uint) (flags & UNSIGNED_FLAG)) &&
2980  ((new_field->flags & AUTO_INCREMENT_FLAG) ==
2981  (uint) (flags & AUTO_INCREMENT_FLAG)) &&
2982  (new_field->length == max_display_length()) &&
2983  (new_field->decimals == dec));
2984 }
2985 
2986 
2999 const uchar *
3001  const uchar *from,
3002  uint param_data,
3003  bool low_byte_first)
3004 {
3005  if (param_data == 0)
3006  return Field::unpack(to, from, param_data, low_byte_first);
3007 
3008  uint from_precision= (param_data & 0xff00) >> 8U;
3009  uint from_decimal= param_data & 0x00ff;
3010  uint length=pack_length();
3011  uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
3012  uint len= (param_data && (from_pack_len < length)) ?
3013  from_pack_len : length;
3014  if ((from_pack_len && (from_pack_len < length)) ||
3015  (from_precision < precision) ||
3016  (from_decimal < decimals()))
3017  {
3018  /*
3019  If the master's data is smaller than the slave, we need to convert
3020  the binary to decimal then resize the decimal converting it back to
3021  a decimal and write that to the raw data buffer.
3022  */
3023  decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
3024  decimal_t dec_val;
3025  dec_val.len= from_precision;
3026  dec_val.buf= dec_buf;
3027  /*
3028  Note: bin2decimal does not change the length of the field. So it is
3029  just the first step the resizing operation. The second step does the
3030  resizing using the precision and decimals from the slave.
3031  */
3032  bin2decimal((uchar *)from, &dec_val, from_precision, from_decimal);
3033  decimal2bin(&dec_val, to, precision, decimals());
3034  }
3035  else
3036  memcpy(to, from, len); // Sizes are the same, just copy the data.
3037  return from+len;
3038 }
3039 
3040 /****************************************************************************
3041 ** tiny int
3042 ****************************************************************************/
3043 
3044 type_conversion_status
3045 Field_tiny::store(const char *from,uint len, const CHARSET_INFO *cs)
3046 {
3047  ASSERT_COLUMN_MARKED_FOR_WRITE;
3048  longlong rnd;
3049 
3050  const type_conversion_status error= get_int(cs, from, len, &rnd,
3051  255, -128, 127);
3052  ptr[0]= unsigned_flag ? (char) (ulonglong) rnd : (char) rnd;
3053  return error;
3054 }
3055 
3056 
3057 type_conversion_status Field_tiny::store(double nr)
3058 {
3059  ASSERT_COLUMN_MARKED_FOR_WRITE;
3060  type_conversion_status error= TYPE_OK;
3061  nr=rint(nr);
3062  if (unsigned_flag)
3063  {
3064  if (nr < 0.0)
3065  {
3066  *ptr=0;
3067  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3068  error= TYPE_WARN_OUT_OF_RANGE;
3069  }
3070  else if (nr > 255.0)
3071  {
3072  *ptr=(char) 255;
3073  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3074  error= TYPE_WARN_OUT_OF_RANGE;
3075  }
3076  else
3077  *ptr=(char) nr;
3078  }
3079  else
3080  {
3081  if (nr < -128.0)
3082  {
3083  *ptr= (char) -128;
3084  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3085  error= TYPE_WARN_OUT_OF_RANGE;
3086  }
3087  else if (nr > 127.0)
3088  {
3089  *ptr=127;
3090  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3091  error= TYPE_WARN_OUT_OF_RANGE;
3092  }
3093  else
3094  *ptr=(char) (int) nr;
3095  }
3096  return error;
3097 }
3098 
3099 
3100 type_conversion_status Field_tiny::store(longlong nr, bool unsigned_val)
3101 {
3102  ASSERT_COLUMN_MARKED_FOR_WRITE;
3103  type_conversion_status error= TYPE_OK;
3104 
3105  if (unsigned_flag)
3106  {
3107  if (nr < 0 && !unsigned_val)
3108  {
3109  *ptr= 0;
3110  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3111  error= TYPE_WARN_OUT_OF_RANGE;
3112  }
3113  else if ((ulonglong) nr > (ulonglong) 255)
3114  {
3115  *ptr= (char) 255;
3116  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3117  error= TYPE_WARN_OUT_OF_RANGE;
3118  }
3119  else
3120  *ptr=(char) nr;
3121  }
3122  else
3123  {
3124  if (nr < 0 && unsigned_val)
3125  nr= 256; // Generate overflow
3126  if (nr < -128)
3127  {
3128  *ptr= (char) -128;
3129  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3130  error= TYPE_WARN_OUT_OF_RANGE;
3131  }
3132  else if (nr > 127)
3133  {
3134  *ptr=127;
3135  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3136  error= TYPE_WARN_OUT_OF_RANGE;
3137  }
3138  else
3139  *ptr=(char) nr;
3140  }
3141  return error;
3142 }
3143 
3144 
3145 double Field_tiny::val_real(void)
3146 {
3147  ASSERT_COLUMN_MARKED_FOR_READ;
3148  int tmp= unsigned_flag ? (int) ptr[0] :
3149  (int) ((signed char*) ptr)[0];
3150  return (double) tmp;
3151 }
3152 
3153 
3154 longlong Field_tiny::val_int(void)
3155 {
3156  ASSERT_COLUMN_MARKED_FOR_READ;
3157  int tmp= unsigned_flag ? (int) ptr[0] :
3158  (int) ((signed char*) ptr)[0];
3159  return (longlong) tmp;
3160 }
3161 
3162 
3163 String *Field_tiny::val_str(String *val_buffer,
3164  String *val_ptr __attribute__((unused)))
3165 {
3166  ASSERT_COLUMN_MARKED_FOR_READ;
3167  const CHARSET_INFO *cs= &my_charset_numeric;
3168  uint length;
3169  uint mlength=max(field_length+1,5*cs->mbmaxlen);
3170  val_buffer->alloc(mlength);
3171  char *to=(char*) val_buffer->ptr();
3172 
3173  if (unsigned_flag)
3174  length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
3175  (long) *ptr);
3176  else
3177  length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
3178  (long) *((signed char*) ptr));
3179 
3180  val_buffer->length(length);
3181  if (zerofill)
3182  prepend_zeros(val_buffer);
3183  val_buffer->set_charset(cs);
3184  return val_buffer;
3185 }
3186 
3187 bool Field_tiny::send_binary(Protocol *protocol)
3188 {
3189  return protocol->store_tiny((longlong) (int8) ptr[0]);
3190 }
3191 
3192 int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
3193 {
3194  signed char a,b;
3195  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
3196  if (unsigned_flag)
3197  return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
3198  return (a < b) ? -1 : (a > b) ? 1 : 0;
3199 }
3200 
3201 void Field_tiny::make_sort_key(uchar *to, uint length)
3202 {
3203  DBUG_ASSERT(length >= 1);
3204  if (unsigned_flag)
3205  *to= *ptr;
3206  else
3207  to[0] = (char) (ptr[0] ^ (uchar) 128); /* Revers signbit */
3208 }
3209 
3210 void Field_tiny::sql_type(String &res) const
3211 {
3212  const CHARSET_INFO *cs=res.charset();
3213  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3214  "tinyint(%d)",(int) field_length));
3215  add_zerofill_and_unsigned(res);
3216 }
3217 
3218 /****************************************************************************
3219  Field type short int (2 byte)
3220 ****************************************************************************/
3221 
3222 type_conversion_status
3223 Field_short::store(const char *from,uint len, const CHARSET_INFO *cs)
3224 {
3225  ASSERT_COLUMN_MARKED_FOR_WRITE;
3226  int store_tmp;
3227  longlong rnd;
3228 
3229  const type_conversion_status error=
3230  get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
3231  store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
3232 #ifdef WORDS_BIGENDIAN
3233  if (table->s->db_low_byte_first)
3234  {
3235  int2store(ptr, store_tmp);
3236  }
3237  else
3238 #endif
3239  shortstore(ptr, (short) store_tmp);
3240  return error;
3241 }
3242 
3243 
3244 type_conversion_status Field_short::store(double nr)
3245 {
3246  ASSERT_COLUMN_MARKED_FOR_WRITE;
3247  type_conversion_status error= TYPE_OK;
3248  int16 res;
3249  nr=rint(nr);
3250  if (unsigned_flag)
3251  {
3252  if (nr < 0)
3253  {
3254  res=0;
3255  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3256  error= TYPE_WARN_OUT_OF_RANGE;
3257  }
3258  else if (nr > (double) UINT_MAX16)
3259  {
3260  res=(int16) UINT_MAX16;
3261  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3262  error= TYPE_WARN_OUT_OF_RANGE;
3263  }
3264  else
3265  res=(int16) (uint16) nr;
3266  }
3267  else
3268  {
3269  if (nr < (double) INT_MIN16)
3270  {
3271  res=INT_MIN16;
3272  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3273  error= TYPE_WARN_OUT_OF_RANGE;
3274  }
3275  else if (nr > (double) INT_MAX16)
3276  {
3277  res=INT_MAX16;
3278  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3279  error= TYPE_WARN_OUT_OF_RANGE;
3280  }
3281  else
3282  res=(int16) (int) nr;
3283  }
3284 #ifdef WORDS_BIGENDIAN
3285  if (table->s->db_low_byte_first)
3286  {
3287  int2store(ptr,res);
3288  }
3289  else
3290 #endif
3291  shortstore(ptr,res);
3292  return error;
3293 }
3294 
3295 
3296 type_conversion_status Field_short::store(longlong nr, bool unsigned_val)
3297 {
3298  ASSERT_COLUMN_MARKED_FOR_WRITE;
3299  type_conversion_status error= TYPE_OK;
3300  int16 res;
3301 
3302  if (unsigned_flag)
3303  {
3304  if (nr < 0L && !unsigned_val)
3305  {
3306  res=0;
3307  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3308  error= TYPE_WARN_OUT_OF_RANGE;
3309  }
3310  else if ((ulonglong) nr > (ulonglong) UINT_MAX16)
3311  {
3312  res=(int16) UINT_MAX16;
3313  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3314  error= TYPE_WARN_OUT_OF_RANGE;
3315  }
3316  else
3317  res=(int16) (uint16) nr;
3318  }
3319  else
3320  {
3321  if (nr < 0 && unsigned_val)
3322  nr= UINT_MAX16+1; // Generate overflow
3323 
3324  if (nr < INT_MIN16)
3325  {
3326  res=INT_MIN16;
3327  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3328  error= TYPE_WARN_OUT_OF_RANGE;
3329  }
3330  else if (nr > (longlong) INT_MAX16)
3331  {
3332  res=INT_MAX16;
3333  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3334  error= TYPE_WARN_OUT_OF_RANGE;
3335  }
3336  else
3337  res=(int16) nr;
3338  }
3339 #ifdef WORDS_BIGENDIAN
3340  if (table->s->db_low_byte_first)
3341  {
3342  int2store(ptr,res);
3343  }
3344  else
3345 #endif
3346  shortstore(ptr,res);
3347  return error;
3348 }
3349 
3350 
3351 double Field_short::val_real(void)
3352 {
3353  ASSERT_COLUMN_MARKED_FOR_READ;
3354  short j;
3355 #ifdef WORDS_BIGENDIAN
3356  if (table->s->db_low_byte_first)
3357  j=sint2korr(ptr);
3358  else
3359 #endif
3360  shortget(j,ptr);
3361  return unsigned_flag ? (double) (unsigned short) j : (double) j;
3362 }
3363 
3364 longlong Field_short::val_int(void)
3365 {
3366  ASSERT_COLUMN_MARKED_FOR_READ;
3367  short j;
3368 #ifdef WORDS_BIGENDIAN
3369  if (table->s->db_low_byte_first)
3370  j=sint2korr(ptr);
3371  else
3372 #endif
3373  shortget(j,ptr);
3374  return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
3375 }
3376 
3377 
3378 String *Field_short::val_str(String *val_buffer,
3379  String *val_ptr __attribute__((unused)))
3380 {
3381  ASSERT_COLUMN_MARKED_FOR_READ;
3382  const CHARSET_INFO *cs= &my_charset_numeric;
3383  uint length;
3384  uint mlength=max(field_length+1,7*cs->mbmaxlen);
3385  val_buffer->alloc(mlength);
3386  char *to=(char*) val_buffer->ptr();
3387  short j;
3388 #ifdef WORDS_BIGENDIAN
3389  if (table->s->db_low_byte_first)
3390  j=sint2korr(ptr);
3391  else
3392 #endif
3393  shortget(j,ptr);
3394 
3395  if (unsigned_flag)
3396  length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10,
3397  (long) (uint16) j);
3398  else
3399  length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
3400  val_buffer->length(length);
3401  if (zerofill)
3402  prepend_zeros(val_buffer);
3403  val_buffer->set_charset(cs);
3404  return val_buffer;
3405 }
3406 
3407 
3408 bool Field_short::send_binary(Protocol *protocol)
3409 {
3410  return protocol->store_short(Field_short::val_int());
3411 }
3412 
3413 
3414 int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
3415 {
3416  short a,b;
3417 #ifdef WORDS_BIGENDIAN
3418  if (table->s->db_low_byte_first)
3419  {
3420  a=sint2korr(a_ptr);
3421  b=sint2korr(b_ptr);
3422  }
3423  else
3424 #endif
3425  {
3426  shortget(a,a_ptr);
3427  shortget(b,b_ptr);
3428  }
3429 
3430  if (unsigned_flag)
3431  return ((unsigned short) a < (unsigned short) b) ? -1 :
3432  ((unsigned short) a > (unsigned short) b) ? 1 : 0;
3433  return (a < b) ? -1 : (a > b) ? 1 : 0;
3434 }
3435 
3436 void Field_short::make_sort_key(uchar *to, uint length)
3437 {
3438  DBUG_ASSERT(length >= 2);
3439 #ifdef WORDS_BIGENDIAN
3440  if (!table->s->db_low_byte_first)
3441  {
3442  if (unsigned_flag)
3443  to[0] = ptr[0];
3444  else
3445  to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
3446  to[1] = ptr[1];
3447  }
3448  else
3449 #endif
3450  {
3451  if (unsigned_flag)
3452  to[0] = ptr[1];
3453  else
3454  to[0] = (char) (ptr[1] ^ 128); /* Revers signbit */
3455  to[1] = ptr[0];
3456  }
3457 }
3458 
3459 void Field_short::sql_type(String &res) const
3460 {
3461  const CHARSET_INFO *cs=res.charset();
3462  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3463  "smallint(%d)",(int) field_length));
3464  add_zerofill_and_unsigned(res);
3465 }
3466 
3467 
3468 /****************************************************************************
3469  Field type medium int (3 byte)
3470 ****************************************************************************/
3471 
3472 type_conversion_status Field_medium::store(const char *from,uint len,
3473  const CHARSET_INFO *cs)
3474 {
3475  ASSERT_COLUMN_MARKED_FOR_WRITE;
3476  int store_tmp;
3477  longlong rnd;
3478 
3479  const type_conversion_status error=
3480  get_int(cs, from, len, &rnd, UINT_MAX24, INT_MIN24, INT_MAX24);
3481  store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
3482  int3store(ptr, store_tmp);
3483  return error;
3484 }
3485 
3486 
3487 type_conversion_status Field_medium::store(double nr)
3488 {
3489  ASSERT_COLUMN_MARKED_FOR_WRITE;
3490  type_conversion_status error= TYPE_OK;
3491  nr=rint(nr);
3492  if (unsigned_flag)
3493  {
3494  if (nr < 0)
3495  {
3496  int3store(ptr,0);
3497  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3498  error= TYPE_WARN_OUT_OF_RANGE;
3499  }
3500  else if (nr >= (double) (long) (1L << 24))
3501  {
3502  uint32 tmp=(uint32) (1L << 24)-1L;
3503  int3store(ptr,tmp);
3504  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3505  error= TYPE_WARN_OUT_OF_RANGE;
3506  }
3507  else
3508  int3store(ptr,(uint32) nr);
3509  }
3510  else
3511  {
3512  if (nr < (double) INT_MIN24)
3513  {
3514  long tmp=(long) INT_MIN24;
3515  int3store(ptr,tmp);
3516  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3517  error= TYPE_WARN_OUT_OF_RANGE;
3518  }
3519  else if (nr > (double) INT_MAX24)
3520  {
3521  long tmp=(long) INT_MAX24;
3522  int3store(ptr,tmp);
3523  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3524  error= TYPE_WARN_OUT_OF_RANGE;
3525  }
3526  else
3527  int3store(ptr,(long) nr);
3528  }
3529  return error;
3530 }
3531 
3532 
3533 type_conversion_status Field_medium::store(longlong nr, bool unsigned_val)
3534 {
3535  ASSERT_COLUMN_MARKED_FOR_WRITE;
3536  type_conversion_status error= TYPE_OK;
3537 
3538  if (unsigned_flag)
3539  {
3540  if (nr < 0 && !unsigned_val)
3541  {
3542  int3store(ptr,0);
3543  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3544  error= TYPE_WARN_OUT_OF_RANGE;
3545  }
3546  else if ((ulonglong) nr >= (ulonglong) (long) (1L << 24))
3547  {
3548  long tmp= (long) (1L << 24)-1L;
3549  int3store(ptr,tmp);
3550  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3551  error= TYPE_WARN_OUT_OF_RANGE;
3552  }
3553  else
3554  int3store(ptr,(uint32) nr);
3555  }
3556  else
3557  {
3558  if (nr < 0 && unsigned_val)
3559  nr= (ulonglong) (long) (1L << 24); // Generate overflow
3560 
3561  if (nr < (longlong) INT_MIN24)
3562  {
3563  long tmp= (long) INT_MIN24;
3564  int3store(ptr,tmp);
3565  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3566  error= TYPE_WARN_OUT_OF_RANGE;
3567  }
3568  else if (nr > (longlong) INT_MAX24)
3569  {
3570  long tmp=(long) INT_MAX24;
3571  int3store(ptr,tmp);
3572  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3573  error= TYPE_WARN_OUT_OF_RANGE;
3574  }
3575  else
3576  int3store(ptr,(long) nr);
3577  }
3578  return error;
3579 }
3580 
3581 
3582 double Field_medium::val_real(void)
3583 {
3584  ASSERT_COLUMN_MARKED_FOR_READ;
3585  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3586  return (double) j;
3587 }
3588 
3589 
3590 longlong Field_medium::val_int(void)
3591 {
3592  ASSERT_COLUMN_MARKED_FOR_READ;
3593  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3594  return (longlong) j;
3595 }
3596 
3597 
3598 String *Field_medium::val_str(String *val_buffer,
3599  String *val_ptr __attribute__((unused)))
3600 {
3601  ASSERT_COLUMN_MARKED_FOR_READ;
3602  const CHARSET_INFO *cs= &my_charset_numeric;
3603  uint length;
3604  uint mlength=max(field_length+1,10*cs->mbmaxlen);
3605  val_buffer->alloc(mlength);
3606  char *to=(char*) val_buffer->ptr();
3607  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3608 
3609  length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
3610  val_buffer->length(length);
3611  if (zerofill)
3612  prepend_zeros(val_buffer); /* purecov: inspected */
3613  val_buffer->set_charset(cs);
3614  return val_buffer;
3615 }
3616 
3617 
3618 bool Field_medium::send_binary(Protocol *protocol)
3619 {
3620  ASSERT_COLUMN_MARKED_FOR_READ;
3621  return protocol->store_long(Field_medium::val_int());
3622 }
3623 
3624 
3625 int Field_medium::cmp(const uchar *a_ptr, const uchar *b_ptr)
3626 {
3627  long a,b;
3628  if (unsigned_flag)
3629  {
3630  a=uint3korr(a_ptr);
3631  b=uint3korr(b_ptr);
3632  }
3633  else
3634  {
3635  a=sint3korr(a_ptr);
3636  b=sint3korr(b_ptr);
3637  }
3638  return (a < b) ? -1 : (a > b) ? 1 : 0;
3639 }
3640 
3641 void Field_medium::make_sort_key(uchar *to, uint length)
3642 {
3643  DBUG_ASSERT(length >= 3);
3644  if (unsigned_flag)
3645  to[0] = ptr[2];
3646  else
3647  to[0] = (uchar) (ptr[2] ^ 128); /* Revers signbit */
3648  to[1] = ptr[1];
3649  to[2] = ptr[0];
3650 }
3651 
3652 
3653 void Field_medium::sql_type(String &res) const
3654 {
3655  const CHARSET_INFO *cs=res.charset();
3656  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3657  "mediumint(%d)",(int) field_length));
3658  add_zerofill_and_unsigned(res);
3659 }
3660 
3661 /****************************************************************************
3662 ** long int
3663 ****************************************************************************/
3664 
3665 type_conversion_status Field_long::store(const char *from,uint len,
3666  const CHARSET_INFO *cs)
3667 {
3668  ASSERT_COLUMN_MARKED_FOR_WRITE;
3669  long store_tmp;
3670  longlong rnd;
3671 
3672  const type_conversion_status error=
3673  get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
3674  store_tmp= unsigned_flag ? (long) (ulonglong) rnd : (long) rnd;
3675 #ifdef WORDS_BIGENDIAN
3676  if (table->s->db_low_byte_first)
3677  {
3678  int4store(ptr, store_tmp);
3679  }
3680  else
3681 #endif
3682  longstore(ptr, store_tmp);
3683  return error;
3684 }
3685 
3686 
3687 type_conversion_status Field_long::store(double nr)
3688 {
3689  ASSERT_COLUMN_MARKED_FOR_WRITE;
3690  type_conversion_status error= TYPE_OK;
3691  int32 res;
3692  nr=rint(nr);
3693  if (unsigned_flag)
3694  {
3695  if (nr < 0)
3696  {
3697  res=0;
3698  error= TYPE_WARN_OUT_OF_RANGE;
3699  }
3700  else if (nr > (double) UINT_MAX32)
3701  {
3702  res= UINT_MAX32;
3703  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3704  error= TYPE_WARN_OUT_OF_RANGE;
3705  }
3706  else
3707  res=(int32) (ulong) nr;
3708  }
3709  else
3710  {
3711  if (nr < (double) INT_MIN32)
3712  {
3713  res=(int32) INT_MIN32;
3714  error= TYPE_WARN_OUT_OF_RANGE;
3715  }
3716  else if (nr > (double) INT_MAX32)
3717  {
3718  res=(int32) INT_MAX32;
3719  error= TYPE_WARN_OUT_OF_RANGE;
3720  }
3721  else
3722  res=(int32) (longlong) nr;
3723  }
3724  if (error)
3725  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3726 
3727 #ifdef WORDS_BIGENDIAN
3728  if (table->s->db_low_byte_first)
3729  {
3730  int4store(ptr,res);
3731  }
3732  else
3733 #endif
3734  longstore(ptr,res);
3735  return error;
3736 }
3737 
3738 
3748 type_conversion_status Field_long::store(longlong nr, bool unsigned_val)
3749 {
3750  ASSERT_COLUMN_MARKED_FOR_WRITE;
3751  type_conversion_status error= TYPE_OK;
3752  int32 res;
3753 
3754  if (unsigned_flag)
3755  {
3756  if (nr < 0 && !unsigned_val)
3757  {
3758  res=0;
3759  error= TYPE_WARN_OUT_OF_RANGE;
3760  }
3761  else if ((ulonglong) nr >= (LL(1) << 32))
3762  {
3763  res=(int32) (uint32) ~0L;
3764  error= TYPE_WARN_OUT_OF_RANGE;
3765  }
3766  else
3767  res=(int32) (uint32) nr;
3768  }
3769  else
3770  {
3771  if (nr < 0 && unsigned_val)
3772  {
3773  nr= ((longlong) INT_MAX32) + 1; // Generate overflow
3774  error= TYPE_WARN_OUT_OF_RANGE;
3775  }
3776  if (nr < (longlong) INT_MIN32)
3777  {
3778  res=(int32) INT_MIN32;
3779  error= TYPE_WARN_OUT_OF_RANGE;
3780  }
3781  else if (nr > (longlong) INT_MAX32)
3782  {
3783  res=(int32) INT_MAX32;
3784  error= TYPE_WARN_OUT_OF_RANGE;
3785  }
3786  else
3787  res=(int32) nr;
3788  }
3789  if (error)
3790  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3791 
3792 #ifdef WORDS_BIGENDIAN
3793  if (table->s->db_low_byte_first)
3794  {
3795  int4store(ptr,res);
3796  }
3797  else
3798 #endif
3799  longstore(ptr,res);
3800  return error;
3801 }
3802 
3803 
3804 double Field_long::val_real(void)
3805 {
3806  ASSERT_COLUMN_MARKED_FOR_READ;
3807  int32 j;
3808 #ifdef WORDS_BIGENDIAN
3809  if (table->s->db_low_byte_first)
3810  j=sint4korr(ptr);
3811  else
3812 #endif
3813  longget(j,ptr);
3814  return unsigned_flag ? (double) (uint32) j : (double) j;
3815 }
3816 
3817 longlong Field_long::val_int(void)
3818 {
3819  ASSERT_COLUMN_MARKED_FOR_READ;
3820  int32 j;
3821  /* See the comment in Field_long::store(long long) */
3822  DBUG_ASSERT(table->in_use == current_thd);
3823 #ifdef WORDS_BIGENDIAN
3824  if (table->s->db_low_byte_first)
3825  j=sint4korr(ptr);
3826  else
3827 #endif
3828  longget(j,ptr);
3829  return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
3830 }
3831 
3832 String *Field_long::val_str(String *val_buffer,
3833  String *val_ptr __attribute__((unused)))
3834 {
3835  ASSERT_COLUMN_MARKED_FOR_READ;
3836  const CHARSET_INFO *cs= &my_charset_numeric;
3837  uint length;
3838  uint mlength=max(field_length+1,12*cs->mbmaxlen);
3839  val_buffer->alloc(mlength);
3840  char *to=(char*) val_buffer->ptr();
3841  int32 j;
3842 #ifdef WORDS_BIGENDIAN
3843  if (table->s->db_low_byte_first)
3844  j=sint4korr(ptr);
3845  else
3846 #endif
3847  longget(j,ptr);
3848 
3849  if (unsigned_flag)
3850  length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
3851  else
3852  length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
3853  val_buffer->length(length);
3854  if (zerofill)
3855  prepend_zeros(val_buffer);
3856  val_buffer->set_charset(cs);
3857  return val_buffer;
3858 }
3859 
3860 
3861 bool Field_long::send_binary(Protocol *protocol)
3862 {
3863  ASSERT_COLUMN_MARKED_FOR_READ;
3864  return protocol->store_long(Field_long::val_int());
3865 }
3866 
3867 int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
3868 {
3869  int32 a,b;
3870 #ifdef WORDS_BIGENDIAN
3871  if (table->s->db_low_byte_first)
3872  {
3873  a=sint4korr(a_ptr);
3874  b=sint4korr(b_ptr);
3875  }
3876  else
3877 #endif
3878  {
3879  longget(a,a_ptr);
3880  longget(b,b_ptr);
3881  }
3882  if (unsigned_flag)
3883  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
3884  return (a < b) ? -1 : (a > b) ? 1 : 0;
3885 }
3886 
3887 void Field_long::make_sort_key(uchar *to, uint length)
3888 {
3889  DBUG_ASSERT(length >= 4);
3890 #ifdef WORDS_BIGENDIAN
3891  if (!table->s->db_low_byte_first)
3892  {
3893  if (unsigned_flag)
3894  to[0] = ptr[0];
3895  else
3896  to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
3897  to[1] = ptr[1];
3898  to[2] = ptr[2];
3899  to[3] = ptr[3];
3900  }
3901  else
3902 #endif
3903  {
3904  if (unsigned_flag)
3905  to[0] = ptr[3];
3906  else
3907  to[0] = (char) (ptr[3] ^ 128); /* Revers signbit */
3908  to[1] = ptr[2];
3909  to[2] = ptr[1];
3910  to[3] = ptr[0];
3911  }
3912 }
3913 
3914 
3915 void Field_long::sql_type(String &res) const
3916 {
3917  const CHARSET_INFO *cs=res.charset();
3918  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3919  "int(%d)",(int) field_length));
3920  add_zerofill_and_unsigned(res);
3921 }
3922 
3923 /****************************************************************************
3924  Field type longlong int (8 bytes)
3925 ****************************************************************************/
3926 
3927 type_conversion_status
3928 Field_longlong::store(const char *from,uint len, const CHARSET_INFO *cs)
3929 {
3930  ASSERT_COLUMN_MARKED_FOR_WRITE;
3931  int conv_err= 0;
3932  type_conversion_status error= TYPE_OK;
3933  char *end;
3934  ulonglong tmp;
3935 
3936  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&conv_err);
3937  if (conv_err == MY_ERRNO_ERANGE)
3938  {
3939  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3940  error= TYPE_WARN_OUT_OF_RANGE;
3941  }
3942  else if (table->in_use->count_cuted_fields &&
3943  check_int(cs, from, len, end, conv_err))
3944  error= TYPE_WARN_OUT_OF_RANGE;
3945  else
3946  error= TYPE_OK;
3947 
3948 #ifdef WORDS_BIGENDIAN
3949  if (table->s->db_low_byte_first)
3950  {
3951  int8store(ptr,tmp);
3952  }
3953  else
3954 #endif
3955  longlongstore(ptr,tmp);
3956  return error;
3957 }
3958 
3959 
3960 type_conversion_status Field_longlong::store(double nr)
3961 {
3962  ASSERT_COLUMN_MARKED_FOR_WRITE;
3963  type_conversion_status error= TYPE_OK;
3964  longlong res;
3965 
3966  nr= rint(nr);
3967  if (unsigned_flag)
3968  {
3969  if (nr < 0)
3970  {
3971  res=0;
3972  error= TYPE_WARN_OUT_OF_RANGE;
3973  }
3974  else if (nr >= (double) ULONGLONG_MAX)
3975  {
3976  res= ~(longlong) 0;
3977  error= TYPE_WARN_OUT_OF_RANGE;
3978  }
3979  else
3980  res=(longlong) double2ulonglong(nr);
3981  }
3982  else
3983  {
3984  if (nr <= (double) LONGLONG_MIN)
3985  {
3986  res= LONGLONG_MIN;
3987  if (nr < (double) LONGLONG_MIN)
3988  error= TYPE_WARN_OUT_OF_RANGE;
3989  }
3990  else if (nr >= (double) (ulonglong) LONGLONG_MAX)
3991  {
3992  res= LONGLONG_MAX;
3993  if (nr > (double) LONGLONG_MAX)
3994  error= TYPE_WARN_OUT_OF_RANGE;
3995  }
3996  else
3997  res=(longlong) nr;
3998  }
3999  if (error)
4000  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4001 
4002 #ifdef WORDS_BIGENDIAN
4003  if (table->s->db_low_byte_first)
4004  {
4005  int8store(ptr,res);
4006  }
4007  else
4008 #endif
4009  longlongstore(ptr,res);
4010  return error;
4011 }
4012 
4013 
4014 type_conversion_status Field_longlong::store(longlong nr, bool unsigned_val)
4015 {
4016  ASSERT_COLUMN_MARKED_FOR_WRITE;
4017  type_conversion_status error= TYPE_OK;
4018 
4019  if (nr < 0) // Only possible error
4020  {
4021  /*
4022  if field is unsigned and value is signed (< 0) or
4023  if field is signed and value is unsigned we have an overflow
4024  */
4025  if (unsigned_flag != unsigned_val)
4026  {
4027  nr= unsigned_flag ? (ulonglong) 0 : (ulonglong) LONGLONG_MAX;
4028  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4029  error= TYPE_WARN_OUT_OF_RANGE;
4030  }
4031  }
4032 
4033 #ifdef WORDS_BIGENDIAN
4034  if (table->s->db_low_byte_first)
4035  {
4036  int8store(ptr,nr);
4037  }
4038  else
4039 #endif
4040  longlongstore(ptr,nr);
4041  return error;
4042 }
4043 
4044 
4045 double Field_longlong::val_real(void)
4046 {
4047  ASSERT_COLUMN_MARKED_FOR_READ;
4048  longlong j;
4049 #ifdef WORDS_BIGENDIAN
4050  if (table->s->db_low_byte_first)
4051  {
4052  j=sint8korr(ptr);
4053  }
4054  else
4055 #endif
4056  longlongget(j,ptr);
4057  /* The following is open coded to avoid a bug in gcc 3.3 */
4058  if (unsigned_flag)
4059  {
4060  ulonglong tmp= (ulonglong) j;
4061  return ulonglong2double(tmp);
4062  }
4063  return (double) j;
4064 }
4065 
4066 
4067 longlong Field_longlong::val_int(void)
4068 {
4069  ASSERT_COLUMN_MARKED_FOR_READ;
4070  longlong j;
4071 #ifdef WORDS_BIGENDIAN
4072  if (table->s->db_low_byte_first)
4073  j=sint8korr(ptr);
4074  else
4075 #endif
4076  longlongget(j,ptr);
4077  return j;
4078 }
4079 
4080 
4081 String *Field_longlong::val_str(String *val_buffer,
4082  String *val_ptr __attribute__((unused)))
4083 {
4084  const CHARSET_INFO *cs= &my_charset_numeric;
4085  uint length;
4086  uint mlength=max(field_length+1,22*cs->mbmaxlen);
4087  val_buffer->alloc(mlength);
4088  char *to=(char*) val_buffer->ptr();
4089  longlong j;
4090 #ifdef WORDS_BIGENDIAN
4091  if (table->s->db_low_byte_first)
4092  j=sint8korr(ptr);
4093  else
4094 #endif
4095  longlongget(j,ptr);
4096 
4097  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
4098  unsigned_flag ? 10 : -10, j);
4099  val_buffer->length(length);
4100  if (zerofill)
4101  prepend_zeros(val_buffer);
4102  val_buffer->set_charset(cs);
4103  return val_buffer;
4104 }
4105 
4106 
4107 bool Field_longlong::send_binary(Protocol *protocol)
4108 {
4109  ASSERT_COLUMN_MARKED_FOR_READ;
4110  return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
4111 }
4112 
4113 
4114 int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr)
4115 {
4116  longlong a,b;
4117 #ifdef WORDS_BIGENDIAN
4118  if (table->s->db_low_byte_first)
4119  {
4120  a=sint8korr(a_ptr);
4121  b=sint8korr(b_ptr);
4122  }
4123  else
4124 #endif
4125  {
4126  longlongget(a,a_ptr);
4127  longlongget(b,b_ptr);
4128  }
4129  if (unsigned_flag)
4130  return ((ulonglong) a < (ulonglong) b) ? -1 :
4131  ((ulonglong) a > (ulonglong) b) ? 1 : 0;
4132  return (a < b) ? -1 : (a > b) ? 1 : 0;
4133 }
4134 
4135 
4136 void Field_longlong::make_sort_key(uchar *to, uint length)
4137 {
4138  const int from_length= PACK_LENGTH;
4139  const int to_length= min<int>(from_length, length);
4140 #ifdef WORDS_BIGENDIAN
4141  if (table == NULL || !table->s->db_low_byte_first)
4142  copy_integer<true>(to, to_length, ptr, from_length, unsigned_flag);
4143  else
4144 #endif
4145  copy_integer<false>(to, to_length, ptr, from_length, unsigned_flag);
4146 }
4147 
4148 
4149 void Field_longlong::sql_type(String &res) const
4150 {
4151  const CHARSET_INFO *cs=res.charset();
4152  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4153  "bigint(%d)",(int) field_length));
4154  add_zerofill_and_unsigned(res);
4155 }
4156 
4157 
4158 /*
4159  Floating-point numbers
4160  */
4161 
4162 uchar *
4163 Field_real::pack(uchar *to, const uchar *from,
4164  uint max_length, bool low_byte_first)
4165 {
4166  DBUG_ENTER("Field_real::pack");
4167  DBUG_ASSERT(max_length >= pack_length());
4168 #ifdef WORDS_BIGENDIAN
4169  if (low_byte_first != table->s->db_low_byte_first)
4170  {
4171  const uchar *dptr= from + pack_length();
4172  while (dptr-- > from)
4173  *to++ = *dptr;
4174  DBUG_RETURN(to);
4175  }
4176  else
4177 #endif
4178  DBUG_RETURN(Field::pack(to, from, max_length, low_byte_first));
4179 }
4180 
4181 const uchar *
4182 Field_real::unpack(uchar *to, const uchar *from,
4183  uint param_data, bool low_byte_first)
4184 {
4185  DBUG_ENTER("Field_real::unpack");
4186 #ifdef WORDS_BIGENDIAN
4187  if (low_byte_first != table->s->db_low_byte_first)
4188  {
4189  const uchar *dptr= from + pack_length();
4190  while (dptr-- > from)
4191  *to++ = *dptr;
4192  DBUG_RETURN(from + pack_length());
4193  }
4194  else
4195 #endif
4196  DBUG_RETURN(Field::unpack(to, from, param_data, low_byte_first));
4197 }
4198 
4199 
4200 type_conversion_status
4202  uint8 dec_arg __attribute__((unused)))
4203 {
4204  double nr= TIME_to_double(ltime);
4205  return store(ltime->neg ? -nr : nr);
4206 }
4207 
4208 
4209 /****************************************************************************
4210  single precision float
4211 ****************************************************************************/
4212 
4213 type_conversion_status
4214 Field_float::store(const char *from,uint len, const CHARSET_INFO *cs)
4215 {
4216  int conv_error;
4217  type_conversion_status err= TYPE_OK;
4218  char *end;
4219  double nr= my_strntod(cs,(char*) from,len,&end,&conv_error);
4220  if (conv_error || (!len || ((uint) (end-from) != len &&
4221  table->in_use->count_cuted_fields)))
4222  {
4223  set_warning(Sql_condition::WARN_LEVEL_WARN,
4224  (conv_error ? ER_WARN_DATA_OUT_OF_RANGE
4225  : WARN_DATA_TRUNCATED),
4226  1);
4227  err= conv_error ? TYPE_WARN_OUT_OF_RANGE : TYPE_WARN_TRUNCATED;
4228  }
4229  Field_float::store(nr);
4230  return err;
4231 }
4232 
4233 
4234 type_conversion_status Field_float::store(double nr)
4235 {
4236  ASSERT_COLUMN_MARKED_FOR_WRITE;
4237  const type_conversion_status error=
4238  truncate(&nr, FLT_MAX) ? TYPE_WARN_OUT_OF_RANGE : TYPE_OK;
4239 
4240  float j= (float)nr;
4241 
4242 #ifdef WORDS_BIGENDIAN
4243  if (table->s->db_low_byte_first)
4244  {
4245  float4store(ptr,j);
4246  }
4247  else
4248 #endif
4249  memcpy(ptr, &j, sizeof(j));
4250  return error;
4251 }
4252 
4253 
4254 type_conversion_status Field_float::store(longlong nr, bool unsigned_val)
4255 {
4256  return Field_float::store(unsigned_val ? ulonglong2double((ulonglong) nr) :
4257  (double) nr);
4258 }
4259 
4260 
4261 double Field_float::val_real(void)
4262 {
4263  ASSERT_COLUMN_MARKED_FOR_READ;
4264  float j;
4265 #ifdef WORDS_BIGENDIAN
4266  if (table->s->db_low_byte_first)
4267  {
4268  float4get(j,ptr);
4269  }
4270  else
4271 #endif
4272  memcpy(&j, ptr, sizeof(j));
4273  return ((double) j);
4274 }
4275 
4276 longlong Field_float::val_int(void)
4277 {
4278  float j;
4279 #ifdef WORDS_BIGENDIAN
4280  if (table->s->db_low_byte_first)
4281  {
4282  float4get(j,ptr);
4283  }
4284  else
4285 #endif
4286  memcpy(&j, ptr, sizeof(j));
4287  return (longlong) rint(j);
4288 }
4289 
4290 
4291 String *Field_float::val_str(String *val_buffer,
4292  String *val_ptr __attribute__((unused)))
4293 {
4294  ASSERT_COLUMN_MARKED_FOR_READ;
4295  DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4296  float nr;
4297 #ifdef WORDS_BIGENDIAN
4298  if (table->s->db_low_byte_first)
4299  {
4300  float4get(nr,ptr);
4301  }
4302  else
4303 #endif
4304  memcpy(&nr, ptr, sizeof(nr));
4305 
4306  uint to_length= 70;
4307  if (val_buffer->alloc(to_length))
4308  {
4309  my_error(ER_OUT_OF_RESOURCES, MYF(0));
4310  return val_buffer;
4311  }
4312 
4313  char *to=(char*) val_buffer->ptr();
4314  size_t len;
4315 
4316  if (dec >= NOT_FIXED_DEC)
4317  len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
4318  else
4319  {
4320  /*
4321  We are safe here because the buffer length is 70, and
4322  fabs(float) < 10^39, dec < NOT_FIXED_DEC. So the resulting string
4323  will be not longer than 69 chars + terminating '\0'.
4324  */
4325  len= my_fcvt(nr, dec, to, NULL);
4326  }
4327  val_buffer->length((uint) len);
4328  if (zerofill)
4329  prepend_zeros(val_buffer);
4330  val_buffer->set_charset(&my_charset_numeric);
4331  return val_buffer;
4332 }
4333 
4334 
4335 int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
4336 {
4337  float a,b;
4338 #ifdef WORDS_BIGENDIAN
4339  if (table->s->db_low_byte_first)
4340  {
4341  float4get(a,a_ptr);
4342  float4get(b,b_ptr);
4343  }
4344  else
4345 #endif
4346  {
4347  memcpy(&a, a_ptr, sizeof(float));
4348  memcpy(&b, b_ptr, sizeof(float));
4349  }
4350  return (a < b) ? -1 : (a > b) ? 1 : 0;
4351 }
4352 
4353 #define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)
4354 
4355 void Field_float::make_sort_key(uchar *to, uint length)
4356 {
4357  DBUG_ASSERT(length >= 4);
4358  float nr;
4359 #ifdef WORDS_BIGENDIAN
4360  if (table->s->db_low_byte_first)
4361  {
4362  float4get(nr,ptr);
4363  }
4364  else
4365 #endif
4366  memcpy(&nr, ptr, min<uint>(length, sizeof(float)));
4367 
4368  uchar *tmp= to;
4369  if (nr == (float) 0.0)
4370  { /* Change to zero string */
4371  tmp[0]=(uchar) 128;
4372  memset(tmp + 1, 0, min<uint>(length, sizeof(nr) - 1));
4373  }
4374  else
4375  {
4376 #ifdef WORDS_BIGENDIAN
4377  memcpy(tmp, &nr, sizeof(nr));
4378 #else
4379  tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
4380 #endif
4381  if (tmp[0] & 128) /* Negative */
4382  { /* make complement */
4383  uint i;
4384  for (i=0 ; i < sizeof(nr); i++)
4385  tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
4386  }
4387  else
4388  {
4389  ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
4390  (ushort) 32768);
4391  exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
4392  tmp[0]= (uchar) (exp_part >> 8);
4393  tmp[1]= (uchar) exp_part;
4394  }
4395  }
4396 }
4397 
4398 
4399 bool Field_float::send_binary(Protocol *protocol)
4400 {
4401  ASSERT_COLUMN_MARKED_FOR_READ;
4402  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
4403 }
4404 
4405 
4415 int Field_float::do_save_field_metadata(uchar *metadata_ptr)
4416 {
4417  *metadata_ptr= pack_length();
4418  return 1;
4419 }
4420 
4421 
4422 void Field_float::sql_type(String &res) const
4423 {
4424  if (dec == NOT_FIXED_DEC)
4425  {
4426  res.set_ascii(STRING_WITH_LEN("float"));
4427  }
4428  else
4429  {
4430  const CHARSET_INFO *cs= res.charset();
4431  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4432  "float(%d,%d)",(int) field_length,dec));
4433  }
4434  add_zerofill_and_unsigned(res);
4435 }
4436 
4437 
4438 /****************************************************************************
4439  double precision floating point numbers
4440 ****************************************************************************/
4441 
4442 type_conversion_status
4443 Field_double::store(const char *from,uint len, const CHARSET_INFO *cs)
4444 {
4445  int conv_error;
4446  type_conversion_status error= TYPE_OK;
4447  char *end;
4448  double nr= my_strntod(cs,(char*) from, len, &end, &conv_error);
4449  if ((conv_error != 0) || (!len || ((uint) (end-from) != len &&
4450  table->in_use->count_cuted_fields)))
4451  {
4452  set_warning(Sql_condition::WARN_LEVEL_WARN,
4453  (conv_error ? ER_WARN_DATA_OUT_OF_RANGE
4454  : WARN_DATA_TRUNCATED),
4455  1);
4456  error= conv_error ? TYPE_WARN_OUT_OF_RANGE : TYPE_WARN_TRUNCATED;
4457  }
4458  Field_double::store(nr);
4459  return error;
4460 }
4461 
4462 
4463 type_conversion_status Field_double::store(double nr)
4464 {
4465  ASSERT_COLUMN_MARKED_FOR_WRITE;
4466  const type_conversion_status error=
4467  truncate(&nr, DBL_MAX) ? TYPE_WARN_OUT_OF_RANGE : TYPE_OK;
4468 
4469 #ifdef WORDS_BIGENDIAN
4470  if (table->s->db_low_byte_first)
4471  {
4472  float8store(ptr,nr);
4473  }
4474  else
4475 #endif
4476  doublestore(ptr,nr);
4477  return error;
4478 }
4479 
4480 
4481 type_conversion_status Field_double::store(longlong nr, bool unsigned_val)
4482 {
4483  return Field_double::store(unsigned_val ? ulonglong2double((ulonglong) nr) :
4484  (double) nr);
4485 }
4486 
4487 /*
4488  If a field has fixed length, truncate the double argument pointed to by 'nr'
4489  appropriately.
4490  Also ensure that the argument is within [-max_value; max_value] range.
4491 */
4492 
4493 bool Field_real::truncate(double *nr, double max_value)
4494 {
4495  if (isnan(*nr))
4496  {
4497  *nr= 0;
4498  set_null();
4499  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4500  return true;
4501  }
4502  else if (unsigned_flag && *nr < 0)
4503  {
4504  *nr= 0;
4505  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4506  return true;
4507  }
4508 
4509  if (!not_fixed)
4510  {
4511  uint order= field_length - dec;
4512  uint step= array_elements(log_10) - 1;
4513  max_value= 1.0;
4514  for (; order > step; order-= step)
4515  max_value*= log_10[step];
4516  max_value*= log_10[order];
4517  max_value-= 1.0 / log_10[dec];
4518 
4519  /* Check for infinity so we don't get NaN in calculations */
4520  if (!my_isinf(*nr))
4521  {
4522  double tmp= rint((*nr - floor(*nr)) * log_10[dec]) / log_10[dec];
4523  *nr= floor(*nr) + tmp;
4524  }
4525  }
4526 
4527  if (*nr < -max_value)
4528  {
4529  *nr= -max_value;
4530  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4531  return true;
4532  }
4533  else if (*nr > max_value)
4534  {
4535  *nr= max_value;
4536  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4537  return true;
4538  }
4539 
4540  return false;
4541 }
4542 
4543 
4544 type_conversion_status Field_real::store_decimal(const my_decimal *dm)
4545 {
4546  double dbl;
4547  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
4548  return store(dbl);
4549 }
4550 
4551 double Field_double::val_real(void)
4552 {
4553  ASSERT_COLUMN_MARKED_FOR_READ;
4554  double j;
4555 #ifdef WORDS_BIGENDIAN
4556  if (table->s->db_low_byte_first)
4557  {
4558  float8get(j,ptr);
4559  }
4560  else
4561 #endif
4562  doubleget(j,ptr);
4563  return j;
4564 }
4565 
4566 longlong Field_double::val_int(void)
4567 {
4568  ASSERT_COLUMN_MARKED_FOR_READ;
4569  double j;
4570  longlong res;
4571 #ifdef WORDS_BIGENDIAN
4572  if (table->s->db_low_byte_first)
4573  {
4574  float8get(j,ptr);
4575  }
4576  else
4577 #endif
4578  doubleget(j,ptr);
4579  /* Check whether we fit into longlong range */
4580  if (j <= (double) LONGLONG_MIN)
4581  {
4582  res= (longlong) LONGLONG_MIN;
4583  goto warn;
4584  }
4585  if (j >= (double) (ulonglong) LONGLONG_MAX)
4586  {
4587  res= (longlong) LONGLONG_MAX;
4588  goto warn;
4589  }
4590  return (longlong) rint(j);
4591 
4592 warn:
4593  {
4594  char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
4595  String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
4596  str= val_str(&tmp, 0);
4597  ErrConvString err(str);
4598  push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
4599  ER_TRUNCATED_WRONG_VALUE,
4600  ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
4601  err.ptr());
4602  }
4603  return res;
4604 }
4605 
4606 
4608 {
4609  ASSERT_COLUMN_MARKED_FOR_READ;
4610  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
4611  return decimal_value;
4612 }
4613 
4614 
4615 bool Field_real::get_date(MYSQL_TIME *ltime, uint fuzzydate)
4616 {
4617  return my_double_to_datetime_with_warn(val_real(), ltime, fuzzydate);
4618 }
4619 
4620 
4621 bool Field_real::get_time(MYSQL_TIME *ltime)
4622 {
4623  return my_double_to_time_with_warn(val_real(), ltime);
4624 }
4625 
4626 
4627 String *Field_double::val_str(String *val_buffer,
4628  String *val_ptr __attribute__((unused)))
4629 {
4630  ASSERT_COLUMN_MARKED_FOR_READ;
4631  DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4632  double nr;
4633 #ifdef WORDS_BIGENDIAN
4634  if (table->s->db_low_byte_first)
4635  {
4636  float8get(nr,ptr);
4637  }
4638  else
4639 #endif
4640  doubleget(nr,ptr);
4641  uint to_length= DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE;
4642  if (val_buffer->alloc(to_length))
4643  {
4644  my_error(ER_OUT_OF_RESOURCES, MYF(0));
4645  return val_buffer;
4646  }
4647 
4648  char *to=(char*) val_buffer->ptr();
4649  size_t len;
4650 
4651  if (dec >= NOT_FIXED_DEC)
4652  len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
4653  else
4654  len= my_fcvt(nr, dec, to, NULL);
4655 
4656  val_buffer->length((uint) len);
4657  if (zerofill)
4658  prepend_zeros(val_buffer);
4659  val_buffer->set_charset(&my_charset_numeric);
4660  return val_buffer;
4661 }
4662 
4663 bool Field_double::send_binary(Protocol *protocol)
4664 {
4665  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
4666 }
4667 
4668 
4669 int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
4670 {
4671  double a,b;
4672 #ifdef WORDS_BIGENDIAN
4673  if (table->s->db_low_byte_first)
4674  {
4675  float8get(a,a_ptr);
4676  float8get(b,b_ptr);
4677  }
4678  else
4679 #endif
4680  {
4681  doubleget(a, a_ptr);
4682  doubleget(b, b_ptr);
4683  }
4684  return (a < b) ? -1 : (a > b) ? 1 : 0;
4685 }
4686 
4687 
4688 #define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
4689 
4690 /* The following should work for IEEE */
4691 
4692 void Field_double::make_sort_key(uchar *to, uint length)
4693 {
4694  double nr;
4695 #ifdef WORDS_BIGENDIAN
4696  if (table->s->db_low_byte_first)
4697  {
4698  float8get(nr, ptr);
4699  }
4700  else
4701 #endif
4702  doubleget(nr, ptr);
4703  if (length < 8)
4704  {
4705  uchar buff[8];
4706  change_double_for_sort(nr, buff);
4707  memcpy(to, buff, length);
4708  }
4709  else
4710  change_double_for_sort(nr, to);
4711 }
4712 
4713 
4724 int Field_double::do_save_field_metadata(uchar *metadata_ptr)
4725 {
4726  *metadata_ptr= pack_length();
4727  return 1;
4728 }
4729 
4730 
4731 void Field_double::sql_type(String &res) const
4732 {
4733  const CHARSET_INFO *cs=res.charset();
4734  if (dec == NOT_FIXED_DEC)
4735  {
4736  res.set_ascii(STRING_WITH_LEN("double"));
4737  }
4738  else
4739  {
4740  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4741  "double(%d,%d)",(int) field_length,dec));
4742  }
4743  add_zerofill_and_unsigned(res);
4744 }
4745 
4746 
4747 /****************************************************************************
4748 ** Common code for all temporal data types: DATE, DATETIME, TIMESTAMP, TIME
4749 *****************************************************************************/
4750 
4752 {
4753  return new_field->sql_type == real_type() &&
4754  new_field->decimals == decimals();
4755 }
4756 
4757 
4759 {
4760  ASSERT_COLUMN_MARKED_FOR_READ;
4761  DBUG_ASSERT(decimals() == 0);
4762  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 0, decimal_value);
4763  return decimal_value;
4764 }
4765 
4766 
4774 void
4776 {
4777  int cut_incremented= 0;
4778  timestamp_type ts_type= field_type_to_timestamp_type(type());
4779 
4780  if (warnings & MYSQL_TIME_WARN_TRUNCATED)
4781  {
4782  set_datetime_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4783  str, ts_type, !cut_incremented);
4784  cut_incremented= 1;
4785  }
4786  if (warnings & (MYSQL_TIME_WARN_OUT_OF_RANGE | MYSQL_TIME_WARN_ZERO_DATE |
4787  MYSQL_TIME_WARN_ZERO_IN_DATE))
4788  {
4789  set_datetime_warning(Sql_condition::WARN_LEVEL_WARN,
4790  ER_WARN_DATA_OUT_OF_RANGE,
4791  str, ts_type, !cut_incremented);
4792  cut_incremented= 1;
4793  }
4794  if (warnings & MYSQL_TIME_WARN_INVALID_TIMESTAMP)
4795  {
4796  set_datetime_warning(Sql_condition::WARN_LEVEL_WARN,
4797  ER_WARN_INVALID_TIMESTAMP,
4798  str, ts_type, !cut_incremented);
4799  cut_incremented= 1;
4800  }
4801  if ((warnings & MYSQL_TIME_NOTE_TRUNCATED) &&
4802  !(warnings & MYSQL_TIME_WARN_TRUNCATED))
4803  {
4804  set_datetime_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED,
4805  str, ts_type, !cut_incremented);
4806  }
4807 }
4808 
4809 
4810 type_conversion_status Field_temporal::store(longlong nr, bool unsigned_val)
4811 {
4812  ASSERT_COLUMN_MARKED_FOR_WRITE;
4813  int warnings= 0;
4814  MYSQL_TIME ltime;
4815  type_conversion_status error= convert_number_to_TIME(nr, unsigned_val, 0,
4816  &ltime, &warnings);
4817  if (error == TYPE_OK || error == TYPE_NOTE_TRUNCATED)
4818  error= store_internal(&ltime, &warnings);
4819  else
4820  {
4821  DBUG_ASSERT(warnings != 0); // Must be set by convert_number_to_TIME
4822 
4823  if (warnings & (MYSQL_TIME_WARN_ZERO_DATE |
4824  MYSQL_TIME_WARN_ZERO_IN_DATE) &&
4825  !current_thd->is_strict_mode())
4826  error= TYPE_NOTE_TIME_TRUNCATED;
4827  }
4828  if (warnings)
4829  set_warnings(ErrConvString(nr, unsigned_val), warnings);
4830  return error;
4831 }
4832 
4833 
4834 type_conversion_status
4835 Field_temporal::store_lldiv_t(const lldiv_t *lld, int *warnings)
4836 {
4837  ASSERT_COLUMN_MARKED_FOR_WRITE;
4838  type_conversion_status error;
4839  MYSQL_TIME ltime;
4840  error= convert_number_to_TIME(lld->quot, 0, lld->rem, &ltime, warnings);
4841  if (error == TYPE_OK || error == TYPE_NOTE_TRUNCATED)
4842  error= store_internal_with_round(&ltime, warnings);
4843  else if (!*warnings)
4844  {
4845  DBUG_ASSERT(warnings != 0); // Must be set by convert_number_to_TIME
4846  if (((*warnings & MYSQL_TIME_WARN_ZERO_DATE) != 0 ||
4847  (*warnings & MYSQL_TIME_WARN_ZERO_IN_DATE) != 0) &&
4848  !current_thd->is_strict_mode())
4849  error= TYPE_NOTE_TIME_TRUNCATED;
4850  }
4851 
4852  return error;
4853 }
4854 
4855 
4856 type_conversion_status Field_temporal::store_decimal(const my_decimal *decimal)
4857 {
4858  ASSERT_COLUMN_MARKED_FOR_WRITE;
4859  lldiv_t lld;
4860  int warnings= 0;
4861  /* Pass 0 in the first argument, not to produce warnings automatically */
4862  my_decimal2lldiv_t(0, decimal, &lld);
4863  const type_conversion_status error= store_lldiv_t(&lld, &warnings);
4864  if (warnings)
4865  set_warnings(ErrConvString(decimal), warnings);
4866  return error;
4867 }
4868 
4869 
4870 type_conversion_status Field_temporal::store(double nr)
4871 {
4872  ASSERT_COLUMN_MARKED_FOR_WRITE;
4873  int warnings= 0;
4874  lldiv_t lld;
4875  double2lldiv_t(nr, &lld);
4876  const type_conversion_status error= store_lldiv_t(&lld, &warnings);
4877  if (warnings)
4878  set_warnings(ErrConvString(nr), warnings);
4879  return error;
4880 }
4881 
4882 
4894 type_conversion_status
4895 Field_temporal::store(const char *str, uint len, const CHARSET_INFO *cs)
4896 {
4897  ASSERT_COLUMN_MARKED_FOR_WRITE;
4898  type_conversion_status error= TYPE_OK;
4899  MYSQL_TIME ltime;
4900  MYSQL_TIME_STATUS status;
4901  if (convert_str_to_TIME(str, len, cs, &ltime, &status))
4902  {
4903  /*
4904  When convert_str_to_TIME() returns error, ltime has been set to
4905  0 so there's nothing to store in the field.
4906  */
4907  reset();
4908  if (status.warnings & (MYSQL_TIME_WARN_ZERO_DATE |
4909  MYSQL_TIME_WARN_ZERO_IN_DATE) &&
4910  !current_thd->is_strict_mode())
4911  error= TYPE_NOTE_TIME_TRUNCATED;
4912  else
4913  error= TYPE_ERR_BAD_VALUE;
4914  }
4915  else
4916  {
4917  error= time_warning_to_type_conversion_status(status.warnings);
4918 
4919  const type_conversion_status tmp_error= store_internal_with_round(&ltime,
4920  &status.warnings);
4921 
4922  // Return the most serious error of the two, see type_conversion_status
4923  if (tmp_error > error)
4924  error= tmp_error;
4925  }
4926  if (status.warnings)
4927  set_warnings(ErrConvString(str, len, cs), status.warnings);
4928  return error;
4929 }
4930 
4931 
4936 longlong
4937 Field_temporal::convert_number_to_datetime(longlong nr, bool unsigned_val,
4938  MYSQL_TIME *ltime, int *warnings)
4939 {
4940  /*
4941  Note, number_to_datetime can return a result different from nr:
4942  e.g. 111111 -> 20111111000000
4943  */
4944  longlong tmp= number_to_datetime(nr, ltime, date_flags(), warnings);
4945  if (tmp == LL(-1))
4946  reset();
4947  return tmp;
4948 }
4949 
4950 
4951 /****************************************************************************
4952 ** Common code for temporal data types with date: DATE, DATETIME, TIMESTAMP
4953 *****************************************************************************/
4954 
4955 
4957  uint fuzzydate)
4958 {
4959  if (get_date_internal(ltime)) /* '0000-00-00' */
4960  {
4961  DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
4962  if (fuzzydate & TIME_NO_ZERO_DATE)
4963  return true;
4964  set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
4965  }
4966  return false;
4967 }
4968 
4969 
4971 {
4972  ASSERT_COLUMN_MARKED_FOR_READ;
4973  MYSQL_TIME ltime;
4974  return get_date_internal(&ltime) ?
4975  0 : TIME_to_longlong_datetime_packed(&ltime);
4976 }
4977 
4978 
4980 {
4981  /*
4982  There are currently no tests covering this method,
4983  as DATETIME seems to always superseed over TIME in comparison.
4984  */
4985  ASSERT_COLUMN_MARKED_FOR_READ;
4986  MYSQL_TIME ltime;
4987  return get_date_internal(&ltime) ? 0 : TIME_to_longlong_time_packed(&ltime);
4988 }
4989 
4990 
4998 static inline int
4999 my_datetime_number_to_str(char *pos, longlong tmp)
5000 {
5001  long part1= (long) (tmp / 1000000LL);
5002  long part2= (long) (tmp - (ulonglong) part1 * 1000000LL);
5003  int part3;
5004  pos+= MAX_DATETIME_WIDTH; /* Start from the end */
5005  *pos--= 0;
5006  *pos--= (char) ('0' + (char) (part2 % 10)); /* Seconds */
5007  part2 /= 10;
5008  *pos--= (char) ('0' + (char) (part2 % 10));
5009  part3= (int) (part2 / 10);
5010  *pos--= ':';
5011  *pos--= (char) ('0' + (char) (part3 % 10)); /* Minutes */
5012  part3 /= 10;
5013  *pos--= (char) ('0' + (char) (part3 % 10));
5014  part3 /= 10;
5015  *pos--= ':';
5016  *pos--= (char) ('0' + (char) (part3 % 10)); /* Hours */
5017  part3 /= 10;
5018  *pos--= (char) ('0' + (char) part3);
5019  *pos--= ' ';
5020  *pos--= (char) ('0' + (char) (part1 % 10)); /* Day */
5021  part1 /= 10;
5022  *pos--= (char) ('0' + (char) (part1 % 10));
5023  part1 /= 10;
5024  *pos--= '-';
5025  *pos--= (char) ('0' + (char) (part1 % 10)); /* Month */
5026  part1 /= 10;
5027  *pos--= (char) ('0' + (char) (part1 % 10));
5028  part3= (int) (part1 / 10);
5029  *pos--= '-';
5030  *pos--= (char) ('0' + (char) (part3 % 10)); /* Year */
5031  part3 /= 10;
5032  *pos--= (char) ('0' + (char) (part3 % 10));
5033  part3 /= 10;
5034  *pos--= (char) ('0' + (char) (part3 % 10));
5035  part3 /= 10;
5036  *pos= (char) ('0'+ (char) part3);
5037  return MAX_DATETIME_WIDTH;
5038 }
5039 
5040 
5041 String *Field_temporal_with_date::val_str(String *val_buffer, String *val_ptr)
5042 {
5043  ASSERT_COLUMN_MARKED_FOR_READ;
5044  MYSQL_TIME ltime;
5045  val_buffer->alloc(field_length + 1);
5046  val_buffer->set_charset(&my_charset_numeric);
5047  if (get_date_internal(&ltime))
5048  {
5049  val_buffer->set_ascii(my_zero_datetime6, field_length);
5050  return val_buffer;
5051  }
5052  make_datetime((DATE_TIME_FORMAT *) 0, &ltime, val_buffer, dec);
5053  return val_buffer;
5054 }
5055 
5056 
5057 type_conversion_status
5059  bool unsigned_val,
5060  int nanoseconds,
5061  MYSQL_TIME *ltime,
5062  int *warnings)
5063 {
5064  if (nr < 0 || nanoseconds < 0)
5065  {
5066  reset();
5067  *warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
5068  return TYPE_WARN_OUT_OF_RANGE;
5069  }
5070 
5071  if (convert_number_to_datetime(nr, unsigned_val, ltime, warnings) == LL(-1))
5072  return TYPE_ERR_BAD_VALUE;
5073 
5074  if (ltime->time_type == MYSQL_TIMESTAMP_DATE && nanoseconds)
5075  {
5076  *warnings|= MYSQL_TIME_WARN_TRUNCATED;
5077  return TYPE_NOTE_TRUNCATED;
5078  }
5079 
5080  ltime->second_part= 0;
5081  if (datetime_add_nanoseconds_with_round(ltime, nanoseconds, warnings))
5082  {
5083  reset();
5084  return TYPE_WARN_OUT_OF_RANGE;
5085  }
5086  return TYPE_OK;
5087 }
5088 
5089 
5090 type_conversion_status
5092  uint8 dec_arg __attribute__((unused)))
5093 {
5094  ASSERT_COLUMN_MARKED_FOR_WRITE;
5095  type_conversion_status error;
5096  int warnings= 0;
5097 
5098  switch (ltime->time_type) // TS-TODO: split into separate methods?
5099  {
5100  case MYSQL_TIMESTAMP_DATETIME:
5101  case MYSQL_TIMESTAMP_DATE:
5102  if (check_date(ltime, non_zero_date(ltime), date_flags(), &warnings))
5103  {
5104  DBUG_ASSERT(warnings &
5105  (MYSQL_TIME_WARN_OUT_OF_RANGE |
5106  MYSQL_TIME_WARN_ZERO_DATE |
5107  MYSQL_TIME_WARN_ZERO_IN_DATE));
5108 
5109  error= time_warning_to_type_conversion_status(warnings);
5110  reset();
5111  }
5112  else
5113  error= store_internal_with_round(ltime, &warnings);
5114  break;
5115  case MYSQL_TIMESTAMP_TIME:
5116  {
5117  /* Convert TIME to DATETIME */
5118  THD *thd= table ? table->in_use : current_thd;
5119  MYSQL_TIME ltime2;
5120  time_to_datetime(thd, ltime, &ltime2);
5121  error= store_internal_with_round(&ltime2, &warnings);
5122  break;
5123  }
5124  case MYSQL_TIMESTAMP_NONE:
5125  case MYSQL_TIMESTAMP_ERROR:
5126  default:
5127  warnings|= MYSQL_TIME_WARN_TRUNCATED;
5128  reset();
5129  error= TYPE_WARN_TRUNCATED;
5130  }
5131 
5132  if (warnings)
5133  set_warnings(ErrConvString(ltime, decimals()), warnings);
5134  return error;
5135 }
5136 
5137 
5138 bool
5140  const CHARSET_INFO *cs,
5141  MYSQL_TIME *ltime,
5142  MYSQL_TIME_STATUS *status)
5143 {
5144  return str_to_datetime(cs, str, len, ltime, date_flags(), status);
5145 }
5146 
5147 
5148 bool Field_temporal_with_date::send_binary(Protocol *protocol)
5149 {
5150  MYSQL_TIME ltime;
5151  if (get_date_internal(&ltime))
5152  {
5153  // Only MYSQL_TYPE_TIMESTAMP can return an error in get_date_internal()
5154  DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
5155  set_zero_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
5156  }
5157  return protocol->store(&ltime, 0);
5158 }
5159 
5160 
5161 type_conversion_status
5163  int *warnings)
5164 {
5165  if (my_datetime_round(ltime, dec, warnings))
5166  {
5167  reset();
5168  return time_warning_to_type_conversion_status(*warnings);
5169  }
5170  else
5171  return store_internal(ltime, warnings);
5172 }
5173 
5174 
5181 type_conversion_status Field_temporal_with_date::validate_stored_val(THD *thd)
5182 {
5183  MYSQL_TIME ltime;
5184  type_conversion_status error= TYPE_OK;
5185  int warnings= 0;
5186 
5187  if (is_real_null())
5188  return error;
5189 
5190  memset(&ltime, 0, sizeof(MYSQL_TIME));
5191  get_date_internal(&ltime);
5192  if (check_date(&ltime, non_zero_date(&ltime), date_flags(), &warnings))
5193  error= time_warning_to_type_conversion_status(warnings);
5194 
5195  if (warnings)
5196  {
5197  ltime.time_type = field_type_to_timestamp_type(type());
5198  set_warnings(ErrConvString(&ltime, dec), warnings);
5199  }
5200 
5201  return error;
5202 }
5203 
5204 
5205 /****************************************************************************
5206 ** Common code for data types with date and time: DATETIME, TIMESTAMP
5207 *****************************************************************************/
5208 
5209 
5211 {
5212  ASSERT_COLUMN_MARKED_FOR_WRITE;
5213  if (!my_time_fraction_remainder(tm->tv_usec, decimals()))
5214  {
5215  store_timestamp_internal(tm);
5216  return;
5217  }
5218  struct timeval tm2= *tm;
5219  my_timeval_round(&tm2, decimals());
5220  store_timestamp_internal(&tm2);
5221 }
5222 
5223 
5224 bool
5225 Field_temporal_with_date_and_time::convert_TIME_to_timestamp(THD *thd,
5226  const MYSQL_TIME *ltime,
5227  struct timeval *tm,
5228  int *warnings)
5229 {
5230  /*
5231  No needs to do check_date(TIME_NO_ZERO_IN_DATE),
5232  because it has been done earlier in
5233  store_time(), number_to_datetime() or str_to_datetime().
5234  */
5235  if (datetime_with_no_zero_in_date_to_timeval(thd, ltime, tm, warnings))
5236  {
5237  tm->tv_sec= tm->tv_usec= 0;
5238  return true;
5239  }
5240  return false;
5241 }
5242 
5243 
5245 {
5246  if (unireg_check != NONE)
5247  {
5248  /*
5249  This TIMESTAMP column is hereby quietly assumed to have an insert or
5250  update default function.
5251  */
5252  flags|= TIMESTAMP_FLAG;
5253  if (unireg_check != TIMESTAMP_DN_FIELD)
5254  flags|= ON_UPDATE_NOW_FLAG;
5255  }
5256 }
5257 
5258 /****************************************************************************
5259 ** Common code for DATETIME(N) and TIMESTAMP(N)
5260 *****************************************************************************/
5261 
5262 double Field_temporal_with_date_and_timef::val_real()
5263 {
5264  ASSERT_COLUMN_MARKED_FOR_READ;
5265  MYSQL_TIME ltime;
5266  return get_date_internal(&ltime) ? 0 : TIME_to_double_datetime(&ltime);
5267 }
5268 
5269 
5270 longlong Field_temporal_with_date_and_timef::val_int()
5271 {
5272  ASSERT_COLUMN_MARKED_FOR_READ;
5273  MYSQL_TIME ltime;
5274  return get_date_internal(&ltime) ?
5275  0 : TIME_to_ulonglong_datetime_round(&ltime);
5276 }
5277 
5278 
5280 {
5281  ASSERT_COLUMN_MARKED_FOR_READ;
5282  MYSQL_TIME ltime;
5283  if (get_date_internal(&ltime))
5284  {
5285  // Only MYSQL_TYPE_TIMESTAMP can return an error in get_date_internal()
5286  DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
5287  set_zero_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
5288  }
5289  return date2my_decimal(&ltime, dec);
5290 }
5291 
5292 
5335 Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg,
5336  uchar *null_ptr_arg, uchar null_bit_arg,
5337  enum utype unireg_check_arg,
5338  const char *field_name_arg)
5339  :Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
5340  unireg_check_arg, field_name_arg, 0)
5341 {
5343  /* For 4.0 MYD and 4.0 InnoDB compatibility */
5344  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
5345 }
5346 
5347 
5348 Field_timestamp::Field_timestamp(bool maybe_null_arg,
5349  const char *field_name_arg)
5350  :Field_temporal_with_date_and_time((uchar *) 0,
5351  maybe_null_arg ? (uchar *) "" : 0, 0,
5352  NONE, field_name_arg, 0)
5353 {
5355  /* For 4.0 MYD and 4.0 InnoDB compatibility */
5356  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
5357 }
5358 
5359 
5360 ulonglong Field_timestamp::date_flags(const THD *thd)
5361 {
5362  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
5363  return (thd->variables.sql_mode & MODE_NO_ZERO_DATE) | MODE_NO_ZERO_IN_DATE;
5364 }
5365 
5366 
5367 type_conversion_status
5368 Field_timestamp::store_internal(const MYSQL_TIME *ltime, int *warnings)
5369 {
5370  THD *thd= table ? table->in_use : current_thd;
5371  struct timeval tm;
5372  convert_TIME_to_timestamp(thd, ltime, &tm, warnings);
5373  const type_conversion_status error=
5374  time_warning_to_type_conversion_status(*warnings);
5376  return error;
5377 }
5378 
5379 
5386 {
5387  ASSERT_COLUMN_MARKED_FOR_READ;
5388  uint32 temp;
5389  THD *thd= table ? table->in_use : current_thd;
5390 #ifdef WORDS_BIGENDIAN
5391  if (table && table->s->db_low_byte_first)
5392  temp= uint4korr(ptr);
5393  else
5394 #endif
5395  longget(temp, ptr);
5396  if (!temp)
5397  return true;
5398  thd->time_zone()->gmt_sec_to_TIME(ltime, (my_time_t) temp);
5399  return false;
5400 }
5401 
5402 
5406 bool Field_timestamp::get_timestamp(struct timeval *tm, int *warnings)
5407 {
5408  if (is_null())
5409  return true;
5410  tm->tv_usec= 0;
5411 #ifdef WORDS_BIGENDIAN
5412  if (table && table->s->db_low_byte_first)
5413  {
5414  tm->tv_sec= sint4korr(ptr);
5415  return false;
5416  }
5417 #endif
5418  long tmp;
5419  longget(tmp,ptr);
5420  tm->tv_sec= tmp;
5421  return false;
5422 }
5423 
5424 
5426 {
5427 #ifdef WORDS_BIGENDIAN
5428  if (table && table->s->db_low_byte_first)
5429  {
5430  int4store(ptr, tm->tv_sec);
5431  }
5432  else
5433 #endif
5434  longstore(ptr, (uint32) tm->tv_sec);
5435 }
5436 
5437 
5438 type_conversion_status Field_timestamp::store_packed(longlong nr)
5439 {
5440  /* Make sure the stored value was previously properly rounded or truncated */
5441  DBUG_ASSERT((MY_PACKED_TIME_GET_FRAC_PART(nr) %
5442  (int) log_10_int[DATETIME_MAX_DECIMALS - decimals()]) == 0);
5443  MYSQL_TIME ltime;
5444  TIME_from_longlong_datetime_packed(&ltime, nr);
5445  return Field_timestamp::store_time(&ltime, 0);
5446 }
5447 
5448 
5449 longlong Field_timestamp::val_int()
5450 {
5451  ASSERT_COLUMN_MARKED_FOR_READ;
5452  MYSQL_TIME ltime;
5453  return get_date_internal(&ltime) ? 0 : TIME_to_ulonglong_datetime(&ltime);
5454 }
5455 
5456 
5457 bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5458 {
5459  /* Don't do check_fuzzy_date() as month and year are never 0 for timestamp */
5460  return get_internal_check_zero(ltime, fuzzydate);
5461 }
5462 
5463 
5464 int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
5465 {
5466  int32 a,b;
5467 #ifdef WORDS_BIGENDIAN
5468  if (table && table->s->db_low_byte_first)
5469  {
5470  a=sint4korr(a_ptr);
5471  b=sint4korr(b_ptr);
5472  }
5473  else
5474 #endif
5475  {
5476  longget(a,a_ptr);
5477  longget(b,b_ptr);
5478  }
5479  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
5480 }
5481 
5482 
5483 void Field_timestamp::make_sort_key(uchar *to,uint length __attribute__((unused)))
5484 {
5485 #ifdef WORDS_BIGENDIAN
5486  if (!table || !table->s->db_low_byte_first)
5487  {
5488  to[0] = ptr[0];
5489  to[1] = ptr[1];
5490  to[2] = ptr[2];
5491  to[3] = ptr[3];
5492  }
5493  else
5494 #endif
5495  {
5496  to[0] = ptr[3];
5497  to[1] = ptr[2];
5498  to[2] = ptr[1];
5499  to[3] = ptr[0];
5500  }
5501 }
5502 
5503 
5504 void Field_timestamp::sql_type(String &res) const
5505 {
5506  res.set_ascii(STRING_WITH_LEN("timestamp"));
5507 }
5508 
5509 
5510 type_conversion_status Field_timestamp::validate_stored_val(THD *thd)
5511 {
5512  /*
5513  While deprecating "TIMESTAMP with implicit DEFAULT value", we can
5514  remove this function implementation and depend directly on
5515  "Field_temporal_with_date::validate_stored_val"
5516  */
5517  if (!thd->variables.explicit_defaults_for_timestamp)
5518  return TYPE_OK;
5519 
5521 }
5522 
5523 
5524 /****************************************************************************
5525 ** timestamp(N) type
5526 ** In string context: YYYY-MM-DD HH:MM:SS.FFFFFF
5527 ** In number context: YYYYMMDDHHMMSS.FFFFFF
5528 ** Stored as a 7 byte value
5529 ****************************************************************************/
5531  uchar *null_ptr_arg, uchar null_bit_arg,
5532  enum utype unireg_check_arg,
5533  const char *field_name_arg,
5534  uint8 dec_arg)
5535  :Field_temporal_with_date_and_timef(ptr_arg, null_ptr_arg, null_bit_arg,
5536  unireg_check_arg, field_name_arg,
5537  dec_arg)
5538 {
5540 }
5541 
5542 
5544  const char *field_name_arg,
5545  uint8 dec_arg)
5547  maybe_null_arg ? (uchar*) "": 0, 0,
5548  NONE, field_name_arg, dec_arg)
5549 {
5550  if (unireg_check != TIMESTAMP_DN_FIELD)
5551  flags|= ON_UPDATE_NOW_FLAG;
5552 }
5553 
5554 
5555 ulonglong Field_timestampf::date_flags(const THD *thd)
5556 {
5557  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
5558  return (thd->variables.sql_mode & MODE_NO_ZERO_DATE) | MODE_NO_ZERO_IN_DATE;
5559 }
5560 
5561 
5562 type_conversion_status Field_timestampf::reset()
5563 {
5564  memset(ptr, 0, pack_length());
5565  return TYPE_OK;
5566 }
5567 
5568 
5570 {
5571  my_timestamp_to_binary(tm, ptr, dec);
5572 }
5573 
5574 
5575 type_conversion_status
5576 Field_timestampf::store_internal(const MYSQL_TIME *ltime, int *warnings)
5577 {
5578  THD *thd= table ? table->in_use : current_thd;
5579  struct timeval tm;
5580  convert_TIME_to_timestamp(thd, ltime, &tm, warnings);
5581  const type_conversion_status error=
5582  time_warning_to_type_conversion_status(*warnings);
5584  return error;
5585 }
5586 
5587 
5588 type_conversion_status Field_timestampf::store_packed(longlong nr)
5589 {
5590  MYSQL_TIME ltime;
5591  TIME_from_longlong_datetime_packed(&ltime, nr);
5592  return Field_timestampf::store_time(&ltime, dec);
5593 }
5594 
5595 
5596 bool Field_timestampf::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5597 {
5598  /* Don't do check_fuzzy_date() as month and year are never 0 for timestamp */
5599  return get_internal_check_zero(ltime, fuzzydate);
5600 }
5601 
5602 
5603 void Field_timestampf::sql_type(String &res) const
5604 {
5605  if (dec == 0)
5606  {
5607  res.set_ascii(STRING_WITH_LEN("timestamp"));
5608  return;
5609  }
5610  const CHARSET_INFO *cs= res.charset();
5611  res.length(cs->cset->snprintf(cs, (char *) res.ptr(), res.alloced_length(),
5612  "timestamp(%d)", dec));
5613 }
5614 
5615 
5616 bool
5618 {
5619  THD *thd= table ? table->in_use : current_thd;
5620  struct timeval tm;
5621  my_timestamp_from_binary(&tm, ptr, dec);
5622  if (tm.tv_sec == 0)
5623  return true;
5624  thd->time_zone()->gmt_sec_to_TIME(ltime, tm);
5625  return false;
5626 }
5627 
5628 
5629 bool Field_timestampf::get_timestamp(struct timeval *tm, int *warnings)
5630 {
5631  THD *thd= table ? table->in_use : current_thd;
5632  thd->time_zone_used= 1;
5633  DBUG_ASSERT(!is_null());
5634  my_timestamp_from_binary(tm, ptr, dec);
5635  return false;
5636 }
5637 
5638 
5639 type_conversion_status Field_timestampf::validate_stored_val(THD *thd)
5640 {
5641  /*
5642  While deprecating "TIMESTAMP with implicit DEFAULT value", we can
5643  remove this function implementation and depend directly on
5644  "Field_temporal_with_date::validate_stored_val"
5645  */
5646  if (!thd->variables.explicit_defaults_for_timestamp)
5647  return TYPE_OK;
5648 
5650 }
5651 
5652 
5653 /****************************************************************************
5654 ** TIME and TIME(N) common methods
5655 ****************************************************************************/
5656 
5657 
5658 bool
5659 Field_time_common::convert_str_to_TIME(const char *str, uint len,
5660  const CHARSET_INFO *cs,
5661  MYSQL_TIME *ltime,
5662  MYSQL_TIME_STATUS *status)
5663 {
5664  return str_to_time(cs, str, len, ltime, 0, status);
5665 }
5666 
5667 
5668 type_conversion_status
5669 Field_time_common::convert_number_to_TIME(longlong nr, bool unsigned_val,
5670  int nanoseconds,
5671  MYSQL_TIME *ltime, int *warnings)
5672 {
5673  if (unsigned_val && nr < 0)
5674  {
5675  *warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
5676  set_max_time(ltime, 0);
5677  store_internal(ltime, warnings);
5678  return TYPE_WARN_OUT_OF_RANGE;
5679  }
5680  if (number_to_time(nr, ltime, warnings))
5681  {
5682  store_internal(ltime, warnings);
5683  return TYPE_WARN_OUT_OF_RANGE;
5684  }
5685  /*
5686  Both number_to_time() call and negative nanoseconds value
5687  affect ltime->neg, hence "|=" to combine them:
5688  */
5689  if ((ltime->neg|= (nanoseconds < 0)))
5690  nanoseconds= -nanoseconds;
5691  ltime->second_part= 0;
5692  bool round_error= time_add_nanoseconds_with_round(ltime, nanoseconds,
5693  warnings);
5694 
5695  return round_error ? time_warning_to_type_conversion_status(*warnings)
5696  : TYPE_OK;
5697 }
5698 
5699 
5700 type_conversion_status
5702  uint8 dec_arg __attribute__((unused)))
5703 {
5704  /* Check if seconds or minutes are out of range */
5705  if (ltime->second >= 60 || ltime->minute >= 60)
5706  {
5707  set_warnings(ErrConvString(ltime, decimals()),
5708  MYSQL_TIME_WARN_OUT_OF_RANGE);
5709  reset();
5710  return TYPE_WARN_OUT_OF_RANGE;
5711  }
5712  int warnings= 0;
5713  return store_internal_with_round(ltime, &warnings);
5714 }
5715 
5716 
5717 type_conversion_status
5719 {
5720  if (my_time_round(ltime, dec))
5721  return TYPE_WARN_OUT_OF_RANGE;
5722 
5723  return store_internal(ltime, warnings);
5724 }
5725 
5726 
5727 String *Field_time_common::val_str(String *val_buffer,
5728  String *val_ptr __attribute__((unused)))
5729 {
5730  ASSERT_COLUMN_MARKED_FOR_READ;
5731  MYSQL_TIME ltime;
5732  val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
5733  val_buffer->set_charset(&my_charset_numeric);
5734  if (get_time(&ltime))
5735  {
5736  DBUG_ASSERT(0);
5737  set_zero_time(&ltime, MYSQL_TIMESTAMP_TIME);
5738  }
5739  make_time((DATE_TIME_FORMAT *) 0, &ltime, val_buffer, dec);
5740  return val_buffer;
5741 }
5742 
5743 
5750 bool Field_time_common::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5751 {
5752  ASSERT_COLUMN_MARKED_FOR_READ;
5753  MYSQL_TIME tm;
5754  if (get_time(&tm))
5755  {
5756  DBUG_ASSERT(0);
5757  set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
5758  }
5759  time_to_datetime(table ? table->in_use : current_thd, &tm, ltime);
5760  return false;
5761 }
5762 
5763 
5765 {
5766  ASSERT_COLUMN_MARKED_FOR_READ;
5767  MYSQL_TIME time, datetime;
5768  if (get_time(&time))
5769  {
5770  DBUG_ASSERT(0); // Field_time*::get_time should not fail
5771  return 0;
5772  }
5773  time_to_datetime(table ? table->in_use : current_thd, &time, &datetime);
5774  return TIME_to_longlong_datetime_packed(&datetime);
5775 }
5776 
5777 
5778 bool Field_time_common::send_binary(Protocol *protocol)
5779 {
5780  MYSQL_TIME ltime;
5781  if (get_time(&ltime))
5782  {
5783  DBUG_ASSERT(0);
5784  set_zero_time(&ltime, MYSQL_TIMESTAMP_TIME);
5785  }
5786  ltime.day= ltime.hour / 24; // Move hours to days
5787  ltime.hour-= ltime.day * 24;
5788  return protocol->store_time(&ltime, 0);
5789 }
5790 
5791 
5792 /****************************************************************************
5793 ** time type
5794 ** In string context: HH:MM:SS
5795 ** In number context: HHMMSS
5796 ** Stored as a 3 byte unsigned int
5797 ****************************************************************************/
5798 
5799 type_conversion_status
5800 Field_time::store_internal(const MYSQL_TIME *ltime, int *warnings)
5801 {
5802  long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
5803  (ltime->minute * 100 + ltime->second);
5804  if (ltime->neg)
5805  tmp= -tmp;
5806  int3store(ptr, tmp);
5807  return TYPE_OK;
5808 }
5809 
5810 
5811 type_conversion_status Field_time::store_packed(longlong nr)
5812 {
5813  MYSQL_TIME ltime;
5814  TIME_from_longlong_time_packed(&ltime, nr);
5815  return Field_time::store_time(&ltime, 0);
5816 }
5817 
5818 
5820 {
5821  ASSERT_COLUMN_MARKED_FOR_READ;
5822  MYSQL_TIME ltime;
5823  return get_time(&ltime) ? 0 : TIME_to_longlong_time_packed(&ltime);
5824 }
5825 
5826 
5827 longlong Field_time::val_int()
5828 {
5829  ASSERT_COLUMN_MARKED_FOR_READ;
5830  return (longlong) sint3korr(ptr);
5831 }
5832 
5833 
5834 bool Field_time::get_time(MYSQL_TIME *ltime)
5835 {
5836  long tmp=(long) sint3korr(ptr);
5837  if ((ltime->neg= tmp < 0))
5838  tmp= -tmp;
5839  ltime->year= ltime->month= ltime->day= 0;
5840  TIME_set_hhmmss(ltime, tmp);
5841  ltime->second_part=0;
5842  ltime->time_type= MYSQL_TIMESTAMP_TIME;
5843  return false;
5844 }
5845 
5846 
5847 int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
5848 {
5849  int32 a,b;
5850  a=(int32) sint3korr(a_ptr);
5851  b=(int32) sint3korr(b_ptr);
5852  return (a < b) ? -1 : (a > b) ? 1 : 0;
5853 }
5854 
5855 
5856 void Field_time::make_sort_key(uchar *to, uint length)
5857 {
5858  DBUG_ASSERT(length >= 3);
5859  to[0] = (uchar) (ptr[2] ^ 128);
5860  to[1] = ptr[1];
5861  to[2] = ptr[0];
5862 }
5863 
5864 
5865 void Field_time::sql_type(String &res) const
5866 {
5867  res.set_ascii(STRING_WITH_LEN("time"));
5868 }
5869 
5870 
5871 /****************************************************************************
5872 ** time type with fsp
5873 ** In string context: HH:MM:SS.FFFFFF
5874 ** In number context: HHMMSS.FFFFFF
5875 ****************************************************************************/
5876 
5877 
5878 longlong Field_timef::val_int()
5879 {
5880  ASSERT_COLUMN_MARKED_FOR_READ;
5881  MYSQL_TIME ltime;
5882  if (get_time(&ltime))
5883  {
5884  DBUG_ASSERT(0);
5885  set_zero_time(&ltime, MYSQL_TIMESTAMP_TIME);
5886  }
5887  longlong tmp= (longlong) TIME_to_ulonglong_time_round(&ltime);
5888  return ltime.neg ? -tmp : tmp;
5889 }
5890 
5891 
5892 my_decimal *Field_timef::val_decimal(my_decimal *decimal_value)
5893 {
5894  ASSERT_COLUMN_MARKED_FOR_READ;
5895  MYSQL_TIME ltime;
5896  if (get_time(&ltime))
5897  {
5898  DBUG_ASSERT(0);
5899  set_zero_time(&ltime, MYSQL_TIMESTAMP_TIME);
5900  }
5901  return time2my_decimal(&ltime, decimal_value);
5902 }
5903 
5904 
5905 double Field_timef::val_real()
5906 {
5907  ASSERT_COLUMN_MARKED_FOR_READ;
5908  MYSQL_TIME ltime;
5909  if (get_time(&ltime))
5910  {
5911  DBUG_ASSERT(0);
5912  return 0;
5913  }
5914  double tmp= TIME_to_double_time(&ltime);
5915  return ltime.neg ? -tmp : tmp;
5916 }
5917 
5918 
5919 void Field_timef::sql_type(String &res) const
5920 {
5921  if (dec == 0)
5922  {
5923  res.set_ascii(STRING_WITH_LEN("time"));
5924  return;
5925  }
5926  const CHARSET_INFO *cs= res.charset();
5927  res.length(cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
5928  "time(%d)", dec));
5929 }
5930 
5931 
5932 type_conversion_status Field_timef::reset()
5933 {
5934  return store_packed(0);
5935 }
5936 
5937 
5938 type_conversion_status Field_timef::store_packed(longlong nr)
5939 {
5940  my_time_packed_to_binary(nr, ptr, dec);
5941  return TYPE_OK;
5942 }
5943 
5944 
5946 {
5947  ASSERT_COLUMN_MARKED_FOR_READ;
5948  return my_time_packed_from_binary(ptr, dec);
5949 }
5950 
5951 
5952 type_conversion_status
5953 Field_timef::store_internal(const MYSQL_TIME *ltime, int *warnings)
5954 {
5955  type_conversion_status rc= store_packed(TIME_to_longlong_time_packed(ltime));
5956  if (rc == TYPE_OK && non_zero_date(ltime))
5957  {
5958  /*
5959  The DATE part got lost; we warn, like in Field_newdate::store_internal,
5960  and trigger some code in get_mm_leaf()
5961  (see err==TYPE_NOTE_TIME_TRUNCATED there).
5962  */
5963  *warnings|= MYSQL_TIME_NOTE_TRUNCATED;
5964  rc= TYPE_NOTE_TIME_TRUNCATED;
5965  }
5966  return rc;
5967 }
5968 
5969 
5970 bool Field_timef::get_time(MYSQL_TIME *ltime)
5971 {
5972  longlong tmp= val_time_temporal();
5973  TIME_from_longlong_time_packed(ltime, tmp);
5974  return false;
5975 }
5976 
5977 
5978 /****************************************************************************
5979 ** year type
5980 ** Save in a byte the year 0, 1901->2155
5981 ** Can handle 2 byte or 4 byte years!
5982 ****************************************************************************/
5983 
5984 type_conversion_status
5985 Field_year::store(const char *from, uint len,const CHARSET_INFO *cs)
5986 {
5987  ASSERT_COLUMN_MARKED_FOR_WRITE;
5988  char *end;
5989  int conv_error;
5990  type_conversion_status ret= TYPE_OK;
5991  longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &conv_error);
5992 
5993  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 ||
5994  conv_error == MY_ERRNO_ERANGE)
5995  {
5996  *ptr=0;
5997  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
5998  return TYPE_WARN_OUT_OF_RANGE;
5999  }
6000  else if (conv_error)
6001  ret= TYPE_ERR_BAD_VALUE;
6002 
6003  if (table->in_use->count_cuted_fields)
6004  {
6005  ret= check_int(cs, from, len, end, conv_error);
6006  if (ret != TYPE_OK)
6007  {
6008  if (ret == TYPE_ERR_BAD_VALUE) /* empty or incorrect string */
6009  {
6010  *ptr= 0;
6011  return TYPE_WARN_OUT_OF_RANGE;
6012  }
6013  ret= TYPE_WARN_OUT_OF_RANGE;
6014  }
6015  }
6016 
6017  if (nr != 0 || len != 4)
6018  {
6019  if (nr < YY_PART_YEAR)
6020  nr+=100; // 2000 - 2069
6021  else if (nr > 1900)
6022  nr-= 1900;
6023  }
6024  *ptr= (char) (uchar) nr;
6025  return ret;
6026 }
6027 
6028 
6029 type_conversion_status Field_year::store(double nr)
6030 {
6031  if (nr < 0.0 || nr >= 2155.0)
6032  {
6033  (void) Field_year::store((longlong) -1, FALSE);
6034  return TYPE_WARN_OUT_OF_RANGE;
6035  }
6036  return Field_year::store((longlong) nr, FALSE);
6037 }
6038 
6039 
6040 type_conversion_status
6042  uint8 dec_arg __attribute__((unused)))
6043 {
6044  if (ltime->time_type != MYSQL_TIMESTAMP_DATETIME &&
6045  ltime->time_type != MYSQL_TIMESTAMP_DATE)
6046  {
6047  /* Convert time to datetime, then store year of the result */
6048  THD *thd= table ? table->in_use : current_thd;
6049  MYSQL_TIME ltime2;
6050  time_to_datetime(thd, ltime, &ltime2);
6051  return store(ltime2.year, 0);
6052  }
6053  return store(ltime->year, 0);
6054 }
6055 
6056 
6057 type_conversion_status Field_year::store(longlong nr, bool unsigned_val)
6058 {
6059  ASSERT_COLUMN_MARKED_FOR_WRITE;
6060  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
6061  {
6062  *ptr= 0;
6063  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
6064  return TYPE_WARN_OUT_OF_RANGE;
6065  }
6066  if (nr != 0 || field_length != 4) // 0000 -> 0; 00 -> 2000
6067  {
6068  if (nr < YY_PART_YEAR)
6069  nr+=100; // 2000 - 2069
6070  else if (nr > 1900)
6071  nr-= 1900;
6072  }
6073  *ptr= (char) (uchar) nr;
6074  return TYPE_OK;
6075 }
6076 
6077 
6078 bool Field_year::send_binary(Protocol *protocol)
6079 {
6080  ASSERT_COLUMN_MARKED_FOR_READ;
6081  ulonglong tmp= Field_year::val_int();
6082  return protocol->store_short(tmp);
6083 }
6084 
6085 
6086 double Field_year::val_real(void)
6087 {
6088  return (double) Field_year::val_int();
6089 }
6090 
6091 
6092 longlong Field_year::val_int(void)
6093 {
6094  ASSERT_COLUMN_MARKED_FOR_READ;
6095  DBUG_ASSERT(field_length == 2 || field_length == 4);
6096  int tmp= (int) ptr[0];
6097  if (field_length != 4)
6098  tmp%=100; // Return last 2 char
6099  else if (tmp)
6100  tmp+=1900;
6101  return (longlong) tmp;
6102 }
6103 
6104 
6105 String *Field_year::val_str(String *val_buffer,
6106  String *val_ptr __attribute__((unused)))
6107 {
6108  DBUG_ASSERT(field_length < 5);
6109  val_buffer->alloc(5);
6110  val_buffer->length(field_length);
6111  char *to=(char*) val_buffer->ptr();
6112  sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
6113  val_buffer->set_charset(&my_charset_numeric);
6114  return val_buffer;
6115 }
6116 
6117 
6118 void Field_year::sql_type(String &res) const
6119 {
6120  const CHARSET_INFO *cs=res.charset();
6121  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
6122  "year(%d)",(int) field_length));
6123 }
6124 
6125 
6126 /****************************************************************************
6127 ** The new date type
6128 ** Stored as 3 bytes
6129 ** In number context: YYYYMMDD
6130 ****************************************************************************/
6131 
6132 ulonglong Field_newdate::date_flags(const THD *thd)
6133 {
6134  return TIME_FUZZY_DATE | thd->datetime_flags();
6135 }
6136 
6137 
6138 type_conversion_status
6139 Field_newdate::store_internal(const MYSQL_TIME *ltime, int *warnings)
6140 {
6141  long tmp= ltime->day + ltime->month * 32 + ltime->year * 16 * 32;
6142  int3store(ptr, tmp);
6143  if (non_zero_time(ltime))
6144  {
6145  *warnings|= MYSQL_TIME_NOTE_TRUNCATED;
6146  return TYPE_NOTE_TIME_TRUNCATED;
6147  }
6148  return TYPE_OK;
6149 }
6150 
6151 
6153 {
6154  uint32 tmp= (uint32) uint3korr(ptr);
6155  ltime->day= tmp & 31;
6156  ltime->month= (tmp >> 5) & 15;
6157  ltime->year= (tmp >> 9);
6158  ltime->time_type= MYSQL_TIMESTAMP_DATE;
6159  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
6160  return false;
6161 }
6162 
6163 
6164 type_conversion_status Field_newdate::store_packed(longlong nr)
6165 {
6166  int warnings= 0;
6167  MYSQL_TIME ltime;
6168  TIME_from_longlong_date_packed(&ltime, nr);
6169  return store_internal(&ltime, &warnings);
6170 }
6171 
6172 
6173 bool Field_newdate::send_binary(Protocol *protocol)
6174 {
6175  MYSQL_TIME ltime;
6176  get_date(&ltime, 0);
6177  return protocol->store_date(&ltime);
6178 }
6179 
6180 
6181 longlong Field_newdate::val_int()
6182 {
6183  ASSERT_COLUMN_MARKED_FOR_READ;
6184  ulong j= uint3korr(ptr);
6185  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
6186  return (longlong) j;
6187 }
6188 
6189 
6191 {
6192  ASSERT_COLUMN_MARKED_FOR_READ;
6193  MYSQL_TIME ltime;
6194  return get_date_internal(&ltime) ? 0 : TIME_to_longlong_date_packed(&ltime);
6195 }
6196 
6197 
6199 {
6200  ASSERT_COLUMN_MARKED_FOR_READ;
6201  return 0;
6202 }
6203 
6204 
6205 String *Field_newdate::val_str(String *val_buffer,
6206  String *val_ptr __attribute__((unused)))
6207 {
6208  ASSERT_COLUMN_MARKED_FOR_READ;
6209  val_buffer->alloc(field_length);
6210  val_buffer->length(field_length);
6211  uint32 tmp=(uint32) uint3korr(ptr);
6212  int part;
6213  char *pos=(char*) val_buffer->ptr()+10;
6214 
6215  /* Open coded to get more speed */
6216  *pos--=0; // End NULL
6217  part=(int) (tmp & 31);
6218  *pos--= (char) ('0'+part%10);
6219  *pos--= (char) ('0'+part/10);
6220  *pos--= '-';
6221  part=(int) (tmp >> 5 & 15);
6222  *pos--= (char) ('0'+part%10);
6223  *pos--= (char) ('0'+part/10);
6224  *pos--= '-';
6225  part=(int) (tmp >> 9);
6226  *pos--= (char) ('0'+part%10); part/=10;
6227  *pos--= (char) ('0'+part%10); part/=10;
6228  *pos--= (char) ('0'+part%10); part/=10;
6229  *pos= (char) ('0'+part);
6230  val_buffer->set_charset(&my_charset_numeric);
6231  return val_buffer;
6232 }
6233 
6234 
6235 bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
6236 {
6237  return get_internal_check_zero(ltime, fuzzydate) ||
6238  check_fuzzy_date(ltime, fuzzydate);
6239 }
6240 
6241 
6242 int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
6243 {
6244  uint32 a,b;
6245  a=(uint32) uint3korr(a_ptr);
6246  b=(uint32) uint3korr(b_ptr);
6247  return (a < b) ? -1 : (a > b) ? 1 : 0;
6248 }
6249 
6250 
6251 void Field_newdate::make_sort_key(uchar *to, uint length)
6252 {
6253  DBUG_ASSERT(length >= 3);
6254  to[0] = ptr[2];
6255  to[1] = ptr[1];
6256  to[2] = ptr[0];
6257 }
6258 
6259 
6260 void Field_newdate::sql_type(String &res) const
6261 {
6262  res.set_ascii(STRING_WITH_LEN("date"));
6263 }
6264 
6265 
6266 /****************************************************************************
6267 ** datetime type
6268 ** In string context: YYYY-MM-DD HH:MM:DD
6269 ** In number context: YYYYMMDDHHMMDD
6270 ** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
6271 ****************************************************************************/
6272 
6273 
6274 ulonglong Field_datetime::date_flags(const THD *thd)
6275 {
6276  return TIME_FUZZY_DATE | thd->datetime_flags();
6277 }
6278 
6279 
6281 {
6282  MYSQL_TIME mysql_time;
6283  THD *thd= current_thd;
6284  thd->variables.time_zone->gmt_sec_to_TIME(&mysql_time, *tm);
6285  thd->time_zone_used= true;
6286  int error= 0;
6287  store_internal(&mysql_time, &error);
6288 }
6289 
6290 
6298 static inline type_conversion_status
6299 datetime_store_internal(TABLE *table, ulonglong tmp, uchar *ptr)
6300 {
6301 #ifdef WORDS_BIGENDIAN
6302  if (table && table->s->db_low_byte_first)
6303  {
6304  int8store(ptr, tmp);
6305  }
6306  else
6307 #endif
6308  longlongstore(ptr, tmp);
6309  return TYPE_OK;
6310 }
6311 
6312 
6320 static inline longlong
6321 datetime_get_internal(TABLE *table, uchar *ptr)
6322 {
6323  longlong tmp;
6324 #ifdef WORDS_BIGENDIAN
6325  if (table && table->s->db_low_byte_first)
6326  tmp= sint8korr(ptr);
6327  else
6328 #endif
6329  longlongget(tmp, ptr);
6330  return tmp;
6331 }
6332 
6333 
6335 {
6336  longlong tmp= datetime_get_internal(table, ptr);
6337  ltime->time_type= MYSQL_TIMESTAMP_DATETIME;
6338  ltime->neg= 0;
6339  ltime->second_part= 0;
6340  TIME_set_yymmdd(ltime, (uint) (tmp / 1000000LL));
6341  TIME_set_hhmmss(ltime, (uint) (tmp % 1000000LL));
6342  return false;
6343 }
6344 
6345 
6346 type_conversion_status
6347 Field_datetime::store_internal(const MYSQL_TIME *ltime, int *warnings)
6348 {
6349  ulonglong tmp= TIME_to_ulonglong_datetime(ltime);
6350  return datetime_store_internal(table, tmp, ptr);
6351 }
6352 
6353 
6354 type_conversion_status Field_datetime::store(longlong nr, bool unsigned_val)
6355 {
6356  ASSERT_COLUMN_MARKED_FOR_WRITE;
6357  MYSQL_TIME ltime;
6358  int warnings;
6359  type_conversion_status error= TYPE_OK;
6360  longlong tmp= convert_number_to_datetime(nr, unsigned_val,
6361  &ltime, &warnings);
6362  if (tmp == LL(-1))
6363  error= TYPE_ERR_BAD_VALUE;
6364  else
6365  {
6366  error= time_warning_to_type_conversion_status(warnings);
6367  datetime_store_internal(table, tmp, ptr);
6368  }
6369  if (warnings)
6370  set_warnings(ErrConvString(nr, unsigned_val), warnings);
6371  return error;
6372 }
6373 
6374 
6375 type_conversion_status Field_datetime::store_packed(longlong nr)
6376 {
6377  MYSQL_TIME ltime;
6378  TIME_from_longlong_datetime_packed(&ltime, nr);
6379  return Field_datetime::store_time(&ltime, 0);
6380 }
6381 
6382 
6383 longlong Field_datetime::val_int()
6384 {
6385  ASSERT_COLUMN_MARKED_FOR_READ;
6386  return datetime_get_internal(table, ptr);
6387 }
6388 
6389 
6390 /*
6391  We don't reuse the parent method for performance purposes,
6392  to avoid convertion from number to MYSQL_TIME.
6393  Using my_datetime_number_to_str() instead of my_datetime_to_str().
6394 */
6395 String *Field_datetime::val_str(String *val_buffer,
6396  String *val_ptr __attribute__((unused)))
6397 {
6398  ASSERT_COLUMN_MARKED_FOR_READ;
6399  val_buffer->alloc(field_length + 1);
6400  val_buffer->set_charset(&my_charset_numeric);
6401  val_buffer->length(MAX_DATETIME_WIDTH);
6402  longlong tmp= datetime_get_internal(table, ptr);
6403  val_buffer->length(my_datetime_number_to_str((char *) val_buffer->ptr(),
6404  tmp));
6405  return val_buffer;
6406 }
6407 
6408 
6409 bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
6410 {
6411  return get_internal_check_zero(ltime, fuzzydate) ||
6412  check_fuzzy_date(ltime, fuzzydate);
6413 }
6414 
6415 
6416 int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
6417 {
6418  longlong a,b;
6419 #ifdef WORDS_BIGENDIAN
6420  if (table && table->s->db_low_byte_first)
6421  {
6422  a=sint8korr(a_ptr);
6423  b=sint8korr(b_ptr);
6424  }
6425  else
6426 #endif
6427  {
6428  longlongget(a,a_ptr);
6429  longlongget(b,b_ptr);
6430  }
6431  return ((ulonglong) a < (ulonglong) b) ? -1 :
6432  ((ulonglong) a > (ulonglong) b) ? 1 : 0;
6433 }
6434 
6435 void Field_datetime::make_sort_key(uchar *to, uint length)
6436 {
6437  const int pack_length= PACK_LENGTH;
6438  const int to_length= min<uint>(pack_length, length);
6439 #ifdef WORDS_BIGENDIAN
6440  if (!table || !table->s->db_low_byte_first)
6441  copy_integer<true>(to, to_length, ptr, pack_length, true);
6442  else
6443 #endif
6444  copy_integer<false>(to, to_length, ptr, pack_length, true);
6445 }
6446 
6447 
6448 void Field_datetime::sql_type(String &res) const
6449 {
6450  res.set_ascii(STRING_WITH_LEN("datetime"));
6451 }
6452 
6453 /****************************************************************************
6454 ** datetimef type
6455 ** In string context: YYYY-MM-DD HH:MM:DD.FFFFFF
6456 ** In number context: YYYYMMDDHHMMDD.FFFFFF
6457 ** Stored as a 8 byte value.
6458 ****************************************************************************/
6459 
6460 
6461 ulonglong Field_datetimef::date_flags(const THD *thd)
6462 {
6463  return TIME_FUZZY_DATE | thd->datetime_flags();
6464 }
6465 
6466 
6468 {
6469  MYSQL_TIME mysql_time;
6470  THD *thd= current_thd;
6471  thd->variables.time_zone->gmt_sec_to_TIME(&mysql_time, *tm);
6472  thd->time_zone_used= true;
6473  int warnings= 0;
6474  store_internal(&mysql_time, &warnings);
6475 }
6476 
6477 
6478 
6479 bool Field_datetimef::get_date(MYSQL_TIME *ltime, uint fuzzydate)
6480 {
6481  return get_internal_check_zero(ltime, fuzzydate) ||
6482  check_fuzzy_date(ltime, fuzzydate);
6483 }
6484 
6485 
6486 void Field_datetimef::sql_type(String &res) const
6487 {
6488  if (dec == 0)
6489  {
6490  res.set_ascii(STRING_WITH_LEN("datetime"));
6491  return;
6492  }
6493  const CHARSET_INFO *cs= res.charset();
6494  res.length(cs->cset->snprintf(cs, (char *) res.ptr(), res.alloced_length(),
6495  "datetime(%d)", dec));
6496 }
6497 
6498 
6500 {
6501  TIME_from_longlong_datetime_packed(ltime, val_date_temporal());
6502  return false;
6503 }
6504 
6505 
6506 type_conversion_status
6507 Field_datetimef::store_internal(const MYSQL_TIME *ltime, int *warnings)
6508 {
6509  store_packed(TIME_to_longlong_datetime_packed(ltime));
6510  return TYPE_OK;
6511 }
6512 
6513 
6514 type_conversion_status Field_datetimef::reset()
6515 {
6516  store_packed(0);
6517  return TYPE_OK;
6518 }
6519 
6520 
6522 {
6523  return my_datetime_packed_from_binary(ptr, dec);
6524 }
6525 
6526 
6527 type_conversion_status Field_datetimef::store_packed(longlong nr)
6528 {
6529  my_datetime_packed_to_binary(nr, ptr, dec);
6530  return TYPE_OK;
6531 }
6532 
6533 
6534 
6535 /****************************************************************************
6536 ** string type
6537 ** A string may be varchar or binary
6538 ****************************************************************************/
6539 
6566 type_conversion_status
6567 Field_longstr::check_string_copy_error(const char *well_formed_error_pos,
6568  const char *cannot_convert_error_pos,
6569  const char *from_end_pos,
6570  const char *end,
6571  bool count_spaces,
6572  const CHARSET_INFO *cs) const
6573 {
6574  const char *pos;
6575  char tmp[32];
6576  THD *thd= table->in_use;
6577 
6578  if (!(pos= well_formed_error_pos) &&
6579  !(pos= cannot_convert_error_pos))
6580  return report_if_important_data(from_end_pos, end, count_spaces);
6581 
6582  convert_to_printable(tmp, sizeof(tmp), pos, (end - pos), cs, 6);
6583 
6584  push_warning_printf(thd,
6585  Sql_condition::WARN_LEVEL_WARN,
6586  ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
6587  ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
6588  "string", tmp, field_name,
6589  thd->get_stmt_da()->current_row_for_warning());
6590  return TYPE_WARN_TRUNCATED;
6591 }
6592 
6593 
6594 /*
6595  Check if we lost any important data and send a truncation error/warning
6596 
6597  SYNOPSIS
6598  Field_longstr::report_if_important_data()
6599  pstr - Truncated rest of string
6600  end - End of truncated string
6601  count_spaces - Treat traling spaces as important data
6602 
6603  RETURN VALUES
6604  false - None was truncated (or we don't count cut fields)
6605  true - Some bytes were truncated
6606 
6607  NOTE
6608  Check if we lost any important data (anything in a binary string,
6609  or any non-space in others). If only trailing spaces was lost,
6610  send a truncation note, otherwise send a truncation error.
6611  Silently ignore traling spaces if the count_space parameter is FALSE.
6612 */
6613 
6614 type_conversion_status
6615 Field_longstr::report_if_important_data(const char *pstr, const char *end,
6616  bool count_spaces) const
6617 {
6618  if ((pstr < end) && table->in_use->count_cuted_fields)
6619  {
6620  if (test_if_important_data(field_charset, pstr, end))
6621  {
6622  if (table->in_use->abort_on_warning)
6623  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
6624  else
6625  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
6626  return TYPE_WARN_TRUNCATED;
6627  }
6628  else if (count_spaces)
6629  { /* If we lost only spaces then produce a NOTE, not a WARNING */
6630  set_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
6631  return TYPE_NOTE_TRUNCATED;
6632  }
6633  }
6634  return TYPE_OK;
6635 }
6636 
6637 
6638  /* Copy a string and fill with space */
6639 
6640 type_conversion_status
6641 Field_string::store(const char *from,uint length,const CHARSET_INFO *cs)
6642 {
6643  ASSERT_COLUMN_MARKED_FOR_WRITE;
6644  uint copy_length;
6645  const char *well_formed_error_pos;
6646  const char *cannot_convert_error_pos;
6647  const char *from_end_pos;
6648 
6649  /* See the comment for Field_long::store(long long) */
6650  DBUG_ASSERT(table->in_use == current_thd);
6651 
6652  copy_length= well_formed_copy_nchars(field_charset,
6653  (char*) ptr, field_length,
6654  cs, from, length,
6655  field_length / field_charset->mbmaxlen,
6656  &well_formed_error_pos,
6657  &cannot_convert_error_pos,
6658  &from_end_pos);
6659 
6660  /* Append spaces if the string was shorter than the field. */
6661  if (copy_length < field_length)
6662  field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
6663  field_length-copy_length,
6664  field_charset->pad_char);
6665 
6666  return check_string_copy_error(well_formed_error_pos,
6667  cannot_convert_error_pos, from_end_pos,
6668  from + length, false, cs);
6669 }
6670 
6671 
6680 type_conversion_status Field_str::store(double nr)
6681 {
6682  ASSERT_COLUMN_MARKED_FOR_WRITE;
6683  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
6684  uint local_char_length= field_length / charset()->mbmaxlen;
6685  size_t length= 0;
6686  my_bool error= (local_char_length == 0);
6687 
6688  // my_gcvt() requires width > 0, and we may have a CHAR(0) column.
6689  if (!error)
6690  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
6691 
6692  if (error)
6693  {
6694  if (table->in_use->abort_on_warning)
6695  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
6696  else
6697  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
6698  }
6699  return store(buff, length, &my_charset_numeric);
6700 }
6701 
6702 
6704 {
6705  return (new_field->sql_type == real_type());
6706 }
6707 
6708 
6710 {
6711  if (field_flags_are_binary() != new_field->field_flags_are_binary())
6712  return 0;
6713 
6714  return ((new_field->sql_type == real_type()) &&
6715  new_field->charset == field_charset &&
6716  new_field->length == max_display_length());
6717 }
6718 
6719 
6720 type_conversion_status Field_string::store(longlong nr, bool unsigned_val)
6721 {
6722  char buff[64];
6723  int l;
6724  const CHARSET_INFO *cs=charset();
6725  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
6726  unsigned_val ? 10 : -10, nr);
6727  return Field_string::store(buff,(uint)l,cs);
6728 }
6729 
6730 
6731 type_conversion_status Field_longstr::store_decimal(const my_decimal *d)
6732 {
6733  char buff[DECIMAL_MAX_STR_LENGTH+1];
6734  String str(buff, sizeof(buff), &my_charset_numeric);
6735  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
6736  return store(str.ptr(), str.length(), str.charset());
6737 }
6738 
6740 {
6741  return field_length + (field_length > 255 ? 2 : 1);
6742 }
6743 
6744 
6745 double Field_string::val_real(void)
6746 {
6747  ASSERT_COLUMN_MARKED_FOR_READ;
6748  int error;
6749  char *end;
6750  const CHARSET_INFO *cs= charset();
6751  double result;
6752 
6753  result= my_strntod(cs,(char*) ptr,field_length,&end,&error);
6754  if (!table->in_use->no_errors &&
6755  (error || (field_length != (uint32)(end - (char*) ptr) &&
6756  !check_if_only_end_space(cs, end,
6757  (char*) ptr + field_length))))
6758  {
6759  ErrConvString err((char*) ptr, field_length, cs);
6760  push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
6761  ER_TRUNCATED_WRONG_VALUE,
6762  ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
6763  err.ptr());
6764  }
6765  return result;
6766 }
6767 
6768 
6769 longlong Field_string::val_int(void)
6770 {
6771  ASSERT_COLUMN_MARKED_FOR_READ;
6772  int error;
6773  char *end;
6774  const CHARSET_INFO *cs= charset();
6775  longlong result;
6776 
6777  result= my_strntoll(cs, (char*) ptr,field_length,10,&end,&error);
6778  if (!table->in_use->no_errors &&
6779  (error || (field_length != (uint32)(end - (char*) ptr) &&
6780  !check_if_only_end_space(cs, end,
6781  (char*) ptr + field_length))))
6782  {
6783  ErrConvString err((char*) ptr, field_length, cs);
6784  push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
6785  ER_TRUNCATED_WRONG_VALUE,
6786  ER(ER_TRUNCATED_WRONG_VALUE),
6787  "INTEGER", err.ptr());
6788  }
6789  return result;
6790 }
6791 
6792 
6793 String *Field_string::val_str(String *val_buffer __attribute__((unused)),
6794  String *val_ptr)
6795 {
6796  ASSERT_COLUMN_MARKED_FOR_READ;
6797  /* See the comment for Field_long::store(long long) */
6798  DBUG_ASSERT(table->in_use == current_thd);
6799  uint length;
6800  if (table->in_use->variables.sql_mode &
6801  MODE_PAD_CHAR_TO_FULL_LENGTH)
6802  length= my_charpos(field_charset, ptr, ptr + field_length,
6803  field_length / field_charset->mbmaxlen);
6804  else
6805  length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
6806  field_length);
6807  val_ptr->set((const char*) ptr, length, field_charset);
6808  return val_ptr;
6809 }
6810 
6811 
6812 my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
6813 {
6814  ASSERT_COLUMN_MARKED_FOR_READ;
6815  int err= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr, field_length,
6816  charset(), decimal_value);
6817  if (!table->in_use->no_errors && err)
6818  {
6819  ErrConvString errmsg((char*) ptr, field_length, charset());
6820  push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
6821  ER_TRUNCATED_WRONG_VALUE,
6822  ER(ER_TRUNCATED_WRONG_VALUE),
6823  "DECIMAL", errmsg.ptr());
6824  }
6825 
6826  return decimal_value;
6827 }
6828 
6829 
6831  Field *field;
6832 };
6833 
6834 #ifdef HAVE_REPLICATION
6835 static bool
6836 check_field_for_37426(const void *param_arg)
6837 {
6838  Check_field_param *param= (Check_field_param*) param_arg;
6839  DBUG_ASSERT(param->field->real_type() == MYSQL_TYPE_STRING);
6840  DBUG_PRINT("debug", ("Field %s - type: %d, size: %d",
6841  param->field->field_name,
6842  param->field->real_type(),
6843  param->field->row_pack_length()));
6844  return param->field->row_pack_length() > 255;
6845 }
6846 #endif
6847 
6848 bool
6850  Relay_log_info *rli_arg,
6851  uint16 mflags __attribute__((unused)),
6852  int *order_var)
6853 {
6854 #ifdef HAVE_REPLICATION
6855  const Check_field_param check_param = { this };
6856  if (!is_mts_worker(rli_arg->info_thd) && rpl_master_has_bug(rli_arg, 37426, TRUE,
6857  check_field_for_37426, &check_param))
6858  return FALSE; // Not compatible field sizes
6859 #endif
6860  return Field::compatible_field_size(field_metadata, rli_arg, mflags, order_var);
6861 }
6862 
6863 
6864 int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
6865 {
6866  uint a_len, b_len;
6867 
6868  if (field_charset->mbmaxlen != 1)
6869  {
6870  uint char_len= field_length/field_charset->mbmaxlen;
6871  a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
6872  b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
6873  }
6874  else
6875  a_len= b_len= field_length;
6876  /*
6877  We have to remove end space to be able to compare multi-byte-characters
6878  like in latin_de 'ae' and 0xe4
6879  */
6880  return field_charset->coll->strnncollsp(field_charset,
6881  a_ptr, a_len,
6882  b_ptr, b_len,
6883  0);
6884 }
6885 
6886 
6887 void Field_string::make_sort_key(uchar *to, uint length)
6888 {
6889  uint tmp __attribute__((unused))=
6890  field_charset->coll->strnxfrm(field_charset,
6891  to, length, char_length(),
6892  ptr, field_length,
6893  MY_STRXFRM_PAD_WITH_SPACE |
6894  MY_STRXFRM_PAD_TO_MAXLEN);
6895  DBUG_ASSERT(tmp == length);
6896 }
6897 
6898 
6899 void Field_string::sql_type(String &res) const
6900 {
6901  THD *thd= table->in_use;
6902  const CHARSET_INFO *cs=res.charset();
6903  ulong length;
6904 
6905  length= cs->cset->snprintf(cs,(char*) res.ptr(),
6906  res.alloced_length(), "%s(%d)",
6907  ((type() == MYSQL_TYPE_VAR_STRING &&
6908  !thd->variables.new_mode) ?
6909  (has_charset() ? "varchar" : "varbinary") :
6910  (has_charset() ? "char" : "binary")),
6911  (int) field_length / charset()->mbmaxlen);
6912  res.length(length);
6913  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
6914  has_charset() && (charset()->state & MY_CS_BINSORT))
6915  res.append(STRING_WITH_LEN(" binary"));
6916 }
6917 
6918 
6919 uchar *Field_string::pack(uchar *to, const uchar *from,
6920  uint max_length,
6921  bool low_byte_first __attribute__((unused)))
6922 {
6923  uint length= min(field_length,max_length);
6924  uint local_char_length= max_length/field_charset->mbmaxlen;
6925  DBUG_PRINT("debug", ("Packing field '%s' - length: %u ", field_name, length));
6926 
6927  if (length > local_char_length)
6928  local_char_length= my_charpos(field_charset, from, from+length,
6929  local_char_length);
6930  set_if_smaller(length, local_char_length);
6931 
6932  /*
6933  TODO: change charset interface to add a new function that does
6934  the following or add a flag to lengthsp to do it itself
6935  (this is for not packing padding adding bytes in BINARY
6936  fields).
6937  */
6938  if (field_charset->mbmaxlen == 1)
6939  {
6940  while (length && from[length-1] == field_charset->pad_char)
6941  length --;
6942  }
6943  else
6944  length= field_charset->cset->lengthsp(field_charset, (const char*) from, length);
6945 
6946  // Length always stored little-endian
6947  *to++= (uchar) length;
6948  if (field_length > 255)
6949  *to++= (uchar) (length >> 8);
6950 
6951  // Store the actual bytes of the string
6952  memcpy(to, from, length);
6953  return to+length;
6954 }
6955 
6956 
6976 const uchar *
6978  const uchar *from,
6979  uint param_data,
6980  bool low_byte_first __attribute__((unused)))
6981 {
6982  uint from_length, length;
6983 
6984  /*
6985  Compute the declared length of the field on the master. This is
6986  used to decide if one or two bytes should be read as length.
6987  */
6988  if (param_data)
6989  from_length= (((param_data >> 4) & 0x300) ^ 0x300) + (param_data & 0x00ff);
6990  else
6991  from_length= field_length;
6992 
6993  DBUG_PRINT("debug",
6994  ("param_data: 0x%x, field_length: %u, from_length: %u",
6995  param_data, field_length, from_length));
6996  /*
6997  Compute the actual length of the data by reading one or two bits
6998  (depending on the declared field length on the master).
6999  */
7000  if (from_length > 255)
7001  {
7002  length= uint2korr(from);
7003  from+= 2;
7004  }
7005  else
7006  length= (uint) *from++;
7007 
7008  memcpy(to, from, length);
7009  // Pad the string with the pad character of the fields charset
7010  field_charset->cset->fill(field_charset, (char*) to + length, field_length - length, field_charset->pad_char);
7011  return from+length;
7012 }
7013 
7014 
7045 int Field_string::do_save_field_metadata(uchar *metadata_ptr)
7046 {
7047  DBUG_ASSERT(field_length < 1024);
7048  DBUG_ASSERT((real_type() & 0xF0) == 0xF0);
7049  DBUG_PRINT("debug", ("field_length: %u, real_type: %u",
7050  field_length, real_type()));
7051  *metadata_ptr= (real_type() ^ ((field_length & 0x300) >> 4));
7052  *(metadata_ptr + 1)= field_length & 0xFF;
7053  return 2;
7054 }
7055 
7056 
7057 uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
7058 {
7059  if (length > 255)
7060  return uint2korr(data_ptr)+2;
7061  return (uint) *data_ptr + 1;
7062 }
7063 
7064 
7065 uint Field_string::max_packed_col_length(uint max_length)
7066 {
7067  return (max_length > 255 ? 2 : 1)+max_length;
7068 }
7069 
7070 
7071 uint Field_string::get_key_image(uchar *buff, uint length, imagetype type_arg)
7072 {
7073  uint bytes = my_charpos(field_charset, (char*) ptr,
7074  (char*) ptr + field_length,
7075  length / field_charset->mbmaxlen);
7076  memcpy(buff, ptr, bytes);
7077  if (bytes < length)
7078  field_charset->cset->fill(field_charset, (char*) buff + bytes,
7079  length - bytes, field_charset->pad_char);
7080  return bytes;
7081 }
7082 
7083 
7084 Field *Field_string::new_field(MEM_ROOT *root, TABLE *new_table,
7085  bool keep_type)
7086 {
7087  Field *field;
7088  if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
7089  field= Field::new_field(root, new_table, keep_type);
7090  else if ((field= new Field_varstring(field_length, maybe_null(), field_name,
7091  new_table->s, charset())))
7092  {
7093  /*
7094  Old VARCHAR field which should be modified to a VARCHAR on copy
7095  This is done to ensure that ALTER TABLE will convert old VARCHAR fields
7096  to now VARCHAR fields.
7097  */
7098  field->init(new_table);
7099  /*
7100  Normally orig_table is different from table only if field was created
7101  via ::new_field. Here we alter the type of field, so ::new_field is
7102  not applicable. But we still need to preserve the original field
7103  metadata for the client-server protocol.
7104  */
7105  field->orig_table= orig_table;
7106  }
7107  return field;
7108 }
7109 
7110 
7111 /****************************************************************************
7112  VARCHAR type
7113  Data in field->ptr is stored as:
7114  1 or 2 bytes length-prefix-header (from Field_varstring::length_bytes)
7115  data
7116 
7117  NOTE:
7118  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
7119  stored with a 2 byte prefix. (Just like blob keys).
7120 
7121  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
7122  The exception is if there is a prefix key field that is part of a long
7123  VARCHAR, in which case field_length for this may be 1 but the length_bytes
7124  is 2.
7125 ****************************************************************************/
7126 
7127 const uint Field_varstring::MAX_SIZE= UINT_MAX16;
7128 
7140 int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
7141 {
7142  DBUG_ASSERT(field_length <= 65535);
7143  int2store((char*)metadata_ptr, field_length);
7144  return 2;
7145 }
7146 
7147 type_conversion_status Field_varstring::store(const char *from,uint length,
7148  const CHARSET_INFO *cs)
7149 {
7150  ASSERT_COLUMN_MARKED_FOR_WRITE;
7151  uint copy_length;
7152  const char *well_formed_error_pos;
7153  const char *cannot_convert_error_pos;
7154  const char *from_end_pos;
7155 
7156  copy_length= well_formed_copy_nchars(field_charset,
7157  (char*) ptr + length_bytes,
7158  field_length,
7159  cs, from, length,
7160  field_length / field_charset->mbmaxlen,
7161  &well_formed_error_pos,
7162  &cannot_convert_error_pos,
7163  &from_end_pos);
7164 
7165  if (length_bytes == 1)
7166  *ptr= (uchar) copy_length;
7167  else
7168  int2store(ptr, copy_length);
7169 
7170  return check_string_copy_error(well_formed_error_pos,
7171  cannot_convert_error_pos, from_end_pos,
7172  from + length, true, cs);
7173 }
7174 
7175 
7176 type_conversion_status Field_varstring::store(longlong nr, bool unsigned_val)
7177 {
7178  char buff[64];
7179  uint length;
7180  length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
7181  buff,
7182  sizeof(buff),
7183  (unsigned_val ? 10:
7184  -10),
7185  nr);
7186  return Field_varstring::store(buff, length, field_charset);
7187 }
7188 
7189 
7190 double Field_varstring::val_real(void)
7191 {
7192  ASSERT_COLUMN_MARKED_FOR_READ;
7193  int error;
7194  char *end;
7195  double result;
7196  const CHARSET_INFO* cs= charset();
7197 
7198  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7199  result= my_strntod(cs, (char*)ptr+length_bytes, length, &end, &error);
7200 
7201  if (!table->in_use->no_errors &&
7202  (error || (length != (uint)(end - (char*)ptr+length_bytes) &&
7203  !check_if_only_end_space(cs, end, (char*)ptr+length_bytes+length))))
7204  {
7205  push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes,
7206  length, cs,"DOUBLE",
7207  ER_TRUNCATED_WRONG_VALUE);
7208  }
7209  return result;
7210 }
7211 
7212 
7213 longlong Field_varstring::val_int(void)
7214 {
7215  ASSERT_COLUMN_MARKED_FOR_READ;
7216  int error;
7217  char *end;
7218  const CHARSET_INFO *cs= charset();
7219 
7220  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7221  longlong result= my_strntoll(cs, (char*) ptr+length_bytes, length, 10,
7222  &end, &error);
7223 
7224  if (!table->in_use->no_errors &&
7225  (error || (length != (uint)(end - (char*)ptr+length_bytes) &&
7226  !check_if_only_end_space(cs, end, (char*)ptr+length_bytes+length))))
7227  {
7228  push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes,
7229  length, cs, "INTEGER",
7230  ER_TRUNCATED_WRONG_VALUE);
7231  }
7232  return result;
7233 }
7234 
7235 String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
7236  String *val_ptr)
7237 {
7238  ASSERT_COLUMN_MARKED_FOR_READ;
7239  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7240  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
7241  return val_ptr;
7242 }
7243 
7244 
7245 my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
7246 {
7247  ASSERT_COLUMN_MARKED_FOR_READ;
7248  const CHARSET_INFO *cs= charset();
7249  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7250  int error= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
7251  cs, decimal_value);
7252 
7253  if (!table->in_use->no_errors && error)
7254  {
7255  push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes,
7256  length, cs, "DECIMAL",
7257  ER_TRUNCATED_WRONG_VALUE);
7258  }
7259  return decimal_value;
7260 }
7261 
7262 
7263 int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
7264  uint max_len)
7265 {
7266  uint a_length, b_length;
7267  int diff;
7268 
7269  if (length_bytes == 1)
7270  {
7271  a_length= (uint) *a_ptr;
7272  b_length= (uint) *b_ptr;
7273  }
7274  else
7275  {
7276  a_length= uint2korr(a_ptr);
7277  b_length= uint2korr(b_ptr);
7278  }
7279  set_if_smaller(a_length, max_len);
7280  set_if_smaller(b_length, max_len);
7281  diff= field_charset->coll->strnncollsp(field_charset,
7282  a_ptr+
7283  length_bytes,
7284  a_length,
7285  b_ptr+
7286  length_bytes,
7287  b_length,0);
7288  return diff;
7289 }
7290 
7291 
7297 int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
7298 {
7299  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7300  uint local_char_length= max_key_length / field_charset->mbmaxlen;
7301 
7302  local_char_length= my_charpos(field_charset, ptr + length_bytes,
7303  ptr + length_bytes + length, local_char_length);
7304  set_if_smaller(length, local_char_length);
7305  return field_charset->coll->strnncollsp(field_charset,
7306  ptr + length_bytes,
7307  length,
7308  key_ptr+
7309  HA_KEY_BLOB_LENGTH,
7310  uint2korr(key_ptr), 0);
7311 }
7312 
7313 
7322 int Field_varstring::key_cmp(const uchar *a,const uchar *b)
7323 {
7324  return field_charset->coll->strnncollsp(field_charset,
7325  a + HA_KEY_BLOB_LENGTH,
7326  uint2korr(a),
7327  b + HA_KEY_BLOB_LENGTH,
7328  uint2korr(b),
7329  0);
7330 }
7331 
7332 
7333 void Field_varstring::make_sort_key(uchar *to,uint length)
7334 {
7335  uint tot_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7336 
7337  if (field_charset == &my_charset_bin)
7338  {
7339  /* Store length last in high-byte order to sort longer strings first */
7340  if (length_bytes == 1)
7341  to[length-1]= tot_length;
7342  else
7343  mi_int2store(to+length-2, tot_length);
7344  length-= length_bytes;
7345  }
7346 
7347  tot_length= field_charset->coll->strnxfrm(field_charset,
7348  to, length, char_length(),
7349  ptr + length_bytes, tot_length,
7350  MY_STRXFRM_PAD_WITH_SPACE |
7351  MY_STRXFRM_PAD_TO_MAXLEN);
7352  DBUG_ASSERT(tot_length == length);
7353 }
7354 
7355 
7356 enum ha_base_keytype Field_varstring::key_type() const
7357 {
7358  enum ha_base_keytype res;
7359 
7360  if (binary())
7361  res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
7362  else
7363  res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
7364  return res;
7365 }
7366 
7367 
7368 void Field_varstring::sql_type(String &res) const
7369 {
7370  THD *thd= table->in_use;
7371  const CHARSET_INFO *cs=res.charset();
7372  ulong length;
7373 
7374  length= cs->cset->snprintf(cs,(char*) res.ptr(),
7375  res.alloced_length(), "%s(%d)",
7376  (has_charset() ? "varchar" : "varbinary"),
7377  (int) field_length / charset()->mbmaxlen);
7378  res.length(length);
7379  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
7380  has_charset() && (charset()->state & MY_CS_BINSORT))
7381  res.append(STRING_WITH_LEN(" binary"));
7382 }
7383 
7384 
7385 uint32 Field_varstring::data_length()
7386 {
7387  return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
7388 }
7389 
7390 /*
7391  Functions to create a packed row.
7392  Here the number of length bytes are depending on the given max_length
7393 */
7394 
7395 uchar *Field_varstring::pack(uchar *to, const uchar *from,
7396  uint max_length,
7397  bool low_byte_first __attribute__((unused)))
7398 {
7399  uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
7400  set_if_smaller(max_length, field_length);
7401  if (length > max_length)
7402  length=max_length;
7403 
7404  /* Length always stored little-endian */
7405  *to++= length & 0xFF;
7406  if (max_length > 255)
7407  *to++= (length >> 8) & 0xFF;
7408 
7409  /* Store bytes of string */
7410  if (length > 0)
7411  memcpy(to, from+length_bytes, length);
7412  return to+length;
7413 }
7414 
7415 
7431 const uchar *
7432 Field_varstring::unpack(uchar *to, const uchar *from,
7433  uint param_data,
7434  bool low_byte_first __attribute__((unused)))
7435 {
7436  uint length;
7437  uint l_bytes= (param_data && (param_data < field_length)) ?
7438  (param_data <= 255) ? 1 : 2 : length_bytes;
7439  if (l_bytes == 1)
7440  {
7441  to[0]= *from++;
7442  length= to[0];
7443  if (length_bytes == 2)
7444  to[1]= 0;
7445  }
7446  else /* l_bytes == 2 */
7447  {
7448  length= uint2korr(from);
7449  to[0]= *from++;
7450  to[1]= *from++;
7451  }
7452  if (length)
7453  memcpy(to+ length_bytes, from, length);
7454  return from+length;
7455 }
7456 
7457 
7458 uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
7459 {
7460  if (length > 255)
7461  return uint2korr(data_ptr)+2;
7462  return (uint) *data_ptr + 1;
7463 }
7464 
7465 
7466 uint Field_varstring::max_packed_col_length(uint max_length)
7467 {
7468  return (max_length > 255 ? 2 : 1)+max_length;
7469 }
7470 
7471 uint Field_varstring::get_key_image(uchar *buff, uint length, imagetype type)
7472 {
7473  uint f_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7474  uint local_char_length= length / field_charset->mbmaxlen;
7475  uchar *pos= ptr+length_bytes;
7476  local_char_length= my_charpos(field_charset, pos, pos + f_length,
7477  local_char_length);
7478  set_if_smaller(f_length, local_char_length);
7479  /* Key is always stored with 2 bytes */
7480  int2store(buff,f_length);
7481  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
7482  if (f_length < length)
7483  {
7484  /*
7485  Must clear this as we do a memcmp in opt_range.cc to detect
7486  identical keys
7487  */
7488  memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
7489  }
7490  return HA_KEY_BLOB_LENGTH+f_length;
7491 }
7492 
7493 
7494 void Field_varstring::set_key_image(const uchar *buff,uint length)
7495 {
7496  length= uint2korr(buff); // Real length is here
7497  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
7498  field_charset);
7499 }
7500 
7501 
7502 int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7503  uint32 max_length)
7504 {
7505  uint32 a_length,b_length;
7506 
7507  if (length_bytes == 1)
7508  {
7509  a_length= (uint) *a_ptr;
7510  b_length= (uint) *b_ptr;
7511  }
7512  else
7513  {
7514  a_length= uint2korr(a_ptr);
7515  b_length= uint2korr(b_ptr);
7516  }
7517  set_if_smaller(a_length, max_length);
7518  set_if_smaller(b_length, max_length);
7519  if (a_length != b_length)
7520  return 1;
7521  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
7522 }
7523 
7524 
7525 Field *Field_varstring::new_field(MEM_ROOT *root, TABLE *new_table,
7526  bool keep_type)
7527 {
7528  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
7529  keep_type);
7530  if (res)
7531  res->length_bytes= length_bytes;
7532  return res;
7533 }
7534 
7535 
7536 Field *Field_varstring::new_key_field(MEM_ROOT *root,
7537  TABLE *new_table,
7538  uchar *new_ptr, uchar *new_null_ptr,
7539  uint new_null_bit)
7540 {
7541  Field_varstring *res;
7542  if ((res= (Field_varstring*) Field::new_key_field(root,
7543  new_table,
7544  new_ptr,
7545  new_null_ptr,
7546  new_null_bit)))
7547  {
7548  /* Keys length prefixes are always packed with 2 bytes */
7549  res->length_bytes= 2;
7550  }
7551  return res;
7552 }
7553 
7554 
7556 {
7557  if (new_field->sql_type == real_type() &&
7558  new_field->charset == field_charset)
7559  {
7560  if (new_field->length == max_display_length())
7561  return IS_EQUAL_YES;
7562  if (new_field->length > max_display_length() &&
7563  ((new_field->length <= 255 && max_display_length() <= 255) ||
7564  (new_field->length > 255 && max_display_length() > 255)))
7565  return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
7566  }
7567  return IS_EQUAL_NO;
7568 }
7569 
7570 
7571 void Field_varstring::hash(ulong *nr, ulong *nr2)
7572 {
7573  if (is_null())
7574  {
7575  *nr^= (*nr << 1) | 1;
7576  }
7577  else
7578  {
7579  uint len= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7580  const CHARSET_INFO *cs= charset();
7581  cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
7582  }
7583 }
7584 
7585 
7586 /****************************************************************************
7587 ** blob type
7588 ** A blob is saved as a length and a pointer. The length is stored in the
7589 ** packlength slot and may be from 1-4.
7590 ****************************************************************************/
7591 
7592 Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
7593  enum utype unireg_check_arg, const char *field_name_arg,
7594  TABLE_SHARE *share, uint blob_pack_length,
7595  const CHARSET_INFO *cs)
7596  :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
7597  null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
7598  cs),
7599  packlength(blob_pack_length)
7600 {
7601  DBUG_ASSERT(blob_pack_length <= 4); // Only pack lengths 1-4 supported currently
7602  flags|= BLOB_FLAG;
7603  share->blob_fields++;
7604  /* TODO: why do not fill table->s->blob_field array here? */
7605 }
7606 
7607 
7608 void Field_blob::store_length(uchar *i_ptr,
7609  uint i_packlength,
7610  uint32 i_number,
7611  bool low_byte_first)
7612 {
7613  switch (i_packlength) {
7614  case 1:
7615  i_ptr[0]= (uchar) i_number;
7616  break;
7617  case 2:
7618 #ifdef WORDS_BIGENDIAN
7619  if (low_byte_first)
7620  {
7621  int2store(i_ptr,(unsigned short) i_number);
7622  }
7623  else
7624 #endif
7625  shortstore(i_ptr,(unsigned short) i_number);
7626  break;
7627  case 3:
7628  int3store(i_ptr,i_number);
7629  break;
7630  case 4:
7631 #ifdef WORDS_BIGENDIAN
7632  if (low_byte_first)
7633  {
7634  int4store(i_ptr,i_number);
7635  }
7636  else
7637 #endif
7638  longstore(i_ptr,i_number);
7639  }
7640 }
7641 
7642 
7643 uint32 Field_blob::get_length(const uchar *pos, uint packlength_arg, bool low_byte_first)
7644 {
7645  switch (packlength_arg) {
7646  case 1:
7647  return (uint32) pos[0];
7648  case 2:
7649  {
7650  uint16 tmp;
7651 #ifdef WORDS_BIGENDIAN
7652  if (low_byte_first)
7653  tmp=sint2korr(pos);
7654  else
7655 #endif
7656  shortget(tmp,pos);
7657  return (uint32) tmp;
7658  }
7659  case 3:
7660  return (uint32) uint3korr(pos);
7661  case 4:
7662  {
7663  uint32 tmp;
7664 #ifdef WORDS_BIGENDIAN
7665  if (low_byte_first)
7666  tmp=uint4korr(pos);
7667  else
7668 #endif
7669  longget(tmp,pos);
7670  return (uint32) tmp;
7671  }
7672  }
7673  /* When expanding this, see also MAX_FIELD_BLOBLENGTH. */
7674  return 0; // Impossible
7675 }
7676 
7677 
7689 void Field_blob::put_length(uchar *pos, uint32 length)
7690 {
7691  switch (packlength) {
7692  case 1:
7693  *pos= (char) length;
7694  break;
7695  case 2:
7696  int2store(pos, length);
7697  break;
7698  case 3:
7699  int3store(pos, length);
7700  break;
7701  case 4:
7702  int4store(pos, length);
7703  break;
7704  }
7705 }
7706 
7707 
7716 type_conversion_status
7717 Field_blob::store_to_mem(const char *from, uint length,
7718  const CHARSET_INFO *cs,
7719  uint max_length,
7720  Blob_mem_storage *blob_storage)
7721 {
7722  DBUG_ASSERT(length > 0);
7723  /*
7724  We don't need to support escaping or character set conversions here,
7725  because store_to_mem() is currently called only when we process
7726  queries having GROUP_CONCAT with ORDER BY or DISTINCT,
7727  hence some assersions:
7728  */
7729  DBUG_ASSERT(!f_is_hex_escape(flags));
7730  DBUG_ASSERT(field_charset == cs);
7731  DBUG_ASSERT(length <= max_data_length());
7732 
7733  if (length > max_length)
7734  {
7735  int well_formed_error;
7736  length= cs->cset->well_formed_len(cs, from, from + max_length,
7737  length, &well_formed_error);
7738  table->blob_storage->set_truncated_value(true);
7739  }
7740  char *tmp;
7741  if (!(tmp= table->blob_storage->store(from, length)))
7742  {
7743  memset(ptr, 0, Field_blob::pack_length());
7744  return TYPE_ERR_OOM;
7745  }
7746  store_ptr_and_length(tmp, length);
7747  return TYPE_OK;
7748 }
7749 
7750 
7751 type_conversion_status
7752 Field_blob::store_internal(const char *from, uint length,
7753  const CHARSET_INFO *cs)
7754 {
7755  uint new_length;
7756  char buff[STRING_BUFFER_USUAL_SIZE], *tmp;
7757  String tmpstr(buff,sizeof(buff), &my_charset_bin);
7758 
7759  DBUG_ASSERT(length > 0);
7760 
7761  /*
7762  If the 'from' address is in the range of the temporary 'value'-
7763  object we need to copy the content to a different location or it will be
7764  invalidated when the 'value'-object is reallocated to make room for
7765  the new character set.
7766  */
7767  if (from >= value.ptr() && from <= value.ptr()+value.length())
7768  {
7769  /*
7770  If content of the 'from'-address is cached in the 'value'-object
7771  it is possible that the content needs a character conversion.
7772  */
7773  uint32 dummy_offset;
7774  if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
7775  {
7776  store_ptr_and_length(from, length);
7777  return TYPE_OK;
7778  }
7779  if (tmpstr.copy(from, length, cs))
7780  goto oom_error;
7781  from= tmpstr.ptr();
7782  }
7783 
7784  new_length= min(max_data_length(), field_charset->mbmaxlen * length);
7785  if (value.alloc(new_length))
7786  goto oom_error;
7787  tmp= const_cast<char*>(value.ptr());
7788 
7789  if (f_is_hex_escape(flags))
7790  {
7791  uint copy_length= my_copy_with_hex_escaping(field_charset,
7792  tmp, new_length,
7793  from, length);
7794  store_ptr_and_length(tmp, copy_length);
7795  return TYPE_OK;
7796  }
7797 
7798 
7799  {
7800  const char *well_formed_error_pos;
7801  const char *cannot_convert_error_pos;
7802  const char *from_end_pos;
7803  /*
7804  "length" is OK as "nchars" argument to well_formed_copy_nchars as this
7805  is never used to limit the length of the data. The cut of long data
7806  is done with the new_length value.
7807  */
7808  uint copy_length= well_formed_copy_nchars(field_charset,
7809  tmp, new_length,
7810  cs, from, length,
7811  length,
7812  &well_formed_error_pos,
7813  &cannot_convert_error_pos,
7814  &from_end_pos);
7815 
7816  store_ptr_and_length(tmp, copy_length);
7817  return check_string_copy_error(well_formed_error_pos,
7818  cannot_convert_error_pos, from_end_pos,
7819  from + length, true, cs);
7820  }
7821 
7822 oom_error:
7823  /* Fatal OOM error */
7824  memset(ptr, 0, Field_blob::pack_length());
7825  return TYPE_ERR_OOM;
7826 }
7827 
7828 
7829 type_conversion_status
7830 Field_blob::store(const char *from, uint length, const CHARSET_INFO *cs)
7831 {
7832  ASSERT_COLUMN_MARKED_FOR_WRITE;
7833 
7834  if (!length)
7835  {
7836  memset(ptr, 0, Field_blob::pack_length());
7837  return TYPE_OK;
7838  }
7839 
7840  if (table->blob_storage) // GROUP_CONCAT with ORDER BY | DISTINCT
7841  return store_to_mem(from, length, cs,
7842  table->in_use->variables.group_concat_max_len,
7843  table->blob_storage);
7844 
7845  return store_internal(from, length, cs);
7846 }
7847 
7848 
7849 type_conversion_status Field_blob::store(double nr)
7850 {
7851  const CHARSET_INFO *cs=charset();
7852  value.set_real(nr, NOT_FIXED_DEC, cs);
7853  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
7854 }
7855 
7856 
7857 type_conversion_status Field_blob::store(longlong nr, bool unsigned_val)
7858 {
7859  const CHARSET_INFO *cs=charset();
7860  value.set_int(nr, unsigned_val, cs);
7861  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
7862 }
7863 
7864 
7865 double Field_blob::val_real(void)
7866 {
7867  ASSERT_COLUMN_MARKED_FOR_READ;
7868  int not_used;
7869  char *end_not_used, *blob;
7870  uint32 length;
7871  const CHARSET_INFO *cs;
7872 
7873  memcpy(&blob, ptr+packlength, sizeof(char*));
7874  if (!blob)
7875  return 0.0;
7876  length= get_length(ptr);
7877  cs= charset();
7878  return my_strntod(cs, blob, length, &end_not_used, &not_used);
7879 }
7880 
7881 
7882 longlong Field_blob::val_int(void)
7883 {
7884  ASSERT_COLUMN_MARKED_FOR_READ;
7885  int not_used;
7886  char *blob;
7887  memcpy(&blob, ptr+packlength, sizeof(char*));
7888  if (!blob)
7889  return 0;
7890  uint32 length=get_length(ptr);
7891  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
7892 }
7893 
7894 String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
7895  String *val_ptr)
7896 {
7897  ASSERT_COLUMN_MARKED_FOR_READ;
7898  char *blob;
7899  memcpy(&blob, ptr+packlength, sizeof(char*));
7900  if (!blob)
7901  val_ptr->set("",0,charset()); // A bit safer than ->length(0)
7902  else
7903  val_ptr->set((const char*) blob,get_length(ptr),charset());
7904  return val_ptr;
7905 }
7906 
7907 
7908 my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
7909 {
7910  ASSERT_COLUMN_MARKED_FOR_READ;
7911  const char *blob;
7912  size_t length;
7913  memcpy(&blob, ptr+packlength, sizeof(const uchar*));
7914  if (!blob)
7915  {
7916  blob= "";
7917  length= 0;
7918  }
7919  else
7920  length= get_length(ptr);
7921 
7922  str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
7923  decimal_value);
7924  return decimal_value;
7925 }
7926 
7927 
7928 int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
7929  uint32 b_length)
7930 {
7931  return field_charset->coll->strnncollsp(field_charset,
7932  a, a_length, b, b_length,
7933  0);
7934 }
7935 
7936 
7937 int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
7938  uint max_length)
7939 {
7940  uchar *blob1,*blob2;
7941  memcpy(&blob1, a_ptr+packlength, sizeof(char*));
7942  memcpy(&blob2, b_ptr+packlength, sizeof(char*));
7943  uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
7944  set_if_smaller(a_len, max_length);
7945  set_if_smaller(b_len, max_length);
7946  return Field_blob::cmp(blob1,a_len,blob2,b_len);
7947 }
7948 
7949 
7950 int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7951  uint32 max_length)
7952 {
7953  char *a,*b;
7954  uint diff;
7955  uint32 a_length,b_length;
7956  memcpy(&a, a_ptr+packlength, sizeof(char*));
7957  memcpy(&b, b_ptr+packlength, sizeof(char*));
7958  a_length=get_length(a_ptr);
7959  if (a_length > max_length)
7960  a_length=max_length;
7961  b_length=get_length(b_ptr);
7962  if (b_length > max_length)
7963  b_length=max_length;
7964  diff=memcmp(a,b,min(a_length,b_length));
7965  return diff ? diff : (int) (a_length - b_length);
7966 }
7967 
7968 
7969 /* The following is used only when comparing a key */
7970 
7971 uint Field_blob::get_key_image(uchar *buff,uint length, imagetype type_arg)
7972 {
7973  uint32 blob_length= get_length(ptr);
7974  uchar *blob;
7975 
7976 #ifdef HAVE_SPATIAL
7977  if (type_arg == itMBR)
7978  {
7979  MBR mbr;
7980  Geometry_buffer buffer;
7981  Geometry *gobj;
7982  const uint image_length= SIZEOF_STORED_DOUBLE*4;
7983 
7984  if (blob_length < SRID_SIZE)
7985  {
7986  memset(buff, 0, image_length);
7987  return image_length;
7988  }
7989  get_ptr(&blob);
7990  gobj= Geometry::construct(&buffer, (char*) blob, blob_length);
7991  if (!gobj || gobj->get_mbr(&mbr))
7992  memset(buff, 0, image_length);
7993  else
7994  {
7995  float8store(buff, mbr.xmin);
7996  float8store(buff+8, mbr.xmax);
7997  float8store(buff+16, mbr.ymin);
7998  float8store(buff+24, mbr.ymax);
7999  }
8000  return image_length;
8001  }
8002 #endif /*HAVE_SPATIAL*/
8003 
8004  get_ptr(&blob);
8005  uint local_char_length= length / field_charset->mbmaxlen;
8006  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
8007  local_char_length);
8008  set_if_smaller(blob_length, local_char_length);
8009 
8010  if ((uint32) length > blob_length)
8011  {
8012  /*
8013  Must clear this as we do a memcmp in opt_range.cc to detect
8014  identical keys
8015  */
8016  memset(buff+HA_KEY_BLOB_LENGTH+blob_length, 0, (length-blob_length));
8017  length=(uint) blob_length;
8018  }
8019  int2store(buff,length);
8020  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
8021  return HA_KEY_BLOB_LENGTH+length;
8022 }
8023 
8024 
8025 void Field_blob::set_key_image(const uchar *buff,uint length)
8026 {
8027  length= uint2korr(buff);
8028  (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
8029  field_charset);
8030 }
8031 
8032 
8033 int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
8034 {
8035  uchar *blob1;
8036  uint blob_length=get_length(ptr);
8037  memcpy(&blob1, ptr+packlength, sizeof(char*));
8038  const CHARSET_INFO *cs= charset();
8039  uint local_char_length= max_key_length / cs->mbmaxlen;
8040  local_char_length= my_charpos(cs, blob1, blob1+blob_length,
8041  local_char_length);
8042  set_if_smaller(blob_length, local_char_length);
8043  return Field_blob::cmp(blob1, blob_length,
8044  key_ptr+HA_KEY_BLOB_LENGTH,
8045  uint2korr(key_ptr));
8046 }
8047 
8048 int Field_blob::key_cmp(const uchar *a,const uchar *b)
8049 {
8050  return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
8051  b+HA_KEY_BLOB_LENGTH, uint2korr(b));
8052 }
8053 
8054 
8065 int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
8066 {
8067  DBUG_ENTER("Field_blob::do_save_field_metadata");
8068  *metadata_ptr= pack_length_no_ptr();
8069  DBUG_PRINT("debug", ("metadata: %u (pack_length_no_ptr)", *metadata_ptr));
8070  DBUG_RETURN(1);
8071 }
8072 
8073 
8074 uint32 Field_blob::sort_length() const
8075 {
8076  return (uint32) (current_thd->variables.max_sort_length +
8077  (field_charset == &my_charset_bin ? 0 : packlength));
8078 }
8079 
8080 
8081 void Field_blob::make_sort_key(uchar *to,uint length)
8082 {
8083  uchar *blob;
8084  uint blob_length=get_length();
8085 
8086  if (!blob_length)
8087  memset(to, 0, length);
8088  else
8089  {
8090  if (field_charset == &my_charset_bin)
8091  {
8092  uchar *pos;
8093 
8094  /*
8095  Store length of blob last in blob to shorter blobs before longer blobs
8096  */
8097  length-= packlength;
8098  pos= to+length;
8099 
8100  switch (packlength) {
8101  case 1:
8102  *pos= (char) blob_length;
8103  break;
8104  case 2:
8105  mi_int2store(pos, blob_length);
8106  break;
8107  case 3:
8108  mi_int3store(pos, blob_length);
8109  break;
8110  case 4:
8111  mi_int4store(pos, blob_length);
8112  break;
8113  }
8114  }
8115  memcpy(&blob, ptr+packlength, sizeof(char*));
8116 
8117  blob_length= field_charset->coll->strnxfrm(field_charset,
8118  to, length, length,
8119  blob, blob_length,
8120  MY_STRXFRM_PAD_WITH_SPACE |
8121  MY_STRXFRM_PAD_TO_MAXLEN);
8122  DBUG_ASSERT(blob_length == length);
8123  }
8124 }
8125 
8126 
8127 void Field_blob::sql_type(String &res) const
8128 {
8129  const char *str;
8130  uint length;
8131  switch (packlength) {
8132  default: str="tiny"; length=4; break;
8133  case 2: str=""; length=0; break;
8134  case 3: str="medium"; length= 6; break;
8135  case 4: str="long"; length=4; break;
8136  }
8137  res.set_ascii(str,length);
8138  if (charset() == &my_charset_bin)
8139  res.append(STRING_WITH_LEN("blob"));
8140  else
8141  {
8142  res.append(STRING_WITH_LEN("text"));
8143  }
8144 }
8145 
8146 uchar *Field_blob::pack(uchar *to, const uchar *from,
8147  uint max_length, bool low_byte_first)
8148 {
8149  uchar *save= ptr;
8150  ptr= (uchar*) from;
8151  uint32 length=get_length(); // Length of from string
8152 
8153  /*
8154  Store max length, which will occupy packlength bytes. If the max
8155  length given is smaller than the actual length of the blob, we
8156  just store the initial bytes of the blob.
8157  */
8158  store_length(to, packlength, min(length, max_length), low_byte_first);
8159 
8160  /*
8161  Store the actual blob data, which will occupy 'length' bytes.
8162  */
8163  if (length > 0)
8164  {
8165  get_ptr((uchar**) &from);
8166  memcpy(to+packlength, from,length);
8167  }
8168  ptr=save; // Restore org row pointer
8169  return to+packlength+length;
8170 }
8171 
8172 
8190 const uchar *Field_blob::unpack(uchar *to,
8191  const uchar *from,
8192  uint param_data,
8193  bool low_byte_first)
8194 {
8195  DBUG_ENTER("Field_blob::unpack");
8196  DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
8197  " param_data: %u; low_byte_first: %d",
8198  (ulong) to, (ulong) from, param_data, low_byte_first));
8199  uint const master_packlength=
8200  param_data > 0 ? param_data & 0xFF : packlength;
8201  uint32 const length= get_length(from, master_packlength, low_byte_first);
8202  DBUG_DUMP("packed", from, length + master_packlength);
8203  bitmap_set_bit(table->write_set, field_index);
8204  store(reinterpret_cast<const char*>(from) + master_packlength,
8205  length, field_charset);
8206 #ifndef DBUG_OFF
8207  uchar *vptr;
8208  get_ptr(&vptr);
8209  DBUG_DUMP("field", ptr, pack_length() /* len bytes + ptr bytes */);
8210  DBUG_DUMP("value", vptr, length /* the blob value length */);
8211 #endif
8212  DBUG_RETURN(from + master_packlength + length);
8213 }
8214 
8215 uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
8216 {
8217  if (length > 255)
8218  return uint2korr(data_ptr)+2;
8219  return (uint) *data_ptr + 1;
8220 }
8221 
8222 
8223 uint Field_blob::max_packed_col_length(uint max_length)
8224 {
8225  return (max_length > 255 ? 2 : 1)+max_length;
8226 }
8227 
8228 
8230 {
8231  if (field_flags_are_binary() != new_field->field_flags_are_binary())
8232  return 0;
8233 
8234  return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
8235  && new_field->charset == field_charset &&
8236  new_field->pack_length == pack_length());
8237 }
8238 
8239 
8240 #ifdef HAVE_SPATIAL
8241 
8242 void Field_geom::sql_type(String &res) const
8243 {
8244  const CHARSET_INFO *cs= &my_charset_latin1;
8245  switch (geom_type)
8246  {
8247  case GEOM_POINT:
8248  res.set(STRING_WITH_LEN("point"), cs);
8249  break;
8250  case GEOM_LINESTRING:
8251  res.set(STRING_WITH_LEN("linestring"), cs);
8252  break;
8253  case GEOM_POLYGON:
8254  res.set(STRING_WITH_LEN("polygon"), cs);
8255  break;
8256  case GEOM_MULTIPOINT:
8257  res.set(STRING_WITH_LEN("multipoint"), cs);
8258  break;
8259  case GEOM_MULTILINESTRING:
8260  res.set(STRING_WITH_LEN("multilinestring"), cs);
8261  break;
8262  case GEOM_MULTIPOLYGON:
8263  res.set(STRING_WITH_LEN("multipolygon"), cs);
8264  break;
8265  case GEOM_GEOMETRYCOLLECTION:
8266  res.set(STRING_WITH_LEN("geometrycollection"), cs);
8267  break;
8268  default:
8269  res.set(STRING_WITH_LEN("geometry"), cs);
8270  }
8271 }
8272 
8273 
8274 type_conversion_status Field_geom::store(double nr)
8275 {
8276  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
8277  ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
8278  return TYPE_ERR_BAD_VALUE;
8279 }
8280 
8281 
8282 type_conversion_status Field_geom::store(longlong nr, bool unsigned_val)
8283 {
8284  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
8285  ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
8286  return TYPE_ERR_BAD_VALUE;
8287 }
8288 
8289 
8290 type_conversion_status Field_geom::store_decimal(const my_decimal *)
8291 {
8292  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
8293  ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
8294  return TYPE_ERR_BAD_VALUE;
8295 }
8296 
8297 
8298 type_conversion_status
8299 Field_geom::store_internal(const char *from, uint length,
8300  const CHARSET_INFO *cs)
8301 {
8302  uint32 wkb_type;
8303 
8304  DBUG_ASSERT(length > 0);
8305 
8306  // Check given WKB
8307  if (from == Geometry::bad_geometry_data.ptr() ||
8308  length < SRID_SIZE + WKB_HEADER_SIZE + SIZEOF_STORED_DOUBLE * 2 ||
8309  (wkb_type= uint4korr(from + SRID_SIZE + 1)) < (uint32) Geometry::wkb_point ||
8310  wkb_type > (uint32) Geometry::wkb_last)
8311  {
8312  memset(ptr, 0, Field_blob::pack_length());
8313  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
8314  ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
8315  return TYPE_ERR_BAD_VALUE;
8316  }
8317 
8318  if (table->copy_blobs || length <= MAX_FIELD_WIDTH)
8319  { // Must make a copy
8320  value.copy(from, length, cs);
8321  from= value.ptr();
8322  }
8323 
8324  store_ptr_and_length(from, length);
8325  return TYPE_OK;
8326 }
8327 
8328 
8329 uint Field_geom::is_equal(Create_field *new_field)
8330 {
8331  return new_field->field_flags_are_binary() == field_flags_are_binary() &&
8332  new_field->sql_type == real_type() &&
8333  new_field->geom_type == get_geometry_type() &&
8334  new_field->charset == field_charset &&
8335  new_field->pack_length == pack_length();
8336 }
8337 
8338 
8339 #endif /*HAVE_SPATIAL*/
8340 
8341 /****************************************************************************
8342 ** enum type.
8343 ** This is a string which only can have a selection of different values.
8344 ** If one uses this string in a number context one gets the type number.
8345 ****************************************************************************/
8346 
8347 enum ha_base_keytype Field_enum::key_type() const
8348 {
8349  switch (packlength) {
8350  default: return HA_KEYTYPE_BINARY;
8351  case 2: return HA_KEYTYPE_USHORT_INT;
8352  case 3: return HA_KEYTYPE_UINT24;
8353  case 4: return HA_KEYTYPE_ULONG_INT;
8354  case 8: return HA_KEYTYPE_ULONGLONG;
8355  }
8356 }
8357 
8358 void Field_enum::store_type(ulonglong value)
8359 {
8360  switch (packlength) {
8361  case 1: ptr[0]= (uchar) value; break;
8362  case 2:
8363 #ifdef WORDS_BIGENDIAN
8364  if (table->s->db_low_byte_first)
8365  {
8366  int2store(ptr,(unsigned short) value);
8367  }
8368  else
8369 #endif
8370  shortstore(ptr,(unsigned short) value);
8371  break;
8372  case 3: int3store(ptr,(long) value); break;
8373  case 4:
8374 #ifdef WORDS_BIGENDIAN
8375  if (table->s->db_low_byte_first)
8376  {
8377  int4store(ptr,value);
8378  }
8379  else
8380 #endif
8381  longstore(ptr,(long) value);
8382  break;
8383  case 8:
8384 #ifdef WORDS_BIGENDIAN
8385  if (table->s->db_low_byte_first)
8386  {
8387  int8store(ptr,value);
8388  }
8389  else
8390 #endif
8391  longlongstore(ptr,value); break;
8392  }
8393 }
8394 
8395 
8402 type_conversion_status
8403 Field_enum::store(const char *from,uint length,const CHARSET_INFO *cs)
8404 {
8405  ASSERT_COLUMN_MARKED_FOR_WRITE;
8406  int err= 0;
8407  type_conversion_status ret= TYPE_OK;
8408  uint32 not_used;
8409  char buff[STRING_BUFFER_USUAL_SIZE];
8410  String tmpstr(buff,sizeof(buff), &my_charset_bin);
8411 
8412  /* Convert character set if necessary */
8413  if (String::needs_conversion(length, cs, field_charset, &not_used))
8414  {
8415  uint dummy_errors;
8416  tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
8417  from= tmpstr.ptr();
8418  length= tmpstr.length();
8419  }
8420 
8421  /* Remove end space */
8422  length= field_charset->cset->lengthsp(field_charset, from, length);
8423  uint tmp=find_type2(typelib, from, length, field_charset);
8424  if (!tmp)
8425  {
8426  if (length < 6) // Can't be more than 99999 enums
8427  {
8428  /* This is for reading numbers with LOAD DATA INFILE */
8429  char *end;
8430  tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
8431  if (err || end != from+length || tmp > typelib->count)
8432  {
8433  tmp=0;
8434  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8435  ret= TYPE_WARN_TRUNCATED;
8436  }
8437  if (!table->in_use->count_cuted_fields)
8438  ret= TYPE_OK;
8439  }
8440  else
8441  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8442  }
8443  store_type((ulonglong) tmp);
8444  return ret;
8445 }
8446 
8447 
8448 type_conversion_status Field_enum::store(double nr)
8449 {
8450  return Field_enum::store((longlong) nr, FALSE);
8451 }
8452 
8453 
8454 type_conversion_status Field_enum::store(longlong nr, bool unsigned_val)
8455 {
8456  ASSERT_COLUMN_MARKED_FOR_WRITE;
8457  type_conversion_status error= TYPE_OK;
8458  if ((ulonglong) nr > typelib->count || nr == 0)
8459  {
8460  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8461  if (nr != 0 || table->in_use->count_cuted_fields)
8462  {
8463  nr= 0;
8464  error= TYPE_WARN_TRUNCATED;
8465  }
8466  }
8467  store_type((ulonglong) (uint) nr);
8468  return error;
8469 }
8470 
8471 
8472 double Field_enum::val_real(void)
8473 {
8474  return (double) Field_enum::val_int();
8475 }
8476 
8477 
8478 my_decimal *Field_enum::val_decimal(my_decimal *decimal_value)
8479 {
8480  ASSERT_COLUMN_MARKED_FOR_READ;
8481  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 0, decimal_value);
8482  return decimal_value;
8483 }
8484 
8485 
8486 longlong Field_enum::val_int(void)
8487 {
8488  ASSERT_COLUMN_MARKED_FOR_READ;
8489  switch (packlength) {
8490  case 1:
8491  return (longlong) ptr[0];
8492  case 2:
8493  {
8494  uint16 tmp;
8495 #ifdef WORDS_BIGENDIAN
8496  if (table->s->db_low_byte_first)
8497  tmp=sint2korr(ptr);
8498  else
8499 #endif
8500  shortget(tmp,ptr);
8501  return (longlong) tmp;
8502  }
8503  case 3:
8504  return (longlong) uint3korr(ptr);
8505  case 4:
8506  {
8507  uint32 tmp;
8508 #ifdef WORDS_BIGENDIAN
8509  if (table->s->db_low_byte_first)
8510  tmp=uint4korr(ptr);
8511  else
8512 #endif
8513  longget(tmp,ptr);
8514  return (longlong) tmp;
8515  }
8516  case 8:
8517  {
8518  longlong tmp;
8519 #ifdef WORDS_BIGENDIAN
8520  if (table->s->db_low_byte_first)
8521  tmp=sint8korr(ptr);
8522  else
8523 #endif
8524  longlongget(tmp,ptr);
8525  return tmp;
8526  }
8527  }
8528  return 0; // impossible
8529 }
8530 
8531 
8543 int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
8544 {
8545  *metadata_ptr= real_type();
8546  *(metadata_ptr + 1)= pack_length();
8547  return 2;
8548 }
8549 
8550 
8551 String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
8552  String *val_ptr)
8553 {
8554  uint tmp=(uint) Field_enum::val_int();
8555  if (!tmp || tmp > typelib->count)
8556  val_ptr->set("", 0, field_charset);
8557  else
8558  val_ptr->set((const char*) typelib->type_names[tmp-1],
8559  typelib->type_lengths[tmp-1],
8560  field_charset);
8561  return val_ptr;
8562 }
8563 
8564 int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
8565 {
8566  uchar *old= ptr;
8567  ptr= (uchar*) a_ptr;
8568  ulonglong a=Field_enum::val_int();
8569  ptr= (uchar*) b_ptr;
8570  ulonglong b=Field_enum::val_int();
8571  ptr= old;
8572  return (a < b) ? -1 : (a > b) ? 1 : 0;
8573 }
8574 
8575 void Field_enum::make_sort_key(uchar *to, uint length)
8576 {
8577 #ifdef WORDS_BIGENDIAN
8578  if (!table->s->db_low_byte_first)
8579  copy_integer<true>(to, length, ptr, packlength, true);
8580  else
8581 #endif
8582  copy_integer<false>(to, length, ptr, packlength, true);
8583 }
8584 
8585 
8586 void Field_enum::sql_type(String &res) const
8587 {
8588  char buffer[255];
8589  String enum_item(buffer, sizeof(buffer), res.charset());
8590 
8591  res.length(0);
8592  res.append(STRING_WITH_LEN("enum("));
8593 
8594  bool flag=0;
8595  uint *len= typelib->type_lengths;
8596  for (const char **pos= typelib->type_names; *pos; pos++, len++)
8597  {
8598  uint dummy_errors;
8599  if (flag)
8600  res.append(',');
8601  /* convert to res.charset() == utf8, then quote */
8602  enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8603  append_unescaped(&res, enum_item.ptr(), enum_item.length());
8604  flag= 1;
8605  }
8606  res.append(')');
8607 }
8608 
8609 
8610 Field *Field_enum::new_field(MEM_ROOT *root, TABLE *new_table,
8611  bool keep_type)
8612 {
8613  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
8614  if (res)
8615  res->typelib= copy_typelib(root, typelib);
8616  return res;
8617 }
8618 
8619 
8620 /*
8621  set type.
8622  This is a string which can have a collection of different values.
8623  Each string value is separated with a ','.
8624  For example "One,two,five"
8625  If one uses this string in a number context one gets the bits as a longlong
8626  number.
8627 */
8628 
8629 
8630 type_conversion_status
8631 Field_set::store(const char *from,uint length,const CHARSET_INFO *cs)
8632 {
8633  ASSERT_COLUMN_MARKED_FOR_WRITE;
8634  bool got_warning= 0;
8635  int err= 0;
8636  type_conversion_status ret= TYPE_OK;
8637  char *not_used;
8638  uint not_used2;
8639  uint32 not_used_offset;
8640  char buff[STRING_BUFFER_USUAL_SIZE];
8641  String tmpstr(buff,sizeof(buff), &my_charset_bin);
8642 
8643  /* Convert character set if necessary */
8644  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
8645  {
8646  uint dummy_errors;
8647  tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
8648  from= tmpstr.ptr();
8649  length= tmpstr.length();
8650  }
8651  ulonglong tmp= find_set(typelib, from, length, field_charset,
8652  &not_used, &not_used2, &got_warning);
8653  if (!tmp && length && length < 22)
8654  {
8655  /* This is for reading numbers with LOAD DATA INFILE */
8656  char *end;
8657  tmp=my_strntoull(cs,from,length,10,&end,&err);
8658  if (err || end != from+length ||
8659  tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
8660  {
8661  tmp=0;
8662  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8663  ret= TYPE_WARN_TRUNCATED;
8664  }
8665  }
8666  else if (got_warning)
8667  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8668  store_type(tmp);
8669  return ret;
8670 }
8671 
8672 
8673 type_conversion_status Field_set::store(longlong nr, bool unsigned_val)
8674 {
8675  ASSERT_COLUMN_MARKED_FOR_WRITE;
8676  type_conversion_status error= TYPE_OK;
8677  ulonglong max_nr;
8678 
8679  if (sizeof(ulonglong)*8 <= typelib->count)
8680  max_nr= ULONGLONG_MAX;
8681  else
8682  max_nr= (ULL(1) << typelib->count) - 1;
8683 
8684  if ((ulonglong) nr > max_nr)
8685  {
8686  nr&= max_nr;
8687  set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8688  error= TYPE_WARN_TRUNCATED;
8689  }
8690  store_type((ulonglong) nr);
8691  return error;
8692 }
8693 
8694 
8695 String *Field_set::val_str(String *val_buffer,
8696  String *val_ptr __attribute__((unused)))
8697 {
8698  ulonglong tmp=(ulonglong) Field_enum::val_int();
8699  uint bitnr=0;
8700 
8701  if (tmp == 0)
8702  {
8703  /*
8704  Some callers expect *val_buffer to contain the result,
8705  so we assign to it, rather than doing 'return &empty_set_string.
8706  */
8707  *val_buffer= empty_set_string;
8708  return val_buffer;
8709  }
8710 
8711  val_buffer->set_charset(field_charset);
8712  val_buffer->length(0);
8713 
8714  while (tmp && bitnr < (uint) typelib->count)
8715  {
8716  if (tmp & 1)
8717  {
8718  if (val_buffer->length())
8719  val_buffer->append(&field_separator, 1, &my_charset_latin1);
8720  String str(typelib->type_names[bitnr],
8721  typelib->type_lengths[bitnr],
8722  field_charset);
8723  val_buffer->append(str);
8724  }
8725  tmp>>=1;
8726  bitnr++;
8727  }
8728  return val_buffer;
8729 }
8730 
8731 
8732 void Field_set::sql_type(String &res) const
8733 {
8734  char buffer[255];
8735  String set_item(buffer, sizeof(buffer), res.charset());
8736 
8737  res.length(0);
8738  res.append(STRING_WITH_LEN("set("));
8739 
8740  bool flag=0;
8741  uint *len= typelib->type_lengths;
8742  for (const char **pos= typelib->type_names; *pos; pos++, len++)
8743  {
8744  uint dummy_errors;
8745  if (flag)
8746  res.append(',');
8747  /* convert to res.charset() == utf8, then quote */
8748  set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8749  append_unescaped(&res, set_item.ptr(), set_item.length());
8750  flag= 1;
8751  }
8752  res.append(')');
8753 }
8754 
8762 bool Field::eq_def(Field *field)
8763 {
8764  if (real_type() != field->real_type() || charset() != field->charset() ||
8765  pack_length() != field->pack_length())
8766  return 0;
8767  return 1;
8768 }
8769 
8770 
8777 static bool compare_type_names(const CHARSET_INFO *charset,
8778  TYPELIB *t1, TYPELIB *t2)
8779 {
8780  for (uint i= 0; i < t1->count; i++)
8781  if (my_strnncoll(charset,
8782  (const uchar*) t1->type_names[i],
8783  t1->type_lengths[i],
8784  (const uchar*) t2->type_names[i],
8785  t2->type_lengths[i]))
8786  return FALSE;
8787  return TRUE;
8788 }
8789 
8796 {
8797  TYPELIB *values;
8798 
8799  if (!Field::eq_def(field))
8800  return FALSE;
8801 
8802  values= ((Field_enum*) field)->typelib;
8803 
8804  /* Definition must be strictly equal. */
8805  if (typelib->count != values->count)
8806  return FALSE;
8807 
8808  return compare_type_names(field_charset, typelib, values);
8809 }
8810 
8811 
8821 uint Field_enum::is_equal(Create_field *new_field)
8822 {
8823  TYPELIB *values= new_field->interval;
8824 
8825  /*
8826  The fields are compatible if they have the same flags,
8827  type, charset and have the same underlying length.
8828  */
8829  if (new_field->field_flags_are_binary() != field_flags_are_binary() ||
8830  new_field->sql_type != real_type() ||
8831  new_field->charset != field_charset ||
8832  new_field->pack_length != pack_length())
8833  return IS_EQUAL_NO;
8834 
8835  /*
8836  Changing the definition of an ENUM or SET column by adding a new
8837  enumeration or set members to the end of the list of valid member
8838  values only alters table metadata and not table data.
8839  */
8840  if (typelib->count > values->count)
8841  return IS_EQUAL_NO;
8842 
8843  /* Check whether there are modification before the end. */
8844  if (! compare_type_names(field_charset, typelib, new_field->interval))
8845  return IS_EQUAL_NO;
8846 
8847  return IS_EQUAL_YES;
8848 }
8849 
8850 
8851 uchar *Field_enum::pack(uchar *to, const uchar *from,
8852  uint max_length, bool low_byte_first)
8853 {
8854  DBUG_ENTER("Field_enum::pack");
8855  DBUG_PRINT("debug", ("packlength: %d", packlength));
8856  DBUG_DUMP("from", from, packlength);
8857 
8858  switch (packlength)
8859  {
8860  case 1:
8861  *to = *from;
8862  DBUG_RETURN(to + 1);
8863  case 2: DBUG_RETURN(pack_int16(to, from, low_byte_first));
8864  case 3: DBUG_RETURN(pack_int24(to, from, low_byte_first));
8865  case 4: DBUG_RETURN(pack_int32(to, from, low_byte_first));
8866  case 8: DBUG_RETURN(pack_int64(to, from, low_byte_first));
8867  default:
8868  DBUG_ASSERT(0);
8869  }
8870  MY_ASSERT_UNREACHABLE();
8871  DBUG_RETURN(NULL);
8872 }
8873 
8874 const uchar *Field_enum::unpack(uchar *to, const uchar *from,
8875  uint param_data, bool low_byte_first)
8876 {
8877  DBUG_ENTER("Field_enum::unpack");
8878  DBUG_PRINT("debug", ("packlength: %d", packlength));
8879  DBUG_DUMP("from", from, packlength);
8880 
8881  switch (packlength)
8882  {
8883  case 1:
8884  *to = *from;
8885  DBUG_RETURN(from + 1);
8886 
8887  case 2: DBUG_RETURN(unpack_int16(to, from, low_byte_first));
8888  case 3: DBUG_RETURN(unpack_int24(to, from, low_byte_first));
8889  case 4: DBUG_RETURN(unpack_int32(to, from, low_byte_first));
8890  case 8: DBUG_RETURN(unpack_int64(to, from, low_byte_first));
8891  default:
8892  DBUG_ASSERT(0);
8893  }
8894  MY_ASSERT_UNREACHABLE();
8895  DBUG_RETURN(NULL);
8896 }
8897 
8898 
8904 {
8905  if (!Field::eq_def(field))
8906  return 0;
8907  Field_num *from_num= (Field_num*) field;
8908 
8909  if (unsigned_flag != from_num->unsigned_flag ||
8910  (zerofill && !from_num->zerofill && !zero_pack()) ||
8911  dec != from_num->dec)
8912  return 0;
8913  return 1;
8914 }
8915 
8916 
8924 {
8925  return ((new_field->sql_type == real_type()) &&
8926  ((new_field->flags & UNSIGNED_FLAG) ==
8927  (uint) (flags & UNSIGNED_FLAG)) &&
8928  ((new_field->flags & AUTO_INCREMENT_FLAG) ==
8929  (uint) (flags & AUTO_INCREMENT_FLAG)) &&
8930  (new_field->pack_length == pack_length()));
8931 }
8932 
8933 
8934 /*
8935  Bit field.
8936 
8937  We store the first 0 - 6 uneven bits among the null bits
8938  at the start of the record. The rest bytes are stored in
8939  the record itself.
8940 
8941  For example:
8942 
8943  CREATE TABLE t1 (a int, b bit(17), c bit(21) not null, d bit(8));
8944  We would store data as follows in the record:
8945 
8946  Byte Bit
8947  1 7 - reserve for delete
8948  6 - null bit for 'a'
8949  5 - null bit for 'b'
8950  4 - first (high) bit of 'b'
8951  3 - first (high) bit of 'c'
8952  2 - second bit of 'c'
8953  1 - third bit of 'c'
8954  0 - forth bit of 'c'
8955  2 7 - firth bit of 'c'
8956  6 - null bit for 'd'
8957  3 - 6 four bytes for 'a'
8958  7 - 8 two bytes for 'b'
8959  9 - 10 two bytes for 'c'
8960  11 one byte for 'd'
8961 */
8962 
8963 Field_bit::Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
8964  uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
8965  enum utype unireg_check_arg, const char *field_name_arg)
8966  : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
8967  unireg_check_arg, field_name_arg),
8968  bit_ptr(bit_ptr_arg), bit_ofs(bit_ofs_arg), bit_len(len_arg & 7),
8969  bytes_in_rec(len_arg / 8)
8970 {
8971  DBUG_ENTER("Field_bit::Field_bit");
8972  DBUG_PRINT("enter", ("ptr_arg: %p, null_ptr_arg: %p, len_arg: %u, bit_len: %u, bytes_in_rec: %u",
8973  ptr_arg, null_ptr_arg, len_arg, bit_len, bytes_in_rec));
8974  flags|= UNSIGNED_FLAG;
8975  /*
8976  Ensure that Field::eq() can distinguish between two different bit fields.
8977  (two bit fields that are not null, may have same ptr and null_ptr)
8978  */
8979  if (!null_ptr_arg)
8980  null_bit= bit_ofs_arg;
8981  DBUG_VOID_RETURN;
8982 }
8983 
8984 
8985 void Field_bit::hash(ulong *nr, ulong *nr2)
8986 {
8987  if (is_null())
8988  {
8989  *nr^= (*nr << 1) | 1;
8990  }
8991  else
8992  {
8993  const CHARSET_INFO *cs= &my_charset_bin;
8994  longlong value= Field_bit::val_int();
8995  uchar tmp[8];
8996  mi_int8store(tmp,value);
8997  cs->coll->hash_sort(cs, tmp, 8, nr, nr2);
8998  }
8999 }
9000 
9001 
9002 size_t
9003 Field_bit::do_last_null_byte() const
9004 {
9005  /*
9006  Code elsewhere is assuming that bytes are 8 bits, so I'm using
9007  that value instead of the correct one: CHAR_BIT.
9008 
9009  REFACTOR SUGGESTION (Matz): Change to use the correct number of
9010  bits. On systems with CHAR_BIT > 8 (not very common), the storage
9011  will lose the extra bits.
9012  */
9013  DBUG_PRINT("test", ("bit_ofs: %d, bit_len: %d bit_ptr: 0x%lx",
9014  bit_ofs, bit_len, (long) bit_ptr));
9015  uchar *result;
9016  if (bit_len == 0)
9017  result= null_ptr;
9018  else if (bit_ofs + bit_len > 8)
9019  result= bit_ptr + 1;
9020  else
9021  result= bit_ptr;
9022 
9023  if (result)
9024  return (size_t) (result - table->record[0]) + 1;
9025  return LAST_NULL_BYTE_UNDEF;
9026 }
9027 
9028 
9029 Field *Field_bit::new_key_field(MEM_ROOT *root,
9030  TABLE *new_table,
9031  uchar *new_ptr, uchar *new_null_ptr,
9032  uint new_null_bit)
9033 {
9034  Field_bit *res;
9035  if ((res= (Field_bit*) Field::new_key_field(root, new_table,
9036  new_ptr, new_null_ptr,
9037  new_null_bit)))
9038  {
9039  /* Move bits normally stored in null_pointer to new_ptr */
9040  res->bit_ptr= new_ptr;
9041  res->bit_ofs= 0;
9042  if (bit_len)
9043  res->ptr++; // Store rest of data here
9044  }
9045  return res;
9046 }
9047 
9048 
9050 {
9051  return (new_field->sql_type == real_type() &&
9052  new_field->length == max_display_length());
9053 }
9054 
9055 
9056 type_conversion_status
9057 Field_bit::store(const char *from, uint length, const CHARSET_INFO *cs)
9058 {
9059  ASSERT_COLUMN_MARKED_FOR_WRITE;
9060  int delta;
9061 
9062  for (; length && !*from; from++, length--) ; // skip left 0's
9063  delta= bytes_in_rec - length;
9064 
9065  /*
9066  *from should probably be treated like uint here see BUG#13727586
9067  */
9068  if (delta < -1 ||
9069  (delta == -1 && (uchar) *from > ((1 << bit_len) - 1)) ||
9070  (!bit_len && delta < 0))
9071  {
9072  set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
9073  memset(ptr, 0xff, bytes_in_rec);
9074  if (table->in_use->really_abort_on_warning())
9075  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
9076  else
9077  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
9078  return TYPE_WARN_OUT_OF_RANGE;
9079  }
9080  /* delta is >= -1 here */
9081  if (delta > 0)
9082  {
9083  if (bit_len)
9084  clr_rec_bits(bit_ptr, bit_ofs, bit_len);
9085  memset(ptr, 0, delta);
9086  memcpy(ptr + delta, from, length);
9087  }
9088  else if (delta == 0)
9089  {
9090  if (bit_len)
9091  clr_rec_bits(bit_ptr, bit_ofs, bit_len);
9092  memcpy(ptr, from, length);
9093  }
9094  else
9095  {
9096  if (bit_len)
9097  {
9098  set_rec_bits((uchar) *from, bit_ptr, bit_ofs, bit_len);
9099  from++;
9100  }
9101  memcpy(ptr, from, bytes_in_rec);
9102  }
9103  return TYPE_OK;
9104 }
9105 
9106 
9107 type_conversion_status Field_bit::store(double nr)
9108 {
9109  return Field_bit::store((longlong) nr, FALSE);
9110 }
9111 
9112 
9113 type_conversion_status Field_bit::store(longlong nr, bool unsigned_val)
9114 {
9115  char buf[8];
9116 
9117  mi_int8store(buf, nr);
9118  return store(buf, 8, NULL);
9119 }
9120 
9121 
9122 type_conversion_status Field_bit::store_decimal(const my_decimal *val)
9123 {
9124  bool has_overflow= false;
9125  longlong i= convert_decimal2longlong(val, 1, &has_overflow);
9126  type_conversion_status res= store(i, TRUE);
9127  return has_overflow ? TYPE_WARN_OUT_OF_RANGE : res;
9128 }
9129 
9130 
9131 double Field_bit::val_real(void)
9132 {
9133  return (double) Field_bit::val_int();
9134 }
9135 
9136 
9137 longlong Field_bit::val_int(void)
9138 {
9139  ASSERT_COLUMN_MARKED_FOR_READ;
9140  ulonglong bits= 0;
9141  if (bit_len)
9142  {
9143  bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9144  bits<<= (bytes_in_rec * 8);
9145  }
9146 
9147  switch (bytes_in_rec) {
9148  case 0: return bits;
9149  case 1: return bits | (ulonglong) ptr[0];
9150  case 2: return bits | mi_uint2korr(ptr);
9151  case 3: return bits | mi_uint3korr(ptr);
9152  case 4: return bits | mi_uint4korr(ptr);
9153  case 5: return bits | mi_uint5korr(ptr);
9154  case 6: return bits | mi_uint6korr(ptr);
9155  case 7: return bits | mi_uint7korr(ptr);
9156  default: return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
9157  }
9158 }
9159 
9160 
9161 String *Field_bit::val_str(String *val_buffer,
9162  String *val_ptr __attribute__((unused)))
9163 {
9164  ASSERT_COLUMN_MARKED_FOR_READ;
9165  char buff[sizeof(longlong)];
9166  uint length= min<uint>(pack_length(), sizeof(longlong));
9167  ulonglong bits= val_int();
9168  mi_int8store(buff,bits);
9169 
9170  val_buffer->alloc(length);
9171  memcpy((char *) val_buffer->ptr(), buff+8-length, length);
9172  val_buffer->length(length);
9173  val_buffer->set_charset(&my_charset_bin);
9174  return val_buffer;
9175 }
9176 
9177 
9178 my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value)
9179 {
9180  ASSERT_COLUMN_MARKED_FOR_READ;
9181  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 1, deciaml_value);
9182  return deciaml_value;
9183 }
9184 
9185 
9186 /*
9187  Compare two bit fields using pointers within the record.
9188  SYNOPSIS
9189  cmp_max()
9190  a Pointer to field->ptr in first record
9191  b Pointer to field->ptr in second record
9192  max_len Maximum length used in index
9193  DESCRIPTION
9194  This method is used from key_rec_cmp used by merge sorts used
9195  by partitioned index read and later other similar places.
9196  The a and b pointer must be pointers to the field in a record
9197  (not the table->record[0] necessarily)
9198 */
9199 int Field_bit::cmp_max(const uchar *a, const uchar *b, uint max_len)
9200 {
9201  my_ptrdiff_t a_diff= a - ptr;
9202  my_ptrdiff_t b_diff= b - ptr;
9203  if (bit_len)
9204  {
9205  int flag;
9206  uchar bits_a= get_rec_bits(bit_ptr+a_diff, bit_ofs, bit_len);
9207  uchar bits_b= get_rec_bits(bit_ptr+b_diff, bit_ofs, bit_len);
9208  if ((flag= (int) (bits_a - bits_b)))
9209  return flag;
9210  }
9211  return memcmp(a, b, field_length);
9212 }
9213 
9214 
9215 int Field_bit::key_cmp(const uchar *str, uint length)
9216 {
9217  if (bit_len)
9218  {
9219  int flag;
9220  uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9221  if ((flag= (int) (bits - *str)))
9222  return flag;
9223  str++;
9224  length--;
9225  }
9226  return memcmp(ptr, str, length);
9227 }
9228 
9229 
9230 int Field_bit::cmp_offset(uint row_offset)
9231 {
9232  if (bit_len)
9233  {
9234  int flag;
9235  uchar bits_a= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9236  uchar bits_b= get_rec_bits(bit_ptr + row_offset, bit_ofs, bit_len);
9237  if ((flag= (int) (bits_a - bits_b)))
9238  return flag;
9239  }
9240  return memcmp(ptr, ptr + row_offset, bytes_in_rec);
9241 }
9242 
9243 
9244 uint Field_bit::get_key_image(uchar *buff, uint length, imagetype type_arg)
9245 {
9246  if (bit_len)
9247  {
9248  uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9249  *buff++= bits;
9250  length--;
9251  }
9252  uint data_length = min(length, bytes_in_rec);
9253  memcpy(buff, ptr, data_length);
9254  return data_length + 1;
9255 }
9256 
9257 
9269 int Field_bit::do_save_field_metadata(uchar *metadata_ptr)
9270 {
9271  DBUG_ENTER("Field_bit::do_save_field_metadata");
9272  DBUG_PRINT("debug", ("bit_len: %d, bytes_in_rec: %d",
9273  bit_len, bytes_in_rec));
9274  /*
9275  Since this class and Field_bit_as_char have different ideas of
9276  what should be stored here, we compute the values of the metadata
9277  explicitly using the field_length.
9278  */
9279  metadata_ptr[0]= field_length % 8;
9280  metadata_ptr[1]= field_length / 8;
9281  DBUG_RETURN(2);
9282 }
9283 
9284 
9297 uint Field_bit::pack_length_from_metadata(uint field_metadata)
9298 {
9299  uint const from_len= (field_metadata >> 8U) & 0x00ff;
9300  uint const from_bit_len= field_metadata & 0x00ff;
9301  uint const source_size= from_len + ((from_bit_len > 0) ? 1 : 0);
9302  return (source_size);
9303 }
9304 
9320 bool
9322  Relay_log_info * __attribute__((unused)),
9323  uint16 mflags,
9324  int *order_var)
9325 {
9326  DBUG_ENTER("Field_bit::compatible_field_size");
9327  DBUG_ASSERT((field_metadata >> 16) == 0);
9328  uint from_bit_len= 8 * (field_metadata >> 8) + (field_metadata & 0xff);
9329  uint to_bit_len= max_display_length();
9330  DBUG_PRINT("debug", ("from_bit_len: %u, to_bit_len: %u",
9331  from_bit_len, to_bit_len));
9332  /*
9333  If the bit length exact flag is clear, we are dealing with an old
9334  master, so we allow some less strict behaviour if replicating by
9335  moving both bit lengths to an even multiple of 8.
9336 
9337  We do this by computing the number of bytes to store the field
9338  instead, and then compare the result.
9339  */
9340  if (!(mflags & Table_map_log_event::TM_BIT_LEN_EXACT_F)) {
9341  from_bit_len= (from_bit_len + 7) / 8;
9342  to_bit_len= (to_bit_len + 7) / 8;
9343  }
9344 
9345  *order_var= compare(from_bit_len, to_bit_len);
9346  DBUG_RETURN(TRUE);
9347 }
9348 
9349 
9350 
9351 void Field_bit::sql_type(String &res) const
9352 {
9353  const CHARSET_INFO *cs= res.charset();
9354  ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
9355  "bit(%d)", (int) field_length);
9356  res.length((uint) length);
9357 }
9358 
9359 
9360 uchar *
9361 Field_bit::pack(uchar *to, const uchar *from, uint max_length,
9362  bool low_byte_first __attribute__((unused)))
9363 {
9364  DBUG_ASSERT(max_length > 0);
9365  uint length;
9366  if (bit_len > 0)
9367  {
9368  /*
9369  We have the following:
9370 
9371  ptr Points into a field in record R1
9372  from Points to a field in a record R2
9373  bit_ptr Points to the byte (in the null bytes) that holds the
9374  odd bits of R1
9375  from_bitp Points to the byte that holds the odd bits of R2
9376 
9377  We have the following:
9378 
9379  ptr - bit_ptr = from - from_bitp
9380 
9381  We want to isolate 'from_bitp', so this gives:
9382 
9383  ptr - bit_ptr - from = - from_bitp
9384  - ptr + bit_ptr + from = from_bitp
9385  bit_ptr + from - ptr = from_bitp
9386  */
9387  uchar bits= get_rec_bits(bit_ptr + (from - ptr), bit_ofs, bit_len);
9388  *to++= bits;
9389  }
9390  length= min(bytes_in_rec, max_length - (bit_len > 0));
9391  memcpy(to, from, length);
9392  return to + length;
9393 }
9394 
9395 
9408 const uchar *
9409 Field_bit::unpack(uchar *to, const uchar *from, uint param_data,
9410  bool low_byte_first __attribute__((unused)))
9411 {
9412  DBUG_ENTER("Field_bit::unpack");
9413  DBUG_PRINT("enter", ("to: %p, from: %p, param_data: 0x%x",
9414  to, from, param_data));
9415  DBUG_PRINT("debug", ("bit_ptr: %p, bit_len: %u, bit_ofs: %u",
9416  bit_ptr, bit_len, bit_ofs));
9417  uint const from_len= (param_data >> 8U) & 0x00ff;
9418  uint const from_bit_len= param_data & 0x00ff;
9419  DBUG_PRINT("debug", ("from_len: %u, from_bit_len: %u",
9420  from_len, from_bit_len));
9421  /*
9422  If the parameter data is zero (i.e., undefined), or if the master
9423  and slave have the same sizes, then use the old unpack() method.
9424  */
9425  if (param_data == 0 ||
9426  ((from_bit_len == bit_len) && (from_len == bytes_in_rec)))
9427  {
9428  if (bit_len > 0)
9429  {
9430  /*
9431  set_rec_bits is a macro, don't put the post-increment in the
9432  argument since that might cause strange side-effects.
9433 
9434  For the choice of the second argument, see the explanation for
9435  Field_bit::pack().
9436  */
9437  set_rec_bits(*from, bit_ptr + (to - ptr), bit_ofs, bit_len);
9438  from++;
9439  }
9440  memcpy(to, from, bytes_in_rec);
9441  DBUG_RETURN(from + bytes_in_rec);
9442  }
9443 
9444  /*
9445  We are converting a smaller bit field to a larger one here.
9446  To do that, we first need to construct a raw value for the original
9447  bit value stored in the from buffer. Then that needs to be converted
9448  to the larger field then sent to store() for writing to the field.
9449  Lastly the odd bits need to be masked out if the bytes_in_rec > 0.
9450  Otherwise stray bits can cause spurious values.
9451  */
9452  uint new_len= (field_length + 7) / 8;
9453  char *value= (char *)my_alloca(new_len);
9454  memset(value, 0, new_len);
9455  uint len= from_len + ((from_bit_len > 0) ? 1 : 0);
9456  memcpy(value + (new_len - len), from, len);
9457  /*
9458  Mask out the unused bits in the partial byte.
9459  TODO: Add code to the master to always mask these bits and remove
9460  the following.
9461  */
9462  if ((from_bit_len > 0) && (from_len > 0))
9463  value[new_len - len]= value[new_len - len] & ((1U << from_bit_len) - 1);
9464  bitmap_set_bit(table->write_set,field_index);
9465  store(value, new_len, system_charset_info);
9466  my_afree(value);
9467  DBUG_RETURN(from + len);
9468 }
9469 
9470 
9471 void Field_bit::set_default()
9472 {
9473  if (bit_len > 0)
9474  {
9475  my_ptrdiff_t const offset= table->s->default_values - table->record[0];
9476  uchar bits= get_rec_bits(bit_ptr + offset, bit_ofs, bit_len);
9477  set_rec_bits(bits, bit_ptr, bit_ofs, bit_len);
9478  }
9479  Field::set_default();
9480 }
9481 
9482 /*
9483  Bit field support for non-MyISAM tables.
9484 */
9485 
9486 Field_bit_as_char::Field_bit_as_char(uchar *ptr_arg, uint32 len_arg,
9487  uchar *null_ptr_arg, uchar null_bit_arg,
9488  enum utype unireg_check_arg,
9489  const char *field_name_arg)
9490  :Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, 0, 0,
9491  unireg_check_arg, field_name_arg)
9492 {
9493  flags|= UNSIGNED_FLAG;
9494  bit_len= 0;
9495  bytes_in_rec= (len_arg + 7) / 8;
9496 }
9497 
9498 
9499 type_conversion_status Field_bit_as_char::store(const char *from, uint length,
9500  const CHARSET_INFO *cs)
9501 {
9502  ASSERT_COLUMN_MARKED_FOR_WRITE;
9503  int delta;
9504  uchar bits= (uchar) (field_length & 7);
9505 
9506  for (; length && !*from; from++, length--) ; // skip left 0's
9507  delta= bytes_in_rec - length;
9508 
9509  if (delta < 0 ||
9510  (delta == 0 && bits && (uint) (uchar) *from >= (uint) (1 << bits)))
9511  {
9512  memset(ptr, 0xff, bytes_in_rec);
9513  if (bits)
9514  *ptr&= ((1 << bits) - 1); /* set first uchar */
9515  if (table->in_use->really_abort_on_warning())
9516  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
9517  else
9518  set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
9519  return TYPE_WARN_OUT_OF_RANGE;
9520  }
9521  memset(ptr, 0, delta);
9522  memcpy(ptr + delta, from, length);
9523  return TYPE_OK;
9524 }
9525 
9526 
9527 void Field_bit_as_char::sql_type(String &res) const
9528 {
9529  const CHARSET_INFO *cs= res.charset();
9530  ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
9531  "bit(%d)", (int) field_length);
9532  res.length((uint) length);
9533 }
9534 
9535 
9536 /*****************************************************************************
9537  Handling of field and Create_field
9538 *****************************************************************************/
9539 
9545 {
9546  switch (sql_type) {
9547  case MYSQL_TYPE_TINY_BLOB:
9548  case MYSQL_TYPE_MEDIUM_BLOB:
9549  case MYSQL_TYPE_LONG_BLOB:
9550  case MYSQL_TYPE_BLOB:
9551  case MYSQL_TYPE_GEOMETRY:
9552  case MYSQL_TYPE_VAR_STRING:
9553  case MYSQL_TYPE_STRING:
9554  case MYSQL_TYPE_VARCHAR:
9555  length*= charset->mbmaxlen;
9556  key_length= length;
9557  pack_length= calc_pack_length(sql_type, length);
9558  break;
9559  case MYSQL_TYPE_ENUM:
9560  case MYSQL_TYPE_SET:
9561  /* Pack_length already calculated in sql_parse.cc */
9562  length*= charset->mbmaxlen;
9563  key_length= pack_length;
9564  break;
9565  case MYSQL_TYPE_BIT:
9566  if (f_bit_as_char(pack_flag))
9567  {
9568  key_length= pack_length= ((length + 7) & ~7) / 8;
9569  }
9570  else
9571  {
9572  pack_length= length / 8;
9573  /* We need one extra byte to store the bits we save among the null bits */
9574  key_length= pack_length + test(length & 7);
9575  }
9576  break;
9577  case MYSQL_TYPE_NEWDECIMAL:
9578  key_length= pack_length=
9579  my_decimal_get_binary_size(my_decimal_length_to_precision(length,
9580  decimals,
9581  flags &
9582  UNSIGNED_FLAG),
9583  decimals);
9584  break;
9585  default:
9586  key_length= pack_length= calc_pack_length(sql_type, length);
9587  break;
9588  }
9589 }
9590 
9591 
9595 void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
9596  uint32 length_arg, uint32 decimals_arg,
9597  bool maybe_null, bool is_unsigned,
9598  uint pack_length_arg)
9599 {
9600  DBUG_ENTER("Create_field::init_for_tmp_table");
9601 
9602  field_name= "";
9603  sql_type= sql_type_arg;
9604  char_length= length= length_arg;;
9605  unireg_check= Field::NONE;
9606  interval= 0;
9607  charset= &my_charset_bin;
9608  geom_type= Field::GEOM_GEOMETRY;
9609 
9610  DBUG_PRINT("enter", ("sql_type: %d, length: %u, pack_length: %u",
9611  sql_type_arg, length_arg, pack_length_arg));
9612 
9613  /*
9614  These pack flags are crafted to get it correctly through the
9615  branches of make_field().
9616  */
9617  switch (sql_type_arg)
9618  {
9619  case MYSQL_TYPE_VARCHAR:
9620  case MYSQL_TYPE_VAR_STRING:
9621  case MYSQL_TYPE_STRING:
9622  case MYSQL_TYPE_SET:
9623  pack_flag= 0;
9624  break;
9625 
9626  case MYSQL_TYPE_GEOMETRY:
9627  pack_flag= FIELDFLAG_GEOM;
9628  break;
9629 
9630  case MYSQL_TYPE_ENUM:
9631  pack_flag= FIELDFLAG_INTERVAL;
9632  break;
9633 
9634  case MYSQL_TYPE_NEWDECIMAL:
9635  DBUG_ASSERT(decimals_arg <= DECIMAL_MAX_SCALE);
9636  case MYSQL_TYPE_DECIMAL:
9637  case MYSQL_TYPE_FLOAT:
9638  case MYSQL_TYPE_DOUBLE:
9639  pack_flag= FIELDFLAG_NUMBER |
9640  (decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT;
9641  break;
9642 
9643  case MYSQL_TYPE_TINY_BLOB:
9644  case MYSQL_TYPE_MEDIUM_BLOB:
9645  case MYSQL_TYPE_LONG_BLOB:
9646  case MYSQL_TYPE_BLOB:
9647  pack_flag= FIELDFLAG_BLOB;
9648  break;
9649 
9650  case MYSQL_TYPE_BIT:
9651  pack_flag= FIELDFLAG_NUMBER | FIELDFLAG_TREAT_BIT_AS_CHAR;
9652  break;
9653 
9654  default:
9655  pack_flag= FIELDFLAG_NUMBER;
9656  break;
9657  }
9658 
9659  /*
9660  Set the pack flag correctly for the blob-like types. This sets the
9661  packtype to something that make_field can use. If the pack type is
9662  not set correctly, the packlength will be reeeeally wierd (like
9663  129 or so).
9664  */
9665  switch (sql_type_arg)
9666  {
9667  case MYSQL_TYPE_ENUM:
9668  case MYSQL_TYPE_SET:
9669  case MYSQL_TYPE_TINY_BLOB:
9670  case MYSQL_TYPE_MEDIUM_BLOB:
9671  case MYSQL_TYPE_LONG_BLOB:
9672  case MYSQL_TYPE_BLOB:
9673  case MYSQL_TYPE_GEOMETRY:
9674  // If you are going to use the above types, you have to pass a
9675  // pack_length as parameter. Assert that is really done.
9676  DBUG_ASSERT(pack_length_arg != ~0U);
9677  pack_flag|= pack_length_to_packflag(pack_length_arg);
9678  break;
9679  default:
9680  /* Nothing */
9681  break;
9682  }
9683 
9684  pack_flag|=
9685  (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
9686  (is_unsigned ? 0 : FIELDFLAG_DECIMAL);
9687 
9688  DBUG_PRINT("debug", ("pack_flag: %s%s%s%s%s%s, pack_type: %d",
9689  FLAGSTR(pack_flag, FIELDFLAG_BINARY),
9690  FLAGSTR(pack_flag, FIELDFLAG_NUMBER),
9691  FLAGSTR(pack_flag, FIELDFLAG_INTERVAL),
9692  FLAGSTR(pack_flag, FIELDFLAG_GEOM),
9693  FLAGSTR(pack_flag, FIELDFLAG_BLOB),
9694  FLAGSTR(pack_flag, FIELDFLAG_DECIMAL),
9695  f_packtype(pack_flag)));
9696  DBUG_VOID_RETURN;
9697 }
9698 
9699 
9725 bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
9726  char *fld_length, char *fld_decimals,
9727  uint fld_type_modifier, Item *fld_default_value,
9728  Item *fld_on_update_value, LEX_STRING *fld_comment,
9729  char *fld_change, List<String> *fld_interval_list,
9730  const CHARSET_INFO *fld_charset, uint fld_geom_type)
9731 {
9732  uint sign_len, allowed_type_modifier= 0;
9733  ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
9734 
9735  DBUG_ENTER("Create_field::init()");
9736 
9737  field= 0;
9738  field_name= fld_name;
9739  flags= fld_type_modifier;
9740  charset= fld_charset;
9741 
9742  const bool on_update_is_function=
9743  (fld_on_update_value != NULL &&
9744  fld_on_update_value->type() == Item::FUNC_ITEM);
9745 
9746  if (fld_default_value != NULL && fld_default_value->type() == Item::FUNC_ITEM)
9747  {
9748  // We have a function default for insertions.
9749  def= NULL;
9750  unireg_check= on_update_is_function ?
9751  Field::TIMESTAMP_DNUN_FIELD : // for insertions and for updates.
9752  Field::TIMESTAMP_DN_FIELD; // only for insertions.
9753  }
9754  else
9755  {
9756  // No function default for insertions. Either NULL or a constant.
9757  def= fld_default_value;
9758  if (on_update_is_function)
9759  // We have a function default for updates only.
9760  unireg_check= Field::TIMESTAMP_UN_FIELD;
9761  else
9762  // No function defaults.
9763  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG) != 0 ?
9764  Field::NEXT_NUMBER : // Automatic increment.
9765  Field::NONE;
9766  }
9767 
9768  decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
9769  if (is_temporal_real_type(fld_type))
9770  {
9771  flags|= BINARY_FLAG;
9772  charset= &my_charset_numeric;
9773  if (decimals > DATETIME_MAX_DECIMALS)
9774  {
9775  my_error(ER_TOO_BIG_PRECISION, MYF(0),
9776  decimals, fld_name, DATETIME_MAX_DECIMALS);
9777  DBUG_RETURN(TRUE);
9778  }
9779  }
9780  else if (decimals >= NOT_FIXED_DEC)
9781  {
9782  my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
9783  static_cast<ulong>(NOT_FIXED_DEC - 1));
9784  DBUG_RETURN(TRUE);
9785  }
9786 
9787  sql_type= fld_type;
9788  length= 0;
9789  change= fld_change;
9790  interval= 0;
9791  pack_length= key_length= 0;
9792  geom_type= (Field::geometry_type) fld_geom_type;
9793  interval_list.empty();
9794 
9795  comment= *fld_comment;
9796  /*
9797  Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
9798  it is NOT NULL and not an AUTO_INCREMENT field.
9799  */
9800  if (!fld_default_value &&
9801  (fld_type_modifier & NOT_NULL_FLAG) &&
9802  !(fld_type_modifier & AUTO_INCREMENT_FLAG))
9803  {
9804  /*
9805  TIMESTAMP columns get implicit DEFAULT value when
9806  explicit_defaults_for_timestamp is not set.
9807  */
9808  if (thd->variables.explicit_defaults_for_timestamp ||
9809  !is_timestamp_type(fld_type))
9810  {
9811  flags|= NO_DEFAULT_VALUE_FLAG;
9812  }
9813  }
9814 
9815  if (fld_length != NULL)
9816  {
9817  errno= 0;
9818  length= strtoul(fld_length, NULL, 10);
9819  if ((errno != 0) || (length > MAX_FIELD_BLOBLENGTH))
9820  {
9821  my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), fld_name, MAX_FIELD_BLOBLENGTH);
9822  DBUG_RETURN(TRUE);
9823  }
9824 
9825  if (length == 0)
9826  fld_length= NULL; /* purecov: inspected */
9827  }
9828 
9829  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
9830 
9831  switch (fld_type) {
9832  case MYSQL_TYPE_TINY:
9833  if (!fld_length)
9834  length= MAX_TINYINT_WIDTH+sign_len;
9835  allowed_type_modifier= AUTO_INCREMENT_FLAG;
9836  break;
9837  case MYSQL_TYPE_SHORT:
9838  if (!fld_length)
9839  length= MAX_SMALLINT_WIDTH+sign_len;
9840  allowed_type_modifier= AUTO_INCREMENT_FLAG;
9841  break;
9842  case MYSQL_TYPE_INT24:
9843  if (!fld_length)
9844  length= MAX_MEDIUMINT_WIDTH+sign_len;
9845  allowed_type_modifier= AUTO_INCREMENT_FLAG;
9846  break;
9847  case MYSQL_TYPE_LONG:
9848  if (!fld_length)
9849  length= MAX_INT_WIDTH+sign_len;
9850  allowed_type_modifier= AUTO_INCREMENT_FLAG;
9851  break;
9852  case MYSQL_TYPE_LONGLONG:
9853  if (!fld_length)
9854  length= MAX_BIGINT_WIDTH;
9855  allowed_type_modifier= AUTO_INCREMENT_FLAG;
9856  break;
9857  case MYSQL_TYPE_NULL:
9858  break;
9859  case MYSQL_TYPE_NEWDECIMAL:
9860  my_decimal_trim(&length, &decimals);
9861  if (length > DECIMAL_MAX_PRECISION)
9862  {
9863  my_error(ER_TOO_BIG_PRECISION, MYF(0), static_cast<int>(length),
9864  fld_name, static_cast<ulong>(DECIMAL_MAX_PRECISION));
9865  DBUG_RETURN(TRUE);
9866  }
9867  if (length < decimals)
9868  {
9869  my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
9870  DBUG_RETURN(TRUE);
9871  }
9872  length=
9873  my_decimal_precision_to_length(length, decimals,
9874  fld_type_modifier & UNSIGNED_FLAG);
9875  pack_length=
9876  my_decimal_get_binary_size(length, decimals);
9877  break;
9878  case MYSQL_TYPE_VARCHAR:
9879  /*
9880  Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
9881  if they don't have a default value
9882  */
9883  max_field_charlength= MAX_FIELD_VARCHARLENGTH;
9884  break;
9885  case MYSQL_TYPE_STRING:
9886  break;
9887  case MYSQL_TYPE_BLOB:
9888  case MYSQL_TYPE_TINY_BLOB:
9889  case MYSQL_TYPE_LONG_BLOB:
9890  case MYSQL_TYPE_MEDIUM_BLOB:
9891  case MYSQL_TYPE_GEOMETRY:
9892  if (fld_default_value)
9893  {
9894  /* Allow empty as default value. */
9895  String str,*res;
9896  res= fld_default_value->val_str(&str);
9897  /*
9898  A default other than '' is always an error, and any non-NULL
9899  specified default is an error in strict mode.
9900  */
9901  if (res->length() || thd->is_strict_mode())
9902  {
9903  my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
9904  fld_name); /* purecov: inspected */
9905  DBUG_RETURN(TRUE);
9906  }
9907  else
9908  {
9909  /*
9910  Otherwise a default of '' is just a warning.
9911  */
9912  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
9913  ER_BLOB_CANT_HAVE_DEFAULT,
9914  ER(ER_BLOB_CANT_HAVE_DEFAULT),
9915  fld_name);
9916  }
9917  def= 0;
9918  }
9919  flags|= BLOB_FLAG;
9920  break;
9921  case MYSQL_TYPE_YEAR:
9922  if (!fld_length || length != 2)
9923  length= 4; /* Default length */
9924  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
9925  break;
9926  case MYSQL_TYPE_FLOAT:
9927  /* change FLOAT(precision) to FLOAT or DOUBLE */
9928  allowed_type_modifier= AUTO_INCREMENT_FLAG;
9929  if (fld_length && !fld_decimals)
9930  {
9931  uint tmp_length= length;
9932  if (tmp_length > PRECISION_FOR_DOUBLE)
9933  {
9934  my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
9935  DBUG_RETURN(TRUE);
9936  }
9937  else if (tmp_length > PRECISION_FOR_FLOAT)
9938  {
9939  sql_type= MYSQL_TYPE_DOUBLE;
9940  length= MAX_DOUBLE_STR_LENGTH;
9941  }
9942  else
9943  length= MAX_FLOAT_STR_LENGTH;
9944  decimals= NOT_FIXED_DEC;
9945  break;
9946  }
9947  if (!fld_length && !fld_decimals)
9948  {
9949  length= MAX_FLOAT_STR_LENGTH;
9950  decimals= NOT_FIXED_DEC;
9951  }
9952  if (length < decimals &&
9953  decimals != NOT_FIXED_DEC)
9954  {
9955  my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
9956  DBUG_RETURN(TRUE);
9957  }
9958  break;
9959  case MYSQL_TYPE_DOUBLE:
9960  allowed_type_modifier= AUTO_INCREMENT_FLAG;
9961  if (!fld_length && !fld_decimals)
9962  {
9963  length= DBL_DIG+7;
9964  decimals= NOT_FIXED_DEC;
9965  }
9966  if (length < decimals &&
9967  decimals != NOT_FIXED_DEC)
9968  {
9969  my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
9970  DBUG_RETURN(TRUE);
9971  }
9972  break;
9973  case MYSQL_TYPE_TIMESTAMP:
9974  /* Add flags for TIMESTAMP for 4.0 MYD and 4.0 InnoDB compatibility */
9975  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
9976  /* Fall through */
9977  case MYSQL_TYPE_TIMESTAMP2:
9978  if (fld_length == NULL)
9979  {
9980  length= MAX_DATETIME_WIDTH + (decimals ? (1 + decimals) : 0);
9981  }
9982  else if (length != MAX_DATETIME_WIDTH)
9983  {
9984  /*
9985  We support only even TIMESTAMP lengths less or equal than 14
9986  and 19 as length of 4.1 compatible representation. Silently
9987  shrink it to MAX_DATETIME_COMPRESSED_WIDTH.
9988  */
9989  DBUG_ASSERT(MAX_DATETIME_COMPRESSED_WIDTH < UINT_MAX);
9990  if (length != UINT_MAX) /* avoid overflow; is safe because of min() */
9991  length= ((length+1)/2)*2;
9992  length= min<ulong>(length, MAX_DATETIME_COMPRESSED_WIDTH);
9993  }
9994 
9995  /*
9996  Since we silently rewrite down to MAX_DATETIME_COMPRESSED_WIDTH bytes,
9997  the parser should not raise errors unless bizzarely large.
9998  */
9999  max_field_charlength= UINT_MAX;
10000 
10001  break;
10002  case MYSQL_TYPE_DATE:
10003  /* Old date type. */
10004  sql_type= MYSQL_TYPE_NEWDATE;
10005  /* fall trough */
10006  case MYSQL_TYPE_NEWDATE:
10007  length= MAX_DATE_WIDTH;
10008  break;
10009  case MYSQL_TYPE_TIME:
10010  case MYSQL_TYPE_TIME2:
10011  length= MAX_TIME_WIDTH + (decimals ? (1 + decimals) : 0);
10012  break;
10013  case MYSQL_TYPE_DATETIME:
10014  case MYSQL_TYPE_DATETIME2:
10015  length= MAX_DATETIME_WIDTH + (decimals ? (1 + decimals) : 0);
10016  break;
10017  case MYSQL_TYPE_SET:
10018  {
10019  pack_length= get_set_pack_length(fld_interval_list->elements);
10020 
10021  List_iterator<String> it(*fld_interval_list);
10022  String *tmp;
10023  while ((tmp= it++))
10024  interval_list.push_back(tmp);
10025  /*
10026  Set fake length to 1 to pass the below conditions.
10027  Real length will be set in mysql_prepare_table()
10028  when we know the character set of the column
10029  */
10030  length= 1;
10031  break;
10032  }
10033  case MYSQL_TYPE_ENUM:
10034  {
10035  /* Should be safe. */
10036  pack_length= get_enum_pack_length(fld_interval_list->elements);
10037 
10038  List_iterator<String> it(*fld_interval_list);
10039  String *tmp;
10040  while ((tmp= it++))
10041  interval_list.push_back(tmp);
10042  length= 1; /* See comment for MYSQL_TYPE_SET above. */
10043  break;
10044  }
10045  case MYSQL_TYPE_VAR_STRING:
10046  DBUG_ASSERT(0); /* Impossible. */
10047  break;
10048  case MYSQL_TYPE_BIT:
10049  {
10050  if (!fld_length)
10051  length= 1;
10052  if (length > MAX_BIT_FIELD_LENGTH)
10053  {
10054  my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), fld_name,
10055  static_cast<ulong>(MAX_BIT_FIELD_LENGTH));
10056  DBUG_RETURN(TRUE);
10057  }
10058  pack_length= (length + 7) / 8;
10059  break;
10060  }
10061  case MYSQL_TYPE_DECIMAL:
10062  DBUG_ASSERT(0); /* Was obsolete */
10063  }
10064  /* Remember the value of length */
10065  char_length= length;
10066 
10067  if (!(flags & BLOB_FLAG) &&
10068  ((length > max_field_charlength && fld_type != MYSQL_TYPE_SET &&
10069  fld_type != MYSQL_TYPE_ENUM &&
10070  (fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
10071  ((length == 0) &&
10072  fld_type != MYSQL_TYPE_STRING &&
10073  fld_type != MYSQL_TYPE_VARCHAR && fld_type != MYSQL_TYPE_GEOMETRY)))
10074  {
10075  my_error((fld_type == MYSQL_TYPE_VAR_STRING ||
10076  fld_type == MYSQL_TYPE_VARCHAR ||
10077  fld_type == MYSQL_TYPE_STRING) ? ER_TOO_BIG_FIELDLENGTH :
10078  ER_TOO_BIG_DISPLAYWIDTH,
10079  MYF(0),
10080  fld_name, max_field_charlength); /* purecov: inspected */
10081  DBUG_RETURN(TRUE);
10082  }
10083  fld_type_modifier&= AUTO_INCREMENT_FLAG;
10084  if ((~allowed_type_modifier) & fld_type_modifier)
10085  {
10086  my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
10087  DBUG_RETURN(TRUE);
10088  }
10089 
10090  DBUG_RETURN(FALSE); /* success */
10091 }
10092 
10093 
10094 enum_field_types get_blob_type_from_length(ulong length)
10095 {
10096  enum_field_types type;
10097  if (length < 256)
10098  type= MYSQL_TYPE_TINY_BLOB;
10099  else if (length < 65536)
10100  type= MYSQL_TYPE_BLOB;
10101  else if (length < 256L*256L*256L)
10102  type= MYSQL_TYPE_MEDIUM_BLOB;
10103  else
10104  type= MYSQL_TYPE_LONG_BLOB;
10105  return type;
10106 }
10107 
10108 
10109 /*
10110  Make a field from the .frm file info
10111 */
10112 
10113 uint32 calc_pack_length(enum_field_types type,uint32 length)
10114 {
10115  switch (type) {
10116  case MYSQL_TYPE_VAR_STRING:
10117  case MYSQL_TYPE_STRING:
10118  case MYSQL_TYPE_DECIMAL: return (length);
10119  case MYSQL_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
10120  case MYSQL_TYPE_YEAR:
10121  case MYSQL_TYPE_TINY : return 1;
10122  case MYSQL_TYPE_SHORT : return 2;
10123  case MYSQL_TYPE_INT24:
10124  case MYSQL_TYPE_NEWDATE: return 3;
10125  case MYSQL_TYPE_TIME: return 3;
10126  case MYSQL_TYPE_TIME2:
10127  return length > MAX_TIME_WIDTH ?
10128  my_time_binary_length(length - MAX_TIME_WIDTH - 1) : 3;
10129  case MYSQL_TYPE_TIMESTAMP: return 4;
10130  case MYSQL_TYPE_TIMESTAMP2:
10131  return length > MAX_DATETIME_WIDTH ?
10132  my_timestamp_binary_length(length - MAX_DATETIME_WIDTH - 1) : 4;
10133  case MYSQL_TYPE_DATE:
10134  case MYSQL_TYPE_LONG : return 4;
10135  case MYSQL_TYPE_FLOAT : return sizeof(float);
10136  case MYSQL_TYPE_DOUBLE: return sizeof(double);
10137  case MYSQL_TYPE_DATETIME: return 8;
10138  case MYSQL_TYPE_DATETIME2:
10139  return length > MAX_DATETIME_WIDTH ?
10140  my_datetime_binary_length(length - MAX_DATETIME_WIDTH - 1) : 5;
10141  case MYSQL_TYPE_LONGLONG: return 8; /* Don't crash if no longlong */
10142  case MYSQL_TYPE_NULL : return 0;
10143  case MYSQL_TYPE_TINY_BLOB: return 1+portable_sizeof_char_ptr;
10144  case MYSQL_TYPE_BLOB: return 2+portable_sizeof_char_ptr;
10145  case MYSQL_TYPE_MEDIUM_BLOB: return 3+portable_sizeof_char_ptr;
10146  case MYSQL_TYPE_LONG_BLOB: return 4+portable_sizeof_char_ptr;
10147  case MYSQL_TYPE_GEOMETRY: return 4+portable_sizeof_char_ptr;
10148  case MYSQL_TYPE_SET:
10149  case MYSQL_TYPE_ENUM:
10150  case MYSQL_TYPE_NEWDECIMAL:
10151  abort(); return 0; // This shouldn't happen
10152  case MYSQL_TYPE_BIT: return length / 8;
10153  default:
10154  return 0;
10155  }
10156 }
10157 
10158 
10159 uint pack_length_to_packflag(uint type)
10160 {
10161  switch (type) {
10162  case 1: return f_settype((uint) MYSQL_TYPE_TINY);
10163  case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
10164  case 3: return f_settype((uint) MYSQL_TYPE_INT24);
10165  case 4: return f_settype((uint) MYSQL_TYPE_LONG);
10166  case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
10167  }
10168  return 0; // This shouldn't happen
10169 }
10170 
10171 
10172 Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
10173  uchar *null_pos, uchar null_bit,
10174  uint pack_flag,
10175  enum_field_types field_type,
10176  const CHARSET_INFO *field_charset,
10177  Field::geometry_type geom_type,
10178  Field::utype unireg_check,
10179  TYPELIB *interval,
10180  const char *field_name)
10181 {
10182  uchar *UNINIT_VAR(bit_ptr);
10183  uchar UNINIT_VAR(bit_offset);
10184  if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
10185  {
10186  bit_ptr= null_pos;
10187  bit_offset= null_bit;
10188  if (f_maybe_null(pack_flag)) // if null field
10189  {
10190  bit_ptr+= (null_bit == 7); // shift bit_ptr and bit_offset
10191  bit_offset= (bit_offset + 1) & 7;
10192  }
10193  }
10194 
10195  if (!f_maybe_null(pack_flag))
10196  {
10197  null_pos=0;
10198  null_bit=0;
10199  }
10200  else
10201  {
10202  null_bit= ((uchar) 1) << null_bit;
10203  }
10204 
10205  if (is_temporal_real_type(field_type))
10206  field_charset= &my_charset_numeric;
10207 
10208  DBUG_PRINT("debug", ("field_type: %d, field_length: %u, interval: %p, pack_flag: %s%s%s%s%s",
10209  field_type, field_length, interval,
10210  FLAGSTR(pack_flag, FIELDFLAG_BINARY),
10211  FLAGSTR(pack_flag, FIELDFLAG_INTERVAL),
10212  FLAGSTR(pack_flag, FIELDFLAG_NUMBER),
10213  FLAGSTR(pack_flag, FIELDFLAG_PACK),
10214  FLAGSTR(pack_flag, FIELDFLAG_BLOB)));
10215 
10216  if (f_is_alpha(pack_flag))
10217  {
10218  if (!f_is_packed(pack_flag))
10219  {
10220  if (field_type == MYSQL_TYPE_STRING ||
10221  field_type == MYSQL_TYPE_DECIMAL || // 3.23 or 4.0 string
10222  field_type == MYSQL_TYPE_VAR_STRING)
10223  return new Field_string(ptr,field_length,null_pos,null_bit,
10224  unireg_check, field_name,
10225  field_charset);
10226  if (field_type == MYSQL_TYPE_VARCHAR)
10227  return new Field_varstring(ptr,field_length,
10228  HA_VARCHAR_PACKLENGTH(field_length),
10229  null_pos,null_bit,
10230  unireg_check, field_name,
10231  share,
10232  field_charset);
10233  return 0; // Error
10234  }
10235 
10236  uint pack_length=calc_pack_length((enum_field_types)
10237  f_packtype(pack_flag),
10238  field_length);
10239 
10240 #ifdef HAVE_SPATIAL
10241  if (f_is_geom(pack_flag))
10242  return new Field_geom(ptr,null_pos,null_bit,
10243  unireg_check, field_name, share,
10244  pack_length, geom_type);
10245 #endif
10246  if (f_is_blob(pack_flag))
10247  return new Field_blob(ptr,null_pos,null_bit,
10248  unireg_check, field_name, share,
10249  pack_length, field_charset);
10250  if (interval)
10251  {
10252  if (f_is_enum(pack_flag))
10253  return new Field_enum(ptr,field_length,null_pos,null_bit,
10254  unireg_check, field_name,
10255  pack_length, interval, field_charset);
10256  else
10257  return new Field_set(ptr,field_length,null_pos,null_bit,
10258  unireg_check, field_name,
10259  pack_length, interval, field_charset);
10260  }
10261  }
10262 
10263  switch (field_type) {
10264  case MYSQL_TYPE_DECIMAL:
10265  return new Field_decimal(ptr,field_length,null_pos,null_bit,
10266  unireg_check, field_name,
10267  f_decimals(pack_flag),
10268  f_is_zerofill(pack_flag) != 0,
10269  f_is_dec(pack_flag) == 0);
10270  case MYSQL_TYPE_NEWDECIMAL:
10271  return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
10272  unireg_check, field_name,
10273  f_decimals(pack_flag),
10274  f_is_zerofill(pack_flag) != 0,
10275  f_is_dec(pack_flag) == 0);
10276  case MYSQL_TYPE_FLOAT:
10277  return new Field_float(ptr,field_length,null_pos,null_bit,
10278  unireg_check, field_name,
10279  f_decimals(pack_flag),
10280  f_is_zerofill(pack_flag) != 0,
10281  f_is_dec(pack_flag)== 0);
10282  case MYSQL_TYPE_DOUBLE:
10283  return new Field_double(ptr,field_length,null_pos,null_bit,
10284  unireg_check, field_name,
10285  f_decimals(pack_flag),
10286  f_is_zerofill(pack_flag) != 0,
10287  f_is_dec(pack_flag)== 0);
10288  case MYSQL_TYPE_TINY:
10289  return new Field_tiny(ptr,field_length,null_pos,null_bit,
10290  unireg_check, field_name,
10291  f_is_zerofill(pack_flag) != 0,
10292  f_is_dec(pack_flag) == 0);
10293  case MYSQL_TYPE_SHORT:
10294  return new Field_short(ptr,field_length,null_pos,null_bit,
10295  unireg_check, field_name,
10296  f_is_zerofill(pack_flag) != 0,
10297  f_is_dec(pack_flag) == 0);
10298  case MYSQL_TYPE_INT24:
10299  return new Field_medium(ptr,field_length,null_pos,null_bit,
10300  unireg_check, field_name,
10301  f_is_zerofill(pack_flag) != 0,
10302  f_is_dec(pack_flag) == 0);
10303  case MYSQL_TYPE_LONG:
10304  return new Field_long(ptr,field_length,null_pos,null_bit,
10305  unireg_check, field_name,
10306  f_is_zerofill(pack_flag) != 0,
10307  f_is_dec(pack_flag) == 0);
10308  case MYSQL_TYPE_LONGLONG:
10309  return new Field_longlong(ptr,field_length,null_pos,null_bit,
10310  unireg_check, field_name,
10311  f_is_zerofill(pack_flag) != 0,
10312  f_is_dec(pack_flag) == 0);
10313  case MYSQL_TYPE_TIMESTAMP:
10314  return new Field_timestamp(ptr, field_length, null_pos, null_bit,
10315  unireg_check, field_name);
10316  case MYSQL_TYPE_TIMESTAMP2:
10317  return new Field_timestampf(ptr, null_pos, null_bit,
10318  unireg_check, field_name,
10319  field_length > MAX_DATETIME_WIDTH ?
10320  field_length - 1 - MAX_DATETIME_WIDTH : 0);
10321  case MYSQL_TYPE_YEAR:
10322  return new Field_year(ptr,field_length,null_pos,null_bit,
10323  unireg_check, field_name);
10324  case MYSQL_TYPE_NEWDATE:
10325  return new Field_newdate(ptr, null_pos, null_bit, unireg_check, field_name);
10326 
10327  case MYSQL_TYPE_TIME:
10328  return new Field_time(ptr, null_pos, null_bit,
10329  unireg_check, field_name);
10330  case MYSQL_TYPE_TIME2:
10331  return new Field_timef(ptr, null_pos, null_bit,
10332  unireg_check, field_name,
10333  (field_length > MAX_TIME_WIDTH) ?
10334  field_length - 1 - MAX_TIME_WIDTH : 0);
10335  case MYSQL_TYPE_DATETIME:
10336  return new Field_datetime(ptr, null_pos, null_bit,
10337  unireg_check, field_name);
10338  case MYSQL_TYPE_DATETIME2:
10339  return new Field_datetimef(ptr, null_pos, null_bit,
10340  unireg_check, field_name,
10341  (field_length > MAX_DATETIME_WIDTH) ?
10342  field_length - 1 - MAX_DATETIME_WIDTH : 0);
10343  case MYSQL_TYPE_NULL:
10344  return new Field_null(ptr, field_length, unireg_check, field_name,
10345  field_charset);
10346  case MYSQL_TYPE_BIT:
10347  return f_bit_as_char(pack_flag) ?
10348  new Field_bit_as_char(ptr, field_length, null_pos, null_bit,
10349  unireg_check, field_name) :
10350  new Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
10351  bit_offset, unireg_check, field_name);
10352 
10353  default: // Impossible (Wrong version)
10354  break;
10355  }
10356  return 0;
10357 }
10358 
10359 
10379 Create_field::Create_field(Field *old_field,Field *orig_field) :
10380  field_name(old_field->field_name),
10381  change(old_field->field_name),
10382  comment(old_field->comment),
10383  sql_type(old_field->real_type()),
10384  length(old_field->field_length),
10385  decimals(old_field->decimals()),
10386  flags(old_field->flags),
10387  pack_length(old_field->pack_length()),
10388  key_length(old_field->key_length()),
10389  unireg_check(old_field->unireg_check),
10390  charset(old_field->charset()), // May be NULL ptr
10391  field(old_field)
10392 {
10393 
10394  switch (sql_type) {
10395  case MYSQL_TYPE_BLOB:
10396  switch (pack_length - portable_sizeof_char_ptr) {
10397  case 1: sql_type= MYSQL_TYPE_TINY_BLOB; break;
10398  case 2: sql_type= MYSQL_TYPE_BLOB; break;
10399  case 3: sql_type= MYSQL_TYPE_MEDIUM_BLOB; break;
10400  default: sql_type= MYSQL_TYPE_LONG_BLOB; break;
10401  }
10402  length/= charset->mbmaxlen;
10403  key_length/= charset->mbmaxlen;
10404  break;
10405  case MYSQL_TYPE_STRING:
10406  /* Change CHAR -> VARCHAR if dynamic record length */
10407  if (old_field->type() == MYSQL_TYPE_VAR_STRING)
10408  sql_type= MYSQL_TYPE_VARCHAR;
10409  /* fall through */
10410 
10411  case MYSQL_TYPE_ENUM:
10412  case MYSQL_TYPE_SET:
10413  case MYSQL_TYPE_VARCHAR:
10414  case MYSQL_TYPE_VAR_STRING:
10415  /* This is corrected in create_length_to_internal_length */
10416  length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
10417  break;
10418 #ifdef HAVE_SPATIAL
10419  case MYSQL_TYPE_GEOMETRY:
10420  geom_type= ((Field_geom*)old_field)->geom_type;
10421  break;
10422 #endif
10423  case MYSQL_TYPE_YEAR:
10424  if (length != 4)
10425  {
10426  push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
10427  ER_INVALID_YEAR_COLUMN_LENGTH,
10428  ER(ER_INVALID_YEAR_COLUMN_LENGTH),
10429  length);
10430  length= 4; // convert obsolete YEAR(2) to YEAR(4)
10431  }
10432  break;
10433  default:
10434  break;
10435  }
10436 
10437  if (flags & (ENUM_FLAG | SET_FLAG))
10438  interval= ((Field_enum*) old_field)->typelib;
10439  else
10440  interval=0;
10441  def=0;
10442  char_length= length;
10443 
10444  /*
10445  Copy the default (constant/function) from the column object orig_field, if
10446  supplied. We do this if all these conditions are met:
10447 
10448  - The column allows a default.
10449 
10450  - The column type is not a BLOB type.
10451 
10452  - The original column (old_field) was properly initialized with a record
10453  buffer pointer.
10454  */
10455  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
10456  old_field->ptr != NULL &&
10457  orig_field != NULL)
10458  {
10459  bool default_now= false;
10460  if (real_type_with_now_as_default(sql_type))
10461  {
10462  // The SQL type of the new field allows a function default:
10463  default_now= orig_field->has_insert_default_function();
10464  bool update_now= orig_field->has_update_default_function();
10465 
10466  if (default_now && update_now)
10467  unireg_check= Field::TIMESTAMP_DNUN_FIELD;
10468  else if (default_now)
10469  unireg_check= Field::TIMESTAMP_DN_FIELD;
10470  else if (update_now)
10471  unireg_check= Field::TIMESTAMP_UN_FIELD;
10472  }
10473  if (!default_now) // Give a constant default
10474  {
10475  char buff[MAX_FIELD_WIDTH];
10476  String tmp(buff,sizeof(buff), charset);
10477  my_ptrdiff_t diff;
10478 
10479  /* Get the value from default_values */
10480  diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
10481  orig_field->table->record[0]);
10482  orig_field->move_field_offset(diff); // Points now at default_values
10483  if (!orig_field->is_real_null())
10484  {
10485  char buff[MAX_FIELD_WIDTH], *pos;
10486  String tmp(buff, sizeof(buff), charset), *res;
10487  res= orig_field->val_str(&tmp);
10488  pos= (char*) sql_strmake(res->ptr(), res->length());
10489  def= new Item_string(pos, res->length(), charset);
10490  }
10491  orig_field->move_field_offset(-diff); // Back to record[0]
10492  }
10493  }
10494 }
10495 
10496 
10512 {
10513  switch (packlength)
10514  {
10515  case 1:
10516  return 255;
10517  case 2:
10518  return 65535;
10519  case 3:
10520  return 16777215;
10521  case 4:
10522  return (uint32) 4294967295U;
10523  default:
10524  DBUG_ASSERT(0); // we should never go here
10525  return 0;
10526  }
10527 }
10528 
10529 
10538 {
10539  switch (packlength)
10540  {
10541  case 1:
10542  return 255 * field_charset->mbmaxlen;
10543  case 2:
10544  return 65535 * field_charset->mbmaxlen;
10545  case 3:
10546  return 16777215 * field_charset->mbmaxlen;
10547  case 4:
10548  return (uint32) 4294967295U;
10549  default:
10550  DBUG_ASSERT(0); // we should never go here
10551  return 0;
10552  }
10553 }
10554 
10555 
10556 /*****************************************************************************
10557  Warning handling
10558 *****************************************************************************/
10559 
10580 bool
10581 Field::set_warning(Sql_condition::enum_warning_level level, uint code,
10582  int cut_increment) const
10583 {
10584  /*
10585  If this field was created only for type conversion purposes it
10586  will have table == NULL.
10587  */
10588  THD *thd= table ? table->in_use : current_thd;
10589  if (thd->count_cuted_fields)
10590  {
10591  thd->cuted_fields+= cut_increment;
10592  push_warning_printf(thd, level, code, ER(code), field_name,
10593  thd->get_stmt_da()->current_row_for_warning());
10594  return 0;
10595  }
10596  return level >= Sql_condition::WARN_LEVEL_WARN;
10597 }
10598 
10599 
10613 void
10614 Field_temporal::set_datetime_warning(Sql_condition::enum_warning_level level,
10615  uint code,
10616  ErrConvString val,
10617  timestamp_type ts_type,
10618  int cut_increment)
10619 {
10620  THD *thd= table ? table->in_use : current_thd;
10621  if (thd->really_abort_on_warning() ||
10622  set_warning(level, code, cut_increment))
10623  make_truncated_value_warning(thd, level, val, ts_type, field_name);
10624 }