1 /*
2 Copyright (c) 2015, 2021, MariaDB
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
16
17 #include "mariadb.h"
18 #include "sql_type.h"
19 #include "sql_type_geom.h"
20 #include "sql_const.h"
21 #include "sql_class.h"
22 #include "sql_time.h"
23 #include "sql_string.h"
24 #include "item.h"
25 #include "log.h"
26 #include "tztime.h"
27 #include <mysql/plugin_data_type.h>
28
29
singleton()30 const DTCollation &DTCollation_numeric::singleton()
31 {
32 static const DTCollation_numeric tmp;
33 return tmp;
34 }
35
36 Named_type_handler<Type_handler_row> type_handler_row("row");
37
38 Named_type_handler<Type_handler_null> type_handler_null("null");
39
40 Named_type_handler<Type_handler_bool> type_handler_bool("boolean");
41 Named_type_handler<Type_handler_tiny> type_handler_stiny("tinyint");
42 Named_type_handler<Type_handler_short> type_handler_sshort("smallint");
43 Named_type_handler<Type_handler_long> type_handler_slong("int");
44 Named_type_handler<Type_handler_int24> type_handler_sint24("mediumint");
45 Named_type_handler<Type_handler_longlong> type_handler_slonglong("bigint");
46 Named_type_handler<Type_handler_utiny> type_handler_utiny("tiny unsigned");
47 Named_type_handler<Type_handler_ushort> type_handler_ushort("smallint unsigned");
48 Named_type_handler<Type_handler_ulong> type_handler_ulong("int unsigned");
49 Named_type_handler<Type_handler_uint24> type_handler_uint24("mediumint unsigned");
50 Named_type_handler<Type_handler_ulonglong> type_handler_ulonglong("bigint unsigned");
51 Named_type_handler<Type_handler_vers_trx_id> type_handler_vers_trx_id("bigint unsigned");
52 Named_type_handler<Type_handler_float> type_handler_float("float");
53 Named_type_handler<Type_handler_double> type_handler_double("double");
54 Named_type_handler<Type_handler_bit> type_handler_bit("bit");
55
56 Named_type_handler<Type_handler_olddecimal> type_handler_olddecimal("decimal");
57 Named_type_handler<Type_handler_newdecimal> type_handler_newdecimal("decimal");
58
59 Named_type_handler<Type_handler_year> type_handler_year("year");
60 Named_type_handler<Type_handler_year> type_handler_year2("year");
61 Named_type_handler<Type_handler_time> type_handler_time("time");
62 Named_type_handler<Type_handler_date> type_handler_date("date");
63 Named_type_handler<Type_handler_timestamp> type_handler_timestamp("timestamp");
64 Named_type_handler<Type_handler_timestamp2> type_handler_timestamp2("timestamp");
65 Named_type_handler<Type_handler_datetime> type_handler_datetime("datetime");
66 Named_type_handler<Type_handler_time2> type_handler_time2("time");
67 Named_type_handler<Type_handler_newdate> type_handler_newdate("date");
68 Named_type_handler<Type_handler_datetime2> type_handler_datetime2("datetime");
69
70 Named_type_handler<Type_handler_enum> type_handler_enum("enum");
71 Named_type_handler<Type_handler_set> type_handler_set("set");
72
73 Named_type_handler<Type_handler_string> type_handler_string("char");
74 Named_type_handler<Type_handler_var_string> type_handler_var_string("varchar");
75 Named_type_handler<Type_handler_varchar> type_handler_varchar("varchar");
76 Named_type_handler<Type_handler_hex_hybrid> type_handler_hex_hybrid("hex_hybrid");
77 Named_type_handler<Type_handler_varchar_compressed> type_handler_varchar_compressed("varchar");
78
79 Named_type_handler<Type_handler_tiny_blob> type_handler_tiny_blob("tinyblob");
80 Named_type_handler<Type_handler_medium_blob> type_handler_medium_blob("mediumblob");
81 Named_type_handler<Type_handler_long_blob> type_handler_long_blob("longblob");
82 Named_type_handler<Type_handler_blob> type_handler_blob("blob");
83 Named_type_handler<Type_handler_blob_compressed> type_handler_blob_compressed("blob");
84
85 Type_handler_interval_DDhhmmssff type_handler_interval_DDhhmmssff;
86
87 Vers_type_timestamp vers_type_timestamp;
88 Vers_type_trx vers_type_trx;
89
90 /***************************************************************************/
91
92
93
94 class Type_collection_std: public Type_collection
95 {
96 public:
handler_by_name(const LEX_CSTRING & name) const97 const Type_handler *handler_by_name(const LEX_CSTRING &name) const override
98 {
99 return NULL;
100 }
aggregate_for_result(const Type_handler * a,const Type_handler * b) const101 const Type_handler *aggregate_for_result(const Type_handler *a,
102 const Type_handler *b)
103 const override
104 {
105 return Type_handler::aggregate_for_result_traditional(a, b);
106 }
107 const Type_handler *aggregate_for_comparison(const Type_handler *a,
108 const Type_handler *b)
109 const override;
110 const Type_handler *aggregate_for_min_max(const Type_handler *a,
111 const Type_handler *b)
112 const override;
113 const Type_handler *aggregate_for_num_op(const Type_handler *a,
114 const Type_handler *b)
115 const override;
116 };
117
118
119 static Type_collection_std type_collection_std;
120
type_collection() const121 const Type_collection *Type_handler::type_collection() const
122 {
123 return &type_collection_std;
124 }
125
126
is_traditional_scalar_type() const127 bool Type_handler::is_traditional_scalar_type() const
128 {
129 return type_collection() == &type_collection_std;
130 }
131
132
133 class Type_collection_row: public Type_collection
134 {
135 public:
init(Type_handler_data * data)136 bool init(Type_handler_data *data) override
137 {
138 return false;
139 }
handler_by_name(const LEX_CSTRING & name) const140 const Type_handler *handler_by_name(const LEX_CSTRING &name) const override
141 {
142 return NULL;
143 }
aggregate_for_result(const Type_handler * a,const Type_handler * b) const144 const Type_handler *aggregate_for_result(const Type_handler *a,
145 const Type_handler *b)
146 const override
147 {
148 return NULL;
149 }
aggregate_for_comparison(const Type_handler * a,const Type_handler * b) const150 const Type_handler *aggregate_for_comparison(const Type_handler *a,
151 const Type_handler *b)
152 const override
153 {
154 DBUG_ASSERT(a == &type_handler_row);
155 DBUG_ASSERT(b == &type_handler_row);
156 return &type_handler_row;
157 }
aggregate_for_min_max(const Type_handler * a,const Type_handler * b) const158 const Type_handler *aggregate_for_min_max(const Type_handler *a,
159 const Type_handler *b)
160 const override
161 {
162 return NULL;
163 }
aggregate_for_num_op(const Type_handler * a,const Type_handler * b) const164 const Type_handler *aggregate_for_num_op(const Type_handler *a,
165 const Type_handler *b)
166 const override
167 {
168 return NULL;
169 }
170 };
171
172
173 static Type_collection_row type_collection_row;
174
type_collection() const175 const Type_collection *Type_handler_row::type_collection() const
176 {
177 return &type_collection_row;
178 }
179
180
init()181 bool Type_handler_data::init()
182 {
183 #ifdef HAVE_SPATIAL
184 return type_collection_geometry.init(this);
185 #endif
186 return false;
187 }
188
189
schema() const190 Schema *Type_handler::schema() const
191 {
192 return &mariadb_schema;
193 }
194
195
196 const Type_handler *
handler_by_name(THD * thd,const LEX_CSTRING & name)197 Type_handler::handler_by_name(THD *thd, const LEX_CSTRING &name)
198 {
199 plugin_ref plugin;
200 if ((plugin= my_plugin_lock_by_name(thd, &name, MariaDB_DATA_TYPE_PLUGIN)))
201 {
202 /*
203 Data type plugins do not maintain ref_count yet.
204 For now we have only mandatory built-in plugins
205 and dynamic plugins for test purposes.
206 It should be safe to unlock the plugin immediately.
207 */
208 const Type_handler *ph= reinterpret_cast<st_mariadb_data_type*>
209 (plugin_decl(plugin)->info)->type_handler;
210 plugin_unlock(thd, plugin);
211 return ph;
212 }
213
214 #ifdef HAVE_SPATIAL
215 const Type_handler *ha= type_collection_geometry.handler_by_name(name);
216 if (ha)
217 return ha;
218 #endif
219 return NULL;
220 }
221
222
223 #ifndef DBUG_OFF
frm_data_type_info_emulate(const LEX_CSTRING & name)224 static const Type_handler *frm_data_type_info_emulate(const LEX_CSTRING &name)
225 {
226 if (Name(STRING_WITH_LEN("xchar")).eq(name))
227 return &type_handler_string;
228 if (Name(STRING_WITH_LEN("xblob")).eq(name))
229 return &type_handler_blob;
230 return NULL;
231 }
232 #endif
233
234
235 const Type_handler *
handler_by_name_or_error(THD * thd,const LEX_CSTRING & name)236 Type_handler::handler_by_name_or_error(THD *thd, const LEX_CSTRING &name)
237 {
238 const Type_handler *h= handler_by_name(thd, name);
239 DBUG_EXECUTE_IF("emulate_handler_by_name_or_error_failure", h= NULL;);
240 if (!h)
241 {
242 DBUG_EXECUTE_IF("frm_data_type_info_emulate",
243 if ((h= frm_data_type_info_emulate(name)))
244 return h;
245 );
246 my_error(ER_UNKNOWN_DATA_TYPE, MYF(0),
247 ErrConvString(name.str, name.length, system_charset_info).ptr());
248 }
249 return h;
250 }
251
252
253 Type_handler_data *type_handler_data= NULL;
254
255
to_string(String * val_buffer,uint dec) const256 bool Float::to_string(String *val_buffer, uint dec) const
257 {
258 uint to_length= 70;
259 if (val_buffer->alloc(to_length))
260 return true;
261
262 char *to=(char*) val_buffer->ptr();
263 size_t len;
264
265 if (dec >= FLOATING_POINT_DECIMALS)
266 len= my_gcvt(m_value, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
267 else
268 {
269 /*
270 We are safe here because the buffer length is 70, and
271 fabs(float) < 10^39, dec < FLOATING_POINT_DECIMALS. So the resulting string
272 will be not longer than 69 chars + terminating '\0'.
273 */
274 len= my_fcvt(m_value, (int) dec, to, NULL);
275 }
276 val_buffer->length((uint) len);
277 val_buffer->set_charset(&my_charset_numeric);
278 return false;
279 }
280
281
String_ptr(Item * item,String * buffer)282 String_ptr::String_ptr(Item *item, String *buffer)
283 :m_string_ptr(item->val_str(buffer))
284 { }
285
286
Ascii_ptr(Item * item,String * buffer)287 Ascii_ptr::Ascii_ptr(Item *item, String *buffer)
288 :String_ptr(item->val_str_ascii(buffer))
289 { }
290
291
set(Item * item)292 void VDec::set(Item *item)
293 {
294 m_ptr= item->val_decimal(&m_buffer);
295 DBUG_ASSERT((m_ptr == NULL) == item->null_value);
296 }
297
298
VDec(Item * item)299 VDec::VDec(Item *item)
300 {
301 m_ptr= item->val_decimal(&m_buffer);
302 DBUG_ASSERT((m_ptr == NULL) == item->null_value);
303 }
304
305
VDec_op(Item_func_hybrid_field_type * item)306 VDec_op::VDec_op(Item_func_hybrid_field_type *item)
307 {
308 m_ptr= item->decimal_op(&m_buffer);
309 DBUG_ASSERT((m_ptr == NULL) == item->null_value);
310 }
311
312
sql_mode_for_dates(THD * thd)313 date_conv_mode_t Temporal::sql_mode_for_dates(THD *thd)
314 {
315 return ::sql_mode_for_dates(thd);
316 }
317
318
default_round_mode(THD * thd)319 time_round_mode_t Temporal::default_round_mode(THD *thd)
320 {
321 return thd->temporal_round_mode();
322 }
323
324
default_round_mode(THD * thd)325 time_round_mode_t Timestamp::default_round_mode(THD *thd)
326 {
327 return thd->temporal_round_mode();
328 }
329
330
to_decimal(my_decimal * to) const331 my_decimal *Temporal::to_decimal(my_decimal *to) const
332 {
333 return date2my_decimal(this, to);
334 }
335
336
bad_to_decimal(my_decimal * to) const337 my_decimal *Temporal::bad_to_decimal(my_decimal *to) const
338 {
339 my_decimal_set_zero(to);
340 return NULL;
341 }
342
343
make_from_str(THD * thd,Warn * warn,const char * str,size_t length,CHARSET_INFO * cs,date_mode_t fuzzydate)344 void Temporal::make_from_str(THD *thd, Warn *warn,
345 const char *str, size_t length,
346 CHARSET_INFO *cs, date_mode_t fuzzydate)
347 {
348 DBUG_EXECUTE_IF("str_to_datetime_warn",
349 push_warning(thd, Sql_condition::WARN_LEVEL_NOTE,
350 ER_YES, ErrConvString(str, length,cs).ptr()););
351
352 if (str_to_temporal(thd, warn, str, length, cs, fuzzydate))
353 make_fuzzy_date(&warn->warnings, date_conv_mode_t(fuzzydate));
354 if (warn->warnings)
355 warn->set_str(str, length, &my_charset_bin);
356 }
357
358
Temporal_hybrid(THD * thd,Item * item,date_mode_t fuzzydate)359 Temporal_hybrid::Temporal_hybrid(THD *thd, Item *item, date_mode_t fuzzydate)
360 {
361 if (item->get_date(thd, this, fuzzydate))
362 time_type= MYSQL_TIMESTAMP_NONE;
363 }
364
365
binary_length_to_precision(uint length)366 uint Timestamp::binary_length_to_precision(uint length)
367 {
368 switch (length) {
369 case 4: return 0;
370 case 5: return 2;
371 case 6: return 4;
372 case 7: return 6;
373 }
374 DBUG_ASSERT(0);
375 return 0;
376 }
377
378
Timestamp(const Native & native)379 Timestamp::Timestamp(const Native &native)
380 {
381 DBUG_ASSERT(native.length() >= 4 && native.length() <= 7);
382 uint dec= binary_length_to_precision(native.length());
383 my_timestamp_from_binary(this, (const uchar *) native.ptr(), dec);
384 }
385
386
to_native(Native * to,uint decimals) const387 bool Timestamp::to_native(Native *to, uint decimals) const
388 {
389 uint len= my_timestamp_binary_length(decimals);
390 if (to->reserve(len))
391 return true;
392 my_timestamp_to_binary(this, (uchar *) to->ptr(), decimals);
393 to->length(len);
394 return false;
395 }
396
397
to_TIME(THD * thd,MYSQL_TIME * to,date_mode_t fuzzydate) const398 bool Timestamp::to_TIME(THD *thd, MYSQL_TIME *to, date_mode_t fuzzydate) const
399 {
400 return thd->timestamp_to_TIME(to, tv_sec, tv_usec, fuzzydate);
401 }
402
403
Timestamp(THD * thd,const MYSQL_TIME * ltime,uint * error_code)404 Timestamp::Timestamp(THD *thd, const MYSQL_TIME *ltime, uint *error_code)
405 :Timeval(TIME_to_timestamp(thd, ltime, error_code), ltime->second_part)
406 { }
407
408
Timestamp_or_zero_datetime(THD * thd,const MYSQL_TIME * ltime,uint * error_code)409 Timestamp_or_zero_datetime::Timestamp_or_zero_datetime(THD *thd,
410 const MYSQL_TIME *ltime,
411 uint *error_code)
412 :Timestamp(thd, ltime, error_code),
413 m_is_zero_datetime(*error_code == ER_WARN_DATA_OUT_OF_RANGE)
414 {
415 if (m_is_zero_datetime)
416 {
417 if (!non_zero_date(ltime))
418 *error_code= 0; // ltime was '0000-00-00 00:00:00'
419 }
420 else if (*error_code == ER_WARN_INVALID_TIMESTAMP)
421 *error_code= 0; // ltime fell into spring time gap, adjusted.
422 }
423
424
to_TIME(THD * thd,MYSQL_TIME * to,date_mode_t fuzzydate) const425 bool Timestamp_or_zero_datetime::to_TIME(THD *thd, MYSQL_TIME *to,
426 date_mode_t fuzzydate) const
427 {
428 if (m_is_zero_datetime)
429 {
430 set_zero_time(to, MYSQL_TIMESTAMP_DATETIME);
431 return false;
432 }
433 return Timestamp::to_TIME(thd, to, fuzzydate);
434 }
435
436
to_native(Native * to,uint decimals) const437 bool Timestamp_or_zero_datetime::to_native(Native *to, uint decimals) const
438 {
439 if (m_is_zero_datetime)
440 {
441 to->length(0);
442 return false;
443 }
444 return Timestamp::to_native(to, decimals);
445 }
446
447
save_in_field(Field * field,uint decimals) const448 int Timestamp_or_zero_datetime_native::save_in_field(Field *field,
449 uint decimals) const
450 {
451 field->set_notnull();
452 if (field->type_handler()->type_handler_for_native_format() ==
453 &type_handler_timestamp2)
454 return field->store_native(*this);
455 if (is_zero_datetime())
456 {
457 static Datetime zero(Datetime::zero());
458 return field->store_time_dec(zero.get_mysql_time(), decimals);
459 }
460 return field->store_timestamp_dec(Timestamp(*this).tv(), decimals);
461 }
462
463
make_from_decimal(const my_decimal * d,ulong * nanoseconds)464 void Sec6::make_from_decimal(const my_decimal *d, ulong *nanoseconds)
465 {
466 m_neg= my_decimal2seconds(d, &m_sec, &m_usec, nanoseconds);
467 m_truncated= (m_sec >= LONGLONG_MAX);
468 }
469
470
make_from_double(double nr,ulong * nanoseconds)471 void Sec6::make_from_double(double nr, ulong *nanoseconds)
472 {
473 if ((m_neg= nr < 0))
474 nr= -nr;
475 if ((m_truncated= nr > (double) LONGLONG_MAX))
476 {
477 m_sec= LONGLONG_MAX;
478 m_usec= 0;
479 *nanoseconds= 0;
480 }
481 else
482 {
483 m_sec= (ulonglong) nr;
484 m_usec= (ulong) ((nr - floor(nr)) * 1000000000);
485 *nanoseconds= m_usec % 1000;
486 m_usec/= 1000;
487 }
488 }
489
490
make_truncated_warning(THD * thd,const char * type_str) const491 void Sec6::make_truncated_warning(THD *thd, const char *type_str) const
492 {
493 char buff[1 + MAX_BIGINT_WIDTH + 1 + 6 + 1]; // '-' int '.' frac '\0'
494 to_string(buff, sizeof(buff));
495 thd->push_warning_truncated_wrong_value(type_str, buff);
496 }
497
498
convert_to_mysql_time(THD * thd,int * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const499 bool Sec6::convert_to_mysql_time(THD *thd, int *warn, MYSQL_TIME *ltime,
500 date_mode_t fuzzydate) const
501 {
502 bool rc= fuzzydate & (TIME_INTERVAL_hhmmssff | TIME_INTERVAL_DAY) ?
503 to_datetime_or_to_interval_hhmmssff(ltime, warn) :
504 fuzzydate & TIME_TIME_ONLY ?
505 to_datetime_or_time(ltime, warn, date_conv_mode_t(fuzzydate)) :
506 to_datetime_or_date(ltime, warn, date_conv_mode_t(fuzzydate));
507 DBUG_ASSERT(*warn || !rc);
508 if (truncated())
509 *warn|= MYSQL_TIME_WARN_TRUNCATED;
510 return rc;
511 }
512
513
push_conversion_warnings(THD * thd,bool totally_useless_value,int warn,const char * typestr,const char * db_name,const char * table_name,const char * field_name,const char * value)514 void Temporal::push_conversion_warnings(THD *thd, bool totally_useless_value,
515 int warn,
516 const char *typestr,
517 const char *db_name,
518 const char *table_name,
519 const char *field_name,
520 const char *value)
521 {
522 if (MYSQL_TIME_WARN_HAVE_WARNINGS(warn))
523 thd->push_warning_wrong_or_truncated_value(Sql_condition::WARN_LEVEL_WARN,
524 totally_useless_value,
525 typestr, value,
526 db_name, table_name,
527 field_name);
528 else if (MYSQL_TIME_WARN_HAVE_NOTES(warn))
529 thd->push_warning_wrong_or_truncated_value(Sql_condition::WARN_LEVEL_NOTE,
530 false, typestr, value,
531 db_name, table_name,
532 field_name);
533 }
534
535
VSec9(THD * thd,Item * item,const char * type_str,ulonglong limit)536 VSec9::VSec9(THD *thd, Item *item, const char *type_str, ulonglong limit)
537 {
538 if (item->decimals == 0)
539 { // optimize for an important special case
540 Longlong_hybrid nr(item->val_int(), item->unsigned_flag);
541 make_from_int(nr);
542 m_is_null= item->null_value;
543 if (!m_is_null && m_sec > limit)
544 {
545 m_sec= limit;
546 m_truncated= true;
547 ErrConvInteger err(nr);
548 thd->push_warning_truncated_wrong_value(type_str, err.ptr());
549 }
550 }
551 else if (item->cmp_type() == REAL_RESULT)
552 {
553 double nr= item->val_real();
554 make_from_double(nr, &m_nsec);
555 m_is_null= item->null_value;
556 if (!m_is_null && m_sec > limit)
557 {
558 m_sec= limit;
559 m_truncated= true;
560 }
561 if (m_truncated)
562 {
563 ErrConvDouble err(nr);
564 thd->push_warning_truncated_wrong_value(type_str, err.ptr());
565 }
566 }
567 else
568 {
569 VDec tmp(item);
570 (m_is_null= tmp.is_null()) ? reset() : make_from_decimal(tmp.ptr(), &m_nsec);
571 if (!m_is_null && m_sec > limit)
572 {
573 m_sec= limit;
574 m_truncated= true;
575 }
576 if (m_truncated)
577 {
578 ErrConvDecimal err(tmp.ptr());
579 thd->push_warning_truncated_wrong_value(type_str, err.ptr());
580 }
581 }
582 }
583
584
Year(longlong value,bool unsigned_flag,uint length)585 Year::Year(longlong value, bool unsigned_flag, uint length)
586 {
587 if ((m_truncated= (value < 0))) // Negative or huge unsigned
588 m_year= unsigned_flag ? 9999 : 0;
589 else if (value > 9999)
590 {
591 m_truncated= true;
592 m_year= 9999;
593 }
594 else if (length == 2)
595 {
596 m_year= value < 70 ? (uint) value + 2000 :
597 value <= 1900 ? (uint) value + 1900 :
598 (uint) value;
599 }
600 else
601 m_year= (uint) value;
602 DBUG_ASSERT(m_year <= 9999);
603 }
604
605
year_precision(const Item * item) const606 uint Year::year_precision(const Item *item) const
607 {
608 return item->type_handler() == &type_handler_year2 ? 2 : 4;
609 }
610
611
VYear(Item * item)612 VYear::VYear(Item *item)
613 :Year_null(item->to_longlong_null(), item->unsigned_flag, year_precision(item))
614 { }
615
616
VYear_op(Item_func_hybrid_field_type * item)617 VYear_op::VYear_op(Item_func_hybrid_field_type *item)
618 :Year_null(item->to_longlong_null_op(), item->unsigned_flag,
619 year_precision(item))
620 { }
621
622
623 const LEX_CSTRING Interval_DDhhmmssff::m_type_name=
624 {STRING_WITH_LEN("INTERVAL DAY TO SECOND")};
625
626
Interval_DDhhmmssff(THD * thd,Status * st,bool push_warnings,Item * item,ulong max_hour,time_round_mode_t mode,uint dec)627 Interval_DDhhmmssff::Interval_DDhhmmssff(THD *thd, Status *st,
628 bool push_warnings,
629 Item *item, ulong max_hour,
630 time_round_mode_t mode, uint dec)
631 {
632 switch (item->cmp_type()) {
633 case ROW_RESULT:
634 DBUG_ASSERT(0);
635 time_type= MYSQL_TIMESTAMP_NONE;
636 break;
637 case TIME_RESULT:
638 {
639 // Rounding mode is not important here
640 if (item->get_date(thd, this, Options(TIME_TIME_ONLY, TIME_FRAC_NONE)))
641 time_type= MYSQL_TIMESTAMP_NONE;
642 else if (time_type != MYSQL_TIMESTAMP_TIME)
643 {
644 st->warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
645 push_warning_wrong_or_truncated_value(thd, ErrConvTime(this),
646 st->warnings);
647 time_type= MYSQL_TIMESTAMP_NONE;
648 }
649 break;
650 }
651 case INT_RESULT:
652 case REAL_RESULT:
653 case DECIMAL_RESULT:
654 case STRING_RESULT:
655 {
656 StringBuffer<STRING_BUFFER_USUAL_SIZE> tmp;
657 String *str= item->val_str(&tmp);
658 if (!str)
659 time_type= MYSQL_TIMESTAMP_NONE;
660 else if (str_to_DDhhmmssff(st, str->ptr(), str->length(), str->charset(),
661 UINT_MAX32))
662 {
663 if (push_warnings)
664 thd->push_warning_wrong_value(Sql_condition::WARN_LEVEL_WARN,
665 m_type_name.str,
666 ErrConvString(str).ptr());
667 time_type= MYSQL_TIMESTAMP_NONE;
668 }
669 else
670 {
671 if (mode == TIME_FRAC_ROUND)
672 time_round_or_set_max(dec, &st->warnings, max_hour, st->nanoseconds);
673 if (hour > max_hour)
674 {
675 st->warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
676 time_type= MYSQL_TIMESTAMP_NONE;
677 }
678 // Warn if hour or nanosecond truncation happened
679 if (push_warnings)
680 push_warning_wrong_or_truncated_value(thd, ErrConvString(str),
681 st->warnings);
682 }
683 }
684 break;
685 }
686 DBUG_ASSERT(is_valid_value_slow());
687 }
688
689
690 void
push_warning_wrong_or_truncated_value(THD * thd,const ErrConv & str,int warnings)691 Interval_DDhhmmssff::push_warning_wrong_or_truncated_value(THD *thd,
692 const ErrConv &str,
693 int warnings)
694 {
695 if (warnings & MYSQL_TIME_WARN_OUT_OF_RANGE)
696 {
697 thd->push_warning_wrong_value(Sql_condition::WARN_LEVEL_WARN,
698 m_type_name.str, str.ptr());
699 }
700 else if (MYSQL_TIME_WARN_HAVE_WARNINGS(warnings))
701 {
702 thd->push_warning_truncated_wrong_value(Sql_condition::WARN_LEVEL_WARN,
703 m_type_name.str, str.ptr());
704 }
705 else if (MYSQL_TIME_WARN_HAVE_NOTES(warnings))
706 {
707 thd->push_warning_truncated_wrong_value(Sql_condition::WARN_LEVEL_NOTE,
708 m_type_name.str, str.ptr());
709 }
710 }
711
712
fsp(THD * thd,Item * item)713 uint Interval_DDhhmmssff::fsp(THD *thd, Item *item)
714 {
715 switch (item->cmp_type()) {
716 case INT_RESULT:
717 case TIME_RESULT:
718 return item->decimals;
719 case REAL_RESULT:
720 case DECIMAL_RESULT:
721 return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
722 case ROW_RESULT:
723 DBUG_ASSERT(0);
724 return 0;
725 case STRING_RESULT:
726 break;
727 }
728 if (!item->const_item() || item->is_expensive())
729 return TIME_SECOND_PART_DIGITS;
730 Status st;
731 Interval_DDhhmmssff it(thd, &st, false/*no warnings*/, item, UINT_MAX32,
732 TIME_FRAC_TRUNCATE, TIME_SECOND_PART_DIGITS);
733 return it.is_valid_interval_DDhhmmssff() ? st.precision :
734 TIME_SECOND_PART_DIGITS;
735 }
736
737
to_native(Native * to,uint decimals) const738 bool Time::to_native(Native *to, uint decimals) const
739 {
740 if (!is_valid_time())
741 {
742 to->length(0);
743 return true;
744 }
745 uint len= my_time_binary_length(decimals);
746 if (to->reserve(len))
747 return true;
748 longlong tmp= TIME_to_longlong_time_packed(get_mysql_time());
749 my_time_packed_to_binary(tmp, (uchar*) to->ptr(), decimals);
750 to->length(len);
751 return false;
752 }
753
754
make_from_item(THD * thd,int * warn,Item * item,const Options opt)755 void Time::make_from_item(THD *thd, int *warn, Item *item, const Options opt)
756 {
757 *warn= 0;
758 if (item->get_date(thd, this, opt))
759 time_type= MYSQL_TIMESTAMP_NONE;
760 else
761 valid_MYSQL_TIME_to_valid_value(thd, warn, opt);
762 }
763
764
765 static uint msec_round_add[7]=
766 {
767 500000000,
768 50000000,
769 5000000,
770 500000,
771 50000,
772 5000,
773 0
774 };
775
776
round(uint dec)777 Sec9 & Sec9::round(uint dec)
778 {
779 DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
780 if (Sec6::add_nanoseconds(m_nsec + msec_round_add[dec]))
781 m_sec++;
782 m_nsec= 0;
783 Sec6::trunc(dec);
784 return *this;
785 }
786
787
round_or_set_max(uint dec,int * warn)788 void Timestamp::round_or_set_max(uint dec, int *warn)
789 {
790 DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
791 if (add_nanoseconds_usec(msec_round_add[dec]) &&
792 tv_sec++ >= TIMESTAMP_MAX_VALUE)
793 {
794 tv_sec= TIMESTAMP_MAX_VALUE;
795 tv_usec= TIME_MAX_SECOND_PART;
796 *warn|= MYSQL_TIME_WARN_OUT_OF_RANGE;
797 }
798 my_timeval_trunc(this, dec);
799 }
800
801
add_nanoseconds_with_round(THD * thd,int * warn,date_conv_mode_t mode,ulong nsec)802 bool Temporal::add_nanoseconds_with_round(THD *thd, int *warn,
803 date_conv_mode_t mode,
804 ulong nsec)
805 {
806 switch (time_type) {
807 case MYSQL_TIMESTAMP_TIME:
808 {
809 ulong max_hour= (mode & (TIME_INTERVAL_DAY | TIME_INTERVAL_hhmmssff)) ?
810 TIME_MAX_INTERVAL_HOUR : TIME_MAX_HOUR;
811 time_round_or_set_max(6, warn, max_hour, nsec);
812 return false;
813 }
814 case MYSQL_TIMESTAMP_DATETIME:
815 return datetime_round_or_invalidate(thd, 6, warn, nsec);
816 case MYSQL_TIMESTAMP_DATE:
817 return false;
818 case MYSQL_TIMESTAMP_NONE:
819 return false;
820 case MYSQL_TIMESTAMP_ERROR:
821 break;
822 }
823 DBUG_ASSERT(0);
824 return false;
825 }
826
827
time_round_or_set_max(uint dec,int * warn,ulong max_hour,ulong nsec)828 void Temporal::time_round_or_set_max(uint dec, int *warn,
829 ulong max_hour, ulong nsec)
830 {
831 DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
832 if (add_nanoseconds_mmssff(nsec) && ++hour > max_hour)
833 {
834 time_hhmmssff_set_max(max_hour);
835 *warn|= MYSQL_TIME_WARN_OUT_OF_RANGE;
836 }
837 my_time_trunc(this, dec);
838 }
839
840
round_or_set_max(uint dec,int * warn,ulong nsec)841 void Time::round_or_set_max(uint dec, int *warn, ulong nsec)
842 {
843 Temporal::time_round_or_set_max(dec, warn, TIME_MAX_HOUR, nsec);
844 DBUG_ASSERT(is_valid_time_slow());
845 }
846
847
round_or_set_max(uint dec,int * warn)848 void Time::round_or_set_max(uint dec, int *warn)
849 {
850 round_or_set_max(dec, warn, msec_round_add[dec]);
851 }
852
853 /**
854 Create from a DATETIME by subtracting a given number of days,
855 implementing an optimized version of calc_time_diff().
856 */
make_from_datetime_with_days_diff(int * warn,const MYSQL_TIME * from,long days)857 void Time::make_from_datetime_with_days_diff(int *warn, const MYSQL_TIME *from,
858 long days)
859 {
860 *warn= 0;
861 DBUG_ASSERT(from->time_type == MYSQL_TIMESTAMP_DATETIME ||
862 from->time_type == MYSQL_TIMESTAMP_DATE);
863 long daynr= calc_daynr(from->year, from->month, from->day);
864 long daydiff= daynr - days;
865 if (!daynr) // Zero date
866 {
867 set_zero_time(this, MYSQL_TIMESTAMP_TIME);
868 neg= true;
869 hour= TIME_MAX_HOUR + 1; // to report "out of range" in "warn"
870 }
871 else if (daydiff >=0)
872 {
873 neg= false;
874 year= month= day= 0;
875 hhmmssff_copy(from);
876 hour+= daydiff * 24;
877 time_type= MYSQL_TIMESTAMP_TIME;
878 }
879 else
880 {
881 longlong timediff= ((((daydiff * 24LL +
882 from->hour) * 60LL +
883 from->minute) * 60LL +
884 from->second) * 1000000LL +
885 from->second_part);
886 unpack_time(timediff, this, MYSQL_TIMESTAMP_TIME);
887 if (year || month)
888 {
889 *warn|= MYSQL_TIME_WARN_OUT_OF_RANGE;
890 year= month= day= 0;
891 hour= TIME_MAX_HOUR + 1;
892 }
893 }
894 // The above code can generate TIME values outside of the valid TIME range.
895 adjust_time_range_or_invalidate(warn);
896 }
897
898
make_from_datetime_move_day_to_hour(int * warn,const MYSQL_TIME * from)899 void Time::make_from_datetime_move_day_to_hour(int *warn,
900 const MYSQL_TIME *from)
901 {
902 *warn= 0;
903 DBUG_ASSERT(from->time_type == MYSQL_TIMESTAMP_DATE ||
904 from->time_type == MYSQL_TIMESTAMP_DATETIME);
905 time_type= MYSQL_TIMESTAMP_TIME;
906 neg= false;
907 year= month= day= 0;
908 hhmmssff_copy(from);
909 datetime_to_time_YYYYMMDD_000000DD_mix_to_hours(warn, from->year,
910 from->month, from->day);
911 adjust_time_range_or_invalidate(warn);
912 }
913
914
make_from_datetime(int * warn,const MYSQL_TIME * from,long curdays)915 void Time::make_from_datetime(int *warn, const MYSQL_TIME *from, long curdays)
916 {
917 if (!curdays)
918 make_from_datetime_move_day_to_hour(warn, from);
919 else
920 make_from_datetime_with_days_diff(warn, from, curdays);
921 }
922
923
make_from_time(int * warn,const MYSQL_TIME * from)924 void Time::make_from_time(int *warn, const MYSQL_TIME *from)
925 {
926 DBUG_ASSERT(from->time_type == MYSQL_TIMESTAMP_TIME);
927 if (from->year || from->month)
928 make_from_out_of_range(warn);
929 else
930 {
931 *warn= 0;
932 DBUG_ASSERT(from->day == 0);
933 *(static_cast<MYSQL_TIME*>(this))= *from;
934 adjust_time_range_or_invalidate(warn);
935 }
936 }
937
938
binary_length_to_precision(uint length)939 uint Time::binary_length_to_precision(uint length)
940 {
941 switch (length) {
942 case 3: return 0;
943 case 4: return 2;
944 case 5: return 4;
945 case 6: return 6;
946 }
947 DBUG_ASSERT(0);
948 return 0;
949 }
950
951
Time(const Native & native)952 Time::Time(const Native &native)
953 {
954 uint dec= binary_length_to_precision(native.length());
955 longlong tmp= my_time_packed_from_binary((const uchar *) native.ptr(), dec);
956 TIME_from_longlong_time_packed(this, tmp);
957 DBUG_ASSERT(is_valid_time());
958 }
959
960
Time(int * warn,const MYSQL_TIME * from,long curdays)961 Time::Time(int *warn, const MYSQL_TIME *from, long curdays)
962 {
963 switch (from->time_type) {
964 case MYSQL_TIMESTAMP_NONE:
965 case MYSQL_TIMESTAMP_ERROR:
966 make_from_out_of_range(warn);
967 break;
968 case MYSQL_TIMESTAMP_DATE:
969 case MYSQL_TIMESTAMP_DATETIME:
970 make_from_datetime(warn, from, curdays);
971 break;
972 case MYSQL_TIMESTAMP_TIME:
973 make_from_time(warn, from);
974 break;
975 }
976 DBUG_ASSERT(is_valid_value_slow());
977 }
978
979
Time(int * warn,bool neg,ulonglong hour,uint minute,const Sec6 & second)980 Time::Time(int *warn, bool neg, ulonglong hour, uint minute, const Sec6 &second)
981 {
982 DBUG_ASSERT(second.sec() <= 59);
983 *warn= 0;
984 set_zero_time(this, MYSQL_TIMESTAMP_TIME);
985 MYSQL_TIME::neg= neg;
986 MYSQL_TIME::hour= hour > TIME_MAX_HOUR ? (uint) (TIME_MAX_HOUR + 1) :
987 (uint) hour;
988 MYSQL_TIME::minute= minute;
989 MYSQL_TIME::second= (uint) second.sec();
990 MYSQL_TIME::second_part= second.usec();
991 adjust_time_range_or_invalidate(warn);
992 }
993
994
make_from_item(THD * thd,Item * item,date_mode_t fuzzydate)995 void Temporal_with_date::make_from_item(THD *thd, Item *item,
996 date_mode_t fuzzydate)
997 {
998 date_conv_mode_t flags= date_conv_mode_t(fuzzydate) & ~TIME_TIME_ONLY;
999 /*
1000 Some TIME type items return error when trying to do get_date()
1001 without TIME_TIME_ONLY set (e.g. Item_field for Field_time).
1002 In the SQL standard time->datetime conversion mode we add TIME_TIME_ONLY.
1003 In the legacy time->datetime conversion mode we do not add TIME_TIME_ONLY
1004 and leave it to get_date() to check date.
1005 */
1006 date_conv_mode_t time_flag= (item->field_type() == MYSQL_TYPE_TIME &&
1007 !(thd->variables.old_behavior & OLD_MODE_ZERO_DATE_TIME_CAST)) ?
1008 TIME_TIME_ONLY : TIME_CONV_NONE;
1009 Options opt(flags | time_flag, time_round_mode_t(fuzzydate));
1010 if (item->get_date(thd, this, opt))
1011 time_type= MYSQL_TIMESTAMP_NONE;
1012 else if (time_type == MYSQL_TIMESTAMP_TIME)
1013 {
1014 MYSQL_TIME tmp;
1015 if (time_to_datetime_with_warn(thd, this, &tmp, flags))
1016 time_type= MYSQL_TIMESTAMP_NONE;
1017 else
1018 *(static_cast<MYSQL_TIME*>(this))= tmp;
1019 }
1020 }
1021
1022
check_date_or_invalidate(int * warn,date_conv_mode_t flags)1023 void Temporal_with_date::check_date_or_invalidate(int *warn,
1024 date_conv_mode_t flags)
1025 {
1026 if (::check_date(this, pack_time(this) != 0,
1027 ulonglong(flags & TIME_MODE_FOR_XXX_TO_DATE), warn))
1028 time_type= MYSQL_TIMESTAMP_NONE;
1029 }
1030
1031
make_from_time(THD * thd,int * warn,const MYSQL_TIME * from,date_conv_mode_t flags)1032 void Datetime::make_from_time(THD *thd, int *warn, const MYSQL_TIME *from,
1033 date_conv_mode_t flags)
1034 {
1035 DBUG_ASSERT(from->time_type == MYSQL_TIMESTAMP_TIME);
1036 if (time_to_datetime(thd, from, this))
1037 make_from_out_of_range(warn);
1038 else
1039 {
1040 *warn= 0;
1041 check_date_or_invalidate(warn, flags);
1042 }
1043 }
1044
1045
make_from_datetime(THD * thd,int * warn,const MYSQL_TIME * from,date_conv_mode_t flags)1046 void Datetime::make_from_datetime(THD *thd, int *warn, const MYSQL_TIME *from,
1047 date_conv_mode_t flags)
1048 {
1049 DBUG_ASSERT(from->time_type == MYSQL_TIMESTAMP_DATE ||
1050 from->time_type == MYSQL_TIMESTAMP_DATETIME);
1051 if (from->neg || check_datetime_range(from))
1052 make_from_out_of_range(warn);
1053 else
1054 {
1055 *warn= 0;
1056 *(static_cast<MYSQL_TIME*>(this))= *from;
1057 date_to_datetime(this);
1058 check_date_or_invalidate(warn, flags);
1059 }
1060 }
1061
1062
Datetime(THD * thd,const timeval & tv)1063 Datetime::Datetime(THD *thd, const timeval &tv)
1064 {
1065 thd->variables.time_zone->gmt_sec_to_TIME(this, tv.tv_sec);
1066 second_part= tv.tv_usec;
1067 thd->time_zone_used= 1;
1068 DBUG_ASSERT(is_valid_value_slow());
1069 }
1070
1071
Datetime(THD * thd,int * warn,const MYSQL_TIME * from,date_conv_mode_t flags)1072 Datetime::Datetime(THD *thd, int *warn, const MYSQL_TIME *from,
1073 date_conv_mode_t flags)
1074 {
1075 DBUG_ASSERT(bool(flags & TIME_TIME_ONLY) == false);
1076 switch (from->time_type) {
1077 case MYSQL_TIMESTAMP_ERROR:
1078 case MYSQL_TIMESTAMP_NONE:
1079 make_from_out_of_range(warn);
1080 break;
1081 case MYSQL_TIMESTAMP_TIME:
1082 make_from_time(thd, warn, from, flags);
1083 break;
1084 case MYSQL_TIMESTAMP_DATETIME:
1085 case MYSQL_TIMESTAMP_DATE:
1086 make_from_datetime(thd, warn, from, flags);
1087 break;
1088 }
1089 DBUG_ASSERT(is_valid_value_slow());
1090 }
1091
Datetime(my_time_t unix_time,ulong second_part_arg,const Time_zone * time_zone)1092 Datetime::Datetime(my_time_t unix_time, ulong second_part_arg,
1093 const Time_zone* time_zone)
1094 {
1095 time_zone->gmt_sec_to_TIME(this, unix_time);
1096 second_part= second_part_arg;
1097 }
1098
1099
datetime_add_nanoseconds_or_invalidate(THD * thd,int * warn,ulong nsec)1100 bool Temporal::datetime_add_nanoseconds_or_invalidate(THD *thd, int *warn, ulong nsec)
1101 {
1102 if (!add_nanoseconds_mmssff(nsec))
1103 return false;
1104 /*
1105 Overflow happened on minutes. Now we need to add 1 hour to the value.
1106 Catch a special case for the maximum possible date and hour==23, to
1107 truncate '9999-12-31 23:59:59.9999999' (with 7 fractional digits)
1108 to '9999-12-31 23:59:59.999999' (with 6 fractional digits),
1109 with a warning, instead of returning an error, so this statement:
1110 INSERT INTO (datetime_column) VALUES ('9999-12-31 23:59:59.9999999');
1111 inserts a value truncated to 6 fractional digits, instead of zero
1112 date '0000-00-00 00:00:00.000000'.
1113 */
1114 if (year == 9999 && month == 12 && day == 31 && hour == 23)
1115 {
1116 minute= 59;
1117 second= 59;
1118 second_part= 999999;
1119 *warn= MYSQL_TIME_WARN_OUT_OF_RANGE;
1120 return false;
1121 }
1122 INTERVAL interval;
1123 memset(&interval, 0, sizeof(interval));
1124 interval.hour= 1;
1125 /*
1126 date_add_interval cannot handle bad dates with zero YYYY or MM.
1127 Note, check_date(NO_ZERO_XX) does not check YYYY against zero,
1128 so let's additionally check it.
1129 */
1130 if (year == 0 ||
1131 check_date(TIME_NO_ZERO_IN_DATE | TIME_NO_ZERO_DATE, warn) ||
1132 date_add_interval(thd, this, INTERVAL_HOUR, interval, false/*no warn*/))
1133 {
1134 char buf[MAX_DATE_STRING_REP_LENGTH];
1135 my_date_to_str(this, buf);
1136 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1137 ER_WRONG_VALUE_FOR_TYPE,
1138 ER_THD(thd, ER_WRONG_VALUE_FOR_TYPE),
1139 "date", buf, "round(datetime)");
1140 make_from_out_of_range(warn);
1141 return true;
1142 }
1143 return false;
1144 }
1145
1146
datetime_round_or_invalidate(THD * thd,uint dec,int * warn,ulong nsec)1147 bool Temporal::datetime_round_or_invalidate(THD *thd, uint dec, int *warn, ulong nsec)
1148 {
1149 DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
1150 if (datetime_add_nanoseconds_or_invalidate(thd, warn, nsec))
1151 return true;
1152 my_datetime_trunc(this, dec);
1153 return false;
1154
1155 }
1156
1157
round_or_invalidate(THD * thd,uint dec,int * warn)1158 bool Datetime::round_or_invalidate(THD *thd, uint dec, int *warn)
1159 {
1160 return round_or_invalidate(thd, dec, warn, msec_round_add[dec]);
1161 }
1162
1163
Datetime_from_temporal(THD * thd,Item * temporal,date_conv_mode_t fuzzydate)1164 Datetime_from_temporal::Datetime_from_temporal(THD *thd, Item *temporal,
1165 date_conv_mode_t fuzzydate)
1166 :Datetime(thd, temporal, Options(fuzzydate, TIME_FRAC_NONE))
1167 {
1168 // Exact rounding mode does not matter
1169 DBUG_ASSERT(temporal->cmp_type() == TIME_RESULT);
1170 }
1171
1172
Datetime_truncation_not_needed(THD * thd,Item * item,date_conv_mode_t mode)1173 Datetime_truncation_not_needed::Datetime_truncation_not_needed(THD *thd, Item *item,
1174 date_conv_mode_t mode)
1175 :Datetime(thd, item, Options(mode, TIME_FRAC_NONE))
1176 {
1177 /*
1178 The called Datetime() constructor only would truncate nanoseconds if they
1179 existed (but we know there were no nanoseconds). Here we assert that there
1180 are also no microsecond digits outside of the scale specified in "dec".
1181 */
1182 DBUG_ASSERT(!is_valid_datetime() ||
1183 fraction_remainder(MY_MIN(item->decimals,
1184 TIME_SECOND_PART_DIGITS)) == 0);
1185 }
1186
1187 /********************************************************************/
1188
find_max_decimals(Item ** item,uint nitems)1189 uint Type_numeric_attributes::find_max_decimals(Item **item, uint nitems)
1190 {
1191 uint res= 0;
1192 for (uint i= 0; i < nitems; i++)
1193 set_if_bigger(res, item[i]->decimals);
1194 return res;
1195 }
1196
1197
count_unsigned(Item ** item,uint nitems)1198 uint Type_numeric_attributes::count_unsigned(Item **item, uint nitems)
1199 {
1200 uint res= 0;
1201 for (uint i= 0 ; i < nitems ; i++)
1202 {
1203 if (item[i]->unsigned_flag)
1204 res++;
1205 }
1206 return res;
1207 }
1208
1209
find_max_char_length(Item ** item,uint nitems)1210 uint32 Type_numeric_attributes::find_max_char_length(Item **item, uint nitems)
1211 {
1212 uint32 char_length= 0;
1213 for (uint i= 0; i < nitems ; i++)
1214 set_if_bigger(char_length, item[i]->max_char_length());
1215 return char_length;
1216 }
1217
1218
find_max_octet_length(Item ** item,uint nitems)1219 uint32 Type_numeric_attributes::find_max_octet_length(Item **item, uint nitems)
1220 {
1221 uint32 octet_length= 0;
1222 for (uint i= 0; i < nitems ; i++)
1223 set_if_bigger(octet_length, item[i]->max_length);
1224 return octet_length;
1225 }
1226
1227
find_max_decimal_int_part(Item ** item,uint nitems)1228 int Type_numeric_attributes::find_max_decimal_int_part(Item **item, uint nitems)
1229 {
1230 int max_int_part= 0;
1231 for (uint i=0 ; i < nitems ; i++)
1232 set_if_bigger(max_int_part, item[i]->decimal_int_part());
1233 return max_int_part;
1234 }
1235
1236
1237 /**
1238 Set max_length/decimals of function if function is fixed point and
1239 result length/precision depends on argument ones.
1240 */
1241
1242 void
aggregate_numeric_attributes_decimal(Item ** item,uint nitems,bool unsigned_arg)1243 Type_numeric_attributes::aggregate_numeric_attributes_decimal(Item **item,
1244 uint nitems,
1245 bool unsigned_arg)
1246 {
1247 int max_int_part= find_max_decimal_int_part(item, nitems);
1248 decimals= find_max_decimals(item, nitems);
1249 int precision= MY_MIN(max_int_part + decimals, DECIMAL_MAX_PRECISION);
1250 max_length= my_decimal_precision_to_length_no_truncation(precision,
1251 (uint8) decimals,
1252 unsigned_flag);
1253 }
1254
1255
1256 /**
1257 Set max_length/decimals of function if function is floating point and
1258 result length/precision depends on argument ones.
1259 */
1260
1261 void
aggregate_numeric_attributes_real(Item ** items,uint nitems)1262 Type_numeric_attributes::aggregate_numeric_attributes_real(Item **items,
1263 uint nitems)
1264 {
1265 uint32 length= 0;
1266 decimals= 0;
1267 max_length= 0;
1268 unsigned_flag= false;
1269 for (uint i=0 ; i < nitems ; i++)
1270 {
1271 if (decimals < FLOATING_POINT_DECIMALS)
1272 {
1273 set_if_bigger(decimals, items[i]->decimals);
1274 /* Will be ignored if items[i]->decimals >= FLOATING_POINT_DECIMALS */
1275 set_if_bigger(length, (items[i]->max_length - items[i]->decimals));
1276 }
1277 set_if_bigger(max_length, items[i]->max_length);
1278 }
1279 if (decimals < FLOATING_POINT_DECIMALS)
1280 {
1281 max_length= length;
1282 length+= decimals;
1283 if (length < max_length) // If previous operation gave overflow
1284 max_length= UINT_MAX32;
1285 else
1286 max_length= length;
1287 }
1288 // Corner case: COALESCE(DOUBLE(255,4), DOUBLE(255,3)) -> FLOAT(255, 4)
1289 set_if_smaller(max_length, MAX_FIELD_CHARLENGTH);
1290 }
1291
1292
1293 /**
1294 Calculate max_length and decimals for string functions.
1295
1296 @param field_type Field type.
1297 @param items Argument array.
1298 @param nitems Number of arguments.
1299
1300 @retval False on success, true on error.
1301 */
aggregate_attributes_string(const char * func_name,Item ** items,uint nitems)1302 bool Type_std_attributes::aggregate_attributes_string(const char *func_name,
1303 Item **items, uint nitems)
1304 {
1305 if (agg_arg_charsets_for_string_result(collation, func_name,
1306 items, nitems, 1))
1307 return true;
1308 if (collation.collation == &my_charset_bin)
1309 max_length= find_max_octet_length(items, nitems);
1310 else
1311 fix_char_length(find_max_char_length(items, nitems));
1312 unsigned_flag= false;
1313 decimals= max_length ? NOT_FIXED_DEC : 0;
1314 return false;
1315 }
1316
1317
1318 /*
1319 Find a handler by its ODBC literal data type.
1320
1321 @param type_str - data type name, not necessarily 0-terminated
1322 @retval - a pointer to data type handler if type_str points
1323 to a known ODBC literal data type, or NULL otherwise
1324 */
1325 const Type_handler *
odbc_literal_type_handler(const LEX_CSTRING * type_str)1326 Type_handler::odbc_literal_type_handler(const LEX_CSTRING *type_str)
1327 {
1328 if (type_str->length == 1)
1329 {
1330 if (type_str->str[0] == 'd') // {d'2001-01-01'}
1331 return &type_handler_newdate;
1332 else if (type_str->str[0] == 't') // {t'10:20:30'}
1333 return &type_handler_time2;
1334 }
1335 else if (type_str->length == 2) // {ts'2001-01-01 10:20:30'}
1336 {
1337 if (type_str->str[0] == 't' && type_str->str[1] == 's')
1338 return &type_handler_datetime2;
1339 }
1340 return NULL; // Not a known ODBC literal type
1341 }
1342
1343
1344 /**
1345 This method is used by:
1346 - Item_user_var_as_out_param::field_type()
1347 - Item_func_udf_str::field_type()
1348 - Item_empty_string::make_send_field()
1349
1350 TODO: type_handler_adjusted_to_max_octet_length() and string_type_handler()
1351 provide very similar functionality, to properly choose between
1352 VARCHAR/VARBINARY vs TEXT/BLOB variations taking into accoung maximum
1353 possible octet length.
1354
1355 We should probably get rid of either of them and use the same method
1356 all around the code.
1357 */
1358 const Type_handler *
string_type_handler(uint max_octet_length)1359 Type_handler::string_type_handler(uint max_octet_length)
1360 {
1361 if (max_octet_length >= 16777216)
1362 return &type_handler_long_blob;
1363 else if (max_octet_length >= 65536)
1364 return &type_handler_medium_blob;
1365 else if (max_octet_length >= MAX_FIELD_VARCHARLENGTH)
1366 return &type_handler_blob;
1367 return &type_handler_varchar;
1368 }
1369
1370
1371 const Type_handler *
varstring_type_handler(const Item * item)1372 Type_handler::varstring_type_handler(const Item *item)
1373 {
1374 if (!item->max_length)
1375 return &type_handler_string;
1376 if (item->too_big_for_varchar())
1377 return blob_type_handler(item->max_length);
1378 return &type_handler_varchar;
1379 }
1380
1381
1382 const Type_handler *
blob_type_handler(uint max_octet_length)1383 Type_handler::blob_type_handler(uint max_octet_length)
1384 {
1385 if (max_octet_length <= 255)
1386 return &type_handler_tiny_blob;
1387 if (max_octet_length <= 65535)
1388 return &type_handler_blob;
1389 if (max_octet_length <= 16777215)
1390 return &type_handler_medium_blob;
1391 return &type_handler_long_blob;
1392 }
1393
1394
1395 const Type_handler *
blob_type_handler(const Item * item)1396 Type_handler::blob_type_handler(const Item *item)
1397 {
1398 return blob_type_handler(item->max_length);
1399 }
1400
1401 /**
1402 This method is used by:
1403 - Item_sum_hybrid, e.g. MAX(item), MIN(item).
1404 - Item_func_set_user_var
1405 */
1406 const Type_handler *
type_handler_adjusted_to_max_octet_length(uint max_octet_length,CHARSET_INFO * cs) const1407 Type_handler_string_result::type_handler_adjusted_to_max_octet_length(
1408 uint max_octet_length,
1409 CHARSET_INFO *cs) const
1410 {
1411 if (max_octet_length / cs->mbmaxlen <= CONVERT_IF_BIGGER_TO_BLOB)
1412 return &type_handler_varchar; // See also Item::too_big_for_varchar()
1413 if (max_octet_length >= 16777216)
1414 return &type_handler_long_blob;
1415 else if (max_octet_length >= 65536)
1416 return &type_handler_medium_blob;
1417 return &type_handler_blob;
1418 }
1419
1420
charset_for_protocol(const Item * item) const1421 CHARSET_INFO *Type_handler::charset_for_protocol(const Item *item) const
1422 {
1423 /*
1424 For backward compatibility, to make numeric
1425 data types return "binary" charset in client-side metadata.
1426 */
1427 return &my_charset_bin;
1428 }
1429
1430
1431 bool
Item_func_or_sum_illegal_param(const char * funcname) const1432 Type_handler::Item_func_or_sum_illegal_param(const char *funcname) const
1433 {
1434 my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
1435 name().ptr(), funcname);
1436 return true;
1437 }
1438
1439
1440 bool
Item_func_or_sum_illegal_param(const Item_func_or_sum * it) const1441 Type_handler::Item_func_or_sum_illegal_param(const Item_func_or_sum *it) const
1442 {
1443 return Item_func_or_sum_illegal_param(it->func_name());
1444 }
1445
1446
1447 CHARSET_INFO *
charset_for_protocol(const Item * item) const1448 Type_handler_string_result::charset_for_protocol(const Item *item) const
1449 {
1450 return item->collation.collation;
1451 }
1452
1453
1454 const Type_handler *
get_handler_by_cmp_type(Item_result type)1455 Type_handler::get_handler_by_cmp_type(Item_result type)
1456 {
1457 switch (type) {
1458 case REAL_RESULT: return &type_handler_double;
1459 case INT_RESULT: return &type_handler_slonglong;
1460 case DECIMAL_RESULT: return &type_handler_newdecimal;
1461 case STRING_RESULT: return &type_handler_long_blob;
1462 case TIME_RESULT: return &type_handler_datetime;
1463 case ROW_RESULT: return &type_handler_row;
1464 }
1465 DBUG_ASSERT(0);
1466 return &type_handler_string;
1467 }
1468
1469
1470 /*
1471 If we have a mixture of:
1472 - a MariaDB standard (built-in permanent) data type, and
1473 - a non-standard (optionally compiled or pluggable) data type,
1474 then we ask the type collection of the non-standard type to aggregate
1475 the mixture.
1476 The standard type collection type_collection_std knows nothing
1477 about non-standard types, while non-standard type collections
1478 know everything about standard data types.
1479 */
1480 const Type_collection *
type_collection_for_aggregation(const Type_handler * h0,const Type_handler * h1)1481 Type_handler::type_collection_for_aggregation(const Type_handler *h0,
1482 const Type_handler *h1)
1483 {
1484 const Type_collection *c0= h0->type_collection();
1485 const Type_collection *c1= h1->type_collection();
1486 if (c0 == c1)
1487 return c0;
1488 if (c0 == &type_collection_std)
1489 return c1;
1490 if (c1 == &type_collection_std)
1491 return c0;
1492 /*
1493 A mixture of two non-standard collections.
1494 The caller code will continue to aggregate through
1495 the type aggregators in Type_handler_data.
1496 */
1497 return NULL;
1498 }
1499
1500
Type_handler_hybrid_field_type()1501 Type_handler_hybrid_field_type::Type_handler_hybrid_field_type()
1502 :m_type_handler(&type_handler_double)
1503 {
1504 }
1505
1506
1507 /***************************************************************************/
1508
1509 /* number of bytes to store second_part part of the TIMESTAMP(N) */
1510 uint Type_handler_timestamp::m_sec_part_bytes[MAX_DATETIME_PRECISION + 1]=
1511 { 0, 1, 1, 2, 2, 3, 3 };
1512
1513 /* number of bytes to store DATETIME(N) */
1514 uint Type_handler_datetime::m_hires_bytes[MAX_DATETIME_PRECISION + 1]=
1515 { 5, 6, 6, 7, 7, 7, 8 };
1516
1517 /* number of bytes to store TIME(N) */
1518 uint Type_handler_time::m_hires_bytes[MAX_DATETIME_PRECISION + 1]=
1519 { 3, 4, 4, 5, 5, 5, 6 };
1520
1521 /***************************************************************************/
1522
version() const1523 const Name Type_handler::version() const
1524 {
1525 static const Name ver(STRING_WITH_LEN(""));
1526 return ver;
1527 }
1528
version_mariadb53()1529 const Name & Type_handler::version_mariadb53()
1530 {
1531 static const Name ver(STRING_WITH_LEN("mariadb-5.3"));
1532 return ver;
1533 }
1534
version_mysql56()1535 const Name & Type_handler::version_mysql56()
1536 {
1537 static const Name ver(STRING_WITH_LEN("mysql-5.6"));
1538 return ver;
1539 }
1540
1541
1542 /***************************************************************************/
1543
type_limits_int() const1544 const Type_limits_int *Type_handler_tiny::type_limits_int() const
1545 {
1546 static const Type_limits_sint8 limits_sint8;
1547 return &limits_sint8;
1548 }
1549
type_limits_int() const1550 const Type_limits_int *Type_handler_utiny::type_limits_int() const
1551 {
1552 static const Type_limits_uint8 limits_uint8;
1553 return &limits_uint8;
1554 }
1555
type_limits_int() const1556 const Type_limits_int *Type_handler_short::type_limits_int() const
1557 {
1558 static const Type_limits_sint16 limits_sint16;
1559 return &limits_sint16;
1560 }
1561
type_limits_int() const1562 const Type_limits_int *Type_handler_ushort::type_limits_int() const
1563 {
1564 static const Type_limits_uint16 limits_uint16;
1565 return &limits_uint16;
1566 }
1567
type_limits_int() const1568 const Type_limits_int *Type_handler_int24::type_limits_int() const
1569 {
1570 static const Type_limits_sint24 limits_sint24;
1571 return &limits_sint24;
1572 }
1573
type_limits_int() const1574 const Type_limits_int *Type_handler_uint24::type_limits_int() const
1575 {
1576 static const Type_limits_uint24 limits_uint24;
1577 return &limits_uint24;
1578 }
1579
type_limits_int() const1580 const Type_limits_int *Type_handler_long::type_limits_int() const
1581 {
1582 static const Type_limits_sint32 limits_sint32;
1583 return &limits_sint32;
1584 }
1585
type_limits_int() const1586 const Type_limits_int *Type_handler_ulong::type_limits_int() const
1587 {
1588 static const Type_limits_uint32 limits_uint32;
1589 return &limits_uint32;
1590 }
1591
type_limits_int() const1592 const Type_limits_int *Type_handler_longlong::type_limits_int() const
1593 {
1594 static const Type_limits_sint64 limits_sint64;
1595 return &limits_sint64;
1596 }
1597
type_limits_int() const1598 const Type_limits_int *Type_handler_ulonglong::type_limits_int() const
1599 {
1600 static const Type_limits_uint64 limits_uint64;
1601 return &limits_uint64;
1602 }
1603
1604
1605 /***************************************************************************/
type_handler_signed() const1606 const Type_handler *Type_handler_bool::type_handler_signed() const
1607 {
1608 return &type_handler_bool;
1609 }
1610
type_handler_unsigned() const1611 const Type_handler *Type_handler_bool::type_handler_unsigned() const
1612 {
1613 return &type_handler_bool;
1614 }
1615
type_handler_signed() const1616 const Type_handler *Type_handler_tiny::type_handler_signed() const
1617 {
1618 return &type_handler_stiny;
1619 }
1620
type_handler_unsigned() const1621 const Type_handler *Type_handler_tiny::type_handler_unsigned() const
1622 {
1623 return &type_handler_utiny;
1624 }
1625
type_handler_signed() const1626 const Type_handler *Type_handler_short::type_handler_signed() const
1627 {
1628 return &type_handler_sshort;
1629 }
1630
type_handler_unsigned() const1631 const Type_handler *Type_handler_short::type_handler_unsigned() const
1632 {
1633 return &type_handler_ushort;
1634 }
1635
type_handler_signed() const1636 const Type_handler *Type_handler_int24::type_handler_signed() const
1637 {
1638 return &type_handler_sint24;
1639 }
1640
type_handler_unsigned() const1641 const Type_handler *Type_handler_int24::type_handler_unsigned() const
1642 {
1643 return &type_handler_uint24;
1644 }
1645
type_handler_signed() const1646 const Type_handler *Type_handler_long::type_handler_signed() const
1647 {
1648 return &type_handler_slong;
1649 }
1650
type_handler_unsigned() const1651 const Type_handler *Type_handler_long::type_handler_unsigned() const
1652 {
1653 return &type_handler_ulong;
1654 }
1655
type_handler_signed() const1656 const Type_handler *Type_handler_longlong::type_handler_signed() const
1657 {
1658 return &type_handler_slonglong;
1659 }
1660
type_handler_unsigned() const1661 const Type_handler *Type_handler_longlong::type_handler_unsigned() const
1662 {
1663 return &type_handler_ulonglong;
1664 }
1665
1666 /***************************************************************************/
1667
type_handler_for_comparison() const1668 const Type_handler *Type_handler_null::type_handler_for_comparison() const
1669 {
1670 return &type_handler_null;
1671 }
1672
1673
type_handler_for_comparison() const1674 const Type_handler *Type_handler_int_result::type_handler_for_comparison() const
1675 {
1676 return &type_handler_slonglong;
1677 }
1678
1679
type_handler_for_comparison() const1680 const Type_handler *Type_handler_string_result::type_handler_for_comparison() const
1681 {
1682 return &type_handler_long_blob;
1683 }
1684
1685
type_handler_for_comparison() const1686 const Type_handler *Type_handler_decimal_result::type_handler_for_comparison() const
1687 {
1688 return &type_handler_newdecimal;
1689 }
1690
1691
type_handler_for_comparison() const1692 const Type_handler *Type_handler_real_result::type_handler_for_comparison() const
1693 {
1694 return &type_handler_double;
1695 }
1696
1697
type_handler_for_comparison() const1698 const Type_handler *Type_handler_time_common::type_handler_for_comparison() const
1699 {
1700 return &type_handler_time;
1701 }
1702
type_handler_for_comparison() const1703 const Type_handler *Type_handler_date_common::type_handler_for_comparison() const
1704 {
1705 return &type_handler_newdate;
1706 }
1707
1708
type_handler_for_comparison() const1709 const Type_handler *Type_handler_datetime_common::type_handler_for_comparison() const
1710 {
1711 return &type_handler_datetime;
1712 }
1713
1714
type_handler_for_comparison() const1715 const Type_handler *Type_handler_timestamp_common::type_handler_for_comparison() const
1716 {
1717 return &type_handler_timestamp;
1718 }
1719
1720
type_handler_for_comparison() const1721 const Type_handler *Type_handler_row::type_handler_for_comparison() const
1722 {
1723 return &type_handler_row;
1724 }
1725
1726 /***************************************************************************/
1727
1728 const Type_handler *
type_handler_for_native_format() const1729 Type_handler_timestamp_common::type_handler_for_native_format() const
1730 {
1731 return &type_handler_timestamp2;
1732 }
1733
1734
1735 const Type_handler *
type_handler_for_native_format() const1736 Type_handler_time_common::type_handler_for_native_format() const
1737 {
1738 return &type_handler_time2;
1739 }
1740
1741
1742 /***************************************************************************/
1743
type_handler_for_item_field() const1744 const Type_handler *Type_handler_typelib::type_handler_for_item_field() const
1745 {
1746 return &type_handler_string;
1747 }
1748
1749
cast_to_int_type_handler() const1750 const Type_handler *Type_handler_typelib::cast_to_int_type_handler() const
1751 {
1752 return &type_handler_slonglong;
1753 }
1754
1755
1756 /***************************************************************************/
1757
1758 bool
aggregate_for_result(const Type_handler * other)1759 Type_handler_hybrid_field_type::aggregate_for_result(const Type_handler *other)
1760 {
1761 Type_handler_pair tp(m_type_handler, other);
1762 do
1763 {
1764 const Type_handler *hres;
1765 const Type_collection *c;
1766 if (((c= Type_handler::type_collection_for_aggregation(tp.a(), tp.b())) &&
1767 (hres= c->aggregate_for_result(tp.a(), tp.b()))) ||
1768 (hres= type_handler_data->
1769 m_type_aggregator_for_result.find_handler(tp.a(), tp.b())))
1770 {
1771 m_type_handler= hres;
1772 return false;
1773 }
1774 } while (tp.to_base());
1775 return true;
1776 }
1777
1778
1779 const Type_handler *
type_handler_long_or_longlong(uint max_char_length,bool unsigned_flag)1780 Type_handler::type_handler_long_or_longlong(uint max_char_length,
1781 bool unsigned_flag)
1782 {
1783 if (unsigned_flag)
1784 {
1785 if (max_char_length <= MY_INT32_NUM_DECIMAL_DIGITS - 2)
1786 return &type_handler_ulong;
1787 return &type_handler_ulonglong;
1788 }
1789 if (max_char_length <= MY_INT32_NUM_DECIMAL_DIGITS - 2)
1790 return &type_handler_slong;
1791 return &type_handler_slonglong;
1792 }
1793
1794
1795 /*
1796 This method is called for CASE (and its abbreviations) and LEAST/GREATEST
1797 when data type aggregation returned LONGLONG and there were some BIT
1798 expressions. This helps to adjust the data type from LONGLONG to LONG
1799 if all expressions fit.
1800 */
1801 const Type_handler *
bit_and_int_mixture_handler(uint max_char_length)1802 Type_handler::bit_and_int_mixture_handler(uint max_char_length)
1803 {
1804 if (max_char_length <= MY_INT32_NUM_DECIMAL_DIGITS)
1805 return &type_handler_slong;
1806 return &type_handler_slonglong;
1807 }
1808
1809
1810 /**
1811 @brief Aggregates field types from the array of items.
1812
1813 @param[in] items array of items to aggregate the type from
1814 @param[in] nitems number of items in the array
1815 @param[in] treat_bit_as_number - if BIT should be aggregated to a non-BIT
1816 counterpart as a LONGLONG number or as a VARBINARY string.
1817
1818 Currently behaviour depends on the function:
1819 - LEAST/GREATEST treat BIT as VARBINARY when
1820 aggregating with a non-BIT counterpart.
1821 Note, UNION also works this way.
1822
1823 - CASE, COALESCE, IF, IFNULL treat BIT as LONGLONG when
1824 aggregating with a non-BIT counterpart;
1825
1826 This inconsistency may be changed in the future. See MDEV-8867.
1827
1828 Note, independently from "treat_bit_as_number":
1829 - a single BIT argument gives BIT as a result
1830 - two BIT couterparts give BIT as a result
1831
1832 @details This function aggregates field types from the array of items.
1833 Found type is supposed to be used later as the result field type
1834 of a multi-argument function.
1835 Aggregation itself is performed by Type_handler::aggregate_for_result().
1836
1837 @note The term "aggregation" is used here in the sense of inferring the
1838 result type of a function from its argument types.
1839
1840 @retval false - on success
1841 @retval true - on error
1842 */
1843
1844 bool
aggregate_for_result(const char * funcname,Item ** items,uint nitems,bool treat_bit_as_number)1845 Type_handler_hybrid_field_type::aggregate_for_result(const char *funcname,
1846 Item **items, uint nitems,
1847 bool treat_bit_as_number)
1848 {
1849 bool bit_and_non_bit_mixture_found= false;
1850 uint32 max_display_length;
1851 if (!nitems || items[0]->result_type() == ROW_RESULT)
1852 {
1853 DBUG_ASSERT(0);
1854 set_handler(&type_handler_null);
1855 return true;
1856 }
1857 set_handler(items[0]->type_handler());
1858 max_display_length= items[0]->max_display_length();
1859 for (uint i= 1 ; i < nitems ; i++)
1860 {
1861 const Type_handler *cur= items[i]->type_handler();
1862 set_if_bigger(max_display_length, items[i]->max_display_length());
1863 if (treat_bit_as_number &&
1864 ((type_handler() == &type_handler_bit) ^ (cur == &type_handler_bit)))
1865 {
1866 bit_and_non_bit_mixture_found= true;
1867 if (type_handler() == &type_handler_bit)
1868 set_handler(&type_handler_slonglong); // BIT + non-BIT
1869 else
1870 cur= &type_handler_slonglong; // non-BIT + BIT
1871 }
1872 if (aggregate_for_result(cur))
1873 {
1874 my_error(ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION, MYF(0),
1875 type_handler()->name().ptr(), cur->name().ptr(), funcname);
1876 return true;
1877 }
1878 }
1879 if (bit_and_non_bit_mixture_found && type_handler() == &type_handler_slonglong)
1880 set_handler(Type_handler::bit_and_int_mixture_handler(max_display_length));
1881 return false;
1882 }
1883
1884 /**
1885 Collect built-in data type handlers for comparison.
1886 This method is very similar to item_cmp_type() defined in item.cc.
1887 Now they coexist. Later item_cmp_type() will be removed.
1888 In addition to item_cmp_type(), this method correctly aggregates
1889 TIME with DATETIME/TIMESTAMP/DATE, so no additional find_date_time_item()
1890 is needed after this call.
1891 */
1892
1893 bool
aggregate_for_comparison(const Type_handler * h)1894 Type_handler_hybrid_field_type::aggregate_for_comparison(const Type_handler *h)
1895 {
1896 DBUG_ASSERT(m_type_handler == m_type_handler->type_handler_for_comparison());
1897 DBUG_ASSERT(h == h->type_handler_for_comparison());
1898 const Type_handler *hres;
1899 const Type_collection *c;
1900 if (!(c= Type_handler::type_collection_for_aggregation(m_type_handler, h)) ||
1901 !(hres= c->aggregate_for_comparison(m_type_handler, h)))
1902 hres= type_handler_data->
1903 m_type_aggregator_for_comparison.find_handler(m_type_handler, h);
1904 if (!hres)
1905 return true;
1906 m_type_handler= hres;
1907 DBUG_ASSERT(m_type_handler == m_type_handler->type_handler_for_comparison());
1908 return false;
1909 }
1910
1911
1912 const Type_handler *
aggregate_for_comparison(const Type_handler * ha,const Type_handler * hb) const1913 Type_collection_std::aggregate_for_comparison(const Type_handler *ha,
1914 const Type_handler *hb) const
1915 {
1916 Item_result a= ha->cmp_type();
1917 Item_result b= hb->cmp_type();
1918 if (a == STRING_RESULT && b == STRING_RESULT)
1919 return &type_handler_long_blob;
1920 if (a == INT_RESULT && b == INT_RESULT)
1921 return &type_handler_slonglong;
1922 if (a == ROW_RESULT || b == ROW_RESULT)
1923 return &type_handler_row;
1924 if (a == TIME_RESULT || b == TIME_RESULT)
1925 {
1926 if ((a == TIME_RESULT) + (b == TIME_RESULT) == 1)
1927 {
1928 /*
1929 We're here if there's only one temporal data type:
1930 either m_type_handler or h.
1931 Temporal types bit non-temporal types.
1932 */
1933 const Type_handler *res= b == TIME_RESULT ? hb : ha;
1934 /*
1935 Compare TIMESTAMP to a non-temporal type as DATETIME.
1936 This is needed to make queries with fuzzy dates work:
1937 SELECT * FROM t1
1938 WHERE
1939 ts BETWEEN '0000-00-00' AND '2010-00-01 00:00:00';
1940 */
1941 if (res->type_handler_for_native_format() == &type_handler_timestamp2)
1942 return &type_handler_datetime;
1943 return res;
1944 }
1945 else
1946 {
1947 /*
1948 We're here if both m_type_handler and h are temporal data types.
1949 - If both data types are TIME, we preserve TIME.
1950 - If both data types are DATE, we preserve DATE.
1951 Preserving DATE is needed for EXPLAIN FORMAT=JSON,
1952 to print DATE constants using proper format:
1953 'YYYY-MM-DD' rather than 'YYYY-MM-DD 00:00:00'.
1954 */
1955 if (ha->field_type() != hb->field_type())
1956 return &type_handler_datetime;
1957 return ha;
1958 }
1959 }
1960 if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1961 (b == INT_RESULT || b == DECIMAL_RESULT))
1962 return &type_handler_newdecimal;
1963 return &type_handler_double;
1964 }
1965
1966
1967 /**
1968 Aggregate data type handler for LEAST/GRATEST.
1969 aggregate_for_min_max() is close to aggregate_for_comparison(),
1970 but tries to preserve the exact type handler for string, int and temporal
1971 data types (instead of converting to super-types).
1972 FLOAT is not preserved and is converted to its super-type (DOUBLE).
1973 This should probably fixed eventually, for symmetry.
1974 */
1975
1976 bool
aggregate_for_min_max(const Type_handler * h)1977 Type_handler_hybrid_field_type::aggregate_for_min_max(const Type_handler *h)
1978 {
1979 Type_handler_pair tp(m_type_handler, h);
1980 do
1981 {
1982 const Type_handler *hres;
1983 const Type_collection *c;
1984 if (((c= Type_handler::type_collection_for_aggregation(tp.a(), tp.b())) &&
1985 (hres= c->aggregate_for_min_max(tp.a(), tp.b()))) ||
1986 (hres= type_handler_data->
1987 m_type_aggregator_for_result.find_handler(tp.a(), tp.b())))
1988 {
1989 /*
1990 For now we suppose that these two expressions:
1991 - LEAST(type1, type2)
1992 - COALESCE(type1, type2)
1993 return the same data type (or both expressions return error)
1994 if type1 and/or type2 are non-traditional.
1995 This may change in the future.
1996 */
1997 m_type_handler= hres;
1998 return false;
1999 }
2000 } while (tp.to_base());
2001 return true;
2002 }
2003
2004
2005 const Type_handler *
aggregate_for_min_max(const Type_handler * ha,const Type_handler * hb) const2006 Type_collection_std::aggregate_for_min_max(const Type_handler *ha,
2007 const Type_handler *hb) const
2008 {
2009 Item_result a= ha->cmp_type();
2010 Item_result b= hb->cmp_type();
2011 DBUG_ASSERT(a != ROW_RESULT); // Disallowed by check_cols() in fix_fields()
2012 DBUG_ASSERT(b != ROW_RESULT); // Disallowed by check_cols() in fix_fields()
2013
2014 if (a == STRING_RESULT && b == STRING_RESULT)
2015 return Type_collection_std::aggregate_for_result(ha, hb);
2016 if (a == INT_RESULT && b == INT_RESULT)
2017 {
2018 // BIT aggregates with non-BIT as BIGINT
2019 if (ha != hb)
2020 {
2021 if (ha == &type_handler_bit)
2022 ha= &type_handler_slonglong;
2023 else if (hb == &type_handler_bit)
2024 hb= &type_handler_slonglong;
2025 }
2026 return Type_collection_std::aggregate_for_result(ha, hb);
2027 }
2028 if (a == TIME_RESULT || b == TIME_RESULT)
2029 {
2030 if ((ha->type_handler_for_native_format() == &type_handler_timestamp2) +
2031 (hb->type_handler_for_native_format() == &type_handler_timestamp2) == 1)
2032 {
2033 /*
2034 Handle LEAST(TIMESTAMP, non-TIMESTAMP) as DATETIME,
2035 to make sure fuzzy dates work in this context:
2036 LEAST('2001-00-00', timestamp_field)
2037 */
2038 return &type_handler_datetime2;
2039 }
2040 if ((a == TIME_RESULT) + (b == TIME_RESULT) == 1)
2041 {
2042 /*
2043 We're here if there's only one temporal data type:
2044 either m_type_handler or h.
2045 Temporal types bit non-temporal types.
2046 */
2047 return (b == TIME_RESULT) ? hb : ha;
2048 }
2049 /*
2050 We're here if both m_type_handler and h are temporal data types.
2051 */
2052 return Type_collection_std::aggregate_for_result(ha, hb);
2053 }
2054 if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
2055 (b == INT_RESULT || b == DECIMAL_RESULT))
2056 {
2057 return &type_handler_newdecimal;
2058 }
2059 // Preserve FLOAT if two FLOATs, set to DOUBLE otherwise.
2060 if (ha == &type_handler_float && hb == &type_handler_float)
2061 return &type_handler_float;
2062 return &type_handler_double;
2063 }
2064
2065
2066 bool
aggregate_for_min_max(const char * funcname,Item ** items,uint nitems)2067 Type_handler_hybrid_field_type::aggregate_for_min_max(const char *funcname,
2068 Item **items, uint nitems)
2069 {
2070 bool bit_and_non_bit_mixture_found= false;
2071 // LEAST/GREATEST require at least two arguments
2072 DBUG_ASSERT(nitems > 1);
2073 set_handler(items[0]->type_handler());
2074 for (uint i= 1; i < nitems; i++)
2075 {
2076 const Type_handler *cur= items[i]->type_handler();
2077 // Check if BIT + non-BIT, or non-BIT + BIT
2078 bit_and_non_bit_mixture_found|= (m_type_handler == &type_handler_bit) !=
2079 (cur == &type_handler_bit);
2080 if (aggregate_for_min_max(cur))
2081 {
2082 my_error(ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION, MYF(0),
2083 type_handler()->name().ptr(), cur->name().ptr(), funcname);
2084 return true;
2085 }
2086 }
2087 if (bit_and_non_bit_mixture_found && type_handler() == &type_handler_slonglong)
2088 {
2089 uint32 max_display_length= items[0]->max_display_length();
2090 for (uint i= 1; i < nitems; i++)
2091 set_if_bigger(max_display_length, items[i]->max_display_length());
2092 set_handler(Type_handler::bit_and_int_mixture_handler(max_display_length));
2093 }
2094 return false;
2095 }
2096
2097
2098 const Type_handler *
aggregate_for_num_op(const Type_handler * h0,const Type_handler * h1) const2099 Type_collection_std::aggregate_for_num_op(const Type_handler *h0,
2100 const Type_handler *h1) const
2101 {
2102 Item_result r0= h0->cmp_type();
2103 Item_result r1= h1->cmp_type();
2104
2105 if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
2106 r0 == STRING_RESULT || r1 ==STRING_RESULT)
2107 return &type_handler_double;
2108
2109 if (r0 == TIME_RESULT || r1 == TIME_RESULT)
2110 return &type_handler_datetime;
2111
2112 if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
2113 return &type_handler_newdecimal;
2114
2115 DBUG_ASSERT(r0 == INT_RESULT && r1 == INT_RESULT);
2116 return &type_handler_slonglong;
2117 }
2118
2119
2120 const Type_aggregator::Pair*
find_pair(const Type_handler * handler1,const Type_handler * handler2) const2121 Type_aggregator::find_pair(const Type_handler *handler1,
2122 const Type_handler *handler2) const
2123 {
2124 for (uint i= 0; i < m_array.elements(); i++)
2125 {
2126 const Pair& el= m_array.at(i);
2127 if (el.eq(handler1, handler2) ||
2128 (m_is_commutative && el.eq(handler2, handler1)))
2129 return ⪙
2130 }
2131 return NULL;
2132 }
2133
2134
2135 bool
aggregate_for_num_op(const Type_aggregator * agg,const Type_handler * h0,const Type_handler * h1)2136 Type_handler_hybrid_field_type::aggregate_for_num_op(const Type_aggregator *agg,
2137 const Type_handler *h0,
2138 const Type_handler *h1)
2139 {
2140 Type_handler_pair tp(h0, h1);
2141 do
2142 {
2143 const Type_handler *hres;
2144 const Type_collection *c;
2145 if (((c= Type_handler::type_collection_for_aggregation(tp.a(), tp.b())) &&
2146 (hres= c->aggregate_for_num_op(tp.a(), tp.b()))) ||
2147 (hres= agg->find_handler(tp.a(), tp.b())))
2148 {
2149 m_type_handler= hres;
2150 return false;
2151 }
2152 } while (tp.to_base());
2153 return true;
2154 }
2155
2156
2157 /***************************************************************************/
2158
2159 const Type_handler *
get_handler_by_field_type(enum_field_types type)2160 Type_handler::get_handler_by_field_type(enum_field_types type)
2161 {
2162 switch (type) {
2163 case MYSQL_TYPE_DECIMAL: return &type_handler_olddecimal;
2164 case MYSQL_TYPE_NEWDECIMAL: return &type_handler_newdecimal;
2165 case MYSQL_TYPE_TINY: return &type_handler_stiny;
2166 case MYSQL_TYPE_SHORT: return &type_handler_sshort;
2167 case MYSQL_TYPE_LONG: return &type_handler_slong;
2168 case MYSQL_TYPE_LONGLONG: return &type_handler_slonglong;
2169 case MYSQL_TYPE_INT24: return &type_handler_sint24;
2170 case MYSQL_TYPE_YEAR: return &type_handler_year;
2171 case MYSQL_TYPE_BIT: return &type_handler_bit;
2172 case MYSQL_TYPE_FLOAT: return &type_handler_float;
2173 case MYSQL_TYPE_DOUBLE: return &type_handler_double;
2174 case MYSQL_TYPE_NULL: return &type_handler_null;
2175 case MYSQL_TYPE_VARCHAR: return &type_handler_varchar;
2176 case MYSQL_TYPE_TINY_BLOB: return &type_handler_tiny_blob;
2177 case MYSQL_TYPE_MEDIUM_BLOB: return &type_handler_medium_blob;
2178 case MYSQL_TYPE_LONG_BLOB: return &type_handler_long_blob;
2179 case MYSQL_TYPE_BLOB: return &type_handler_blob;
2180 case MYSQL_TYPE_VAR_STRING: return &type_handler_varchar; // Map to VARCHAR
2181 case MYSQL_TYPE_STRING: return &type_handler_string;
2182 case MYSQL_TYPE_ENUM: return &type_handler_varchar; // Map to VARCHAR
2183 case MYSQL_TYPE_SET: return &type_handler_varchar; // Map to VARCHAR
2184 case MYSQL_TYPE_GEOMETRY:
2185 #ifdef HAVE_SPATIAL
2186 return &type_handler_geometry;
2187 #else
2188 return NULL;
2189 #endif
2190 case MYSQL_TYPE_TIMESTAMP: return &type_handler_timestamp2;// Map to timestamp2
2191 case MYSQL_TYPE_TIMESTAMP2: return &type_handler_timestamp2;
2192 case MYSQL_TYPE_DATE: return &type_handler_newdate; // Map to newdate
2193 case MYSQL_TYPE_TIME: return &type_handler_time2; // Map to time2
2194 case MYSQL_TYPE_TIME2: return &type_handler_time2;
2195 case MYSQL_TYPE_DATETIME: return &type_handler_datetime2; // Map to datetime2
2196 case MYSQL_TYPE_DATETIME2: return &type_handler_datetime2;
2197 case MYSQL_TYPE_NEWDATE:
2198 /*
2199 NEWDATE is actually a real_type(), not a field_type(),
2200 but it's used around the code in field_type() context.
2201 We should probably clean up the code not to use MYSQL_TYPE_NEWDATE
2202 in field_type() context and add DBUG_ASSERT(0) here.
2203 */
2204 return &type_handler_newdate;
2205 case MYSQL_TYPE_VARCHAR_COMPRESSED:
2206 case MYSQL_TYPE_BLOB_COMPRESSED:
2207 break;
2208 };
2209 DBUG_ASSERT(0);
2210 return &type_handler_string;
2211 }
2212
2213
2214 const Type_handler *
get_handler_by_real_type(enum_field_types type)2215 Type_handler::get_handler_by_real_type(enum_field_types type)
2216 {
2217 switch (type) {
2218 case MYSQL_TYPE_DECIMAL: return &type_handler_olddecimal;
2219 case MYSQL_TYPE_NEWDECIMAL: return &type_handler_newdecimal;
2220 case MYSQL_TYPE_TINY: return &type_handler_stiny;
2221 case MYSQL_TYPE_SHORT: return &type_handler_sshort;
2222 case MYSQL_TYPE_LONG: return &type_handler_slong;
2223 case MYSQL_TYPE_LONGLONG: return &type_handler_slonglong;
2224 case MYSQL_TYPE_INT24: return &type_handler_sint24;
2225 case MYSQL_TYPE_YEAR: return &type_handler_year;
2226 case MYSQL_TYPE_BIT: return &type_handler_bit;
2227 case MYSQL_TYPE_FLOAT: return &type_handler_float;
2228 case MYSQL_TYPE_DOUBLE: return &type_handler_double;
2229 case MYSQL_TYPE_NULL: return &type_handler_null;
2230 case MYSQL_TYPE_VARCHAR: return &type_handler_varchar;
2231 case MYSQL_TYPE_VARCHAR_COMPRESSED: return &type_handler_varchar_compressed;
2232 case MYSQL_TYPE_TINY_BLOB: return &type_handler_tiny_blob;
2233 case MYSQL_TYPE_MEDIUM_BLOB: return &type_handler_medium_blob;
2234 case MYSQL_TYPE_LONG_BLOB: return &type_handler_long_blob;
2235 case MYSQL_TYPE_BLOB: return &type_handler_blob;
2236 case MYSQL_TYPE_BLOB_COMPRESSED: return &type_handler_blob_compressed;
2237 case MYSQL_TYPE_VAR_STRING: return &type_handler_var_string;
2238 case MYSQL_TYPE_STRING: return &type_handler_string;
2239 case MYSQL_TYPE_ENUM: return &type_handler_enum;
2240 case MYSQL_TYPE_SET: return &type_handler_set;
2241 case MYSQL_TYPE_GEOMETRY:
2242 #ifdef HAVE_SPATIAL
2243 return &type_handler_geometry;
2244 #else
2245 return NULL;
2246 #endif
2247 case MYSQL_TYPE_TIMESTAMP: return &type_handler_timestamp;
2248 case MYSQL_TYPE_TIMESTAMP2: return &type_handler_timestamp2;
2249 case MYSQL_TYPE_DATE: return &type_handler_date;
2250 case MYSQL_TYPE_TIME: return &type_handler_time;
2251 case MYSQL_TYPE_TIME2: return &type_handler_time2;
2252 case MYSQL_TYPE_DATETIME: return &type_handler_datetime;
2253 case MYSQL_TYPE_DATETIME2: return &type_handler_datetime2;
2254 case MYSQL_TYPE_NEWDATE: return &type_handler_newdate;
2255 };
2256 return NULL;
2257 }
2258
2259
2260 /**
2261 Create a DOUBLE field by default.
2262 */
2263 Field *
make_num_distinct_aggregator_field(MEM_ROOT * mem_root,const Item * item) const2264 Type_handler::make_num_distinct_aggregator_field(MEM_ROOT *mem_root,
2265 const Item *item) const
2266 {
2267 return new(mem_root)
2268 Field_double(NULL, item->max_length,
2269 (uchar *) (item->maybe_null ? "" : 0),
2270 item->maybe_null ? 1 : 0, Field::NONE,
2271 &item->name, (uint8) item->decimals,
2272 0, item->unsigned_flag);
2273 }
2274
2275
2276 Field *
make_num_distinct_aggregator_field(MEM_ROOT * mem_root,const Item * item) const2277 Type_handler_float::make_num_distinct_aggregator_field(MEM_ROOT *mem_root,
2278 const Item *item)
2279 const
2280 {
2281 return new(mem_root)
2282 Field_float(NULL, item->max_length,
2283 (uchar *) (item->maybe_null ? "" : 0),
2284 item->maybe_null ? 1 : 0, Field::NONE,
2285 &item->name, (uint8) item->decimals,
2286 0, item->unsigned_flag);
2287 }
2288
2289
2290 Field *
make_num_distinct_aggregator_field(MEM_ROOT * mem_root,const Item * item) const2291 Type_handler_decimal_result::make_num_distinct_aggregator_field(
2292 MEM_ROOT *mem_root,
2293 const Item *item)
2294 const
2295 {
2296 DBUG_ASSERT(item->decimals <= DECIMAL_MAX_SCALE);
2297 return new (mem_root)
2298 Field_new_decimal(NULL, item->max_length,
2299 (uchar *) (item->maybe_null ? "" : 0),
2300 item->maybe_null ? 1 : 0, Field::NONE,
2301 &item->name, (uint8) item->decimals,
2302 0, item->unsigned_flag);
2303 }
2304
2305
2306 Field *
make_num_distinct_aggregator_field(MEM_ROOT * mem_root,const Item * item) const2307 Type_handler_int_result::make_num_distinct_aggregator_field(MEM_ROOT *mem_root,
2308 const Item *item)
2309 const
2310 {
2311 /**
2312 Make a longlong field for all INT-alike types. It could create
2313 smaller fields for TINYINT, SMALLINT, MEDIUMINT, INT though.
2314 */
2315 return new(mem_root)
2316 Field_longlong(NULL, item->max_length,
2317 (uchar *) (item->maybe_null ? "" : 0),
2318 item->maybe_null ? 1 : 0, Field::NONE,
2319 &item->name, 0, item->unsigned_flag);
2320 }
2321
2322
2323 /***********************************************************************/
2324
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2325 Field *Type_handler_tiny::make_conversion_table_field(MEM_ROOT *root,
2326 TABLE *table,
2327 uint metadata,
2328 const Field *target)
2329 const
2330 {
2331 /*
2332 As we don't know if the integer was signed or not on the master,
2333 assume we have same sign on master and slave. This is true when not
2334 using conversions so it should be true also when using conversions.
2335 */
2336 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
2337 return new (root)
2338 Field_tiny(NULL, 4 /*max_length*/, (uchar *) "", 1, Field::NONE,
2339 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
2340 }
2341
2342
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2343 Field *Type_handler_short::make_conversion_table_field(MEM_ROOT *root,
2344 TABLE *table,
2345 uint metadata,
2346 const Field *target)
2347 const
2348 {
2349 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
2350 return new (root)
2351 Field_short(NULL, 6 /*max_length*/, (uchar *) "", 1, Field::NONE,
2352 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
2353 }
2354
2355
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2356 Field *Type_handler_int24::make_conversion_table_field(MEM_ROOT *root,
2357 TABLE *table,
2358 uint metadata,
2359 const Field *target)
2360 const
2361 {
2362 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
2363 return new (root)
2364 Field_medium(NULL, 9 /*max_length*/, (uchar *) "", 1, Field::NONE,
2365 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
2366 }
2367
2368
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2369 Field *Type_handler_long::make_conversion_table_field(MEM_ROOT *root,
2370 TABLE *table,
2371 uint metadata,
2372 const Field *target)
2373 const
2374 {
2375 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
2376 return new (root)
2377 Field_long(NULL, 11 /*max_length*/, (uchar *) "", 1, Field::NONE,
2378 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
2379 }
2380
2381
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2382 Field *Type_handler_longlong::make_conversion_table_field(MEM_ROOT *root,
2383 TABLE *table,
2384 uint metadata,
2385 const Field *target)
2386 const
2387 {
2388 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
2389 return new (root)
2390 Field_longlong(NULL, 20 /*max_length*/,(uchar *) "", 1, Field::NONE,
2391 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
2392 }
2393
2394
2395
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2396 Field *Type_handler_float::make_conversion_table_field(MEM_ROOT *root,
2397 TABLE *table,
2398 uint metadata,
2399 const Field *target)
2400 const
2401 {
2402 return new (root)
2403 Field_float(NULL, 12 /*max_length*/, (uchar *) "", 1, Field::NONE,
2404 &empty_clex_str, 0/*dec*/, 0/*zerofill*/, 0/*unsigned_flag*/);
2405 }
2406
2407
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2408 Field *Type_handler_double::make_conversion_table_field(MEM_ROOT *root,
2409 TABLE *table,
2410 uint metadata,
2411 const Field *target)
2412 const
2413 {
2414 return new (root)
2415 Field_double(NULL, 22 /*max_length*/, (uchar *) "", 1, Field::NONE,
2416 &empty_clex_str, 0/*dec*/, 0/*zerofill*/, 0/*unsigned_flag*/);
2417 }
2418
2419
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2420 Field *Type_handler_newdecimal::make_conversion_table_field(MEM_ROOT *root,
2421 TABLE *table,
2422 uint metadata,
2423 const Field *target)
2424 const
2425 {
2426 int precision= metadata >> 8;
2427 uint8 decimals= metadata & 0x00ff;
2428 uint32 max_length= my_decimal_precision_to_length(precision, decimals, false);
2429 DBUG_ASSERT(decimals <= DECIMAL_MAX_SCALE);
2430 return new (root)
2431 Field_new_decimal(NULL, max_length, (uchar *) "", 1, Field::NONE,
2432 &empty_clex_str, decimals, 0/*zerofill*/, 0/*unsigned*/);
2433 }
2434
2435
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2436 Field *Type_handler_olddecimal::make_conversion_table_field(MEM_ROOT *root,
2437 TABLE *table,
2438 uint metadata,
2439 const Field *target)
2440 const
2441 {
2442 sql_print_error("In RBR mode, Slave received incompatible DECIMAL field "
2443 "(old-style decimal field) from Master while creating "
2444 "conversion table. Please consider changing datatype on "
2445 "Master to new style decimal by executing ALTER command for"
2446 " column Name: %s.%s.%s.",
2447 target->table->s->db.str,
2448 target->table->s->table_name.str,
2449 target->field_name.str);
2450 return NULL;
2451 }
2452
2453
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2454 Field *Type_handler_year::make_conversion_table_field(MEM_ROOT *root,
2455 TABLE *table,
2456 uint metadata,
2457 const Field *target)
2458 const
2459 {
2460 return new(root)
2461 Field_year(NULL, 4, (uchar *) "", 1, Field::NONE, &empty_clex_str);
2462 }
2463
2464
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2465 Field *Type_handler_null::make_conversion_table_field(MEM_ROOT *root,
2466 TABLE *table,
2467 uint metadata,
2468 const Field *target)
2469 const
2470 {
2471 return new(root)
2472 Field_null(NULL, 0, Field::NONE, &empty_clex_str, target->charset());
2473 }
2474
2475
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2476 Field *Type_handler_timestamp::make_conversion_table_field(MEM_ROOT *root,
2477 TABLE *table,
2478 uint metadata,
2479 const Field *target)
2480 const
2481 {
2482 return new_Field_timestamp(root, NULL, (uchar *) "", 1,
2483 Field::NONE, &empty_clex_str,
2484 table->s, target->decimals());
2485 }
2486
2487
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2488 Field *Type_handler_timestamp2::make_conversion_table_field(MEM_ROOT *root,
2489 TABLE *table,
2490 uint metadata,
2491 const Field *target)
2492 const
2493 {
2494 return new(root)
2495 Field_timestampf(NULL, (uchar *) "", 1, Field::NONE,
2496 &empty_clex_str, table->s, metadata);
2497 }
2498
2499
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2500 Field *Type_handler_newdate::make_conversion_table_field(MEM_ROOT *root,
2501 TABLE *table,
2502 uint metadata,
2503 const Field *target)
2504 const
2505 {
2506 return new(root)
2507 Field_newdate(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str);
2508 }
2509
2510
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2511 Field *Type_handler_date::make_conversion_table_field(MEM_ROOT *root,
2512 TABLE *table,
2513 uint metadata,
2514 const Field *target)
2515 const
2516 {
2517 return new(root)
2518 Field_date(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str);
2519 }
2520
2521
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2522 Field *Type_handler_time::make_conversion_table_field(MEM_ROOT *root,
2523 TABLE *table,
2524 uint metadata,
2525 const Field *target)
2526 const
2527 {
2528 return new_Field_time(root, NULL, (uchar *) "", 1,
2529 Field::NONE, &empty_clex_str, target->decimals());
2530 }
2531
2532
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2533 Field *Type_handler_time2::make_conversion_table_field(MEM_ROOT *root,
2534 TABLE *table,
2535 uint metadata,
2536 const Field *target)
2537 const
2538 {
2539 return new(root)
2540 Field_timef(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str, metadata);
2541 }
2542
2543
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2544 Field *Type_handler_datetime::make_conversion_table_field(MEM_ROOT *root,
2545 TABLE *table,
2546 uint metadata,
2547 const Field *target)
2548 const
2549 {
2550 return new_Field_datetime(root, NULL, (uchar *) "", 1,
2551 Field::NONE, &empty_clex_str, target->decimals());
2552 }
2553
2554
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2555 Field *Type_handler_datetime2::make_conversion_table_field(MEM_ROOT *root,
2556 TABLE *table,
2557 uint metadata,
2558 const Field *target)
2559 const
2560 {
2561 return new(root)
2562 Field_datetimef(NULL, (uchar *) "", 1,
2563 Field::NONE, &empty_clex_str, metadata);
2564 }
2565
2566
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2567 Field *Type_handler_bit::make_conversion_table_field(MEM_ROOT *root,
2568 TABLE *table,
2569 uint metadata,
2570 const Field *target)
2571 const
2572 {
2573 DBUG_ASSERT((metadata & 0xff) <= 7);
2574 uint32 max_length= 8 * (metadata >> 8U) + (metadata & 0x00ff);
2575 return new(root)
2576 Field_bit_as_char(NULL, max_length, (uchar *) "", 1,
2577 Field::NONE, &empty_clex_str);
2578 }
2579
2580
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2581 Field *Type_handler_string::make_conversion_table_field(MEM_ROOT *root,
2582 TABLE *table,
2583 uint metadata,
2584 const Field *target)
2585 const
2586 {
2587 /* This is taken from Field_string::unpack. */
2588 uint32 max_length= (((metadata >> 4) & 0x300) ^ 0x300) + (metadata & 0x00ff);
2589 return new(root)
2590 Field_string(NULL, max_length, (uchar *) "", 1,
2591 Field::NONE, &empty_clex_str, target->charset());
2592 }
2593
2594
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2595 Field *Type_handler_varchar::make_conversion_table_field(MEM_ROOT *root,
2596 TABLE *table,
2597 uint metadata,
2598 const Field *target)
2599 const
2600 {
2601 DBUG_ASSERT(HA_VARCHAR_PACKLENGTH(metadata) <= MAX_FIELD_VARCHARLENGTH);
2602 return new(root)
2603 Field_varstring(NULL, metadata, HA_VARCHAR_PACKLENGTH(metadata),
2604 (uchar *) "", 1, Field::NONE, &empty_clex_str,
2605 table->s, target->charset());
2606 }
2607
2608
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2609 Field *Type_handler_varchar_compressed::make_conversion_table_field(
2610 MEM_ROOT *root,
2611 TABLE *table,
2612 uint metadata,
2613 const Field *target)
2614 const
2615 {
2616 return new(root)
2617 Field_varstring_compressed(NULL, metadata,
2618 HA_VARCHAR_PACKLENGTH(metadata),
2619 (uchar *) "", 1, Field::NONE,
2620 &empty_clex_str,
2621 table->s, target->charset(),
2622 zlib_compression_method);
2623 }
2624
2625
2626
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2627 Field *Type_handler_blob_compressed::make_conversion_table_field(
2628 MEM_ROOT *root,
2629 TABLE *table,
2630 uint metadata,
2631 const Field *target)
2632 const
2633 {
2634 uint pack_length= metadata & 0x00ff;
2635 if (pack_length < 1 || pack_length > 4)
2636 return NULL; // Broken binary log?
2637 return new(root)
2638 Field_blob_compressed(NULL, (uchar *) "", 1, Field::NONE,
2639 &empty_clex_str,
2640 table->s, pack_length, target->charset(),
2641 zlib_compression_method);
2642 }
2643
2644
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2645 Field *Type_handler_enum::make_conversion_table_field(MEM_ROOT *root,
2646 TABLE *table,
2647 uint metadata,
2648 const Field *target)
2649 const
2650 {
2651 DBUG_ASSERT(target->type() == MYSQL_TYPE_STRING);
2652 DBUG_ASSERT(target->real_type() == MYSQL_TYPE_ENUM);
2653 return new(root)
2654 Field_enum(NULL, target->field_length,
2655 (uchar *) "", 1, Field::NONE, &empty_clex_str,
2656 metadata & 0x00ff/*pack_length()*/,
2657 ((const Field_enum*) target)->typelib, target->charset());
2658 }
2659
2660
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const2661 Field *Type_handler_set::make_conversion_table_field(MEM_ROOT *root,
2662 TABLE *table,
2663 uint metadata,
2664 const Field *target)
2665 const
2666 {
2667 DBUG_ASSERT(target->type() == MYSQL_TYPE_STRING);
2668 DBUG_ASSERT(target->real_type() == MYSQL_TYPE_SET);
2669 return new(root)
2670 Field_set(NULL, target->field_length,
2671 (uchar *) "", 1, Field::NONE, &empty_clex_str,
2672 metadata & 0x00ff/*pack_length()*/,
2673 ((const Field_enum*) target)->typelib, target->charset());
2674 }
2675
2676
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const2677 Field *Type_handler_enum::make_schema_field(MEM_ROOT *root, TABLE *table,
2678 const Record_addr &addr,
2679 const ST_FIELD_INFO &def) const
2680 {
2681 LEX_CSTRING name= def.name();
2682 const Typelib *typelib= def.typelib();
2683 DBUG_ASSERT(typelib);
2684 /*
2685 Assume I_S columns don't have non-ASCII characters in names.
2686 If we eventually want to, Typelib::max_char_length() must be implemented.
2687 */
2688 return new (root)
2689 Field_enum(addr.ptr(), (uint32) typelib->max_octet_length(),
2690 addr.null_ptr(), addr.null_bit(),
2691 Field::NONE, &name,
2692 get_enum_pack_length(typelib->count),
2693 typelib, system_charset_info);
2694
2695 }
2696
2697
2698 /*************************************************************************/
2699
2700 bool Type_handler::
Column_definition_validate_check_constraint(THD * thd,Column_definition * c) const2701 Column_definition_validate_check_constraint(THD *thd,
2702 Column_definition * c) const
2703 {
2704 return c->validate_check_constraint(thd);
2705 }
2706
2707
2708 /*************************************************************************/
2709
2710 bool
Column_definition_set_attributes(THD * thd,Column_definition * def,const Lex_field_type_st & attr,CHARSET_INFO * cs,column_definition_type_t type) const2711 Type_handler::Column_definition_set_attributes(THD *thd,
2712 Column_definition *def,
2713 const Lex_field_type_st &attr,
2714 CHARSET_INFO *cs,
2715 column_definition_type_t type)
2716 const
2717 {
2718 def->charset= cs;
2719 def->set_length_and_dec(attr);
2720 return false;
2721 }
2722
2723
2724 /*
2725 In sql_mode=ORACLE, real size of VARCHAR and CHAR with no length
2726 in SP parameters is fixed at runtime with the length of real args.
2727 Let's translate VARCHAR to VARCHAR(4000) for return value.
2728
2729 Since Oracle 9, maximum size for VARCHAR in PL/SQL is 32767.
2730
2731 In MariaDB the limit for VARCHAR is 65535 bytes.
2732 We could translate VARCHAR with no length to VARCHAR(65535), but
2733 it would mean that for multi-byte character sets we'd have to translate
2734 VARCHAR to MEDIUMTEXT, to guarantee 65535 characters.
2735
2736 Also we could translate VARCHAR to VARCHAR(16383), where 16383 is
2737 the maximum possible length in characters in case of mbmaxlen=4
2738 (e.g. utf32, utf16, utf8mb4). However, we'll have character sets with
2739 mbmaxlen=5 soon (e.g. gb18030).
2740 */
2741
2742 bool
Column_definition_set_attributes(THD * thd,Column_definition * def,const Lex_field_type_st & attr,CHARSET_INFO * cs,column_definition_type_t type) const2743 Type_handler_string::Column_definition_set_attributes(
2744 THD *thd,
2745 Column_definition *def,
2746 const Lex_field_type_st &attr,
2747 CHARSET_INFO *cs,
2748 column_definition_type_t type)
2749 const
2750 {
2751 Type_handler::Column_definition_set_attributes(thd, def, attr, cs, type);
2752 if (attr.length())
2753 return false;
2754 switch (type) {
2755 case COLUMN_DEFINITION_ROUTINE_PARAM:
2756 case COLUMN_DEFINITION_FUNCTION_RETURN:
2757 if (thd->variables.sql_mode & MODE_ORACLE)
2758 {
2759 // See Type_handler_varchar::Column_definition_set_attributes()
2760 def->length= def->decimals= 2000;
2761 def->set_handler(&type_handler_varchar);
2762 return false;
2763 }
2764 break;
2765 case COLUMN_DEFINITION_ROUTINE_LOCAL:
2766 case COLUMN_DEFINITION_TABLE_FIELD:
2767 break;
2768 }
2769 def->length= 1;
2770 return false;
2771 }
2772
2773
2774 bool
Column_definition_set_attributes(THD * thd,Column_definition * def,const Lex_field_type_st & attr,CHARSET_INFO * cs,column_definition_type_t type) const2775 Type_handler_varchar::Column_definition_set_attributes(
2776 THD *thd,
2777 Column_definition *def,
2778 const Lex_field_type_st &attr,
2779 CHARSET_INFO *cs,
2780 column_definition_type_t type)
2781 const
2782 {
2783 Type_handler::Column_definition_set_attributes(thd, def, attr, cs, type);
2784 if (attr.length())
2785 return false;
2786 switch (type) {
2787 case COLUMN_DEFINITION_ROUTINE_PARAM:
2788 case COLUMN_DEFINITION_FUNCTION_RETURN:
2789 if (thd->variables.sql_mode & MODE_ORACLE)
2790 {
2791 /*
2792 Type_handler_varchar::adjust_spparam_type() tests "decimals"
2793 to detect if the formal parameter length needs to be adjusted to
2794 the actual parameter length. Non-zero decimals means that the length
2795 was set implicitly to the default value and needs to be adjusted.
2796 */
2797 def->length= def->decimals= 4000;
2798 return false;
2799 }
2800 break;
2801 case COLUMN_DEFINITION_ROUTINE_LOCAL:
2802 case COLUMN_DEFINITION_TABLE_FIELD:
2803 break;
2804 }
2805 thd->parse_error();
2806 return true;
2807 }
2808
2809
2810 /*************************************************************************/
2811 bool Type_handler_null::
Column_definition_fix_attributes(Column_definition * def) const2812 Column_definition_fix_attributes(Column_definition *def) const
2813 {
2814 return false;
2815 }
2816
2817 bool Type_handler_tiny::
Column_definition_fix_attributes(Column_definition * def) const2818 Column_definition_fix_attributes(Column_definition *def) const
2819 {
2820 return def->fix_attributes_int(MAX_TINYINT_WIDTH + def->sign_length());
2821 }
2822
2823 bool Type_handler_short::
Column_definition_fix_attributes(Column_definition * def) const2824 Column_definition_fix_attributes(Column_definition *def) const
2825 {
2826 return def->fix_attributes_int(MAX_SMALLINT_WIDTH + def->sign_length());
2827 }
2828
2829 bool Type_handler_int24::
Column_definition_fix_attributes(Column_definition * def) const2830 Column_definition_fix_attributes(Column_definition *def) const
2831 {
2832 return def->fix_attributes_int(MAX_MEDIUMINT_WIDTH + def->sign_length());
2833 }
2834
2835 bool Type_handler_long::
Column_definition_fix_attributes(Column_definition * def) const2836 Column_definition_fix_attributes(Column_definition *def) const
2837 {
2838 return def->fix_attributes_int(MAX_INT_WIDTH + def->sign_length());
2839 }
2840
2841 bool Type_handler_longlong::
Column_definition_fix_attributes(Column_definition * def) const2842 Column_definition_fix_attributes(Column_definition *def) const
2843 {
2844 return def->fix_attributes_int(MAX_BIGINT_WIDTH/*no sign_length() added*/);
2845 }
2846
2847 bool Type_handler_newdecimal::
Column_definition_fix_attributes(Column_definition * def) const2848 Column_definition_fix_attributes(Column_definition *def) const
2849 {
2850 return def->fix_attributes_decimal();
2851 }
2852
2853 bool Type_handler_olddecimal::
Column_definition_fix_attributes(Column_definition * def) const2854 Column_definition_fix_attributes(Column_definition *def) const
2855 {
2856 DBUG_ASSERT(0); // Obsolete
2857 return true;
2858 }
2859
2860 bool Type_handler_var_string::
Column_definition_fix_attributes(Column_definition * def) const2861 Column_definition_fix_attributes(Column_definition *def) const
2862 {
2863 DBUG_ASSERT(0); // Obsolete
2864 return true;
2865 }
2866
2867 bool Type_handler_varchar::
Column_definition_fix_attributes(Column_definition * def) const2868 Column_definition_fix_attributes(Column_definition *def) const
2869 {
2870 /*
2871 Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
2872 if they don't have a default value
2873 */
2874 return def->check_length(ER_TOO_BIG_DISPLAYWIDTH, MAX_FIELD_BLOBLENGTH);
2875 }
2876
2877 bool Type_handler_string::
Column_definition_fix_attributes(Column_definition * def) const2878 Column_definition_fix_attributes(Column_definition *def) const
2879 {
2880 return def->check_length(ER_TOO_BIG_FIELDLENGTH, MAX_FIELD_CHARLENGTH);
2881 }
2882
2883 bool Type_handler_blob_common::
Column_definition_fix_attributes(Column_definition * def) const2884 Column_definition_fix_attributes(Column_definition *def) const
2885 {
2886 def->flags|= BLOB_FLAG;
2887 return def->check_length(ER_TOO_BIG_DISPLAYWIDTH, MAX_FIELD_BLOBLENGTH);
2888 }
2889
2890
2891 bool Type_handler_year::
Column_definition_fix_attributes(Column_definition * def) const2892 Column_definition_fix_attributes(Column_definition *def) const
2893 {
2894 if (!def->length || def->length != 2)
2895 def->length= 4; // Default length
2896 def->flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
2897 return false;
2898 }
2899
2900 bool Type_handler_float::
Column_definition_fix_attributes(Column_definition * def) const2901 Column_definition_fix_attributes(Column_definition *def) const
2902 {
2903 return def->fix_attributes_real(MAX_FLOAT_STR_LENGTH);
2904 }
2905
2906
2907 bool Type_handler_double::
Column_definition_fix_attributes(Column_definition * def) const2908 Column_definition_fix_attributes(Column_definition *def) const
2909 {
2910 return def->fix_attributes_real(DBL_DIG + 7);
2911 }
2912
2913 bool Type_handler_timestamp_common::
Column_definition_fix_attributes(Column_definition * def) const2914 Column_definition_fix_attributes(Column_definition *def) const
2915 {
2916 def->flags|= UNSIGNED_FLAG;
2917 return def->fix_attributes_temporal_with_time(MAX_DATETIME_WIDTH);
2918 }
2919
2920 bool Type_handler_date_common::
Column_definition_fix_attributes(Column_definition * def) const2921 Column_definition_fix_attributes(Column_definition *def) const
2922 {
2923 // We don't support creation of MYSQL_TYPE_DATE anymore
2924 def->set_handler(&type_handler_newdate);
2925 def->length= MAX_DATE_WIDTH;
2926 return false;
2927 }
2928
2929 bool Type_handler_time_common::
Column_definition_fix_attributes(Column_definition * def) const2930 Column_definition_fix_attributes(Column_definition *def) const
2931 {
2932 return def->fix_attributes_temporal_with_time(MIN_TIME_WIDTH);
2933 }
2934
2935 bool Type_handler_datetime_common::
Column_definition_fix_attributes(Column_definition * def) const2936 Column_definition_fix_attributes(Column_definition *def) const
2937 {
2938 return def->fix_attributes_temporal_with_time(MAX_DATETIME_WIDTH);
2939 }
2940
2941 bool Type_handler_set::
Column_definition_fix_attributes(Column_definition * def) const2942 Column_definition_fix_attributes(Column_definition *def) const
2943 {
2944 def->pack_length= get_set_pack_length(def->interval_list.elements);
2945 return false;
2946 }
2947
2948 bool Type_handler_enum::
Column_definition_fix_attributes(Column_definition * def) const2949 Column_definition_fix_attributes(Column_definition *def) const
2950 {
2951 def->pack_length= get_enum_pack_length(def->interval_list.elements);
2952 return false;
2953 }
2954
2955 bool Type_handler_bit::
Column_definition_fix_attributes(Column_definition * def) const2956 Column_definition_fix_attributes(Column_definition *def) const
2957 {
2958 return def->fix_attributes_bit();
2959 }
2960
2961 /*************************************************************************/
2962
2963 void Type_handler_typelib::
Column_definition_reuse_fix_attributes(THD * thd,Column_definition * def,const Field * field) const2964 Column_definition_reuse_fix_attributes(THD *thd,
2965 Column_definition *def,
2966 const Field *field) const
2967 {
2968 DBUG_ASSERT(def->flags & (ENUM_FLAG | SET_FLAG));
2969 def->interval= field->get_typelib();
2970 }
2971
2972
2973 void Type_handler_year::
Column_definition_reuse_fix_attributes(THD * thd,Column_definition * def,const Field * field) const2974 Column_definition_reuse_fix_attributes(THD *thd,
2975 Column_definition *def,
2976 const Field *field) const
2977 {
2978 if (def->length != 4)
2979 {
2980 char buff[sizeof("YEAR()") + MY_INT64_NUM_DECIMAL_DIGITS + 1];
2981 my_snprintf(buff, sizeof(buff), "YEAR(%llu)", def->length);
2982 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
2983 ER_WARN_DEPRECATED_SYNTAX,
2984 ER_THD(thd, ER_WARN_DEPRECATED_SYNTAX),
2985 buff, "YEAR(4)");
2986 }
2987 }
2988
2989
2990 void Type_handler_real_result::
Column_definition_reuse_fix_attributes(THD * thd,Column_definition * def,const Field * field) const2991 Column_definition_reuse_fix_attributes(THD *thd,
2992 Column_definition *def,
2993 const Field *field) const
2994 {
2995 /*
2996 Floating points are stored with FLOATING_POINT_DECIMALS but internally
2997 in MariaDB used with NOT_FIXED_DEC, which is >= FLOATING_POINT_DECIMALS.
2998 */
2999 if (def->decimals >= FLOATING_POINT_DECIMALS)
3000 def->decimals= NOT_FIXED_DEC;
3001 }
3002
3003
3004 /*************************************************************************/
3005
3006 bool Type_handler::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3007 Column_definition_prepare_stage1(THD *thd,
3008 MEM_ROOT *mem_root,
3009 Column_definition *def,
3010 handler *file,
3011 ulonglong table_flags,
3012 const Column_derived_attributes
3013 *derived_attr)
3014 const
3015 {
3016 def->prepare_stage1_simple(&my_charset_bin);
3017 return false;
3018 }
3019
3020 bool Type_handler_null::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3021 Column_definition_prepare_stage1(THD *thd,
3022 MEM_ROOT *mem_root,
3023 Column_definition *def,
3024 handler *file,
3025 ulonglong table_flags,
3026 const Column_derived_attributes
3027 *derived_attr)
3028 const
3029 {
3030 def->prepare_charset_for_string(derived_attr);
3031 def->create_length_to_internal_length_null();
3032 return false;
3033 }
3034
3035 bool Type_handler_row::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3036 Column_definition_prepare_stage1(THD *thd,
3037 MEM_ROOT *mem_root,
3038 Column_definition *def,
3039 handler *file,
3040 ulonglong table_flags,
3041 const Column_derived_attributes
3042 *derived_attr)
3043 const
3044 {
3045 def->charset= &my_charset_bin;
3046 def->create_length_to_internal_length_null();
3047 return false;
3048 }
3049
3050 bool Type_handler_temporal_result::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3051 Column_definition_prepare_stage1(THD *thd,
3052 MEM_ROOT *mem_root,
3053 Column_definition *def,
3054 handler *file,
3055 ulonglong table_flags,
3056 const Column_derived_attributes
3057 *derived_attr)
3058 const
3059 {
3060 def->prepare_stage1_simple(&my_charset_numeric);
3061 return false;
3062 }
3063
3064
3065 bool Type_handler_numeric::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3066 Column_definition_prepare_stage1(THD *thd,
3067 MEM_ROOT *mem_root,
3068 Column_definition *def,
3069 handler *file,
3070 ulonglong table_flags,
3071 const Column_derived_attributes
3072 *derived_attr)
3073 const
3074 {
3075 def->prepare_stage1_simple(&my_charset_numeric);
3076 return false;
3077 }
3078
3079 bool Type_handler_newdecimal::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3080 Column_definition_prepare_stage1(THD *thd,
3081 MEM_ROOT *mem_root,
3082 Column_definition *def,
3083 handler *file,
3084 ulonglong table_flags,
3085 const Column_derived_attributes
3086 *derived_attr)
3087 const
3088 {
3089 def->charset= &my_charset_numeric;
3090 def->create_length_to_internal_length_newdecimal();
3091 return false;
3092 }
3093
3094 bool Type_handler_bit::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3095 Column_definition_prepare_stage1(THD *thd,
3096 MEM_ROOT *mem_root,
3097 Column_definition *def,
3098 handler *file,
3099 ulonglong table_flags,
3100 const Column_derived_attributes
3101 *derived_attr)
3102 const
3103 {
3104 def->charset= &my_charset_numeric;
3105 return def->prepare_stage1_bit(thd, mem_root, file, table_flags);
3106 }
3107
3108 bool Type_handler_typelib::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3109 Column_definition_prepare_stage1(THD *thd,
3110 MEM_ROOT *mem_root,
3111 Column_definition *def,
3112 handler *file,
3113 ulonglong table_flags,
3114 const Column_derived_attributes
3115 *derived_attr)
3116 const
3117 {
3118 return def->prepare_charset_for_string(derived_attr) ||
3119 def->prepare_stage1_typelib(thd, mem_root, file, table_flags);
3120 }
3121
3122
3123 bool Type_handler_string_result::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags,const Column_derived_attributes * derived_attr) const3124 Column_definition_prepare_stage1(THD *thd,
3125 MEM_ROOT *mem_root,
3126 Column_definition *def,
3127 handler *file,
3128 ulonglong table_flags,
3129 const Column_derived_attributes
3130 *derived_attr)
3131 const
3132 {
3133 return def->prepare_charset_for_string(derived_attr) ||
3134 def->prepare_stage1_string(thd, mem_root, file, table_flags);
3135 }
3136
3137
3138 /*************************************************************************/
3139
3140 bool Type_handler_general_purpose_string::
Column_definition_bulk_alter(Column_definition * def,const Column_derived_attributes * derived_attr,const Column_bulk_alter_attributes * bulk_alter_attr) const3141 Column_definition_bulk_alter(Column_definition *def,
3142 const Column_derived_attributes
3143 *derived_attr,
3144 const Column_bulk_alter_attributes
3145 *bulk_alter_attr)
3146 const
3147 {
3148 if (!bulk_alter_attr->alter_table_convert_to_charset())
3149 return false; // No "CONVERT TO" clause.
3150 CHARSET_INFO *defcs= def->explicit_or_derived_charset(derived_attr);
3151 DBUG_ASSERT(defcs);
3152 /*
3153 Handle 'ALTER TABLE t1 CONVERT TO CHARACTER SET csname'.
3154 Change character sets for all varchar/char/text columns,
3155 but do not touch varbinary/binary/blob columns.
3156 */
3157 if (defcs != &my_charset_bin)
3158 def->charset= bulk_alter_attr->alter_table_convert_to_charset();
3159 return false;
3160 };
3161
3162
3163 /*************************************************************************/
3164
3165 bool Type_handler::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file) const3166 Column_definition_redefine_stage1(Column_definition *def,
3167 const Column_definition *dup,
3168 const handler *file)
3169 const
3170 {
3171 def->redefine_stage1_common(dup, file);
3172 def->create_length_to_internal_length_simple();
3173 return false;
3174 }
3175
3176
3177 bool Type_handler_null::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file) const3178 Column_definition_redefine_stage1(Column_definition *def,
3179 const Column_definition *dup,
3180 const handler *file)
3181 const
3182 {
3183 def->redefine_stage1_common(dup, file);
3184 def->create_length_to_internal_length_null();
3185 return false;
3186 }
3187
3188
3189 bool Type_handler_newdecimal::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file) const3190 Column_definition_redefine_stage1(Column_definition *def,
3191 const Column_definition *dup,
3192 const handler *file)
3193 const
3194 {
3195 def->redefine_stage1_common(dup, file);
3196 def->create_length_to_internal_length_newdecimal();
3197 return false;
3198 }
3199
3200
3201 bool Type_handler_string_result::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file) const3202 Column_definition_redefine_stage1(Column_definition *def,
3203 const Column_definition *dup,
3204 const handler *file)
3205 const
3206 {
3207 def->redefine_stage1_common(dup, file);
3208 def->set_compression_method(dup->compression_method());
3209 def->create_length_to_internal_length_string();
3210 return false;
3211 }
3212
3213
3214 bool Type_handler_typelib::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file) const3215 Column_definition_redefine_stage1(Column_definition *def,
3216 const Column_definition *dup,
3217 const handler *file)
3218 const
3219 {
3220 def->redefine_stage1_common(dup, file);
3221 def->create_length_to_internal_length_typelib();
3222 return false;
3223 }
3224
3225
3226 bool Type_handler_bit::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file) const3227 Column_definition_redefine_stage1(Column_definition *def,
3228 const Column_definition *dup,
3229 const handler *file)
3230 const
3231 {
3232 def->redefine_stage1_common(dup, file);
3233 /*
3234 If we are replacing a field with a BIT field, we need
3235 to initialize pack_flag.
3236 */
3237 def->pack_flag= FIELDFLAG_NUMBER;
3238 if (!(file->ha_table_flags() & HA_CAN_BIT_FIELD))
3239 def->pack_flag|= FIELDFLAG_TREAT_BIT_AS_CHAR;
3240 def->create_length_to_internal_length_bit();
3241 return false;
3242 }
3243
3244
3245 /*************************************************************************/
3246
3247 bool Type_handler::
Column_definition_prepare_stage2_legacy(Column_definition * def,enum_field_types type) const3248 Column_definition_prepare_stage2_legacy(Column_definition *def,
3249 enum_field_types type) const
3250 {
3251 def->pack_flag= f_settype((uint) type);
3252 return false;
3253 }
3254
3255 bool Type_handler::
Column_definition_prepare_stage2_legacy_num(Column_definition * def,enum_field_types type) const3256 Column_definition_prepare_stage2_legacy_num(Column_definition *def,
3257 enum_field_types type) const
3258 {
3259 def->pack_flag= def->pack_flag_numeric() | f_settype((uint) type);
3260 return false;
3261 }
3262
3263 bool Type_handler::
Column_definition_prepare_stage2_legacy_real(Column_definition * def,enum_field_types type) const3264 Column_definition_prepare_stage2_legacy_real(Column_definition *def,
3265 enum_field_types type) const
3266 {
3267 uint dec= def->decimals;
3268 /*
3269 User specified FLOAT() or DOUBLE() without precision. Change to
3270 FLOATING_POINT_DECIMALS to keep things compatible with earlier MariaDB
3271 versions.
3272 */
3273 if (dec >= FLOATING_POINT_DECIMALS)
3274 dec= FLOATING_POINT_DECIMALS;
3275 def->decimals= dec;
3276 def->pack_flag= def->pack_flag_numeric() | f_settype((uint) type);
3277 return false;
3278 }
3279
3280 bool Type_handler_newdecimal::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const3281 Column_definition_prepare_stage2(Column_definition *def,
3282 handler *file,
3283 ulonglong table_flags) const
3284 {
3285 def->pack_flag= def->pack_flag_numeric();
3286 return false;
3287 }
3288
3289 bool Type_handler_blob_common::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const3290 Column_definition_prepare_stage2(Column_definition *def,
3291 handler *file,
3292 ulonglong table_flags) const
3293 {
3294 return def->prepare_stage2_blob(file, table_flags, FIELDFLAG_BLOB);
3295 }
3296
3297 bool Type_handler_varchar::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const3298 Column_definition_prepare_stage2(Column_definition *def,
3299 handler *file,
3300 ulonglong table_flags) const
3301 {
3302 return def->prepare_stage2_varchar(table_flags);
3303 }
3304
3305 bool Type_handler_string::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const3306 Column_definition_prepare_stage2(Column_definition *def,
3307 handler *file,
3308 ulonglong table_flags) const
3309 {
3310 def->pack_flag= (def->charset->state & MY_CS_BINSORT) ? FIELDFLAG_BINARY : 0;
3311 return false;
3312 }
3313
3314 bool Type_handler_enum::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const3315 Column_definition_prepare_stage2(Column_definition *def,
3316 handler *file,
3317 ulonglong table_flags) const
3318 {
3319 uint dummy;
3320 return def->prepare_stage2_typelib("ENUM", FIELDFLAG_INTERVAL, &dummy);
3321 }
3322
3323 bool Type_handler_set::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const3324 Column_definition_prepare_stage2(Column_definition *def,
3325 handler *file,
3326 ulonglong table_flags) const
3327 {
3328 uint dup_count;
3329 if (def->prepare_stage2_typelib("SET", FIELDFLAG_BITFIELD, &dup_count))
3330 return true;
3331 /* Check that count of unique members is not more then 64 */
3332 if (def->interval->count - dup_count > sizeof(longlong)*8)
3333 {
3334 my_error(ER_TOO_BIG_SET, MYF(0), def->field_name.str);
3335 return true;
3336 }
3337 return false;
3338 }
3339
3340 bool Type_handler_bit::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const3341 Column_definition_prepare_stage2(Column_definition *def,
3342 handler *file,
3343 ulonglong table_flags) const
3344 {
3345 /*
3346 We have sql_field->pack_flag already set here, see
3347 mysql_prepare_create_table().
3348 */
3349 return false;
3350 }
3351
3352
3353 /*************************************************************************/
Key_part_spec_init_primary(Key_part_spec * part,const Column_definition & def,const handler * file) const3354 bool Type_handler::Key_part_spec_init_primary(Key_part_spec *part,
3355 const Column_definition &def,
3356 const handler *file) const
3357 {
3358 part->length*= def.charset->mbmaxlen;
3359 return false;
3360 }
3361
3362
Key_part_spec_init_unique(Key_part_spec * part,const Column_definition & def,const handler * file,bool * has_field_needed) const3363 bool Type_handler::Key_part_spec_init_unique(Key_part_spec *part,
3364 const Column_definition &def,
3365 const handler *file,
3366 bool *has_field_needed) const
3367 {
3368 part->length*= def.charset->mbmaxlen;
3369 return false;
3370 }
3371
3372
Key_part_spec_init_multiple(Key_part_spec * part,const Column_definition & def,const handler * file) const3373 bool Type_handler::Key_part_spec_init_multiple(Key_part_spec *part,
3374 const Column_definition &def,
3375 const handler *file) const
3376 {
3377 part->length*= def.charset->mbmaxlen;
3378 return false;
3379 }
3380
3381
Key_part_spec_init_foreign(Key_part_spec * part,const Column_definition & def,const handler * file) const3382 bool Type_handler::Key_part_spec_init_foreign(Key_part_spec *part,
3383 const Column_definition &def,
3384 const handler *file) const
3385 {
3386 part->length*= def.charset->mbmaxlen;
3387 return false;
3388 }
3389
3390
Key_part_spec_init_spatial(Key_part_spec * part,const Column_definition & def) const3391 bool Type_handler::Key_part_spec_init_spatial(Key_part_spec *part,
3392 const Column_definition &def)
3393 const
3394 {
3395 my_error(ER_WRONG_ARGUMENTS, MYF(0), "SPATIAL INDEX");
3396 return true;
3397 }
3398
3399
Key_part_spec_init_primary(Key_part_spec * part,const Column_definition & def,const handler * file) const3400 bool Type_handler_blob_common::Key_part_spec_init_primary(Key_part_spec *part,
3401 const Column_definition &def,
3402 const handler *file) const
3403 {
3404 part->length*= def.charset->mbmaxlen;
3405 return part->check_primary_key_for_blob(file);
3406 }
3407
3408
Key_part_spec_init_unique(Key_part_spec * part,const Column_definition & def,const handler * file,bool * hash_field_needed) const3409 bool Type_handler_blob_common::Key_part_spec_init_unique(Key_part_spec *part,
3410 const Column_definition &def,
3411 const handler *file,
3412 bool *hash_field_needed) const
3413 {
3414 if (!(part->length*= def.charset->mbmaxlen))
3415 *hash_field_needed= true;
3416 return part->check_key_for_blob(file);
3417 }
3418
3419
Key_part_spec_init_multiple(Key_part_spec * part,const Column_definition & def,const handler * file) const3420 bool Type_handler_blob_common::Key_part_spec_init_multiple(Key_part_spec *part,
3421 const Column_definition &def,
3422 const handler *file) const
3423 {
3424 part->length*= def.charset->mbmaxlen;
3425 return part->init_multiple_key_for_blob(file);
3426 }
3427
3428
Key_part_spec_init_foreign(Key_part_spec * part,const Column_definition & def,const handler * file) const3429 bool Type_handler_blob_common::Key_part_spec_init_foreign(Key_part_spec *part,
3430 const Column_definition &def,
3431 const handler *file) const
3432 {
3433 part->length*= def.charset->mbmaxlen;
3434 return part->check_foreign_key_for_blob(file);
3435 }
3436
3437
3438
3439 /*************************************************************************/
3440
calc_pack_length(uint32 length) const3441 uint32 Type_handler_time::calc_pack_length(uint32 length) const
3442 {
3443 return length > MIN_TIME_WIDTH ?
3444 hires_bytes(length - 1 - MIN_TIME_WIDTH) : 3;
3445 }
3446
calc_pack_length(uint32 length) const3447 uint32 Type_handler_time2::calc_pack_length(uint32 length) const
3448 {
3449 return length > MIN_TIME_WIDTH ?
3450 my_time_binary_length(length - MIN_TIME_WIDTH - 1) : 3;
3451 }
3452
calc_pack_length(uint32 length) const3453 uint32 Type_handler_timestamp::calc_pack_length(uint32 length) const
3454 {
3455 return length > MAX_DATETIME_WIDTH ?
3456 4 + sec_part_bytes(length - 1 - MAX_DATETIME_WIDTH) : 4;
3457 }
3458
calc_pack_length(uint32 length) const3459 uint32 Type_handler_timestamp2::calc_pack_length(uint32 length) const
3460 {
3461 return length > MAX_DATETIME_WIDTH ?
3462 my_timestamp_binary_length(length - MAX_DATETIME_WIDTH - 1) : 4;
3463 }
3464
calc_pack_length(uint32 length) const3465 uint32 Type_handler_datetime::calc_pack_length(uint32 length) const
3466 {
3467 return length > MAX_DATETIME_WIDTH ?
3468 hires_bytes(length - 1 - MAX_DATETIME_WIDTH) : 8;
3469 }
3470
calc_pack_length(uint32 length) const3471 uint32 Type_handler_datetime2::calc_pack_length(uint32 length) const
3472 {
3473 return length > MAX_DATETIME_WIDTH ?
3474 my_datetime_binary_length(length - MAX_DATETIME_WIDTH - 1) : 5;
3475 }
3476
calc_pack_length(uint32 length) const3477 uint32 Type_handler_tiny_blob::calc_pack_length(uint32 length) const
3478 {
3479 return 1 + portable_sizeof_char_ptr;
3480 }
3481
calc_pack_length(uint32 length) const3482 uint32 Type_handler_blob::calc_pack_length(uint32 length) const
3483 {
3484 return 2 + portable_sizeof_char_ptr;
3485 }
3486
calc_pack_length(uint32 length) const3487 uint32 Type_handler_medium_blob::calc_pack_length(uint32 length) const
3488 {
3489 return 3 + portable_sizeof_char_ptr;
3490 }
3491
calc_pack_length(uint32 length) const3492 uint32 Type_handler_long_blob::calc_pack_length(uint32 length) const
3493 {
3494 return 4 + portable_sizeof_char_ptr;
3495 }
3496
calc_pack_length(uint32 length) const3497 uint32 Type_handler_newdecimal::calc_pack_length(uint32 length) const
3498 {
3499 abort(); // This shouldn't happen
3500 return 0;
3501 }
3502
calc_pack_length(uint32 length) const3503 uint32 Type_handler_set::calc_pack_length(uint32 length) const
3504 {
3505 abort(); // This shouldn't happen
3506 return 0;
3507 }
3508
calc_pack_length(uint32 length) const3509 uint32 Type_handler_enum::calc_pack_length(uint32 length) const
3510 {
3511 abort(); // This shouldn't happen
3512 return 0;
3513 }
3514
3515
3516 /*************************************************************************/
calc_key_length(const Column_definition & def) const3517 uint Type_handler::calc_key_length(const Column_definition &def) const
3518 {
3519 DBUG_ASSERT(def.pack_length == calc_pack_length((uint32) def.length));
3520 return def.pack_length;
3521 }
3522
calc_key_length(const Column_definition & def) const3523 uint Type_handler_bit::calc_key_length(const Column_definition &def) const
3524 {
3525 if (f_bit_as_char(def.pack_flag))
3526 return def.pack_length;
3527 /* We need one extra byte to store the bits we save among the null bits */
3528 return def.pack_length + MY_TEST(def.length & 7);
3529 }
3530
calc_key_length(const Column_definition & def) const3531 uint Type_handler_newdecimal::calc_key_length(const Column_definition &def) const
3532 {
3533 return def.pack_length;
3534 }
3535
3536 uint
calc_key_length(const Column_definition & def) const3537 Type_handler_string_result::calc_key_length(const Column_definition &def) const
3538 {
3539 return (uint) def.length;
3540 }
3541
calc_key_length(const Column_definition & def) const3542 uint Type_handler_enum::calc_key_length(const Column_definition &def) const
3543 {
3544 DBUG_ASSERT(def.interval);
3545 return get_enum_pack_length(def.interval->count);
3546 }
3547
calc_key_length(const Column_definition & def) const3548 uint Type_handler_set::calc_key_length(const Column_definition &def) const
3549 {
3550 DBUG_ASSERT(def.interval);
3551 return get_set_pack_length(def.interval->count);
3552 }
3553
calc_key_length(const Column_definition & def) const3554 uint Type_handler_blob_common::calc_key_length(const Column_definition &def) const
3555 {
3556 return 0;
3557 }
3558
3559 /*************************************************************************/
make_and_init_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const3560 Field *Type_handler::make_and_init_table_field(MEM_ROOT *root,
3561 const LEX_CSTRING *name,
3562 const Record_addr &addr,
3563 const Type_all_attributes &attr,
3564 TABLE *table) const
3565 {
3566 Field *field= make_table_field(root, name, addr, attr, table->s);
3567 if (field)
3568 field->init(table);
3569 return field;
3570 }
3571
3572
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3573 Field *Type_handler_int_result::make_table_field(MEM_ROOT *root,
3574 const LEX_CSTRING *name,
3575 const Record_addr &addr,
3576 const Type_all_attributes &attr,
3577 TABLE_SHARE *share) const
3578 {
3579 DBUG_ASSERT(is_unsigned() == attr.unsigned_flag);
3580 Column_definition_attributes dattr(attr);
3581 return make_table_field_from_def(share, root, name, addr,
3582 Bit_addr(), &dattr, 0);
3583 }
3584
3585
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3586 Field *Type_handler_vers_trx_id::make_table_field(MEM_ROOT *root,
3587 const LEX_CSTRING *name,
3588 const Record_addr &addr,
3589 const Type_all_attributes &attr,
3590 TABLE_SHARE *share) const
3591 {
3592 DBUG_ASSERT(is_unsigned() == attr.unsigned_flag);
3593 return new (root)
3594 Field_vers_trx_id(addr.ptr(), attr.max_char_length(),
3595 addr.null_ptr(), addr.null_bit(),
3596 Field::NONE, name,
3597 0/*zerofill*/, attr.unsigned_flag);
3598 }
3599
3600
3601 Field *
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3602 Type_handler_real_result::make_table_field(MEM_ROOT *root,
3603 const LEX_CSTRING *name,
3604 const Record_addr &addr,
3605 const Type_all_attributes &attr,
3606 TABLE_SHARE *share) const
3607 {
3608 Column_definition_attributes dattr(attr);
3609 return make_table_field_from_def(share, root, name, addr,
3610 Bit_addr(), &dattr, 0);
3611 }
3612
3613
3614 Field *
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3615 Type_handler_olddecimal::make_table_field(MEM_ROOT *root,
3616 const LEX_CSTRING *name,
3617 const Record_addr &addr,
3618 const Type_all_attributes &attr,
3619 TABLE_SHARE *share) const
3620 {
3621 /*
3622 Currently make_table_field() is used for Item purpose only.
3623 On Item level we have type_handler_newdecimal only.
3624 For now we have DBUG_ASSERT(0).
3625 It will be removed when we reuse Type_handler::make_table_field()
3626 in make_field() in field.cc, to open old tables with old decimal.
3627 */
3628 DBUG_ASSERT(0);
3629 Column_definition_attributes dattr(attr);
3630 return make_table_field_from_def(share, root, name, addr,
3631 Bit_addr(), &dattr, 0);
3632 }
3633
3634
3635 Field *
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3636 Type_handler_newdecimal::make_table_field(MEM_ROOT *root,
3637 const LEX_CSTRING *name,
3638 const Record_addr &addr,
3639 const Type_all_attributes &attr,
3640 TABLE_SHARE *share) const
3641 {
3642 uint8 dec= (uint8) attr.decimals;
3643 uint8 intg= (uint8) (attr.decimal_precision() - dec);
3644 uint32 len= attr.max_char_length();
3645
3646 /*
3647 Trying to put too many digits overall in a DECIMAL(prec,dec)
3648 will always throw a warning. We must limit dec to
3649 DECIMAL_MAX_SCALE however to prevent an assert() later.
3650 */
3651
3652 if (dec > 0)
3653 {
3654 signed int overflow;
3655
3656 dec= MY_MIN(dec, DECIMAL_MAX_SCALE);
3657
3658 /*
3659 If the value still overflows the field with the corrected dec,
3660 we'll throw out decimals rather than integers. This is still
3661 bad and of course throws a truncation warning.
3662 +1: for decimal point
3663 */
3664
3665 const int required_length=
3666 my_decimal_precision_to_length(intg + dec, dec, attr.unsigned_flag);
3667
3668 overflow= required_length - len;
3669
3670 if (overflow > 0)
3671 dec= MY_MAX(0, dec - overflow); // too long, discard fract
3672 else
3673 /* Corrected value fits. */
3674 len= required_length;
3675 }
3676 return new (root)
3677 Field_new_decimal(addr.ptr(), len, addr.null_ptr(), addr.null_bit(),
3678 Field::NONE, name,
3679 dec, 0/*zerofill*/, attr.unsigned_flag);
3680 }
3681
3682
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3683 Field *Type_handler_null::make_table_field(MEM_ROOT *root,
3684 const LEX_CSTRING *name,
3685 const Record_addr &addr,
3686 const Type_all_attributes &attr,
3687 TABLE_SHARE *share) const
3688
3689 {
3690 return new (root)
3691 Field_null(addr.ptr(), attr.max_length,
3692 Field::NONE, name, attr.collation.collation);
3693 }
3694
3695
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3696 Field *Type_handler_timestamp::make_table_field(MEM_ROOT *root,
3697 const LEX_CSTRING *name,
3698 const Record_addr &addr,
3699 const Type_all_attributes &attr,
3700 TABLE_SHARE *share) const
3701
3702 {
3703 return new_Field_timestamp(root,
3704 addr.ptr(), addr.null_ptr(), addr.null_bit(),
3705 Field::NONE, name, share, attr.decimals);
3706 }
3707
3708
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3709 Field *Type_handler_timestamp2::make_table_field(MEM_ROOT *root,
3710 const LEX_CSTRING *name,
3711 const Record_addr &addr,
3712 const Type_all_attributes &attr,
3713 TABLE_SHARE *share) const
3714
3715 {
3716 /*
3717 Will be changed to "new Field_timestampf" when we reuse
3718 make_table_field() for make_field() purposes in field.cc.
3719 */
3720 return new_Field_timestamp(root,
3721 addr.ptr(), addr.null_ptr(), addr.null_bit(),
3722 Field::NONE, name, share, attr.decimals);
3723 }
3724
3725
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3726 Field *Type_handler_newdate::make_table_field(MEM_ROOT *root,
3727 const LEX_CSTRING *name,
3728 const Record_addr &addr,
3729 const Type_all_attributes &attr,
3730 TABLE_SHARE *share) const
3731
3732 {
3733 return new (root)
3734 Field_newdate(addr.ptr(), addr.null_ptr(), addr.null_bit(),
3735 Field::NONE, name);
3736 }
3737
3738
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3739 Field *Type_handler_date::make_table_field(MEM_ROOT *root,
3740 const LEX_CSTRING *name,
3741 const Record_addr &addr,
3742 const Type_all_attributes &attr,
3743 TABLE_SHARE *share) const
3744
3745 {
3746 /*
3747 DBUG_ASSERT will be removed when we reuse make_table_field()
3748 for make_field() in field.cc
3749 */
3750 DBUG_ASSERT(0);
3751 return new (root)
3752 Field_date(addr.ptr(), addr.null_ptr(), addr.null_bit(),
3753 Field::NONE, name);
3754 }
3755
3756
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3757 Field *Type_handler_time::make_table_field(MEM_ROOT *root,
3758 const LEX_CSTRING *name,
3759 const Record_addr &addr,
3760 const Type_all_attributes &attr,
3761 TABLE_SHARE *share) const
3762
3763 {
3764 return new_Field_time(root,
3765 addr.ptr(), addr.null_ptr(), addr.null_bit(),
3766 Field::NONE, name, attr.decimals);
3767 }
3768
3769
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3770 Field *Type_handler_time2::make_table_field(MEM_ROOT *root,
3771 const LEX_CSTRING *name,
3772 const Record_addr &addr,
3773 const Type_all_attributes &attr,
3774 TABLE_SHARE *share) const
3775
3776
3777 {
3778 /*
3779 Will be changed to "new Field_timef" when we reuse
3780 make_table_field() for make_field() purposes in field.cc.
3781 */
3782 return new_Field_time(root,
3783 addr.ptr(), addr.null_ptr(), addr.null_bit(),
3784 Field::NONE, name, attr.decimals);
3785 }
3786
3787
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3788 Field *Type_handler_datetime::make_table_field(MEM_ROOT *root,
3789 const LEX_CSTRING *name,
3790 const Record_addr &addr,
3791 const Type_all_attributes &attr,
3792 TABLE_SHARE *share) const
3793
3794 {
3795 return new_Field_datetime(root,
3796 addr.ptr(), addr.null_ptr(), addr.null_bit(),
3797 Field::NONE, name, attr.decimals);
3798 }
3799
3800
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3801 Field *Type_handler_datetime2::make_table_field(MEM_ROOT *root,
3802 const LEX_CSTRING *name,
3803 const Record_addr &addr,
3804 const Type_all_attributes &attr,
3805 TABLE_SHARE *share) const
3806 {
3807 /*
3808 Will be changed to "new Field_datetimef" when we reuse
3809 make_table_field() for make_field() purposes in field.cc.
3810 */
3811 return new_Field_datetime(root,
3812 addr.ptr(), addr.null_ptr(), addr.null_bit(),
3813 Field::NONE, name, attr.decimals);
3814 }
3815
3816
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3817 Field *Type_handler_bit::make_table_field(MEM_ROOT *root,
3818 const LEX_CSTRING *name,
3819 const Record_addr &addr,
3820 const Type_all_attributes &attr,
3821 TABLE_SHARE *share) const
3822
3823 {
3824 return new (root)
3825 Field_bit_as_char(addr.ptr(), attr.max_length,
3826 addr.null_ptr(), addr.null_bit(),
3827 Field::NONE, name);
3828 }
3829
3830
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3831 Field *Type_handler_string::make_table_field(MEM_ROOT *root,
3832 const LEX_CSTRING *name,
3833 const Record_addr &addr,
3834 const Type_all_attributes &attr,
3835 TABLE_SHARE *share) const
3836
3837 {
3838 return new (root)
3839 Field_string(addr.ptr(), attr.max_length,
3840 addr.null_ptr(), addr.null_bit(),
3841 Field::NONE, name, attr.collation);
3842 }
3843
3844
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3845 Field *Type_handler_varchar::make_table_field(MEM_ROOT *root,
3846 const LEX_CSTRING *name,
3847 const Record_addr &addr,
3848 const Type_all_attributes &attr,
3849 TABLE_SHARE *share) const
3850
3851 {
3852 DBUG_ASSERT(HA_VARCHAR_PACKLENGTH(attr.max_length) <=
3853 MAX_FIELD_VARCHARLENGTH);
3854 return new (root)
3855 Field_varstring(addr.ptr(), attr.max_length,
3856 HA_VARCHAR_PACKLENGTH(attr.max_length),
3857 addr.null_ptr(), addr.null_bit(),
3858 Field::NONE, name,
3859 share, attr.collation);
3860 }
3861
3862
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3863 Field *Type_handler_tiny_blob::make_table_field(MEM_ROOT *root,
3864 const LEX_CSTRING *name,
3865 const Record_addr &addr,
3866 const Type_all_attributes &attr,
3867 TABLE_SHARE *share) const
3868
3869 {
3870 return new (root)
3871 Field_blob(addr.ptr(), addr.null_ptr(), addr.null_bit(),
3872 Field::NONE, name, share,
3873 1, attr.collation);
3874 }
3875
3876
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3877 Field *Type_handler_blob::make_table_field(MEM_ROOT *root,
3878 const LEX_CSTRING *name,
3879 const Record_addr &addr,
3880 const Type_all_attributes &attr,
3881 TABLE_SHARE *share) const
3882
3883 {
3884 return new (root)
3885 Field_blob(addr.ptr(), addr.null_ptr(), addr.null_bit(),
3886 Field::NONE, name, share,
3887 2, attr.collation);
3888 }
3889
3890
3891 Field *
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3892 Type_handler_medium_blob::make_table_field(MEM_ROOT *root,
3893 const LEX_CSTRING *name,
3894 const Record_addr &addr,
3895 const Type_all_attributes &attr,
3896 TABLE_SHARE *share) const
3897
3898 {
3899 return new (root)
3900 Field_blob(addr.ptr(), addr.null_ptr(), addr.null_bit(),
3901 Field::NONE, name, share,
3902 3, attr.collation);
3903 }
3904
3905
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3906 Field *Type_handler_long_blob::make_table_field(MEM_ROOT *root,
3907 const LEX_CSTRING *name,
3908 const Record_addr &addr,
3909 const Type_all_attributes &attr,
3910 TABLE_SHARE *share) const
3911
3912 {
3913 return new (root)
3914 Field_blob(addr.ptr(), addr.null_ptr(), addr.null_bit(),
3915 Field::NONE, name, share,
3916 4, attr.collation);
3917 }
3918
3919
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3920 Field *Type_handler_enum::make_table_field(MEM_ROOT *root,
3921 const LEX_CSTRING *name,
3922 const Record_addr &addr,
3923 const Type_all_attributes &attr,
3924 TABLE_SHARE *share) const
3925 {
3926 const TYPELIB *typelib= attr.get_typelib();
3927 DBUG_ASSERT(typelib);
3928 return new (root)
3929 Field_enum(addr.ptr(), attr.max_length,
3930 addr.null_ptr(), addr.null_bit(),
3931 Field::NONE, name,
3932 get_enum_pack_length(typelib->count), typelib,
3933 attr.collation);
3934 }
3935
3936
make_table_field(MEM_ROOT * root,const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE_SHARE * share) const3937 Field *Type_handler_set::make_table_field(MEM_ROOT *root,
3938 const LEX_CSTRING *name,
3939 const Record_addr &addr,
3940 const Type_all_attributes &attr,
3941 TABLE_SHARE *share) const
3942
3943 {
3944 const TYPELIB *typelib= attr.get_typelib();
3945 DBUG_ASSERT(typelib);
3946 return new (root)
3947 Field_set(addr.ptr(), attr.max_length,
3948 addr.null_ptr(), addr.null_bit(),
3949 Field::NONE, name,
3950 get_enum_pack_length(typelib->count), typelib,
3951 attr.collation);
3952 }
3953
3954
3955 /*************************************************************************/
3956
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const3957 Field *Type_handler_float::make_schema_field(MEM_ROOT *root, TABLE *table,
3958 const Record_addr &addr,
3959 const ST_FIELD_INFO &def) const
3960 {
3961 LEX_CSTRING name= def.name();
3962 return new (root)
3963 Field_float(addr.ptr(), def.char_length(),
3964 addr.null_ptr(), addr.null_bit(),
3965 Field::NONE, &name,
3966 (uint8) NOT_FIXED_DEC,
3967 0/*zerofill*/, def.unsigned_flag());
3968 }
3969
3970
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const3971 Field *Type_handler_double::make_schema_field(MEM_ROOT *root, TABLE *table,
3972 const Record_addr &addr,
3973 const ST_FIELD_INFO &def) const
3974 {
3975 LEX_CSTRING name= def.name();
3976 return new (root)
3977 Field_double(addr.ptr(), def.char_length(),
3978 addr.null_ptr(), addr.null_bit(),
3979 Field::NONE, &name,
3980 (uint8) NOT_FIXED_DEC,
3981 0/*zerofill*/, def.unsigned_flag());
3982 }
3983
3984
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const3985 Field *Type_handler_decimal_result::make_schema_field(MEM_ROOT *root,
3986 TABLE *table,
3987 const Record_addr &addr,
3988 const ST_FIELD_INFO &def) const
3989 {
3990 LEX_CSTRING name= def.name();
3991 uint dec= def.decimal_scale();
3992 uint prec= def.decimal_precision();
3993 DBUG_ASSERT(dec <= DECIMAL_MAX_SCALE);
3994 uint32 len= my_decimal_precision_to_length(prec, dec, def.unsigned_flag());
3995 return new (root)
3996 Field_new_decimal(addr.ptr(), len, addr.null_ptr(), addr.null_bit(),
3997 Field::NONE, &name,
3998 (uint8) dec, 0/*zerofill*/, def.unsigned_flag());
3999 }
4000
4001
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4002 Field *Type_handler_blob_common::make_schema_field(MEM_ROOT *root, TABLE *table,
4003 const Record_addr &addr,
4004 const ST_FIELD_INFO &def) const
4005 {
4006 LEX_CSTRING name= def.name();
4007 return new (root)
4008 Field_blob(addr.ptr(), addr.null_ptr(), addr.null_bit(),
4009 Field::NONE, &name, table->s,
4010 length_bytes(),
4011 &my_charset_bin);
4012 }
4013
4014
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4015 Field *Type_handler_varchar::make_schema_field(MEM_ROOT *root, TABLE *table,
4016 const Record_addr &addr,
4017 const ST_FIELD_INFO &def) const
4018 {
4019 DBUG_ASSERT(def.char_length());
4020 LEX_CSTRING name= def.name();
4021 uint32 octet_length= (uint32) def.char_length() * 3;
4022 if (octet_length > MAX_FIELD_VARCHARLENGTH)
4023 {
4024 Field *field= new (root)
4025 Field_blob(addr.ptr(), addr.null_ptr(), addr.null_bit(), Field::NONE,
4026 &name, table->s, 4, system_charset_info);
4027 if (field)
4028 field->field_length= octet_length;
4029 return field;
4030 }
4031 else
4032 {
4033 return new (root)
4034 Field_varstring(addr.ptr(), octet_length,
4035 HA_VARCHAR_PACKLENGTH(octet_length),
4036 addr.null_ptr(), addr.null_bit(),
4037 Field::NONE, &name,
4038 table->s, system_charset_info);
4039 }
4040 }
4041
4042
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4043 Field *Type_handler_tiny::make_schema_field(MEM_ROOT *root, TABLE *table,
4044 const Record_addr &addr,
4045 const ST_FIELD_INFO &def) const
4046 {
4047 LEX_CSTRING name= def.name();
4048 return new (root)
4049 Field_tiny(addr.ptr(), def.char_length(),
4050 addr.null_ptr(), addr.null_bit(), Field::NONE, &name,
4051 0/*zerofill*/, def.unsigned_flag());
4052 }
4053
4054
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4055 Field *Type_handler_short::make_schema_field(MEM_ROOT *root, TABLE *table,
4056 const Record_addr &addr,
4057 const ST_FIELD_INFO &def) const
4058 {
4059 LEX_CSTRING name= def.name();
4060 return new (root)
4061 Field_short(addr.ptr(), def.char_length(),
4062 addr.null_ptr(), addr.null_bit(), Field::NONE, &name,
4063 0/*zerofill*/, def.unsigned_flag());
4064 }
4065
4066
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4067 Field *Type_handler_long::make_schema_field(MEM_ROOT *root, TABLE *table,
4068 const Record_addr &addr,
4069 const ST_FIELD_INFO &def) const
4070 {
4071 LEX_CSTRING name= def.name();
4072 return new (root)
4073 Field_long(addr.ptr(), def.char_length(),
4074 addr.null_ptr(), addr.null_bit(), Field::NONE, &name,
4075 0/*zerofill*/, def.unsigned_flag());
4076 }
4077
4078
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4079 Field *Type_handler_longlong::make_schema_field(MEM_ROOT *root, TABLE *table,
4080 const Record_addr &addr,
4081 const ST_FIELD_INFO &def) const
4082 {
4083 LEX_CSTRING name= def.name();
4084 return new (root)
4085 Field_longlong(addr.ptr(), def.char_length(),
4086 addr.null_ptr(), addr.null_bit(), Field::NONE, &name,
4087 0/*zerofill*/, def.unsigned_flag());
4088 }
4089
4090
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4091 Field *Type_handler_date_common::make_schema_field(MEM_ROOT *root, TABLE *table,
4092 const Record_addr &addr,
4093 const ST_FIELD_INFO &def) const
4094 {
4095 LEX_CSTRING name= def.name();
4096 return new (root)
4097 Field_newdate(addr.ptr(), addr.null_ptr(), addr.null_bit(),
4098 Field::NONE, &name);
4099 }
4100
4101
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4102 Field *Type_handler_time_common::make_schema_field(MEM_ROOT *root, TABLE *table,
4103 const Record_addr &addr,
4104 const ST_FIELD_INFO &def) const
4105 {
4106 LEX_CSTRING name= def.name();
4107 return new_Field_time(root,
4108 addr.ptr(), addr.null_ptr(), addr.null_bit(),
4109 Field::NONE, &name, def.fsp());
4110 }
4111
4112
make_schema_field(MEM_ROOT * root,TABLE * table,const Record_addr & addr,const ST_FIELD_INFO & def) const4113 Field *Type_handler_datetime_common::make_schema_field(MEM_ROOT *root,
4114 TABLE *table,
4115 const Record_addr &addr,
4116 const ST_FIELD_INFO &def) const
4117 {
4118 LEX_CSTRING name= def.name();
4119 return new (root) Field_datetimef(addr.ptr(),
4120 addr.null_ptr(), addr.null_bit(),
4121 Field::NONE, &name, def.fsp());
4122 }
4123
4124
4125 /*************************************************************************/
4126
4127 /*
4128 If length is not specified for a varchar parameter, set length to the
4129 maximum length of the actual argument. Goals are:
4130 - avoid to allocate too much unused memory for m_var_table
4131 - allow length check inside the callee rather than during copy of
4132 returned values in output variables.
4133 - allow varchar parameter size greater than 4000
4134 Default length has been stored in "decimal" member during parse.
4135 */
adjust_spparam_type(Spvar_definition * def,Item * from) const4136 bool Type_handler_varchar::adjust_spparam_type(Spvar_definition *def,
4137 Item *from) const
4138 {
4139 if (def->decimals)
4140 {
4141 uint def_max_char_length= MAX_FIELD_VARCHARLENGTH / def->charset->mbmaxlen;
4142 uint arg_max_length= from->max_char_length();
4143 set_if_smaller(arg_max_length, def_max_char_length);
4144 def->length= arg_max_length > 0 ? arg_max_length : def->decimals;
4145 def->create_length_to_internal_length_string();
4146 }
4147 return false;
4148 }
4149
4150 /*************************************************************************/
4151
max_display_length(const Item * item) const4152 uint32 Type_handler_decimal_result::max_display_length(const Item *item) const
4153 {
4154 return item->max_length;
4155 }
4156
4157
max_display_length(const Item * item) const4158 uint32 Type_handler_temporal_result::max_display_length(const Item *item) const
4159 {
4160 return item->max_length;
4161 }
4162
max_display_length(const Item * item) const4163 uint32 Type_handler_string_result::max_display_length(const Item *item) const
4164 {
4165 return item->max_length;
4166 }
4167
4168
max_display_length(const Item * item) const4169 uint32 Type_handler_year::max_display_length(const Item *item) const
4170 {
4171 return item->max_length;
4172 }
4173
4174
max_display_length(const Item * item) const4175 uint32 Type_handler_bit::max_display_length(const Item *item) const
4176 {
4177 return item->max_length;
4178 }
4179
4180 /*************************************************************************/
4181
4182 uint32
Item_decimal_notation_int_digits(const Item * item) const4183 Type_handler_decimal_result::Item_decimal_notation_int_digits(const Item *item)
4184 const
4185 {
4186 return item->decimal_int_part();
4187 }
4188
4189
4190 uint32
Item_decimal_notation_int_digits(const Item * item) const4191 Type_handler_temporal_result::Item_decimal_notation_int_digits(const Item *item)
4192 const
4193 {
4194 return item->decimal_int_part();
4195 }
4196
4197
4198 uint32
Item_decimal_notation_int_digits(const Item * item) const4199 Type_handler_bit::Item_decimal_notation_int_digits(const Item *item)
4200 const
4201 {
4202 return Bit_decimal_notation_int_digits_by_nbits(item->max_length);
4203 }
4204
4205
4206 uint32
Item_decimal_notation_int_digits(const Item * item) const4207 Type_handler_general_purpose_int::Item_decimal_notation_int_digits(
4208 const Item *item) const
4209 {
4210 return type_limits_int()->precision();
4211 }
4212
4213 /*************************************************************************/
4214
4215 /*
4216 Binary to Decimal digits ratio converges to log2(10) thus using 3 as
4217 a divisor.
4218 */
4219 uint32
Bit_decimal_notation_int_digits_by_nbits(uint nbits)4220 Type_handler_bit::Bit_decimal_notation_int_digits_by_nbits(uint nbits)
4221 {
4222 DBUG_ASSERT(nbits > 0);
4223 DBUG_ASSERT(nbits <= 64);
4224 set_if_smaller(nbits, 64); // Safety
4225 static uint ndigits[65]=
4226 {0,
4227 1,1,1,2,2,2,3,3, // 1..8 bits
4228 3,4,4,4,4,5,5,5, // 9..16 bits
4229 6,6,6,7,7,7,7,8, // 17..24 bits
4230 8,8,9,9,9,10,10,10, // 25..32 bits
4231 10,11,11,11,12,12,12,13, // 33..40 bits
4232 13,13,13,14,14,14,15,15, // 41..48 bits
4233 15,16,16,16,16,17,17,17, // 49..56 bits
4234 18,18,18,19,19,19,19,20 // 57..64 bits
4235 };
4236 return ndigits[nbits];
4237 }
4238
4239 /*************************************************************************/
4240
Item_update_null_value(Item * item) const4241 void Type_handler_row::Item_update_null_value(Item *item) const
4242 {
4243 DBUG_ASSERT(0);
4244 item->null_value= true;
4245 }
4246
4247
Item_update_null_value(Item * item) const4248 void Type_handler_time_common::Item_update_null_value(Item *item) const
4249 {
4250 MYSQL_TIME ltime;
4251 THD *thd= current_thd;
4252 (void) item->get_date(thd, <ime, Time::Options(TIME_TIME_ONLY, thd));
4253 }
4254
4255
Item_update_null_value(Item * item) const4256 void Type_handler_temporal_with_date::Item_update_null_value(Item *item) const
4257 {
4258 MYSQL_TIME ltime;
4259 THD *thd= current_thd;
4260 (void) item->get_date(thd, <ime, Datetime::Options(thd));
4261 }
4262
4263 bool
4264 Type_handler_timestamp_common::
Column_definition_set_attributes(THD * thd,Column_definition * def,const Lex_field_type_st & attr,CHARSET_INFO * cs,column_definition_type_t type) const4265 Column_definition_set_attributes(THD *thd,
4266 Column_definition *def,
4267 const Lex_field_type_st &attr,
4268 CHARSET_INFO *cs,
4269 column_definition_type_t type) const
4270 {
4271 Type_handler::Column_definition_set_attributes(thd, def, attr, cs, type);
4272 if (!opt_explicit_defaults_for_timestamp)
4273 def->flags|= NOT_NULL_FLAG;
4274 return false;
4275 }
4276
Item_update_null_value(Item * item) const4277 void Type_handler_string_result::Item_update_null_value(Item *item) const
4278 {
4279 StringBuffer<MAX_FIELD_WIDTH> tmp;
4280 (void) item->val_str(&tmp);
4281 }
4282
4283
Item_update_null_value(Item * item) const4284 void Type_handler_real_result::Item_update_null_value(Item *item) const
4285 {
4286 (void) item->val_real();
4287 }
4288
4289
Item_update_null_value(Item * item) const4290 void Type_handler_decimal_result::Item_update_null_value(Item *item) const
4291 {
4292 my_decimal tmp;
4293 (void) item->val_decimal(&tmp);
4294 }
4295
4296
Item_update_null_value(Item * item) const4297 void Type_handler_int_result::Item_update_null_value(Item *item) const
4298 {
4299 (void) item->val_int();
4300 }
4301
4302
Item_update_null_value(Item * item) const4303 void Type_handler_bool::Item_update_null_value(Item *item) const
4304 {
4305 (void) item->val_bool();
4306 }
4307
4308
4309 /*************************************************************************/
4310
Item_save_in_field(Item * item,Field * field,bool no_conversions) const4311 int Type_handler_time_common::Item_save_in_field(Item *item, Field *field,
4312 bool no_conversions) const
4313 {
4314 return item->save_time_in_field(field, no_conversions);
4315 }
4316
Item_save_in_field(Item * item,Field * field,bool no_conversions) const4317 int Type_handler_temporal_with_date::Item_save_in_field(Item *item,
4318 Field *field,
4319 bool no_conversions)
4320 const
4321 {
4322 return item->save_date_in_field(field, no_conversions);
4323 }
4324
4325
Item_save_in_field(Item * item,Field * field,bool no_conversions) const4326 int Type_handler_timestamp_common::Item_save_in_field(Item *item,
4327 Field *field,
4328 bool no_conversions)
4329 const
4330 {
4331 Timestamp_or_zero_datetime_native_null tmp(field->table->in_use, item, true);
4332 if (tmp.is_null())
4333 return set_field_to_null_with_conversions(field, no_conversions);
4334 return tmp.save_in_field(field, item->decimals);
4335 }
4336
4337
Item_save_in_field(Item * item,Field * field,bool no_conversions) const4338 int Type_handler_string_result::Item_save_in_field(Item *item, Field *field,
4339 bool no_conversions) const
4340 {
4341 return item->save_str_in_field(field, no_conversions);
4342 }
4343
4344
Item_save_in_field(Item * item,Field * field,bool no_conversions) const4345 int Type_handler_real_result::Item_save_in_field(Item *item, Field *field,
4346 bool no_conversions) const
4347 {
4348 return item->save_real_in_field(field, no_conversions);
4349 }
4350
4351
Item_save_in_field(Item * item,Field * field,bool no_conversions) const4352 int Type_handler_decimal_result::Item_save_in_field(Item *item, Field *field,
4353 bool no_conversions) const
4354 {
4355 return item->save_decimal_in_field(field, no_conversions);
4356 }
4357
4358
Item_save_in_field(Item * item,Field * field,bool no_conversions) const4359 int Type_handler_int_result::Item_save_in_field(Item *item, Field *field,
4360 bool no_conversions) const
4361 {
4362 return item->save_int_in_field(field, no_conversions);
4363 }
4364
4365
4366 /***********************************************************************/
4367
set_comparator_func(Arg_comparator * cmp) const4368 bool Type_handler_row::set_comparator_func(Arg_comparator *cmp) const
4369 {
4370 return cmp->set_cmp_func_row();
4371 }
4372
set_comparator_func(Arg_comparator * cmp) const4373 bool Type_handler_int_result::set_comparator_func(Arg_comparator *cmp) const
4374 {
4375 return cmp->set_cmp_func_int();
4376 }
4377
set_comparator_func(Arg_comparator * cmp) const4378 bool Type_handler_real_result::set_comparator_func(Arg_comparator *cmp) const
4379 {
4380 return cmp->set_cmp_func_real();
4381 }
4382
set_comparator_func(Arg_comparator * cmp) const4383 bool Type_handler_decimal_result::set_comparator_func(Arg_comparator *cmp) const
4384 {
4385 return cmp->set_cmp_func_decimal();
4386 }
4387
set_comparator_func(Arg_comparator * cmp) const4388 bool Type_handler_string_result::set_comparator_func(Arg_comparator *cmp) const
4389 {
4390 return cmp->set_cmp_func_string();
4391 }
4392
set_comparator_func(Arg_comparator * cmp) const4393 bool Type_handler_time_common::set_comparator_func(Arg_comparator *cmp) const
4394 {
4395 return cmp->set_cmp_func_time();
4396 }
4397
4398 bool
set_comparator_func(Arg_comparator * cmp) const4399 Type_handler_temporal_with_date::set_comparator_func(Arg_comparator *cmp) const
4400 {
4401 return cmp->set_cmp_func_datetime();
4402 }
4403
4404 bool
set_comparator_func(Arg_comparator * cmp) const4405 Type_handler_timestamp_common::set_comparator_func(Arg_comparator *cmp) const
4406 {
4407 return cmp->set_cmp_func_native();
4408 }
4409
4410
4411 /*************************************************************************/
4412
4413 bool Type_handler_temporal_result::
can_change_cond_ref_to_const(Item_bool_func2 * target,Item * target_expr,Item * target_value,Item_bool_func2 * source,Item * source_expr,Item * source_const) const4414 can_change_cond_ref_to_const(Item_bool_func2 *target,
4415 Item *target_expr, Item *target_value,
4416 Item_bool_func2 *source,
4417 Item *source_expr, Item *source_const)
4418 const
4419 {
4420 if (source->compare_type_handler()->cmp_type() != TIME_RESULT)
4421 return false;
4422
4423 /*
4424 Can't rewrite:
4425 WHERE COALESCE(time_column)='00:00:00'
4426 AND COALESCE(time_column)=DATE'2015-09-11'
4427 to
4428 WHERE DATE'2015-09-11'='00:00:00'
4429 AND COALESCE(time_column)=DATE'2015-09-11'
4430 because the left part will erroneously try to parse '00:00:00'
4431 as DATE, not as TIME.
4432
4433 TODO: It could still be rewritten to:
4434 WHERE DATE'2015-09-11'=TIME'00:00:00'
4435 AND COALESCE(time_column)=DATE'2015-09-11'
4436 i.e. we need to replace both target_expr and target_value
4437 at the same time. This is not supported yet.
4438 */
4439 return target_value->cmp_type() == TIME_RESULT;
4440 }
4441
4442
4443 bool Type_handler_string_result::
can_change_cond_ref_to_const(Item_bool_func2 * target,Item * target_expr,Item * target_value,Item_bool_func2 * source,Item * source_expr,Item * source_const) const4444 can_change_cond_ref_to_const(Item_bool_func2 *target,
4445 Item *target_expr, Item *target_value,
4446 Item_bool_func2 *source,
4447 Item *source_expr, Item *source_const)
4448 const
4449 {
4450 if (source->compare_type_handler()->cmp_type() != STRING_RESULT)
4451 return false;
4452 /*
4453 In this example:
4454 SET NAMES utf8 COLLATE utf8_german2_ci;
4455 DROP TABLE IF EXISTS t1;
4456 CREATE TABLE t1 (a CHAR(10) CHARACTER SET utf8);
4457 INSERT INTO t1 VALUES ('o-umlaut'),('oe');
4458 SELECT * FROM t1 WHERE a='oe' COLLATE utf8_german2_ci AND a='oe';
4459
4460 the query should return only the row with 'oe'.
4461 It should not return 'o-umlaut', because 'o-umlaut' does not match
4462 the right part of the condition: a='oe'
4463 ('o-umlaut' is not equal to 'oe' in utf8mb3_general_ci,
4464 which is the collation of the field "a").
4465
4466 If we change the right part from:
4467 ... AND a='oe'
4468 to
4469 ... AND 'oe' COLLATE utf8_german2_ci='oe'
4470 it will be evalulated to TRUE and removed from the condition,
4471 so the overall query will be simplified to:
4472
4473 SELECT * FROM t1 WHERE a='oe' COLLATE utf8_german2_ci;
4474
4475 which will erroneously start to return both 'oe' and 'o-umlaut'.
4476 So changing "expr" to "const" is not possible if the effective
4477 collations of "target" and "source" are not exactly the same.
4478
4479 Note, the code before the fix for MDEV-7152 only checked that
4480 collations of "source_const" and "target_value" are the same.
4481 This was not enough, as the bug report demonstrated.
4482 */
4483 return
4484 target->compare_collation() == source->compare_collation() &&
4485 target_value->collation.collation == source_const->collation.collation;
4486 }
4487
4488
4489 bool Type_handler_numeric::
can_change_cond_ref_to_const(Item_bool_func2 * target,Item * target_expr,Item * target_value,Item_bool_func2 * source,Item * source_expr,Item * source_const) const4490 can_change_cond_ref_to_const(Item_bool_func2 *target,
4491 Item *target_expr, Item *target_value,
4492 Item_bool_func2 *source,
4493 Item *source_expr, Item *source_const)
4494 const
4495 {
4496 /*
4497 The collations of "target" and "source" do not make sense for numeric
4498 data types.
4499 */
4500 return target->compare_type_handler() == source->compare_type_handler();
4501 }
4502
4503
4504 /*************************************************************************/
4505
4506 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4507 Type_handler_row::Item_get_cache(THD *thd, const Item *item) const
4508 {
4509 return new (thd->mem_root) Item_cache_row(thd);
4510 }
4511
4512 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4513 Type_handler_int_result::Item_get_cache(THD *thd, const Item *item) const
4514 {
4515 return new (thd->mem_root) Item_cache_int(thd, item->type_handler());
4516 }
4517
4518 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4519 Type_handler_year::Item_get_cache(THD *thd, const Item *item) const
4520 {
4521 return new (thd->mem_root) Item_cache_year(thd, item->type_handler());
4522 }
4523
4524 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4525 Type_handler_double::Item_get_cache(THD *thd, const Item *item) const
4526 {
4527 return new (thd->mem_root) Item_cache_double(thd);
4528 }
4529
4530 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4531 Type_handler_float::Item_get_cache(THD *thd, const Item *item) const
4532 {
4533 return new (thd->mem_root) Item_cache_float(thd);
4534 }
4535
4536 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4537 Type_handler_decimal_result::Item_get_cache(THD *thd, const Item *item) const
4538 {
4539 return new (thd->mem_root) Item_cache_decimal(thd);
4540 }
4541
4542 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4543 Type_handler_string_result::Item_get_cache(THD *thd, const Item *item) const
4544 {
4545 return new (thd->mem_root) Item_cache_str(thd, item);
4546 }
4547
4548 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4549 Type_handler_timestamp_common::Item_get_cache(THD *thd, const Item *item) const
4550 {
4551 return new (thd->mem_root) Item_cache_timestamp(thd);
4552 }
4553
4554 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4555 Type_handler_datetime_common::Item_get_cache(THD *thd, const Item *item) const
4556 {
4557 return new (thd->mem_root) Item_cache_datetime(thd);
4558 }
4559
4560 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4561 Type_handler_time_common::Item_get_cache(THD *thd, const Item *item) const
4562 {
4563 return new (thd->mem_root) Item_cache_time(thd);
4564 }
4565
4566 Item_cache *
Item_get_cache(THD * thd,const Item * item) const4567 Type_handler_date_common::Item_get_cache(THD *thd, const Item *item) const
4568 {
4569 return new (thd->mem_root) Item_cache_date(thd);
4570 }
4571
4572
4573 /*************************************************************************/
4574
4575 Item_copy *
create_item_copy(THD * thd,Item * item) const4576 Type_handler::create_item_copy(THD *thd, Item *item) const
4577 {
4578 return new (thd->mem_root) Item_copy_string(thd, item);
4579 }
4580
4581
4582 Item_copy *
create_item_copy(THD * thd,Item * item) const4583 Type_handler_timestamp_common::create_item_copy(THD *thd, Item *item) const
4584 {
4585 return new (thd->mem_root) Item_copy_timestamp(thd, item);
4586 }
4587
4588 /*************************************************************************/
4589
4590 bool Type_handler_int_result::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4591 Item_hybrid_func_fix_attributes(THD *thd,
4592 const char *func_name,
4593 Type_handler_hybrid_field_type *handler,
4594 Type_all_attributes *func,
4595 Item **items, uint nitems) const
4596 {
4597 bool unsigned_flag= items[0]->unsigned_flag;
4598 for (uint i= 1; i < nitems; i++)
4599 {
4600 if (unsigned_flag != items[i]->unsigned_flag)
4601 {
4602 // Convert a mixture of signed and unsigned int to decimal
4603 handler->set_handler(&type_handler_newdecimal);
4604 func->aggregate_attributes_decimal(items, nitems, false);
4605 return false;
4606 }
4607 }
4608 func->aggregate_attributes_int(items, nitems);
4609 handler->set_handler(func->unsigned_flag ?
4610 handler->type_handler()->type_handler_unsigned() :
4611 handler->type_handler()->type_handler_signed());
4612 return false;
4613 }
4614
4615
4616 bool Type_handler_real_result::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4617 Item_hybrid_func_fix_attributes(THD *thd,
4618 const char *func_name,
4619 Type_handler_hybrid_field_type *handler,
4620 Type_all_attributes *func,
4621 Item **items, uint nitems) const
4622 {
4623 func->aggregate_attributes_real(items, nitems);
4624 return false;
4625 }
4626
4627
4628 bool Type_handler_decimal_result::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4629 Item_hybrid_func_fix_attributes(THD *thd,
4630 const char *func_name,
4631 Type_handler_hybrid_field_type *handler,
4632 Type_all_attributes *func,
4633 Item **items, uint nitems) const
4634 {
4635 uint unsigned_count= func->count_unsigned(items, nitems);
4636 func->aggregate_attributes_decimal(items, nitems, unsigned_count == nitems);
4637 return false;
4638 }
4639
4640
4641 bool Type_handler_string_result::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4642 Item_hybrid_func_fix_attributes(THD *thd,
4643 const char *func_name,
4644 Type_handler_hybrid_field_type *handler,
4645 Type_all_attributes *func,
4646 Item **items, uint nitems) const
4647 {
4648 return func->aggregate_attributes_string(func_name, items, nitems);
4649 }
4650
4651
4652
4653 /*
4654 We can have enum/set type after merging only if we have one enum|set
4655 field (or MIN|MAX(enum|set field)) and number of NULL fields
4656 */
4657 bool Type_handler_typelib::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4658 Item_hybrid_func_fix_attributes(THD *thd,
4659 const char *func_name,
4660 Type_handler_hybrid_field_type *handler,
4661 Type_all_attributes *func,
4662 Item **items, uint nitems) const
4663 {
4664 const TYPELIB *typelib= NULL;
4665 for (uint i= 0; i < nitems; i++)
4666 {
4667 const TYPELIB *typelib2;
4668 if ((typelib2= items[i]->get_typelib()))
4669 {
4670 if (typelib)
4671 {
4672 /*
4673 Two ENUM/SET columns found. We convert such combinations to VARCHAR.
4674 This may change in the future to preserve ENUM/SET
4675 if typelib definitions are equal.
4676 */
4677 handler->set_handler(&type_handler_varchar);
4678 return func->aggregate_attributes_string(func_name, items, nitems);
4679 }
4680 typelib= typelib2;
4681 }
4682 }
4683 DBUG_ASSERT(typelib); // There must be at least one typelib
4684 func->set_typelib(typelib);
4685 return func->aggregate_attributes_string(func_name, items, nitems);
4686 }
4687
4688
4689 bool Type_handler_blob_common::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4690 Item_hybrid_func_fix_attributes(THD *thd,
4691 const char *func_name,
4692 Type_handler_hybrid_field_type *handler,
4693 Type_all_attributes *func,
4694 Item **items, uint nitems) const
4695 {
4696 if (func->aggregate_attributes_string(func_name, items, nitems))
4697 return true;
4698 handler->set_handler(blob_type_handler(func->max_length));
4699 return false;
4700 }
4701
4702
4703 bool Type_handler_date_common::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4704 Item_hybrid_func_fix_attributes(THD *thd,
4705 const char *func_name,
4706 Type_handler_hybrid_field_type *handler,
4707 Type_all_attributes *func,
4708 Item **items, uint nitems) const
4709 {
4710 func->fix_attributes_date();
4711 return false;
4712 }
4713
4714
4715 bool Type_handler_time_common::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4716 Item_hybrid_func_fix_attributes(THD *thd,
4717 const char *func_name,
4718 Type_handler_hybrid_field_type *handler,
4719 Type_all_attributes *func,
4720 Item **items, uint nitems) const
4721 {
4722 func->aggregate_attributes_temporal(MIN_TIME_WIDTH, items, nitems);
4723 return false;
4724 }
4725
4726
4727 bool Type_handler_datetime_common::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4728 Item_hybrid_func_fix_attributes(THD *thd,
4729 const char *func_name,
4730 Type_handler_hybrid_field_type *handler,
4731 Type_all_attributes *func,
4732 Item **items, uint nitems) const
4733 {
4734 func->aggregate_attributes_temporal(MAX_DATETIME_WIDTH, items, nitems);
4735 return false;
4736 }
4737
4738
4739 bool Type_handler_timestamp_common::
Item_hybrid_func_fix_attributes(THD * thd,const char * func_name,Type_handler_hybrid_field_type * handler,Type_all_attributes * func,Item ** items,uint nitems) const4740 Item_hybrid_func_fix_attributes(THD *thd,
4741 const char *func_name,
4742 Type_handler_hybrid_field_type *handler,
4743 Type_all_attributes *func,
4744 Item **items, uint nitems) const
4745 {
4746 func->aggregate_attributes_temporal(MAX_DATETIME_WIDTH, items, nitems);
4747 return false;
4748 }
4749
4750 /*************************************************************************/
4751
4752 bool Type_handler::
Item_func_min_max_fix_attributes(THD * thd,Item_func_min_max * func,Item ** items,uint nitems) const4753 Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
4754 Item **items, uint nitems) const
4755 {
4756 /*
4757 Aggregating attributes for LEAST/GREATES is exactly the same
4758 with aggregating for CASE-alike functions (e.g. COALESCE)
4759 for the majority of data type handlers.
4760 */
4761 return Item_hybrid_func_fix_attributes(thd, func->func_name(),
4762 func, func, items, nitems);
4763 }
4764
4765
4766 bool Type_handler_temporal_result::
Item_func_min_max_fix_attributes(THD * thd,Item_func_min_max * func,Item ** items,uint nitems) const4767 Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
4768 Item **items, uint nitems) const
4769 {
4770 DBUG_ASSERT(func->field_type() != MYSQL_TYPE_DATE);
4771 bool rc= Type_handler::Item_func_min_max_fix_attributes(thd, func,
4772 items, nitems);
4773 bool is_time= func->field_type() == MYSQL_TYPE_TIME;
4774 func->decimals= 0;
4775 for (uint i= 0; i < nitems; i++)
4776 {
4777 uint deci= is_time ? items[i]->time_precision(thd) :
4778 items[i]->datetime_precision(thd);
4779 set_if_bigger(func->decimals, deci);
4780 }
4781
4782 if (rc || func->maybe_null)
4783 return rc;
4784 /*
4785 LEAST/GREATES(non-temporal, temporal) can return NULL.
4786 CAST functions Item_{time|datetime|date}_typecast always set maybe_full
4787 to true. Here we try to detect nullability more thoroughly.
4788 Perhaps CAST functions should also reuse this idea eventually.
4789 */
4790 const Type_handler *hf= func->type_handler();
4791 for (uint i= 0; i < nitems; i++)
4792 {
4793 /*
4794 If items[i] does not need conversion to the current temporal data
4795 type, then we trust items[i]->maybe_null, which was already ORred
4796 to func->maybe_null in the argument loop in fix_fields().
4797 If items[i] requires conversion to the current temporal data type,
4798 then conversion can fail and return NULL even for NOT NULL items.
4799 */
4800 const Type_handler *ha= items[i]->type_handler();
4801 if (hf == ha)
4802 continue; // No conversion.
4803 if (ha->cmp_type() != TIME_RESULT)
4804 {
4805 func->maybe_null= true; // Conversion from non-temporal is not safe
4806 break;
4807 }
4808 timestamp_type tf= hf->mysql_timestamp_type();
4809 timestamp_type ta= ha->mysql_timestamp_type();
4810 if (tf == ta ||
4811 (tf == MYSQL_TIMESTAMP_DATETIME && ta == MYSQL_TIMESTAMP_DATE))
4812 {
4813 /*
4814 If handlers have the same mysql_timestamp_type(),
4815 then conversion is NULL safe. Conversion from DATE to DATETIME
4816 is also safe. This branch includes data type pairs:
4817 Function return type Argument type Comment
4818 -------------------- ------------- -------------
4819 TIMESTAMP TIMESTAMP no conversion
4820 TIMESTAMP DATETIME not possible
4821 TIMESTAMP DATE not possible
4822 DATETIME DATETIME no conversion
4823 DATETIME TIMESTAMP safe conversion
4824 DATETIME DATE safe conversion
4825 TIME TIME no conversion
4826
4827 Note, a function cannot return TIMESTAMP if it has non-TIMESTAMP
4828 arguments (it would return DATETIME in such case).
4829 */
4830 DBUG_ASSERT(hf->field_type() != MYSQL_TYPE_TIMESTAMP || tf == ta);
4831 continue;
4832 }
4833 /*
4834 Here we have the following data type pairs that did not match
4835 the condition above:
4836
4837 Function return type Argument type Comment
4838 -------------------- ------------- -------
4839 TIMESTAMP TIME Not possible
4840 DATETIME TIME depends on OLD_MODE_ZERO_DATE_TIME_CAST
4841 TIME TIMESTAMP Not possible
4842 TIME DATETIME Not possible
4843 TIME DATE Not possible
4844
4845 Most pairs are not possible, because the function data type
4846 would be DATETIME (according to LEAST/GREATEST aggregation rules).
4847 Conversion to DATETIME from TIME is not safe when
4848 OLD_MODE_ZERO_DATE_TIME_CAST is set:
4849 - negative TIME values cannot be converted to not-NULL DATETIME values
4850 - TIME values can produce DATETIME values that do not pass
4851 NO_ZERO_DATE and NO_ZERO_IN_DATE tests.
4852 */
4853 DBUG_ASSERT(hf->field_type() == MYSQL_TYPE_DATETIME);
4854 if (!(thd->variables.old_behavior & OLD_MODE_ZERO_DATE_TIME_CAST))
4855 continue;
4856 func->maybe_null= true;
4857 break;
4858 }
4859 return rc;
4860 }
4861
4862
4863 bool Type_handler_date_common::
Item_func_min_max_fix_attributes(THD * thd,Item_func_min_max * func,Item ** items,uint nitems) const4864 Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
4865 Item **items, uint nitems) const
4866 {
4867 func->fix_attributes_date();
4868 if (func->maybe_null)
4869 return false;
4870 /*
4871 We cannot trust the generic maybe_null value calculated during fix_fields().
4872 If a conversion from non-temoral types to DATE happens,
4873 then the result can be NULL (even if all arguments are not NULL).
4874 */
4875 for (uint i= 0; i < nitems; i++)
4876 {
4877 if (items[i]->type_handler()->cmp_type() != TIME_RESULT)
4878 {
4879 func->maybe_null= true;
4880 break;
4881 }
4882 }
4883 return false;
4884 }
4885
4886
4887 bool Type_handler_real_result::
Item_func_min_max_fix_attributes(THD * thd,Item_func_min_max * func,Item ** items,uint nitems) const4888 Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
4889 Item **items, uint nitems) const
4890 {
4891 /*
4892 DOUBLE is an exception and aggregates attributes differently
4893 for LEAST/GREATEST vs CASE-alike functions. See the comment in
4894 Item_func_min_max::aggregate_attributes_real().
4895 */
4896 func->aggregate_attributes_real(items, nitems);
4897 return false;
4898 }
4899
4900 /*************************************************************************/
4901
4902 bool Type_handler_int_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const4903 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
4904 {
4905 /*
4906 "this" is equal func->args[0]->type_handler() here, e.g. for MIN()/MAX().
4907 func->unsigned_flag is not reliably set yet.
4908 It will be set by the call below (copied from args[0]).
4909 */
4910 const Type_handler *h= is_unsigned()
4911 ? (Type_handler *)&type_handler_ulonglong
4912 : (Type_handler *)&type_handler_slonglong;
4913 return func->fix_length_and_dec_numeric(h);
4914 }
4915
4916
4917 bool Type_handler_bool::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const4918 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
4919 {
4920 return func->fix_length_and_dec_numeric(&type_handler_bool);
4921 }
4922
4923
4924 bool Type_handler_real_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const4925 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
4926 {
4927 (void) func->fix_length_and_dec_numeric(&type_handler_double);
4928 func->max_length= func->float_length(func->decimals);
4929 return false;
4930 }
4931
4932
4933 bool Type_handler_decimal_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const4934 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
4935 {
4936 return func->fix_length_and_dec_numeric(&type_handler_newdecimal);
4937 }
4938
4939
4940 bool Type_handler_string_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const4941 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
4942 {
4943 return func->fix_length_and_dec_string();
4944 }
4945
4946
4947 bool Type_handler_temporal_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const4948 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
4949 {
4950 return func->fix_length_and_dec_generic();
4951 }
4952
4953
4954 /*************************************************************************/
4955
4956 bool Type_handler_int_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const4957 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
4958 {
4959 item->fix_length_and_dec_decimal();
4960 return false;
4961 }
4962
4963
4964 bool Type_handler_decimal_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const4965 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
4966 {
4967 item->fix_length_and_dec_decimal();
4968 return false;
4969 }
4970
4971
4972 bool Type_handler_temporal_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const4973 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
4974 {
4975 item->fix_length_and_dec_decimal();
4976 return false;
4977 }
4978
4979
4980 bool Type_handler_real_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const4981 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
4982 {
4983 item->fix_length_and_dec_double();
4984 return false;
4985 }
4986
4987
4988 bool Type_handler_string_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const4989 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
4990 {
4991 item->fix_length_and_dec_double();
4992 return false;
4993 }
4994
4995
4996
4997
4998 /*************************************************************************/
4999
5000 bool Type_handler_int_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const5001 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
5002 {
5003 item->fix_length_and_dec_decimal();
5004 return false;
5005 }
5006
5007
5008 bool Type_handler_decimal_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const5009 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
5010 {
5011 item->fix_length_and_dec_decimal();
5012 return false;
5013 }
5014
5015
5016 bool Type_handler_temporal_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const5017 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
5018 {
5019 item->fix_length_and_dec_decimal();
5020 return false;
5021 }
5022
5023
5024 bool Type_handler_real_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const5025 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
5026 {
5027 item->fix_length_and_dec_double();
5028 return false;
5029 }
5030
5031
5032 bool Type_handler_string_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const5033 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
5034 {
5035 item->fix_length_and_dec_double();
5036 return false;
5037 }
5038
5039
5040
5041
5042 /*************************************************************************/
5043
5044 bool Type_handler_int_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const5045 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
5046 {
5047 item->fix_length_and_dec_decimal();
5048 return false;
5049 }
5050
5051
5052 bool Type_handler_decimal_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const5053 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
5054 {
5055 item->fix_length_and_dec_decimal();
5056 return false;
5057 }
5058
5059
5060 bool Type_handler_temporal_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const5061 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
5062 {
5063 item->fix_length_and_dec_decimal();
5064 return false;
5065 }
5066
5067
5068 bool Type_handler_real_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const5069 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
5070 {
5071 item->fix_length_and_dec_double();
5072 return false;
5073 }
5074
5075
5076 bool Type_handler_string_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const5077 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
5078 {
5079 item->fix_length_and_dec_double();
5080 return false;
5081 }
5082
5083
5084 /*************************************************************************/
5085
Item_val_bool(Item * item) const5086 bool Type_handler_real_result::Item_val_bool(Item *item) const
5087 {
5088 return item->val_real() != 0.0;
5089 }
5090
Item_val_bool(Item * item) const5091 bool Type_handler_int_result::Item_val_bool(Item *item) const
5092 {
5093 return item->val_int() != 0;
5094 }
5095
Item_val_bool(Item * item) const5096 bool Type_handler_temporal_result::Item_val_bool(Item *item) const
5097 {
5098 return item->val_real() != 0.0;
5099 }
5100
Item_val_bool(Item * item) const5101 bool Type_handler_string_result::Item_val_bool(Item *item) const
5102 {
5103 return item->val_real() != 0.0;
5104 }
5105
5106
5107 /*************************************************************************/
5108
5109
Item_get_date_with_warn(THD * thd,Item * item,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5110 bool Type_handler::Item_get_date_with_warn(THD *thd, Item *item,
5111 MYSQL_TIME *ltime,
5112 date_mode_t fuzzydate) const
5113 {
5114 const TABLE_SHARE *s= item->field_table_or_null();
5115 Temporal::Warn_push warn(thd, s ? s->db.str : nullptr,
5116 s ? s->table_name.str : nullptr,
5117 item->field_name_or_null(), ltime, fuzzydate);
5118 Item_get_date(thd, item, &warn, ltime, fuzzydate);
5119 return ltime->time_type < 0;
5120 }
5121
5122
Item_func_hybrid_field_type_get_date_with_warn(THD * thd,Item_func_hybrid_field_type * item,MYSQL_TIME * ltime,date_mode_t mode) const5123 bool Type_handler::Item_func_hybrid_field_type_get_date_with_warn(THD *thd,
5124 Item_func_hybrid_field_type *item,
5125 MYSQL_TIME *ltime,
5126 date_mode_t mode) const
5127 {
5128 const TABLE_SHARE *s= item->field_table_or_null();
5129 Temporal::Warn_push warn(thd, s ? s->db.str : nullptr,
5130 s ? s->table_name.str : nullptr,
5131 item->field_name_or_null(), ltime, mode);
5132 Item_func_hybrid_field_type_get_date(thd, item, &warn, ltime, mode);
5133 return ltime->time_type < 0;
5134 }
5135
5136
5137 /************************************************************************/
Item_get_date(THD * thd,Item * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5138 void Type_handler_decimal_result::Item_get_date(THD *thd, Item *item,
5139 Temporal::Warn *warn,
5140 MYSQL_TIME *ltime,
5141 date_mode_t fuzzydate) const
5142 {
5143 new(ltime) Temporal_hybrid(thd, warn, VDec(item).ptr(), fuzzydate);
5144 }
5145
5146
Item_get_date(THD * thd,Item * item,Temporal::Warn * warn,MYSQL_TIME * to,date_mode_t mode) const5147 void Type_handler_int_result::Item_get_date(THD *thd, Item *item,
5148 Temporal::Warn *warn,
5149 MYSQL_TIME *to,
5150 date_mode_t mode) const
5151 {
5152 new(to) Temporal_hybrid(thd, warn, item->to_longlong_hybrid_null(), mode);
5153 }
5154
5155
Item_get_date(THD * thd,Item * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5156 void Type_handler_year::Item_get_date(THD *thd, Item *item,
5157 Temporal::Warn *warn,
5158 MYSQL_TIME *ltime,
5159 date_mode_t fuzzydate) const
5160 {
5161 VYear year(item);
5162 DBUG_ASSERT(!year.truncated());
5163 Longlong_hybrid_null nr(Longlong_null(year.to_YYYYMMDD(), year.is_null()),
5164 item->unsigned_flag);
5165 new(ltime) Temporal_hybrid(thd, warn, nr, fuzzydate);
5166 }
5167
5168
Item_get_date(THD * thd,Item * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5169 void Type_handler_real_result::Item_get_date(THD *thd, Item *item,
5170 Temporal::Warn *warn,
5171 MYSQL_TIME *ltime,
5172 date_mode_t fuzzydate) const
5173 {
5174 new(ltime) Temporal_hybrid(thd, warn, item->to_double_null(), fuzzydate);
5175 }
5176
5177
Item_get_date(THD * thd,Item * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t mode) const5178 void Type_handler_string_result::Item_get_date(THD *thd, Item *item,
5179 Temporal::Warn *warn,
5180 MYSQL_TIME *ltime,
5181 date_mode_t mode) const
5182 {
5183 StringBuffer<40> tmp;
5184 new(ltime) Temporal_hybrid(thd, warn, item->val_str(&tmp), mode);
5185 }
5186
5187
Item_get_date(THD * thd,Item * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5188 void Type_handler_temporal_result::Item_get_date(THD *thd, Item *item,
5189 Temporal::Warn *warn,
5190 MYSQL_TIME *ltime,
5191 date_mode_t fuzzydate) const
5192 {
5193 DBUG_ASSERT(0); // Temporal type items must implement native get_date()
5194 item->null_value= true;
5195 set_zero_time(ltime, MYSQL_TIMESTAMP_NONE);
5196 }
5197
5198
5199 /*************************************************************************/
5200
5201 longlong Type_handler_real_result::
Item_val_int_signed_typecast(Item * item) const5202 Item_val_int_signed_typecast(Item *item) const
5203 {
5204 return item->val_int_signed_typecast_from_real();
5205 }
5206
5207 longlong Type_handler_int_result::
Item_val_int_signed_typecast(Item * item) const5208 Item_val_int_signed_typecast(Item *item) const
5209 {
5210 return item->val_int();
5211 }
5212
5213 longlong Type_handler_decimal_result::
Item_val_int_signed_typecast(Item * item) const5214 Item_val_int_signed_typecast(Item *item) const
5215 {
5216 return VDec(item).to_longlong(false);
5217 }
5218
5219 longlong Type_handler_temporal_result::
Item_val_int_signed_typecast(Item * item) const5220 Item_val_int_signed_typecast(Item *item) const
5221 {
5222 return item->val_int();
5223 }
5224
5225 longlong Type_handler_string_result::
Item_val_int_signed_typecast(Item * item) const5226 Item_val_int_signed_typecast(Item *item) const
5227 {
5228 return item->val_int_signed_typecast_from_str();
5229 }
5230
5231 /*************************************************************************/
5232
5233 longlong Type_handler_real_result::
Item_val_int_unsigned_typecast(Item * item) const5234 Item_val_int_unsigned_typecast(Item *item) const
5235 {
5236 return item->val_int_unsigned_typecast_from_real();
5237 }
5238
5239 longlong Type_handler_int_result::
Item_val_int_unsigned_typecast(Item * item) const5240 Item_val_int_unsigned_typecast(Item *item) const
5241 {
5242 return item->val_int_unsigned_typecast_from_int();
5243 }
5244
5245 longlong Type_handler_temporal_result::
Item_val_int_unsigned_typecast(Item * item) const5246 Item_val_int_unsigned_typecast(Item *item) const
5247 {
5248 return item->val_int_unsigned_typecast_from_int();
5249 }
5250
5251 longlong Type_handler_time_common::
Item_val_int_unsigned_typecast(Item * item) const5252 Item_val_int_unsigned_typecast(Item *item) const
5253 {
5254 /*
5255 TODO: this should eventually be fixed to do rounding
5256 when TIME_ROUND_FRACTIONAL is enabled, together with
5257 Field_{tiny|short|long|longlong}::store_time_dec().
5258 See MDEV-19502.
5259 */
5260 THD *thd= current_thd;
5261 Time tm(thd, item);
5262 DBUG_ASSERT(!tm.is_valid_time() == item->null_value);
5263 if (!tm.is_valid_time())
5264 return 0;
5265 longlong res= tm.to_longlong();
5266 if (res < 0)
5267 {
5268 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
5269 ER_DATA_OVERFLOW, ER_THD(thd, ER_DATA_OVERFLOW),
5270 ErrConvTime(tm.get_mysql_time()).ptr(),
5271 "UNSIGNED BIGINT");
5272 return 0;
5273 }
5274 return res;
5275 }
5276
5277 longlong Type_handler_string_result::
Item_val_int_unsigned_typecast(Item * item) const5278 Item_val_int_unsigned_typecast(Item *item) const
5279 {
5280 return item->val_int_unsigned_typecast_from_str();
5281 }
5282
5283 /*************************************************************************/
5284
5285 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const5286 Type_handler_real_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
5287 String *str) const
5288 {
5289 return item->val_str_ascii_from_val_real(str);
5290 }
5291
5292
5293 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const5294 Type_handler_decimal_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
5295 String *str) const
5296 {
5297 return item->val_str_ascii_from_val_real(str);
5298 }
5299
5300
5301 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const5302 Type_handler_int_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
5303 String *str) const
5304 {
5305 return item->val_str_ascii_from_val_int(str);
5306 }
5307
5308
5309 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const5310 Type_handler_temporal_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
5311 String *str) const
5312 {
5313 return item->val_str_ascii_from_val_str(str);
5314 }
5315
5316
5317 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const5318 Type_handler_string_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
5319 String *str) const
5320 {
5321 return item->val_str_ascii_from_val_str(str);
5322 }
5323
5324 /***************************************************************************/
5325
5326 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const5327 Type_handler_decimal_result::Item_func_hybrid_field_type_val_str(
5328 Item_func_hybrid_field_type *item,
5329 String *str) const
5330 {
5331 return VDec_op(item).to_string_round(str, item->decimals);
5332 }
5333
5334
5335 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const5336 Type_handler_decimal_result::Item_func_hybrid_field_type_val_real(
5337 Item_func_hybrid_field_type *item)
5338 const
5339 {
5340 return VDec_op(item).to_double();
5341 }
5342
5343
5344 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const5345 Type_handler_decimal_result::Item_func_hybrid_field_type_val_int(
5346 Item_func_hybrid_field_type *item)
5347 const
5348 {
5349 return VDec_op(item).to_longlong(item->unsigned_flag);
5350 }
5351
5352
5353 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const5354 Type_handler_decimal_result::Item_func_hybrid_field_type_val_decimal(
5355 Item_func_hybrid_field_type *item,
5356 my_decimal *dec) const
5357 {
5358 return VDec_op(item).to_decimal(dec);
5359 }
5360
5361
5362 void
Item_func_hybrid_field_type_get_date(THD * thd,Item_func_hybrid_field_type * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5363 Type_handler_decimal_result::Item_func_hybrid_field_type_get_date(
5364 THD *thd,
5365 Item_func_hybrid_field_type *item,
5366 Temporal::Warn *warn,
5367 MYSQL_TIME *ltime,
5368 date_mode_t fuzzydate) const
5369 {
5370 new (ltime) Temporal_hybrid(thd, warn, VDec_op(item).ptr(), fuzzydate);
5371 }
5372
5373
5374 void
Item_func_hybrid_field_type_get_date(THD * thd,Item_func_hybrid_field_type * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5375 Type_handler_year::Item_func_hybrid_field_type_get_date(
5376 THD *thd,
5377 Item_func_hybrid_field_type *item,
5378 Temporal::Warn *warn,
5379 MYSQL_TIME *ltime,
5380 date_mode_t fuzzydate) const
5381 {
5382 VYear_op year(item);
5383 DBUG_ASSERT(!year.truncated());
5384 Longlong_hybrid_null nr(Longlong_null(year.to_YYYYMMDD(), year.is_null()),
5385 item->unsigned_flag);
5386 new(ltime) Temporal_hybrid(thd, warn, nr, fuzzydate);
5387 }
5388
5389
5390 /***************************************************************************/
5391
5392
5393 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const5394 Type_handler_int_result::Item_func_hybrid_field_type_val_str(
5395 Item_func_hybrid_field_type *item,
5396 String *str) const
5397 {
5398 return item->val_str_from_int_op(str);
5399 }
5400
5401
5402 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const5403 Type_handler_int_result::Item_func_hybrid_field_type_val_real(
5404 Item_func_hybrid_field_type *item)
5405 const
5406 {
5407 return item->val_real_from_int_op();
5408 }
5409
5410
5411 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const5412 Type_handler_int_result::Item_func_hybrid_field_type_val_int(
5413 Item_func_hybrid_field_type *item)
5414 const
5415 {
5416 return item->val_int_from_int_op();
5417 }
5418
5419
5420 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const5421 Type_handler_int_result::Item_func_hybrid_field_type_val_decimal(
5422 Item_func_hybrid_field_type *item,
5423 my_decimal *dec) const
5424 {
5425 return item->val_decimal_from_int_op(dec);
5426 }
5427
5428
5429 void
Item_func_hybrid_field_type_get_date(THD * thd,Item_func_hybrid_field_type * item,Temporal::Warn * warn,MYSQL_TIME * to,date_mode_t mode) const5430 Type_handler_int_result::Item_func_hybrid_field_type_get_date(
5431 THD *thd,
5432 Item_func_hybrid_field_type *item,
5433 Temporal::Warn *warn,
5434 MYSQL_TIME *to,
5435 date_mode_t mode) const
5436 {
5437 new(to) Temporal_hybrid(thd, warn, item->to_longlong_hybrid_null_op(), mode);
5438 }
5439
5440
5441 /***************************************************************************/
5442
5443 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const5444 Type_handler_double::Item_func_hybrid_field_type_val_str(
5445 Item_func_hybrid_field_type *item,
5446 String *str) const
5447 {
5448 return item->val_str_from_real_op(str);
5449 }
5450
5451 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const5452 Type_handler_float::Item_func_hybrid_field_type_val_str(
5453 Item_func_hybrid_field_type *item,
5454 String *str) const
5455 {
5456 Float nr(item->real_op());
5457 if (item->null_value)
5458 return 0;
5459 nr.to_string(str, item->decimals);
5460 return str;
5461 }
5462
5463 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const5464 Type_handler_real_result::Item_func_hybrid_field_type_val_real(
5465 Item_func_hybrid_field_type *item)
5466 const
5467 {
5468 return item->val_real_from_real_op();
5469 }
5470
5471
5472 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const5473 Type_handler_real_result::Item_func_hybrid_field_type_val_int(
5474 Item_func_hybrid_field_type *item)
5475 const
5476 {
5477 return item->val_int_from_real_op();
5478 }
5479
5480
5481 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const5482 Type_handler_real_result::Item_func_hybrid_field_type_val_decimal(
5483 Item_func_hybrid_field_type *item,
5484 my_decimal *dec) const
5485 {
5486 return item->val_decimal_from_real_op(dec);
5487 }
5488
5489
5490 void
Item_func_hybrid_field_type_get_date(THD * thd,Item_func_hybrid_field_type * item,Temporal::Warn * warn,MYSQL_TIME * to,date_mode_t mode) const5491 Type_handler_real_result::Item_func_hybrid_field_type_get_date(
5492 THD *thd,
5493 Item_func_hybrid_field_type *item,
5494 Temporal::Warn *warn,
5495 MYSQL_TIME *to,
5496 date_mode_t mode) const
5497 {
5498 new(to) Temporal_hybrid(thd, warn, item->to_double_null_op(), mode);
5499 }
5500
5501
5502 /***************************************************************************/
5503
5504 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const5505 Type_handler_temporal_result::Item_func_hybrid_field_type_val_str(
5506 Item_func_hybrid_field_type *item,
5507 String *str) const
5508 {
5509 return item->val_str_from_date_op(str);
5510 }
5511
5512
5513 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const5514 Type_handler_temporal_result::Item_func_hybrid_field_type_val_real(
5515 Item_func_hybrid_field_type *item)
5516 const
5517 {
5518 return item->val_real_from_date_op();
5519 }
5520
5521
5522 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const5523 Type_handler_temporal_result::Item_func_hybrid_field_type_val_int(
5524 Item_func_hybrid_field_type *item)
5525 const
5526 {
5527 return item->val_int_from_date_op();
5528 }
5529
5530
5531 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const5532 Type_handler_temporal_result::Item_func_hybrid_field_type_val_decimal(
5533 Item_func_hybrid_field_type *item,
5534 my_decimal *dec) const
5535 {
5536 return item->val_decimal_from_date_op(dec);
5537 }
5538
5539
5540 void
Item_func_hybrid_field_type_get_date(THD * thd,Item_func_hybrid_field_type * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5541 Type_handler_temporal_result::Item_func_hybrid_field_type_get_date(
5542 THD *thd,
5543 Item_func_hybrid_field_type *item,
5544 Temporal::Warn *warn,
5545 MYSQL_TIME *ltime,
5546 date_mode_t fuzzydate) const
5547 {
5548 if (item->date_op(thd, ltime, fuzzydate))
5549 set_zero_time(ltime, MYSQL_TIMESTAMP_NONE);
5550 }
5551
5552
5553 /***************************************************************************/
5554
5555 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const5556 Type_handler_time_common::Item_func_hybrid_field_type_val_str(
5557 Item_func_hybrid_field_type *item,
5558 String *str) const
5559 {
5560 return item->val_str_from_time_op(str);
5561 }
5562
5563
5564 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const5565 Type_handler_time_common::Item_func_hybrid_field_type_val_real(
5566 Item_func_hybrid_field_type *item)
5567 const
5568 {
5569 return item->val_real_from_time_op();
5570 }
5571
5572
5573 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const5574 Type_handler_time_common::Item_func_hybrid_field_type_val_int(
5575 Item_func_hybrid_field_type *item)
5576 const
5577 {
5578 return item->val_int_from_time_op();
5579 }
5580
5581
5582 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const5583 Type_handler_time_common::Item_func_hybrid_field_type_val_decimal(
5584 Item_func_hybrid_field_type *item,
5585 my_decimal *dec) const
5586 {
5587 return item->val_decimal_from_time_op(dec);
5588 }
5589
5590
5591 void
Item_func_hybrid_field_type_get_date(THD * thd,Item_func_hybrid_field_type * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t fuzzydate) const5592 Type_handler_time_common::Item_func_hybrid_field_type_get_date(
5593 THD *thd,
5594 Item_func_hybrid_field_type *item,
5595 Temporal::Warn *warn,
5596 MYSQL_TIME *ltime,
5597 date_mode_t fuzzydate) const
5598 {
5599 if (item->time_op(thd, ltime))
5600 set_zero_time(ltime, MYSQL_TIMESTAMP_NONE);
5601 }
5602
5603
5604 /***************************************************************************/
5605
5606 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const5607 Type_handler_string_result::Item_func_hybrid_field_type_val_str(
5608 Item_func_hybrid_field_type *item,
5609 String *str) const
5610 {
5611 return item->val_str_from_str_op(str);
5612 }
5613
5614
5615 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const5616 Type_handler_string_result::Item_func_hybrid_field_type_val_real(
5617 Item_func_hybrid_field_type *item)
5618 const
5619 {
5620 return item->val_real_from_str_op();
5621 }
5622
5623
5624 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const5625 Type_handler_string_result::Item_func_hybrid_field_type_val_int(
5626 Item_func_hybrid_field_type *item)
5627 const
5628 {
5629 return item->val_int_from_str_op();
5630 }
5631
5632
5633 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const5634 Type_handler_string_result::Item_func_hybrid_field_type_val_decimal(
5635 Item_func_hybrid_field_type *item,
5636 my_decimal *dec) const
5637 {
5638 return item->val_decimal_from_str_op(dec);
5639 }
5640
5641
5642 void
Item_func_hybrid_field_type_get_date(THD * thd,Item_func_hybrid_field_type * item,Temporal::Warn * warn,MYSQL_TIME * ltime,date_mode_t mode) const5643 Type_handler_string_result::Item_func_hybrid_field_type_get_date(
5644 THD *thd,
5645 Item_func_hybrid_field_type *item,
5646 Temporal::Warn *warn,
5647 MYSQL_TIME *ltime,
5648 date_mode_t mode) const
5649 {
5650 StringBuffer<40> tmp;
5651 String *res= item->str_op(&tmp);
5652 DBUG_ASSERT((res == NULL) == item->null_value);
5653 new(ltime) Temporal_hybrid(thd, warn, res, mode);
5654 }
5655
5656 /***************************************************************************/
5657
5658 bool Type_handler_numeric::
Item_func_between_fix_length_and_dec(Item_func_between * func) const5659 Item_func_between_fix_length_and_dec(Item_func_between *func) const
5660 {
5661 return func->fix_length_and_dec_numeric(current_thd);
5662 }
5663
5664 bool Type_handler_temporal_result::
Item_func_between_fix_length_and_dec(Item_func_between * func) const5665 Item_func_between_fix_length_and_dec(Item_func_between *func) const
5666 {
5667 return func->fix_length_and_dec_temporal(current_thd);
5668 }
5669
5670 bool Type_handler_string_result::
Item_func_between_fix_length_and_dec(Item_func_between * func) const5671 Item_func_between_fix_length_and_dec(Item_func_between *func) const
5672 {
5673 return func->fix_length_and_dec_string(current_thd);
5674 }
5675
5676
5677 longlong Type_handler_row::
Item_func_between_val_int(Item_func_between * func) const5678 Item_func_between_val_int(Item_func_between *func) const
5679 {
5680 DBUG_ASSERT(0);
5681 func->null_value= true;
5682 return 0;
5683 }
5684
5685 longlong Type_handler_string_result::
Item_func_between_val_int(Item_func_between * func) const5686 Item_func_between_val_int(Item_func_between *func) const
5687 {
5688 return func->val_int_cmp_string();
5689 }
5690
5691 longlong Type_handler_temporal_with_date::
Item_func_between_val_int(Item_func_between * func) const5692 Item_func_between_val_int(Item_func_between *func) const
5693 {
5694 return func->val_int_cmp_datetime();
5695 }
5696
5697 longlong Type_handler_time_common::
Item_func_between_val_int(Item_func_between * func) const5698 Item_func_between_val_int(Item_func_between *func) const
5699 {
5700 return func->val_int_cmp_time();
5701 }
5702
5703 longlong Type_handler_timestamp_common::
Item_func_between_val_int(Item_func_between * func) const5704 Item_func_between_val_int(Item_func_between *func) const
5705 {
5706 return func->val_int_cmp_native();
5707 }
5708
5709 longlong Type_handler_int_result::
Item_func_between_val_int(Item_func_between * func) const5710 Item_func_between_val_int(Item_func_between *func) const
5711 {
5712 return func->val_int_cmp_int();
5713 }
5714
5715 longlong Type_handler_real_result::
Item_func_between_val_int(Item_func_between * func) const5716 Item_func_between_val_int(Item_func_between *func) const
5717 {
5718 return func->val_int_cmp_real();
5719 }
5720
5721 longlong Type_handler_decimal_result::
Item_func_between_val_int(Item_func_between * func) const5722 Item_func_between_val_int(Item_func_between *func) const
5723 {
5724 return func->val_int_cmp_decimal();
5725 }
5726
5727 /***************************************************************************/
5728
make_cmp_item(THD * thd,CHARSET_INFO * cs) const5729 cmp_item *Type_handler_int_result::make_cmp_item(THD *thd,
5730 CHARSET_INFO *cs) const
5731 {
5732 return new (thd->mem_root) cmp_item_int;
5733 }
5734
make_cmp_item(THD * thd,CHARSET_INFO * cs) const5735 cmp_item *Type_handler_real_result::make_cmp_item(THD *thd,
5736 CHARSET_INFO *cs) const
5737 {
5738 return new (thd->mem_root) cmp_item_real;
5739 }
5740
make_cmp_item(THD * thd,CHARSET_INFO * cs) const5741 cmp_item *Type_handler_decimal_result::make_cmp_item(THD *thd,
5742 CHARSET_INFO *cs) const
5743 {
5744 return new (thd->mem_root) cmp_item_decimal;
5745 }
5746
5747
make_cmp_item(THD * thd,CHARSET_INFO * cs) const5748 cmp_item *Type_handler_string_result::make_cmp_item(THD *thd,
5749 CHARSET_INFO *cs) const
5750 {
5751 return new (thd->mem_root) cmp_item_sort_string(cs);
5752 }
5753
make_cmp_item(THD * thd,CHARSET_INFO * cs) const5754 cmp_item *Type_handler_row::make_cmp_item(THD *thd,
5755 CHARSET_INFO *cs) const
5756 {
5757 return new (thd->mem_root) cmp_item_row;
5758 }
5759
make_cmp_item(THD * thd,CHARSET_INFO * cs) const5760 cmp_item *Type_handler_time_common::make_cmp_item(THD *thd,
5761 CHARSET_INFO *cs) const
5762 {
5763 return new (thd->mem_root) cmp_item_time;
5764 }
5765
make_cmp_item(THD * thd,CHARSET_INFO * cs) const5766 cmp_item *Type_handler_temporal_with_date::make_cmp_item(THD *thd,
5767 CHARSET_INFO *cs) const
5768 {
5769 return new (thd->mem_root) cmp_item_datetime;
5770 }
5771
make_cmp_item(THD * thd,CHARSET_INFO * cs) const5772 cmp_item *Type_handler_timestamp_common::make_cmp_item(THD *thd,
5773 CHARSET_INFO *cs) const
5774 {
5775 return new (thd->mem_root) cmp_item_timestamp;
5776 }
5777
5778 /***************************************************************************/
5779
srtcmp_in(const void * cs_,const void * x_,const void * y_)5780 static int srtcmp_in(const void *cs_, const void *x_, const void *y_)
5781 {
5782 const CHARSET_INFO *cs= static_cast<const CHARSET_INFO *>(cs_);
5783 const String *x= static_cast<const String *>(x_);
5784 const String *y= static_cast<const String *>(y_);
5785 return cs->strnncollsp(x->ptr(), x->length(), y->ptr(), y->length());
5786 }
5787
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const5788 in_vector *Type_handler_string_result::make_in_vector(THD *thd,
5789 const Item_func_in *func,
5790 uint nargs) const
5791 {
5792 return new (thd->mem_root) in_string(thd, nargs, (qsort2_cmp) srtcmp_in,
5793 func->compare_collation());
5794
5795 }
5796
5797
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const5798 in_vector *Type_handler_int_result::make_in_vector(THD *thd,
5799 const Item_func_in *func,
5800 uint nargs) const
5801 {
5802 return new (thd->mem_root) in_longlong(thd, nargs);
5803 }
5804
5805
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const5806 in_vector *Type_handler_real_result::make_in_vector(THD *thd,
5807 const Item_func_in *func,
5808 uint nargs) const
5809 {
5810 return new (thd->mem_root) in_double(thd, nargs);
5811 }
5812
5813
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const5814 in_vector *Type_handler_decimal_result::make_in_vector(THD *thd,
5815 const Item_func_in *func,
5816 uint nargs) const
5817 {
5818 return new (thd->mem_root) in_decimal(thd, nargs);
5819 }
5820
5821
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const5822 in_vector *Type_handler_time_common::make_in_vector(THD *thd,
5823 const Item_func_in *func,
5824 uint nargs) const
5825 {
5826 return new (thd->mem_root) in_time(thd, nargs);
5827 }
5828
5829
5830 in_vector *
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const5831 Type_handler_temporal_with_date::make_in_vector(THD *thd,
5832 const Item_func_in *func,
5833 uint nargs) const
5834 {
5835 return new (thd->mem_root) in_datetime(thd, nargs);
5836 }
5837
5838
5839 in_vector *
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const5840 Type_handler_timestamp_common::make_in_vector(THD *thd,
5841 const Item_func_in *func,
5842 uint nargs) const
5843 {
5844 return new (thd->mem_root) in_timestamp(thd, nargs);
5845 }
5846
5847
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const5848 in_vector *Type_handler_row::make_in_vector(THD *thd,
5849 const Item_func_in *func,
5850 uint nargs) const
5851 {
5852 return new (thd->mem_root) in_row(thd, nargs, 0);
5853 }
5854
5855 /***************************************************************************/
5856
5857 bool Type_handler_string_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const5858 Item_func_in_fix_comparator_compatible_types(THD *thd,
5859 Item_func_in *func) const
5860 {
5861 if (func->agg_all_arg_charsets_for_comparison())
5862 return true;
5863 if (func->compatible_types_scalar_bisection_possible())
5864 {
5865 return func->value_list_convert_const_to_int(thd) ||
5866 func->fix_for_scalar_comparison_using_bisection(thd);
5867 }
5868 return
5869 func->fix_for_scalar_comparison_using_cmp_items(thd,
5870 1U << (uint) STRING_RESULT);
5871 }
5872
5873
5874 bool Type_handler_int_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const5875 Item_func_in_fix_comparator_compatible_types(THD *thd,
5876 Item_func_in *func) const
5877 {
5878 /*
5879 Does not need to call value_list_convert_const_to_int()
5880 as already handled by int handler.
5881 */
5882 return func->compatible_types_scalar_bisection_possible() ?
5883 func->fix_for_scalar_comparison_using_bisection(thd) :
5884 func->fix_for_scalar_comparison_using_cmp_items(thd,
5885 1U << (uint) INT_RESULT);
5886 }
5887
5888
5889 bool Type_handler_real_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const5890 Item_func_in_fix_comparator_compatible_types(THD *thd,
5891 Item_func_in *func) const
5892 {
5893 return func->compatible_types_scalar_bisection_possible() ?
5894 (func->value_list_convert_const_to_int(thd) ||
5895 func->fix_for_scalar_comparison_using_bisection(thd)) :
5896 func->fix_for_scalar_comparison_using_cmp_items(thd,
5897 1U << (uint) REAL_RESULT);
5898 }
5899
5900
5901 bool Type_handler_decimal_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const5902 Item_func_in_fix_comparator_compatible_types(THD *thd,
5903 Item_func_in *func) const
5904 {
5905 return func->compatible_types_scalar_bisection_possible() ?
5906 (func->value_list_convert_const_to_int(thd) ||
5907 func->fix_for_scalar_comparison_using_bisection(thd)) :
5908 func->fix_for_scalar_comparison_using_cmp_items(thd,
5909 1U << (uint) DECIMAL_RESULT);
5910 }
5911
5912
5913 bool Type_handler_temporal_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const5914 Item_func_in_fix_comparator_compatible_types(THD *thd,
5915 Item_func_in *func) const
5916 {
5917 return func->compatible_types_scalar_bisection_possible() ?
5918 (func->value_list_convert_const_to_int(thd) ||
5919 func->fix_for_scalar_comparison_using_bisection(thd)) :
5920 func->fix_for_scalar_comparison_using_cmp_items(thd,
5921 1U << (uint) TIME_RESULT);
5922 }
5923
5924
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const5925 bool Type_handler_row::Item_func_in_fix_comparator_compatible_types(THD *thd,
5926 Item_func_in *func) const
5927 {
5928 return func->compatible_types_row_bisection_possible() ?
5929 func->fix_for_row_comparison_using_bisection(thd) :
5930 func->fix_for_row_comparison_using_cmp_items(thd);
5931 }
5932
5933 /***************************************************************************/
5934
5935 String *Type_handler_string_result::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5936 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5937 {
5938 return func->val_str_native(str);
5939 }
5940
5941
5942 String *Type_handler_time_common::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5943 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5944 {
5945 return Time(func).to_string(str, func->decimals);
5946 }
5947
5948
5949 String *Type_handler_date_common::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5950 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5951 {
5952 return Date(func).to_string(str);
5953 }
5954
5955
5956 String *Type_handler_datetime_common::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5957 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5958 {
5959 return Datetime(func).to_string(str, func->decimals);
5960 }
5961
5962
5963 String *Type_handler_timestamp_common::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5964 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5965 {
5966 THD *thd= current_thd;
5967 return Timestamp_or_zero_datetime_native_null(thd, func).
5968 to_datetime(thd).to_string(str, func->decimals);
5969 }
5970
5971
5972 String *Type_handler_int_result::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5973 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5974 {
5975 return func->val_string_from_int(str);
5976 }
5977
5978
5979 String *Type_handler_decimal_result::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5980 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5981 {
5982 return VDec(func).to_string_round(str, func->decimals);
5983 }
5984
5985
5986 String *Type_handler_double::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5987 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5988 {
5989 return func->val_string_from_real(str);
5990 }
5991
5992
5993 String *Type_handler_float::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const5994 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
5995 {
5996 Float nr(func->val_real());
5997 if (func->null_value)
5998 return 0;
5999 nr.to_string(str, func->decimals);
6000 return str;
6001 }
6002
6003
6004 double Type_handler_string_result::
Item_func_min_max_val_real(Item_func_min_max * func) const6005 Item_func_min_max_val_real(Item_func_min_max *func) const
6006 {
6007 return func->val_real_native();
6008 }
6009
6010
6011 double Type_handler_time_common::
Item_func_min_max_val_real(Item_func_min_max * func) const6012 Item_func_min_max_val_real(Item_func_min_max *func) const
6013 {
6014 return Time(current_thd, func).to_double();
6015 }
6016
6017
6018 double Type_handler_date_common::
Item_func_min_max_val_real(Item_func_min_max * func) const6019 Item_func_min_max_val_real(Item_func_min_max *func) const
6020 {
6021 return Date(current_thd, func).to_double();
6022 }
6023
6024
6025 double Type_handler_datetime_common::
Item_func_min_max_val_real(Item_func_min_max * func) const6026 Item_func_min_max_val_real(Item_func_min_max *func) const
6027 {
6028 return Datetime(current_thd, func).to_double();
6029 }
6030
6031
6032 double Type_handler_timestamp_common::
Item_func_min_max_val_real(Item_func_min_max * func) const6033 Item_func_min_max_val_real(Item_func_min_max *func) const
6034 {
6035 THD *thd= current_thd;
6036 return Timestamp_or_zero_datetime_native_null(thd, func).
6037 to_datetime(thd).to_double();
6038 }
6039
6040
6041 double Type_handler_numeric::
Item_func_min_max_val_real(Item_func_min_max * func) const6042 Item_func_min_max_val_real(Item_func_min_max *func) const
6043 {
6044 return func->val_real_native();
6045 }
6046
6047
6048 longlong Type_handler_string_result::
Item_func_min_max_val_int(Item_func_min_max * func) const6049 Item_func_min_max_val_int(Item_func_min_max *func) const
6050 {
6051 return func->val_int_native();
6052 }
6053
6054
6055 longlong Type_handler_time_common::
Item_func_min_max_val_int(Item_func_min_max * func) const6056 Item_func_min_max_val_int(Item_func_min_max *func) const
6057 {
6058 return Time(current_thd, func).to_longlong();
6059 }
6060
6061
6062 longlong Type_handler_date_common::
Item_func_min_max_val_int(Item_func_min_max * func) const6063 Item_func_min_max_val_int(Item_func_min_max *func) const
6064 {
6065 return Date(current_thd, func).to_longlong();
6066 }
6067
6068
6069 longlong Type_handler_datetime_common::
Item_func_min_max_val_int(Item_func_min_max * func) const6070 Item_func_min_max_val_int(Item_func_min_max *func) const
6071 {
6072 return Datetime(current_thd, func).to_longlong();
6073 }
6074
6075
6076 longlong Type_handler_timestamp_common::
Item_func_min_max_val_int(Item_func_min_max * func) const6077 Item_func_min_max_val_int(Item_func_min_max *func) const
6078 {
6079 THD *thd= current_thd;
6080 return Timestamp_or_zero_datetime_native_null(thd, func).
6081 to_datetime(thd).to_longlong();
6082 }
6083
6084
6085 longlong Type_handler_numeric::
Item_func_min_max_val_int(Item_func_min_max * func) const6086 Item_func_min_max_val_int(Item_func_min_max *func) const
6087 {
6088 return func->val_int_native();
6089 }
6090
6091
6092 my_decimal *Type_handler_string_result::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const6093 Item_func_min_max_val_decimal(Item_func_min_max *func,
6094 my_decimal *dec) const
6095 {
6096 return func->val_decimal_native(dec);
6097 }
6098
6099
6100 my_decimal *Type_handler_numeric::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const6101 Item_func_min_max_val_decimal(Item_func_min_max *func,
6102 my_decimal *dec) const
6103 {
6104 return func->val_decimal_native(dec);
6105 }
6106
6107
6108 my_decimal *Type_handler_time_common::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const6109 Item_func_min_max_val_decimal(Item_func_min_max *func,
6110 my_decimal *dec) const
6111 {
6112 return Time(current_thd, func).to_decimal(dec);
6113 }
6114
6115
6116 my_decimal *Type_handler_date_common::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const6117 Item_func_min_max_val_decimal(Item_func_min_max *func,
6118 my_decimal *dec) const
6119 {
6120 return Date(current_thd, func).to_decimal(dec);
6121 }
6122
6123
6124 my_decimal *Type_handler_datetime_common::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const6125 Item_func_min_max_val_decimal(Item_func_min_max *func,
6126 my_decimal *dec) const
6127 {
6128 return Datetime(current_thd, func).to_decimal(dec);
6129 }
6130
6131
6132 my_decimal *Type_handler_timestamp_common::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const6133 Item_func_min_max_val_decimal(Item_func_min_max *func,
6134 my_decimal *dec) const
6135 {
6136 THD *thd= current_thd;
6137 return Timestamp_or_zero_datetime_native_null(thd, func).
6138 to_datetime(thd).to_decimal(dec);
6139 }
6140
6141
6142 bool Type_handler_string_result::
Item_func_min_max_get_date(THD * thd,Item_func_min_max * func,MYSQL_TIME * ltime,date_mode_t fuzzydate) const6143 Item_func_min_max_get_date(THD *thd, Item_func_min_max *func,
6144 MYSQL_TIME *ltime, date_mode_t fuzzydate) const
6145 {
6146 /*
6147 just like ::val_int() method of a string item can be called,
6148 for example, SELECT CONCAT("10", "12") + 1,
6149 ::get_date() can be called for non-temporal values,
6150 for example, SELECT MONTH(GREATEST("2011-11-21", "2010-10-09"))
6151 */
6152 return func->get_date_from_string(thd, ltime, fuzzydate);
6153 }
6154
6155
6156 bool Type_handler_numeric::
Item_func_min_max_get_date(THD * thd,Item_func_min_max * func,MYSQL_TIME * ltime,date_mode_t fuzzydate) const6157 Item_func_min_max_get_date(THD *thd, Item_func_min_max *func,
6158 MYSQL_TIME *ltime, date_mode_t fuzzydate) const
6159 {
6160 return Item_get_date_with_warn(thd, func, ltime, fuzzydate);
6161 }
6162
6163
6164 bool Type_handler_temporal_result::
Item_func_min_max_get_date(THD * thd,Item_func_min_max * func,MYSQL_TIME * ltime,date_mode_t fuzzydate) const6165 Item_func_min_max_get_date(THD *thd, Item_func_min_max *func,
6166 MYSQL_TIME *ltime, date_mode_t fuzzydate) const
6167 {
6168 /*
6169 - If the caller specified TIME_TIME_ONLY, then it's going to convert
6170 a DATETIME or DATE to TIME. So we pass the default flags for date. This is
6171 exactly the same with what Item_func_min_max_val_{int|real|decimal|str} or
6172 Item_send_datetime() do. We return the value in accordance with the
6173 current session date flags and let the caller further convert it to TIME.
6174 - If the caller did not specify TIME_TIME_ONLY, then return the value
6175 according to the flags supplied by the caller.
6176 */
6177 return func->get_date_native(thd, ltime,
6178 fuzzydate & TIME_TIME_ONLY ?
6179 Datetime::Options(thd) :
6180 fuzzydate);
6181 }
6182
6183 bool Type_handler_time_common::
Item_func_min_max_get_date(THD * thd,Item_func_min_max * func,MYSQL_TIME * ltime,date_mode_t fuzzydate) const6184 Item_func_min_max_get_date(THD *thd, Item_func_min_max *func,
6185 MYSQL_TIME *ltime, date_mode_t fuzzydate) const
6186 {
6187 return func->get_time_native(thd, ltime);
6188 }
6189
6190
6191 bool Type_handler_timestamp_common::
Item_func_min_max_get_date(THD * thd,Item_func_min_max * func,MYSQL_TIME * ltime,date_mode_t fuzzydate) const6192 Item_func_min_max_get_date(THD *thd, Item_func_min_max *func,
6193 MYSQL_TIME *ltime, date_mode_t fuzzydate) const
6194 {
6195 return Timestamp_or_zero_datetime_native_null(thd, func).
6196 to_datetime(thd).copy_to_mysql_time(ltime);
6197 }
6198
6199 /***************************************************************************/
6200
6201 /**
6202 Get a string representation of the Item value.
6203 See sql_type.h for details.
6204 */
6205 String *Type_handler_row::
print_item_value(THD * thd,Item * item,String * str) const6206 print_item_value(THD *thd, Item *item, String *str) const
6207 {
6208 CHARSET_INFO *cs= thd->variables.character_set_client;
6209 StringBuffer<STRING_BUFFER_USUAL_SIZE> val(cs);
6210 str->append(STRING_WITH_LEN("ROW("));
6211 for (uint i= 0 ; i < item->cols(); i++)
6212 {
6213 if (i > 0)
6214 str->append(',');
6215 Item *elem= item->element_index(i);
6216 String *tmp= elem->type_handler()->print_item_value(thd, elem, &val);
6217 if (tmp)
6218 str->append(*tmp);
6219 else
6220 str->append(STRING_WITH_LEN("NULL"));
6221 }
6222 str->append(STRING_WITH_LEN(")"));
6223 return str;
6224 }
6225
6226
6227 /**
6228 Get a string representation of the Item value,
6229 using the character string format with its charset and collation, e.g.
6230 latin1 'string' COLLATE latin1_german2_ci
6231 */
6232 String *Type_handler::
print_item_value_csstr(THD * thd,Item * item,String * str) const6233 print_item_value_csstr(THD *thd, Item *item, String *str) const
6234 {
6235 String *result= item->val_str(str);
6236
6237 if (!result)
6238 return NULL;
6239
6240 StringBuffer<STRING_BUFFER_USUAL_SIZE> buf(result->charset());
6241 CHARSET_INFO *cs= thd->variables.character_set_client;
6242
6243 buf.append('_');
6244 buf.append(result->charset()->csname);
6245 if (cs->escape_with_backslash_is_dangerous)
6246 buf.append(' ');
6247 append_query_string(cs, &buf, result->ptr(), result->length(),
6248 thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES);
6249 buf.append(" COLLATE '");
6250 buf.append(item->collation.collation->name);
6251 buf.append('\'');
6252 str->copy(buf);
6253
6254 return str;
6255 }
6256
6257
6258 String *Type_handler_numeric::
print_item_value(THD * thd,Item * item,String * str) const6259 print_item_value(THD *thd, Item *item, String *str) const
6260 {
6261 return item->val_str(str);
6262 }
6263
6264
6265 String *Type_handler::
print_item_value_temporal(THD * thd,Item * item,String * str,const Name & type_name,String * buf) const6266 print_item_value_temporal(THD *thd, Item *item, String *str,
6267 const Name &type_name, String *buf) const
6268 {
6269 String *result= item->val_str(buf);
6270 return !result ||
6271 str->realloc(type_name.length() + result->length() + 2) ||
6272 str->copy(type_name.ptr(), type_name.length(), &my_charset_latin1) ||
6273 str->append('\'') ||
6274 str->append(result->ptr(), result->length()) ||
6275 str->append('\'') ?
6276 NULL :
6277 str;
6278 }
6279
6280
6281 String *Type_handler_time_common::
print_item_value(THD * thd,Item * item,String * str) const6282 print_item_value(THD *thd, Item *item, String *str) const
6283 {
6284 StringBuffer<MAX_TIME_FULL_WIDTH+1> buf;
6285 return print_item_value_temporal(thd, item, str,
6286 Name(STRING_WITH_LEN("TIME")), &buf);
6287 }
6288
6289
6290 String *Type_handler_date_common::
print_item_value(THD * thd,Item * item,String * str) const6291 print_item_value(THD *thd, Item *item, String *str) const
6292 {
6293 StringBuffer<MAX_DATE_WIDTH+1> buf;
6294 return print_item_value_temporal(thd, item, str,
6295 Name(STRING_WITH_LEN("DATE")), &buf);
6296 }
6297
6298
6299 String *Type_handler_datetime_common::
print_item_value(THD * thd,Item * item,String * str) const6300 print_item_value(THD *thd, Item *item, String *str) const
6301 {
6302 StringBuffer<MAX_DATETIME_FULL_WIDTH+1> buf;
6303 return print_item_value_temporal(thd, item, str,
6304 Name(STRING_WITH_LEN("TIMESTAMP")), &buf);
6305 }
6306
6307
6308 String *Type_handler_timestamp_common::
print_item_value(THD * thd,Item * item,String * str) const6309 print_item_value(THD *thd, Item *item, String *str) const
6310 {
6311 StringBuffer<MAX_DATETIME_FULL_WIDTH+1> buf;
6312 return print_item_value_temporal(thd, item, str,
6313 Name(STRING_WITH_LEN("TIMESTAMP")), &buf);
6314 }
6315
6316
6317 /***************************************************************************/
6318
6319 bool Type_handler_row::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6320 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6321 {
6322 DBUG_ASSERT(0);
6323 return false;
6324 }
6325
6326
6327 bool Type_handler_int_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6328 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6329 {
6330 item->fix_arg_int(this, item->arguments()[0],
6331 field_type() == MYSQL_TYPE_LONGLONG);
6332 return false;
6333 }
6334
6335
6336 bool Type_handler_year::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6337 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6338 {
6339 item->fix_arg_int(&type_handler_ulong, item->arguments()[0], false);
6340 return false;
6341 }
6342
6343
6344 bool Type_handler_hex_hybrid::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6345 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6346 {
6347 item->fix_arg_hex_hybrid();
6348 return false;
6349 }
6350
6351
6352 bool Type_handler_bit::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6353 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6354 {
6355 uint nbits= item->arguments()[0]->max_length;
6356 item->fix_length_and_dec_ulong_or_ulonglong_by_nbits(nbits);
6357 return false;
6358 }
6359
6360
6361 bool Type_handler_typelib::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6362 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6363 {
6364 item->fix_length_and_dec_long_or_longlong(5, true);
6365 return false;
6366 }
6367
6368
6369 bool Type_handler_real_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6370 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6371 {
6372 item->fix_arg_double();
6373 return false;
6374 }
6375
6376
6377 bool Type_handler_decimal_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6378 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6379 {
6380 item->fix_arg_decimal();
6381 return false;
6382 }
6383
6384
6385 bool Type_handler_date_common::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6386 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6387 {
6388 static const Type_std_attributes attr(Type_numeric_attributes(8, 0, true),
6389 DTCollation_numeric());
6390 item->fix_arg_int(&type_handler_ulong, &attr, false);
6391 return false;
6392 }
6393
6394
6395 bool Type_handler_time_common::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6396 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6397 {
6398 item->fix_arg_time();
6399 return false;
6400 }
6401
6402
6403 bool Type_handler_datetime_common::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6404 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6405 {
6406 item->fix_arg_datetime();
6407 return false;
6408 }
6409
6410
6411 bool Type_handler_timestamp_common::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6412 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6413 {
6414 item->fix_arg_datetime();
6415 return false;
6416 }
6417
6418
6419 bool Type_handler_string_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const6420 Item_func_round_fix_length_and_dec(Item_func_round *item) const
6421 {
6422 item->fix_arg_double();
6423 return false;
6424 }
6425
6426
6427 /***************************************************************************/
6428
6429 bool Type_handler_row::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6430 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6431 {
6432 DBUG_ASSERT(0);
6433 return false;
6434 }
6435
6436
6437 bool Type_handler_int_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6438 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6439 {
6440 item->Type_std_attributes::set(item->arguments()[0]);
6441 item->set_handler(this);
6442 return false;
6443 }
6444
6445
6446 bool Type_handler_year::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6447 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6448 {
6449 item->Type_std_attributes::set(item->arguments()[0]);
6450 item->set_handler(&type_handler_ulong);
6451 return false;
6452 }
6453
6454
6455 bool Type_handler_bit::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6456 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6457 {
6458 uint nbits= item->arguments()[0]->max_length;
6459 item->fix_length_and_dec_ulong_or_ulonglong_by_nbits(nbits);
6460 return false;
6461 }
6462
6463
6464 bool Type_handler_typelib::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6465 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6466 {
6467 item->fix_length_and_dec_long_or_longlong(5, true);
6468 return false;
6469 }
6470
6471
6472 bool Type_handler_hex_hybrid::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6473 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6474 {
6475 uint nchars= item->arguments()[0]->decimal_precision();
6476 item->fix_length_and_dec_long_or_longlong(nchars, true);
6477 return false;
6478 }
6479
6480
6481 bool Type_handler_real_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6482 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6483 {
6484 item->fix_length_and_dec_double();
6485 return false;
6486 }
6487
6488
6489 bool Type_handler_decimal_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6490 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6491 {
6492 item->fix_length_and_dec_int_or_decimal();
6493 return false;
6494 }
6495
6496
6497 bool Type_handler_date_common::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6498 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6499 {
6500 static const Type_numeric_attributes attr(8, 0/*dec*/, true/*unsigned*/);
6501 item->Type_std_attributes::set(attr, DTCollation_numeric());
6502 item->set_handler(&type_handler_ulong);
6503 return false;
6504 }
6505
6506
6507 bool Type_handler_time_common::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6508 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6509 {
6510 item->fix_length_and_dec_time();
6511 return false;
6512 }
6513
6514
6515 bool Type_handler_datetime_common::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6516 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6517 {
6518 item->fix_length_and_dec_datetime();
6519 return false;
6520 }
6521
6522
6523 bool Type_handler_timestamp_common::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6524 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6525 {
6526 item->fix_length_and_dec_datetime();
6527 return false;
6528 }
6529
6530
6531 bool Type_handler_string_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const6532 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
6533 {
6534 item->fix_length_and_dec_double();
6535 return false;
6536 }
6537
6538
6539 /***************************************************************************/
6540
6541 bool Type_handler_row::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const6542 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
6543 {
6544 DBUG_ASSERT(0);
6545 return false;
6546 }
6547
6548
6549 bool Type_handler_int_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const6550 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
6551 {
6552 item->fix_length_and_dec_int();
6553 return false;
6554 }
6555
6556
6557 bool Type_handler_real_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const6558 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
6559 {
6560 item->fix_length_and_dec_double();
6561 return false;
6562 }
6563
6564
6565 bool Type_handler_decimal_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const6566 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
6567 {
6568 item->fix_length_and_dec_decimal();
6569 return false;
6570 }
6571
6572
6573 bool Type_handler_temporal_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const6574 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
6575 {
6576 item->fix_length_and_dec_decimal();
6577 return false;
6578 }
6579
6580
6581 bool Type_handler_string_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const6582 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
6583 {
6584 item->fix_length_and_dec_double();
6585 return false;
6586 }
6587
6588
6589 /***************************************************************************/
6590
6591 bool Type_handler_row::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const6592 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
6593 {
6594 DBUG_ASSERT(0);
6595 return false;
6596 }
6597
6598
6599 bool Type_handler_int_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const6600 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
6601 {
6602 item->fix_length_and_dec_int();
6603 return false;
6604 }
6605
6606
6607 bool Type_handler_real_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const6608 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
6609 {
6610 item->fix_length_and_dec_double();
6611 return false;
6612 }
6613
6614
6615 bool Type_handler_decimal_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const6616 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
6617 {
6618 item->fix_length_and_dec_decimal();
6619 return false;
6620 }
6621
6622
6623 bool Type_handler_temporal_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const6624 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
6625 {
6626 item->fix_length_and_dec_decimal();
6627 return false;
6628 }
6629
6630
6631 bool Type_handler_string_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const6632 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
6633 {
6634 item->fix_length_and_dec_double();
6635 return false;
6636 }
6637
6638
6639 /***************************************************************************/
6640
6641 bool Type_handler::
Item_func_signed_fix_length_and_dec(Item_func_signed * item) const6642 Item_func_signed_fix_length_and_dec(Item_func_signed *item) const
6643 {
6644 item->fix_length_and_dec_generic();
6645 return false;
6646 }
6647
6648
6649 bool Type_handler::
Item_func_unsigned_fix_length_and_dec(Item_func_unsigned * item) const6650 Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const
6651 {
6652 const Item *arg= item->arguments()[0];
6653 if (!arg->unsigned_flag && arg->val_int_min() < 0)
6654 {
6655 /*
6656 Negative arguments produce long results:
6657 CAST(1-2 AS UNSIGNED) -> 18446744073709551615
6658 */
6659 item->max_length= MAX_BIGINT_WIDTH;
6660 return false;
6661 }
6662 item->fix_length_and_dec_generic();
6663 return false;
6664 }
6665
6666
6667 bool Type_handler_string_result::
Item_func_signed_fix_length_and_dec(Item_func_signed * item) const6668 Item_func_signed_fix_length_and_dec(Item_func_signed *item) const
6669 {
6670 item->fix_length_and_dec_string();
6671 return false;
6672 }
6673
6674
6675 bool Type_handler_string_result::
Item_func_unsigned_fix_length_and_dec(Item_func_unsigned * item) const6676 Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const
6677 {
6678 const Item *arg= item->arguments()[0];
6679 if (!arg->unsigned_flag && // Not HEX hybrid
6680 arg->max_char_length() > 1) // Can be negative
6681 {
6682 // String arguments can give long results: '-1' -> 18446744073709551614
6683 item->max_length= MAX_BIGINT_WIDTH;
6684 return false;
6685 }
6686 item->fix_length_and_dec_string();
6687 return false;
6688 }
6689
6690 bool Type_handler_real_result::
Item_func_signed_fix_length_and_dec(Item_func_signed * item) const6691 Item_func_signed_fix_length_and_dec(Item_func_signed *item) const
6692 {
6693 item->fix_length_and_dec_double();
6694 return false;
6695 }
6696
6697
6698 bool Type_handler_real_result::
Item_func_unsigned_fix_length_and_dec(Item_func_unsigned * item) const6699 Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const
6700 {
6701 item->fix_length_and_dec_double();
6702 return false;
6703 }
6704
6705
6706 bool Type_handler::
Item_double_typecast_fix_length_and_dec(Item_double_typecast * item) const6707 Item_double_typecast_fix_length_and_dec(Item_double_typecast *item) const
6708 {
6709 item->fix_length_and_dec_generic();
6710 return false;
6711 }
6712
6713
6714 bool Type_handler::
Item_float_typecast_fix_length_and_dec(Item_float_typecast * item) const6715 Item_float_typecast_fix_length_and_dec(Item_float_typecast *item) const
6716 {
6717 item->fix_length_and_dec_generic();
6718 return false;
6719 }
6720
6721
6722 bool Type_handler::
Item_decimal_typecast_fix_length_and_dec(Item_decimal_typecast * item) const6723 Item_decimal_typecast_fix_length_and_dec(Item_decimal_typecast *item) const
6724 {
6725 item->fix_length_and_dec_generic();
6726 return false;
6727 }
6728
6729
6730 bool Type_handler::
Item_char_typecast_fix_length_and_dec(Item_char_typecast * item) const6731 Item_char_typecast_fix_length_and_dec(Item_char_typecast *item) const
6732 {
6733 item->fix_length_and_dec_generic();
6734 return false;
6735 }
6736
6737
6738 bool Type_handler_numeric::
Item_char_typecast_fix_length_and_dec(Item_char_typecast * item) const6739 Item_char_typecast_fix_length_and_dec(Item_char_typecast *item) const
6740 {
6741 item->fix_length_and_dec_numeric();
6742 return false;
6743 }
6744
6745
6746 bool Type_handler_string_result::
Item_char_typecast_fix_length_and_dec(Item_char_typecast * item) const6747 Item_char_typecast_fix_length_and_dec(Item_char_typecast *item) const
6748 {
6749 item->fix_length_and_dec_str();
6750 return false;
6751 }
6752
6753
6754 bool Type_handler::
Item_time_typecast_fix_length_and_dec(Item_time_typecast * item) const6755 Item_time_typecast_fix_length_and_dec(Item_time_typecast *item) const
6756 {
6757 uint dec= item->decimals == NOT_FIXED_DEC ?
6758 item->arguments()[0]->time_precision(current_thd) :
6759 item->decimals;
6760 item->fix_attributes_temporal(MIN_TIME_WIDTH, dec);
6761 item->maybe_null= true;
6762 return false;
6763 }
6764
6765
6766 bool Type_handler::
Item_date_typecast_fix_length_and_dec(Item_date_typecast * item) const6767 Item_date_typecast_fix_length_and_dec(Item_date_typecast *item) const
6768 {
6769 item->fix_attributes_temporal(MAX_DATE_WIDTH, 0);
6770 item->maybe_null= true;
6771 return false;
6772 }
6773
6774
6775 bool Type_handler::
Item_datetime_typecast_fix_length_and_dec(Item_datetime_typecast * item) const6776 Item_datetime_typecast_fix_length_and_dec(Item_datetime_typecast *item)
6777 const
6778 {
6779 uint dec= item->decimals == NOT_FIXED_DEC ?
6780 item->arguments()[0]->datetime_precision(current_thd) :
6781 item->decimals;
6782 item->fix_attributes_temporal(MAX_DATETIME_WIDTH, dec);
6783 item->maybe_null= true;
6784 return false;
6785 }
6786
6787
6788 /***************************************************************************/
6789
6790 bool Type_handler_row::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const6791 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
6792 {
6793 DBUG_ASSERT(0);
6794 return true;
6795 }
6796
6797
6798 bool Type_handler_int_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const6799 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
6800 {
6801 item->fix_length_and_dec_int();
6802 return false;
6803 }
6804
6805
6806 bool Type_handler_real_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const6807 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
6808 {
6809 item->fix_length_and_dec_double();
6810 return false;
6811 }
6812
6813
6814 bool Type_handler_decimal_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const6815 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
6816 {
6817 item->fix_length_and_dec_decimal();
6818 return false;
6819 }
6820
6821
6822 bool Type_handler_temporal_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const6823 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
6824 {
6825 item->fix_length_and_dec_temporal(true);
6826 return false;
6827 }
6828
6829
6830 bool Type_handler_string_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const6831 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
6832 {
6833 item->fix_length_and_dec_double();
6834 return false;
6835 }
6836
6837 /***************************************************************************/
6838
6839 bool Type_handler_row::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const6840 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
6841 {
6842 DBUG_ASSERT(0);
6843 return true;
6844 }
6845
6846
6847 bool Type_handler_int_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const6848 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
6849 {
6850 item->fix_length_and_dec_int();
6851 return false;
6852 }
6853
6854
6855 bool Type_handler_real_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const6856 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
6857 {
6858 item->fix_length_and_dec_double();
6859 return false;
6860 }
6861
6862
6863 bool Type_handler_decimal_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const6864 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
6865 {
6866 item->fix_length_and_dec_decimal();
6867 return false;
6868 }
6869
6870
6871 bool Type_handler_temporal_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const6872 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
6873 {
6874 item->fix_length_and_dec_temporal(true);
6875 return false;
6876 }
6877
6878
6879 bool Type_handler_string_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const6880 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
6881 {
6882 item->fix_length_and_dec_double();
6883 return false;
6884 }
6885
6886 /***************************************************************************/
6887
6888 bool Type_handler_row::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const6889 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
6890 {
6891 DBUG_ASSERT(0);
6892 return true;
6893 }
6894
6895
6896 bool Type_handler_int_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const6897 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
6898 {
6899 item->fix_length_and_dec_int();
6900 return false;
6901 }
6902
6903
6904 bool Type_handler_real_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const6905 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
6906 {
6907 item->fix_length_and_dec_double();
6908 return false;
6909 }
6910
6911
6912 bool Type_handler_decimal_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const6913 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
6914 {
6915 item->fix_length_and_dec_decimal();
6916 return false;
6917 }
6918
6919
6920 bool Type_handler_temporal_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const6921 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
6922 {
6923 item->fix_length_and_dec_temporal(true);
6924 return false;
6925 }
6926
6927
6928 bool Type_handler_string_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const6929 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
6930 {
6931 item->fix_length_and_dec_double();
6932 return false;
6933 }
6934
6935 /***************************************************************************/
6936
6937 bool Type_handler_row::
Item_func_div_fix_length_and_dec(Item_func_div * item) const6938 Item_func_div_fix_length_and_dec(Item_func_div *item) const
6939 {
6940 DBUG_ASSERT(0);
6941 return true;
6942 }
6943
6944
6945 bool Type_handler_int_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const6946 Item_func_div_fix_length_and_dec(Item_func_div *item) const
6947 {
6948 item->fix_length_and_dec_int();
6949 return false;
6950 }
6951
6952
6953 bool Type_handler_real_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const6954 Item_func_div_fix_length_and_dec(Item_func_div *item) const
6955 {
6956 item->fix_length_and_dec_double();
6957 return false;
6958 }
6959
6960
6961 bool Type_handler_decimal_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const6962 Item_func_div_fix_length_and_dec(Item_func_div *item) const
6963 {
6964 item->fix_length_and_dec_decimal();
6965 return false;
6966 }
6967
6968
6969 bool Type_handler_temporal_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const6970 Item_func_div_fix_length_and_dec(Item_func_div *item) const
6971 {
6972 item->fix_length_and_dec_temporal(false);
6973 return false;
6974 }
6975
6976
6977 bool Type_handler_string_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const6978 Item_func_div_fix_length_and_dec(Item_func_div *item) const
6979 {
6980 item->fix_length_and_dec_double();
6981 return false;
6982 }
6983
6984 /***************************************************************************/
6985
6986 bool Type_handler_row::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const6987 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
6988 {
6989 DBUG_ASSERT(0);
6990 return true;
6991 }
6992
6993
6994 bool Type_handler_int_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const6995 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
6996 {
6997 item->fix_length_and_dec_int();
6998 return false;
6999 }
7000
7001
7002 bool Type_handler_real_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const7003 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
7004 {
7005 item->fix_length_and_dec_double();
7006 return false;
7007 }
7008
7009
7010 bool Type_handler_decimal_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const7011 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
7012 {
7013 item->fix_length_and_dec_decimal();
7014 return false;
7015 }
7016
7017
7018 bool Type_handler_temporal_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const7019 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
7020 {
7021 item->fix_length_and_dec_temporal(true);
7022 return false;
7023 }
7024
7025
7026 bool Type_handler_string_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const7027 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
7028 {
7029 item->fix_length_and_dec_double();
7030 return false;
7031 }
7032
7033 /***************************************************************************/
7034
vers() const7035 const Vers_type_handler* Type_handler_temporal_result::vers() const
7036 {
7037 return &vers_type_timestamp;
7038 }
7039
vers() const7040 const Vers_type_handler* Type_handler_string_result::vers() const
7041 {
7042 return &vers_type_timestamp;
7043 }
7044
vers() const7045 const Vers_type_handler* Type_handler_blob_common::vers() const
7046
7047 {
7048 return &vers_type_timestamp;
7049 }
7050
7051 /***************************************************************************/
7052
Item_time_precision(THD * thd,Item * item) const7053 uint Type_handler::Item_time_precision(THD *thd, Item *item) const
7054 {
7055 return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
7056 }
7057
7058
Item_datetime_precision(THD * thd,Item * item) const7059 uint Type_handler::Item_datetime_precision(THD *thd, Item *item) const
7060 {
7061 return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
7062 }
7063
7064
Item_temporal_precision(THD * thd,Item * item,bool is_time) const7065 uint Type_handler_string_result::Item_temporal_precision(THD *thd, Item *item,
7066 bool is_time) const
7067 {
7068 StringBuffer<64> buf;
7069 String *tmp;
7070 MYSQL_TIME_STATUS status;
7071 DBUG_ASSERT(item->is_fixed());
7072 // Nanosecond rounding is not needed here, for performance purposes
7073 if ((tmp= item->val_str(&buf)) &&
7074 (is_time ?
7075 Time(thd, &status, tmp->ptr(), tmp->length(), tmp->charset(),
7076 Time::Options(TIME_TIME_ONLY, TIME_FRAC_TRUNCATE,
7077 Time::DATETIME_TO_TIME_YYYYMMDD_TRUNCATE)).
7078 is_valid_time() :
7079 Datetime(thd, &status, tmp->ptr(), tmp->length(), tmp->charset(),
7080 Datetime::Options(TIME_FUZZY_DATES, TIME_FRAC_TRUNCATE)).
7081 is_valid_datetime()))
7082 return MY_MIN(status.precision, TIME_SECOND_PART_DIGITS);
7083 return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
7084 }
7085
7086 /***************************************************************************/
7087
Item_decimal_scale(const Item * item) const7088 uint Type_handler::Item_decimal_scale(const Item *item) const
7089 {
7090 return item->decimals < NOT_FIXED_DEC ?
7091 item->decimals :
7092 MY_MIN(item->max_length, DECIMAL_MAX_SCALE);
7093 }
7094
7095 uint Type_handler_temporal_result::
Item_decimal_scale_with_seconds(const Item * item) const7096 Item_decimal_scale_with_seconds(const Item *item) const
7097 {
7098 return item->decimals < NOT_FIXED_DEC ?
7099 item->decimals :
7100 TIME_SECOND_PART_DIGITS;
7101 }
7102
Item_divisor_precision_increment(const Item * item) const7103 uint Type_handler::Item_divisor_precision_increment(const Item *item) const
7104 {
7105 return item->decimals;
7106 }
7107
7108 uint Type_handler_temporal_result::
Item_divisor_precision_increment_with_seconds(const Item * item) const7109 Item_divisor_precision_increment_with_seconds(const Item *item) const
7110 {
7111 return item->decimals < NOT_FIXED_DEC ?
7112 item->decimals :
7113 TIME_SECOND_PART_DIGITS;
7114 }
7115
7116 /***************************************************************************/
7117
Item_decimal_precision(const Item * item) const7118 uint Type_handler_string_result::Item_decimal_precision(const Item *item) const
7119 {
7120 uint res= item->max_char_length();
7121 /*
7122 Return at least one decimal digit, even if Item::max_char_length()
7123 returned 0. This is important to avoid attempts to create fields of types
7124 INT(0) or DECIMAL(0,0) when converting NULL or empty strings to INT/DECIMAL:
7125 CREATE TABLE t1 AS SELECT CONVERT(NULL,SIGNED) AS a;
7126 */
7127 return res ? MY_MIN(res, DECIMAL_MAX_PRECISION) : 1;
7128 }
7129
Item_decimal_precision(const Item * item) const7130 uint Type_handler_real_result::Item_decimal_precision(const Item *item) const
7131 {
7132 uint res= item->max_char_length();
7133 return res ? MY_MIN(res, DECIMAL_MAX_PRECISION) : 1;
7134 }
7135
Item_decimal_precision(const Item * item) const7136 uint Type_handler_decimal_result::Item_decimal_precision(const Item *item) const
7137 {
7138 uint prec= my_decimal_length_to_precision(item->max_char_length(),
7139 item->decimals,
7140 item->unsigned_flag);
7141 return MY_MIN(prec, DECIMAL_MAX_PRECISION);
7142 }
7143
Item_decimal_precision(const Item * item) const7144 uint Type_handler_int_result::Item_decimal_precision(const Item *item) const
7145 {
7146 uint prec= my_decimal_length_to_precision(item->max_char_length(),
7147 item->decimals,
7148 item->unsigned_flag);
7149 return MY_MIN(prec, DECIMAL_MAX_PRECISION);
7150 }
7151
Item_decimal_precision(const Item * item) const7152 uint Type_handler_time_common::Item_decimal_precision(const Item *item) const
7153 {
7154 return 7 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
7155 }
7156
Item_decimal_precision(const Item * item) const7157 uint Type_handler_date_common::Item_decimal_precision(const Item *item) const
7158 {
7159 return 8;
7160 }
7161
Item_decimal_precision(const Item * item) const7162 uint Type_handler_datetime_common::Item_decimal_precision(const Item *item) const
7163 {
7164 return 14 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
7165 }
7166
Item_decimal_precision(const Item * item) const7167 uint Type_handler_timestamp_common::Item_decimal_precision(const Item *item) const
7168 {
7169 return 14 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
7170 }
7171
7172 /***************************************************************************/
7173
7174 bool Type_handler_real_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const7175 subquery_type_allows_materialization(const Item *inner,
7176 const Item *outer,
7177 bool is_in_predicate) const
7178 {
7179 DBUG_ASSERT(inner->cmp_type() == REAL_RESULT);
7180 return outer->cmp_type() == REAL_RESULT;
7181 }
7182
7183
7184 bool Type_handler_int_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const7185 subquery_type_allows_materialization(const Item *inner,
7186 const Item *outer,
7187 bool is_in_predicate) const
7188 {
7189 DBUG_ASSERT(inner->cmp_type() == INT_RESULT);
7190 return outer->cmp_type() == INT_RESULT;
7191 }
7192
7193
7194 bool Type_handler_decimal_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const7195 subquery_type_allows_materialization(const Item *inner,
7196 const Item *outer,
7197 bool is_in_predicate) const
7198 {
7199 DBUG_ASSERT(inner->cmp_type() == DECIMAL_RESULT);
7200 return outer->cmp_type() == DECIMAL_RESULT;
7201 }
7202
7203
7204 bool Type_handler_string_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const7205 subquery_type_allows_materialization(const Item *inner,
7206 const Item *outer,
7207 bool is_in_predicate) const
7208 {
7209 DBUG_ASSERT(inner->cmp_type() == STRING_RESULT);
7210 if (outer->cmp_type() == STRING_RESULT &&
7211 /*
7212 Materialization also is unable to work when create_tmp_table() will
7213 create a blob column because item->max_length is too big.
7214 The following test is copied from varstring_type_handler().
7215 */
7216 !inner->too_big_for_varchar())
7217 {
7218 if (outer->collation.collation == inner->collation.collation)
7219 return true;
7220 if (is_in_predicate)
7221 {
7222 Charset inner_col(inner->collation.collation);
7223 if (inner_col.encoding_allows_reinterpret_as(outer->
7224 collation.collation) &&
7225 inner_col.eq_collation_specific_names(outer->collation.collation))
7226 return true;
7227 }
7228 }
7229 return false;
7230 }
7231
7232
7233 bool Type_handler_temporal_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const7234 subquery_type_allows_materialization(const Item *inner,
7235 const Item *outer,
7236 bool is_in_predicate) const
7237 {
7238 DBUG_ASSERT(inner->cmp_type() == TIME_RESULT);
7239 return mysql_timestamp_type() ==
7240 outer->type_handler()->mysql_timestamp_type();
7241 }
7242
7243 /***************************************************************************/
7244
7245
7246 const Type_handler *
type_handler_for_tmp_table(const Item * item) const7247 Type_handler_null::type_handler_for_tmp_table(const Item *item) const
7248 {
7249 return &type_handler_string;
7250 }
7251
7252
7253 const Type_handler *
type_handler_for_union(const Item * item) const7254 Type_handler_null::type_handler_for_union(const Item *item) const
7255 {
7256 return &type_handler_string;
7257 }
7258
7259
7260 const Type_handler *
type_handler_for_tmp_table(const Item * item) const7261 Type_handler_olddecimal::type_handler_for_tmp_table(const Item *item) const
7262 {
7263 return &type_handler_newdecimal;
7264 }
7265
7266 const Type_handler *
type_handler_for_union(const Item * item) const7267 Type_handler_olddecimal::type_handler_for_union(const Item *item) const
7268 {
7269 return &type_handler_newdecimal;
7270 }
7271
7272
7273 /***************************************************************************/
7274
check_null(const Item * item,st_value * value) const7275 bool Type_handler::check_null(const Item *item, st_value *value) const
7276 {
7277 if (item->null_value)
7278 {
7279 value->m_type= DYN_COL_NULL;
7280 return true;
7281 }
7282 return false;
7283 }
7284
7285
7286 bool Type_handler_null::
Item_save_in_value(THD * thd,Item * item,st_value * value) const7287 Item_save_in_value(THD *thd, Item *item, st_value *value) const
7288 {
7289 value->m_type= DYN_COL_NULL;
7290 return true;
7291 }
7292
7293
7294 bool Type_handler_row::
Item_save_in_value(THD * thd,Item * item,st_value * value) const7295 Item_save_in_value(THD *thd, Item *item, st_value *value) const
7296 {
7297 DBUG_ASSERT(0);
7298 value->m_type= DYN_COL_NULL;
7299 return true;
7300 }
7301
7302
7303 bool Type_handler_int_result::
Item_save_in_value(THD * thd,Item * item,st_value * value) const7304 Item_save_in_value(THD *thd, Item *item, st_value *value) const
7305 {
7306 value->m_type= item->unsigned_flag ? DYN_COL_UINT : DYN_COL_INT;
7307 value->value.m_longlong= item->val_int();
7308 return check_null(item, value);
7309 }
7310
7311
7312 bool Type_handler_real_result::
Item_save_in_value(THD * thd,Item * item,st_value * value) const7313 Item_save_in_value(THD *thd, Item *item, st_value *value) const
7314 {
7315 value->m_type= DYN_COL_DOUBLE;
7316 value->value.m_double= item->val_real();
7317 return check_null(item, value);
7318 }
7319
7320
7321 bool Type_handler_decimal_result::
Item_save_in_value(THD * thd,Item * item,st_value * value) const7322 Item_save_in_value(THD *thd, Item *item, st_value *value) const
7323 {
7324 value->m_type= DYN_COL_DECIMAL;
7325 my_decimal *dec= item->val_decimal(&value->m_decimal);
7326 if (dec != &value->m_decimal && !item->null_value)
7327 my_decimal2decimal(dec, &value->m_decimal);
7328 return check_null(item, value);
7329 }
7330
7331
7332 bool Type_handler_string_result::
Item_save_in_value(THD * thd,Item * item,st_value * value) const7333 Item_save_in_value(THD *thd, Item *item, st_value *value) const
7334 {
7335 value->m_type= DYN_COL_STRING;
7336 String *str= item->val_str(&value->m_string);
7337 if (str != &value->m_string && !item->null_value)
7338 value->m_string.set(str->ptr(), str->length(), str->charset());
7339 return check_null(item, value);
7340 }
7341
7342
7343 bool Type_handler_temporal_with_date::
Item_save_in_value(THD * thd,Item * item,st_value * value) const7344 Item_save_in_value(THD *thd, Item *item, st_value *value) const
7345 {
7346 value->m_type= DYN_COL_DATETIME;
7347 item->get_date(thd, &value->value.m_time,
7348 Datetime::Options(thd, TIME_FRAC_NONE));
7349 return check_null(item, value);
7350 }
7351
7352
7353 bool Type_handler_time_common::
Item_save_in_value(THD * thd,Item * item,st_value * value) const7354 Item_save_in_value(THD *thd, Item *item, st_value *value) const
7355 {
7356 value->m_type= DYN_COL_DATETIME;
7357 item->get_time(thd, &value->value.m_time);
7358 return check_null(item, value);
7359 }
7360
7361 /***************************************************************************/
7362
7363 bool Type_handler_row::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const7364 Item_param_set_from_value(THD *thd,
7365 Item_param *param,
7366 const Type_all_attributes *attr,
7367 const st_value *val) const
7368 {
7369 DBUG_ASSERT(0);
7370 param->set_null();
7371 return true;
7372 }
7373
7374
7375 bool Type_handler_real_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const7376 Item_param_set_from_value(THD *thd,
7377 Item_param *param,
7378 const Type_all_attributes *attr,
7379 const st_value *val) const
7380 {
7381 param->unsigned_flag= attr->unsigned_flag;
7382 param->set_double(val->value.m_double);
7383 return false;
7384 }
7385
7386
7387 bool Type_handler_int_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const7388 Item_param_set_from_value(THD *thd,
7389 Item_param *param,
7390 const Type_all_attributes *attr,
7391 const st_value *val) const
7392 {
7393 param->unsigned_flag= attr->unsigned_flag;
7394 param->set_int(val->value.m_longlong, attr->max_length);
7395 return false;
7396 }
7397
7398
7399 bool Type_handler_decimal_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const7400 Item_param_set_from_value(THD *thd,
7401 Item_param *param,
7402 const Type_all_attributes *attr,
7403 const st_value *val) const
7404 {
7405 param->unsigned_flag= attr->unsigned_flag;
7406 param->set_decimal(&val->m_decimal, attr->unsigned_flag);
7407 return false;
7408 }
7409
7410
7411 bool Type_handler_string_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const7412 Item_param_set_from_value(THD *thd,
7413 Item_param *param,
7414 const Type_all_attributes *attr,
7415 const st_value *val) const
7416 {
7417 param->unsigned_flag= false;
7418 param->setup_conversion_string(thd, attr->collation.collation);
7419 /*
7420 Exact value of max_length is not known unless data is converted to
7421 charset of connection, so we have to set it later.
7422 */
7423 return param->set_str(val->m_string.ptr(), val->m_string.length(),
7424 attr->collation.collation,
7425 attr->collation.collation);
7426 }
7427
7428
7429 bool Type_handler_temporal_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const7430 Item_param_set_from_value(THD *thd,
7431 Item_param *param,
7432 const Type_all_attributes *attr,
7433 const st_value *val) const
7434 {
7435 param->unsigned_flag= attr->unsigned_flag;
7436 param->set_time(&val->value.m_time, attr->max_length, attr->decimals);
7437 return false;
7438 }
7439
7440
7441 /***************************************************************************/
7442
7443 bool Type_handler_null::
Item_send(Item * item,Protocol * protocol,st_value * buf) const7444 Item_send(Item *item, Protocol *protocol, st_value *buf) const
7445 {
7446 return protocol->store_null();
7447 }
7448
7449
7450 bool Type_handler::
Item_send_str(Item * item,Protocol * protocol,st_value * buf) const7451 Item_send_str(Item *item, Protocol *protocol, st_value *buf) const
7452 {
7453 String *res;
7454 if ((res= item->val_str(&buf->m_string)))
7455 {
7456 DBUG_ASSERT(!item->null_value);
7457 return protocol->store(res->ptr(), res->length(), res->charset());
7458 }
7459 DBUG_ASSERT(item->null_value);
7460 return protocol->store_null();
7461 }
7462
7463
7464 bool Type_handler::
Item_send_tiny(Item * item,Protocol * protocol,st_value * buf) const7465 Item_send_tiny(Item *item, Protocol *protocol, st_value *buf) const
7466 {
7467 longlong nr= item->val_int();
7468 if (!item->null_value)
7469 return protocol->store_tiny(nr);
7470 return protocol->store_null();
7471 }
7472
7473
7474 bool Type_handler::
Item_send_short(Item * item,Protocol * protocol,st_value * buf) const7475 Item_send_short(Item *item, Protocol *protocol, st_value *buf) const
7476 {
7477 longlong nr= item->val_int();
7478 if (!item->null_value)
7479 return protocol->store_short(nr);
7480 return protocol->store_null();
7481 }
7482
7483
7484 bool Type_handler::
Item_send_long(Item * item,Protocol * protocol,st_value * buf) const7485 Item_send_long(Item *item, Protocol *protocol, st_value *buf) const
7486 {
7487 longlong nr= item->val_int();
7488 if (!item->null_value)
7489 return protocol->store_long(nr);
7490 return protocol->store_null();
7491 }
7492
7493 bool Type_handler::
Item_send_longlong(Item * item,Protocol * protocol,st_value * buf) const7494 Item_send_longlong(Item *item, Protocol *protocol, st_value *buf) const
7495 {
7496 longlong nr= item->val_int();
7497 if (!item->null_value)
7498 return protocol->store_longlong(nr, item->unsigned_flag);
7499 return protocol->store_null();
7500 }
7501
7502
7503 bool Type_handler::
Item_send_float(Item * item,Protocol * protocol,st_value * buf) const7504 Item_send_float(Item *item, Protocol *protocol, st_value *buf) const
7505 {
7506 float nr= (float) item->val_real();
7507 if (!item->null_value)
7508 return protocol->store_float(nr, item->decimals);
7509 return protocol->store_null();
7510 }
7511
7512
7513 bool Type_handler::
Item_send_double(Item * item,Protocol * protocol,st_value * buf) const7514 Item_send_double(Item *item, Protocol *protocol, st_value *buf) const
7515 {
7516 double nr= item->val_real();
7517 if (!item->null_value)
7518 return protocol->store_double(nr, item->decimals);
7519 return protocol->store_null();
7520 }
7521
7522
Item_send_timestamp(Item * item,Protocol * protocol,st_value * buf) const7523 bool Type_handler::Item_send_timestamp(Item *item,
7524 Protocol *protocol,
7525 st_value *buf) const
7526 {
7527 Timestamp_or_zero_datetime_native_null native(protocol->thd, item);
7528 if (native.is_null())
7529 return protocol->store_null();
7530 native.to_TIME(protocol->thd, &buf->value.m_time);
7531 return protocol->store(&buf->value.m_time, item->decimals);
7532 }
7533
7534
7535 bool Type_handler::
Item_send_datetime(Item * item,Protocol * protocol,st_value * buf) const7536 Item_send_datetime(Item *item, Protocol *protocol, st_value *buf) const
7537 {
7538 item->get_date(protocol->thd, &buf->value.m_time,
7539 Datetime::Options(protocol->thd));
7540 if (!item->null_value)
7541 return protocol->store(&buf->value.m_time, item->decimals);
7542 return protocol->store_null();
7543 }
7544
7545
7546 bool Type_handler::
Item_send_date(Item * item,Protocol * protocol,st_value * buf) const7547 Item_send_date(Item *item, Protocol *protocol, st_value *buf) const
7548 {
7549 item->get_date(protocol->thd, &buf->value.m_time,
7550 Date::Options(protocol->thd));
7551 if (!item->null_value)
7552 return protocol->store_date(&buf->value.m_time);
7553 return protocol->store_null();
7554 }
7555
7556
7557 bool Type_handler::
Item_send_time(Item * item,Protocol * protocol,st_value * buf) const7558 Item_send_time(Item *item, Protocol *protocol, st_value *buf) const
7559 {
7560 item->get_time(protocol->thd, &buf->value.m_time);
7561 if (!item->null_value)
7562 return protocol->store_time(&buf->value.m_time, item->decimals);
7563 return protocol->store_null();
7564 }
7565
7566 /***************************************************************************/
7567
7568 Item *Type_handler_int_result::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const7569 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
7570 {
7571 longlong result= item->val_int();
7572 if (item->null_value)
7573 return new (thd->mem_root) Item_null(thd, item->name.str);
7574 return new (thd->mem_root) Item_int(thd, item->name.str, result,
7575 item->max_length);
7576 }
7577
7578
7579 Item *Type_handler_real_result::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const7580 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
7581 {
7582 double result= item->val_real();
7583 if (item->null_value)
7584 return new (thd->mem_root) Item_null(thd, item->name.str);
7585 return new (thd->mem_root) Item_float(thd, item->name.str, result,
7586 item->decimals, item->max_length);
7587 }
7588
7589
7590 Item *Type_handler_decimal_result::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const7591 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
7592 {
7593 VDec result(item);
7594 if (result.is_null())
7595 return new (thd->mem_root) Item_null(thd, item->name.str);
7596 return new (thd->mem_root) Item_decimal(thd, item->name.str, result.ptr(),
7597 item->max_length, item->decimals);
7598 }
7599
7600
7601 Item *Type_handler_string_result::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const7602 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
7603 {
7604 StringBuffer<MAX_FIELD_WIDTH> tmp;
7605 String *result= item->val_str(&tmp);
7606 if (item->null_value)
7607 return new (thd->mem_root) Item_null(thd, item->name.str);
7608 LEX_CSTRING value;
7609 thd->make_lex_string(&value, result->ptr(), result->length());
7610 return new (thd->mem_root) Item_string(thd, item->name, value,
7611 result->charset());
7612 }
7613
7614
7615 Item *Type_handler_time_common::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const7616 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
7617 {
7618 Item_cache_temporal *cache;
7619 longlong value= item->val_time_packed(thd);
7620 if (item->null_value)
7621 return new (thd->mem_root) Item_null(thd, item->name.str);
7622 cache= new (thd->mem_root) Item_cache_time(thd);
7623 if (cache)
7624 cache->store_packed(value, item);
7625 return cache;
7626 }
7627
7628
7629 Item *Type_handler_temporal_with_date::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const7630 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
7631 {
7632 Item_cache_temporal *cache;
7633 longlong value= item->val_datetime_packed(thd);
7634 if (item->null_value)
7635 return new (thd->mem_root) Item_null(thd, item->name.str);
7636 cache= new (thd->mem_root) Item_cache_datetime(thd);
7637 if (cache)
7638 cache->store_packed(value, item);
7639 return cache;
7640 }
7641
7642
7643 Item *Type_handler_row::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const7644 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
7645 {
7646 if (item->type() == Item::ROW_ITEM && cmp->type() == Item::ROW_ITEM)
7647 {
7648 /*
7649 Substitute constants only in Item_row's. Don't affect other Items
7650 with ROW_RESULT (eg Item_singlerow_subselect).
7651
7652 For such Items more optimal is to detect if it is constant and replace
7653 it with Item_row. This would optimize queries like this:
7654 SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
7655 */
7656 Item_row *item_row= (Item_row*) item;
7657 Item_row *comp_item_row= (Item_row*) cmp;
7658 uint col;
7659 /*
7660 If item and comp_item are both Item_row's and have same number of cols
7661 then process items in Item_row one by one.
7662 We can't ignore NULL values here as this item may be used with <=>, in
7663 which case NULL's are significant.
7664 */
7665 DBUG_ASSERT(item->result_type() == cmp->result_type());
7666 DBUG_ASSERT(item_row->cols() == comp_item_row->cols());
7667 col= item_row->cols();
7668 while (col-- > 0)
7669 resolve_const_item(thd, item_row->addr(col),
7670 comp_item_row->element_index(col));
7671 }
7672 return NULL;
7673 }
7674
7675 /***************************************************************************/
7676
item_name(Item * a,String * str)7677 static const char* item_name(Item *a, String *str)
7678 {
7679 if (a->name.str)
7680 return a->name.str;
7681 str->length(0);
7682 a->print(str, QT_ORDINARY);
7683 return str->c_ptr_safe();
7684 }
7685
7686
wrong_precision_error(uint errcode,Item * a,ulonglong number,uint maximum)7687 static void wrong_precision_error(uint errcode, Item *a,
7688 ulonglong number, uint maximum)
7689 {
7690 StringBuffer<1024> buf(system_charset_info);
7691 my_error(errcode, MYF(0), number, item_name(a, &buf), maximum);
7692 }
7693
7694
7695 /**
7696 Get precision and scale for a declaration
7697
7698 return
7699 0 ok
7700 1 error
7701 */
7702
get_length_and_scale(ulonglong length,ulonglong decimals,uint * out_length,uint * out_decimals,uint max_precision,uint max_scale,Item * a)7703 bool get_length_and_scale(ulonglong length, ulonglong decimals,
7704 uint *out_length, uint *out_decimals,
7705 uint max_precision, uint max_scale,
7706 Item *a)
7707 {
7708 if (length > (ulonglong) max_precision)
7709 {
7710 wrong_precision_error(ER_TOO_BIG_PRECISION, a, length, max_precision);
7711 return 1;
7712 }
7713 if (decimals > (ulonglong) max_scale)
7714 {
7715 wrong_precision_error(ER_TOO_BIG_SCALE, a, decimals, max_scale);
7716 return 1;
7717 }
7718
7719 *out_decimals= (uint) decimals;
7720 my_decimal_trim(&length, out_decimals);
7721 *out_length= (uint) length;
7722
7723 if (*out_length < *out_decimals)
7724 {
7725 my_error(ER_M_BIGGER_THAN_D, MYF(0), "");
7726 return 1;
7727 }
7728 return 0;
7729 }
7730
7731
7732 Item *Type_handler_longlong::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7733 create_typecast_item(THD *thd, Item *item,
7734 const Type_cast_attributes &attr) const
7735 {
7736 if (this != &type_handler_ulonglong)
7737 return new (thd->mem_root) Item_func_signed(thd, item);
7738 return new (thd->mem_root) Item_func_unsigned(thd, item);
7739
7740 }
7741
7742
7743 Item *Type_handler_date_common::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7744 create_typecast_item(THD *thd, Item *item,
7745 const Type_cast_attributes &attr) const
7746 {
7747 return new (thd->mem_root) Item_date_typecast(thd, item);
7748 }
7749
7750
7751
7752 Item *Type_handler_time_common::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7753 create_typecast_item(THD *thd, Item *item,
7754 const Type_cast_attributes &attr) const
7755 {
7756 if (attr.decimals() > MAX_DATETIME_PRECISION)
7757 {
7758 wrong_precision_error(ER_TOO_BIG_PRECISION, item, attr.decimals(),
7759 MAX_DATETIME_PRECISION);
7760 return 0;
7761 }
7762 return new (thd->mem_root)
7763 Item_time_typecast(thd, item, (uint) attr.decimals());
7764 }
7765
7766
7767 Item *Type_handler_datetime_common::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7768 create_typecast_item(THD *thd, Item *item,
7769 const Type_cast_attributes &attr) const
7770 {
7771 if (attr.decimals() > MAX_DATETIME_PRECISION)
7772 {
7773 wrong_precision_error(ER_TOO_BIG_PRECISION, item, attr.decimals(),
7774 MAX_DATETIME_PRECISION);
7775 return 0;
7776 }
7777 return new (thd->mem_root)
7778 Item_datetime_typecast(thd, item, (uint) attr.decimals());
7779
7780 }
7781
7782
7783 Item *Type_handler_decimal_result::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7784 create_typecast_item(THD *thd, Item *item,
7785 const Type_cast_attributes &attr) const
7786 {
7787 uint len, dec;
7788 if (get_length_and_scale(attr.length(), attr.decimals(), &len, &dec,
7789 DECIMAL_MAX_PRECISION, DECIMAL_MAX_SCALE, item))
7790 return NULL;
7791 return new (thd->mem_root) Item_decimal_typecast(thd, item, len, dec);
7792 }
7793
7794
7795 Item *Type_handler_double::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7796 create_typecast_item(THD *thd, Item *item,
7797 const Type_cast_attributes &attr) const
7798 {
7799 uint len, dec;
7800 if (!attr.length_specified())
7801 return new (thd->mem_root) Item_double_typecast(thd, item,
7802 DBL_DIG + 7,
7803 NOT_FIXED_DEC);
7804
7805 if (get_length_and_scale(attr.length(), attr.decimals(), &len, &dec,
7806 DECIMAL_MAX_PRECISION, NOT_FIXED_DEC - 1, item))
7807 return NULL;
7808 return new (thd->mem_root) Item_double_typecast(thd, item, len, dec);
7809 }
7810
7811
7812 Item *Type_handler_float::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7813 create_typecast_item(THD *thd, Item *item,
7814 const Type_cast_attributes &attr) const
7815 {
7816 DBUG_ASSERT(!attr.length_specified());
7817 return new (thd->mem_root) Item_float_typecast(thd, item);
7818 }
7819
7820
7821 Item *Type_handler_long_blob::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7822 create_typecast_item(THD *thd, Item *item,
7823 const Type_cast_attributes &attr) const
7824 {
7825 int len= -1;
7826 CHARSET_INFO *real_cs= attr.charset() ?
7827 attr.charset() :
7828 thd->variables.collation_connection;
7829 if (attr.length_specified())
7830 {
7831 if (attr.length() > MAX_FIELD_BLOBLENGTH)
7832 {
7833 char buff[1024];
7834 String buf(buff, sizeof(buff), system_charset_info);
7835 my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), item_name(item, &buf),
7836 MAX_FIELD_BLOBLENGTH);
7837 return NULL;
7838 }
7839 len= (int) attr.length();
7840 }
7841 return new (thd->mem_root) Item_char_typecast(thd, item, len, real_cs);
7842 }
7843
7844 Item *Type_handler_interval_DDhhmmssff::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const7845 create_typecast_item(THD *thd, Item *item,
7846 const Type_cast_attributes &attr) const
7847 {
7848 if (attr.decimals() > MAX_DATETIME_PRECISION)
7849 {
7850 wrong_precision_error(ER_TOO_BIG_PRECISION, item, attr.decimals(),
7851 MAX_DATETIME_PRECISION);
7852 return 0;
7853 }
7854 return new (thd->mem_root) Item_interval_DDhhmmssff_typecast(thd, item,
7855 (uint)
7856 attr.decimals());
7857 }
7858
7859 /***************************************************************************/
7860
Item_param_setup_conversion(THD * thd,Item_param * param) const7861 void Type_handler_string_result::Item_param_setup_conversion(THD *thd,
7862 Item_param *param)
7863 const
7864 {
7865 param->setup_conversion_string(thd, thd->variables.character_set_client);
7866 }
7867
7868
Item_param_setup_conversion(THD * thd,Item_param * param) const7869 void Type_handler_blob_common::Item_param_setup_conversion(THD *thd,
7870 Item_param *param)
7871 const
7872 {
7873 param->setup_conversion_blob(thd);
7874 }
7875
7876
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7877 void Type_handler_tiny::Item_param_set_param_func(Item_param *param,
7878 uchar **pos, ulong len) const
7879 {
7880 param->set_param_tiny(pos, len);
7881 }
7882
7883
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7884 void Type_handler_short::Item_param_set_param_func(Item_param *param,
7885 uchar **pos, ulong len) const
7886 {
7887 param->set_param_short(pos, len);
7888 }
7889
7890
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7891 void Type_handler_long::Item_param_set_param_func(Item_param *param,
7892 uchar **pos, ulong len) const
7893 {
7894 param->set_param_int32(pos, len);
7895 }
7896
7897
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7898 void Type_handler_longlong::Item_param_set_param_func(Item_param *param,
7899 uchar **pos,
7900 ulong len) const
7901 {
7902 param->set_param_int64(pos, len);
7903 }
7904
7905
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7906 void Type_handler_float::Item_param_set_param_func(Item_param *param,
7907 uchar **pos,
7908 ulong len) const
7909 {
7910 param->set_param_float(pos, len);
7911 }
7912
7913
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7914 void Type_handler_double::Item_param_set_param_func(Item_param *param,
7915 uchar **pos,
7916 ulong len) const
7917 {
7918 param->set_param_double(pos, len);
7919 }
7920
7921
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7922 void Type_handler_decimal_result::Item_param_set_param_func(Item_param *param,
7923 uchar **pos,
7924 ulong len) const
7925 {
7926 param->set_param_decimal(pos, len);
7927 }
7928
7929
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7930 void Type_handler_string_result::Item_param_set_param_func(Item_param *param,
7931 uchar **pos,
7932 ulong len) const
7933 {
7934 param->set_param_str(pos, len);
7935 }
7936
7937
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7938 void Type_handler_time_common::Item_param_set_param_func(Item_param *param,
7939 uchar **pos,
7940 ulong len) const
7941 {
7942 param->set_param_time(pos, len);
7943 }
7944
7945
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7946 void Type_handler_date_common::Item_param_set_param_func(Item_param *param,
7947 uchar **pos,
7948 ulong len) const
7949 {
7950 param->set_param_date(pos, len);
7951 }
7952
7953
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7954 void Type_handler_datetime_common::Item_param_set_param_func(Item_param *param,
7955 uchar **pos,
7956 ulong len) const
7957 {
7958 param->set_param_datetime(pos, len);
7959 }
7960
make_conversion_table_field(MEM_ROOT * root,TABLE * table,uint metadata,const Field * target) const7961 Field *Type_handler_blob_common::make_conversion_table_field(MEM_ROOT *root,
7962 TABLE *table,
7963 uint metadata,
7964 const Field *target)
7965 const
7966 {
7967 uint pack_length= metadata & 0x00ff;
7968 if (pack_length < 1 || pack_length > 4)
7969 return NULL; // Broken binary log?
7970 return new(root)
7971 Field_blob(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str,
7972 table->s, pack_length, target->charset());
7973 }
7974
7975
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7976 void Type_handler_timestamp_common::Item_param_set_param_func(Item_param *param,
7977 uchar **pos,
7978 ulong len) const
7979 {
7980 param->set_param_datetime(pos, len);
7981 }
7982
7983
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7984 void Type_handler::Item_param_set_param_func(Item_param *param,
7985 uchar **pos,
7986 ulong len) const
7987 {
7988 param->set_null(); // Not possible type code in the client-server protocol
7989 }
7990
7991
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const7992 void Type_handler_typelib::Item_param_set_param_func(Item_param *param,
7993 uchar **pos,
7994 ulong len) const
7995 {
7996 param->set_null(); // Not possible type code in the client-server protocol
7997 }
7998
7999
8000 /***************************************************************************/
8001
8002 Field *Type_handler_row::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8003 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8004 const LEX_CSTRING *name,
8005 const Record_addr &rec, const Bit_addr &bit,
8006 const Column_definition_attributes *attr,
8007 uint32 flags) const
8008 {
8009 DBUG_ASSERT(attr->length == 0);
8010 DBUG_ASSERT(f_maybe_null(attr->pack_flag));
8011 return new (mem_root) Field_row(rec.ptr(), name);
8012 }
8013
8014
8015 Field *Type_handler_olddecimal::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8016 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8017 const LEX_CSTRING *name,
8018 const Record_addr &rec, const Bit_addr &bit,
8019 const Column_definition_attributes *attr,
8020 uint32 flags) const
8021 {
8022 DBUG_ASSERT(f_decimals(attr->pack_flag) == 0);
8023 return new (mem_root)
8024 Field_decimal(rec.ptr(), (uint32) attr->length,
8025 rec.null_ptr(), rec.null_bit(),
8026 attr->unireg_check, name,
8027 (uint8) attr->decimals,
8028 f_is_zerofill(attr->pack_flag) != 0,
8029 f_is_dec(attr->pack_flag) == 0);
8030 }
8031
8032
8033 Field *Type_handler_newdecimal::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8034 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8035 const LEX_CSTRING *name,
8036 const Record_addr &rec, const Bit_addr &bit,
8037 const Column_definition_attributes *attr,
8038 uint32 flags) const
8039 {
8040 DBUG_ASSERT(f_decimals(attr->pack_flag) == 0);
8041 return new (mem_root)
8042 Field_new_decimal(rec.ptr(), (uint32) attr->length,
8043 rec.null_ptr(), rec.null_bit(),
8044 attr->unireg_check, name,
8045 (uint8) attr->decimals,
8046 f_is_zerofill(attr->pack_flag) != 0,
8047 f_is_dec(attr->pack_flag) == 0);
8048 }
8049
8050
8051 Field *Type_handler_float::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8052 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8053 const LEX_CSTRING *name,
8054 const Record_addr &rec, const Bit_addr &bit,
8055 const Column_definition_attributes *attr,
8056 uint32 flags) const
8057 {
8058 DBUG_ASSERT(f_decimals(attr->pack_flag) == 0);
8059 uint decimals= attr->decimals;
8060 if (decimals == FLOATING_POINT_DECIMALS)
8061 decimals= NOT_FIXED_DEC;
8062 return new (mem_root)
8063 Field_float(rec.ptr(), (uint32) attr->length,
8064 rec.null_ptr(), rec.null_bit(),
8065 attr->unireg_check, name, decimals,
8066 f_is_zerofill(attr->pack_flag) != 0,
8067 f_is_dec(attr->pack_flag)== 0);
8068 }
8069
8070
8071 Field *Type_handler_double::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8072 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8073 const LEX_CSTRING *name,
8074 const Record_addr &rec, const Bit_addr &bit,
8075 const Column_definition_attributes *attr,
8076 uint32 flags) const
8077 {
8078 DBUG_ASSERT(f_decimals(attr->pack_flag) == 0);
8079 uint decimals= attr->decimals;
8080 if (decimals == FLOATING_POINT_DECIMALS)
8081 decimals= NOT_FIXED_DEC;
8082 return new (mem_root)
8083 Field_double(rec.ptr(), (uint32) attr->length,
8084 rec.null_ptr(), rec.null_bit(),
8085 attr->unireg_check, name, decimals,
8086 f_is_zerofill(attr->pack_flag) != 0,
8087 f_is_dec(attr->pack_flag)== 0);
8088 }
8089
8090
8091 Field *Type_handler_tiny::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8092 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8093 const LEX_CSTRING *name,
8094 const Record_addr &rec, const Bit_addr &bit,
8095 const Column_definition_attributes *attr,
8096 uint32 flags) const
8097 {
8098 return new (mem_root)
8099 Field_tiny(rec.ptr(), (uint32) attr->length, rec.null_ptr(), rec.null_bit(),
8100 attr->unireg_check, name,
8101 f_is_zerofill(attr->pack_flag) != 0,
8102 f_is_dec(attr->pack_flag) == 0);
8103 }
8104
8105
8106 Field *Type_handler_short::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8107 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8108 const LEX_CSTRING *name,
8109 const Record_addr &rec, const Bit_addr &bit,
8110 const Column_definition_attributes *attr,
8111 uint32 flags) const
8112 {
8113 return new (mem_root)
8114 Field_short(rec.ptr(), (uint32) attr->length,
8115 rec.null_ptr(), rec.null_bit(),
8116 attr->unireg_check, name,
8117 f_is_zerofill(attr->pack_flag) != 0,
8118 f_is_dec(attr->pack_flag) == 0);
8119 }
8120
8121
8122 Field *Type_handler_int24::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8123 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8124 const LEX_CSTRING *name,
8125 const Record_addr &rec, const Bit_addr &bit,
8126 const Column_definition_attributes *attr,
8127 uint32 flags) const
8128 {
8129 return new (mem_root)
8130 Field_medium(rec.ptr(), (uint32) attr->length,
8131 rec.null_ptr(), rec.null_bit(),
8132 attr->unireg_check, name,
8133 f_is_zerofill(attr->pack_flag) != 0,
8134 f_is_dec(attr->pack_flag) == 0);
8135 }
8136
8137
8138 Field *Type_handler_long::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8139 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8140 const LEX_CSTRING *name,
8141 const Record_addr &rec, const Bit_addr &bit,
8142 const Column_definition_attributes *attr,
8143 uint32 flags) const
8144 {
8145 return new (mem_root)
8146 Field_long(rec.ptr(), (uint32) attr->length, rec.null_ptr(), rec.null_bit(),
8147 attr->unireg_check, name,
8148 f_is_zerofill(attr->pack_flag) != 0,
8149 f_is_dec(attr->pack_flag) == 0);
8150 }
8151
8152
8153 Field *Type_handler_longlong::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8154 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8155 const LEX_CSTRING *name,
8156 const Record_addr &rec, const Bit_addr &bit,
8157 const Column_definition_attributes *attr,
8158 uint32 flags) const
8159 {
8160 if (flags & (VERS_ROW_START|VERS_ROW_END))
8161 return new (mem_root)
8162 Field_vers_trx_id(rec.ptr(), (uint32) attr->length,
8163 rec.null_ptr(), rec.null_bit(),
8164 attr->unireg_check, name,
8165 f_is_zerofill(attr->pack_flag) != 0,
8166 f_is_dec(attr->pack_flag) == 0);
8167 return new (mem_root)
8168 Field_longlong(rec.ptr(), (uint32) attr->length,
8169 rec.null_ptr(), rec.null_bit(),
8170 attr->unireg_check, name,
8171 f_is_zerofill(attr->pack_flag) != 0,
8172 f_is_dec(attr->pack_flag) == 0);
8173 }
8174
8175
8176 Field *Type_handler_timestamp::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8177 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8178 const LEX_CSTRING *name,
8179 const Record_addr &rec, const Bit_addr &bit,
8180 const Column_definition_attributes *attr,
8181 uint32 flags) const
8182 {
8183 DBUG_ASSERT(attr->decimals == attr->temporal_dec(MAX_DATETIME_WIDTH));
8184 return new_Field_timestamp(mem_root,
8185 rec.ptr(), rec.null_ptr(), rec.null_bit(),
8186 attr->unireg_check, name, share,
8187 attr->temporal_dec(MAX_DATETIME_WIDTH));
8188 }
8189
8190
8191 Field *Type_handler_timestamp2::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8192 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8193 const LEX_CSTRING *name,
8194 const Record_addr &rec, const Bit_addr &bit,
8195 const Column_definition_attributes *attr,
8196 uint32 flags) const
8197 {
8198 DBUG_ASSERT(attr->decimals == attr->temporal_dec(MAX_DATETIME_WIDTH));
8199 return new (mem_root)
8200 Field_timestampf(rec.ptr(), rec.null_ptr(), rec.null_bit(),
8201 attr->unireg_check,
8202 name, share, attr->temporal_dec(MAX_DATETIME_WIDTH));
8203 }
8204
8205
8206 Field *Type_handler_year::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8207 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8208 const LEX_CSTRING *name,
8209 const Record_addr &rec, const Bit_addr &bit,
8210 const Column_definition_attributes *attr,
8211 uint32 flags) const
8212 {
8213 return new (mem_root)
8214 Field_year(rec.ptr(), (uint32) attr->length, rec.null_ptr(), rec.null_bit(),
8215 attr->unireg_check, name);
8216 }
8217
8218
8219 Field *Type_handler_date::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8220 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8221 const LEX_CSTRING *name,
8222 const Record_addr &rec, const Bit_addr &bit,
8223 const Column_definition_attributes *attr,
8224 uint32 flags) const
8225 {
8226 return new (mem_root)
8227 Field_date(rec.ptr(),rec.null_ptr(),rec.null_bit(),
8228 attr->unireg_check, name);
8229 }
8230
8231
8232 Field *Type_handler_newdate::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8233 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8234 const LEX_CSTRING *name,
8235 const Record_addr &rec, const Bit_addr &bit,
8236 const Column_definition_attributes *attr,
8237 uint32 flags) const
8238 {
8239 return new (mem_root)
8240 Field_newdate(rec.ptr(), rec.null_ptr(), rec.null_bit(),
8241 attr->unireg_check, name);
8242 }
8243
8244
8245 Field *Type_handler_time::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8246 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8247 const LEX_CSTRING *name,
8248 const Record_addr &rec, const Bit_addr &bit,
8249 const Column_definition_attributes *attr,
8250 uint32 flags) const
8251 {
8252 DBUG_ASSERT(attr->decimals == attr->temporal_dec(MIN_TIME_WIDTH));
8253 return new_Field_time(mem_root, rec.ptr(), rec.null_ptr(), rec.null_bit(),
8254 attr->unireg_check, name,
8255 attr->temporal_dec(MIN_TIME_WIDTH));
8256 }
8257
8258
8259 Field *Type_handler_time2::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8260 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8261 const LEX_CSTRING *name,
8262 const Record_addr &rec, const Bit_addr &bit,
8263 const Column_definition_attributes *attr,
8264 uint32 flags) const
8265 {
8266 DBUG_ASSERT(attr->decimals == attr->temporal_dec(MIN_TIME_WIDTH));
8267 return new (mem_root)
8268 Field_timef(rec.ptr(), rec.null_ptr(), rec.null_bit(),
8269 attr->unireg_check, name,
8270 attr->temporal_dec(MIN_TIME_WIDTH));
8271 }
8272
8273
8274 Field *Type_handler_datetime::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8275 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8276 const LEX_CSTRING *name,
8277 const Record_addr &rec, const Bit_addr &bit,
8278 const Column_definition_attributes *attr,
8279 uint32 flags) const
8280 {
8281 DBUG_ASSERT(attr->decimals == attr->temporal_dec(MAX_DATETIME_WIDTH));
8282 return new_Field_datetime(mem_root, rec.ptr(), rec.null_ptr(), rec.null_bit(),
8283 attr->unireg_check, name,
8284 attr->temporal_dec(MAX_DATETIME_WIDTH));
8285 }
8286
8287
8288 Field *Type_handler_datetime2::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8289 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8290 const LEX_CSTRING *name,
8291 const Record_addr &rec, const Bit_addr &bit,
8292 const Column_definition_attributes *attr,
8293 uint32 flags) const
8294 {
8295 DBUG_ASSERT(attr->decimals == attr->temporal_dec(MAX_DATETIME_WIDTH));
8296 return new (mem_root)
8297 Field_datetimef(rec.ptr(), rec.null_ptr(), rec.null_bit(),
8298 attr->unireg_check, name,
8299 attr->temporal_dec(MAX_DATETIME_WIDTH));
8300 }
8301
8302
8303 Field *Type_handler_null::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8304 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8305 const LEX_CSTRING *name,
8306 const Record_addr &rec, const Bit_addr &bit,
8307 const Column_definition_attributes *attr,
8308 uint32 flags) const
8309 {
8310 return new (mem_root)
8311 Field_null(rec.ptr(), (uint32) attr->length, attr->unireg_check,
8312 name, attr->charset);
8313 }
8314
8315
8316 Field *Type_handler_bit::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8317 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8318 const LEX_CSTRING *name,
8319 const Record_addr &rec, const Bit_addr &bit,
8320 const Column_definition_attributes *attr,
8321 uint32 flags) const
8322 {
8323 return f_bit_as_char(attr->pack_flag) ?
8324 new (mem_root) Field_bit_as_char(rec.ptr(), (uint32) attr->length,
8325 rec.null_ptr(), rec.null_bit(),
8326 attr->unireg_check, name) :
8327 new (mem_root) Field_bit(rec.ptr(), (uint32) attr->length,
8328 rec.null_ptr(), rec.null_bit(),
8329 bit.ptr(), bit.offs(), attr->unireg_check, name);
8330 }
8331
8332
8333
8334
8335 Field *Type_handler_string::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8336 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8337 const LEX_CSTRING *name,
8338 const Record_addr &rec, const Bit_addr &bit,
8339 const Column_definition_attributes *attr,
8340 uint32 flags) const
8341 {
8342 return new (mem_root)
8343 Field_string(rec.ptr(), (uint32) attr->length,
8344 rec.null_ptr(), rec.null_bit(),
8345 attr->unireg_check, name, attr->charset);
8346 }
8347
8348
8349 Field *Type_handler_varchar::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8350 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8351 const LEX_CSTRING *name,
8352 const Record_addr &rec, const Bit_addr &bit,
8353 const Column_definition_attributes *attr,
8354 uint32 flags) const
8355 {
8356 if (attr->unireg_check == Field::TMYSQL_COMPRESSED)
8357 return new (mem_root)
8358 Field_varstring_compressed(rec.ptr(), (uint32) attr->length,
8359 HA_VARCHAR_PACKLENGTH((uint32) attr->length),
8360 rec.null_ptr(), rec.null_bit(),
8361 attr->unireg_check, name, share, attr->charset,
8362 zlib_compression_method);
8363 return new (mem_root)
8364 Field_varstring(rec.ptr(), (uint32) attr->length,
8365 HA_VARCHAR_PACKLENGTH((uint32) attr->length),
8366 rec.null_ptr(), rec.null_bit(),
8367 attr->unireg_check, name, share, attr->charset);
8368 }
8369
8370
8371 Field *Type_handler_blob_common::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8372 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8373 const LEX_CSTRING *name,
8374 const Record_addr &rec, const Bit_addr &bit,
8375 const Column_definition_attributes *attr,
8376 uint32 flags) const
8377 {
8378 if (attr->unireg_check == Field::TMYSQL_COMPRESSED)
8379 return new (mem_root)
8380 Field_blob_compressed(rec.ptr(), rec.null_ptr(), rec.null_bit(),
8381 attr->unireg_check, name, share,
8382 attr->pack_flag_to_pack_length(), attr->charset,
8383 zlib_compression_method);
8384 return new (mem_root)
8385 Field_blob(rec.ptr(), rec.null_ptr(), rec.null_bit(),
8386 attr->unireg_check, name, share,
8387 attr->pack_flag_to_pack_length(), attr->charset);
8388 }
8389
8390
8391 Field *Type_handler_enum::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8392 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8393 const LEX_CSTRING *name,
8394 const Record_addr &rec, const Bit_addr &bit,
8395 const Column_definition_attributes *attr,
8396 uint32 flags) const
8397 {
8398 return new (mem_root)
8399 Field_enum(rec.ptr(), (uint32) attr->length, rec.null_ptr(), rec.null_bit(),
8400 attr->unireg_check, name, attr->pack_flag_to_pack_length(),
8401 attr->interval, attr->charset);
8402 }
8403
8404
8405 Field *Type_handler_set::
make_table_field_from_def(TABLE_SHARE * share,MEM_ROOT * mem_root,const LEX_CSTRING * name,const Record_addr & rec,const Bit_addr & bit,const Column_definition_attributes * attr,uint32 flags) const8406 make_table_field_from_def(TABLE_SHARE *share, MEM_ROOT *mem_root,
8407 const LEX_CSTRING *name,
8408 const Record_addr &rec, const Bit_addr &bit,
8409 const Column_definition_attributes *attr,
8410 uint32 flags) const
8411 {
8412 return new (mem_root)
8413 Field_set(rec.ptr(), (uint32) attr->length, rec.null_ptr(), rec.null_bit(),
8414 attr->unireg_check, name, attr->pack_flag_to_pack_length(),
8415 attr->interval, attr->charset);
8416 }
8417
8418
8419 /***************************************************************************/
8420
8421 void Type_handler::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8422 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8423 uchar *buff) const
8424 {
8425 def->frm_pack_basic(buff);
8426 def->frm_pack_charset(buff);
8427 }
8428
8429
8430 void Type_handler_real_result::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8431 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8432 uchar *buff) const
8433 {
8434 def->frm_pack_numeric_with_dec(buff);
8435 }
8436
8437
8438 void Type_handler_decimal_result::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8439 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8440 uchar *buff) const
8441 {
8442 def->frm_pack_numeric_with_dec(buff);
8443 }
8444
8445
8446 void Type_handler_int_result::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8447 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8448 uchar *buff) const
8449 {
8450 DBUG_ASSERT(f_decimals(def->pack_flag) == 0);
8451 DBUG_ASSERT(def->decimals == 0);
8452 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8453 }
8454
8455
8456 void Type_handler_date_common::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8457 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8458 uchar *buff) const
8459 {
8460 DBUG_ASSERT(f_decimals(def->pack_flag) == 0);
8461 DBUG_ASSERT(def->decimals == 0);
8462 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8463 }
8464
8465
8466 void Type_handler_bit::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8467 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8468 uchar *buff) const
8469 {
8470 DBUG_ASSERT(f_decimals(def->pack_flag & ~FIELDFLAG_TREAT_BIT_AS_CHAR) == 0);
8471 DBUG_ASSERT(def->decimals == 0);
8472 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8473 }
8474
8475
8476 void Type_handler_blob_common::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8477 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8478 uchar *buff) const
8479 {
8480 DBUG_ASSERT(f_decimals(def->pack_flag & ~FIELDFLAG_BLOB) == 0);
8481 DBUG_ASSERT(def->decimals == 0 ||
8482 def->decimals == NOT_FIXED_DEC);
8483 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8484 }
8485
8486
8487 void Type_handler_null::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8488 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8489 uchar *buff) const
8490 {
8491 DBUG_ASSERT(f_decimals(def->pack_flag) == 0);
8492 DBUG_ASSERT(def->decimals == NOT_FIXED_DEC);
8493 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8494 }
8495
8496
8497 void Type_handler_string_result::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8498 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8499 uchar *buff) const
8500 {
8501 DBUG_ASSERT(f_decimals(def->pack_flag) == 0);
8502 DBUG_ASSERT(def->decimals == 0 || def->decimals == NOT_FIXED_DEC);
8503 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8504 }
8505
8506
8507 void Type_handler_enum::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8508 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8509 uchar *buff) const
8510 {
8511 DBUG_ASSERT(f_decimals(def->pack_flag & ~FIELDFLAG_INTERVAL) == 0);
8512 DBUG_ASSERT(def->decimals == 0);
8513 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8514 }
8515
8516
8517 void Type_handler_set::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8518 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8519 uchar *buff) const
8520 {
8521 DBUG_ASSERT(f_decimals(def->pack_flag & ~FIELDFLAG_BITFIELD) == 0);
8522 DBUG_ASSERT(def->decimals == 0);
8523 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8524 }
8525
8526
8527 void Type_handler_temporal_result::
Column_definition_attributes_frm_pack(const Column_definition_attributes * def,uchar * buff) const8528 Column_definition_attributes_frm_pack(const Column_definition_attributes *def,
8529 uchar *buff) const
8530 {
8531 DBUG_ASSERT(f_decimals(def->pack_flag) == 0);
8532 Type_handler::Column_definition_attributes_frm_pack(def, buff);
8533 }
8534
8535
8536 /***************************************************************************/
8537
8538 bool Type_handler::
Column_definition_attributes_frm_unpack(Column_definition_attributes * attr,TABLE_SHARE * share,const uchar * buffer,LEX_CUSTRING * gis_options) const8539 Column_definition_attributes_frm_unpack(Column_definition_attributes *attr,
8540 TABLE_SHARE *share,
8541 const uchar *buffer,
8542 LEX_CUSTRING *gis_options)
8543 const
8544 {
8545 attr->frm_unpack_basic(buffer);
8546 return attr->frm_unpack_charset(share, buffer);
8547 }
8548
8549
8550 bool Type_handler_real_result::
Column_definition_attributes_frm_unpack(Column_definition_attributes * attr,TABLE_SHARE * share,const uchar * buffer,LEX_CUSTRING * gis_options) const8551 Column_definition_attributes_frm_unpack(Column_definition_attributes *attr,
8552 TABLE_SHARE *share,
8553 const uchar *buffer,
8554 LEX_CUSTRING *gis_options)
8555 const
8556 {
8557 return attr->frm_unpack_numeric_with_dec(share, buffer);
8558 }
8559
8560
8561 bool Type_handler_decimal_result::
Column_definition_attributes_frm_unpack(Column_definition_attributes * attr,TABLE_SHARE * share,const uchar * buffer,LEX_CUSTRING * gis_options) const8562 Column_definition_attributes_frm_unpack(Column_definition_attributes *attr,
8563 TABLE_SHARE *share,
8564 const uchar *buffer,
8565 LEX_CUSTRING *gis_options)
8566 const
8567 {
8568 return attr->frm_unpack_numeric_with_dec(share, buffer);
8569 }
8570
8571
8572 bool Type_handler_time_common::
Column_definition_attributes_frm_unpack(Column_definition_attributes * attr,TABLE_SHARE * share,const uchar * buffer,LEX_CUSTRING * gis_options) const8573 Column_definition_attributes_frm_unpack(Column_definition_attributes *attr,
8574 TABLE_SHARE *share,
8575 const uchar *buffer,
8576 LEX_CUSTRING *gis_options)
8577 const
8578 {
8579 return attr->frm_unpack_temporal_with_dec(share, MIN_TIME_WIDTH, buffer);
8580 }
8581
8582
8583 bool Type_handler_datetime_common::
Column_definition_attributes_frm_unpack(Column_definition_attributes * attr,TABLE_SHARE * share,const uchar * buffer,LEX_CUSTRING * gis_options) const8584 Column_definition_attributes_frm_unpack(Column_definition_attributes *attr,
8585 TABLE_SHARE *share,
8586 const uchar *buffer,
8587 LEX_CUSTRING *gis_options)
8588 const
8589 {
8590 return attr->frm_unpack_temporal_with_dec(share, MAX_DATETIME_WIDTH, buffer);
8591 }
8592
8593
8594 bool Type_handler_timestamp_common::
Column_definition_attributes_frm_unpack(Column_definition_attributes * attr,TABLE_SHARE * share,const uchar * buffer,LEX_CUSTRING * gis_options) const8595 Column_definition_attributes_frm_unpack(Column_definition_attributes *attr,
8596 TABLE_SHARE *share,
8597 const uchar *buffer,
8598 LEX_CUSTRING *gis_options)
8599 const
8600 {
8601 return attr->frm_unpack_temporal_with_dec(share, MAX_DATETIME_WIDTH, buffer);
8602 }
8603
8604
Item_const_eq(const Item_const * a,const Item_const * b,bool binary_cmp) const8605 bool Type_handler_null::Item_const_eq(const Item_const *a,
8606 const Item_const *b,
8607 bool binary_cmp) const
8608 {
8609 return true;
8610 }
8611
8612
Item_const_eq(const Item_const * a,const Item_const * b,bool binary_cmp) const8613 bool Type_handler_real_result::Item_const_eq(const Item_const *a,
8614 const Item_const *b,
8615 bool binary_cmp) const
8616 {
8617 const double *va= a->const_ptr_double();
8618 const double *vb= b->const_ptr_double();
8619 return va[0] == vb[0];
8620 }
8621
8622
Item_const_eq(const Item_const * a,const Item_const * b,bool binary_cmp) const8623 bool Type_handler_int_result::Item_const_eq(const Item_const *a,
8624 const Item_const *b,
8625 bool binary_cmp) const
8626 {
8627 const longlong *va= a->const_ptr_longlong();
8628 const longlong *vb= b->const_ptr_longlong();
8629 bool res= va[0] == vb[0] &&
8630 (va[0] >= 0 ||
8631 (a->get_type_all_attributes_from_const()->unsigned_flag ==
8632 b->get_type_all_attributes_from_const()->unsigned_flag));
8633 return res;
8634 }
8635
8636
Item_const_eq(const Item_const * a,const Item_const * b,bool binary_cmp) const8637 bool Type_handler_string_result::Item_const_eq(const Item_const *a,
8638 const Item_const *b,
8639 bool binary_cmp) const
8640 {
8641 const String *sa= a->const_ptr_string();
8642 const String *sb= b->const_ptr_string();
8643 return binary_cmp ? sa->bin_eq(sb) :
8644 a->get_type_all_attributes_from_const()->collation.collation ==
8645 b->get_type_all_attributes_from_const()->collation.collation &&
8646 sa->eq(sb, a->get_type_all_attributes_from_const()->collation.collation);
8647 }
8648
8649
8650 bool
Item_const_eq(const Item_const * a,const Item_const * b,bool binary_cmp) const8651 Type_handler_decimal_result::Item_const_eq(const Item_const *a,
8652 const Item_const *b,
8653 bool binary_cmp) const
8654 {
8655 const my_decimal *da= a->const_ptr_my_decimal();
8656 const my_decimal *db= b->const_ptr_my_decimal();
8657 return !da->cmp(db) &&
8658 (!binary_cmp ||
8659 a->get_type_all_attributes_from_const()->decimals ==
8660 b->get_type_all_attributes_from_const()->decimals);
8661 }
8662
8663
8664 bool
Item_const_eq(const Item_const * a,const Item_const * b,bool binary_cmp) const8665 Type_handler_temporal_result::Item_const_eq(const Item_const *a,
8666 const Item_const *b,
8667 bool binary_cmp) const
8668 {
8669 const MYSQL_TIME *ta= a->const_ptr_mysql_time();
8670 const MYSQL_TIME *tb= b->const_ptr_mysql_time();
8671 return !my_time_compare(ta, tb) &&
8672 (!binary_cmp ||
8673 a->get_type_all_attributes_from_const()->decimals ==
8674 b->get_type_all_attributes_from_const()->decimals);
8675 }
8676
8677 /***************************************************************************/
8678
8679 const Type_handler *
cast_to_int_type_handler() const8680 Type_handler_hex_hybrid::cast_to_int_type_handler() const
8681 {
8682 return &type_handler_ulonglong;
8683 }
8684
8685
8686 /***************************************************************************/
8687
Item_eq_value(THD * thd,const Type_cmp_attributes * attr,Item * a,Item * b) const8688 bool Type_handler_row::Item_eq_value(THD *thd, const Type_cmp_attributes *attr,
8689 Item *a, Item *b) const
8690 {
8691 DBUG_ASSERT(0);
8692 return false;
8693 }
8694
8695
Item_eq_value(THD * thd,const Type_cmp_attributes * attr,Item * a,Item * b) const8696 bool Type_handler_int_result::Item_eq_value(THD *thd,
8697 const Type_cmp_attributes *attr,
8698 Item *a, Item *b) const
8699 {
8700 longlong value0= a->val_int();
8701 longlong value1= b->val_int();
8702 return !a->null_value && !b->null_value && value0 == value1 &&
8703 (value0 >= 0 || a->unsigned_flag == b->unsigned_flag);
8704 }
8705
8706
Item_eq_value(THD * thd,const Type_cmp_attributes * attr,Item * a,Item * b) const8707 bool Type_handler_real_result::Item_eq_value(THD *thd,
8708 const Type_cmp_attributes *attr,
8709 Item *a, Item *b) const
8710 {
8711 double value0= a->val_real();
8712 double value1= b->val_real();
8713 return !a->null_value && !b->null_value && value0 == value1;
8714 }
8715
8716
Item_eq_value(THD * thd,const Type_cmp_attributes * attr,Item * a,Item * b) const8717 bool Type_handler_time_common::Item_eq_value(THD *thd,
8718 const Type_cmp_attributes *attr,
8719 Item *a, Item *b) const
8720 {
8721 longlong value0= a->val_time_packed(thd);
8722 longlong value1= b->val_time_packed(thd);
8723 return !a->null_value && !b->null_value && value0 == value1;
8724 }
8725
8726
Item_eq_value(THD * thd,const Type_cmp_attributes * attr,Item * a,Item * b) const8727 bool Type_handler_temporal_with_date::Item_eq_value(THD *thd,
8728 const Type_cmp_attributes *attr,
8729 Item *a, Item *b) const
8730 {
8731 longlong value0= a->val_datetime_packed(thd);
8732 longlong value1= b->val_datetime_packed(thd);
8733 return !a->null_value && !b->null_value && value0 == value1;
8734 }
8735
8736
Item_eq_value(THD * thd,const Type_cmp_attributes * attr,Item * a,Item * b) const8737 bool Type_handler_timestamp_common::Item_eq_value(THD *thd,
8738 const Type_cmp_attributes *attr,
8739 Item *a, Item *b) const
8740 {
8741 Timestamp_or_zero_datetime_native_null na(thd, a, true);
8742 Timestamp_or_zero_datetime_native_null nb(thd, b, true);
8743 return !na.is_null() && !nb.is_null() && !cmp_native(na, nb);
8744 }
8745
8746
Item_eq_value(THD * thd,const Type_cmp_attributes * attr,Item * a,Item * b) const8747 bool Type_handler_string_result::Item_eq_value(THD *thd,
8748 const Type_cmp_attributes *attr,
8749 Item *a, Item *b) const
8750 {
8751 String *va, *vb;
8752 StringBuffer<128> cmp_value1, cmp_value2;
8753 return (va= a->val_str(&cmp_value1)) &&
8754 (vb= b->val_str(&cmp_value2)) &&
8755 va->eq(vb, attr->compare_collation());
8756 }
8757
8758
8759 /***************************************************************************/
8760
union_element_finalize(Item_type_holder * item) const8761 bool Type_handler_string_result::union_element_finalize(Item_type_holder* item) const
8762 {
8763 if (item->collation.derivation == DERIVATION_NONE)
8764 {
8765 my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION");
8766 return true;
8767 }
8768 return false;
8769 }
8770
8771
8772 /***************************************************************************/
8773
8774 void Type_handler_var_string::
Column_definition_implicit_upgrade(Column_definition * c) const8775 Column_definition_implicit_upgrade(Column_definition *c) const
8776 {
8777 // Change old VARCHAR to new VARCHAR
8778 c->set_handler(&type_handler_varchar);
8779 }
8780
8781
8782 void Type_handler_time_common::
Column_definition_implicit_upgrade(Column_definition * c) const8783 Column_definition_implicit_upgrade(Column_definition *c) const
8784 {
8785 if (opt_mysql56_temporal_format)
8786 c->set_handler(&type_handler_time2);
8787 else
8788 c->set_handler(&type_handler_time);
8789 }
8790
8791
8792 void Type_handler_datetime_common::
Column_definition_implicit_upgrade(Column_definition * c) const8793 Column_definition_implicit_upgrade(Column_definition *c) const
8794 {
8795 if (opt_mysql56_temporal_format)
8796 c->set_handler(&type_handler_datetime2);
8797 else
8798 c->set_handler(&type_handler_datetime);
8799 }
8800
8801
8802 void Type_handler_timestamp_common::
Column_definition_implicit_upgrade(Column_definition * c) const8803 Column_definition_implicit_upgrade(Column_definition *c) const
8804 {
8805 if (opt_mysql56_temporal_format)
8806 c->set_handler(&type_handler_timestamp2);
8807 else
8808 c->set_handler(&type_handler_timestamp);
8809 }
8810
8811
8812 /***************************************************************************/
8813
8814
stored_field_cmp_to_item(THD * thd,Field * field,Item * item) const8815 int Type_handler_temporal_with_date::stored_field_cmp_to_item(THD *thd,
8816 Field *field,
8817 Item *item) const
8818 {
8819 MYSQL_TIME field_time, item_time, item_time2, *item_time_cmp= &item_time;
8820 field->get_date(&field_time, Datetime::Options(TIME_INVALID_DATES, thd));
8821 item->get_date(thd, &item_time, Datetime::Options(TIME_INVALID_DATES, thd));
8822 if (item_time.time_type == MYSQL_TIMESTAMP_TIME &&
8823 time_to_datetime(thd, &item_time, item_time_cmp= &item_time2))
8824 return 1;
8825 return my_time_compare(&field_time, item_time_cmp);
8826 }
8827
8828
stored_field_cmp_to_item(THD * thd,Field * field,Item * item) const8829 int Type_handler_time_common::stored_field_cmp_to_item(THD *thd,
8830 Field *field,
8831 Item *item) const
8832 {
8833 MYSQL_TIME field_time, item_time;
8834 field->get_date(&field_time, Time::Options(thd));
8835 item->get_date(thd, &item_time, Time::Options(thd));
8836 return my_time_compare(&field_time, &item_time);
8837 }
8838
8839
stored_field_cmp_to_item(THD * thd,Field * field,Item * item) const8840 int Type_handler_string_result::stored_field_cmp_to_item(THD *thd,
8841 Field *field,
8842 Item *item) const
8843 {
8844 StringBuffer<MAX_FIELD_WIDTH> item_tmp;
8845 StringBuffer<MAX_FIELD_WIDTH> field_tmp;
8846 String *item_result= item->val_str(&item_tmp);
8847 /*
8848 Some implementations of Item::val_str(String*) actually modify
8849 the field Item::null_value, hence we can't check it earlier.
8850 */
8851 if (item->null_value)
8852 return 0;
8853 String *field_result= field->val_str(&field_tmp);
8854 return sortcmp(field_result, item_result, field->charset());
8855 }
8856
8857
stored_field_cmp_to_item(THD * thd,Field * field,Item * item) const8858 int Type_handler_int_result::stored_field_cmp_to_item(THD *thd,
8859 Field *field,
8860 Item *item) const
8861 {
8862 DBUG_ASSERT(0); // Not used yet
8863 return 0;
8864 }
8865
8866
stored_field_cmp_to_item(THD * thd,Field * field,Item * item) const8867 int Type_handler_real_result::stored_field_cmp_to_item(THD *thd,
8868 Field *field,
8869 Item *item) const
8870 {
8871 /*
8872 The patch for Bug#13463415 started using this function for comparing
8873 BIGINTs. That uncovered a bug in Visual Studio 32bit optimized mode.
8874 Prefixing the auto variables with volatile fixes the problem....
8875 */
8876 volatile double result= item->val_real();
8877 if (item->null_value)
8878 return 0;
8879 volatile double field_result= field->val_real();
8880 if (field_result < result)
8881 return -1;
8882 else if (field_result > result)
8883 return 1;
8884 return 0;
8885 }
8886
8887
8888 /***************************************************************************/
8889
8890
have_important_literal_warnings(const MYSQL_TIME_STATUS * status)8891 static bool have_important_literal_warnings(const MYSQL_TIME_STATUS *status)
8892 {
8893 return (status->warnings & ~MYSQL_TIME_NOTE_TRUNCATED) != 0;
8894 }
8895
8896
literal_warn(THD * thd,const Item * item,const char * str,size_t length,CHARSET_INFO * cs,const MYSQL_TIME_STATUS * st,const char * typestr,bool send_error)8897 static void literal_warn(THD *thd, const Item *item,
8898 const char *str, size_t length, CHARSET_INFO *cs,
8899 const MYSQL_TIME_STATUS *st,
8900 const char *typestr, bool send_error)
8901 {
8902 if (likely(item))
8903 {
8904 if (st->warnings) // e.g. a note on nanosecond truncation
8905 {
8906 ErrConvString err(str, length, cs);
8907 thd->push_warning_wrong_or_truncated_value(
8908 Sql_condition::time_warn_level(st->warnings),
8909 false, typestr, err.ptr(),
8910 nullptr, nullptr, nullptr);
8911 }
8912 }
8913 else if (send_error)
8914 {
8915 ErrConvString err(str, length, cs);
8916 my_error(ER_WRONG_VALUE, MYF(0), typestr, err.ptr());
8917 }
8918 }
8919
8920
8921 Item_literal *
create_literal_item(THD * thd,const char * str,size_t length,CHARSET_INFO * cs,bool send_error) const8922 Type_handler_date_common::create_literal_item(THD *thd,
8923 const char *str,
8924 size_t length,
8925 CHARSET_INFO *cs,
8926 bool send_error) const
8927 {
8928 Temporal::Warn st;
8929 Item_literal *item= NULL;
8930 Temporal_hybrid tmp(thd, &st, str, length, cs, Temporal_hybrid::Options(thd));
8931 if (tmp.is_valid_temporal() &&
8932 tmp.get_mysql_time()->time_type == MYSQL_TIMESTAMP_DATE &&
8933 !have_important_literal_warnings(&st))
8934 {
8935 Date d(&tmp);
8936 item= new (thd->mem_root) Item_date_literal(thd, &d);
8937 }
8938 literal_warn(thd, item, str, length, cs, &st, "DATE", send_error);
8939 return item;
8940 }
8941
8942
8943 Item_literal *
create_literal_item(THD * thd,const char * str,size_t length,CHARSET_INFO * cs,bool send_error) const8944 Type_handler_temporal_with_date::create_literal_item(THD *thd,
8945 const char *str,
8946 size_t length,
8947 CHARSET_INFO *cs,
8948 bool send_error) const
8949 {
8950 Temporal::Warn st;
8951 Item_literal *item= NULL;
8952 Temporal_hybrid tmp(thd, &st, str, length, cs, Temporal_hybrid::Options(thd));
8953 if (tmp.is_valid_temporal() &&
8954 tmp.get_mysql_time()->time_type == MYSQL_TIMESTAMP_DATETIME &&
8955 !have_important_literal_warnings(&st))
8956 {
8957 Datetime dt(&tmp);
8958 item= new (thd->mem_root) Item_datetime_literal(thd, &dt, st.precision);
8959 }
8960 literal_warn(thd, item, str, length, cs, &st, "DATETIME", send_error);
8961 return item;
8962 }
8963
8964
8965 Item_literal *
create_literal_item(THD * thd,const char * str,size_t length,CHARSET_INFO * cs,bool send_error) const8966 Type_handler_time_common::create_literal_item(THD *thd,
8967 const char *str,
8968 size_t length,
8969 CHARSET_INFO *cs,
8970 bool send_error) const
8971 {
8972 MYSQL_TIME_STATUS st;
8973 Item_literal *item= NULL;
8974 Time::Options opt(TIME_TIME_ONLY, thd, Time::DATETIME_TO_TIME_DISALLOW);
8975 Time tmp(thd, &st, str, length, cs, opt);
8976 if (tmp.is_valid_time() &&
8977 !have_important_literal_warnings(&st))
8978 item= new (thd->mem_root) Item_time_literal(thd, &tmp, st.precision);
8979 literal_warn(thd, item, str, length, cs, &st, "TIME", send_error);
8980 return item;
8981 }
8982
8983
8984 bool
Item_val_native_with_conversion(THD * thd,Item * item,Native * to) const8985 Type_handler_time_common::Item_val_native_with_conversion(THD *thd,
8986 Item *item,
8987 Native *to) const
8988 {
8989 if (item->type_handler()->type_handler_for_native_format() ==
8990 &type_handler_time2)
8991 return item->val_native(thd, to);
8992 return Time(thd, item).to_native(to, item->time_precision(thd));
8993 }
8994
8995
8996 bool
Item_val_native_with_conversion_result(THD * thd,Item * item,Native * to) const8997 Type_handler_time_common::Item_val_native_with_conversion_result(THD *thd,
8998 Item *item,
8999 Native *to)
9000 const
9001 {
9002 if (item->type_handler()->type_handler_for_native_format() ==
9003 &type_handler_time2)
9004 return item->val_native_result(thd, to);
9005 MYSQL_TIME ltime;
9006 if (item->get_date_result(thd, <ime, Time::Options(thd)))
9007 return true;
9008 int warn;
9009 return Time(&warn, <ime, 0).to_native(to, item->time_precision(thd));
9010 }
9011
9012
cmp_native(const Native & a,const Native & b) const9013 int Type_handler_time_common::cmp_native(const Native &a,
9014 const Native &b) const
9015 {
9016 // Optimize a simple case: equal fractional precision:
9017 if (a.length() == b.length())
9018 return memcmp(a.ptr(), b.ptr(), a.length());
9019 longlong lla= Time(a).to_packed();
9020 longlong llb= Time(b).to_packed();
9021 if (lla < llb)
9022 return -1;
9023 if (lla> llb)
9024 return 1;
9025 return 0;
9026 }
9027
9028
TIME_to_native(THD * thd,const MYSQL_TIME * ltime,Native * to,uint decimals) const9029 bool Type_handler_timestamp_common::TIME_to_native(THD *thd,
9030 const MYSQL_TIME *ltime,
9031 Native *to,
9032 uint decimals) const
9033 {
9034 uint error_code;
9035 Timestamp_or_zero_datetime tm(thd, ltime, &error_code);
9036 if (error_code)
9037 return true;
9038 tm.trunc(decimals);
9039 return tm.to_native(to, decimals);
9040 }
9041
9042
9043 bool
Item_val_native_with_conversion(THD * thd,Item * item,Native * to) const9044 Type_handler_timestamp_common::Item_val_native_with_conversion(THD *thd,
9045 Item *item,
9046 Native *to) const
9047 {
9048 MYSQL_TIME ltime;
9049 if (item->type_handler()->type_handler_for_native_format() ==
9050 &type_handler_timestamp2)
9051 return item->val_native(thd, to);
9052 return
9053 item->get_date(thd, <ime, Datetime::Options(TIME_NO_ZERO_IN_DATE, thd)) ||
9054 TIME_to_native(thd, <ime, to, item->datetime_precision(thd));
9055 }
9056
union_element_finalize(Item_type_holder * item) const9057 bool Type_handler_null::union_element_finalize(Item_type_holder *item) const
9058 {
9059 item->set_handler(&type_handler_string);
9060 return false;
9061 }
9062
9063
9064 bool
Item_val_native_with_conversion_result(THD * thd,Item * item,Native * to) const9065 Type_handler_timestamp_common::Item_val_native_with_conversion_result(THD *thd,
9066 Item *item,
9067 Native *to)
9068 const
9069 {
9070 MYSQL_TIME ltime;
9071 if (item->type_handler()->type_handler_for_native_format() ==
9072 &type_handler_timestamp2)
9073 return item->val_native_result(thd, to);
9074 return
9075 item->get_date_result(thd, <ime,
9076 Datetime::Options(TIME_NO_ZERO_IN_DATE, thd)) ||
9077 TIME_to_native(thd, <ime, to, item->datetime_precision(thd));
9078 }
9079
9080
cmp_native(const Native & a,const Native & b) const9081 int Type_handler_timestamp_common::cmp_native(const Native &a,
9082 const Native &b) const
9083 {
9084 /*
9085 Optimize a simple case:
9086 Either both timeatamp values have the same fractional precision,
9087 or both values are zero datetime '0000-00-00 00:00:00.000000',
9088 */
9089 if (a.length() == b.length())
9090 return memcmp(a.ptr(), b.ptr(), a.length());
9091 return Timestamp_or_zero_datetime(a).cmp(Timestamp_or_zero_datetime(b));
9092 }
9093
9094
9095 Timestamp_or_zero_datetime_native_null::
Timestamp_or_zero_datetime_native_null(THD * thd,Item * item,bool conv)9096 Timestamp_or_zero_datetime_native_null(THD *thd, Item *item, bool conv)
9097 :Null_flag(false)
9098 {
9099 DBUG_ASSERT(item->type_handler()->type_handler_for_native_format() ==
9100 &type_handler_timestamp2 || conv);
9101 if (conv ?
9102 type_handler_timestamp2.Item_val_native_with_conversion(thd, item, this) :
9103 item->val_native(thd, this))
9104 Null_flag::operator=(true);
9105 // If no conversion, then is_null() should be equal to item->null_value
9106 DBUG_ASSERT(is_null() == item->null_value || conv);
9107 /*
9108 is_null() can be true together with item->null_value==false, which means
9109 a non-NULL item was evaluated, but then the conversion to TIMESTAMP failed.
9110 But is_null() can never be false if item->null_value==true.
9111 */
9112 DBUG_ASSERT(is_null() >= item->null_value);
9113 }
9114
9115
9116 bool
Item_param_val_native(THD * thd,Item_param * item,Native * to) const9117 Type_handler::Item_param_val_native(THD *thd,
9118 Item_param *item,
9119 Native *to) const
9120 {
9121 DBUG_ASSERT(0); // TODO-TYPE: MDEV-14271
9122 return item->null_value= true;
9123 }
9124
9125
9126 bool
Item_param_val_native(THD * thd,Item_param * item,Native * to) const9127 Type_handler_timestamp_common::Item_param_val_native(THD *thd,
9128 Item_param *item,
9129 Native *to) const
9130 {
9131 /*
9132 The below code may not run well in corner cases.
9133 This will be fixed under terms of MDEV-14271.
9134 Item_param should:
9135 - either remember @@time_zone at bind time
9136 - or store TIMESTAMP in my_time_t format, rather than in MYSQL_TIME format.
9137 */
9138 MYSQL_TIME ltime;
9139 return
9140 item->get_date(thd, <ime, Datetime::Options(TIME_NO_ZERO_IN_DATE, thd)) ||
9141 TIME_to_native(thd, <ime, to, item->datetime_precision(thd));
9142 }
9143
9144
9145 bool
Item_param_val_native(THD * thd,Item_param * item,Native * to) const9146 Type_handler_time_common::Item_param_val_native(THD *thd,
9147 Item_param *item,
9148 Native *to) const
9149 {
9150 return Time(thd, item).to_native(to, item->decimals);
9151 }
9152
9153
9154 /***************************************************************************/
9155
validate_implicit_default_value(THD * thd,const Column_definition & def) const9156 bool Type_handler::validate_implicit_default_value(THD *thd,
9157 const Column_definition &def) const
9158 {
9159 DBUG_EXECUTE_IF("validate_implicit_default_value_error", return true;);
9160 return false;
9161 }
9162
9163
validate_implicit_default_value(THD * thd,const Column_definition & def) const9164 bool Type_handler_date_common::validate_implicit_default_value(THD *thd,
9165 const Column_definition &def) const
9166 {
9167 return thd->variables.sql_mode & MODE_NO_ZERO_DATE;
9168 }
9169
9170
validate_implicit_default_value(THD * thd,const Column_definition & def) const9171 bool Type_handler_datetime_common::validate_implicit_default_value(THD *thd,
9172 const Column_definition &def) const
9173 {
9174 return thd->variables.sql_mode & MODE_NO_ZERO_DATE;
9175 }
9176
9177
9178 /***************************************************************************/
9179
default_value() const9180 const Name & Type_handler_row::default_value() const
9181 {
9182 DBUG_ASSERT(0);
9183 static Name def(STRING_WITH_LEN(""));
9184 return def;
9185 }
9186
default_value() const9187 const Name & Type_handler_numeric::default_value() const
9188 {
9189 static Name def(STRING_WITH_LEN("0"));
9190 return def;
9191 }
9192
default_value() const9193 const Name & Type_handler_string_result::default_value() const
9194 {
9195 static Name def(STRING_WITH_LEN(""));
9196 return def;
9197 }
9198
default_value() const9199 const Name & Type_handler_time_common::default_value() const
9200 {
9201 static Name def(STRING_WITH_LEN("00:00:00"));
9202 return def;
9203 }
9204
default_value() const9205 const Name & Type_handler_date_common::default_value() const
9206 {
9207 static Name def(STRING_WITH_LEN("0000-00-00"));
9208 return def;
9209 }
9210
default_value() const9211 const Name & Type_handler_datetime_common::default_value() const
9212 {
9213 static Name def(STRING_WITH_LEN("0000-00-00 00:00:00"));
9214 return def;
9215 }
9216
default_value() const9217 const Name & Type_handler_timestamp_common::default_value() const
9218 {
9219 static Name def(STRING_WITH_LEN("0000-00-00 00:00:00"));
9220 return def;
9221 }
9222
9223 /***************************************************************************/
9224
Column_definition_data_type_info_image(Binary_string * to,const Column_definition & def) const9225 bool Type_handler::Column_definition_data_type_info_image(Binary_string *to,
9226 const Column_definition &def)
9227 const
9228 {
9229 // Have *some* columns write type info (let's use string fields as an example)
9230 DBUG_EXECUTE_IF("frm_data_type_info_emulate",
9231 if (cmp_type() == STRING_RESULT)
9232 return to->append("x", 1) ||
9233 to->append(name().lex_cstring()););
9234 if (type_collection() != &type_collection_std)
9235 return to->append(name().lex_cstring());
9236 return false;
9237 }
9238
9239
9240 /***************************************************************************/
9241
9242 void
partition_field_type_not_allowed(const LEX_CSTRING & field_name)9243 Type_handler::partition_field_type_not_allowed(const LEX_CSTRING &field_name)
9244 {
9245 my_error(ER_FIELD_TYPE_NOT_ALLOWED_AS_PARTITION_FIELD, MYF(0),
9246 field_name.str);
9247 }
9248
9249
9250 bool
partition_field_check_result_type(Item * item,Item_result expected_type)9251 Type_handler::partition_field_check_result_type(Item *item,
9252 Item_result expected_type)
9253 {
9254 if (item->result_type() != expected_type)
9255 {
9256 my_error(ER_WRONG_TYPE_COLUMN_VALUE_ERROR, MYF(0));
9257 return TRUE;
9258 }
9259 return false;
9260 }
9261
9262
9263 bool
partition_field_check(const LEX_CSTRING & field_name,Item * item_expr) const9264 Type_handler_blob_common::partition_field_check(const LEX_CSTRING &field_name,
9265 Item *item_expr) const
9266 {
9267 my_error(ER_BLOB_FIELD_IN_PART_FUNC_ERROR, MYF(0));
9268 return true;
9269 }
9270
9271
9272 bool
partition_field_append_value(String * str,Item * item_expr,CHARSET_INFO * field_cs,partition_value_print_mode_t mode) const9273 Type_handler_general_purpose_int::partition_field_append_value(
9274 String *str,
9275 Item *item_expr,
9276 CHARSET_INFO *field_cs,
9277 partition_value_print_mode_t mode)
9278 const
9279 {
9280 DBUG_ASSERT(item_expr->cmp_type() == INT_RESULT);
9281 StringBuffer<21> tmp;
9282 longlong value= item_expr->val_int();
9283 tmp.set(value, system_charset_info);
9284 return str->append(tmp);
9285 }
9286
9287
9288 /*
9289 Append an Item value to a String using a desired mode.
9290
9291 @param [OUT] str The string to append the value to.
9292 @param item_expr The item to get the value from
9293 @param field_cs The character set of the value owner field.
9294 @param mode The mode.
9295 @retval true on error
9296 @retval false on success
9297
9298 The value is added using system_charset_info (no matter what mode is).
9299
9300 (1) If mode is equal to PARTITION_VALUE_PRINT_MODE_FRM,
9301 the value is appended as a pure ASCII string in the format '_latin1 0xdf',
9302 i.e. a character set introducer followed by a hex hybrid.
9303
9304 Before appending, we value is first converted to field_cs.
9305 a) If the conversion succeeds, the value is printed in its field_cs
9306 represenation.
9307 b) If the conversion fails, the value is printed without conversion,
9308 using the original character set introducer followed by the original
9309 string hex representation.
9310 In this case, open_table_from_share() will later notice that
9311 the value cannot be actually stored to the field, and report
9312 the error. So here we don't need to report errors such as
9313 ER_PARTITION_FUNCTION_IS_NOT_ALLOWED.
9314
9315 (2) If the mode is equal to PARTITION_VALUE_PRINT_SHOW,
9316 then the value is needed for:
9317 - SHOW CREATE TABLE, or
9318 - the PARTITION_DESCRIPTION column in a
9319 INFORMATION_SCHEMA.PARTITION query.
9320
9321 The value generated here will be later sent to the client and
9322 therefore will be converted to the client character set in the protocol.
9323
9324 We try to generate the value as a simple quoted utf8 string without
9325 introducers (e.g. 'utf8-string') when possible, to make it:
9326 - as human readable as possible
9327 - but still safe for mysqldump purposes.
9328
9329 Simple quoted utf8 string is generated when these two conditions are true
9330 at the same time:
9331 a) The value can be safely converted to utf8,
9332 so we can return it without data loss from this function.
9333 b) The value can be safely converted to the client character set,
9334 so we can convert it later without data loss to the client character
9335 set in the protocol.
9336
9337 If one of the conditions fail, the value is returned using
9338 PARTITION_VALUE_PRINT_MODE_FRM representation. See (1).
9339 */
partition_field_append_value(String * str,Item * item_expr,CHARSET_INFO * field_cs,partition_value_print_mode_t mode) const9340 bool Type_handler::partition_field_append_value(
9341 String *str,
9342 Item *item_expr,
9343 CHARSET_INFO *field_cs,
9344 partition_value_print_mode_t mode)
9345 const
9346 {
9347 DBUG_ASSERT(cmp_type() != INT_RESULT);
9348 StringBuffer<MAX_KEY_LENGTH> buf;
9349 String *res;
9350
9351 if (!(res= item_expr->val_str(&buf)))
9352 return str->append(STRING_WITH_LEN("NULL"), system_charset_info);
9353
9354 if (!res->length())
9355 return str->append(STRING_WITH_LEN("''"), system_charset_info);
9356
9357 if (mode == PARTITION_VALUE_PRINT_MODE_FRM ||
9358 !res->can_be_safely_converted_to(current_thd->
9359 variables.character_set_client) ||
9360 !res->can_be_safely_converted_to(system_charset_info))
9361 {
9362 StringBuffer<64> buf2;
9363 uint cnverr2= 0;
9364 buf2.copy(res->ptr(), res->length(), res->charset(), field_cs, &cnverr2);
9365 if (!cnverr2)
9366 return str->append_introducer_and_hex(&buf2);
9367 return str->append_introducer_and_hex(res);
9368 }
9369
9370 StringBuffer<64> val(system_charset_info);
9371 uint cnverr= 0;
9372 val.copy(res->ptr(), res->length(), res->charset(),
9373 system_charset_info, &cnverr);
9374 append_unescaped(str, val.ptr(), val.length());
9375 return false;
9376 }
9377
9378
can_return_extract_source(interval_type int_type) const9379 bool Type_handler::can_return_extract_source(interval_type int_type) const
9380 {
9381 return type_collection() == &type_collection_std;
9382 }
9383
9384 /***************************************************************************/
9385
collation_specific_name() const9386 LEX_CSTRING Charset::collation_specific_name() const
9387 {
9388 /*
9389 User defined collations can provide arbitrary names
9390 for character sets and collations, so a collation
9391 name not necessarily starts with the character set name.
9392 */
9393 LEX_CSTRING retval;
9394 size_t csname_length= strlen(m_charset->csname);
9395 if (strncmp(m_charset->name, m_charset->csname, csname_length))
9396 {
9397 retval.str= NULL;
9398 retval.length= 0;
9399 return retval;
9400 }
9401 const char *ptr= m_charset->name + csname_length;
9402 retval.str= ptr;
9403 retval.length= strlen(ptr);
9404 return retval;
9405 }
9406
9407
9408 bool
encoding_allows_reinterpret_as(const CHARSET_INFO * cs) const9409 Charset::encoding_allows_reinterpret_as(const CHARSET_INFO *cs) const
9410 {
9411 if (!strcmp(m_charset->csname, cs->csname))
9412 return true;
9413
9414 if (!strcmp(m_charset->csname, MY_UTF8MB3) &&
9415 !strcmp(cs->csname, MY_UTF8MB4))
9416 return true;
9417
9418 /*
9419 Originally we allowed here instat ALTER for ASCII-to-LATIN1
9420 and UCS2-to-UTF16, but this was wrong:
9421 - MariaDB's ascii is not a subset for 8-bit character sets
9422 like latin1, because it allows storing bytes 0x80..0xFF as
9423 "unassigned" characters (see MDEV-19285).
9424 - MariaDB's ucs2 (as in Unicode-1.1) is not a subset for UTF16,
9425 because they treat surrogate codes differently (MDEV-19284).
9426 */
9427 return false;
9428 }
9429
9430
9431 bool
eq_collation_specific_names(CHARSET_INFO * cs) const9432 Charset::eq_collation_specific_names(CHARSET_INFO *cs) const
9433 {
9434 LEX_CSTRING name0= collation_specific_name();
9435 LEX_CSTRING name1= Charset(cs).collation_specific_name();
9436 return name0.length && !cmp(&name0, &name1);
9437 }
9438
initialize_data_type_plugin(st_plugin_int * plugin)9439 int initialize_data_type_plugin(st_plugin_int *plugin)
9440 {
9441 st_mariadb_data_type *data= (st_mariadb_data_type*) plugin->plugin->info;
9442 data->type_handler->set_name(Name(plugin->name));
9443 if (plugin->plugin->init && plugin->plugin->init(NULL))
9444 {
9445 sql_print_error("Plugin '%s' init function returned error.",
9446 plugin->name.str);
9447 return 1;
9448 }
9449 return 0;
9450 }
9451