1 /*
2 Copyright (c) 2000, 2017, Oracle and/or its affiliates.
3 Copyright (c) 2008, 2021, MariaDB
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
17 */
18
19 /**
20 @file
21
22 @brief
23 This file implements classes defined in field.h
24 */
25
26 #ifdef USE_PRAGMA_IMPLEMENTATION
27 #pragma implementation // gcc: Class implementation
28 #endif
29
30 #include "mariadb.h"
31 #include "sql_priv.h"
32 #include "sql_select.h"
33 #include "rpl_rli.h" // Pull in Relay_log_info
34 #include "slave.h" // Pull in rpl_master_has_bug()
35 #include "strfunc.h" // find_type2, find_set
36 #include "sql_time.h" // str_to_datetime_with_warn,
37 // str_to_time_with_warn,
38 // TIME_to_timestamp,
39 // make_time, make_date,
40 // make_truncated_value_warning
41 #include "tztime.h" // struct Time_zone
42 #include "filesort.h" // change_double_for_sort
43 #include "log_event.h" // class Table_map_log_event
44 #include <m_ctype.h>
45
46 // Maximum allowed exponent value for converting string to decimal
47 #define MAX_EXPONENT 1024
48
49 /*****************************************************************************
50 Instantiate templates and static variables
51 *****************************************************************************/
52
53 static const char *zero_timestamp="0000-00-00 00:00:00.000000";
54 LEX_CSTRING temp_lex_str= {STRING_WITH_LEN("temp")};
55
56 uchar Field_null::null[1]={1};
57 const char field_separator=',';
58
59 #define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
60 #define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
61 #define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
62 #define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
63 ((ulong) ((1LL << MY_MIN(arg, 4) * 8) - 1))
64
65 // Column marked for read or the field set to read out of record[0]
66 #define ASSERT_COLUMN_MARKED_FOR_READ \
67 DBUG_ASSERT(!table || \
68 (!table->read_set || \
69 bitmap_is_set(table->read_set, field_index) || \
70 (!(ptr >= table->record[0] && \
71 ptr < table->record[0] + table->s->reclength))))
72
73 #define ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED \
74 DBUG_ASSERT(!table || \
75 (!table->write_set || \
76 bitmap_is_set(table->write_set, field_index) || \
77 (!(ptr >= table->record[0] && \
78 ptr < table->record[0] + table->s->reclength))) || \
79 (table->vcol_set && bitmap_is_set(table->vcol_set, field_index)))
80
81 #define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
82
83 /*
84 Rules for merging different types of fields in UNION
85
86 NOTE: to avoid 256*256 table, gap in table types numeration is skipped
87 following #defines describe that gap and how to canculate number of fields
88 and index of field in this array.
89 */
90 const int FIELDTYPE_TEAR_FROM= (MYSQL_TYPE_BIT + 1);
91 const int FIELDTYPE_TEAR_TO= (MYSQL_TYPE_NEWDECIMAL - 1);
92 const int FIELDTYPE_LAST= 254;
93 const int FIELDTYPE_NUM= FIELDTYPE_TEAR_FROM + (FIELDTYPE_LAST -
94 FIELDTYPE_TEAR_TO);
95
field_type2index(enum_field_types field_type)96 static inline int field_type2index (enum_field_types field_type)
97 {
98 DBUG_ASSERT(real_type_to_type(field_type) < FIELDTYPE_TEAR_FROM ||
99 real_type_to_type(field_type) > FIELDTYPE_TEAR_TO);
100 DBUG_ASSERT(field_type <= FIELDTYPE_LAST);
101 field_type= real_type_to_type(field_type);
102 if (field_type < FIELDTYPE_TEAR_FROM)
103 return field_type;
104 return FIELDTYPE_TEAR_FROM + (field_type - FIELDTYPE_TEAR_TO) - 1;
105 }
106
107
108 /**
109 Implements data type merge rules for the built-in traditional data types.
110 Used for operations such as:
111 - UNION
112 - CASE and its abbreviations COALESCE, IF, IFNULL
113 - LEAST/GREATEST
114
115 Given Fields A and B of real_types a and b, we find the result type of
116 COALESCE(A, B) by querying:
117 field_types_merge_rules[field_type_to_index(a)][field_type_to_index(b)].
118 */
119 static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
120 {
121 /* MYSQL_TYPE_DECIMAL -> */
122 {
123 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
124 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
125 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
126 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
127 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
128 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
129 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
130 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
131 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
132 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
133 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
134 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
135 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
136 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
137 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
138 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
139 //MYSQL_TYPE_BIT <16>-<245>
140 MYSQL_TYPE_VARCHAR,
141 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
142 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
143 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
144 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
145 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
146 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
147 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
148 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
149 //MYSQL_TYPE_STRING
150 MYSQL_TYPE_STRING
151 },
152 /* MYSQL_TYPE_TINY -> */
153 {
154 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
155 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
156 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
157 MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
158 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
159 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
160 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
161 MYSQL_TYPE_TINY, MYSQL_TYPE_VARCHAR,
162 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
163 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
164 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
165 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
166 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
167 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY,
168 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
169 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
170 //MYSQL_TYPE_BIT <16>-<245>
171 MYSQL_TYPE_VARCHAR,
172 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
173 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
174 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
175 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
176 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
177 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
178 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
179 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
180 //MYSQL_TYPE_STRING
181 MYSQL_TYPE_STRING
182 },
183 /* MYSQL_TYPE_SHORT -> */
184 {
185 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
186 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_SHORT,
187 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
188 MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
189 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
190 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
191 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
192 MYSQL_TYPE_SHORT, MYSQL_TYPE_VARCHAR,
193 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
194 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
195 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
196 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
197 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
198 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_SHORT,
199 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
200 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
201 //MYSQL_TYPE_BIT <16>-<245>
202 MYSQL_TYPE_VARCHAR,
203 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
204 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
205 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
206 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
207 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
208 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
209 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
210 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
211 //MYSQL_TYPE_STRING
212 MYSQL_TYPE_STRING
213 },
214 /* MYSQL_TYPE_LONG -> */
215 {
216 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
217 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONG,
218 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
219 MYSQL_TYPE_LONG, MYSQL_TYPE_LONG,
220 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
221 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
222 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
223 MYSQL_TYPE_LONG, MYSQL_TYPE_VARCHAR,
224 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
225 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG,
226 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
227 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
228 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
229 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONG,
230 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
231 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
232 //MYSQL_TYPE_BIT <16>-<245>
233 MYSQL_TYPE_VARCHAR,
234 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
235 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
236 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
237 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
238 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
239 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
240 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
241 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
242 //MYSQL_TYPE_STRING
243 MYSQL_TYPE_STRING
244 },
245 /* MYSQL_TYPE_FLOAT -> */
246 {
247 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
248 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_FLOAT,
249 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
250 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
251 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
252 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
253 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
254 MYSQL_TYPE_FLOAT, MYSQL_TYPE_VARCHAR,
255 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
256 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_FLOAT,
257 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
258 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
259 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
260 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_FLOAT,
261 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
262 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
263 //MYSQL_TYPE_BIT <16>-<245>
264 MYSQL_TYPE_VARCHAR,
265 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
266 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
267 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
268 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
269 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
270 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
271 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
272 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
273 //MYSQL_TYPE_STRING
274 MYSQL_TYPE_STRING
275 },
276 /* MYSQL_TYPE_DOUBLE -> */
277 {
278 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
279 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
280 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
281 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
282 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
283 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
284 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
285 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
286 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
287 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
288 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
289 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
290 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
291 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_DOUBLE,
292 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
293 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
294 //MYSQL_TYPE_BIT <16>-<245>
295 MYSQL_TYPE_VARCHAR,
296 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
297 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
298 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
299 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
300 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
301 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
302 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
303 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
304 //MYSQL_TYPE_STRING
305 MYSQL_TYPE_STRING
306 },
307 /* MYSQL_TYPE_NULL -> */
308 {
309 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
310 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
311 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
312 MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
313 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
314 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
315 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
316 MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP,
317 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
318 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
319 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
320 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_TIME,
321 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
322 MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR,
323 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
324 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
325 //MYSQL_TYPE_BIT <16>-<245>
326 MYSQL_TYPE_BIT,
327 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
328 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_ENUM,
329 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
330 MYSQL_TYPE_SET, MYSQL_TYPE_TINY_BLOB,
331 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
332 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
333 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
334 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
335 //MYSQL_TYPE_STRING
336 MYSQL_TYPE_STRING
337 },
338 /* MYSQL_TYPE_TIMESTAMP -> */
339 {
340 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
341 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
342 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
343 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
344 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
345 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
346 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
347 MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_TIMESTAMP,
348 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
349 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
350 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
351 MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
352 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
353 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
354 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
355 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
356 //MYSQL_TYPE_BIT <16>-<245>
357 MYSQL_TYPE_VARCHAR,
358 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
359 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
360 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
361 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
362 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
363 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
364 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
365 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
366 //MYSQL_TYPE_STRING
367 MYSQL_TYPE_STRING
368 },
369 /* MYSQL_TYPE_LONGLONG -> */
370 {
371 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
372 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONGLONG,
373 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
374 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
375 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
376 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
377 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
378 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_VARCHAR,
379 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
380 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
381 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
382 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
383 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
384 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONGLONG,
385 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
386 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
387 //MYSQL_TYPE_BIT <16>-<245>
388 MYSQL_TYPE_VARCHAR,
389 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
390 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
391 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
392 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
393 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
394 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
395 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
396 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
397 //MYSQL_TYPE_STRING
398 MYSQL_TYPE_STRING
399 },
400 /* MYSQL_TYPE_INT24 -> */
401 {
402 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
403 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_INT24,
404 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
405 MYSQL_TYPE_INT24, MYSQL_TYPE_LONG,
406 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
407 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
408 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
409 MYSQL_TYPE_INT24, MYSQL_TYPE_VARCHAR,
410 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
411 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
412 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
413 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
414 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
415 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_INT24,
416 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
417 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
418 //MYSQL_TYPE_BIT <16>-<245>
419 MYSQL_TYPE_VARCHAR,
420 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
421 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
422 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
423 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
424 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
425 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
426 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
427 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
428 //MYSQL_TYPE_STRING
429 MYSQL_TYPE_STRING
430 },
431 /* MYSQL_TYPE_DATE -> */
432 {
433 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
434 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
435 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
436 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
437 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
438 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
439 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
440 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
441 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
442 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
443 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
444 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
445 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
446 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
447 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
448 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
449 //MYSQL_TYPE_BIT <16>-<245>
450 MYSQL_TYPE_VARCHAR,
451 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
452 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
453 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
454 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
455 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
456 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
457 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
458 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
459 //MYSQL_TYPE_STRING
460 MYSQL_TYPE_STRING
461 },
462 /* MYSQL_TYPE_TIME -> */
463 {
464 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
465 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
466 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
467 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
468 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
469 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
470 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
471 MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME,
472 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
473 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
474 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
475 MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIME,
476 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
477 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
478 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
479 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
480 //MYSQL_TYPE_BIT <16>-<245>
481 MYSQL_TYPE_VARCHAR,
482 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
483 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
484 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
485 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
486 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
487 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
488 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
489 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
490 //MYSQL_TYPE_STRING
491 MYSQL_TYPE_STRING
492 },
493 /* MYSQL_TYPE_DATETIME -> */
494 {
495 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
496 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
497 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
498 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
499 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
500 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
501 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
502 MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
503 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
504 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
505 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
506 MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
507 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
508 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
509 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
510 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
511 //MYSQL_TYPE_BIT <16>-<245>
512 MYSQL_TYPE_VARCHAR,
513 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
514 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
515 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
516 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
517 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
518 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
519 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
520 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
521 //MYSQL_TYPE_STRING
522 MYSQL_TYPE_STRING
523 },
524 /* MYSQL_TYPE_YEAR -> */
525 {
526 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
527 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
528 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
529 MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
530 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
531 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
532 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
533 MYSQL_TYPE_YEAR, MYSQL_TYPE_VARCHAR,
534 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
535 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
536 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
537 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
538 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
539 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_YEAR,
540 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
541 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
542 //MYSQL_TYPE_BIT <16>-<245>
543 MYSQL_TYPE_VARCHAR,
544 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
545 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
546 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
547 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
548 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
549 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
550 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
551 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
552 //MYSQL_TYPE_STRING
553 MYSQL_TYPE_STRING
554 },
555 /* MYSQL_TYPE_NEWDATE -> */
556 {
557 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
558 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
559 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
560 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
561 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
562 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
563 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
564 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
565 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
566 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
567 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
568 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
569 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
570 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
571 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
572 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
573 //MYSQL_TYPE_BIT <16>-<245>
574 MYSQL_TYPE_VARCHAR,
575 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
576 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
577 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
578 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
579 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
580 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
581 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
582 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
583 //MYSQL_TYPE_STRING
584 MYSQL_TYPE_STRING
585 },
586 /* MYSQL_TYPE_VARCHAR -> */
587 {
588 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
589 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
590 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
591 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
592 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
593 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
594 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
595 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
596 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
597 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
598 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
599 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
600 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
601 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
602 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
603 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
604 //MYSQL_TYPE_BIT <16>-<245>
605 MYSQL_TYPE_VARCHAR,
606 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
607 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
608 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
609 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
610 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
611 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
612 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
613 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
614 //MYSQL_TYPE_STRING
615 MYSQL_TYPE_VARCHAR
616 },
617 /* MYSQL_TYPE_BIT -> */
618 {
619 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
620 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
621 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
622 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
623 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
624 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
625 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
626 MYSQL_TYPE_BIT, MYSQL_TYPE_VARCHAR,
627 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
628 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
629 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
630 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
631 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
632 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
633 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
634 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
635 //MYSQL_TYPE_BIT <16>-<245>
636 MYSQL_TYPE_BIT,
637 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
638 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
639 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
640 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
641 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
642 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
643 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
644 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
645 //MYSQL_TYPE_STRING
646 MYSQL_TYPE_STRING
647 },
648 /* MYSQL_TYPE_NEWDECIMAL -> */
649 {
650 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
651 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
652 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
653 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
654 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
655 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
656 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
657 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
658 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
659 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
660 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
661 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
662 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
663 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_NEWDECIMAL,
664 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
665 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
666 //MYSQL_TYPE_BIT <16>-<245>
667 MYSQL_TYPE_VARCHAR,
668 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
669 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
670 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
671 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
672 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
673 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
674 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
675 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
676 //MYSQL_TYPE_STRING
677 MYSQL_TYPE_STRING
678 },
679 /* MYSQL_TYPE_ENUM -> */
680 {
681 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
682 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
683 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
684 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
685 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
686 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
687 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
688 MYSQL_TYPE_ENUM, MYSQL_TYPE_VARCHAR,
689 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
690 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
691 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
692 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
693 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
694 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
695 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
696 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
697 //MYSQL_TYPE_BIT <16>-<245>
698 MYSQL_TYPE_VARCHAR,
699 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
700 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
701 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
702 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
703 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
704 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
705 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
706 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
707 //MYSQL_TYPE_STRING
708 MYSQL_TYPE_STRING
709 },
710 /* MYSQL_TYPE_SET -> */
711 {
712 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
713 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
714 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
715 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
716 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
717 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
718 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
719 MYSQL_TYPE_SET, MYSQL_TYPE_VARCHAR,
720 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
721 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
722 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
723 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
724 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
725 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
726 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
727 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
728 //MYSQL_TYPE_BIT <16>-<245>
729 MYSQL_TYPE_VARCHAR,
730 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
731 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
732 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
733 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
734 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
735 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
736 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
737 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
738 //MYSQL_TYPE_STRING
739 MYSQL_TYPE_STRING
740 },
741 /* MYSQL_TYPE_TINY_BLOB -> */
742 {
743 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
744 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
745 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
746 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
747 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
748 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
749 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
750 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
751 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
752 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
753 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
754 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
755 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
756 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
757 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
758 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
759 //MYSQL_TYPE_BIT <16>-<245>
760 MYSQL_TYPE_TINY_BLOB,
761 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
762 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
763 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
764 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
765 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
766 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
767 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
768 MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB,
769 //MYSQL_TYPE_STRING
770 MYSQL_TYPE_TINY_BLOB
771 },
772 /* MYSQL_TYPE_MEDIUM_BLOB -> */
773 {
774 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
775 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
776 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
777 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
778 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
779 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
780 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
781 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
782 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
783 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
784 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
785 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
786 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
787 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
788 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
789 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
790 //MYSQL_TYPE_BIT <16>-<245>
791 MYSQL_TYPE_MEDIUM_BLOB,
792 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
793 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
794 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
795 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
796 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
797 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
798 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
799 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
800 //MYSQL_TYPE_STRING
801 MYSQL_TYPE_MEDIUM_BLOB
802 },
803 /* MYSQL_TYPE_LONG_BLOB -> */
804 {
805 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
806 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
807 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
808 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
809 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
810 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
811 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
812 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
813 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
814 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
815 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
816 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
817 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
818 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
819 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
820 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
821 //MYSQL_TYPE_BIT <16>-<245>
822 MYSQL_TYPE_LONG_BLOB,
823 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
824 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
825 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
826 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
827 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
828 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
829 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
830 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
831 //MYSQL_TYPE_STRING
832 MYSQL_TYPE_LONG_BLOB
833 },
834 /* MYSQL_TYPE_BLOB -> */
835 {
836 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
837 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
838 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
839 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
840 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
841 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
842 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
843 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
844 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
845 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
846 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
847 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
848 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
849 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
850 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
851 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
852 //MYSQL_TYPE_BIT <16>-<245>
853 MYSQL_TYPE_BLOB,
854 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
855 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
856 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
857 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
858 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
859 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
860 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
861 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
862 //MYSQL_TYPE_STRING
863 MYSQL_TYPE_BLOB
864 },
865 /* MYSQL_TYPE_VAR_STRING -> */
866 {
867 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
868 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
869 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
870 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
871 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
872 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
873 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
874 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
875 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
876 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
877 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
878 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
879 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
880 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
881 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
882 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
883 //MYSQL_TYPE_BIT <16>-<245>
884 MYSQL_TYPE_VARCHAR,
885 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
886 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
887 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
888 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
889 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
890 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
891 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
892 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
893 //MYSQL_TYPE_STRING
894 MYSQL_TYPE_VARCHAR
895 },
896 /* MYSQL_TYPE_STRING -> */
897 {
898 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
899 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
900 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
901 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
902 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
903 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
904 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
905 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
906 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
907 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
908 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
909 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
910 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
911 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
912 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
913 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR,
914 //MYSQL_TYPE_BIT <16>-<245>
915 MYSQL_TYPE_STRING,
916 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
917 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
918 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
919 MYSQL_TYPE_STRING, MYSQL_TYPE_TINY_BLOB,
920 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
921 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
922 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
923 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
924 //MYSQL_TYPE_STRING
925 MYSQL_TYPE_STRING
926 }
927 };
928
929 /**
930 Return type of which can carry value of both given types in UNION result.
931
932 @param a type for merging
933 @param b type for merging
934
935 @return
936 type of field
937 */
938
field_type_merge(enum_field_types a,enum_field_types b)939 enum_field_types Field::field_type_merge(enum_field_types a,
940 enum_field_types b)
941 {
942 return field_types_merge_rules[field_type2index(a)]
943 [field_type2index(b)];
944 }
945
946 const Type_handler *
aggregate_for_result_traditional(const Type_handler * a,const Type_handler * b)947 Type_handler::aggregate_for_result_traditional(const Type_handler *a,
948 const Type_handler *b)
949 {
950 enum_field_types ta= a->real_field_type();
951 enum_field_types tb= b->real_field_type();
952 return
953 Type_handler::get_handler_by_real_type(Field::field_type_merge(ta, tb));
954 }
955
956
957 /*
958 Test if the given string contains important data:
959 not spaces for character string,
960 or any data for binary string.
961
962 SYNOPSIS
963 test_if_important_data()
964 cs Character set
965 str String to test
966 strend String end
967
968 RETURN
969 FALSE - If string does not have important data
970 TRUE - If string has some important data
971 */
972
973 static bool
test_if_important_data(CHARSET_INFO * cs,const char * str,const char * strend)974 test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
975 {
976 if (cs != &my_charset_bin)
977 str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
978 return (str < strend);
979 }
980
981
982 /**
983 Function to compare two unsigned integers for their relative order.
984 Used below. In an anonymous namespace to not clash with definitions
985 in other files.
986 */
987
988 CPP_UNNAMED_NS_START
989
compare(unsigned int a,unsigned int b)990 int compare(unsigned int a, unsigned int b)
991 {
992 if (a < b)
993 return -1;
994 if (b < a)
995 return 1;
996 return 0;
997 }
998
999 CPP_UNNAMED_NS_END
1000
1001
1002 /*****************************************************************************
1003 Static help functions
1004 *****************************************************************************/
1005
1006
make_sort_key(uchar * buff,uint length)1007 void Field::make_sort_key(uchar *buff,uint length)
1008 {
1009 if (maybe_null())
1010 {
1011 if (is_null())
1012 {
1013 bzero(buff, length + 1);
1014 return;
1015 }
1016 *buff++= 1;
1017 }
1018 sort_string(buff, length);
1019 }
1020
1021
1022 /**
1023 @brief
1024 Determine the relative position of the field value in a numeric interval
1025
1026 @details
1027 The function returns a double number between 0.0 and 1.0 as the relative
1028 position of the value of the this field in the numeric interval of [min,max].
1029 If the value is not in the interval the the function returns 0.0 when
1030 the value is less than min, and, 1.0 when the value is greater than max.
1031
1032 @param min value of the left end of the interval
1033 @param max value of the right end of the interval
1034
1035 @return
1036 relative position of the field value in the numeric interval [min,max]
1037 */
1038
pos_in_interval_val_real(Field * min,Field * max)1039 double Field::pos_in_interval_val_real(Field *min, Field *max)
1040 {
1041 double n, d;
1042 n= val_real() - min->val_real();
1043 if (n < 0)
1044 return 0.0;
1045 d= max->val_real() - min->val_real();
1046 if (d <= 0)
1047 return 1.0;
1048 return MY_MIN(n/d, 1.0);
1049 }
1050
1051
1052 static
char_prefix_to_ulonglong(uchar * src)1053 inline ulonglong char_prefix_to_ulonglong(uchar *src)
1054 {
1055 uint sz= sizeof(ulonglong);
1056 for (uint i= 0; i < sz/2; i++)
1057 {
1058 uchar tmp= src[i];
1059 src[i]= src[sz-1-i];
1060 src[sz-1-i]= tmp;
1061 }
1062 return uint8korr(src);
1063 }
1064
1065 /*
1066 Compute res = a - b, without losing precision and taking care that these are
1067 unsigned numbers.
1068 */
safe_substract(ulonglong a,ulonglong b)1069 static inline double safe_substract(ulonglong a, ulonglong b)
1070 {
1071 return (a > b)? double(a - b) : -double(b - a);
1072 }
1073
1074
1075 /**
1076 @brief
1077 Determine the relative position of the field value in a string interval
1078
1079 @details
1080 The function returns a double number between 0.0 and 1.0 as the relative
1081 position of the value of the this field in the string interval of [min,max].
1082 If the value is not in the interval the the function returns 0.0 when
1083 the value is less than min, and, 1.0 when the value is greater than max.
1084
1085 @note
1086 To calculate the relative position of the string value v in the interval
1087 [min, max] the function first converts the beginning of these three
1088 strings v, min, max into the strings that are used for byte comparison.
1089 For each string not more sizeof(ulonglong) first bytes are taken
1090 from the result of conversion. Then these bytes are interpreted as the
1091 big-endian representation of an ulonglong integer. The values of these
1092 integer numbers obtained for the strings v, min, max are used to calculate
1093 the position of v in [min,max] in the same way is it's done for numeric
1094 fields (see Field::pos_in_interval_val_real).
1095
1096 @todo
1097 Improve the procedure for the case when min and max have the same
1098 beginning
1099
1100 @param min value of the left end of the interval
1101 @param max value of the right end of the interval
1102
1103 @return
1104 relative position of the field value in the string interval [min,max]
1105 */
1106
pos_in_interval_val_str(Field * min,Field * max,uint data_offset)1107 double Field::pos_in_interval_val_str(Field *min, Field *max, uint data_offset)
1108 {
1109 uchar mp_prefix[sizeof(ulonglong)];
1110 uchar minp_prefix[sizeof(ulonglong)];
1111 uchar maxp_prefix[sizeof(ulonglong)];
1112 ulonglong mp, minp, maxp;
1113 my_strnxfrm(charset(), mp_prefix, sizeof(mp),
1114 ptr + data_offset,
1115 data_length());
1116 my_strnxfrm(charset(), minp_prefix, sizeof(minp),
1117 min->ptr + data_offset,
1118 min->data_length());
1119 my_strnxfrm(charset(), maxp_prefix, sizeof(maxp),
1120 max->ptr + data_offset,
1121 max->data_length());
1122 mp= char_prefix_to_ulonglong(mp_prefix);
1123 minp= char_prefix_to_ulonglong(minp_prefix);
1124 maxp= char_prefix_to_ulonglong(maxp_prefix);
1125 double n, d;
1126 n= safe_substract(mp, minp);
1127 if (n < 0)
1128 return 0.0;
1129 d= safe_substract(maxp, minp);
1130 if (d <= 0)
1131 return 1.0;
1132 return MY_MIN(n/d, 1.0);
1133 }
1134
1135
test_if_equality_guarantees_uniqueness(const Item * item) const1136 bool Field::test_if_equality_guarantees_uniqueness(const Item *item) const
1137 {
1138 DBUG_ASSERT(cmp_type() != STRING_RESULT); // For STRING_RESULT see Field_str
1139 /*
1140 We use result_type() rather than cmp_type() in the below condition,
1141 because it covers a special case that string literals guarantee uniqueness
1142 for temporal columns, so the query:
1143 WHERE temporal_column='string'
1144 cannot return multiple distinct temporal values.
1145
1146 TODO: perhaps we could allow INT/DECIMAL/DOUBLE types for temporal items.
1147 */
1148 return result_type() == item->result_type();
1149 }
1150
1151
1152 /**
1153 Check whether a field item can be substituted for an equal item
1154
1155 @details
1156 The function checks whether a substitution of a field item for
1157 an equal item is valid.
1158
1159 @param arg *arg != NULL <-> the field is in the context
1160 where substitution for an equal item is valid
1161
1162 @note
1163 The following statement is not always true:
1164 @n
1165 x=y => F(x)=F(x/y).
1166 @n
1167 This means substitution of an item for an equal item not always
1168 yields an equavalent condition. Here's an example:
1169 @code
1170 'a'='a '
1171 (LENGTH('a')=1) != (LENGTH('a ')=2)
1172 @endcode
1173 Such a substitution is surely valid if either the substituted
1174 field is not of a STRING type or if it is an argument of
1175 a comparison predicate.
1176
1177 @retval
1178 TRUE substitution is valid
1179 @retval
1180 FALSE otherwise
1181 */
1182
can_be_substituted_to_equal_item(const Context & ctx,const Item_equal * item_equal)1183 bool Field::can_be_substituted_to_equal_item(const Context &ctx,
1184 const Item_equal *item_equal)
1185 {
1186 DBUG_ASSERT(item_equal->compare_type_handler()->cmp_type() != STRING_RESULT);
1187 DBUG_ASSERT(cmp_type() != STRING_RESULT);
1188 switch (ctx.subst_constraint()) {
1189 case ANY_SUBST:
1190 /*
1191 Disable const propagation for items used in different comparison contexts.
1192 This must be done because, for example, Item_hex_string->val_int() is not
1193 the same as (Item_hex_string->val_str() in BINARY column)->val_int().
1194 We cannot simply disable the replacement in a particular context (
1195 e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
1196 Items don't know the context they are in and there are functions like
1197 IF (<hex_string>, 'yes', 'no').
1198 */
1199 return ctx.compare_type_handler() == item_equal->compare_type_handler();
1200 case IDENTITY_SUBST:
1201 return true;
1202 }
1203 return false;
1204 }
1205
1206
1207 /*
1208 This handles all numeric and BIT data types.
1209 */
can_optimize_keypart_ref(const Item_bool_func * cond,const Item * item) const1210 bool Field::can_optimize_keypart_ref(const Item_bool_func *cond,
1211 const Item *item) const
1212 {
1213 DBUG_ASSERT(cmp_type() != STRING_RESULT);
1214 DBUG_ASSERT(cmp_type() != TIME_RESULT);
1215 return item->cmp_type() != TIME_RESULT;
1216 }
1217
1218
1219 /*
1220 This handles all numeric and BIT data types.
1221 */
can_optimize_group_min_max(const Item_bool_func * cond,const Item * const_item) const1222 bool Field::can_optimize_group_min_max(const Item_bool_func *cond,
1223 const Item *const_item) const
1224 {
1225 DBUG_ASSERT(cmp_type() != STRING_RESULT);
1226 DBUG_ASSERT(cmp_type() != TIME_RESULT);
1227 return const_item->cmp_type() != TIME_RESULT;
1228 }
1229
1230
1231 /*
1232 This covers all numeric types, BIT
1233 */
can_optimize_range(const Item_bool_func * cond,const Item * item,bool is_eq_func) const1234 bool Field::can_optimize_range(const Item_bool_func *cond,
1235 const Item *item,
1236 bool is_eq_func) const
1237 {
1238 DBUG_ASSERT(cmp_type() != TIME_RESULT); // Handled in Field_temporal
1239 DBUG_ASSERT(cmp_type() != STRING_RESULT); // Handled in Field_str descendants
1240 return item->cmp_type() != TIME_RESULT;
1241 }
1242
1243
store_hex_hybrid(const char * str,size_t length)1244 int Field::store_hex_hybrid(const char *str, size_t length)
1245 {
1246 DBUG_ASSERT(result_type() != STRING_RESULT);
1247 ulonglong nr;
1248
1249 if (length > 8)
1250 {
1251 nr= flags & UNSIGNED_FLAG ? ULONGLONG_MAX : LONGLONG_MAX;
1252 goto warn;
1253 }
1254 nr= (ulonglong) longlong_from_hex_hybrid(str, length);
1255 if ((length == 8) && !(flags & UNSIGNED_FLAG) && (nr > LONGLONG_MAX))
1256 {
1257 nr= LONGLONG_MAX;
1258 goto warn;
1259 }
1260 return store((longlong) nr, true); // Assume hex numbers are unsigned
1261
1262 warn:
1263 if (!store((longlong) nr, true))
1264 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1265 return 1;
1266 }
1267
1268
1269 /**
1270 If a field does not have a corresponding data, it's behavior can vary:
1271 - In case of the fixed file format
1272 it's set to the default value for the data type,
1273 such as 0 for numbers or '' for strings.
1274 - In case of a non-fixed format
1275 it's set to NULL for nullable fields, and
1276 it's set to the default value for the data type for NOT NULL fields.
1277 This seems to be by design.
1278 */
load_data_set_no_data(THD * thd,bool fixed_format)1279 bool Field::load_data_set_no_data(THD *thd, bool fixed_format)
1280 {
1281 reset(); // Do not use the DEFAULT value
1282 if (fixed_format)
1283 {
1284 set_notnull();
1285 /*
1286 We're loading a fixed format file, e.g.:
1287 LOAD DATA INFILE 't1.txt' INTO TABLE t1 FIELDS TERMINATED BY '';
1288 Suppose the file ended unexpectedly and no data was provided for an
1289 auto-increment column in the current row.
1290 Historically, if sql_mode=NO_AUTO_VALUE_ON_ZERO, then the column value
1291 is set to 0 in such case (the next auto_increment value is not used).
1292 This behaviour was introduced by the fix for "bug#12053" in mysql-4.1.
1293 Note, loading a delimited file works differently:
1294 "no data" is not converted to 0 on NO_AUTO_VALUE_ON_ZERO:
1295 it's considered as equal to setting the column to NULL,
1296 which is then replaced to the next auto_increment value.
1297 This difference seems to be intentional.
1298 */
1299 if (this == table->next_number_field)
1300 table->auto_increment_field_not_null= true;
1301 }
1302 set_has_explicit_value(); // Do not auto-update this field
1303 return false;
1304 }
1305
1306
load_data_set_null(THD * thd)1307 bool Field::load_data_set_null(THD *thd)
1308 {
1309 reset();
1310 set_null();
1311 if (!maybe_null())
1312 {
1313 if (this != table->next_number_field)
1314 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_NULL_TO_NOTNULL, 1);
1315 }
1316 set_has_explicit_value(); // Do not auto-update this field
1317 return false;
1318 }
1319
1320
load_data_set_value(const char * pos,uint length,CHARSET_INFO * cs)1321 void Field::load_data_set_value(const char *pos, uint length,
1322 CHARSET_INFO *cs)
1323 {
1324 /*
1325 Mark field as not null, we should do this for each row because of
1326 restore_record...
1327 */
1328 set_notnull();
1329 if (this == table->next_number_field)
1330 table->auto_increment_field_not_null= true;
1331 store(pos, length, cs);
1332 set_has_explicit_value(); // Do not auto-update this field
1333 }
1334
1335
sp_prepare_and_store_item(THD * thd,Item ** value)1336 bool Field::sp_prepare_and_store_item(THD *thd, Item **value)
1337 {
1338 DBUG_ENTER("Field::sp_prepare_and_store_item");
1339 DBUG_ASSERT(value);
1340
1341 Item *expr_item;
1342
1343 if (!(expr_item= thd->sp_prepare_func_item(value, 1)))
1344 goto error;
1345
1346 /*
1347 expr_item is now fixed, it's safe to call cmp_type()
1348 */
1349 if (expr_item->cmp_type() == ROW_RESULT)
1350 {
1351 my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
1352 goto error;
1353 }
1354
1355 /* Save the value in the field. Convert the value if needed. */
1356
1357 expr_item->save_in_field(this, 0);
1358
1359 if (likely(!thd->is_error()))
1360 DBUG_RETURN(false);
1361
1362 error:
1363 /*
1364 In case of error during evaluation, leave the result field set to NULL.
1365 Sic: we can't do it in the beginning of the function because the
1366 result field might be needed for its own re-evaluation, e.g. case of
1367 set x = x + 1;
1368 */
1369 set_null();
1370 DBUG_ASSERT(thd->is_error());
1371 DBUG_RETURN(true);
1372 }
1373
1374
error_generated_column_function_is_not_allowed(THD * thd,bool error) const1375 void Field::error_generated_column_function_is_not_allowed(THD *thd,
1376 bool error) const
1377 {
1378 StringBuffer<64> tmp;
1379 vcol_info->expr->print(&tmp, (enum_query_type)
1380 (QT_TO_SYSTEM_CHARSET |
1381 QT_ITEM_IDENT_SKIP_DB_NAMES |
1382 QT_ITEM_IDENT_SKIP_TABLE_NAMES));
1383 my_error(ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED,
1384 MYF(error ? 0 : ME_JUST_WARNING),
1385 tmp.c_ptr_safe(), vcol_info->get_vcol_type_name(),
1386 const_cast<const char*>(field_name.str));
1387 }
1388
1389
1390 /*
1391 Check if an indexed or a persistent virtual column depends on sql_mode flags
1392 that it cannot handle.
1393 See sql_mode.h for details.
1394 */
check_vcol_sql_mode_dependency(THD * thd,vcol_init_mode mode) const1395 bool Field::check_vcol_sql_mode_dependency(THD *thd, vcol_init_mode mode) const
1396 {
1397 DBUG_ASSERT(vcol_info);
1398 if ((flags & PART_KEY_FLAG) != 0 || stored_in_db())
1399 {
1400 Sql_mode_dependency dep=
1401 vcol_info->expr->value_depends_on_sql_mode() &
1402 Sql_mode_dependency(~0, ~can_handle_sql_mode_dependency_on_store());
1403 if (dep)
1404 {
1405 bool error= (mode & VCOL_INIT_DEPENDENCY_FAILURE_IS_ERROR) != 0;
1406 error_generated_column_function_is_not_allowed(thd, error);
1407 dep.push_dependency_warnings(thd);
1408 return error;
1409 }
1410 }
1411 return false;
1412 }
1413
1414
1415 /**
1416 Numeric fields base class constructor.
1417 */
Field_num(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)1418 Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1419 uchar null_bit_arg, utype unireg_check_arg,
1420 const LEX_CSTRING *field_name_arg,
1421 uint8 dec_arg, bool zero_arg, bool unsigned_arg)
1422 :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1423 unireg_check_arg, field_name_arg),
1424 dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
1425 {
1426 if (zerofill)
1427 flags|=ZEROFILL_FLAG;
1428 if (unsigned_flag)
1429 flags|=UNSIGNED_FLAG;
1430 }
1431
1432
prepend_zeros(String * value) const1433 void Field_num::prepend_zeros(String *value) const
1434 {
1435 int diff;
1436 if ((diff= (int) (field_length - value->length())) > 0)
1437 {
1438 const bool error= value->realloc(field_length);
1439 if (likely(!error))
1440 {
1441 bmove_upp((uchar*) value->ptr()+field_length,
1442 (uchar*) value->ptr()+value->length(),
1443 value->length());
1444 bfill((uchar*) value->ptr(),diff,'0');
1445 value->length(field_length);
1446 }
1447 }
1448 }
1449
1450
can_handle_sql_mode_dependency_on_store() const1451 sql_mode_t Field_num::can_handle_sql_mode_dependency_on_store() const
1452 {
1453 return MODE_PAD_CHAR_TO_FULL_LENGTH;
1454 }
1455
1456
get_equal_zerofill_const_item(THD * thd,const Context & ctx,Item * const_item)1457 Item *Field_num::get_equal_zerofill_const_item(THD *thd, const Context &ctx,
1458 Item *const_item)
1459 {
1460 switch (ctx.subst_constraint()) {
1461 case IDENTITY_SUBST:
1462 return NULL; // Not safe to propagate if not in comparison. See MDEV-8369.
1463 case ANY_SUBST:
1464 break;
1465 }
1466 DBUG_ASSERT(const_item->const_item());
1467 DBUG_ASSERT(ctx.compare_type_handler()->cmp_type() != STRING_RESULT);
1468 return const_item;
1469 }
1470
1471
1472 /**
1473 Construct warning parameters using thd->no_errors
1474 to determine whether to generate or suppress warnings.
1475 We can get here in a query like this:
1476 SELECT COUNT(@@basedir);
1477 from Item_func_get_system_var::update_null_value().
1478 */
Warn_filter(const THD * thd)1479 Value_source::Warn_filter::Warn_filter(const THD *thd)
1480 :m_want_warning_edom(!thd->no_errors),
1481 m_want_note_truncated_spaces(!thd->no_errors)
1482 { }
1483
1484
1485 /**
1486 Check string-to-number conversion and produce a warning if
1487 - could not convert any digits (EDOM-alike error)
1488 - found garbage at the end of the string
1489 - found trailing spaces (a note)
1490 See also Field_num::check_edom_and_truncation() for a similar function.
1491
1492 @param thd - the thread
1493 @param filter - which warnings/notes are allowed
1494 @param type - name of the data type (e.g. "INTEGER", "DECIMAL", "DOUBLE")
1495 @param cs - character set of the original string
1496 @param str - the original string
1497 @param end - the end of the string
1498
1499 Unlike Field_num::check_edom_and_truncation(), this function does not
1500 distinguish between EDOM and truncation and reports the same warning for
1501 both cases. Perhaps we should eventually print different warnings, to make
1502 the explicit CAST work closer to the implicit cast in Field_xxx::store().
1503 */
1504 void
check_edom_and_truncation(THD * thd,Warn_filter filter,const char * type,CHARSET_INFO * cs,const char * str,size_t length) const1505 Value_source::Converter_string_to_number::check_edom_and_truncation(THD *thd,
1506 Warn_filter filter,
1507 const char *type,
1508 CHARSET_INFO *cs,
1509 const char *str,
1510 size_t length) const
1511 {
1512 DBUG_ASSERT(str <= m_end_of_num);
1513 DBUG_ASSERT(m_end_of_num <= str + length);
1514 if (m_edom || (m_end_of_num < str + length &&
1515 !check_if_only_end_space(cs, m_end_of_num, str + length)))
1516 {
1517 // EDOM or important trailing data truncation
1518 if (filter.want_warning_edom())
1519 {
1520 /*
1521 We can use err.ptr() here as ErrConvString is guaranteed to put an
1522 end \0 here.
1523 */
1524 THD *wthd= thd ? thd : current_thd;
1525 push_warning_printf(wthd, Sql_condition::WARN_LEVEL_WARN,
1526 ER_TRUNCATED_WRONG_VALUE,
1527 ER_THD(wthd, ER_TRUNCATED_WRONG_VALUE), type,
1528 ErrConvString(str, length, cs).ptr());
1529 }
1530 }
1531 else if (m_end_of_num < str + length)
1532 {
1533 // Unimportant trailing data (spaces) truncation
1534 if (filter.want_note_truncated_spaces())
1535 {
1536 THD *wthd= thd ? thd : current_thd;
1537 push_warning_printf(wthd, Sql_condition::WARN_LEVEL_NOTE,
1538 ER_TRUNCATED_WRONG_VALUE,
1539 ER_THD(wthd, ER_TRUNCATED_WRONG_VALUE), type,
1540 ErrConvString(str, length, cs).ptr());
1541 }
1542 }
1543 }
1544
1545
1546 /**
1547 Check a string-to-number conversion routine result and generate warnings
1548 in case when it:
1549 - could not convert any digits
1550 - found garbage at the end of the string.
1551
1552 @param type Data type name (e.g. "decimal", "integer", "double")
1553 @param edom Indicates that the string-to-number routine returned
1554 an error code equivalent to EDOM (value out of domain),
1555 i.e. the string fully consisted of garbage and the
1556 conversion routine could not get any digits from it.
1557 @param str The original string
1558 @param length Length of 'str'
1559 @param cs Character set
1560 @param end Pointer to char after last used digit
1561
1562 @note
1563 This is called after one has called one of the following functions:
1564 - strntoull10rnd()
1565 - my_strntod()
1566 - str2my_decimal()
1567
1568 @retval
1569 0 OK
1570 @retval
1571 1 error: could not scan any digits (EDOM),
1572 e.g. empty string, or garbage.
1573 @retval
1574 2 error: scanned some digits,
1575 but then found garbage at the end of the string.
1576 */
1577
1578
check_edom_and_important_data_truncation(const char * type,bool edom,CHARSET_INFO * cs,const char * str,size_t length,const char * end)1579 int Field_num::check_edom_and_important_data_truncation(const char *type,
1580 bool edom,
1581 CHARSET_INFO *cs,
1582 const char *str, size_t length,
1583 const char *end)
1584 {
1585 /* Test if we get an empty string or garbage */
1586 if (edom)
1587 {
1588 ErrConvString err(str, length, cs);
1589 set_warning_truncated_wrong_value(type, err.ptr());
1590 return 1;
1591 }
1592 /* Test if we have garbage at the end of the given string. */
1593 if (test_if_important_data(cs, end, str + length))
1594 {
1595 set_warning(WARN_DATA_TRUNCATED, 1);
1596 return 2;
1597 }
1598 return 0;
1599 }
1600
1601
check_edom_and_truncation(const char * type,bool edom,CHARSET_INFO * cs,const char * str,size_t length,const char * end)1602 int Field_num::check_edom_and_truncation(const char *type, bool edom,
1603 CHARSET_INFO *cs,
1604 const char *str, size_t length,
1605 const char *end)
1606 {
1607 int rc= check_edom_and_important_data_truncation(type, edom,
1608 cs, str, length, end);
1609 if (!rc && end < str + length)
1610 set_note(WARN_DATA_TRUNCATED, 1);
1611 return rc;
1612 }
1613
1614
1615 /*
1616 Convert a string to an integer then check bounds.
1617
1618 SYNOPSIS
1619 Field_num::get_int
1620 cs Character set
1621 from String to convert
1622 len Length of the string
1623 rnd OUT longlong value
1624 unsigned_max max unsigned value
1625 signed_min min signed value
1626 signed_max max signed value
1627
1628 DESCRIPTION
1629 The function calls strntoull10rnd() to get an integer value then
1630 check bounds and errors returned. In case of any error a warning
1631 is raised.
1632
1633 RETURN
1634 0 ok
1635 1 error
1636 */
1637
get_int(CHARSET_INFO * cs,const char * from,size_t len,longlong * rnd,ulonglong unsigned_max,longlong signed_min,longlong signed_max)1638 bool Field_num::get_int(CHARSET_INFO *cs, const char *from, size_t len,
1639 longlong *rnd, ulonglong unsigned_max,
1640 longlong signed_min, longlong signed_max)
1641 {
1642 char *end;
1643 int error;
1644
1645 *rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len,
1646 unsigned_flag, &end,
1647 &error);
1648 if (unsigned_flag)
1649 {
1650
1651 if ((((ulonglong) *rnd > unsigned_max) &&
1652 (*rnd= (longlong) unsigned_max)) ||
1653 error == MY_ERRNO_ERANGE)
1654 {
1655 goto out_of_range;
1656 }
1657 }
1658 else
1659 {
1660 if (*rnd < signed_min)
1661 {
1662 *rnd= signed_min;
1663 goto out_of_range;
1664 }
1665 else if (*rnd > signed_max)
1666 {
1667 *rnd= signed_max;
1668 goto out_of_range;
1669 }
1670 }
1671 if (get_thd()->count_cuted_fields > CHECK_FIELD_EXPRESSION &&
1672 check_int(cs, from, len, end, error))
1673 return 1;
1674 return 0;
1675
1676 out_of_range:
1677 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
1678 return 1;
1679 }
1680
1681
get_double(const char * str,size_t length,CHARSET_INFO * cs,int * error)1682 double Field_real::get_double(const char *str, size_t length, CHARSET_INFO *cs,
1683 int *error)
1684 {
1685 char *end;
1686 double nr= my_strntod(cs,(char*) str, length, &end, error);
1687 if (unlikely(*error))
1688 {
1689 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
1690 *error= 1;
1691 }
1692 else if (get_thd()->count_cuted_fields > CHECK_FIELD_EXPRESSION &&
1693 check_edom_and_truncation("double", str == end,
1694 cs, str, length, end))
1695 *error= 1;
1696 return nr;
1697 }
1698
1699
1700 /**
1701 Process decimal library return codes and issue warnings for overflow and
1702 truncation.
1703
1704 @param op_result decimal library return code (E_DEC_* see include/decimal.h)
1705
1706 @retval
1707 1 there was overflow
1708 @retval
1709 0 no error or some other errors except overflow
1710 */
1711
warn_if_overflow(int op_result)1712 int Field::warn_if_overflow(int op_result)
1713 {
1714 if (op_result == E_DEC_OVERFLOW)
1715 {
1716 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
1717 return 1;
1718 }
1719 if (op_result == E_DEC_TRUNCATED)
1720 {
1721 set_note(WARN_DATA_TRUNCATED, 1);
1722 /* We return 0 here as this is not a critical issue */
1723 }
1724 return 0;
1725 }
1726
1727
1728 /**
1729 Interpret field value as an integer but return the result as a string.
1730
1731 This is used for printing bit_fields as numbers while debugging.
1732 */
1733
val_int_as_str(String * val_buffer,bool unsigned_val)1734 String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
1735 {
1736 ASSERT_COLUMN_MARKED_FOR_READ;
1737 CHARSET_INFO *cs= &my_charset_bin;
1738 uint length;
1739 longlong value= val_int();
1740
1741 if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
1742 return 0;
1743 length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
1744 MY_INT64_NUM_DECIMAL_DIGITS,
1745 unsigned_val ? 10 : -10,
1746 value);
1747 val_buffer->length(length);
1748 return val_buffer;
1749 }
1750
1751
1752 /// 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,utype unireg_check_arg,const LEX_CSTRING * field_name_arg)1753 Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
1754 uchar null_bit_arg,
1755 utype unireg_check_arg, const LEX_CSTRING *field_name_arg)
1756 :ptr(ptr_arg), invisible(VISIBLE),
1757 null_ptr(null_ptr_arg), table(0), orig_table(0),
1758 table_name(0), field_name(*field_name_arg), option_list(0),
1759 option_struct(0), key_start(0), part_of_key(0),
1760 part_of_key_not_clustered(0), part_of_sortkey(0),
1761 unireg_check(unireg_check_arg), field_length(length_arg),
1762 null_bit(null_bit_arg), is_created_from_null_item(FALSE),
1763 read_stats(NULL), collected_stats(0), vcol_info(0), check_constraint(0),
1764 default_value(0)
1765 {
1766 flags=null_ptr ? 0: NOT_NULL_FLAG;
1767 comment.str= (char*) "";
1768 comment.length=0;
1769 field_index= 0;
1770 cond_selectivity= 1.0;
1771 next_equal_field= NULL;
1772 }
1773
1774
hash(ulong * nr,ulong * nr2)1775 void Field::hash(ulong *nr, ulong *nr2)
1776 {
1777 if (is_null())
1778 {
1779 *nr^= (*nr << 1) | 1;
1780 }
1781 else
1782 {
1783 uint len= pack_length();
1784 CHARSET_INFO *cs= sort_charset();
1785 cs->coll->hash_sort(cs, ptr, len, nr, nr2);
1786 }
1787 }
1788
1789 size_t
do_last_null_byte() const1790 Field::do_last_null_byte() const
1791 {
1792 DBUG_ASSERT(null_ptr == NULL || null_ptr >= table->record[0]);
1793 if (null_ptr)
1794 return (size_t) (null_ptr - table->record[0]) + 1;
1795 return LAST_NULL_BYTE_UNDEF;
1796 }
1797
1798
copy_from_tmp(int row_offset)1799 void Field::copy_from_tmp(int row_offset)
1800 {
1801 memcpy(ptr,ptr+row_offset,pack_length());
1802 if (null_ptr)
1803 {
1804 *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
1805 (null_ptr[row_offset] & (uchar) null_bit));
1806 }
1807 }
1808
1809
send_binary(Protocol * protocol)1810 bool Field::send_binary(Protocol *protocol)
1811 {
1812 char buff[MAX_FIELD_WIDTH];
1813 String tmp(buff,sizeof(buff),charset());
1814 val_str(&tmp);
1815 return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
1816 }
1817
1818
1819 /**
1820 Check to see if field size is compatible with destination.
1821
1822 This method is used in row-based replication to verify that the
1823 slave's field size is less than or equal to the master's field
1824 size. The encoded field metadata (from the master or source) is
1825 decoded and compared to the size of this field (the slave or
1826 destination).
1827
1828 @note
1829
1830 The comparison is made so that if the source data (from the master)
1831 is less than the target data (on the slave), -1 is returned in @c
1832 <code>*order_var</code>. This implies that a conversion is
1833 necessary, but that it is lossy and can result in truncation of the
1834 value.
1835
1836 If the source data is strictly greater than the target data, 1 is
1837 returned in <code>*order_var</code>. This implies that the source
1838 type can is contained in the target type and that a conversion is
1839 necessary but is non-lossy.
1840
1841 If no conversion is required to fit the source type in the target
1842 type, 0 is returned in <code>*order_var</code>.
1843
1844 @param field_metadata Encoded size in field metadata
1845 @param mflags Flags from the table map event for the table.
1846 @param order_var Pointer to variable where the order
1847 between the source field and this field
1848 will be returned.
1849
1850 @return @c true if this field's size is compatible with the
1851 master's field size, @c false otherwise.
1852 */
compatible_field_size(uint field_metadata,Relay_log_info * rli_arg,uint16 mflags,int * order_var)1853 bool Field::compatible_field_size(uint field_metadata,
1854 Relay_log_info *rli_arg __attribute__((unused)),
1855 uint16 mflags __attribute__((unused)),
1856 int *order_var)
1857 {
1858 uint const source_size= pack_length_from_metadata(field_metadata);
1859 uint const destination_size= row_pack_length();
1860 DBUG_PRINT("debug", ("real_type: %d, source_size: %u, destination_size: %u",
1861 real_type(), source_size, destination_size));
1862 *order_var = compare(source_size, destination_size);
1863 return true;
1864 }
1865
1866
store(const char * to,size_t length,CHARSET_INFO * cs,enum_check_fields check_level)1867 int Field::store(const char *to, size_t length, CHARSET_INFO *cs,
1868 enum_check_fields check_level)
1869 {
1870 Check_level_instant_set check_level_save(get_thd(), check_level);
1871 return store(to, length, cs);
1872 }
1873
1874
store_timestamp(my_time_t ts,ulong sec_part)1875 int Field::store_timestamp(my_time_t ts, ulong sec_part)
1876 {
1877 MYSQL_TIME ltime;
1878 get_thd()->timestamp_to_TIME(<ime, ts, sec_part, 0);
1879 return store_time_dec(<ime, decimals());
1880 }
1881
1882 /**
1883 Pack the field into a format suitable for storage and transfer.
1884
1885 To implement packing functionality, only the virtual function
1886 should be overridden. The other functions are just convenience
1887 functions and hence should not be overridden.
1888
1889 @note The default method for packing fields just copy the raw bytes
1890 of the record into the destination, but never more than
1891 <code>max_length</code> characters.
1892
1893 @param to
1894 Pointer to memory area where representation of field should be put.
1895
1896 @param from
1897 Pointer to memory area where record representation of field is
1898 stored.
1899
1900 @param max_length
1901 Maximum length of the field, as given in the column definition. For
1902 example, for <code>CHAR(1000)</code>, the <code>max_length</code>
1903 is 1000. This information is sometimes needed to decide how to pack
1904 the data.
1905
1906 */
1907 uchar *
pack(uchar * to,const uchar * from,uint max_length)1908 Field::pack(uchar *to, const uchar *from, uint max_length)
1909 {
1910 uint32 length= pack_length();
1911 set_if_smaller(length, max_length);
1912 memcpy(to, from, length);
1913 return to+length;
1914 }
1915
1916 /**
1917 Unpack a field from row data.
1918
1919 This method is used to unpack a field from a master whose size of
1920 the field is less than that of the slave.
1921
1922 The <code>param_data</code> parameter is a two-byte integer (stored
1923 in the least significant 16 bits of the unsigned integer) usually
1924 consisting of two parts: the real type in the most significant byte
1925 and a original pack length in the least significant byte.
1926
1927 The exact layout of the <code>param_data</code> field is given by
1928 the <code>Table_map_log_event::save_field_metadata()</code>.
1929
1930 This is the default method for unpacking a field. It just copies
1931 the memory block in byte order (of original pack length bytes or
1932 length of field, whichever is smaller).
1933
1934 @param to Destination of the data
1935 @param from Source of the data
1936 @param param_data Real type and original pack length of the field
1937 data
1938
1939 @return New pointer into memory based on from + length of the data
1940 @return 0 if wrong data
1941 */
1942 const uchar *
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)1943 Field::unpack(uchar* to, const uchar *from, const uchar *from_end,
1944 uint param_data)
1945 {
1946 uint length=pack_length(), len;
1947 int from_type= 0;
1948 /*
1949 If from length is > 255, it has encoded data in the upper bits. Need
1950 to mask it out.
1951 */
1952 if (param_data > 255)
1953 {
1954 from_type= (param_data & 0xff00) >> 8U; // real_type.
1955 param_data= param_data & 0x00ff; // length.
1956 }
1957
1958 if ((param_data == 0) ||
1959 (length == param_data) ||
1960 (from_type != real_type()))
1961 {
1962 if (from + length > from_end)
1963 return 0; // Error in data
1964
1965 memcpy(to, from, length);
1966 return from+length;
1967 }
1968
1969 len= (param_data && (param_data < length)) ? param_data : length;
1970
1971 if (from + len > from_end)
1972 return 0; // Error in data
1973
1974 memcpy(to, from, len);
1975 return from+len;
1976 }
1977
1978
val_decimal(my_decimal * decimal)1979 my_decimal *Field::val_decimal(my_decimal *decimal)
1980 {
1981 /* This never have to be called */
1982 DBUG_ASSERT(0);
1983 return 0;
1984 }
1985
1986
add_zerofill_and_unsigned(String & res) const1987 void Field_num::add_zerofill_and_unsigned(String &res) const
1988 {
1989 if (unsigned_flag)
1990 res.append(STRING_WITH_LEN(" unsigned"));
1991 if (zerofill)
1992 res.append(STRING_WITH_LEN(" zerofill"));
1993 }
1994
1995
make_send_field(Send_field * field)1996 void Field::make_send_field(Send_field *field)
1997 {
1998 if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
1999 {
2000 field->db_name= orig_table->s->db.str;
2001 if (orig_table->pos_in_table_list &&
2002 orig_table->pos_in_table_list->schema_table)
2003 field->org_table_name= (orig_table->pos_in_table_list->
2004 schema_table->table_name);
2005 else
2006 field->org_table_name= orig_table->s->table_name.str;
2007 }
2008 else
2009 field->org_table_name= field->db_name= "";
2010 if (orig_table && orig_table->alias.ptr())
2011 {
2012 field->table_name= orig_table->alias.ptr();
2013 field->org_col_name= field_name;
2014 }
2015 else
2016 {
2017 field->table_name= "";
2018 field->org_col_name= empty_clex_str;
2019 }
2020 field->col_name= field_name;
2021 field->length=field_length;
2022 field->type=type();
2023 field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
2024 field->decimals= 0;
2025 }
2026
2027
2028 /**
2029 Conversion from decimal to longlong with checking overflow and
2030 setting correct value (min/max) in case of overflow.
2031
2032 @param val value which have to be converted
2033 @param unsigned_flag type of integer in which we convert val
2034 @param err variable to pass error code
2035
2036 @return
2037 value converted from val
2038 */
convert_decimal2longlong(const my_decimal * val,bool unsigned_flag,int * err)2039 longlong Field::convert_decimal2longlong(const my_decimal *val,
2040 bool unsigned_flag, int *err)
2041 {
2042 longlong i;
2043 if (unsigned_flag)
2044 {
2045 if (val->sign())
2046 {
2047 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
2048 i= 0;
2049 *err= 1;
2050 }
2051 else if (warn_if_overflow(my_decimal2int((E_DEC_ERROR &
2052 ~E_DEC_OVERFLOW &
2053 ~E_DEC_TRUNCATED),
2054 val, TRUE, &i)))
2055 {
2056 i= ~(longlong) 0;
2057 *err= 1;
2058 }
2059 }
2060 else if (warn_if_overflow(my_decimal2int((E_DEC_ERROR &
2061 ~E_DEC_OVERFLOW &
2062 ~E_DEC_TRUNCATED),
2063 val, FALSE, &i)))
2064 {
2065 i= (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
2066 *err= 1;
2067 }
2068 return i;
2069 }
2070
2071
2072 /**
2073 Storing decimal in integer fields.
2074
2075 @param val value for storing
2076
2077 @note
2078 This method is used by all integer fields, real/decimal redefine it
2079
2080 @retval
2081 0 OK
2082 @retval
2083 !=0 error
2084 */
2085
store_decimal(const my_decimal * val)2086 int Field_int::store_decimal(const my_decimal *val)
2087 {
2088 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
2089 int err= 0;
2090 longlong i= convert_decimal2longlong(val, unsigned_flag, &err);
2091 return MY_TEST(err | store(i, unsigned_flag));
2092 }
2093
2094
2095 /**
2096 Return decimal value of integer field.
2097
2098 @param decimal_value buffer for storing decimal value
2099
2100 @note
2101 This method is used by all integer fields, real/decimal redefine it.
2102 All longlong values fit in our decimal buffer which cal store 8*9=72
2103 digits of integer number
2104
2105 @return
2106 pointer to decimal buffer with value of field
2107 */
2108
val_decimal(my_decimal * decimal_value)2109 my_decimal* Field_int::val_decimal(my_decimal *decimal_value)
2110 {
2111 ASSERT_COLUMN_MARKED_FOR_READ;
2112 longlong nr= val_int();
2113 int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
2114 return decimal_value;
2115 }
2116
2117
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2118 bool Field_int::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
2119 {
2120 ASSERT_COLUMN_MARKED_FOR_READ;
2121 longlong nr= val_int();
2122 bool neg= !(flags & UNSIGNED_FLAG) && nr < 0;
2123 return int_to_datetime_with_warn(neg, neg ? -nr : nr, ltime, fuzzydate,
2124 table->s, field_name.str);
2125 }
2126
2127
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate,ulonglong trx_id)2128 bool Field_vers_trx_id::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate, ulonglong trx_id)
2129 {
2130 ASSERT_COLUMN_MARKED_FOR_READ;
2131 DBUG_ASSERT(ltime);
2132 if (!table || !table->s)
2133 return true;
2134 DBUG_ASSERT(table->versioned(VERS_TRX_ID) ||
2135 (table->versioned() && table->s->table_category == TABLE_CATEGORY_TEMPORARY));
2136 if (!trx_id)
2137 return true;
2138
2139 THD *thd= get_thd();
2140 DBUG_ASSERT(thd);
2141 if (trx_id == ULONGLONG_MAX)
2142 {
2143 thd->variables.time_zone->gmt_sec_to_TIME(ltime, TIMESTAMP_MAX_VALUE);
2144 ltime->second_part= TIME_MAX_SECOND_PART;
2145 return false;
2146 }
2147 if (cached == trx_id)
2148 {
2149 *ltime= cache;
2150 return false;
2151 }
2152
2153 TR_table trt(thd);
2154 bool found= trt.query(trx_id);
2155 if (found)
2156 {
2157 trt[TR_table::FLD_COMMIT_TS]->get_date(&cache, fuzzydate);
2158 *ltime= cache;
2159 cached= trx_id;
2160 return false;
2161 }
2162
2163 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
2164 ER_VERS_NO_TRX_ID, ER_THD(thd, ER_VERS_NO_TRX_ID),
2165 (longlong) trx_id);
2166 return true;
2167 }
2168
2169
Field_str(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)2170 Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
2171 uchar null_bit_arg, utype unireg_check_arg,
2172 const LEX_CSTRING *field_name_arg,
2173 const DTCollation &collation)
2174 :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2175 unireg_check_arg, field_name_arg)
2176 {
2177 field_charset= collation.collation;
2178 if (collation.collation->state & MY_CS_BINSORT)
2179 flags|=BINARY_FLAG;
2180 field_derivation= collation.derivation;
2181 field_repertoire= collation.repertoire;
2182 }
2183
2184
test_if_equality_guarantees_uniqueness(const Item * item) const2185 bool Field_str::test_if_equality_guarantees_uniqueness(const Item *item) const
2186 {
2187 /*
2188 Can't guarantee uniqueness when comparing a CHAR/VARCHAR/TEXT,
2189 BINARY/VARBINARY/BLOB, ENUM,SET columns to an item with cmp_type()
2190 of INT_RESULT, DOUBLE_RESULT, DECIMAL_RESULT or TIME_RESULT.
2191 Example:
2192 SELECT * FROM t1 WHERE varchar_column=DATE'2001-01-01'
2193 return non-unuque values, e.g. '2001-01-01' and '2001-01-01x'.
2194 */
2195 if (!field_charset->coll->propagate(field_charset, 0, 0) ||
2196 item->cmp_type() != STRING_RESULT)
2197 return false;
2198 /*
2199 Can't guarantee uniqueness when comparing to
2200 an item of a different collation.
2201 Example:
2202 SELECT * FROM t1
2203 WHERE latin1_bin_column = _latin1'A' COLLATE latin1_swedish_ci
2204 return non-unique values 'a' and 'A'.
2205 */
2206 DTCollation tmp(field_charset, field_derivation, repertoire());
2207 return !tmp.aggregate(item->collation) && tmp.collation == field_charset;
2208 }
2209
2210
can_be_substituted_to_equal_item(const Context & ctx,const Item_equal * item_equal)2211 bool Field_str::can_be_substituted_to_equal_item(const Context &ctx,
2212 const Item_equal *item_equal)
2213 {
2214 DBUG_ASSERT(item_equal->compare_type_handler()->cmp_type() == STRING_RESULT);
2215 switch (ctx.subst_constraint()) {
2216 case ANY_SUBST:
2217 return ctx.compare_type_handler() == item_equal->compare_type_handler() &&
2218 (ctx.compare_type_handler()->cmp_type() != STRING_RESULT ||
2219 ctx.compare_collation() == item_equal->compare_collation());
2220 case IDENTITY_SUBST:
2221 return ((charset()->state & MY_CS_BINSORT) &&
2222 (charset()->state & MY_CS_NOPAD));
2223 }
2224 return false;
2225 }
2226
2227
make_send_field(Send_field * field)2228 void Field_num::make_send_field(Send_field *field)
2229 {
2230 Field::make_send_field(field);
2231 field->decimals= dec;
2232 }
2233
2234 /**
2235 Decimal representation of Field_str.
2236
2237 @param d value for storing
2238
2239 @note
2240 Field_str is the base class for fields like Field_enum,
2241 Field_date and some similar. Some dates use fraction and also
2242 string value should be converted to floating point value according
2243 our rules, so we use double to store value of decimal in string.
2244
2245 @todo
2246 use decimal2string?
2247
2248 @retval
2249 0 OK
2250 @retval
2251 !=0 error
2252 */
2253
store_decimal(const my_decimal * d)2254 int Field_str::store_decimal(const my_decimal *d)
2255 {
2256 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
2257 double val;
2258 /* TODO: use decimal2string? */
2259 int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
2260 ~E_DEC_OVERFLOW, d, &val));
2261 return err | store(val);
2262 }
2263
2264
val_decimal(my_decimal * decimal_value)2265 my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
2266 {
2267 ASSERT_COLUMN_MARKED_FOR_READ;
2268 longlong nr= val_int();
2269 int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
2270 return decimal_value;
2271 }
2272
2273
fill_cache_field(CACHE_FIELD * copy)2274 uint Field::fill_cache_field(CACHE_FIELD *copy)
2275 {
2276 uint store_length;
2277 copy->str= ptr;
2278 copy->length= pack_length_in_rec();
2279 copy->field= this;
2280 if (flags & BLOB_FLAG)
2281 {
2282 copy->type= CACHE_BLOB;
2283 copy->length-= portable_sizeof_char_ptr;
2284 return copy->length;
2285 }
2286 else if (!zero_pack() &&
2287 (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
2288 copy->length < 256))
2289 {
2290 copy->type= CACHE_STRIPPED; /* Remove end space */
2291 store_length= 2;
2292 }
2293 else if (type() == MYSQL_TYPE_VARCHAR)
2294 {
2295 copy->type= pack_length()-row_pack_length() == 1 ? CACHE_VARSTR1:
2296 CACHE_VARSTR2;
2297 store_length= 0;
2298 }
2299 else
2300 {
2301 copy->type= 0;
2302 store_length= 0;
2303 }
2304 return copy->length + store_length;
2305 }
2306
2307
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2308 bool Field::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
2309 {
2310 char buff[40];
2311 String tmp(buff,sizeof(buff),&my_charset_bin),*res;
2312 if (!(res=val_str(&tmp)) ||
2313 str_to_datetime_with_warn(res->charset(), res->ptr(), res->length(),
2314 ltime, fuzzydate))
2315 return 1;
2316 return 0;
2317 }
2318
2319 /**
2320 This is called when storing a date in a string.
2321
2322 @note
2323 Needs to be changed if/when we want to support different time formats.
2324 */
2325
store_time_dec(const MYSQL_TIME * ltime,uint dec)2326 int Field::store_time_dec(const MYSQL_TIME *ltime, uint dec)
2327 {
2328 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
2329 char buff[MAX_DATE_STRING_REP_LENGTH];
2330 uint length= (uint) my_TIME_to_str(ltime, buff, dec);
2331 /* Avoid conversion when field character set is ASCII compatible */
2332 return store(buff, length, (charset()->state & MY_CS_NONASCII) ?
2333 &my_charset_latin1 : charset());
2334 }
2335
2336
optimize_range(uint idx,uint part) const2337 bool Field::optimize_range(uint idx, uint part) const
2338 {
2339 return MY_TEST(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
2340 }
2341
2342
make_new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type)2343 Field *Field::make_new_field(MEM_ROOT *root, TABLE *new_table,
2344 bool keep_type __attribute__((unused)))
2345 {
2346 Field *tmp;
2347 if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
2348 return 0;
2349
2350 if (tmp->table->maybe_null)
2351 tmp->flags&= ~NOT_NULL_FLAG;
2352 tmp->table= new_table;
2353 tmp->key_start.init(0);
2354 tmp->part_of_key.init(0);
2355 tmp->part_of_sortkey.init(0);
2356 /*
2357 TODO: it is not clear why this method needs to reset unireg_check.
2358 Try not to reset it, or explain why it needs to be reset.
2359 */
2360 tmp->unireg_check= Field::NONE;
2361 tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
2362 ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG |
2363 VERS_ROW_START | VERS_ROW_END |
2364 VERS_UPDATE_UNVERSIONED_FLAG);
2365 tmp->reset_fields();
2366 tmp->invisible= VISIBLE;
2367 return tmp;
2368 }
2369
2370
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uint32 length,uchar * new_null_ptr,uint new_null_bit)2371 Field *Field::new_key_field(MEM_ROOT *root, TABLE *new_table,
2372 uchar *new_ptr, uint32 length,
2373 uchar *new_null_ptr, uint new_null_bit)
2374 {
2375 Field *tmp;
2376 if ((tmp= make_new_field(root, new_table, table == new_table)))
2377 {
2378 tmp->ptr= new_ptr;
2379 tmp->null_ptr= new_null_ptr;
2380 tmp->null_bit= new_null_bit;
2381 }
2382 return tmp;
2383 }
2384
2385
2386 /* This is used to generate a field in TABLE from TABLE_SHARE */
2387
clone(MEM_ROOT * root,TABLE * new_table)2388 Field *Field::clone(MEM_ROOT *root, TABLE *new_table)
2389 {
2390 Field *tmp;
2391 if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
2392 {
2393 tmp->init(new_table);
2394 tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
2395 new_table->s->default_values));
2396 }
2397 return tmp;
2398 }
2399
2400
clone(MEM_ROOT * root,TABLE * new_table,my_ptrdiff_t diff)2401 Field *Field::clone(MEM_ROOT *root, TABLE *new_table, my_ptrdiff_t diff)
2402 {
2403 Field *tmp;
2404 if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
2405 {
2406 if (new_table)
2407 tmp->init(new_table);
2408 tmp->move_field_offset(diff);
2409 }
2410 return tmp;
2411 }
2412
2413
set_default()2414 int Field::set_default()
2415 {
2416 if (default_value)
2417 {
2418 Query_arena backup_arena;
2419 table->in_use->set_n_backup_active_arena(table->expr_arena, &backup_arena);
2420 int rc= default_value->expr->save_in_field(this, 0);
2421 table->in_use->restore_active_arena(table->expr_arena, &backup_arena);
2422 return rc;
2423 }
2424 /* Copy constant value stored in s->default_values */
2425 my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
2426 table->record[0]);
2427 memcpy(ptr, ptr + l_offset, pack_length_in_rec());
2428 if (maybe_null_in_table())
2429 *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
2430 (null_ptr[l_offset] & null_bit));
2431 return 0;
2432 }
2433
2434
2435 /****************************************************************************
2436 Field_null, a field that always return NULL
2437 ****************************************************************************/
2438
sql_type(String & res) const2439 void Field_null::sql_type(String &res) const
2440 {
2441 res.set_ascii(STRING_WITH_LEN("null"));
2442 }
2443
2444
2445 /****************************************************************************
2446 Field_row, e.g. for ROW-type SP variables
2447 ****************************************************************************/
2448
~Field_row()2449 Field_row::~Field_row()
2450 {
2451 delete m_table;
2452 }
2453
2454
sp_prepare_and_store_item(THD * thd,Item ** value)2455 bool Field_row::sp_prepare_and_store_item(THD *thd, Item **value)
2456 {
2457 DBUG_ENTER("Field_row::sp_prepare_and_store_item");
2458
2459 if (value[0]->type() == Item::NULL_ITEM)
2460 {
2461 /*
2462 We're in a auto-generated sp_inst_set, to assign
2463 the explicit default NULL value to a ROW variable.
2464 */
2465 m_table->set_all_fields_to_null();
2466 DBUG_RETURN(false);
2467 }
2468
2469 /**
2470 - In case if we're assigning a ROW variable from another ROW variable,
2471 value[0] points to Item_splocal. sp_fix_func_item() will return the
2472 fixed underlying Item_field pointing to Field_row.
2473 - In case if we're assigning from a ROW() value, src and value[0] will
2474 point to the same Item_row.
2475 */
2476 Item *src;
2477 if (!(src= thd->sp_fix_func_item(value)) ||
2478 src->cmp_type() != ROW_RESULT ||
2479 src->cols() != m_table->s->fields)
2480 {
2481 my_error(ER_OPERAND_COLUMNS, MYF(0), m_table->s->fields);
2482 m_table->set_all_fields_to_null();
2483 DBUG_RETURN(true);
2484 }
2485
2486 DBUG_RETURN(m_table->sp_set_all_fields_from_item(thd, src));
2487 }
2488
2489
2490 /****************************************************************************
2491 Functions for the Field_decimal class
2492 This is an number stored as a pre-space (or pre-zero) string
2493 ****************************************************************************/
2494
2495 int
reset(void)2496 Field_decimal::reset(void)
2497 {
2498 Field_decimal::store(STRING_WITH_LEN("0"),&my_charset_bin);
2499 return 0;
2500 }
2501
overflow(bool negative)2502 void Field_decimal::overflow(bool negative)
2503 {
2504 uint len=field_length;
2505 uchar *to=ptr, filler= '9';
2506
2507 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
2508 if (negative)
2509 {
2510 if (!unsigned_flag)
2511 {
2512 /* Put - sign as a first digit so we'll have -999..999 or 999..999 */
2513 *to++ = '-';
2514 len--;
2515 }
2516 else
2517 {
2518 filler= '0'; // Fill up with 0
2519 if (!zerofill)
2520 {
2521 /*
2522 Handle unsigned integer without zerofill, in which case
2523 the number should be of format ' 0' or ' 0.000'
2524 */
2525 uint whole_part=field_length- (dec ? dec+2 : 1);
2526 // Fill with spaces up to the first digit
2527 bfill(to, whole_part, ' ');
2528 to+= whole_part;
2529 len-= whole_part;
2530 // The main code will also handle the 0 before the decimal point
2531 }
2532 }
2533 }
2534 bfill(to, len, filler);
2535 if (dec)
2536 ptr[field_length-dec-1]='.';
2537 return;
2538 }
2539
2540
store(const char * from_arg,size_t len,CHARSET_INFO * cs)2541 int Field_decimal::store(const char *from_arg, size_t len, CHARSET_INFO *cs)
2542 {
2543 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
2544 char buff[STRING_BUFFER_USUAL_SIZE];
2545 String tmp(buff,sizeof(buff), &my_charset_bin);
2546 const uchar *from= (uchar*) from_arg;
2547
2548 /* Convert character set if the old one is multi uchar */
2549 if (cs->mbmaxlen > 1)
2550 {
2551 uint dummy_errors;
2552 tmp.copy((char*) from, len, cs, &my_charset_bin, &dummy_errors);
2553 from= (uchar*) tmp.ptr();
2554 len= tmp.length();
2555 }
2556
2557 const uchar *end= from+len;
2558 /* The pointer where the field value starts (i.e., "where to write") */
2559 uchar *to= ptr;
2560 uint tmp_dec, tmp_uint;
2561 /*
2562 The sign of the number : will be 0 (means positive but sign not
2563 specified), '+' or '-'
2564 */
2565 uchar sign_char=0;
2566 /* The pointers where prezeros start and stop */
2567 const uchar *pre_zeros_from, *pre_zeros_end;
2568 /* The pointers where digits at the left of '.' start and stop */
2569 const uchar *int_digits_from, *int_digits_end;
2570 /* The pointers where digits at the right of '.' start and stop */
2571 const uchar *frac_digits_from, *frac_digits_end;
2572 /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
2573 char expo_sign_char=0;
2574 uint exponent=0; // value of the exponent
2575 /*
2576 Pointers used when digits move from the left of the '.' to the
2577 right of the '.' (explained below)
2578 */
2579 const uchar *UNINIT_VAR(int_digits_tail_from);
2580 /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
2581 uint UNINIT_VAR(int_digits_added_zeros);
2582 /*
2583 Pointer used when digits move from the right of the '.' to the left
2584 of the '.'
2585 */
2586 const uchar *UNINIT_VAR(frac_digits_head_end);
2587 /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
2588 uint UNINIT_VAR(frac_digits_added_zeros);
2589 uchar *pos,*tmp_left_pos,*tmp_right_pos;
2590 /* Pointers that are used as limits (begin and end of the field buffer) */
2591 uchar *left_wall,*right_wall;
2592 uchar tmp_char;
2593 /*
2594 To remember if get_thd()->cuted_fields has already been incremented,
2595 to do that only once
2596 */
2597 bool is_cuted_fields_incr=0;
2598
2599 /*
2600 There are three steps in this function :
2601 - parse the input string
2602 - modify the position of digits around the decimal dot '.'
2603 according to the exponent value (if specified)
2604 - write the formatted number
2605 */
2606
2607 if ((tmp_dec=dec))
2608 tmp_dec++;
2609
2610 /* skip pre-space */
2611 while (from != end && my_isspace(&my_charset_bin,*from))
2612 from++;
2613 if (from == end)
2614 {
2615 set_warning(WARN_DATA_TRUNCATED, 1);
2616 is_cuted_fields_incr=1;
2617 }
2618 else if (*from == '+' || *from == '-') // Found some sign ?
2619 {
2620 sign_char= *from++;
2621 /*
2622 We allow "+" for unsigned decimal unless defined different
2623 Both options allowed as one may wish not to have "+" for unsigned numbers
2624 because of data processing issues
2625 */
2626 if (unsigned_flag)
2627 {
2628 if (sign_char=='-')
2629 {
2630 Field_decimal::overflow(1);
2631 return 1;
2632 }
2633 /*
2634 Defining this will not store "+" for unsigned decimal type even if
2635 it is passed in numeric string. This will make some tests to fail
2636 */
2637 #ifdef DONT_ALLOW_UNSIGNED_PLUS
2638 else
2639 sign_char=0;
2640 #endif
2641 }
2642 }
2643
2644 pre_zeros_from= from;
2645 for (; from!=end && *from == '0'; from++) ; // Read prezeros
2646 pre_zeros_end=int_digits_from=from;
2647 /* Read non zero digits at the left of '.'*/
2648 for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
2649 int_digits_end=from;
2650 if (from!=end && *from == '.') // Some '.' ?
2651 from++;
2652 frac_digits_from= from;
2653 /* Read digits at the right of '.' */
2654 for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
2655 frac_digits_end=from;
2656 // Some exponentiation symbol ?
2657 if (from != end && (*from == 'e' || *from == 'E'))
2658 {
2659 from++;
2660 if (from != end && (*from == '+' || *from == '-')) // Some exponent sign ?
2661 expo_sign_char= *from++;
2662 else
2663 expo_sign_char= '+';
2664 /*
2665 Read digits of the exponent and compute its value. We must care about
2666 'exponent' overflow, because as unsigned arithmetic is "modulo", big
2667 exponents will become small (e.g. 1e4294967296 will become 1e0, and the
2668 field will finally contain 1 instead of its max possible value).
2669 */
2670 for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
2671 {
2672 exponent=10*exponent+(*from-'0');
2673 if (exponent>MAX_EXPONENT)
2674 break;
2675 }
2676 }
2677
2678 /*
2679 We only have to generate warnings if count_cuted_fields is set.
2680 This is to avoid extra checks of the number when they are not needed.
2681 Even if this flag is not set, it's OK to increment warnings, if
2682 it makes the code easier to read.
2683 */
2684
2685 if (get_thd()->count_cuted_fields > CHECK_FIELD_EXPRESSION)
2686 {
2687 // Skip end spaces
2688 for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
2689 if (from != end) // If still something left, warn
2690 {
2691 set_warning(WARN_DATA_TRUNCATED, 1);
2692 is_cuted_fields_incr=1;
2693 }
2694 }
2695
2696 /*
2697 Now "move" digits around the decimal dot according to the exponent value,
2698 and add necessary zeros.
2699 Examples :
2700 - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
2701 - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
2702 between '.' and '1'
2703 - 1234.5E-3 : '234' moves at the right of '.'
2704 These moves are implemented with pointers which point at the begin
2705 and end of each moved segment. Examples :
2706 - 1234.5E-3 : before the code below is executed, the int_digits part is
2707 from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2708 below, the int_digits part is from '1' to '1', the frac_digits_head
2709 part is from '2' to '4', and the frac_digits part from '5' to '5'.
2710 - 1234.5E3 : before the code below is executed, the int_digits part is
2711 from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2712 below, the int_digits part is from '1' to '4', the int_digits_tail
2713 part is from '5' to '5', the frac_digits part is empty, and
2714 int_digits_added_zeros=2 (to make 1234500).
2715 */
2716
2717 /*
2718 Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
2719 as int_digits_added_zeros<=exponent<4G and
2720 (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
2721 (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
2722 */
2723
2724 if (!expo_sign_char)
2725 tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
2726 else if (expo_sign_char == '-')
2727 {
2728 tmp_uint=MY_MIN(exponent,(uint)(int_digits_end-int_digits_from));
2729 frac_digits_added_zeros=exponent-tmp_uint;
2730 int_digits_end -= tmp_uint;
2731 frac_digits_head_end=int_digits_end+tmp_uint;
2732 tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
2733 }
2734 else // (expo_sign_char=='+')
2735 {
2736 tmp_uint=MY_MIN(exponent,(uint)(frac_digits_end-frac_digits_from));
2737 int_digits_added_zeros=exponent-tmp_uint;
2738 int_digits_tail_from=frac_digits_from;
2739 frac_digits_from=frac_digits_from+tmp_uint;
2740 /*
2741 We "eat" the heading zeros of the
2742 int_digits.int_digits_tail.int_digits_added_zeros concatenation
2743 (for example 0.003e3 must become 3 and not 0003)
2744 */
2745 if (int_digits_from == int_digits_end)
2746 {
2747 /*
2748 There was nothing in the int_digits part, so continue
2749 eating int_digits_tail zeros
2750 */
2751 for (; int_digits_tail_from != frac_digits_from &&
2752 *int_digits_tail_from == '0'; int_digits_tail_from++) ;
2753 if (int_digits_tail_from == frac_digits_from)
2754 {
2755 // there were only zeros in int_digits_tail too
2756 int_digits_added_zeros=0;
2757 }
2758 }
2759 tmp_uint= (uint) (tmp_dec+(int_digits_end-int_digits_from)+
2760 (uint)(frac_digits_from-int_digits_tail_from)+
2761 int_digits_added_zeros);
2762 }
2763
2764 /*
2765 Now write the formatted number
2766
2767 First the digits of the int_% parts.
2768 Do we have enough room to write these digits ?
2769 If the sign is defined and '-', we need one position for it
2770 */
2771
2772 if (field_length < tmp_uint + (int) (sign_char == '-'))
2773 {
2774 // too big number, change to max or min number
2775 Field_decimal::overflow(sign_char == '-');
2776 return 1;
2777 }
2778
2779 /*
2780 Tmp_left_pos is the position where the leftmost digit of
2781 the int_% parts will be written
2782 */
2783 tmp_left_pos=pos=to+(uint)(field_length-tmp_uint);
2784
2785 // Write all digits of the int_% parts
2786 while (int_digits_from != int_digits_end)
2787 *pos++ = *int_digits_from++ ;
2788
2789 if (expo_sign_char == '+')
2790 {
2791 while (int_digits_tail_from != frac_digits_from)
2792 *pos++= *int_digits_tail_from++;
2793 while (int_digits_added_zeros-- >0)
2794 *pos++= '0';
2795 }
2796 /*
2797 Note the position where the rightmost digit of the int_% parts has been
2798 written (this is to later check if the int_% parts contained nothing,
2799 meaning an extra 0 is needed).
2800 */
2801 tmp_right_pos=pos;
2802
2803 /*
2804 Step back to the position of the leftmost digit of the int_% parts,
2805 to write sign and fill with zeros or blanks or prezeros.
2806 */
2807 pos=tmp_left_pos-1;
2808 if (zerofill)
2809 {
2810 left_wall=to-1;
2811 while (pos > left_wall) // Fill with zeros
2812 *pos--='0';
2813 }
2814 else
2815 {
2816 left_wall=to+(sign_char != 0)-1;
2817 if (!expo_sign_char) // If exponent was specified, ignore prezeros
2818 {
2819 for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
2820 pre_zeros_from++)
2821 *pos--= '0';
2822 }
2823 if (pos == tmp_right_pos-1)
2824 *pos--= '0'; // no 0 has ever been written, so write one
2825 left_wall= to-1;
2826 if (sign_char && pos != left_wall)
2827 {
2828 /* Write sign if possible (it is if sign is '-') */
2829 *pos--= sign_char;
2830 }
2831 while (pos != left_wall)
2832 *pos--=' '; //fill with blanks
2833 }
2834
2835 /*
2836 Write digits of the frac_% parts ;
2837 Depending on get_thd()->count_cuted_fields, we may also want
2838 to know if some non-zero tail of these parts will
2839 be truncated (for example, 0.002->0.00 will generate a warning,
2840 while 0.000->0.00 will not)
2841 (and 0E1000000000 will not, while 1E-1000000000 will)
2842 */
2843
2844 pos=to+(uint)(field_length-tmp_dec); // Calculate post to '.'
2845 right_wall=to+field_length;
2846 if (pos != right_wall)
2847 *pos++='.';
2848
2849 if (expo_sign_char == '-')
2850 {
2851 while (frac_digits_added_zeros-- > 0)
2852 {
2853 if (pos == right_wall)
2854 {
2855 if (get_thd()->count_cuted_fields > CHECK_FIELD_EXPRESSION &&
2856 !is_cuted_fields_incr)
2857 break; // Go on below to see if we lose non zero digits
2858 return 0;
2859 }
2860 *pos++='0';
2861 }
2862 while (int_digits_end != frac_digits_head_end)
2863 {
2864 tmp_char= *int_digits_end++;
2865 if (pos == right_wall)
2866 {
2867 if (tmp_char != '0') // Losing a non zero digit ?
2868 {
2869 if (!is_cuted_fields_incr)
2870 set_warning(WARN_DATA_TRUNCATED, 1);
2871 return 0;
2872 }
2873 continue;
2874 }
2875 *pos++= tmp_char;
2876 }
2877 }
2878
2879 for (;frac_digits_from!=frac_digits_end;)
2880 {
2881 tmp_char= *frac_digits_from++;
2882 if (pos == right_wall)
2883 {
2884 if (tmp_char != '0') // Losing a non zero digit ?
2885 {
2886 if (!is_cuted_fields_incr)
2887 {
2888 /*
2889 This is a note, not a warning, as we don't want to abort
2890 when we cut decimals in strict mode
2891 */
2892 set_note(WARN_DATA_TRUNCATED, 1);
2893 }
2894 return 0;
2895 }
2896 continue;
2897 }
2898 *pos++= tmp_char;
2899 }
2900
2901 while (pos != right_wall)
2902 *pos++='0'; // Fill with zeros at right of '.'
2903 return 0;
2904 }
2905
2906
store(double nr)2907 int Field_decimal::store(double nr)
2908 {
2909 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
2910 if (unsigned_flag && nr < 0)
2911 {
2912 overflow(1);
2913 return 1;
2914 }
2915
2916 if (!std::isfinite(nr)) // Handle infinity as special case
2917 {
2918 overflow(nr < 0.0);
2919 return 1;
2920 }
2921
2922 size_t length;
2923 uchar fyllchar,*to;
2924 char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
2925
2926 fyllchar = zerofill ? (char) '0' : (char) ' ';
2927 length= my_fcvt(nr, dec, buff, NULL);
2928
2929 if (length > field_length)
2930 {
2931 overflow(nr < 0.0);
2932 return 1;
2933 }
2934 else
2935 {
2936 to=ptr;
2937 for (size_t i=field_length-length ; i-- > 0 ;)
2938 *to++ = fyllchar;
2939 memcpy(to,buff,length);
2940 return 0;
2941 }
2942 }
2943
2944
store(longlong nr,bool unsigned_val)2945 int Field_decimal::store(longlong nr, bool unsigned_val)
2946 {
2947 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
2948 char buff[22];
2949 uint length, int_part;
2950 char fyllchar;
2951 uchar *to;
2952
2953 if (nr < 0 && unsigned_flag && !unsigned_val)
2954 {
2955 overflow(1);
2956 return 1;
2957 }
2958 length= (uint) (longlong10_to_str(nr,buff,unsigned_val ? 10 : -10) - buff);
2959 int_part= field_length- (dec ? dec+1 : 0);
2960
2961 if (length > int_part)
2962 {
2963 overflow(!unsigned_val && nr < 0L); /* purecov: inspected */
2964 return 1;
2965 }
2966
2967 fyllchar = zerofill ? (char) '0' : (char) ' ';
2968 to= ptr;
2969 for (uint i=int_part-length ; i-- > 0 ;)
2970 *to++ = fyllchar;
2971 memcpy(to,buff,length);
2972 if (dec)
2973 {
2974 to[length]='.';
2975 bfill(to+length+1,dec,'0');
2976 }
2977 return 0;
2978 }
2979
2980
val_real(void)2981 double Field_decimal::val_real(void)
2982 {
2983 ASSERT_COLUMN_MARKED_FOR_READ;
2984 int not_used;
2985 char *end_not_used;
2986 return my_strntod(&my_charset_bin, (char*) ptr, field_length, &end_not_used,
2987 ¬_used);
2988 }
2989
val_int(void)2990 longlong Field_decimal::val_int(void)
2991 {
2992 ASSERT_COLUMN_MARKED_FOR_READ;
2993 int not_used;
2994 if (unsigned_flag)
2995 return my_strntoull(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2996 ¬_used);
2997 return my_strntoll(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2998 ¬_used);
2999 }
3000
3001
val_str(String * val_buffer,String * val_ptr)3002 String *Field_decimal::val_str(String *val_buffer __attribute__((unused)),
3003 String *val_ptr)
3004 {
3005 ASSERT_COLUMN_MARKED_FOR_READ;
3006 uchar *str;
3007 size_t tmp_length;
3008
3009 for (str=ptr ; *str == ' ' ; str++) ;
3010 val_ptr->set_charset(&my_charset_numeric);
3011 tmp_length= (size_t) (str-ptr);
3012 if (field_length < tmp_length) // Error in data
3013 val_ptr->length(0);
3014 else
3015 val_ptr->set_ascii((const char*) str, field_length-tmp_length);
3016 return val_ptr;
3017 }
3018
3019 /**
3020 Should be able to handle at least the following fixed decimal formats:
3021 5.00 , -1.0, 05, -05, +5 with optional pre/end space
3022 */
3023
cmp(const uchar * a_ptr,const uchar * b_ptr)3024 int Field_decimal::cmp(const uchar *a_ptr,const uchar *b_ptr)
3025 {
3026 const uchar *end;
3027 int swap=0;
3028 /* First remove prefixes '0', ' ', and '-' */
3029 for (end=a_ptr+field_length;
3030 a_ptr != end &&
3031 (*a_ptr == *b_ptr ||
3032 ((my_isspace(&my_charset_bin,*a_ptr) || *a_ptr == '+' ||
3033 *a_ptr == '0') &&
3034 (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' ||
3035 *b_ptr == '0')));
3036 a_ptr++,b_ptr++)
3037 {
3038 if (*a_ptr == '-') // If both numbers are negative
3039 swap= -1 ^ 1; // Swap result
3040 }
3041 if (a_ptr == end)
3042 return 0;
3043 if (*a_ptr == '-')
3044 return -1;
3045 if (*b_ptr == '-')
3046 return 1;
3047
3048 while (a_ptr != end)
3049 {
3050 if (*a_ptr++ != *b_ptr++)
3051 return swap ^ (a_ptr[-1] < b_ptr[-1] ? -1 : 1); // compare digits
3052 }
3053 return 0;
3054 }
3055
3056
sort_string(uchar * to,uint length)3057 void Field_decimal::sort_string(uchar *to,uint length)
3058 {
3059 uchar *str,*end;
3060 for (str=ptr,end=ptr+length;
3061 str != end &&
3062 ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
3063 *str == '0')) ;
3064 str++)
3065 *to++=' ';
3066 if (str == end)
3067 return; /* purecov: inspected */
3068
3069 if (*str == '-')
3070 {
3071 *to++=1; // Smaller than any number
3072 str++;
3073 while (str != end)
3074 if (my_isdigit(&my_charset_bin,*str))
3075 *to++= (char) ('9' - *str++);
3076 else
3077 *to++= *str++;
3078 }
3079 else memcpy(to,str,(uint) (end-str));
3080 }
3081
3082
sql_type(String & res) const3083 void Field_decimal::sql_type(String &res) const
3084 {
3085 CHARSET_INFO *cs=res.charset();
3086 uint tmp=field_length;
3087 if (!unsigned_flag)
3088 tmp--;
3089 if (dec)
3090 tmp--;
3091 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3092 "decimal(%d,%d)/*old*/",tmp,dec));
3093 add_zerofill_and_unsigned(res);
3094 }
3095
3096
make_new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type)3097 Field *Field_decimal::make_new_field(MEM_ROOT *root, TABLE *new_table,
3098 bool keep_type)
3099 {
3100 if (keep_type)
3101 return Field_real::make_new_field(root, new_table, keep_type);
3102
3103 Field *field= new (root) Field_new_decimal(NULL, field_length,
3104 maybe_null() ? (uchar*) "" : 0, 0,
3105 NONE, &field_name,
3106 dec, flags & ZEROFILL_FLAG,
3107 unsigned_flag);
3108 if (field)
3109 field->init_for_make_new_field(new_table, orig_table);
3110 return field;
3111 }
3112
3113
3114 /****************************************************************************
3115 ** Field_new_decimal
3116 ****************************************************************************/
3117
get_decimal_precision(uint len,uint8 dec,bool unsigned_val)3118 static uint get_decimal_precision(uint len, uint8 dec, bool unsigned_val)
3119 {
3120 uint precision= my_decimal_length_to_precision(len, dec, unsigned_val);
3121 return MY_MIN(precision, DECIMAL_MAX_PRECISION);
3122 }
3123
Field_new_decimal(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)3124 Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
3125 uint32 len_arg, uchar *null_ptr_arg,
3126 uchar null_bit_arg,
3127 enum utype unireg_check_arg,
3128 const LEX_CSTRING *field_name_arg,
3129 uint8 dec_arg,bool zero_arg,
3130 bool unsigned_arg)
3131 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3132 unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
3133 {
3134 precision= get_decimal_precision(len_arg, dec_arg, unsigned_arg);
3135 DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
3136 (dec <= DECIMAL_MAX_SCALE));
3137 bin_size= my_decimal_get_binary_size(precision, dec);
3138 }
3139
3140
reset(void)3141 int Field_new_decimal::reset(void)
3142 {
3143 store_value(&decimal_zero);
3144 return 0;
3145 }
3146
3147
3148 /**
3149 Generate max/min decimal value in case of overflow.
3150
3151 @param decimal_value buffer for value
3152 @param sign sign of value which caused overflow
3153 */
3154
set_value_on_overflow(my_decimal * decimal_value,bool sign)3155 void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
3156 bool sign)
3157 {
3158 DBUG_ENTER("Field_new_decimal::set_value_on_overflow");
3159 max_my_decimal(decimal_value, precision, decimals());
3160 if (sign)
3161 {
3162 if (unsigned_flag)
3163 my_decimal_set_zero(decimal_value);
3164 else
3165 decimal_value->sign(TRUE);
3166 }
3167 DBUG_VOID_RETURN;
3168 }
3169
3170
3171 /**
3172 Store decimal value in the binary buffer.
3173
3174 Checks if decimal_value fits into field size.
3175 If it does, stores the decimal in the buffer using binary format.
3176 Otherwise sets maximal number that can be stored in the field.
3177
3178 @param decimal_value my_decimal
3179 @param [OUT] native_error the error returned by my_decimal2binary().
3180
3181 @retval
3182 0 ok
3183 @retval
3184 1 error
3185 */
3186
store_value(const my_decimal * decimal_value,int * native_error)3187 bool Field_new_decimal::store_value(const my_decimal *decimal_value,
3188 int *native_error)
3189 {
3190 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3191 int error= 0;
3192 DBUG_ENTER("Field_new_decimal::store_value");
3193 #ifndef DBUG_OFF
3194 {
3195 char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
3196 DBUG_PRINT("enter", ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
3197 }
3198 #endif
3199
3200 /* check that we do not try to write negative value in unsigned field */
3201 if (unsigned_flag && decimal_value->sign())
3202 {
3203 DBUG_PRINT("info", ("unsigned overflow"));
3204 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3205 error= 1;
3206 decimal_value= &decimal_zero;
3207 }
3208 #ifndef DBUG_OFF
3209 {
3210 char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
3211 DBUG_PRINT("info", ("saving with precision %d scale: %d value %s",
3212 (int)precision, (int)dec,
3213 dbug_decimal_as_string(dbug_buff, decimal_value)));
3214 }
3215 #endif
3216
3217 *native_error= my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
3218 decimal_value, ptr, precision, dec);
3219
3220 if (unlikely(*native_error == E_DEC_OVERFLOW))
3221 {
3222 my_decimal buff;
3223 DBUG_PRINT("info", ("overflow"));
3224 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3225 set_value_on_overflow(&buff, decimal_value->sign());
3226 my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
3227 error= 1;
3228 }
3229 DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
3230 bin_size););
3231 DBUG_RETURN(error);
3232 }
3233
3234
store_value(const my_decimal * decimal_value)3235 bool Field_new_decimal::store_value(const my_decimal *decimal_value)
3236 {
3237 int native_error;
3238 bool rc= store_value(decimal_value, &native_error);
3239 if (unlikely(!rc && native_error == E_DEC_TRUNCATED))
3240 set_note(WARN_DATA_TRUNCATED, 1);
3241 return rc;
3242 }
3243
3244
store(const char * from,size_t length,CHARSET_INFO * charset_arg)3245 int Field_new_decimal::store(const char *from, size_t length,
3246 CHARSET_INFO *charset_arg)
3247 {
3248 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3249 my_decimal decimal_value;
3250 THD *thd= get_thd();
3251 DBUG_ENTER("Field_new_decimal::store(char*)");
3252
3253 const char *end;
3254 int err= str2my_decimal(E_DEC_FATAL_ERROR &
3255 ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
3256 from, length, charset_arg,
3257 &decimal_value, &end);
3258
3259 if (err == E_DEC_OVERFLOW) // Too many digits (>81) in the integer part
3260 {
3261 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3262 if (!thd->abort_on_warning)
3263 {
3264 set_value_on_overflow(&decimal_value, decimal_value.sign());
3265 store_decimal(&decimal_value);
3266 }
3267 DBUG_RETURN(1);
3268 }
3269
3270 if (thd->count_cuted_fields > CHECK_FIELD_EXPRESSION)
3271 {
3272 if (check_edom_and_important_data_truncation("decimal",
3273 err && err != E_DEC_TRUNCATED,
3274 charset_arg,
3275 from, length, end))
3276 {
3277 if (!thd->abort_on_warning)
3278 {
3279 if (err && err != E_DEC_TRUNCATED)
3280 {
3281 /*
3282 If check_decimal() failed because of EDOM-alike error,
3283 (e.g. E_DEC_BAD_NUM), we have to initialize decimal_value to zero.
3284 Note: if check_decimal() failed because of truncation,
3285 decimal_value is already properly initialized.
3286 */
3287 my_decimal_set_zero(&decimal_value);
3288 /*
3289 TODO: check str2my_decimal() with HF. It seems to do
3290 decimal_make_zero() on fatal errors, so my_decimal_set_zero()
3291 is probably not needed here.
3292 */
3293 }
3294 store_decimal(&decimal_value);
3295 }
3296 DBUG_RETURN(1);
3297 }
3298 }
3299
3300 #ifndef DBUG_OFF
3301 char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
3302 DBUG_PRINT("enter", ("value: %s",
3303 dbug_decimal_as_string(dbug_buff, &decimal_value)));
3304 #endif
3305 int err2;
3306 if (store_value(&decimal_value, &err2))
3307 DBUG_RETURN(1);
3308
3309 /*
3310 E_DEC_TRUNCATED means minor truncation, a note should be enough:
3311 - in err: str2my_decimal() truncated '1e-1000000000000' to 0.0
3312 - in err2: store_value() truncated 1.123 to 1.12, e.g. for DECIMAL(10,2)
3313 Also, we send a note if a string had some trailing spaces: '1.12 '
3314 */
3315 if (thd->count_cuted_fields > CHECK_FIELD_EXPRESSION &&
3316 (err == E_DEC_TRUNCATED ||
3317 err2 == E_DEC_TRUNCATED ||
3318 end < from + length))
3319 set_note(WARN_DATA_TRUNCATED, 1);
3320 DBUG_RETURN(0);
3321 }
3322
3323
3324 /**
3325 @todo
3326 Fix following when double2my_decimal when double2decimal
3327 will return E_DEC_TRUNCATED always correctly
3328 */
3329
store(double nr)3330 int Field_new_decimal::store(double nr)
3331 {
3332 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3333 my_decimal decimal_value;
3334 int err;
3335 THD *thd= get_thd();
3336 DBUG_ENTER("Field_new_decimal::store(double)");
3337
3338 err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
3339 &decimal_value);
3340 if (err)
3341 {
3342 if (check_overflow(err))
3343 set_value_on_overflow(&decimal_value, decimal_value.sign());
3344 /* Only issue a warning if store_value doesn't issue an warning */
3345 thd->got_warning= 0;
3346 }
3347 if (store_value(&decimal_value))
3348 err= 1;
3349 else if (err && !thd->got_warning)
3350 err= warn_if_overflow(err);
3351 DBUG_RETURN(err);
3352 }
3353
3354
store(longlong nr,bool unsigned_val)3355 int Field_new_decimal::store(longlong nr, bool unsigned_val)
3356 {
3357 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3358 my_decimal decimal_value;
3359 int err;
3360
3361 if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
3362 nr, unsigned_val, &decimal_value)))
3363 {
3364 if (check_overflow(err))
3365 set_value_on_overflow(&decimal_value, decimal_value.sign());
3366 /* Only issue a warning if store_value doesn't issue an warning */
3367 get_thd()->got_warning= 0;
3368 }
3369 if (store_value(&decimal_value))
3370 err= 1;
3371 else if (err && !get_thd()->got_warning)
3372 err= warn_if_overflow(err);
3373 return err;
3374 }
3375
3376
store_decimal(const my_decimal * decimal_value)3377 int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
3378 {
3379 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3380 return store_value(decimal_value);
3381 }
3382
3383
store_time_dec(const MYSQL_TIME * ltime,uint dec_arg)3384 int Field_new_decimal::store_time_dec(const MYSQL_TIME *ltime, uint dec_arg)
3385 {
3386 my_decimal decimal_value;
3387 return store_value(date2my_decimal(ltime, &decimal_value));
3388 }
3389
3390
val_real(void)3391 double Field_new_decimal::val_real(void)
3392 {
3393 ASSERT_COLUMN_MARKED_FOR_READ;
3394 double dbl;
3395 my_decimal decimal_value;
3396 my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
3397 return dbl;
3398 }
3399
3400
val_int(void)3401 longlong Field_new_decimal::val_int(void)
3402 {
3403 ASSERT_COLUMN_MARKED_FOR_READ;
3404 longlong i;
3405 my_decimal decimal_value;
3406 my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
3407 unsigned_flag, &i);
3408 return i;
3409 }
3410
3411
val_uint(void)3412 ulonglong Field_new_decimal::val_uint(void)
3413 {
3414 ASSERT_COLUMN_MARKED_FOR_READ;
3415 longlong i;
3416 my_decimal decimal_value;
3417 my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), true, &i);
3418 return i;
3419 }
3420
3421
val_decimal(my_decimal * decimal_value)3422 my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
3423 {
3424 ASSERT_COLUMN_MARKED_FOR_READ;
3425 DBUG_ENTER("Field_new_decimal::val_decimal");
3426 binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
3427 precision, dec);
3428 DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
3429 bin_size););
3430 DBUG_RETURN(decimal_value);
3431 }
3432
3433
val_str(String * val_buffer,String * val_ptr)3434 String *Field_new_decimal::val_str(String *val_buffer,
3435 String *val_ptr __attribute__((unused)))
3436 {
3437 ASSERT_COLUMN_MARKED_FOR_READ;
3438 my_decimal decimal_value;
3439 uint fixed_precision= zerofill ? precision : 0;
3440 my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
3441 fixed_precision, dec, '0', val_buffer);
3442 val_buffer->set_charset(&my_charset_numeric);
3443 return val_buffer;
3444 }
3445
3446
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)3447 bool Field_new_decimal::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
3448 {
3449 my_decimal value;
3450 return decimal_to_datetime_with_warn(val_decimal(&value),
3451 ltime, fuzzydate, table->s,
3452 field_name.str);
3453 }
3454
3455
cmp(const uchar * a,const uchar * b)3456 int Field_new_decimal::cmp(const uchar *a,const uchar*b)
3457 {
3458 return memcmp(a, b, bin_size);
3459 }
3460
3461
sort_string(uchar * buff,uint length)3462 void Field_new_decimal::sort_string(uchar *buff, uint length)
3463 {
3464 memcpy(buff, ptr, length);
3465 }
3466
3467
sql_type(String & str) const3468 void Field_new_decimal::sql_type(String &str) const
3469 {
3470 CHARSET_INFO *cs= str.charset();
3471 str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
3472 "decimal(%d,%d)", precision, (int)dec));
3473 add_zerofill_and_unsigned(str);
3474 }
3475
3476
3477 /**
3478 Save the field metadata for new decimal fields.
3479
3480 Saves the precision in the first byte and decimals() in the second
3481 byte of the field metadata array at index of *metadata_ptr and
3482 *(metadata_ptr + 1).
3483
3484 @param metadata_ptr First byte of field metadata
3485
3486 @returns number of bytes written to metadata_ptr
3487 */
save_field_metadata(uchar * metadata_ptr)3488 int Field_new_decimal::save_field_metadata(uchar *metadata_ptr)
3489 {
3490 *metadata_ptr= precision;
3491 *(metadata_ptr + 1)= decimals();
3492 return 2;
3493 }
3494
3495
3496 /**
3497 Returns the number of bytes field uses in row-based replication
3498 row packed size.
3499
3500 This method is used in row-based replication to determine the number
3501 of bytes that the field consumes in the row record format. This is
3502 used to skip fields in the master that do not exist on the slave.
3503
3504 @param field_metadata Encoded size in field metadata
3505
3506 @returns The size of the field based on the field metadata.
3507 */
pack_length_from_metadata(uint field_metadata)3508 uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
3509 {
3510 uint const source_precision= (field_metadata >> 8U) & 0x00ff;
3511 uint const source_decimal= field_metadata & 0x00ff;
3512 uint const source_size= my_decimal_get_binary_size(source_precision,
3513 source_decimal);
3514 return (source_size);
3515 }
3516
3517
compatible_field_size(uint field_metadata,Relay_log_info *,uint16 mflags,int * order_var)3518 bool Field_new_decimal::compatible_field_size(uint field_metadata,
3519 Relay_log_info * __attribute__((unused)),
3520 uint16 mflags __attribute__((unused)),
3521 int *order_var)
3522 {
3523 uint const source_precision= (field_metadata >> 8U) & 0x00ff;
3524 uint const source_decimal= field_metadata & 0x00ff;
3525 int order= compare(source_precision, precision);
3526 *order_var= order != 0 ? order : compare(source_decimal, dec);
3527 return true;
3528 }
3529
3530
is_equal(Create_field * new_field)3531 uint Field_new_decimal::is_equal(Create_field *new_field)
3532 {
3533 return ((new_field->type_handler() == type_handler()) &&
3534 ((new_field->flags & UNSIGNED_FLAG) ==
3535 (uint) (flags & UNSIGNED_FLAG)) &&
3536 ((new_field->flags & AUTO_INCREMENT_FLAG) ==
3537 (uint) (flags & AUTO_INCREMENT_FLAG)) &&
3538 (new_field->length == max_display_length()) &&
3539 (new_field->decimals == dec));
3540 }
3541
3542
3543 /**
3544 Unpack a decimal field from row data.
3545
3546 This method is used to unpack a decimal or numeric field from a master
3547 whose size of the field is less than that of the slave.
3548
3549 @param to Destination of the data
3550 @param from Source of the data
3551 @param param_data Precision (upper) and decimal (lower) values
3552
3553 @return New pointer into memory based on from + length of the data
3554 */
3555 const uchar *
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)3556 Field_new_decimal::unpack(uchar* to, const uchar *from, const uchar *from_end,
3557 uint param_data)
3558 {
3559 if (param_data == 0)
3560 return Field::unpack(to, from, from_end, param_data);
3561
3562 uint from_precision= (param_data & 0xff00) >> 8U;
3563 uint from_decimal= param_data & 0x00ff;
3564 uint length=pack_length();
3565 uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
3566 uint len= (param_data && (from_pack_len < length)) ?
3567 from_pack_len : length;
3568 if ((from_pack_len && (from_pack_len < length)) ||
3569 (from_precision < precision) ||
3570 (from_decimal < decimals()))
3571 {
3572 /*
3573 If the master's data is smaller than the slave, we need to convert
3574 the binary to decimal then resize the decimal converting it back to
3575 a decimal and write that to the raw data buffer.
3576 */
3577 decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
3578 decimal_t dec_val;
3579 dec_val.len= from_precision;
3580 dec_val.buf= dec_buf;
3581 /*
3582 Note: bin2decimal does not change the length of the field. So it is
3583 just the first step the resizing operation. The second step does the
3584 resizing using the precision and decimals from the slave.
3585 */
3586 bin2decimal((uchar *)from, &dec_val, from_precision, from_decimal);
3587 decimal2bin(&dec_val, to, precision, decimals());
3588 }
3589 else
3590 {
3591 if (from + len > from_end)
3592 return 0; // Wrong data
3593 memcpy(to, from, len); // Sizes are the same, just copy the data.
3594 }
3595 return from+len;
3596 }
3597
3598
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)3599 Item *Field_new_decimal::get_equal_const_item(THD *thd, const Context &ctx,
3600 Item *const_item)
3601 {
3602 if (flags & ZEROFILL_FLAG)
3603 return Field_num::get_equal_zerofill_const_item(thd, ctx, const_item);
3604 switch (ctx.subst_constraint()) {
3605 case IDENTITY_SUBST:
3606 if (const_item->field_type() != MYSQL_TYPE_NEWDECIMAL ||
3607 const_item->decimal_scale() != decimals())
3608 {
3609 my_decimal *val, val_buffer, val_buffer2;
3610 if (!(val= const_item->val_decimal(&val_buffer)))
3611 {
3612 DBUG_ASSERT(0);
3613 return const_item;
3614 }
3615 /*
3616 Truncate or extend the decimal value to the scale of the field.
3617 See comments about truncation in the same place in
3618 Field_time::get_equal_const_item().
3619 */
3620 my_decimal_round(E_DEC_FATAL_ERROR, val, decimals(), true, &val_buffer2);
3621 return new (thd->mem_root) Item_decimal(thd, field_name.str,
3622 &val_buffer2,
3623 decimals(), field_length);
3624 }
3625 break;
3626 case ANY_SUBST:
3627 break;
3628 }
3629 return const_item;
3630 }
3631
3632
store_time_dec(const MYSQL_TIME * ltime,uint dec_arg)3633 int Field_int::store_time_dec(const MYSQL_TIME *ltime, uint dec_arg)
3634 {
3635 longlong v= TIME_to_ulonglong(ltime);
3636 if (ltime->neg == 0)
3637 return store(v, true);
3638 return store(-v, false);
3639 }
3640
3641
3642 /****************************************************************************
3643 ** tiny int
3644 ****************************************************************************/
3645
store(const char * from,size_t len,CHARSET_INFO * cs)3646 int Field_tiny::store(const char *from,size_t len,CHARSET_INFO *cs)
3647 {
3648 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3649 int error;
3650 longlong rnd;
3651
3652 error= get_int(cs, from, len, &rnd, 255, -128, 127);
3653 ptr[0]= unsigned_flag ? (char) (ulonglong) rnd : (char) rnd;
3654 return error;
3655 }
3656
3657
store(double nr)3658 int Field_tiny::store(double nr)
3659 {
3660 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3661 int error= 0;
3662 nr=rint(nr);
3663 if (unsigned_flag)
3664 {
3665 if (nr < 0.0)
3666 {
3667 *ptr=0;
3668 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3669 error= 1;
3670 }
3671 else if (nr > 255.0)
3672 {
3673 *ptr= (uchar) 255;
3674 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3675 error= 1;
3676 }
3677 else
3678 *ptr= (uchar) nr;
3679 }
3680 else
3681 {
3682 if (nr < -128.0)
3683 {
3684 *ptr= (uchar) -128;
3685 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3686 error= 1;
3687 }
3688 else if (nr > 127.0)
3689 {
3690 *ptr=127;
3691 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3692 error= 1;
3693 }
3694 else
3695 *ptr=(uchar) (int) nr;
3696 }
3697 return error;
3698 }
3699
3700
store(longlong nr,bool unsigned_val)3701 int Field_tiny::store(longlong nr, bool unsigned_val)
3702 {
3703 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3704 int error= 0;
3705
3706 if (unsigned_flag)
3707 {
3708 if (nr < 0 && !unsigned_val)
3709 {
3710 *ptr= 0;
3711 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3712 error= 1;
3713 }
3714 else if ((ulonglong) nr > (ulonglong) 255)
3715 {
3716 *ptr= (char) 255;
3717 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3718 error= 1;
3719 }
3720 else
3721 *ptr=(char) nr;
3722 }
3723 else
3724 {
3725 if (nr < 0 && unsigned_val)
3726 nr= 256; // Generate overflow
3727 if (nr < -128)
3728 {
3729 *ptr= (char) -128;
3730 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3731 error= 1;
3732 }
3733 else if (nr > 127)
3734 {
3735 *ptr=127;
3736 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3737 error= 1;
3738 }
3739 else
3740 *ptr=(char) nr;
3741 }
3742 return error;
3743 }
3744
3745
val_real(void)3746 double Field_tiny::val_real(void)
3747 {
3748 ASSERT_COLUMN_MARKED_FOR_READ;
3749 int tmp= unsigned_flag ? (int) ptr[0] :
3750 (int) ((signed char*) ptr)[0];
3751 return (double) tmp;
3752 }
3753
3754
val_int(void)3755 longlong Field_tiny::val_int(void)
3756 {
3757 ASSERT_COLUMN_MARKED_FOR_READ;
3758 int tmp= unsigned_flag ? (int) ptr[0] :
3759 (int) ((signed char*) ptr)[0];
3760 return (longlong) tmp;
3761 }
3762
3763
val_str(String * val_buffer,String * val_ptr)3764 String *Field_tiny::val_str(String *val_buffer,
3765 String *val_ptr __attribute__((unused)))
3766 {
3767 ASSERT_COLUMN_MARKED_FOR_READ;
3768 long nr= unsigned_flag ? (long) ptr[0] : (long) ((signed char*) ptr)[0];
3769 return val_str_from_long(val_buffer, 5, -10, nr);
3770 }
3771
send_binary(Protocol * protocol)3772 bool Field_tiny::send_binary(Protocol *protocol)
3773 {
3774 return protocol->store_tiny((longlong) (int8) ptr[0]);
3775 }
3776
cmp(const uchar * a_ptr,const uchar * b_ptr)3777 int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
3778 {
3779 signed char a,b;
3780 a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
3781 if (unsigned_flag)
3782 return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
3783 return (a < b) ? -1 : (a > b) ? 1 : 0;
3784 }
3785
sort_string(uchar * to,uint length)3786 void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
3787 {
3788 if (unsigned_flag)
3789 *to= *ptr;
3790 else
3791 to[0] = (char) (ptr[0] ^ (uchar) 128); /* Revers signbit */
3792 }
3793
sql_type(String & res) const3794 void Field_tiny::sql_type(String &res) const
3795 {
3796 CHARSET_INFO *cs=res.charset();
3797 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3798 "tinyint(%d)",(int) field_length));
3799 add_zerofill_and_unsigned(res);
3800 }
3801
3802 /****************************************************************************
3803 Field type short int (2 byte)
3804 ****************************************************************************/
3805
store(const char * from,size_t len,CHARSET_INFO * cs)3806 int Field_short::store(const char *from,size_t len,CHARSET_INFO *cs)
3807 {
3808 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3809 int store_tmp;
3810 int error;
3811 longlong rnd;
3812
3813 error= get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
3814 store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
3815 int2store(ptr, store_tmp);
3816 return error;
3817 }
3818
3819
store(double nr)3820 int Field_short::store(double nr)
3821 {
3822 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3823 int error= 0;
3824 int16 res;
3825 nr=rint(nr);
3826 if (unsigned_flag)
3827 {
3828 if (nr < 0)
3829 {
3830 res=0;
3831 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3832 error= 1;
3833 }
3834 else if (nr > (double) UINT_MAX16)
3835 {
3836 res=(int16) UINT_MAX16;
3837 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3838 error= 1;
3839 }
3840 else
3841 res=(int16) (uint16) nr;
3842 }
3843 else
3844 {
3845 if (nr < (double) INT_MIN16)
3846 {
3847 res=INT_MIN16;
3848 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3849 error= 1;
3850 }
3851 else if (nr > (double) INT_MAX16)
3852 {
3853 res=INT_MAX16;
3854 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3855 error= 1;
3856 }
3857 else
3858 res=(int16) (int) nr;
3859 }
3860 int2store(ptr,res);
3861 return error;
3862 }
3863
3864
store(longlong nr,bool unsigned_val)3865 int Field_short::store(longlong nr, bool unsigned_val)
3866 {
3867 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3868 int error= 0;
3869 int16 res;
3870
3871 if (unsigned_flag)
3872 {
3873 if (nr < 0L && !unsigned_val)
3874 {
3875 res=0;
3876 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3877 error= 1;
3878 }
3879 else if ((ulonglong) nr > (ulonglong) UINT_MAX16)
3880 {
3881 res=(int16) UINT_MAX16;
3882 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3883 error= 1;
3884 }
3885 else
3886 res=(int16) (uint16) nr;
3887 }
3888 else
3889 {
3890 if (nr < 0 && unsigned_val)
3891 nr= UINT_MAX16+1; // Generate overflow
3892
3893 if (nr < INT_MIN16)
3894 {
3895 res=INT_MIN16;
3896 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3897 error= 1;
3898 }
3899 else if (nr > (longlong) INT_MAX16)
3900 {
3901 res=INT_MAX16;
3902 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
3903 error= 1;
3904 }
3905 else
3906 res=(int16) nr;
3907 }
3908 int2store(ptr,res);
3909 return error;
3910 }
3911
3912
val_real(void)3913 double Field_short::val_real(void)
3914 {
3915 ASSERT_COLUMN_MARKED_FOR_READ;
3916 short j;
3917 j=sint2korr(ptr);
3918 return unsigned_flag ? (double) (unsigned short) j : (double) j;
3919 }
3920
val_int(void)3921 longlong Field_short::val_int(void)
3922 {
3923 ASSERT_COLUMN_MARKED_FOR_READ;
3924 short j;
3925 j=sint2korr(ptr);
3926 return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
3927 }
3928
3929
val_str(String * val_buffer,String * val_ptr)3930 String *Field_short::val_str(String *val_buffer,
3931 String *val_ptr __attribute__((unused)))
3932 {
3933 ASSERT_COLUMN_MARKED_FOR_READ;
3934 short j= sint2korr(ptr);
3935 long nr= unsigned_flag ? (long) (unsigned short) j : (long) j;
3936 return val_str_from_long(val_buffer, 7, -10, nr);
3937 }
3938
3939
send_binary(Protocol * protocol)3940 bool Field_short::send_binary(Protocol *protocol)
3941 {
3942 return protocol->store_short(Field_short::val_int());
3943 }
3944
3945
cmp(const uchar * a_ptr,const uchar * b_ptr)3946 int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
3947 {
3948 short a,b;
3949 a=sint2korr(a_ptr);
3950 b=sint2korr(b_ptr);
3951
3952 if (unsigned_flag)
3953 return ((unsigned short) a < (unsigned short) b) ? -1 :
3954 ((unsigned short) a > (unsigned short) b) ? 1 : 0;
3955 return (a < b) ? -1 : (a > b) ? 1 : 0;
3956 }
3957
sort_string(uchar * to,uint length)3958 void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
3959 {
3960 if (unsigned_flag)
3961 to[0] = ptr[1];
3962 else
3963 to[0] = (char) (ptr[1] ^ 128); /* Revers signbit */
3964 to[1] = ptr[0];
3965 }
3966
sql_type(String & res) const3967 void Field_short::sql_type(String &res) const
3968 {
3969 CHARSET_INFO *cs=res.charset();
3970 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3971 "smallint(%d)",(int) field_length));
3972 add_zerofill_and_unsigned(res);
3973 }
3974
3975
3976 /****************************************************************************
3977 Field type medium int (3 byte)
3978 ****************************************************************************/
3979
store(const char * from,size_t len,CHARSET_INFO * cs)3980 int Field_medium::store(const char *from,size_t len,CHARSET_INFO *cs)
3981 {
3982 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3983 int store_tmp;
3984 int error;
3985 longlong rnd;
3986
3987 error= get_int(cs, from, len, &rnd, UINT_MAX24, INT_MIN24, INT_MAX24);
3988 store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
3989 int3store(ptr, store_tmp);
3990 return error;
3991 }
3992
3993
store(double nr)3994 int Field_medium::store(double nr)
3995 {
3996 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
3997 int error= 0;
3998 nr=rint(nr);
3999 if (unsigned_flag)
4000 {
4001 if (nr < 0)
4002 {
4003 int3store(ptr,0);
4004 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4005 error= 1;
4006 }
4007 else if (nr >= (double) (long) (1L << 24))
4008 {
4009 uint32 tmp=(uint32) (1L << 24)-1L;
4010 int3store(ptr,tmp);
4011 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4012 error= 1;
4013 }
4014 else
4015 int3store(ptr,(uint32) nr);
4016 }
4017 else
4018 {
4019 if (nr < (double) INT_MIN24)
4020 {
4021 long tmp=(long) INT_MIN24;
4022 int3store(ptr,tmp);
4023 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4024 error= 1;
4025 }
4026 else if (nr > (double) INT_MAX24)
4027 {
4028 long tmp=(long) INT_MAX24;
4029 int3store(ptr,tmp);
4030 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4031 error= 1;
4032 }
4033 else
4034 int3store(ptr,(long) nr);
4035 }
4036 return error;
4037 }
4038
4039
store(longlong nr,bool unsigned_val)4040 int Field_medium::store(longlong nr, bool unsigned_val)
4041 {
4042 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4043 int error= 0;
4044
4045 if (unsigned_flag)
4046 {
4047 if (nr < 0 && !unsigned_val)
4048 {
4049 int3store(ptr,0);
4050 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4051 error= 1;
4052 }
4053 else if ((ulonglong) nr >= (ulonglong) (long) (1L << 24))
4054 {
4055 long tmp= (long) (1L << 24)-1L;
4056 int3store(ptr,tmp);
4057 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4058 error= 1;
4059 }
4060 else
4061 int3store(ptr,(uint32) nr);
4062 }
4063 else
4064 {
4065 if (nr < 0 && unsigned_val)
4066 nr= (ulonglong) (long) (1L << 24); // Generate overflow
4067
4068 if (nr < (longlong) INT_MIN24)
4069 {
4070 long tmp= (long) INT_MIN24;
4071 int3store(ptr,tmp);
4072 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4073 error= 1;
4074 }
4075 else if (nr > (longlong) INT_MAX24)
4076 {
4077 long tmp=(long) INT_MAX24;
4078 int3store(ptr,tmp);
4079 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4080 error= 1;
4081 }
4082 else
4083 int3store(ptr,(long) nr);
4084 }
4085 return error;
4086 }
4087
4088
val_real(void)4089 double Field_medium::val_real(void)
4090 {
4091 ASSERT_COLUMN_MARKED_FOR_READ;
4092 long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
4093 return (double) j;
4094 }
4095
4096
val_int(void)4097 longlong Field_medium::val_int(void)
4098 {
4099 ASSERT_COLUMN_MARKED_FOR_READ;
4100 long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
4101 return (longlong) j;
4102 }
4103
4104
val_str(String * val_buffer,String * val_ptr)4105 String *Field_medium::val_str(String *val_buffer,
4106 String *val_ptr __attribute__((unused)))
4107 {
4108 ASSERT_COLUMN_MARKED_FOR_READ;
4109 long nr= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
4110 return val_str_from_long(val_buffer, 10, -10, nr);
4111 }
4112
4113
val_str_from_long(String * val_buffer,uint max_char_length,int radix,long nr)4114 String *Field_int::val_str_from_long(String *val_buffer,
4115 uint max_char_length,
4116 int radix, long nr)
4117 {
4118 CHARSET_INFO *cs= &my_charset_numeric;
4119 uint length;
4120 uint mlength= MY_MAX(field_length + 1, max_char_length * cs->mbmaxlen);
4121 val_buffer->alloc(mlength);
4122 char *to=(char*) val_buffer->ptr();
4123 length= (uint) cs->cset->long10_to_str(cs, to, mlength, radix, nr);
4124 val_buffer->length(length);
4125 if (zerofill)
4126 prepend_zeros(val_buffer); /* purecov: inspected */
4127 val_buffer->set_charset(cs);
4128 return val_buffer;
4129 }
4130
4131
send_binary(Protocol * protocol)4132 bool Field_medium::send_binary(Protocol *protocol)
4133 {
4134 ASSERT_COLUMN_MARKED_FOR_READ;
4135 return protocol->store_long(Field_medium::val_int());
4136 }
4137
4138
cmp(const uchar * a_ptr,const uchar * b_ptr)4139 int Field_medium::cmp(const uchar *a_ptr, const uchar *b_ptr)
4140 {
4141 long a,b;
4142 if (unsigned_flag)
4143 {
4144 a=uint3korr(a_ptr);
4145 b=uint3korr(b_ptr);
4146 }
4147 else
4148 {
4149 a=sint3korr(a_ptr);
4150 b=sint3korr(b_ptr);
4151 }
4152 return (a < b) ? -1 : (a > b) ? 1 : 0;
4153 }
4154
sort_string(uchar * to,uint length)4155 void Field_medium::sort_string(uchar *to,uint length __attribute__((unused)))
4156 {
4157 if (unsigned_flag)
4158 to[0] = ptr[2];
4159 else
4160 to[0] = (uchar) (ptr[2] ^ 128); /* Revers signbit */
4161 to[1] = ptr[1];
4162 to[2] = ptr[0];
4163 }
4164
4165
sql_type(String & res) const4166 void Field_medium::sql_type(String &res) const
4167 {
4168 CHARSET_INFO *cs=res.charset();
4169 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4170 "mediumint(%d)",(int) field_length));
4171 add_zerofill_and_unsigned(res);
4172 }
4173
4174 /****************************************************************************
4175 ** long int
4176 ****************************************************************************/
4177
store(const char * from,size_t len,CHARSET_INFO * cs)4178 int Field_long::store(const char *from,size_t len,CHARSET_INFO *cs)
4179 {
4180 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4181 long store_tmp;
4182 int error;
4183 longlong rnd;
4184
4185 error= get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
4186 store_tmp= unsigned_flag ? (long) (ulonglong) rnd : (long) rnd;
4187 int4store(ptr, store_tmp);
4188 return error;
4189 }
4190
4191
store(double nr)4192 int Field_long::store(double nr)
4193 {
4194 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4195 int error= 0;
4196 int32 res;
4197 nr=rint(nr);
4198 if (unsigned_flag)
4199 {
4200 if (nr < 0)
4201 {
4202 res=0;
4203 error= 1;
4204 }
4205 else if (nr > (double) UINT_MAX32)
4206 {
4207 res= UINT_MAX32;
4208 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4209 error= 1;
4210 }
4211 else
4212 res=(int32) (ulong) nr;
4213 }
4214 else
4215 {
4216 if (nr < (double) INT_MIN32)
4217 {
4218 res=(int32) INT_MIN32;
4219 error= 1;
4220 }
4221 else if (nr > (double) INT_MAX32)
4222 {
4223 res=(int32) INT_MAX32;
4224 error= 1;
4225 }
4226 else
4227 res=(int32) (longlong) nr;
4228 }
4229 if (unlikely(error))
4230 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4231
4232 int4store(ptr,res);
4233 return error;
4234 }
4235
4236
store(longlong nr,bool unsigned_val)4237 int Field_long::store(longlong nr, bool unsigned_val)
4238 {
4239 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4240 int error= 0;
4241 int32 res;
4242
4243 if (unsigned_flag)
4244 {
4245 if (nr < 0 && !unsigned_val)
4246 {
4247 res=0;
4248 error= 1;
4249 }
4250 else if ((ulonglong) nr >= (1LL << 32))
4251 {
4252 res=(int32) (uint32) ~0L;
4253 error= 1;
4254 }
4255 else
4256 res=(int32) (uint32) nr;
4257 }
4258 else
4259 {
4260 if (nr < 0 && unsigned_val)
4261 nr= ((longlong) INT_MAX32) + 1; // Generate overflow
4262 if (nr < (longlong) INT_MIN32)
4263 {
4264 res=(int32) INT_MIN32;
4265 error= 1;
4266 }
4267 else if (nr > (longlong) INT_MAX32)
4268 {
4269 res=(int32) INT_MAX32;
4270 error= 1;
4271 }
4272 else
4273 res=(int32) nr;
4274 }
4275 if (unlikely(error))
4276 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4277
4278 int4store(ptr,res);
4279 return error;
4280 }
4281
4282
val_real(void)4283 double Field_long::val_real(void)
4284 {
4285 ASSERT_COLUMN_MARKED_FOR_READ;
4286 int32 j;
4287 j=sint4korr(ptr);
4288 return unsigned_flag ? (double) (uint32) j : (double) j;
4289 }
4290
val_int(void)4291 longlong Field_long::val_int(void)
4292 {
4293 ASSERT_COLUMN_MARKED_FOR_READ;
4294 int32 j;
4295 /* See the comment in Field_long::store(long long) */
4296 DBUG_ASSERT(!table || table->in_use == current_thd);
4297 j=sint4korr(ptr);
4298 return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
4299 }
4300
4301
val_str(String * val_buffer,String * val_ptr)4302 String *Field_long::val_str(String *val_buffer,
4303 String *val_ptr __attribute__((unused)))
4304 {
4305 ASSERT_COLUMN_MARKED_FOR_READ;
4306 long nr= unsigned_flag ? (long) uint4korr(ptr) : sint4korr(ptr);
4307 return val_str_from_long(val_buffer, 12, unsigned_flag ? 10 : -10, nr);
4308 }
4309
4310
send_binary(Protocol * protocol)4311 bool Field_long::send_binary(Protocol *protocol)
4312 {
4313 ASSERT_COLUMN_MARKED_FOR_READ;
4314 return protocol->store_long(Field_long::val_int());
4315 }
4316
cmp(const uchar * a_ptr,const uchar * b_ptr)4317 int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
4318 {
4319 int32 a,b;
4320 a=sint4korr(a_ptr);
4321 b=sint4korr(b_ptr);
4322 if (unsigned_flag)
4323 return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
4324 return (a < b) ? -1 : (a > b) ? 1 : 0;
4325 }
4326
sort_string(uchar * to,uint length)4327 void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
4328 {
4329 if (unsigned_flag)
4330 to[0] = ptr[3];
4331 else
4332 to[0] = (char) (ptr[3] ^ 128); /* Revers signbit */
4333 to[1] = ptr[2];
4334 to[2] = ptr[1];
4335 to[3] = ptr[0];
4336 }
4337
4338
sql_type(String & res) const4339 void Field_long::sql_type(String &res) const
4340 {
4341 CHARSET_INFO *cs=res.charset();
4342 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4343 "int(%d)",(int) field_length));
4344 add_zerofill_and_unsigned(res);
4345 }
4346
4347 /****************************************************************************
4348 Field type longlong int (8 bytes)
4349 ****************************************************************************/
4350
store(const char * from,size_t len,CHARSET_INFO * cs)4351 int Field_longlong::store(const char *from,size_t len,CHARSET_INFO *cs)
4352 {
4353 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4354 int error= 0;
4355 char *end;
4356 ulonglong tmp;
4357
4358 tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
4359 if (unlikely(error == MY_ERRNO_ERANGE))
4360 {
4361 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4362 error= 1;
4363 }
4364 else if (get_thd()->count_cuted_fields > CHECK_FIELD_EXPRESSION &&
4365 check_int(cs, from, len, end, error))
4366 error= 1;
4367 else
4368 error= 0;
4369 int8store(ptr,tmp);
4370 return error;
4371 }
4372
4373
store(double nr)4374 int Field_longlong::store(double nr)
4375 {
4376 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4377 Converter_double_to_longlong conv(nr, unsigned_flag);
4378
4379 if (unlikely(conv.error()))
4380 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4381
4382 int8store(ptr, conv.result());
4383 return conv.error();
4384 }
4385
4386
store(longlong nr,bool unsigned_val)4387 int Field_longlong::store(longlong nr, bool unsigned_val)
4388 {
4389 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4390 int error= 0;
4391
4392 if (unlikely(nr < 0)) // Only possible error
4393 {
4394 /*
4395 if field is unsigned and value is signed (< 0) or
4396 if field is signed and value is unsigned we have an overflow
4397 */
4398 if (unsigned_flag != unsigned_val)
4399 {
4400 nr= unsigned_flag ? (ulonglong) 0 : (ulonglong) LONGLONG_MAX;
4401 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4402 error= 1;
4403 }
4404 }
4405
4406 int8store(ptr,nr);
4407 return error;
4408 }
4409
4410
val_real(void)4411 double Field_longlong::val_real(void)
4412 {
4413 ASSERT_COLUMN_MARKED_FOR_READ;
4414 longlong j;
4415 j=sint8korr(ptr);
4416 /* The following is open coded to avoid a bug in gcc 3.3 */
4417 if (unsigned_flag)
4418 {
4419 ulonglong tmp= (ulonglong) j;
4420 return ulonglong2double(tmp);
4421 }
4422 return (double) j;
4423 }
4424
4425
val_int(void)4426 longlong Field_longlong::val_int(void)
4427 {
4428 ASSERT_COLUMN_MARKED_FOR_READ;
4429 longlong j;
4430 j=sint8korr(ptr);
4431 return j;
4432 }
4433
4434
val_str(String * val_buffer,String * val_ptr)4435 String *Field_longlong::val_str(String *val_buffer,
4436 String *val_ptr __attribute__((unused)))
4437 {
4438 CHARSET_INFO *cs= &my_charset_numeric;
4439 uint length;
4440 uint mlength=MY_MAX(field_length+1,22*cs->mbmaxlen);
4441 val_buffer->alloc(mlength);
4442 char *to=(char*) val_buffer->ptr();
4443 longlong j;
4444 j=sint8korr(ptr);
4445
4446 length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
4447 unsigned_flag ? 10 : -10, j);
4448 val_buffer->length(length);
4449 if (zerofill)
4450 prepend_zeros(val_buffer);
4451 val_buffer->set_charset(cs);
4452 return val_buffer;
4453 }
4454
4455
send_binary(Protocol * protocol)4456 bool Field_longlong::send_binary(Protocol *protocol)
4457 {
4458 ASSERT_COLUMN_MARKED_FOR_READ;
4459 return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
4460 }
4461
4462
cmp(const uchar * a_ptr,const uchar * b_ptr)4463 int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr)
4464 {
4465 longlong a,b;
4466 a=sint8korr(a_ptr);
4467 b=sint8korr(b_ptr);
4468 if (unsigned_flag)
4469 return ((ulonglong) a < (ulonglong) b) ? -1 :
4470 ((ulonglong) a > (ulonglong) b) ? 1 : 0;
4471 return (a < b) ? -1 : (a > b) ? 1 : 0;
4472 }
4473
sort_string(uchar * to,uint length)4474 void Field_longlong::sort_string(uchar *to,uint length __attribute__((unused)))
4475 {
4476 if (unsigned_flag)
4477 to[0] = ptr[7];
4478 else
4479 to[0] = (char) (ptr[7] ^ 128); /* Revers signbit */
4480 to[1] = ptr[6];
4481 to[2] = ptr[5];
4482 to[3] = ptr[4];
4483 to[4] = ptr[3];
4484 to[5] = ptr[2];
4485 to[6] = ptr[1];
4486 to[7] = ptr[0];
4487 }
4488
4489
sql_type(String & res) const4490 void Field_longlong::sql_type(String &res) const
4491 {
4492 CHARSET_INFO *cs=res.charset();
4493 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4494 "bigint(%d)",(int) field_length));
4495 add_zerofill_and_unsigned(res);
4496 }
4497
set_max()4498 void Field_longlong::set_max()
4499 {
4500 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4501 set_notnull();
4502 int8store(ptr, unsigned_flag ? ULONGLONG_MAX : LONGLONG_MAX);
4503 }
4504
is_max()4505 bool Field_longlong::is_max()
4506 {
4507 ASSERT_COLUMN_MARKED_FOR_READ;
4508 if (unsigned_flag)
4509 {
4510 ulonglong j;
4511 j= uint8korr(ptr);
4512 return j == ULONGLONG_MAX;
4513 }
4514 longlong j;
4515 j= sint8korr(ptr);
4516 return j == LONGLONG_MAX;
4517 }
4518
4519 /*
4520 Floating-point numbers
4521 */
4522
4523 /****************************************************************************
4524 single precision float
4525 ****************************************************************************/
4526
store(const char * from,size_t len,CHARSET_INFO * cs)4527 int Field_float::store(const char *from,size_t len,CHARSET_INFO *cs)
4528 {
4529 int error;
4530 Field_float::store(get_double(from, len, cs, &error));
4531 return error;
4532 }
4533
4534
store(double nr)4535 int Field_float::store(double nr)
4536 {
4537 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4538 int error= truncate_double(&nr, field_length,
4539 not_fixed ? NOT_FIXED_DEC : dec,
4540 unsigned_flag, FLT_MAX);
4541 if (unlikely(error))
4542 {
4543 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4544 if (error < 0) // Wrong double value
4545 {
4546 error= 1;
4547 set_null();
4548 }
4549 }
4550 float j= (float)nr;
4551
4552 float4store(ptr,j);
4553 return error;
4554 }
4555
4556
store(longlong nr,bool unsigned_val)4557 int Field_float::store(longlong nr, bool unsigned_val)
4558 {
4559 return Field_float::store(unsigned_val ? ulonglong2double((ulonglong) nr) :
4560 (double) nr);
4561 }
4562
4563
val_real(void)4564 double Field_float::val_real(void)
4565 {
4566 ASSERT_COLUMN_MARKED_FOR_READ;
4567 float j;
4568 float4get(j,ptr);
4569 return ((double) j);
4570 }
4571
val_int(void)4572 longlong Field_float::val_int(void)
4573 {
4574 float j;
4575 float4get(j,ptr);
4576 return Converter_double_to_longlong(j, false).result();
4577 }
4578
4579
val_str(String * val_buffer,String * val_ptr)4580 String *Field_float::val_str(String *val_buffer,
4581 String *val_ptr __attribute__((unused)))
4582 {
4583 ASSERT_COLUMN_MARKED_FOR_READ;
4584 DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4585
4586 if (Float(ptr).to_string(val_buffer, dec))
4587 {
4588 my_error(ER_OUT_OF_RESOURCES, MYF(0));
4589 return val_buffer;
4590 }
4591
4592 if (zerofill)
4593 prepend_zeros(val_buffer);
4594 return val_buffer;
4595 }
4596
4597
cmp(const uchar * a_ptr,const uchar * b_ptr)4598 int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
4599 {
4600 float a,b;
4601 float4get(a,a_ptr);
4602 float4get(b,b_ptr);
4603 return (a < b) ? -1 : (a > b) ? 1 : 0;
4604 }
4605
4606 #define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)
4607
sort_string(uchar * to,uint length)4608 void Field_float::sort_string(uchar *to,uint length __attribute__((unused)))
4609 {
4610 float nr;
4611 float4get(nr,ptr);
4612
4613 uchar *tmp= to;
4614 if (nr == (float) 0.0)
4615 { /* Change to zero string */
4616 tmp[0]=(uchar) 128;
4617 bzero((char*) tmp+1,sizeof(nr)-1);
4618 }
4619 else
4620 {
4621 #ifdef WORDS_BIGENDIAN
4622 memcpy(tmp, &nr, sizeof(nr));
4623 #else
4624 tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
4625 #endif
4626 if (tmp[0] & 128) /* Negative */
4627 { /* make complement */
4628 uint i;
4629 for (i=0 ; i < sizeof(nr); i++)
4630 tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
4631 }
4632 else
4633 {
4634 ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
4635 (ushort) 32768);
4636 exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
4637 tmp[0]= (uchar) (exp_part >> 8);
4638 tmp[1]= (uchar) exp_part;
4639 }
4640 }
4641 }
4642
4643
send_binary(Protocol * protocol)4644 bool Field_float::send_binary(Protocol *protocol)
4645 {
4646 ASSERT_COLUMN_MARKED_FOR_READ;
4647 return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
4648 }
4649
4650
4651 /**
4652 Save the field metadata for float fields.
4653
4654 Saves the pack length in the first byte.
4655
4656 @param metadata_ptr First byte of field metadata
4657
4658 @returns number of bytes written to metadata_ptr
4659 */
save_field_metadata(uchar * metadata_ptr)4660 int Field_float::save_field_metadata(uchar *metadata_ptr)
4661 {
4662 *metadata_ptr= pack_length();
4663 return 1;
4664 }
4665
4666
sql_type(String & res) const4667 void Field_float::sql_type(String &res) const
4668 {
4669 if (dec >= FLOATING_POINT_DECIMALS)
4670 {
4671 res.set_ascii(STRING_WITH_LEN("float"));
4672 }
4673 else
4674 {
4675 CHARSET_INFO *cs= res.charset();
4676 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4677 "float(%d,%d)",(int) field_length,dec));
4678 }
4679 add_zerofill_and_unsigned(res);
4680 }
4681
4682
4683 /****************************************************************************
4684 double precision floating point numbers
4685 ****************************************************************************/
4686
store(const char * from,size_t len,CHARSET_INFO * cs)4687 int Field_double::store(const char *from,size_t len,CHARSET_INFO *cs)
4688 {
4689 int error;
4690 Field_double::store(get_double(from, len, cs, &error));
4691 return error;
4692 }
4693
4694
store(double nr)4695 int Field_double::store(double nr)
4696 {
4697 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
4698 int error= truncate_double(&nr, field_length,
4699 not_fixed ? NOT_FIXED_DEC : dec,
4700 unsigned_flag, DBL_MAX);
4701 if (unlikely(error))
4702 {
4703 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
4704 if (error < 0) // Wrong double value
4705 {
4706 error= 1;
4707 set_null();
4708 }
4709 }
4710
4711 float8store(ptr,nr);
4712 return error;
4713 }
4714
4715
store(longlong nr,bool unsigned_val)4716 int Field_double::store(longlong nr, bool unsigned_val)
4717 {
4718 return Field_double::store(unsigned_val ? ulonglong2double((ulonglong) nr) :
4719 (double) nr);
4720 }
4721
4722 /*
4723 If a field has fixed length, truncate the double argument pointed to by 'nr'
4724 appropriately.
4725 Also ensure that the argument is within [-max_value; max_value] range.
4726
4727 return
4728 0 ok
4729 -1 Illegal double value
4730 1 Value was truncated
4731 */
4732
truncate_double(double * nr,uint field_length,uint dec,bool unsigned_flag,double max_value)4733 int truncate_double(double *nr, uint field_length, uint dec,
4734 bool unsigned_flag, double max_value)
4735 {
4736 int error= 0;
4737 double res= *nr;
4738
4739 if (std::isnan(res))
4740 {
4741 *nr= 0;
4742 return -1;
4743 }
4744 else if (unsigned_flag && res < 0)
4745 {
4746 *nr= 0;
4747 return 1;
4748 }
4749
4750 if (dec < FLOATING_POINT_DECIMALS)
4751 {
4752 uint order= field_length - dec;
4753 uint step= array_elements(log_10) - 1;
4754 double max_value_by_dec= 1.0;
4755 for (; order > step; order-= step)
4756 max_value_by_dec*= log_10[step];
4757 max_value_by_dec*= log_10[order];
4758 max_value_by_dec-= 1.0 / log_10[dec];
4759 set_if_smaller(max_value, max_value_by_dec);
4760
4761 /* Check for infinity so we don't get NaN in calculations */
4762 if (!std::isinf(res))
4763 {
4764 double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
4765 res= floor(res) + tmp;
4766 }
4767 }
4768
4769 if (res < -max_value)
4770 {
4771 res= -max_value;
4772 error= 1;
4773 }
4774 else if (res > max_value)
4775 {
4776 res= max_value;
4777 error= 1;
4778 }
4779
4780 *nr= res;
4781 return error;
4782 }
4783
4784 /*
4785 Convert double to longlong / ulonglong.
4786 If double is outside of the supported range,
4787 adjust m_result and set m_error.
4788
4789 @param nr Number to convert
4790 @param unsigned_flag true if result is unsigned
4791 */
4792
4793 Value_source::
Converter_double_to_longlong(double nr,bool unsigned_flag)4794 Converter_double_to_longlong::Converter_double_to_longlong(double nr,
4795 bool unsigned_flag)
4796 :m_error(false)
4797 {
4798 nr= rint(nr);
4799 if (unsigned_flag)
4800 {
4801 if (nr < 0)
4802 {
4803 m_result= 0;
4804 m_error= true;
4805 }
4806 else if (nr >= (double) ULONGLONG_MAX)
4807 {
4808 m_result= ~(longlong) 0;
4809 m_error= true;
4810 }
4811 else
4812 m_result= (longlong) double2ulonglong(nr);
4813 }
4814 else
4815 {
4816 if (nr <= (double) LONGLONG_MIN)
4817 {
4818 m_result= LONGLONG_MIN;
4819 m_error= (nr < (double) LONGLONG_MIN);
4820 }
4821 else if (nr >= (double) (ulonglong) LONGLONG_MAX)
4822 {
4823 m_result= LONGLONG_MAX;
4824 m_error= (nr > (double) LONGLONG_MAX);
4825 }
4826 else
4827 m_result= (longlong) nr;
4828 }
4829 }
4830
4831
4832 void Value_source::
push_warning(THD * thd,double nr,bool unsigned_flag)4833 Converter_double_to_longlong::push_warning(THD *thd,
4834 double nr,
4835 bool unsigned_flag)
4836 {
4837 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
4838 ER_DATA_OVERFLOW, ER_THD(thd, ER_DATA_OVERFLOW),
4839 ErrConvDouble(nr).ptr(),
4840 unsigned_flag ? "UNSIGNED INT" : "INT");
4841 }
4842
4843
store_decimal(const my_decimal * dm)4844 int Field_real::store_decimal(const my_decimal *dm)
4845 {
4846 double dbl;
4847 my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
4848 return store(dbl);
4849 }
4850
store_time_dec(const MYSQL_TIME * ltime,uint dec_arg)4851 int Field_real::store_time_dec(const MYSQL_TIME *ltime, uint dec_arg)
4852 {
4853 return store(TIME_to_double(ltime));
4854 }
4855
4856
val_real(void)4857 double Field_double::val_real(void)
4858 {
4859 ASSERT_COLUMN_MARKED_FOR_READ;
4860 double j;
4861 float8get(j,ptr);
4862 return j;
4863 }
4864
4865
val_int_from_real(bool want_unsigned_result)4866 longlong Field_double::val_int_from_real(bool want_unsigned_result)
4867 {
4868 Converter_double_to_longlong conv(val_real(), want_unsigned_result);
4869 if (unlikely(!want_unsigned_result && conv.error()))
4870 conv.push_warning(get_thd(), Field_double::val_real(), false);
4871 return conv.result();
4872 }
4873
4874
val_decimal(my_decimal * decimal_value)4875 my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
4876 {
4877 ASSERT_COLUMN_MARKED_FOR_READ;
4878 double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
4879 return decimal_value;
4880 }
4881
4882
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)4883 bool Field_real::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
4884 {
4885 ASSERT_COLUMN_MARKED_FOR_READ;
4886 double nr= val_real();
4887 return double_to_datetime_with_warn(nr, ltime, fuzzydate,
4888 table->s, field_name.str);
4889 }
4890
4891
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)4892 Item *Field_real::get_equal_const_item(THD *thd, const Context &ctx,
4893 Item *const_item)
4894 {
4895 if (flags & ZEROFILL_FLAG)
4896 return Field_num::get_equal_zerofill_const_item(thd, ctx, const_item);
4897 switch (ctx.subst_constraint()) {
4898 case IDENTITY_SUBST:
4899 if (const_item->decimal_scale() != Field_real::decimals())
4900 {
4901 double val= const_item->val_real();
4902 return new (thd->mem_root) Item_float(thd, val, Field_real::decimals());
4903 }
4904 break;
4905 case ANY_SUBST:
4906 break;
4907 }
4908 return const_item;
4909 }
4910
4911
val_str(String * val_buffer,String * val_ptr)4912 String *Field_double::val_str(String *val_buffer,
4913 String *val_ptr __attribute__((unused)))
4914 {
4915 ASSERT_COLUMN_MARKED_FOR_READ;
4916 DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4917 double nr;
4918 float8get(nr,ptr);
4919
4920 uint to_length= DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE;
4921 if (val_buffer->alloc(to_length))
4922 {
4923 my_error(ER_OUT_OF_RESOURCES, MYF(0));
4924 return val_buffer;
4925 }
4926
4927 char *to=(char*) val_buffer->ptr();
4928 size_t len;
4929
4930 if (dec >= FLOATING_POINT_DECIMALS)
4931 len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
4932 else
4933 len= my_fcvt(nr, dec, to, NULL);
4934
4935 val_buffer->length((uint) len);
4936 if (zerofill)
4937 prepend_zeros(val_buffer);
4938 val_buffer->set_charset(&my_charset_numeric);
4939 return val_buffer;
4940 }
4941
send_binary(Protocol * protocol)4942 bool Field_double::send_binary(Protocol *protocol)
4943 {
4944 return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
4945 }
4946
4947
cmp(const uchar * a_ptr,const uchar * b_ptr)4948 int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
4949 {
4950 double a,b;
4951 float8get(a,a_ptr);
4952 float8get(b,b_ptr);
4953 return (a < b) ? -1 : (a > b) ? 1 : 0;
4954 }
4955
4956
4957 #define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
4958
4959 /* The following should work for IEEE */
4960
sort_string(uchar * to,uint length)4961 void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
4962 {
4963 double nr;
4964 float8get(nr,ptr);
4965 change_double_for_sort(nr, to);
4966 }
4967
4968
4969 /**
4970 Save the field metadata for double fields.
4971
4972 Saves the pack length in the first byte of the field metadata array
4973 at index of *metadata_ptr.
4974
4975 @param metadata_ptr First byte of field metadata
4976
4977 @returns number of bytes written to metadata_ptr
4978 */
save_field_metadata(uchar * metadata_ptr)4979 int Field_double::save_field_metadata(uchar *metadata_ptr)
4980 {
4981 *metadata_ptr= pack_length();
4982 return 1;
4983 }
4984
4985
sql_type(String & res) const4986 void Field_double::sql_type(String &res) const
4987 {
4988 CHARSET_INFO *cs=res.charset();
4989 if (dec >= FLOATING_POINT_DECIMALS)
4990 {
4991 res.set_ascii(STRING_WITH_LEN("double"));
4992 }
4993 else
4994 {
4995 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4996 "double(%d,%d)",(int) field_length,dec));
4997 }
4998 add_zerofill_and_unsigned(res);
4999 }
5000
5001
5002 /**
5003 TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
5004 2038-01-01 00:00:00 UTC stored as number of seconds since Unix
5005 Epoch in UTC.
5006
5007 Actually SQL-99 says that we should allow niladic functions (like NOW())
5008 as defaults for any field. The current limitation (only NOW() and only
5009 for TIMESTAMP and DATETIME fields) are because of restricted binary .frm
5010 format and should go away in the future.
5011
5012 Also because of this limitation of binary .frm format we use 5 different
5013 unireg_check values with TIMESTAMP field to distinguish various cases of
5014 DEFAULT or ON UPDATE values. These values are:
5015
5016 TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
5017 auto-set-on-update (or now() as default) in this table before, then this
5018 field has NOW() as default and is updated when row changes, else it is
5019 field which has 0 as default value and is not automatically updated.
5020 TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
5021 automatically (TIMESTAMP DEFAULT NOW()), not used in Field since 10.2.2
5022 TIMESTAMP_UN_FIELD - field which is set on update automatically but has not
5023 NOW() as default (but it may has 0 or some other const timestamp as
5024 default) (TIMESTAMP ON UPDATE NOW()).
5025 TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on
5026 update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW()), not used in Field since 10.2.2
5027 NONE - field which is not auto-set on update with some other than NOW()
5028 default value (TIMESTAMP DEFAULT 0).
5029
5030 Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are
5031 left only for preserving ability to read old tables. Such fields replaced
5032 with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is
5033 because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for
5034 "TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such
5035 specification too but ignored default value for first timestamp, which of
5036 course is non-standard.) In most cases user won't notice any change, only
5037 exception is different behavior of old/new timestamps during ALTER TABLE.
5038 */
5039
Field_timestamp(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share)5040 Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg,
5041 uchar *null_ptr_arg, uchar null_bit_arg,
5042 enum utype unireg_check_arg,
5043 const LEX_CSTRING *field_name_arg,
5044 TABLE_SHARE *share)
5045 :Field_temporal(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
5046 unireg_check_arg, field_name_arg)
5047 {
5048 /* For 4.0 MYD and 4.0 InnoDB compatibility */
5049 flags|= UNSIGNED_FLAG;
5050 if (unireg_check != NONE)
5051 {
5052 /*
5053 We mark the flag with TIMESTAMP_FLAG to indicate to the client that
5054 this field will be automatically updated on insert.
5055 */
5056 flags|= TIMESTAMP_FLAG;
5057 if (unireg_check != TIMESTAMP_DN_FIELD)
5058 flags|= ON_UPDATE_NOW_FLAG;
5059 }
5060 }
5061
5062
save_in_field(Field * to)5063 int Field_timestamp::save_in_field(Field *to)
5064 {
5065 ulong sec_part;
5066 my_time_t ts= get_timestamp(&sec_part);
5067 return to->store_timestamp(ts, sec_part);
5068 }
5069
get_timestamp(const uchar * pos,ulong * sec_part) const5070 my_time_t Field_timestamp::get_timestamp(const uchar *pos,
5071 ulong *sec_part) const
5072 {
5073 ASSERT_COLUMN_MARKED_FOR_READ;
5074 *sec_part= 0;
5075 return sint4korr(pos);
5076 }
5077
5078
store_TIME_with_warning(THD * thd,MYSQL_TIME * l_time,const ErrConv * str,int was_cut,bool have_smth_to_conv)5079 int Field_timestamp::store_TIME_with_warning(THD *thd, MYSQL_TIME *l_time,
5080 const ErrConv *str,
5081 int was_cut,
5082 bool have_smth_to_conv)
5083 {
5084 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
5085 uint error = 0;
5086 my_time_t timestamp;
5087
5088 if (MYSQL_TIME_WARN_HAVE_WARNINGS(was_cut) || !have_smth_to_conv)
5089 {
5090 error= 1;
5091 set_datetime_warning(WARN_DATA_TRUNCATED,
5092 str, MYSQL_TIMESTAMP_DATETIME, 1);
5093 }
5094 else if (MYSQL_TIME_WARN_HAVE_NOTES(was_cut))
5095 {
5096 error= 3;
5097 set_datetime_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED,
5098 str, MYSQL_TIMESTAMP_DATETIME, 1);
5099 }
5100 /* Only convert a correct date (not a zero date) */
5101 if (have_smth_to_conv && l_time->month)
5102 {
5103 uint conversion_error;
5104 timestamp= TIME_to_timestamp(thd, l_time, &conversion_error);
5105 if (timestamp == 0 && l_time->second_part == 0)
5106 conversion_error= ER_WARN_DATA_OUT_OF_RANGE;
5107 if (unlikely(conversion_error))
5108 {
5109 set_datetime_warning(conversion_error,
5110 str, MYSQL_TIMESTAMP_DATETIME, !error);
5111 error= 1;
5112 }
5113 }
5114 else
5115 {
5116 timestamp= 0;
5117 l_time->second_part= 0;
5118 }
5119 store_TIME(timestamp, l_time->second_part);
5120 return error;
5121 }
5122
5123
5124 static bool
copy_or_convert_to_datetime(THD * thd,const MYSQL_TIME * from,MYSQL_TIME * to)5125 copy_or_convert_to_datetime(THD *thd, const MYSQL_TIME *from, MYSQL_TIME *to)
5126 {
5127 if (from->time_type == MYSQL_TIMESTAMP_TIME)
5128 return time_to_datetime(thd, from, to);
5129 *to= *from;
5130 return false;
5131 }
5132
5133
sql_mode_for_timestamp(THD * thd) const5134 sql_mode_t Field_timestamp::sql_mode_for_timestamp(THD *thd) const
5135 {
5136 // We don't want to store invalid or fuzzy datetime values in TIMESTAMP
5137 return (thd->variables.sql_mode & MODE_NO_ZERO_DATE) | MODE_NO_ZERO_IN_DATE;
5138 }
5139
5140
store_time_dec(const MYSQL_TIME * ltime,uint dec)5141 int Field_timestamp::store_time_dec(const MYSQL_TIME *ltime, uint dec)
5142 {
5143 int unused;
5144 ErrConvTime str(ltime);
5145 THD *thd= get_thd();
5146 MYSQL_TIME l_time;
5147 bool valid= !copy_or_convert_to_datetime(thd, ltime, &l_time) &&
5148 !check_date(&l_time, pack_time(&l_time) != 0,
5149 sql_mode_for_timestamp(thd), &unused);
5150 return store_TIME_with_warning(thd, &l_time, &str, false, valid);
5151 }
5152
5153
store(const char * from,size_t len,CHARSET_INFO * cs)5154 int Field_timestamp::store(const char *from,size_t len,CHARSET_INFO *cs)
5155 {
5156 MYSQL_TIME l_time;
5157 MYSQL_TIME_STATUS status;
5158 bool have_smth_to_conv;
5159 ErrConvString str(from, len, cs);
5160 THD *thd= get_thd();
5161
5162 have_smth_to_conv= !str_to_datetime(cs, from, len, &l_time,
5163 sql_mode_for_timestamp(thd), &status);
5164 return store_TIME_with_warning(thd, &l_time, &str,
5165 status.warnings, have_smth_to_conv);
5166 }
5167
5168
store(double nr)5169 int Field_timestamp::store(double nr)
5170 {
5171 MYSQL_TIME l_time;
5172 int error;
5173 ErrConvDouble str(nr);
5174 THD *thd= get_thd();
5175
5176 longlong tmp= double_to_datetime(nr, &l_time, sql_mode_for_timestamp(thd),
5177 &error);
5178 return store_TIME_with_warning(thd, &l_time, &str, error, tmp != -1);
5179 }
5180
5181
store(longlong nr,bool unsigned_val)5182 int Field_timestamp::store(longlong nr, bool unsigned_val)
5183 {
5184 MYSQL_TIME l_time;
5185 int error;
5186 ErrConvInteger str(nr, unsigned_val);
5187 THD *thd= get_thd();
5188
5189 longlong tmp= number_to_datetime(nr, 0, &l_time, sql_mode_for_timestamp(thd),
5190 &error);
5191 return store_TIME_with_warning(thd, &l_time, &str, error, tmp != -1);
5192 }
5193
5194
store_timestamp(my_time_t ts,ulong sec_part)5195 int Field_timestamp::store_timestamp(my_time_t ts, ulong sec_part)
5196 {
5197 store_TIME(ts, sec_part);
5198 if (ts == 0 && sec_part == 0 &&
5199 get_thd()->variables.sql_mode & TIME_NO_ZERO_DATE)
5200 {
5201 ErrConvString s(
5202 STRING_WITH_LEN("0000-00-00 00:00:00.000000") - (decimals() ? 6 - decimals() : 7),
5203 system_charset_info);
5204 set_datetime_warning(WARN_DATA_TRUNCATED, &s, MYSQL_TIMESTAMP_DATETIME, 1);
5205 return 1;
5206 }
5207 return 0;
5208 }
5209
5210
val_real(void)5211 double Field_timestamp::val_real(void)
5212 {
5213 return (double) Field_timestamp::val_int();
5214 }
5215
5216
val_int(void)5217 longlong Field_timestamp::val_int(void)
5218 {
5219 MYSQL_TIME ltime;
5220 if (get_date(<ime, TIME_NO_ZERO_DATE))
5221 return 0;
5222
5223 return ltime.year * 10000000000LL + ltime.month * 100000000LL +
5224 ltime.day * 1000000L + ltime.hour * 10000L +
5225 ltime.minute * 100 + ltime.second;
5226 }
5227
5228
val_str(String * val_buffer,String * val_ptr)5229 String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
5230 {
5231 MYSQL_TIME ltime;
5232 uint32 temp, temp2;
5233 uint dec;
5234 char *to;
5235
5236 val_buffer->alloc(field_length+1);
5237 to= (char*) val_buffer->ptr();
5238 val_buffer->length(field_length);
5239
5240 if (get_date(<ime, TIME_NO_ZERO_DATE))
5241 { /* Zero time is "000000" */
5242 val_ptr->set(zero_timestamp, field_length, &my_charset_numeric);
5243 return val_ptr;
5244 }
5245 val_buffer->set_charset(&my_charset_numeric); // Safety
5246
5247 temp= ltime.year % 100;
5248 if (temp < YY_PART_YEAR - 1)
5249 {
5250 *to++= '2';
5251 *to++= '0';
5252 }
5253 else
5254 {
5255 *to++= '1';
5256 *to++= '9';
5257 }
5258 temp2=temp/10; temp=temp-temp2*10;
5259 *to++= (char) ('0'+(char) (temp2));
5260 *to++= (char) ('0'+(char) (temp));
5261 *to++= '-';
5262 temp=ltime.month;
5263 temp2=temp/10; temp=temp-temp2*10;
5264 *to++= (char) ('0'+(char) (temp2));
5265 *to++= (char) ('0'+(char) (temp));
5266 *to++= '-';
5267 temp=ltime.day;
5268 temp2=temp/10; temp=temp-temp2*10;
5269 *to++= (char) ('0'+(char) (temp2));
5270 *to++= (char) ('0'+(char) (temp));
5271 *to++= ' ';
5272 temp=ltime.hour;
5273 temp2=temp/10; temp=temp-temp2*10;
5274 *to++= (char) ('0'+(char) (temp2));
5275 *to++= (char) ('0'+(char) (temp));
5276 *to++= ':';
5277 temp=ltime.minute;
5278 temp2=temp/10; temp=temp-temp2*10;
5279 *to++= (char) ('0'+(char) (temp2));
5280 *to++= (char) ('0'+(char) (temp));
5281 *to++= ':';
5282 temp=ltime.second;
5283 temp2=temp/10; temp=temp-temp2*10;
5284 *to++= (char) ('0'+(char) (temp2));
5285 *to++= (char) ('0'+(char) (temp));
5286 *to= 0;
5287 val_buffer->set_charset(&my_charset_numeric);
5288
5289 if ((dec= decimals()))
5290 {
5291 ulong sec_part= (ulong) sec_part_shift(ltime.second_part, dec);
5292 char *buf= const_cast<char*>(val_buffer->ptr() + MAX_DATETIME_WIDTH);
5293 for (int i= dec; i > 0; i--, sec_part/= 10)
5294 buf[i]= (char)(sec_part % 10) + '0';
5295 buf[0]= '.';
5296 buf[dec + 1]= 0;
5297 }
5298 return val_buffer;
5299 }
5300
5301
5302 bool
validate_value_in_record(THD * thd,const uchar * record) const5303 Field_timestamp::validate_value_in_record(THD *thd, const uchar *record) const
5304 {
5305 DBUG_ASSERT(!is_null_in_record(record));
5306 ulong sec_part;
5307 return !get_timestamp(ptr_in_record(record), &sec_part) && !sec_part &&
5308 (sql_mode_for_dates(thd) & TIME_NO_ZERO_DATE) != 0;
5309 }
5310
5311
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)5312 bool Field_timestamp::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
5313 {
5314 ulong sec_part;
5315 my_time_t ts= get_timestamp(&sec_part);
5316 return get_thd()->timestamp_to_TIME(ltime, ts, sec_part, fuzzydate);
5317 }
5318
5319
send_binary(Protocol * protocol)5320 bool Field_timestamp::send_binary(Protocol *protocol)
5321 {
5322 MYSQL_TIME ltime;
5323 Field_timestamp::get_date(<ime, 0);
5324 return protocol->store(<ime, 0);
5325 }
5326
5327
cmp(const uchar * a_ptr,const uchar * b_ptr)5328 int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
5329 {
5330 int32 a,b;
5331 a=sint4korr(a_ptr);
5332 b=sint4korr(b_ptr);
5333 return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
5334 }
5335
5336
sort_string(uchar * to,uint length)5337 void Field_timestamp::sort_string(uchar *to,uint length __attribute__((unused)))
5338 {
5339 to[0] = ptr[3];
5340 to[1] = ptr[2];
5341 to[2] = ptr[1];
5342 to[3] = ptr[0];
5343 }
5344
5345
sql_type(String & res) const5346 void Field_timestamp::sql_type(String &res) const
5347 {
5348 if (!decimals())
5349 {
5350 res.set_ascii(STRING_WITH_LEN("timestamp"));
5351 return;
5352 }
5353 CHARSET_INFO *cs=res.charset();
5354 res.length(cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
5355 "timestamp(%u)", decimals()));
5356 }
5357
5358
set_time()5359 int Field_timestamp::set_time()
5360 {
5361 set_notnull();
5362 store_TIME(get_thd()->query_start(), 0);
5363 return 0;
5364 }
5365
5366
load_data_set_no_data(THD * thd,bool fixed_format)5367 bool Field_timestamp::load_data_set_no_data(THD *thd, bool fixed_format)
5368 {
5369 if (!maybe_null())
5370 {
5371 /*
5372 Timestamp fields that are NOT NULL are autoupdated if there is no
5373 corresponding value in the data file.
5374 */
5375 set_time();
5376 set_has_explicit_value();
5377 return false;
5378 }
5379 return Field::load_data_set_no_data(thd, fixed_format);
5380 }
5381
5382
load_data_set_null(THD * thd)5383 bool Field_timestamp::load_data_set_null(THD *thd)
5384 {
5385 if (!maybe_null())
5386 {
5387 /*
5388 Timestamp fields that are NOT NULL are autoupdated if there is no
5389 corresponding value in the data file.
5390 */
5391 set_time();
5392 }
5393 else
5394 {
5395 reset();
5396 set_null();
5397 }
5398 set_has_explicit_value(); // Do not auto-update this field
5399 return false;
5400 }
5401
5402
5403 #ifdef NOT_USED
store_native(ulonglong num,uchar * to,uint bytes)5404 static void store_native(ulonglong num, uchar *to, uint bytes)
5405 {
5406 switch(bytes) {
5407 case 1: *to= (uchar)num; break;
5408 case 2: shortstore(to, (ushort)num); break;
5409 case 3: int3store(to, num); /* Sic!*/ break;
5410 case 4: longstore(to, (ulong)num); break;
5411 case 8: longlongstore(to, num); break;
5412 default: DBUG_ASSERT(0);
5413 }
5414 }
5415
read_native(const uchar * from,uint bytes)5416 static longlong read_native(const uchar *from, uint bytes)
5417 {
5418 switch(bytes) {
5419 case 1: return from[0];
5420 case 2: { uint16 tmp; shortget(tmp, from); return tmp; }
5421 case 3: return uint3korr(from);
5422 case 4: { uint32 tmp; longget(tmp, from); return tmp; }
5423 case 8: { longlong tmp; longlongget(tmp, from); return tmp; }
5424 default: DBUG_ASSERT(0); return 0;
5425 }
5426 }
5427 #endif
5428
store_lowendian(ulonglong num,uchar * to,uint bytes)5429 static void store_lowendian(ulonglong num, uchar *to, uint bytes)
5430 {
5431 switch(bytes) {
5432 case 1: *to= (uchar)num; break;
5433 case 2: int2store(to, num); break;
5434 case 3: int3store(to, num); break;
5435 case 4: int4store(to, num); break;
5436 case 8: int8store(to, num); break;
5437 default: DBUG_ASSERT(0);
5438 }
5439 }
5440
read_lowendian(const uchar * from,uint bytes)5441 static longlong read_lowendian(const uchar *from, uint bytes)
5442 {
5443 switch(bytes) {
5444 case 1: return from[0];
5445 case 2: return uint2korr(from);
5446 case 3: return uint3korr(from);
5447 case 4: return uint4korr(from);
5448 case 8: return sint8korr(from);
5449 default: DBUG_ASSERT(0); return 0;
5450 }
5451 }
5452
store_TIME(my_time_t timestamp,ulong sec_part)5453 void Field_timestamp_hires::store_TIME(my_time_t timestamp, ulong sec_part)
5454 {
5455 mi_int4store(ptr, timestamp);
5456 store_bigendian(sec_part_shift(sec_part, dec), ptr+4, sec_part_bytes(dec));
5457 }
5458
get_timestamp(const uchar * pos,ulong * sec_part) const5459 my_time_t Field_timestamp_hires::get_timestamp(const uchar *pos,
5460 ulong *sec_part) const
5461 {
5462 ASSERT_COLUMN_MARKED_FOR_READ;
5463 *sec_part= (long)sec_part_unshift(read_bigendian(pos+4, sec_part_bytes(dec)), dec);
5464 return mi_uint4korr(pos);
5465 }
5466
val_real(void)5467 double Field_timestamp_with_dec::val_real(void)
5468 {
5469 MYSQL_TIME ltime;
5470 if (get_date(<ime, TIME_NO_ZERO_DATE))
5471 return 0;
5472
5473 return ltime.year * 1e10 + ltime.month * 1e8 +
5474 ltime.day * 1e6 + ltime.hour * 1e4 +
5475 ltime.minute * 1e2 + ltime.second + ltime.second_part*1e-6;
5476 }
5477
val_decimal(my_decimal * d)5478 my_decimal *Field_timestamp_with_dec::val_decimal(my_decimal *d)
5479 {
5480 MYSQL_TIME ltime;
5481 get_date(<ime, 0);
5482 return TIME_to_my_decimal(<ime, d);
5483 }
5484
store_decimal(const my_decimal * d)5485 int Field_timestamp::store_decimal(const my_decimal *d)
5486 {
5487 ulonglong nr;
5488 ulong sec_part;
5489 int error;
5490 MYSQL_TIME ltime;
5491 longlong tmp;
5492 THD *thd= get_thd();
5493 ErrConvDecimal str(d);
5494
5495 if (my_decimal2seconds(d, &nr, &sec_part))
5496 {
5497 tmp= -1;
5498 error= 2;
5499 }
5500 else
5501 tmp= number_to_datetime(nr, sec_part, <ime, sql_mode_for_timestamp(thd),
5502 &error);
5503 return store_TIME_with_warning(thd, <ime, &str, error, tmp != -1);
5504 }
5505
set_time()5506 int Field_timestamp_with_dec::set_time()
5507 {
5508 THD *thd= get_thd();
5509 set_notnull();
5510 // Avoid writing microseconds into binlog for FSP=0
5511 store_TIME(thd->query_start(), decimals() ? thd->query_start_sec_part() : 0);
5512 return 0;
5513 }
5514
send_binary(Protocol * protocol)5515 bool Field_timestamp_with_dec::send_binary(Protocol *protocol)
5516 {
5517 MYSQL_TIME ltime;
5518 Field_timestamp::get_date(<ime, 0);
5519 return protocol->store(<ime, dec);
5520 }
5521
5522
cmp(const uchar * a_ptr,const uchar * b_ptr)5523 int Field_timestamp_hires::cmp(const uchar *a_ptr, const uchar *b_ptr)
5524 {
5525 int32 a,b;
5526 ulong a_sec_part, b_sec_part;
5527 a= mi_uint4korr(a_ptr);
5528 a_sec_part= (ulong)read_bigendian(a_ptr+4, sec_part_bytes(dec));
5529 b= mi_uint4korr(b_ptr);
5530 b_sec_part= (ulong)read_bigendian(b_ptr+4, sec_part_bytes(dec));
5531 return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 :
5532 a_sec_part < b_sec_part ? -1 : a_sec_part > b_sec_part ? 1 : 0;
5533 }
5534
5535
make_send_field(Send_field * field)5536 void Field_timestamp_with_dec::make_send_field(Send_field *field)
5537 {
5538 Field::make_send_field(field);
5539 field->decimals= dec;
5540 }
5541
5542
5543 /*************************************************************
5544 ** MySQL-5.6 compatible TIMESTAMP(N)
5545 **************************************************************/
5546
store_TIME(my_time_t timestamp,ulong sec_part)5547 void Field_timestampf::store_TIME(my_time_t timestamp, ulong sec_part)
5548 {
5549 struct timeval tm;
5550 tm.tv_sec= timestamp;
5551 tm.tv_usec= sec_part;
5552 my_timeval_trunc(&tm, dec);
5553 my_timestamp_to_binary(&tm, ptr, dec);
5554 }
5555
set_max()5556 void Field_timestampf::set_max()
5557 {
5558 DBUG_ENTER("Field_timestampf::set_max");
5559 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
5560 DBUG_ASSERT(dec == TIME_SECOND_PART_DIGITS);
5561
5562 set_notnull();
5563 mi_int4store(ptr, TIMESTAMP_MAX_VALUE);
5564 mi_int3store(ptr + 4, TIME_MAX_SECOND_PART);
5565
5566 DBUG_VOID_RETURN;
5567 }
5568
is_max()5569 bool Field_timestampf::is_max()
5570 {
5571 DBUG_ENTER("Field_timestampf::is_max");
5572 ASSERT_COLUMN_MARKED_FOR_READ;
5573
5574 DBUG_RETURN(mi_sint4korr(ptr) == TIMESTAMP_MAX_VALUE &&
5575 mi_sint3korr(ptr + 4) == TIME_MAX_SECOND_PART);
5576 }
5577
get_timestamp(const uchar * pos,ulong * sec_part) const5578 my_time_t Field_timestampf::get_timestamp(const uchar *pos,
5579 ulong *sec_part) const
5580 {
5581 struct timeval tm;
5582 my_timestamp_from_binary(&tm, pos, dec);
5583 *sec_part= tm.tv_usec;
5584 return tm.tv_sec;
5585 }
5586
5587
5588 /*************************************************************/
can_handle_sql_mode_dependency_on_store() const5589 sql_mode_t Field_temporal::can_handle_sql_mode_dependency_on_store() const
5590 {
5591 return MODE_PAD_CHAR_TO_FULL_LENGTH;
5592 }
5593
5594
is_equal(Create_field * new_field)5595 uint Field_temporal::is_equal(Create_field *new_field)
5596 {
5597 return new_field->type_handler() == type_handler() &&
5598 new_field->length == max_display_length();
5599 }
5600
5601
set_warnings(Sql_condition::enum_warning_level trunc_level,const ErrConv * str,int was_cut,timestamp_type ts_type)5602 void Field_temporal::set_warnings(Sql_condition::enum_warning_level trunc_level,
5603 const ErrConv *str, int was_cut,
5604 timestamp_type ts_type)
5605 {
5606 /*
5607 error code logic:
5608 MYSQL_TIME_WARN_TRUNCATED means that the value was not a date/time at all.
5609 it will be stored as zero date/time.
5610 MYSQL_TIME_WARN_OUT_OF_RANGE means that the value was a date/time,
5611 that is, it was parsed as such, but the value was invalid.
5612
5613 Also, MYSQL_TIME_WARN_TRUNCATED is used when storing a DATETIME in
5614 a DATE field and non-zero time part is thrown away.
5615 */
5616 if (was_cut & MYSQL_TIME_WARN_TRUNCATED)
5617 set_datetime_warning(trunc_level, WARN_DATA_TRUNCATED, str, ts_type, 1);
5618 if (was_cut & MYSQL_TIME_WARN_OUT_OF_RANGE)
5619 set_datetime_warning(ER_WARN_DATA_OUT_OF_RANGE, str, ts_type, 1);
5620 }
5621
5622
5623 /*
5624 Store string into a date/time field
5625
5626 RETURN
5627 0 ok
5628 1 Value was cut during conversion
5629 2 value was out of range
5630 3 Datetime value that was cut (warning level NOTE)
5631 This is used by opt_range.cc:get_mm_leaf().
5632 */
store_TIME_with_warning(MYSQL_TIME * ltime,const ErrConv * str,int was_cut,int have_smth_to_conv)5633 int Field_temporal_with_date::store_TIME_with_warning(MYSQL_TIME *ltime,
5634 const ErrConv *str,
5635 int was_cut,
5636 int have_smth_to_conv)
5637 {
5638 Sql_condition::enum_warning_level trunc_level= Sql_condition::WARN_LEVEL_WARN;
5639 int ret= 2;
5640
5641 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
5642
5643 if (was_cut == 0 && have_smth_to_conv == 0) // special case: zero date
5644 {
5645 was_cut= MYSQL_TIME_WARN_OUT_OF_RANGE;
5646 }
5647 else if (!have_smth_to_conv)
5648 {
5649 bzero(ltime, sizeof(*ltime));
5650 was_cut= MYSQL_TIME_WARN_TRUNCATED;
5651 ret= 1;
5652 }
5653 else if (!MYSQL_TIME_WARN_HAVE_WARNINGS(was_cut) &&
5654 (MYSQL_TIME_WARN_HAVE_NOTES(was_cut) ||
5655 (type_handler()->mysql_timestamp_type() == MYSQL_TIMESTAMP_DATE &&
5656 (ltime->hour || ltime->minute || ltime->second || ltime->second_part))))
5657 {
5658 trunc_level= Sql_condition::WARN_LEVEL_NOTE;
5659 was_cut|= MYSQL_TIME_WARN_TRUNCATED;
5660 ret= 3;
5661 }
5662 set_warnings(trunc_level, str, was_cut,
5663 type_handler()->mysql_timestamp_type());
5664 store_TIME(ltime);
5665 return was_cut ? ret : 0;
5666 }
5667
5668
store(const char * from,size_t len,CHARSET_INFO * cs)5669 int Field_temporal_with_date::store(const char *from, size_t len, CHARSET_INFO *cs)
5670 {
5671 MYSQL_TIME ltime;
5672 MYSQL_TIME_STATUS status;
5673 THD *thd= get_thd();
5674 ErrConvString str(from, len, cs);
5675 bool func_res= !str_to_datetime(cs, from, len, <ime,
5676 sql_mode_for_dates(thd),
5677 &status);
5678 return store_TIME_with_warning(<ime, &str, status.warnings, func_res);
5679 }
5680
5681
store(double nr)5682 int Field_temporal_with_date::store(double nr)
5683 {
5684 int error= 0;
5685 MYSQL_TIME ltime;
5686 THD *thd= get_thd();
5687 ErrConvDouble str(nr);
5688
5689 longlong tmp= double_to_datetime(nr, <ime,
5690 (uint) sql_mode_for_dates(thd), &error);
5691 return store_TIME_with_warning(<ime, &str, error, tmp != -1);
5692 }
5693
5694
store(longlong nr,bool unsigned_val)5695 int Field_temporal_with_date::store(longlong nr, bool unsigned_val)
5696 {
5697 int error;
5698 MYSQL_TIME ltime;
5699 longlong tmp;
5700 THD *thd= get_thd();
5701 ErrConvInteger str(nr, unsigned_val);
5702
5703 tmp= number_to_datetime(nr, 0, <ime, sql_mode_for_dates(thd), &error);
5704
5705 return store_TIME_with_warning(<ime, &str, error, tmp != -1);
5706 }
5707
5708
store_time_dec(const MYSQL_TIME * ltime,uint dec)5709 int Field_temporal_with_date::store_time_dec(const MYSQL_TIME *ltime, uint dec)
5710 {
5711 int error= 0, have_smth_to_conv= 1;
5712 ErrConvTime str(ltime);
5713 MYSQL_TIME l_time;
5714
5715 if (copy_or_convert_to_datetime(get_thd(), ltime, &l_time))
5716 {
5717 /*
5718 Set have_smth_to_conv and error in a way to have
5719 store_TIME_with_warning do bzero().
5720 */
5721 have_smth_to_conv= false;
5722 error= MYSQL_TIME_WARN_OUT_OF_RANGE;
5723 }
5724 else
5725 {
5726 /*
5727 We don't perform range checking here since values stored in TIME
5728 structure always fit into DATETIME range.
5729 */
5730 have_smth_to_conv= !check_date(&l_time, pack_time(&l_time) != 0,
5731 sql_mode_for_dates(get_thd()), &error);
5732 }
5733 return store_TIME_with_warning(&l_time, &str, error, have_smth_to_conv);
5734 }
5735
5736
5737 bool
validate_value_in_record(THD * thd,const uchar * record) const5738 Field_temporal_with_date::validate_value_in_record(THD *thd,
5739 const uchar *record) const
5740 {
5741 DBUG_ASSERT(!is_null_in_record(record));
5742 MYSQL_TIME ltime;
5743 return get_TIME(<ime, ptr_in_record(record), sql_mode_for_dates(thd));
5744 }
5745
5746
val_decimal(my_decimal * d)5747 my_decimal *Field_temporal::val_decimal(my_decimal *d)
5748 {
5749 MYSQL_TIME ltime;
5750 if (get_date(<ime, 0))
5751 {
5752 bzero(<ime, sizeof(ltime));
5753 ltime.time_type= type_handler()->mysql_timestamp_type();
5754 }
5755 return TIME_to_my_decimal(<ime, d);
5756 }
5757
5758
can_optimize_keypart_ref(const Item_bool_func * cond,const Item * value) const5759 bool Field_temporal::can_optimize_keypart_ref(const Item_bool_func *cond,
5760 const Item *value) const
5761 {
5762 return true; // Field is of TIME_RESULT, which supersedes everything else.
5763 }
5764
5765
can_optimize_group_min_max(const Item_bool_func * cond,const Item * const_item) const5766 bool Field_temporal::can_optimize_group_min_max(const Item_bool_func *cond,
5767 const Item *const_item) const
5768 {
5769 return true; // Field is of TIME_RESULT, which supersedes everything else.
5770 }
5771
5772
get_equal_const_item_datetime(THD * thd,const Context & ctx,Item * const_item)5773 Item *Field_temporal::get_equal_const_item_datetime(THD *thd,
5774 const Context &ctx,
5775 Item *const_item)
5776 {
5777 switch (ctx.subst_constraint()) {
5778 case IDENTITY_SUBST:
5779 if ((const_item->field_type() != MYSQL_TYPE_DATETIME &&
5780 const_item->field_type() != MYSQL_TYPE_TIMESTAMP) ||
5781 const_item->decimals != decimals())
5782 {
5783 Datetime dt(thd, const_item, 0);
5784 if (!dt.is_valid_datetime())
5785 return NULL;
5786 /*
5787 See comments about truncation in the same place in
5788 Field_time::get_equal_const_item().
5789 */
5790 return new (thd->mem_root) Item_datetime_literal(thd,
5791 dt.get_mysql_time(),
5792 decimals());
5793 }
5794 break;
5795 case ANY_SUBST:
5796 if (!is_temporal_type_with_date(const_item->field_type()))
5797 {
5798 Datetime dt(thd, const_item, TIME_FUZZY_DATES | TIME_INVALID_DATES);
5799 if (!dt.is_valid_datetime())
5800 return NULL;
5801 return new (thd->mem_root)
5802 Item_datetime_literal_for_invalid_dates(thd, dt.get_mysql_time(),
5803 dt.get_mysql_time()->
5804 second_part ?
5805 TIME_SECOND_PART_DIGITS : 0);
5806 }
5807 break;
5808 }
5809 return const_item;
5810 }
5811
5812
5813 /****************************************************************************
5814 ** time type
5815 ** In string context: HH:MM:SS
5816 ** In number context: HHMMSS
5817 ** Stored as a 3 byte unsigned int
5818 ****************************************************************************/
store_TIME_with_warning(MYSQL_TIME * ltime,const ErrConv * str,int was_cut,int have_smth_to_conv)5819 int Field_time::store_TIME_with_warning(MYSQL_TIME *ltime,
5820 const ErrConv *str,
5821 int was_cut,
5822 int have_smth_to_conv)
5823 {
5824 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
5825
5826 if (!have_smth_to_conv)
5827 {
5828 bzero(ltime, sizeof(*ltime));
5829 store_TIME(ltime);
5830 set_warnings(Sql_condition::WARN_LEVEL_WARN, str, MYSQL_TIME_WARN_TRUNCATED);
5831 return 1;
5832 }
5833 if (ltime->year != 0 || ltime->month != 0)
5834 {
5835 ltime->year= ltime->month= ltime->day= 0;
5836 was_cut|= MYSQL_TIME_NOTE_TRUNCATED;
5837 }
5838 my_time_trunc(ltime, decimals());
5839 store_TIME(ltime);
5840 if (!MYSQL_TIME_WARN_HAVE_WARNINGS(was_cut) &&
5841 MYSQL_TIME_WARN_HAVE_NOTES(was_cut))
5842 {
5843 set_warnings(Sql_condition::WARN_LEVEL_NOTE, str,
5844 was_cut | MYSQL_TIME_WARN_TRUNCATED);
5845 return 3;
5846 }
5847 set_warnings(Sql_condition::WARN_LEVEL_WARN, str, was_cut);
5848 return was_cut ? 2 : 0;
5849 }
5850
5851
store_TIME(const MYSQL_TIME * ltime)5852 void Field_time::store_TIME(const MYSQL_TIME *ltime)
5853 {
5854 DBUG_ASSERT(ltime->year == 0);
5855 DBUG_ASSERT(ltime->month == 0);
5856 long tmp= (ltime->day*24L+ltime->hour)*10000L +
5857 (ltime->minute*100+ltime->second);
5858 if (ltime->neg)
5859 tmp= -tmp;
5860 int3store(ptr,tmp);
5861 }
5862
store(const char * from,size_t len,CHARSET_INFO * cs)5863 int Field_time::store(const char *from,size_t len,CHARSET_INFO *cs)
5864 {
5865 MYSQL_TIME ltime;
5866 MYSQL_TIME_STATUS status;
5867 ErrConvString str(from, len, cs);
5868 bool have_smth_to_conv=
5869 !str_to_time(cs, from, len, <ime, sql_mode_for_dates(get_thd()),
5870 &status);
5871
5872 return store_TIME_with_warning(<ime, &str,
5873 status.warnings, have_smth_to_conv);
5874 }
5875
5876
5877 /**
5878 subtract a given number of days from DATETIME, return TIME
5879
5880 optimized version of calc_time_diff()
5881
5882 @note it might generate TIME values outside of the valid TIME range!
5883 */
calc_datetime_days_diff(MYSQL_TIME * ltime,long days)5884 static void calc_datetime_days_diff(MYSQL_TIME *ltime, long days)
5885 {
5886 long daydiff= calc_daynr(ltime->year, ltime->month, ltime->day) - days;
5887 ltime->year= ltime->month= 0;
5888 if (daydiff >=0 )
5889 {
5890 ltime->day= daydiff;
5891 ltime->time_type= MYSQL_TIMESTAMP_TIME;
5892 }
5893 else
5894 {
5895 longlong timediff= ((((daydiff * 24LL +
5896 ltime->hour) * 60LL +
5897 ltime->minute) * 60LL +
5898 ltime->second) * 1000000LL +
5899 ltime->second_part);
5900 unpack_time(timediff, ltime, MYSQL_TIMESTAMP_TIME);
5901 }
5902 }
5903
5904
store_time_dec(const MYSQL_TIME * ltime,uint dec)5905 int Field_time::store_time_dec(const MYSQL_TIME *ltime, uint dec)
5906 {
5907 MYSQL_TIME l_time= *ltime;
5908 ErrConvTime str(ltime);
5909 int was_cut= 0;
5910
5911 if (curdays && l_time.time_type != MYSQL_TIMESTAMP_TIME)
5912 calc_datetime_days_diff(&l_time, curdays);
5913
5914 int have_smth_to_conv= !check_time_range(&l_time, decimals(), &was_cut);
5915 return store_TIME_with_warning(&l_time, &str, was_cut, have_smth_to_conv);
5916 }
5917
5918
store(double nr)5919 int Field_time::store(double nr)
5920 {
5921 MYSQL_TIME ltime;
5922 ErrConvDouble str(nr);
5923 int was_cut;
5924 bool neg= nr < 0;
5925 if (neg)
5926 nr= -nr;
5927 int have_smth_to_conv= !number_to_time(neg, (ulonglong) nr,
5928 (ulong)((nr - floor(nr)) * TIME_SECOND_PART_FACTOR),
5929 <ime, &was_cut);
5930
5931 return store_TIME_with_warning(<ime, &str, was_cut, have_smth_to_conv);
5932 }
5933
5934
store(longlong nr,bool unsigned_val)5935 int Field_time::store(longlong nr, bool unsigned_val)
5936 {
5937 MYSQL_TIME ltime;
5938 ErrConvInteger str(nr, unsigned_val);
5939 int was_cut;
5940 if (nr < 0 && unsigned_val)
5941 nr= 99991231235959LL + 1;
5942 int have_smth_to_conv= !number_to_time(nr < 0,
5943 (ulonglong) (nr < 0 ? -nr : nr),
5944 0, <ime, &was_cut);
5945
5946 return store_TIME_with_warning(<ime, &str, was_cut, have_smth_to_conv);
5947 }
5948
5949
set_curdays(THD * thd)5950 void Field_time::set_curdays(THD *thd)
5951 {
5952 MYSQL_TIME ltime;
5953 set_current_date(thd, <ime);
5954 curdays= calc_daynr(ltime.year, ltime.month, ltime.day);
5955 }
5956
5957
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uint32 length,uchar * new_null_ptr,uint new_null_bit)5958 Field *Field_time::new_key_field(MEM_ROOT *root, TABLE *new_table,
5959 uchar *new_ptr, uint32 length,
5960 uchar *new_null_ptr, uint new_null_bit)
5961 {
5962 THD *thd= get_thd();
5963 Field_time *res=
5964 (Field_time*) Field::new_key_field(root, new_table, new_ptr, length,
5965 new_null_ptr, new_null_bit);
5966 if (!(thd->variables.old_behavior & OLD_MODE_ZERO_DATE_TIME_CAST) && res)
5967 res->set_curdays(thd);
5968 return res;
5969 }
5970
5971
val_real(void)5972 double Field_time::val_real(void)
5973 {
5974 ASSERT_COLUMN_MARKED_FOR_READ;
5975 uint32 j= (uint32) uint3korr(ptr);
5976 return (double) j;
5977 }
5978
val_int(void)5979 longlong Field_time::val_int(void)
5980 {
5981 ASSERT_COLUMN_MARKED_FOR_READ;
5982 return (longlong) sint3korr(ptr);
5983 }
5984
5985
5986 /**
5987 @note
5988 This function is multi-byte safe as the result string is always of type
5989 my_charset_bin
5990 */
5991
val_str(String * str,String * unused)5992 String *Field_time::val_str(String *str,
5993 String *unused __attribute__((unused)))
5994 {
5995 ASSERT_COLUMN_MARKED_FOR_READ;
5996 MYSQL_TIME ltime;
5997 get_date(<ime, TIME_TIME_ONLY);
5998 str->alloc(field_length + 1);
5999 str->length(my_time_to_str(<ime, const_cast<char*>(str->ptr()), decimals()));
6000 str->set_charset(&my_charset_numeric);
6001 return str;
6002 }
6003
6004
check_zero_in_date_with_warn(ulonglong fuzzydate)6005 bool Field_time::check_zero_in_date_with_warn(ulonglong fuzzydate)
6006 {
6007 if (!(fuzzydate & TIME_TIME_ONLY) && (fuzzydate & TIME_NO_ZERO_IN_DATE))
6008 {
6009 THD *thd= get_thd();
6010 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
6011 ER_WARN_DATA_OUT_OF_RANGE,
6012 ER_THD(thd, ER_WARN_DATA_OUT_OF_RANGE), field_name.str,
6013 thd->get_stmt_da()->current_row_for_warning());
6014 return true;
6015 }
6016 return false;
6017 }
6018
6019
6020 /**
6021 @note
6022 Normally we would not consider 'time' as a valid date, but we allow
6023 get_date() here to be able to do things like
6024 DATE_FORMAT(time, "%l.%i %p")
6025 */
6026
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)6027 bool Field_time::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6028 {
6029 if (check_zero_in_date_with_warn(fuzzydate))
6030 return true;
6031 long tmp=(long) sint3korr(ptr);
6032 ltime->neg=0;
6033 if (tmp < 0)
6034 {
6035 ltime->neg= 1;
6036 tmp=-tmp;
6037 }
6038 ltime->year= ltime->month= ltime->day= 0;
6039 ltime->hour= (int) (tmp/10000);
6040 tmp-=ltime->hour*10000;
6041 ltime->minute= (int) tmp/100;
6042 ltime->second= (int) tmp % 100;
6043 ltime->second_part=0;
6044 ltime->time_type= MYSQL_TIMESTAMP_TIME;
6045 return 0;
6046 }
6047
6048
send_binary(Protocol * protocol)6049 bool Field_time::send_binary(Protocol *protocol)
6050 {
6051 MYSQL_TIME ltime;
6052 get_date(<ime, TIME_TIME_ONLY);
6053 return protocol->store_time(<ime, decimals());
6054 }
6055
6056
cmp(const uchar * a_ptr,const uchar * b_ptr)6057 int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
6058 {
6059 int32 a,b;
6060 a=(int32) sint3korr(a_ptr);
6061 b=(int32) sint3korr(b_ptr);
6062 return (a < b) ? -1 : (a > b) ? 1 : 0;
6063 }
6064
sort_string(uchar * to,uint length)6065 void Field_time::sort_string(uchar *to,uint length __attribute__((unused)))
6066 {
6067 to[0] = (uchar) (ptr[2] ^ 128);
6068 to[1] = ptr[1];
6069 to[2] = ptr[0];
6070 }
6071
sql_type(String & res) const6072 void Field_time::sql_type(String &res) const
6073 {
6074 if (decimals() == 0)
6075 {
6076 res.set_ascii(STRING_WITH_LEN("time"));
6077 return;
6078 }
6079 CHARSET_INFO *cs= res.charset();
6080 res.length(cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
6081 "time(%d)", decimals()));
6082 }
6083
reset()6084 int Field_time_hires::reset()
6085 {
6086 store_bigendian(zero_point, ptr, Field_time_hires::pack_length());
6087 return 0;
6088 }
6089
6090
store_TIME(const MYSQL_TIME * ltime)6091 void Field_time_hires::store_TIME(const MYSQL_TIME *ltime)
6092 {
6093 DBUG_ASSERT(ltime->year == 0);
6094 DBUG_ASSERT(ltime->month == 0);
6095 ulonglong packed= sec_part_shift(pack_time(ltime), dec) + zero_point;
6096 store_bigendian(packed, ptr, Field_time_hires::pack_length());
6097 }
6098
store_decimal(const my_decimal * d)6099 int Field_time::store_decimal(const my_decimal *d)
6100 {
6101 ulonglong nr;
6102 ulong sec_part;
6103 ErrConvDecimal str(d);
6104 MYSQL_TIME ltime;
6105 int was_cut;
6106 bool neg= my_decimal2seconds(d, &nr, &sec_part);
6107
6108 int have_smth_to_conv= !number_to_time(neg, nr, sec_part, <ime, &was_cut);
6109
6110 return store_TIME_with_warning(<ime, &str, was_cut, have_smth_to_conv);
6111 }
6112
6113
can_be_substituted_to_equal_item(const Context & ctx,const Item_equal * item_equal)6114 bool Field_time::can_be_substituted_to_equal_item(const Context &ctx,
6115 const Item_equal *item_equal)
6116 {
6117 DBUG_ASSERT(item_equal->compare_type_handler()->cmp_type() != STRING_RESULT);
6118 switch (ctx.subst_constraint()) {
6119 case ANY_SUBST:
6120 /*
6121 A TIME field in a DATETIME comparison can be substituted to
6122 Item_equal with TIME comparison.
6123
6124 SET timestamp=UNIX_TIMESTAMP('2015-08-30 10:20:30');
6125 CREATE OR REPLACE TABLE t1 (a TIME);
6126 INSERT INTO t1 VALUES ('00:00:00'),('00:00:01');
6127 SELECT * FROM t1 WHERE a>=TIMESTAMP'2015-08-30 00:00:00'
6128 AND a='00:00:00';
6129
6130 The above query can be simplified to:
6131 SELECT * FROM t1 WHERE TIME'00:00:00'>=TIMESTAMP'2015-08-30 00:00:00'
6132 AND a='00:00:00';
6133 And further to:
6134 SELECT * FROM t1 WHERE a=TIME'00:00:00';
6135 */
6136 if (ctx.compare_type_handler() == &type_handler_datetime &&
6137 item_equal->compare_type_handler() == &type_handler_time)
6138 return true;
6139 return ctx.compare_type_handler() == item_equal->compare_type_handler();
6140 case IDENTITY_SUBST:
6141 return true;
6142 }
6143 return false;
6144 }
6145
6146
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)6147 Item *Field_time::get_equal_const_item(THD *thd, const Context &ctx,
6148 Item *const_item)
6149 {
6150 switch (ctx.subst_constraint()) {
6151 case ANY_SUBST:
6152 if (const_item->field_type() != MYSQL_TYPE_TIME)
6153 {
6154 MYSQL_TIME ltime;
6155 // Get the value of const_item with conversion from DATETIME to TIME
6156 ulonglong fuzzydate= Time::comparison_flags_for_get_date();
6157 if (const_item->get_time_with_conversion(thd, <ime, fuzzydate))
6158 return NULL;
6159 /*
6160 Replace a DATE/DATETIME constant to a TIME constant:
6161 WHERE LENGTH(time_column)=8
6162 AND time_column=TIMESTAMP'2015-08-30 10:20:30';
6163 to:
6164 WHERE LENGTH(time_column)=10
6165 AND time_column=TIME'10:20:30'
6166
6167 (assuming CURRENT_DATE is '2015-08-30'
6168 */
6169 return new (thd->mem_root) Item_time_literal(thd, <ime,
6170 ltime.second_part ?
6171 TIME_SECOND_PART_DIGITS :
6172 0);
6173 }
6174 break;
6175 case IDENTITY_SUBST:
6176 if (const_item->field_type() != MYSQL_TYPE_TIME ||
6177 const_item->decimals != decimals())
6178 {
6179 MYSQL_TIME ltime;
6180 if (const_item->get_time_with_conversion(thd, <ime, TIME_TIME_ONLY))
6181 return NULL;
6182 /*
6183 Note, the value returned in "ltime" can have more fractional
6184 digits that decimals(). The Item_time_literal constructor will
6185 truncate these digits. We could disallow propagation is such
6186 cases, but it's still useful (and safe) to optimize:
6187 WHERE time0_column='00:00:00.123' AND LENGTH(a)=12
6188 to
6189 WHERE time0_column='00:00:00.123' AND LENGTH(TIME'00:00:00')=12
6190 and then to
6191 WHERE FALSE
6192 The original WHERE would do the full table scan (in case of no keys).
6193 The optimized WHERE will return with "Impossible WHERE", without
6194 having to do the full table scan.
6195 */
6196 return new (thd->mem_root) Item_time_literal(thd, <ime, decimals());
6197 }
6198 break;
6199 }
6200 return const_item;
6201 }
6202
6203
val_int(void)6204 longlong Field_time_with_dec::val_int(void)
6205 {
6206 ASSERT_COLUMN_MARKED_FOR_READ;
6207 MYSQL_TIME ltime;
6208 get_date(<ime, TIME_TIME_ONLY);
6209 longlong val= TIME_to_ulonglong_time(<ime);
6210 return ltime.neg ? -val : val;
6211 }
6212
val_real(void)6213 double Field_time_with_dec::val_real(void)
6214 {
6215 ASSERT_COLUMN_MARKED_FOR_READ;
6216 MYSQL_TIME ltime;
6217 get_date(<ime, TIME_TIME_ONLY);
6218 return TIME_to_double(<ime);
6219 }
6220
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)6221 bool Field_time_hires::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6222 {
6223 if (check_zero_in_date_with_warn(fuzzydate))
6224 return true;
6225 uint32 len= pack_length();
6226 longlong packed= read_bigendian(ptr, len);
6227
6228 packed= sec_part_unshift(packed - zero_point, dec);
6229
6230 unpack_time(packed, ltime, MYSQL_TIMESTAMP_TIME);
6231 return false;
6232 }
6233
6234
cmp(const uchar * a_ptr,const uchar * b_ptr)6235 int Field_time_hires::cmp(const uchar *a_ptr, const uchar *b_ptr)
6236 {
6237 ulonglong a=read_bigendian(a_ptr, Field_time_hires::pack_length());
6238 ulonglong b=read_bigendian(b_ptr, Field_time_hires::pack_length());
6239 return (a < b) ? -1 : (a > b) ? 1 : 0;
6240 }
6241
sort_string(uchar * to,uint length)6242 void Field_time_hires::sort_string(uchar *to,uint length __attribute__((unused)))
6243 {
6244 DBUG_ASSERT(length == Field_time_hires::pack_length());
6245 memcpy(to, ptr, length);
6246 to[0]^= 128;
6247 }
6248
make_send_field(Send_field * field)6249 void Field_time_with_dec::make_send_field(Send_field *field)
6250 {
6251 Field::make_send_field(field);
6252 field->decimals= dec;
6253 }
6254
6255 /****************************************************************************
6256 ** time type with fsp (MySQL-5.6 version)
6257 ** In string context: HH:MM:SS.FFFFFF
6258 ** In number context: HHMMSS.FFFFFF
6259 ****************************************************************************/
6260
reset()6261 int Field_timef::reset()
6262 {
6263 my_time_packed_to_binary(0, ptr, dec);
6264 return 0;
6265 }
6266
store_TIME(const MYSQL_TIME * ltime)6267 void Field_timef::store_TIME(const MYSQL_TIME *ltime)
6268 {
6269 longlong tmp= TIME_to_longlong_time_packed(ltime);
6270 my_time_packed_to_binary(tmp, ptr, dec);
6271 }
6272
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)6273 bool Field_timef::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6274 {
6275 if (check_zero_in_date_with_warn(fuzzydate))
6276 return true;
6277 longlong tmp= my_time_packed_from_binary(ptr, dec);
6278 TIME_from_longlong_time_packed(ltime, tmp);
6279 return false;
6280 }
6281
6282 /****************************************************************************
6283 ** year type
6284 ** Save in a byte the year 0, 1901->2155
6285 ** Can handle 2 byte or 4 byte years!
6286 ****************************************************************************/
6287
store(const char * from,size_t len,CHARSET_INFO * cs)6288 int Field_year::store(const char *from, size_t len,CHARSET_INFO *cs)
6289 {
6290 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
6291 THD *thd= get_thd();
6292 char *end;
6293 int error;
6294 longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
6295
6296 if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 ||
6297 error == MY_ERRNO_ERANGE)
6298 {
6299 *ptr=0;
6300 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
6301 return 1;
6302 }
6303
6304 if (thd->count_cuted_fields <= CHECK_FIELD_EXPRESSION && error == MY_ERRNO_EDOM)
6305 {
6306 *ptr= 0;
6307 return 1;
6308 }
6309
6310 if (thd->count_cuted_fields > CHECK_FIELD_EXPRESSION &&
6311 (error= check_int(cs, from, len, end, error)))
6312 {
6313 if (unlikely(error == 1) /* empty or incorrect string */)
6314 {
6315 *ptr= 0;
6316 return 1;
6317 }
6318 error= 1;
6319 }
6320
6321 if (nr != 0 || len != 4)
6322 {
6323 if (nr < YY_PART_YEAR)
6324 nr+=100; // 2000 - 2069
6325 else if (nr > 1900)
6326 nr-= 1900;
6327 }
6328 *ptr= (char) (uchar) nr;
6329 return error;
6330 }
6331
6332
store(double nr)6333 int Field_year::store(double nr)
6334 {
6335 if (nr < 0.0 || nr > 2155.0)
6336 {
6337 (void) Field_year::store((longlong) -1, FALSE);
6338 return 1;
6339 }
6340 return Field_year::store((longlong) nr, FALSE);
6341 }
6342
6343
store(longlong nr,bool unsigned_val)6344 int Field_year::store(longlong nr, bool unsigned_val)
6345 {
6346 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
6347 if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
6348 {
6349 *ptr= 0;
6350 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
6351 return 1;
6352 }
6353 if (nr != 0 || field_length != 4) // 0000 -> 0; 00 -> 2000
6354 {
6355 if (nr < YY_PART_YEAR)
6356 nr+=100; // 2000 - 2069
6357 else if (nr > 1900)
6358 nr-= 1900;
6359 }
6360 *ptr= (char) (uchar) nr;
6361 return 0;
6362 }
6363
6364
store_time_dec(const MYSQL_TIME * ltime,uint dec_arg)6365 int Field_year::store_time_dec(const MYSQL_TIME *ltime, uint dec_arg)
6366 {
6367 ErrConvTime str(ltime);
6368 if (Field_year::store(ltime->year, 0))
6369 return 1;
6370
6371 set_datetime_warning(WARN_DATA_TRUNCATED, &str, ltime->time_type, 1);
6372 return 0;
6373 }
6374
send_binary(Protocol * protocol)6375 bool Field_year::send_binary(Protocol *protocol)
6376 {
6377 ASSERT_COLUMN_MARKED_FOR_READ;
6378 ulonglong tmp= Field_year::val_int();
6379 return protocol->store_short(tmp);
6380 }
6381
6382
val_real(void)6383 double Field_year::val_real(void)
6384 {
6385 return (double) Field_year::val_int();
6386 }
6387
6388
val_int(void)6389 longlong Field_year::val_int(void)
6390 {
6391 ASSERT_COLUMN_MARKED_FOR_READ;
6392 DBUG_ASSERT(field_length == 2 || field_length == 4);
6393 int tmp= (int) ptr[0];
6394 if (field_length != 4)
6395 tmp%=100; // Return last 2 char
6396 else if (tmp)
6397 tmp+=1900;
6398 return (longlong) tmp;
6399 }
6400
6401
val_str(String * val_buffer,String * val_ptr)6402 String *Field_year::val_str(String *val_buffer,
6403 String *val_ptr __attribute__((unused)))
6404 {
6405 DBUG_ASSERT(field_length < 5);
6406 val_buffer->alloc(5);
6407 val_buffer->length(field_length);
6408 char *to=(char*) val_buffer->ptr();
6409 sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
6410 val_buffer->set_charset(&my_charset_numeric);
6411 return val_buffer;
6412 }
6413
6414
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)6415 bool Field_year::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
6416 {
6417 int tmp= (int) ptr[0];
6418 if (tmp || field_length != 4)
6419 tmp+= 1900;
6420 return int_to_datetime_with_warn(false, tmp * 10000,
6421 ltime, fuzzydate, table->s, field_name.str);
6422 }
6423
6424
sql_type(String & res) const6425 void Field_year::sql_type(String &res) const
6426 {
6427 CHARSET_INFO *cs=res.charset();
6428 res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
6429 "year(%d)",(int) field_length));
6430 }
6431
6432
6433 /****************************************************************************
6434 ** date type
6435 ** In string context: YYYY-MM-DD
6436 ** In number context: YYYYMMDD
6437 ** Stored as a 4 byte unsigned int
6438 ****************************************************************************/
6439
store_TIME(MYSQL_TIME * ltime)6440 void Field_date::store_TIME(MYSQL_TIME *ltime)
6441 {
6442 uint tmp= ltime->year*10000L + ltime->month*100+ltime->day;
6443 int4store(ptr,tmp);
6444 }
6445
send_binary(Protocol * protocol)6446 bool Field_date::send_binary(Protocol *protocol)
6447 {
6448 longlong tmp= Field_date::val_int();
6449 MYSQL_TIME tm;
6450 tm.year= (uint32) tmp/10000L % 10000;
6451 tm.month= (uint32) tmp/100 % 100;
6452 tm.day= (uint32) tmp % 100;
6453 return protocol->store_date(&tm);
6454 }
6455
6456
val_real(void)6457 double Field_date::val_real(void)
6458 {
6459 ASSERT_COLUMN_MARKED_FOR_READ;
6460 int32 j;
6461 j=sint4korr(ptr);
6462 return (double) (uint32) j;
6463 }
6464
6465
val_int(void)6466 longlong Field_date::val_int(void)
6467 {
6468 ASSERT_COLUMN_MARKED_FOR_READ;
6469 int32 j;
6470 j=sint4korr(ptr);
6471 return (longlong) (uint32) j;
6472 }
6473
6474
get_TIME(MYSQL_TIME * ltime,const uchar * pos,ulonglong fuzzydate) const6475 bool Field_date::get_TIME(MYSQL_TIME *ltime, const uchar *pos,
6476 ulonglong fuzzydate) const
6477 {
6478 ASSERT_COLUMN_MARKED_FOR_READ;
6479 int32 tmp= sint4korr(pos);
6480 ltime->year= (int) ((uint32) tmp/10000L % 10000);
6481 ltime->month= (int) ((uint32) tmp/100 % 100);
6482 ltime->day= (int) ((uint32) tmp % 100);
6483 ltime->time_type= MYSQL_TIMESTAMP_DATE;
6484 ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
6485 return validate_MMDD(tmp, ltime->month, ltime->day, fuzzydate);
6486 }
6487
6488
val_str(String * val_buffer,String * val_ptr)6489 String *Field_date::val_str(String *val_buffer,
6490 String *val_ptr __attribute__((unused)))
6491 {
6492 MYSQL_TIME ltime;
6493 get_TIME(<ime, ptr, 0);
6494 val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
6495 uint length= (uint) my_date_to_str(<ime,
6496 const_cast<char*>(val_buffer->ptr()));
6497 val_buffer->length(length);
6498 val_buffer->set_charset(&my_charset_numeric);
6499
6500 return val_buffer;
6501 }
6502
6503
cmp(const uchar * a_ptr,const uchar * b_ptr)6504 int Field_date::cmp(const uchar *a_ptr, const uchar *b_ptr)
6505 {
6506 int32 a,b;
6507 a=sint4korr(a_ptr);
6508 b=sint4korr(b_ptr);
6509 return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
6510 }
6511
6512
sort_string(uchar * to,uint length)6513 void Field_date::sort_string(uchar *to,uint length __attribute__((unused)))
6514 {
6515 to[0] = ptr[3];
6516 to[1] = ptr[2];
6517 to[2] = ptr[1];
6518 to[3] = ptr[0];
6519 }
6520
sql_type(String & res) const6521 void Field_date::sql_type(String &res) const
6522 {
6523 res.set_ascii(STRING_WITH_LEN("date"));
6524 }
6525
6526
6527 /****************************************************************************
6528 ** The new date type
6529 ** This is identical to the old date type, but stored on 3 bytes instead of 4
6530 ** In number context: YYYYMMDD
6531 ****************************************************************************/
6532
store_TIME(MYSQL_TIME * ltime)6533 void Field_newdate::store_TIME(MYSQL_TIME *ltime)
6534 {
6535 uint tmp= ltime->year*16*32 + ltime->month*32+ltime->day;
6536 int3store(ptr,tmp);
6537 }
6538
6539
send_binary(Protocol * protocol)6540 bool Field_newdate::send_binary(Protocol *protocol)
6541 {
6542 MYSQL_TIME tm;
6543 Field_newdate::get_date(&tm,0);
6544 return protocol->store_date(&tm);
6545 }
6546
6547
val_real(void)6548 double Field_newdate::val_real(void)
6549 {
6550 ASSERT_COLUMN_MARKED_FOR_READ;
6551 return (double) Field_newdate::val_int();
6552 }
6553
6554
val_int(void)6555 longlong Field_newdate::val_int(void)
6556 {
6557 ASSERT_COLUMN_MARKED_FOR_READ;
6558 ulong j= uint3korr(ptr);
6559 j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
6560 return (longlong) j;
6561 }
6562
6563
val_str(String * val_buffer,String * val_ptr)6564 String *Field_newdate::val_str(String *val_buffer,
6565 String *val_ptr __attribute__((unused)))
6566 {
6567 ASSERT_COLUMN_MARKED_FOR_READ;
6568 val_buffer->alloc(field_length);
6569 val_buffer->length(field_length);
6570 uint32 tmp=(uint32) uint3korr(ptr);
6571 int part;
6572 char *pos=(char*) val_buffer->ptr()+10;
6573
6574 /* Open coded to get more speed */
6575 *pos--=0; // End NULL
6576 part=(int) (tmp & 31);
6577 *pos--= (char) ('0'+part%10);
6578 *pos--= (char) ('0'+part/10);
6579 *pos--= '-';
6580 part=(int) (tmp >> 5 & 15);
6581 *pos--= (char) ('0'+part%10);
6582 *pos--= (char) ('0'+part/10);
6583 *pos--= '-';
6584 part=(int) (tmp >> 9);
6585 *pos--= (char) ('0'+part%10); part/=10;
6586 *pos--= (char) ('0'+part%10); part/=10;
6587 *pos--= (char) ('0'+part%10); part/=10;
6588 *pos= (char) ('0'+part);
6589 val_buffer->set_charset(&my_charset_numeric);
6590 return val_buffer;
6591 }
6592
6593
get_TIME(MYSQL_TIME * ltime,const uchar * pos,ulonglong fuzzydate) const6594 bool Field_newdate::get_TIME(MYSQL_TIME *ltime, const uchar *pos,
6595 ulonglong fuzzydate) const
6596 {
6597 ASSERT_COLUMN_MARKED_FOR_READ;
6598 uint32 tmp=(uint32) uint3korr(pos);
6599 ltime->day= tmp & 31;
6600 ltime->month= (tmp >> 5) & 15;
6601 ltime->year= (tmp >> 9);
6602 ltime->time_type= MYSQL_TIMESTAMP_DATE;
6603 ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
6604 return validate_MMDD(tmp, ltime->month, ltime->day, fuzzydate);
6605 }
6606
6607
cmp(const uchar * a_ptr,const uchar * b_ptr)6608 int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
6609 {
6610 uint32 a,b;
6611 a=(uint32) uint3korr(a_ptr);
6612 b=(uint32) uint3korr(b_ptr);
6613 return (a < b) ? -1 : (a > b) ? 1 : 0;
6614 }
6615
6616
sort_string(uchar * to,uint length)6617 void Field_newdate::sort_string(uchar *to,uint length __attribute__((unused)))
6618 {
6619 to[0] = ptr[2];
6620 to[1] = ptr[1];
6621 to[2] = ptr[0];
6622 }
6623
6624
sql_type(String & res) const6625 void Field_newdate::sql_type(String &res) const
6626 {
6627 res.set_ascii(STRING_WITH_LEN("date"));
6628 }
6629
6630
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)6631 Item *Field_newdate::get_equal_const_item(THD *thd, const Context &ctx,
6632 Item *const_item)
6633 {
6634 switch (ctx.subst_constraint()) {
6635 case ANY_SUBST:
6636 if (!is_temporal_type_with_date(const_item->field_type()))
6637 {
6638 // Get the value of const_item with conversion from TIME to DATETIME
6639 Datetime dt(thd, const_item, TIME_FUZZY_DATES | TIME_INVALID_DATES);
6640 if (!dt.is_valid_datetime())
6641 return NULL;
6642 /*
6643 Replace the constant to a DATE or DATETIME constant.
6644 Example:
6645 WHERE LENGTH(date_column)=10
6646 AND date_column=TIME'10:20:30';
6647 to:
6648 WHERE LENGTH(date_column)=10
6649 AND date_column=TIMESTAMP'2015-08-30 10:20:30'
6650
6651 (assuming CURRENT_DATE is '2015-08-30'
6652 */
6653 if (!dt.hhmmssff_is_zero())
6654 return new (thd->mem_root)
6655 Item_datetime_literal_for_invalid_dates(thd, dt.get_mysql_time(),
6656 dt.get_mysql_time()->
6657 second_part ?
6658 TIME_SECOND_PART_DIGITS : 0);
6659 return new (thd->mem_root)
6660 Item_date_literal_for_invalid_dates(thd, Date(&dt).get_mysql_time());
6661 }
6662 break;
6663 case IDENTITY_SUBST:
6664 if (const_item->field_type() != MYSQL_TYPE_DATE)
6665 {
6666 Date d(thd, const_item, 0);
6667 if (!d.is_valid_date())
6668 return NULL;
6669 return new (thd->mem_root) Item_date_literal(thd, d.get_mysql_time());
6670 }
6671 break;
6672 }
6673 return const_item;
6674 }
6675
6676
6677 /****************************************************************************
6678 ** datetime type
6679 ** In string context: YYYY-MM-DD HH:MM:DD
6680 ** In number context: YYYYMMDDHHMMDD
6681 ** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
6682 ****************************************************************************/
6683
store_TIME(MYSQL_TIME * ltime)6684 void Field_datetime::store_TIME(MYSQL_TIME *ltime)
6685 {
6686 ulonglong tmp= TIME_to_ulonglong_datetime(ltime);
6687 int8store(ptr,tmp);
6688 }
6689
send_binary(Protocol * protocol)6690 bool Field_datetime::send_binary(Protocol *protocol)
6691 {
6692 MYSQL_TIME tm;
6693 Field_datetime::get_date(&tm, 0);
6694 return protocol->store(&tm, 0);
6695 }
6696
6697
val_real(void)6698 double Field_datetime::val_real(void)
6699 {
6700 return (double) Field_datetime::val_int();
6701 }
6702
val_int(void)6703 longlong Field_datetime::val_int(void)
6704 {
6705 ASSERT_COLUMN_MARKED_FOR_READ;
6706 longlong j;
6707 j=sint8korr(ptr);
6708 return j;
6709 }
6710
6711
val_str(String * val_buffer,String * val_ptr)6712 String *Field_datetime::val_str(String *val_buffer,
6713 String *val_ptr __attribute__((unused)))
6714 {
6715 val_buffer->alloc(field_length);
6716 val_buffer->length(field_length);
6717
6718 ASSERT_COLUMN_MARKED_FOR_READ;
6719 ulonglong tmp;
6720 long part1,part2;
6721 char *pos;
6722 int part3;
6723
6724 tmp= Field_datetime::val_int();
6725
6726 /*
6727 Avoid problem with slow longlong arithmetic and sprintf
6728 */
6729
6730 part1=(long) (tmp/1000000LL);
6731 part2=(long) (tmp - (ulonglong) part1*1000000LL);
6732
6733 pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
6734 *pos--=0;
6735 *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
6736 *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
6737 *pos--= ':';
6738 *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6739 *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6740 *pos--= ':';
6741 *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6742 *pos--= (char) ('0'+(char) part3);
6743 *pos--= ' ';
6744 *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
6745 *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
6746 *pos--= '-';
6747 *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
6748 *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
6749 *pos--= '-';
6750 *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6751 *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6752 *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6753 *pos=(char) ('0'+(char) part3);
6754 val_buffer->set_charset(&my_charset_numeric);
6755 return val_buffer;
6756 }
6757
get_TIME(MYSQL_TIME * ltime,const uchar * pos,ulonglong fuzzydate) const6758 bool Field_datetime::get_TIME(MYSQL_TIME *ltime, const uchar *pos,
6759 ulonglong fuzzydate) const
6760 {
6761 ASSERT_COLUMN_MARKED_FOR_READ;
6762 longlong tmp= sint8korr(pos);
6763 uint32 part1,part2;
6764 part1=(uint32) (tmp/1000000LL);
6765 part2=(uint32) (tmp - (ulonglong) part1*1000000LL);
6766
6767 ltime->time_type= MYSQL_TIMESTAMP_DATETIME;
6768 ltime->neg= 0;
6769 ltime->second_part= 0;
6770 ltime->second= (int) (part2%100);
6771 ltime->minute= (int) (part2/100%100);
6772 ltime->hour= (int) (part2/10000);
6773 ltime->day= (int) (part1%100);
6774 ltime->month= (int) (part1/100%100);
6775 ltime->year= (int) (part1/10000);
6776 return validate_MMDD(tmp, ltime->month, ltime->day, fuzzydate);
6777 }
6778
6779
cmp(const uchar * a_ptr,const uchar * b_ptr)6780 int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
6781 {
6782 longlong a,b;
6783 a=sint8korr(a_ptr);
6784 b=sint8korr(b_ptr);
6785 return ((ulonglong) a < (ulonglong) b) ? -1 :
6786 ((ulonglong) a > (ulonglong) b) ? 1 : 0;
6787 }
6788
sort_string(uchar * to,uint length)6789 void Field_datetime::sort_string(uchar *to,uint length __attribute__((unused)))
6790 {
6791 to[0] = ptr[7];
6792 to[1] = ptr[6];
6793 to[2] = ptr[5];
6794 to[3] = ptr[4];
6795 to[4] = ptr[3];
6796 to[5] = ptr[2];
6797 to[6] = ptr[1];
6798 to[7] = ptr[0];
6799 }
6800
6801
sql_type(String & res) const6802 void Field_datetime::sql_type(String &res) const
6803 {
6804 if (decimals() == 0)
6805 {
6806 res.set_ascii(STRING_WITH_LEN("datetime"));
6807 return;
6808 }
6809 CHARSET_INFO *cs= res.charset();
6810 res.length(cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
6811 "datetime(%u)", decimals()));
6812 }
6813
6814
set_time()6815 int Field_datetime::set_time()
6816 {
6817 THD *thd= table->in_use;
6818 MYSQL_TIME now_time;
6819 thd->variables.time_zone->gmt_sec_to_TIME(&now_time, thd->query_start());
6820 now_time.second_part= thd->query_start_sec_part();
6821 set_notnull();
6822 store_TIME(&now_time);
6823 thd->time_zone_used= 1;
6824 return 0;
6825 }
6826
6827
store_TIME(MYSQL_TIME * ltime)6828 void Field_datetime_hires::store_TIME(MYSQL_TIME *ltime)
6829 {
6830 ulonglong packed= sec_part_shift(pack_time(ltime), dec);
6831 store_bigendian(packed, ptr, Field_datetime_hires::pack_length());
6832 }
6833
store_decimal(const my_decimal * d)6834 int Field_temporal_with_date::store_decimal(const my_decimal *d)
6835 {
6836 ulonglong nr;
6837 ulong sec_part;
6838 int error;
6839 MYSQL_TIME ltime;
6840 longlong tmp;
6841 THD *thd= get_thd();
6842 ErrConvDecimal str(d);
6843
6844 if (my_decimal2seconds(d, &nr, &sec_part))
6845 {
6846 tmp= -1;
6847 error= 2;
6848 }
6849 else
6850 tmp= number_to_datetime(nr, sec_part, <ime, sql_mode_for_dates(thd),
6851 &error);
6852
6853 return store_TIME_with_warning(<ime, &str, error, tmp != -1);
6854 }
6855
send_binary(Protocol * protocol)6856 bool Field_datetime_with_dec::send_binary(Protocol *protocol)
6857 {
6858 MYSQL_TIME ltime;
6859 get_date(<ime, 0);
6860 return protocol->store(<ime, dec);
6861 }
6862
6863
val_real(void)6864 double Field_datetime_with_dec::val_real(void)
6865 {
6866 MYSQL_TIME ltime;
6867 get_date(<ime, 0);
6868 return TIME_to_double(<ime);
6869 }
6870
val_int(void)6871 longlong Field_datetime_with_dec::val_int(void)
6872 {
6873 MYSQL_TIME ltime;
6874 get_date(<ime, 0);
6875 return TIME_to_ulonglong_datetime(<ime);
6876 }
6877
6878
val_str(String * str,String * unused)6879 String *Field_datetime_with_dec::val_str(String *str,
6880 String *unused __attribute__((unused)))
6881 {
6882 MYSQL_TIME ltime;
6883 get_date(<ime, 0);
6884 str->alloc(field_length+1);
6885 str->length(field_length);
6886 my_datetime_to_str(<ime, (char*) str->ptr(), dec);
6887 str->set_charset(&my_charset_numeric);
6888 return str;
6889 }
6890
6891
get_TIME(MYSQL_TIME * ltime,const uchar * pos,ulonglong fuzzydate) const6892 bool Field_datetime_hires::get_TIME(MYSQL_TIME *ltime, const uchar *pos,
6893 ulonglong fuzzydate) const
6894 {
6895 ASSERT_COLUMN_MARKED_FOR_READ;
6896 ulonglong packed= read_bigendian(pos, Field_datetime_hires::pack_length());
6897 unpack_time(sec_part_unshift(packed, dec), ltime, MYSQL_TIMESTAMP_DATETIME);
6898 return validate_MMDD(packed, ltime->month, ltime->day, fuzzydate);
6899 }
6900
6901
cmp(const uchar * a_ptr,const uchar * b_ptr)6902 int Field_datetime_hires::cmp(const uchar *a_ptr, const uchar *b_ptr)
6903 {
6904 ulonglong a=read_bigendian(a_ptr, Field_datetime_hires::pack_length());
6905 ulonglong b=read_bigendian(b_ptr, Field_datetime_hires::pack_length());
6906 return a < b ? -1 : a > b ? 1 : 0;
6907 }
6908
make_send_field(Send_field * field)6909 void Field_datetime_with_dec::make_send_field(Send_field *field)
6910 {
6911 Field::make_send_field(field);
6912 field->decimals= dec;
6913 }
6914
6915
6916 /****************************************************************************
6917 ** MySQL-5.6 compatible DATETIME(N)
6918 **
6919 ****************************************************************************/
reset()6920 int Field_datetimef::reset()
6921 {
6922 my_datetime_packed_to_binary(0, ptr, dec);
6923 return 0;
6924 }
6925
store_TIME(MYSQL_TIME * ltime)6926 void Field_datetimef::store_TIME(MYSQL_TIME *ltime)
6927 {
6928 my_time_trunc(ltime, decimals());
6929 longlong tmp= TIME_to_longlong_datetime_packed(ltime);
6930 my_datetime_packed_to_binary(tmp, ptr, dec);
6931 }
6932
get_TIME(MYSQL_TIME * ltime,const uchar * pos,ulonglong fuzzydate) const6933 bool Field_datetimef::get_TIME(MYSQL_TIME *ltime, const uchar *pos,
6934 ulonglong fuzzydate) const
6935 {
6936 ASSERT_COLUMN_MARKED_FOR_READ;
6937 longlong tmp= my_datetime_packed_from_binary(pos, dec);
6938 TIME_from_longlong_datetime_packed(ltime, tmp);
6939 return validate_MMDD(tmp, ltime->month, ltime->day, fuzzydate);
6940 }
6941
6942 /****************************************************************************
6943 ** string type
6944 ** A string may be varchar or binary
6945 ****************************************************************************/
6946
6947 /*
6948 Report "not well formed" or "cannot convert" error
6949 after storing a character string info a field.
6950
6951 SYNOPSIS
6952 check_string_copy_error()
6953 copier - the conversion status
6954 end - the very end of the source string
6955 that was just copied
6956 cs - character set of the string
6957
6958 NOTES
6959 As of version 5.0 both cases return the same error:
6960
6961 "Invalid string value: 'xxx' for column 't' at row 1"
6962
6963 Future versions will possibly introduce a new error message:
6964
6965 "Cannot convert character string: 'xxx' for column 't' at row 1"
6966
6967 RETURN
6968 FALSE - If errors didn't happen
6969 TRUE - If an error happened
6970 */
6971
6972 bool
check_string_copy_error(const String_copier * copier,const char * end,CHARSET_INFO * cs)6973 Field_longstr::check_string_copy_error(const String_copier *copier,
6974 const char *end,
6975 CHARSET_INFO *cs)
6976 {
6977 const char *pos;
6978 char tmp[32];
6979
6980 if (likely(!(pos= copier->most_important_error_pos())))
6981 return FALSE;
6982
6983 /* Ignore errors from internal expressions */
6984 if (get_thd()->count_cuted_fields > CHECK_FIELD_EXPRESSION)
6985 {
6986 convert_to_printable(tmp, sizeof(tmp), pos, (end - pos), cs, 6);
6987 set_warning_truncated_wrong_value("string", tmp);
6988 }
6989 return TRUE;
6990 }
6991
6992
6993 /*
6994 Check if we lost any important data and send a truncation error/warning
6995
6996 SYNOPSIS
6997 Field_longstr::report_if_important_data()
6998 pstr - Truncated rest of string
6999 end - End of truncated string
7000 count_spaces - Treat traling spaces as important data
7001
7002 RETURN VALUES
7003 0 - None was truncated (or we don't count cut fields)
7004 2 - Some bytes was truncated
7005
7006 NOTE
7007 Check if we lost any important data (anything in a binary string,
7008 or any non-space in others). If only trailing spaces was lost,
7009 send a truncation note, otherwise send a truncation error.
7010 Silently ignore traling spaces if the count_space parameter is FALSE.
7011 */
7012
7013 int
report_if_important_data(const char * pstr,const char * end,bool count_spaces)7014 Field_longstr::report_if_important_data(const char *pstr, const char *end,
7015 bool count_spaces)
7016 {
7017 THD *thd;
7018 if ((pstr < end) &&
7019 (thd= get_thd())->count_cuted_fields > CHECK_FIELD_EXPRESSION)
7020 {
7021 if (test_if_important_data(field_charset, pstr, end))
7022 {
7023 if (thd->abort_on_warning)
7024 set_warning(ER_DATA_TOO_LONG, 1);
7025 else
7026 set_warning(WARN_DATA_TRUNCATED, 1);
7027 return 2;
7028 }
7029 else if (count_spaces)
7030 {
7031 /* If we lost only spaces then produce a NOTE, not a WARNING */
7032 set_note(WARN_DATA_TRUNCATED, 1);
7033 return 2;
7034 }
7035 }
7036 return 0;
7037 }
7038
7039
7040 /* Copy a string and fill with space */
7041
store(const char * from,size_t length,CHARSET_INFO * cs)7042 int Field_string::store(const char *from, size_t length,CHARSET_INFO *cs)
7043 {
7044 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
7045 uint copy_length;
7046 int rc;
7047
7048 /* See the comment for Field_long::store(long long) */
7049 DBUG_ASSERT(!table || table->in_use == current_thd);
7050
7051 rc= well_formed_copy_with_check((char*) ptr, field_length,
7052 cs, from, length,
7053 field_length / field_charset->mbmaxlen,
7054 false, ©_length);
7055
7056 /* Append spaces if the string was shorter than the field. */
7057 if (copy_length < field_length)
7058 field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
7059 field_length-copy_length,
7060 field_charset->pad_char);
7061
7062 return rc;
7063 }
7064
7065
store(longlong nr,bool unsigned_val)7066 int Field_str::store(longlong nr, bool unsigned_val)
7067 {
7068 char buff[64];
7069 uint length;
7070 length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
7071 buff,
7072 sizeof(buff),
7073 (unsigned_val ? 10:
7074 -10),
7075 nr);
7076 return store(buff, length, field_charset);
7077 }
7078
7079
7080 /**
7081 Store double value in Field_string or Field_varstring.
7082
7083 Pretty prints double number into field_length characters buffer.
7084
7085 @param nr number
7086 */
7087
store(double nr)7088 int Field_str::store(double nr)
7089 {
7090 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
7091 char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
7092 uint local_char_length= MY_MIN(sizeof(buff),
7093 field_length / field_charset->mbmaxlen);
7094 size_t length= 0;
7095 my_bool error= (local_char_length == 0);
7096
7097 // my_gcvt() requires width > 0, and we may have a CHAR(0) column.
7098 if (likely(!error))
7099 length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
7100
7101 if (unlikely(error))
7102 {
7103 if (get_thd()->abort_on_warning)
7104 set_warning(ER_DATA_TOO_LONG, 1);
7105 else
7106 set_warning(WARN_DATA_TRUNCATED, 1);
7107 }
7108 return store(buff, (uint)length, &my_charset_numeric);
7109 }
7110
is_equal(Create_field * new_field)7111 uint Field::is_equal(Create_field *new_field)
7112 {
7113 return new_field->type_handler() == type_handler();
7114 }
7115
7116
is_equal(Create_field * new_field)7117 uint Field_str::is_equal(Create_field *new_field)
7118 {
7119 return new_field->type_handler() == type_handler() &&
7120 new_field->charset == field_charset &&
7121 new_field->length == max_display_length();
7122 }
7123
7124
store_decimal(const my_decimal * d)7125 int Field_longstr::store_decimal(const my_decimal *d)
7126 {
7127 char buff[DECIMAL_MAX_STR_LENGTH+1];
7128 String str(buff, sizeof(buff), &my_charset_numeric);
7129 my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
7130 return store(str.ptr(), str.length(), str.charset());
7131 }
7132
max_data_length() const7133 uint32 Field_longstr::max_data_length() const
7134 {
7135 return field_length + (field_length > 255 ? 2 : 1);
7136 }
7137
7138
7139 bool
cmp_to_string_with_same_collation(const Item_bool_func * cond,const Item * item) const7140 Field_longstr::cmp_to_string_with_same_collation(const Item_bool_func *cond,
7141 const Item *item) const
7142 {
7143 return item->cmp_type() == STRING_RESULT &&
7144 charset() == cond->compare_collation();
7145 }
7146
7147
7148 bool
cmp_to_string_with_stricter_collation(const Item_bool_func * cond,const Item * item) const7149 Field_longstr::cmp_to_string_with_stricter_collation(const Item_bool_func *cond,
7150 const Item *item) const
7151 {
7152 return item->cmp_type() == STRING_RESULT &&
7153 (charset() == cond->compare_collation() ||
7154 cond->compare_collation()->state & MY_CS_BINSORT);
7155 }
7156
7157
can_optimize_keypart_ref(const Item_bool_func * cond,const Item * item) const7158 bool Field_longstr::can_optimize_keypart_ref(const Item_bool_func *cond,
7159 const Item *item) const
7160 {
7161 DBUG_ASSERT(cmp_type() == STRING_RESULT);
7162 return cmp_to_string_with_stricter_collation(cond, item);
7163 }
7164
7165
can_optimize_hash_join(const Item_bool_func * cond,const Item * item) const7166 bool Field_longstr::can_optimize_hash_join(const Item_bool_func *cond,
7167 const Item *item) const
7168 {
7169 DBUG_ASSERT(cmp_type() == STRING_RESULT);
7170 return cmp_to_string_with_same_collation(cond, item);
7171 }
7172
7173
can_optimize_group_min_max(const Item_bool_func * cond,const Item * const_item) const7174 bool Field_longstr::can_optimize_group_min_max(const Item_bool_func *cond,
7175 const Item *const_item) const
7176 {
7177 /*
7178 Can't use indexes when comparing a string to a number or a date
7179 Don't use an index when comparing strings of different collations.
7180 */
7181 DBUG_ASSERT(cmp_type() == STRING_RESULT);
7182 return cmp_to_string_with_same_collation(cond, const_item);
7183 }
7184
7185
can_optimize_range(const Item_bool_func * cond,const Item * item,bool is_eq_func) const7186 bool Field_longstr::can_optimize_range(const Item_bool_func *cond,
7187 const Item *item,
7188 bool is_eq_func) const
7189 {
7190 return is_eq_func ?
7191 cmp_to_string_with_stricter_collation(cond, item) :
7192 cmp_to_string_with_same_collation(cond, item);
7193 }
7194
7195
7196 /**
7197 This overrides the default behavior of the parent constructor
7198 Warn_filter(thd) to suppress notes about trailing spaces in case of CHAR(N),
7199 as they are truncated during val_str().
7200 We still do want truncation notes in case of BINARY(N),
7201 as trailing spaces are not truncated in val_str().
7202 */
Warn_filter_string(const THD * thd,const Field_string * field)7203 Field_string::Warn_filter_string::Warn_filter_string(const THD *thd,
7204 const Field_string *field)
7205 :Warn_filter(!thd->no_errors,
7206 !thd->no_errors &&
7207 field->Field_string::charset() == &my_charset_bin)
7208 { }
7209
7210
val_real(void)7211 double Field_string::val_real(void)
7212 {
7213 ASSERT_COLUMN_MARKED_FOR_READ;
7214 THD *thd= get_thd();
7215 return Converter_strntod_with_warn(get_thd(),
7216 Warn_filter_string(thd, this),
7217 Field_string::charset(),
7218 (const char *) ptr,
7219 field_length).result();
7220 }
7221
7222
val_int(void)7223 longlong Field_string::val_int(void)
7224 {
7225 ASSERT_COLUMN_MARKED_FOR_READ;
7226 THD *thd= get_thd();
7227 return Converter_strntoll_with_warn(thd, Warn_filter_string(thd, this),
7228 Field_string::charset(),
7229 (const char *) ptr,
7230 field_length).result();
7231 }
7232
7233
value_depends_on_sql_mode() const7234 sql_mode_t Field_string::value_depends_on_sql_mode() const
7235 {
7236 return has_charset() ? MODE_PAD_CHAR_TO_FULL_LENGTH : sql_mode_t(0);
7237 };
7238
7239
can_handle_sql_mode_dependency_on_store() const7240 sql_mode_t Field_string::can_handle_sql_mode_dependency_on_store() const
7241 {
7242 return has_charset() ? MODE_PAD_CHAR_TO_FULL_LENGTH : sql_mode_t(0);
7243 }
7244
7245
val_str(String * val_buffer,String * val_ptr)7246 String *Field_string::val_str(String *val_buffer __attribute__((unused)),
7247 String *val_ptr)
7248 {
7249 ASSERT_COLUMN_MARKED_FOR_READ;
7250 /* See the comment for Field_long::store(long long) */
7251 DBUG_ASSERT(!table || table->in_use == current_thd);
7252 size_t length;
7253 if (get_thd()->variables.sql_mode &
7254 MODE_PAD_CHAR_TO_FULL_LENGTH)
7255 length= my_charpos(field_charset, ptr, ptr + field_length,
7256 field_length / field_charset->mbmaxlen);
7257 else
7258 length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
7259 field_length);
7260 val_ptr->set((const char*) ptr, length, field_charset);
7261 return val_ptr;
7262 }
7263
7264
val_decimal(my_decimal * decimal_value)7265 my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
7266 {
7267 ASSERT_COLUMN_MARKED_FOR_READ;
7268 THD *thd= get_thd();
7269 Converter_str2my_decimal_with_warn(thd,
7270 Warn_filter_string(thd, this),
7271 E_DEC_FATAL_ERROR,
7272 Field_string::charset(),
7273 (const char *) ptr,
7274 field_length, decimal_value);
7275 return decimal_value;
7276 }
7277
7278
7279 struct Check_field_param {
7280 Field *field;
7281 };
7282
7283 #ifdef HAVE_REPLICATION
7284 static bool
check_field_for_37426(const void * param_arg)7285 check_field_for_37426(const void *param_arg)
7286 {
7287 Check_field_param *param= (Check_field_param*) param_arg;
7288 DBUG_ASSERT(param->field->real_type() == MYSQL_TYPE_STRING);
7289 DBUG_PRINT("debug", ("Field %s - type: %d, size: %d",
7290 param->field->field_name.str,
7291 param->field->real_type(),
7292 param->field->row_pack_length()));
7293 return param->field->row_pack_length() > 255;
7294 }
7295 #endif
7296
7297 bool
compatible_field_size(uint field_metadata,Relay_log_info * rli_arg,uint16 mflags,int * order_var)7298 Field_string::compatible_field_size(uint field_metadata,
7299 Relay_log_info *rli_arg,
7300 uint16 mflags __attribute__((unused)),
7301 int *order_var)
7302 {
7303 #ifdef HAVE_REPLICATION
7304 const Check_field_param check_param = { this };
7305 if (rpl_master_has_bug(rli_arg, 37426, TRUE,
7306 check_field_for_37426, &check_param))
7307 return FALSE; // Not compatible field sizes
7308 #endif
7309 return Field::compatible_field_size(field_metadata, rli_arg, mflags, order_var);
7310 }
7311
7312
cmp(const uchar * a_ptr,const uchar * b_ptr)7313 int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
7314 {
7315 size_t a_len, b_len;
7316
7317 if (field_charset->mbmaxlen != 1)
7318 {
7319 size_t char_len= field_length/field_charset->mbmaxlen;
7320 a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
7321 b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
7322 }
7323 else
7324 a_len= b_len= field_length;
7325 /*
7326 We have to remove end space to be able to compare multi-byte-characters
7327 like in latin_de 'ae' and 0xe4
7328 */
7329 return field_charset->coll->strnncollsp(field_charset,
7330 a_ptr, a_len,
7331 b_ptr, b_len);
7332 }
7333
7334
sort_string(uchar * to,uint length)7335 void Field_string::sort_string(uchar *to,uint length)
7336 {
7337 #ifdef DBUG_ASSERT_EXISTS
7338 size_t tmp=
7339 #endif
7340 field_charset->coll->strnxfrm(field_charset,
7341 to, length,
7342 char_length() *
7343 field_charset->strxfrm_multiply,
7344 ptr, field_length,
7345 MY_STRXFRM_PAD_WITH_SPACE |
7346 MY_STRXFRM_PAD_TO_MAXLEN);
7347 DBUG_ASSERT(tmp == length);
7348 }
7349
7350
sql_type(String & res) const7351 void Field_string::sql_type(String &res) const
7352 {
7353 THD *thd= table->in_use;
7354 CHARSET_INFO *cs=res.charset();
7355 size_t length;
7356
7357 length= cs->cset->snprintf(cs,(char*) res.ptr(),
7358 res.alloced_length(), "%s(%d)",
7359 (type() == MYSQL_TYPE_VAR_STRING ?
7360 (has_charset() ? "varchar" : "varbinary") :
7361 (has_charset() ? "char" : "binary")),
7362 (int) field_length / charset()->mbmaxlen);
7363 res.length(length);
7364 if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
7365 has_charset() && (charset()->state & MY_CS_BINSORT))
7366 res.append(STRING_WITH_LEN(" binary"));
7367 }
7368
7369 /**
7370 For fields which are associated with character sets their length is provided
7371 in octets and their character set information is also provided as part of
7372 type information.
7373
7374 @param res String which contains filed type and length.
7375 */
sql_rpl_type(String * res) const7376 void Field_string::sql_rpl_type(String *res) const
7377 {
7378 CHARSET_INFO *cs=charset();
7379 if (Field_string::has_charset())
7380 {
7381 size_t length= cs->cset->snprintf(cs, (char*) res->ptr(),
7382 res->alloced_length(),
7383 "char(%u octets) character set %s",
7384 field_length,
7385 charset()->csname);
7386 res->length(length);
7387 }
7388 else
7389 Field_string::sql_type(*res);
7390 }
7391
pack(uchar * to,const uchar * from,uint max_length)7392 uchar *Field_string::pack(uchar *to, const uchar *from, uint max_length)
7393 {
7394 size_t length= MY_MIN(field_length,max_length);
7395 size_t local_char_length= max_length/field_charset->mbmaxlen;
7396 DBUG_PRINT("debug", ("Packing field '%s' - length: %zu ", field_name.str,
7397 length));
7398
7399 if (length > local_char_length)
7400 local_char_length= my_charpos(field_charset, from, from+length,
7401 local_char_length);
7402 set_if_smaller(length, local_char_length);
7403
7404 /*
7405 TODO: change charset interface to add a new function that does
7406 the following or add a flag to lengthsp to do it itself
7407 (this is for not packing padding adding bytes in BINARY
7408 fields).
7409 */
7410 if (field_charset->mbmaxlen == 1)
7411 {
7412 while (length && from[length-1] == field_charset->pad_char)
7413 length --;
7414 }
7415 else
7416 length= field_charset->cset->lengthsp(field_charset, (const char*) from, length);
7417
7418 // Length always stored little-endian
7419 *to++= (uchar) length;
7420 if (field_length > 255)
7421 *to++= (uchar) (length >> 8);
7422
7423 // Store the actual bytes of the string
7424 memcpy(to, from, length);
7425 return to+length;
7426 }
7427
7428
7429 /**
7430 Unpack a string field from row data.
7431
7432 This method is used to unpack a string field from a master whose size
7433 of the field is less than that of the slave. Note that there can be a
7434 variety of field types represented with this class. Certain types like
7435 ENUM or SET are processed differently. Hence, the upper byte of the
7436 @c param_data argument contains the result of field->real_type() from
7437 the master.
7438
7439 @note For information about how the length is packed, see @c
7440 Field_string::save_field_metadata
7441
7442 @param to Destination of the data
7443 @param from Source of the data
7444 @param param_data Real type (upper) and length (lower) values
7445
7446 @return New pointer into memory based on from + length of the data
7447 */
7448 const uchar *
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)7449 Field_string::unpack(uchar *to, const uchar *from, const uchar *from_end,
7450 uint param_data)
7451 {
7452 uint from_length, length;
7453
7454 /*
7455 Compute the declared length of the field on the master. This is
7456 used to decide if one or two bytes should be read as length.
7457 */
7458 if (param_data)
7459 from_length= (((param_data >> 4) & 0x300) ^ 0x300) + (param_data & 0x00ff);
7460 else
7461 from_length= field_length;
7462
7463 DBUG_PRINT("debug",
7464 ("param_data: 0x%x, field_length: %u, from_length: %u",
7465 param_data, field_length, from_length));
7466 /*
7467 Compute the actual length of the data by reading one or two bits
7468 (depending on the declared field length on the master).
7469 */
7470 if (from_length > 255)
7471 {
7472 if (from + 2 > from_end)
7473 return 0;
7474 length= uint2korr(from);
7475 from+= 2;
7476 }
7477 else
7478 {
7479 if (from + 1 > from_end)
7480 return 0;
7481 length= (uint) *from++;
7482 }
7483 if (from + length > from_end || length > field_length)
7484 return 0;
7485
7486 memcpy(to, from, length);
7487 // Pad the string with the pad character of the fields charset
7488 field_charset->cset->fill(field_charset, (char*) to + length, field_length - length, field_charset->pad_char);
7489 return from+length;
7490 }
7491
7492
7493 /**
7494 Save the field metadata for string fields.
7495
7496 Saves the real type in the first byte and the field length in the
7497 second byte of the field metadata array at index of *metadata_ptr and
7498 *(metadata_ptr + 1).
7499
7500 @note In order to be able to handle lengths exceeding 255 and be
7501 backwards-compatible with pre-5.1.26 servers, an extra two bits of
7502 the length has been added to the metadata in such a way that if
7503 they are set, a new unrecognized type is generated. This will
7504 cause pre-5.1-26 servers to stop due to a field type mismatch,
7505 while new servers will be able to extract the extra bits. If the
7506 length is <256, there will be no difference and both a new and an
7507 old server will be able to handle it.
7508
7509 @note The extra two bits are added to bits 13 and 14 of the
7510 parameter data (with 1 being the least siginficant bit and 16 the
7511 most significant bit of the word) by xoring the extra length bits
7512 with the real type. Since all allowable types have 0xF as most
7513 significant bits of the metadata word, lengths <256 will not affect
7514 the real type at all, while all other values will result in a
7515 non-existent type in the range 17-244.
7516
7517 @see Field_string::unpack
7518
7519 @param metadata_ptr First byte of field metadata
7520
7521 @returns number of bytes written to metadata_ptr
7522 */
save_field_metadata(uchar * metadata_ptr)7523 int Field_string::save_field_metadata(uchar *metadata_ptr)
7524 {
7525 DBUG_ASSERT(field_length < 1024);
7526 DBUG_ASSERT((real_type() & 0xF0) == 0xF0);
7527 DBUG_PRINT("debug", ("field_length: %u, real_type: %u",
7528 field_length, real_type()));
7529 *metadata_ptr= (real_type() ^ ((field_length & 0x300) >> 4));
7530 *(metadata_ptr + 1)= field_length & 0xFF;
7531 return 2;
7532 }
7533
7534
packed_col_length(const uchar * data_ptr,uint length)7535 uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
7536 {
7537 if (length > 255)
7538 return uint2korr(data_ptr)+2;
7539 return (uint) *data_ptr + 1;
7540 }
7541
7542
max_packed_col_length(uint max_length)7543 uint Field_string::max_packed_col_length(uint max_length)
7544 {
7545 return (max_length > 255 ? 2 : 1)+max_length;
7546 }
7547
7548
get_key_image(uchar * buff,uint length,imagetype type_arg)7549 uint Field_string::get_key_image(uchar *buff, uint length, imagetype type_arg)
7550 {
7551 size_t bytes = my_charpos(field_charset, (char*) ptr,
7552 (char*) ptr + field_length,
7553 length / field_charset->mbmaxlen);
7554 memcpy(buff, ptr, bytes);
7555 if (bytes < length)
7556 field_charset->cset->fill(field_charset, (char*) buff + bytes,
7557 length - bytes, field_charset->pad_char);
7558 return (uint)bytes;
7559 }
7560
7561
make_new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type)7562 Field *Field_string::make_new_field(MEM_ROOT *root, TABLE *new_table,
7563 bool keep_type)
7564 {
7565 Field *field;
7566 if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
7567 field= Field::make_new_field(root, new_table, keep_type);
7568 else if ((field= new (root) Field_varstring(field_length, maybe_null(),
7569 &field_name,
7570 new_table->s, charset())))
7571 {
7572 /*
7573 Old VARCHAR field which should be modified to a VARCHAR on copy
7574 This is done to ensure that ALTER TABLE will convert old VARCHAR fields
7575 to now VARCHAR fields.
7576 */
7577 field->init_for_make_new_field(new_table, orig_table);
7578 }
7579 return field;
7580 }
7581
7582
7583 /****************************************************************************
7584 VARCHAR type
7585 Data in field->ptr is stored as:
7586 1 or 2 bytes length-prefix-header (from Field_varstring::length_bytes)
7587 data
7588
7589 NOTE:
7590 When VARCHAR is stored in a key (for handler::index_read() etc) it's always
7591 stored with a 2 byte prefix. (Just like blob keys).
7592
7593 Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
7594 The exception is if there is a prefix key field that is part of a long
7595 VARCHAR, in which case field_length for this may be 1 but the length_bytes
7596 is 2.
7597 ****************************************************************************/
7598
7599 const uint Field_varstring::MAX_SIZE= UINT_MAX16;
7600
7601 /**
7602 Save the field metadata for varstring fields.
7603
7604 Saves the field length in the first byte. Note: may consume
7605 2 bytes. Caller must ensure second byte is contiguous with
7606 first byte (e.g. array index 0,1).
7607
7608 @param metadata_ptr First byte of field metadata
7609
7610 @returns number of bytes written to metadata_ptr
7611 */
save_field_metadata(uchar * metadata_ptr)7612 int Field_varstring::save_field_metadata(uchar *metadata_ptr)
7613 {
7614 DBUG_ASSERT(field_length <= 65535);
7615 int2store((char*)metadata_ptr, field_length);
7616 return 2;
7617 }
7618
7619
memcpy_field_possible(const Field * from) const7620 bool Field_varstring::memcpy_field_possible(const Field *from) const
7621 {
7622 return (Field_str::memcpy_field_possible(from) &&
7623 !compression_method() == !from->compression_method() &&
7624 length_bytes == ((Field_varstring*) from)->length_bytes &&
7625 (table->file && !(table->file->ha_table_flags() &
7626 HA_RECORD_MUST_BE_CLEAN_ON_WRITE)));
7627 }
7628
7629
store(const char * from,size_t length,CHARSET_INFO * cs)7630 int Field_varstring::store(const char *from,size_t length,CHARSET_INFO *cs)
7631 {
7632 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
7633 uint copy_length;
7634 int rc;
7635
7636 rc= well_formed_copy_with_check((char*) get_data(), field_length,
7637 cs, from, length,
7638 field_length / field_charset->mbmaxlen,
7639 true, ©_length);
7640
7641 store_length(copy_length);
7642
7643 return rc;
7644 }
7645
7646
val_real(void)7647 double Field_varstring::val_real(void)
7648 {
7649 ASSERT_COLUMN_MARKED_FOR_READ;
7650 THD *thd= get_thd();
7651 return Converter_strntod_with_warn(thd, Warn_filter(thd),
7652 Field_varstring::charset(),
7653 (const char *) get_data(),
7654 get_length()).result();
7655 }
7656
7657
val_int(void)7658 longlong Field_varstring::val_int(void)
7659 {
7660 ASSERT_COLUMN_MARKED_FOR_READ;
7661 THD *thd= get_thd();
7662 return Converter_strntoll_with_warn(thd, Warn_filter(thd),
7663 Field_varstring::charset(),
7664 (const char *) get_data(),
7665 get_length()).result();
7666 }
7667
7668
val_str(String * val_buffer,String * val_ptr)7669 String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
7670 String *val_ptr)
7671 {
7672 ASSERT_COLUMN_MARKED_FOR_READ;
7673 val_ptr->set((const char*) get_data(), get_length(), field_charset);
7674 return val_ptr;
7675 }
7676
7677
val_decimal(my_decimal * decimal_value)7678 my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
7679 {
7680 ASSERT_COLUMN_MARKED_FOR_READ;
7681 THD *thd= get_thd();
7682 Converter_str2my_decimal_with_warn(thd, Warn_filter(thd),
7683 E_DEC_FATAL_ERROR,
7684 Field_varstring::charset(),
7685 (const char *) get_data(),
7686 get_length(), decimal_value);
7687 return decimal_value;
7688
7689 }
7690
7691
7692 #ifdef HAVE_valgrind
mark_unused_memory_as_defined()7693 void Field_varstring::mark_unused_memory_as_defined()
7694 {
7695 uint used_length __attribute__((unused)) = get_length();
7696 MEM_MAKE_DEFINED(get_data() + used_length, field_length - used_length);
7697 }
7698 #endif
7699
7700
cmp(const uchar * a_ptr,const uchar * b_ptr)7701 int Field_varstring::cmp(const uchar *a_ptr, const uchar *b_ptr)
7702 {
7703 uint a_length, b_length;
7704 int diff;
7705
7706 if (length_bytes == 1)
7707 {
7708 a_length= (uint) *a_ptr;
7709 b_length= (uint) *b_ptr;
7710 }
7711 else
7712 {
7713 a_length= uint2korr(a_ptr);
7714 b_length= uint2korr(b_ptr);
7715 }
7716 set_if_smaller(a_length, field_length);
7717 set_if_smaller(b_length, field_length);
7718 diff= field_charset->coll->strnncollsp(field_charset,
7719 a_ptr+
7720 length_bytes,
7721 a_length,
7722 b_ptr+
7723 length_bytes,
7724 b_length);
7725 return diff;
7726 }
7727
7728
cmp_str_prefix(const uchar * ua,size_t alen,const uchar * ub,size_t blen,size_t prefix,CHARSET_INFO * cs)7729 static int cmp_str_prefix(const uchar *ua, size_t alen, const uchar *ub,
7730 size_t blen, size_t prefix, CHARSET_INFO *cs)
7731 {
7732 const char *a= (char*)ua, *b= (char*)ub;
7733 MY_STRCOPY_STATUS status;
7734 prefix/= cs->mbmaxlen;
7735 alen= cs->cset->well_formed_char_length(cs, a, a + alen, prefix, &status);
7736 blen= cs->cset->well_formed_char_length(cs, b, b + blen, prefix, &status);
7737 return cs->coll->strnncollsp(cs, ua, alen, ub, blen);
7738 }
7739
7740
7741
cmp_prefix(const uchar * a_ptr,const uchar * b_ptr,size_t prefix_len)7742 int Field_varstring::cmp_prefix(const uchar *a_ptr, const uchar *b_ptr,
7743 size_t prefix_len)
7744 {
7745 /* avoid expensive well_formed_char_length if possible */
7746 if (prefix_len == table->field[field_index]->field_length)
7747 return Field_varstring::cmp(a_ptr, b_ptr);
7748
7749 size_t a_length, b_length;
7750
7751 if (length_bytes == 1)
7752 {
7753 a_length= *a_ptr;
7754 b_length= *b_ptr;
7755 }
7756 else
7757 {
7758 a_length= uint2korr(a_ptr);
7759 b_length= uint2korr(b_ptr);
7760 }
7761 return cmp_str_prefix(a_ptr+length_bytes, a_length, b_ptr+length_bytes,
7762 b_length, prefix_len, field_charset);
7763 }
7764
7765
7766 /**
7767 @note
7768 varstring and blob keys are ALWAYS stored with a 2 byte length prefix
7769 */
7770
key_cmp(const uchar * key_ptr,uint max_key_length)7771 int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
7772 {
7773 size_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7774 size_t local_char_length= max_key_length / field_charset->mbmaxlen;
7775
7776 local_char_length= my_charpos(field_charset, ptr + length_bytes,
7777 ptr + length_bytes + length, local_char_length);
7778 set_if_smaller(length, local_char_length);
7779 return field_charset->coll->strnncollsp(field_charset,
7780 ptr + length_bytes,
7781 length,
7782 key_ptr+
7783 HA_KEY_BLOB_LENGTH,
7784 uint2korr(key_ptr));
7785 }
7786
7787
7788 /**
7789 Compare to key segments (always 2 byte length prefix).
7790
7791 @note
7792 This is used only to compare key segments created for index_read().
7793 (keys are created and compared in key.cc)
7794 */
7795
key_cmp(const uchar * a,const uchar * b)7796 int Field_varstring::key_cmp(const uchar *a,const uchar *b)
7797 {
7798 return field_charset->coll->strnncollsp(field_charset,
7799 a + HA_KEY_BLOB_LENGTH,
7800 uint2korr(a),
7801 b + HA_KEY_BLOB_LENGTH,
7802 uint2korr(b));
7803 }
7804
7805
sort_string(uchar * to,uint length)7806 void Field_varstring::sort_string(uchar *to,uint length)
7807 {
7808 String buf;
7809
7810 val_str(&buf, &buf);
7811
7812 if (field_charset == &my_charset_bin)
7813 {
7814 /* Store length last in high-byte order to sort longer strings first */
7815 if (length_bytes == 1)
7816 to[length - 1]= buf.length();
7817 else
7818 mi_int2store(to + length - 2, buf.length());
7819 length-= length_bytes;
7820 }
7821
7822 #ifdef DBUG_ASSERT_EXISTS
7823 size_t rc=
7824 #endif
7825 field_charset->coll->strnxfrm(field_charset, to, length,
7826 char_length() * field_charset->strxfrm_multiply,
7827 (const uchar*) buf.ptr(), buf.length(),
7828 MY_STRXFRM_PAD_WITH_SPACE |
7829 MY_STRXFRM_PAD_TO_MAXLEN);
7830 DBUG_ASSERT(rc == length);
7831 }
7832
7833
key_type() const7834 enum ha_base_keytype Field_varstring::key_type() const
7835 {
7836 enum ha_base_keytype res;
7837
7838 if (binary())
7839 res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
7840 else
7841 res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
7842 return res;
7843 }
7844
7845
7846 /*
7847 Compressed columns need one extra byte to store the compression method.
7848 This byte is invisible to the end user, but not for the storage engine.
7849 */
7850
sql_type(String & res) const7851 void Field_varstring::sql_type(String &res) const
7852 {
7853 THD *thd= table->in_use;
7854 CHARSET_INFO *cs=res.charset();
7855 size_t length;
7856
7857 length= cs->cset->snprintf(cs,(char*) res.ptr(),
7858 res.alloced_length(), "%s(%u)",
7859 (has_charset() ? "varchar" : "varbinary"),
7860 (uint) char_length());
7861 res.length(length);
7862 if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
7863 has_charset() && (charset()->state & MY_CS_BINSORT))
7864 res.append(STRING_WITH_LEN(" binary"));
7865 }
7866
7867 /**
7868 For fields which are associated with character sets their length is provided
7869 in octets and their character set information is also provided as part of
7870 type information.
7871
7872 @param res String which contains filed type and length.
7873 */
sql_rpl_type(String * res) const7874 void Field_varstring::sql_rpl_type(String *res) const
7875 {
7876 CHARSET_INFO *cs=charset();
7877 if (Field_varstring::has_charset())
7878 {
7879 size_t length= cs->cset->snprintf(cs, (char*) res->ptr(),
7880 res->alloced_length(),
7881 "varchar(%u octets) character set %s",
7882 field_length,
7883 charset()->csname);
7884 res->length(length);
7885 }
7886 else
7887 Field_varstring::sql_type(*res);
7888 }
7889
7890
data_length()7891 uint32 Field_varstring::data_length()
7892 {
7893 return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
7894 }
7895
7896 /*
7897 Functions to create a packed row.
7898 Here the number of length bytes are depending on the given max_length
7899 */
7900
pack(uchar * to,const uchar * from,uint max_length)7901 uchar *Field_varstring::pack(uchar *to, const uchar *from, uint max_length)
7902 {
7903 uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
7904 set_if_smaller(max_length, field_length);
7905 if (length > max_length)
7906 length=max_length;
7907
7908 /* Length always stored little-endian */
7909 *to++= length & 0xFF;
7910 if (max_length > 255)
7911 *to++= (length >> 8) & 0xFF;
7912
7913 /* Store bytes of string */
7914 if (length > 0)
7915 memcpy(to, from+length_bytes, length);
7916 return to+length;
7917 }
7918
7919
7920 /**
7921 Unpack a varstring field from row data.
7922
7923 This method is used to unpack a varstring field from a master
7924 whose size of the field is less than that of the slave.
7925
7926 @note
7927 The string length is always packed little-endian.
7928
7929 @param to Destination of the data
7930 @param from Source of the data
7931 @param param_data Length bytes from the master's field data
7932
7933 @return New pointer into memory based on from + length of the data
7934 */
7935 const uchar *
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)7936 Field_varstring::unpack(uchar *to, const uchar *from, const uchar *from_end,
7937 uint param_data)
7938 {
7939 uint length;
7940 uint l_bytes= (param_data && (param_data < field_length)) ?
7941 (param_data <= 255) ? 1 : 2 : length_bytes;
7942
7943 if (from + l_bytes > from_end)
7944 return 0; // Error in data
7945
7946 if (l_bytes == 1)
7947 {
7948 to[0]= *from++;
7949 length= to[0];
7950 if (length_bytes == 2)
7951 to[1]= 0;
7952 }
7953 else /* l_bytes == 2 */
7954 {
7955 length= uint2korr(from);
7956 to[0]= *from++;
7957 to[1]= *from++;
7958 }
7959 if (length)
7960 {
7961 if (from + length > from_end || length > field_length)
7962 return 0; // Error in data
7963 memcpy(to+ length_bytes, from, length);
7964 }
7965 return from+length;
7966 }
7967
7968
packed_col_length(const uchar * data_ptr,uint length)7969 uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
7970 {
7971 if (length > 255)
7972 return uint2korr(data_ptr)+2;
7973 return (uint) *data_ptr + 1;
7974 }
7975
7976
max_packed_col_length(uint max_length)7977 uint Field_varstring::max_packed_col_length(uint max_length)
7978 {
7979 return (max_length > 255 ? 2 : 1)+max_length;
7980 }
7981
get_key_image(uchar * buff,uint length,imagetype type_arg)7982 uint Field_varstring::get_key_image(uchar *buff, uint length,
7983 imagetype type_arg)
7984 {
7985 String val;
7986 uint local_char_length;
7987
7988 MY_BITMAP *old_map= dbug_tmp_use_all_columns(table, &table->read_set);
7989 val_str(&val, &val);
7990 dbug_tmp_restore_column_map(&table->read_set, old_map);
7991
7992 local_char_length= val.charpos(length / field_charset->mbmaxlen);
7993 if (local_char_length < val.length())
7994 val.length(local_char_length);
7995 /* Key is always stored with 2 bytes */
7996 int2store(buff, val.length());
7997 memcpy(buff + HA_KEY_BLOB_LENGTH, val.ptr(), val.length());
7998 if (val.length() < length)
7999 {
8000 /*
8001 Must clear this as we do a memcmp in opt_range.cc to detect
8002 identical keys
8003 */
8004 memset(buff + HA_KEY_BLOB_LENGTH + val.length(), 0, length - val.length());
8005 }
8006 return HA_KEY_BLOB_LENGTH + val.length();
8007 }
8008
8009
set_key_image(const uchar * buff,uint length)8010 void Field_varstring::set_key_image(const uchar *buff,uint length)
8011 {
8012 length= uint2korr(buff); // Real length is here
8013 (void) store((const char*) buff + HA_KEY_BLOB_LENGTH, length, field_charset);
8014 }
8015
8016
cmp_binary(const uchar * a_ptr,const uchar * b_ptr,uint32 max_length)8017 int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
8018 uint32 max_length)
8019 {
8020 uint32 a_length,b_length;
8021
8022 if (length_bytes == 1)
8023 {
8024 a_length= (uint) *a_ptr;
8025 b_length= (uint) *b_ptr;
8026 }
8027 else
8028 {
8029 a_length= uint2korr(a_ptr);
8030 b_length= uint2korr(b_ptr);
8031 }
8032 set_if_smaller(a_length, max_length);
8033 set_if_smaller(b_length, max_length);
8034 if (a_length != b_length)
8035 return 1;
8036 return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
8037 }
8038
8039
make_new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type)8040 Field *Field_varstring::make_new_field(MEM_ROOT *root, TABLE *new_table,
8041 bool keep_type)
8042 {
8043 Field_varstring *res= (Field_varstring*) Field::make_new_field(root,
8044 new_table,
8045 keep_type);
8046 if (res)
8047 res->length_bytes= length_bytes;
8048 return res;
8049 }
8050
8051
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uint32 length,uchar * new_null_ptr,uint new_null_bit)8052 Field *Field_varstring::new_key_field(MEM_ROOT *root, TABLE *new_table,
8053 uchar *new_ptr, uint32 length,
8054 uchar *new_null_ptr, uint new_null_bit)
8055 {
8056 Field_varstring *res;
8057 if ((res= (Field_varstring*) Field::new_key_field(root, new_table,
8058 new_ptr, length,
8059 new_null_ptr, new_null_bit)))
8060 {
8061 /* Keys length prefixes are always packed with 2 bytes */
8062 res->length_bytes= 2;
8063 }
8064 return res;
8065 }
8066
is_equal(Create_field * new_field)8067 uint Field_varstring::is_equal(Create_field *new_field)
8068 {
8069 if (new_field->type_handler() == type_handler() &&
8070 new_field->charset == field_charset &&
8071 !new_field->compression_method() == !compression_method())
8072 {
8073 if (new_field->length == field_length)
8074 return IS_EQUAL_YES;
8075 if (new_field->length > field_length &&
8076 ((new_field->length <= 255 && field_length <= 255) ||
8077 (new_field->length > 255 && field_length > 255)))
8078 return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
8079 }
8080 return IS_EQUAL_NO;
8081 }
8082
8083
hash(ulong * nr,ulong * nr2)8084 void Field_varstring::hash(ulong *nr, ulong *nr2)
8085 {
8086 if (is_null())
8087 {
8088 *nr^= (*nr << 1) | 1;
8089 }
8090 else
8091 {
8092 uint len= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
8093 CHARSET_INFO *cs= charset();
8094 cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
8095 }
8096 }
8097
8098
8099 /**
8100 Compress field
8101
8102 @param[out] to destination buffer for compressed data
8103 @param[in] to_length size of to
8104 @param[in] from data to compress
8105 @param[in] length from length
8106 @param[in] max_length truncate `from' to this length
8107 @param[out] out_length compessed data length
8108 @param[in] cs from character set
8109 @param[in] nchars copy no more than "nchars" characters
8110
8111 In worst case (no compression performed) storage requirement is increased by
8112 1 byte to store header. If it exceeds field length, normal data truncation is
8113 performed.
8114
8115 Generic compressed header format (1 byte):
8116
8117 Bits 1-4: method specific bits
8118 Bits 5-8: compression method
8119
8120 If compression method is 0 then header is immediately followed by
8121 uncompressed data.
8122
8123 If compression method is zlib:
8124
8125 Bits 1-3: number of bytes occupied by original data length
8126 Bits 4: true if zlib wrapper not present
8127 Bits 5-8: store 8 (zlib)
8128
8129 Header is immediately followed by original data length,
8130 followed by compressed data.
8131 */
8132
compress(char * to,uint to_length,const char * from,uint length,uint max_length,uint * out_length,CHARSET_INFO * cs,size_t nchars)8133 int Field_longstr::compress(char *to, uint to_length,
8134 const char *from, uint length,
8135 uint max_length,
8136 uint *out_length,
8137 CHARSET_INFO *cs, size_t nchars)
8138 {
8139 THD *thd= get_thd();
8140 char *buf;
8141 uint buf_length;
8142 int rc= 0;
8143
8144 if (String::needs_conversion_on_storage(length, cs, field_charset) ||
8145 max_length < length)
8146 {
8147 set_if_smaller(max_length, static_cast<ulonglong>(field_charset->mbmaxlen) * length + 1);
8148 if (!(buf= (char*) my_malloc(max_length, MYF(MY_WME))))
8149 {
8150 *out_length= 0;
8151 return -1;
8152 }
8153
8154 rc= well_formed_copy_with_check(buf, max_length, cs, from, length,
8155 nchars, true, &buf_length);
8156 }
8157 else
8158 {
8159 buf= const_cast<char*>(from);
8160 buf_length= length;
8161 }
8162
8163 if (buf_length == 0)
8164 *out_length= 0;
8165 else if (buf_length >= thd->variables.column_compression_threshold &&
8166 (*out_length= compression_method()->compress(thd, to, buf, buf_length)))
8167 status_var_increment(thd->status_var.column_compressions);
8168 else
8169 {
8170 /* Store uncompressed */
8171 to[0]= 0;
8172 if (buf_length < to_length)
8173 memcpy(to + 1, buf, buf_length);
8174 else
8175 {
8176 /* Storing string at blob capacity, e.g. 255 bytes string to TINYBLOB. */
8177 rc= well_formed_copy_with_check(to + 1, to_length - 1, cs, from, length,
8178 nchars, true, &buf_length);
8179 }
8180 *out_length= buf_length + 1;
8181 }
8182
8183 if (buf != from)
8184 my_free(buf);
8185 return rc;
8186 }
8187
8188
8189 /*
8190 Memory is allocated only when original data was actually compressed.
8191 Otherwise val_ptr points at data located immediately after header.
8192
8193 Data can be stored uncompressed if data was shorter than threshold
8194 or compressed data was longer than original data.
8195 */
8196
uncompress(String * val_buffer,String * val_ptr,const uchar * from,uint from_length)8197 String *Field_longstr::uncompress(String *val_buffer, String *val_ptr,
8198 const uchar *from, uint from_length)
8199 {
8200 if (from_length)
8201 {
8202 uchar method= (*from & 0xF0) >> 4;
8203
8204 /* Uncompressed data */
8205 if (!method)
8206 {
8207 val_ptr->set((const char*) from + 1, from_length - 1, field_charset);
8208 return val_ptr;
8209 }
8210
8211 if (compression_methods[method].uncompress)
8212 {
8213 if (!compression_methods[method].uncompress(val_buffer, from, from_length,
8214 field_length))
8215 {
8216 val_buffer->set_charset(field_charset);
8217 status_var_increment(get_thd()->status_var.column_decompressions);
8218 return val_buffer;
8219 }
8220 }
8221 }
8222
8223 /*
8224 It would be better to return 0 in case of errors, but to take the
8225 safer route, let's return a zero string and let the general
8226 handler catch the error.
8227 */
8228 val_ptr->set("", 0, field_charset);
8229 return val_ptr;
8230 }
8231
8232
store(const char * from,size_t length,CHARSET_INFO * cs)8233 int Field_varstring_compressed::store(const char *from, size_t length,
8234 CHARSET_INFO *cs)
8235 {
8236 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
8237 uint compressed_length;
8238 int rc= compress((char*) get_data(), field_length, from, (uint) length,
8239 Field_varstring_compressed::max_display_length(),
8240 &compressed_length, cs,
8241 Field_varstring_compressed::char_length());
8242 store_length(compressed_length);
8243 return rc;
8244 }
8245
8246
val_str(String * val_buffer,String * val_ptr)8247 String *Field_varstring_compressed::val_str(String *val_buffer, String *val_ptr)
8248 {
8249 ASSERT_COLUMN_MARKED_FOR_READ;
8250 return uncompress(val_buffer, val_ptr, get_data(), get_length());
8251 }
8252
8253
val_real(void)8254 double Field_varstring_compressed::val_real(void)
8255 {
8256 ASSERT_COLUMN_MARKED_FOR_READ;
8257 THD *thd= get_thd();
8258 String buf;
8259 val_str(&buf, &buf);
8260 return Converter_strntod_with_warn(thd, Warn_filter(thd), field_charset,
8261 buf.ptr(), buf.length()).result();
8262 }
8263
8264
val_int(void)8265 longlong Field_varstring_compressed::val_int(void)
8266 {
8267 ASSERT_COLUMN_MARKED_FOR_READ;
8268 THD *thd= get_thd();
8269 String buf;
8270 val_str(&buf, &buf);
8271 return Converter_strntoll_with_warn(thd, Warn_filter(thd), field_charset,
8272 buf.ptr(), buf.length()).result();
8273 }
8274
8275
cmp(const uchar * a_ptr,const uchar * b_ptr)8276 int Field_varstring_compressed::cmp(const uchar *a_ptr, const uchar *b_ptr)
8277 {
8278 String a, b;
8279 uint a_length, b_length;
8280
8281 if (length_bytes == 1)
8282 {
8283 a_length= (uint) *a_ptr;
8284 b_length= (uint) *b_ptr;
8285 }
8286 else
8287 {
8288 a_length= uint2korr(a_ptr);
8289 b_length= uint2korr(b_ptr);
8290 }
8291
8292 uncompress(&a, &a, a_ptr + length_bytes, a_length);
8293 uncompress(&b, &b, b_ptr + length_bytes, b_length);
8294
8295 return sortcmp(&a, &b, field_charset);
8296 }
8297
8298
8299 /****************************************************************************
8300 ** blob type
8301 ** A blob is saved as a length and a pointer. The length is stored in the
8302 ** packlength slot and may be from 1-4.
8303 ****************************************************************************/
8304
Field_blob(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint blob_pack_length,const DTCollation & collation)8305 Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
8306 enum utype unireg_check_arg,
8307 const LEX_CSTRING *field_name_arg,
8308 TABLE_SHARE *share, uint blob_pack_length,
8309 const DTCollation &collation)
8310 :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
8311 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
8312 collation),
8313 packlength(blob_pack_length)
8314 {
8315 DBUG_ASSERT(blob_pack_length <= 4); // Only pack lengths 1-4 supported currently
8316 flags|= BLOB_FLAG;
8317 share->blob_fields++;
8318 /* TODO: why do not fill table->s->blob_field array here? */
8319 }
8320
8321
store_length(uchar * i_ptr,uint i_packlength,uint32 i_number)8322 void Field_blob::store_length(uchar *i_ptr, uint i_packlength, uint32 i_number)
8323 {
8324 store_lowendian(i_number, i_ptr, i_packlength);
8325 }
8326
8327
get_length(const uchar * pos,uint packlength_arg) const8328 uint32 Field_blob::get_length(const uchar *pos, uint packlength_arg) const
8329 {
8330 return (uint32)read_lowendian(pos, packlength_arg);
8331 }
8332
8333
8334 /**
8335 Copy a value from another BLOB field of the same character set.
8336 This method is used by Copy_field, e.g. during ALTER TABLE.
8337 */
copy_value(Field_blob * from)8338 int Field_blob::copy_value(Field_blob *from)
8339 {
8340 DBUG_ASSERT(field_charset == from->charset());
8341 DBUG_ASSERT(!compression_method() == !from->compression_method());
8342 int rc= 0;
8343 uint32 length= from->get_length();
8344 uchar *data= from->get_ptr();
8345 if (packlength < from->packlength)
8346 {
8347 set_if_smaller(length, Field_blob::max_data_length());
8348 length= (uint32) Well_formed_prefix(field_charset,
8349 (const char *) data, length).length();
8350 rc= report_if_important_data((const char *) data + length,
8351 (const char *) data + from->get_length(),
8352 true);
8353 }
8354 store_length(length);
8355 bmove(ptr + packlength, (uchar*) &data, sizeof(char*));
8356 return rc;
8357 }
8358
8359
store(const char * from,size_t length,CHARSET_INFO * cs)8360 int Field_blob::store(const char *from,size_t length,CHARSET_INFO *cs)
8361 {
8362 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
8363 size_t copy_length, new_length;
8364 uint copy_len;
8365 char *tmp;
8366 char buff[STRING_BUFFER_USUAL_SIZE];
8367 String tmpstr(buff,sizeof(buff), &my_charset_bin);
8368 int rc;
8369
8370 if (!length)
8371 {
8372 bzero(ptr,Field_blob::pack_length());
8373 return 0;
8374 }
8375
8376 /*
8377 For min/max fields of statistical data 'table' is set to NULL.
8378 It could not be otherwise as this data is shared by many instances
8379 of the same base table.
8380 */
8381
8382 if (table && table->blob_storage) // GROUP_CONCAT with ORDER BY | DISTINCT
8383 {
8384 DBUG_ASSERT(!f_is_hex_escape(flags));
8385 DBUG_ASSERT(field_charset == cs);
8386 DBUG_ASSERT(length <= max_data_length());
8387
8388 new_length= length;
8389 copy_length= table->in_use->variables.group_concat_max_len;
8390 if (new_length > copy_length)
8391 {
8392 new_length= Well_formed_prefix(cs,
8393 from, copy_length, new_length).length();
8394 table->blob_storage->set_truncated_value(true);
8395 }
8396 if (!(tmp= table->blob_storage->store(from, new_length)))
8397 goto oom_error;
8398
8399 Field_blob::store_length(new_length);
8400 bmove(ptr + packlength, (uchar*) &tmp, sizeof(char*));
8401 return 0;
8402 }
8403
8404 /*
8405 If the 'from' address is in the range of the temporary 'value'-
8406 object we need to copy the content to a different location or it will be
8407 invalidated when the 'value'-object is reallocated to make room for
8408 the new character set.
8409 */
8410 if (from >= value.ptr() && from <= value.ptr()+value.length())
8411 {
8412 /*
8413 If content of the 'from'-address is cached in the 'value'-object
8414 it is possible that the content needs a character conversion.
8415 */
8416 if (!String::needs_conversion_on_storage(length, cs, field_charset))
8417 {
8418 Field_blob::store_length(length);
8419 bmove(ptr + packlength, &from, sizeof(char*));
8420 return 0;
8421 }
8422 if (tmpstr.copy(from, length, cs))
8423 goto oom_error;
8424 from= tmpstr.ptr();
8425 }
8426
8427 new_length= MY_MIN(max_data_length(), field_charset->mbmaxlen * length);
8428 if (value.alloc(new_length))
8429 goto oom_error;
8430 tmp= const_cast<char*>(value.ptr());
8431
8432 if (f_is_hex_escape(flags))
8433 {
8434 copy_length= my_copy_with_hex_escaping(field_charset,
8435 tmp, new_length,
8436 from, length);
8437 Field_blob::store_length(copy_length);
8438 bmove(ptr + packlength, (uchar*) &tmp, sizeof(char*));
8439 return 0;
8440 }
8441 rc= well_formed_copy_with_check((char*) value.ptr(), (uint) new_length,
8442 cs, from, length,
8443 length, true, ©_len);
8444 value.length(copy_len);
8445 Field_blob::store_length(copy_len);
8446 bmove(ptr+packlength,(uchar*) &tmp,sizeof(char*));
8447
8448 return rc;
8449
8450 oom_error:
8451 /* Fatal OOM error */
8452 bzero(ptr,Field_blob::pack_length());
8453 return -1;
8454 }
8455
8456
val_real(void)8457 double Field_blob::val_real(void)
8458 {
8459 ASSERT_COLUMN_MARKED_FOR_READ;
8460 char *blob;
8461 memcpy(&blob, ptr+packlength, sizeof(char*));
8462 if (!blob)
8463 return 0.0;
8464 THD *thd= get_thd();
8465 return Converter_strntod_with_warn(thd, Warn_filter(thd),
8466 Field_blob::charset(),
8467 blob, get_length(ptr)).result();
8468 }
8469
8470
val_int(void)8471 longlong Field_blob::val_int(void)
8472 {
8473 ASSERT_COLUMN_MARKED_FOR_READ;
8474 char *blob;
8475 memcpy(&blob, ptr+packlength, sizeof(char*));
8476 if (!blob)
8477 return 0;
8478 THD *thd= get_thd();
8479 return Converter_strntoll_with_warn(thd, Warn_filter(thd),
8480 Field_blob::charset(),
8481 blob, get_length(ptr)).result();
8482 }
8483
8484
val_str(String * val_buffer,String * val_ptr)8485 String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
8486 String *val_ptr)
8487 {
8488 ASSERT_COLUMN_MARKED_FOR_READ;
8489 char *blob;
8490 memcpy(&blob, ptr+packlength, sizeof(char*));
8491 if (!blob)
8492 val_ptr->set("",0,charset()); // A bit safer than ->length(0)
8493 else
8494 val_ptr->set((const char*) blob,get_length(ptr),charset());
8495 return val_ptr;
8496 }
8497
8498
val_decimal(my_decimal * decimal_value)8499 my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
8500 {
8501 ASSERT_COLUMN_MARKED_FOR_READ;
8502 const char *blob;
8503 size_t length;
8504 memcpy(&blob, ptr+packlength, sizeof(const uchar*));
8505 if (!blob)
8506 {
8507 blob= "";
8508 length= 0;
8509 }
8510 else
8511 length= get_length(ptr);
8512
8513 THD *thd= get_thd();
8514 Converter_str2my_decimal_with_warn(thd, Warn_filter(thd),
8515 E_DEC_FATAL_ERROR,
8516 Field_blob::charset(),
8517 blob, length, decimal_value);
8518 return decimal_value;
8519 }
8520
8521
cmp(const uchar * a,uint32 a_length,const uchar * b,uint32 b_length)8522 int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
8523 uint32 b_length)
8524 {
8525 return field_charset->coll->strnncollsp(field_charset,
8526 a, a_length, b, b_length);
8527 }
8528
8529
cmp(const uchar * a_ptr,const uchar * b_ptr)8530 int Field_blob::cmp(const uchar *a_ptr, const uchar *b_ptr)
8531 {
8532 uchar *blob1,*blob2;
8533 memcpy(&blob1, a_ptr+packlength, sizeof(char*));
8534 memcpy(&blob2, b_ptr+packlength, sizeof(char*));
8535 size_t a_len= get_length(a_ptr), b_len= get_length(b_ptr);
8536 return cmp(blob1, (uint32)a_len, blob2, (uint32)b_len);
8537 }
8538
8539
cmp_prefix(const uchar * a_ptr,const uchar * b_ptr,size_t prefix_len)8540 int Field_blob::cmp_prefix(const uchar *a_ptr, const uchar *b_ptr,
8541 size_t prefix_len)
8542 {
8543 uchar *blob1,*blob2;
8544 memcpy(&blob1, a_ptr+packlength, sizeof(char*));
8545 memcpy(&blob2, b_ptr+packlength, sizeof(char*));
8546 size_t a_len= get_length(a_ptr), b_len= get_length(b_ptr);
8547 return cmp_str_prefix(blob1, a_len, blob2, b_len, prefix_len, field_charset);
8548 }
8549
8550
cmp_binary(const uchar * a_ptr,const uchar * b_ptr,uint32 max_length)8551 int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
8552 uint32 max_length)
8553 {
8554 char *a,*b;
8555 uint diff;
8556 uint32 a_length,b_length;
8557 memcpy(&a, a_ptr+packlength, sizeof(char*));
8558 memcpy(&b, b_ptr+packlength, sizeof(char*));
8559 a_length=get_length(a_ptr);
8560 if (a_length > max_length)
8561 a_length=max_length;
8562 b_length=get_length(b_ptr);
8563 if (b_length > max_length)
8564 b_length=max_length;
8565 if (uint32 len= MY_MIN(a_length,b_length))
8566 diff= memcmp(a,b,len);
8567 else
8568 diff= 0;
8569 return diff ? diff : (int) (a_length - b_length);
8570 }
8571
8572
8573 /* The following is used only when comparing a key */
8574
get_key_image(uchar * buff,uint length,imagetype type_arg)8575 uint Field_blob::get_key_image(uchar *buff,uint length, imagetype type_arg)
8576 {
8577 size_t blob_length= get_length(ptr);
8578 uchar *blob;
8579
8580 #ifdef HAVE_SPATIAL
8581 if (type_arg == itMBR)
8582 {
8583 const char *dummy;
8584 MBR mbr;
8585 Geometry_buffer buffer;
8586 Geometry *gobj;
8587 const uint image_length= SIZEOF_STORED_DOUBLE*4;
8588
8589 if (blob_length < SRID_SIZE)
8590 {
8591 bzero(buff, image_length);
8592 return image_length;
8593 }
8594 blob= get_ptr();
8595 gobj= Geometry::construct(&buffer, (char*) blob, (uint32)blob_length);
8596 if (!gobj || gobj->get_mbr(&mbr, &dummy))
8597 bzero(buff, image_length);
8598 else
8599 {
8600 float8store(buff, mbr.xmin);
8601 float8store(buff+8, mbr.xmax);
8602 float8store(buff+16, mbr.ymin);
8603 float8store(buff+24, mbr.ymax);
8604 }
8605 return image_length;
8606 }
8607 #endif /*HAVE_SPATIAL*/
8608
8609 blob= get_ptr();
8610 size_t local_char_length= length / field_charset->mbmaxlen;
8611 local_char_length= my_charpos(field_charset, blob, blob + blob_length,
8612 local_char_length);
8613 set_if_smaller(blob_length, local_char_length);
8614
8615 if (length > blob_length)
8616 {
8617 /*
8618 Must clear this as we do a memcmp in opt_range.cc to detect
8619 identical keys
8620 */
8621 bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
8622 length=(uint) blob_length;
8623 }
8624 int2store(buff,length);
8625 if (length)
8626 memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
8627 return HA_KEY_BLOB_LENGTH+length;
8628 }
8629
8630
set_key_image(const uchar * buff,uint length)8631 void Field_blob::set_key_image(const uchar *buff,uint length)
8632 {
8633 length= uint2korr(buff);
8634 (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
8635 field_charset);
8636 }
8637
8638
key_cmp(const uchar * key_ptr,uint max_key_length)8639 int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
8640 {
8641 uchar *blob1;
8642 size_t blob_length=get_length(ptr);
8643 memcpy(&blob1, ptr+packlength, sizeof(char*));
8644 CHARSET_INFO *cs= charset();
8645 size_t local_char_length= max_key_length / cs->mbmaxlen;
8646 local_char_length= my_charpos(cs, blob1, blob1+blob_length,
8647 local_char_length);
8648 set_if_smaller(blob_length, local_char_length);
8649 return Field_blob::cmp(blob1, (uint32)blob_length,
8650 key_ptr+HA_KEY_BLOB_LENGTH,
8651 uint2korr(key_ptr));
8652 }
8653
key_cmp(const uchar * a,const uchar * b)8654 int Field_blob::key_cmp(const uchar *a,const uchar *b)
8655 {
8656 return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
8657 b+HA_KEY_BLOB_LENGTH, uint2korr(b));
8658 }
8659
8660
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uint32 length,uchar * new_null_ptr,uint new_null_bit)8661 Field *Field_blob::new_key_field(MEM_ROOT *root, TABLE *new_table,
8662 uchar *new_ptr, uint32 length,
8663 uchar *new_null_ptr, uint new_null_bit)
8664 {
8665 Field_varstring *res= new (root) Field_varstring(new_ptr, length, 2,
8666 new_null_ptr,
8667 new_null_bit, Field::NONE,
8668 &field_name,
8669 table->s, charset());
8670 res->init(new_table);
8671 return res;
8672 }
8673
8674
8675 /**
8676 Save the field metadata for blob fields.
8677
8678 Saves the pack length in the first byte of the field metadata array
8679 at index of *metadata_ptr.
8680
8681 @param metadata_ptr First byte of field metadata
8682
8683 @returns number of bytes written to metadata_ptr
8684 */
save_field_metadata(uchar * metadata_ptr)8685 int Field_blob::save_field_metadata(uchar *metadata_ptr)
8686 {
8687 DBUG_ENTER("Field_blob::save_field_metadata");
8688 *metadata_ptr= pack_length_no_ptr();
8689 DBUG_PRINT("debug", ("metadata: %u (pack_length_no_ptr)", *metadata_ptr));
8690 DBUG_RETURN(1);
8691 }
8692
8693
sort_length() const8694 uint32 Field_blob::sort_length() const
8695 {
8696 return (uint32) (get_thd()->variables.max_sort_length +
8697 (field_charset == &my_charset_bin ? 0 : packlength));
8698 }
8699
8700
sort_string(uchar * to,uint length)8701 void Field_blob::sort_string(uchar *to,uint length)
8702 {
8703 String buf;
8704
8705 val_str(&buf, &buf);
8706 if (!buf.length() && field_charset->pad_char == 0)
8707 bzero(to,length);
8708 else
8709 {
8710 if (field_charset == &my_charset_bin)
8711 {
8712 /*
8713 Store length of blob last in blob to shorter blobs before longer blobs
8714 */
8715 length-= packlength;
8716 store_bigendian(buf.length(), to + length, packlength);
8717 }
8718
8719 #ifdef DBUG_ASSERT_EXISTS
8720 size_t rc=
8721 #endif
8722 field_charset->coll->strnxfrm(field_charset, to, length, length,
8723 (const uchar*) buf.ptr(), buf.length(),
8724 MY_STRXFRM_PAD_WITH_SPACE |
8725 MY_STRXFRM_PAD_TO_MAXLEN);
8726 DBUG_ASSERT(rc == length);
8727 }
8728 }
8729
8730
8731 /*
8732 Return the data type handler, according to packlength.
8733 Implemented in field.cc rather than in field.h
8734 to avoid exporting type_handler_xxx with MYSQL_PLUGIN_IMPORT.
8735 */
type_handler() const8736 const Type_handler *Field_blob::type_handler() const
8737 {
8738 switch (packlength) {
8739 case 1: return &type_handler_tiny_blob;
8740 case 2: return &type_handler_blob;
8741 case 3: return &type_handler_medium_blob;
8742 }
8743 return &type_handler_long_blob;
8744 }
8745
8746
sql_type(String & res) const8747 void Field_blob::sql_type(String &res) const
8748 {
8749 const char *str;
8750 uint length;
8751 switch (packlength) {
8752 default: str="tiny"; length=4; break;
8753 case 2: str=""; length=0; break;
8754 case 3: str="medium"; length= 6; break;
8755 case 4: str="long"; length=4; break;
8756 }
8757 res.set_ascii(str,length);
8758 if (charset() == &my_charset_bin)
8759 {
8760 res.append(STRING_WITH_LEN("blob"));
8761 if (packlength == 2 && (get_thd()->variables.sql_mode & MODE_ORACLE))
8762 res.append(STRING_WITH_LEN("(65535)"));
8763 }
8764 else
8765 {
8766 res.append(STRING_WITH_LEN("text"));
8767 }
8768 }
8769
pack(uchar * to,const uchar * from,uint max_length)8770 uchar *Field_blob::pack(uchar *to, const uchar *from, uint max_length)
8771 {
8772 uchar *save= ptr;
8773 ptr= (uchar*) from;
8774 uint32 length=get_length(); // Length of from string
8775
8776 /*
8777 Store max length, which will occupy packlength bytes. If the max
8778 length given is smaller than the actual length of the blob, we
8779 just store the initial bytes of the blob.
8780 */
8781 store_length(to, packlength, MY_MIN(length, max_length));
8782
8783 /*
8784 Store the actual blob data, which will occupy 'length' bytes.
8785 */
8786 if (length > 0)
8787 {
8788 from= get_ptr();
8789 memcpy(to+packlength, from,length);
8790 }
8791 ptr=save; // Restore org row pointer
8792 return to+packlength+length;
8793 }
8794
8795
8796 /**
8797 Unpack a blob field from row data.
8798
8799 This method is used to unpack a blob field from a master whose size of
8800 the field is less than that of the slave. Note: This method is included
8801 to satisfy inheritance rules, but is not needed for blob fields. It
8802 simply is used as a pass-through to the original unpack() method for
8803 blob fields.
8804
8805 @param to Destination of the data
8806 @param from Source of the data
8807 @param param_data @c TRUE if base types should be stored in little-
8808 endian format, @c FALSE if native format should
8809 be used.
8810
8811 @return New pointer into memory based on from + length of the data
8812 */
8813
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)8814 const uchar *Field_blob::unpack(uchar *to, const uchar *from,
8815 const uchar *from_end, uint param_data)
8816 {
8817 DBUG_ENTER("Field_blob::unpack");
8818 DBUG_PRINT("enter", ("to: %p; from: %p; param_data: %u",
8819 to, from, param_data));
8820 uint const master_packlength=
8821 param_data > 0 ? param_data & 0xFF : packlength;
8822 if (from + master_packlength > from_end)
8823 DBUG_RETURN(0); // Error in data
8824 uint32 const length= get_length(from, master_packlength);
8825 DBUG_DUMP("packed", from, length + master_packlength);
8826 if (from + master_packlength + length > from_end)
8827 DBUG_RETURN(0);
8828 set_ptr(length, const_cast<uchar*> (from) + master_packlength);
8829 DBUG_RETURN(from + master_packlength + length);
8830 }
8831
8832
packed_col_length(const uchar * data_ptr,uint length)8833 uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
8834 {
8835 if (length > 255)
8836 return uint2korr(data_ptr)+2;
8837 return (uint) *data_ptr + 1;
8838 }
8839
8840
max_packed_col_length(uint max_length)8841 uint Field_blob::max_packed_col_length(uint max_length)
8842 {
8843 return (max_length > 255 ? 2 : 1)+max_length;
8844 }
8845
8846
8847 /*
8848 Blob fields are regarded equal if they have same character set,
8849 same blob store length and if either both are compressed or both are
8850 uncompressed.
8851 The logic for compression is that we don't have to uncompress and compress
8852 again an already compressed field just because compression method changes.
8853 */
8854
is_equal(Create_field * new_field)8855 uint Field_blob::is_equal(Create_field *new_field)
8856 {
8857 return new_field->type_handler() == type_handler() &&
8858 new_field->charset == field_charset &&
8859 new_field->pack_length == pack_length() &&
8860 !new_field->compression_method() == !compression_method();
8861 }
8862
8863
store(const char * from,size_t length,CHARSET_INFO * cs)8864 int Field_blob_compressed::store(const char *from, size_t length,
8865 CHARSET_INFO *cs)
8866 {
8867 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
8868 uint compressed_length;
8869 uint max_length= max_data_length();
8870 uint to_length= (uint) MY_MIN(max_length,
8871 field_charset->mbmaxlen * length + 1);
8872 String tmp(from, length, cs);
8873 int rc;
8874
8875 if (from >= value.ptr() && from <= value.end() && tmp.copy(from, length, cs))
8876 goto oom;
8877
8878 if (value.alloc(to_length))
8879 goto oom;
8880
8881 rc= compress((char*) value.ptr(), to_length, tmp.ptr(), (uint) length,
8882 max_length, &compressed_length, cs, (uint) length);
8883 set_ptr(compressed_length, (uchar*) value.ptr());
8884 return rc;
8885
8886 oom:
8887 set_ptr((uint32) 0, NULL);
8888 return -1;
8889 }
8890
8891
val_str(String * val_buffer,String * val_ptr)8892 String *Field_blob_compressed::val_str(String *val_buffer, String *val_ptr)
8893 {
8894 ASSERT_COLUMN_MARKED_FOR_READ;
8895 return uncompress(val_buffer, val_ptr, get_ptr(), get_length());
8896 }
8897
8898
val_real(void)8899 double Field_blob_compressed::val_real(void)
8900 {
8901 ASSERT_COLUMN_MARKED_FOR_READ;
8902 THD *thd= get_thd();
8903 String buf;
8904 val_str(&buf, &buf);
8905 return Converter_strntod_with_warn(thd, Warn_filter(thd), field_charset,
8906 buf.ptr(), buf.length()).result();
8907 }
8908
8909
val_int(void)8910 longlong Field_blob_compressed::val_int(void)
8911 {
8912 ASSERT_COLUMN_MARKED_FOR_READ;
8913 THD *thd= get_thd();
8914 String buf;
8915 val_str(&buf, &buf);
8916 return Converter_strntoll_with_warn(thd, Warn_filter(thd), field_charset,
8917 buf.ptr(), buf.length()).result();
8918 }
8919
8920
8921 #ifdef HAVE_SPATIAL
8922 /* Values 1-40 reserved for 1-byte options,
8923 41-80 for 2-byte options,
8924 81-120 for 4-byte options,
8925 121-160 for 8-byte options,
8926 other - varied length in next 1-3 bytes.
8927 */
8928 enum extra2_gis_field_options {
8929 FIELDGEOM_END=0,
8930 FIELDGEOM_STORAGE_MODEL=1,
8931 FIELDGEOM_PRECISION=2,
8932 FIELDGEOM_SCALE=3,
8933 FIELDGEOM_SRID=81,
8934 };
8935
8936
gis_field_options_image(uchar * buff,List<Create_field> & create_fields)8937 uint gis_field_options_image(uchar *buff, List<Create_field> &create_fields)
8938 {
8939 uint image_size= 0;
8940 List_iterator<Create_field> it(create_fields);
8941 Create_field *field;
8942 while ((field= it++))
8943 {
8944 if (field->real_field_type() != MYSQL_TYPE_GEOMETRY)
8945 continue;
8946 if (buff)
8947 {
8948 uchar *cbuf= buff + image_size;
8949
8950 cbuf[0]= FIELDGEOM_STORAGE_MODEL;
8951 cbuf[1]= (uchar) Field_geom::GEOM_STORAGE_WKB;
8952
8953 cbuf[2]= FIELDGEOM_PRECISION;
8954 cbuf[3]= (uchar) field->length;
8955
8956 cbuf[4]= FIELDGEOM_SCALE;
8957 cbuf[5]= (uchar) field->decimals;
8958
8959 cbuf[6]= FIELDGEOM_SRID;
8960 int4store(cbuf + 7, ((uint32) field->srid));
8961
8962 cbuf[11]= FIELDGEOM_END;
8963 }
8964 image_size+= 12;
8965 }
8966
8967 return image_size;
8968 }
8969
8970
gis_field_options_read(const uchar * buf,size_t buf_len,Field_geom::storage_type * st_type,uint * precision,uint * scale,uint * srid)8971 uint gis_field_options_read(const uchar *buf, size_t buf_len,
8972 Field_geom::storage_type *st_type,uint *precision, uint *scale, uint *srid)
8973 {
8974 const uchar *buf_end= buf + buf_len;
8975 const uchar *cbuf= buf;
8976 int option_id;
8977
8978 *precision= *scale= *srid= 0;
8979 *st_type= Field_geom::GEOM_STORAGE_WKB;
8980
8981 if (!buf) /* can only happen with the old FRM file */
8982 goto end_of_record;
8983
8984 while (cbuf < buf_end)
8985 {
8986 switch ((option_id= *(cbuf++)))
8987 {
8988 case FIELDGEOM_STORAGE_MODEL:
8989 *st_type= (Field_geom::storage_type) cbuf[0];
8990 break;
8991 case FIELDGEOM_PRECISION:
8992 *precision= cbuf[0];
8993 break;
8994 case FIELDGEOM_SCALE:
8995 *scale= cbuf[0];
8996 break;
8997 case FIELDGEOM_SRID:
8998 *srid= uint4korr(cbuf);
8999 break;
9000 case FIELDGEOM_END:
9001 goto end_of_record;
9002 }
9003 if (option_id > 0 && option_id <= 40)
9004 cbuf+= 1;
9005 else if (option_id > 40 && option_id <= 80)
9006 cbuf+= 2;
9007 else if (option_id > 80 && option_id <= 120)
9008 cbuf+= 4;
9009 else if (option_id > 120 && option_id <= 160)
9010 cbuf+= 8;
9011 else /* > 160 and <=255 */
9012 cbuf+= cbuf[0] ? 1 + cbuf[0] : 3 + uint2korr(cbuf+1);
9013 }
9014
9015 end_of_record:
9016 return (uint)(cbuf - buf);
9017 }
9018
9019
9020
sql_type(String & res) const9021 void Field_geom::sql_type(String &res) const
9022 {
9023 CHARSET_INFO *cs= &my_charset_latin1;
9024 switch (geom_type)
9025 {
9026 case GEOM_POINT:
9027 res.set(STRING_WITH_LEN("point"), cs);
9028 break;
9029 case GEOM_LINESTRING:
9030 res.set(STRING_WITH_LEN("linestring"), cs);
9031 break;
9032 case GEOM_POLYGON:
9033 res.set(STRING_WITH_LEN("polygon"), cs);
9034 break;
9035 case GEOM_MULTIPOINT:
9036 res.set(STRING_WITH_LEN("multipoint"), cs);
9037 break;
9038 case GEOM_MULTILINESTRING:
9039 res.set(STRING_WITH_LEN("multilinestring"), cs);
9040 break;
9041 case GEOM_MULTIPOLYGON:
9042 res.set(STRING_WITH_LEN("multipolygon"), cs);
9043 break;
9044 case GEOM_GEOMETRYCOLLECTION:
9045 res.set(STRING_WITH_LEN("geometrycollection"), cs);
9046 break;
9047 default:
9048 res.set(STRING_WITH_LEN("geometry"), cs);
9049 }
9050 }
9051
9052
store(double nr)9053 int Field_geom::store(double nr)
9054 {
9055 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
9056 ER_THD(get_thd(), ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
9057 return -1;
9058 }
9059
9060
store(longlong nr,bool unsigned_val)9061 int Field_geom::store(longlong nr, bool unsigned_val)
9062 {
9063 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
9064 ER_THD(get_thd(), ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
9065 return -1;
9066 }
9067
9068
store_decimal(const my_decimal *)9069 int Field_geom::store_decimal(const my_decimal *)
9070 {
9071 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
9072 ER_THD(get_thd(), ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
9073 return -1;
9074 }
9075
9076
store(const char * from,size_t length,CHARSET_INFO * cs)9077 int Field_geom::store(const char *from, size_t length, CHARSET_INFO *cs)
9078 {
9079 if (!length)
9080 bzero(ptr, Field_blob::pack_length());
9081 else
9082 {
9083 if (from == Geometry::bad_geometry_data.ptr())
9084 goto err;
9085 // Check given WKB
9086 uint32 wkb_type;
9087 if (length < SRID_SIZE + WKB_HEADER_SIZE + 4)
9088 goto err;
9089 wkb_type= uint4korr(from + SRID_SIZE + 1);
9090 if (wkb_type < (uint32) Geometry::wkb_point ||
9091 wkb_type > (uint32) Geometry::wkb_last)
9092 goto err;
9093
9094 if (geom_type != Field::GEOM_GEOMETRY &&
9095 geom_type != Field::GEOM_GEOMETRYCOLLECTION &&
9096 (uint32) geom_type != wkb_type)
9097 {
9098 const char *db= table->s->db.str;
9099 const char *tab_name= table->s->table_name.str;
9100 Geometry_buffer buffer;
9101 Geometry *geom= NULL;
9102 String wkt;
9103 const char *dummy;
9104
9105 if (!db)
9106 db= "";
9107 if (!tab_name)
9108 tab_name= "";
9109 wkt.set_charset(&my_charset_latin1);
9110 if (!(geom= Geometry::construct(&buffer, from, uint32(length))) ||
9111 geom->as_wkt(&wkt, &dummy))
9112 goto err;
9113
9114 my_error(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, MYF(0),
9115 Geometry::ci_collection[geom_type]->m_name.str,
9116 wkt.c_ptr_safe(), db, tab_name, field_name.str,
9117 (ulong) table->in_use->get_stmt_da()->
9118 current_row_for_warning());
9119
9120 goto err_exit;
9121 }
9122
9123 Field_blob::store_length(length);
9124 if ((table->copy_blobs || length <= MAX_FIELD_WIDTH) &&
9125 from != value.ptr())
9126 { // Must make a copy
9127 value.copy(from, length, cs);
9128 from= value.ptr();
9129 }
9130 bmove(ptr + packlength, &from, sizeof(char*));
9131 }
9132 return 0;
9133
9134 err:
9135 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
9136 ER_THD(get_thd(), ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
9137 err_exit:
9138 bzero(ptr, Field_blob::pack_length());
9139 return -1;
9140 }
9141
geometry_type_merge(geometry_type a,geometry_type b)9142 Field::geometry_type Field_geom::geometry_type_merge(geometry_type a,
9143 geometry_type b)
9144 {
9145 if (a == b)
9146 return a;
9147 return Field::GEOM_GEOMETRY;
9148 }
9149
9150
is_equal(Create_field * new_field)9151 uint Field_geom::is_equal(Create_field *new_field)
9152 {
9153 return new_field->type_handler() == type_handler() &&
9154 /*
9155 - Allow ALTER..INPLACE to supertype (GEOMETRY),
9156 e.g. POINT to GEOMETRY or POLYGON to GEOMETRY.
9157 - Allow ALTER..INPLACE to the same geometry type: POINT -> POINT
9158 */
9159 (new_field->geom_type == geom_type ||
9160 new_field->geom_type == GEOM_GEOMETRY);
9161 }
9162
9163
can_optimize_range(const Item_bool_func * cond,const Item * item,bool is_eq_func) const9164 bool Field_geom::can_optimize_range(const Item_bool_func *cond,
9165 const Item *item,
9166 bool is_eq_func) const
9167 {
9168 return item->cmp_type() == STRING_RESULT;
9169 }
9170
9171
load_data_set_no_data(THD * thd,bool fixed_format)9172 bool Field_geom::load_data_set_no_data(THD *thd, bool fixed_format)
9173 {
9174 return Field_geom::load_data_set_null(thd);
9175 }
9176
9177
load_data_set_null(THD * thd)9178 bool Field_geom::load_data_set_null(THD *thd)
9179 {
9180 Field_blob::reset();
9181 if (!maybe_null())
9182 {
9183 my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0), field_name.str,
9184 thd->get_stmt_da()->current_row_for_warning());
9185 return true;
9186 }
9187 set_null();
9188 set_has_explicit_value(); // Do not auto-update this field
9189 return false;
9190 }
9191
9192
9193 #endif /*HAVE_SPATIAL*/
9194
9195 /****************************************************************************
9196 ** enum type.
9197 ** This is a string which only can have a selection of different values.
9198 ** If one uses this string in a number context one gets the type number.
9199 ****************************************************************************/
9200
can_handle_sql_mode_dependency_on_store() const9201 sql_mode_t Field_enum::can_handle_sql_mode_dependency_on_store() const
9202 {
9203 return MODE_PAD_CHAR_TO_FULL_LENGTH;
9204 }
9205
9206
key_type() const9207 enum ha_base_keytype Field_enum::key_type() const
9208 {
9209 switch (packlength) {
9210 default: return HA_KEYTYPE_BINARY;
9211 case 2: return HA_KEYTYPE_USHORT_INT;
9212 case 3: return HA_KEYTYPE_UINT24;
9213 case 4: return HA_KEYTYPE_ULONG_INT;
9214 case 8: return HA_KEYTYPE_ULONGLONG;
9215 }
9216 }
9217
store_type(ulonglong value)9218 void Field_enum::store_type(ulonglong value)
9219 {
9220 store_lowendian(value, ptr, packlength);
9221 }
9222
9223
9224 /**
9225 @note
9226 Storing a empty string in a enum field gives a warning
9227 (if there isn't a empty value in the enum)
9228 */
9229
store(const char * from,size_t length,CHARSET_INFO * cs)9230 int Field_enum::store(const char *from,size_t length,CHARSET_INFO *cs)
9231 {
9232 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
9233 int err= 0;
9234 char buff[STRING_BUFFER_USUAL_SIZE];
9235 String tmpstr(buff,sizeof(buff), &my_charset_bin);
9236
9237 /* Convert character set if necessary */
9238 if (String::needs_conversion_on_storage(length, cs, field_charset))
9239 {
9240 uint dummy_errors;
9241 tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
9242 from= tmpstr.ptr();
9243 length= tmpstr.length();
9244 }
9245
9246 /* Remove end space */
9247 length= (uint)field_charset->cset->lengthsp(field_charset, from, length);
9248 uint tmp=find_type2(typelib, from, length, field_charset);
9249 if (!tmp)
9250 {
9251 if (length < 6) // Can't be more than 99999 enums
9252 {
9253 /* This is for reading numbers with LOAD DATA INFILE */
9254 char *end;
9255 tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
9256 if (err || end != from+length || tmp > typelib->count)
9257 {
9258 tmp=0;
9259 set_warning(WARN_DATA_TRUNCATED, 1);
9260 err= 1;
9261 }
9262 if ((get_thd()->count_cuted_fields <= CHECK_FIELD_EXPRESSION) && !length)
9263 err= 0;
9264 }
9265 else
9266 {
9267 set_warning(WARN_DATA_TRUNCATED, 1);
9268 err= 1;
9269 }
9270 }
9271 store_type((ulonglong) tmp);
9272 return err;
9273 }
9274
9275
store(double nr)9276 int Field_enum::store(double nr)
9277 {
9278 return Field_enum::store((longlong) nr, FALSE);
9279 }
9280
9281
store(longlong nr,bool unsigned_val)9282 int Field_enum::store(longlong nr, bool unsigned_val)
9283 {
9284 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
9285 int error= 0;
9286 if ((ulonglong) nr > typelib->count || nr == 0)
9287 {
9288 set_warning(WARN_DATA_TRUNCATED, 1);
9289 if (nr != 0 || get_thd()->count_cuted_fields > CHECK_FIELD_EXPRESSION)
9290 {
9291 nr= 0;
9292 error= 1;
9293 }
9294 }
9295 store_type((ulonglong) (uint) nr);
9296 return error;
9297 }
9298
9299
val_real(void)9300 double Field_enum::val_real(void)
9301 {
9302 return (double) Field_enum::val_int();
9303 }
9304
9305
val_int(void)9306 longlong Field_enum::val_int(void)
9307 {
9308 ASSERT_COLUMN_MARKED_FOR_READ;
9309 return read_lowendian(ptr, packlength);
9310 }
9311
9312
9313 /**
9314 Save the field metadata for enum fields.
9315
9316 Saves the real type in the first byte and the pack length in the
9317 second byte of the field metadata array at index of *metadata_ptr and
9318 *(metadata_ptr + 1).
9319
9320 @param metadata_ptr First byte of field metadata
9321
9322 @returns number of bytes written to metadata_ptr
9323 */
save_field_metadata(uchar * metadata_ptr)9324 int Field_enum::save_field_metadata(uchar *metadata_ptr)
9325 {
9326 *metadata_ptr= real_type();
9327 *(metadata_ptr + 1)= pack_length();
9328 return 2;
9329 }
9330
9331
val_str(String * val_buffer,String * val_ptr)9332 String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
9333 String *val_ptr)
9334 {
9335 uint tmp=(uint) Field_enum::val_int();
9336 if (!tmp || tmp > typelib->count)
9337 val_ptr->set("", 0, field_charset);
9338 else
9339 val_ptr->set((const char*) typelib->type_names[tmp-1],
9340 typelib->type_lengths[tmp-1],
9341 field_charset);
9342 return val_ptr;
9343 }
9344
cmp(const uchar * a_ptr,const uchar * b_ptr)9345 int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
9346 {
9347 uchar *old= ptr;
9348 ptr= (uchar*) a_ptr;
9349 ulonglong a=Field_enum::val_int();
9350 ptr= (uchar*) b_ptr;
9351 ulonglong b=Field_enum::val_int();
9352 ptr= old;
9353 return (a < b) ? -1 : (a > b) ? 1 : 0;
9354 }
9355
sort_string(uchar * to,uint length)9356 void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
9357 {
9358 ulonglong value=Field_enum::val_int();
9359 to+=packlength-1;
9360 for (uint i=0 ; i < packlength ; i++)
9361 {
9362 *to-- = (uchar) (value & 255);
9363 value>>=8;
9364 }
9365 }
9366
9367
sql_type(String & res) const9368 void Field_enum::sql_type(String &res) const
9369 {
9370 char buffer[255];
9371 String enum_item(buffer, sizeof(buffer), res.charset());
9372
9373 res.length(0);
9374 res.append(STRING_WITH_LEN("enum("));
9375
9376 bool flag=0;
9377 uint *len= typelib->type_lengths;
9378 for (const char **pos= typelib->type_names; *pos; pos++, len++)
9379 {
9380 uint dummy_errors;
9381 if (flag)
9382 res.append(',');
9383 /* convert to res.charset() == utf8, then quote */
9384 enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
9385 append_unescaped(&res, enum_item.ptr(), enum_item.length());
9386 flag= 1;
9387 }
9388 res.append(')');
9389 }
9390
9391
make_new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type)9392 Field *Field_enum::make_new_field(MEM_ROOT *root, TABLE *new_table,
9393 bool keep_type)
9394 {
9395 Field_enum *res= (Field_enum*) Field::make_new_field(root, new_table,
9396 keep_type);
9397 if (res)
9398 res->typelib= copy_typelib(root, typelib);
9399 return res;
9400 }
9401
9402
9403 /*
9404 set type.
9405 This is a string which can have a collection of different values.
9406 Each string value is separated with a ','.
9407 For example "One,two,five"
9408 If one uses this string in a number context one gets the bits as a longlong
9409 number.
9410 */
9411
9412
store(const char * from,size_t length,CHARSET_INFO * cs)9413 int Field_set::store(const char *from,size_t length,CHARSET_INFO *cs)
9414 {
9415 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
9416 bool got_warning= 0;
9417 int err= 0;
9418 char *not_used;
9419 uint not_used2;
9420 char buff[STRING_BUFFER_USUAL_SIZE];
9421 String tmpstr(buff,sizeof(buff), &my_charset_bin);
9422
9423 /* Convert character set if necessary */
9424 if (String::needs_conversion_on_storage(length, cs, field_charset))
9425 {
9426 uint dummy_errors;
9427 tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
9428 from= tmpstr.ptr();
9429 length= tmpstr.length();
9430 }
9431 ulonglong tmp= find_set(typelib, from, length, field_charset,
9432 ¬_used, ¬_used2, &got_warning);
9433 if (!tmp && length && length < 22)
9434 {
9435 /* This is for reading numbers with LOAD DATA INFILE */
9436 char *end;
9437 tmp=my_strntoull(cs,from,length,10,&end,&err);
9438 if (err || end != from+length ||
9439 tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
9440 {
9441 tmp=0;
9442 set_warning(WARN_DATA_TRUNCATED, 1);
9443 err= 1;
9444 }
9445 }
9446 else if (got_warning)
9447 set_warning(WARN_DATA_TRUNCATED, 1);
9448 store_type(tmp);
9449 return err;
9450 }
9451
9452
store(longlong nr,bool unsigned_val)9453 int Field_set::store(longlong nr, bool unsigned_val)
9454 {
9455 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
9456 int error= 0;
9457 ulonglong max_nr;
9458
9459 if (sizeof(ulonglong)*8 <= typelib->count)
9460 max_nr= ULONGLONG_MAX;
9461 else
9462 max_nr= (1ULL << typelib->count) - 1;
9463
9464 if ((ulonglong) nr > max_nr)
9465 {
9466 nr&= max_nr;
9467 set_warning(WARN_DATA_TRUNCATED, 1);
9468 error=1;
9469 }
9470 store_type((ulonglong) nr);
9471 return error;
9472 }
9473
9474
val_str(String * val_buffer,String * val_ptr)9475 String *Field_set::val_str(String *val_buffer,
9476 String *val_ptr __attribute__((unused)))
9477 {
9478 ulonglong tmp=(ulonglong) Field_enum::val_int();
9479 uint bitnr=0;
9480
9481 /*
9482 Some callers expect *val_buffer to contain the result,
9483 so we assign to it, rather than doing 'return &empty_set_string.
9484 */
9485 *val_buffer= empty_set_string;
9486 if (tmp == 0)
9487 {
9488 return val_buffer;
9489 }
9490
9491 val_buffer->set_charset(field_charset);
9492 val_buffer->length(0);
9493
9494 while (tmp && bitnr < (uint) typelib->count)
9495 {
9496 if (tmp & 1)
9497 {
9498 if (val_buffer->length())
9499 val_buffer->append(&field_separator, 1, &my_charset_latin1);
9500 String str(typelib->type_names[bitnr],
9501 typelib->type_lengths[bitnr],
9502 field_charset);
9503 val_buffer->append(str);
9504 }
9505 tmp>>=1;
9506 bitnr++;
9507 }
9508 return val_buffer;
9509 }
9510
9511
sql_type(String & res) const9512 void Field_set::sql_type(String &res) const
9513 {
9514 char buffer[255];
9515 String set_item(buffer, sizeof(buffer), res.charset());
9516
9517 res.length(0);
9518 res.append(STRING_WITH_LEN("set("));
9519
9520 bool flag=0;
9521 uint *len= typelib->type_lengths;
9522 for (const char **pos= typelib->type_names; *pos; pos++, len++)
9523 {
9524 uint dummy_errors;
9525 if (flag)
9526 res.append(',');
9527 /* convert to res.charset() == utf8, then quote */
9528 set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
9529 append_unescaped(&res, set_item.ptr(), set_item.length());
9530 flag= 1;
9531 }
9532 res.append(')');
9533 }
9534
9535 /**
9536 @retval
9537 1 if the fields are equally defined
9538 @retval
9539 0 if the fields are unequally defined
9540 */
9541
eq_def(const Field * field) const9542 bool Field::eq_def(const Field *field) const
9543 {
9544 if (real_type() != field->real_type() || charset() != field->charset() ||
9545 pack_length() != field->pack_length())
9546 return 0;
9547 return 1;
9548 }
9549
9550
9551 /**
9552 Compare the first t1::count type names.
9553
9554 @return TRUE if the type names of t1 match those of t2. FALSE otherwise.
9555 */
9556
compare_type_names(CHARSET_INFO * charset,TYPELIB * t1,TYPELIB * t2)9557 static bool compare_type_names(CHARSET_INFO *charset, TYPELIB *t1, TYPELIB *t2)
9558 {
9559 for (uint i= 0; i < t1->count; i++)
9560 if (my_strnncoll(charset,
9561 (const uchar*) t1->type_names[i],
9562 t1->type_lengths[i],
9563 (const uchar*) t2->type_names[i],
9564 t2->type_lengths[i]))
9565 return FALSE;
9566 return TRUE;
9567 }
9568
9569 /**
9570 @return
9571 returns 1 if the fields are equally defined
9572 */
9573
eq_def(const Field * field) const9574 bool Field_enum::eq_def(const Field *field) const
9575 {
9576 TYPELIB *values;
9577
9578 if (!Field::eq_def(field))
9579 return FALSE;
9580
9581 values= ((Field_enum*) field)->typelib;
9582
9583 /* Definition must be strictly equal. */
9584 if (typelib->count != values->count)
9585 return FALSE;
9586
9587 return compare_type_names(field_charset, typelib, values);
9588 }
9589
9590
9591 /**
9592 Check whether two fields can be considered 'equal' for table
9593 alteration purposes. Fields are equal if they retain the same
9594 pack length and if new members are added to the end of the list.
9595
9596 @return IS_EQUAL_YES if fields are compatible.
9597 IS_EQUAL_NO otherwise.
9598 */
9599
is_equal(Create_field * new_field)9600 uint Field_enum::is_equal(Create_field *new_field)
9601 {
9602 TYPELIB *values= new_field->interval;
9603
9604 /*
9605 The fields are compatible if they have the same flags,
9606 type, charset and have the same underlying length.
9607 */
9608 if (new_field->type_handler() != type_handler() ||
9609 new_field->charset != field_charset ||
9610 new_field->pack_length != pack_length())
9611 return IS_EQUAL_NO;
9612
9613 /*
9614 Changing the definition of an ENUM or SET column by adding a new
9615 enumeration or set members to the end of the list of valid member
9616 values only alters table metadata and not table data.
9617 */
9618 if (typelib->count > values->count)
9619 return IS_EQUAL_NO;
9620
9621 /* Check whether there are modification before the end. */
9622 if (! compare_type_names(field_charset, typelib, new_field->interval))
9623 return IS_EQUAL_NO;
9624
9625 return IS_EQUAL_YES;
9626 }
9627
9628
pack(uchar * to,const uchar * from,uint max_length)9629 uchar *Field_enum::pack(uchar *to, const uchar *from, uint max_length)
9630 {
9631 DBUG_ENTER("Field_enum::pack");
9632 DBUG_PRINT("debug", ("packlength: %d", packlength));
9633 DBUG_DUMP("from", from, packlength);
9634 DBUG_RETURN(pack_int(to, from, packlength));
9635 }
9636
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)9637 const uchar *Field_enum::unpack(uchar *to, const uchar *from,
9638 const uchar *from_end, uint param_data)
9639 {
9640 DBUG_ENTER("Field_enum::unpack");
9641 DBUG_PRINT("debug", ("packlength: %d", packlength));
9642 DBUG_DUMP("from", from, packlength);
9643 DBUG_RETURN(unpack_int(to, from, from_end, packlength));
9644 }
9645
9646
9647 /**
9648 @return
9649 returns 1 if the fields are equally defined
9650 */
eq_def(const Field * field) const9651 bool Field_num::eq_def(const Field *field) const
9652 {
9653 if (!Field::eq_def(field))
9654 return 0;
9655 Field_num *from_num= (Field_num*) field;
9656
9657 if (unsigned_flag != from_num->unsigned_flag ||
9658 (zerofill && !from_num->zerofill && !zero_pack()) ||
9659 dec != from_num->dec)
9660 return 0;
9661 return 1;
9662 }
9663
9664
9665 /**
9666 Check whether two numeric fields can be considered 'equal' for table
9667 alteration purposes. Fields are equal if they are of the same type
9668 and retain the same pack length.
9669 */
9670
is_equal(Create_field * new_field)9671 uint Field_num::is_equal(Create_field *new_field)
9672 {
9673 return ((new_field->type_handler() == type_handler()) &&
9674 ((new_field->flags & UNSIGNED_FLAG) ==
9675 (uint) (flags & UNSIGNED_FLAG)) &&
9676 ((new_field->flags & AUTO_INCREMENT_FLAG) ==
9677 (uint) (flags & AUTO_INCREMENT_FLAG)) &&
9678 (new_field->pack_length == pack_length()));
9679 }
9680
9681
can_optimize_range_or_keypart_ref(const Item_bool_func * cond,const Item * item) const9682 bool Field_enum::can_optimize_range_or_keypart_ref(const Item_bool_func *cond,
9683 const Item *item) const
9684 {
9685 switch (item->cmp_type())
9686 {
9687 case TIME_RESULT:
9688 return false;
9689 case INT_RESULT:
9690 case DECIMAL_RESULT:
9691 case REAL_RESULT:
9692 return true;
9693 case STRING_RESULT:
9694 return charset() == cond->compare_collation();
9695 case ROW_RESULT:
9696 DBUG_ASSERT(0);
9697 break;
9698 }
9699 return false;
9700 }
9701
9702
9703 /*
9704 Bit field.
9705
9706 We store the first 0 - 6 uneven bits among the null bits
9707 at the start of the record. The rest bytes are stored in
9708 the record itself.
9709
9710 For example:
9711
9712 CREATE TABLE t1 (a int, b bit(17), c bit(21) not null, d bit(8));
9713 We would store data as follows in the record:
9714
9715 Byte Bit
9716 1 7 - reserve for delete
9717 6 - null bit for 'a'
9718 5 - null bit for 'b'
9719 4 - first (high) bit of 'b'
9720 3 - first (high) bit of 'c'
9721 2 - second bit of 'c'
9722 1 - third bit of 'c'
9723 0 - forth bit of 'c'
9724 2 7 - firth bit of 'c'
9725 6 - null bit for 'd'
9726 3 - 6 four bytes for 'a'
9727 7 - 8 two bytes for 'b'
9728 9 - 10 two bytes for 'c'
9729 11 one byte for 'd'
9730 */
9731
Field_bit(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar * bit_ptr_arg,uchar bit_ofs_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)9732 Field_bit::Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
9733 uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
9734 enum utype unireg_check_arg,
9735 const LEX_CSTRING *field_name_arg)
9736 : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
9737 unireg_check_arg, field_name_arg),
9738 bit_ptr(bit_ptr_arg), bit_ofs(bit_ofs_arg), bit_len(len_arg & 7),
9739 bytes_in_rec(len_arg / 8)
9740 {
9741 DBUG_ENTER("Field_bit::Field_bit");
9742 DBUG_PRINT("enter", ("ptr_arg: %p, null_ptr_arg: %p, len_arg: %u, bit_len: %u, bytes_in_rec: %u",
9743 ptr_arg, null_ptr_arg, len_arg, bit_len, bytes_in_rec));
9744 flags|= UNSIGNED_FLAG;
9745 /*
9746 Ensure that Field::eq() can distinguish between two different bit fields.
9747 (two bit fields that are not null, may have same ptr and null_ptr)
9748 */
9749 if (!null_ptr_arg)
9750 null_bit= bit_ofs_arg;
9751 DBUG_VOID_RETURN;
9752 }
9753
9754
hash(ulong * nr,ulong * nr2)9755 void Field_bit::hash(ulong *nr, ulong *nr2)
9756 {
9757 if (is_null())
9758 {
9759 *nr^= (*nr << 1) | 1;
9760 }
9761 else
9762 {
9763 CHARSET_INFO *cs= &my_charset_bin;
9764 longlong value= Field_bit::val_int();
9765 uchar tmp[8];
9766 mi_int8store(tmp,value);
9767 cs->coll->hash_sort(cs, tmp, 8, nr, nr2);
9768 }
9769 }
9770
9771
9772 size_t
do_last_null_byte() const9773 Field_bit::do_last_null_byte() const
9774 {
9775 /*
9776 Code elsewhere is assuming that bytes are 8 bits, so I'm using
9777 that value instead of the correct one: CHAR_BIT.
9778
9779 REFACTOR SUGGESTION (Matz): Change to use the correct number of
9780 bits. On systems with CHAR_BIT > 8 (not very common), the storage
9781 will lose the extra bits.
9782 */
9783 DBUG_PRINT("test", ("bit_ofs: %d, bit_len: %d bit_ptr: %p",
9784 bit_ofs, bit_len, bit_ptr));
9785 uchar *result;
9786 if (bit_len == 0)
9787 result= null_ptr;
9788 else if (bit_ofs + bit_len > 8)
9789 result= bit_ptr + 1;
9790 else
9791 result= bit_ptr;
9792
9793 if (result)
9794 return (size_t) (result - table->record[0]) + 1;
9795 return LAST_NULL_BYTE_UNDEF;
9796 }
9797
9798
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uint32 length,uchar * new_null_ptr,uint new_null_bit)9799 Field *Field_bit::new_key_field(MEM_ROOT *root, TABLE *new_table,
9800 uchar *new_ptr, uint32 length,
9801 uchar *new_null_ptr, uint new_null_bit)
9802 {
9803 Field_bit *res;
9804 if ((res= (Field_bit*) Field::new_key_field(root, new_table, new_ptr, length,
9805 new_null_ptr, new_null_bit)))
9806 {
9807 /* Move bits normally stored in null_pointer to new_ptr */
9808 res->bit_ptr= new_ptr;
9809 res->bit_ofs= 0;
9810 if (bit_len)
9811 res->ptr++; // Store rest of data here
9812 }
9813 return res;
9814 }
9815
9816
is_equal(Create_field * new_field)9817 uint Field_bit::is_equal(Create_field *new_field)
9818 {
9819 return new_field->type_handler() == type_handler() &&
9820 new_field->length == max_display_length();
9821 }
9822
9823
store(const char * from,size_t length,CHARSET_INFO * cs)9824 int Field_bit::store(const char *from, size_t length, CHARSET_INFO *cs)
9825 {
9826 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
9827 int delta;
9828
9829 for (; length && !*from; from++, length--) // skip left 0's
9830 ;
9831 delta= (int)(bytes_in_rec - length);
9832
9833 if (delta < -1 ||
9834 (delta == -1 && (uchar) *from > ((1 << bit_len) - 1)) ||
9835 (!bit_len && delta < 0))
9836 {
9837 set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
9838 memset(ptr, 0xff, bytes_in_rec);
9839 if (get_thd()->really_abort_on_warning())
9840 set_warning(ER_DATA_TOO_LONG, 1);
9841 else
9842 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
9843 return 1;
9844 }
9845 /* delta is >= -1 here */
9846 if (delta > 0)
9847 {
9848 if (bit_len)
9849 clr_rec_bits(bit_ptr, bit_ofs, bit_len);
9850 bzero(ptr, delta);
9851 memcpy(ptr + delta, from, length);
9852 }
9853 else if (delta == 0)
9854 {
9855 if (bit_len)
9856 clr_rec_bits(bit_ptr, bit_ofs, bit_len);
9857 memcpy(ptr, from, length);
9858 }
9859 else
9860 {
9861 if (bit_len)
9862 {
9863 set_rec_bits((uchar) *from, bit_ptr, bit_ofs, bit_len);
9864 from++;
9865 }
9866 memcpy(ptr, from, bytes_in_rec);
9867 }
9868 return 0;
9869 }
9870
9871
store(double nr)9872 int Field_bit::store(double nr)
9873 {
9874 return Field_bit::store((longlong) nr, FALSE);
9875 }
9876
9877
store(longlong nr,bool unsigned_val)9878 int Field_bit::store(longlong nr, bool unsigned_val)
9879 {
9880 char buf[8];
9881
9882 mi_int8store(buf, nr);
9883 return store(buf, 8, NULL);
9884 }
9885
9886
store_decimal(const my_decimal * val)9887 int Field_bit::store_decimal(const my_decimal *val)
9888 {
9889 int err= 0;
9890 longlong i= convert_decimal2longlong(val, 1, &err);
9891 return MY_TEST(err | store(i, TRUE));
9892 }
9893
9894
val_real(void)9895 double Field_bit::val_real(void)
9896 {
9897 return (double) Field_bit::val_int();
9898 }
9899
9900
val_int(void)9901 longlong Field_bit::val_int(void)
9902 {
9903 ASSERT_COLUMN_MARKED_FOR_READ;
9904 ulonglong bits= 0;
9905 if (bit_len)
9906 {
9907 bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9908 bits<<= (bytes_in_rec * 8);
9909 }
9910
9911 switch (bytes_in_rec) {
9912 case 0: return bits;
9913 case 1: return bits | (ulonglong) ptr[0];
9914 case 2: return bits | mi_uint2korr(ptr);
9915 case 3: return bits | mi_uint3korr(ptr);
9916 case 4: return bits | mi_uint4korr(ptr);
9917 case 5: return bits | mi_uint5korr(ptr);
9918 case 6: return bits | mi_uint6korr(ptr);
9919 case 7: return bits | mi_uint7korr(ptr);
9920 default: return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
9921 }
9922 }
9923
9924
val_str(String * val_buffer,String * val_ptr)9925 String *Field_bit::val_str(String *val_buffer,
9926 String *val_ptr __attribute__((unused)))
9927 {
9928 ASSERT_COLUMN_MARKED_FOR_READ;
9929 char buff[sizeof(longlong)];
9930 uint length= MY_MIN(pack_length(), sizeof(longlong));
9931 ulonglong bits= val_int();
9932 mi_int8store(buff,bits);
9933
9934 val_buffer->alloc(length);
9935 memcpy((char *) val_buffer->ptr(), buff+8-length, length);
9936 val_buffer->length(length);
9937 val_buffer->set_charset(&my_charset_bin);
9938 return val_buffer;
9939 }
9940
9941
val_decimal(my_decimal * deciaml_value)9942 my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value)
9943 {
9944 ASSERT_COLUMN_MARKED_FOR_READ;
9945 int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 1, deciaml_value);
9946 return deciaml_value;
9947 }
9948
9949
9950 /*
9951 Compare two bit fields using pointers within the record.
9952 SYNOPSIS
9953 cmp_max()
9954 a Pointer to field->ptr in first record
9955 b Pointer to field->ptr in second record
9956 max_len Maximum length used in index
9957 DESCRIPTION
9958 This method is used from key_rec_cmp used by merge sorts used
9959 by partitioned index read and later other similar places.
9960 The a and b pointer must be pointers to the field in a record
9961 (not the table->record[0] necessarily)
9962 */
cmp_prefix(const uchar * a,const uchar * b,size_t prefix_len)9963 int Field_bit::cmp_prefix(const uchar *a, const uchar *b, size_t prefix_len)
9964 {
9965 my_ptrdiff_t a_diff= a - ptr;
9966 my_ptrdiff_t b_diff= b - ptr;
9967 if (bit_len)
9968 {
9969 int flag;
9970 uchar bits_a= get_rec_bits(bit_ptr+a_diff, bit_ofs, bit_len);
9971 uchar bits_b= get_rec_bits(bit_ptr+b_diff, bit_ofs, bit_len);
9972 if ((flag= (int) (bits_a - bits_b)))
9973 return flag;
9974 }
9975 if (!bytes_in_rec)
9976 return 0;
9977 return memcmp(a, b, bytes_in_rec);
9978 }
9979
9980
key_cmp(const uchar * str,uint length)9981 int Field_bit::key_cmp(const uchar *str, uint length)
9982 {
9983 if (bit_len)
9984 {
9985 int flag;
9986 uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9987 if ((flag= (int) (bits - *str)))
9988 return flag;
9989 str++;
9990 length--;
9991 }
9992 return memcmp(ptr, str, bytes_in_rec);
9993 }
9994
9995
cmp_offset(uint row_offset)9996 int Field_bit::cmp_offset(uint row_offset)
9997 {
9998 if (bit_len)
9999 {
10000 int flag;
10001 uchar bits_a= get_rec_bits(bit_ptr, bit_ofs, bit_len);
10002 uchar bits_b= get_rec_bits(bit_ptr + row_offset, bit_ofs, bit_len);
10003 if ((flag= (int) (bits_a - bits_b)))
10004 return flag;
10005 }
10006 return memcmp(ptr, ptr + row_offset, bytes_in_rec);
10007 }
10008
10009
get_key_image(uchar * buff,uint length,imagetype type_arg)10010 uint Field_bit::get_key_image(uchar *buff, uint length, imagetype type_arg)
10011 {
10012 if (bit_len)
10013 {
10014 uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
10015 *buff++= bits;
10016 length--;
10017 }
10018 uint tmp_data_length = MY_MIN(length, bytes_in_rec);
10019 memcpy(buff, ptr, tmp_data_length);
10020 return tmp_data_length + 1;
10021 }
10022
10023
10024 /**
10025 Save the field metadata for bit fields.
10026
10027 Saves the bit length in the first byte and bytes in record in the
10028 second byte of the field metadata array at index of *metadata_ptr and
10029 *(metadata_ptr + 1).
10030
10031 @param metadata_ptr First byte of field metadata
10032
10033 @returns number of bytes written to metadata_ptr
10034 */
save_field_metadata(uchar * metadata_ptr)10035 int Field_bit::save_field_metadata(uchar *metadata_ptr)
10036 {
10037 DBUG_ENTER("Field_bit::save_field_metadata");
10038 DBUG_PRINT("debug", ("bit_len: %d, bytes_in_rec: %d",
10039 bit_len, bytes_in_rec));
10040 /*
10041 Since this class and Field_bit_as_char have different ideas of
10042 what should be stored here, we compute the values of the metadata
10043 explicitly using the field_length.
10044 */
10045 metadata_ptr[0]= field_length % 8;
10046 metadata_ptr[1]= field_length / 8;
10047 DBUG_RETURN(2);
10048 }
10049
10050
10051 /**
10052 Returns the number of bytes field uses in row-based replication
10053 row packed size.
10054
10055 This method is used in row-based replication to determine the number
10056 of bytes that the field consumes in the row record format. This is
10057 used to skip fields in the master that do not exist on the slave.
10058
10059 @param field_metadata Encoded size in field metadata
10060
10061 @returns The size of the field based on the field metadata.
10062 */
pack_length_from_metadata(uint field_metadata)10063 uint Field_bit::pack_length_from_metadata(uint field_metadata)
10064 {
10065 uint const from_len= (field_metadata >> 8U) & 0x00ff;
10066 uint const from_bit_len= field_metadata & 0x00ff;
10067 uint const source_size= from_len + ((from_bit_len > 0) ? 1 : 0);
10068 return (source_size);
10069 }
10070
10071
10072 bool
compatible_field_size(uint field_metadata,Relay_log_info *,uint16 mflags,int * order_var)10073 Field_bit::compatible_field_size(uint field_metadata,
10074 Relay_log_info * __attribute__((unused)),
10075 uint16 mflags,
10076 int *order_var)
10077 {
10078 DBUG_ENTER("Field_bit::compatible_field_size");
10079 DBUG_ASSERT((field_metadata >> 16) == 0);
10080 uint from_bit_len=
10081 8 * (field_metadata >> 8) + (field_metadata & 0xff);
10082 uint to_bit_len= max_display_length();
10083 DBUG_PRINT("debug", ("from_bit_len: %u, to_bit_len: %u",
10084 from_bit_len, to_bit_len));
10085 /*
10086 If the bit length exact flag is clear, we are dealing with an old
10087 master, so we allow some less strict behaviour if replicating by
10088 moving both bit lengths to an even multiple of 8.
10089
10090 We do this by computing the number of bytes to store the field
10091 instead, and then compare the result.
10092 */
10093 if (!(mflags & Table_map_log_event::TM_BIT_LEN_EXACT_F)) {
10094 from_bit_len= (from_bit_len + 7) / 8;
10095 to_bit_len= (to_bit_len + 7) / 8;
10096 }
10097
10098 *order_var= compare(from_bit_len, to_bit_len);
10099 DBUG_RETURN(TRUE);
10100 }
10101
10102
10103
sql_type(String & res) const10104 void Field_bit::sql_type(String &res) const
10105 {
10106 CHARSET_INFO *cs= res.charset();
10107 size_t length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
10108 "bit(%d)", (int) field_length);
10109 res.length(length);
10110 }
10111
10112
10113 uchar *
pack(uchar * to,const uchar * from,uint max_length)10114 Field_bit::pack(uchar *to, const uchar *from, uint max_length)
10115 {
10116 DBUG_ASSERT(max_length > 0);
10117 uint length;
10118 if (bit_len > 0)
10119 {
10120 /*
10121 We have the following:
10122
10123 ptr Points into a field in record R1
10124 from Points to a field in a record R2
10125 bit_ptr Points to the byte (in the null bytes) that holds the
10126 odd bits of R1
10127 from_bitp Points to the byte that holds the odd bits of R2
10128
10129 We have the following:
10130
10131 ptr - bit_ptr = from - from_bitp
10132
10133 We want to isolate 'from_bitp', so this gives:
10134
10135 ptr - bit_ptr - from = - from_bitp
10136 - ptr + bit_ptr + from = from_bitp
10137 bit_ptr + from - ptr = from_bitp
10138 */
10139 uchar bits= get_rec_bits(bit_ptr + (from - ptr), bit_ofs, bit_len);
10140 *to++= bits;
10141 }
10142 length= MY_MIN(bytes_in_rec, max_length - (bit_len > 0));
10143 memcpy(to, from, length);
10144 return to + length;
10145 }
10146
10147
10148 /**
10149 Unpack a bit field from row data.
10150
10151 This method is used to unpack a bit field from a master whose size
10152 of the field is less than that of the slave.
10153
10154 @param to Destination of the data
10155 @param from Source of the data
10156 @param param_data Bit length (upper) and length (lower) values
10157
10158 @return New pointer into memory based on from + length of the data
10159 */
10160 const uchar *
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)10161 Field_bit::unpack(uchar *to, const uchar *from, const uchar *from_end,
10162 uint param_data)
10163 {
10164 DBUG_ENTER("Field_bit::unpack");
10165 DBUG_PRINT("enter", ("to: %p, from: %p, param_data: 0x%x",
10166 to, from, param_data));
10167 DBUG_PRINT("debug", ("bit_ptr: %p, bit_len: %u, bit_ofs: %u",
10168 bit_ptr, bit_len, bit_ofs));
10169 uint const from_len= (param_data >> 8U) & 0x00ff;
10170 uint const from_bit_len= param_data & 0x00ff;
10171 DBUG_PRINT("debug", ("from_len: %u, from_bit_len: %u",
10172 from_len, from_bit_len));
10173 /*
10174 If the parameter data is zero (i.e., undefined), or if the master
10175 and slave have the same sizes, then use the old unpack() method.
10176 */
10177 if (param_data == 0 ||
10178 ((from_bit_len == bit_len) && (from_len == bytes_in_rec)))
10179 {
10180 if (from + bytes_in_rec + MY_TEST(bit_len) > from_end)
10181 return 0; // Error in data
10182
10183 if (bit_len > 0)
10184 {
10185 /*
10186 set_rec_bits is a macro, don't put the post-increment in the
10187 argument since that might cause strange side-effects.
10188
10189 For the choice of the second argument, see the explanation for
10190 Field_bit::pack().
10191 */
10192 set_rec_bits(*from, bit_ptr + (to - ptr), bit_ofs, bit_len);
10193 from++;
10194 }
10195 memcpy(to, from, bytes_in_rec);
10196 DBUG_RETURN(from + bytes_in_rec);
10197 }
10198
10199 /*
10200 We are converting a smaller bit field to a larger one here.
10201 To do that, we first need to construct a raw value for the original
10202 bit value stored in the from buffer. Then that needs to be converted
10203 to the larger field then sent to store() for writing to the field.
10204 Lastly the odd bits need to be masked out if the bytes_in_rec > 0.
10205 Otherwise stray bits can cause spurious values.
10206 */
10207
10208 uint len= from_len + ((from_bit_len > 0) ? 1 : 0);
10209 uint new_len= (field_length + 7) / 8;
10210
10211 if (from + len > from_end || new_len < len)
10212 return 0; // Error in data
10213
10214 char *value= (char *)my_alloca(new_len);
10215 bzero(value, new_len);
10216
10217 memcpy(value + (new_len - len), from, len);
10218 /*
10219 Mask out the unused bits in the partial byte.
10220 TODO: Add code to the master to always mask these bits and remove
10221 the following.
10222 */
10223 if ((from_bit_len > 0) && (from_len > 0))
10224 value[new_len - len]= value[new_len - len] & ((1U << from_bit_len) - 1);
10225 bitmap_set_bit(table->write_set,field_index);
10226 store(value, new_len, system_charset_info);
10227 my_afree(value);
10228 DBUG_RETURN(from + len);
10229 }
10230
10231
set_default()10232 int Field_bit::set_default()
10233 {
10234 if (bit_len > 0)
10235 {
10236 my_ptrdiff_t const col_offset= table->s->default_values - table->record[0];
10237 uchar bits= get_rec_bits(bit_ptr + col_offset, bit_ofs, bit_len);
10238 set_rec_bits(bits, bit_ptr, bit_ofs, bit_len);
10239 }
10240 return Field::set_default();
10241 }
10242
10243 /*
10244 Bit field support for non-MyISAM tables.
10245 */
10246
Field_bit_as_char(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)10247 Field_bit_as_char::Field_bit_as_char(uchar *ptr_arg, uint32 len_arg,
10248 uchar *null_ptr_arg, uchar null_bit_arg,
10249 enum utype unireg_check_arg,
10250 const LEX_CSTRING *field_name_arg)
10251 :Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, 0, 0,
10252 unireg_check_arg, field_name_arg)
10253 {
10254 flags|= UNSIGNED_FLAG;
10255 bit_len= 0;
10256 bytes_in_rec= (len_arg + 7) / 8;
10257 }
10258
10259
store(const char * from,size_t length,CHARSET_INFO * cs)10260 int Field_bit_as_char::store(const char *from, size_t length, CHARSET_INFO *cs)
10261 {
10262 ASSERT_COLUMN_MARKED_FOR_WRITE_OR_COMPUTED;
10263 int delta;
10264 uchar bits= (uchar) (field_length & 7);
10265
10266 for (; length && !*from; from++, length--) // skip left 0's
10267 ;
10268 delta= (int)(bytes_in_rec - length);
10269
10270 if (delta < 0 ||
10271 (delta == 0 && bits && (uint) (uchar) *from >= (uint) (1 << bits)))
10272 {
10273 memset(ptr, 0xff, bytes_in_rec);
10274 if (bits)
10275 *ptr&= ((1 << bits) - 1); /* set first uchar */
10276 if (get_thd()->really_abort_on_warning())
10277 set_warning(ER_DATA_TOO_LONG, 1);
10278 else
10279 set_warning(ER_WARN_DATA_OUT_OF_RANGE, 1);
10280 return 1;
10281 }
10282 bzero(ptr, delta);
10283 memcpy(ptr + delta, from, length);
10284 return 0;
10285 }
10286
10287
sql_type(String & res) const10288 void Field_bit_as_char::sql_type(String &res) const
10289 {
10290 CHARSET_INFO *cs= res.charset();
10291 size_t length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
10292 "bit(%d)", (int) field_length);
10293 res.length(length);
10294 }
10295
10296
10297 /*****************************************************************************
10298 Handling of field and Create_field
10299 *****************************************************************************/
10300
create_interval_from_interval_list(MEM_ROOT * mem_root,bool reuse_interval_list_values)10301 bool Column_definition::create_interval_from_interval_list(MEM_ROOT *mem_root,
10302 bool reuse_interval_list_values)
10303 {
10304 DBUG_ENTER("Column_definition::create_interval_from_interval_list");
10305 DBUG_ASSERT(!interval);
10306 if (!(interval= (TYPELIB*) alloc_root(mem_root, sizeof(TYPELIB))))
10307 DBUG_RETURN(true); // EOM
10308
10309 List_iterator<String> it(interval_list);
10310 StringBuffer<64> conv;
10311 char comma_buf[5]; /* 5 bytes for 'filename' charset */
10312 DBUG_ASSERT(sizeof(comma_buf) >= charset->mbmaxlen);
10313 int comma_length= charset->cset->wc_mb(charset, ',',
10314 (uchar*) comma_buf,
10315 (uchar*) comma_buf +
10316 sizeof(comma_buf));
10317 DBUG_ASSERT(comma_length >= 0 && comma_length <= (int) sizeof(comma_buf));
10318
10319 if (!multi_alloc_root(mem_root,
10320 &interval->type_names,
10321 sizeof(char*) * (interval_list.elements + 1),
10322 &interval->type_lengths,
10323 sizeof(uint) * (interval_list.elements + 1),
10324 NullS))
10325 goto err; // EOM
10326
10327 interval->name= "";
10328 interval->count= interval_list.elements;
10329
10330 for (uint i= 0; i < interval->count; i++)
10331 {
10332 uint32 dummy;
10333 String *tmp= it++;
10334 LEX_CSTRING value;
10335 if (String::needs_conversion(tmp->length(), tmp->charset(),
10336 charset, &dummy))
10337 {
10338 uint cnv_errs;
10339 conv.copy(tmp->ptr(), tmp->length(), tmp->charset(), charset, &cnv_errs);
10340 value.str= strmake_root(mem_root, conv.ptr(), conv.length());
10341 value.length= conv.length();
10342 }
10343 else
10344 {
10345 value.str= reuse_interval_list_values ? tmp->ptr() :
10346 strmake_root(mem_root,
10347 tmp->ptr(),
10348 tmp->length());
10349 value.length= tmp->length();
10350 }
10351 if (!value.str)
10352 goto err; // EOM
10353
10354 // Strip trailing spaces.
10355 value.length= charset->cset->lengthsp(charset, value.str, value.length);
10356 ((char*) value.str)[value.length]= '\0';
10357
10358 if (real_field_type() == MYSQL_TYPE_SET)
10359 {
10360 if (charset->coll->instr(charset, value.str, value.length,
10361 comma_buf, comma_length, NULL, 0))
10362 {
10363 ErrConvString err(tmp);
10364 my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "set", err.ptr());
10365 goto err;
10366 }
10367 }
10368 interval->type_names[i]= value.str;
10369 interval->type_lengths[i]= (uint)value.length;
10370 }
10371 interval->type_names[interval->count]= 0; // End marker
10372 interval->type_lengths[interval->count]= 0;
10373 interval_list.empty(); // Don't need interval_list anymore
10374 DBUG_RETURN(false);
10375 err:
10376 interval= NULL; // Avoid having both non-empty interval_list and interval
10377 DBUG_RETURN(true);
10378 }
10379
10380
prepare_interval_field(MEM_ROOT * mem_root,bool reuse_interval_list_values)10381 bool Column_definition::prepare_interval_field(MEM_ROOT *mem_root,
10382 bool reuse_interval_list_values)
10383 {
10384 DBUG_ENTER("Column_definition::prepare_interval_field");
10385 DBUG_ASSERT(real_field_type() == MYSQL_TYPE_ENUM ||
10386 real_field_type() == MYSQL_TYPE_SET);
10387 /*
10388 Interval values are either in "interval" or in "interval_list",
10389 but not in both at the same time, and are not empty at the same time.
10390 - Values are in "interval_list" when we're coming from the parser
10391 in CREATE TABLE or in CREATE {FUNCTION|PROCEDURE}.
10392 - Values are in "interval" when we're in ALTER TABLE.
10393
10394 In a corner case with an empty set like SET(''):
10395 - after the parser we have interval_list.elements==1
10396 - in ALTER TABLE we have a non-NULL interval with interval->count==1,
10397 with interval->type_names[0]=="" and interval->type_lengths[0]==0.
10398 So the assert is still valid for this corner case.
10399
10400 ENUM and SET with no values at all (e.g. ENUM(), SET()) are not possible,
10401 as the parser requires at least one element, so for a ENUM or SET field it
10402 should never happen that both internal_list.elements and interval are 0.
10403 */
10404 DBUG_ASSERT((interval == NULL) == (interval_list.elements > 0));
10405
10406 /*
10407 Create typelib from interval_list, and if necessary
10408 convert strings from client character set to the
10409 column character set.
10410 */
10411 if (interval_list.elements &&
10412 create_interval_from_interval_list(mem_root,
10413 reuse_interval_list_values))
10414 DBUG_RETURN(true);
10415
10416 if (!reuse_interval_list_values)
10417 {
10418 /*
10419 We're initializing from an existing table or view Field_enum
10420 (e.g. for a %TYPE variable) rather than from the parser.
10421 The constructor Column_definition(THD*,Field*,Field*) has already
10422 copied the TYPELIB pointer from the original Field_enum.
10423 Now we need to make a permanent copy of that TYPELIB,
10424 as the original field can be freed before the end of the life
10425 cycle of "this".
10426 */
10427 DBUG_ASSERT(interval);
10428 if (!(interval= copy_typelib(mem_root, interval)))
10429 DBUG_RETURN(true);
10430 }
10431 prepare_interval_field_calc_length();
10432 DBUG_RETURN(false);
10433 }
10434
10435
set_attributes(const Lex_field_type_st & type,CHARSET_INFO * cs)10436 void Column_definition::set_attributes(const Lex_field_type_st &type,
10437 CHARSET_INFO *cs)
10438 {
10439 DBUG_ASSERT(type_handler() == &type_handler_null);
10440 DBUG_ASSERT(charset == &my_charset_bin || charset == NULL);
10441 DBUG_ASSERT(length == 0);
10442 DBUG_ASSERT(decimals == 0);
10443
10444 set_handler(type.type_handler());
10445 charset= cs;
10446
10447 #if MYSQL_VERSION_ID > 100500
10448 #error When merging to 10.5, please move the code below to
10449 #error Type_handler_timestamp_common::Column_definition_set_attributes()
10450 #else
10451 /*
10452 Unlike other types TIMESTAMP fields are NOT NULL by default.
10453 Unless --explicit-defaults-for-timestamp is given.
10454 */
10455 if (!opt_explicit_defaults_for_timestamp &&
10456 type.type_handler()->field_type() == MYSQL_TYPE_TIMESTAMP)
10457 flags|= NOT_NULL_FLAG;
10458 #endif
10459
10460 if (type.length())
10461 {
10462 int err;
10463 length= my_strtoll10(type.length(), NULL, &err);
10464 if (err)
10465 length= ~0ULL; // safety
10466 }
10467
10468 if (type.dec())
10469 decimals= (uint) atoi(type.dec());
10470 }
10471
10472
create_length_to_internal_length_bit()10473 void Column_definition::create_length_to_internal_length_bit()
10474 {
10475 if (f_bit_as_char(pack_flag))
10476 {
10477 key_length= pack_length= ((length + 7) & ~7) / 8;
10478 }
10479 else
10480 {
10481 pack_length= (uint) length / 8;
10482 /* We need one extra byte to store the bits we save among the null bits */
10483 key_length= pack_length + MY_TEST(length & 7);
10484 }
10485 }
10486
10487
create_length_to_internal_length_newdecimal()10488 void Column_definition::create_length_to_internal_length_newdecimal()
10489 {
10490 DBUG_ASSERT(length < UINT_MAX32);
10491 uint prec= get_decimal_precision((uint)length, decimals, flags & UNSIGNED_FLAG);
10492 key_length= pack_length= my_decimal_get_binary_size(prec, decimals);
10493 }
10494
10495
check_expression(Virtual_column_info * vcol,LEX_CSTRING * name,enum_vcol_info_type type)10496 bool check_expression(Virtual_column_info *vcol, LEX_CSTRING *name,
10497 enum_vcol_info_type type)
10498
10499 {
10500 bool ret;
10501 Item::vcol_func_processor_result res;
10502
10503 if (!vcol->name.length)
10504 vcol->name= *name;
10505
10506 /*
10507 Walk through the Item tree checking if all items are valid
10508 to be part of the virtual column
10509 */
10510 res.errors= 0;
10511 ret= vcol->expr->walk(&Item::check_vcol_func_processor, 0, &res);
10512 vcol->flags= res.errors;
10513
10514 uint filter= VCOL_IMPOSSIBLE;
10515 if (type != VCOL_GENERATED_VIRTUAL && type != VCOL_DEFAULT)
10516 filter|= VCOL_NOT_STRICTLY_DETERMINISTIC;
10517 if (type == VCOL_GENERATED_VIRTUAL)
10518 filter|= VCOL_NOT_VIRTUAL;
10519
10520 if (unlikely(ret || (res.errors & filter)))
10521 {
10522 my_error(ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), res.name,
10523 vcol_type_name(type), name->str);
10524 return TRUE;
10525 }
10526 /*
10527 Safe to call before fix_fields as long as vcol's don't include sub
10528 queries (which is now checked in check_vcol_func_processor)
10529 */
10530 if (vcol->expr->check_cols(1))
10531 return TRUE;
10532 return FALSE;
10533 }
10534
10535
check_length(uint mysql_errno,uint limit) const10536 bool Column_definition::check_length(uint mysql_errno, uint limit) const
10537 {
10538 if (length <= limit)
10539 return false;
10540 my_error(mysql_errno, MYF(0), field_name.str, static_cast<ulong>(limit));
10541 return true;
10542 }
10543
10544
fix_attributes_int(uint default_length)10545 bool Column_definition::fix_attributes_int(uint default_length)
10546 {
10547 if (length)
10548 return check_length(ER_TOO_BIG_DISPLAYWIDTH, MAX_FIELD_CHARLENGTH);
10549 length= default_length;
10550 return false;
10551 }
10552
10553
fix_attributes_real(uint default_length)10554 bool Column_definition::fix_attributes_real(uint default_length)
10555 {
10556 /* change FLOAT(precision) to FLOAT or DOUBLE */
10557 if (!length && !decimals)
10558 {
10559 length= default_length;
10560 decimals= NOT_FIXED_DEC;
10561 }
10562 if (length < decimals && decimals != NOT_FIXED_DEC)
10563 {
10564 my_error(ER_M_BIGGER_THAN_D, MYF(0), field_name.str);
10565 return true;
10566 }
10567 if (decimals != NOT_FIXED_DEC && decimals >= FLOATING_POINT_DECIMALS)
10568 {
10569 my_error(ER_TOO_BIG_SCALE, MYF(0), static_cast<ulonglong>(decimals),
10570 field_name.str, static_cast<uint>(FLOATING_POINT_DECIMALS-1));
10571 return true;
10572 }
10573 return check_length(ER_TOO_BIG_DISPLAYWIDTH, MAX_FIELD_CHARLENGTH);
10574 }
10575
10576
fix_attributes_decimal()10577 bool Column_definition::fix_attributes_decimal()
10578 {
10579 if (decimals >= NOT_FIXED_DEC)
10580 {
10581 my_error(ER_TOO_BIG_SCALE, MYF(0), static_cast<ulonglong>(decimals),
10582 field_name.str, static_cast<uint>(NOT_FIXED_DEC - 1));
10583 return true;
10584 }
10585 my_decimal_trim(&length, &decimals);
10586 if (length > DECIMAL_MAX_PRECISION)
10587 {
10588 my_error(ER_TOO_BIG_PRECISION, MYF(0), length, field_name.str,
10589 DECIMAL_MAX_PRECISION);
10590 return true;
10591 }
10592 if (length < decimals)
10593 {
10594 my_error(ER_M_BIGGER_THAN_D, MYF(0), field_name.str);
10595 return true;
10596 }
10597 length= my_decimal_precision_to_length((uint) length, decimals,
10598 flags & UNSIGNED_FLAG);
10599 pack_length= my_decimal_get_binary_size((uint) length, decimals);
10600 return false;
10601 }
10602
10603
fix_attributes_bit()10604 bool Column_definition::fix_attributes_bit()
10605 {
10606 if (!length)
10607 length= 1;
10608 pack_length= ((uint) length + 7) / 8;
10609 return check_length(ER_TOO_BIG_DISPLAYWIDTH, MAX_BIT_FIELD_LENGTH);
10610 }
10611
10612
fix_attributes_temporal_with_time(uint int_part_length)10613 bool Column_definition::fix_attributes_temporal_with_time(uint int_part_length)
10614 {
10615 if (length > MAX_DATETIME_PRECISION)
10616 {
10617 my_error(ER_TOO_BIG_PRECISION, MYF(0), length, field_name.str,
10618 MAX_DATETIME_PRECISION);
10619 return true;
10620 }
10621 length+= int_part_length + (length ? 1 : 0);
10622 return false;
10623 }
10624
10625
check(THD * thd)10626 bool Column_definition::check(THD *thd)
10627 {
10628 DBUG_ENTER("Column_definition::check");
10629
10630 /* Initialize data for a computed field */
10631 if (vcol_info)
10632 {
10633 DBUG_ASSERT(vcol_info->expr);
10634 vcol_info->set_field_type(real_field_type());
10635 if (check_expression(vcol_info, &field_name, vcol_info->stored_in_db
10636 ? VCOL_GENERATED_STORED : VCOL_GENERATED_VIRTUAL))
10637 DBUG_RETURN(TRUE);
10638 }
10639
10640 if (check_constraint &&
10641 check_expression(check_constraint, &field_name, VCOL_CHECK_FIELD))
10642 DBUG_RETURN(1);
10643
10644 if (default_value)
10645 {
10646 Item *def_expr= default_value->expr;
10647 if (check_expression(default_value, &field_name, VCOL_DEFAULT))
10648 DBUG_RETURN(TRUE);
10649
10650 /* Constant's are stored in the 'empty_record', except for blobs */
10651 if (def_expr->basic_const_item())
10652 {
10653 if (def_expr->type() == Item::NULL_ITEM)
10654 {
10655 default_value= 0;
10656 if ((flags & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
10657 {
10658 my_error(ER_INVALID_DEFAULT, MYF(0), field_name.str);
10659 DBUG_RETURN(1);
10660 }
10661 }
10662 }
10663 }
10664
10665 if (default_value && (flags & AUTO_INCREMENT_FLAG))
10666 {
10667 my_error(ER_INVALID_DEFAULT, MYF(0), field_name.str);
10668 DBUG_RETURN(1);
10669 }
10670
10671 if (default_value && !default_value->expr->basic_const_item() &&
10672 mysql_timestamp_type() == MYSQL_TIMESTAMP_DATETIME &&
10673 default_value->expr->type() == Item::FUNC_ITEM)
10674 {
10675 /*
10676 Special case: NOW() for TIMESTAMP and DATETIME fields are handled
10677 as in MariaDB 10.1 by marking them in unireg_check.
10678 */
10679 Item_func *fn= static_cast<Item_func*>(default_value->expr);
10680 if (fn->functype() == Item_func::NOW_FUNC &&
10681 (fn->decimals == 0 || fn->decimals >= length))
10682 {
10683 default_value= 0;
10684 unireg_check= Field::TIMESTAMP_DN_FIELD;
10685 }
10686 }
10687
10688 if (on_update)
10689 {
10690 if (mysql_timestamp_type() != MYSQL_TIMESTAMP_DATETIME ||
10691 on_update->decimals < length)
10692 {
10693 my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name.str);
10694 DBUG_RETURN(TRUE);
10695 }
10696 unireg_check= unireg_check == Field::NONE ? Field::TIMESTAMP_UN_FIELD
10697 : Field::TIMESTAMP_DNUN_FIELD;
10698 }
10699 else if (flags & AUTO_INCREMENT_FLAG)
10700 unireg_check= Field::NEXT_NUMBER;
10701
10702 if (type_handler()->Column_definition_fix_attributes(this))
10703 DBUG_RETURN(true);
10704
10705 /* Remember the value of length */
10706 char_length= (uint)length;
10707
10708 /*
10709 Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
10710 it is NOT NULL, not an AUTO_INCREMENT field.
10711 We need to do this check here and in mysql_create_prepare_table() as
10712 sp_head::fill_field_definition() calls this function.
10713 */
10714 if (!default_value && unireg_check == Field::NONE && (flags & NOT_NULL_FLAG))
10715 {
10716 /*
10717 TIMESTAMP columns get implicit DEFAULT value when
10718 explicit_defaults_for_timestamp is not set.
10719 */
10720 if ((opt_explicit_defaults_for_timestamp ||
10721 !is_timestamp_type()) && !vers_sys_field())
10722 {
10723 flags|= NO_DEFAULT_VALUE_FLAG;
10724 }
10725 }
10726
10727
10728 if ((flags & AUTO_INCREMENT_FLAG) &&
10729 !type_handler()->type_can_have_auto_increment_attribute())
10730 {
10731 my_error(ER_WRONG_FIELD_SPEC, MYF(0), field_name.str);
10732 DBUG_RETURN(TRUE);
10733 }
10734
10735 DBUG_RETURN(FALSE); /* success */
10736 }
10737
get_blob_type_from_length(ulong length)10738 enum_field_types get_blob_type_from_length(ulong length)
10739 {
10740 enum_field_types type;
10741 if (length < 256)
10742 type= MYSQL_TYPE_TINY_BLOB;
10743 else if (length < 65536)
10744 type= MYSQL_TYPE_BLOB;
10745 else if (length < 256L*256L*256L)
10746 type= MYSQL_TYPE_MEDIUM_BLOB;
10747 else
10748 type= MYSQL_TYPE_LONG_BLOB;
10749 return type;
10750 }
10751
10752
pack_length_to_packflag(uint type)10753 uint pack_length_to_packflag(uint type)
10754 {
10755 switch (type) {
10756 case 1: return f_settype((uint) MYSQL_TYPE_TINY);
10757 case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
10758 case 3: return f_settype((uint) MYSQL_TYPE_INT24);
10759 case 4: return f_settype((uint) MYSQL_TYPE_LONG);
10760 case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
10761 }
10762 return 0; // This shouldn't happen
10763 }
10764
10765
make_field(TABLE_SHARE * share,MEM_ROOT * mem_root,uchar * ptr,uint32 field_length,uchar * null_pos,uchar null_bit,uint pack_flag,const Type_handler * handler,CHARSET_INFO * field_charset,Field::geometry_type geom_type,uint srid,Field::utype unireg_check,TYPELIB * interval,const LEX_CSTRING * field_name,uint32 flags)10766 Field *make_field(TABLE_SHARE *share,
10767 MEM_ROOT *mem_root,
10768 uchar *ptr, uint32 field_length,
10769 uchar *null_pos, uchar null_bit,
10770 uint pack_flag,
10771 const Type_handler *handler,
10772 CHARSET_INFO *field_charset,
10773 Field::geometry_type geom_type, uint srid,
10774 Field::utype unireg_check,
10775 TYPELIB *interval,
10776 const LEX_CSTRING *field_name,
10777 uint32 flags)
10778 {
10779 uchar *UNINIT_VAR(bit_ptr);
10780 uchar UNINIT_VAR(bit_offset);
10781
10782 DBUG_PRINT("debug", ("field_type: %s, field_length: %u, interval: %p, pack_flag: %s%s%s%s%s",
10783 handler->name().ptr(), field_length, interval,
10784 FLAGSTR(pack_flag, FIELDFLAG_BINARY),
10785 FLAGSTR(pack_flag, FIELDFLAG_INTERVAL),
10786 FLAGSTR(pack_flag, FIELDFLAG_NUMBER),
10787 FLAGSTR(pack_flag, FIELDFLAG_PACK),
10788 FLAGSTR(pack_flag, FIELDFLAG_BLOB)));
10789
10790 if (handler == &type_handler_row)
10791 {
10792 DBUG_ASSERT(field_length == 0);
10793 DBUG_ASSERT(f_maybe_null(pack_flag));
10794 return new (mem_root) Field_row(ptr, field_name);
10795 }
10796
10797 if (handler->real_field_type() == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
10798 {
10799 bit_ptr= null_pos;
10800 bit_offset= null_bit;
10801 if (f_maybe_null(pack_flag)) // if null field
10802 {
10803 bit_ptr+= (null_bit == 7); // shift bit_ptr and bit_offset
10804 bit_offset= (bit_offset + 1) & 7;
10805 }
10806 }
10807
10808 if (!f_maybe_null(pack_flag))
10809 {
10810 null_pos=0;
10811 null_bit=0;
10812 }
10813 else
10814 {
10815 null_bit= ((uchar) 1) << null_bit;
10816 }
10817
10818
10819 if (f_is_alpha(pack_flag))
10820 {
10821 if (!f_is_packed(pack_flag))
10822 {
10823 enum_field_types field_type= handler->real_field_type();
10824 if (field_type == MYSQL_TYPE_STRING ||
10825 field_type == MYSQL_TYPE_DECIMAL || // 3.23 or 4.0 string
10826 field_type == MYSQL_TYPE_VAR_STRING)
10827 return new (mem_root)
10828 Field_string(ptr,field_length,null_pos,null_bit,
10829 unireg_check, field_name,
10830 field_charset);
10831 if (field_type == MYSQL_TYPE_VARCHAR)
10832 {
10833 if (unireg_check == Field::TMYSQL_COMPRESSED)
10834 return new (mem_root)
10835 Field_varstring_compressed(
10836 ptr, field_length,
10837 HA_VARCHAR_PACKLENGTH(field_length),
10838 null_pos, null_bit,
10839 unireg_check, field_name,
10840 share, field_charset, zlib_compression_method);
10841
10842 return new (mem_root)
10843 Field_varstring(ptr,field_length,
10844 HA_VARCHAR_PACKLENGTH(field_length),
10845 null_pos,null_bit,
10846 unireg_check, field_name,
10847 share,
10848 field_charset);
10849 }
10850 return 0; // Error
10851 }
10852
10853 // MYSQL_TYPE_VAR_STRING is handled above
10854 DBUG_ASSERT(f_packtype(pack_flag) != MYSQL_TYPE_VAR_STRING);
10855 const Type_handler *tmp;
10856 tmp= Type_handler::get_handler_by_real_type((enum_field_types)
10857 f_packtype(pack_flag));
10858 uint pack_length= tmp->calc_pack_length(field_length);
10859
10860 #ifdef HAVE_SPATIAL
10861 if (f_is_geom(pack_flag))
10862 {
10863 status_var_increment(current_thd->status_var.feature_gis);
10864 return new (mem_root)
10865 Field_geom(ptr,null_pos,null_bit,
10866 unireg_check, field_name, share,
10867 pack_length, geom_type, srid);
10868 }
10869 #endif
10870 if (f_is_blob(pack_flag))
10871 {
10872 if (unireg_check == Field::TMYSQL_COMPRESSED)
10873 return new (mem_root)
10874 Field_blob_compressed(ptr, null_pos, null_bit,
10875 unireg_check, field_name, share,
10876 pack_length, field_charset, zlib_compression_method);
10877
10878 return new (mem_root)
10879 Field_blob(ptr,null_pos,null_bit,
10880 unireg_check, field_name, share,
10881 pack_length, field_charset);
10882 }
10883 if (interval)
10884 {
10885 if (f_is_enum(pack_flag))
10886 return new (mem_root)
10887 Field_enum(ptr,field_length,null_pos,null_bit,
10888 unireg_check, field_name,
10889 pack_length, interval, field_charset);
10890 else
10891 return new (mem_root)
10892 Field_set(ptr,field_length,null_pos,null_bit,
10893 unireg_check, field_name,
10894 pack_length, interval, field_charset);
10895 }
10896 }
10897
10898 switch (handler->real_field_type()) {
10899 case MYSQL_TYPE_DECIMAL:
10900 return new (mem_root)
10901 Field_decimal(ptr,field_length,null_pos,null_bit,
10902 unireg_check, field_name,
10903 f_decimals(pack_flag),
10904 f_is_zerofill(pack_flag) != 0,
10905 f_is_dec(pack_flag) == 0);
10906 case MYSQL_TYPE_NEWDECIMAL:
10907 return new (mem_root)
10908 Field_new_decimal(ptr,field_length,null_pos,null_bit,
10909 unireg_check, field_name,
10910 f_decimals(pack_flag),
10911 f_is_zerofill(pack_flag) != 0,
10912 f_is_dec(pack_flag) == 0);
10913 case MYSQL_TYPE_FLOAT:
10914 {
10915 int decimals= f_decimals(pack_flag);
10916 if (decimals == FLOATING_POINT_DECIMALS)
10917 decimals= NOT_FIXED_DEC;
10918 return new (mem_root)
10919 Field_float(ptr,field_length,null_pos,null_bit,
10920 unireg_check, field_name,
10921 decimals,
10922 f_is_zerofill(pack_flag) != 0,
10923 f_is_dec(pack_flag)== 0);
10924 }
10925 case MYSQL_TYPE_DOUBLE:
10926 {
10927 int decimals= f_decimals(pack_flag);
10928 if (decimals == FLOATING_POINT_DECIMALS)
10929 decimals= NOT_FIXED_DEC;
10930 return new (mem_root)
10931 Field_double(ptr,field_length,null_pos,null_bit,
10932 unireg_check, field_name,
10933 decimals,
10934 f_is_zerofill(pack_flag) != 0,
10935 f_is_dec(pack_flag)== 0);
10936 }
10937 case MYSQL_TYPE_TINY:
10938 return new (mem_root)
10939 Field_tiny(ptr,field_length,null_pos,null_bit,
10940 unireg_check, field_name,
10941 f_is_zerofill(pack_flag) != 0,
10942 f_is_dec(pack_flag) == 0);
10943 case MYSQL_TYPE_SHORT:
10944 return new (mem_root)
10945 Field_short(ptr,field_length,null_pos,null_bit,
10946 unireg_check, field_name,
10947 f_is_zerofill(pack_flag) != 0,
10948 f_is_dec(pack_flag) == 0);
10949 case MYSQL_TYPE_INT24:
10950 return new (mem_root)
10951 Field_medium(ptr,field_length,null_pos,null_bit,
10952 unireg_check, field_name,
10953 f_is_zerofill(pack_flag) != 0,
10954 f_is_dec(pack_flag) == 0);
10955 case MYSQL_TYPE_LONG:
10956 return new (mem_root)
10957 Field_long(ptr,field_length,null_pos,null_bit,
10958 unireg_check, field_name,
10959 f_is_zerofill(pack_flag) != 0,
10960 f_is_dec(pack_flag) == 0);
10961 case MYSQL_TYPE_LONGLONG:
10962 if (flags & (VERS_ROW_START|VERS_ROW_END))
10963 {
10964 return new (mem_root)
10965 Field_vers_trx_id(ptr, field_length, null_pos, null_bit,
10966 unireg_check, field_name,
10967 f_is_zerofill(pack_flag) != 0,
10968 f_is_dec(pack_flag) == 0);
10969 }
10970 else
10971 {
10972 return new (mem_root)
10973 Field_longlong(ptr,field_length,null_pos,null_bit,
10974 unireg_check, field_name,
10975 f_is_zerofill(pack_flag) != 0,
10976 f_is_dec(pack_flag) == 0);
10977 }
10978 case MYSQL_TYPE_TIMESTAMP:
10979 {
10980 uint dec= field_length > MAX_DATETIME_WIDTH ?
10981 field_length - MAX_DATETIME_WIDTH - 1: 0;
10982 return new_Field_timestamp(mem_root, ptr, null_pos, null_bit, unireg_check,
10983 field_name, share, dec);
10984 }
10985 case MYSQL_TYPE_TIMESTAMP2:
10986 {
10987 uint dec= field_length > MAX_DATETIME_WIDTH ?
10988 field_length - MAX_DATETIME_WIDTH - 1: 0;
10989 return new (mem_root)
10990 Field_timestampf(ptr, null_pos, null_bit, unireg_check,
10991 field_name, share, dec);
10992 }
10993 case MYSQL_TYPE_YEAR:
10994 return new (mem_root)
10995 Field_year(ptr,field_length,null_pos,null_bit,
10996 unireg_check, field_name);
10997 case MYSQL_TYPE_DATE:
10998 return new (mem_root)
10999 Field_date(ptr,null_pos,null_bit,
11000 unireg_check, field_name);
11001 case MYSQL_TYPE_NEWDATE:
11002 return new (mem_root)
11003 Field_newdate(ptr,null_pos,null_bit,
11004 unireg_check, field_name);
11005 case MYSQL_TYPE_TIME:
11006 {
11007 uint dec= field_length > MIN_TIME_WIDTH ?
11008 field_length - MIN_TIME_WIDTH - 1: 0;
11009 return new_Field_time(mem_root, ptr, null_pos, null_bit, unireg_check,
11010 field_name, dec);
11011 }
11012 case MYSQL_TYPE_TIME2:
11013 {
11014 uint dec= field_length > MIN_TIME_WIDTH ?
11015 field_length - MIN_TIME_WIDTH - 1: 0;
11016 return new (mem_root)
11017 Field_timef(ptr, null_pos, null_bit, unireg_check,
11018 field_name, dec);
11019 }
11020 case MYSQL_TYPE_DATETIME:
11021 {
11022 uint dec= field_length > MAX_DATETIME_WIDTH ?
11023 field_length - MAX_DATETIME_WIDTH - 1: 0;
11024 return new_Field_datetime(mem_root, ptr, null_pos, null_bit, unireg_check,
11025 field_name, dec);
11026 }
11027 case MYSQL_TYPE_DATETIME2:
11028 {
11029 uint dec= field_length > MAX_DATETIME_WIDTH ?
11030 field_length - MAX_DATETIME_WIDTH - 1: 0;
11031 return new (mem_root)
11032 Field_datetimef(ptr, null_pos, null_bit, unireg_check,
11033 field_name, dec);
11034 }
11035 case MYSQL_TYPE_NULL:
11036 return new (mem_root)
11037 Field_null(ptr, field_length, unireg_check, field_name,
11038 field_charset);
11039 case MYSQL_TYPE_BIT:
11040 return (f_bit_as_char(pack_flag) ?
11041 new (mem_root)
11042 Field_bit_as_char(ptr, field_length, null_pos, null_bit,
11043 unireg_check, field_name) :
11044 new (mem_root)
11045 Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
11046 bit_offset, unireg_check, field_name));
11047
11048 default: // Impossible (Wrong version)
11049 break;
11050 }
11051 return 0;
11052 }
11053
test_if_equality_guarantees_uniqueness(const Item * item) const11054 bool Field_vers_trx_id::test_if_equality_guarantees_uniqueness(const Item* item) const
11055 {
11056 return item->type() == Item::DATE_ITEM;
11057 }
11058
11059
11060 /** Create a field suitable for create of table. */
11061
Column_definition(THD * thd,Field * old_field,Field * orig_field)11062 Column_definition::Column_definition(THD *thd, Field *old_field,
11063 Field *orig_field)
11064 {
11065 on_update= NULL;
11066 field_name= old_field->field_name;
11067 length= old_field->field_length;
11068 flags= old_field->flags;
11069 unireg_check=old_field->unireg_check;
11070 pack_length=old_field->pack_length();
11071 key_length= old_field->key_length();
11072 set_handler(old_field->type_handler());
11073 charset= old_field->charset(); // May be NULL ptr
11074 comment= old_field->comment;
11075 decimals= old_field->decimals();
11076 vcol_info= old_field->vcol_info;
11077 option_list= old_field->option_list;
11078 pack_flag= 0;
11079 compression_method_ptr= 0;
11080 versioning= VERSIONING_NOT_SET;
11081 invisible= old_field->invisible;
11082
11083 if (orig_field)
11084 {
11085 default_value= orig_field->default_value;
11086 check_constraint= orig_field->check_constraint;
11087 if (orig_field->unireg_check == Field::TMYSQL_COMPRESSED)
11088 {
11089 unireg_check= Field::TMYSQL_COMPRESSED;
11090 compression_method_ptr= zlib_compression_method;
11091 }
11092 }
11093 else
11094 {
11095 default_value= 0;
11096 check_constraint= 0;
11097 }
11098
11099 switch (real_field_type()) {
11100 case MYSQL_TYPE_TINY_BLOB:
11101 case MYSQL_TYPE_BLOB:
11102 case MYSQL_TYPE_MEDIUM_BLOB:
11103 case MYSQL_TYPE_LONG_BLOB:
11104 length/= charset->mbmaxlen;
11105 key_length/= charset->mbmaxlen;
11106 break;
11107 case MYSQL_TYPE_STRING:
11108 /* Change CHAR -> VARCHAR if dynamic record length */
11109 if (old_field->type() == MYSQL_TYPE_VAR_STRING)
11110 set_handler(&type_handler_varchar);
11111 /* fall through */
11112
11113 case MYSQL_TYPE_ENUM:
11114 case MYSQL_TYPE_SET:
11115 case MYSQL_TYPE_VARCHAR:
11116 case MYSQL_TYPE_VAR_STRING:
11117 /* This is corrected in create_length_to_internal_length */
11118 length= (length+charset->mbmaxlen-1) / charset->mbmaxlen -
11119 MY_TEST(old_field->compression_method());
11120 break;
11121 #ifdef HAVE_SPATIAL
11122 case MYSQL_TYPE_GEOMETRY:
11123 geom_type= ((Field_geom*)old_field)->geom_type;
11124 srid= ((Field_geom*)old_field)->srid;
11125 break;
11126 #endif
11127 case MYSQL_TYPE_YEAR:
11128 if (length != 4)
11129 {
11130 char buff[sizeof("YEAR()") + MY_INT64_NUM_DECIMAL_DIGITS + 1];
11131 my_snprintf(buff, sizeof(buff), "YEAR(%llu)", length);
11132 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
11133 ER_WARN_DEPRECATED_SYNTAX,
11134 ER_THD(thd, ER_WARN_DEPRECATED_SYNTAX),
11135 buff, "YEAR(4)");
11136 }
11137 break;
11138 case MYSQL_TYPE_FLOAT:
11139 case MYSQL_TYPE_DOUBLE:
11140 /*
11141 Floating points are stored with FLOATING_POINT_DECIMALS but internally
11142 in MariaDB used with NOT_FIXED_DEC, which is >= FLOATING_POINT_DECIMALS.
11143 */
11144 if (decimals >= FLOATING_POINT_DECIMALS)
11145 decimals= NOT_FIXED_DEC;
11146 break;
11147 default:
11148 break;
11149 }
11150
11151 if (flags & (ENUM_FLAG | SET_FLAG))
11152 interval= ((Field_enum*) old_field)->typelib;
11153 else
11154 interval=0;
11155
11156 interval_list.empty(); // prepare_interval_field() needs this
11157
11158 char_length= (uint)length;
11159
11160 /*
11161 Copy the default (constant/function) from the column object orig_field, if
11162 supplied. We do this if all these conditions are met:
11163
11164 - The column allows a default.
11165
11166 - The column type is not a BLOB type (as BLOB's doesn't have constant
11167 defaults)
11168
11169 - The original column (old_field) was properly initialized with a record
11170 buffer pointer.
11171
11172 - The column didn't have a default expression
11173 */
11174 if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
11175 old_field->ptr != NULL && orig_field != NULL)
11176 {
11177 if (orig_field->unireg_check != Field::NEXT_NUMBER)
11178 unireg_check= orig_field->unireg_check;
11179
11180 /* Get the value from default_values */
11181 const uchar *dv= orig_field->table->s->default_values;
11182 if (!default_value && !orig_field->is_null_in_record(dv))
11183 {
11184 StringBuffer<MAX_FIELD_WIDTH> tmp(charset);
11185 String *res= orig_field->val_str(&tmp, orig_field->ptr_in_record(dv));
11186 char *pos= (char*) thd->strmake(res->ptr(), res->length());
11187 default_value= new (thd->mem_root) Virtual_column_info();
11188 default_value->expr=
11189 new (thd->mem_root) Item_string(thd, pos, res->length(), charset);
11190 default_value->utf8= 0;
11191 }
11192 }
11193 }
11194
11195
11196 /**
11197 The common part for data type redefinition:
11198 CREATE TABLE t1 (a INT) AS SELECT a FROM t2;
11199 See Type_handler::Column_definition_redefine_stage1()
11200 for data type specific code.
11201 */
11202 void
redefine_stage1_common(const Column_definition * dup_field,const handler * file,const Schema_specification_st * schema)11203 Column_definition::redefine_stage1_common(const Column_definition *dup_field,
11204 const handler *file,
11205 const Schema_specification_st *schema)
11206 {
11207 set_handler(dup_field->type_handler());
11208 default_value= dup_field->default_value;
11209 charset= dup_field->charset ? dup_field->charset :
11210 schema->default_table_charset;
11211 length= dup_field->char_length;
11212 pack_length= dup_field->pack_length;
11213 key_length= dup_field->key_length;
11214 decimals= dup_field->decimals;
11215 unireg_check= dup_field->unireg_check;
11216 flags= dup_field->flags;
11217 interval= dup_field->interval;
11218 vcol_info= dup_field->vcol_info;
11219 invisible= dup_field->invisible;
11220 check_constraint= dup_field->check_constraint;
11221 }
11222
11223
11224
11225 /**
11226 maximum possible character length for blob.
11227
11228 This method is used in Item_field::set_field to calculate
11229 max_length for Item.
11230
11231 For example:
11232 CREATE TABLE t2 SELECT CONCAT(tinyblob_utf8_column) FROM t1;
11233 must create a "VARCHAR(255) CHARACTER SET utf8" column.
11234
11235 @return
11236 length
11237 */
11238
char_length() const11239 uint32 Field_blob::char_length() const
11240 {
11241 return Field_blob::octet_length();
11242 }
11243
11244
octet_length() const11245 uint32 Field_blob::octet_length() const
11246 {
11247 switch (packlength)
11248 {
11249 case 1:
11250 return 255;
11251 case 2:
11252 return 65535;
11253 case 3:
11254 return 16777215;
11255 case 4:
11256 return (uint32) UINT_MAX32;
11257 default:
11258 DBUG_ASSERT(0); // we should never go here
11259 return 0;
11260 }
11261 }
11262
11263
11264 /**
11265 Makes a clone of this object for ALTER/CREATE TABLE
11266
11267 @param mem_root MEM_ROOT where to clone the field
11268 */
11269
clone(MEM_ROOT * mem_root) const11270 Create_field *Create_field::clone(MEM_ROOT *mem_root) const
11271 {
11272 Create_field *res= new (mem_root) Create_field(*this);
11273 return res;
11274 }
11275
11276 /**
11277 Return true if default is an expression that must be saved explicitely
11278
11279 This is:
11280 - Not basic constants
11281 - If field is a BLOB (Which doesn't support normal DEFAULT)
11282 */
11283
has_default_expression()11284 bool Column_definition::has_default_expression()
11285 {
11286 return (default_value &&
11287 (!default_value->expr->basic_const_item() ||
11288 (flags & BLOB_FLAG)));
11289 }
11290
11291
set_compressed(const char * method)11292 bool Column_definition::set_compressed(const char *method)
11293 {
11294 if (!method || !strcmp(method, zlib_compression_method->name))
11295 {
11296 unireg_check= Field::TMYSQL_COMPRESSED;
11297 compression_method_ptr= zlib_compression_method;
11298 return false;
11299 }
11300 my_error(ER_UNKNOWN_COMPRESSION_METHOD, MYF(0), method);
11301 return true;
11302 }
11303
11304
set_compressed_deprecated(THD * thd,const char * method)11305 bool Column_definition::set_compressed_deprecated(THD *thd, const char *method)
11306 {
11307 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
11308 ER_WARN_DEPRECATED_SYNTAX,
11309 ER_THD(thd, ER_WARN_DEPRECATED_SYNTAX),
11310 "<data type> <character set clause> ... COMPRESSED...",
11311 "'<data type> COMPRESSED... <character set clause> ...'");
11312 return set_compressed(method);
11313 }
11314
11315
11316 bool
set_compressed_deprecated_column_attribute(THD * thd,const char * pos,const char * method)11317 Column_definition::set_compressed_deprecated_column_attribute(THD *thd,
11318 const char *pos,
11319 const char *method)
11320 {
11321 if (compression_method_ptr)
11322 {
11323 /*
11324 Compression method has already been set, e.g.:
11325 a VARCHAR(10) COMPRESSED DEFAULT 10 COMPRESSED
11326 */
11327 thd->parse_error(ER_SYNTAX_ERROR, pos);
11328 return true;
11329 }
11330 enum enum_field_types sql_type= real_field_type();
11331 /* We can't use f_is_blob here as pack_flag is not yet set */
11332 if (sql_type == MYSQL_TYPE_VARCHAR || sql_type == MYSQL_TYPE_TINY_BLOB ||
11333 sql_type == MYSQL_TYPE_BLOB || sql_type == MYSQL_TYPE_MEDIUM_BLOB ||
11334 sql_type == MYSQL_TYPE_LONG_BLOB)
11335 return set_compressed_deprecated(thd, method);
11336 else
11337 my_error(ER_WRONG_FIELD_SPEC, MYF(0), field_name.str);
11338 return true;
11339 }
11340
11341
11342 /**
11343 maximum possible display length for blob.
11344
11345 @return
11346 length
11347 */
11348
max_display_length() const11349 uint32 Field_blob::max_display_length() const
11350 {
11351 switch (packlength)
11352 {
11353 case 1:
11354 return 255 * field_charset->mbmaxlen;
11355 case 2:
11356 return 65535 * field_charset->mbmaxlen;
11357 case 3:
11358 return 16777215 * field_charset->mbmaxlen;
11359 case 4:
11360 return (uint32) UINT_MAX32;
11361 default:
11362 DBUG_ASSERT(0); // we should never go here
11363 return 0;
11364 }
11365 }
11366
11367
11368 /*****************************************************************************
11369 Warning handling
11370 *****************************************************************************/
11371
11372 /**
11373 * Produce warning or note about data saved into field.
11374
11375 @param level - level of message (Note/Warning/Error)
11376 @param code - error code of message to be produced
11377 @param cut_increment - whenever we should increase cut fields count
11378 @current_row - current row number
11379
11380 @note
11381 This function won't produce warning or notes or increase cut fields counter
11382 if count_cuted_fields == CHECK_FIELD_IGNORE or CHECK_FIELD_EXPRESSION
11383 for the current thread.
11384
11385 This allows us to avoid notes in optimisation, like
11386 convert_constant_item().
11387
11388 @retval
11389 1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
11390 @retval
11391 0 otherwise
11392 */
11393
11394 bool
set_warning(Sql_condition::enum_warning_level level,uint code,int cut_increment,ulong current_row) const11395 Field::set_warning(Sql_condition::enum_warning_level level, uint code,
11396 int cut_increment, ulong current_row) const
11397 {
11398 /*
11399 If this field was created only for type conversion purposes it
11400 will have table == NULL.
11401 */
11402 THD *thd= get_thd();
11403 if (thd->count_cuted_fields > CHECK_FIELD_EXPRESSION)
11404 {
11405 thd->cuted_fields+= cut_increment;
11406 push_warning_printf(thd, level, code, ER_THD(thd, code), field_name.str,
11407 current_row ? current_row
11408 : thd->get_stmt_da()->current_row_for_warning());
11409 return 0;
11410 }
11411 return level >= Sql_condition::WARN_LEVEL_WARN;
11412 }
11413
11414
11415 /**
11416 Produce warning or note about datetime string data saved into field.
11417
11418 @param level level of message (Note/Warning/Error)
11419 @param code error code of message to be produced
11420 @param str string value which we tried to save
11421 @param ts_type type of datetime value (datetime/date/time)
11422 @param cuted_increment whenever we should increase cut fields count or not
11423
11424 @note
11425 This function will always produce some warning but won't increase cut
11426 fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
11427 thread.
11428
11429 See also bug#2336
11430
11431 */
11432
set_datetime_warning(Sql_condition::enum_warning_level level,uint code,const ErrConv * str,timestamp_type ts_type,int cuted_increment) const11433 void Field::set_datetime_warning(Sql_condition::enum_warning_level level,
11434 uint code, const ErrConv *str,
11435 timestamp_type ts_type, int cuted_increment)
11436 const
11437 {
11438 THD *thd= get_thd();
11439 if (thd->really_abort_on_warning() && level >= Sql_condition::WARN_LEVEL_WARN)
11440 make_truncated_value_warning(thd, level, str, ts_type,
11441 table->s->db.str, table->s->table_name.str,
11442 field_name.str);
11443 else
11444 set_warning(level, code, cuted_increment);
11445 }
11446
11447
set_warning_truncated_wrong_value(const char * type_arg,const char * value)11448 void Field::set_warning_truncated_wrong_value(const char *type_arg,
11449 const char *value)
11450 {
11451 THD *thd= get_thd();
11452 const char *db_name;
11453 const char *table_name;
11454 /*
11455 table has in the past been 0 in case of wrong calls when processing
11456 statistics tables. Let's protect against that.
11457 */
11458 DBUG_ASSERT(table);
11459
11460 db_name= (table && table->s->db.str) ? table->s->db.str : "";
11461 table_name= (table && table->s->table_name.str) ?
11462 table->s->table_name.str : "";
11463
11464 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
11465 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
11466 ER_THD(thd, ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
11467 type_arg, value, db_name, table_name, field_name.str,
11468 static_cast<ulong>(thd->get_stmt_da()->
11469 current_row_for_warning()));
11470 }
11471
11472
11473 /*
11474 @brief
11475 Return possible keys for a field
11476
11477 @details
11478 Return bit map of keys over this field which can be used by the range
11479 optimizer. For a field of a generic table such keys are all keys that starts
11480 from this field. For a field of a materialized derived table/view such keys
11481 are all keys in which this field takes a part. This is less restrictive as
11482 keys for a materialized derived table/view are generated on the fly from
11483 present fields, thus the case when a field for the beginning of a key is
11484 absent is impossible.
11485
11486 @return map of possible keys
11487 */
11488
get_possible_keys()11489 key_map Field::get_possible_keys()
11490 {
11491 DBUG_ASSERT(table->pos_in_table_list);
11492 return (table->pos_in_table_list->is_materialized_derived() ?
11493 part_of_key : key_start);
11494 }
11495
11496
validate_value_in_record_with_warn(THD * thd,const uchar * record)11497 bool Field::validate_value_in_record_with_warn(THD *thd, const uchar *record)
11498 {
11499 MY_BITMAP *old_map= dbug_tmp_use_all_columns(table, &table->read_set);
11500 bool rc;
11501 if ((rc= validate_value_in_record(thd, record)))
11502 {
11503 // Get and report val_str() for the DEFAULT value
11504 StringBuffer<MAX_FIELD_WIDTH> tmp;
11505 val_str(&tmp, ptr_in_record(record));
11506 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
11507 ER_INVALID_DEFAULT_VALUE_FOR_FIELD,
11508 ER_THD(thd, ER_INVALID_DEFAULT_VALUE_FOR_FIELD),
11509 ErrConvString(&tmp).ptr(), field_name.str);
11510 }
11511 dbug_tmp_restore_column_map(&table->read_set, old_map);
11512 return rc;
11513 }
11514
11515
save_in_field_default_value(bool view_error_processing)11516 bool Field::save_in_field_default_value(bool view_error_processing)
11517 {
11518 THD *thd= table->in_use;
11519
11520 /*
11521 TODO: MDEV-19597 Refactor TABLE::vers_update_fields() via stored virtual columns
11522 This condition will go away as well as other conditions with vers_sys_field().
11523 */
11524 if (vers_sys_field())
11525 return false;
11526
11527 if (unlikely(flags & NO_DEFAULT_VALUE_FLAG &&
11528 real_type() != MYSQL_TYPE_ENUM))
11529 {
11530 if (reset())
11531 {
11532 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
11533 ER_THD(thd, ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
11534 return true;
11535 }
11536
11537 if (view_error_processing)
11538 {
11539 TABLE_LIST *view= table->pos_in_table_list->top_table();
11540 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
11541 ER_NO_DEFAULT_FOR_VIEW_FIELD,
11542 ER_THD(thd, ER_NO_DEFAULT_FOR_VIEW_FIELD),
11543 view->view_db.str,
11544 view->view_name.str);
11545 }
11546 else
11547 {
11548 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
11549 ER_NO_DEFAULT_FOR_FIELD,
11550 ER_THD(thd, ER_NO_DEFAULT_FOR_FIELD),
11551 field_name.str);
11552 }
11553 return true;
11554 }
11555 set_default();
11556 return
11557 !is_null() &&
11558 validate_value_in_record_with_warn(thd, table->record[0]) &&
11559 thd->is_error();
11560 }
11561
11562
save_in_field_ignore_value(bool view_error_processing)11563 bool Field::save_in_field_ignore_value(bool view_error_processing)
11564 {
11565 enum_sql_command com= table->in_use->lex->sql_command;
11566 // All insert-like commands
11567 if (com == SQLCOM_INSERT || com == SQLCOM_REPLACE ||
11568 com == SQLCOM_INSERT_SELECT || com == SQLCOM_REPLACE_SELECT ||
11569 com == SQLCOM_LOAD)
11570 return save_in_field_default_value(view_error_processing);
11571 return 0; // ignore
11572 }
11573
11574
register_field_in_read_map()11575 void Field::register_field_in_read_map()
11576 {
11577 if (vcol_info)
11578 {
11579 Item *vcol_item= vcol_info->expr;
11580 vcol_item->walk(&Item::register_field_in_read_map, 1, 0);
11581 }
11582 bitmap_set_bit(table->read_set, field_index);
11583 }
11584
11585
val_str_nopad(MEM_ROOT * mem_root,LEX_CSTRING * to)11586 bool Field::val_str_nopad(MEM_ROOT *mem_root, LEX_CSTRING *to)
11587 {
11588 StringBuffer<MAX_FIELD_WIDTH> str;
11589 bool rc= false;
11590 THD *thd= get_thd();
11591 sql_mode_t sql_mode_backup= thd->variables.sql_mode;
11592 thd->variables.sql_mode&= ~MODE_PAD_CHAR_TO_FULL_LENGTH;
11593
11594 val_str(&str);
11595 if (!(to->length= str.length()))
11596 *to= empty_clex_str;
11597 else if ((rc= !(to->str= strmake_root(mem_root, str.ptr(), str.length()))))
11598 to->length= 0;
11599
11600 thd->variables.sql_mode= sql_mode_backup;
11601 return rc;
11602 }
11603