1 /*
2    Copyright (c) 2000, 2020, 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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "sql/field.h"
26 
27 #include <float.h>
28 #include <stddef.h>
29 
30 #include "m_ctype.h"
31 #include "my_config.h"
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35 
36 #include <algorithm>
37 #include <cmath>   // isnan
38 #include <memory>  // unique_ptr
39 
40 #include "decimal.h"
41 #include "m_string.h"
42 #include "my_alloc.h"
43 #include "my_bit.h"
44 #include "my_byteorder.h"
45 #include "my_compare.h"
46 #include "my_dbug.h"
47 #include "my_double2ulonglong.h"
48 #include "my_sqlcommand.h"
49 #include "myisampack.h"
50 #include "sql/create_field.h"
51 #include "sql/current_thd.h"
52 #include "sql/dd/cache/dictionary_client.h"
53 #include "sql/dd/types/table.h"
54 #include "sql/dd_table_share.h"     // dd_get_old_field_type
55 #include "sql/derror.h"             // ER_THD
56 #include "sql/filesort.h"           // change_double_for_sort
57 #include "sql/gis/rtree_support.h"  // get_mbr_from_store
58 #include "sql/gis/srid.h"
59 #include "sql/handler.h"
60 #include "sql/item.h"
61 #include "sql/item_json_func.h"  // ensure_utf8mb4
62 #include "sql/item_timefunc.h"   // Item_func_now_local
63 #include "sql/json_binary.h"     // json_binary::serialize
64 #include "sql/json_diff.h"       // Json_diff_vector
65 #include "sql/json_dom.h"        // Json_dom, Json_wrapper
66 #include "sql/key.h"
67 #include "sql/log_event.h"  // class Table_map_log_event
68 #include "sql/my_decimal.h"
69 #include "sql/mysqld.h"  // log_10
70 #include "sql/protocol.h"
71 #include "sql/psi_memory_key.h"
72 #include "sql/rpl_rli.h"                // Relay_log_info
73 #include "sql/rpl_slave.h"              // rpl_master_has_bug
74 #include "sql/spatial.h"                // Geometry
75 #include "sql/sql_class.h"              // THD
76 #include "sql/sql_exception_handler.h"  // handle_std_exception
77 #include "sql/sql_lex.h"
78 #include "sql/sql_time.h"       // str_to_datetime_with_warn
79 #include "sql/sql_tmp_table.h"  // create_tmp_field
80 #include "sql/srs_fetcher.h"
81 #include "sql/strfunc.h"  // find_type2
82 #include "sql/system_variables.h"
83 #include "sql/transaction_info.h"
84 #include "sql/tztime.h"      // Time_zone
85 #include "template_utils.h"  // pointer_cast
86 #include "typelib.h"
87 
88 namespace dd {
89 class Spatial_reference_system;
90 }  // namespace dd
91 
92 using std::max;
93 using std::min;
94 
95 #define FLAGSTR(V, F) ((V) & (F) ? #F " " : "")
96 
97 // Maximum allowed exponent value for converting string to decimal
98 #define MAX_EXPONENT 1024
99 
100 /**
101   Static variables
102 */
103 const char field_separator = ',';
104 uchar Field::dummy_null_buffer = ' ';
105 
106 #define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
107 #define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
108 #define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
109 #define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
110   ((ulong)((1LL << std::min(arg, 4U) * 8) - 1LL))
111 
112 /*
113   Rules for merging different types of fields in UNION
114 
115   NOTE: to avoid 256*256 table, gap in table types numeration is skiped
116   following #defines describe that gap and how to canculate number of fields
117   and index of field in thia array.
118 */
119 #define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_BIT + 1)
120 #define FIELDTYPE_TEAR_TO (MYSQL_TYPE_JSON - 1)
121 #define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
122 
123 namespace {
124 /**
125    Predicate to determine if a field type change prevents alter
126    from being done inplace.
127 
128    @param from - existing Field object.
129    @param to   - Create_field object describing new version of field.
130 
131    @return true if alter cannot be done inplace due to specified
132    condition, false otherwise.
133 */
sql_type_prevents_inplace(const Field & from,const Create_field & to)134 bool sql_type_prevents_inplace(const Field &from, const Create_field &to) {
135   DBUG_TRACE;
136   return to.sql_type != from.real_type();
137 }
138 
139 /**
140    Predicate to determine if a length change prevents alter from being
141    done inplace. Length cannot decrease and cannot cross the 256 byte
142    row format barrier.
143 
144    @param from - existing Field object.
145    @param to   - Create_field object describing new version of field.
146 
147    @return true if alter cannot be done inplace due to specified
148    condition, false otherwise.
149 */
length_prevents_inplace(const Field & from,const Create_field & to)150 bool length_prevents_inplace(const Field &from, const Create_field &to) {
151   DBUG_TRACE;
152   DBUG_PRINT(
153       "inplace",
154       ("from:%p, to.field:%p, to.field->row_pack_length():%u, "
155        "to.max_display_width_in_bytes():%zu",
156        &from, to.field, to.field ? to.field->row_pack_length() : (uint)-1,
157        to.max_display_width_in_bytes()));
158 
159   if (to.pack_length() < from.pack_length()) {
160     DBUG_PRINT(
161         "inplace",
162         ("decreasing pack_length from %u to %zu, -> true for '%s'",
163          from.pack_length(), to.pack_length(), current_thd->query().str));
164     return true;
165   }
166 
167   if (to.max_display_width_in_bytes() >= 256 && from.row_pack_length() < 256) {
168     DBUG_PRINT("inplace",
169                ("row_pack_length increases past the 256 threshold, from %u to "
170                 "%zu, -> true for '%s'",
171                 from.row_pack_length(), to.max_display_width_in_bytes(),
172                 current_thd->query().str));
173     DBUG_PRINT("inplace",
174                ("from:%p, to.field:%p, to.field->row_pack_length():%u", &from,
175                 to.field, to.field ? to.field->row_pack_length() : (uint)-1));
176     return true;
177   }
178   DBUG_PRINT("inplace", ("-> false"));
179   return false;
180 }
181 
182 /**
183    Predicate to determine if a charset change prevents alter from being
184    done inplace.
185 
186    For changes other than the following, we can immediately reject using
187    the inplace algorithm:
188 
189       - Changing collation while keeping the charset.
190       - Changing any charset to the binary charset.
191       - Changing utf8mb3 to utf8mb4.
192 
193    @note The changes listed above are potentially acceptable if the field
194    is not indexed in the target table. This information is not available
195    here, and is checked later in fill_alter_inplace_info().
196 
197    @note ASCII cannot be converted to UTF-8 inplace because inserting
198    non-ascii values into an ASCII column only trigger a warning not an
199    error.
200 
201    @param from - existing Field object.
202    @param to   - Create_field object describing new version of field.
203 
204    @return true if alter cannot be done inplace due to specified
205    condition, false otherwise.
206 */
charset_prevents_inplace(const Field_str & from,const Create_field & to)207 bool charset_prevents_inplace(const Field_str &from, const Create_field &to) {
208   DBUG_TRACE;
209 
210   if (my_charset_same(to.charset, from.charset()) ||
211       my_charset_same(to.charset, &my_charset_bin)) {
212     return false;
213   }
214   return (0 != strcmp(to.charset->csname, MY_UTF8MB4) ||
215           0 != strcmp(from.charset()->csname, MY_UTF8MB3));
216 }
217 
218 /**
219    Predicate to determine if the difference between a Field and the
220    new Create_field prevents alter from being done
221    inplace. Convenience wrapper for the preceeding predicates.
222 
223    @param from - existing Field object.
224    @param to   - Create_field object describing new version of field.
225 
226    @return true if alter cannot be done inplace due to specified
227    condition, false otherwise.
228 */
change_prevents_inplace(const Field_str & from,const Create_field & to)229 bool change_prevents_inplace(const Field_str &from, const Create_field &to) {
230   DBUG_TRACE;
231   return sql_type_prevents_inplace(from, to) ||
232          length_prevents_inplace(from, to) ||
233          charset_prevents_inplace(from, to);
234 }
235 }  // namespace
236 
field_type2index(enum_field_types field_type)237 inline int field_type2index(enum_field_types field_type) {
238   field_type = real_type_to_type(field_type);
239   DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM ||
240               field_type > FIELDTYPE_TEAR_TO);
241   return (field_type < FIELDTYPE_TEAR_FROM
242               ? field_type
243               : ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) -
244                     1);
245 }
246 
247 static enum_field_types field_types_merge_rules[FIELDTYPE_NUM][FIELDTYPE_NUM] =
248     {
249         /* MYSQL_TYPE_DECIMAL -> */
250         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
251          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
252          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
253          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
254          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
255          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
256          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
257          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
258          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
259          MYSQL_TYPE_DECIMAL, MYSQL_TYPE_DECIMAL,
260          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
261          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
262          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
263          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
264          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
265          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
266          // MYSQL_TYPE_BIT          <16>-<244>
267          MYSQL_TYPE_NEWDECIMAL,
268          // MYSQL_TYPE_JSON
269          MYSQL_TYPE_VARCHAR,
270          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
271          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
272          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
273          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
274          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
275          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
276          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
277          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
278          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
279          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
280         /* MYSQL_TYPE_TINY -> */
281         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
282          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
283          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
284          MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
285          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
286          MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
287          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
288          MYSQL_TYPE_TINY, MYSQL_TYPE_VARCHAR,
289          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
290          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
291          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
292          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
293          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
294          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY,
295          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
296          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
297          // MYSQL_TYPE_BIT          <16>-<244>
298          MYSQL_TYPE_LONGLONG,
299          // MYSQL_TYPE_JSON
300          MYSQL_TYPE_VARCHAR,
301          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
302          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
303          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
304          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
305          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
306          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
307          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
308          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
309          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
310          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
311         /* MYSQL_TYPE_SHORT -> */
312         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
313          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_SHORT,
314          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
315          MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
316          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
317          MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
318          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
319          MYSQL_TYPE_SHORT, MYSQL_TYPE_VARCHAR,
320          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
321          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
322          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
323          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
324          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
325          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_SHORT,
326          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
327          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
328          // MYSQL_TYPE_BIT          <16>-<244>
329          MYSQL_TYPE_LONGLONG,
330          // MYSQL_TYPE_JSON
331          MYSQL_TYPE_VARCHAR,
332          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
333          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
334          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
335          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
336          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
337          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
338          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
339          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
340          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
341          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
342         /* MYSQL_TYPE_LONG -> */
343         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
344          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONG,
345          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
346          MYSQL_TYPE_LONG, MYSQL_TYPE_LONG,
347          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
348          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
349          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
350          MYSQL_TYPE_LONG, MYSQL_TYPE_VARCHAR,
351          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
352          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG,
353          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
354          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
355          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
356          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONG,
357          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
358          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
359          // MYSQL_TYPE_BIT          <16>-<244>
360          MYSQL_TYPE_LONGLONG,
361          // MYSQL_TYPE_JSON
362          MYSQL_TYPE_VARCHAR,
363          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
364          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
365          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
366          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
367          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
368          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
369          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
370          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
371          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
372          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
373         /* MYSQL_TYPE_FLOAT -> */
374         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
375          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_FLOAT,
376          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
377          MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
378          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
379          MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
380          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
381          MYSQL_TYPE_FLOAT, MYSQL_TYPE_VARCHAR,
382          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
383          MYSQL_TYPE_FLOAT, MYSQL_TYPE_FLOAT,
384          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
385          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
386          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
387          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_FLOAT,
388          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
389          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
390          // MYSQL_TYPE_BIT          <16>-<244>
391          MYSQL_TYPE_DOUBLE,
392          // MYSQL_TYPE_JSON
393          MYSQL_TYPE_VARCHAR,
394          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
395          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
396          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
397          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
398          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
399          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
400          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
401          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
402          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
403          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
404         /* MYSQL_TYPE_DOUBLE -> */
405         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
406          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
407          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
408          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
409          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
410          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
411          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
412          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
413          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
414          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
415          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
416          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
417          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
418          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_DOUBLE,
419          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
420          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
421          // MYSQL_TYPE_BIT          <16>-<244>
422          MYSQL_TYPE_DOUBLE,
423          // MYSQL_TYPE_JSON
424          MYSQL_TYPE_VARCHAR,
425          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
426          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
427          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
428          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
429          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
430          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
431          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
432          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
433          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
434          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
435         /* MYSQL_TYPE_NULL -> */
436         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
437          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
438          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
439          MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
440          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
441          MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
442          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
443          MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP,
444          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
445          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
446          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
447          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_TIME,
448          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
449          MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR,
450          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
451          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
452          // MYSQL_TYPE_BIT          <16>-<244>
453          MYSQL_TYPE_BIT,
454          // MYSQL_TYPE_JSON
455          MYSQL_TYPE_JSON,
456          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
457          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_ENUM,
458          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
459          MYSQL_TYPE_SET, MYSQL_TYPE_TINY_BLOB,
460          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
461          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
462          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
463          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
464          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
465          MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY},
466         /* MYSQL_TYPE_TIMESTAMP -> */
467         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
468          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
469          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
470          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
471          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
472          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
473          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
474          MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_TIMESTAMP,
475          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
476          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
477          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
478          MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
479          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
480          MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
481          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
482          MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
483          // MYSQL_TYPE_BIT          <16>-<244>
484          MYSQL_TYPE_VARCHAR,
485          // MYSQL_TYPE_JSON
486          MYSQL_TYPE_VARCHAR,
487          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
488          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
489          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
490          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
491          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
492          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
493          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
494          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
495          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
496          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
497         /* MYSQL_TYPE_LONGLONG -> */
498         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
499          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONGLONG,
500          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
501          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
502          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
503          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
504          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
505          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_VARCHAR,
506          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
507          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
508          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
509          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
510          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
511          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONGLONG,
512          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
513          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
514          // MYSQL_TYPE_BIT          <16>-<244>
515          MYSQL_TYPE_LONGLONG,
516          // MYSQL_TYPE_JSON
517          MYSQL_TYPE_VARCHAR,
518          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
519          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
520          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
521          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
522          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
523          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
524          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
525          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
526          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
527          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
528         /* MYSQL_TYPE_INT24 -> */
529         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
530          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_INT24,
531          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
532          MYSQL_TYPE_INT24, MYSQL_TYPE_LONG,
533          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
534          MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
535          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
536          MYSQL_TYPE_INT24, MYSQL_TYPE_VARCHAR,
537          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
538          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
539          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
540          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
541          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
542          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_INT24,
543          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
544          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
545          // MYSQL_TYPE_BIT          <16>-<244>
546          MYSQL_TYPE_LONGLONG,
547          // MYSQL_TYPE_JSON
548          MYSQL_TYPE_VARCHAR,
549          // MYSQL_TYPE_NEWDECIMAL    MYSQL_TYPE_ENUM
550          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
551          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
552          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
553          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
554          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
555          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
556          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
557          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
558          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
559         /* MYSQL_TYPE_DATE -> */
560         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
561          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
562          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
563          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
564          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
565          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
566          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
567          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
568          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
569          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
570          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
571          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
572          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
573          MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
574          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
575          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
576          // MYSQL_TYPE_BIT          <16>-<244>
577          MYSQL_TYPE_VARCHAR,
578          // MYSQL_TYPE_JSON
579          MYSQL_TYPE_VARCHAR,
580          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
581          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
582          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
583          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
584          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
585          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
586          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
587          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
588          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
589          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
590         /* MYSQL_TYPE_TIME -> */
591         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
592          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
593          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
594          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
595          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
596          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
597          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
598          MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME,
599          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
600          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
601          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
602          MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIME,
603          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
604          MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
605          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
606          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
607          // MYSQL_TYPE_BIT          <16>-<244>
608          MYSQL_TYPE_VARCHAR,
609          // MYSQL_TYPE_JSON
610          MYSQL_TYPE_VARCHAR,
611          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
612          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
613          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
614          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
615          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
616          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
617          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
618          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
619          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
620          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
621         /* MYSQL_TYPE_DATETIME -> */
622         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
623          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
624          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
625          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
626          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
627          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
628          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
629          MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
630          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
631          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
632          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
633          MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
634          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
635          MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
636          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
637          MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
638          // MYSQL_TYPE_BIT          <16>-<244>
639          MYSQL_TYPE_VARCHAR,
640          // MYSQL_TYPE_JSON
641          MYSQL_TYPE_VARCHAR,
642          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
643          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
644          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
645          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
646          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
647          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
648          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
649          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
650          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
651          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
652         /* MYSQL_TYPE_YEAR -> */
653         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
654          MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY,
655          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
656          MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
657          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
658          MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
659          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
660          MYSQL_TYPE_YEAR, MYSQL_TYPE_VARCHAR,
661          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
662          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
663          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
664          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
665          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
666          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_YEAR,
667          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
668          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
669          // MYSQL_TYPE_BIT          <16>-<244>
670          MYSQL_TYPE_LONGLONG,
671          // MYSQL_TYPE_JSON
672          MYSQL_TYPE_VARCHAR,
673          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
674          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
675          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
676          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
677          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
678          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
679          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
680          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
681          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
682          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
683         /* MYSQL_TYPE_NEWDATE -> */
684         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
685          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
686          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
687          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
688          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
689          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
690          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
691          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
692          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
693          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
694          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
695          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
696          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
697          MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
698          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
699          MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
700          // MYSQL_TYPE_BIT          <16>-<244>
701          MYSQL_TYPE_VARCHAR,
702          // MYSQL_TYPE_JSON
703          MYSQL_TYPE_VARCHAR,
704          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
705          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
706          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
707          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
708          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
709          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
710          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
711          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
712          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
713          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
714         /* MYSQL_TYPE_VARCHAR -> */
715         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
716          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
717          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
718          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
719          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
720          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
721          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
722          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
723          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
724          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
725          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
726          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
727          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
728          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
729          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
730          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
731          // MYSQL_TYPE_BIT          <16>-<244>
732          MYSQL_TYPE_VARCHAR,
733          // MYSQL_TYPE_JSON
734          MYSQL_TYPE_VARCHAR,
735          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
736          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
737          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
738          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
739          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
740          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
741          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
742          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
743          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
744          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR},
745         /* MYSQL_TYPE_BIT -> */
746         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
747          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONGLONG,
748          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
749          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
750          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
751          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
752          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
753          MYSQL_TYPE_BIT, MYSQL_TYPE_VARCHAR,
754          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
755          MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
756          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
757          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
758          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
759          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONGLONG,
760          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
761          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
762          // MYSQL_TYPE_BIT          <16>-<244>
763          MYSQL_TYPE_BIT,
764          // MYSQL_TYPE_JSON
765          MYSQL_TYPE_VARCHAR,
766          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
767          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
768          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
769          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
770          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
771          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
772          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
773          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
774          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
775          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
776         /* MYSQL_TYPE_JSON -> */
777         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
778          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
779          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
780          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
781          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
782          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
783          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
784          MYSQL_TYPE_JSON, MYSQL_TYPE_VARCHAR,
785          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
786          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
787          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
788          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
789          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
790          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
791          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
792          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
793          // MYSQL_TYPE_BIT          <16>-<244>
794          MYSQL_TYPE_VARCHAR,
795          // MYSQL_TYPE_JSON
796          MYSQL_TYPE_JSON,
797          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
798          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
799          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
800          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONG_BLOB,
801          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
802          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
803          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
804          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_VARCHAR,
805          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
806          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
807         /* MYSQL_TYPE_NEWDECIMAL -> */
808         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
809          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
810          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
811          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
812          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
813          MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
814          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
815          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
816          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
817          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
818          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
819          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
820          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
821          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_NEWDECIMAL,
822          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
823          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
824          // MYSQL_TYPE_BIT          <16>-<244>
825          MYSQL_TYPE_NEWDECIMAL,
826          // MYSQL_TYPE_JSON
827          MYSQL_TYPE_VARCHAR,
828          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
829          MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
830          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
831          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
832          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
833          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
834          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
835          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
836          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
837          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
838         /* MYSQL_TYPE_ENUM -> */
839         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
840          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
841          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
842          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
843          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
844          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
845          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
846          MYSQL_TYPE_ENUM, MYSQL_TYPE_VARCHAR,
847          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
848          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
849          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
850          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
851          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
852          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
853          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
854          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
855          // MYSQL_TYPE_BIT          <16>-<244>
856          MYSQL_TYPE_VARCHAR,
857          // MYSQL_TYPE_JSON
858          MYSQL_TYPE_VARCHAR,
859          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
860          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
861          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
862          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
863          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
864          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
865          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
866          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
867          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
868          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
869         /* MYSQL_TYPE_SET -> */
870         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
871          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
872          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
873          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
874          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
875          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
876          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
877          MYSQL_TYPE_SET, MYSQL_TYPE_VARCHAR,
878          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
879          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
880          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
881          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
882          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
883          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
884          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
885          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
886          // MYSQL_TYPE_BIT          <16>-<244>
887          MYSQL_TYPE_VARCHAR,
888          // MYSQL_TYPE_JSON
889          MYSQL_TYPE_VARCHAR,
890          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
891          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
892          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
893          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
894          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
895          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
896          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
897          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
898          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
899          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR},
900         /* MYSQL_TYPE_TINY_BLOB -> */
901         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
902          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
903          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
904          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
905          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
906          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
907          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
908          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
909          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
910          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
911          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
912          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
913          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
914          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
915          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
916          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
917          // MYSQL_TYPE_BIT          <16>-<244>
918          MYSQL_TYPE_TINY_BLOB,
919          // MYSQL_TYPE_JSON
920          MYSQL_TYPE_LONG_BLOB,
921          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
922          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
923          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
924          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
925          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
926          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
927          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
928          MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB,
929          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
930          MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB},
931         /* MYSQL_TYPE_MEDIUM_BLOB -> */
932         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
933          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
934          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
935          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
936          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
937          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
938          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
939          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
940          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
941          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
942          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
943          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
944          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
945          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
946          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
947          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
948          // MYSQL_TYPE_BIT          <16>-<244>
949          MYSQL_TYPE_MEDIUM_BLOB,
950          // MYSQL_TYPE_JSON
951          MYSQL_TYPE_LONG_BLOB,
952          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
953          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
954          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
955          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
956          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
957          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
958          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
959          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
960          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
961          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB},
962         /* MYSQL_TYPE_LONG_BLOB -> */
963         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
964          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
965          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
966          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
967          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
968          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
969          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
970          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
971          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
972          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
973          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
974          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
975          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
976          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
977          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
978          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
979          // MYSQL_TYPE_BIT          <16>-<244>
980          MYSQL_TYPE_LONG_BLOB,
981          // MYSQL_TYPE_JSON
982          MYSQL_TYPE_LONG_BLOB,
983          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
984          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
985          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
986          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
987          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
988          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
989          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
990          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
991          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
992          MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB},
993         /* MYSQL_TYPE_BLOB -> */
994         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
995          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
996          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
997          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
998          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
999          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1000          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
1001          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1002          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
1003          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1004          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
1005          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1006          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
1007          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1008          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
1009          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1010          // MYSQL_TYPE_BIT          <16>-<244>
1011          MYSQL_TYPE_BLOB,
1012          // MYSQL_TYPE_JSON
1013          MYSQL_TYPE_LONG_BLOB,
1014          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
1015          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1016          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
1017          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1018          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
1019          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
1020          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
1021          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
1022          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
1023          MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB},
1024         /* MYSQL_TYPE_VAR_STRING -> */
1025         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
1026          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1027          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
1028          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1029          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
1030          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1031          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
1032          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1033          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
1034          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1035          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
1036          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1037          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
1038          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1039          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
1040          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1041          // MYSQL_TYPE_BIT          <16>-<244>
1042          MYSQL_TYPE_VARCHAR,
1043          // MYSQL_TYPE_JSON
1044          MYSQL_TYPE_VARCHAR,
1045          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
1046          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1047          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
1048          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
1049          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
1050          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
1051          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
1052          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
1053          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
1054          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR},
1055         /* MYSQL_TYPE_STRING -> */
1056         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
1057          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
1058          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
1059          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
1060          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
1061          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
1062          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
1063          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
1064          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
1065          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
1066          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
1067          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
1068          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
1069          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
1070          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
1071          MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR,
1072          // MYSQL_TYPE_BIT          <16>-<244>
1073          MYSQL_TYPE_STRING,
1074          // MYSQL_TYPE_JSON
1075          MYSQL_TYPE_STRING,
1076          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
1077          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
1078          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
1079          MYSQL_TYPE_STRING, MYSQL_TYPE_TINY_BLOB,
1080          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
1081          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
1082          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
1083          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
1084          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
1085          MYSQL_TYPE_STRING, MYSQL_TYPE_STRING},
1086         /* MYSQL_TYPE_GEOMETRY -> */
1087         {// MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
1088          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1089          // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
1090          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1091          // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
1092          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1093          // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
1094          MYSQL_TYPE_GEOMETRY, MYSQL_TYPE_VARCHAR,
1095          // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
1096          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1097          // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
1098          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1099          // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
1100          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1101          // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
1102          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1103          // MYSQL_TYPE_BIT          <16>-<244>
1104          MYSQL_TYPE_VARCHAR,
1105          // MYSQL_TYPE_JSON
1106          MYSQL_TYPE_VARCHAR,
1107          // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
1108          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
1109          // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
1110          MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
1111          // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
1112          MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
1113          // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
1114          MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
1115          // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
1116          MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY}};
1117 
pre_validate_value_generator_expr(Item * expression,const char * name,Value_generator_source source)1118 bool pre_validate_value_generator_expr(Item *expression, const char *name,
1119                                        Value_generator_source source) {
1120   enum error_type { ER_GENERATED_ROW, ER_NAMED_FUNCTION, MAX_ERROR };
1121   int error_code_map[][MAX_ERROR] = {
1122       // Generated column
1123       {ER_GENERATED_COLUMN_ROW_VALUE,
1124        ER_GENERATED_COLUMN_NAMED_FUNCTION_IS_NOT_ALLOWED},
1125       // Default expression
1126       {ER_DEFAULT_VAL_GENERATED_ROW_VALUE,
1127        ER_DEFAULT_VAL_GENERATED_NAMED_FUNCTION_IS_NOT_ALLOWED},
1128       // Check constraint
1129       {ER_CHECK_CONSTRAINT_ROW_VALUE,
1130        ER_CHECK_CONSTRAINT_NAMED_FUNCTION_IS_NOT_ALLOWED}};
1131 
1132   // ROW values are not allowed
1133   if (expression->type() == Item::ROW_ITEM) {
1134     my_error(error_code_map[source][ER_GENERATED_ROW], MYF(0), name);
1135     return true;
1136   }
1137 
1138   Check_function_as_value_generator_parameters checker_args(
1139       error_code_map[source][ER_NAMED_FUNCTION], source);
1140 
1141   if (expression->walk(&Item::check_function_as_value_generator,
1142                        enum_walk::SUBQUERY_POSTFIX,
1143                        pointer_cast<uchar *>(&checker_args))) {
1144     my_error(checker_args.err_code, MYF(0), name,
1145              checker_args.banned_function_name);
1146     return true;
1147   }
1148 
1149   return false;
1150 }
1151 
1152 /**
1153   Set field to temporary value NULL.
1154 */
set_tmp_null()1155 void Field::set_tmp_null() {
1156   m_is_tmp_null = true;
1157 
1158   m_check_for_truncated_fields_saved =
1159       table ? table->in_use->check_for_truncated_fields
1160             : current_thd->check_for_truncated_fields;
1161 }
1162 
is_equal(const Create_field * new_field) const1163 uint Field::is_equal(const Create_field *new_field) const {
1164   return (real_type() == new_field->sql_type);
1165 }
1166 /**
1167   Return type of which can carry value of both given types in UNION result.
1168 
1169   @param a  type for merging
1170   @param b  type for merging
1171 
1172   @return
1173     type of field
1174 */
1175 
field_type_merge(enum_field_types a,enum_field_types b)1176 enum_field_types Field::field_type_merge(enum_field_types a,
1177                                          enum_field_types b) {
1178   return field_types_merge_rules[field_type2index(a)][field_type2index(b)];
1179 }
1180 
1181 static Item_result field_types_result_type[FIELDTYPE_NUM] = {
1182     // MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
1183     DECIMAL_RESULT, INT_RESULT,
1184     // MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
1185     INT_RESULT, INT_RESULT,
1186     // MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
1187     REAL_RESULT, REAL_RESULT,
1188     // MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
1189     STRING_RESULT, STRING_RESULT,
1190     // MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
1191     INT_RESULT, INT_RESULT,
1192     // MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
1193     STRING_RESULT, STRING_RESULT,
1194     // MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
1195     STRING_RESULT, INT_RESULT,
1196     // MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
1197     STRING_RESULT, STRING_RESULT,
1198     // MYSQL_TYPE_BIT          <16>-<244>
1199     INT_RESULT,
1200     // MYSQL_TYPE_JSON
1201     STRING_RESULT,
1202     // MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
1203     DECIMAL_RESULT, STRING_RESULT,
1204     // MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
1205     STRING_RESULT, STRING_RESULT,
1206     // MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
1207     STRING_RESULT, STRING_RESULT,
1208     // MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
1209     STRING_RESULT, STRING_RESULT,
1210     // MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
1211     STRING_RESULT, STRING_RESULT};
1212 
1213 /**
1214   Convert Field::geometry_type to the corresponding Geometry::wkbType.
1215 
1216   @param t The geometry_type to convert
1217 
1218   @return The corresponding Geometry::wkbType, or
1219           Geometry::wkb_invalid_type if there's not suitable type.
1220 */
geometry_type_to_wkb_type(Field::geometry_type t)1221 static Geometry::wkbType geometry_type_to_wkb_type(Field::geometry_type t) {
1222   switch (t) {
1223     case Field::GEOM_GEOMETRY:
1224       return Geometry::wkb_invalid_type;
1225     case Field::GEOM_POINT:
1226       return Geometry::wkb_point;
1227     case Field::GEOM_LINESTRING:
1228       return Geometry::wkb_linestring;
1229     case Field::GEOM_POLYGON:
1230       return Geometry::wkb_polygon;
1231     case Field::GEOM_MULTIPOINT:
1232       return Geometry::wkb_multipoint;
1233     case Field::GEOM_MULTILINESTRING:
1234       return Geometry::wkb_multilinestring;
1235     case Field::GEOM_MULTIPOLYGON:
1236       return Geometry::wkb_multipolygon;
1237     case Field::GEOM_GEOMETRYCOLLECTION:
1238       return Geometry::wkb_geometrycollection;
1239     default:
1240       DBUG_ASSERT(0);
1241       return Geometry::wkb_invalid_type;
1242   }
1243 }
1244 
1245 /*
1246   Test if the given string contains important data:
1247   not spaces for character string,
1248   or any data for binary string.
1249 
1250   SYNOPSIS
1251     test_if_important_data()
1252     cs          Character set
1253     str         String to test
1254     strend      String end
1255 
1256   RETURN
1257     false - If string does not have important data
1258     true  - If string has some important data
1259 */
1260 
test_if_important_data(const CHARSET_INFO * cs,const char * str,const char * strend)1261 static bool test_if_important_data(const CHARSET_INFO *cs, const char *str,
1262                                    const char *strend) {
1263   if (cs != &my_charset_bin)
1264     str += cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
1265   return (str < strend);
1266 }
1267 
1268 /**
1269    Function to compare two unsigned integers for their relative order.
1270    Used below. In an anonymous namespace to not clash with definitions
1271    in other files.
1272  */
1273 
1274 namespace {
1275 
compare(unsigned int a,unsigned int b)1276 int compare(unsigned int a, unsigned int b) {
1277   if (a < b) return -1;
1278   if (b < a) return 1;
1279   return 0;
1280 }
1281 
1282 }  // namespace
1283 
1284 /**
1285   Detect Item_result by given field type of UNION merge result.
1286 
1287   @param field_type  given field type
1288 
1289   @return
1290     Item_result (type of internal MySQL expression result)
1291 */
1292 
result_merge_type(enum_field_types field_type)1293 Item_result Field::result_merge_type(enum_field_types field_type) {
1294   return field_types_result_type[field_type2index(field_type)];
1295 }
1296 
1297 /*****************************************************************************
1298   Static help functions
1299 *****************************************************************************/
1300 
1301 /**
1302   Output a warning for erroneous conversion of strings to numerical
1303   values. For use with ER_TRUNCATED_WRONG_VALUE[_FOR_FIELD]
1304 
1305   @param thd         THD object
1306   @param str         pointer to string that failed to be converted
1307   @param length      length of string
1308   @param cs          charset for string
1309   @param typestr     string describing type converted to
1310   @param error       error value to output
1311   @param field_name  (for *_FOR_FIELD) name of field
1312   @param row_num     (for *_FOR_FIELD) row number
1313  */
push_numerical_conversion_warning(THD * thd,const char * str,uint length,const CHARSET_INFO * cs,const char * typestr,int error,const char * field_name="UNKNOWN",ulong row_num=0)1314 static void push_numerical_conversion_warning(
1315     THD *thd, const char *str, uint length, const CHARSET_INFO *cs,
1316     const char *typestr, int error, const char *field_name = "UNKNOWN",
1317     ulong row_num = 0) {
1318   char buf[std::max(std::max(DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE,
1319                              LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE),
1320                     DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE)];
1321 
1322   String tmp(buf, sizeof(buf), cs);
1323   tmp.copy(str, length, cs);
1324   push_warning_printf(thd, Sql_condition::SL_WARNING, error,
1325                       ER_THD_NONCONST(thd, error), typestr, tmp.c_ptr(),
1326                       field_name, row_num);
1327 }
1328 
1329 /**
1330   Emits a warning for the decimal conversion error. May modify
1331   dec_value if there was conversion overflow or bad number.
1332 
1333   @param field             Field to operate on
1334   @param dec_error         decimal library return code
1335                            (E_DEC_* see include/decimal.h)
1336   @param [in,out] dec_value Decimal value returned by conversion function.
1337   @param from              Value converted from
1338   @param length            Length of 'from'
1339   @param charset_arg       Charset of 'from'
1340 */
set_decimal_warning(Field_new_decimal * field,int dec_error,my_decimal * dec_value,const char * from,size_t length,const CHARSET_INFO * charset_arg)1341 static void set_decimal_warning(Field_new_decimal *field, int dec_error,
1342                                 my_decimal *dec_value, const char *from,
1343                                 size_t length,
1344                                 const CHARSET_INFO *charset_arg) {
1345   switch (dec_error) {
1346     case E_DEC_TRUNCATED:
1347       field->set_warning(Sql_condition::SL_NOTE, WARN_DATA_TRUNCATED, 1);
1348       break;
1349     case E_DEC_OVERFLOW:
1350       field->set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE,
1351                          1);
1352       field->set_value_on_overflow(dec_value, dec_value->sign());
1353       break;
1354     case E_DEC_BAD_NUM:
1355       ErrConvString errmsg(from, length, charset_arg);
1356       const Diagnostics_area *da = field->table->in_use->get_stmt_da();
1357       push_warning_printf(
1358           field->table->in_use, Sql_condition::SL_WARNING,
1359           ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1360           ER_THD(field->table->in_use, ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1361           "decimal", errmsg.ptr(), field->field_name,
1362           da->current_row_for_condition());
1363       my_decimal_set_zero(dec_value);
1364   }
1365 }
1366 
1367 /**
1368   Copy string with optional character set conversion.
1369 
1370   This calls helper function well_formed_copy_nchars to copy string
1371   with optional character set convertion. Specially, it checks if
1372   the ASCII code point exceeds code range. If YES, it allows the
1373   input but raises a warning.
1374 
1375   @param to_cs                    Character set of "to" string
1376   @param to                       Store result here
1377   @param to_length                Maxinum length of "to" string
1378   @param from_cs                  From character set
1379   @param from                     Copy from here
1380   @param from_length              Length of from string
1381   @param nchars                   Copy not more that nchars characters
1382   @param well_formed_error_pos    Return position when "from" is not well
1383                                   formed or NULL otherwise.
1384   @param cannot_convert_error_pos Return position where a not convertable
1385                                   character met, or NULL otherwise.
1386   @param from_end_pos             Return position where scanning of "from"
1387                                   string stopped.
1388 
1389   @retval length of bytes copied to 'to'
1390 */
1391 
field_well_formed_copy_nchars(const CHARSET_INFO * to_cs,char * to,size_t to_length,const CHARSET_INFO * from_cs,const char * from,size_t from_length,size_t nchars,const char ** well_formed_error_pos,const char ** cannot_convert_error_pos,const char ** from_end_pos)1392 static size_t field_well_formed_copy_nchars(
1393     const CHARSET_INFO *to_cs, char *to, size_t to_length,
1394     const CHARSET_INFO *from_cs, const char *from, size_t from_length,
1395     size_t nchars, const char **well_formed_error_pos,
1396     const char **cannot_convert_error_pos, const char **from_end_pos) {
1397   size_t res = well_formed_copy_nchars(
1398       to_cs, to, to_length, from_cs, from, from_length, nchars,
1399       well_formed_error_pos, cannot_convert_error_pos, from_end_pos);
1400   /*
1401    If the code point is out of ascii range, we only give user a warning
1402    in 5.7. Need to change to give a ERROR in future version.
1403   */
1404   if ((to_cs->state & MY_CS_PUREASCII) && *well_formed_error_pos != nullptr) {
1405     char tmp[32];
1406     *well_formed_error_pos = nullptr;
1407     convert_to_printable(tmp, sizeof(tmp), from, from_length, from_cs, 6);
1408     push_warning_printf(
1409         current_thd, Sql_condition::SL_WARNING, ER_INVALID_CHARACTER_STRING,
1410         ER_THD(current_thd, ER_INVALID_CHARACTER_STRING), "ascii", tmp);
1411   }
1412   return res;
1413 }
1414 
1415 /**
1416   Check whether a field type can be partially indexed by a key.
1417 
1418   This is a static method, rather than a virtual function, because we need
1419   to check the type of a non-Field in mysql_alter_table().
1420 
1421   @param type  field type
1422 
1423   @retval
1424     true  Type can have a prefixed key
1425   @retval
1426     false Type can not have a prefixed key
1427 */
1428 
type_can_have_key_part(enum enum_field_types type)1429 bool Field::type_can_have_key_part(enum enum_field_types type) {
1430   switch (type) {
1431     case MYSQL_TYPE_VARCHAR:
1432     case MYSQL_TYPE_TINY_BLOB:
1433     case MYSQL_TYPE_MEDIUM_BLOB:
1434     case MYSQL_TYPE_LONG_BLOB:
1435     case MYSQL_TYPE_BLOB:
1436     case MYSQL_TYPE_VAR_STRING:
1437     case MYSQL_TYPE_STRING:
1438     case MYSQL_TYPE_GEOMETRY:
1439       return true;
1440     default:
1441       return false;
1442   }
1443 }
1444 
1445 /**
1446   Numeric fields base class constructor.
1447 */
Field_num(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)1448 Field_num::Field_num(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1449                      uchar null_bit_arg, uchar auto_flags_arg,
1450                      const char *field_name_arg, uint8 dec_arg, bool zero_arg,
1451                      bool unsigned_arg)
1452     : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
1453             field_name_arg),
1454       unsigned_flag(unsigned_arg),
1455       dec(dec_arg),
1456       zerofill(zero_arg) {
1457   if (zerofill) set_flag(ZEROFILL_FLAG);
1458   if (unsigned_flag) set_flag(UNSIGNED_FLAG);
1459 }
1460 
prepend_zeros(String * value) const1461 void Field_num::prepend_zeros(String *value) const {
1462   int diff;
1463   if ((diff = (int)(field_length - value->length())) > 0) {
1464     const bool error = value->mem_realloc(field_length);
1465     if (!error) {
1466       memmove(value->ptr() + field_length - value->length(), value->ptr(),
1467               value->length());
1468       memset(value->ptr(), '0', diff);
1469       value->length(field_length);
1470     }
1471   }
1472 }
1473 
1474 /**
1475   Test if given number is a int.
1476 
1477   @todo
1478     Make this multi-byte-character safe
1479 
1480   @param cs		Character set
1481   @param str		String to test
1482   @param length         Length of 'str'
1483   @param int_end	Pointer to char after last used digit
1484   @param error          Return code from strntoull10rnd()
1485 
1486   @note
1487     This is called after one has called strntoull10rnd() function.
1488 
1489   @return TYPE_OK, TYPE_ERR_BAD_VALUE or TYPE_WARN_TRUNCATED
1490 */
1491 
check_int(const CHARSET_INFO * cs,const char * str,size_t length,const char * int_end,int error)1492 type_conversion_status Field_num::check_int(const CHARSET_INFO *cs,
1493                                             const char *str, size_t length,
1494                                             const char *int_end, int error) {
1495   /* Test if we get an empty string or wrong integer */
1496   if (str == int_end || error == MY_ERRNO_EDOM) {
1497     ErrConvString err(str, length, cs);
1498     push_warning_printf(
1499         table->in_use, Sql_condition::SL_WARNING,
1500         ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1501         ER_THD(table->in_use, ER_TRUNCATED_WRONG_VALUE_FOR_FIELD), "integer",
1502         err.ptr(), field_name,
1503         table->in_use->get_stmt_da()->current_row_for_condition());
1504     return TYPE_ERR_BAD_VALUE;
1505   }
1506   /* Test if we have garbage at the end of the given string. */
1507   if (test_if_important_data(cs, int_end, str + length)) {
1508     set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
1509     return TYPE_WARN_TRUNCATED;
1510   }
1511   return TYPE_OK;
1512 }
1513 
1514 /*
1515   Conver a string to an integer then check bounds.
1516 
1517   SYNOPSIS
1518     Field_num::get_int
1519     cs            Character set
1520     from          String to convert
1521     len           Length of the string
1522     rnd           OUT longlong value
1523     unsigned_max  max unsigned value
1524     signed_min    min signed value
1525     signed_max    max signed value
1526 
1527   DESCRIPTION
1528     The function calls strntoull10rnd() to get an integer value then
1529     check bounds and errors returned. In case of any error a warning
1530     is raised.
1531 
1532   @return TYPE_OK, TYPE_WARN_OUT_OF_RANGE, TYPE_ERR_BAD_VALUE or
1533           TYPE_WARN_TRUNCATED
1534 */
1535 
get_int(const CHARSET_INFO * cs,const char * from,size_t len,longlong * rnd,ulonglong unsigned_max,longlong signed_min,longlong signed_max)1536 type_conversion_status Field_num::get_int(const CHARSET_INFO *cs,
1537                                           const char *from, size_t len,
1538                                           longlong *rnd, ulonglong unsigned_max,
1539                                           longlong signed_min,
1540                                           longlong signed_max) {
1541   const char *end;
1542   int error;
1543 
1544   *rnd = (longlong)cs->cset->strntoull10rnd(cs, from, len, is_unsigned(), &end,
1545                                             &error);
1546   if (is_unsigned()) {
1547     if ((((ulonglong)*rnd > unsigned_max) && (*rnd = (longlong)unsigned_max)) ||
1548         error == MY_ERRNO_ERANGE)
1549       goto out_of_range;
1550   } else {
1551     if (*rnd < signed_min) {
1552       *rnd = signed_min;
1553       goto out_of_range;
1554     } else if (*rnd > signed_max) {
1555       *rnd = signed_max;
1556       goto out_of_range;
1557     }
1558   }
1559   if (table->in_use->check_for_truncated_fields != 0)
1560     return check_int(cs, from, len, end, error);
1561 
1562   return TYPE_OK;
1563 
1564 out_of_range:
1565   set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
1566   return TYPE_WARN_OUT_OF_RANGE;
1567 }
1568 
1569 /*
1570   This is a generic method which is executed only for
1571   Field_short, Field_medium, Field_long, Field_longlong and Field_tiny.
1572 
1573   The other field types that come from Field_num override this method:
1574   Field_real (common parent for Field_decimal, Field_float, Field_double),
1575   Field_new_decimal, Field_year.
1576 */
store_time(MYSQL_TIME * ltime,uint8)1577 type_conversion_status Field_num::store_time(MYSQL_TIME *ltime, uint8) {
1578   longlong nr = propagate_datetime_overflow(
1579       current_thd, [&](int *w) { return TIME_to_ulonglong_round(*ltime, w); });
1580   return store(ltime->neg ? -nr : nr, false);
1581 }
1582 
1583 /**
1584   Process decimal library return codes and issue warnings for overflow and
1585   truncation.
1586 
1587   @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
1588 
1589   @retval 0 No error or some other errors except overflow
1590   @retval 1 There was overflow
1591 */
1592 
warn_if_overflow(int op_result)1593 bool Field::warn_if_overflow(int op_result) {
1594   if (op_result == E_DEC_OVERFLOW) {
1595     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
1596     return true;
1597   }
1598   if (op_result == E_DEC_TRUNCATED) {
1599     set_warning(Sql_condition::SL_NOTE, WARN_DATA_TRUNCATED, 1);
1600     /* We return 0 here as this is not a critical issue */
1601   }
1602   return false;
1603 }
1604 
1605 /**
1606   Interpret field value as an integer but return the result as a string.
1607 
1608   This is used for printing bit_fields as numbers while debugging.
1609 */
1610 
val_int_as_str(String * val_buffer,bool unsigned_val) const1611 String *Field::val_int_as_str(String *val_buffer, bool unsigned_val) const {
1612   ASSERT_COLUMN_MARKED_FOR_READ;
1613   const CHARSET_INFO *cs = &my_charset_bin;
1614   size_t length;
1615   longlong value = val_int();
1616 
1617   if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS)) return nullptr;
1618   length = (*cs->cset->longlong10_to_str)(cs, val_buffer->ptr(),
1619                                           MY_INT64_NUM_DECIMAL_DIGITS,
1620                                           unsigned_val ? 10 : -10, value);
1621   val_buffer->length(length);
1622   return val_buffer;
1623 }
1624 
1625 /// This is used as a table name when the table structure is not set up
Field(uchar * ptr_arg,uint32 length_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg)1626 Field::Field(uchar *ptr_arg, uint32 length_arg, uchar *null_ptr_arg,
1627              uchar null_bit_arg, uchar auto_flags_arg,
1628              const char *field_name_arg)
1629     : ptr(ptr_arg),
1630       m_hidden(dd::Column::enum_hidden_type::HT_VISIBLE),
1631       m_null_ptr(null_ptr_arg),
1632       m_is_tmp_nullable(false),
1633       m_is_tmp_null(false),
1634       m_check_for_truncated_fields_saved(CHECK_FIELD_IGNORE),
1635       table(nullptr),
1636       orig_table(nullptr),
1637       table_name(nullptr),
1638       field_name(field_name_arg),
1639       field_length(length_arg),
1640       null_bit(null_bit_arg),
1641       auto_flags(auto_flags_arg),
1642       is_created_from_null_item(false),
1643       m_indexed(false),
1644       m_warnings_pushed(0),
1645       gcol_info(nullptr),
1646       stored_in_db(true),
1647       m_default_val_expr(nullptr)
1648 
1649 {
1650   if (!is_nullable()) set_flag(NOT_NULL_FLAG);
1651   comment.str = "";
1652   comment.length = 0;
1653   m_field_index = 0;
1654 }
1655 
1656 /**
1657   Check NOT NULL constraint on the field after temporary nullability is
1658   disabled.
1659 
1660   @param mysql_errno Warning to report.
1661 
1662   @return TYPE_OK if the value is Ok, or corresponding error code from
1663   the type_conversion_status enum.
1664 */
check_constraints(int mysql_errno)1665 type_conversion_status Field::check_constraints(int mysql_errno) {
1666   /*
1667     Ensure that Field::check_constraints() is called only when temporary
1668     nullability is disabled.
1669   */
1670 
1671   DBUG_ASSERT(!is_tmp_nullable());
1672 
1673   if (is_nullable()) return TYPE_OK;  // If the field is nullable, we're Ok.
1674 
1675   if (!m_is_tmp_null) return TYPE_OK;  // If the field was not NULL, we're Ok.
1676 
1677   // The field has been set to NULL.
1678 
1679   /*
1680     If the field is of AUTO_INCREMENT, and the next number
1681     has been assigned to it, we're Ok.
1682   */
1683 
1684   if (this == table->next_number_field) return TYPE_OK;
1685 
1686   switch (m_check_for_truncated_fields_saved) {
1687     case CHECK_FIELD_WARN:
1688       set_warning(Sql_condition::SL_WARNING, mysql_errno, 1);
1689       /* fall through */
1690     case CHECK_FIELD_IGNORE:
1691       return TYPE_OK;
1692     case CHECK_FIELD_ERROR_FOR_NULL:
1693       my_error(ER_BAD_NULL_ERROR, MYF(0), field_name);
1694       return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
1695   }
1696 
1697   DBUG_ASSERT(0);  // impossible
1698   my_error(ER_BAD_NULL_ERROR, MYF(0), field_name);
1699   return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
1700 }
1701 
1702 /**
1703   Set field to value NULL.
1704 
1705   @param row_offset    This is the offset between the row being updated
1706                        and table->record[0]
1707 */
set_null(ptrdiff_t row_offset)1708 void Field::set_null(ptrdiff_t row_offset) {
1709   if (is_nullable()) {
1710     DBUG_ASSERT(m_null_ptr != &dummy_null_buffer);
1711     m_null_ptr[row_offset] |= null_bit;
1712   } else if (is_tmp_nullable()) {
1713     set_tmp_null();
1714   }
1715 }
1716 
1717 /**
1718   Set field to value NOT NULL.
1719 
1720   @param row_offset    This is the offset between the row being updated
1721                        and table->record[0]
1722 */
set_notnull(ptrdiff_t row_offset)1723 void Field::set_notnull(ptrdiff_t row_offset) {
1724   if (is_nullable()) {
1725     DBUG_ASSERT(m_null_ptr != &dummy_null_buffer);
1726     m_null_ptr[row_offset] &= (uchar)~null_bit;
1727   } else if (is_tmp_nullable()) {
1728     reset_tmp_null();
1729   }
1730 }
1731 
hash(ulong * nr,ulong * nr2) const1732 void Field::hash(ulong *nr, ulong *nr2) const {
1733   if (is_null()) {
1734     *nr ^= (*nr << 1) | 1;
1735   } else {
1736     uint len = pack_length();
1737     const CHARSET_INFO *cs = sort_charset();
1738     uint64 tmp1 = *nr;
1739     uint64 tmp2 = *nr2;
1740     cs->coll->hash_sort(cs, ptr, len, &tmp1, &tmp2);
1741 
1742     // NOTE: This truncates to 32-bit on Windows, to keep on-disk stability.
1743     *nr = static_cast<ulong>(tmp1);
1744     *nr2 = static_cast<ulong>(tmp2);
1745   }
1746 }
1747 
copy_data(ptrdiff_t src_record_offset)1748 void Field::copy_data(ptrdiff_t src_record_offset) {
1749   memcpy(ptr, ptr + src_record_offset, pack_length());
1750 
1751   if (is_nullable()) {
1752     // Set to NULL if the source record is NULL, otherwise set to NOT-NULL.
1753     DBUG_ASSERT(m_null_ptr != &dummy_null_buffer);
1754     m_null_ptr[0] = (m_null_ptr[0] & ~null_bit) |
1755                     (m_null_ptr[src_record_offset] & null_bit);
1756   } else if (is_tmp_nullable())
1757     m_is_tmp_null = false;
1758 }
1759 
send_to_protocol(Protocol * protocol) const1760 bool Field::send_to_protocol(Protocol *protocol) const {
1761   if (is_null()) return protocol->store_null();
1762   char buff[MAX_FIELD_WIDTH];
1763   String tmp(buff, sizeof(buff), charset());
1764   String *res = val_str(&tmp);
1765   return res ? protocol->store(res) : protocol->store_null();
1766 }
1767 
1768 /**
1769    Check to see if field size is compatible with destination.
1770 
1771    This method is used in row-based replication to verify that the
1772    slave's field size is less than or equal to the master's field
1773    size. The encoded field metadata (from the master or source) is
1774    decoded and compared to the size of this field (the slave or
1775    destination).
1776 
1777    The comparison is made so that if the source data (from the master)
1778    is less than the target data (on the slave), -1 is returned in
1779    <code>*order_var</code>. This implies that a conversion is
1780    necessary, but that it is lossy and can result in truncation of the
1781    value.
1782 
1783    If the source data is strictly greater than the target data, 1 is
1784    returned in <code>*order_var</code>. This implies that the source
1785    type can is contained in the target type and that a conversion is
1786    necessary but is non-lossy.
1787 
1788    If no conversion is required to fit the source type in the target
1789    type, 0 is returned in <code>*order_var</code>.
1790 
1791    @param   field_metadata   Encoded size in field metadata
1792    @param   order_var        Pointer to variable where the order
1793                              between the source field and this field
1794                              will be returned.
1795 
1796    @return @c true if this field's size is compatible with the
1797    master's field size, @c false otherwise.
1798 */
compatible_field_size(uint field_metadata,Relay_log_info *,uint16,int * order_var) const1799 bool Field::compatible_field_size(uint field_metadata, Relay_log_info *, uint16,
1800                                   int *order_var) const {
1801   uint const source_size = pack_length_from_metadata(field_metadata);
1802   uint const destination_size = row_pack_length();
1803   DBUG_PRINT("debug", ("real_type: %d, source_size: %u, destination_size: %u",
1804                        real_type(), source_size, destination_size));
1805   *order_var = compare(source_size, destination_size);
1806   return true;
1807 }
1808 
store(const char * to,size_t length,const CHARSET_INFO * cs,enum_check_fields check_level)1809 type_conversion_status Field::store(const char *to, size_t length,
1810                                     const CHARSET_INFO *cs,
1811                                     enum_check_fields check_level) {
1812   enum_check_fields old_check_level = table->in_use->check_for_truncated_fields;
1813   table->in_use->check_for_truncated_fields = check_level;
1814   const type_conversion_status res = store(to, length, cs);
1815   table->in_use->check_for_truncated_fields = old_check_level;
1816   return res;
1817 }
1818 
pack(uchar * to,const uchar * from,size_t max_length) const1819 uchar *Field::pack(uchar *to, const uchar *from, size_t max_length) const {
1820   size_t length = std::min<size_t>(pack_length(), max_length);
1821   memcpy(to, from, length);
1822   return to + length;
1823 }
1824 
1825 /**
1826    Unpack a field from row data.
1827 
1828    This method is used to unpack a field from a master whose size of
1829    the field is less than that of the slave.
1830 
1831    The <code>param_data</code> parameter is a two-byte integer (stored
1832    in the least significant 16 bits of the unsigned integer) usually
1833    consisting of two parts: the real type in the most significant byte
1834    and a original pack length in the least significant byte.
1835 
1836    The exact layout of the <code>param_data</code> field is given by
1837    the <code>Table_map_log_event::save_field_metadata()</code>.
1838 
1839    This is the default method for unpacking a field. It just copies
1840    the memory block in byte order (of original pack length bytes or
1841    length of field, whichever is smaller).
1842 
1843    @param   to         Destination of the data
1844    @param   from       Source of the data
1845    @param   param_data Real type and original pack length of the field
1846                        data
1847 
1848    @return  New pointer into memory based on from + length of the data
1849 */
unpack(uchar * to,const uchar * from,uint param_data)1850 const uchar *Field::unpack(uchar *to, const uchar *from, uint param_data) {
1851   uint length = pack_length();
1852   int from_type = 0;
1853   /*
1854     If from length is > 255, it has encoded data in the upper bits. Need
1855     to mask it out.
1856   */
1857   if (param_data > 255) {
1858     from_type = (param_data & 0xff00) >> 8U;  // real_type.
1859     param_data = param_data & 0x00ff;         // length.
1860   }
1861 
1862   if ((param_data == 0) || (length == param_data) ||
1863       (from_type != real_type())) {
1864     memcpy(to, from, length);
1865     return from + length;
1866   }
1867 
1868   uint len = (param_data && (param_data < length)) ? param_data : length;
1869 
1870   memcpy(to, from, param_data > length ? length : len);
1871   return from + len;
1872 }
1873 
1874 /**
1875   Appends the UNSIGNED and ZEROFILL attributes to a String if a Field_num has
1876   these attributes.
1877 
1878   @param field the field with the attributes to append
1879   @param[in,out] res the String to append to
1880 */
append_zerofill_and_unsigned(const Field_num * field,String * res)1881 static void append_zerofill_and_unsigned(const Field_num *field, String *res) {
1882   if (field->is_unsigned()) res->append(STRING_WITH_LEN(" unsigned"));
1883   if (field->zerofill) res->append(STRING_WITH_LEN(" zerofill"));
1884 }
1885 
1886 /// Writes an integer type specification to a string.
integer_sql_type(const Field_num * field,const char * type_name,String * res)1887 static void integer_sql_type(const Field_num *field, const char *type_name,
1888                              String *res) {
1889   res->length(0);
1890   res->append(type_name);
1891   if (field->zerofill) res->append_parenthesized(field->field_length);
1892   append_zerofill_and_unsigned(field, res);
1893 }
1894 
make_send_field(Send_field * field) const1895 void Field::make_send_field(Send_field *field) const {
1896   if (orig_table && orig_table->s->db.str && *orig_table->s->db.str) {
1897     field->db_name = orig_table->s->db.str;
1898     if (orig_table->pos_in_table_list &&
1899         orig_table->pos_in_table_list->schema_table)
1900       field->org_table_name =
1901           (orig_table->pos_in_table_list->schema_table->table_name);
1902     else
1903       field->org_table_name = orig_table->s->table_name.str;
1904   } else
1905     field->org_table_name = field->db_name = "";
1906   if (orig_table && orig_table->alias) {
1907     field->table_name = orig_table->alias;
1908     field->org_col_name = field_name;
1909   } else {
1910     field->table_name = "";
1911     field->org_col_name = "";
1912   }
1913   field->col_name = field_name;
1914   field->charsetnr = charset()->number;
1915   field->length = field_length;
1916   field->type = type();
1917   field->flags = all_flags();
1918   if (table->is_nullable()) field->flags &= ~NOT_NULL_FLAG;
1919   field->decimals = decimals();
1920   field->field = false;
1921 }
1922 
1923 /**
1924   Conversion from decimal to longlong. Checks overflow and returns
1925   correct value (min/max) in case of overflow.
1926 
1927   @param val             value to be converted
1928   @param unsigned_flag   type of integer to which we convert val
1929   @param has_overflow    true if there is overflow
1930 
1931   @return
1932     value converted from val
1933 */
convert_decimal2longlong(const my_decimal * val,bool unsigned_flag,bool * has_overflow)1934 longlong Field::convert_decimal2longlong(const my_decimal *val,
1935                                          bool unsigned_flag,
1936                                          bool *has_overflow) {
1937   if (unsigned_flag && val->sign()) {
1938     // Converting a signed decimal to unsigned int
1939     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
1940     *has_overflow = true;
1941     return 0;
1942   }
1943 
1944   longlong val_ll;
1945   int conversion_error =
1946       my_decimal2int(E_DEC_ERROR & ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED, val,
1947                      unsigned_flag, &val_ll);
1948 
1949   if (warn_if_overflow(conversion_error)) {
1950     *has_overflow = true;
1951     if (unsigned_flag) return ULLONG_MAX;
1952 
1953     return (val->sign() ? LLONG_MIN : LLONG_MAX);
1954   }
1955 
1956   return val_ll;
1957 }
1958 
1959 /**
1960   Storing decimal in integer fields.
1961 
1962   @param val       value for storing
1963 
1964   @note
1965     This method is used by all integer fields, real/decimal redefine it
1966 
1967   @retval TYPE_OK   Storage of value went fine without warnings or errors
1968   @retval !TYPE_OK  Warning/error as indicated by type_conversion_status enum
1969                     value
1970 */
store_decimal(const my_decimal * val)1971 type_conversion_status Field_num::store_decimal(const my_decimal *val) {
1972   ASSERT_COLUMN_MARKED_FOR_WRITE;
1973   bool has_overflow = false;
1974   longlong i = convert_decimal2longlong(val, is_unsigned(), &has_overflow);
1975   const type_conversion_status res = store(i, is_unsigned());
1976   return has_overflow ? TYPE_WARN_OUT_OF_RANGE : res;
1977 }
1978 
1979 /**
1980   Return decimal value of integer field.
1981 
1982   @param decimal_value     buffer for storing decimal value
1983 
1984   @note
1985     This method is used by all integer fields, real/decimal redefine it.
1986     All longlong values fit in our decimal buffer which cal store 8*9=72
1987     digits of integer number
1988 
1989   @return
1990     pointer to decimal buffer with value of field
1991 */
1992 
val_decimal(my_decimal * decimal_value) const1993 my_decimal *Field_num::val_decimal(my_decimal *decimal_value) const {
1994   ASSERT_COLUMN_MARKED_FOR_READ;
1995   DBUG_ASSERT(result_type() == INT_RESULT);
1996   longlong nr = val_int();
1997   int2my_decimal(E_DEC_FATAL_ERROR, nr, is_unsigned(), decimal_value);
1998   return decimal_value;
1999 }
2000 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const2001 bool Field_num::get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const {
2002   DBUG_ASSERT(result_type() == INT_RESULT);
2003   return my_longlong_to_datetime_with_warn(val_int(), ltime, fuzzydate);
2004 }
2005 
get_time(MYSQL_TIME * ltime) const2006 bool Field_num::get_time(MYSQL_TIME *ltime) const {
2007   DBUG_ASSERT(result_type() == INT_RESULT);
2008   return my_longlong_to_time_with_warn(val_int(), ltime);
2009 }
2010 
Field_str(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,const CHARSET_INFO * charset_arg)2011 Field_str::Field_str(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2012                      uchar null_bit_arg, uchar auto_flags_arg,
2013                      const char *field_name_arg,
2014                      const CHARSET_INFO *charset_arg)
2015     : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2016             field_name_arg) {
2017   field_charset = charset_arg;
2018   if (charset_arg->state & MY_CS_BINSORT) set_flag(BINARY_FLAG);
2019   field_derivation = DERIVATION_IMPLICIT;
2020   char_length_cache = char_length();
2021 }
2022 
make_send_field(Send_field * field) const2023 void Field_str::make_send_field(Send_field *field) const {
2024   Field::make_send_field(field);
2025   field->decimals = 0;
2026 }
2027 
2028 /**
2029   Decimal representation of Field_str.
2030 
2031   @param d         value for storing
2032 
2033   @note
2034     Field_str is the base class for fields implemeting
2035     [VAR]CHAR, VAR[BINARY], BLOB/TEXT, GEOMETRY, JSON.
2036     String value should be converted to floating point value according
2037     our rules, so we use double to store value of decimal in string.
2038 
2039   @todo
2040     use decimal2string?
2041 
2042   @retval
2043     0     OK
2044   @retval
2045     !=0  error
2046 */
2047 
store_decimal(const my_decimal * d)2048 type_conversion_status Field_str::store_decimal(const my_decimal *d) {
2049   ASSERT_COLUMN_MARKED_FOR_WRITE;
2050   double val;
2051   /* TODO: use decimal2string? */
2052   int err = my_decimal2double(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, d, &val);
2053   warn_if_overflow(err);
2054   const type_conversion_status res = store(val);
2055 
2056   return (err != E_DEC_OK) ? decimal_err_to_type_conv_status(err) : res;
2057 }
2058 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const2059 bool Field::get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const {
2060   char buff[MAX_DATE_STRING_REP_LENGTH];
2061   String tmp(buff, sizeof(buff), &my_charset_bin), *res;
2062   return !(res = val_str(&tmp)) ||
2063          str_to_datetime_with_warn(res, ltime, fuzzydate);
2064 }
2065 
get_time(MYSQL_TIME * ltime) const2066 bool Field::get_time(MYSQL_TIME *ltime) const {
2067   char buff[MAX_DATE_STRING_REP_LENGTH];
2068   String tmp(buff, sizeof(buff), &my_charset_bin), *res;
2069   return !(res = val_str(&tmp)) || str_to_time_with_warn(res, ltime);
2070 }
2071 
get_timestamp(struct timeval * tm,int * warnings) const2072 bool Field::get_timestamp(struct timeval *tm, int *warnings) const {
2073   MYSQL_TIME ltime;
2074   DBUG_ASSERT(!is_null());
2075   return get_date(&ltime, TIME_FUZZY_DATE) ||
2076          datetime_to_timeval(current_thd, &ltime, tm, warnings);
2077 }
2078 
2079 /**
2080   This is called when storing a date in a string.
2081 
2082   @note
2083     Needs to be changed if/when we want to support different time formats.
2084 */
2085 
store_time(MYSQL_TIME * ltime,uint8 dec_arg)2086 type_conversion_status Field::store_time(MYSQL_TIME *ltime, uint8 dec_arg) {
2087   ASSERT_COLUMN_MARKED_FOR_WRITE;
2088   char buff[MAX_DATE_STRING_REP_LENGTH];
2089   uint length = my_TIME_to_str(*ltime, buff,
2090                                std::min(dec_arg, uint8{DATETIME_MAX_DECIMALS}));
2091   /* Avoid conversion when field character set is ASCII compatible */
2092   return store(
2093       buff, length,
2094       (charset()->state & MY_CS_NONASCII) ? &my_charset_latin1 : charset());
2095 }
2096 
optimize_range(uint idx,uint part) const2097 bool Field::optimize_range(uint idx, uint part) const {
2098   return table->file->index_flags(idx, part, true) & HA_READ_RANGE;
2099 }
2100 
new_field(MEM_ROOT * root,TABLE * new_table) const2101 Field *Field::new_field(MEM_ROOT *root, TABLE *new_table) const {
2102   Field *tmp = clone(root);
2103   if (tmp == nullptr) return nullptr;
2104 
2105   if (tmp->table && tmp->table->is_nullable()) tmp->clear_flag(NOT_NULL_FLAG);
2106   tmp->table = new_table;
2107   tmp->key_start.init(0);
2108   tmp->part_of_key.init(0);
2109   tmp->part_of_prefixkey.init(0);
2110   tmp->part_of_sortkey.init(0);
2111   tmp->m_indexed = false;
2112   /*
2113     todo: We should never alter auto_flags after an object is constructed,
2114     and the member should be made const. But a lot of code depends upon this
2115     hack, and some flags are completely unrelated so we can never be quite
2116     sure which parts of the server will break.
2117   */
2118   tmp->auto_flags = Field::NONE;
2119   tmp->flags &= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | ZEROFILL_FLAG |
2120                  BINARY_FLAG | ENUM_FLAG | SET_FLAG | NOT_SECONDARY_FLAG);
2121   return tmp;
2122 }
2123 
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uchar * new_null_ptr,uint new_null_bit) const2124 Field *Field::new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
2125                             uchar *new_null_ptr, uint new_null_bit) const {
2126   Field *tmp = new_field(root, new_table);
2127   if (tmp != nullptr) {
2128     tmp->ptr = new_ptr;
2129     tmp->m_null_ptr = new_null_ptr;
2130     tmp->null_bit = new_null_bit;
2131   }
2132   return tmp;
2133 }
2134 
evaluate_insert_default_function()2135 void Field::evaluate_insert_default_function() {
2136   if (has_insert_default_datetime_value_expression())
2137     Item_func_now_local::store_in(this);
2138   // evaluate and store the values generated by default expression
2139   else if (has_insert_default_general_value_expression())
2140     m_default_val_expr->expr_item->save_in_field(this, false);
2141 }
2142 
evaluate_update_default_function()2143 void Field::evaluate_update_default_function() {
2144   if (has_update_default_datetime_value_expression())
2145     Item_func_now_local::store_in(this);
2146 }
2147 
2148 /****************************************************************************
2149   Field_null, a field that always return NULL
2150 ****************************************************************************/
2151 
sql_type(String & res) const2152 void Field_null::sql_type(String &res) const {
2153   res.set_ascii(STRING_WITH_LEN("null"));
2154 }
2155 
2156 /****************************************************************************
2157   Functions for the Field_decimal class
2158   This is an number stored as a pre-space (or pre-zero) string
2159 ****************************************************************************/
2160 
overflow(bool negative)2161 void Field_decimal::overflow(bool negative) {
2162   uint len = field_length;
2163   uchar *to = ptr, filler = '9';
2164 
2165   set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
2166   if (negative) {
2167     if (!is_unsigned()) {
2168       /* Put - sign as a first digit so we'll have -999..999 or 999..999 */
2169       *to++ = '-';
2170       len--;
2171     } else {
2172       filler = '0';  // Fill up with 0
2173       if (!zerofill) {
2174         /*
2175           Handle unsigned integer without zerofill, in which case
2176           the number should be of format '   0' or '   0.000'
2177         */
2178         uint whole_part = field_length - (dec ? dec + 2 : 1);
2179         // Fill with spaces up to the first digit
2180         memset(to, ' ', whole_part);
2181         to += whole_part;
2182         len -= whole_part;
2183         // The main code will also handle the 0 before the decimal point
2184       }
2185     }
2186   }
2187   memset(to, filler, len);
2188   if (dec) ptr[field_length - dec - 1] = '.';
2189   return;
2190 }
2191 
store(const char * from_arg,size_t len,const CHARSET_INFO * cs)2192 type_conversion_status Field_decimal::store(const char *from_arg, size_t len,
2193                                             const CHARSET_INFO *cs) {
2194   ASSERT_COLUMN_MARKED_FOR_WRITE;
2195   char buff[STRING_BUFFER_USUAL_SIZE];
2196   String tmp(buff, sizeof(buff), &my_charset_bin);
2197   const uchar *from = pointer_cast<const uchar *>(from_arg);
2198 
2199   /* Convert character set if the old one is multi uchar */
2200   if (cs->mbmaxlen > 1) {
2201     uint dummy_errors;
2202     tmp.copy(from_arg, len, cs, &my_charset_bin, &dummy_errors);
2203     from = (uchar *)tmp.ptr();
2204     len = tmp.length();
2205   }
2206 
2207   const uchar *end = from + len;
2208   /* The pointer where the field value starts (i.e., "where to write") */
2209   uchar *to = ptr;
2210   uint tmp_dec, tmp_uint;
2211   /*
2212     The sign of the number : will be 0 (means positive but sign not
2213     specified), '+' or '-'
2214   */
2215   uchar sign_char = 0;
2216   /* The pointers where prezeros start and stop */
2217   const uchar *pre_zeros_from, *pre_zeros_end;
2218   /* The pointers where digits at the left of '.' start and stop */
2219   const uchar *int_digits_from, *int_digits_end;
2220   /* The pointers where digits at the right of '.' start and stop */
2221   const uchar *frac_digits_from, *frac_digits_end;
2222   /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
2223   char expo_sign_char = 0;
2224   uint exponent = 0;  // value of the exponent
2225   /*
2226     Pointers used when digits move from the left of the '.' to the
2227     right of the '.' (explained below)
2228   */
2229   const uchar *int_digits_tail_from = nullptr;
2230   /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
2231   uint int_digits_added_zeros = 0;
2232   /*
2233     Pointer used when digits move from the right of the '.' to the left
2234     of the '.'
2235   */
2236   const uchar *frac_digits_head_end = nullptr;
2237   /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
2238   uint frac_digits_added_zeros = 0;
2239   uchar *pos, *tmp_left_pos, *tmp_right_pos;
2240   /* Pointers that are used as limits (begin and end of the field buffer) */
2241   uchar *left_wall, *right_wall;
2242   uchar tmp_char;
2243   /*
2244     To remember if table->in_use->num_truncated_fields has already
2245     been incremented, to do that only once
2246   */
2247   bool has_incremented_num_truncated_fields = false;
2248 
2249   /*
2250     There are three steps in this function :
2251     - parse the input string
2252     - modify the position of digits around the decimal dot '.'
2253       according to the exponent value (if specified)
2254     - write the formatted number
2255   */
2256 
2257   if ((tmp_dec = dec)) tmp_dec++;
2258 
2259   /* skip pre-space */
2260   while (from != end && my_isspace(&my_charset_bin, *from)) from++;
2261   if (from == end) {
2262     set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
2263     has_incremented_num_truncated_fields = true;
2264   } else if (*from == '+' || *from == '-')  // Found some sign ?
2265   {
2266     sign_char = *from++;
2267     /*
2268       We allow "+" for unsigned decimal unless defined different
2269       Both options allowed as one may wish not to have "+" for unsigned numbers
2270       because of data processing issues
2271     */
2272     if (is_unsigned()) {
2273       if (sign_char == '-') {
2274         Field_decimal::overflow(true);
2275         return TYPE_WARN_OUT_OF_RANGE;
2276       }
2277     }
2278   }
2279 
2280   pre_zeros_from = from;
2281   for (; from != end && *from == '0'; from++)
2282     ;  // Read prezeros
2283   pre_zeros_end = int_digits_from = from;
2284   /* Read non zero digits at the left of '.'*/
2285   for (; from != end && my_isdigit(&my_charset_bin, *from); from++)
2286     ;
2287   int_digits_end = from;
2288   if (from != end && *from == '.')  // Some '.' ?
2289     from++;
2290   frac_digits_from = from;
2291   /* Read digits at the right of '.' */
2292   for (; from != end && my_isdigit(&my_charset_bin, *from); from++)
2293     ;
2294   frac_digits_end = from;
2295   // Some exponentiation symbol ?
2296   if (from != end && (*from == 'e' || *from == 'E')) {
2297     from++;
2298     if (from != end && (*from == '+' || *from == '-'))  // Some exponent sign ?
2299       expo_sign_char = *from++;
2300     else
2301       expo_sign_char = '+';
2302     /*
2303       Read digits of the exponent and compute its value.  We must care about
2304       'exponent' overflow, because as unsigned arithmetic is "modulo", big
2305       exponents will become small (e.g. 1e4294967296 will become 1e0, and the
2306       field will finally contain 1 instead of its max possible value).
2307     */
2308     for (; from != end && my_isdigit(&my_charset_bin, *from); from++) {
2309       exponent = 10 * exponent + (*from - '0');
2310       if (exponent > MAX_EXPONENT) break;
2311     }
2312   }
2313 
2314   /*
2315     We only have to generate warnings if check_for_truncated_fields is set.
2316     This is to avoid extra checks of the number when they are not needed.
2317     Even if this flag is not set, it's OK to increment warnings, if
2318     it makes the code easer to read.
2319   */
2320 
2321   if (table->in_use->check_for_truncated_fields) {
2322     // Skip end spaces
2323     for (; from != end && my_isspace(&my_charset_bin, *from); from++)
2324       ;
2325     if (from != end)  // If still something left, warn
2326     {
2327       set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
2328       has_incremented_num_truncated_fields = true;
2329     }
2330   }
2331 
2332   /*
2333     Now "move" digits around the decimal dot according to the exponent value,
2334     and add necessary zeros.
2335     Examples :
2336     - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
2337     - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
2338     between '.' and '1'
2339     - 1234.5E-3 : '234' moves at the right of '.'
2340     These moves are implemented with pointers which point at the begin
2341     and end of each moved segment. Examples :
2342     - 1234.5E-3 : before the code below is executed, the int_digits part is
2343     from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2344     below, the int_digits part is from '1' to '1', the frac_digits_head
2345     part is from '2' to '4', and the frac_digits part from '5' to '5'.
2346     - 1234.5E3 : before the code below is executed, the int_digits part is
2347     from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2348     below, the int_digits part is from '1' to '4', the int_digits_tail
2349     part is from '5' to '5', the frac_digits part is empty, and
2350     int_digits_added_zeros=2 (to make 1234500).
2351   */
2352 
2353   /*
2354      Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
2355      as int_digits_added_zeros<=exponent<4G and
2356      (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
2357      (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
2358   */
2359 
2360   if (!expo_sign_char)
2361     tmp_uint = tmp_dec + (uint)(int_digits_end - int_digits_from);
2362   else if (expo_sign_char == '-') {
2363     tmp_uint = min(exponent, (uint)(int_digits_end - int_digits_from));
2364     frac_digits_added_zeros = exponent - tmp_uint;
2365     int_digits_end -= tmp_uint;
2366     frac_digits_head_end = int_digits_end + tmp_uint;
2367     tmp_uint = tmp_dec + (uint)(int_digits_end - int_digits_from);
2368   } else  // (expo_sign_char=='+')
2369   {
2370     tmp_uint = min(exponent, (uint)(frac_digits_end - frac_digits_from));
2371     int_digits_added_zeros = exponent - tmp_uint;
2372     int_digits_tail_from = frac_digits_from;
2373     frac_digits_from = frac_digits_from + tmp_uint;
2374     /*
2375       We "eat" the heading zeros of the
2376       int_digits.int_digits_tail.int_digits_added_zeros concatenation
2377       (for example 0.003e3 must become 3 and not 0003)
2378     */
2379     if (int_digits_from == int_digits_end) {
2380       /*
2381         There was nothing in the int_digits part, so continue
2382         eating int_digits_tail zeros
2383       */
2384       for (; int_digits_tail_from != frac_digits_from &&
2385              *int_digits_tail_from == '0';
2386            int_digits_tail_from++)
2387         ;
2388       if (int_digits_tail_from == frac_digits_from) {
2389         // there were only zeros in int_digits_tail too
2390         int_digits_added_zeros = 0;
2391       }
2392     }
2393     tmp_uint = (uint)(tmp_dec + (int_digits_end - int_digits_from) +
2394                       (uint)(frac_digits_from - int_digits_tail_from) +
2395                       int_digits_added_zeros);
2396   }
2397 
2398   /*
2399     Now write the formated number
2400 
2401     First the digits of the int_% parts.
2402     Do we have enough room to write these digits ?
2403     If the sign is defined and '-', we need one position for it
2404   */
2405 
2406   if (field_length < tmp_uint + (int)(sign_char == '-')) {
2407     // too big number, change to max or min number
2408     Field_decimal::overflow(sign_char == '-');
2409     return TYPE_WARN_OUT_OF_RANGE;
2410   }
2411 
2412   /*
2413     Tmp_left_pos is the position where the leftmost digit of
2414     the int_% parts will be written
2415   */
2416   tmp_left_pos = pos = to + (uint)(field_length - tmp_uint);
2417 
2418   // Write all digits of the int_% parts
2419   while (int_digits_from != int_digits_end) *pos++ = *int_digits_from++;
2420 
2421   if (expo_sign_char == '+') {
2422     while (int_digits_tail_from != frac_digits_from)
2423       *pos++ = *int_digits_tail_from++;
2424     while (int_digits_added_zeros-- > 0) *pos++ = '0';
2425   }
2426   /*
2427     Note the position where the rightmost digit of the int_% parts has been
2428     written (this is to later check if the int_% parts contained nothing,
2429     meaning an extra 0 is needed).
2430   */
2431   tmp_right_pos = pos;
2432 
2433   /*
2434     Step back to the position of the leftmost digit of the int_% parts,
2435     to write sign and fill with zeros or blanks or prezeros.
2436   */
2437   pos = tmp_left_pos - 1;
2438   if (zerofill) {
2439     left_wall = to - 1;
2440     while (pos > left_wall)  // Fill with zeros
2441       *pos-- = '0';
2442   } else {
2443     left_wall = to + (sign_char != 0) - 1;
2444     if (!expo_sign_char)  // If exponent was specified, ignore prezeros
2445     {
2446       for (; pos > left_wall && pre_zeros_from != pre_zeros_end;
2447            pre_zeros_from++)
2448         *pos-- = '0';
2449     }
2450     if (pos == tmp_right_pos - 1)
2451       *pos-- = '0';  // no 0 has ever been written, so write one
2452     left_wall = to - 1;
2453     if (sign_char && pos != left_wall) {
2454       /* Write sign if possible (it is if sign is '-') */
2455       *pos-- = sign_char;
2456     }
2457     while (pos != left_wall) *pos-- = ' ';  // fill with blanks
2458   }
2459 
2460   /*
2461     Write digits of the frac_% parts ;
2462     Depending on table->in_use->count_cutted_fields, we may also want
2463     to know if some non-zero tail of these parts will
2464     be truncated (for example, 0.002->0.00 will generate a warning,
2465     while 0.000->0.00 will not)
2466     (and 0E1000000000 will not, while 1E-1000000000 will)
2467   */
2468 
2469   pos = to + (uint)(field_length - tmp_dec);  // Calculate post to '.'
2470   right_wall = to + field_length;
2471   if (pos != right_wall) *pos++ = '.';
2472 
2473   if (expo_sign_char == '-') {
2474     while (frac_digits_added_zeros-- > 0) {
2475       if (pos == right_wall) {
2476         if (table->in_use->check_for_truncated_fields &&
2477             !has_incremented_num_truncated_fields)
2478           break;  // Go on below to see if we lose non zero digits
2479         return TYPE_OK;
2480       }
2481       *pos++ = '0';
2482     }
2483     while (int_digits_end != frac_digits_head_end) {
2484       tmp_char = *int_digits_end++;
2485       if (pos == right_wall) {
2486         if (tmp_char != '0')  // Losing a non zero digit ?
2487         {
2488           if (!has_incremented_num_truncated_fields)
2489             set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
2490           return TYPE_OK;
2491         }
2492         continue;
2493       }
2494       *pos++ = tmp_char;
2495     }
2496   }
2497 
2498   for (; frac_digits_from != frac_digits_end;) {
2499     tmp_char = *frac_digits_from++;
2500     if (pos == right_wall) {
2501       if (tmp_char != '0')  // Losing a non zero digit ?
2502       {
2503         if (!has_incremented_num_truncated_fields) {
2504           /*
2505             This is a note, not a warning, as we don't want to abort
2506             when we cut decimals in strict mode
2507           */
2508           set_warning(Sql_condition::SL_NOTE, WARN_DATA_TRUNCATED, 1);
2509         }
2510         return TYPE_OK;
2511       }
2512       continue;
2513     }
2514     *pos++ = tmp_char;
2515   }
2516 
2517   while (pos != right_wall) *pos++ = '0';  // Fill with zeros at right of '.'
2518   return TYPE_OK;
2519 }
2520 
store(double nr)2521 type_conversion_status Field_decimal::store(double nr) {
2522   ASSERT_COLUMN_MARKED_FOR_WRITE;
2523   if (is_unsigned() && nr < 0) {
2524     overflow(true);
2525     return TYPE_WARN_OUT_OF_RANGE;
2526   }
2527 
2528   if (!std::isfinite(nr))  // Handle infinity as special case
2529   {
2530     overflow(nr < 0.0);
2531     return TYPE_WARN_OUT_OF_RANGE;
2532   }
2533 
2534   size_t i;
2535   size_t length;
2536   uchar fyllchar, *to;
2537   char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
2538 
2539   fyllchar = zerofill ? '0' : ' ';
2540   length = my_fcvt(nr, dec, buff, nullptr);
2541 
2542   if (length > field_length) {
2543     overflow(nr < 0.0);
2544     return TYPE_WARN_OUT_OF_RANGE;
2545   } else {
2546     to = ptr;
2547     for (i = field_length - length; i-- > 0;) *to++ = fyllchar;
2548     memcpy(to, buff, length);
2549     return TYPE_OK;
2550   }
2551 }
2552 
store(longlong nr,bool unsigned_val)2553 type_conversion_status Field_decimal::store(longlong nr, bool unsigned_val) {
2554   ASSERT_COLUMN_MARKED_FOR_WRITE;
2555   char buff[22];
2556   uint length, int_part;
2557   char fyllchar;
2558   uchar *to;
2559 
2560   if (nr < 0 && is_unsigned() && !unsigned_val) {
2561     overflow(true);
2562     return TYPE_WARN_OUT_OF_RANGE;
2563   }
2564   length = (uint)(longlong10_to_str(nr, buff, unsigned_val ? 10 : -10) - buff);
2565   int_part = field_length - (dec ? dec + 1 : 0);
2566 
2567   if (length > int_part) {
2568     overflow(!unsigned_val && nr < 0L); /* purecov: inspected */
2569     return TYPE_WARN_OUT_OF_RANGE;
2570   }
2571 
2572   fyllchar = zerofill ? '0' : ' ';
2573   to = ptr;
2574   for (uint i = int_part - length; i-- > 0;) *to++ = fyllchar;
2575   memcpy(to, buff, length);
2576   if (dec) {
2577     to[length] = '.';
2578     memset(to + length + 1, '0', dec);
2579   }
2580   return TYPE_OK;
2581 }
2582 
val_real() const2583 double Field_decimal::val_real() const {
2584   ASSERT_COLUMN_MARKED_FOR_READ;
2585   int not_used;
2586   const char *end_not_used;
2587   return my_strntod(&my_charset_bin, pointer_cast<const char *>(ptr),
2588                     field_length, &end_not_used, &not_used);
2589 }
2590 
val_int() const2591 longlong Field_decimal::val_int() const {
2592   ASSERT_COLUMN_MARKED_FOR_READ;
2593   int not_used;
2594   if (is_unsigned())
2595     return my_strntoull(&my_charset_bin, pointer_cast<const char *>(ptr),
2596                         field_length, 10, NULL, &not_used);
2597   return my_strntoll(&my_charset_bin, pointer_cast<const char *>(ptr),
2598                      field_length, 10, NULL, &not_used);
2599 }
2600 
val_str(String *,String * val_ptr) const2601 String *Field_decimal::val_str(String *, String *val_ptr) const {
2602   ASSERT_COLUMN_MARKED_FOR_READ;
2603   const uchar *str;
2604   size_t tmp_length;
2605 
2606   for (str = ptr; *str == ' '; str++)
2607     ;
2608   val_ptr->set_charset(&my_charset_numeric);
2609   tmp_length = (size_t)(str - ptr);
2610   if (field_length < tmp_length)  // Error in data
2611     val_ptr->length(0);
2612   else
2613     val_ptr->set_ascii(pointer_cast<const char *>(str),
2614                        field_length - tmp_length);
2615   return val_ptr;
2616 }
2617 
2618 /**
2619   Should be able to handle at least the following fixed decimal formats:
2620   5.00 , -1.0,  05,  -05, +5 with optional pre/end space
2621 */
2622 
cmp(const uchar * a_ptr,const uchar * b_ptr) const2623 int Field_decimal::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
2624   const uchar *end;
2625   int swap = 0;
2626   /* First remove prefixes '0', ' ', and '-' */
2627   for (end = a_ptr + field_length;
2628        a_ptr != end &&
2629        (*a_ptr == *b_ptr || ((my_isspace(&my_charset_bin, *a_ptr) ||
2630                               *a_ptr == '+' || *a_ptr == '0') &&
2631                              (my_isspace(&my_charset_bin, *b_ptr) ||
2632                               *b_ptr == '+' || *b_ptr == '0')));
2633        a_ptr++, b_ptr++) {
2634     if (*a_ptr == '-')  // If both numbers are negative
2635       swap = -1 ^ 1;    // Swap result
2636   }
2637   if (a_ptr == end) return 0;
2638   if (*a_ptr == '-') return -1;
2639   if (*b_ptr == '-') return 1;
2640 
2641   while (a_ptr != end) {
2642     if (*a_ptr++ != *b_ptr++)
2643       return swap ^ (a_ptr[-1] < b_ptr[-1] ? -1 : 1);  // compare digits
2644   }
2645   return 0;
2646 }
2647 
make_sort_key(uchar * to,size_t length) const2648 size_t Field_decimal::make_sort_key(uchar *to, size_t length) const {
2649   uchar *str, *end;
2650   for (str = ptr, end = ptr + length;
2651        str != end &&
2652        ((my_isspace(&my_charset_bin, *str) || *str == '+' || *str == '0'));
2653        str++)
2654     *to++ = ' ';
2655   if (str == end) return length; /* purecov: inspected */
2656 
2657   if (*str == '-') {
2658     *to++ = 1;  // Smaller than any number
2659     str++;
2660     while (str != end)
2661       if (my_isdigit(&my_charset_bin, *str))
2662         *to++ = (char)('9' - *str++);
2663       else
2664         *to++ = *str++;
2665   } else
2666     memcpy(to, str, (uint)(end - str));
2667   return length;
2668 }
2669 
sql_type(String & res) const2670 void Field_decimal::sql_type(String &res) const {
2671   const CHARSET_INFO *cs = res.charset();
2672   uint tmp = field_length;
2673   if (!is_unsigned()) tmp--;
2674   if (dec) tmp--;
2675   res.length(cs->cset->snprintf(cs, res.ptr(), res.alloced_length(),
2676                                 "decimal(%d,%d)", tmp, dec));
2677   append_zerofill_and_unsigned(this, &res);
2678 }
2679 
2680 /****************************************************************************
2681 ** Field_new_decimal
2682 ****************************************************************************/
2683 
Field_new_decimal(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2684 Field_new_decimal::Field_new_decimal(uchar *ptr_arg, uint32 len_arg,
2685                                      uchar *null_ptr_arg, uchar null_bit_arg,
2686                                      uchar auto_flags_arg,
2687                                      const char *field_name_arg, uint8 dec_arg,
2688                                      bool zero_arg, bool unsigned_arg)
2689     : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2690                 field_name_arg, dec_arg, zero_arg, unsigned_arg) {
2691   precision =
2692       std::min(my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg),
2693                uint(DECIMAL_MAX_PRECISION));
2694   DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
2695               (dec <= DECIMAL_MAX_SCALE));
2696   bin_size = my_decimal_get_binary_size(precision, dec);
2697 }
2698 
Field_new_decimal(uint32 len_arg,bool is_nullable_arg,const char * name,uint8 dec_arg,bool unsigned_arg)2699 Field_new_decimal::Field_new_decimal(uint32 len_arg, bool is_nullable_arg,
2700                                      const char *name, uint8 dec_arg,
2701                                      bool unsigned_arg)
2702     : Field_num(nullptr, len_arg,
2703                 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE, name,
2704                 dec_arg, false, unsigned_arg) {
2705   precision =
2706       std::min(my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg),
2707                uint(DECIMAL_MAX_PRECISION));
2708   DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
2709               (dec <= DECIMAL_MAX_SCALE));
2710   bin_size = my_decimal_get_binary_size(precision, dec);
2711 }
2712 
create_from_item(const Item * item)2713 Field *Field_new_decimal::create_from_item(const Item *item) {
2714   uint8 dec = item->decimals;
2715   uint8 intg = item->decimal_precision() - dec;
2716   uint32 len = item->max_char_length();
2717 
2718   DBUG_ASSERT(item->result_type() == DECIMAL_RESULT);
2719 
2720   /*
2721     Trying to put too many digits overall in a DECIMAL(prec,dec)
2722     will always throw a warning. We must limit dec to
2723     DECIMAL_MAX_SCALE however to prevent an assert() later.
2724   */
2725 
2726   if (dec > 0) {
2727     signed int overflow;
2728 
2729     dec = min<int>(dec, DECIMAL_MAX_SCALE);
2730 
2731     /*
2732       If the value still overflows the field with the corrected dec,
2733       we'll throw out decimals rather than integers. This is still
2734       bad and of course throws a truncation warning.
2735       +1: for decimal point
2736       */
2737 
2738     const int required_length =
2739         my_decimal_precision_to_length(intg + dec, dec, item->unsigned_flag);
2740 
2741     overflow = required_length - len;
2742 
2743     if (overflow > 0)
2744       dec = max(0, dec - overflow);  // too long, discard fract
2745     else
2746       /* Corrected value fits. */
2747       len = required_length;
2748   }
2749   return new (*THR_MALLOC) Field_new_decimal(
2750       len, item->maybe_null, item->item_name.ptr(), dec, item->unsigned_flag);
2751 }
2752 
reset()2753 type_conversion_status Field_new_decimal::reset() {
2754   (void)my_decimal2binary(0, &decimal_zero, ptr, precision, dec);
2755   return TYPE_OK;
2756 }
2757 
2758 /**
2759   Generate max/min decimal value in case of overflow.
2760 
2761   @param decimal_value     buffer for value
2762   @param sign              sign of value which caused overflow
2763 */
2764 
set_value_on_overflow(my_decimal * decimal_value,bool sign) const2765 void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
2766                                               bool sign) const {
2767   DBUG_TRACE;
2768   max_my_decimal(decimal_value, precision, decimals());
2769   if (sign) {
2770     if (is_unsigned())
2771       my_decimal_set_zero(decimal_value);
2772     else
2773       decimal_value->sign(true);
2774   }
2775 }
2776 
2777 /**
2778   Store decimal value in the binary buffer.
2779 
2780   Checks if decimal_value fits into field size.
2781   If it does, stores the decimal in the buffer using binary format.
2782   Otherwise sets maximal number that can be stored in the field.
2783 
2784   @param decimal_value   my_decimal
2785 
2786   @retval
2787     0 ok
2788   @retval
2789     1 error
2790 */
store_value(const my_decimal * decimal_value)2791 type_conversion_status Field_new_decimal::store_value(
2792     const my_decimal *decimal_value) {
2793   ASSERT_COLUMN_MARKED_FOR_WRITE;
2794   type_conversion_status error = TYPE_OK;
2795   DBUG_TRACE;
2796 #ifndef DBUG_OFF
2797   {
2798     char dbug_buff[DECIMAL_MAX_STR_LENGTH + 2];
2799     DBUG_PRINT("enter",
2800                ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
2801   }
2802 #endif
2803 
2804   /* check that we do not try to write negative value in unsigned field */
2805   if (is_unsigned() && decimal_value->sign()) {
2806     DBUG_PRINT("info", ("unsigned overflow"));
2807     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
2808     error = TYPE_WARN_OUT_OF_RANGE;
2809     decimal_value = &decimal_zero;
2810   }
2811 #ifndef DBUG_OFF
2812   {
2813     char dbug_buff[DECIMAL_MAX_STR_LENGTH + 2];
2814     DBUG_PRINT("info",
2815                ("saving with precision %d  scale: %d  value %s", (int)precision,
2816                 (int)dec, dbug_decimal_as_string(dbug_buff, decimal_value)));
2817   }
2818 #endif
2819 
2820   int err = my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2821                               decimal_value, ptr, precision, dec);
2822   if (warn_if_overflow(err)) {
2823     my_decimal buff;
2824     DBUG_PRINT("info", ("overflow"));
2825     set_value_on_overflow(&buff, decimal_value->sign());
2826     my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
2827   }
2828   DBUG_EXECUTE("info", print_decimal_buff(decimal_value, ptr, bin_size););
2829   return (err != E_DEC_OK) ? decimal_err_to_type_conv_status(err) : error;
2830 }
2831 
store(const char * from,size_t length,const CHARSET_INFO * charset_arg)2832 type_conversion_status Field_new_decimal::store(
2833     const char *from, size_t length, const CHARSET_INFO *charset_arg) {
2834   ASSERT_COLUMN_MARKED_FOR_WRITE;
2835   my_decimal decimal_value;
2836   DBUG_TRACE;
2837 
2838   int err =
2839       str2my_decimal(E_DEC_FATAL_ERROR & ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
2840                      from, length, charset_arg, &decimal_value);
2841 
2842   if (err != 0 && !table->in_use->lex->is_ignore() &&
2843       table->in_use->is_strict_mode()) {
2844     ErrConvString errmsg(from, length, charset_arg);
2845     const Diagnostics_area *da = table->in_use->get_stmt_da();
2846     push_warning_printf(
2847         table->in_use, Sql_condition::SL_WARNING,
2848         ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
2849         ER_THD(table->in_use, ER_TRUNCATED_WRONG_VALUE_FOR_FIELD), "decimal",
2850         errmsg.ptr(), field_name, da->current_row_for_condition());
2851     return decimal_err_to_type_conv_status(err);
2852   }
2853 
2854   if (err != 0)
2855     set_decimal_warning(this, err, &decimal_value, from, length, charset_arg);
2856 
2857 #ifndef DBUG_OFF
2858   char dbug_buff[DECIMAL_MAX_STR_LENGTH + 2];
2859   DBUG_PRINT("enter",
2860              ("value: %s", dbug_decimal_as_string(dbug_buff, &decimal_value)));
2861 #endif
2862 
2863   type_conversion_status store_stat = store_value(&decimal_value);
2864   return err != 0 ? decimal_err_to_type_conv_status(err) : store_stat;
2865 }
2866 
store_internal_with_error_check(Field_new_decimal * field,int err,my_decimal * value)2867 type_conversion_status store_internal_with_error_check(Field_new_decimal *field,
2868                                                        int err,
2869                                                        my_decimal *value) {
2870   type_conversion_status stat = TYPE_OK;
2871   if (err == E_DEC_OVERFLOW) {
2872     field->set_value_on_overflow(value, value->sign());
2873     stat = TYPE_WARN_OUT_OF_RANGE;
2874   } else if (err == E_DEC_TRUNCATED) {
2875     stat = TYPE_NOTE_TRUNCATED;
2876   }
2877   uint cond_count = field->table->in_use->get_stmt_da()->cond_count();
2878   type_conversion_status store_stat = field->store_value(value);
2879   if (store_stat != TYPE_OK)
2880     return store_stat;
2881   else if (err != 0 &&
2882            (field->table->in_use->get_stmt_da()->cond_count() == cond_count)) {
2883     /* Only issue a warning if store_value doesn't issue an warning */
2884     field->warn_if_overflow(err);
2885   }
2886   return stat;
2887 }
2888 
2889 /**
2890   @todo
2891   Fix following when double2my_decimal when double2decimal
2892   will return E_DEC_TRUNCATED always correctly
2893 */
2894 
store(double nr)2895 type_conversion_status Field_new_decimal::store(double nr) {
2896   ASSERT_COLUMN_MARKED_FOR_WRITE;
2897   DBUG_TRACE;
2898   my_decimal decimal_value;
2899 
2900   int conv_err = double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
2901                                    &decimal_value);
2902   return store_internal_with_error_check(this, conv_err, &decimal_value);
2903 }
2904 
store(longlong nr,bool unsigned_val)2905 type_conversion_status Field_new_decimal::store(longlong nr,
2906                                                 bool unsigned_val) {
2907   ASSERT_COLUMN_MARKED_FOR_WRITE;
2908   DBUG_TRACE;
2909   my_decimal decimal_value;
2910   int conv_err = int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
2911                                 unsigned_val, &decimal_value);
2912   return store_internal_with_error_check(this, conv_err, &decimal_value);
2913 }
2914 
store_decimal(const my_decimal * decimal_value)2915 type_conversion_status Field_new_decimal::store_decimal(
2916     const my_decimal *decimal_value) {
2917   ASSERT_COLUMN_MARKED_FOR_WRITE;
2918   return store_value(decimal_value);
2919 }
2920 
store_time(MYSQL_TIME * ltime,uint8)2921 type_conversion_status Field_new_decimal::store_time(MYSQL_TIME *ltime, uint8) {
2922   my_decimal decimal_value;
2923   return store_value(date2my_decimal(ltime, &decimal_value));
2924 }
2925 
val_real() const2926 double Field_new_decimal::val_real() const {
2927   ASSERT_COLUMN_MARKED_FOR_READ;
2928   double dbl;
2929   my_decimal decimal_value;
2930   my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
2931   return dbl;
2932 }
2933 
val_int() const2934 longlong Field_new_decimal::val_int() const {
2935   ASSERT_COLUMN_MARKED_FOR_READ;
2936   longlong i;
2937   my_decimal decimal_value;
2938   my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), is_unsigned(),
2939                  &i);
2940   return i;
2941 }
2942 
val_decimal(my_decimal * decimal_value) const2943 my_decimal *Field_new_decimal::val_decimal(my_decimal *decimal_value) const {
2944   ASSERT_COLUMN_MARKED_FOR_READ;
2945   DBUG_TRACE;
2946   binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value, precision, dec,
2947                     m_keep_precision);
2948   DBUG_EXECUTE("info", print_decimal_buff(decimal_value, ptr, bin_size););
2949   return decimal_value;
2950 }
2951 
val_str(String * val_buffer,String *) const2952 String *Field_new_decimal::val_str(String *val_buffer, String *) const {
2953   ASSERT_COLUMN_MARKED_FOR_READ;
2954   my_decimal decimal_value;
2955   uint fixed_precision = zerofill ? precision : 0;
2956   my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2957                     fixed_precision, dec, val_buffer);
2958   val_buffer->set_charset(&my_charset_numeric);
2959   return val_buffer;
2960 }
2961 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const2962 bool Field_new_decimal::get_date(MYSQL_TIME *ltime,
2963                                  my_time_flags_t fuzzydate) const {
2964   my_decimal buf, *decimal_value = val_decimal(&buf);
2965   if (!decimal_value) {
2966     set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
2967     return true;
2968   }
2969   return my_decimal_to_datetime_with_warn(decimal_value, ltime, fuzzydate);
2970 }
2971 
get_time(MYSQL_TIME * ltime) const2972 bool Field_new_decimal::get_time(MYSQL_TIME *ltime) const {
2973   my_decimal buf, *decimal_value = val_decimal(&buf);
2974   if (!decimal_value) {
2975     set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
2976     return true;
2977   }
2978   return my_decimal_to_time_with_warn(decimal_value, ltime);
2979 }
2980 
cmp(const uchar * a,const uchar * b) const2981 int Field_new_decimal::cmp(const uchar *a, const uchar *b) const {
2982   return memcmp(a, b, bin_size);
2983 }
2984 
make_sort_key(uchar * buff,size_t length) const2985 size_t Field_new_decimal::make_sort_key(uchar *buff, size_t length) const {
2986   memcpy(buff, ptr, min(length, static_cast<size_t>(bin_size)));
2987   return length;
2988 }
2989 
sql_type(String & str) const2990 void Field_new_decimal::sql_type(String &str) const {
2991   const CHARSET_INFO *cs = str.charset();
2992   str.length(cs->cset->snprintf(cs, str.ptr(), str.alloced_length(),
2993                                 "decimal(%d,%d)", precision, (int)dec));
2994   append_zerofill_and_unsigned(this, &str);
2995 }
2996 
2997 /**
2998    Save the field metadata for new decimal fields.
2999 
3000    Saves the precision in the first byte and decimals() in the second
3001    byte of the field metadata array at index of *metadata_ptr and
3002    *(metadata_ptr + 1).
3003 
3004    @param   metadata_ptr   First byte of field metadata
3005 
3006    @returns number of bytes written to metadata_ptr
3007 */
do_save_field_metadata(uchar * metadata_ptr) const3008 int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr) const {
3009   *metadata_ptr = precision;
3010   *(metadata_ptr + 1) = decimals();
3011   return 2;
3012 }
3013 
3014 /**
3015    Returns the number of bytes field uses in row-based replication
3016    row packed size.
3017 
3018    This method is used in row-based replication to determine the number
3019    of bytes that the field consumes in the row record format. This is
3020    used to skip fields in the master that do not exist on the slave.
3021 
3022    @param   field_metadata   Encoded size in field metadata
3023 
3024    @returns The size of the field based on the field metadata.
3025 */
pack_length_from_metadata(uint field_metadata) const3026 uint Field_new_decimal::pack_length_from_metadata(uint field_metadata) const {
3027   uint const source_precision = (field_metadata >> 8U) & 0x00ff;
3028   uint const source_decimal = field_metadata & 0x00ff;
3029   uint const source_size =
3030       my_decimal_get_binary_size(source_precision, source_decimal);
3031   return (source_size);
3032 }
3033 
3034 /**
3035    Check to see if field size is compatible with destination.
3036 
3037    This method is used in row-based replication to verify that the slave's
3038    field size is less than or equal to the master's field size. The
3039    encoded field metadata (from the master or source) is decoded and compared
3040    to the size of this field (the slave or destination).
3041 
3042    @param   field_metadata   Encoded size in field metadata
3043    @param   order_var        Pointer to variable where the order
3044                              between the source field and this field
3045                              will be returned.
3046 
3047    @return @c true
3048 */
compatible_field_size(uint field_metadata,Relay_log_info *,uint16,int * order_var) const3049 bool Field_new_decimal::compatible_field_size(uint field_metadata,
3050                                               Relay_log_info *, uint16,
3051                                               int *order_var) const {
3052   uint const source_precision = (field_metadata >> 8U) & 0x00ff;
3053   uint const source_decimal = field_metadata & 0x00ff;
3054   int order = compare(source_precision, precision);
3055   *order_var = order != 0 ? order : compare(source_decimal, dec);
3056   return true;
3057 }
3058 
is_equal(const Create_field * new_field) const3059 uint Field_new_decimal::is_equal(const Create_field *new_field) const {
3060   return (new_field->sql_type == real_type()) &&
3061          (Overlaps(new_field->flags, UNSIGNED_FLAG) ==
3062           is_flag_set(UNSIGNED_FLAG)) &&
3063          (Overlaps(new_field->flags, AUTO_INCREMENT_FLAG) ==
3064           is_flag_set(AUTO_INCREMENT_FLAG)) &&
3065          (new_field->max_display_width_in_bytes() == max_display_length()) &&
3066          (new_field->decimals == dec);
3067 }
3068 
3069 /**
3070    Unpack a decimal field from row data.
3071 
3072    This method is used to unpack a decimal or numeric field from a master
3073    whose size of the field is less than that of the slave.
3074 
3075    @param   to         Destination of the data
3076    @param   from       Source of the data
3077    @param   param_data Precision (upper) and decimal (lower) values
3078 
3079    @return  New pointer into memory based on from + length of the data
3080 */
unpack(uchar * to,const uchar * from,uint param_data)3081 const uchar *Field_new_decimal::unpack(uchar *to, const uchar *from,
3082                                        uint param_data) {
3083   if (param_data == 0) return Field::unpack(to, from, param_data);
3084 
3085   uint from_precision = (param_data & 0xff00) >> 8U;
3086   uint from_decimal = param_data & 0x00ff;
3087   uint length = pack_length();
3088   uint from_pack_len = my_decimal_get_binary_size(from_precision, from_decimal);
3089   uint len = (param_data && (from_pack_len < length)) ? from_pack_len : length;
3090   if ((from_pack_len && (from_pack_len < length)) ||
3091       (from_precision < precision) || (from_decimal < decimals())) {
3092     /*
3093       If the master's data is smaller than the slave, we need to convert
3094       the binary to decimal then resize the decimal converting it back to
3095       a decimal and write that to the raw data buffer.
3096     */
3097     decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
3098     decimal_t dec_val;
3099     dec_val.len = from_precision;
3100     dec_val.buf = dec_buf;
3101     /*
3102       Note: bin2decimal does not change the length of the field. So it is
3103       just the first step the resizing operation. The second step does the
3104       resizing using the precision and decimals from the slave.
3105     */
3106     bin2decimal(from, &dec_val, from_precision, from_decimal);
3107     decimal2bin(&dec_val, to, precision, decimals());
3108   } else
3109     memcpy(to, from, len);  // Sizes are the same, just copy the data.
3110   return from + len;
3111 }
3112 
send_to_protocol(Protocol * protocol) const3113 bool Field_new_decimal::send_to_protocol(Protocol *protocol) const {
3114   if (is_null()) return protocol->store_null();
3115   my_decimal dec_value;
3116   return protocol->store_decimal(val_decimal(&dec_value),
3117                                  zerofill ? precision : 0, dec);
3118 }
3119 
3120 /****************************************************************************
3121 ** tiny int
3122 ****************************************************************************/
3123 
store(const char * from,size_t len,const CHARSET_INFO * cs)3124 type_conversion_status Field_tiny::store(const char *from, size_t len,
3125                                          const CHARSET_INFO *cs) {
3126   ASSERT_COLUMN_MARKED_FOR_WRITE;
3127   longlong rnd;
3128 
3129   const type_conversion_status error =
3130       get_int(cs, from, len, &rnd, 255, -128, 127);
3131   ptr[0] = is_unsigned() ? (char)(ulonglong)rnd : (char)rnd;
3132   return error;
3133 }
3134 
store(double nr)3135 type_conversion_status Field_tiny::store(double nr) {
3136   ASSERT_COLUMN_MARKED_FOR_WRITE;
3137   type_conversion_status error = TYPE_OK;
3138   nr = rint(nr);
3139   if (is_unsigned()) {
3140     if (nr < 0.0) {
3141       *ptr = 0;
3142       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3143       error = TYPE_WARN_OUT_OF_RANGE;
3144     } else if (nr > 255.0) {
3145       *ptr = (char)255;
3146       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3147       error = TYPE_WARN_OUT_OF_RANGE;
3148     } else
3149       *ptr = static_cast<unsigned char>(nr);
3150   } else {
3151     if (nr < -128.0) {
3152       *ptr = (char)-128;
3153       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3154       error = TYPE_WARN_OUT_OF_RANGE;
3155     } else if (nr > 127.0) {
3156       *ptr = 127;
3157       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3158       error = TYPE_WARN_OUT_OF_RANGE;
3159     } else
3160       *ptr = (char)(int)nr;
3161   }
3162   return error;
3163 }
3164 
store(longlong nr,bool unsigned_val)3165 type_conversion_status Field_tiny::store(longlong nr, bool unsigned_val) {
3166   ASSERT_COLUMN_MARKED_FOR_WRITE;
3167   type_conversion_status error = TYPE_OK;
3168 
3169   if (is_unsigned()) {
3170     if (nr < 0 && !unsigned_val) {
3171       *ptr = 0;
3172       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3173       error = TYPE_WARN_OUT_OF_RANGE;
3174     } else if ((ulonglong)nr > (ulonglong)255) {
3175       *ptr = (char)255;
3176       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3177       error = TYPE_WARN_OUT_OF_RANGE;
3178     } else
3179       *ptr = (char)nr;
3180   } else {
3181     if (nr < 0 && unsigned_val) nr = 256;  // Generate overflow
3182     if (nr < -128) {
3183       *ptr = (char)-128;
3184       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3185       error = TYPE_WARN_OUT_OF_RANGE;
3186     } else if (nr > 127) {
3187       *ptr = 127;
3188       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3189       error = TYPE_WARN_OUT_OF_RANGE;
3190     } else
3191       *ptr = (char)nr;
3192   }
3193   return error;
3194 }
3195 
val_real() const3196 double Field_tiny::val_real() const {
3197   ASSERT_COLUMN_MARKED_FOR_READ;
3198   int tmp = is_unsigned() ? (int)ptr[0] : (int)((signed char *)ptr)[0];
3199   return (double)tmp;
3200 }
3201 
val_int() const3202 longlong Field_tiny::val_int() const {
3203   ASSERT_COLUMN_MARKED_FOR_READ;
3204   int tmp = is_unsigned() ? (int)ptr[0] : (int)((signed char *)ptr)[0];
3205   return (longlong)tmp;
3206 }
3207 
val_str(String * val_buffer,String *) const3208 String *Field_tiny::val_str(String *val_buffer, String *) const {
3209   ASSERT_COLUMN_MARKED_FOR_READ;
3210   const CHARSET_INFO *cs = &my_charset_numeric;
3211   uint length;
3212   uint mlength = max(field_length + 1, 5 * cs->mbmaxlen);
3213   val_buffer->alloc(mlength);
3214   char *to = val_buffer->ptr();
3215 
3216   if (is_unsigned())
3217     length = (uint)cs->cset->long10_to_str(cs, to, mlength, 10, (long)*ptr);
3218   else
3219     length = (uint)cs->cset->long10_to_str(cs, to, mlength, -10,
3220                                            (long)*((signed char *)ptr));
3221 
3222   val_buffer->length(length);
3223   if (zerofill) prepend_zeros(val_buffer);
3224   val_buffer->set_charset(cs);
3225   return val_buffer;
3226 }
3227 
send_to_protocol(Protocol * protocol) const3228 bool Field_tiny::send_to_protocol(Protocol *protocol) const {
3229   if (is_null()) return protocol->store_null();
3230   return protocol->store_tiny(Field_tiny::val_int(),
3231                               zerofill ? field_length : 0);
3232 }
3233 
cmp(const uchar * a_ptr,const uchar * b_ptr) const3234 int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
3235   signed char a, b;
3236   a = (signed char)a_ptr[0];
3237   b = (signed char)b_ptr[0];
3238   if (is_unsigned())
3239     return ((uchar)a < (uchar)b) ? -1 : ((uchar)a > (uchar)b) ? 1 : 0;
3240   return (a < b) ? -1 : (a > b) ? 1 : 0;
3241 }
3242 
make_sort_key(uchar * to,size_t length MY_ATTRIBUTE ((unused))) const3243 size_t Field_tiny::make_sort_key(uchar *to,
3244                                  size_t length MY_ATTRIBUTE((unused))) const {
3245   DBUG_ASSERT(length == 1);
3246   if (is_unsigned())
3247     *to = *ptr;
3248   else
3249     to[0] = (char)(ptr[0] ^ (uchar)128); /* Revers signbit */
3250   return 1;
3251 }
3252 
sql_type(String & res) const3253 void Field_tiny::sql_type(String &res) const {
3254   if (field_length == 1 && !is_unsigned() && !zerofill) {
3255     // Print TINYINT(1) since connectors use this to indicate BOOLEAN
3256     res.length(0);
3257     res.append("tinyint(1)");
3258   } else {
3259     integer_sql_type(this, "tinyint", &res);
3260   }
3261 }
3262 
3263 /****************************************************************************
3264  Field type short int (2 byte)
3265 ****************************************************************************/
3266 
store(const char * from,size_t len,const CHARSET_INFO * cs)3267 type_conversion_status Field_short::store(const char *from, size_t len,
3268                                           const CHARSET_INFO *cs) {
3269   ASSERT_COLUMN_MARKED_FOR_WRITE;
3270   int store_tmp;
3271   longlong rnd;
3272 
3273   const type_conversion_status error =
3274       get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
3275   store_tmp = is_unsigned() ? (int)(ulonglong)rnd : (int)rnd;
3276   if (table->s->db_low_byte_first)
3277     int2store(ptr, store_tmp);
3278   else
3279     shortstore(ptr, (short)store_tmp);
3280   return error;
3281 }
3282 
store(double nr)3283 type_conversion_status Field_short::store(double nr) {
3284   ASSERT_COLUMN_MARKED_FOR_WRITE;
3285   type_conversion_status error = TYPE_OK;
3286   int16 res;
3287   nr = rint(nr);
3288   if (is_unsigned()) {
3289     if (nr < 0) {
3290       res = 0;
3291       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3292       error = TYPE_WARN_OUT_OF_RANGE;
3293     } else if (nr > (double)UINT_MAX16) {
3294       res = (int16)UINT_MAX16;
3295       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3296       error = TYPE_WARN_OUT_OF_RANGE;
3297     } else
3298       res = (int16)(uint16)nr;
3299   } else {
3300     if (nr < (double)INT_MIN16) {
3301       res = INT_MIN16;
3302       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3303       error = TYPE_WARN_OUT_OF_RANGE;
3304     } else if (nr > (double)INT_MAX16) {
3305       res = INT_MAX16;
3306       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3307       error = TYPE_WARN_OUT_OF_RANGE;
3308     } else
3309       res = (int16)(int)nr;
3310   }
3311   if (table->s->db_low_byte_first)
3312     int2store(ptr, res);
3313   else
3314     shortstore(ptr, res);
3315   return error;
3316 }
3317 
store(longlong nr,bool unsigned_val)3318 type_conversion_status Field_short::store(longlong nr, bool unsigned_val) {
3319   ASSERT_COLUMN_MARKED_FOR_WRITE;
3320   type_conversion_status error = TYPE_OK;
3321   int16 res;
3322 
3323   if (is_unsigned()) {
3324     if (nr < 0L && !unsigned_val) {
3325       res = 0;
3326       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3327       error = TYPE_WARN_OUT_OF_RANGE;
3328     } else if ((ulonglong)nr > (ulonglong)UINT_MAX16) {
3329       res = (int16)UINT_MAX16;
3330       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3331       error = TYPE_WARN_OUT_OF_RANGE;
3332     } else
3333       res = (int16)(uint16)nr;
3334   } else {
3335     if (nr < 0 && unsigned_val) nr = UINT_MAX16 + 1;  // Generate overflow
3336 
3337     if (nr < INT_MIN16) {
3338       res = INT_MIN16;
3339       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3340       error = TYPE_WARN_OUT_OF_RANGE;
3341     } else if (nr > (longlong)INT_MAX16) {
3342       res = INT_MAX16;
3343       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3344       error = TYPE_WARN_OUT_OF_RANGE;
3345     } else
3346       res = (int16)nr;
3347   }
3348   if (table->s->db_low_byte_first)
3349     int2store(ptr, res);
3350   else
3351     shortstore(ptr, res);
3352   return error;
3353 }
3354 
val_real() const3355 double Field_short::val_real() const {
3356   ASSERT_COLUMN_MARKED_FOR_READ;
3357   short j;
3358   if (table->s->db_low_byte_first)
3359     j = sint2korr(ptr);
3360   else
3361     j = shortget(ptr);
3362   return is_unsigned() ? (double)(unsigned short)j : (double)j;
3363 }
3364 
val_int() const3365 longlong Field_short::val_int() const {
3366   ASSERT_COLUMN_MARKED_FOR_READ;
3367   short j;
3368   if (table->s->db_low_byte_first)
3369     j = sint2korr(ptr);
3370   else
3371     j = shortget(ptr);
3372   return is_unsigned() ? (longlong)(unsigned short)j : (longlong)j;
3373 }
3374 
val_str(String * val_buffer,String *) const3375 String *Field_short::val_str(String *val_buffer, String *) const {
3376   ASSERT_COLUMN_MARKED_FOR_READ;
3377   const CHARSET_INFO *cs = &my_charset_numeric;
3378   uint length;
3379   uint mlength = max(field_length + 1, 7 * cs->mbmaxlen);
3380   val_buffer->alloc(mlength);
3381   char *to = val_buffer->ptr();
3382   short j;
3383   if (table->s->db_low_byte_first)
3384     j = sint2korr(ptr);
3385   else
3386     j = shortget(ptr);
3387 
3388   if (is_unsigned())
3389     length =
3390         (uint)cs->cset->long10_to_str(cs, to, mlength, 10, (long)(uint16)j);
3391   else
3392     length = (uint)cs->cset->long10_to_str(cs, to, mlength, -10, (long)j);
3393   val_buffer->length(length);
3394   if (zerofill) prepend_zeros(val_buffer);
3395   val_buffer->set_charset(cs);
3396   return val_buffer;
3397 }
3398 
send_to_protocol(Protocol * protocol) const3399 bool Field_short::send_to_protocol(Protocol *protocol) const {
3400   if (is_null()) return protocol->store_null();
3401   return protocol->store_short(Field_short::val_int(),
3402                                zerofill ? field_length : 0);
3403 }
3404 
cmp(const uchar * a_ptr,const uchar * b_ptr) const3405 int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
3406   short a, b;
3407   if (table->s->db_low_byte_first) {
3408     a = sint2korr(a_ptr);
3409     b = sint2korr(b_ptr);
3410   } else {
3411     a = shortget(a_ptr);
3412     b = shortget(b_ptr);
3413   }
3414 
3415   if (is_unsigned())
3416     return ((unsigned short)a < (unsigned short)b)
3417                ? -1
3418                : ((unsigned short)a > (unsigned short)b) ? 1 : 0;
3419   return (a < b) ? -1 : (a > b) ? 1 : 0;
3420 }
3421 
make_sort_key(uchar * to,size_t length MY_ATTRIBUTE ((unused))) const3422 size_t Field_short::make_sort_key(uchar *to,
3423                                   size_t length MY_ATTRIBUTE((unused))) const {
3424   DBUG_ASSERT(length == 2);
3425 #ifdef WORDS_BIGENDIAN
3426   if (!table->s->db_low_byte_first) {
3427     if (is_unsigned())
3428       to[0] = ptr[0];
3429     else
3430       to[0] = (char)(ptr[0] ^ 128); /* Revers signbit */
3431     to[1] = ptr[1];
3432   } else
3433 #endif
3434   {
3435     if (is_unsigned())
3436       to[0] = ptr[1];
3437     else
3438       to[0] = (char)(ptr[1] ^ 128); /* Revers signbit */
3439     to[1] = ptr[0];
3440   }
3441   return 2;
3442 }
3443 
sql_type(String & res) const3444 void Field_short::sql_type(String &res) const {
3445   integer_sql_type(this, "smallint", &res);
3446 }
3447 
3448 /****************************************************************************
3449   Field type medium int (3 byte)
3450 ****************************************************************************/
3451 
store(const char * from,size_t len,const CHARSET_INFO * cs)3452 type_conversion_status Field_medium::store(const char *from, size_t len,
3453                                            const CHARSET_INFO *cs) {
3454   ASSERT_COLUMN_MARKED_FOR_WRITE;
3455   int store_tmp;
3456   longlong rnd;
3457 
3458   const type_conversion_status error =
3459       get_int(cs, from, len, &rnd, UINT_MAX24, INT_MIN24, INT_MAX24);
3460   store_tmp = is_unsigned() ? (int)(ulonglong)rnd : (int)rnd;
3461   int3store(ptr, store_tmp);
3462   return error;
3463 }
3464 
store(double nr)3465 type_conversion_status Field_medium::store(double nr) {
3466   ASSERT_COLUMN_MARKED_FOR_WRITE;
3467   type_conversion_status error = TYPE_OK;
3468   nr = rint(nr);
3469   if (is_unsigned()) {
3470     if (nr < 0) {
3471       int3store(ptr, 0);
3472       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3473       error = TYPE_WARN_OUT_OF_RANGE;
3474     } else if (nr >= (double)(long)(1L << 24)) {
3475       uint32 tmp = (uint32)(1L << 24) - 1L;
3476       int3store(ptr, tmp);
3477       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3478       error = TYPE_WARN_OUT_OF_RANGE;
3479     } else
3480       int3store(ptr, (uint32)nr);
3481   } else {
3482     if (nr < (double)INT_MIN24) {
3483       long tmp = (long)INT_MIN24;
3484       int3store(ptr, tmp);
3485       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3486       error = TYPE_WARN_OUT_OF_RANGE;
3487     } else if (nr > (double)INT_MAX24) {
3488       long tmp = (long)INT_MAX24;
3489       int3store(ptr, tmp);
3490       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3491       error = TYPE_WARN_OUT_OF_RANGE;
3492     } else
3493       int3store(ptr, (long)nr);
3494   }
3495   return error;
3496 }
3497 
store(longlong nr,bool unsigned_val)3498 type_conversion_status Field_medium::store(longlong nr, bool unsigned_val) {
3499   ASSERT_COLUMN_MARKED_FOR_WRITE;
3500   type_conversion_status error = TYPE_OK;
3501 
3502   if (is_unsigned()) {
3503     if (nr < 0 && !unsigned_val) {
3504       int3store(ptr, 0);
3505       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3506       error = TYPE_WARN_OUT_OF_RANGE;
3507     } else if ((ulonglong)nr >= (ulonglong)(long)(1L << 24)) {
3508       long tmp = (long)(1L << 24) - 1L;
3509       int3store(ptr, tmp);
3510       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3511       error = TYPE_WARN_OUT_OF_RANGE;
3512     } else
3513       int3store(ptr, (uint32)nr);
3514   } else {
3515     if (nr < 0 && unsigned_val)
3516       nr = (ulonglong)(long)(1L << 24);  // Generate overflow
3517 
3518     if (nr < (longlong)INT_MIN24) {
3519       long tmp = (long)INT_MIN24;
3520       int3store(ptr, tmp);
3521       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3522       error = TYPE_WARN_OUT_OF_RANGE;
3523     } else if (nr > (longlong)INT_MAX24) {
3524       long tmp = (long)INT_MAX24;
3525       int3store(ptr, tmp);
3526       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3527       error = TYPE_WARN_OUT_OF_RANGE;
3528     } else
3529       int3store(ptr, (long)nr);
3530   }
3531   return error;
3532 }
3533 
val_real() const3534 double Field_medium::val_real() const {
3535   ASSERT_COLUMN_MARKED_FOR_READ;
3536   long j = is_unsigned() ? (long)uint3korr(ptr) : sint3korr(ptr);
3537   return (double)j;
3538 }
3539 
val_int() const3540 longlong Field_medium::val_int() const {
3541   ASSERT_COLUMN_MARKED_FOR_READ;
3542   long j = is_unsigned() ? (long)uint3korr(ptr) : sint3korr(ptr);
3543   return (longlong)j;
3544 }
3545 
val_str(String * val_buffer,String *) const3546 String *Field_medium::val_str(String *val_buffer, String *) const {
3547   ASSERT_COLUMN_MARKED_FOR_READ;
3548   const CHARSET_INFO *cs = &my_charset_numeric;
3549   uint length;
3550   uint mlength = max(field_length + 1, 10 * cs->mbmaxlen);
3551   val_buffer->alloc(mlength);
3552   char *to = val_buffer->ptr();
3553   long j = is_unsigned() ? (long)uint3korr(ptr) : sint3korr(ptr);
3554 
3555   length = (uint)cs->cset->long10_to_str(cs, to, mlength, -10, j);
3556   val_buffer->length(length);
3557   if (zerofill) prepend_zeros(val_buffer); /* purecov: inspected */
3558   val_buffer->set_charset(cs);
3559   return val_buffer;
3560 }
3561 
send_to_protocol(Protocol * protocol) const3562 bool Field_medium::send_to_protocol(Protocol *protocol) const {
3563   ASSERT_COLUMN_MARKED_FOR_READ;
3564   if (is_null()) return protocol->store_null();
3565   return protocol->store_long(Field_medium::val_int(),
3566                               zerofill ? field_length : 0);
3567 }
3568 
cmp(const uchar * a_ptr,const uchar * b_ptr) const3569 int Field_medium::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
3570   long a, b;
3571   if (is_unsigned()) {
3572     a = uint3korr(a_ptr);
3573     b = uint3korr(b_ptr);
3574   } else {
3575     a = sint3korr(a_ptr);
3576     b = sint3korr(b_ptr);
3577   }
3578   return (a < b) ? -1 : (a > b) ? 1 : 0;
3579 }
3580 
make_sort_key(uchar * to,size_t length MY_ATTRIBUTE ((unused))) const3581 size_t Field_medium::make_sort_key(uchar *to,
3582                                    size_t length MY_ATTRIBUTE((unused))) const {
3583   DBUG_ASSERT(length == 3);
3584   if (is_unsigned())
3585     to[0] = ptr[2];
3586   else
3587     to[0] = (uchar)(ptr[2] ^ 128); /* Revers signbit */
3588   to[1] = ptr[1];
3589   to[2] = ptr[0];
3590   return 3;
3591 }
3592 
sql_type(String & res) const3593 void Field_medium::sql_type(String &res) const {
3594   integer_sql_type(this, "mediumint", &res);
3595 }
3596 
3597 /****************************************************************************
3598 ** long int
3599 ****************************************************************************/
3600 
store(const char * from,size_t len,const CHARSET_INFO * cs)3601 type_conversion_status Field_long::store(const char *from, size_t len,
3602                                          const CHARSET_INFO *cs) {
3603   ASSERT_COLUMN_MARKED_FOR_WRITE;
3604   long store_tmp;
3605   longlong rnd;
3606 
3607   const type_conversion_status error =
3608       get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
3609   store_tmp = is_unsigned() ? (long)(ulonglong)rnd : (long)rnd;
3610   if (table->s->db_low_byte_first)
3611     int4store(ptr, store_tmp);
3612   else
3613     longstore(ptr, store_tmp);
3614   return error;
3615 }
3616 
store(double nr)3617 type_conversion_status Field_long::store(double nr) {
3618   ASSERT_COLUMN_MARKED_FOR_WRITE;
3619   type_conversion_status error = TYPE_OK;
3620   int32 res;
3621   nr = rint(nr);
3622   if (is_unsigned()) {
3623     if (nr < 0) {
3624       res = 0;
3625       error = TYPE_WARN_OUT_OF_RANGE;
3626     } else if (nr > (double)UINT_MAX32) {
3627       res = UINT_MAX32;
3628       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3629       error = TYPE_WARN_OUT_OF_RANGE;
3630     } else
3631       res = (int32)(ulong)nr;
3632   } else {
3633     if (nr < (double)INT_MIN32) {
3634       res = (int32)INT_MIN32;
3635       error = TYPE_WARN_OUT_OF_RANGE;
3636     } else if (nr > (double)INT_MAX32) {
3637       res = (int32)INT_MAX32;
3638       error = TYPE_WARN_OUT_OF_RANGE;
3639     } else
3640       res = (int32)(longlong)nr;
3641   }
3642   if (error)
3643     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3644 
3645   if (table->s->db_low_byte_first)
3646     int4store(ptr, res);
3647   else
3648     longstore(ptr, res);
3649   return error;
3650 }
3651 
3652 /**
3653   Store a longlong in the field
3654 
3655   @param nr            the value to store
3656   @param unsigned_val  whether or not 'nr' should be interpreted as
3657                        signed or unsigned. E.g., if 'nr' has all bits
3658                        set it is interpreted as -1 if unsigned_val is
3659                        false and ULLONG_MAX if unsigned_val is true.
3660 */
store(longlong nr,bool unsigned_val)3661 type_conversion_status Field_long::store(longlong nr, bool unsigned_val) {
3662   ASSERT_COLUMN_MARKED_FOR_WRITE;
3663   type_conversion_status error = TYPE_OK;
3664   int32 res;
3665 
3666   if (is_unsigned()) {
3667     if (nr < 0 && !unsigned_val) {
3668       res = 0;
3669       error = TYPE_WARN_OUT_OF_RANGE;
3670     } else if ((ulonglong)nr >= (1LL << 32)) {
3671       res = (int32)(uint32)~0L;
3672       error = TYPE_WARN_OUT_OF_RANGE;
3673     } else
3674       res = (int32)(uint32)nr;
3675   } else {
3676     if (nr < 0 && unsigned_val) {
3677       nr = ((longlong)INT_MAX32) + 1;  // Generate overflow
3678       error = TYPE_WARN_OUT_OF_RANGE;
3679     }
3680     if (nr < (longlong)INT_MIN32) {
3681       res = (int32)INT_MIN32;
3682       error = TYPE_WARN_OUT_OF_RANGE;
3683     } else if (nr > (longlong)INT_MAX32) {
3684       res = (int32)INT_MAX32;
3685       error = TYPE_WARN_OUT_OF_RANGE;
3686     } else
3687       res = (int32)nr;
3688   }
3689   if (error)
3690     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3691 
3692   if (table->s->db_low_byte_first)
3693     int4store(ptr, res);
3694   else
3695     longstore(ptr, res);
3696   return error;
3697 }
3698 
val_real() const3699 double Field_long::val_real() const {
3700   ASSERT_COLUMN_MARKED_FOR_READ;
3701   int32 j;
3702   if (table->s->db_low_byte_first)
3703     j = sint4korr(ptr);
3704   else
3705     j = longget(ptr);
3706   return is_unsigned() ? (double)(uint32)j : (double)j;
3707 }
3708 
val_int() const3709 longlong Field_long::val_int() const {
3710   ASSERT_COLUMN_MARKED_FOR_READ;
3711   int32 j;
3712   /* See the comment in Field_long::store(long long) */
3713   DBUG_ASSERT(table->in_use == current_thd);
3714   if (table->s->db_low_byte_first)
3715     j = sint4korr(ptr);
3716   else
3717     j = longget(ptr);
3718   return is_unsigned() ? (longlong)(uint32)j : (longlong)j;
3719 }
3720 
val_str(String * val_buffer,String *) const3721 String *Field_long::val_str(String *val_buffer, String *) const {
3722   ASSERT_COLUMN_MARKED_FOR_READ;
3723   const CHARSET_INFO *cs = &my_charset_numeric;
3724   size_t length;
3725   uint mlength = max(field_length + 1, 12 * cs->mbmaxlen);
3726   val_buffer->alloc(mlength);
3727   char *to = val_buffer->ptr();
3728   int32 j;
3729   if (table->s->db_low_byte_first)
3730     j = sint4korr(ptr);
3731   else
3732     j = longget(ptr);
3733 
3734   if (is_unsigned())
3735     length = cs->cset->long10_to_str(cs, to, mlength, 10, (long)(uint32)j);
3736   else
3737     length = cs->cset->long10_to_str(cs, to, mlength, -10, (long)j);
3738   val_buffer->length(length);
3739   if (zerofill) prepend_zeros(val_buffer);
3740   val_buffer->set_charset(cs);
3741   return val_buffer;
3742 }
3743 
send_to_protocol(Protocol * protocol) const3744 bool Field_long::send_to_protocol(Protocol *protocol) const {
3745   ASSERT_COLUMN_MARKED_FOR_READ;
3746   if (is_null()) return protocol->store_null();
3747   return protocol->store_long(Field_long::val_int(),
3748                               zerofill ? field_length : 0);
3749 }
3750 
cmp(const uchar * a_ptr,const uchar * b_ptr) const3751 int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
3752   int32 a, b;
3753   if (table->s->db_low_byte_first) {
3754     a = sint4korr(a_ptr);
3755     b = sint4korr(b_ptr);
3756   } else {
3757     a = longget(a_ptr);
3758     b = longget(b_ptr);
3759   }
3760   if (is_unsigned())
3761     return ((uint32)a < (uint32)b) ? -1 : ((uint32)a > (uint32)b) ? 1 : 0;
3762   return (a < b) ? -1 : (a > b) ? 1 : 0;
3763 }
3764 
make_sort_key(uchar * to,size_t length MY_ATTRIBUTE ((unused))) const3765 size_t Field_long::make_sort_key(uchar *to,
3766                                  size_t length MY_ATTRIBUTE((unused))) const {
3767   DBUG_ASSERT(length == 4);
3768 #ifdef WORDS_BIGENDIAN
3769   if (!table->s->db_low_byte_first) {
3770     if (is_unsigned())
3771       to[0] = ptr[0];
3772     else
3773       to[0] = (char)(ptr[0] ^ 128); /* Reverse sign bit */
3774     to[1] = ptr[1];
3775     to[2] = ptr[2];
3776     to[3] = ptr[3];
3777   } else
3778 #endif
3779   {
3780     if (is_unsigned())
3781       to[0] = ptr[3];
3782     else
3783       to[0] = (char)(ptr[3] ^ 128); /* Reverse sign bit */
3784     to[1] = ptr[2];
3785     to[2] = ptr[1];
3786     to[3] = ptr[0];
3787   }
3788   return 4;
3789 }
3790 
sql_type(String & res) const3791 void Field_long::sql_type(String &res) const {
3792   integer_sql_type(this, "int", &res);
3793 }
3794 
3795 /****************************************************************************
3796  Field type longlong int (8 bytes)
3797 ****************************************************************************/
3798 
store(const char * from,size_t len,const CHARSET_INFO * cs)3799 type_conversion_status Field_longlong::store(const char *from, size_t len,
3800                                              const CHARSET_INFO *cs) {
3801   ASSERT_COLUMN_MARKED_FOR_WRITE;
3802   int conv_err = 0;
3803   type_conversion_status error = TYPE_OK;
3804   const char *end;
3805   ulonglong tmp;
3806 
3807   tmp = cs->cset->strntoull10rnd(cs, from, len, is_unsigned(), &end, &conv_err);
3808   if (conv_err == MY_ERRNO_ERANGE) {
3809     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3810     error = TYPE_WARN_OUT_OF_RANGE;
3811   } else if (table->in_use->check_for_truncated_fields &&
3812              check_int(cs, from, len, end, conv_err))
3813     error = TYPE_WARN_OUT_OF_RANGE;
3814   else
3815     error = TYPE_OK;
3816 
3817   if (table->s->db_low_byte_first)
3818     int8store(ptr, tmp);
3819   else
3820     longlongstore(ptr, tmp);
3821   return error;
3822 }
3823 
store(double nr)3824 type_conversion_status Field_longlong::store(double nr) {
3825   ASSERT_COLUMN_MARKED_FOR_WRITE;
3826   type_conversion_status error = TYPE_OK;
3827   longlong res;
3828 
3829   nr = rint(nr);
3830   if (is_unsigned()) {
3831     if (nr < 0) {
3832       res = 0;
3833       error = TYPE_WARN_OUT_OF_RANGE;
3834     } else if (nr >= (double)ULLONG_MAX) {
3835       res = ~(longlong)0;
3836       error = TYPE_WARN_OUT_OF_RANGE;
3837     } else
3838       res = (longlong)double2ulonglong(nr);
3839   } else {
3840     if (nr <= (double)LLONG_MIN) {
3841       res = LLONG_MIN;
3842       if (nr < (double)LLONG_MIN) error = TYPE_WARN_OUT_OF_RANGE;
3843     } else if (nr >= (double)(ulonglong)LLONG_MAX) {
3844       res = LLONG_MAX;
3845       if (nr > (double)LLONG_MAX) error = TYPE_WARN_OUT_OF_RANGE;
3846     } else
3847       res = (longlong)nr;
3848   }
3849   if (error)
3850     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3851 
3852   if (table->s->db_low_byte_first)
3853     int8store(ptr, res);
3854   else
3855     longlongstore(ptr, res);
3856   return error;
3857 }
3858 
store(longlong nr,bool unsigned_val)3859 type_conversion_status Field_longlong::store(longlong nr, bool unsigned_val) {
3860   ASSERT_COLUMN_MARKED_FOR_WRITE;
3861   type_conversion_status error = TYPE_OK;
3862 
3863   if (nr < 0)  // Only possible error
3864   {
3865     /*
3866       if field is unsigned and value is signed (< 0) or
3867       if field is signed and value is unsigned we have an overflow
3868     */
3869     if (is_unsigned() != unsigned_val) {
3870       nr = is_unsigned() ? (ulonglong)0 : (ulonglong)LLONG_MAX;
3871       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
3872       error = TYPE_WARN_OUT_OF_RANGE;
3873     }
3874   }
3875 
3876   if (table->s->db_low_byte_first)
3877     int8store(ptr, nr);
3878   else
3879     longlongstore(ptr, nr);
3880   return error;
3881 }
3882 
val_real() const3883 double Field_longlong::val_real() const {
3884   ASSERT_COLUMN_MARKED_FOR_READ;
3885   longlong j;
3886   if (table->s->db_low_byte_first)
3887     j = sint8korr(ptr);
3888   else
3889     j = longlongget(ptr);
3890   if (is_unsigned()) {
3891     return ulonglong2double(static_cast<ulonglong>(j));
3892   }
3893   return static_cast<double>(j);
3894 }
3895 
val_int() const3896 longlong Field_longlong::val_int() const {
3897   ASSERT_COLUMN_MARKED_FOR_READ;
3898   if (table->s->db_low_byte_first)
3899     return sint8korr(ptr);
3900   else
3901     return longlongget(ptr);
3902 }
3903 
val_str(String * val_buffer,String *) const3904 String *Field_longlong::val_str(String *val_buffer, String *) const {
3905   const CHARSET_INFO *cs = &my_charset_numeric;
3906   uint length;
3907   uint mlength = max(field_length + 1, 22 * cs->mbmaxlen);
3908   val_buffer->alloc(mlength);
3909   char *to = val_buffer->ptr();
3910   longlong j;
3911   if (table->s->db_low_byte_first)
3912     j = sint8korr(ptr);
3913   else
3914     j = longlongget(ptr);
3915 
3916   length = (uint)(cs->cset->longlong10_to_str)(cs, to, mlength,
3917                                                is_unsigned() ? 10 : -10, j);
3918   val_buffer->length(length);
3919   if (zerofill) prepend_zeros(val_buffer);
3920   val_buffer->set_charset(cs);
3921   return val_buffer;
3922 }
3923 
send_to_protocol(Protocol * protocol) const3924 bool Field_longlong::send_to_protocol(Protocol *protocol) const {
3925   ASSERT_COLUMN_MARKED_FOR_READ;
3926   if (is_null()) return protocol->store_null();
3927   return protocol->store_longlong(Field_longlong::val_int(), is_unsigned(),
3928                                   zerofill ? field_length : 0);
3929 }
3930 
cmp(const uchar * a_ptr,const uchar * b_ptr) const3931 int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
3932   longlong a, b;
3933   if (table->s->db_low_byte_first) {
3934     a = sint8korr(a_ptr);
3935     b = sint8korr(b_ptr);
3936   } else {
3937     a = longlongget(a_ptr);
3938     b = longlongget(b_ptr);
3939   }
3940   if (is_unsigned())
3941     return ((ulonglong)a < (ulonglong)b)
3942                ? -1
3943                : ((ulonglong)a > (ulonglong)b) ? 1 : 0;
3944   return (a < b) ? -1 : (a > b) ? 1 : 0;
3945 }
3946 
make_sort_key(uchar * to,size_t length) const3947 size_t Field_longlong::make_sort_key(uchar *to, size_t length) const {
3948   DBUG_ASSERT(length == PACK_LENGTH);
3949 #ifdef WORDS_BIGENDIAN
3950   if (table == NULL || !table->s->db_low_byte_first)
3951     copy_integer<true>(to, length, ptr, PACK_LENGTH, is_unsigned());
3952   else
3953 #endif
3954     copy_integer<false>(to, length, ptr, PACK_LENGTH, is_unsigned());
3955   return PACK_LENGTH;
3956 }
3957 
sql_type(String & res) const3958 void Field_longlong::sql_type(String &res) const {
3959   integer_sql_type(this, "bigint", &res);
3960 }
3961 
3962 /*
3963   Floating-point numbers
3964  */
3965 
pack(uchar * to,const uchar * from,size_t max_length) const3966 uchar *Field_real::pack(uchar *to, const uchar *from, size_t max_length) const {
3967   DBUG_TRACE;
3968 #ifdef WORDS_BIGENDIAN
3969   if (!table->s->db_low_byte_first) {
3970     size_t len = std::min<size_t>(pack_length(), max_length);
3971     for (size_t i = 0; i < len; ++i) {
3972       to[i] = from[pack_length() - i - 1];
3973     }
3974     return to + pack_length();
3975   } else
3976 #endif
3977     return Field::pack(to, from, max_length);
3978 }
3979 
unpack(uchar * to,const uchar * from,uint param_data)3980 const uchar *Field_real::unpack(uchar *to, const uchar *from, uint param_data) {
3981   DBUG_TRACE;
3982 #ifdef WORDS_BIGENDIAN
3983   if (!table->s->db_low_byte_first) {
3984     const uchar *dptr = from + pack_length();
3985     while (dptr-- > from) *to++ = *dptr;
3986     return from + pack_length();
3987   } else
3988 #endif
3989     return Field::unpack(to, from, param_data);
3990 }
3991 
store_time(MYSQL_TIME * ltime,uint8)3992 type_conversion_status Field_real::store_time(MYSQL_TIME *ltime, uint8) {
3993   double nr = TIME_to_double(*ltime);
3994   return store(ltime->neg ? -nr : nr);
3995 }
3996 
3997 /****************************************************************************
3998   single precision float
3999 ****************************************************************************/
4000 
store(const char * from,size_t len,const CHARSET_INFO * cs)4001 type_conversion_status Field_float::store(const char *from, size_t len,
4002                                           const CHARSET_INFO *cs) {
4003   int conv_error;
4004   type_conversion_status err = TYPE_OK;
4005   const char *end;
4006   double nr = my_strntod(cs, from, len, &end, &conv_error);
4007   if (conv_error || (!len || ((uint)(end - from) != len &&
4008                               table->in_use->check_for_truncated_fields))) {
4009     set_warning(Sql_condition::SL_WARNING,
4010                 (conv_error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED),
4011                 1);
4012     err = conv_error ? TYPE_WARN_OUT_OF_RANGE : TYPE_WARN_TRUNCATED;
4013   }
4014   Field_float::store(nr);
4015   return err;
4016 }
4017 
store(double nr)4018 type_conversion_status Field_float::store(double nr) {
4019   ASSERT_COLUMN_MARKED_FOR_WRITE;
4020   const type_conversion_status error =
4021       truncate(&nr, FLT_MAX) ? TYPE_WARN_OUT_OF_RANGE : TYPE_OK;
4022 
4023   float j = (float)nr;
4024 
4025   if (table->s->db_low_byte_first)
4026     float4store(ptr, j);
4027   else
4028     floatstore(ptr, j);
4029   return error;
4030 }
4031 
store(longlong nr,bool unsigned_val)4032 type_conversion_status Field_float::store(longlong nr, bool unsigned_val) {
4033   return Field_float::store(unsigned_val ? ulonglong2double((ulonglong)nr)
4034                                          : (double)nr);
4035 }
4036 
val_real() const4037 double Field_float::val_real() const {
4038   ASSERT_COLUMN_MARKED_FOR_READ;
4039   if (table->s->db_low_byte_first)
4040     return double{float4get(ptr)};
4041   else
4042     return double{floatget(ptr)};
4043 }
4044 
val_int() const4045 longlong Field_float::val_int() const {
4046   float j;
4047   if (table->s->db_low_byte_first)
4048     j = float4get(ptr);
4049   else
4050     j = floatget(ptr);
4051   return (longlong)rint(j);
4052 }
4053 
val_str(String * val_buffer,String *) const4054 String *Field_float::val_str(String *val_buffer, String *) const {
4055   ASSERT_COLUMN_MARKED_FOR_READ;
4056   DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4057   float nr;
4058   if (table && table->s->db_low_byte_first)
4059     nr = float4get(ptr);
4060   else
4061     nr = floatget(ptr);
4062 
4063   uint to_length = 70;
4064   if (val_buffer->alloc(to_length)) {
4065     my_error(ER_OUT_OF_RESOURCES, MYF(0));
4066     return val_buffer;
4067   }
4068 
4069   char *to = val_buffer->ptr();
4070   size_t len;
4071 
4072   if (dec >= DECIMAL_NOT_SPECIFIED)
4073     len = my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, nullptr);
4074   else {
4075     /*
4076       We are safe here because the buffer length is 70, and
4077       fabs(float) < 10^39, dec < DECIMAL_NOT_SPECIFIED. So the resulting string
4078       will be not longer than 69 chars + terminating '\0'.
4079     */
4080     len = my_fcvt(nr, dec, to, nullptr);
4081   }
4082   val_buffer->length((uint)len);
4083   if (zerofill) prepend_zeros(val_buffer);
4084   val_buffer->set_charset(&my_charset_numeric);
4085   return val_buffer;
4086 }
4087 
cmp(const uchar * a_ptr,const uchar * b_ptr) const4088 int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
4089   float a, b;
4090   if (table->s->db_low_byte_first) {
4091     a = float4get(a_ptr);
4092     b = float4get(b_ptr);
4093   } else {
4094     a = floatget(a_ptr);
4095     b = floatget(b_ptr);
4096   }
4097   return (a < b) ? -1 : (a > b) ? 1 : 0;
4098 }
4099 
make_sort_key(uchar * to,size_t length MY_ATTRIBUTE ((unused))) const4100 size_t Field_float::make_sort_key(uchar *to,
4101                                   size_t length MY_ATTRIBUTE((unused))) const {
4102   DBUG_ASSERT(length == sizeof(float));
4103   float nr;
4104   if (table->s->db_low_byte_first)
4105     nr = float4get(ptr);
4106   else
4107     nr = floatget(ptr);
4108 
4109   /*
4110     -0.0 and +0.0 compare identically, so make sure they use exactly the same
4111     bit pattern.
4112   */
4113   if (nr == 0.0f) nr = 0.0f;
4114 
4115   /*
4116     Positive floats sort exactly as ints; negative floats need
4117     bit flipping. The bit flipping sets the upper bit to 0
4118     unconditionally, so put 1 in there for positive numbers
4119     (so they sort later for our unsigned comparison).
4120     NOTE: This does not sort infinities or NaN correctly.
4121   */
4122   int32 nr_int;
4123   memcpy(&nr_int, &nr, sizeof(nr));
4124   nr_int = (nr_int ^ (nr_int >> 31)) | ((~nr_int) & 0x80000000);
4125   store32be(to, nr_int);
4126 
4127   return sizeof(float);
4128 }
4129 
send_to_protocol(Protocol * protocol) const4130 bool Field_float::send_to_protocol(Protocol *protocol) const {
4131   ASSERT_COLUMN_MARKED_FOR_READ;
4132   if (is_null()) return protocol->store_null();
4133   return protocol->store_float(static_cast<float>(Field_float::val_real()), dec,
4134                                zerofill ? field_length : 0);
4135 }
4136 
4137 /**
4138    Save the field metadata for float fields.
4139 
4140    Saves the pack length in the first byte.
4141 
4142    @param   metadata_ptr   First byte of field metadata
4143 
4144    @returns number of bytes written to metadata_ptr
4145 */
do_save_field_metadata(uchar * metadata_ptr) const4146 int Field_float::do_save_field_metadata(uchar *metadata_ptr) const {
4147   *metadata_ptr = pack_length();
4148   return 1;
4149 }
4150 
sql_type(String & res) const4151 void Field_float::sql_type(String &res) const {
4152   if (dec == DECIMAL_NOT_SPECIFIED) {
4153     res.set_ascii(STRING_WITH_LEN("float"));
4154   } else {
4155     const CHARSET_INFO *cs = res.charset();
4156     res.length(cs->cset->snprintf(cs, res.ptr(), res.alloced_length(),
4157                                   "float(%d,%d)", (int)field_length, dec));
4158   }
4159   append_zerofill_and_unsigned(this, &res);
4160 }
4161 
4162 /****************************************************************************
4163   double precision floating point numbers
4164 ****************************************************************************/
4165 
store(const char * from,size_t len,const CHARSET_INFO * cs)4166 type_conversion_status Field_double::store(const char *from, size_t len,
4167                                            const CHARSET_INFO *cs) {
4168   int conv_error;
4169   type_conversion_status error = TYPE_OK;
4170   const char *end;
4171   double nr = my_strntod(cs, from, len, &end, &conv_error);
4172   if (conv_error != 0 || len == 0 ||
4173       (((uint)(end - from) != len &&
4174         table->in_use->check_for_truncated_fields))) {
4175     set_warning(Sql_condition::SL_WARNING,
4176                 (conv_error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED),
4177                 1);
4178     error = conv_error ? TYPE_WARN_OUT_OF_RANGE : TYPE_WARN_TRUNCATED;
4179   }
4180   Field_double::store(nr);
4181   return error;
4182 }
4183 
store(double nr)4184 type_conversion_status Field_double::store(double nr) {
4185   ASSERT_COLUMN_MARKED_FOR_WRITE;
4186   const type_conversion_status error =
4187       truncate(&nr, DBL_MAX) ? TYPE_WARN_OUT_OF_RANGE : TYPE_OK;
4188 
4189   if (table->s->db_low_byte_first)
4190     float8store(ptr, nr);
4191   else
4192     doublestore(ptr, nr);
4193   return error;
4194 }
4195 
store(longlong nr,bool unsigned_val)4196 type_conversion_status Field_double::store(longlong nr, bool unsigned_val) {
4197   return Field_double::store(unsigned_val ? ulonglong2double((ulonglong)nr)
4198                                           : (double)nr);
4199 }
4200 
4201 /**
4202   If a field has fixed length, truncate the double argument pointed to by 'nr'
4203   appropriately.
4204   Also ensure that the argument is within [min_value; max_value] where
4205   min_value == 0 if unsigned_flag is set, else -max_value.
4206 
4207   @param[in,out] nr         the real number (FLOAT or DOUBLE) to be truncated
4208   @param[in]     max_value  the maximum (absolute) value of the real type
4209 
4210   @returns truncation result
4211 */
4212 
truncate(double * nr,double max_value)4213 Field_real::Truncate_result Field_real::truncate(double *nr, double max_value) {
4214   if (std::isnan(*nr)) {
4215     *nr = 0;
4216     set_null();
4217     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
4218     return TR_POSITIVE_OVERFLOW;
4219   } else if (is_unsigned() && *nr < 0) {
4220     *nr = 0;
4221     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
4222     return TR_NEGATIVE_OVERFLOW;
4223   }
4224 
4225   if (!not_fixed) {
4226     double orig_max_value = max_value;
4227     uint order = field_length - dec;
4228     uint step = array_elements(log_10) - 1;
4229     max_value = 1.0;
4230     for (; order > step; order -= step) max_value *= log_10[step];
4231     max_value *= log_10[order];
4232     max_value -= 1.0 / log_10[dec];
4233     max_value = std::min(max_value, orig_max_value);
4234 
4235     /* Check for infinity so we don't get NaN in calculations */
4236     if (!std::isinf(*nr)) {
4237       double tmp = rint((*nr - floor(*nr)) * log_10[dec]) / log_10[dec];
4238       *nr = floor(*nr) + tmp;
4239     }
4240   }
4241 
4242   if (*nr < -max_value) {
4243     *nr = -max_value;
4244     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
4245     return TR_NEGATIVE_OVERFLOW;
4246   } else if (*nr > max_value) {
4247     *nr = max_value;
4248     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
4249     return TR_POSITIVE_OVERFLOW;
4250   }
4251 
4252   return TR_OK;
4253 }
4254 
store_decimal(const my_decimal * dm)4255 type_conversion_status Field_real::store_decimal(const my_decimal *dm) {
4256   double dbl;
4257   my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
4258   return store(dbl);
4259 }
4260 
val_real() const4261 double Field_double::val_real() const {
4262   ASSERT_COLUMN_MARKED_FOR_READ;
4263   if (table->s->db_low_byte_first)
4264     return float8get(ptr);
4265   else
4266     return doubleget(ptr);
4267 }
4268 
val_int() const4269 longlong Field_double::val_int() const {
4270   ASSERT_COLUMN_MARKED_FOR_READ;
4271   double j;
4272   longlong res;
4273   if (table->s->db_low_byte_first)
4274     j = float8get(ptr);
4275   else
4276     j = doubleget(ptr);
4277   /* Check whether we fit into longlong range */
4278   if (j <= (double)LLONG_MIN) {
4279     res = (longlong)LLONG_MIN;
4280     goto warn;
4281   }
4282   if (j >= (double)(ulonglong)LLONG_MAX) {
4283     res = (longlong)LLONG_MAX;
4284     goto warn;
4285   }
4286   return (longlong)rint(j);
4287 
4288 warn : {
4289   char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
4290   String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
4291   str = val_str(&tmp, nullptr);
4292   ErrConvString err(str);
4293   push_warning_printf(
4294       current_thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE,
4295       ER_THD(current_thd, ER_TRUNCATED_WRONG_VALUE), "INTEGER", err.ptr());
4296 }
4297   return res;
4298 }
4299 
val_decimal(my_decimal * decimal_value) const4300 my_decimal *Field_real::val_decimal(my_decimal *decimal_value) const {
4301   ASSERT_COLUMN_MARKED_FOR_READ;
4302   double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
4303   return decimal_value;
4304 }
4305 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const4306 bool Field_real::get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const {
4307   return my_double_to_datetime_with_warn(val_real(), ltime, fuzzydate);
4308 }
4309 
get_time(MYSQL_TIME * ltime) const4310 bool Field_real::get_time(MYSQL_TIME *ltime) const {
4311   return my_double_to_time_with_warn(val_real(), ltime);
4312 }
4313 
val_str(String * val_buffer,String *) const4314 String *Field_double::val_str(String *val_buffer, String *) const {
4315   ASSERT_COLUMN_MARKED_FOR_READ;
4316   DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4317   double nr;
4318   if (table && table->s->db_low_byte_first)
4319     nr = float8get(ptr);
4320   else
4321     nr = doubleget(ptr);
4322   uint to_length = DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE;
4323   if (val_buffer->alloc(to_length)) {
4324     my_error(ER_OUT_OF_RESOURCES, MYF(0));
4325     return val_buffer;
4326   }
4327 
4328   char *to = val_buffer->ptr();
4329   size_t len;
4330 
4331   if (dec >= DECIMAL_NOT_SPECIFIED)
4332     len = my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, nullptr);
4333   else
4334     len = my_fcvt(nr, dec, to, nullptr);
4335 
4336   val_buffer->length((uint)len);
4337   if (zerofill) prepend_zeros(val_buffer);
4338   val_buffer->set_charset(&my_charset_numeric);
4339   return val_buffer;
4340 }
4341 
send_to_protocol(Protocol * protocol) const4342 bool Field_double::send_to_protocol(Protocol *protocol) const {
4343   if (is_null()) return protocol->store_null();
4344   return protocol->store_double(Field_double::val_real(), dec,
4345                                 zerofill ? field_length : 0);
4346 }
4347 
cmp(const uchar * a_ptr,const uchar * b_ptr) const4348 int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
4349   double a, b;
4350   if (table->s->db_low_byte_first) {
4351     a = float8get(a_ptr);
4352     b = float8get(b_ptr);
4353   } else {
4354     a = doubleget(a_ptr);
4355     b = doubleget(b_ptr);
4356   }
4357   return (a < b) ? -1 : (a > b) ? 1 : 0;
4358 }
4359 
4360 /* The following should work for IEEE */
4361 
make_sort_key(uchar * to,size_t length) const4362 size_t Field_double::make_sort_key(uchar *to, size_t length) const {
4363   DBUG_ASSERT(length == sizeof(double));
4364   double nr;
4365   if (table->s->db_low_byte_first)
4366     nr = float8get(ptr);
4367   else
4368     nr = doubleget(ptr);
4369   if (length < 8) {
4370     uchar buff[8];
4371     change_double_for_sort(nr, buff);
4372     memcpy(to, buff, length);
4373   } else
4374     change_double_for_sort(nr, to);
4375   return sizeof(double);
4376 }
4377 
4378 /**
4379    Save the field metadata for double fields.
4380 
4381    Saves the pack length in the first byte of the field metadata array
4382    at index of *metadata_ptr.
4383 
4384    @param   metadata_ptr   First byte of field metadata
4385 
4386    @returns number of bytes written to metadata_ptr
4387 */
do_save_field_metadata(uchar * metadata_ptr) const4388 int Field_double::do_save_field_metadata(uchar *metadata_ptr) const {
4389   *metadata_ptr = pack_length();
4390   return 1;
4391 }
4392 
sql_type(String & res) const4393 void Field_double::sql_type(String &res) const {
4394   const CHARSET_INFO *cs = res.charset();
4395   if (dec == DECIMAL_NOT_SPECIFIED) {
4396     res.set_ascii(STRING_WITH_LEN("double"));
4397   } else {
4398     res.length(cs->cset->snprintf(cs, res.ptr(), res.alloced_length(),
4399                                   "double(%d,%d)", (int)field_length, dec));
4400   }
4401   append_zerofill_and_unsigned(this, &res);
4402 }
4403 
4404 /****************************************************************************
4405 ** Common code for all temporal data types: DATE, DATETIME, TIMESTAMP, TIME
4406 *****************************************************************************/
4407 
date_flags() const4408 my_time_flags_t Field_temporal::date_flags() const {
4409   return date_flags(table ? table->in_use : current_thd);
4410 }
4411 
is_equal(const Create_field * new_field) const4412 uint Field_temporal::is_equal(const Create_field *new_field) const {
4413   return new_field->sql_type == real_type() &&
4414          new_field->decimals == decimals();
4415 }
4416 
val_decimal(my_decimal * decimal_value) const4417 my_decimal *Field_temporal::val_decimal(my_decimal *decimal_value) const {
4418   ASSERT_COLUMN_MARKED_FOR_READ;
4419   DBUG_ASSERT(decimals() == 0);
4420   int2my_decimal(E_DEC_FATAL_ERROR, val_int(), false, decimal_value);
4421   return decimal_value;
4422 }
4423 
4424 /**
4425   Set warnings from a warning vector.
4426   Note, multiple warnings can be set at the same time.
4427 
4428   @param str       Value.
4429   @param warnings  Warning vector.
4430 
4431   @retval false  Function reported warning
4432   @retval true   Function reported error
4433 
4434   @note STRICT mode can convert warnings to error.
4435 */
set_warnings(const ErrConvString & str,int warnings)4436 bool Field_temporal::set_warnings(const ErrConvString &str, int warnings) {
4437   bool truncate_incremented = false;
4438   enum_mysql_timestamp_type ts_type = field_type_to_timestamp_type(type());
4439 
4440   if (warnings & MYSQL_TIME_WARN_TRUNCATED) {
4441     if (set_datetime_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED,
4442                              str, ts_type, !truncate_incremented))
4443       return true;
4444     truncate_incremented = true;
4445   }
4446   if (warnings & (MYSQL_TIME_WARN_OUT_OF_RANGE | MYSQL_TIME_WARN_ZERO_DATE |
4447                   MYSQL_TIME_WARN_ZERO_IN_DATE)) {
4448     if (set_datetime_warning(Sql_condition::SL_WARNING,
4449                              ER_WARN_DATA_OUT_OF_RANGE, str, ts_type,
4450                              !truncate_incremented))
4451       return true;
4452     truncate_incremented = true;
4453   }
4454   if (warnings & MYSQL_TIME_WARN_INVALID_TIMESTAMP) {
4455     if (set_datetime_warning(Sql_condition::SL_WARNING,
4456                              ER_WARN_INVALID_TIMESTAMP, str, ts_type,
4457                              !truncate_incremented))
4458       return true;
4459     truncate_incremented = true;
4460   }
4461   if ((warnings & MYSQL_TIME_NOTE_TRUNCATED) &&
4462       !(warnings & MYSQL_TIME_WARN_TRUNCATED)) {
4463     if (set_datetime_warning(Sql_condition::SL_NOTE, WARN_DATA_TRUNCATED, str,
4464                              ts_type, !truncate_incremented))
4465       return true;
4466   }
4467   return false;
4468 }
4469 
store(longlong nr,bool unsigned_val)4470 type_conversion_status Field_temporal::store(longlong nr, bool unsigned_val) {
4471   ASSERT_COLUMN_MARKED_FOR_WRITE;
4472   int warnings = 0;
4473   MYSQL_TIME ltime;
4474   type_conversion_status error =
4475       convert_number_to_TIME(nr, unsigned_val, 0, &ltime, &warnings);
4476   if (error == TYPE_OK || error == TYPE_NOTE_TRUNCATED)
4477     error = store_internal(&ltime, &warnings);
4478   else {
4479     DBUG_ASSERT(warnings != 0);  // Must be set by convert_number_to_TIME
4480 
4481     if (warnings & (MYSQL_TIME_WARN_ZERO_DATE | MYSQL_TIME_WARN_ZERO_IN_DATE) &&
4482         !current_thd->is_strict_mode())
4483       error = TYPE_NOTE_TIME_TRUNCATED;
4484   }
4485   if (warnings && set_warnings(ErrConvString(nr, unsigned_val), warnings))
4486     return TYPE_ERR_BAD_VALUE;
4487 
4488   return error;
4489 }
4490 
store_lldiv_t(const lldiv_t * lld,int * warnings)4491 type_conversion_status Field_temporal::store_lldiv_t(const lldiv_t *lld,
4492                                                      int *warnings) {
4493   ASSERT_COLUMN_MARKED_FOR_WRITE;
4494   type_conversion_status error;
4495   MYSQL_TIME ltime;
4496   error = convert_number_to_TIME(lld->quot, false, static_cast<int>(lld->rem),
4497                                  &ltime, warnings);
4498   if (error == TYPE_OK || error == TYPE_NOTE_TRUNCATED)
4499     error = store_internal_adjust_frac(&ltime, warnings);
4500   else if (!*warnings) {
4501     DBUG_ASSERT(warnings != nullptr);  // Must be set by convert_number_to_TIME
4502     if (((*warnings & MYSQL_TIME_WARN_ZERO_DATE) != 0 ||
4503          (*warnings & MYSQL_TIME_WARN_ZERO_IN_DATE) != 0) &&
4504         !current_thd->is_strict_mode())
4505       error = TYPE_NOTE_TIME_TRUNCATED;
4506   }
4507 
4508   return error;
4509 }
4510 
store_decimal(const my_decimal * decimal)4511 type_conversion_status Field_temporal::store_decimal(
4512     const my_decimal *decimal) {
4513   ASSERT_COLUMN_MARKED_FOR_WRITE;
4514   lldiv_t lld;
4515   int warnings = 0;
4516   /* Pass 0 in the first argument, not to produce warnings automatically */
4517   my_decimal2lldiv_t(0, decimal, &lld);
4518   const type_conversion_status error = store_lldiv_t(&lld, &warnings);
4519   if (warnings && set_warnings(ErrConvString(decimal), warnings))
4520     return TYPE_ERR_BAD_VALUE;
4521 
4522   return error;
4523 }
4524 
store(double nr)4525 type_conversion_status Field_temporal::store(double nr) {
4526   ASSERT_COLUMN_MARKED_FOR_WRITE;
4527   int warnings = 0;
4528   lldiv_t lld;
4529   double2lldiv_t(nr, &lld);
4530   const type_conversion_status error = store_lldiv_t(&lld, &warnings);
4531   if (warnings && set_warnings(ErrConvString(nr), warnings))
4532     return TYPE_ERR_BAD_VALUE;
4533 
4534   return error;
4535 }
4536 
4537 /**
4538   Store string into a date/time/datetime field.
4539 
4540   @param str      Date/time string
4541   @param  len     Length of the string
4542   @param  cs      Character set of the string
4543 
4544   @retval TYPE_OK   Storage of value went fine without warnings or errors
4545   @retval !TYPE_OK  Warning/error as indicated by type_conversion_status enum
4546                     value
4547 */
store(const char * str,size_t len,const CHARSET_INFO * cs)4548 type_conversion_status Field_temporal::store(const char *str, size_t len,
4549                                              const CHARSET_INFO *cs) {
4550   ASSERT_COLUMN_MARKED_FOR_WRITE;
4551   type_conversion_status error = TYPE_OK;
4552   MYSQL_TIME ltime;
4553   MYSQL_TIME_STATUS status;
4554   if (convert_str_to_TIME(str, len, cs, &ltime, &status)) {
4555     /*
4556       When convert_str_to_TIME() returns error, ltime has been set to
4557       0 so there's nothing to store in the field.
4558     */
4559     reset();
4560     if (status.warnings &
4561             (MYSQL_TIME_WARN_ZERO_DATE | MYSQL_TIME_WARN_ZERO_IN_DATE) &&
4562         !current_thd->is_strict_mode())
4563       error = TYPE_NOTE_TIME_TRUNCATED;
4564     else
4565       error = TYPE_ERR_BAD_VALUE;
4566   } else {
4567     error = time_warning_to_type_conversion_status(status.warnings);
4568     const type_conversion_status tmp_error =
4569         store_internal_adjust_frac(&ltime, &status.warnings);
4570 
4571     // Return the most serious error of the two, see type_conversion_status
4572     if (tmp_error > error) error = tmp_error;
4573   }
4574   if (status.warnings &&
4575       set_warnings(ErrConvString(str, len, cs), status.warnings))
4576     return TYPE_ERR_BAD_VALUE;
4577 
4578   return error;
4579 }
4580 
4581 /**
4582 
4583   @param nr The datetime value specified as "number", see number_to_datetime()
4584   for details on this format.
4585 
4586   @param [out] ltime A MYSQL_TIME struct where the result is stored.
4587   @param warnings Truncation warning code, see was_cut in number_to_datetime().
4588 
4589   @retval -1    Timestamp with wrong values.
4590   @retval other DATETIME as integer in YYYYMMDDHHMMSS format.
4591 */
convert_number_to_datetime(longlong nr,bool,MYSQL_TIME * ltime,int * warnings)4592 longlong Field_temporal::convert_number_to_datetime(longlong nr, bool,
4593                                                     MYSQL_TIME *ltime,
4594                                                     int *warnings) {
4595   /*
4596     Note, number_to_datetime can return a result different from nr:
4597     e.g. 111111 -> 20111111000000
4598   */
4599   longlong tmp = number_to_datetime(nr, ltime, date_flags(), warnings);
4600   if (tmp == -1LL) reset();
4601   return tmp;
4602 }
4603 
4604 /****************************************************************************
4605 ** Common code for temporal data types with date: DATE, DATETIME, TIMESTAMP
4606 *****************************************************************************/
4607 
get_internal_check_zero(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const4608 bool Field_temporal_with_date::get_internal_check_zero(
4609     MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const {
4610   if (get_date_internal(ltime)) /* '0000-00-00' */
4611   {
4612     DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
4613     if (fuzzydate & TIME_NO_ZERO_DATE) return true;
4614     set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
4615   }
4616   return false;
4617 }
4618 
val_date_temporal() const4619 longlong Field_temporal_with_date::val_date_temporal() const {
4620   ASSERT_COLUMN_MARKED_FOR_READ;
4621   MYSQL_TIME ltime;
4622   return get_date_internal(&ltime) ? 0
4623                                    : TIME_to_longlong_datetime_packed(ltime);
4624 }
4625 
val_time_temporal() const4626 longlong Field_temporal_with_date::val_time_temporal() const {
4627   /*
4628     There are currently no tests covering this method,
4629     as DATETIME seems to always superseed over TIME in comparison.
4630   */
4631   ASSERT_COLUMN_MARKED_FOR_READ;
4632   MYSQL_TIME ltime;
4633   return get_date_internal(&ltime) ? 0 : TIME_to_longlong_time_packed(ltime);
4634 }
4635 
4636 /**
4637   Convert a number in format YYMMDDhhmmss to string.
4638   Straight coded to avoid problem with slow longlong arithmetic and sprintf.
4639 
4640   @param[out] pos      pointer to convert to.
4641   @param      tmp      number with datetime value.
4642 */
my_datetime_number_to_str(char * pos,longlong tmp)4643 static inline int my_datetime_number_to_str(char *pos, longlong tmp) {
4644   long part1 = (long)(tmp / 1000000LL);
4645   long part2 = (long)(tmp - (ulonglong)part1 * 1000000LL);
4646   int part3;
4647   pos += MAX_DATETIME_WIDTH; /* Start from the end */
4648   *pos-- = 0;
4649   *pos-- = (char)('0' + (char)(part2 % 10)); /* Seconds */
4650   part2 /= 10;
4651   *pos-- = (char)('0' + (char)(part2 % 10));
4652   part3 = (int)(part2 / 10);
4653   *pos-- = ':';
4654   *pos-- = (char)('0' + (char)(part3 % 10)); /* Minutes */
4655   part3 /= 10;
4656   *pos-- = (char)('0' + (char)(part3 % 10));
4657   part3 /= 10;
4658   *pos-- = ':';
4659   *pos-- = (char)('0' + (char)(part3 % 10)); /* Hours */
4660   part3 /= 10;
4661   *pos-- = (char)('0' + (char)part3);
4662   *pos-- = ' ';
4663   *pos-- = (char)('0' + (char)(part1 % 10)); /* Day */
4664   part1 /= 10;
4665   *pos-- = (char)('0' + (char)(part1 % 10));
4666   part1 /= 10;
4667   *pos-- = '-';
4668   *pos-- = (char)('0' + (char)(part1 % 10)); /* Month */
4669   part1 /= 10;
4670   *pos-- = (char)('0' + (char)(part1 % 10));
4671   part3 = (int)(part1 / 10);
4672   *pos-- = '-';
4673   *pos-- = (char)('0' + (char)(part3 % 10)); /* Year */
4674   part3 /= 10;
4675   *pos-- = (char)('0' + (char)(part3 % 10));
4676   part3 /= 10;
4677   *pos-- = (char)('0' + (char)(part3 % 10));
4678   part3 /= 10;
4679   *pos = (char)('0' + (char)part3);
4680   return MAX_DATETIME_WIDTH;
4681 }
4682 
val_str(String * val_buffer,String *) const4683 String *Field_temporal_with_date::val_str(String *val_buffer, String *) const {
4684   ASSERT_COLUMN_MARKED_FOR_READ;
4685   MYSQL_TIME ltime;
4686   val_buffer->alloc(field_length + 1);
4687   val_buffer->set_charset(&my_charset_numeric);
4688   if (get_date_internal(&ltime)) {
4689     val_buffer->set_ascii(my_zero_datetime6, field_length);
4690     return val_buffer;
4691   }
4692   make_datetime((Date_time_format *)nullptr, &ltime, val_buffer, dec);
4693   return val_buffer;
4694 }
4695 
convert_number_to_TIME(longlong nr,bool unsigned_val,int nanoseconds,MYSQL_TIME * ltime,int * warnings)4696 type_conversion_status Field_temporal_with_date::convert_number_to_TIME(
4697     longlong nr, bool unsigned_val, int nanoseconds, MYSQL_TIME *ltime,
4698     int *warnings) {
4699   if (nr < 0 || nanoseconds < 0) {
4700     reset();
4701     *warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
4702     return TYPE_WARN_OUT_OF_RANGE;
4703   }
4704 
4705   if (convert_number_to_datetime(nr, unsigned_val, ltime, warnings) == -1LL)
4706     return TYPE_ERR_BAD_VALUE;
4707 
4708   if (ltime->time_type == MYSQL_TIMESTAMP_DATE && nanoseconds) {
4709     *warnings |= MYSQL_TIME_WARN_TRUNCATED;
4710     return TYPE_NOTE_TRUNCATED;
4711   }
4712 
4713   ltime->second_part = 0;
4714   if (propagate_datetime_overflow(current_thd, warnings,
4715                                   datetime_add_nanoseconds_adjust_frac(
4716                                       ltime, nanoseconds, warnings,
4717                                       (date_flags() & TIME_FRAC_TRUNCATE)))) {
4718     reset();
4719     return TYPE_WARN_OUT_OF_RANGE;
4720   }
4721   return TYPE_OK;
4722 }
4723 
store_time(MYSQL_TIME * ltime,uint8)4724 type_conversion_status Field_temporal_with_date::store_time(MYSQL_TIME *ltime,
4725                                                             uint8) {
4726   ASSERT_COLUMN_MARKED_FOR_WRITE;
4727   type_conversion_status error;
4728   int warnings = 0;
4729 
4730   switch (ltime->time_type)  // TS-TODO: split into separate methods?
4731   {
4732     case MYSQL_TIMESTAMP_DATETIME:
4733     case MYSQL_TIMESTAMP_DATETIME_TZ:
4734     case MYSQL_TIMESTAMP_DATE:
4735       if (check_date(*ltime, non_zero_date(*ltime), date_flags(), &warnings)) {
4736         DBUG_ASSERT(warnings &
4737                     (MYSQL_TIME_WARN_OUT_OF_RANGE | MYSQL_TIME_WARN_ZERO_DATE |
4738                      MYSQL_TIME_WARN_ZERO_IN_DATE));
4739 
4740         error = time_warning_to_type_conversion_status(warnings);
4741         reset();
4742       } else {
4743         error = store_internal_adjust_frac(ltime, &warnings);
4744       }
4745       break;
4746     case MYSQL_TIMESTAMP_TIME: {
4747       /* Convert TIME to DATETIME */
4748       THD *thd = table ? table->in_use : current_thd;
4749       MYSQL_TIME ltime2;
4750       time_to_datetime(thd, ltime, &ltime2);
4751       error = store_internal_adjust_frac(&ltime2, &warnings);
4752       break;
4753     }
4754     case MYSQL_TIMESTAMP_NONE:
4755     case MYSQL_TIMESTAMP_ERROR:
4756     default:
4757       warnings |= MYSQL_TIME_WARN_TRUNCATED;
4758       reset();
4759       error = TYPE_WARN_TRUNCATED;
4760   }
4761 
4762   if (warnings && set_warnings(ErrConvString(ltime, decimals()), warnings))
4763     return TYPE_ERR_BAD_VALUE;
4764 
4765   return error;
4766 }
4767 
convert_str_to_TIME(const char * str,size_t len,const CHARSET_INFO * cs,MYSQL_TIME * ltime,MYSQL_TIME_STATUS * status)4768 bool Field_temporal_with_date::convert_str_to_TIME(const char *str, size_t len,
4769                                                    const CHARSET_INFO *cs,
4770                                                    MYSQL_TIME *ltime,
4771                                                    MYSQL_TIME_STATUS *status) {
4772   return propagate_datetime_overflow(
4773       current_thd, &status->warnings,
4774       str_to_datetime(cs, str, len, ltime, date_flags(), status));
4775 }
4776 
send_to_protocol(Protocol * protocol) const4777 bool Field_temporal_with_date::send_to_protocol(Protocol *protocol) const {
4778   if (is_null()) return protocol->store_null();
4779   MYSQL_TIME ltime;
4780   if (get_date_internal(&ltime)) {
4781     // Only MYSQL_TYPE_TIMESTAMP can return an error in get_date_internal()
4782     DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
4783     set_zero_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
4784   }
4785   return protocol->store_datetime(ltime, dec);
4786 }
4787 
store_internal_adjust_frac(MYSQL_TIME * ltime,int * warnings)4788 type_conversion_status Field_temporal_with_date::store_internal_adjust_frac(
4789     MYSQL_TIME *ltime, int *warnings) {
4790   if (propagate_datetime_overflow(
4791           current_thd, warnings,
4792           my_datetime_adjust_frac(ltime, dec, warnings,
4793                                   (date_flags() & TIME_FRAC_TRUNCATE)))) {
4794     reset();
4795     return time_warning_to_type_conversion_status(*warnings);
4796   } else
4797     return store_internal(ltime, warnings);
4798 }
4799 
4800 /**
4801   Validate date value stored in the field.
4802 
4803   Now we check whether date value is zero or has zero in date or not and sets
4804   warning/error message appropriately(depending on the sql_mode).
4805 */
validate_stored_val(THD *)4806 type_conversion_status Field_temporal_with_date::validate_stored_val(THD *) {
4807   MYSQL_TIME ltime;
4808   type_conversion_status error = TYPE_OK;
4809   int warnings = 0;
4810 
4811   if (is_real_null()) return error;
4812 
4813   memset(&ltime, 0, sizeof(MYSQL_TIME));
4814   get_date_internal(&ltime);
4815   if (check_date(ltime, non_zero_date(ltime), date_flags(), &warnings))
4816     error = time_warning_to_type_conversion_status(warnings);
4817 
4818   if (warnings) {
4819     ltime.time_type = field_type_to_timestamp_type(type());
4820     if (set_warnings(ErrConvString(&ltime, dec), warnings))
4821       return TYPE_ERR_BAD_VALUE;
4822   }
4823 
4824   return error;
4825 }
4826 
4827 /****************************************************************************
4828 ** Common code for data types with date and time: DATETIME, TIMESTAMP
4829 *****************************************************************************/
4830 
store_timestamp(const struct timeval * tm)4831 void Field_temporal_with_date_and_time::store_timestamp(
4832     const struct timeval *tm) {
4833   ASSERT_COLUMN_MARKED_FOR_WRITE;
4834   if (!my_time_fraction_remainder(tm->tv_usec, decimals())) {
4835     store_timestamp_internal(tm);
4836     return;
4837   }
4838   struct timeval tm2 = *tm;
4839   my_timeval_round(&tm2, decimals());
4840   store_timestamp_internal(&tm2);
4841 }
4842 
convert_TIME_to_timestamp(THD * thd,const MYSQL_TIME * ltime,struct timeval * tm,int * warnings)4843 bool Field_temporal_with_date_and_time::convert_TIME_to_timestamp(
4844     THD *thd, const MYSQL_TIME *ltime, struct timeval *tm, int *warnings) {
4845   /*
4846     No need to do check_date(TIME_NO_ZERO_IN_DATE),
4847     because it has been done earlier in
4848     store_time(), number_to_datetime() or str_to_datetime().
4849   */
4850   if (datetime_with_no_zero_in_date_to_timeval(thd, ltime, tm, warnings)) {
4851     tm->tv_sec = tm->tv_usec = 0;
4852     return true;
4853   }
4854   return false;
4855 }
4856 
init_timestamp_flags()4857 void Field_temporal_with_date_and_time::init_timestamp_flags() {
4858   if (auto_flags != NONE && (!(auto_flags & GENERATED_FROM_EXPRESSION))) {
4859     /*
4860       This TIMESTAMP column is hereby quietly assumed to have an insert or
4861       update default function.
4862     */
4863     set_flag(TIMESTAMP_FLAG);
4864     if (auto_flags & ON_UPDATE_NOW) set_flag(ON_UPDATE_NOW_FLAG);
4865   }
4866 }
4867 
4868 /****************************************************************************
4869 ** Common code for DATETIME(N) and TIMESTAMP(N)
4870 *****************************************************************************/
4871 
val_real() const4872 double Field_temporal_with_date_and_timef::val_real() const {
4873   ASSERT_COLUMN_MARKED_FOR_READ;
4874   MYSQL_TIME ltime;
4875   return get_date_internal(&ltime) ? 0 : TIME_to_double_datetime(ltime);
4876 }
4877 
val_int() const4878 longlong Field_temporal_with_date_and_timef::val_int() const {
4879   ASSERT_COLUMN_MARKED_FOR_READ;
4880   MYSQL_TIME ltime;
4881   return get_date_internal(&ltime)
4882              ? 0
4883              : propagate_datetime_overflow(current_thd, [&](int *w) {
4884                  return TIME_to_ulonglong_datetime_round(ltime, w);
4885                });
4886 }
4887 
val_decimal(my_decimal * dec_arg) const4888 my_decimal *Field_temporal_with_date_and_timef::val_decimal(
4889     my_decimal *dec_arg) const {
4890   ASSERT_COLUMN_MARKED_FOR_READ;
4891   MYSQL_TIME ltime;
4892   if (get_date_internal(&ltime)) {
4893     // Only MYSQL_TYPE_TIMESTAMP can return an error in get_date_internal()
4894     DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
4895     set_zero_time(&ltime, MYSQL_TIMESTAMP_DATETIME);
4896   }
4897   return date2my_decimal(&ltime, dec_arg);
4898 }
4899 
4900 /**
4901   TIMESTAMP type columns hold date and time values in the range 1970-01-01
4902   00:00:01 UTC to 2038-01-01 00:00:00 UTC, stored as number of seconds since
4903   the start of the Unix Epoch (1970-01-01 00:00:01 UTC.)
4904 
4905   TIMESTAMP columns can be automatically set on row updates to and/or have
4906   CURRENT_TIMESTAMP as default value for inserts.
4907   We use flags Field::auto_flags member to control this behavior.
4908 */
Field_timestamp(uchar * ptr_arg,uint32,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg)4909 Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32, uchar *null_ptr_arg,
4910                                  uchar null_bit_arg, uchar auto_flags_arg,
4911                                  const char *field_name_arg)
4912     : Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
4913                                         auto_flags_arg, field_name_arg, 0) {
4914   init_timestamp_flags();
4915   /* For 4.0 MYD and 4.0 InnoDB compatibility */
4916   set_flag(ZEROFILL_FLAG);
4917   set_flag(UNSIGNED_FLAG);
4918 }
4919 
Field_timestamp(bool is_nullable_arg,const char * field_name_arg)4920 Field_timestamp::Field_timestamp(bool is_nullable_arg,
4921                                  const char *field_name_arg)
4922     : Field_temporal_with_date_and_time(
4923           nullptr, is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
4924           field_name_arg, 0) {
4925   init_timestamp_flags();
4926   /* For 4.0 MYD and 4.0 InnoDB compatibility */
4927   set_flag(ZEROFILL_FLAG);
4928   set_flag(UNSIGNED_FLAG);
4929 }
4930 
date_flags(const THD * thd) const4931 my_time_flags_t Field_timestamp::date_flags(const THD *thd) const {
4932   /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
4933   my_time_flags_t date_flags = TIME_NO_ZERO_IN_DATE;
4934   if (thd->variables.sql_mode & MODE_NO_ZERO_DATE)
4935     date_flags |= TIME_NO_ZERO_DATE;
4936   if (thd->variables.sql_mode & MODE_TIME_TRUNCATE_FRACTIONAL)
4937     date_flags |= TIME_FRAC_TRUNCATE;
4938 
4939   return date_flags;
4940 }
4941 
store_internal(const MYSQL_TIME * ltime,int * warnings)4942 type_conversion_status Field_timestamp::store_internal(const MYSQL_TIME *ltime,
4943                                                        int *warnings) {
4944   THD *thd = table ? table->in_use : current_thd;
4945   struct timeval tm;
4946   convert_TIME_to_timestamp(thd, ltime, &tm, warnings);
4947   const type_conversion_status error =
4948       time_warning_to_type_conversion_status(*warnings);
4949   store_timestamp_internal(&tm);
4950   return error;
4951 }
4952 
4953 /**
4954   Get a value from record, without checking fuzzy date flags.
4955   @retval true  - if timestamp is 0, ltime is not touched in this case.
4956   @retval false - if timestamp is non-zero.
4957 */
get_date_internal(MYSQL_TIME * ltime) const4958 bool Field_timestamp::get_date_internal(MYSQL_TIME *ltime) const {
4959   ASSERT_COLUMN_MARKED_FOR_READ;
4960   uint32 temp;
4961   THD *thd = table ? table->in_use : current_thd;
4962   if (table && table->s->db_low_byte_first)
4963     temp = uint4korr(ptr);
4964   else
4965     temp = ulongget(ptr);
4966   if (!temp) return true;
4967   thd->time_zone()->gmt_sec_to_TIME(ltime, (my_time_t)temp);
4968   return false;
4969 }
4970 
4971 /**
4972    Get TIMESTAMP field value as seconds since begging of Unix Epoch
4973 */
get_timestamp(struct timeval * tm,int *) const4974 bool Field_timestamp::get_timestamp(struct timeval *tm, int *) const {
4975   if (is_null()) return true;
4976   tm->tv_usec = 0;
4977   if (table && table->s->db_low_byte_first) {
4978     tm->tv_sec = sint4korr(ptr);
4979     return false;
4980   }
4981   tm->tv_sec = longget(ptr);
4982   return false;
4983 }
4984 
store_timestamp_internal(const struct timeval * tm)4985 void Field_timestamp::store_timestamp_internal(const struct timeval *tm) {
4986   if (table && table->s->db_low_byte_first)
4987     int4store(ptr, tm->tv_sec);
4988   else
4989     longstore(ptr, (uint32)tm->tv_sec);
4990 }
4991 
store_packed(longlong nr)4992 type_conversion_status Field_timestamp::store_packed(longlong nr) {
4993   /* Make sure the stored value was previously properly rounded or truncated */
4994   DBUG_ASSERT((my_packed_time_get_frac_part(nr) %
4995                (int)log_10_int[DATETIME_MAX_DECIMALS - decimals()]) == 0);
4996   MYSQL_TIME ltime;
4997   TIME_from_longlong_datetime_packed(&ltime, nr);
4998   return Field_timestamp::store_time(&ltime, 0);
4999 }
5000 
val_int() const5001 longlong Field_timestamp::val_int() const {
5002   ASSERT_COLUMN_MARKED_FOR_READ;
5003   MYSQL_TIME ltime;
5004   return get_date_internal(&ltime) ? 0 : TIME_to_ulonglong_datetime(ltime);
5005 }
5006 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const5007 bool Field_timestamp::get_date(MYSQL_TIME *ltime,
5008                                my_time_flags_t fuzzydate) const {
5009   /* Don't do check_fuzzy_date() as month and year are never 0 for timestamp */
5010   return get_internal_check_zero(ltime, fuzzydate);
5011 }
5012 
cmp(const uchar * a_ptr,const uchar * b_ptr) const5013 int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
5014   int32 a, b;
5015   if (table && table->s->db_low_byte_first) {
5016     a = sint4korr(a_ptr);
5017     b = sint4korr(b_ptr);
5018   } else {
5019     a = longget(a_ptr);
5020     b = longget(b_ptr);
5021   }
5022   return ((uint32)a < (uint32)b) ? -1 : ((uint32)a > (uint32)b) ? 1 : 0;
5023 }
5024 
make_sort_key(uchar * to,size_t length MY_ATTRIBUTE ((unused))) const5025 size_t Field_timestamp::make_sort_key(
5026     uchar *to, size_t length MY_ATTRIBUTE((unused))) const {
5027   DBUG_ASSERT(length == 4);
5028 #ifdef WORDS_BIGENDIAN
5029   if (!table || !table->s->db_low_byte_first) {
5030     to[0] = ptr[0];
5031     to[1] = ptr[1];
5032     to[2] = ptr[2];
5033     to[3] = ptr[3];
5034   } else
5035 #endif
5036   {
5037     to[0] = ptr[3];
5038     to[1] = ptr[2];
5039     to[2] = ptr[1];
5040     to[3] = ptr[0];
5041   }
5042   return 4;
5043 }
5044 
sql_type(String & res) const5045 void Field_timestamp::sql_type(String &res) const {
5046   res.set_ascii(STRING_WITH_LEN("timestamp"));
5047 }
5048 
validate_stored_val(THD * thd)5049 type_conversion_status Field_timestamp::validate_stored_val(THD *thd) {
5050   /*
5051     While deprecating "TIMESTAMP with implicit DEFAULT value", we can
5052     remove this function implementation and depend directly on
5053     "Field_temporal_with_date::validate_stored_val"
5054   */
5055   if (!thd->variables.explicit_defaults_for_timestamp) return TYPE_OK;
5056 
5057   return (Field_temporal_with_date::validate_stored_val(thd));
5058 }
5059 
5060 /****************************************************************************
5061 ** timestamp(N) type
5062 ** In string context: YYYY-MM-DD HH:MM:SS.FFFFFF
5063 ** In number context: YYYYMMDDHHMMSS.FFFFFF
5064 ** Stored as a 7 byte value
5065 ****************************************************************************/
Field_timestampf(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg)5066 Field_timestampf::Field_timestampf(uchar *ptr_arg, uchar *null_ptr_arg,
5067                                    uchar null_bit_arg, uchar auto_flags_arg,
5068                                    const char *field_name_arg, uint8 dec_arg)
5069     : Field_temporal_with_date_and_timef(ptr_arg, null_ptr_arg, null_bit_arg,
5070                                          auto_flags_arg, field_name_arg,
5071                                          dec_arg) {
5072   init_timestamp_flags();
5073 }
5074 
Field_timestampf(bool is_nullable_arg,const char * field_name_arg,uint8 dec_arg)5075 Field_timestampf::Field_timestampf(bool is_nullable_arg,
5076                                    const char *field_name_arg, uint8 dec_arg)
5077     : Field_temporal_with_date_and_timef(
5078           nullptr, is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
5079           field_name_arg, dec_arg) {
5080   if (auto_flags & ON_UPDATE_NOW) set_flag(ON_UPDATE_NOW_FLAG);
5081 }
5082 
date_flags(const THD * thd) const5083 my_time_flags_t Field_timestampf::date_flags(const THD *thd) const {
5084   /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
5085   my_time_flags_t date_flags = TIME_NO_ZERO_IN_DATE;
5086   if (thd->variables.sql_mode & MODE_NO_ZERO_DATE)
5087     date_flags |= TIME_NO_ZERO_DATE;
5088   if (thd->variables.sql_mode & MODE_TIME_TRUNCATE_FRACTIONAL)
5089     date_flags |= TIME_FRAC_TRUNCATE;
5090 
5091   return date_flags;
5092 }
5093 
store_timestamp_internal(const struct timeval * tm)5094 void Field_timestampf::store_timestamp_internal(const struct timeval *tm) {
5095   my_timestamp_to_binary(tm, ptr, dec);
5096 }
5097 
store_internal(const MYSQL_TIME * ltime,int * warnings)5098 type_conversion_status Field_timestampf::store_internal(const MYSQL_TIME *ltime,
5099                                                         int *warnings) {
5100   THD *thd = table ? table->in_use : current_thd;
5101   struct timeval tm;
5102   convert_TIME_to_timestamp(thd, ltime, &tm, warnings);
5103   const type_conversion_status error =
5104       time_warning_to_type_conversion_status(*warnings);
5105   store_timestamp_internal(&tm);
5106   return error;
5107 }
5108 
store_packed(longlong nr)5109 type_conversion_status Field_timestampf::store_packed(longlong nr) {
5110   MYSQL_TIME ltime;
5111   TIME_from_longlong_datetime_packed(&ltime, nr);
5112   return Field_timestampf::store_time(&ltime, dec);
5113 }
5114 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const5115 bool Field_timestampf::get_date(MYSQL_TIME *ltime,
5116                                 my_time_flags_t fuzzydate) const {
5117   /* Don't do check_fuzzy_date() as month and year are never 0 for timestamp */
5118   return get_internal_check_zero(ltime, fuzzydate);
5119 }
5120 
sql_type(String & res) const5121 void Field_timestampf::sql_type(String &res) const {
5122   if (dec == 0) {
5123     res.set_ascii(STRING_WITH_LEN("timestamp"));
5124     return;
5125   }
5126   const CHARSET_INFO *cs = res.charset();
5127   res.length(cs->cset->snprintf(cs, res.ptr(), res.alloced_length(),
5128                                 "timestamp(%d)", dec));
5129 }
5130 
get_date_internal(MYSQL_TIME * ltime) const5131 bool Field_timestampf::get_date_internal(MYSQL_TIME *ltime) const {
5132   THD *thd = table ? table->in_use : current_thd;
5133   struct timeval tm;
5134   my_timestamp_from_binary(&tm, ptr, dec);
5135   if (tm.tv_sec == 0) return true;
5136   thd->time_zone()->gmt_sec_to_TIME(ltime, tm);
5137   return false;
5138 }
5139 
get_timestamp(struct timeval * tm,int *) const5140 bool Field_timestampf::get_timestamp(struct timeval *tm, int *) const {
5141   THD *thd = table ? table->in_use : current_thd;
5142   thd->time_zone_used = true;
5143   DBUG_ASSERT(!is_null());
5144   my_timestamp_from_binary(tm, ptr, dec);
5145   return false;
5146 }
5147 
validate_stored_val(THD * thd)5148 type_conversion_status Field_timestampf::validate_stored_val(THD *thd) {
5149   /*
5150     While deprecating "TIMESTAMP with implicit DEFAULT value", we can
5151     remove this function implementation and depend directly on
5152     "Field_temporal_with_date::validate_stored_val"
5153   */
5154   if (!thd->variables.explicit_defaults_for_timestamp) return TYPE_OK;
5155 
5156   return (Field_temporal_with_date::validate_stored_val(thd));
5157 }
5158 
5159 /****************************************************************************
5160 ** TIME and TIME(N) common methods
5161 ****************************************************************************/
5162 
convert_str_to_TIME(const char * str,size_t len,const CHARSET_INFO * cs,MYSQL_TIME * ltime,MYSQL_TIME_STATUS * status)5163 bool Field_time_common::convert_str_to_TIME(const char *str, size_t len,
5164                                             const CHARSET_INFO *cs,
5165                                             MYSQL_TIME *ltime,
5166                                             MYSQL_TIME_STATUS *status) {
5167   return str_to_time(cs, str, len, ltime, date_flags(), status);
5168 }
5169 
convert_number_to_TIME(longlong nr,bool unsigned_val,int nanoseconds,MYSQL_TIME * ltime,int * warnings)5170 type_conversion_status Field_time_common::convert_number_to_TIME(
5171     longlong nr, bool unsigned_val, int nanoseconds, MYSQL_TIME *ltime,
5172     int *warnings) {
5173   if (unsigned_val && nr < 0) {
5174     *warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
5175     set_max_time(ltime, false);
5176     store_internal(ltime, warnings);
5177     return TYPE_WARN_OUT_OF_RANGE;
5178   }
5179   if (number_to_time(nr, ltime, warnings)) {
5180     store_internal(ltime, warnings);
5181     return TYPE_WARN_OUT_OF_RANGE;
5182   }
5183   /*
5184     Both number_to_time() call and negative nanoseconds value
5185     affect ltime->neg, hence "|=" to combine them:
5186   */
5187   if ((ltime->neg |= (nanoseconds < 0))) nanoseconds = -nanoseconds;
5188   ltime->second_part = 0;
5189 
5190   bool error = time_add_nanoseconds_adjust_frac(
5191       ltime, nanoseconds, warnings, (date_flags() & TIME_FRAC_TRUNCATE));
5192   return error ? time_warning_to_type_conversion_status(*warnings) : TYPE_OK;
5193 }
5194 
store_time(MYSQL_TIME * ltime,uint8)5195 type_conversion_status Field_time_common::store_time(MYSQL_TIME *ltime, uint8) {
5196   /* Check if seconds or minutes are out of range */
5197   if (ltime->second >= 60 || ltime->minute >= 60) {
5198     if (set_warnings(ErrConvString(ltime, decimals()),
5199                      MYSQL_TIME_WARN_OUT_OF_RANGE))
5200       return TYPE_ERR_BAD_VALUE;
5201     reset();
5202     return TYPE_WARN_OUT_OF_RANGE;
5203   }
5204   int warnings = 0;
5205   return store_internal_adjust_frac(ltime, &warnings);
5206 }
5207 
store_internal_adjust_frac(MYSQL_TIME * ltime,int * warnings)5208 type_conversion_status Field_time_common::store_internal_adjust_frac(
5209     MYSQL_TIME *ltime, int *warnings) {
5210   if (my_time_adjust_frac(ltime, dec, (date_flags() & TIME_FRAC_TRUNCATE)))
5211     return TYPE_WARN_OUT_OF_RANGE;
5212 
5213   return store_internal(ltime, warnings);
5214 }
5215 
val_str(String * val_buffer,String *) const5216 String *Field_time_common::val_str(String *val_buffer, String *) const {
5217   ASSERT_COLUMN_MARKED_FOR_READ;
5218   MYSQL_TIME ltime;
5219   val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
5220   val_buffer->set_charset(&my_charset_numeric);
5221   if (get_time(&ltime)) {
5222     DBUG_ASSERT(0);
5223     set_zero_time(&ltime, MYSQL_TIMESTAMP_TIME);
5224   }
5225   make_time((Date_time_format *)nullptr, &ltime, val_buffer, dec);
5226   return val_buffer;
5227 }
5228 
5229 /**
5230   For a column for TIME type, get_date() takes the time
5231   value of the field, adds current date to it and returns
5232   the result as a DATETIME value.
5233 */
5234 
get_date(MYSQL_TIME * ltime,my_time_flags_t) const5235 bool Field_time_common::get_date(MYSQL_TIME *ltime, my_time_flags_t) const {
5236   ASSERT_COLUMN_MARKED_FOR_READ;
5237   MYSQL_TIME tm;
5238   if (get_time(&tm)) {
5239     DBUG_ASSERT(0);
5240     set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
5241   }
5242   time_to_datetime(table ? table->in_use : current_thd, &tm, ltime);
5243   return false;
5244 }
5245 
val_date_temporal() const5246 longlong Field_time_common::val_date_temporal() const {
5247   ASSERT_COLUMN_MARKED_FOR_READ;
5248   MYSQL_TIME time, datetime;
5249   if (get_time(&time)) {
5250     DBUG_ASSERT(0);  // Field_time*::get_time should not fail
5251     return 0;
5252   }
5253   time_to_datetime(table ? table->in_use : current_thd, &time, &datetime);
5254   return TIME_to_longlong_datetime_packed(datetime);
5255 }
5256 
send_to_protocol(Protocol * protocol) const5257 bool Field_time_common::send_to_protocol(Protocol *protocol) const {
5258   if (is_null()) return protocol->store_null();
5259   MYSQL_TIME ltime;
5260   if (get_time(&ltime)) {
5261     DBUG_ASSERT(0);
5262     set_zero_time(&ltime, MYSQL_TIMESTAMP_TIME);
5263   }
5264   return protocol->store_time(ltime, dec);
5265 }
5266 
date_flags(const THD * thd) const5267 my_time_flags_t Field_time_common::date_flags(const THD *thd) const {
5268   my_time_flags_t date_flags = 0;
5269   if (thd->variables.sql_mode & MODE_TIME_TRUNCATE_FRACTIONAL)
5270     date_flags = TIME_FRAC_TRUNCATE;
5271 
5272   return date_flags;
5273 }
5274 
5275 /****************************************************************************
5276 ** time type
5277 ** In string context: HH:MM:SS
5278 ** In number context: HHMMSS
5279 ** Stored as a 3 byte unsigned int
5280 ****************************************************************************/
5281 
store_internal(const MYSQL_TIME * ltime,int *)5282 type_conversion_status Field_time::store_internal(const MYSQL_TIME *ltime,
5283                                                   int *) {
5284   long tmp = ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
5285              (ltime->minute * 100 + ltime->second);
5286   if (ltime->neg) tmp = -tmp;
5287   int3store(ptr, tmp);
5288   return TYPE_OK;
5289 }
5290 
store_packed(longlong nr)5291 type_conversion_status Field_time::store_packed(longlong nr) {
5292   MYSQL_TIME ltime;
5293   TIME_from_longlong_time_packed(&ltime, nr);
5294   return Field_time::store_time(&ltime, 0);
5295 }
5296 
val_time_temporal() const5297 longlong Field_time::val_time_temporal() const {
5298   ASSERT_COLUMN_MARKED_FOR_READ;
5299   MYSQL_TIME ltime;
5300   return get_time(&ltime) ? 0 : TIME_to_longlong_time_packed(ltime);
5301 }
5302 
val_int() const5303 longlong Field_time::val_int() const {
5304   ASSERT_COLUMN_MARKED_FOR_READ;
5305   return (longlong)sint3korr(ptr);
5306 }
5307 
get_time(MYSQL_TIME * ltime) const5308 bool Field_time::get_time(MYSQL_TIME *ltime) const {
5309   long tmp = (long)sint3korr(ptr);
5310   if ((ltime->neg = tmp < 0)) tmp = -tmp;
5311   ltime->year = ltime->month = ltime->day = 0;
5312   TIME_set_hhmmss(ltime, tmp);
5313   ltime->second_part = 0;
5314   ltime->time_type = MYSQL_TIMESTAMP_TIME;
5315   return false;
5316 }
5317 
cmp(const uchar * a_ptr,const uchar * b_ptr) const5318 int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
5319   int32 a, b;
5320   a = sint3korr(a_ptr);
5321   b = sint3korr(b_ptr);
5322   return (a < b) ? -1 : (a > b) ? 1 : 0;
5323 }
5324 
make_sort_key(uchar * to,size_t length MY_ATTRIBUTE ((unused))) const5325 size_t Field_time::make_sort_key(uchar *to,
5326                                  size_t length MY_ATTRIBUTE((unused))) const {
5327   DBUG_ASSERT(length == 3);
5328   to[0] = (uchar)(ptr[2] ^ 128);
5329   to[1] = ptr[1];
5330   to[2] = ptr[0];
5331   return 3;
5332 }
5333 
sql_type(String & res) const5334 void Field_time::sql_type(String &res) const {
5335   res.set_ascii(STRING_WITH_LEN("time"));
5336 }
5337 
5338 /****************************************************************************
5339 ** time type with fsp
5340 ** In string context: HH:MM:SS.FFFFFF
5341 ** In number context: HHMMSS.FFFFFF
5342 ****************************************************************************/
5343 
val_int() const5344 longlong Field_timef::val_int() const {
5345   ASSERT_COLUMN_MARKED_FOR_READ;
5346   MYSQL_TIME ltime;
5347   if (get_time(&ltime)) {
5348     DBUG_ASSERT(0);
5349     set_zero_time(&ltime, MYSQL_TIMESTAMP_TIME);
5350   }
5351   longlong tmp = (longlong)TIME_to_ulonglong_time_round(ltime);
5352   return ltime.neg ? -tmp : tmp;
5353 }
5354 
val_decimal(my_decimal * decimal_value) const5355 my_decimal *Field_timef::val_decimal(my_decimal *decimal_value) const {
5356   ASSERT_COLUMN_MARKED_FOR_READ;
5357   MYSQL_TIME ltime;
5358   if (get_time(&ltime)) {
5359     DBUG_ASSERT(0);
5360     set_zero_time(&ltime, MYSQL_TIMESTAMP_TIME);
5361   }
5362   return time2my_decimal(&ltime, decimal_value);
5363 }
5364 
val_real() const5365 double Field_timef::val_real() const {
5366   ASSERT_COLUMN_MARKED_FOR_READ;
5367   MYSQL_TIME ltime;
5368   if (get_time(&ltime)) {
5369     DBUG_ASSERT(0);
5370     return 0;
5371   }
5372   double tmp = TIME_to_double_time(ltime);
5373   return ltime.neg ? -tmp : tmp;
5374 }
5375 
sql_type(String & res) const5376 void Field_timef::sql_type(String &res) const {
5377   if (dec == 0) {
5378     res.set_ascii(STRING_WITH_LEN("time"));
5379     return;
5380   }
5381   const CHARSET_INFO *cs = res.charset();
5382   res.length(
5383       cs->cset->snprintf(cs, res.ptr(), res.alloced_length(), "time(%d)", dec));
5384 }
5385 
reset()5386 type_conversion_status Field_timef::reset() { return store_packed(0); }
5387 
store_packed(longlong nr)5388 type_conversion_status Field_timef::store_packed(longlong nr) {
5389   my_time_packed_to_binary(nr, ptr, dec);
5390   return TYPE_OK;
5391 }
5392 
val_time_temporal() const5393 longlong Field_timef::val_time_temporal() const {
5394   ASSERT_COLUMN_MARKED_FOR_READ;
5395   return my_time_packed_from_binary(ptr, dec);
5396 }
5397 
store_internal(const MYSQL_TIME * ltime,int * warnings)5398 type_conversion_status Field_timef::store_internal(const MYSQL_TIME *ltime,
5399                                                    int *warnings) {
5400   type_conversion_status rc =
5401       store_packed(TIME_to_longlong_time_packed(*ltime));
5402   if (rc == TYPE_OK && non_zero_date(*ltime)) {
5403     /*
5404       The DATE part got lost; we warn, like in Field_newdate::store_internal,
5405       and trigger some code in get_mm_leaf()
5406       (see err==TYPE_NOTE_TIME_TRUNCATED there).
5407     */
5408     *warnings |= MYSQL_TIME_NOTE_TRUNCATED;
5409     rc = TYPE_NOTE_TIME_TRUNCATED;
5410   }
5411   return rc;
5412 }
5413 
get_time(MYSQL_TIME * ltime) const5414 bool Field_timef::get_time(MYSQL_TIME *ltime) const {
5415   longlong tmp = val_time_temporal();
5416   TIME_from_longlong_time_packed(ltime, tmp);
5417   return false;
5418 }
5419 
5420 /****************************************************************************
5421 ** year type
5422 ** Save in a byte the year 0, 1901->2155
5423 ****************************************************************************/
5424 
store(const char * from,size_t len,const CHARSET_INFO * cs)5425 type_conversion_status Field_year::store(const char *from, size_t len,
5426                                          const CHARSET_INFO *cs) {
5427   ASSERT_COLUMN_MARKED_FOR_WRITE;
5428   const char *end;
5429   int conv_error;
5430   type_conversion_status ret = TYPE_OK;
5431   longlong nr = cs->cset->strntoull10rnd(cs, from, len, 0, &end, &conv_error);
5432 
5433   if (nr < 0 || (nr >= 100 && nr < MIN_YEAR) || nr > MAX_YEAR ||
5434       conv_error == MY_ERRNO_ERANGE) {
5435     *ptr = 0;
5436     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
5437     return TYPE_WARN_OUT_OF_RANGE;
5438   }
5439 
5440   if (conv_error) ret = TYPE_ERR_BAD_VALUE;
5441 
5442   if (table->in_use->check_for_truncated_fields)
5443     ret = check_int(cs, from, len, end, conv_error);
5444 
5445   if (ret != TYPE_OK) {
5446     if (ret == TYPE_ERR_BAD_VALUE) /* empty or incorrect string */
5447     {
5448       *ptr = 0;  // Invalid date
5449       return ret;
5450     }
5451     ret = TYPE_WARN_OUT_OF_RANGE;
5452   }
5453 
5454   if (nr != 0 || len != 4) {
5455     if (nr < YY_PART_YEAR)
5456       nr += 100;  // 2000 - 2069
5457     else if (nr > 1900)
5458       nr -= 1900;
5459   }
5460   *ptr = (char)(uchar)nr;
5461   return ret;
5462 }
5463 
store(double nr)5464 type_conversion_status Field_year::store(double nr) {
5465   if (nr < 0.0 || nr > MAX_YEAR) {
5466     (void)Field_year::store((longlong)-1, false);
5467     return TYPE_WARN_OUT_OF_RANGE;
5468   }
5469   return Field_year::store((longlong)nr, false);
5470 }
5471 
store_time(MYSQL_TIME * ltime,uint8)5472 type_conversion_status Field_year::store_time(MYSQL_TIME *ltime, uint8) {
5473   if (ltime->time_type != MYSQL_TIMESTAMP_DATETIME &&
5474       ltime->time_type != MYSQL_TIMESTAMP_DATE) {
5475     /* Convert time to datetime, then store year of the result */
5476     THD *thd = table ? table->in_use : current_thd;
5477     MYSQL_TIME ltime2;
5478     time_to_datetime(thd, ltime, &ltime2);
5479     return store(ltime2.year, false);
5480   }
5481   return store(ltime->year, false);
5482 }
5483 
store(longlong nr,bool)5484 type_conversion_status Field_year::store(longlong nr, bool) {
5485   ASSERT_COLUMN_MARKED_FOR_WRITE;
5486   if (nr < 0 || (nr >= 100 && nr < MIN_YEAR) || nr > MAX_YEAR) {
5487     *ptr = 0;
5488     set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
5489     return TYPE_WARN_OUT_OF_RANGE;
5490   }
5491   if (nr != 0)  // 0000 -> 0
5492   {
5493     if (nr < YY_PART_YEAR)
5494       nr += 100;  // 2000 - 2069
5495     else if (nr > 1900)
5496       nr -= 1900;
5497   }
5498   *ptr = (char)(uchar)nr;
5499   return TYPE_OK;
5500 }
5501 
send_to_protocol(Protocol * protocol) const5502 bool Field_year::send_to_protocol(Protocol *protocol) const {
5503   ASSERT_COLUMN_MARKED_FOR_READ;
5504   if (is_null()) return protocol->store_null();
5505   // YEAR is always ZEROFILL. Always zero-pad values up to 4 digits.
5506   DBUG_ASSERT(zerofill);
5507   ulonglong tmp = Field_year::val_int();
5508   return protocol->store_short(tmp, field_length);
5509 }
5510 
val_real() const5511 double Field_year::val_real() const { return (double)Field_year::val_int(); }
5512 
val_int() const5513 longlong Field_year::val_int() const {
5514   ASSERT_COLUMN_MARKED_FOR_READ;
5515   DBUG_ASSERT(field_length == 4);
5516   int tmp = (int)ptr[0];
5517   if (tmp != 0) tmp += 1900;
5518   return (longlong)tmp;
5519 }
5520 
val_str(String * val_buffer,String *) const5521 String *Field_year::val_str(String *val_buffer, String *) const {
5522   DBUG_ASSERT(field_length == 4);
5523   val_buffer->length(0);
5524   const longlong year = val_int();
5525   // YEAR is always ZEROFILL. Always zero-pad values up to 4 digits.
5526   DBUG_ASSERT(zerofill);
5527   if (year == 0)
5528     val_buffer->fill(field_length, '0');
5529   else  // If year != 0, year is always 4 digits
5530     val_buffer->append_longlong(year);
5531   val_buffer->set_charset(&my_charset_numeric);
5532   return val_buffer;
5533 }
5534 
sql_type(String & res) const5535 void Field_year::sql_type(String &res) const {
5536   res.length(0);
5537   res.append(STRING_WITH_LEN("year"));
5538 }
5539 
5540 /****************************************************************************
5541 ** The new date type
5542 ** Stored as 3 bytes
5543 ** In number context: YYYYMMDD
5544 ****************************************************************************/
5545 
date_flags(const THD * thd) const5546 my_time_flags_t Field_newdate::date_flags(const THD *thd) const {
5547   my_time_flags_t date_flags = TIME_FUZZY_DATE;
5548   if (thd->variables.sql_mode & MODE_NO_ZERO_DATE)
5549     date_flags |= TIME_NO_ZERO_DATE;
5550   if (thd->variables.sql_mode & MODE_NO_ZERO_IN_DATE)
5551     date_flags |= TIME_NO_ZERO_IN_DATE;
5552   if (thd->variables.sql_mode & MODE_INVALID_DATES)
5553     date_flags |= TIME_INVALID_DATES;
5554   if (thd->variables.sql_mode & MODE_TIME_TRUNCATE_FRACTIONAL)
5555     date_flags |= TIME_FRAC_TRUNCATE;
5556 
5557   return date_flags;
5558 }
5559 
store_internal(const MYSQL_TIME * ltime,int * warnings)5560 type_conversion_status Field_newdate::store_internal(const MYSQL_TIME *ltime,
5561                                                      int *warnings) {
5562   my_date_to_binary(ltime, ptr);
5563   if (non_zero_time(*ltime)) {
5564     *warnings |= MYSQL_TIME_NOTE_TRUNCATED;
5565     return TYPE_NOTE_TIME_TRUNCATED;
5566   }
5567   return TYPE_OK;
5568 }
5569 
get_date_internal(MYSQL_TIME * ltime) const5570 bool Field_newdate::get_date_internal(MYSQL_TIME *ltime) const {
5571   uint32 tmp = uint3korr(ptr);
5572   ltime->day = tmp & 31;
5573   ltime->month = (tmp >> 5) & 15;
5574   ltime->year = (tmp >> 9);
5575   ltime->time_type = MYSQL_TIMESTAMP_DATE;
5576   ltime->hour = ltime->minute = ltime->second = ltime->second_part =
5577       ltime->neg = false;
5578   ltime->time_zone_displacement = 0;
5579   return false;
5580 }
5581 
store_packed(longlong nr)5582 type_conversion_status Field_newdate::store_packed(longlong nr) {
5583   int warnings = 0;
5584   MYSQL_TIME ltime;
5585   TIME_from_longlong_date_packed(&ltime, nr);
5586   return store_internal(&ltime, &warnings);
5587 }
5588 
send_to_protocol(Protocol * protocol) const5589 bool Field_newdate::send_to_protocol(Protocol *protocol) const {
5590   if (is_null()) return protocol->store_null();
5591   MYSQL_TIME ltime;
5592   get_date(&ltime, 0);
5593   return protocol->store_date(ltime);
5594 }
5595 
val_int() const5596 longlong Field_newdate::val_int() const {
5597   ASSERT_COLUMN_MARKED_FOR_READ;
5598   ulong j = uint3korr(ptr);
5599   j = (j % 32L) + (j / 32L % 16L) * 100L + (j / (16L * 32L)) * 10000L;
5600   return (longlong)j;
5601 }
5602 
val_date_temporal() const5603 longlong Field_newdate::val_date_temporal() const {
5604   ASSERT_COLUMN_MARKED_FOR_READ;
5605   MYSQL_TIME ltime;
5606   return get_date_internal(&ltime) ? 0 : TIME_to_longlong_date_packed(ltime);
5607 }
5608 
val_time_temporal() const5609 longlong Field_newdate::val_time_temporal() const {
5610   ASSERT_COLUMN_MARKED_FOR_READ;
5611   return 0;
5612 }
5613 
val_str(String * val_buffer,String *) const5614 String *Field_newdate::val_str(String *val_buffer, String *) const {
5615   ASSERT_COLUMN_MARKED_FOR_READ;
5616   val_buffer->alloc(field_length);
5617   val_buffer->length(field_length);
5618   uint32 tmp = uint3korr(ptr);
5619   int part;
5620   char *pos = val_buffer->ptr() + 10;
5621 
5622   /* Open coded to get more speed */
5623   *pos-- = 0;  // End NULL
5624   part = (int)(tmp & 31);
5625   *pos-- = (char)('0' + part % 10);
5626   *pos-- = (char)('0' + part / 10);
5627   *pos-- = '-';
5628   part = (int)(tmp >> 5 & 15);
5629   *pos-- = (char)('0' + part % 10);
5630   *pos-- = (char)('0' + part / 10);
5631   *pos-- = '-';
5632   part = (int)(tmp >> 9);
5633   *pos-- = (char)('0' + part % 10);
5634   part /= 10;
5635   *pos-- = (char)('0' + part % 10);
5636   part /= 10;
5637   *pos-- = (char)('0' + part % 10);
5638   part /= 10;
5639   *pos = (char)('0' + part);
5640   val_buffer->set_charset(&my_charset_numeric);
5641   return val_buffer;
5642 }
5643 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const5644 bool Field_newdate::get_date(MYSQL_TIME *ltime,
5645                              my_time_flags_t fuzzydate) const {
5646   return get_internal_check_zero(ltime, fuzzydate) ||
5647          check_fuzzy_date(*ltime, fuzzydate);
5648 }
5649 
cmp(const uchar * a_ptr,const uchar * b_ptr) const5650 int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
5651   uint32 a, b;
5652   a = uint3korr(a_ptr);
5653   b = uint3korr(b_ptr);
5654   return (a < b) ? -1 : (a > b) ? 1 : 0;
5655 }
5656 
make_sort_key(uchar * to,size_t length) const5657 size_t Field_newdate::make_sort_key(uchar *to, size_t length) const {
5658   memset(to, 0, length);
5659   to[0] = ptr[2];
5660   to[1] = ptr[1];
5661   to[2] = ptr[0];
5662   return 3;
5663 }
5664 
sql_type(String & res) const5665 void Field_newdate::sql_type(String &res) const {
5666   res.set_ascii(STRING_WITH_LEN("date"));
5667 }
5668 
5669 /****************************************************************************
5670 ** datetime type
5671 ** In string context: YYYY-MM-DD HH:MM:DD
5672 ** In number context: YYYYMMDDHHMMDD
5673 ** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
5674 ****************************************************************************/
5675 
date_flags(const THD * thd) const5676 my_time_flags_t Field_datetime::date_flags(const THD *thd) const {
5677   my_time_flags_t date_flags = TIME_FUZZY_DATE;
5678   if (thd->variables.sql_mode & MODE_NO_ZERO_DATE)
5679     date_flags |= TIME_NO_ZERO_DATE;
5680   if (thd->variables.sql_mode & MODE_NO_ZERO_IN_DATE)
5681     date_flags |= TIME_NO_ZERO_IN_DATE;
5682   if (thd->variables.sql_mode & MODE_INVALID_DATES)
5683     date_flags |= TIME_INVALID_DATES;
5684   if (thd->variables.sql_mode & MODE_TIME_TRUNCATE_FRACTIONAL)
5685     date_flags |= TIME_FRAC_TRUNCATE;
5686 
5687   return date_flags;
5688 }
5689 
store_timestamp_internal(const timeval * tm)5690 void Field_datetime::store_timestamp_internal(const timeval *tm) {
5691   MYSQL_TIME mysql_time;
5692   THD *thd = current_thd;
5693   thd->variables.time_zone->gmt_sec_to_TIME(&mysql_time, *tm);
5694   thd->time_zone_used = true;
5695   int error = 0;
5696   store_internal(&mysql_time, &error);
5697 }
5698 
5699 /**
5700   Store a DATETIME in a 8-byte integer to record.
5701 
5702   @param table  Table
5703   @param tmp    The number, in YYYYMMDDhhmmss format
5704   @param ptr    Where to store to
5705 */
datetime_store_internal(TABLE * table,ulonglong tmp,uchar * ptr)5706 static inline type_conversion_status datetime_store_internal(TABLE *table,
5707                                                              ulonglong tmp,
5708                                                              uchar *ptr) {
5709   if (table && table->s->db_low_byte_first)
5710     int8store(ptr, tmp);
5711   else
5712     longlongstore(ptr, tmp);
5713   return TYPE_OK;
5714 }
5715 
5716 /**
5717   Read a DATETIME from record to a 8-byte integer
5718 
5719   @param table  Table
5720   @param ptr    Where to read from
5721   @retval       An integer in format YYYYMMDDhhmmss
5722 */
datetime_get_internal(TABLE * table,uchar * ptr)5723 static inline longlong datetime_get_internal(TABLE *table, uchar *ptr) {
5724   if (table && table->s->db_low_byte_first)
5725     return sint8korr(ptr);
5726   else
5727     return longlongget(ptr);
5728 }
5729 
get_date_internal(MYSQL_TIME * ltime) const5730 bool Field_datetime::get_date_internal(MYSQL_TIME *ltime) const {
5731   longlong tmp = datetime_get_internal(table, ptr);
5732   ltime->time_type = MYSQL_TIMESTAMP_DATETIME;
5733   ltime->neg = false;
5734   ltime->second_part = 0;
5735   TIME_set_yymmdd(ltime, (uint)(tmp / 1000000LL));
5736   TIME_set_hhmmss(ltime, (uint)(tmp % 1000000LL));
5737   ltime->time_zone_displacement = 0;
5738   return false;
5739 }
5740 
store_internal(const MYSQL_TIME * ltime,int *)5741 type_conversion_status Field_datetime::store_internal(const MYSQL_TIME *ltime,
5742                                                       int *) {
5743   ulonglong tmp = TIME_to_ulonglong_datetime(*ltime);
5744   return datetime_store_internal(table, tmp, ptr);
5745 }
5746 
store(longlong nr,bool unsigned_val)5747 type_conversion_status Field_datetime::store(longlong nr, bool unsigned_val) {
5748   ASSERT_COLUMN_MARKED_FOR_WRITE;
5749   MYSQL_TIME ltime;
5750   int warnings;
5751   type_conversion_status error = TYPE_OK;
5752   longlong tmp =
5753       convert_number_to_datetime(nr, unsigned_val, &ltime, &warnings);
5754   if (tmp == -1LL)
5755     error = TYPE_ERR_BAD_VALUE;
5756   else {
5757     error = time_warning_to_type_conversion_status(warnings);
5758     datetime_store_internal(table, tmp, ptr);
5759   }
5760   if (warnings && set_warnings(ErrConvString(nr, unsigned_val), warnings))
5761     error = TYPE_ERR_BAD_VALUE;
5762   return error;
5763 }
5764 
store_packed(longlong nr)5765 type_conversion_status Field_datetime::store_packed(longlong nr) {
5766   MYSQL_TIME ltime;
5767   TIME_from_longlong_datetime_packed(&ltime, nr);
5768   return Field_datetime::store_time(&ltime, 0);
5769 }
5770 
val_int() const5771 longlong Field_datetime::val_int() const {
5772   ASSERT_COLUMN_MARKED_FOR_READ;
5773   return datetime_get_internal(table, ptr);
5774 }
5775 
5776 /*
5777   We don't reuse the parent method for performance purposes,
5778   to avoid convertion from number to MYSQL_TIME.
5779   Using my_datetime_number_to_str() instead of my_datetime_to_str().
5780 */
val_str(String * val_buffer,String *) const5781 String *Field_datetime::val_str(String *val_buffer, String *) const {
5782   ASSERT_COLUMN_MARKED_FOR_READ;
5783   val_buffer->alloc(field_length + 1);
5784   val_buffer->set_charset(&my_charset_numeric);
5785   val_buffer->length(MAX_DATETIME_WIDTH);
5786   longlong tmp = datetime_get_internal(table, ptr);
5787   val_buffer->length(my_datetime_number_to_str(val_buffer->ptr(), tmp));
5788   return val_buffer;
5789 }
5790 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const5791 bool Field_datetime::get_date(MYSQL_TIME *ltime,
5792                               my_time_flags_t fuzzydate) const {
5793   return get_internal_check_zero(ltime, fuzzydate) ||
5794          check_fuzzy_date(*ltime, fuzzydate);
5795 }
5796 
cmp(const uchar * a_ptr,const uchar * b_ptr) const5797 int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
5798   longlong a, b;
5799   if (table && table->s->db_low_byte_first) {
5800     a = sint8korr(a_ptr);
5801     b = sint8korr(b_ptr);
5802   } else {
5803     a = longlongget(a_ptr);
5804     b = longlongget(b_ptr);
5805   }
5806   return ((ulonglong)a < (ulonglong)b) ? -1
5807                                        : ((ulonglong)a > (ulonglong)b) ? 1 : 0;
5808 }
5809 
make_sort_key(uchar * to,size_t length) const5810 size_t Field_datetime::make_sort_key(uchar *to, size_t length) const {
5811   DBUG_ASSERT(length == PACK_LENGTH);
5812 #ifdef WORDS_BIGENDIAN
5813   if (!table || !table->s->db_low_byte_first)
5814     copy_integer<true>(to, length, ptr, PACK_LENGTH, true);
5815   else
5816 #endif
5817     copy_integer<false>(to, length, ptr, PACK_LENGTH, true);
5818   return PACK_LENGTH;
5819 }
5820 
sql_type(String & res) const5821 void Field_datetime::sql_type(String &res) const {
5822   res.set_ascii(STRING_WITH_LEN("datetime"));
5823 }
5824 
5825 /****************************************************************************
5826 ** datetimef type
5827 ** In string context: YYYY-MM-DD HH:MM:DD.FFFFFF
5828 ** In number context: YYYYMMDDHHMMDD.FFFFFF
5829 ** Stored as a 8 byte value.
5830 ****************************************************************************/
5831 
date_flags(const THD * thd) const5832 my_time_flags_t Field_datetimef::date_flags(const THD *thd) const {
5833   my_time_flags_t date_flags = TIME_FUZZY_DATE;
5834   if (thd->variables.sql_mode & MODE_NO_ZERO_DATE)
5835     date_flags |= TIME_NO_ZERO_DATE;
5836   if (thd->variables.sql_mode & MODE_NO_ZERO_IN_DATE)
5837     date_flags |= TIME_NO_ZERO_IN_DATE;
5838   if (thd->variables.sql_mode & MODE_INVALID_DATES)
5839     date_flags |= TIME_INVALID_DATES;
5840   if (thd->variables.sql_mode & MODE_TIME_TRUNCATE_FRACTIONAL)
5841     date_flags |= TIME_FRAC_TRUNCATE;
5842 
5843   return date_flags;
5844 }
5845 
store_timestamp_internal(const timeval * tm)5846 void Field_datetimef::store_timestamp_internal(const timeval *tm) {
5847   MYSQL_TIME mysql_time;
5848   THD *thd = current_thd;
5849   thd->variables.time_zone->gmt_sec_to_TIME(&mysql_time, *tm);
5850   thd->time_zone_used = true;
5851   int warnings = 0;
5852   store_internal(&mysql_time, &warnings);
5853 }
5854 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate) const5855 bool Field_datetimef::get_date(MYSQL_TIME *ltime,
5856                                my_time_flags_t fuzzydate) const {
5857   return get_internal_check_zero(ltime, fuzzydate) ||
5858          check_fuzzy_date(*ltime, fuzzydate);
5859 }
5860 
sql_type(String & res) const5861 void Field_datetimef::sql_type(String &res) const {
5862   if (dec == 0) {
5863     res.set_ascii(STRING_WITH_LEN("datetime"));
5864     return;
5865   }
5866   const CHARSET_INFO *cs = res.charset();
5867   res.length(cs->cset->snprintf(cs, res.ptr(), res.alloced_length(),
5868                                 "datetime(%d)", dec));
5869 }
5870 
get_date_internal(MYSQL_TIME * ltime) const5871 bool Field_datetimef::get_date_internal(MYSQL_TIME *ltime) const {
5872   TIME_from_longlong_datetime_packed(ltime, val_date_temporal());
5873   return false;
5874 }
5875 
store_internal(const MYSQL_TIME * ltime,int *)5876 type_conversion_status Field_datetimef::store_internal(const MYSQL_TIME *ltime,
5877                                                        int *) {
5878   /*
5879     If time zone displacement information is present in "ltime"
5880     - adjust the value to UTC based on the time zone
5881     - convert to the local time zone
5882   */
5883   MYSQL_TIME temp_t = *ltime;
5884   adjust_time_zone_displacement(current_thd->time_zone(), &temp_t);
5885   store_packed(TIME_to_longlong_datetime_packed(temp_t));
5886 
5887   return TYPE_OK;
5888 }
5889 
reset()5890 type_conversion_status Field_datetimef::reset() {
5891   store_packed(0);
5892   return TYPE_OK;
5893 }
5894 
val_date_temporal() const5895 longlong Field_datetimef::val_date_temporal() const {
5896   return my_datetime_packed_from_binary(ptr, dec);
5897 }
5898 
store_packed(longlong nr)5899 type_conversion_status Field_datetimef::store_packed(longlong nr) {
5900   my_datetime_packed_to_binary(nr, ptr, dec);
5901   return TYPE_OK;
5902 }
5903 
5904 /****************************************************************************
5905 ** string type
5906 ** A string may be varchar or binary
5907 ****************************************************************************/
5908 
5909 /**
5910   Report "not well formed" or "cannot convert" error
5911   after storing a character string info a field.
5912 
5913   As of version 5.0 both cases return the same error:
5914 
5915       "Invalid string value: 'xxx' for column 't' at row 1"
5916 
5917   Future versions will possibly introduce a new error message:
5918 
5919       "Cannot convert character string: 'xxx' for column 't' at row 1"
5920 
5921   @param  well_formed_error_pos      position of the first non-wellformed
5922                                      character in the source string
5923   @param  cannot_convert_error_pos   position of the first non-convertable
5924                                      character in the source string
5925   @param  from_end_pos               position where conversion stopped in
5926                                      the source string
5927   @param  end                        end of the source string
5928   @param  count_spaces               treat trailing spaces as important data
5929   @param  cs                         character set of the string
5930 
5931   @return TYPE_OK, TYPE_NOTE_TRUNCATED, TYPE_WARN_TRUNCATED,
5932           TYPE_WARN_INVALID_STRING
5933 
5934 */
5935 
check_string_copy_error(const char * well_formed_error_pos,const char * cannot_convert_error_pos,const char * from_end_pos,const char * end,bool count_spaces,const CHARSET_INFO * cs)5936 type_conversion_status Field_longstr::check_string_copy_error(
5937     const char *well_formed_error_pos, const char *cannot_convert_error_pos,
5938     const char *from_end_pos, const char *end, bool count_spaces,
5939     const CHARSET_INFO *cs) {
5940   const char *pos;
5941   char tmp[32];
5942   THD *thd = table->in_use;
5943 
5944   if (!(pos = well_formed_error_pos) && !(pos = cannot_convert_error_pos))
5945     return report_if_important_data(from_end_pos, end, count_spaces);
5946 
5947   convert_to_printable(tmp, sizeof(tmp), pos, (end - pos), cs, 6);
5948 
5949   push_warning_printf(
5950       thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
5951       ER_THD(thd, ER_TRUNCATED_WRONG_VALUE_FOR_FIELD), "string", tmp,
5952       field_name, thd->get_stmt_da()->current_row_for_condition());
5953 
5954   if (well_formed_error_pos != nullptr) return TYPE_WARN_INVALID_STRING;
5955 
5956   return TYPE_WARN_TRUNCATED;
5957 }
5958 
5959 /*
5960   Check if we lost any important data and send a truncation error/warning
5961 
5962   SYNOPSIS
5963     Field_longstr::report_if_important_data()
5964     pstr                     - Truncated rest of string
5965     end                      - End of truncated string
5966     count_spaces             - Treat traling spaces as important data
5967 
5968   RETURN VALUES
5969     TYPE_OK    - None was truncated
5970     != TYPE_OK - Some bytes were truncated
5971 
5972   NOTE
5973     Check if we lost any important data (anything in a binary string,
5974     or any non-space in others). If only trailing spaces was lost,
5975     send a truncation note, otherwise send a truncation error.
5976     Silently ignore traling spaces if the count_space parameter is false.
5977 */
5978 
report_if_important_data(const char * pstr,const char * end,bool count_spaces)5979 type_conversion_status Field_longstr::report_if_important_data(
5980     const char *pstr, const char *end, bool count_spaces) {
5981   if (pstr < end)  // String is truncated
5982   {
5983     if (test_if_important_data(field_charset, pstr, end)) {
5984       // Warning should only be written when check_for_truncated_fields is set
5985       if (table->in_use->check_for_truncated_fields) {
5986         if (!table->in_use->lex->is_ignore() && table->in_use->is_strict_mode())
5987           set_warning(Sql_condition::SL_WARNING, ER_DATA_TOO_LONG, 1);
5988         else
5989           set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
5990       }
5991       return TYPE_WARN_TRUNCATED;
5992     } else if (count_spaces) {
5993       // If we lost only spaces then produce a NOTE, not a WARNING
5994       if (table->in_use->check_for_truncated_fields) {
5995         set_warning(Sql_condition::SL_NOTE, WARN_DATA_TRUNCATED, 1);
5996       }
5997       return TYPE_NOTE_TRUNCATED;
5998     }
5999   }
6000   return TYPE_OK;
6001 }
6002 
6003 /* Copy a string and fill with space */
6004 
store(const char * from,size_t length,const CHARSET_INFO * cs)6005 type_conversion_status Field_string::store(const char *from, size_t length,
6006                                            const CHARSET_INFO *cs) {
6007   ASSERT_COLUMN_MARKED_FOR_WRITE;
6008   size_t copy_length;
6009   const char *well_formed_error_pos;
6010   const char *cannot_convert_error_pos;
6011   const char *from_end_pos;
6012 
6013   /* See the comment for Field_long::store(long long) */
6014   DBUG_ASSERT(table->in_use == current_thd);
6015 
6016   copy_length = field_well_formed_copy_nchars(
6017       field_charset, (char *)ptr, field_length, cs, from, length,
6018       field_length / field_charset->mbmaxlen, &well_formed_error_pos,
6019       &cannot_convert_error_pos, &from_end_pos);
6020 
6021   /* Append spaces if the string was shorter than the field. */
6022   if (copy_length < field_length)
6023     field_charset->cset->fill(field_charset, (char *)ptr + copy_length,
6024                               field_length - copy_length,
6025                               field_charset->pad_char);
6026 
6027   return check_string_copy_error(well_formed_error_pos,
6028                                  cannot_convert_error_pos, from_end_pos,
6029                                  from + length, false, cs);
6030 }
6031 
6032 /**
6033   Store double value in Field_string or Field_varstring.
6034 
6035   Pretty prints double number into field_length characters buffer.
6036 
6037   @param nr            number
6038 */
6039 
store(double nr)6040 type_conversion_status Field_str::store(double nr) {
6041   ASSERT_COLUMN_MARKED_FOR_WRITE;
6042   char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
6043   uint local_char_length = field_length / charset()->mbmaxlen;
6044   size_t length = 0;
6045   bool error = (local_char_length == 0);
6046 
6047   // my_gcvt() requires width > 0, and we may have a CHAR(0) column.
6048   if (!error)
6049     length = my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
6050 
6051   if (error) {
6052     if (!table->in_use->lex->is_ignore() && table->in_use->is_strict_mode())
6053       set_warning(Sql_condition::SL_WARNING, ER_DATA_TOO_LONG, 1);
6054     else
6055       set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
6056   }
6057   return store(buff, length, &my_charset_numeric);
6058 }
6059 
6060 /**
6061   Check whether generated columns' expressions are the same.
6062 
6063   @param field  A new field to compare against
6064 
6065   @return true means the same, otherwise not.
6066 */
6067 
gcol_expr_is_equal(const Create_field * field) const6068 bool Field::gcol_expr_is_equal(const Create_field *field) const {
6069   DBUG_ASSERT(is_gcol() && field->is_gcol());
6070   return gcol_info->expr_item->eq(field->gcol_info->expr_item, true);
6071 }
6072 
is_equal(const Create_field * new_field) const6073 uint Field_str::is_equal(const Create_field *new_field) const {
6074   DBUG_TRACE;
6075 
6076   if (change_prevents_inplace(*this, *new_field)) {
6077     return IS_EQUAL_NO;
6078   }
6079 
6080   size_t new_char_len = new_field->max_display_width_in_codepoints();
6081   if (new_char_len != char_length()  // Changed char len cannot be done
6082                                      // inplace due to padding
6083   ) {
6084     return IS_EQUAL_NO;
6085   }
6086 
6087   if (new_field->charset == field_charset) {
6088     return IS_EQUAL_YES;
6089   }
6090 
6091   return IS_EQUAL_PACK_LENGTH;
6092 }
6093 
store(longlong nr,bool unsigned_val)6094 type_conversion_status Field_string::store(longlong nr, bool unsigned_val) {
6095   char buff[64];
6096   size_t l;
6097   const CHARSET_INFO *cs = charset();
6098   l = (cs->cset->longlong10_to_str)(cs, buff, sizeof(buff),
6099                                     unsigned_val ? 10 : -10, nr);
6100   return Field_string::store(buff, l, cs);
6101 }
6102 
store_decimal(const my_decimal * d)6103 type_conversion_status Field_longstr::store_decimal(const my_decimal *d) {
6104   StringBuffer<DECIMAL_MAX_STR_LENGTH + 1> str(&my_charset_numeric);
6105   my_decimal2string(E_DEC_FATAL_ERROR, d, &str);
6106   return store(str.ptr(), str.length(), str.charset());
6107 }
6108 
max_data_length() const6109 uint32 Field_longstr::max_data_length() const {
6110   return field_length + (field_length > 255 ? 2 : 1);
6111 }
6112 
val_real() const6113 double Field_string::val_real() const {
6114   ASSERT_COLUMN_MARKED_FOR_READ;
6115   int error;
6116   const char *end;
6117   const CHARSET_INFO *cs = charset();
6118   double result;
6119 
6120   result = my_strntod(cs, (char *)ptr, field_length, &end, &error);
6121   if ((error ||
6122        (field_length != (uint32)(end - (char *)ptr) &&
6123         !check_if_only_end_space(cs, end, (char *)ptr + field_length)))) {
6124     size_t length =
6125         cs->cset->lengthsp(cs, pointer_cast<const char *>(ptr), field_length);
6126     ErrConvString err((char *)ptr, length, cs);
6127     push_warning_printf(
6128         current_thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE,
6129         ER_THD(current_thd, ER_TRUNCATED_WRONG_VALUE), "DOUBLE", err.ptr());
6130   }
6131   return result;
6132 }
6133 
val_int() const6134 longlong Field_string::val_int() const {
6135   ASSERT_COLUMN_MARKED_FOR_READ;
6136   int error;
6137   const char *end;
6138   const CHARSET_INFO *cs = charset();
6139   longlong result;
6140 
6141   result = my_strntoll(cs, (char *)ptr, field_length, 10, &end, &error);
6142   if ((error ||
6143        (field_length != (uint32)(end - (char *)ptr) &&
6144         !check_if_only_end_space(cs, end, (char *)ptr + field_length)))) {
6145     size_t length =
6146         cs->cset->lengthsp(cs, pointer_cast<const char *>(ptr), field_length);
6147     ErrConvString err((char *)ptr, length, cs);
6148     push_warning_printf(
6149         current_thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE,
6150         ER_THD(current_thd, ER_TRUNCATED_WRONG_VALUE), "INTEGER", err.ptr());
6151   }
6152   return result;
6153 }
6154 
val_str(String *,String * val_ptr) const6155 String *Field_string::val_str(String *, String *val_ptr) const {
6156   ASSERT_COLUMN_MARKED_FOR_READ;
6157   /* See the comment for Field_long::store(long long) */
6158   DBUG_ASSERT(table->in_use == current_thd);
6159   size_t length;
6160   if (table->in_use->variables.sql_mode & MODE_PAD_CHAR_TO_FULL_LENGTH)
6161     length = my_charpos(field_charset, ptr, ptr + field_length,
6162                         field_length / field_charset->mbmaxlen);
6163   else
6164     length = field_charset->cset->lengthsp(field_charset, (const char *)ptr,
6165                                            field_length);
6166   val_ptr->set((const char *)ptr, length, field_charset);
6167   return val_ptr;
6168 }
6169 
val_decimal(my_decimal * decimal_value) const6170 my_decimal *Field_string::val_decimal(my_decimal *decimal_value) const {
6171   ASSERT_COLUMN_MARKED_FOR_READ;
6172   const CHARSET_INFO *cs = charset();
6173   int err = str2my_decimal(E_DEC_FATAL_ERROR, (char *)ptr, field_length, cs,
6174                            decimal_value);
6175   if (err) {
6176     size_t length =
6177         cs->cset->lengthsp(cs, pointer_cast<const char *>(ptr), field_length);
6178     ErrConvString errmsg((char *)ptr, length, cs);
6179     push_warning_printf(
6180         current_thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE,
6181         ER_THD(current_thd, ER_TRUNCATED_WRONG_VALUE), "DECIMAL", errmsg.ptr());
6182   }
6183 
6184   return decimal_value;
6185 }
6186 
6187 struct Check_field_param {
6188   const Field *field;
6189 };
6190 
check_field_for_37426(const void * param_arg)6191 static bool check_field_for_37426(const void *param_arg) {
6192   const Check_field_param *param =
6193       static_cast<const Check_field_param *>(param_arg);
6194   DBUG_ASSERT(param->field->real_type() == MYSQL_TYPE_STRING);
6195   DBUG_PRINT("debug",
6196              ("Field %s - type: %d, size: %d", param->field->field_name,
6197               param->field->real_type(), param->field->row_pack_length()));
6198   return param->field->row_pack_length() > 255;
6199 }
6200 
compatible_field_size(uint field_metadata,Relay_log_info * rli_arg,uint16 mflags,int * order_var) const6201 bool Field_string::compatible_field_size(uint field_metadata,
6202                                          Relay_log_info *rli_arg, uint16 mflags,
6203                                          int *order_var) const {
6204   const Check_field_param check_param = {this};
6205   if (!is_mts_worker(rli_arg->info_thd) &&
6206       rpl_master_has_bug(rli_arg, 37426, true, check_field_for_37426,
6207                          &check_param))
6208     return false;  // Not compatible field sizes
6209   return Field::compatible_field_size(field_metadata, rli_arg, mflags,
6210                                       order_var);
6211 }
6212 
cmp(const uchar * a_ptr,const uchar * b_ptr) const6213 int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
6214   size_t a_len, b_len;
6215 
6216   if (field_charset->mbmaxlen != 1) {
6217     uint char_len = field_length / field_charset->mbmaxlen;
6218     a_len = my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
6219     b_len = my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
6220   } else
6221     a_len = b_len = field_length;
6222 
6223   if (field_charset->pad_attribute == NO_PAD &&
6224       !(table->in_use->variables.sql_mode & MODE_PAD_CHAR_TO_FULL_LENGTH)) {
6225     /*
6226       Our CHAR default behavior is to strip spaces. For PAD SPACE collations,
6227       this doesn't matter, for but NO PAD, we need to do it ourselves here.
6228     */
6229     a_len = field_charset->cset->lengthsp(field_charset, (const char *)a_ptr,
6230                                           a_len);
6231     b_len = field_charset->cset->lengthsp(field_charset, (const char *)b_ptr,
6232                                           b_len);
6233   }
6234 
6235   return field_charset->coll->strnncollsp(field_charset, a_ptr, a_len, b_ptr,
6236                                           b_len);
6237 }
6238 
make_sort_key(uchar * to,size_t length) const6239 size_t Field_string::make_sort_key(uchar *to, size_t length) const {
6240   /*
6241     We don't store explicitly how many bytes long this string is.
6242     Find out by calling charpos, since just using field_length
6243     could give strnxfrm a buffer with more than char_length() code
6244     points, which is not allowed.
6245 
6246     The min() is because charpos() is allowed to return a value past
6247     the end of the string for “end of string”.
6248   */
6249   size_t input_length = std::min<size_t>(
6250       field_length,
6251       field_charset->cset->charpos(
6252           field_charset, pointer_cast<const char *>(ptr),
6253           pointer_cast<const char *>(ptr) + field_length, char_length()));
6254 
6255   if (field_charset->pad_attribute == NO_PAD &&
6256       !(table->in_use->variables.sql_mode & MODE_PAD_CHAR_TO_FULL_LENGTH)) {
6257     /*
6258       Our CHAR default behavior is to strip spaces. For PAD SPACE collations,
6259       this doesn't matter, for but NO PAD, we need to do it ourselves here.
6260     */
6261     input_length = field_charset->cset->lengthsp(
6262         field_charset, (const char *)ptr, input_length);
6263   }
6264 
6265   DBUG_ASSERT(char_length_cache == char_length());
6266   size_t tmp MY_ATTRIBUTE((unused)) = field_charset->coll->strnxfrm(
6267       field_charset, to, length, char_length_cache, ptr, input_length,
6268       MY_STRXFRM_PAD_TO_MAXLEN);
6269   DBUG_ASSERT(tmp == length);
6270   return length;
6271 }
6272 
sql_type(String & res) const6273 void Field_string::sql_type(String &res) const {
6274   THD *thd = table->in_use;
6275   const CHARSET_INFO *cs = res.charset();
6276   size_t length;
6277 
6278   length = cs->cset->snprintf(
6279       cs, res.ptr(), res.alloced_length(), "%s(%d)",
6280       ((type() == MYSQL_TYPE_VAR_STRING && !thd->variables.new_mode)
6281            ? (has_charset() ? "varchar" : "varbinary")
6282            : (has_charset() ? "char" : "binary")),
6283       (int)field_length / charset()->mbmaxlen);
6284   res.length(length);
6285 }
6286 
pack(uchar * to,const uchar * from,size_t max_length) const6287 uchar *Field_string::pack(uchar *to, const uchar *from,
6288                           size_t max_length) const {
6289   uint length = my_charpos(field_charset, from, from + field_length,
6290                            field_length / field_charset->mbmaxlen);
6291   uint length_bytes = (field_length > 255) ? 2 : 1;
6292 
6293   /*
6294      TODO: change charset interface to add a new function that does
6295            the following or add a flag to lengthsp to do it itself
6296            (this is for not packing padding adding bytes in BINARY
6297            fields).
6298   */
6299   if (field_charset->mbmaxlen == 1) {
6300     while (length && from[length - 1] == field_charset->pad_char) length--;
6301   } else
6302     length = field_charset->cset->lengthsp(field_charset, (const char *)from,
6303                                            length);
6304 
6305   if (max_length < length_bytes)
6306     length = 0;
6307   else if (length > max_length - length_bytes)
6308     length = max_length - length_bytes;
6309 
6310   /* Length always stored little-endian */
6311   if (max_length >= 1) {
6312     *to++ = length & 0xFF;
6313     if (length_bytes == 2 && max_length >= 2) *to++ = (length >> 8) & 0xFF;
6314   }
6315 
6316   /* Store bytes of string */
6317   memcpy(to, from, length);
6318 
6319   return to + length;
6320 }
6321 
6322 /**
6323    Unpack a string field from row data.
6324 
6325    This method is used to unpack a string field from a master whose size
6326    of the field is less than that of the slave. Note that there can be a
6327    variety of field types represented with this class. Certain types like
6328    ENUM or SET are processed differently. Hence, the upper byte of the
6329    @c param_data argument contains the result of field->real_type() from
6330    the master.
6331 
6332    @note For information about how the length is packed, see @c
6333    Field_string::do_save_field_metadata
6334 
6335    @param   to         Destination of the data
6336    @param   from       Source of the data
6337    @param   param_data Real type (upper) and length (lower) values
6338 
6339    @return  New pointer into memory based on from + length of the data
6340 */
unpack(uchar * to,const uchar * from,uint param_data)6341 const uchar *Field_string::unpack(uchar *to, const uchar *from,
6342                                   uint param_data) {
6343   uint from_length, length;
6344 
6345   /*
6346     Compute the declared length of the field on the master. This is
6347     used to decide if one or two bytes should be read as length.
6348    */
6349   if (param_data)
6350     from_length = (((param_data >> 4) & 0x300) ^ 0x300) + (param_data & 0x00ff);
6351   else
6352     from_length = field_length;
6353 
6354   DBUG_PRINT("debug", ("param_data: 0x%x, field_length: %u, from_length: %u",
6355                        param_data, field_length, from_length));
6356   /*
6357     Compute the actual length of the data by reading one or two bits
6358     (depending on the declared field length on the master).
6359    */
6360   if (from_length > 255) {
6361     length = uint2korr(from);
6362     from += 2;
6363   } else
6364     length = (uint)*from++;
6365 
6366   memcpy(to, from, length);
6367   // Pad the string with the pad character of the fields charset
6368   field_charset->cset->fill(field_charset, (char *)to + length,
6369                             field_length - length, field_charset->pad_char);
6370   return from + length;
6371 }
6372 
6373 /**
6374    Save the field metadata for string fields.
6375 
6376    Saves the real type in the first byte and the field length in the
6377    second byte of the field metadata array at index of *metadata_ptr and
6378    *(metadata_ptr + 1).
6379 
6380    @note In order to be able to handle lengths exceeding 255 and be
6381    backwards-compatible with pre-5.1.26 servers, an extra two bits of
6382    the length has been added to the metadata in such a way that if
6383    they are set, a new unrecognized type is generated.  This will
6384    cause pre-5.1-26 servers to stop due to a field type mismatch,
6385    while new servers will be able to extract the extra bits. If the
6386    length is <256, there will be no difference and both a new and an
6387    old server will be able to handle it.
6388 
6389    @note The extra two bits are added to bits 13 and 14 of the
6390    parameter data (with 1 being the least siginficant bit and 16 the
6391    most significant bit of the word) by xoring the extra length bits
6392    with the real type.  Since all allowable types have 0xF as most
6393    significant bits of the metadata word, lengths <256 will not affect
6394    the real type at all, while all other values will result in a
6395    non-existant type in the range 17-244.
6396 
6397    @see Field_string::unpack
6398 
6399    @param   metadata_ptr   First byte of field metadata
6400 
6401    @returns number of bytes written to metadata_ptr
6402 */
do_save_field_metadata(uchar * metadata_ptr) const6403 int Field_string::do_save_field_metadata(uchar *metadata_ptr) const {
6404   DBUG_ASSERT(field_length < 1024);
6405   DBUG_ASSERT((real_type() & 0xF0) == 0xF0);
6406   DBUG_PRINT("debug",
6407              ("field_length: %u, real_type: %u", field_length, real_type()));
6408   *metadata_ptr = (real_type() ^ ((field_length & 0x300) >> 4));
6409   *(metadata_ptr + 1) = field_length & 0xFF;
6410   return 2;
6411 }
6412 
max_packed_col_length() const6413 uint Field_string::max_packed_col_length() const {
6414   const size_t max_length = pack_length();
6415   return (max_length > 255 ? 2 : 1) + max_length;
6416 }
6417 
get_key_image(uchar * buff,size_t length,imagetype) const6418 size_t Field_string::get_key_image(uchar *buff, size_t length,
6419                                    imagetype) const {
6420   size_t bytes =
6421       my_charpos(field_charset, (char *)ptr, (char *)ptr + field_length,
6422                  length / field_charset->mbmaxlen);
6423   memcpy(buff, ptr, bytes);
6424   if (bytes < length)
6425     field_charset->cset->fill(field_charset, (char *)buff + bytes,
6426                               length - bytes, field_charset->pad_char);
6427   return bytes;
6428 }
6429 
6430 /****************************************************************************
6431   VARCHAR type
6432   Data in field->ptr is stored as:
6433     1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
6434     data
6435 
6436   NOTE:
6437   When VARCHAR is stored in a key (for handler::index_read() etc) it's always
6438   stored with a 2 byte prefix. (Just like blob keys).
6439 
6440   Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
6441   The exception is if there is a prefix key field that is part of a long
6442   VARCHAR, in which case field_length for this may be 1 but the length_bytes
6443   is 2.
6444 ****************************************************************************/
6445 
6446 /**
6447    Save the field metadata for varstring fields.
6448 
6449    Saves the field length in the first byte. Note: may consume
6450    2 bytes. Caller must ensure second byte is contiguous with
6451    first byte (e.g. array index 0,1).
6452 
6453    @param   metadata_ptr   First byte of field metadata
6454 
6455    @returns number of bytes written to metadata_ptr
6456 */
do_save_field_metadata(uchar * metadata_ptr) const6457 int Field_varstring::do_save_field_metadata(uchar *metadata_ptr) const {
6458   DBUG_ASSERT(field_length <= 65535);
6459   int2store((char *)metadata_ptr, field_length);
6460   return 2;
6461 }
6462 
store(const char * from,size_t length,const CHARSET_INFO * cs)6463 type_conversion_status Field_varstring::store(const char *from, size_t length,
6464                                               const CHARSET_INFO *cs) {
6465   ASSERT_COLUMN_MARKED_FOR_WRITE;
6466   size_t copy_length;
6467   const char *well_formed_error_pos;
6468   const char *cannot_convert_error_pos;
6469   const char *from_end_pos;
6470 
6471   copy_length = field_well_formed_copy_nchars(
6472       field_charset, (char *)ptr + length_bytes, field_length, cs, from, length,
6473       field_length / field_charset->mbmaxlen, &well_formed_error_pos,
6474       &cannot_convert_error_pos, &from_end_pos);
6475 
6476   if (length_bytes == 1)
6477     *ptr = (uchar)copy_length;
6478   else
6479     int2store(ptr, static_cast<uint16>(copy_length));
6480 
6481   return check_string_copy_error(well_formed_error_pos,
6482                                  cannot_convert_error_pos, from_end_pos,
6483                                  from + length, true, cs);
6484 }
6485 
store(longlong nr,bool unsigned_val)6486 type_conversion_status Field_varstring::store(longlong nr, bool unsigned_val) {
6487   char buff[64];
6488   uint length;
6489   length = (uint)(field_charset->cset->longlong10_to_str)(
6490       field_charset, buff, sizeof(buff), (unsigned_val ? 10 : -10), nr);
6491   return Field_varstring::store(buff, length, field_charset);
6492 }
6493 
val_real() const6494 double Field_varstring::val_real() const {
6495   ASSERT_COLUMN_MARKED_FOR_READ;
6496   int error;
6497   const char *end;
6498   double result;
6499   const CHARSET_INFO *cs = charset();
6500 
6501   uint length = data_length();
6502   result = my_strntod(cs, (char *)ptr + length_bytes, length, &end, &error);
6503 
6504   if ((error || (length != (uint)(end - (char *)ptr + length_bytes) &&
6505                  !check_if_only_end_space(
6506                      cs, end, (char *)ptr + length_bytes + length)))) {
6507     push_numerical_conversion_warning(current_thd, (char *)ptr + length_bytes,
6508                                       length, cs, "DOUBLE",
6509                                       ER_TRUNCATED_WRONG_VALUE);
6510   }
6511   return result;
6512 }
6513 
val_int() const6514 longlong Field_varstring::val_int() const {
6515   ASSERT_COLUMN_MARKED_FOR_READ;
6516   int error;
6517   const char *end;
6518   const CHARSET_INFO *cs = charset();
6519 
6520   uint length = data_length();
6521   longlong result =
6522       my_strntoll(cs, (char *)ptr + length_bytes, length, 10, &end, &error);
6523 
6524   if ((error || (length != (uint)(end - (char *)ptr + length_bytes) &&
6525                  !check_if_only_end_space(
6526                      cs, end, (char *)ptr + length_bytes + length)))) {
6527     push_numerical_conversion_warning(current_thd, (char *)ptr + length_bytes,
6528                                       length, cs, "INTEGER",
6529                                       ER_TRUNCATED_WRONG_VALUE);
6530   }
6531   return result;
6532 }
6533 
val_str(String *,String * val_ptr) const6534 String *Field_varstring::val_str(String *, String *val_ptr) const {
6535   ASSERT_COLUMN_MARKED_FOR_READ;
6536   val_ptr->set(pointer_cast<const char *>(data_ptr()), data_length(),
6537                field_charset);
6538   return val_ptr;
6539 }
6540 
val_decimal(my_decimal * decimal_value) const6541 my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value) const {
6542   ASSERT_COLUMN_MARKED_FOR_READ;
6543   const CHARSET_INFO *cs = charset();
6544   uint length = data_length();
6545   int error = str2my_decimal(E_DEC_FATAL_ERROR, (char *)ptr + length_bytes,
6546                              length, cs, decimal_value);
6547 
6548   if (error) {
6549     push_numerical_conversion_warning(current_thd, (char *)ptr + length_bytes,
6550                                       length, cs, "DECIMAL",
6551                                       ER_TRUNCATED_WRONG_VALUE);
6552   }
6553   return decimal_value;
6554 }
6555 
cmp_max(const uchar * a_ptr,const uchar * b_ptr,uint max_len) const6556 int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
6557                              uint max_len) const {
6558   uint a_length, b_length;
6559   int diff;
6560 
6561   if (length_bytes == 1) {
6562     a_length = (uint)*a_ptr;
6563     b_length = (uint)*b_ptr;
6564   } else {
6565     a_length = uint2korr(a_ptr);
6566     b_length = uint2korr(b_ptr);
6567   }
6568   a_length = std::min(a_length, max_len);
6569   b_length = std::min(b_length, max_len);
6570   diff = field_charset->coll->strnncollsp(field_charset, a_ptr + length_bytes,
6571                                           a_length, b_ptr + length_bytes,
6572                                           b_length);
6573   return diff;
6574 }
6575 
6576 /**
6577   @note
6578     varstring and blob keys are ALWAYS stored with a 2 byte length prefix
6579 */
6580 
key_cmp(const uchar * key_ptr,uint max_key_length) const6581 int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length) const {
6582   uint length = data_length();
6583   uint local_char_length = max_key_length / field_charset->mbmaxlen;
6584 
6585   local_char_length =
6586       my_charpos(field_charset, ptr + length_bytes, ptr + length_bytes + length,
6587                  local_char_length);
6588   length = std::min(length, local_char_length);
6589   return field_charset->coll->strnncollsp(field_charset, ptr + length_bytes,
6590                                           length, key_ptr + HA_KEY_BLOB_LENGTH,
6591                                           uint2korr(key_ptr));
6592 }
6593 
6594 /**
6595   Compare to key segments (always 2 byte length prefix).
6596 
6597   @note
6598     This is used only to compare key segments created for index_read().
6599     (keys are created and compared in key.cc)
6600 */
6601 
key_cmp(const uchar * a,const uchar * b) const6602 int Field_varstring::key_cmp(const uchar *a, const uchar *b) const {
6603   return field_charset->coll->strnncollsp(field_charset, a + HA_KEY_BLOB_LENGTH,
6604                                           uint2korr(a), b + HA_KEY_BLOB_LENGTH,
6605                                           uint2korr(b));
6606 }
6607 
make_sort_key(uchar * to,size_t length) const6608 size_t Field_varstring::make_sort_key(uchar *to, size_t length) const {
6609   const int flags =
6610       (field_charset->pad_attribute == NO_PAD) ? 0 : MY_STRXFRM_PAD_TO_MAXLEN;
6611 
6612   DBUG_ASSERT(char_length_cache == char_length());
6613   return field_charset->coll->strnxfrm(field_charset, to, length,
6614                                        char_length_cache, ptr + length_bytes,
6615                                        data_length(), flags);
6616 }
6617 
key_type() const6618 enum ha_base_keytype Field_varstring::key_type() const {
6619   enum ha_base_keytype res;
6620 
6621   if (binary())
6622     res = length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
6623   else
6624     res = length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
6625   return res;
6626 }
6627 
sql_type(String & res) const6628 void Field_varstring::sql_type(String &res) const {
6629   const CHARSET_INFO *cs = res.charset();
6630   size_t length;
6631 
6632   length = cs->cset->snprintf(cs, res.ptr(), res.alloced_length(), "%s(%d)",
6633                               (has_charset() ? "varchar" : "varbinary"),
6634                               (int)field_length / charset()->mbmaxlen);
6635   res.length(length);
6636 }
6637 
data_length(ptrdiff_t row_offset) const6638 uint32 Field_varstring::data_length(ptrdiff_t row_offset) const {
6639   return length_bytes == 1 ? (uint32) * (ptr + row_offset)
6640                            : uint2korr(ptr + row_offset);
6641 }
6642 
6643 /*
6644   Functions to create a packed row.
6645   Here the number of length bytes are depending on the given max_length
6646 */
6647 
pack(uchar * to,const uchar * from,size_t max_length) const6648 uchar *Field_varstring::pack(uchar *to, const uchar *from,
6649                              size_t max_length) const {
6650   uint length = length_bytes == 1 ? (uint)*from : uint2korr(from);
6651   if (max_length < length_bytes)
6652     length = 0;
6653   else if (length > max_length - length_bytes)
6654     length = max_length - length_bytes;
6655 
6656   /* Length always stored little-endian */
6657   if (max_length >= 1) {
6658     *to++ = length & 0xFF;
6659     if (length_bytes == 2 && max_length >= 2) *to++ = (length >> 8) & 0xFF;
6660   }
6661 
6662   /* Store bytes of string */
6663   memcpy(to, from + length_bytes, length);
6664   return to + length;
6665 }
6666 
6667 /**
6668    Unpack a varstring field from row data.
6669 
6670    This method is used to unpack a varstring field from a master
6671    whose size of the field is less than that of the slave.
6672 
6673    @note
6674    The string length is always packed little-endian.
6675 
6676    @param   to         Destination of the data
6677    @param   from       Source of the data
6678    @param   param_data Length bytes from the master's field data
6679 
6680    @return  New pointer into memory based on from + length of the data
6681 */
unpack(uchar * to,const uchar * from,uint param_data)6682 const uchar *Field_varstring::unpack(uchar *to, const uchar *from,
6683                                      uint param_data) {
6684   uint length;
6685   uint l_bytes = (param_data && (param_data < field_length))
6686                      ? (param_data <= 255) ? 1 : 2
6687                      : length_bytes;
6688   if (l_bytes == 1) {
6689     to[0] = *from++;
6690     length = to[0];
6691     if (length_bytes == 2) to[1] = 0;
6692   } else /* l_bytes == 2 */
6693   {
6694     length = uint2korr(from);
6695     to[0] = *from++;
6696     to[1] = *from++;
6697   }
6698   if (length) memcpy(to + length_bytes, from, length);
6699   return from + length;
6700 }
6701 
get_key_image(uchar * buff,size_t length,imagetype) const6702 size_t Field_varstring::get_key_image(uchar *buff, size_t length,
6703                                       imagetype) const {
6704   /*
6705     If NULL, data bytes may have been left random by the storage engine, so
6706     don't try to read them.
6707   */
6708   uint f_length = is_null() ? 0 : data_length();
6709   uint local_char_length = length / field_charset->mbmaxlen;
6710   uchar *pos = ptr + length_bytes;
6711   local_char_length =
6712       my_charpos(field_charset, pos, pos + f_length, local_char_length);
6713   f_length = std::min(f_length, local_char_length);
6714   /* Key is always stored with 2 bytes */
6715   int2store(buff, f_length);
6716   memcpy(buff + HA_KEY_BLOB_LENGTH, pos, f_length);
6717   if (f_length < length) {
6718     /*
6719       Must clear this as we do a memcmp in opt_range.cc to detect
6720       identical keys
6721     */
6722     memset(buff + HA_KEY_BLOB_LENGTH + f_length, 0, (length - f_length));
6723   }
6724   return HA_KEY_BLOB_LENGTH + f_length;
6725 }
6726 
set_key_image(const uchar * buff,size_t length)6727 void Field_varstring::set_key_image(const uchar *buff, size_t length) {
6728   length = uint2korr(buff);  // Real length is here
6729   (void)Field_varstring::store((const char *)buff + HA_KEY_BLOB_LENGTH, length,
6730                                field_charset);
6731 }
6732 
cmp_binary(const uchar * a_ptr,const uchar * b_ptr,uint32 max_length) const6733 int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
6734                                 uint32 max_length) const {
6735   uint32 a_length, b_length;
6736 
6737   if (length_bytes == 1) {
6738     a_length = (uint)*a_ptr;
6739     b_length = (uint)*b_ptr;
6740   } else {
6741     a_length = uint2korr(a_ptr);
6742     b_length = uint2korr(b_ptr);
6743   }
6744   a_length = std::min(a_length, max_length);
6745   b_length = std::min(b_length, max_length);
6746   if (a_length != b_length) return 1;
6747   return memcmp(a_ptr + length_bytes, b_ptr + length_bytes, a_length);
6748 }
6749 
new_field(MEM_ROOT * root,TABLE * new_table) const6750 Field *Field_varstring::new_field(MEM_ROOT *root, TABLE *new_table) const {
6751   Field_varstring *res =
6752       down_cast<Field_varstring *>(Field::new_field(root, new_table));
6753   if (res) res->length_bytes = length_bytes;
6754   return res;
6755 }
6756 
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uchar * new_null_ptr,uint new_null_bit) const6757 Field *Field_varstring::new_key_field(MEM_ROOT *root, TABLE *new_table,
6758                                       uchar *new_ptr, uchar *new_null_ptr,
6759                                       uint new_null_bit) const {
6760   Field_varstring *res;
6761   if ((res = (Field_varstring *)Field::new_key_field(
6762            root, new_table, new_ptr, new_null_ptr, new_null_bit))) {
6763     /* Keys length prefixes are always packed with 2 bytes */
6764     res->length_bytes = 2;
6765   }
6766   return res;
6767 }
6768 
is_equal(const Create_field * new_field) const6769 uint Field_varstring::is_equal(const Create_field *new_field) const {
6770   DBUG_TRACE;
6771   if (change_prevents_inplace(*this, *new_field)) {
6772     return IS_EQUAL_NO;
6773   }
6774 
6775   if (new_field->charset == field_charset &&
6776       new_field->pack_length() == pack_length()) {
6777     return IS_EQUAL_YES;
6778   }
6779 
6780   return IS_EQUAL_PACK_LENGTH;
6781 }
6782 
hash(ulong * nr,ulong * nr2) const6783 void Field_varstring::hash(ulong *nr, ulong *nr2) const {
6784   if (is_null()) {
6785     *nr ^= (*nr << 1) | 1;
6786   } else {
6787     uint len = data_length();
6788     const CHARSET_INFO *cs = charset();
6789     uint64 tmp1 = *nr;
6790     uint64 tmp2 = *nr2;
6791     cs->coll->hash_sort(cs, ptr + length_bytes, len, &tmp1, &tmp2);
6792 
6793     // NOTE: This truncates to 32-bit on Windows, to keep on-disk stability.
6794     *nr = static_cast<ulong>(tmp1);
6795     *nr2 = static_cast<ulong>(tmp2);
6796   }
6797 }
6798 
6799 /****************************************************************************
6800 ** blob type
6801 ** A blob is saved as a length and a pointer. The length is stored in the
6802 ** packlength slot and may be from 1-4.
6803 ****************************************************************************/
6804 
Field_blob(uint32 packlength_arg)6805 Field_blob::Field_blob(uint32 packlength_arg)
6806     : Field_longstr(nullptr, 0, &dummy_null_buffer, 0, NONE, "temp",
6807                     system_charset_info),
6808       packlength(packlength_arg),
6809       m_keep_old_value(false) {}
6810 
Field_blob(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,TABLE_SHARE * share,uint blob_pack_length,const CHARSET_INFO * cs)6811 Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
6812                        uchar auto_flags_arg, const char *field_name_arg,
6813                        TABLE_SHARE *share, uint blob_pack_length,
6814                        const CHARSET_INFO *cs)
6815     : Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
6816                     null_ptr_arg, null_bit_arg, auto_flags_arg, field_name_arg,
6817                     cs),
6818       packlength(blob_pack_length),
6819       m_keep_old_value(false) {
6820   DBUG_ASSERT(blob_pack_length <=
6821               4);  // Only pack lengths 1-4 supported currently
6822   set_flag(BLOB_FLAG);
6823   share->blob_fields++;
6824   /* TODO: why do not fill table->s->blob_field array here? */
6825 }
6826 
store_blob_length(uchar * i_ptr,uint i_packlength,uint32 i_number)6827 static void store_blob_length(uchar *i_ptr, uint i_packlength,
6828                               uint32 i_number) {
6829   switch (i_packlength) {
6830     case 1:
6831       i_ptr[0] = (uchar)i_number;
6832       break;
6833     case 2:
6834       int2store(i_ptr, (unsigned short)i_number);
6835       break;
6836     case 3:
6837       int3store(i_ptr, i_number);
6838       break;
6839     case 4:
6840       int4store(i_ptr, i_number);
6841   }
6842 }
6843 
get_length(const uchar * pos,uint packlength_arg) const6844 uint32 Field_blob::get_length(const uchar *pos, uint packlength_arg) const {
6845   switch (packlength_arg) {
6846     case 1:
6847       return (uint32)pos[0];
6848     case 2:
6849       return uint2korr(pos);
6850     case 3:
6851       return uint3korr(pos);
6852     case 4:
6853       return uint4korr(pos);
6854   }
6855   /* When expanding this, see also MAX_FIELD_BLOBLENGTH. */
6856   return 0;  // Impossible
6857 }
6858 
6859 /**
6860   Store a blob value to memory storage.
6861   @param      from         the string value to store.
6862   @param      length       length of the string value.
6863   @param      cs           character set of the string value.
6864   @param      max_length   Cut at this length safely (multibyte aware).
6865 */
store_to_mem(const char * from,size_t length,const CHARSET_INFO * cs,size_t max_length,Blob_mem_storage *)6866 type_conversion_status Field_blob::store_to_mem(const char *from, size_t length,
6867                                                 const CHARSET_INFO *cs,
6868                                                 size_t max_length,
6869                                                 Blob_mem_storage *) {
6870   /*
6871     We don't need to support escaping or character set conversions here,
6872     because store_to_mem() is currently called only when we process
6873     queries having GROUP_CONCAT with ORDER BY or DISTINCT,
6874     hence some assersions:
6875   */
6876   DBUG_ASSERT(field_charset == cs);
6877   DBUG_ASSERT(length <= max_data_length());
6878 
6879   if (length > max_length) {
6880     int well_formed_error;
6881     length = cs->cset->well_formed_len(cs, from, from + max_length, length,
6882                                        &well_formed_error);
6883     table->blob_storage->set_truncated_value(true);
6884   }
6885   char *tmp;
6886   if (!(tmp = table->blob_storage->store(from, length))) {
6887     reset();
6888     return TYPE_ERR_OOM;
6889   }
6890   store_ptr_and_length(tmp, length);
6891   return TYPE_OK;
6892 }
6893 
store_internal(const char * from,size_t length,const CHARSET_INFO * cs)6894 type_conversion_status Field_blob::store_internal(const char *from,
6895                                                   size_t length,
6896                                                   const CHARSET_INFO *cs) {
6897   size_t new_length;
6898   char buff[STRING_BUFFER_USUAL_SIZE], *tmp;
6899   String tmpstr(buff, sizeof(buff), &my_charset_bin);
6900 
6901   /*
6902     If the 'from' address is in the range of the temporary 'value'-
6903     object we need to copy the content to a different location or it will be
6904     invalidated when the 'value'-object is reallocated to make room for
6905     the new character set.
6906   */
6907   if (from >= value.ptr() && from <= value.ptr() + value.length()) {
6908     /*
6909       If content of the 'from'-address is cached in the 'value'-object
6910       it is possible that the content needs a character conversion.
6911     */
6912     if (!String::needs_conversion_on_storage(length, cs, field_charset)) {
6913       store_ptr_and_length(from, length);
6914       return TYPE_OK;
6915     }
6916     if (tmpstr.copy(from, length, cs)) goto oom_error;
6917     from = tmpstr.ptr();
6918   }
6919 
6920   new_length = min<size_t>(max_data_length(), field_charset->mbmaxlen * length);
6921 
6922   if (value.alloc(new_length)) goto oom_error;
6923 
6924   tmp = value.ptr();
6925 
6926   {
6927     const char *well_formed_error_pos;
6928     const char *cannot_convert_error_pos;
6929     const char *from_end_pos;
6930     /*
6931       "length" is OK as "nchars" argument to well_formed_copy_nchars as this
6932       is never used to limit the length of the data. The cut of long data
6933       is done with the new_length value.
6934     */
6935     size_t copy_length = field_well_formed_copy_nchars(
6936         field_charset, tmp, new_length, cs, from, length, length,
6937         &well_formed_error_pos, &cannot_convert_error_pos, &from_end_pos);
6938 
6939     store_ptr_and_length(tmp, copy_length);
6940     return check_string_copy_error(well_formed_error_pos,
6941                                    cannot_convert_error_pos, from_end_pos,
6942                                    from + length, true, cs);
6943   }
6944 
6945 oom_error:
6946   /* Fatal OOM error */
6947   reset();
6948   return TYPE_ERR_OOM;
6949 }
6950 
store(const char * from,size_t length,const CHARSET_INFO * cs)6951 type_conversion_status Field_blob::store(const char *from, size_t length,
6952                                          const CHARSET_INFO *cs) {
6953   ASSERT_COLUMN_MARKED_FOR_WRITE;
6954 
6955   if (table->blob_storage)  // GROUP_CONCAT with ORDER BY | DISTINCT
6956     return store_to_mem(from, length, cs,
6957                         table->in_use->variables.group_concat_max_len,
6958                         table->blob_storage);
6959 
6960   return store_internal(from, length, cs);
6961 }
6962 
store(double nr)6963 type_conversion_status Field_blob::store(double nr) {
6964   const CHARSET_INFO *cs = charset();
6965   value.set_real(nr, DECIMAL_NOT_SPECIFIED, cs);
6966   return Field_blob::store(value.ptr(), value.length(), cs);
6967 }
6968 
store(longlong nr,bool unsigned_val)6969 type_conversion_status Field_blob::store(longlong nr, bool unsigned_val) {
6970   const CHARSET_INFO *cs = charset();
6971   value.set_int(nr, unsigned_val, cs);
6972   return Field_blob::store(value.ptr(), value.length(), cs);
6973 }
6974 
store(const Field * from)6975 type_conversion_status Field_blob::store(const Field *from) {
6976   from->val_str(&value);
6977 
6978   /*
6979     Copy value if copy_blobs is set, or source is part of the table's
6980     writeset.
6981   */
6982   if (table->copy_blobs || (!value.is_alloced() && from->is_updatable()))
6983     value.copy();
6984 
6985   return store(value.ptr(), value.length(), from->charset());
6986 }
6987 
val_real() const6988 double Field_blob::val_real() const {
6989   ASSERT_COLUMN_MARKED_FOR_READ;
6990 
6991   const char *blob = pointer_cast<const char *>(get_blob_data());
6992   if (blob == nullptr) return 0.0;
6993 
6994   int not_used;
6995   const char *end_not_used;
6996   return my_strntod(charset(), blob, get_length(ptr), &end_not_used, &not_used);
6997 }
6998 
val_int() const6999 longlong Field_blob::val_int() const {
7000   ASSERT_COLUMN_MARKED_FOR_READ;
7001 
7002   const char *blob = pointer_cast<const char *>(get_blob_data());
7003   if (blob == nullptr) return 0;
7004 
7005   int not_used;
7006   return my_strntoll(charset(), blob, get_length(ptr), 10, nullptr, &not_used);
7007 }
7008 
val_str(String *,String * val_ptr) const7009 String *Field_blob::val_str(String *, String *val_ptr) const {
7010   ASSERT_COLUMN_MARKED_FOR_READ;
7011 
7012   const char *blob = pointer_cast<const char *>(get_blob_data());
7013   if (blob == nullptr)
7014     val_ptr->set("", 0, charset());  // A bit safer than ->length(0)
7015   else
7016     val_ptr->set(blob, get_length(ptr), charset());
7017   return val_ptr;
7018 }
7019 
val_decimal(my_decimal * decimal_value) const7020 my_decimal *Field_blob::val_decimal(my_decimal *decimal_value) const {
7021   ASSERT_COLUMN_MARKED_FOR_READ;
7022   size_t length;
7023   const char *blob = pointer_cast<const char *>(get_blob_data());
7024   if (!blob) {
7025     blob = "";
7026     length = 0;
7027   } else
7028     length = get_length(ptr);
7029 
7030   str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(), decimal_value);
7031   return decimal_value;
7032 }
7033 
cmp(const uchar * a,uint32 a_length,const uchar * b,uint32 b_length) const7034 int Field_blob::cmp(const uchar *a, uint32 a_length, const uchar *b,
7035                     uint32 b_length) const {
7036   return field_charset->coll->strnncollsp(field_charset, a, a_length, b,
7037                                           b_length);
7038 }
7039 
cmp_max(const uchar * a_ptr,const uchar * b_ptr,uint max_length) const7040 int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
7041                         uint max_length) const {
7042   const uchar *blob1 = get_blob_data(a_ptr + packlength);
7043   const uchar *blob2 = get_blob_data(b_ptr + packlength);
7044   uint32 a_len = min(get_length(a_ptr), max_length);
7045   uint32 b_len = min(get_length(b_ptr), max_length);
7046   return Field_blob::cmp(blob1, a_len, blob2, b_len);
7047 }
7048 
cmp_binary(const uchar * a_ptr,const uchar * b_ptr,uint32 max_length) const7049 int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7050                            uint32 max_length) const {
7051   const uchar *a = get_blob_data(a_ptr + packlength);
7052   const uchar *b = get_blob_data(b_ptr + packlength);
7053   uint32 a_length = min(get_length(a_ptr), max_length);
7054   uint32 b_length = min(get_length(b_ptr), max_length);
7055   const uint32 min_a_b = min(a_length, b_length);
7056   uint diff = min_a_b == 0 ? 0 : memcmp(a, b, min_a_b);  // memcmp(a, b, 0) == 0
7057   return diff ? diff : (int)(a_length - b_length);
7058 }
7059 
7060 /* The following is used only when comparing a key */
7061 
get_key_image(uchar * buff,size_t length,imagetype type_arg) const7062 size_t Field_blob::get_key_image(uchar *buff, size_t length,
7063                                  imagetype type_arg) const {
7064   uint32 blob_length = get_length();
7065   const uchar *const blob = get_blob_data();
7066 
7067   if (type_arg == itMBR) {
7068     const uint image_length = SIZEOF_STORED_DOUBLE * 4;
7069 
7070     if (blob_length < SRID_SIZE) {
7071       memset(buff, 0, image_length);
7072       return image_length;
7073     }
7074     gis::srid_t srid = uint4korr(blob);
7075     const dd::Spatial_reference_system *srs = nullptr;
7076     dd::cache::Dictionary_client::Auto_releaser m_releaser(
7077         current_thd->dd_client());
7078     Srs_fetcher fetcher(current_thd);
7079     if (srid != 0) fetcher.acquire(srid, &srs);
7080     if (get_mbr_from_store(srs, blob, blob_length, 2,
7081                            pointer_cast<double *>(buff), &srid)) {
7082       memset(buff, 0, image_length);
7083     } else {
7084       // get_mbr_from_store returns the MBR in machine byte order, but buff
7085       // should always be in little-endian order.
7086       float8store(buff, *pointer_cast<double *>(buff));
7087       float8store(buff + 8, *(pointer_cast<double *>(buff) + 1));
7088       float8store(buff + 16, *(pointer_cast<double *>(buff) + 2));
7089       float8store(buff + 24, *(pointer_cast<double *>(buff) + 3));
7090     }
7091 
7092     return image_length;
7093   }
7094 
7095   uint local_char_length = length / field_charset->mbmaxlen;
7096   local_char_length = blob == nullptr
7097                           ? 0
7098                           : my_charpos(field_charset, blob, blob + blob_length,
7099                                        local_char_length);
7100   blob_length = std::min(blob_length, local_char_length);
7101 
7102   if ((uint32)length > blob_length) {
7103     /*
7104       Must clear this as we do a memcmp in opt_range.cc to detect
7105       identical keys
7106     */
7107     memset(buff + HA_KEY_BLOB_LENGTH + blob_length, 0, (length - blob_length));
7108     length = (uint)blob_length;
7109   }
7110   int2store(buff, static_cast<uint16>(length));
7111   if (length > 0) memcpy(buff + HA_KEY_BLOB_LENGTH, blob, length);
7112   return HA_KEY_BLOB_LENGTH + length;
7113 }
7114 
set_key_image(const uchar * buff,size_t length)7115 void Field_blob::set_key_image(const uchar *buff, size_t length) {
7116   length = uint2korr(buff);
7117   (void)Field_blob::store((const char *)buff + HA_KEY_BLOB_LENGTH, length,
7118                           field_charset);
7119 }
7120 
key_cmp(const uchar * key_ptr,uint max_key_length) const7121 int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length) const {
7122   uint32 blob_length = get_length(ptr);
7123   const uchar *blob1 = get_blob_data();
7124   const CHARSET_INFO *cs = charset();
7125   uint local_char_length = max_key_length / cs->mbmaxlen;
7126   local_char_length =
7127       my_charpos(cs, blob1, blob1 + blob_length, local_char_length);
7128   blob_length = min(blob_length, local_char_length);
7129   return Field_blob::cmp(blob1, blob_length, key_ptr + HA_KEY_BLOB_LENGTH,
7130                          uint2korr(key_ptr));
7131 }
7132 
key_cmp(const uchar * a,const uchar * b) const7133 int Field_blob::key_cmp(const uchar *a, const uchar *b) const {
7134   return Field_blob::cmp(a + HA_KEY_BLOB_LENGTH, uint2korr(a),
7135                          b + HA_KEY_BLOB_LENGTH, uint2korr(b));
7136 }
7137 
7138 /**
7139    Save the field metadata for blob fields.
7140 
7141    Saves the pack length in the first byte of the field metadata array
7142    at index of *metadata_ptr.
7143 
7144    @param   metadata_ptr   First byte of field metadata
7145 
7146    @returns number of bytes written to metadata_ptr
7147 */
do_save_field_metadata(uchar * metadata_ptr) const7148 int Field_blob::do_save_field_metadata(uchar *metadata_ptr) const {
7149   DBUG_TRACE;
7150   *metadata_ptr = pack_length_no_ptr();
7151   DBUG_PRINT("debug", ("metadata: %u (pack_length_no_ptr)", *metadata_ptr));
7152   return 1;
7153 }
7154 
make_sort_key(uchar * to,size_t length) const7155 size_t Field_blob::make_sort_key(uchar *to, size_t length) const {
7156   static const uchar EMPTY_BLOB[1] = {0};
7157   uint32 blob_length = get_length();
7158 
7159   const int flags =
7160       (field_charset->pad_attribute == NO_PAD) ? 0 : MY_STRXFRM_PAD_TO_MAXLEN;
7161 
7162   const uchar *blob = blob_length > 0 ? get_blob_data() : EMPTY_BLOB;
7163 
7164   return field_charset->coll->strnxfrm(field_charset, to, length, length, blob,
7165                                        blob_length, flags);
7166 }
7167 
sql_type(String & res) const7168 void Field_blob::sql_type(String &res) const {
7169   const char *str;
7170   uint length;
7171   switch (packlength) {
7172     default:
7173       str = "tiny";
7174       length = 4;
7175       break;
7176     case 2:
7177       str = "";
7178       length = 0;
7179       break;
7180     case 3:
7181       str = "medium";
7182       length = 6;
7183       break;
7184     case 4:
7185       str = "long";
7186       length = 4;
7187       break;
7188   }
7189   res.set_ascii(str, length);
7190   if (charset() == &my_charset_bin)
7191     res.append(STRING_WITH_LEN("blob"));
7192   else {
7193     res.append(STRING_WITH_LEN("text"));
7194   }
7195 }
7196 
copy()7197 bool Field_blob::copy() {
7198   const uint32 length = get_length();
7199   if (value.copy(pointer_cast<const char *>(get_blob_data()), length,
7200                  charset())) {
7201     Field_blob::reset();
7202     my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), length);
7203     return true;
7204   }
7205   DBUG_ASSERT(value.length() == length);
7206   set_ptr(length, pointer_cast<const uchar *>(value.ptr()));
7207   return false;
7208 }
7209 
pack(uchar * to,const uchar * from,size_t max_length) const7210 uchar *Field_blob::pack(uchar *to, const uchar *from, size_t max_length) const {
7211   uint32 length = get_length(from);  // Length of from string
7212 
7213   /*
7214     Store max length, which will occupy packlength bytes.
7215   */
7216   uchar len_buf[4];
7217   DBUG_ASSERT(packlength <= sizeof(len_buf));
7218   store_blob_length(len_buf, packlength, length);
7219 
7220   if (packlength >= max_length) {
7221     memcpy(to, len_buf, max_length);
7222     return to + max_length;
7223   }
7224 
7225   memcpy(to, len_buf, packlength);
7226 
7227   /*
7228     Store the actual blob data, which will occupy 'length' bytes.
7229    */
7230   size_t store_length = min<size_t>(length, max_length - packlength);
7231   if (store_length > 0) {
7232     memcpy(to + packlength, get_blob_data(from + packlength), store_length);
7233   }
7234 
7235   return to + packlength + store_length;
7236 }
7237 
pack_with_metadata_bytes(uchar * to,const uchar * from,uint max_length) const7238 uchar *Field_blob::pack_with_metadata_bytes(uchar *to, const uchar *from,
7239                                             uint max_length) const {
7240   uint32 length = get_length(from);  // Length of from string
7241 
7242   /*
7243     Store max length, which will occupy packlength bytes. If the max
7244     length given is smaller than the actual length of the blob, we
7245     just store the initial bytes of the blob.
7246    */
7247   store_blob_length(to, packlength, min(length, max_length));
7248 
7249   /*
7250     Store the actual blob data, which will occupy 'length' bytes.
7251    */
7252   if (length > 0) {
7253     memcpy(to + packlength, get_blob_data(from + packlength), length);
7254   }
7255   return to + packlength + length;
7256 }
7257 
7258 /**
7259    Unpack a blob field from row data.
7260 
7261    This method is used to unpack a blob field from a master whose size of
7262    the field is less than that of the slave. Note: This method is included
7263    to satisfy inheritance rules, but is not needed for blob fields. It
7264    simply is used as a pass-through to the original unpack() method for
7265    blob fields.
7266 
7267    @param   from       Source of the data
7268    @param   param_data The "metadata", as stored in the Table_map_log_event
7269                        for this field. This metadata is the number of bytes
7270                        used to represent the length of the blob (1, 2, 3, or
7271                        4).
7272 
7273    @return  New pointer into memory based on from + length of the data
7274 */
unpack(uchar *,const uchar * from,uint param_data)7275 const uchar *Field_blob::unpack(uchar *, const uchar *from, uint param_data) {
7276   DBUG_TRACE;
7277   DBUG_PRINT("enter", ("from: %p;"
7278                        " param_data: %u; ",
7279                        from, param_data));
7280   uint const master_packlength =
7281       param_data > 0 ? param_data & 0xFF : packlength;
7282   uint32 const length = get_length(from, master_packlength);
7283   DBUG_DUMP("packed", from, length + master_packlength);
7284   bitmap_set_bit(table->write_set, field_index());
7285   Field_blob::store(pointer_cast<const char *>(from) + master_packlength,
7286                     length, field_charset);
7287   DBUG_DUMP("field", ptr, pack_length() /* len bytes + ptr bytes */);
7288   DBUG_DUMP("value", get_blob_data(), length /* the blob value length */);
7289   return from + master_packlength + length;
7290 }
7291 
max_packed_col_length() const7292 uint Field_blob::max_packed_col_length() const {
7293   switch (packlength) {
7294     case 1:
7295     case 2:
7296     case 3:
7297       return packlength + (1u << (packlength * 8)) - 1;
7298     case 4:
7299       return UINT_MAX;
7300     default:
7301       DBUG_ASSERT(false);
7302       return UINT_MAX;
7303   }
7304 }
7305 
is_equal(const Create_field * new_field) const7306 uint Field_blob::is_equal(const Create_field *new_field) const {
7307   DBUG_TRACE;
7308 
7309   // Can't use change_prevents_inplace() here as it uses
7310   // sql_type_prevents_inplace() which checks real_type(), and
7311   // Field_blob::real_type() does NOT return the actual blob type as
7312   // one could expect, so we have to check against
7313   // get_blob_type_from_length(max_data_length() instead.
7314   // length_prevents_inplace() is less strict than pack_length
7315   // equality so would be redundant here.
7316   if (new_field->sql_type != get_blob_type_from_length(max_data_length()) ||
7317       new_field->pack_length() != pack_length() ||
7318       charset_prevents_inplace(*this, *new_field)) {
7319     return IS_EQUAL_NO;
7320   }
7321 
7322   if (new_field->charset == field_charset) {
7323     return IS_EQUAL_YES;
7324   }
7325 
7326   return IS_EQUAL_PACK_LENGTH;
7327 }
7328 
sql_type(String & res) const7329 void Field_geom::sql_type(String &res) const {
7330   const CHARSET_INFO *cs = &my_charset_latin1;
7331   switch (geom_type) {
7332     case GEOM_POINT:
7333       res.set(STRING_WITH_LEN("point"), cs);
7334       break;
7335     case GEOM_LINESTRING:
7336       res.set(STRING_WITH_LEN("linestring"), cs);
7337       break;
7338     case GEOM_POLYGON:
7339       res.set(STRING_WITH_LEN("polygon"), cs);
7340       break;
7341     case GEOM_MULTIPOINT:
7342       res.set(STRING_WITH_LEN("multipoint"), cs);
7343       break;
7344     case GEOM_MULTILINESTRING:
7345       res.set(STRING_WITH_LEN("multilinestring"), cs);
7346       break;
7347     case GEOM_MULTIPOLYGON:
7348       res.set(STRING_WITH_LEN("multipolygon"), cs);
7349       break;
7350     case GEOM_GEOMETRYCOLLECTION:
7351       res.set(STRING_WITH_LEN("geomcollection"), cs);
7352       break;
7353     default:
7354       res.set(STRING_WITH_LEN("geometry"), cs);
7355   }
7356 }
7357 
store(double)7358 type_conversion_status Field_geom::store(double) {
7359   my_error(ER_CANT_CREATE_GEOMETRY_OBJECT, MYF(0));
7360   return TYPE_ERR_BAD_VALUE;
7361 }
7362 
store(longlong,bool)7363 type_conversion_status Field_geom::store(longlong, bool) {
7364   my_error(ER_CANT_CREATE_GEOMETRY_OBJECT, MYF(0));
7365   return TYPE_ERR_BAD_VALUE;
7366 }
7367 
store_decimal(const my_decimal *)7368 type_conversion_status Field_geom::store_decimal(const my_decimal *) {
7369   my_error(ER_CANT_CREATE_GEOMETRY_OBJECT, MYF(0));
7370   return TYPE_ERR_BAD_VALUE;
7371 }
7372 
store(const char * from,size_t length,const CHARSET_INFO * cs)7373 type_conversion_status Field_geom::store(const char *from, size_t length,
7374                                          const CHARSET_INFO *cs) {
7375   if (length < SRID_SIZE + WKB_HEADER_SIZE + sizeof(uint32)) {
7376     Field_blob::reset();
7377     my_error(ER_CANT_CREATE_GEOMETRY_OBJECT, MYF(0));
7378     return TYPE_ERR_BAD_VALUE;
7379   }
7380 
7381   return Field_blob::store(from, length, cs);
7382 }
7383 
store_internal(const char * from,size_t length,const CHARSET_INFO * cs)7384 type_conversion_status Field_geom::store_internal(const char *from,
7385                                                   size_t length,
7386                                                   const CHARSET_INFO *cs) {
7387   // Check that the given WKB
7388   // 1. is at least 13 bytes long (length of GEOMETRYCOLLECTION EMPTY)
7389   // 2. isn't marked as bad geometry data
7390   // 3. isn't shorter than empty geometrycollection
7391   // 4. is a valid geometry type
7392   // 5. is well formed
7393   if (length < 13 ||                                                   // 1
7394       from == Geometry::bad_geometry_data.ptr() ||                     // 2
7395       length < SRID_SIZE + WKB_HEADER_SIZE + sizeof(uint32) ||         // 3
7396       !Geometry::is_valid_geotype(uint4korr(from + SRID_SIZE + 1)) ||  // 4
7397       !Geometry::is_well_formed(from, length,                          // 5
7398                                 geometry_type_to_wkb_type(geom_type),
7399                                 Geometry::wkb_ndr)) {
7400     Field_blob::reset();
7401     my_error(ER_CANT_CREATE_GEOMETRY_OBJECT, MYF(0));
7402     return TYPE_ERR_BAD_VALUE;
7403   }
7404 
7405   /*
7406     Check that the SRID of the geometry matches the expected SRID for this
7407     field
7408   */
7409   if (get_srid().has_value()) {
7410     gis::srid_t geometry_srid = uint4korr(from);
7411     if (geometry_srid != get_srid().value()) {
7412       Field_blob::reset();
7413       my_error(ER_WRONG_SRID_FOR_COLUMN, MYF(0), field_name, geometry_srid,
7414                get_srid().value());
7415       return TYPE_ERR_BAD_VALUE;
7416     }
7417   }
7418 
7419   if (table->copy_blobs || length <= MAX_FIELD_WIDTH) {  // Must make a copy
7420     value.copy(from, length, cs);
7421     from = value.ptr();
7422   }
7423 
7424   store_ptr_and_length(from, length);
7425   return TYPE_OK;
7426 }
7427 
is_equal(const Create_field * new_field) const7428 uint Field_geom::is_equal(const Create_field *new_field) const {
7429   return new_field->sql_type == real_type() &&
7430          new_field->geom_type == get_geometry_type() &&
7431          new_field->charset == field_charset &&
7432          new_field->pack_length() == pack_length();
7433 }
7434 
7435 /**
7436   Get the type of this field (json).
7437   @param str  the string that receives the type
7438 */
sql_type(String & str) const7439 void Field_json::sql_type(String &str) const {
7440   str.set_ascii(STRING_WITH_LEN("json"));
7441 }
7442 
7443 /// Create a shallow clone of this field in the specified MEM_ROOT.
clone(MEM_ROOT * mem_root) const7444 Field_json *Field_json::clone(MEM_ROOT *mem_root) const {
7445   DBUG_ASSERT(type() == MYSQL_TYPE_JSON);
7446   return new (mem_root) Field_json(*this);
7447 }
7448 
7449 /**
7450   Check if a new field is compatible with this one.
7451   @param new_field  the new field
7452   @return true if new_field is compatible with this field, false otherwise
7453 */
is_equal(const Create_field * new_field) const7454 uint Field_json::is_equal(const Create_field *new_field) const {
7455   // All JSON fields are compatible with each other.
7456   return (new_field->sql_type == real_type());
7457 }
7458 
7459 /**
7460   Store data in this JSON field.
7461 
7462   JSON data is usually stored using store(Field_json*) or store_json(), so this
7463   function will only be called if non-JSON data is attempted stored in a JSON
7464   field. This results in an error in most cases.
7465 
7466   It will attempt to parse the string (unless it's binary) as JSON text, and
7467   store a binary representation of JSON document if the string could be parsed.
7468 
7469   Note that we override store() and not store_internal() because
7470   Field_blob::store() contains logic that bypasses store_internal() in
7471   some cases we care about. In particular:
7472 
7473   - When supplied an empty string, we want to raise a JSON syntax
7474     error instead of silently inserting an empty byte string.
7475 
7476   - When called from GROUP_CONCAT with ORDER BY or DISTINCT, we want
7477     to do the same data conversion as usual, whereas
7478     Field_blob::store() jumps directly to Field_blob::store_to_mem()
7479     with the unprocessed input data.
7480 
7481   @param from   the start of the data to be stored
7482   @param length the length of the data
7483   @param cs     the character set of the data
7484   @return zero on success, non-zero on failure
7485 */
store(const char * from,size_t length,const CHARSET_INFO * cs)7486 type_conversion_status Field_json::store(const char *from, size_t length,
7487                                          const CHARSET_INFO *cs) {
7488   ASSERT_COLUMN_MARKED_FOR_WRITE;
7489 
7490   /*
7491     First clear the field so that it doesn't contain garbage if we
7492     return with an error. Some callers continue for a while even after
7493     an error has been raised, and they could get into trouble if the
7494     field contains garbage.
7495   */
7496   reset();
7497 
7498   const char *s;
7499   size_t ss;
7500   String v(from, length, cs);
7501 
7502   if (ensure_utf8mb4(v, &value, &s, &ss, true)) {
7503     return TYPE_ERR_BAD_VALUE;
7504   }
7505 
7506   const char *parse_err;
7507   size_t err_offset;
7508   std::unique_ptr<Json_dom> dom(
7509       Json_dom::parse(s, ss, &parse_err, &err_offset));
7510 
7511   if (dom.get() == nullptr) {
7512     if (parse_err != nullptr) {
7513       // Syntax error.
7514       invalid_text(parse_err, err_offset);
7515     }
7516     return TYPE_ERR_BAD_VALUE;
7517   }
7518 
7519   if (json_binary::serialize(table->in_use, dom.get(), &value))
7520     return TYPE_ERR_BAD_VALUE;
7521 
7522   return store_binary(value.ptr(), value.length());
7523 }
7524 
7525 /**
7526   Helper function for raising an error when trying to store a value
7527   into a JSON column, and that value needs to be cast to JSON before
7528   it can be stored.
7529 */
unsupported_conversion()7530 type_conversion_status Field_json::unsupported_conversion() {
7531   ASSERT_COLUMN_MARKED_FOR_WRITE;
7532   invalid_text("not a JSON text, may need CAST", 0);
7533   return TYPE_ERR_BAD_VALUE;
7534 }
7535 
7536 /**
7537   Store the provided JSON binary data in this field.
7538 
7539   @param[in] data    pointer to JSON binary data
7540   @param[in] length  the length of the binary data
7541   @return zero on success, non-zero on failure
7542 */
store_binary(const char * data,size_t length)7543 type_conversion_status Field_json::store_binary(const char *data,
7544                                                 size_t length) {
7545   /*
7546     We expect that a valid binary representation of a JSON document is
7547     passed to us.
7548   */
7549   DBUG_ASSERT(json_binary::parse_binary(data, length).is_valid());
7550 
7551   if (length > UINT_MAX32) {
7552     /* purecov: begin inspected */
7553     my_error(ER_JSON_VALUE_TOO_BIG, MYF(0));
7554     return TYPE_ERR_BAD_VALUE;
7555     /* purecov: end */
7556   }
7557 
7558   return Field_blob::store(data, length, field_charset);
7559 }
7560 
7561 /// Store a double in a JSON field. Will raise an error for now.
store(double)7562 type_conversion_status Field_json::store(double) {
7563   return unsupported_conversion();
7564 }
7565 
7566 /// Store an integer in a JSON field. Will raise an error for now.
store(longlong,bool)7567 type_conversion_status Field_json::store(longlong, bool) {
7568   return unsupported_conversion();
7569 }
7570 
7571 /// Store a decimal in a JSON field. Will raise an error for now.
store_decimal(const my_decimal *)7572 type_conversion_status Field_json::store_decimal(const my_decimal *) {
7573   return unsupported_conversion();
7574 }
7575 
7576 /// Store a TIME value in a JSON field. Will raise an error for now.
store_time(MYSQL_TIME *,uint8)7577 type_conversion_status Field_json::store_time(MYSQL_TIME *, uint8) {
7578   return unsupported_conversion();
7579 }
7580 
7581 /**
7582   Store a JSON value as binary.
7583 
7584   @param json  the JSON value to store
7585   @return zero on success, non-zero otherwise
7586 */
store_json(const Json_wrapper * json)7587 type_conversion_status Field_json::store_json(const Json_wrapper *json) {
7588   ASSERT_COLUMN_MARKED_FOR_WRITE;
7589 
7590   /*
7591     We want to serialize the JSON value directly into Field_blob::value if
7592     possible, so that we don't have to copy it there afterwards.
7593 
7594     If the Json_wrapper is pointing to a document that already lives in
7595     Field_blob::value, it isn't safe to do so, since the source buffer and
7596     destination buffer is the same. This can happen if an UPDATE statement
7597     updates the same JSON column twice and the second update of the column
7598     reads data that the first update wrote to the output buffer. For example:
7599 
7600       UPDATE t SET json_col = <something>, json_col = json_col->'$.path'
7601 
7602     In that case, we serialize into a temporary string, which is later copied
7603     into Field_blob::value by store_binary().
7604   */
7605   StringBuffer<STRING_BUFFER_USUAL_SIZE> tmpstr;
7606   String *buffer = json->is_binary_backed_by(&value) ? &tmpstr : &value;
7607 
7608   if (json->to_binary(table->in_use, buffer)) return TYPE_ERR_BAD_VALUE;
7609 
7610   return store_binary(buffer->ptr(), buffer->length());
7611 }
7612 
7613 /**
7614   Copy the contents of a non-null JSON field into this field.
7615 
7616   @param[in] field the field to copy data from
7617   @return zero on success, non-zero on failure
7618 */
store(const Field_json * field)7619 type_conversion_status Field_json::store(const Field_json *field) {
7620   /*
7621     The callers of this function have already checked for null, so we
7622     don't need to handle it here for now. Assert that field is not
7623     null.
7624   */
7625   DBUG_ASSERT(!field->is_null());
7626 
7627   String tmp;
7628   String *s = field->Field_blob::val_str(&tmp, &tmp);
7629   return store_binary(s->ptr(), s->length());
7630 }
7631 
val_json(Json_wrapper * wr) const7632 bool Field_json::val_json(Json_wrapper *wr) const {
7633   DBUG_TRACE;
7634   ASSERT_COLUMN_MARKED_FOR_READ;
7635 
7636   String tmp;
7637   String *s = Field_blob::val_str(&tmp, &tmp);
7638 
7639   json_binary::Value v(json_binary::parse_binary(s->ptr(), s->length()));
7640   if (v.type() == json_binary::Value::ERROR) {
7641     /* purecov: begin inspected */
7642     my_error(ER_INVALID_JSON_BINARY_DATA, MYF(0));
7643     return true;
7644     /* purecov: end */
7645   }
7646 
7647   *wr = Json_wrapper(v);
7648   return false;
7649 }
7650 
val_int() const7651 longlong Field_json::val_int() const {
7652   ASSERT_COLUMN_MARKED_FOR_READ;
7653 
7654   Json_wrapper wr;
7655   if (val_json(&wr)) return 0; /* purecov: inspected */
7656 
7657   return wr.coerce_int(field_name);
7658 }
7659 
val_real() const7660 double Field_json::val_real() const {
7661   ASSERT_COLUMN_MARKED_FOR_READ;
7662 
7663   Json_wrapper wr;
7664   if (val_json(&wr)) return 0.0; /* purecov: inspected */
7665 
7666   return wr.coerce_real(field_name);
7667 }
7668 
val_str(String * buf1,String *) const7669 String *Field_json::val_str(String *buf1, String *) const {
7670   ASSERT_COLUMN_MARKED_FOR_READ;
7671 
7672   /*
7673     Per contract of Field::val_str(String*,String*), buf1 should be
7674     used if the value needs to be converted to string, and buf2 should
7675     be used if the string value is already known. We need to convert,
7676     so use buf1.
7677   */
7678   buf1->length(0);
7679 
7680   Json_wrapper wr;
7681   if (val_json(&wr) || wr.to_string(buf1, true, field_name)) buf1->length(0);
7682 
7683   return buf1;
7684 }
7685 
val_decimal(my_decimal * decimal_value) const7686 my_decimal *Field_json::val_decimal(my_decimal *decimal_value) const {
7687   ASSERT_COLUMN_MARKED_FOR_READ;
7688 
7689   Json_wrapper wr;
7690   if (val_json(&wr)) {
7691     /* purecov: begin inspected */
7692     my_decimal_set_zero(decimal_value);
7693     return decimal_value;
7694     /* purecov: end */
7695   }
7696 
7697   return wr.coerce_decimal(decimal_value, field_name);
7698 }
7699 
pack_diff(uchar ** to,ulonglong value_format) const7700 bool Field_json::pack_diff(uchar **to, ulonglong value_format) const {
7701   DBUG_TRACE;
7702 
7703   const Json_diff_vector *diff_vector;
7704   get_diff_vector_and_length(value_format, &diff_vector);
7705   if (diff_vector == nullptr) return true;
7706 
7707   // We know the caller has allocated enough space, but we don't
7708   // know how much it is.  So just say that it is large, to
7709   // suppress bounds checks.
7710   String to_string((char *)*to, 0xffffFFFF, &my_charset_bin);
7711   to_string.length(0);
7712   if (diff_vector->write_binary(&to_string))
7713     // write_binary only returns true (error) in case it failed to
7714     // allocate memory. But now we know it will not try to
7715     // allocate.
7716     DBUG_ASSERT(0); /* purecov: inspected */
7717 
7718   // It should not have reallocated.
7719   DBUG_ASSERT(*to == (uchar *)to_string.ptr());
7720 
7721   *to += to_string.length();
7722   return false;
7723 }
7724 
is_before_image_equal_to_after_image() const7725 bool Field_json::is_before_image_equal_to_after_image() const {
7726   ptrdiff_t row_offset = table->record[1] - table->record[0];
7727   if (get_length(row_offset) != get_length()) return false;
7728   if (cmp(ptr, ptr + row_offset) != 0) return false;
7729   return true;
7730 }
7731 
get_diff_vector_and_length(ulonglong value_options,const Json_diff_vector ** diff_vector_p) const7732 longlong Field_json::get_diff_vector_and_length(
7733     ulonglong value_options, const Json_diff_vector **diff_vector_p) const {
7734   DBUG_TRACE;
7735 
7736   if (is_null()) {
7737     DBUG_PRINT("info", ("Returning 0 because field is NULL"));
7738     if (diff_vector_p) *diff_vector_p = nullptr;
7739     return 0;
7740   }
7741 
7742   longlong length_of_full_format = get_length();
7743   longlong length = -1;
7744   const Json_diff_vector *diff_vector = nullptr;
7745   // Is the partial update smaller than the full update?
7746   DBUG_PRINT("info", ("length_of_full_format=%lu",
7747                       (unsigned long)length_of_full_format));
7748 
7749   // Is partial JSON updates enabled at all?
7750   if ((value_options & PARTIAL_JSON_UPDATES) == 0) {
7751     DBUG_PRINT("info", ("Using full JSON format because "
7752                         "binlog_row_value_format does not include "
7753                         "PARTIAL_JSON"));
7754     length = length_of_full_format;
7755   } else {
7756     // Was the optimizer able to compute a partial update?
7757     diff_vector = table->get_logical_diffs(this);
7758     if (diff_vector == nullptr) {
7759       // Are before-image and after-image equal?
7760       if (is_before_image_equal_to_after_image()) {
7761         DBUG_PRINT("info", ("Using empty Json_diff_vector because before-image "
7762                             "is different from after-image."));
7763         diff_vector = &Json_diff_vector::EMPTY_JSON_DIFF_VECTOR;
7764         length = diff_vector->binary_length();
7765       } else {
7766         DBUG_PRINT("info",
7767                    ("Using full JSON format because there is no diff vector "
7768                     "and before-image is different from after-image."));
7769         length = length_of_full_format;
7770       }
7771     } else {
7772       longlong length_of_diff_vector = diff_vector->binary_length();
7773       longlong length_of_empty_diff_vector =
7774           Json_diff_vector::EMPTY_JSON_DIFF_VECTOR.binary_length();
7775       DBUG_PRINT("info", ("length_of_diff_vector=%lu diff_vector->size()=%u",
7776                           (unsigned long)length_of_diff_vector,
7777                           (uint)diff_vector->size()));
7778 
7779       // If the vector is empty, no need to do the expensive comparison
7780       // between before-image and after-image.
7781       if (length_of_diff_vector == length_of_empty_diff_vector) {
7782         DBUG_PRINT("info",
7783                    ("Using empty Json_diff_vector provided by optimizer."));
7784         length = length_of_diff_vector;
7785       }
7786       // Are the before-image and the after-image equal? (This can
7787       // happen despite having a nonempty diff vector, in case
7788       // optimizer does not detect the equality)
7789       else if (is_before_image_equal_to_after_image()) {
7790         DBUG_PRINT("info", ("Using Json_diff_vector::EMPTY_JSON_DIFF_VECTOR "
7791                             "because the before-image equals the after-image "
7792                             "but the diff vector provided by the optimizer "
7793                             "is non-empty."));
7794         diff_vector = &Json_diff_vector::EMPTY_JSON_DIFF_VECTOR;
7795         length = length_of_diff_vector;
7796       }
7797       // Is the diff vector better than the full format?
7798       else if (length_of_diff_vector < length_of_full_format) {
7799         DBUG_PRINT("info", ("Using non-empty Json_diff_vector provided by "
7800                             "optimizer because it is smaller than "
7801                             "full format."));
7802         length = length_of_diff_vector;
7803       } else {
7804         DBUG_PRINT("info",
7805                    ("Using full JSON format because diff vector was not "
7806                     "smaller."));
7807         diff_vector = nullptr;
7808         length = length_of_full_format;
7809       }
7810     }
7811   }
7812 
7813   /*
7814     Can be equal to zero in the corner case where user inserted NULL
7815     value in a JSON NOT NULL column in non-strict mode.  The server
7816     will store this as a zero-length non-NULL object, and interpret it
7817     as a JSON 'null' literal.
7818   */
7819   DBUG_ASSERT(length >= 0);
7820 
7821   if (diff_vector_p != nullptr) *diff_vector_p = diff_vector;
7822   return length;
7823 }
7824 
unpack_diff(const uchar ** from)7825 bool Field_json::unpack_diff(const uchar **from) {
7826   DBUG_TRACE;
7827 
7828   // Use a temporary mem_root so that the thread does not hold the
7829   // memory for the Json_diff_vector until the end of the statement.
7830   int memory_page_size = my_getpagesize();
7831   MEM_ROOT mem_root(key_memory_Slave_applier_json_diff_vector,
7832                     memory_page_size);
7833 
7834   Json_diff_vector diff_vector{Json_diff_vector::allocator_type(&mem_root)};
7835 
7836   // The caller should have verified that the buffer at 'from' is
7837   // sufficiently big to hold the whole diff_vector.
7838   if (diff_vector.read_binary(pointer_cast<const char **>(from), table,
7839                               field_name))
7840     return true;
7841 
7842   // Apply
7843   switch (apply_json_diffs(this, &diff_vector)) {
7844     case enum_json_diff_status::REJECTED:
7845       my_error(ER_COULD_NOT_APPLY_JSON_DIFF, MYF(0),
7846                (int)table->s->table_name.length, table->s->table_name.str,
7847                field_name);
7848       return true;
7849     case enum_json_diff_status::ERROR:
7850       return true; /* purecov: inspected */
7851     case enum_json_diff_status::SUCCESS:
7852       break;
7853   }
7854   return false;
7855 }
7856 
get_date(MYSQL_TIME * ltime,my_time_flags_t) const7857 bool Field_json::get_date(MYSQL_TIME *ltime, my_time_flags_t) const {
7858   ASSERT_COLUMN_MARKED_FOR_READ;
7859 
7860   Json_wrapper wr;
7861   bool result = val_json(&wr) || wr.coerce_date(ltime, field_name);
7862   if (result)
7863     set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME); /* purecov: inspected */
7864   return result;
7865 }
7866 
get_time(MYSQL_TIME * ltime) const7867 bool Field_json::get_time(MYSQL_TIME *ltime) const {
7868   ASSERT_COLUMN_MARKED_FOR_READ;
7869 
7870   Json_wrapper wr;
7871   bool result = val_json(&wr) || wr.coerce_time(ltime, field_name);
7872   if (result)
7873     set_zero_time(ltime, MYSQL_TIMESTAMP_TIME); /* purecov: inspected */
7874   return result;
7875 }
7876 
cmp_binary(const uchar * a_ptr,const uchar * b_ptr,uint32) const7877 int Field_json::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7878                            uint32 /* max_length */) const {
7879   const char *a = pointer_cast<char *>(get_blob_data(a_ptr + packlength));
7880   const char *b = pointer_cast<char *>(get_blob_data(b_ptr + packlength));
7881   uint32 a_length = get_length(a_ptr);
7882   uint32 b_length = get_length(b_ptr);
7883   Json_wrapper aw(json_binary::parse_binary(a, a_length));
7884   Json_wrapper bw(json_binary::parse_binary(b, b_length));
7885   return aw.compare(bw);
7886 }
7887 
make_sort_key(uchar * to,size_t length) const7888 size_t Field_json::make_sort_key(uchar *to, size_t length) const {
7889   Json_wrapper wr;
7890   if (val_json(&wr)) {
7891     return 0;
7892   }
7893   return wr.make_sort_key(to, length);
7894 }
7895 
make_hash_key(ulonglong hash_val) const7896 ulonglong Field_json::make_hash_key(ulonglong hash_val) const {
7897   Json_wrapper wr;
7898   if (val_json(&wr)) return hash_val; /* purecov: inspected */
7899   return wr.make_hash_key(hash_val);
7900 }
7901 
get_binary(ptrdiff_t row_offset) const7902 const char *Field_json::get_binary(ptrdiff_t row_offset) const {
7903   return pointer_cast<char *>(get_blob_data(ptr + packlength + row_offset));
7904 }
7905 
7906 /****************************************************************************
7907 ** enum type.
7908 ** This is a string which only can have a selection of different values.
7909 ** If one uses this string in a number context one gets the type number.
7910 ****************************************************************************/
7911 
key_type() const7912 enum ha_base_keytype Field_enum::key_type() const {
7913   switch (packlength) {
7914     default:
7915       return HA_KEYTYPE_BINARY;
7916     case 2:
7917       return HA_KEYTYPE_USHORT_INT;
7918     case 3:
7919       return HA_KEYTYPE_UINT24;
7920     case 4:
7921       return HA_KEYTYPE_ULONG_INT;
7922     case 8:
7923       return HA_KEYTYPE_ULONGLONG;
7924   }
7925 }
7926 
store_type(ulonglong value)7927 void Field_enum::store_type(ulonglong value) {
7928   switch (packlength) {
7929     case 1:
7930       ptr[0] = (uchar)value;
7931       break;
7932     case 2:
7933       if (table->s->db_low_byte_first)
7934         int2store(ptr, (unsigned short)value);
7935       else
7936         shortstore(ptr, (unsigned short)value);
7937       break;
7938     case 3:
7939       int3store(ptr, (long)value);
7940       break;
7941     case 4:
7942       if (table->s->db_low_byte_first)
7943         int4store(ptr, value);
7944       else
7945         longstore(ptr, (long)value);
7946       break;
7947     case 8:
7948       if (table->s->db_low_byte_first)
7949         int8store(ptr, value);
7950       else
7951         longlongstore(ptr, value);
7952       break;
7953   }
7954 }
7955 
7956 /**
7957   @note
7958     Storing a empty string in a enum field gives a warning
7959     (if there isn't a empty value in the enum)
7960 */
7961 
store(const char * from,size_t length,const CHARSET_INFO * cs)7962 type_conversion_status Field_enum::store(const char *from, size_t length,
7963                                          const CHARSET_INFO *cs) {
7964   ASSERT_COLUMN_MARKED_FOR_WRITE;
7965   int err = 0;
7966   type_conversion_status ret = TYPE_OK;
7967   char buff[STRING_BUFFER_USUAL_SIZE];
7968   String tmpstr(buff, sizeof(buff), &my_charset_bin);
7969 
7970   /* Convert character set if necessary */
7971   if (String::needs_conversion_on_storage(length, cs, field_charset)) {
7972     uint dummy_errors;
7973     tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
7974     from = tmpstr.ptr();
7975     length = tmpstr.length();
7976   }
7977 
7978   /* Remove end space */
7979   length = field_charset->cset->lengthsp(field_charset, from, length);
7980   uint tmp = find_type2(typelib, from, length, field_charset);
7981   if (!tmp) {
7982     if (length < 6)  // Can't be more than 99999 enums
7983     {
7984       /* This is for reading numbers with LOAD DATA INFILE */
7985       const char *end;
7986       tmp = (uint)my_strntoul(cs, from, length, 10, &end, &err);
7987       if (err || end != from + length || tmp > typelib->count) {
7988         tmp = 0;
7989         set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
7990         ret = TYPE_WARN_TRUNCATED;
7991       }
7992       if (!table->in_use->check_for_truncated_fields) ret = TYPE_OK;
7993     } else
7994       set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
7995   }
7996   store_type((ulonglong)tmp);
7997   return ret;
7998 }
7999 
store(double nr)8000 type_conversion_status Field_enum::store(double nr) {
8001   return Field_enum::store((longlong)nr, false);
8002 }
8003 
store(longlong nr,bool)8004 type_conversion_status Field_enum::store(longlong nr, bool) {
8005   ASSERT_COLUMN_MARKED_FOR_WRITE;
8006   type_conversion_status error = TYPE_OK;
8007   if ((ulonglong)nr > typelib->count || nr == 0) {
8008     set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
8009     if (nr != 0 || table->in_use->check_for_truncated_fields) {
8010       nr = 0;
8011       error = TYPE_WARN_TRUNCATED;
8012     }
8013   }
8014   store_type((ulonglong)(uint)nr);
8015   return error;
8016 }
8017 
val_real() const8018 double Field_enum::val_real() const { return (double)Field_enum::val_int(); }
8019 
val_decimal(my_decimal * decimal_value) const8020 my_decimal *Field_enum::val_decimal(my_decimal *decimal_value) const {
8021   ASSERT_COLUMN_MARKED_FOR_READ;
8022   int2my_decimal(E_DEC_FATAL_ERROR, val_int(), false, decimal_value);
8023   return decimal_value;
8024 }
8025 
8026 // Utility function used by ::val_int() and ::cmp()
enum_val_int(const uchar * ptr,uint packlength,bool low_byte_first)8027 static longlong enum_val_int(const uchar *ptr, uint packlength,
8028                              bool low_byte_first) {
8029   switch (packlength) {
8030     case 1:
8031       return ptr[0];
8032     case 2: {
8033       if (low_byte_first)
8034         return uint2korr(ptr);
8035       else
8036         return ushortget(ptr);
8037     }
8038     case 3:
8039       return uint3korr(ptr);
8040     case 4: {
8041       if (low_byte_first)
8042         return uint4korr(ptr);
8043       else
8044         return ulongget(ptr);
8045     }
8046     case 8: {
8047       if (low_byte_first)
8048         return sint8korr(ptr);
8049       else
8050         return longlongget(ptr);
8051     }
8052   }
8053   return 0;  // impossible
8054 }
8055 
val_int() const8056 longlong Field_enum::val_int() const {
8057   ASSERT_COLUMN_MARKED_FOR_READ;
8058   return enum_val_int(ptr, packlength, table->s->db_low_byte_first);
8059 }
8060 
8061 /**
8062    Save the field metadata for enum fields.
8063 
8064    Saves the real type in the first byte and the pack length in the
8065    second byte of the field metadata array at index of *metadata_ptr and
8066    *(metadata_ptr + 1).
8067 
8068    @param   metadata_ptr   First byte of field metadata
8069 
8070    @returns number of bytes written to metadata_ptr
8071 */
do_save_field_metadata(uchar * metadata_ptr) const8072 int Field_enum::do_save_field_metadata(uchar *metadata_ptr) const {
8073   *metadata_ptr = real_type();
8074   *(metadata_ptr + 1) = pack_length();
8075   return 2;
8076 }
8077 
val_str(String *,String * val_ptr) const8078 String *Field_enum::val_str(String *, String *val_ptr) const {
8079   uint tmp = (uint)Field_enum::val_int();
8080   if (!tmp || tmp > typelib->count)
8081     val_ptr->set("", 0, field_charset);
8082   else
8083     val_ptr->set(typelib->type_names[tmp - 1], typelib->type_lengths[tmp - 1],
8084                  field_charset);
8085   return val_ptr;
8086 }
8087 
cmp(const uchar * a_ptr,const uchar * b_ptr) const8088 int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr) const {
8089   const longlong a =
8090       enum_val_int(a_ptr, packlength, table->s->db_low_byte_first);
8091   const longlong b =
8092       enum_val_int(b_ptr, packlength, table->s->db_low_byte_first);
8093   return (a < b) ? -1 : (a > b) ? 1 : 0;
8094 }
8095 
make_sort_key(uchar * to,size_t length) const8096 size_t Field_enum::make_sort_key(uchar *to, size_t length) const {
8097 #ifdef WORDS_BIGENDIAN
8098   if (!table->s->db_low_byte_first)
8099     copy_integer<true>(to, length, ptr, packlength, true);
8100   else
8101 #endif
8102     copy_integer<false>(to, length, ptr, packlength, true);
8103   return length;
8104 }
8105 
sql_type(String & res) const8106 void Field_enum::sql_type(String &res) const {
8107   char buffer[255];
8108   String enum_item(buffer, sizeof(buffer), res.charset());
8109 
8110   res.length(0);
8111   res.append(STRING_WITH_LEN("enum("));
8112 
8113   bool flag = false;
8114   uint *len = typelib->type_lengths;
8115   for (const char **pos = typelib->type_names; *pos; pos++, len++) {
8116     uint dummy_errors;
8117     if (flag) res.append(',');
8118     /* convert to res.charset() == utf8, then quote */
8119     enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8120 
8121     const CHARSET_INFO *cs = res.charset();
8122     int well_formed_error = 42;
8123 #ifndef DBUG_OFF
8124     size_t wl =
8125 #endif
8126         cs->cset->well_formed_len(cs, enum_item.ptr(),
8127                                   enum_item.ptr() + enum_item.length(),
8128                                   enum_item.length(), &well_formed_error);
8129     DBUG_ASSERT(wl <= enum_item.length());
8130     if (well_formed_error) {
8131       // Append the hex literal instead
8132       res.append("x'");
8133       char b[6];
8134       const char *eip = enum_item.ptr();
8135       for (size_t i = 0; i < enum_item.length(); ++i) {
8136         unsigned char v = static_cast<unsigned char>(eip[i]);
8137         snprintf(b, sizeof(b), "%x", v);
8138         res.append(b);
8139       }
8140       res.append("'");
8141     } else {
8142       append_unescaped(&res, enum_item.ptr(), enum_item.length());
8143     }
8144     flag = true;
8145   }
8146   res.append(')');
8147 }
8148 
new_field(MEM_ROOT * root,TABLE * new_table) const8149 Field *Field_enum::new_field(MEM_ROOT *root, TABLE *new_table) const {
8150   Field_enum *res = down_cast<Field_enum *>(Field::new_field(root, new_table));
8151   if (res) res->typelib = copy_typelib(root, typelib);
8152   return res;
8153 }
8154 
8155 /*
8156    set type.
8157    This is a string which can have a collection of different values.
8158    Each string value is separated with a ','.
8159    For example "One,two,five"
8160    If one uses this string in a number context one gets the bits as a longlong
8161    number.
8162 */
8163 
store(const char * from,size_t length,const CHARSET_INFO * cs)8164 type_conversion_status Field_set::store(const char *from, size_t length,
8165                                         const CHARSET_INFO *cs) {
8166   ASSERT_COLUMN_MARKED_FOR_WRITE;
8167   bool got_warning = false;
8168   int err = 0;
8169   type_conversion_status ret = TYPE_OK;
8170   const char *not_used;
8171   uint not_used2;
8172   char buff[STRING_BUFFER_USUAL_SIZE];
8173   String tmpstr(buff, sizeof(buff), &my_charset_bin);
8174 
8175   /* Convert character set if necessary */
8176   if (String::needs_conversion_on_storage(length, cs, field_charset)) {
8177     uint dummy_errors;
8178     tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
8179     from = tmpstr.ptr();
8180     length = tmpstr.length();
8181   }
8182   ulonglong tmp = find_set(typelib, from, length, field_charset, &not_used,
8183                            &not_used2, &got_warning);
8184   if (!tmp && length && length < 22) {
8185     /* This is for reading numbers with LOAD DATA INFILE */
8186     const char *end;
8187     tmp = my_strntoull(cs, from, length, 10, &end, &err);
8188     if (err || end != from + length ||
8189         (typelib->count < 64 && tmp >= (1ULL << typelib->count))) {
8190       tmp = 0;
8191       set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
8192       ret = TYPE_WARN_TRUNCATED;
8193     }
8194   } else if (got_warning)
8195     set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
8196   store_type(tmp);
8197   return ret;
8198 }
8199 
store(longlong nr,bool)8200 type_conversion_status Field_set::store(longlong nr, bool) {
8201   ASSERT_COLUMN_MARKED_FOR_WRITE;
8202   type_conversion_status error = TYPE_OK;
8203   ulonglong max_nr;
8204 
8205   if (sizeof(ulonglong) * 8 <= typelib->count)
8206     max_nr = ULLONG_MAX;
8207   else
8208     max_nr = (1ULL << typelib->count) - 1;
8209 
8210   if ((ulonglong)nr > max_nr) {
8211     nr &= max_nr;
8212     set_warning(Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED, 1);
8213     error = TYPE_WARN_TRUNCATED;
8214   }
8215   store_type((ulonglong)nr);
8216   return error;
8217 }
8218 
val_str(String * val_buffer,String *) const8219 String *Field_set::val_str(String *val_buffer, String *) const {
8220   ulonglong tmp = (ulonglong)Field_enum::val_int();
8221   uint bitnr = 0;
8222 
8223   /*
8224     Some callers expect *val_buffer to contain the result,
8225     so we assign to it, rather than doing 'return &empty_set_string.
8226   */
8227   *val_buffer = empty_set_string;
8228   if (tmp == 0) {
8229     return val_buffer;
8230   }
8231 
8232   val_buffer->set_charset(field_charset);
8233   val_buffer->length(0);
8234 
8235   while (tmp && bitnr < typelib->count) {
8236     if (tmp & 1) {
8237       if (val_buffer->length())
8238         val_buffer->append(&field_separator, 1, &my_charset_latin1);
8239       String str(typelib->type_names[bitnr], typelib->type_lengths[bitnr],
8240                  field_charset);
8241       val_buffer->append(str);
8242     }
8243     tmp >>= 1;
8244     bitnr++;
8245   }
8246   return val_buffer;
8247 }
8248 
sql_type(String & res) const8249 void Field_set::sql_type(String &res) const {
8250   char buffer[255];
8251   String set_item(buffer, sizeof(buffer), res.charset());
8252 
8253   res.length(0);
8254   res.append(STRING_WITH_LEN("set("));
8255 
8256   bool flag = false;
8257   uint *len = typelib->type_lengths;
8258   for (const char **pos = typelib->type_names; *pos; pos++, len++) {
8259     uint dummy_errors;
8260     if (flag) res.append(',');
8261     /* convert to res.charset() == utf8, then quote */
8262     set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8263     append_unescaped(&res, set_item.ptr(), set_item.length());
8264     flag = true;
8265   }
8266   res.append(')');
8267 }
8268 
8269 /**
8270   @retval
8271     true   if the fields are equally defined
8272   @retval
8273     false  if the fields are unequally defined
8274 */
8275 
eq_def(const Field * field) const8276 bool Field::eq_def(const Field *field) const {
8277   if (real_type() != field->real_type() || charset() != field->charset() ||
8278       pack_length() != field->pack_length())
8279     return false;
8280   return true;
8281 }
8282 
8283 /**
8284   Compare the first t1::count type names.
8285 
8286   @return true if the type names of t1 match those of t2. false otherwise.
8287 */
8288 
compare_type_names(const CHARSET_INFO * charset,TYPELIB * t1,TYPELIB * t2)8289 static bool compare_type_names(const CHARSET_INFO *charset, TYPELIB *t1,
8290                                TYPELIB *t2) {
8291   for (uint i = 0; i < t1->count; i++)
8292     if (my_strnncoll(charset, (const uchar *)t1->type_names[i],
8293                      t1->type_lengths[i], (const uchar *)t2->type_names[i],
8294                      t2->type_lengths[i]))
8295       return false;
8296   return true;
8297 }
8298 
8299 /**
8300   @return
8301   returns 1 if the fields are equally defined
8302 */
8303 
eq_def(const Field * field) const8304 bool Field_enum::eq_def(const Field *field) const {
8305   TYPELIB *values;
8306 
8307   if (!Field::eq_def(field)) return false;
8308 
8309   values = down_cast<const Field_enum *>(field)->typelib;
8310 
8311   /* Definition must be strictly equal. */
8312   if (typelib->count != values->count) return false;
8313 
8314   return compare_type_names(field_charset, typelib, values);
8315 }
8316 
8317 /**
8318   Check whether two fields can be considered 'equal' for table
8319   alteration purposes. Fields are equal if they retain the same
8320   pack length and if new members are added to the end of the list.
8321 
8322   @return IS_EQUAL_YES if fields are compatible.
8323           IS_EQUAL_NO otherwise.
8324 */
8325 
is_equal(const Create_field * new_field) const8326 uint Field_enum::is_equal(const Create_field *new_field) const {
8327   DBUG_TRACE;
8328   /*
8329     The fields are compatible if they have the same flags,
8330     type, charset and have the same underlying length.
8331   */
8332   if (sql_type_prevents_inplace(*this, *new_field)) {
8333     DBUG_PRINT("inplace", ("-> IS_EQUAL_NO"));
8334     return IS_EQUAL_NO;
8335   }
8336   TYPELIB *new_typelib = new_field->interval;
8337 
8338   /*
8339     Changing the definition of an ENUM or SET column by adding a new
8340     enumeration or set members to the end of the list of valid member
8341     values only alters table metadata and not table data.
8342   */
8343   if (typelib->count > new_typelib->count) {
8344     DBUG_PRINT("inplace", ("(typelib->count > new_typelib->count) -> "
8345                            "IS_EQUAL_NO"));
8346     return IS_EQUAL_NO;
8347   }
8348 
8349   /* Check whether there are modification before the end. Since we
8350      know that the charset change (if there is one) is
8351      inplace-compatible, it is safe to perform the comparison using the
8352      new charset.
8353   */
8354   if (!compare_type_names(new_field->charset, typelib, new_typelib)) {
8355     DBUG_PRINT("inplace", ("!compare_type_names() -> IS_EQUAL_NO"));
8356     return IS_EQUAL_NO;
8357   }
8358 
8359   DBUG_PRINT("inplace",
8360              ("Field_enum::is_equal() -> %s",
8361               (new_field->pack_length() == pack_length() ? "IS_EQUAL_YES"
8362                                                          : "IS_EQUAL_NO")));
8363 
8364   // Can't return IS_EQUAL_PACK_LENGTH here as using inplace leads to
8365   // assert when trying to insert set elements that use bits outside
8366   // the original length:
8367   // Assertion failure: rem0rec.cc:408:len <= fixed_len thread 140301224261376
8368   return (new_field->pack_length() == pack_length() ? IS_EQUAL_YES
8369                                                     : IS_EQUAL_NO);
8370 }
8371 
pack(uchar * to,const uchar * from,size_t max_length) const8372 uchar *Field_enum::pack(uchar *to, const uchar *from, size_t max_length) const {
8373   DBUG_TRACE;
8374   DBUG_PRINT("debug", ("packlength: %d", packlength));
8375   DBUG_DUMP("from", from, packlength);
8376 
8377   switch (packlength) {
8378     case 1:
8379       if (max_length > 0) *to = *from;
8380       return to + 1;
8381     case 2:
8382       return pack_int16(to, from, max_length);
8383     case 3:
8384       return pack_int24(to, from, max_length);
8385     case 4:
8386       return pack_int32(to, from, max_length);
8387     case 8:
8388       return pack_int64(to, from, max_length);
8389     default:
8390       DBUG_ASSERT(0);
8391   }
8392   MY_ASSERT_UNREACHABLE();
8393   return nullptr;
8394 }
8395 
unpack(uchar * to,const uchar * from,uint)8396 const uchar *Field_enum::unpack(uchar *to, const uchar *from, uint) {
8397   DBUG_TRACE;
8398   DBUG_PRINT("debug", ("packlength: %d", packlength));
8399   DBUG_DUMP("from", from, packlength);
8400 
8401   switch (packlength) {
8402     case 1:
8403       *to = *from;
8404       return from + 1;
8405 
8406     case 2:
8407       return unpack_int16(to, from);
8408     case 3:
8409       return unpack_int24(to, from);
8410     case 4:
8411       return unpack_int32(to, from);
8412     case 8:
8413       return unpack_int64(to, from);
8414     default:
8415       DBUG_ASSERT(0);
8416   }
8417   MY_ASSERT_UNREACHABLE();
8418   return nullptr;
8419 }
8420 
8421 /**
8422   @return
8423   returns true if the fields are equally defined
8424 */
eq_def(const Field * field) const8425 bool Field_num::eq_def(const Field *field) const {
8426   if (!Field::eq_def(field)) return false;
8427   const Field_num *from_num = down_cast<const Field_num *>(field);
8428 
8429   if (is_unsigned() != from_num->is_unsigned() ||
8430       (zerofill && !from_num->zerofill && !zero_pack()) || dec != from_num->dec)
8431     return false;
8432   return true;
8433 }
8434 
8435 /**
8436   Check whether two numeric fields can be considered 'equal' for table
8437   alteration purposes. Fields are equal if they are of the same type
8438   and retain the same pack length.
8439 */
8440 
is_equal(const Create_field * new_field) const8441 uint Field_num::is_equal(const Create_field *new_field) const {
8442   return (new_field->sql_type == real_type()) &&
8443          (Overlaps(new_field->flags, UNSIGNED_FLAG) ==
8444           is_flag_set(UNSIGNED_FLAG)) &&
8445          (Overlaps(new_field->flags, AUTO_INCREMENT_FLAG) ==
8446           is_flag_set(AUTO_INCREMENT_FLAG)) &&
8447          (new_field->pack_length() == pack_length());
8448 }
8449 
8450 /*
8451   Bit field.
8452 
8453   We store the first 0 - 6 uneven bits among the null bits
8454   at the start of the record. The rest bytes are stored in
8455   the record itself.
8456 
8457   For example:
8458 
8459   CREATE TABLE t1 (a int, b bit(17), c bit(21) not null, d bit(8));
8460   We would store data  as follows in the record:
8461 
8462   Byte        Bit
8463   1           7 - reserve for delete
8464               6 - null bit for 'a'
8465               5 - null bit for 'b'
8466               4 - first (high) bit of 'b'
8467               3 - first (high) bit of 'c'
8468               2 - second bit of 'c'
8469               1 - third bit of 'c'
8470               0 - forth bit of 'c'
8471   2           7 - firth bit of 'c'
8472               6 - null bit for 'd'
8473   3 - 6       four bytes for 'a'
8474   7 - 8       two bytes for 'b'
8475   9 - 10      two bytes for 'c'
8476   11          one byte for 'd'
8477 */
8478 
Field_bit(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar * bit_ptr_arg,uchar bit_ofs_arg,uchar auto_flags_arg,const char * field_name_arg)8479 Field_bit::Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
8480                      uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
8481                      uchar auto_flags_arg, const char *field_name_arg)
8482     : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
8483             field_name_arg),
8484       bit_ptr(bit_ptr_arg),
8485       bit_ofs(bit_ofs_arg),
8486       bit_len(len_arg & 7),
8487       bytes_in_rec(len_arg / 8) {
8488   DBUG_TRACE;
8489   DBUG_PRINT("enter", ("ptr_arg: %p, null_ptr_arg: %p, len_arg: %u, bit_len: "
8490                        "%u, bytes_in_rec: %u",
8491                        ptr_arg, null_ptr_arg, len_arg, bit_len, bytes_in_rec));
8492   set_flag(UNSIGNED_FLAG);
8493   /*
8494     Ensure that Field::eq() can distinguish between two different bit fields.
8495     (two bit fields that are not null, may have same ptr and m_null_ptr)
8496   */
8497   if (!null_ptr_arg) null_bit = bit_ofs_arg;
8498 }
8499 
hash(ulong * nr,ulong * nr2) const8500 void Field_bit::hash(ulong *nr, ulong *nr2) const {
8501   if (is_null()) {
8502     *nr ^= (*nr << 1) | 1;
8503   } else {
8504     const CHARSET_INFO *cs = &my_charset_bin;
8505     longlong value = Field_bit::val_int();
8506     uchar tmp[8];
8507     mi_int8store(tmp, value);
8508 
8509     uint64 tmp1 = *nr;
8510     uint64 tmp2 = *nr2;
8511     cs->coll->hash_sort(cs, tmp, 8, &tmp1, &tmp2);
8512 
8513     // NOTE: This truncates to 32-bit on Windows, to keep on-disk stability.
8514     *nr = static_cast<ulong>(tmp1);
8515     *nr2 = static_cast<ulong>(tmp2);
8516   }
8517 }
8518 
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uchar * new_null_ptr,uint new_null_bit) const8519 Field *Field_bit::new_key_field(MEM_ROOT *root, TABLE *new_table,
8520                                 uchar *new_ptr, uchar *new_null_ptr,
8521                                 uint new_null_bit) const {
8522   Field_bit *res;
8523   if ((res = (Field_bit *)Field::new_key_field(root, new_table, new_ptr,
8524                                                new_null_ptr, new_null_bit))) {
8525     /* Move bits normally stored in null_pointer to new_ptr */
8526     res->bit_ptr = new_ptr;
8527     res->bit_ofs = 0;
8528     if (bit_len) res->ptr++;  // Store rest of data here
8529   }
8530   return res;
8531 }
8532 
is_equal(const Create_field * new_field) const8533 uint Field_bit::is_equal(const Create_field *new_field) const {
8534   return (new_field->sql_type == real_type() &&
8535           new_field->max_display_width_in_bytes() == max_display_length());
8536 }
8537 
reset()8538 type_conversion_status Field_bit::reset() {
8539   memset(ptr, 0, bytes_in_rec);
8540   if (bit_ptr && (bit_len > 0))  // reset odd bits among null bits
8541     clr_rec_bits(bit_ptr, bit_ofs, bit_len);
8542   return TYPE_OK;
8543 }
8544 
store(const char * from,size_t length,const CHARSET_INFO *)8545 type_conversion_status Field_bit::store(const char *from, size_t length,
8546                                         const CHARSET_INFO *) {
8547   ASSERT_COLUMN_MARKED_FOR_WRITE;
8548   int delta;
8549 
8550   for (; length && !*from; from++, length--)
8551     ;  // skip left 0's
8552   delta = bytes_in_rec - static_cast<int>(length);
8553 
8554   /*
8555    *from should probably be treated like uint here see BUG#13727586
8556    */
8557   if (delta < -1 || (delta == -1 && (uchar)*from > ((1 << bit_len) - 1)) ||
8558       (!bit_len && delta < 0)) {
8559     set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
8560     memset(ptr, 0xff, bytes_in_rec);
8561     if (table->in_use->is_strict_mode())
8562       set_warning(Sql_condition::SL_WARNING, ER_DATA_TOO_LONG, 1);
8563     else
8564       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
8565     return TYPE_WARN_OUT_OF_RANGE;
8566   }
8567   /* delta is >= -1 here */
8568   if (delta > 0) {
8569     if (bit_len) clr_rec_bits(bit_ptr, bit_ofs, bit_len);
8570     memset(ptr, 0, delta);
8571     memcpy(ptr + delta, from, length);
8572   } else if (delta == 0) {
8573     if (bit_len) clr_rec_bits(bit_ptr, bit_ofs, bit_len);
8574     memcpy(ptr, from, length);
8575   } else {
8576     if (bit_len) {
8577       set_rec_bits((uchar)*from, bit_ptr, bit_ofs, bit_len);
8578       from++;
8579     }
8580     memcpy(ptr, from, bytes_in_rec);
8581   }
8582   return TYPE_OK;
8583 }
8584 
store(double nr)8585 type_conversion_status Field_bit::store(double nr) {
8586   return Field_bit::store((longlong)nr, false);
8587 }
8588 
store(longlong nr,bool)8589 type_conversion_status Field_bit::store(longlong nr, bool) {
8590   char buf[8];
8591 
8592   mi_int8store(buf, nr);
8593   return store(buf, 8, nullptr);
8594 }
8595 
store_decimal(const my_decimal * val)8596 type_conversion_status Field_bit::store_decimal(const my_decimal *val) {
8597   bool has_overflow = false;
8598   longlong i = convert_decimal2longlong(val, true, &has_overflow);
8599   type_conversion_status res = store(i, true);
8600   return has_overflow ? TYPE_WARN_OUT_OF_RANGE : res;
8601 }
8602 
val_real() const8603 double Field_bit::val_real() const { return (double)Field_bit::val_int(); }
8604 
val_int() const8605 longlong Field_bit::val_int() const {
8606   ASSERT_COLUMN_MARKED_FOR_READ;
8607   ulonglong bits = 0;
8608   if (bit_len) {
8609     bits = get_rec_bits(bit_ptr, bit_ofs, bit_len);
8610     bits <<= (bytes_in_rec * 8);
8611   }
8612 
8613   switch (bytes_in_rec) {
8614     case 0:
8615       return bits;
8616     case 1:
8617       return bits | (ulonglong)ptr[0];
8618     case 2:
8619       return bits | mi_uint2korr(ptr);
8620     case 3:
8621       return bits | mi_uint3korr(ptr);
8622     case 4:
8623       return bits | mi_uint4korr(ptr);
8624     case 5:
8625       return bits | mi_uint5korr(ptr);
8626     case 6:
8627       return bits | mi_uint6korr(ptr);
8628     case 7:
8629       return bits | mi_uint7korr(ptr);
8630     default:
8631       return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
8632   }
8633 }
8634 
val_str(String * val_buffer,String *) const8635 String *Field_bit::val_str(String *val_buffer, String *) const {
8636   ASSERT_COLUMN_MARKED_FOR_READ;
8637   char buff[sizeof(longlong)];
8638   uint length = min<uint>(pack_length(), sizeof(longlong));
8639   ulonglong bits = val_int();
8640   mi_int8store(buff, bits);
8641 
8642   val_buffer->alloc(length);
8643   memcpy(val_buffer->ptr(), buff + 8 - length, length);
8644   val_buffer->length(length);
8645   val_buffer->set_charset(&my_charset_bin);
8646   return val_buffer;
8647 }
8648 
val_decimal(my_decimal * deciaml_value) const8649 my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value) const {
8650   ASSERT_COLUMN_MARKED_FOR_READ;
8651   int2my_decimal(E_DEC_FATAL_ERROR, val_int(), true, deciaml_value);
8652   return deciaml_value;
8653 }
8654 
8655 /*
8656   Compare two bit fields using pointers within the record.
8657   SYNOPSIS
8658     cmp_max()
8659     a                 Pointer to field->ptr in first record
8660     b                 Pointer to field->ptr in second record
8661   DESCRIPTION
8662     This method is used from key_rec_cmp used by merge sorts used
8663     by partitioned index read and later other similar places.
8664     The a and b pointer must be pointers to the field in a record
8665     (not the table->record[0] necessarily)
8666 */
cmp_max(const uchar * a,const uchar * b,uint) const8667 int Field_bit::cmp_max(const uchar *a, const uchar *b, uint) const {
8668   ptrdiff_t a_diff = a - ptr;
8669   ptrdiff_t b_diff = b - ptr;
8670   if (bit_len) {
8671     int flag;
8672     uchar bits_a = get_rec_bits(bit_ptr + a_diff, bit_ofs, bit_len);
8673     uchar bits_b = get_rec_bits(bit_ptr + b_diff, bit_ofs, bit_len);
8674     if ((flag = (int)(bits_a - bits_b))) return flag;
8675   }
8676   return memcmp(a, b, pack_length());
8677 }
8678 
key_cmp(const uchar * str,uint length) const8679 int Field_bit::key_cmp(const uchar *str, uint length) const {
8680   if (bit_len) {
8681     int flag;
8682     uchar bits = get_rec_bits(bit_ptr, bit_ofs, bit_len);
8683     if ((flag = (int)(bits - *str))) return flag;
8684     str++;
8685     length--;
8686   }
8687   return memcmp(ptr, str, length);
8688 }
8689 
cmp_offset(ptrdiff_t row_offset) const8690 int Field_bit::cmp_offset(ptrdiff_t row_offset) const {
8691   if (bit_len) {
8692     int flag;
8693     uchar bits_a = get_rec_bits(bit_ptr, bit_ofs, bit_len);
8694     uchar bits_b = get_rec_bits(bit_ptr + row_offset, bit_ofs, bit_len);
8695     if ((flag = (int)(bits_a - bits_b))) return flag;
8696   }
8697   return memcmp(ptr, ptr + row_offset, bytes_in_rec);
8698 }
8699 
get_key_image(uchar * buff,size_t length,imagetype) const8700 size_t Field_bit::get_key_image(uchar *buff, size_t length, imagetype) const {
8701   if (bit_len) {
8702     uchar bits = get_rec_bits(bit_ptr, bit_ofs, bit_len);
8703     *buff++ = bits;
8704     length--;
8705   }
8706   size_t data_length = min(length, static_cast<size_t>(bytes_in_rec));
8707   memcpy(buff, ptr, data_length);
8708   return data_length + 1;
8709 }
8710 
8711 /**
8712    Save the field metadata for bit fields.
8713 
8714    Saves the bit length in the first byte and bytes in record in the
8715    second byte of the field metadata array at index of *metadata_ptr and
8716    *(metadata_ptr + 1).
8717 
8718    @param   metadata_ptr   First byte of field metadata
8719 
8720    @returns number of bytes written to metadata_ptr
8721 */
do_save_field_metadata(uchar * metadata_ptr) const8722 int Field_bit::do_save_field_metadata(uchar *metadata_ptr) const {
8723   DBUG_TRACE;
8724   DBUG_PRINT("debug", ("bit_len: %d, bytes_in_rec: %d", bit_len, bytes_in_rec));
8725   /*
8726     Since this class and Field_bit_as_char have different ideas of
8727     what should be stored here, we compute the values of the metadata
8728     explicitly using the field_length.
8729    */
8730   metadata_ptr[0] = field_length % 8;
8731   metadata_ptr[1] = field_length / 8;
8732   return 2;
8733 }
8734 
8735 /**
8736    Returns the number of bytes field uses in row-based replication
8737    row packed size.
8738 
8739    This method is used in row-based replication to determine the number
8740    of bytes that the field consumes in the row record format. This is
8741    used to skip fields in the master that do not exist on the slave.
8742 
8743    @param   field_metadata   Encoded size in field metadata
8744 
8745    @returns The size of the field based on the field metadata.
8746 */
pack_length_from_metadata(uint field_metadata) const8747 uint Field_bit::pack_length_from_metadata(uint field_metadata) const {
8748   uint const from_len = (field_metadata >> 8U) & 0x00ff;
8749   uint const from_bit_len = field_metadata & 0x00ff;
8750   uint const source_size = from_len + ((from_bit_len > 0) ? 1 : 0);
8751   return (source_size);
8752 }
8753 
8754 /**
8755    Check to see if field size is compatible with destination.
8756 
8757    This method is used in row-based replication to verify that the slave's
8758    field size is less than or equal to the master's field size. The
8759    encoded field metadata (from the master or source) is decoded and compared
8760    to the size of this field (the slave or destination).
8761 
8762    @param   field_metadata   Encoded size in field metadata
8763    @param   mflags           Flags from the table map event for the table.
8764    @param   order_var        Pointer to variable where the order
8765                              between the source field and this field
8766                              will be returned.
8767 
8768    @return @c true
8769 */
compatible_field_size(uint field_metadata,Relay_log_info *,uint16 mflags,int * order_var) const8770 bool Field_bit::compatible_field_size(uint field_metadata, Relay_log_info *,
8771                                       uint16 mflags, int *order_var) const {
8772   DBUG_TRACE;
8773   DBUG_ASSERT((field_metadata >> 16) == 0);
8774   uint from_bit_len = 8 * (field_metadata >> 8) + (field_metadata & 0xff);
8775   uint to_bit_len = max_display_length();
8776   DBUG_PRINT("debug",
8777              ("from_bit_len: %u, to_bit_len: %u", from_bit_len, to_bit_len));
8778   /*
8779     If the bit length exact flag is clear, we are dealing with an old
8780     master, so we allow some less strict behaviour if replicating by
8781     moving both bit lengths to an even multiple of 8.
8782 
8783     We do this by computing the number of bytes to store the field
8784     instead, and then compare the result.
8785    */
8786   if (!(mflags & Table_map_log_event::TM_BIT_LEN_EXACT_F)) {
8787     from_bit_len = (from_bit_len + 7) / 8;
8788     to_bit_len = (to_bit_len + 7) / 8;
8789   }
8790 
8791   *order_var = compare(from_bit_len, to_bit_len);
8792   return true;
8793 }
8794 
sql_type(String & res) const8795 void Field_bit::sql_type(String &res) const {
8796   const CHARSET_INFO *cs = res.charset();
8797   size_t length = cs->cset->snprintf(cs, res.ptr(), res.alloced_length(),
8798                                      "bit(%d)", (int)field_length);
8799   res.length(length);
8800 }
8801 
pack(uchar * to,const uchar * from,size_t max_length) const8802 uchar *Field_bit::pack(uchar *to, const uchar *from, size_t max_length) const {
8803   if (max_length == 0) {
8804     return to + 1;
8805   }
8806   size_t length;
8807   if (bit_len > 0) {
8808     /*
8809       We have the following:
8810 
8811       ptr        Points into a field in record R1
8812       from       Points to a field in a record R2
8813       bit_ptr    Points to the byte (in the null bytes) that holds the
8814                  odd bits of R1
8815       from_bitp  Points to the byte that holds the odd bits of R2
8816 
8817       We have the following:
8818 
8819           ptr - bit_ptr = from - from_bitp
8820 
8821       We want to isolate 'from_bitp', so this gives:
8822 
8823           ptr - bit_ptr - from = - from_bitp
8824           - ptr + bit_ptr + from = from_bitp
8825           bit_ptr + from - ptr = from_bitp
8826      */
8827     uchar bits = get_rec_bits(bit_ptr + (from - ptr), bit_ofs, bit_len);
8828     *to++ = bits;
8829   }
8830   length = min<size_t>(bytes_in_rec, max_length - (bit_len > 0));
8831   memcpy(to, from, length);
8832   return to + length;
8833 }
8834 
8835 /**
8836    Unpack a bit field from row data.
8837 
8838    This method is used to unpack a bit field from a master whose size
8839    of the field is less than that of the slave.
8840 
8841    @param   to         Destination of the data
8842    @param   from       Source of the data
8843    @param   param_data Bit length (upper) and length (lower) values
8844 
8845    @return  New pointer into memory based on from + length of the data
8846 */
unpack(uchar * to,const uchar * from,uint param_data)8847 const uchar *Field_bit::unpack(uchar *to, const uchar *from, uint param_data) {
8848   DBUG_TRACE;
8849   DBUG_PRINT("enter",
8850              ("to: %p, from: %p, param_data: 0x%x", to, from, param_data));
8851   DBUG_PRINT("debug", ("bit_ptr: %p, bit_len: %u, bit_ofs: %u", bit_ptr,
8852                        bit_len, bit_ofs));
8853   uint const from_len = (param_data >> 8U) & 0x00ff;
8854   uint const from_bit_len = param_data & 0x00ff;
8855   DBUG_PRINT("debug",
8856              ("from_len: %u, from_bit_len: %u", from_len, from_bit_len));
8857   /*
8858     If the parameter data is zero (i.e., undefined), or if the master
8859     and slave have the same sizes, then use the old unpack() method.
8860   */
8861   if (param_data == 0 ||
8862       ((from_bit_len == bit_len) && (from_len == bytes_in_rec))) {
8863     if (bit_len > 0) {
8864       /*
8865         set_rec_bits is a macro, don't put the post-increment in the
8866         argument since that might cause strange side-effects.
8867 
8868         For the choice of the second argument, see the explanation for
8869         Field_bit::pack().
8870       */
8871       set_rec_bits(*from, bit_ptr + (to - ptr), bit_ofs, bit_len);
8872       from++;
8873     }
8874     memcpy(to, from, bytes_in_rec);
8875     return from + bytes_in_rec;
8876   }
8877 
8878   /*
8879     We are converting a smaller bit field to a larger one here.
8880     To do that, we first need to construct a raw value for the original
8881     bit value stored in the from buffer. Then that needs to be converted
8882     to the larger field then sent to store() for writing to the field.
8883     Lastly the odd bits need to be masked out if the bytes_in_rec > 0.
8884     Otherwise stray bits can cause spurious values.
8885   */
8886   uint new_len = (field_length + 7) / 8;
8887   char *value = (char *)my_alloca(new_len);
8888   memset(value, 0, new_len);
8889   uint len = from_len + ((from_bit_len > 0) ? 1 : 0);
8890   memcpy(value + (new_len - len), from, len);
8891   /*
8892     Mask out the unused bits in the partial byte.
8893     TODO: Add code to the master to always mask these bits and remove
8894           the following.
8895   */
8896   if ((from_bit_len > 0) && (from_len > 0))
8897     value[new_len - len] = value[new_len - len] & ((1U << from_bit_len) - 1);
8898   bitmap_set_bit(table->write_set, field_index());
8899   store(value, new_len, system_charset_info);
8900   return from + len;
8901 }
8902 
set_default()8903 void Field_bit::set_default() {
8904   if (bit_len > 0) {
8905     ptrdiff_t offset = table->default_values_offset();
8906     uchar bits = get_rec_bits(bit_ptr + offset, bit_ofs, bit_len);
8907     set_rec_bits(bits, bit_ptr, bit_ofs, bit_len);
8908   }
8909   Field::set_default();
8910 }
8911 
8912 /*
8913   Bit field support for non-MyISAM tables.
8914 */
8915 
Field_bit_as_char(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg)8916 Field_bit_as_char::Field_bit_as_char(uchar *ptr_arg, uint32 len_arg,
8917                                      uchar *null_ptr_arg, uchar null_bit_arg,
8918                                      uchar auto_flags_arg,
8919                                      const char *field_name_arg)
8920     : Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, nullptr, 0,
8921                 auto_flags_arg, field_name_arg) {
8922   set_flag(UNSIGNED_FLAG);
8923   bit_len = 0;
8924   bytes_in_rec = (len_arg + 7) / 8;
8925 }
8926 
store(const char * from,size_t length,const CHARSET_INFO *)8927 type_conversion_status Field_bit_as_char::store(const char *from, size_t length,
8928                                                 const CHARSET_INFO *) {
8929   ASSERT_COLUMN_MARKED_FOR_WRITE;
8930   int delta;
8931   uchar bits = (uchar)(field_length & 7);
8932 
8933   for (; length && !*from; from++, length--)
8934     ;  // skip left 0's
8935   delta = bytes_in_rec - static_cast<int>(length);
8936 
8937   if (delta < 0 ||
8938       (delta == 0 && bits && (uint)(uchar)*from >= (uint)(1 << bits))) {
8939     memset(ptr, 0xff, bytes_in_rec);
8940     if (bits) *ptr &= ((1 << bits) - 1); /* set first uchar */
8941     if (table->in_use->is_strict_mode())
8942       set_warning(Sql_condition::SL_WARNING, ER_DATA_TOO_LONG, 1);
8943     else
8944       set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
8945     return TYPE_WARN_OUT_OF_RANGE;
8946   }
8947   memset(ptr, 0, delta);
8948   memcpy(ptr + delta, from, length);
8949   return TYPE_OK;
8950 }
8951 
sql_type(String & res) const8952 void Field_bit_as_char::sql_type(String &res) const {
8953   const CHARSET_INFO *cs = res.charset();
8954   size_t length = cs->cset->snprintf(cs, res.ptr(), res.alloced_length(),
8955                                      "bit(%d)", (int)field_length);
8956   res.length(length);
8957 }
8958 
8959 /*****************************************************************************
8960   Handling of field and Create_field
8961 *****************************************************************************/
8962 
8963 /**
8964   Calculate key length for field from its type, length and other attributes.
8965 
8966   @note for string fields "length" parameter is assumed to take into account
8967         character set.
8968 
8969   TODO: Get rid of this function as its code is redundant with
8970         Field::key_length() code. However creation of Field object using
8971         make_field() just to call Field::key_length() is probably overkill.
8972 */
8973 
calc_key_length(enum_field_types sql_type,uint32 length,uint32 decimals,bool is_unsigned,uint32 elements)8974 uint32 calc_key_length(enum_field_types sql_type, uint32 length,
8975                        uint32 decimals, bool is_unsigned, uint32 elements) {
8976   uint precision;
8977   switch (sql_type) {
8978     case MYSQL_TYPE_TINY_BLOB:
8979     case MYSQL_TYPE_MEDIUM_BLOB:
8980     case MYSQL_TYPE_LONG_BLOB:
8981     case MYSQL_TYPE_BLOB:
8982     case MYSQL_TYPE_GEOMETRY:
8983     case MYSQL_TYPE_JSON:
8984       return 0;
8985     case MYSQL_TYPE_VARCHAR:
8986       return length;
8987     case MYSQL_TYPE_ENUM:
8988       return get_enum_pack_length(elements);
8989     case MYSQL_TYPE_SET:
8990       return get_set_pack_length(elements);
8991     case MYSQL_TYPE_BIT:
8992       return length / 8 + (length & 7 ? 1 : 0);
8993       break;
8994     case MYSQL_TYPE_NEWDECIMAL:
8995       precision = std::min<uint>(
8996           my_decimal_length_to_precision(length, decimals, is_unsigned),
8997           DECIMAL_MAX_PRECISION);
8998       return my_decimal_get_binary_size(precision, decimals);
8999     default:
9000       return calc_pack_length(sql_type, length);
9001   }
9002 }
9003 
get_blob_type_from_length(size_t length)9004 enum_field_types get_blob_type_from_length(size_t length) {
9005   enum_field_types type;
9006   if (length < 256)
9007     type = MYSQL_TYPE_TINY_BLOB;
9008   else if (length < 65536)
9009     type = MYSQL_TYPE_BLOB;
9010   else if (length < 256L * 256L * 256L)
9011     type = MYSQL_TYPE_MEDIUM_BLOB;
9012   else
9013     type = MYSQL_TYPE_LONG_BLOB;
9014   return type;
9015 }
9016 
calc_pack_length(enum_field_types type,size_t length)9017 size_t calc_pack_length(enum_field_types type, size_t length) {
9018   switch (type) {
9019     case MYSQL_TYPE_VAR_STRING:
9020     case MYSQL_TYPE_STRING:
9021     case MYSQL_TYPE_DECIMAL:
9022       return (length);
9023     case MYSQL_TYPE_VARCHAR:
9024       return (length + (length < 256 ? 1 : 2));
9025     case MYSQL_TYPE_YEAR:
9026     case MYSQL_TYPE_TINY:
9027       return 1;
9028     case MYSQL_TYPE_SHORT:
9029       return 2;
9030     case MYSQL_TYPE_INT24:
9031     case MYSQL_TYPE_NEWDATE:
9032       return 3;
9033     case MYSQL_TYPE_TIME:
9034       return 3;
9035     case MYSQL_TYPE_TIME2:
9036       return length > MAX_TIME_WIDTH
9037                  ? my_time_binary_length(length - MAX_TIME_WIDTH - 1)
9038                  : 3;
9039     case MYSQL_TYPE_TIMESTAMP:
9040       return 4;
9041     case MYSQL_TYPE_TIMESTAMP2:
9042       return length > MAX_DATETIME_WIDTH
9043                  ? my_timestamp_binary_length(length - MAX_DATETIME_WIDTH - 1)
9044                  : 4;
9045     case MYSQL_TYPE_DATE:
9046     case MYSQL_TYPE_LONG:
9047       return 4;
9048     case MYSQL_TYPE_FLOAT:
9049       return sizeof(float);
9050     case MYSQL_TYPE_DOUBLE:
9051       return sizeof(double);
9052     case MYSQL_TYPE_DATETIME:
9053       return 8;
9054     case MYSQL_TYPE_DATETIME2:
9055       return length > MAX_DATETIME_WIDTH
9056                  ? my_datetime_binary_length(length - MAX_DATETIME_WIDTH - 1)
9057                  : 5;
9058     case MYSQL_TYPE_LONGLONG:
9059       return 8; /* Don't crash if no longlong */
9060     case MYSQL_TYPE_NULL:
9061       return 0;
9062     case MYSQL_TYPE_TINY_BLOB:
9063       return 1 + portable_sizeof_char_ptr;
9064     case MYSQL_TYPE_BLOB:
9065       return 2 + portable_sizeof_char_ptr;
9066     case MYSQL_TYPE_MEDIUM_BLOB:
9067       return 3 + portable_sizeof_char_ptr;
9068     case MYSQL_TYPE_LONG_BLOB:
9069       return 4 + portable_sizeof_char_ptr;
9070     case MYSQL_TYPE_GEOMETRY:
9071       return 4 + portable_sizeof_char_ptr;
9072     case MYSQL_TYPE_JSON:
9073       return 4 + portable_sizeof_char_ptr;
9074     case MYSQL_TYPE_SET:
9075     case MYSQL_TYPE_ENUM:
9076     case MYSQL_TYPE_NEWDECIMAL:
9077       DBUG_ASSERT(false);
9078       return 0;  // This shouldn't happen
9079     case MYSQL_TYPE_BIT:
9080       return length / 8;
9081     default:
9082       return 0;
9083   }
9084 }
9085 
calc_pack_length(dd::enum_column_types type,size_t char_length,size_t elements_count,bool treat_bit_as_char,uint numeric_scale,bool is_unsigned)9086 size_t calc_pack_length(dd::enum_column_types type, size_t char_length,
9087                         size_t elements_count, bool treat_bit_as_char,
9088                         uint numeric_scale, bool is_unsigned) {
9089   size_t pack_length = 0;
9090 
9091   switch (type) {
9092     case dd::enum_column_types::TINY_BLOB:
9093     case dd::enum_column_types::MEDIUM_BLOB:
9094     case dd::enum_column_types::LONG_BLOB:
9095     case dd::enum_column_types::BLOB:
9096     case dd::enum_column_types::GEOMETRY:
9097     case dd::enum_column_types::VAR_STRING:
9098     case dd::enum_column_types::STRING:
9099     case dd::enum_column_types::VARCHAR:
9100       // The length is already calculated in number of bytes, no need
9101       // to multiply by number of bytes per symbol.
9102       pack_length = calc_pack_length(dd_get_old_field_type(type), char_length);
9103       break;
9104     case dd::enum_column_types::ENUM:
9105       pack_length = get_enum_pack_length(elements_count);
9106       break;
9107     case dd::enum_column_types::SET:
9108       pack_length = get_set_pack_length(elements_count);
9109       break;
9110     case dd::enum_column_types::BIT: {
9111       if (treat_bit_as_char)
9112         pack_length = ((char_length + 7) & ~7) / 8;
9113       else
9114         pack_length = char_length / 8;
9115     } break;
9116     case dd::enum_column_types::NEWDECIMAL: {
9117       uint decimals = numeric_scale;
9118       uint precision = std::min(
9119           my_decimal_length_to_precision(char_length, decimals, is_unsigned),
9120           uint(DECIMAL_MAX_PRECISION));
9121       DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
9122                   (decimals <= DECIMAL_MAX_SCALE));
9123       pack_length = my_decimal_get_binary_size(precision, decimals);
9124     } break;
9125     default:
9126       pack_length = calc_pack_length(dd_get_old_field_type(type), char_length);
9127       break;
9128   }
9129   return pack_length;
9130 }
9131 
make_field(MEM_ROOT * mem_root,TABLE_SHARE * share,uchar * ptr,size_t field_length,uchar * null_pos,uchar null_bit,enum_field_types field_type,const CHARSET_INFO * field_charset,Field::geometry_type geom_type,uchar auto_flags,TYPELIB * interval,const char * field_name,bool is_nullable,bool is_zerofill,bool is_unsigned,uint decimals,bool treat_bit_as_char,uint pack_length_override,Nullable<gis::srid_t> srid,bool is_array)9132 Field *make_field(MEM_ROOT *mem_root, TABLE_SHARE *share, uchar *ptr,
9133                   size_t field_length, uchar *null_pos, uchar null_bit,
9134                   enum_field_types field_type,
9135                   const CHARSET_INFO *field_charset,
9136                   Field::geometry_type geom_type, uchar auto_flags,
9137                   TYPELIB *interval, const char *field_name, bool is_nullable,
9138                   bool is_zerofill, bool is_unsigned, uint decimals,
9139                   bool treat_bit_as_char, uint pack_length_override,
9140                   Nullable<gis::srid_t> srid, bool is_array) {
9141   uchar *bit_ptr = nullptr;
9142   uchar bit_offset = 0;
9143   DBUG_ASSERT(mem_root);
9144 
9145   if (field_type == MYSQL_TYPE_BIT && !treat_bit_as_char) {
9146     bit_ptr = null_pos;
9147     bit_offset = null_bit;
9148     if (is_nullable)  // if null field
9149     {
9150       bit_ptr += (null_bit == 7);  // shift bit_ptr and bit_offset
9151       bit_offset = (bit_offset + 1) & 7;
9152     }
9153   }
9154 
9155   if (!is_nullable) {
9156     null_pos = nullptr;
9157     null_bit = 0;
9158   } else {
9159     null_bit = ((uchar)1) << null_bit;
9160   }
9161 
9162   if (is_temporal_real_type(field_type)) field_charset = &my_charset_numeric;
9163 
9164   DBUG_PRINT("debug", ("field_type: %d, field_length: %zu, "
9165                        "interval: %p, is_array: %s",
9166                        field_type, field_length, interval,
9167                        (is_array ? "true" : "false")));
9168 
9169   if (is_array) {
9170     // See Item_func_array_cast::resolve_type() for supported types
9171     switch (field_type) {
9172       case MYSQL_TYPE_VARCHAR:
9173       case MYSQL_TYPE_NEWDECIMAL:
9174       case MYSQL_TYPE_LONGLONG:
9175       case MYSQL_TYPE_NEWDATE:
9176         break;
9177       case MYSQL_TYPE_TIME2:
9178         decimals = (field_length > MAX_TIME_WIDTH)
9179                        ? field_length - 1 - MAX_TIME_WIDTH
9180                        : 0;
9181         break;
9182       case MYSQL_TYPE_DATETIME2:
9183         decimals = (field_length > MAX_DATETIME_WIDTH)
9184                        ? field_length - 1 - MAX_DATETIME_WIDTH
9185                        : 0;
9186         break;
9187       default:
9188         DBUG_ASSERT(0);  // Shouldn't happen
9189         return nullptr;
9190     }
9191     /*
9192       Field_json constructor expects number of bytes used to represent length
9193       of the underlying blob as parameter and not the real field pack_length.
9194       JSON is used as array storage.
9195     */
9196     uint pack_length = calc_pack_length(MYSQL_TYPE_JSON, field_length) -
9197                        portable_sizeof_char_ptr;
9198 
9199     return new (mem_root) Field_typed_array(
9200         field_type, is_unsigned, field_length, decimals, ptr, null_pos,
9201         null_bit, auto_flags, field_name, share, pack_length, field_charset);
9202   }
9203   /*
9204     FRMs from 3.23/4.0 can have strings with field_type == MYSQL_TYPE_DECIMAL.
9205     We should not be getting them after upgrade to new data-dictionary.
9206   */
9207 
9208   switch (field_type) {
9209     case MYSQL_TYPE_VAR_STRING:
9210     case MYSQL_TYPE_STRING:
9211       return new (mem_root) Field_string(ptr, field_length, null_pos, null_bit,
9212                                          auto_flags, field_name, field_charset);
9213     case MYSQL_TYPE_VARCHAR:
9214       return new (mem_root) Field_varstring(
9215           ptr, field_length, HA_VARCHAR_PACKLENGTH(field_length), null_pos,
9216           null_bit, auto_flags, field_name, share, field_charset);
9217     case MYSQL_TYPE_BLOB:
9218     case MYSQL_TYPE_MEDIUM_BLOB:
9219     case MYSQL_TYPE_TINY_BLOB:
9220     case MYSQL_TYPE_LONG_BLOB: {
9221       /*
9222         Field_blob constructor expects number of bytes used to represent length
9223         of the blob as parameter and not the real field pack_length.
9224       */
9225       uint pack_length =
9226           calc_pack_length(field_type, field_length) - portable_sizeof_char_ptr;
9227 
9228       return new (mem_root)
9229           Field_blob(ptr, null_pos, null_bit, auto_flags, field_name, share,
9230                      pack_length, field_charset);
9231     }
9232     case MYSQL_TYPE_GEOMETRY: {
9233       /*
9234         Field_geom constructor expects number of bytes used to represent length
9235         of the underlying blob as parameter and not the real field pack_length.
9236       */
9237       uint pack_length =
9238           calc_pack_length(field_type, field_length) - portable_sizeof_char_ptr;
9239 
9240       return new (mem_root)
9241           Field_geom(ptr, null_pos, null_bit, auto_flags, field_name, share,
9242                      pack_length, geom_type, srid);
9243     }
9244     case MYSQL_TYPE_JSON: {
9245       /*
9246         Field_json constructor expects number of bytes used to represent length
9247         of the underlying blob as parameter and not the real field pack_length.
9248       */
9249       uint pack_length =
9250           calc_pack_length(field_type, field_length) - portable_sizeof_char_ptr;
9251 
9252       return new (mem_root) Field_json(ptr, null_pos, null_bit, auto_flags,
9253                                        field_name, share, pack_length);
9254     }
9255     case MYSQL_TYPE_ENUM:
9256       DBUG_ASSERT(interval);
9257       return new (mem_root) Field_enum(
9258           ptr, field_length, null_pos, null_bit, auto_flags, field_name,
9259           (pack_length_override ? pack_length_override
9260                                 : get_enum_pack_length(interval->count)),
9261           interval, field_charset);
9262     case MYSQL_TYPE_SET:
9263       DBUG_ASSERT(interval);
9264       return new (mem_root) Field_set(
9265           ptr, field_length, null_pos, null_bit, auto_flags, field_name,
9266           (pack_length_override ? pack_length_override
9267                                 : get_set_pack_length(interval->count)),
9268           interval, field_charset);
9269     case MYSQL_TYPE_DECIMAL:
9270       return new (mem_root)
9271           Field_decimal(ptr, field_length, null_pos, null_bit, auto_flags,
9272                         field_name, decimals, is_zerofill, is_unsigned);
9273     case MYSQL_TYPE_NEWDECIMAL:
9274       return new (mem_root)
9275           Field_new_decimal(ptr, field_length, null_pos, null_bit, auto_flags,
9276                             field_name, decimals, is_zerofill, is_unsigned);
9277     case MYSQL_TYPE_FLOAT:
9278       return new (mem_root)
9279           Field_float(ptr, field_length, null_pos, null_bit, auto_flags,
9280                       field_name, decimals, is_zerofill, is_unsigned);
9281     case MYSQL_TYPE_DOUBLE:
9282       return new (mem_root)
9283           Field_double(ptr, field_length, null_pos, null_bit, auto_flags,
9284                        field_name, decimals, is_zerofill, is_unsigned);
9285     case MYSQL_TYPE_TINY:
9286       return new (mem_root)
9287           Field_tiny(ptr, field_length, null_pos, null_bit, auto_flags,
9288                      field_name, is_zerofill, is_unsigned);
9289     case MYSQL_TYPE_SHORT:
9290       return new (mem_root)
9291           Field_short(ptr, field_length, null_pos, null_bit, auto_flags,
9292                       field_name, is_zerofill, is_unsigned);
9293     case MYSQL_TYPE_INT24:
9294       return new (mem_root)
9295           Field_medium(ptr, field_length, null_pos, null_bit, auto_flags,
9296                        field_name, is_zerofill, is_unsigned);
9297     case MYSQL_TYPE_LONG:
9298       return new (mem_root)
9299           Field_long(ptr, field_length, null_pos, null_bit, auto_flags,
9300                      field_name, is_zerofill, is_unsigned);
9301     case MYSQL_TYPE_LONGLONG:
9302       return new (mem_root)
9303           Field_longlong(ptr, field_length, null_pos, null_bit, auto_flags,
9304                          field_name, is_zerofill, is_unsigned);
9305     case MYSQL_TYPE_TIMESTAMP:
9306       return new (mem_root) Field_timestamp(ptr, field_length, null_pos,
9307                                             null_bit, auto_flags, field_name);
9308     case MYSQL_TYPE_TIMESTAMP2:
9309       return new (mem_root)
9310           Field_timestampf(ptr, null_pos, null_bit, auto_flags, field_name,
9311                            field_length > MAX_DATETIME_WIDTH
9312                                ? field_length - 1 - MAX_DATETIME_WIDTH
9313                                : 0);
9314     case MYSQL_TYPE_YEAR:
9315       DBUG_ASSERT(field_length == 4);  // Field_year is only for length 4.
9316       return new (mem_root)
9317           Field_year(ptr, null_pos, null_bit, auto_flags, field_name);
9318     case MYSQL_TYPE_NEWDATE:
9319       return new (mem_root)
9320           Field_newdate(ptr, null_pos, null_bit, auto_flags, field_name);
9321 
9322     case MYSQL_TYPE_TIME:
9323       return new (mem_root)
9324           Field_time(ptr, null_pos, null_bit, auto_flags, field_name);
9325     case MYSQL_TYPE_TIME2:
9326       return new (mem_root) Field_timef(
9327           ptr, null_pos, null_bit, auto_flags, field_name,
9328           (field_length > MAX_TIME_WIDTH) ? field_length - 1 - MAX_TIME_WIDTH
9329                                           : 0);
9330     case MYSQL_TYPE_DATETIME:
9331       return new (mem_root)
9332           Field_datetime(ptr, null_pos, null_bit, auto_flags, field_name);
9333     case MYSQL_TYPE_DATETIME2:
9334       return new (mem_root)
9335           Field_datetimef(ptr, null_pos, null_bit, auto_flags, field_name,
9336                           (field_length > MAX_DATETIME_WIDTH)
9337                               ? field_length - 1 - MAX_DATETIME_WIDTH
9338                               : 0);
9339     case MYSQL_TYPE_NULL:
9340       return new (mem_root)
9341           Field_null(ptr, field_length, auto_flags, field_name, field_charset);
9342     case MYSQL_TYPE_BIT:
9343       return treat_bit_as_char
9344                  ? new (mem_root)
9345                        Field_bit_as_char(ptr, field_length, null_pos, null_bit,
9346                                          auto_flags, field_name)
9347                  : new (mem_root)
9348                        Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
9349                                  bit_offset, auto_flags, field_name);
9350 
9351     default:  // Impossible (Wrong version)
9352       break;
9353   }
9354   return nullptr;
9355 }
9356 
make_field(const Create_field & create_field,TABLE_SHARE * share,const char * field_name,size_t field_length,uchar * ptr,uchar * null_pos,size_t null_bit)9357 Field *make_field(const Create_field &create_field, TABLE_SHARE *share,
9358                   const char *field_name, size_t field_length, uchar *ptr,
9359                   uchar *null_pos, size_t null_bit) {
9360   return make_field(*THR_MALLOC, share, ptr, field_length, null_pos, null_bit,
9361                     create_field.sql_type, create_field.charset,
9362                     create_field.geom_type, create_field.auto_flags,
9363                     create_field.interval, field_name, create_field.is_nullable,
9364                     create_field.is_zerofill, create_field.is_unsigned,
9365                     create_field.decimals, create_field.treat_bit_as_char,
9366                     create_field.pack_length_override, create_field.m_srid,
9367                     create_field.is_array);
9368 }
9369 
make_field(const Create_field & create_field,TABLE_SHARE * share,uchar * ptr,uchar * null_pos,size_t null_bit)9370 Field *make_field(const Create_field &create_field, TABLE_SHARE *share,
9371                   uchar *ptr, uchar *null_pos, size_t null_bit) {
9372   return make_field(create_field, share, create_field.field_name,
9373                     create_field.max_display_width_in_bytes(), ptr, null_pos,
9374                     null_bit);
9375 }
9376 
make_field(const Create_field & create_field,TABLE_SHARE * share)9377 Field *make_field(const Create_field &create_field, TABLE_SHARE *share) {
9378   return make_field(create_field, share, create_field.field_name,
9379                     create_field.max_display_width_in_bytes(), nullptr, nullptr,
9380                     0);
9381 }
9382 
9383 /**
9384   maximum possible character length for blob.
9385 
9386   This method is used in Item_field::set_field to calculate
9387   max_length for Item.
9388 
9389   For example:
9390     CREATE TABLE t2 SELECT CONCAT(tinyblob_utf8_column) FROM t1;
9391   must create a "VARCHAR(255) CHARACTER SET utf8" column.
9392 
9393   @return
9394     length
9395 */
9396 
char_length() const9397 uint32 Field_blob::char_length() const {
9398   switch (packlength) {
9399     case 1:
9400       return 255;
9401     case 2:
9402       return 65535;
9403     case 3:
9404       return 16777215;
9405     case 4:
9406       return (uint32)4294967295U;
9407     default:
9408       DBUG_ASSERT(0);  // we should never go here
9409       return 0;
9410   }
9411 }
9412 
9413 /**
9414   This function creates a separate copy of blob value.
9415 
9416   @param [in] mem_root
9417     mem_root that is used to allocate memory for 'copy_of_value'.
9418 
9419   @return - Can fail if we are out of memory.
9420     @retval false   Success
9421     @retval true    Failure
9422 */
9423 
copy_blob_value(MEM_ROOT * mem_root)9424 bool Field_blob::copy_blob_value(MEM_ROOT *mem_root) {
9425   DBUG_TRACE;
9426 
9427   // Testing memory allocation failure
9428   DBUG_EXECUTE_IF("simulate_blob_memory_allocation_fail",
9429                   DBUG_SET("+d,simulate_out_of_memory"););
9430 
9431   // Allocate new memory location
9432   size_t ulen = get_length(ptr);
9433   if (ulen == 0) {
9434     value.set("", 0, value.charset());
9435   } else {
9436     char *blob_value =
9437         static_cast<char *>(memdup_root(mem_root, get_blob_data(), ulen));
9438     if (blob_value == nullptr) return true;
9439 
9440     // Set 'value' with the duplicated data
9441     value.set(blob_value, ulen, value.charset());
9442   }
9443 
9444   // Set ptr of Field for duplicated data
9445   store_ptr_and_length(value.ptr(), ulen);
9446 
9447   return false;
9448 }
9449 
9450 /**
9451   maximum possible display length for blob.
9452 
9453   @return
9454     length
9455 */
9456 
max_display_length() const9457 uint32 Field_blob::max_display_length() const {
9458   switch (packlength) {
9459     case 1:
9460       return 255 * field_charset->mbmaxlen;
9461     case 2:
9462       return 65535 * field_charset->mbmaxlen;
9463     case 3:
9464       return 16777215 * field_charset->mbmaxlen;
9465     case 4:
9466       return (uint32)4294967295U;
9467     default:
9468       DBUG_ASSERT(0);  // we should never go here
9469       return 0;
9470   }
9471 }
9472 
9473 /*****************************************************************************
9474  Warning handling
9475 *****************************************************************************/
9476 
9477 /**
9478   Produce warning or note about data saved into field.
9479 
9480   @param level            - level of message (Note/Warning/Error)
9481   @param code             - error code of message to be produced
9482   @param truncate_increment  - whether we should increase truncated fields
9483   count
9484   @param view_db_name     - if set this is the database name for view
9485                             that causes the warning
9486   @param view_name        - if set this is the name of view that causes
9487                             the warning
9488 
9489   @note
9490     This function won't produce warning and increase cut fields counter
9491     if check_for_truncated_fields == CHECK_FIELD_IGNORE for current thread.
9492 
9493     if check_for_truncated_fields == CHECK_FIELD_IGNORE then we ignore notes.
9494     This allows us to avoid notes in optimisation, like convert_constant_item().
9495 
9496     In case of execution statements INSERT/INSERT SELECT/REPLACE/REPLACE SELECT
9497     the method emits only one warning message for the following
9498     types of warning: ER_BAD_NULL_ERROR, ER_WARN_NULL_TO_NOTNULL,
9499     ER_NO_DEFAULT_FOR_FIELD.
9500   @retval
9501     1 if check_for_truncated_fields == CHECK_FIELD_IGNORE and error level
9502     is not NOTE
9503   @retval
9504     0 otherwise
9505 */
9506 
set_warning(Sql_condition::enum_severity_level level,uint code,int truncate_increment,const char * view_db_name,const char * view_name)9507 bool Field::set_warning(Sql_condition::enum_severity_level level, uint code,
9508                         int truncate_increment, const char *view_db_name,
9509                         const char *view_name) {
9510   /*
9511     If this field was created only for type conversion purposes it
9512     will have table == NULL.
9513   */
9514 
9515   THD *thd = table ? table->in_use : current_thd;
9516 
9517   if (!thd->check_for_truncated_fields)
9518     return level >= Sql_condition::SL_WARNING;
9519 
9520   thd->num_truncated_fields += truncate_increment;
9521 
9522   if (thd->lex->sql_command != SQLCOM_INSERT &&
9523       thd->lex->sql_command != SQLCOM_INSERT_SELECT &&
9524       thd->lex->sql_command != SQLCOM_REPLACE &&
9525       thd->lex->sql_command != SQLCOM_REPLACE_SELECT) {
9526     // We aggregate warnings from only INSERT and REPLACE statements.
9527 
9528     push_warning_printf(thd, level, code, ER_THD_NONCONST(thd, code),
9529                         field_name,
9530                         thd->get_stmt_da()->current_row_for_condition());
9531 
9532     return false;
9533   }
9534 
9535   unsigned int current_warning_mask = 0;
9536 
9537   if (code == ER_BAD_NULL_ERROR)
9538     current_warning_mask = BAD_NULL_ERROR_PUSHED;
9539   else if (code == ER_NO_DEFAULT_FOR_FIELD)
9540     current_warning_mask = NO_DEFAULT_FOR_FIELD_PUSHED;
9541 
9542   if (current_warning_mask) {
9543     if (!(m_warnings_pushed & current_warning_mask)) {
9544       push_warning_printf(thd, level, code, ER_THD_NONCONST(thd, code),
9545                           field_name,
9546                           thd->get_stmt_da()->current_row_for_condition());
9547       m_warnings_pushed |= current_warning_mask;
9548     }
9549   } else if (code == ER_NO_DEFAULT_FOR_VIEW_FIELD) {
9550     if (!(m_warnings_pushed & NO_DEFAULT_FOR_VIEW_FIELD_PUSHED)) {
9551       push_warning_printf(
9552           thd, Sql_condition::SL_WARNING, ER_NO_DEFAULT_FOR_VIEW_FIELD,
9553           ER_THD(thd, ER_NO_DEFAULT_FOR_VIEW_FIELD), view_db_name, view_name);
9554       m_warnings_pushed |= NO_DEFAULT_FOR_VIEW_FIELD_PUSHED;
9555     }
9556   } else {
9557     push_warning_printf(thd, level, code, ER_THD_NONCONST(thd, code),
9558                         field_name,
9559                         thd->get_stmt_da()->current_row_for_condition());
9560   }
9561 
9562   return false;
9563 }
9564 
9565 /**
9566   Produce warning or note about double datetime data saved into field.
9567 
9568   @param level            level of message (Note/Warning/Error)
9569   @param code             error code of message to be produced
9570   @param val              error parameter (the value)
9571   @param ts_type          type of datetime value (datetime/date/time)
9572   @param truncate_increment  whether we should increase truncated fields count
9573 
9574   @retval false  Function reported warning
9575   @retval true   Function reported error
9576 
9577   @note
9578     This function will always produce some warning but won't increase truncated
9579     fields counter if check_for_truncated_fields == FIELD_CHECK_IGNORE
9580     for current thread.
9581 */
set_datetime_warning(Sql_condition::enum_severity_level level,uint code,const ErrConvString & val,enum_mysql_timestamp_type ts_type,int truncate_increment)9582 bool Field_temporal::set_datetime_warning(
9583     Sql_condition::enum_severity_level level, uint code,
9584     const ErrConvString &val, enum_mysql_timestamp_type ts_type,
9585     int truncate_increment) {
9586   THD *thd = table ? table->in_use : current_thd;
9587   if ((!thd->lex->is_ignore() &&
9588        ((thd->variables.sql_mode & MODE_STRICT_ALL_TABLES) ||
9589         (thd->variables.sql_mode & MODE_STRICT_TRANS_TABLES &&
9590          !thd->get_transaction()->cannot_safely_rollback(
9591              Transaction_ctx::STMT)))) ||
9592       set_warning(level, code, truncate_increment))
9593     return make_truncated_value_warning(thd, level, val, ts_type, field_name);
9594 
9595   return false;
9596 }
9597 
is_part_of_actual_key(THD * thd,uint cur_index,KEY * cur_index_info) const9598 bool Field::is_part_of_actual_key(THD *thd, uint cur_index,
9599                                   KEY *cur_index_info) const {
9600   return thd->optimizer_switch_flag(OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS) &&
9601                  !(cur_index_info->flags & HA_NOSAME)
9602              ? part_of_key.is_set(cur_index)
9603              : part_of_key_not_extended.is_set(cur_index);
9604 }
9605 
Field_typed_array(const Field_typed_array & other)9606 Field_typed_array::Field_typed_array(const Field_typed_array &other)
9607     : Field_json(other),
9608       m_elt_type(other.m_elt_type),
9609       m_elt_decimals(other.m_elt_decimals),
9610       m_elt_charset(other.m_elt_charset),
9611       unsigned_flag(other.is_unsigned()) {}
9612 
Field_typed_array(enum_field_types elt_type,bool elt_is_unsigned,size_t elt_length,uint elt_decimals,uchar * ptr_arg,uchar * null_ptr_arg,uint null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,TABLE_SHARE * share,uint blob_pack_length,const CHARSET_INFO * cs)9613 Field_typed_array::Field_typed_array(
9614     enum_field_types elt_type, bool elt_is_unsigned, size_t elt_length,
9615     uint elt_decimals, uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
9616     uchar auto_flags_arg, const char *field_name_arg, TABLE_SHARE *share,
9617     uint blob_pack_length, const CHARSET_INFO *cs)
9618     : Field_json(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
9619                  field_name_arg, share, blob_pack_length),
9620       m_elt_type(elt_type),
9621       m_elt_decimals(elt_decimals),
9622       m_elt_charset(cs),
9623       unsigned_flag(elt_is_unsigned) {
9624   if (elt_is_unsigned) set_flag(UNSIGNED_FLAG);
9625   if (Field_typed_array::binary()) set_flag(BINARY_FLAG);
9626   field_length = elt_length;
9627   /*
9628     Arrays of BLOB aren't supported and can't be created, so mask the BLOB
9629     flag of JSON
9630   */
9631   clear_flag(BLOB_FLAG);
9632   DBUG_ASSERT(elt_type != MYSQL_TYPE_STRING &&
9633               elt_type != MYSQL_TYPE_VAR_STRING);
9634 }
9635 
key_length() const9636 uint32 Field_typed_array::key_length() const {
9637   return calc_key_length(m_elt_type, field_length, m_elt_decimals,
9638                          is_unsigned(),
9639                          // Number of intervals isn't applicable here
9640                          0);
9641 }
9642 
clone(MEM_ROOT * mem_root) const9643 Field_typed_array *Field_typed_array::clone(MEM_ROOT *mem_root) const {
9644   DBUG_ASSERT(is_array());
9645   return new (mem_root) Field_typed_array(*this);
9646 }
9647 
result_type() const9648 Item_result Field_typed_array::result_type() const {
9649   return field_types_result_type[field_type2index(m_elt_type)];
9650 }
9651 
store(const char * to,size_t length,const CHARSET_INFO * charset)9652 type_conversion_status Field_typed_array::store(const char *to, size_t length,
9653                                                 const CHARSET_INFO *charset) {
9654   return m_conv_item->field->store(to, length, charset);
9655 }
9656 
store(double nr)9657 type_conversion_status Field_typed_array::store(double nr) {
9658   return m_conv_item->field->store(nr);
9659 }
9660 
store(longlong nr,bool unsigned_val)9661 type_conversion_status Field_typed_array::store(longlong nr,
9662                                                 bool unsigned_val) {
9663   return m_conv_item->field->store(nr, unsigned_val);
9664 }
9665 
store_array(const Json_wrapper * data,Json_array * array)9666 type_conversion_status Field_typed_array::store_array(const Json_wrapper *data,
9667                                                       Json_array *array) {
9668   array->clear();
9669 
9670   set_null();
9671 
9672   try {
9673     // How to store values
9674     switch (data->type()) {
9675       case enum_json_type::J_NULL: {
9676         /*
9677           Unlike SQL NULL, JSON null is a value, but a special one and it
9678           can't be coerced to any data type. The latter means it can't be
9679           indexed by relational SE. Due to that an error is thrown.
9680         */
9681         my_error(ER_INVALID_JSON_VALUE_FOR_FUNC_INDEX, MYF(0),
9682                  get_index_name());
9683         return TYPE_ERR_BAD_VALUE;
9684       }
9685       case enum_json_type::J_DECIMAL:
9686       case enum_json_type::J_DOUBLE:
9687       case enum_json_type::J_STRING:
9688       case enum_json_type::J_DATE:
9689       case enum_json_type::J_TIME:
9690       case enum_json_type::J_DATETIME:
9691       case enum_json_type::J_TIMESTAMP:
9692       case enum_json_type::J_INT:
9693       case enum_json_type::J_UINT: {
9694         // Handle scalars
9695         Json_wrapper coerced;
9696         if (coerce_json_value(data, false, &coerced))
9697           return TYPE_ERR_BAD_VALUE; /* purecov: inspected */
9698         coerced.set_alias();
9699         if (array->append_alias(coerced.to_dom(table->in_use)))
9700           return TYPE_ERR_OOM;
9701         Json_wrapper wr(array, true);
9702         /*
9703           No need to check multi-valued key limits, as single value is always
9704           allowed if engine supports multi-valued index, and single value
9705           can't outgrow index length limit.
9706         */
9707         set_notnull();
9708         return store_json(&wr);
9709       }
9710       case enum_json_type::J_ARRAY: {
9711         // Handle array
9712         Json_wrapper coerced;
9713         uint max_num_keys = 0;
9714         size_t keys_length = 0, max_keys_length = 0;
9715 
9716         // Empty array stored as non-NULL empty array
9717         if (data->length() == 0) {
9718           Json_wrapper wr(array, true);
9719           set_notnull();
9720           return store_json(&wr);
9721         }
9722         table->file->ha_mv_key_capacity(&max_num_keys, &max_keys_length);
9723         DBUG_ASSERT(max_num_keys && max_keys_length);
9724         for (size_t i = 0; i < data->length(); i++) {
9725           Json_wrapper elt = (*data)[i];
9726           if (elt.type() == enum_json_type::J_NULL) {
9727             /*
9728               Unlike SQL NULL, JSON null is a value, but a special one and it
9729               can't be coerced to any data type. The latter means it can't be
9730               indexed by relational SE. Due to that an error is thrown.
9731             */
9732             my_error(ER_INVALID_JSON_VALUE_FOR_FUNC_INDEX, MYF(0),
9733                      get_index_name());
9734             return TYPE_ERR_BAD_VALUE;
9735           }
9736 
9737           if (coerce_json_value(&elt, false, &coerced))
9738             return TYPE_ERR_BAD_VALUE;
9739           coerced.set_alias();
9740           if (array->append_alias(coerced.to_dom(table->in_use)))
9741             return TYPE_ERR_OOM;
9742           if (type() == MYSQL_TYPE_VARCHAR)
9743             keys_length += coerced.get_data_length();
9744           else
9745             keys_length += m_conv_item->field->pack_length();
9746         }
9747         /*
9748           Non-strict mode issue:
9749           While consisting of unique values, bad input can cause duplicates
9750           after coercion. Remove them, as SE doesn't expect dups in the array.
9751           This is why we need to sort & remove duplicates only after
9752           processing all keys.
9753         */
9754         if (array->size() > 1)
9755           array->remove_duplicates(type() == MYSQL_TYPE_VARCHAR ? m_elt_charset
9756                                                                 : nullptr);
9757         if (array->size() > max_num_keys) {
9758           my_error(ER_EXCEEDED_MV_KEYS_NUM, MYF(0), get_index_name(),
9759                    array->size() - max_num_keys);
9760           return TYPE_ERR_BAD_VALUE;
9761         }
9762         if (keys_length > max_keys_length) {
9763           // Array fields have only one index defined over them
9764           my_error(ER_EXCEEDED_MV_KEYS_SPACE, MYF(0), get_index_name(),
9765                    (keys_length - max_keys_length));
9766           return TYPE_ERR_BAD_VALUE;
9767         }
9768         Json_wrapper wr(array, true);
9769         set_notnull();
9770         return store_json(&wr);
9771       }
9772       case enum_json_type::J_BOOLEAN: {
9773         my_error(ER_NOT_SUPPORTED_YET, MYF(0),
9774                  "CAST-ing JSON BOOLEAN type to array");
9775         return TYPE_ERR_BAD_VALUE;
9776       }
9777 
9778       case enum_json_type::J_OBJECT: {
9779         my_error(ER_NOT_SUPPORTED_YET, MYF(0),
9780                  "CAST-ing JSON OBJECT type to array");
9781         return TYPE_ERR_BAD_VALUE;
9782       }
9783       case enum_json_type::J_ERROR:
9784         my_error(ER_INVALID_JSON_VALUE_FOR_FUNC_INDEX, MYF(0),
9785                  get_index_name());
9786         return TYPE_ERR_BAD_VALUE;
9787       /* purecov: begin inspected */
9788       default:
9789         // Shouldn't happen
9790         DBUG_ASSERT(0);
9791         return TYPE_ERR_BAD_VALUE;
9792     }
9793   } catch (...) {
9794     handle_std_exception("typed array field");
9795   }
9796   return TYPE_ERR_BAD_VALUE;
9797   /* purecov: end */
9798 }
9799 
get_key_image(uchar * buff,size_t length,imagetype type) const9800 size_t Field_typed_array::get_key_image(uchar *buff, size_t length,
9801                                         imagetype type) const {
9802   return m_conv_item->field->get_key_image(buff, length, type);
9803 }
9804 
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uchar *,uint) const9805 Field *Field_typed_array::new_key_field(MEM_ROOT *root, TABLE *new_table,
9806                                         uchar *new_ptr, uchar *, uint) const {
9807   Field *res = m_conv_item->field->new_key_field(root, new_table, new_ptr);
9808   if (res != nullptr) {
9809     // Keep the field hidden to allow error handler to catch functional
9810     // index's errors
9811     res->set_hidden(dd::Column::enum_hidden_type::HT_HIDDEN_SQL);
9812     res->part_of_key = part_of_key;
9813   }
9814   return res;
9815 }
9816 
init(TABLE * table_arg)9817 void Field_typed_array::init(TABLE *table_arg) {
9818   Field::init(table_arg);
9819 
9820   switch (type()) {
9821     case MYSQL_TYPE_VARCHAR:
9822     case MYSQL_TYPE_TIME:
9823     case MYSQL_TYPE_DATETIME:
9824     case MYSQL_TYPE_TIMESTAMP:
9825     case MYSQL_TYPE_DATE:
9826     case MYSQL_TYPE_LONG:
9827     case MYSQL_TYPE_LONGLONG:
9828     case MYSQL_TYPE_NEWDECIMAL:
9829       break;
9830     default:
9831       // Shouldn't happen
9832       DBUG_ASSERT(0); /* purecov: inspected */
9833       return;
9834   }
9835 
9836   // Set mem_root for conversion field allocation.
9837   MEM_ROOT *actual_mem_root =
9838       (table_arg->s->table_category == TABLE_CATEGORY_TEMPORARY)
9839           ? &table_arg->s->mem_root
9840           : &table_arg->mem_root;
9841   // Create field for data conversion
9842   Field *conv_field = ::make_field(
9843       // Allocate conversion field in table's mem_root for non-temp
9844       // tables. Allocate conversion field in TABLE_SHARE's mem_root
9845       // for internal temporary tables.
9846       actual_mem_root,
9847       nullptr,       // TABLE_SHARE, not needed
9848       nullptr,       // data buffer, isn't allocated yet
9849       field_length,  // field_length
9850       nullptr, 0,    // null_pos, nul_bit
9851       real_type(),   // field_type
9852       m_elt_charset,
9853       Field::GEOM_GEOMETRY,  // geom type
9854       Field::NONE,           // auto_flags
9855       nullptr,               // intervals aren't supported in array
9856       field_name, is_nullable(),
9857       false,  // zerofill is meaningless with JSON
9858       is_unsigned(), m_elt_decimals,
9859       false,  // treat_bit_as_char
9860       0,      // pack_length_override
9861       {},     // srid
9862       false   // is_array
9863   );
9864   if (conv_field == nullptr) return;
9865   uchar *buf =
9866       actual_mem_root->ArrayAlloc<uchar>(conv_field->pack_length() + 1);
9867   if (buf == nullptr) return;
9868   if (type() == MYSQL_TYPE_NEWDECIMAL)
9869     (down_cast<Field_new_decimal *>(conv_field))->set_keep_precision(true);
9870   conv_field->move_field(buf + 1, buf, 0);
9871   // Allow conv_field to use table->in_use
9872   conv_field->table = table;
9873   conv_field->table_name = table_name;
9874   conv_field->set_field_index(field_index());
9875 
9876   // Swap arena so that the Item_field is allocated on TABLE::mem_root for
9877   // non-temp tables and so it does not end up in THD's item list which will
9878   // have a different lifetime than TABLE::mem_root
9879   Query_arena tmp_arena(actual_mem_root, Query_arena::STMT_REGULAR_EXECUTION);
9880   Query_arena backup_arena;
9881   current_thd->swap_query_arena(tmp_arena, &backup_arena);
9882   m_conv_item = new Item_field(conv_field);
9883   current_thd->swap_query_arena(backup_arena, &tmp_arena);
9884 }
9885 
get_index_name() const9886 const char *Field_typed_array::get_index_name() const {
9887   uint key = part_of_key.get_first_set();
9888   DBUG_ASSERT(key != MY_BIT_NONE);
9889   return table->s->key_info[key].name;
9890 }
9891 
make_sort_key(Json_wrapper * wr,uchar * to,size_t length) const9892 size_t Field_typed_array::make_sort_key(Json_wrapper *wr, uchar *to,
9893                                         size_t length) const {
9894 #ifndef DBUG_OFF
9895   switch (wr->type()) {
9896     case enum_json_type::J_ERROR:
9897     case enum_json_type::J_OBJECT:
9898     case enum_json_type::J_ARRAY:
9899       // Only scalars are supported
9900       DBUG_ASSERT(false);
9901       break;
9902     default:
9903       break;
9904   }
9905 #endif
9906   THD *thd = table->in_use;
9907   // Force error on bad data
9908 #ifndef DBUG_OFF
9909   bool res =
9910 #endif
9911       save_json_to_field(thd, m_conv_item->field, wr, true);
9912   // Data should be already properly converted so no error is expected here
9913   DBUG_ASSERT(!res && !thd->is_error());
9914 
9915   return m_conv_item->field->make_sort_key(to, length);
9916 }
9917 
do_save_field_metadata(uchar * metadata_ptr) const9918 int Field_typed_array::do_save_field_metadata(uchar *metadata_ptr) const {
9919   *metadata_ptr = static_cast<uchar>(m_elt_type);
9920   switch (m_elt_type) {
9921     case MYSQL_TYPE_VARCHAR: {
9922       DBUG_ASSERT(field_length < 65536);
9923       char *param_ptr = (char *)(metadata_ptr + 1);
9924       int3store(param_ptr, field_length);
9925       return 4;
9926     }
9927     case MYSQL_TYPE_NEWDECIMAL: {
9928       DBUG_ASSERT(field_length < 128);
9929       uint8 precision = my_decimal_length_to_precision(field_length, decimals(),
9930                                                        is_unsigned());
9931       *(metadata_ptr + 1) = precision;
9932       *(metadata_ptr + 2) = decimals();
9933       return 3;
9934     }
9935     case MYSQL_TYPE_LONGLONG:
9936     case MYSQL_TYPE_NEWDATE:
9937       return 1;
9938     case MYSQL_TYPE_TIME2:
9939     case MYSQL_TYPE_DATETIME2:
9940       *(metadata_ptr + 1) = decimals();
9941       return 2;
9942     default:
9943       break;
9944   }
9945   DBUG_ASSERT(0);  // Shouldn't happen
9946   return 0;
9947 }
9948 
sql_type(String & str) const9949 void Field_typed_array::sql_type(String &str) const {
9950   const Field *const conv_field = m_conv_item->field;
9951   // There is no need to append the character set and collation to the type,
9952   // since utf8mb4_0900_bin is the only collation supported for arrays.
9953   DBUG_ASSERT(!conv_field->has_charset() ||
9954               conv_field->charset() == &my_charset_utf8mb4_0900_bin);
9955   conv_field->sql_type(str);
9956   str.append(STRING_WITH_LEN(" array"));
9957 }
9958 
make_send_field(Send_field * field) const9959 void Field_typed_array::make_send_field(Send_field *field) const {
9960   Field_json::make_send_field(field);
9961   // When sending the array to the client (only possible using the debug flag
9962   // show_hidden_columns), it should be sent as a JSON array. Set the type to
9963   // JSON instead of the array element type.
9964   field->type = MYSQL_TYPE_JSON;
9965 }
9966 
set_field_index(uint16 field_index)9967 void Field_typed_array::set_field_index(uint16 field_index) {
9968   Field::set_field_index(field_index);
9969   if (m_conv_item) m_conv_item->field->set_field_index(field_index);
9970 }
9971 
get_covering_prefix_keys() const9972 Key_map Field::get_covering_prefix_keys() const {
9973   if (table == nullptr) {
9974     // This function might be called when creating functional indexes. In those
9975     // cases, we do not have a table object available. Assert that the function
9976     // is indeed called in a functional index context, and then return an empty
9977     // key map.
9978     DBUG_ASSERT(down_cast<const Create_field_wrapper *>(this) != nullptr);
9979     return Key_map();
9980   }
9981   Key_map covering_prefix_keys = part_of_prefixkey;
9982   covering_prefix_keys.intersect(table->covering_keys);
9983   return covering_prefix_keys;
9984 }
9985 
set_default()9986 void Field::set_default() {
9987   if (has_insert_default_datetime_value_expression() ||
9988       has_insert_default_general_value_expression())
9989     evaluate_insert_default_function();
9990   else
9991     copy_data(table->default_values_offset());
9992 }
9993 
null_offset() const9994 uint Field::null_offset() const { return null_offset(table->record[0]); }
9995 
init(TABLE * table_arg)9996 void Field::init(TABLE *table_arg) {
9997   orig_table = table = table_arg;
9998   table_name = &table_arg->alias;
9999 }
10000 
10001 // Byteswaps and/or truncates int16 values; used for both pack() and unpack().
handle_int16(uchar * to,const uchar * from,size_t max_length,bool low_byte_first_from,bool low_byte_first_to)10002 static inline void handle_int16(uchar *to, const uchar *from, size_t max_length,
10003                                 bool low_byte_first_from,
10004                                 bool low_byte_first_to) {
10005   int16 val;
10006   uchar buf[sizeof(val)];
10007   if (low_byte_first_from)
10008     val = sint2korr(from);
10009   else
10010     val = shortget(from);
10011 
10012   if (low_byte_first_to)
10013     int2store(buf, val);
10014   else
10015     shortstore(buf, val);
10016   if (max_length >= sizeof(buf)) {
10017     // Common case.
10018     memcpy(to, buf, sizeof(buf));
10019   } else {
10020     memcpy(to, buf, max_length);
10021   }
10022 }
10023 
10024 // Byteswaps and/or truncates int24 values; used for both pack() and unpack().
handle_int24(uchar * to,const uchar * from,size_t max_length,bool low_byte_first_from MY_ATTRIBUTE ((unused)),bool low_byte_first_to MY_ATTRIBUTE ((unused)))10025 static inline void handle_int24(uchar *to, const uchar *from, size_t max_length,
10026                                 bool low_byte_first_from MY_ATTRIBUTE((unused)),
10027                                 bool low_byte_first_to MY_ATTRIBUTE((unused))) {
10028   int32 val;
10029   uchar buf[3];
10030 #ifdef WORDS_BIGENDIAN
10031   if (low_byte_first_from)
10032     val = sint3korr(from);
10033   else
10034 #endif
10035     val = (from[0] << 16) + (from[1] << 8) + from[2];
10036 
10037 #ifdef WORDS_BIGENDIAN
10038   if (low_byte_first_to)
10039     int3store(buf, val);
10040   else
10041 #endif
10042   {
10043     buf[0] = 0xFF & (val >> 16);
10044     buf[1] = 0xFF & (val >> 8);
10045     buf[2] = 0xFF & val;
10046   }
10047   if (max_length >= sizeof(buf)) {
10048     // Common case.
10049     memcpy(to, buf, sizeof(buf));
10050   } else {
10051     memcpy(to, buf, max_length);
10052   }
10053 }
10054 
10055 // Byteswaps and/or truncates int32 values; used for both pack() and unpack().
handle_int32(uchar * to,const uchar * from,size_t max_length,bool low_byte_first_from,bool low_byte_first_to)10056 static inline void handle_int32(uchar *to, const uchar *from, size_t max_length,
10057                                 bool low_byte_first_from,
10058                                 bool low_byte_first_to) {
10059   int32 val;
10060   uchar buf[sizeof(val)];
10061   if (low_byte_first_from)
10062     val = sint4korr(from);
10063   else
10064     val = longget(from);
10065 
10066   if (low_byte_first_to)
10067     int4store(buf, val);
10068   else
10069     longstore(buf, val);
10070   if (max_length >= sizeof(buf)) {
10071     // Common case.
10072     memcpy(to, buf, sizeof(buf));
10073   } else {
10074     memcpy(to, buf, max_length);
10075   }
10076 }
10077 
10078 // Byteswaps and/or truncates int64 values; used for both pack() and unpack().
handle_int64(uchar * to,const uchar * from,size_t max_length,bool low_byte_first_from MY_ATTRIBUTE ((unused)),bool low_byte_first_to MY_ATTRIBUTE ((unused)))10079 static inline void handle_int64(uchar *to, const uchar *from, size_t max_length,
10080                                 bool low_byte_first_from MY_ATTRIBUTE((unused)),
10081                                 bool low_byte_first_to MY_ATTRIBUTE((unused))) {
10082   int64 val;
10083   uchar buf[sizeof(val)];
10084 #ifdef WORDS_BIGENDIAN
10085   if (low_byte_first_from)
10086     val = sint8korr(from);
10087   else
10088 #endif
10089     memcpy(&val, from, sizeof(val));
10090 
10091 #ifdef WORDS_BIGENDIAN
10092   if (low_byte_first_to)
10093     int8store(buf, val);
10094   else
10095 #endif
10096     longlongstore(buf, val);
10097   if (max_length >= sizeof(buf)) {
10098     // Common case.
10099     memcpy(to, buf, sizeof(buf));
10100   } else {
10101     memcpy(to, buf, max_length);
10102   }
10103 }
10104 
pack_int16(uchar * to,const uchar * from,size_t max_length) const10105 uchar *Field::pack_int16(uchar *to, const uchar *from,
10106                          size_t max_length) const {
10107   handle_int16(to, from, max_length, table->s->db_low_byte_first, true);
10108   return to + sizeof(int16);
10109 }
10110 
unpack_int16(uchar * to,const uchar * from) const10111 const uchar *Field::unpack_int16(uchar *to, const uchar *from) const {
10112   handle_int16(to, from, UINT_MAX, true, table->s->db_low_byte_first);
10113   return from + sizeof(int16);
10114 }
10115 
pack_int24(uchar * to,const uchar * from,size_t max_length) const10116 uchar *Field::pack_int24(uchar *to, const uchar *from,
10117                          size_t max_length) const {
10118   handle_int24(to, from, max_length, table->s->db_low_byte_first, true);
10119   return to + 3;
10120 }
10121 
unpack_int24(uchar * to,const uchar * from) const10122 const uchar *Field::unpack_int24(uchar *to, const uchar *from) const {
10123   handle_int24(to, from, UINT_MAX, true, table->s->db_low_byte_first);
10124   return from + 3;
10125 }
10126 
pack_int32(uchar * to,const uchar * from,size_t max_length) const10127 uchar *Field::pack_int32(uchar *to, const uchar *from,
10128                          size_t max_length) const {
10129   handle_int32(to, from, max_length, table->s->db_low_byte_first, true);
10130   return to + sizeof(int32);
10131 }
10132 
unpack_int32(uchar * to,const uchar * from) const10133 const uchar *Field::unpack_int32(uchar *to, const uchar *from) const {
10134   handle_int32(to, from, UINT_MAX, true, table->s->db_low_byte_first);
10135   return from + sizeof(int32);
10136 }
10137 
pack_int64(uchar * to,const uchar * from,size_t max_length) const10138 uchar *Field::pack_int64(uchar *to, const uchar *from,
10139                          size_t max_length) const {
10140   handle_int64(to, from, max_length, table->s->db_low_byte_first, true);
10141   return to + sizeof(int64);
10142 }
10143 
unpack_int64(uchar * to,const uchar * from) const10144 const uchar *Field::unpack_int64(uchar *to, const uchar *from) const {
10145   handle_int64(to, from, UINT_MAX, true, table->s->db_low_byte_first);
10146   return from + sizeof(int64);
10147 }
10148 
is_updatable() const10149 bool Field_longstr::is_updatable() const {
10150   DBUG_ASSERT(table && table->write_set);
10151   return bitmap_is_set(table->write_set, field_index());
10152 }
10153 
Field_varstring(uchar * ptr_arg,uint32 len_arg,uint length_bytes_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,TABLE_SHARE * share,const CHARSET_INFO * cs)10154 Field_varstring::Field_varstring(uchar *ptr_arg, uint32 len_arg,
10155                                  uint length_bytes_arg, uchar *null_ptr_arg,
10156                                  uchar null_bit_arg, uchar auto_flags_arg,
10157                                  const char *field_name_arg, TABLE_SHARE *share,
10158                                  const CHARSET_INFO *cs)
10159     : Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
10160                     auto_flags_arg, field_name_arg, cs),
10161       length_bytes(length_bytes_arg) {
10162   if (share != nullptr) {
10163     share->varchar_fields++;
10164   }
10165 }
10166 
Field_varstring(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,TABLE_SHARE * share,const CHARSET_INFO * cs)10167 Field_varstring::Field_varstring(uint32 len_arg, bool is_nullable_arg,
10168                                  const char *field_name_arg, TABLE_SHARE *share,
10169                                  const CHARSET_INFO *cs)
10170     : Field_longstr(nullptr, len_arg,
10171                     is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
10172                     field_name_arg, cs),
10173       length_bytes(len_arg < 256 ? 1 : 2) {
10174   if (share != nullptr) {
10175     share->varchar_fields++;
10176   }
10177 }
10178 
store_length(uchar * i_ptr,uint i_packlength,uint32 i_number)10179 void Field_blob::store_length(uchar *i_ptr, uint i_packlength,
10180                               uint32 i_number) {
10181   store_blob_length(i_ptr, i_packlength, i_number);
10182 }
10183 
get_length(ptrdiff_t row_offset) const10184 uint32 Field_blob::get_length(ptrdiff_t row_offset) const {
10185   return get_length(ptr + row_offset, this->packlength);
10186 }
10187 
get_length(const uchar * ptr_arg) const10188 uint32 Field_blob::get_length(const uchar *ptr_arg) const {
10189   return get_length(ptr_arg, this->packlength);
10190 }
10191 
backup_blob_field()10192 bool Field_blob::backup_blob_field() {
10193   value.swap(m_blob_backup);
10194 #ifndef DBUG_OFF
10195   m_uses_backup = true;
10196 #endif
10197   return false;
10198 }
10199 
restore_blob_backup()10200 void Field_blob::restore_blob_backup() {
10201   DBUG_ASSERT(m_uses_backup);
10202   value.swap(m_blob_backup);
10203 #ifndef DBUG_OFF
10204   m_uses_backup = false;
10205 #endif
10206 }
10207 
Create_field_wrapper(const Create_field * fld)10208 Create_field_wrapper::Create_field_wrapper(const Create_field *fld)
10209     : Field(nullptr, fld->max_display_width_in_codepoints(), nullptr, 0,
10210             fld->auto_flags, fld->field_name),
10211       m_field(fld) {
10212   if (fld->is_unsigned) {
10213     set_flag(UNSIGNED_FLAG);
10214   }
10215 }
10216 
result_type() const10217 Item_result Create_field_wrapper::result_type() const {
10218   return field_types_result_type[field_type2index(m_field->sql_type)];
10219 }
10220 
numeric_context_result_type() const10221 Item_result Create_field_wrapper::numeric_context_result_type() const {
10222   return ::numeric_context_result_type(type(), result_type(),
10223                                        m_field->decimals);
10224 }
10225 
type() const10226 enum_field_types Create_field_wrapper::type() const {
10227   return m_field->sql_type;
10228 }
10229 
charset() const10230 const CHARSET_INFO *Create_field_wrapper::charset() const {
10231   return m_field->charset;
10232 }
10233 
pack_length() const10234 uint32 Create_field_wrapper::pack_length() const {
10235   return m_field->pack_length();
10236 }
10237 
max_display_length() const10238 uint32 Create_field_wrapper::max_display_length() const {
10239   return m_field->max_display_width_in_codepoints();
10240 }
10241 
10242 /**
10243   Generate a Create_field from an Item.
10244 
10245   This function generates a Create_field from an Item by first creating a
10246   temporary table Field from the Item, and then creating the Create_field from
10247   this Field (there is currently no way to go directly from Item to
10248   Create_field). It is used several places:
10249   - In CREATE TABLE AS SELECT for creating the target table definition.
10250   - In functional indexes for creating the hidden generated column from the
10251     indexed expression.
10252 
10253   @param thd       Thread handler
10254   @param item      The item to generate a Create_field from
10255   @param tmp_table A table object which is used to generate a temporary table
10256                    field, as described above. This doesn't need to be an
10257                    existing table.
10258   @return          A Create_field generated from the input item, or nullptr
10259                    in case of errors.
10260 */
generate_create_field(THD * thd,Item * item,TABLE * tmp_table)10261 Create_field *generate_create_field(THD *thd, Item *item, TABLE *tmp_table) {
10262   Field *tmp_table_field;
10263   if (item->type() == Item::FUNC_ITEM) {
10264     /*
10265       If the function returns an array, use the method provided by the function
10266       to create the tmp table field, as the generic
10267       tmp_table_field_from_field_type() can't handle typed arrays.
10268     */
10269     if (item->result_type() != STRING_RESULT || item->returns_array())
10270       tmp_table_field = item->tmp_table_field(tmp_table);
10271     else
10272       tmp_table_field = item->tmp_table_field_from_field_type(tmp_table, false);
10273   } else {
10274     Field *from_field, *default_field;
10275     tmp_table_field = create_tmp_field(thd, tmp_table, item, item->type(),
10276                                        nullptr, &from_field, &default_field,
10277                                        false, false, false, false, false);
10278   }
10279 
10280   if (!tmp_table_field) {
10281     return nullptr; /* purecov: inspected */
10282   }
10283 
10284   Field *table_field;
10285 
10286   switch (item->type()) {
10287     case Item::FIELD_ITEM:
10288     case Item::TRIGGER_FIELD_ITEM: {
10289       /*
10290         We have to take into account both the real table's fields
10291         and pseudo-fields used in trigger's body. These fields are used to copy
10292         defaults values later inside constructor of the class Create_field.
10293        */
10294       table_field = ((Item_field *)item)->field;
10295       break;
10296     }
10297     default: {
10298       /*
10299         If the expression is of temporal type having date and non-nullable,
10300         a zero date is generated. If in strict mode, then zero date is
10301         invalid. For such cases no default is generated.
10302        */
10303       table_field = nullptr;
10304       if (is_temporal_type_with_date(tmp_table_field->type()) &&
10305           thd->is_strict_mode() && !item->maybe_null)
10306         tmp_table_field->set_flag(NO_DEFAULT_VALUE_FLAG);
10307     }
10308   }
10309 
10310   DBUG_ASSERT(tmp_table_field->gcol_info == nullptr &&
10311               tmp_table_field->stored_in_db);
10312   Create_field *cr_field =
10313       new (thd->mem_root) Create_field(tmp_table_field, table_field);
10314 
10315   if (!cr_field) {
10316     return nullptr; /* purecov: inspected */
10317   }
10318 
10319   // Mark if collation was specified explicitly by user for the column.
10320   if (item->type() == Item::FIELD_ITEM) {
10321     const TABLE *table = table_field->orig_table;
10322     DBUG_ASSERT(table);
10323     const dd::Table *table_obj =
10324         table->s->tmp_table ? table->s->tmp_table_def : nullptr;
10325 
10326     if (!table_obj && table->s->table_category != TABLE_UNKNOWN_CATEGORY) {
10327       dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
10328 
10329       if (thd->dd_client()->acquire(table->s->db.str, table->s->table_name.str,
10330                                     &table_obj)) {
10331         return nullptr; /* purecov: inspected */
10332       }
10333     }
10334 
10335     cr_field->is_explicit_collation = false;
10336     if (table_obj) {
10337       const dd::Column *c = table_obj->get_column(table_field->field_name);
10338       if (c) cr_field->is_explicit_collation = c->is_explicit_collation();
10339     }
10340   }
10341 
10342   if (item->maybe_null) cr_field->flags &= ~NOT_NULL_FLAG;
10343 
10344   return cr_field;
10345 }
10346 
get_field_name_or_expression(THD * thd,const Field * field)10347 const char *get_field_name_or_expression(THD *thd, const Field *field) {
10348   if (field->is_field_for_functional_index()) {
10349     String expression_buffer;
10350     field->gcol_info->print_expr(thd, &expression_buffer);
10351     return thd->strmake(expression_buffer.ptr(), expression_buffer.length());
10352   }
10353 
10354   return field->field_name;
10355 }
10356