1 /*
2 Copyright (c) 2015, 2020, 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_const.h"
20 #include "sql_class.h"
21 #include "sql_time.h"
22 #include "sql_string.h"
23 #include "item.h"
24 #include "log.h"
25
26 Type_handler_row type_handler_row;
27
28 Type_handler_null type_handler_null;
29
30 Type_handler_tiny type_handler_tiny;
31 Type_handler_short type_handler_short;
32 Type_handler_long type_handler_long;
33 Type_handler_int24 type_handler_int24;
34 Type_handler_longlong type_handler_longlong;
35 Type_handler_longlong type_handler_ulonglong; // Only used for CAST() for now
36 Type_handler_vers_trx_id type_handler_vers_trx_id;
37 Type_handler_float type_handler_float;
38 Type_handler_double type_handler_double;
39 Type_handler_bit type_handler_bit;
40
41 Type_handler_olddecimal type_handler_olddecimal;
42 Type_handler_newdecimal type_handler_newdecimal;
43
44 Type_handler_year type_handler_year;
45 Type_handler_time type_handler_time;
46 Type_handler_date type_handler_date;
47 Type_handler_timestamp type_handler_timestamp;
48 Type_handler_timestamp2 type_handler_timestamp2;
49 Type_handler_datetime type_handler_datetime;
50 Type_handler_time2 type_handler_time2;
51 Type_handler_newdate type_handler_newdate;
52 Type_handler_datetime2 type_handler_datetime2;
53
54 Type_handler_enum type_handler_enum;
55 Type_handler_set type_handler_set;
56
57 Type_handler_string type_handler_string;
58 Type_handler_var_string type_handler_var_string;
59 Type_handler_varchar type_handler_varchar;
60 static Type_handler_varchar_compressed type_handler_varchar_compressed;
61
62 Type_handler_tiny_blob type_handler_tiny_blob;
63 Type_handler_medium_blob type_handler_medium_blob;
64 Type_handler_long_blob type_handler_long_blob;
65 Type_handler_blob type_handler_blob;
66 static Type_handler_blob_compressed type_handler_blob_compressed;
67
68 #ifdef HAVE_SPATIAL
69 Type_handler_geometry type_handler_geometry;
70 #endif
71
72
schema() const73 Schema *Type_handler::schema() const
74 {
75 return &mariadb_schema;
76 }
77
78
init()79 bool Type_handler_data::init()
80 {
81 #ifdef HAVE_SPATIAL
82
83 #ifndef DBUG_OFF
84 if (m_type_aggregator_non_commutative_test.add(&type_handler_geometry,
85 &type_handler_geometry,
86 &type_handler_geometry) ||
87 m_type_aggregator_non_commutative_test.add(&type_handler_geometry,
88 &type_handler_varchar,
89 &type_handler_long_blob))
90 return true;
91 #endif
92
93 return
94 m_type_aggregator_for_result.add(&type_handler_geometry,
95 &type_handler_null,
96 &type_handler_geometry) ||
97 m_type_aggregator_for_result.add(&type_handler_geometry,
98 &type_handler_geometry,
99 &type_handler_geometry) ||
100 m_type_aggregator_for_result.add(&type_handler_geometry,
101 &type_handler_tiny_blob,
102 &type_handler_long_blob) ||
103 m_type_aggregator_for_result.add(&type_handler_geometry,
104 &type_handler_blob,
105 &type_handler_long_blob) ||
106 m_type_aggregator_for_result.add(&type_handler_geometry,
107 &type_handler_medium_blob,
108 &type_handler_long_blob) ||
109 m_type_aggregator_for_result.add(&type_handler_geometry,
110 &type_handler_long_blob,
111 &type_handler_long_blob) ||
112 m_type_aggregator_for_result.add(&type_handler_geometry,
113 &type_handler_varchar,
114 &type_handler_long_blob) ||
115 m_type_aggregator_for_result.add(&type_handler_geometry,
116 &type_handler_string,
117 &type_handler_long_blob) ||
118 m_type_aggregator_for_comparison.add(&type_handler_geometry,
119 &type_handler_geometry,
120 &type_handler_geometry) ||
121 m_type_aggregator_for_comparison.add(&type_handler_geometry,
122 &type_handler_null,
123 &type_handler_geometry) ||
124 m_type_aggregator_for_comparison.add(&type_handler_geometry,
125 &type_handler_long_blob,
126 &type_handler_long_blob);
127 #endif
128 return false;
129 }
130
131
132 Type_handler_data *type_handler_data= NULL;
133
134
to_string(String * val_buffer,uint dec) const135 bool Float::to_string(String *val_buffer, uint dec) const
136 {
137 uint to_length= 70;
138 if (val_buffer->alloc(to_length))
139 return true;
140
141 char *to=(char*) val_buffer->ptr();
142 size_t len;
143
144 if (dec >= FLOATING_POINT_DECIMALS)
145 len= my_gcvt(m_value, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
146 else
147 {
148 /*
149 We are safe here because the buffer length is 70, and
150 fabs(float) < 10^39, dec < FLOATING_POINT_DECIMALS. So the resulting string
151 will be not longer than 69 chars + terminating '\0'.
152 */
153 len= my_fcvt(m_value, (int) dec, to, NULL);
154 }
155 val_buffer->length((uint) len);
156 val_buffer->set_charset(&my_charset_numeric);
157 return false;
158 }
159
160
make_from_item(Item * item,const Options opt)161 void Time::make_from_item(Item *item, const Options opt)
162 {
163 if (item->get_date(this, opt.get_date_flags()))
164 time_type= MYSQL_TIMESTAMP_NONE;
165 else
166 valid_MYSQL_TIME_to_valid_value(opt);
167 }
168
169
make_from_item(THD * thd,Item * item,sql_mode_t flags)170 void Temporal_with_date::make_from_item(THD *thd, Item *item, sql_mode_t flags)
171 {
172 flags&= ~TIME_TIME_ONLY;
173 /*
174 Some TIME type items return error when trying to do get_date()
175 without TIME_TIME_ONLY set (e.g. Item_field for Field_time).
176 In the SQL standard time->datetime conversion mode we add TIME_TIME_ONLY.
177 In the legacy time->datetime conversion mode we do not add TIME_TIME_ONLY
178 and leave it to get_date() to check date.
179 */
180 ulonglong time_flag= (item->field_type() == MYSQL_TYPE_TIME &&
181 !(thd->variables.old_behavior & OLD_MODE_ZERO_DATE_TIME_CAST)) ?
182 TIME_TIME_ONLY : 0;
183 if (item->get_date(this, flags | time_flag))
184 time_type= MYSQL_TIMESTAMP_NONE;
185 else if (time_type == MYSQL_TIMESTAMP_TIME)
186 {
187 MYSQL_TIME tmp;
188 if (time_to_datetime_with_warn(thd, this, &tmp, flags))
189 time_type= MYSQL_TIMESTAMP_NONE;
190 else
191 *(static_cast<MYSQL_TIME*>(this))= tmp;
192 }
193 }
194
195
count_max_decimals(Item ** item,uint nitems)196 uint Type_std_attributes::count_max_decimals(Item **item, uint nitems)
197 {
198 uint res= 0;
199 for (uint i= 0; i < nitems; i++)
200 set_if_bigger(res, item[i]->decimals);
201 return res;
202 }
203
204
205 /**
206 Set max_length/decimals of function if function is fixed point and
207 result length/precision depends on argument ones.
208 */
209
count_decimal_length(Item ** item,uint nitems)210 void Type_std_attributes::count_decimal_length(Item **item, uint nitems)
211 {
212 int max_int_part= 0;
213 decimals= 0;
214 unsigned_flag= 1;
215 for (uint i=0 ; i < nitems ; i++)
216 {
217 set_if_bigger(decimals, item[i]->decimals);
218 set_if_bigger(max_int_part, item[i]->decimal_int_part());
219 set_if_smaller(unsigned_flag, item[i]->unsigned_flag);
220 }
221 int precision= MY_MIN(max_int_part + decimals, DECIMAL_MAX_PRECISION);
222 fix_char_length(my_decimal_precision_to_length_no_truncation(precision,
223 (uint8) decimals,
224 unsigned_flag));
225 }
226
227
228 /**
229 Set max_length of if it is maximum length of its arguments.
230 */
231
count_only_length(Item ** item,uint nitems)232 void Type_std_attributes::count_only_length(Item **item, uint nitems)
233 {
234 uint32 char_length= 0;
235 unsigned_flag= 0;
236 for (uint i= 0; i < nitems ; i++)
237 {
238 set_if_bigger(char_length, item[i]->max_char_length());
239 set_if_bigger(unsigned_flag, item[i]->unsigned_flag);
240 }
241 fix_char_length(char_length);
242 }
243
244
count_octet_length(Item ** item,uint nitems)245 void Type_std_attributes::count_octet_length(Item **item, uint nitems)
246 {
247 max_length= 0;
248 unsigned_flag= 0;
249 for (uint i= 0; i < nitems ; i++)
250 {
251 set_if_bigger(max_length, item[i]->max_length);
252 set_if_bigger(unsigned_flag, item[i]->unsigned_flag);
253 }
254 }
255
256
257 /**
258 Set max_length/decimals of function if function is floating point and
259 result length/precision depends on argument ones.
260 */
261
count_real_length(Item ** items,uint nitems)262 void Type_std_attributes::count_real_length(Item **items, uint nitems)
263 {
264 uint32 length= 0;
265 decimals= 0;
266 max_length= 0;
267 unsigned_flag= false;
268 for (uint i=0 ; i < nitems ; i++)
269 {
270 if (decimals < FLOATING_POINT_DECIMALS)
271 {
272 set_if_bigger(decimals, items[i]->decimals);
273 /* Will be ignored if items[i]->decimals >= FLOATING_POINT_DECIMALS */
274 set_if_bigger(length, (items[i]->max_length - items[i]->decimals));
275 }
276 set_if_bigger(max_length, items[i]->max_length);
277 }
278 if (decimals < FLOATING_POINT_DECIMALS)
279 {
280 max_length= length;
281 length+= decimals;
282 if (length < max_length) // If previous operation gave overflow
283 max_length= UINT_MAX32;
284 else
285 max_length= length;
286 }
287 // Corner case: COALESCE(DOUBLE(255,4), DOUBLE(255,3)) -> FLOAT(255, 4)
288 set_if_smaller(max_length, MAX_FIELD_CHARLENGTH);
289 }
290
291
292 /**
293 Calculate max_length and decimals for string functions.
294
295 @param field_type Field type.
296 @param items Argument array.
297 @param nitems Number of arguments.
298
299 @retval False on success, true on error.
300 */
count_string_length(const char * func_name,Item ** items,uint nitems)301 bool Type_std_attributes::count_string_length(const char *func_name,
302 Item **items, uint nitems)
303 {
304 if (agg_arg_charsets_for_string_result(collation, func_name,
305 items, nitems, 1))
306 return true;
307 if (collation.collation == &my_charset_bin)
308 count_octet_length(items, nitems);
309 else
310 count_only_length(items, nitems);
311 decimals= max_length ? NOT_FIXED_DEC : 0;
312 return false;
313 }
314
315
316 /**
317 This method is used by:
318 - Item_user_var_as_out_param::field_type()
319 - Item_func_udf_str::field_type()
320 - Item_empty_string::make_send_field()
321
322 TODO: type_handler_adjusted_to_max_octet_length() and string_type_handler()
323 provide very similar functionality, to properly choose between
324 VARCHAR/VARBINARY vs TEXT/BLOB variations taking into accoung maximum
325 possible octet length.
326
327 We should probably get rid of either of them and use the same method
328 all around the code.
329 */
330 const Type_handler *
string_type_handler(uint max_octet_length)331 Type_handler::string_type_handler(uint max_octet_length)
332 {
333 if (max_octet_length >= 16777216)
334 return &type_handler_long_blob;
335 else if (max_octet_length >= 65536)
336 return &type_handler_medium_blob;
337 else if (max_octet_length >= MAX_FIELD_VARCHARLENGTH)
338 return &type_handler_blob;
339 return &type_handler_varchar;
340 }
341
342
343 const Type_handler *
varstring_type_handler(const Item * item)344 Type_handler::varstring_type_handler(const Item *item)
345 {
346 if (!item->max_length)
347 return &type_handler_string;
348 if (item->too_big_for_varchar())
349 return blob_type_handler(item->max_length);
350 return &type_handler_varchar;
351 }
352
353
354 const Type_handler *
blob_type_handler(uint max_octet_length)355 Type_handler::blob_type_handler(uint max_octet_length)
356 {
357 if (max_octet_length <= 255)
358 return &type_handler_tiny_blob;
359 if (max_octet_length <= 65535)
360 return &type_handler_blob;
361 if (max_octet_length <= 16777215)
362 return &type_handler_medium_blob;
363 return &type_handler_long_blob;
364 }
365
366
367 const Type_handler *
blob_type_handler(const Item * item)368 Type_handler::blob_type_handler(const Item *item)
369 {
370 return blob_type_handler(item->max_length);
371 }
372
373 /**
374 This method is used by:
375 - Item_sum_hybrid, e.g. MAX(item), MIN(item).
376 - Item_func_set_user_var
377 */
378 const Type_handler *
type_handler_adjusted_to_max_octet_length(uint max_octet_length,CHARSET_INFO * cs) const379 Type_handler_string_result::type_handler_adjusted_to_max_octet_length(
380 uint max_octet_length,
381 CHARSET_INFO *cs) const
382 {
383 if (max_octet_length / cs->mbmaxlen <= CONVERT_IF_BIGGER_TO_BLOB)
384 return &type_handler_varchar; // See also Item::too_big_for_varchar()
385 if (max_octet_length >= 16777216)
386 return &type_handler_long_blob;
387 else if (max_octet_length >= 65536)
388 return &type_handler_medium_blob;
389 return &type_handler_blob;
390 }
391
392
charset_for_protocol(const Item * item) const393 CHARSET_INFO *Type_handler::charset_for_protocol(const Item *item) const
394 {
395 /*
396 For backward compatibility, to make numeric
397 data types return "binary" charset in client-side metadata.
398 */
399 return &my_charset_bin;
400 }
401
402
403 bool
Item_func_or_sum_illegal_param(const char * funcname) const404 Type_handler::Item_func_or_sum_illegal_param(const char *funcname) const
405 {
406 my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
407 name().ptr(), funcname);
408 return true;
409 }
410
411
412 bool
Item_func_or_sum_illegal_param(const Item_func_or_sum * it) const413 Type_handler::Item_func_or_sum_illegal_param(const Item_func_or_sum *it) const
414 {
415 return Item_func_or_sum_illegal_param(it->func_name());
416 }
417
418
419 CHARSET_INFO *
charset_for_protocol(const Item * item) const420 Type_handler_string_result::charset_for_protocol(const Item *item) const
421 {
422 return item->collation.collation;
423 }
424
425
426 const Type_handler *
get_handler_by_cmp_type(Item_result type)427 Type_handler::get_handler_by_cmp_type(Item_result type)
428 {
429 switch (type) {
430 case REAL_RESULT: return &type_handler_double;
431 case INT_RESULT: return &type_handler_longlong;
432 case DECIMAL_RESULT: return &type_handler_newdecimal;
433 case STRING_RESULT: return &type_handler_long_blob;
434 case TIME_RESULT: return &type_handler_datetime;
435 case ROW_RESULT: return &type_handler_row;
436 }
437 DBUG_ASSERT(0);
438 return &type_handler_string;
439 }
440
441
Type_handler_hybrid_field_type()442 Type_handler_hybrid_field_type::Type_handler_hybrid_field_type()
443 :m_type_handler(&type_handler_double)
444 {
445 }
446
447
448 /***************************************************************************/
449
450 /* number of bytes to store second_part part of the TIMESTAMP(N) */
451 uint Type_handler_timestamp::m_sec_part_bytes[MAX_DATETIME_PRECISION + 1]=
452 { 0, 1, 1, 2, 2, 3, 3 };
453
454 /* number of bytes to store DATETIME(N) */
455 uint Type_handler_datetime::m_hires_bytes[MAX_DATETIME_PRECISION + 1]=
456 { 5, 6, 6, 7, 7, 7, 8 };
457
458 /* number of bytes to store TIME(N) */
459 uint Type_handler_time::m_hires_bytes[MAX_DATETIME_PRECISION + 1]=
460 { 3, 4, 4, 5, 5, 5, 6 };
461
462 /***************************************************************************/
463 const Name Type_handler_row::m_name_row(STRING_WITH_LEN("row"));
464
465 const Name Type_handler_null::m_name_null(STRING_WITH_LEN("null"));
466
467 const Name
468 Type_handler_string::m_name_char(STRING_WITH_LEN("char")),
469 Type_handler_var_string::m_name_var_string(STRING_WITH_LEN("varchar")),
470 Type_handler_varchar::m_name_varchar(STRING_WITH_LEN("varchar")),
471 Type_handler_tiny_blob::m_name_tinyblob(STRING_WITH_LEN("tinyblob")),
472 Type_handler_medium_blob::m_name_mediumblob(STRING_WITH_LEN("mediumblob")),
473 Type_handler_long_blob::m_name_longblob(STRING_WITH_LEN("longblob")),
474 Type_handler_blob::m_name_blob(STRING_WITH_LEN("blob"));
475
476 const Name
477 Type_handler_enum::m_name_enum(STRING_WITH_LEN("enum")),
478 Type_handler_set::m_name_set(STRING_WITH_LEN("set"));
479
480 const Name
481 Type_handler_tiny::m_name_tiny(STRING_WITH_LEN("tinyint")),
482 Type_handler_short::m_name_short(STRING_WITH_LEN("smallint")),
483 Type_handler_long::m_name_int(STRING_WITH_LEN("int")),
484 Type_handler_longlong::m_name_longlong(STRING_WITH_LEN("bigint")),
485 Type_handler_int24::m_name_mediumint(STRING_WITH_LEN("mediumint")),
486 Type_handler_year::m_name_year(STRING_WITH_LEN("year")),
487 Type_handler_bit::m_name_bit(STRING_WITH_LEN("bit"));
488
489 const Name
490 Type_handler_float::m_name_float(STRING_WITH_LEN("float")),
491 Type_handler_double::m_name_double(STRING_WITH_LEN("double"));
492
493 const Name
494 Type_handler_olddecimal::m_name_decimal(STRING_WITH_LEN("decimal")),
495 Type_handler_newdecimal::m_name_decimal(STRING_WITH_LEN("decimal"));
496
497 const Name
498 Type_handler_time_common::m_name_time(STRING_WITH_LEN("time")),
499 Type_handler_date_common::m_name_date(STRING_WITH_LEN("date")),
500 Type_handler_datetime_common::m_name_datetime(STRING_WITH_LEN("datetime")),
501 Type_handler_timestamp_common::m_name_timestamp(STRING_WITH_LEN("timestamp"));
502
503
504 const Type_limits_int
505 Type_handler_tiny::m_limits_sint8= Type_limits_sint8(),
506 Type_handler_tiny::m_limits_uint8= Type_limits_uint8(),
507 Type_handler_short::m_limits_sint16= Type_limits_sint16(),
508 Type_handler_short::m_limits_uint16= Type_limits_uint16(),
509 Type_handler_int24::m_limits_sint24= Type_limits_sint24(),
510 Type_handler_int24::m_limits_uint24= Type_limits_uint24(),
511 Type_handler_long::m_limits_sint32= Type_limits_sint32(),
512 Type_handler_long::m_limits_uint32= Type_limits_uint32(),
513 Type_handler_longlong::m_limits_sint64= Type_limits_sint64(),
514 Type_handler_longlong::m_limits_uint64= Type_limits_uint64();
515
516
517 /***************************************************************************/
518
type_handler_for_comparison() const519 const Type_handler *Type_handler_null::type_handler_for_comparison() const
520 {
521 return &type_handler_null;
522 }
523
524
type_handler_for_comparison() const525 const Type_handler *Type_handler_int_result::type_handler_for_comparison() const
526 {
527 return &type_handler_longlong;
528 }
529
530
type_handler_for_comparison() const531 const Type_handler *Type_handler_string_result::type_handler_for_comparison() const
532 {
533 return &type_handler_long_blob;
534 }
535
536
type_handler_for_comparison() const537 const Type_handler *Type_handler_decimal_result::type_handler_for_comparison() const
538 {
539 return &type_handler_newdecimal;
540 }
541
542
type_handler_for_comparison() const543 const Type_handler *Type_handler_real_result::type_handler_for_comparison() const
544 {
545 return &type_handler_double;
546 }
547
548
type_handler_for_comparison() const549 const Type_handler *Type_handler_time_common::type_handler_for_comparison() const
550 {
551 return &type_handler_time;
552 }
553
type_handler_for_comparison() const554 const Type_handler *Type_handler_date_common::type_handler_for_comparison() const
555 {
556 return &type_handler_newdate;
557 }
558
559
type_handler_for_comparison() const560 const Type_handler *Type_handler_datetime_common::type_handler_for_comparison() const
561 {
562 return &type_handler_datetime;
563 }
564
565
type_handler_for_comparison() const566 const Type_handler *Type_handler_timestamp_common::type_handler_for_comparison() const
567 {
568 return &type_handler_datetime;
569 }
570
571
type_handler_for_comparison() const572 const Type_handler *Type_handler_row::type_handler_for_comparison() const
573 {
574 return &type_handler_row;
575 }
576
577 /***************************************************************************/
578
type_handler_for_item_field() const579 const Type_handler *Type_handler_typelib::type_handler_for_item_field() const
580 {
581 return &type_handler_string;
582 }
583
584
cast_to_int_type_handler() const585 const Type_handler *Type_handler_typelib::cast_to_int_type_handler() const
586 {
587 return &type_handler_longlong;
588 }
589
590
591 /***************************************************************************/
592
593 bool
aggregate_for_result(const Type_handler * other)594 Type_handler_hybrid_field_type::aggregate_for_result(const Type_handler *other)
595 {
596 if (m_type_handler->is_traditional_type() && other->is_traditional_type())
597 {
598 m_type_handler=
599 Type_handler::aggregate_for_result_traditional(m_type_handler, other);
600 return false;
601 }
602 other= type_handler_data->
603 m_type_aggregator_for_result.find_handler(m_type_handler, other);
604 if (!other)
605 return true;
606 m_type_handler= other;
607 return false;
608 }
609
610
611 const Type_handler *
type_handler_long_or_longlong(uint max_char_length)612 Type_handler::type_handler_long_or_longlong(uint max_char_length)
613 {
614 if (max_char_length <= MY_INT32_NUM_DECIMAL_DIGITS - 2)
615 return &type_handler_long;
616 return &type_handler_longlong;
617 }
618
619 /*
620 This method is called for CASE (and its abbreviations) and LEAST/GREATEST
621 when data type aggregation returned LONGLONG and there were some BIT
622 expressions. This helps to adjust the data type from LONGLONG to LONG
623 if all expressions fit.
624 */
625 const Type_handler *
bit_and_int_mixture_handler(uint max_char_length)626 Type_handler::bit_and_int_mixture_handler(uint max_char_length)
627 {
628 if (max_char_length <= MY_INT32_NUM_DECIMAL_DIGITS)
629 return &type_handler_long;
630 return &type_handler_longlong;
631 }
632
633
634 /**
635 @brief Aggregates field types from the array of items.
636
637 @param[in] items array of items to aggregate the type from
638 @param[in] nitems number of items in the array
639 @param[in] treat_bit_as_number - if BIT should be aggregated to a non-BIT
640 counterpart as a LONGLONG number or as a VARBINARY string.
641
642 Currently behaviour depends on the function:
643 - LEAST/GREATEST treat BIT as VARBINARY when
644 aggregating with a non-BIT counterpart.
645 Note, UNION also works this way.
646
647 - CASE, COALESCE, IF, IFNULL treat BIT as LONGLONG when
648 aggregating with a non-BIT counterpart;
649
650 This inconsistency may be changed in the future. See MDEV-8867.
651
652 Note, independently from "treat_bit_as_number":
653 - a single BIT argument gives BIT as a result
654 - two BIT couterparts give BIT as a result
655
656 @details This function aggregates field types from the array of items.
657 Found type is supposed to be used later as the result field type
658 of a multi-argument function.
659 Aggregation itself is performed by Type_handler::aggregate_for_result().
660
661 @note The term "aggregation" is used here in the sense of inferring the
662 result type of a function from its argument types.
663
664 @retval false - on success
665 @retval true - on error
666 */
667
668 bool
aggregate_for_result(const char * funcname,Item ** items,uint nitems,bool treat_bit_as_number)669 Type_handler_hybrid_field_type::aggregate_for_result(const char *funcname,
670 Item **items, uint nitems,
671 bool treat_bit_as_number)
672 {
673 bool bit_and_non_bit_mixture_found= false;
674 uint32 max_display_length;
675 if (!nitems || items[0]->result_type() == ROW_RESULT)
676 {
677 DBUG_ASSERT(0);
678 set_handler(&type_handler_null);
679 return true;
680 }
681 set_handler(items[0]->type_handler());
682 max_display_length= items[0]->max_display_length();
683 for (uint i= 1 ; i < nitems ; i++)
684 {
685 const Type_handler *cur= items[i]->type_handler();
686 set_if_bigger(max_display_length, items[i]->max_display_length());
687 if (treat_bit_as_number &&
688 ((type_handler() == &type_handler_bit) ^ (cur == &type_handler_bit)))
689 {
690 bit_and_non_bit_mixture_found= true;
691 if (type_handler() == &type_handler_bit)
692 set_handler(&type_handler_longlong); // BIT + non-BIT
693 else
694 cur= &type_handler_longlong; // non-BIT + BIT
695 }
696 if (aggregate_for_result(cur))
697 {
698 my_error(ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION, MYF(0),
699 type_handler()->name().ptr(), cur->name().ptr(), funcname);
700 return true;
701 }
702 }
703 if (bit_and_non_bit_mixture_found && type_handler() == &type_handler_longlong)
704 set_handler(Type_handler::bit_and_int_mixture_handler(max_display_length));
705 return false;
706 }
707
708 /**
709 Collect built-in data type handlers for comparison.
710 This method is very similar to item_cmp_type() defined in item.cc.
711 Now they coexist. Later item_cmp_type() will be removed.
712 In addition to item_cmp_type(), this method correctly aggregates
713 TIME with DATETIME/TIMESTAMP/DATE, so no additional find_date_time_item()
714 is needed after this call.
715 */
716
717 bool
aggregate_for_comparison(const Type_handler * h)718 Type_handler_hybrid_field_type::aggregate_for_comparison(const Type_handler *h)
719 {
720 DBUG_ASSERT(m_type_handler == m_type_handler->type_handler_for_comparison());
721 DBUG_ASSERT(h == h->type_handler_for_comparison());
722
723 if (!m_type_handler->is_traditional_type() ||
724 !h->is_traditional_type())
725 {
726 h= type_handler_data->
727 m_type_aggregator_for_comparison.find_handler(m_type_handler, h);
728 if (!h)
729 return true;
730 m_type_handler= h;
731 DBUG_ASSERT(m_type_handler == m_type_handler->type_handler_for_comparison());
732 return false;
733 }
734
735 Item_result a= cmp_type();
736 Item_result b= h->cmp_type();
737 if (a == STRING_RESULT && b == STRING_RESULT)
738 m_type_handler= &type_handler_long_blob;
739 else if (a == INT_RESULT && b == INT_RESULT)
740 m_type_handler= &type_handler_longlong;
741 else if (a == ROW_RESULT || b == ROW_RESULT)
742 m_type_handler= &type_handler_row;
743 else if (a == TIME_RESULT || b == TIME_RESULT)
744 {
745 if ((a == TIME_RESULT) + (b == TIME_RESULT) == 1)
746 {
747 /*
748 We're here if there's only one temporal data type:
749 either m_type_handler or h.
750 */
751 if (b == TIME_RESULT)
752 m_type_handler= h; // Temporal types bit non-temporal types
753 }
754 else
755 {
756 /*
757 We're here if both m_type_handler and h are temporal data types.
758 - If both data types are TIME, we preserve TIME.
759 - If both data types are DATE, we preserve DATE.
760 Preserving DATE is needed for EXPLAIN FORMAT=JSON,
761 to print DATE constants using proper format:
762 'YYYY-MM-DD' rather than 'YYYY-MM-DD 00:00:00'.
763 */
764 if (m_type_handler->field_type() != h->field_type())
765 m_type_handler= &type_handler_datetime;
766 }
767 }
768 else if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
769 (b == INT_RESULT || b == DECIMAL_RESULT))
770 {
771 m_type_handler= &type_handler_newdecimal;
772 }
773 else
774 m_type_handler= &type_handler_double;
775 DBUG_ASSERT(m_type_handler == m_type_handler->type_handler_for_comparison());
776 return false;
777 }
778
779
780 /**
781 Aggregate data type handler for LEAST/GRATEST.
782 aggregate_for_min_max() is close to aggregate_for_comparison(),
783 but tries to preserve the exact type handler for string, int and temporal
784 data types (instead of converting to super-types).
785 FLOAT is not preserved and is converted to its super-type (DOUBLE).
786 This should probably fixed eventually, for symmetry.
787 */
788
789 bool
aggregate_for_min_max(const Type_handler * h)790 Type_handler_hybrid_field_type::aggregate_for_min_max(const Type_handler *h)
791 {
792 if (!m_type_handler->is_traditional_type() ||
793 !h->is_traditional_type())
794 {
795 /*
796 If at least one data type is non-traditional,
797 do aggregation for result immediately.
798 For now we suppose that these two expressions:
799 - LEAST(type1, type2)
800 - COALESCE(type1, type2)
801 return the same data type (or both expressions return error)
802 if type1 and/or type2 are non-traditional.
803 This may change in the future.
804 */
805 h= type_handler_data->
806 m_type_aggregator_for_result.find_handler(m_type_handler, h);
807 if (!h)
808 return true;
809 m_type_handler= h;
810 return false;
811 }
812
813 Item_result a= cmp_type();
814 Item_result b= h->cmp_type();
815 DBUG_ASSERT(a != ROW_RESULT); // Disallowed by check_cols() in fix_fields()
816 DBUG_ASSERT(b != ROW_RESULT); // Disallowed by check_cols() in fix_fields()
817
818 if (a == STRING_RESULT && b == STRING_RESULT)
819 m_type_handler=
820 Type_handler::aggregate_for_result_traditional(m_type_handler, h);
821 else if (a == INT_RESULT && b == INT_RESULT)
822 {
823 // BIT aggregates with non-BIT as BIGINT
824 if (m_type_handler != h)
825 {
826 if (m_type_handler == &type_handler_bit)
827 m_type_handler= &type_handler_longlong;
828 else if (h == &type_handler_bit)
829 h= &type_handler_longlong;
830 }
831 m_type_handler=
832 Type_handler::aggregate_for_result_traditional(m_type_handler, h);
833 }
834 else if (a == TIME_RESULT || b == TIME_RESULT)
835 {
836 if ((a == TIME_RESULT) + (b == TIME_RESULT) == 1)
837 {
838 /*
839 We're here if there's only one temporal data type:
840 either m_type_handler or h.
841 */
842 if (b == TIME_RESULT)
843 m_type_handler= h; // Temporal types bit non-temporal types
844 }
845 else
846 {
847 /*
848 We're here if both m_type_handler and h are temporal data types.
849 */
850 m_type_handler=
851 Type_handler::aggregate_for_result_traditional(m_type_handler, h);
852 }
853 }
854 else if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
855 (b == INT_RESULT || b == DECIMAL_RESULT))
856 {
857 m_type_handler= &type_handler_newdecimal;
858 }
859 else
860 {
861 // Preserve FLOAT if two FLOATs, set to DOUBLE otherwise.
862 if (m_type_handler != &type_handler_float || h != &type_handler_float)
863 m_type_handler= &type_handler_double;
864 }
865 return false;
866 }
867
868
869 bool
aggregate_for_min_max(const char * funcname,Item ** items,uint nitems)870 Type_handler_hybrid_field_type::aggregate_for_min_max(const char *funcname,
871 Item **items, uint nitems)
872 {
873 bool bit_and_non_bit_mixture_found= false;
874 uint32 max_display_length;
875 // LEAST/GREATEST require at least two arguments
876 DBUG_ASSERT(nitems > 1);
877 set_handler(items[0]->type_handler());
878 max_display_length= items[0]->max_display_length();
879 for (uint i= 1; i < nitems; i++)
880 {
881 const Type_handler *cur= items[i]->type_handler();
882 set_if_bigger(max_display_length, items[i]->max_display_length());
883 // Check if BIT + non-BIT, or non-BIT + BIT
884 bit_and_non_bit_mixture_found|= (m_type_handler == &type_handler_bit) !=
885 (cur == &type_handler_bit);
886 if (aggregate_for_min_max(cur))
887 {
888 my_error(ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION, MYF(0),
889 type_handler()->name().ptr(), cur->name().ptr(), funcname);
890 return true;
891 }
892 }
893 if (bit_and_non_bit_mixture_found && type_handler() == &type_handler_longlong)
894 set_handler(Type_handler::bit_and_int_mixture_handler(max_display_length));
895 return false;
896 }
897
898
899 const Type_handler *
aggregate_for_num_op_traditional(const Type_handler * h0,const Type_handler * h1)900 Type_handler::aggregate_for_num_op_traditional(const Type_handler *h0,
901 const Type_handler *h1)
902 {
903 Item_result r0= h0->cmp_type();
904 Item_result r1= h1->cmp_type();
905
906 if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
907 r0 == STRING_RESULT || r1 ==STRING_RESULT)
908 return &type_handler_double;
909
910 if (r0 == TIME_RESULT || r1 == TIME_RESULT)
911 return &type_handler_datetime;
912
913 if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
914 return &type_handler_newdecimal;
915
916 DBUG_ASSERT(r0 == INT_RESULT && r1 == INT_RESULT);
917 return &type_handler_longlong;
918 }
919
920
921 const Type_aggregator::Pair*
find_pair(const Type_handler * handler1,const Type_handler * handler2) const922 Type_aggregator::find_pair(const Type_handler *handler1,
923 const Type_handler *handler2) const
924 {
925 for (uint i= 0; i < m_array.elements(); i++)
926 {
927 const Pair& el= m_array.at(i);
928 if (el.eq(handler1, handler2) ||
929 (m_is_commutative && el.eq(handler2, handler1)))
930 return ⪙
931 }
932 return NULL;
933 }
934
935
936 bool
aggregate_for_num_op(const Type_aggregator * agg,const Type_handler * h0,const Type_handler * h1)937 Type_handler_hybrid_field_type::aggregate_for_num_op(const Type_aggregator *agg,
938 const Type_handler *h0,
939 const Type_handler *h1)
940 {
941 const Type_handler *hres;
942 if (h0->is_traditional_type() && h1->is_traditional_type())
943 {
944 set_handler(Type_handler::aggregate_for_num_op_traditional(h0, h1));
945 return false;
946 }
947 if ((hres= agg->find_handler(h0, h1)))
948 {
949 set_handler(hres);
950 return false;
951 }
952 return true;
953 }
954
955
956 /***************************************************************************/
957
958 const Type_handler *
get_handler_by_field_type(enum_field_types type)959 Type_handler::get_handler_by_field_type(enum_field_types type)
960 {
961 switch (type) {
962 case MYSQL_TYPE_DECIMAL: return &type_handler_olddecimal;
963 case MYSQL_TYPE_NEWDECIMAL: return &type_handler_newdecimal;
964 case MYSQL_TYPE_TINY: return &type_handler_tiny;
965 case MYSQL_TYPE_SHORT: return &type_handler_short;
966 case MYSQL_TYPE_LONG: return &type_handler_long;
967 case MYSQL_TYPE_LONGLONG: return &type_handler_longlong;
968 case MYSQL_TYPE_INT24: return &type_handler_int24;
969 case MYSQL_TYPE_YEAR: return &type_handler_year;
970 case MYSQL_TYPE_BIT: return &type_handler_bit;
971 case MYSQL_TYPE_FLOAT: return &type_handler_float;
972 case MYSQL_TYPE_DOUBLE: return &type_handler_double;
973 case MYSQL_TYPE_NULL: return &type_handler_null;
974 case MYSQL_TYPE_VARCHAR: return &type_handler_varchar;
975 case MYSQL_TYPE_TINY_BLOB: return &type_handler_tiny_blob;
976 case MYSQL_TYPE_MEDIUM_BLOB: return &type_handler_medium_blob;
977 case MYSQL_TYPE_LONG_BLOB: return &type_handler_long_blob;
978 case MYSQL_TYPE_BLOB: return &type_handler_blob;
979 case MYSQL_TYPE_VAR_STRING: return &type_handler_varchar; // Map to VARCHAR
980 case MYSQL_TYPE_STRING: return &type_handler_string;
981 case MYSQL_TYPE_ENUM: return &type_handler_varchar; // Map to VARCHAR
982 case MYSQL_TYPE_SET: return &type_handler_varchar; // Map to VARCHAR
983 case MYSQL_TYPE_GEOMETRY:
984 #ifdef HAVE_SPATIAL
985 return &type_handler_geometry;
986 #else
987 return NULL;
988 #endif
989 case MYSQL_TYPE_TIMESTAMP: return &type_handler_timestamp2;// Map to timestamp2
990 case MYSQL_TYPE_TIMESTAMP2: return &type_handler_timestamp2;
991 case MYSQL_TYPE_DATE: return &type_handler_newdate; // Map to newdate
992 case MYSQL_TYPE_TIME: return &type_handler_time2; // Map to time2
993 case MYSQL_TYPE_TIME2: return &type_handler_time2;
994 case MYSQL_TYPE_DATETIME: return &type_handler_datetime2; // Map to datetime2
995 case MYSQL_TYPE_DATETIME2: return &type_handler_datetime2;
996 case MYSQL_TYPE_NEWDATE:
997 /*
998 NEWDATE is actually a real_type(), not a field_type(),
999 but it's used around the code in field_type() context.
1000 We should probably clean up the code not to use MYSQL_TYPE_NEWDATE
1001 in field_type() context and add DBUG_ASSERT(0) here.
1002 */
1003 return &type_handler_newdate;
1004 case MYSQL_TYPE_VARCHAR_COMPRESSED:
1005 case MYSQL_TYPE_BLOB_COMPRESSED:
1006 break;
1007 };
1008 DBUG_ASSERT(0);
1009 return &type_handler_string;
1010 }
1011
1012
1013 const Type_handler *
get_handler_by_real_type(enum_field_types type)1014 Type_handler::get_handler_by_real_type(enum_field_types type)
1015 {
1016 switch (type) {
1017 case MYSQL_TYPE_DECIMAL: return &type_handler_olddecimal;
1018 case MYSQL_TYPE_NEWDECIMAL: return &type_handler_newdecimal;
1019 case MYSQL_TYPE_TINY: return &type_handler_tiny;
1020 case MYSQL_TYPE_SHORT: return &type_handler_short;
1021 case MYSQL_TYPE_LONG: return &type_handler_long;
1022 case MYSQL_TYPE_LONGLONG: return &type_handler_longlong;
1023 case MYSQL_TYPE_INT24: return &type_handler_int24;
1024 case MYSQL_TYPE_YEAR: return &type_handler_year;
1025 case MYSQL_TYPE_BIT: return &type_handler_bit;
1026 case MYSQL_TYPE_FLOAT: return &type_handler_float;
1027 case MYSQL_TYPE_DOUBLE: return &type_handler_double;
1028 case MYSQL_TYPE_NULL: return &type_handler_null;
1029 case MYSQL_TYPE_VARCHAR: return &type_handler_varchar;
1030 case MYSQL_TYPE_VARCHAR_COMPRESSED: return &type_handler_varchar_compressed;
1031 case MYSQL_TYPE_TINY_BLOB: return &type_handler_tiny_blob;
1032 case MYSQL_TYPE_MEDIUM_BLOB: return &type_handler_medium_blob;
1033 case MYSQL_TYPE_LONG_BLOB: return &type_handler_long_blob;
1034 case MYSQL_TYPE_BLOB: return &type_handler_blob;
1035 case MYSQL_TYPE_BLOB_COMPRESSED: return &type_handler_blob_compressed;
1036 case MYSQL_TYPE_VAR_STRING:
1037 /*
1038 VAR_STRING is actually a field_type(), not a real_type(),
1039 but it's used around the code in real_type() context.
1040 We should clean up the code and add DBUG_ASSERT(0) here.
1041 */
1042 return &type_handler_string;
1043 case MYSQL_TYPE_STRING: return &type_handler_string;
1044 case MYSQL_TYPE_ENUM: return &type_handler_enum;
1045 case MYSQL_TYPE_SET: return &type_handler_set;
1046 case MYSQL_TYPE_GEOMETRY:
1047 #ifdef HAVE_SPATIAL
1048 return &type_handler_geometry;
1049 #else
1050 return NULL;
1051 #endif
1052 case MYSQL_TYPE_TIMESTAMP: return &type_handler_timestamp;
1053 case MYSQL_TYPE_TIMESTAMP2: return &type_handler_timestamp2;
1054 case MYSQL_TYPE_DATE: return &type_handler_date;
1055 case MYSQL_TYPE_TIME: return &type_handler_time;
1056 case MYSQL_TYPE_TIME2: return &type_handler_time2;
1057 case MYSQL_TYPE_DATETIME: return &type_handler_datetime;
1058 case MYSQL_TYPE_DATETIME2: return &type_handler_datetime2;
1059 case MYSQL_TYPE_NEWDATE: return &type_handler_newdate;
1060 };
1061 DBUG_ASSERT(0);
1062 return &type_handler_string;
1063 }
1064
1065
1066 /**
1067 Create a DOUBLE field by default.
1068 */
1069 Field *
make_num_distinct_aggregator_field(MEM_ROOT * mem_root,const Item * item) const1070 Type_handler::make_num_distinct_aggregator_field(MEM_ROOT *mem_root,
1071 const Item *item) const
1072 {
1073 return new(mem_root)
1074 Field_double(NULL, item->max_length,
1075 (uchar *) (item->maybe_null ? "" : 0),
1076 item->maybe_null ? 1 : 0, Field::NONE,
1077 &item->name, (uint8) item->decimals,
1078 0, item->unsigned_flag);
1079 }
1080
1081
1082 Field *
make_num_distinct_aggregator_field(MEM_ROOT * mem_root,const Item * item) const1083 Type_handler_float::make_num_distinct_aggregator_field(MEM_ROOT *mem_root,
1084 const Item *item)
1085 const
1086 {
1087 return new(mem_root)
1088 Field_float(NULL, item->max_length,
1089 (uchar *) (item->maybe_null ? "" : 0),
1090 item->maybe_null ? 1 : 0, Field::NONE,
1091 &item->name, (uint8) item->decimals,
1092 0, item->unsigned_flag);
1093 }
1094
1095
1096 Field *
make_num_distinct_aggregator_field(MEM_ROOT * mem_root,const Item * item) const1097 Type_handler_decimal_result::make_num_distinct_aggregator_field(
1098 MEM_ROOT *mem_root,
1099 const Item *item)
1100 const
1101 {
1102 DBUG_ASSERT(item->decimals <= DECIMAL_MAX_SCALE);
1103 return new (mem_root)
1104 Field_new_decimal(NULL, item->max_length,
1105 (uchar *) (item->maybe_null ? "" : 0),
1106 item->maybe_null ? 1 : 0, Field::NONE,
1107 &item->name, (uint8) item->decimals,
1108 0, item->unsigned_flag);
1109 }
1110
1111
1112 Field *
make_num_distinct_aggregator_field(MEM_ROOT * mem_root,const Item * item) const1113 Type_handler_int_result::make_num_distinct_aggregator_field(MEM_ROOT *mem_root,
1114 const Item *item)
1115 const
1116 {
1117 /**
1118 Make a longlong field for all INT-alike types. It could create
1119 smaller fields for TINYINT, SMALLINT, MEDIUMINT, INT though.
1120 */
1121 return new(mem_root)
1122 Field_longlong(NULL, item->max_length,
1123 (uchar *) (item->maybe_null ? "" : 0),
1124 item->maybe_null ? 1 : 0, Field::NONE,
1125 &item->name, 0, item->unsigned_flag);
1126 }
1127
1128
1129 /***********************************************************************/
1130
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1131 Field *Type_handler_tiny::make_conversion_table_field(TABLE *table,
1132 uint metadata,
1133 const Field *target)
1134 const
1135 {
1136 /*
1137 As we don't know if the integer was signed or not on the master,
1138 assume we have same sign on master and slave. This is true when not
1139 using conversions so it should be true also when using conversions.
1140 */
1141 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
1142 return new (table->in_use->mem_root)
1143 Field_tiny(NULL, 4 /*max_length*/, (uchar *) "", 1, Field::NONE,
1144 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
1145 }
1146
1147
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1148 Field *Type_handler_short::make_conversion_table_field(TABLE *table,
1149 uint metadata,
1150 const Field *target)
1151 const
1152 {
1153 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
1154 return new (table->in_use->mem_root)
1155 Field_short(NULL, 6 /*max_length*/, (uchar *) "", 1, Field::NONE,
1156 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
1157 }
1158
1159
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1160 Field *Type_handler_int24::make_conversion_table_field(TABLE *table,
1161 uint metadata,
1162 const Field *target)
1163 const
1164 {
1165 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
1166 return new (table->in_use->mem_root)
1167 Field_medium(NULL, 9 /*max_length*/, (uchar *) "", 1, Field::NONE,
1168 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
1169 }
1170
1171
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1172 Field *Type_handler_long::make_conversion_table_field(TABLE *table,
1173 uint metadata,
1174 const Field *target)
1175 const
1176 {
1177 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
1178 return new (table->in_use->mem_root)
1179 Field_long(NULL, 11 /*max_length*/, (uchar *) "", 1, Field::NONE,
1180 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
1181 }
1182
1183
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1184 Field *Type_handler_longlong::make_conversion_table_field(TABLE *table,
1185 uint metadata,
1186 const Field *target)
1187 const
1188 {
1189 bool unsigned_flag= ((Field_num*) target)->unsigned_flag;
1190 return new (table->in_use->mem_root)
1191 Field_longlong(NULL, 20 /*max_length*/,(uchar *) "", 1, Field::NONE,
1192 &empty_clex_str, 0/*zerofill*/, unsigned_flag);
1193 }
1194
1195
1196
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1197 Field *Type_handler_float::make_conversion_table_field(TABLE *table,
1198 uint metadata,
1199 const Field *target)
1200 const
1201 {
1202 return new (table->in_use->mem_root)
1203 Field_float(NULL, 12 /*max_length*/, (uchar *) "", 1, Field::NONE,
1204 &empty_clex_str, 0/*dec*/, 0/*zerofill*/, 0/*unsigned_flag*/);
1205 }
1206
1207
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1208 Field *Type_handler_double::make_conversion_table_field(TABLE *table,
1209 uint metadata,
1210 const Field *target)
1211 const
1212 {
1213 return new (table->in_use->mem_root)
1214 Field_double(NULL, 22 /*max_length*/, (uchar *) "", 1, Field::NONE,
1215 &empty_clex_str, 0/*dec*/, 0/*zerofill*/, 0/*unsigned_flag*/);
1216 }
1217
1218
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1219 Field *Type_handler_newdecimal::make_conversion_table_field(TABLE *table,
1220 uint metadata,
1221 const Field *target)
1222 const
1223 {
1224 int precision= metadata >> 8;
1225 uint8 decimals= metadata & 0x00ff;
1226 uint32 max_length= my_decimal_precision_to_length(precision, decimals, false);
1227 DBUG_ASSERT(decimals <= DECIMAL_MAX_SCALE);
1228 return new (table->in_use->mem_root)
1229 Field_new_decimal(NULL, max_length, (uchar *) "", 1, Field::NONE,
1230 &empty_clex_str, decimals, 0/*zerofill*/, 0/*unsigned*/);
1231 }
1232
1233
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1234 Field *Type_handler_olddecimal::make_conversion_table_field(TABLE *table,
1235 uint metadata,
1236 const Field *target)
1237 const
1238 {
1239 sql_print_error("In RBR mode, Slave received incompatible DECIMAL field "
1240 "(old-style decimal field) from Master while creating "
1241 "conversion table. Please consider changing datatype on "
1242 "Master to new style decimal by executing ALTER command for"
1243 " column Name: %s.%s.%s.",
1244 target->table->s->db.str,
1245 target->table->s->table_name.str,
1246 target->field_name.str);
1247 return NULL;
1248 }
1249
1250
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1251 Field *Type_handler_year::make_conversion_table_field(TABLE *table,
1252 uint metadata,
1253 const Field *target)
1254 const
1255 {
1256 return new(table->in_use->mem_root)
1257 Field_year(NULL, 4, (uchar *) "", 1, Field::NONE, &empty_clex_str);
1258 }
1259
1260
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1261 Field *Type_handler_null::make_conversion_table_field(TABLE *table,
1262 uint metadata,
1263 const Field *target)
1264 const
1265 {
1266 return new(table->in_use->mem_root)
1267 Field_null(NULL, 0, Field::NONE, &empty_clex_str, target->charset());
1268 }
1269
1270
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1271 Field *Type_handler_timestamp::make_conversion_table_field(TABLE *table,
1272 uint metadata,
1273 const Field *target)
1274 const
1275 {
1276 return new_Field_timestamp(table->in_use->mem_root, NULL, (uchar *) "", 1,
1277 Field::NONE, &empty_clex_str, table->s, target->decimals());
1278 }
1279
1280
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1281 Field *Type_handler_timestamp2::make_conversion_table_field(TABLE *table,
1282 uint metadata,
1283 const Field *target)
1284 const
1285 {
1286 return new(table->in_use->mem_root)
1287 Field_timestampf(NULL, (uchar *) "", 1, Field::NONE,
1288 &empty_clex_str, table->s, metadata);
1289 }
1290
1291
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1292 Field *Type_handler_newdate::make_conversion_table_field(TABLE *table,
1293 uint metadata,
1294 const Field *target)
1295 const
1296 {
1297 return new(table->in_use->mem_root)
1298 Field_newdate(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str);
1299 }
1300
1301
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1302 Field *Type_handler_date::make_conversion_table_field(TABLE *table,
1303 uint metadata,
1304 const Field *target)
1305 const
1306 {
1307 return new(table->in_use->mem_root)
1308 Field_date(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str);
1309 }
1310
1311
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1312 Field *Type_handler_time::make_conversion_table_field(TABLE *table,
1313 uint metadata,
1314 const Field *target)
1315 const
1316 {
1317 return new_Field_time(table->in_use->mem_root, NULL, (uchar *) "", 1,
1318 Field::NONE, &empty_clex_str, target->decimals());
1319 }
1320
1321
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1322 Field *Type_handler_time2::make_conversion_table_field(TABLE *table,
1323 uint metadata,
1324 const Field *target)
1325 const
1326 {
1327 return new(table->in_use->mem_root)
1328 Field_timef(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str, metadata);
1329 }
1330
1331
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1332 Field *Type_handler_datetime::make_conversion_table_field(TABLE *table,
1333 uint metadata,
1334 const Field *target)
1335 const
1336 {
1337 return new_Field_datetime(table->in_use->mem_root, NULL, (uchar *) "", 1,
1338 Field::NONE, &empty_clex_str, target->decimals());
1339 }
1340
1341
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1342 Field *Type_handler_datetime2::make_conversion_table_field(TABLE *table,
1343 uint metadata,
1344 const Field *target)
1345 const
1346 {
1347 return new(table->in_use->mem_root)
1348 Field_datetimef(NULL, (uchar *) "", 1,
1349 Field::NONE, &empty_clex_str, metadata);
1350 }
1351
1352
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1353 Field *Type_handler_bit::make_conversion_table_field(TABLE *table,
1354 uint metadata,
1355 const Field *target)
1356 const
1357 {
1358 DBUG_ASSERT((metadata & 0xff) <= 7);
1359 uint32 max_length= 8 * (metadata >> 8U) + (metadata & 0x00ff);
1360 return new(table->in_use->mem_root)
1361 Field_bit_as_char(NULL, max_length, (uchar *) "", 1,
1362 Field::NONE, &empty_clex_str);
1363 }
1364
1365
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1366 Field *Type_handler_string::make_conversion_table_field(TABLE *table,
1367 uint metadata,
1368 const Field *target)
1369 const
1370 {
1371 /* This is taken from Field_string::unpack. */
1372 uint32 max_length= (((metadata >> 4) & 0x300) ^ 0x300) + (metadata & 0x00ff);
1373 return new(table->in_use->mem_root)
1374 Field_string(NULL, max_length, (uchar *) "", 1,
1375 Field::NONE, &empty_clex_str, target->charset());
1376 }
1377
1378
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1379 Field *Type_handler_varchar::make_conversion_table_field(TABLE *table,
1380 uint metadata,
1381 const Field *target)
1382 const
1383 {
1384 DBUG_ASSERT(HA_VARCHAR_PACKLENGTH(metadata) <= MAX_FIELD_VARCHARLENGTH);
1385 return new(table->in_use->mem_root)
1386 Field_varstring(NULL, metadata, HA_VARCHAR_PACKLENGTH(metadata),
1387 (uchar *) "", 1, Field::NONE, &empty_clex_str,
1388 table->s, target->charset());
1389 }
1390
1391
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1392 Field *Type_handler_varchar_compressed::make_conversion_table_field(TABLE *table,
1393 uint metadata,
1394 const Field *target)
1395 const
1396 {
1397 return new(table->in_use->mem_root)
1398 Field_varstring_compressed(NULL, metadata,
1399 HA_VARCHAR_PACKLENGTH(metadata),
1400 (uchar *) "", 1, Field::NONE,
1401 &empty_clex_str,
1402 table->s, target->charset(),
1403 zlib_compression_method);
1404 }
1405
1406
1407
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1408 Field *Type_handler_blob_compressed::make_conversion_table_field(TABLE *table,
1409 uint metadata,
1410 const Field *target)
1411 const
1412 {
1413 uint pack_length= metadata & 0x00ff;
1414 if (pack_length < 1 || pack_length > 4)
1415 return NULL; // Broken binary log?
1416 return new(table->in_use->mem_root)
1417 Field_blob_compressed(NULL, (uchar *) "", 1, Field::NONE,
1418 &empty_clex_str,
1419 table->s, pack_length, target->charset(),
1420 zlib_compression_method);
1421 }
1422
1423
1424 #ifdef HAVE_SPATIAL
1425 const Name Type_handler_geometry::m_name_geometry(STRING_WITH_LEN("geometry"));
1426
1427
type_handler_for_comparison() const1428 const Type_handler *Type_handler_geometry::type_handler_for_comparison() const
1429 {
1430 return &type_handler_geometry;
1431 }
1432
1433
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1434 Field *Type_handler_geometry::make_conversion_table_field(TABLE *table,
1435 uint metadata,
1436 const Field *target)
1437 const
1438 {
1439 DBUG_ASSERT(target->type() == MYSQL_TYPE_GEOMETRY);
1440 /*
1441 We do not do not update feature_gis statistics here:
1442 status_var_increment(target->table->in_use->status_var.feature_gis);
1443 as this is only a temporary field.
1444 The statistics was already incremented when "target" was created.
1445 */
1446 return new(table->in_use->mem_root)
1447 Field_geom(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str, table->s, 4,
1448 ((const Field_geom*) target)->geom_type,
1449 ((const Field_geom*) target)->srid);
1450 }
1451 #endif
1452
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1453 Field *Type_handler_enum::make_conversion_table_field(TABLE *table,
1454 uint metadata,
1455 const Field *target)
1456 const
1457 {
1458 DBUG_ASSERT(target->type() == MYSQL_TYPE_STRING);
1459 DBUG_ASSERT(target->real_type() == MYSQL_TYPE_ENUM);
1460 return new(table->in_use->mem_root)
1461 Field_enum(NULL, target->field_length,
1462 (uchar *) "", 1, Field::NONE, &empty_clex_str,
1463 metadata & 0x00ff/*pack_length()*/,
1464 ((const Field_enum*) target)->typelib, target->charset());
1465 }
1466
1467
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const1468 Field *Type_handler_set::make_conversion_table_field(TABLE *table,
1469 uint metadata,
1470 const Field *target)
1471 const
1472 {
1473 DBUG_ASSERT(target->type() == MYSQL_TYPE_STRING);
1474 DBUG_ASSERT(target->real_type() == MYSQL_TYPE_SET);
1475 return new(table->in_use->mem_root)
1476 Field_set(NULL, target->field_length,
1477 (uchar *) "", 1, Field::NONE, &empty_clex_str,
1478 metadata & 0x00ff/*pack_length()*/,
1479 ((const Field_enum*) target)->typelib, target->charset());
1480 }
1481
1482 /*************************************************************************/
1483 bool Type_handler_null::
Column_definition_fix_attributes(Column_definition * def) const1484 Column_definition_fix_attributes(Column_definition *def) const
1485 {
1486 return false;
1487 }
1488
1489 bool Type_handler_tiny::
Column_definition_fix_attributes(Column_definition * def) const1490 Column_definition_fix_attributes(Column_definition *def) const
1491 {
1492 return def->fix_attributes_int(MAX_TINYINT_WIDTH + def->sign_length());
1493 }
1494
1495 bool Type_handler_short::
Column_definition_fix_attributes(Column_definition * def) const1496 Column_definition_fix_attributes(Column_definition *def) const
1497 {
1498 return def->fix_attributes_int(MAX_SMALLINT_WIDTH + def->sign_length());
1499 }
1500
1501 bool Type_handler_int24::
Column_definition_fix_attributes(Column_definition * def) const1502 Column_definition_fix_attributes(Column_definition *def) const
1503 {
1504 return def->fix_attributes_int(MAX_MEDIUMINT_WIDTH + def->sign_length());
1505 }
1506
1507 bool Type_handler_long::
Column_definition_fix_attributes(Column_definition * def) const1508 Column_definition_fix_attributes(Column_definition *def) const
1509 {
1510 return def->fix_attributes_int(MAX_INT_WIDTH + def->sign_length());
1511 }
1512
1513 bool Type_handler_longlong::
Column_definition_fix_attributes(Column_definition * def) const1514 Column_definition_fix_attributes(Column_definition *def) const
1515 {
1516 return def->fix_attributes_int(MAX_BIGINT_WIDTH/*no sign_length() added*/);
1517 }
1518
1519 bool Type_handler_newdecimal::
Column_definition_fix_attributes(Column_definition * def) const1520 Column_definition_fix_attributes(Column_definition *def) const
1521 {
1522 return def->fix_attributes_decimal();
1523 }
1524
1525 bool Type_handler_olddecimal::
Column_definition_fix_attributes(Column_definition * def) const1526 Column_definition_fix_attributes(Column_definition *def) const
1527 {
1528 DBUG_ASSERT(0); // Obsolete
1529 return true;
1530 }
1531
1532 bool Type_handler_var_string::
Column_definition_fix_attributes(Column_definition * def) const1533 Column_definition_fix_attributes(Column_definition *def) const
1534 {
1535 DBUG_ASSERT(0); // Obsolete
1536 return true;
1537 }
1538
1539 bool Type_handler_varchar::
Column_definition_fix_attributes(Column_definition * def) const1540 Column_definition_fix_attributes(Column_definition *def) const
1541 {
1542 /*
1543 Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1544 if they don't have a default value
1545 */
1546 return def->check_length(ER_TOO_BIG_DISPLAYWIDTH, MAX_FIELD_BLOBLENGTH);
1547 }
1548
1549 bool Type_handler_string::
Column_definition_fix_attributes(Column_definition * def) const1550 Column_definition_fix_attributes(Column_definition *def) const
1551 {
1552 return def->check_length(ER_TOO_BIG_FIELDLENGTH, MAX_FIELD_CHARLENGTH);
1553 }
1554
1555 bool Type_handler_blob_common::
Column_definition_fix_attributes(Column_definition * def) const1556 Column_definition_fix_attributes(Column_definition *def) const
1557 {
1558 def->flags|= BLOB_FLAG;
1559 return def->check_length(ER_TOO_BIG_DISPLAYWIDTH, MAX_FIELD_BLOBLENGTH);
1560 }
1561
1562 #ifdef HAVE_SPATIAL
1563 bool Type_handler_geometry::
Column_definition_fix_attributes(Column_definition * def) const1564 Column_definition_fix_attributes(Column_definition *def) const
1565 {
1566 def->flags|= BLOB_FLAG;
1567 return false;
1568 }
1569 #endif
1570
1571 bool Type_handler_year::
Column_definition_fix_attributes(Column_definition * def) const1572 Column_definition_fix_attributes(Column_definition *def) const
1573 {
1574 if (!def->length || def->length != 2)
1575 def->length= 4; // Default length
1576 def->flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
1577 return false;
1578 }
1579
1580 bool Type_handler_float::
Column_definition_fix_attributes(Column_definition * def) const1581 Column_definition_fix_attributes(Column_definition *def) const
1582 {
1583 return def->fix_attributes_real(MAX_FLOAT_STR_LENGTH);
1584 }
1585
1586
1587 bool Type_handler_double::
Column_definition_fix_attributes(Column_definition * def) const1588 Column_definition_fix_attributes(Column_definition *def) const
1589 {
1590 return def->fix_attributes_real(DBL_DIG + 7);
1591 }
1592
1593 bool Type_handler_timestamp_common::
Column_definition_fix_attributes(Column_definition * def) const1594 Column_definition_fix_attributes(Column_definition *def) const
1595 {
1596 def->flags|= UNSIGNED_FLAG;
1597 return def->fix_attributes_temporal_with_time(MAX_DATETIME_WIDTH);
1598 }
1599
1600 bool Type_handler_date_common::
Column_definition_fix_attributes(Column_definition * def) const1601 Column_definition_fix_attributes(Column_definition *def) const
1602 {
1603 // We don't support creation of MYSQL_TYPE_DATE anymore
1604 def->set_handler(&type_handler_newdate);
1605 def->length= MAX_DATE_WIDTH;
1606 return false;
1607 }
1608
1609 bool Type_handler_time_common::
Column_definition_fix_attributes(Column_definition * def) const1610 Column_definition_fix_attributes(Column_definition *def) const
1611 {
1612 return def->fix_attributes_temporal_with_time(MIN_TIME_WIDTH);
1613 }
1614
1615 bool Type_handler_datetime_common::
Column_definition_fix_attributes(Column_definition * def) const1616 Column_definition_fix_attributes(Column_definition *def) const
1617 {
1618 return def->fix_attributes_temporal_with_time(MAX_DATETIME_WIDTH);
1619 }
1620
1621 bool Type_handler_set::
Column_definition_fix_attributes(Column_definition * def) const1622 Column_definition_fix_attributes(Column_definition *def) const
1623 {
1624 def->pack_length= get_set_pack_length(def->interval_list.elements);
1625 return false;
1626 }
1627
1628 bool Type_handler_enum::
Column_definition_fix_attributes(Column_definition * def) const1629 Column_definition_fix_attributes(Column_definition *def) const
1630 {
1631 def->pack_length= get_enum_pack_length(def->interval_list.elements);
1632 return false;
1633 }
1634
1635 bool Type_handler_bit::
Column_definition_fix_attributes(Column_definition * def) const1636 Column_definition_fix_attributes(Column_definition *def) const
1637 {
1638 return def->fix_attributes_bit();
1639 }
1640
1641 /*************************************************************************/
1642
1643 bool Type_handler::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags) const1644 Column_definition_prepare_stage1(THD *thd,
1645 MEM_ROOT *mem_root,
1646 Column_definition *def,
1647 handler *file,
1648 ulonglong table_flags) const
1649 {
1650 def->create_length_to_internal_length_simple();
1651 return false;
1652 }
1653
1654 bool Type_handler_null::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags) const1655 Column_definition_prepare_stage1(THD *thd,
1656 MEM_ROOT *mem_root,
1657 Column_definition *def,
1658 handler *file,
1659 ulonglong table_flags) const
1660 {
1661 def->create_length_to_internal_length_null();
1662 return false;
1663 }
1664
1665 bool Type_handler_row::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags) const1666 Column_definition_prepare_stage1(THD *thd,
1667 MEM_ROOT *mem_root,
1668 Column_definition *def,
1669 handler *file,
1670 ulonglong table_flags) const
1671 {
1672 def->create_length_to_internal_length_null();
1673 return false;
1674 }
1675
1676 bool Type_handler_newdecimal::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags) const1677 Column_definition_prepare_stage1(THD *thd,
1678 MEM_ROOT *mem_root,
1679 Column_definition *def,
1680 handler *file,
1681 ulonglong table_flags) const
1682 {
1683 def->create_length_to_internal_length_newdecimal();
1684 return false;
1685 }
1686
1687 bool Type_handler_bit::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags) const1688 Column_definition_prepare_stage1(THD *thd,
1689 MEM_ROOT *mem_root,
1690 Column_definition *def,
1691 handler *file,
1692 ulonglong table_flags) const
1693 {
1694 return def->prepare_stage1_bit(thd, mem_root, file, table_flags);
1695 }
1696
1697 bool Type_handler_typelib::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags) const1698 Column_definition_prepare_stage1(THD *thd,
1699 MEM_ROOT *mem_root,
1700 Column_definition *def,
1701 handler *file,
1702 ulonglong table_flags) const
1703 {
1704 return def->prepare_stage1_typelib(thd, mem_root, file, table_flags);
1705 }
1706
1707
1708 bool Type_handler_string_result::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags) const1709 Column_definition_prepare_stage1(THD *thd,
1710 MEM_ROOT *mem_root,
1711 Column_definition *def,
1712 handler *file,
1713 ulonglong table_flags) const
1714 {
1715 return def->prepare_stage1_string(thd, mem_root, file, table_flags);
1716 }
1717
1718
1719 #ifdef HAVE_SPATIAL
1720 bool Type_handler_geometry::
Column_definition_prepare_stage1(THD * thd,MEM_ROOT * mem_root,Column_definition * def,handler * file,ulonglong table_flags) const1721 Column_definition_prepare_stage1(THD *thd,
1722 MEM_ROOT *mem_root,
1723 Column_definition *def,
1724 handler *file,
1725 ulonglong table_flags) const
1726 {
1727 def->create_length_to_internal_length_string();
1728 return def->prepare_blob_field(thd);
1729 }
1730 #endif
1731
1732
1733 /*************************************************************************/
1734
1735 bool Type_handler::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file,const Schema_specification_st * schema) const1736 Column_definition_redefine_stage1(Column_definition *def,
1737 const Column_definition *dup,
1738 const handler *file,
1739 const Schema_specification_st *schema)
1740 const
1741 {
1742 def->redefine_stage1_common(dup, file, schema);
1743 def->create_length_to_internal_length_simple();
1744 return false;
1745 }
1746
1747
1748 bool Type_handler_null::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file,const Schema_specification_st * schema) const1749 Column_definition_redefine_stage1(Column_definition *def,
1750 const Column_definition *dup,
1751 const handler *file,
1752 const Schema_specification_st *schema)
1753 const
1754 {
1755 def->redefine_stage1_common(dup, file, schema);
1756 def->create_length_to_internal_length_null();
1757 return false;
1758 }
1759
1760
1761 bool Type_handler_newdecimal::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file,const Schema_specification_st * schema) const1762 Column_definition_redefine_stage1(Column_definition *def,
1763 const Column_definition *dup,
1764 const handler *file,
1765 const Schema_specification_st *schema)
1766 const
1767 {
1768 def->redefine_stage1_common(dup, file, schema);
1769 def->create_length_to_internal_length_newdecimal();
1770 return false;
1771 }
1772
1773
1774 bool Type_handler_string_result::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file,const Schema_specification_st * schema) const1775 Column_definition_redefine_stage1(Column_definition *def,
1776 const Column_definition *dup,
1777 const handler *file,
1778 const Schema_specification_st *schema)
1779 const
1780 {
1781 def->redefine_stage1_common(dup, file, schema);
1782 def->set_compression_method(dup->compression_method());
1783 def->create_length_to_internal_length_string();
1784 return false;
1785 }
1786
1787
1788 bool Type_handler_typelib::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file,const Schema_specification_st * schema) const1789 Column_definition_redefine_stage1(Column_definition *def,
1790 const Column_definition *dup,
1791 const handler *file,
1792 const Schema_specification_st *schema)
1793 const
1794 {
1795 def->redefine_stage1_common(dup, file, schema);
1796 def->create_length_to_internal_length_typelib();
1797 return false;
1798 }
1799
1800
1801 bool Type_handler_bit::
Column_definition_redefine_stage1(Column_definition * def,const Column_definition * dup,const handler * file,const Schema_specification_st * schema) const1802 Column_definition_redefine_stage1(Column_definition *def,
1803 const Column_definition *dup,
1804 const handler *file,
1805 const Schema_specification_st *schema)
1806 const
1807 {
1808 def->redefine_stage1_common(dup, file, schema);
1809 /*
1810 If we are replacing a field with a BIT field, we need
1811 to initialize pack_flag.
1812 */
1813 def->pack_flag= FIELDFLAG_NUMBER;
1814 if (!(file->ha_table_flags() & HA_CAN_BIT_FIELD))
1815 def->pack_flag|= FIELDFLAG_TREAT_BIT_AS_CHAR;
1816 def->create_length_to_internal_length_bit();
1817 return false;
1818 }
1819
1820
1821 /*************************************************************************/
1822
1823 bool Type_handler::
Column_definition_prepare_stage2_legacy(Column_definition * def,enum_field_types type) const1824 Column_definition_prepare_stage2_legacy(Column_definition *def,
1825 enum_field_types type) const
1826 {
1827 def->pack_flag= f_settype((uint) type);
1828 return false;
1829 }
1830
1831 bool Type_handler::
Column_definition_prepare_stage2_legacy_num(Column_definition * def,enum_field_types type) const1832 Column_definition_prepare_stage2_legacy_num(Column_definition *def,
1833 enum_field_types type) const
1834 {
1835 def->pack_flag= def->pack_flag_numeric(def->decimals) |
1836 f_settype((uint) type);
1837 return false;
1838 }
1839
1840 bool Type_handler::
Column_definition_prepare_stage2_legacy_real(Column_definition * def,enum_field_types type) const1841 Column_definition_prepare_stage2_legacy_real(Column_definition *def,
1842 enum_field_types type) const
1843 {
1844 uint dec= def->decimals;
1845 /*
1846 User specified FLOAT() or DOUBLE() without precision. Change to
1847 FLOATING_POINT_DECIMALS to keep things compatible with earlier MariaDB
1848 versions.
1849 */
1850 if (dec >= FLOATING_POINT_DECIMALS)
1851 dec= FLOATING_POINT_DECIMALS;
1852 def->pack_flag= def->pack_flag_numeric(dec) | f_settype((uint) type);
1853 return false;
1854 }
1855
1856 bool Type_handler_newdecimal::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const1857 Column_definition_prepare_stage2(Column_definition *def,
1858 handler *file,
1859 ulonglong table_flags) const
1860 {
1861 def->pack_flag= def->pack_flag_numeric(def->decimals);
1862 return false;
1863 }
1864
1865 bool Type_handler_blob_common::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const1866 Column_definition_prepare_stage2(Column_definition *def,
1867 handler *file,
1868 ulonglong table_flags) const
1869 {
1870 return def->prepare_stage2_blob(file, table_flags, FIELDFLAG_BLOB);
1871 }
1872
1873 #ifdef HAVE_SPATIAL
1874 bool Type_handler_geometry::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const1875 Column_definition_prepare_stage2(Column_definition *def,
1876 handler *file,
1877 ulonglong table_flags) const
1878 {
1879 if (!(table_flags & HA_CAN_GEOMETRY))
1880 {
1881 my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0), "GEOMETRY");
1882 return true;
1883 }
1884 return def->prepare_stage2_blob(file, table_flags, FIELDFLAG_GEOM);
1885 }
1886 #endif
1887
1888 bool Type_handler_varchar::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const1889 Column_definition_prepare_stage2(Column_definition *def,
1890 handler *file,
1891 ulonglong table_flags) const
1892 {
1893 return def->prepare_stage2_varchar(table_flags);
1894 }
1895
1896 bool Type_handler_string::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const1897 Column_definition_prepare_stage2(Column_definition *def,
1898 handler *file,
1899 ulonglong table_flags) const
1900 {
1901 def->pack_flag= (def->charset->state & MY_CS_BINSORT) ? FIELDFLAG_BINARY : 0;
1902 return false;
1903 }
1904
1905 bool Type_handler_enum::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const1906 Column_definition_prepare_stage2(Column_definition *def,
1907 handler *file,
1908 ulonglong table_flags) const
1909 {
1910 uint dummy;
1911 return def->prepare_stage2_typelib("ENUM", FIELDFLAG_INTERVAL, &dummy);
1912 }
1913
1914 bool Type_handler_set::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const1915 Column_definition_prepare_stage2(Column_definition *def,
1916 handler *file,
1917 ulonglong table_flags) const
1918 {
1919 uint dup_count;
1920 if (def->prepare_stage2_typelib("SET", FIELDFLAG_BITFIELD, &dup_count))
1921 return true;
1922 /* Check that count of unique members is not more then 64 */
1923 if (def->interval->count - dup_count > sizeof(longlong)*8)
1924 {
1925 my_error(ER_TOO_BIG_SET, MYF(0), def->field_name.str);
1926 return true;
1927 }
1928 return false;
1929 }
1930
1931 bool Type_handler_bit::
Column_definition_prepare_stage2(Column_definition * def,handler * file,ulonglong table_flags) const1932 Column_definition_prepare_stage2(Column_definition *def,
1933 handler *file,
1934 ulonglong table_flags) const
1935 {
1936 /*
1937 We have sql_field->pack_flag already set here, see
1938 mysql_prepare_create_table().
1939 */
1940 return false;
1941 }
1942
1943 /*************************************************************************/
1944
calc_pack_length(uint32 length) const1945 uint32 Type_handler_time::calc_pack_length(uint32 length) const
1946 {
1947 return length > MIN_TIME_WIDTH ?
1948 hires_bytes(length - 1 - MIN_TIME_WIDTH) : 3;
1949 }
1950
calc_pack_length(uint32 length) const1951 uint32 Type_handler_time2::calc_pack_length(uint32 length) const
1952 {
1953 return length > MIN_TIME_WIDTH ?
1954 my_time_binary_length(length - MIN_TIME_WIDTH - 1) : 3;
1955 }
1956
calc_pack_length(uint32 length) const1957 uint32 Type_handler_timestamp::calc_pack_length(uint32 length) const
1958 {
1959 return length > MAX_DATETIME_WIDTH ?
1960 4 + sec_part_bytes(length - 1 - MAX_DATETIME_WIDTH) : 4;
1961 }
1962
calc_pack_length(uint32 length) const1963 uint32 Type_handler_timestamp2::calc_pack_length(uint32 length) const
1964 {
1965 return length > MAX_DATETIME_WIDTH ?
1966 my_timestamp_binary_length(length - MAX_DATETIME_WIDTH - 1) : 4;
1967 }
1968
calc_pack_length(uint32 length) const1969 uint32 Type_handler_datetime::calc_pack_length(uint32 length) const
1970 {
1971 return length > MAX_DATETIME_WIDTH ?
1972 hires_bytes(length - 1 - MAX_DATETIME_WIDTH) : 8;
1973 }
1974
calc_pack_length(uint32 length) const1975 uint32 Type_handler_datetime2::calc_pack_length(uint32 length) const
1976 {
1977 return length > MAX_DATETIME_WIDTH ?
1978 my_datetime_binary_length(length - MAX_DATETIME_WIDTH - 1) : 5;
1979 }
1980
calc_pack_length(uint32 length) const1981 uint32 Type_handler_tiny_blob::calc_pack_length(uint32 length) const
1982 {
1983 return 1 + portable_sizeof_char_ptr;
1984 }
1985
calc_pack_length(uint32 length) const1986 uint32 Type_handler_blob::calc_pack_length(uint32 length) const
1987 {
1988 return 2 + portable_sizeof_char_ptr;
1989 }
1990
calc_pack_length(uint32 length) const1991 uint32 Type_handler_medium_blob::calc_pack_length(uint32 length) const
1992 {
1993 return 3 + portable_sizeof_char_ptr;
1994 }
1995
calc_pack_length(uint32 length) const1996 uint32 Type_handler_long_blob::calc_pack_length(uint32 length) const
1997 {
1998 return 4 + portable_sizeof_char_ptr;
1999 }
2000
2001 #ifdef HAVE_SPATIAL
calc_pack_length(uint32 length) const2002 uint32 Type_handler_geometry::calc_pack_length(uint32 length) const
2003 {
2004 return 4 + portable_sizeof_char_ptr;
2005 }
2006 #endif
2007
calc_pack_length(uint32 length) const2008 uint32 Type_handler_newdecimal::calc_pack_length(uint32 length) const
2009 {
2010 abort(); // This shouldn't happen
2011 return 0;
2012 }
2013
calc_pack_length(uint32 length) const2014 uint32 Type_handler_set::calc_pack_length(uint32 length) const
2015 {
2016 abort(); // This shouldn't happen
2017 return 0;
2018 }
2019
calc_pack_length(uint32 length) const2020 uint32 Type_handler_enum::calc_pack_length(uint32 length) const
2021 {
2022 abort(); // This shouldn't happen
2023 return 0;
2024 }
2025
2026
2027 /*************************************************************************/
make_and_init_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2028 Field *Type_handler::make_and_init_table_field(const LEX_CSTRING *name,
2029 const Record_addr &addr,
2030 const Type_all_attributes &attr,
2031 TABLE *table) const
2032 {
2033 Field *field= make_table_field(name, addr, attr, table);
2034 if (field)
2035 field->init(table);
2036 return field;
2037 }
2038
2039
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2040 Field *Type_handler_tiny::make_table_field(const LEX_CSTRING *name,
2041 const Record_addr &addr,
2042 const Type_all_attributes &attr,
2043 TABLE *table) const
2044 {
2045 return new (table->in_use->mem_root)
2046 Field_tiny(addr.ptr, attr.max_char_length(),
2047 addr.null_ptr, addr.null_bit,
2048 Field::NONE, name, 0/*zerofill*/, attr.unsigned_flag);
2049 }
2050
2051
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2052 Field *Type_handler_short::make_table_field(const LEX_CSTRING *name,
2053 const Record_addr &addr,
2054 const Type_all_attributes &attr,
2055 TABLE *table) const
2056
2057 {
2058 return new (table->in_use->mem_root)
2059 Field_short(addr.ptr, attr.max_char_length(),
2060 addr.null_ptr, addr.null_bit,
2061 Field::NONE, name, 0/*zerofill*/, attr.unsigned_flag);
2062 }
2063
2064
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2065 Field *Type_handler_int24::make_table_field(const LEX_CSTRING *name,
2066 const Record_addr &addr,
2067 const Type_all_attributes &attr,
2068 TABLE *table) const
2069 {
2070 return new (table->in_use->mem_root)
2071 Field_medium(addr.ptr, attr.max_char_length(),
2072 addr.null_ptr, addr.null_bit,
2073 Field::NONE, name,
2074 0/*zerofill*/, attr.unsigned_flag);
2075 }
2076
2077
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2078 Field *Type_handler_long::make_table_field(const LEX_CSTRING *name,
2079 const Record_addr &addr,
2080 const Type_all_attributes &attr,
2081 TABLE *table) const
2082 {
2083 return new (table->in_use->mem_root)
2084 Field_long(addr.ptr, attr.max_char_length(),
2085 addr.null_ptr, addr.null_bit,
2086 Field::NONE, name, 0/*zerofill*/, attr.unsigned_flag);
2087 }
2088
2089
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2090 Field *Type_handler_longlong::make_table_field(const LEX_CSTRING *name,
2091 const Record_addr &addr,
2092 const Type_all_attributes &attr,
2093 TABLE *table) const
2094 {
2095 return new (table->in_use->mem_root)
2096 Field_longlong(addr.ptr, attr.max_char_length(),
2097 addr.null_ptr, addr.null_bit,
2098 Field::NONE, name,
2099 0/*zerofill*/, attr.unsigned_flag);
2100 }
2101
2102
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2103 Field *Type_handler_vers_trx_id::make_table_field(const LEX_CSTRING *name,
2104 const Record_addr &addr,
2105 const Type_all_attributes &attr,
2106 TABLE *table) const
2107 {
2108 return new (table->in_use->mem_root)
2109 Field_vers_trx_id(addr.ptr, attr.max_char_length(),
2110 addr.null_ptr, addr.null_bit,
2111 Field::NONE, name,
2112 0/*zerofill*/, attr.unsigned_flag);
2113 }
2114
2115
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2116 Field *Type_handler_float::make_table_field(const LEX_CSTRING *name,
2117 const Record_addr &addr,
2118 const Type_all_attributes &attr,
2119 TABLE *table) const
2120 {
2121 return new (table->in_use->mem_root)
2122 Field_float(addr.ptr, attr.max_char_length(),
2123 addr.null_ptr, addr.null_bit,
2124 Field::NONE, name,
2125 (uint8) attr.decimals, 0/*zerofill*/, attr.unsigned_flag);
2126 }
2127
2128
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2129 Field *Type_handler_double::make_table_field(const LEX_CSTRING *name,
2130 const Record_addr &addr,
2131 const Type_all_attributes &attr,
2132 TABLE *table) const
2133 {
2134 return new (table->in_use->mem_root)
2135 Field_double(addr.ptr, attr.max_char_length(),
2136 addr.null_ptr, addr.null_bit,
2137 Field::NONE, name,
2138 (uint8) attr.decimals, 0/*zerofill*/, attr.unsigned_flag);
2139 }
2140
2141
2142 Field *
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2143 Type_handler_olddecimal::make_table_field(const LEX_CSTRING *name,
2144 const Record_addr &addr,
2145 const Type_all_attributes &attr,
2146 TABLE *table) const
2147 {
2148 /*
2149 Currently make_table_field() is used for Item purpose only.
2150 On Item level we have type_handler_newdecimal only.
2151 For now we have DBUG_ASSERT(0).
2152 It will be removed when we reuse Type_handler::make_table_field()
2153 in make_field() in field.cc, to open old tables with old decimal.
2154 */
2155 DBUG_ASSERT(0);
2156 return new (table->in_use->mem_root)
2157 Field_decimal(addr.ptr, attr.max_length, addr.null_ptr, addr.null_bit,
2158 Field::NONE, name, (uint8) attr.decimals,
2159 0/*zerofill*/,attr.unsigned_flag);
2160 }
2161
2162
2163 Field *
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2164 Type_handler_newdecimal::make_table_field(const LEX_CSTRING *name,
2165 const Record_addr &addr,
2166 const Type_all_attributes &attr,
2167 TABLE *table) const
2168 {
2169 uint8 dec= (uint8) attr.decimals;
2170 uint8 intg= (uint8) (attr.decimal_precision() - dec);
2171 uint32 len= attr.max_char_length();
2172
2173 /*
2174 Trying to put too many digits overall in a DECIMAL(prec,dec)
2175 will always throw a warning. We must limit dec to
2176 DECIMAL_MAX_SCALE however to prevent an assert() later.
2177 */
2178
2179 if (dec > 0)
2180 {
2181 signed int overflow;
2182
2183 dec= MY_MIN(dec, DECIMAL_MAX_SCALE);
2184
2185 /*
2186 If the value still overflows the field with the corrected dec,
2187 we'll throw out decimals rather than integers. This is still
2188 bad and of course throws a truncation warning.
2189 +1: for decimal point
2190 */
2191
2192 const int required_length=
2193 my_decimal_precision_to_length(intg + dec, dec, attr.unsigned_flag);
2194
2195 overflow= required_length - len;
2196
2197 if (overflow > 0)
2198 dec= MY_MAX(0, dec - overflow); // too long, discard fract
2199 else
2200 /* Corrected value fits. */
2201 len= required_length;
2202 }
2203 return new (table->in_use->mem_root)
2204 Field_new_decimal(addr.ptr, len, addr.null_ptr, addr.null_bit,
2205 Field::NONE, name,
2206 dec, 0/*zerofill*/, attr.unsigned_flag);
2207 }
2208
2209
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2210 Field *Type_handler_year::make_table_field(const LEX_CSTRING *name,
2211 const Record_addr &addr,
2212 const Type_all_attributes &attr,
2213 TABLE *table) const
2214 {
2215 return new (table->in_use->mem_root)
2216 Field_year(addr.ptr, attr.max_length, addr.null_ptr, addr.null_bit,
2217 Field::NONE, name);
2218 }
2219
2220
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2221 Field *Type_handler_null::make_table_field(const LEX_CSTRING *name,
2222 const Record_addr &addr,
2223 const Type_all_attributes &attr,
2224 TABLE *table) const
2225
2226 {
2227 return new (table->in_use->mem_root)
2228 Field_null(addr.ptr, attr.max_length,
2229 Field::NONE, name, attr.collation.collation);
2230 }
2231
2232
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2233 Field *Type_handler_timestamp::make_table_field(const LEX_CSTRING *name,
2234 const Record_addr &addr,
2235 const Type_all_attributes &attr,
2236 TABLE *table) const
2237
2238 {
2239 return new_Field_timestamp(table->in_use->mem_root,
2240 addr.ptr, addr.null_ptr, addr.null_bit,
2241 Field::NONE, name, table->s, attr.decimals);
2242 }
2243
2244
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2245 Field *Type_handler_timestamp2::make_table_field(const LEX_CSTRING *name,
2246 const Record_addr &addr,
2247 const Type_all_attributes &attr,
2248 TABLE *table) const
2249
2250 {
2251 /*
2252 Will be changed to "new Field_timestampf" when we reuse
2253 make_table_field() for make_field() purposes in field.cc.
2254 */
2255 return new_Field_timestamp(table->in_use->mem_root,
2256 addr.ptr, addr.null_ptr, addr.null_bit,
2257 Field::NONE, name, table->s, attr.decimals);
2258 }
2259
2260
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2261 Field *Type_handler_newdate::make_table_field(const LEX_CSTRING *name,
2262 const Record_addr &addr,
2263 const Type_all_attributes &attr,
2264 TABLE *table) const
2265
2266 {
2267 return new (table->in_use->mem_root)
2268 Field_newdate(addr.ptr, addr.null_ptr, addr.null_bit,
2269 Field::NONE, name);
2270 }
2271
2272
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2273 Field *Type_handler_date::make_table_field(const LEX_CSTRING *name,
2274 const Record_addr &addr,
2275 const Type_all_attributes &attr,
2276 TABLE *table) const
2277
2278 {
2279 /*
2280 DBUG_ASSERT will be removed when we reuse make_table_field()
2281 for make_field() in field.cc
2282 */
2283 DBUG_ASSERT(0);
2284 return new (table->in_use->mem_root)
2285 Field_date(addr.ptr, addr.null_ptr, addr.null_bit,
2286 Field::NONE, name);
2287 }
2288
2289
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2290 Field *Type_handler_time::make_table_field(const LEX_CSTRING *name,
2291 const Record_addr &addr,
2292 const Type_all_attributes &attr,
2293 TABLE *table) const
2294
2295 {
2296 return new_Field_time(table->in_use->mem_root,
2297 addr.ptr, addr.null_ptr, addr.null_bit,
2298 Field::NONE, name, attr.decimals);
2299 }
2300
2301
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2302 Field *Type_handler_time2::make_table_field(const LEX_CSTRING *name,
2303 const Record_addr &addr,
2304 const Type_all_attributes &attr,
2305 TABLE *table) const
2306
2307
2308 {
2309 /*
2310 Will be changed to "new Field_timef" when we reuse
2311 make_table_field() for make_field() purposes in field.cc.
2312 */
2313 return new_Field_time(table->in_use->mem_root,
2314 addr.ptr, addr.null_ptr, addr.null_bit,
2315 Field::NONE, name, attr.decimals);
2316 }
2317
2318
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2319 Field *Type_handler_datetime::make_table_field(const LEX_CSTRING *name,
2320 const Record_addr &addr,
2321 const Type_all_attributes &attr,
2322 TABLE *table) const
2323
2324 {
2325 return new_Field_datetime(table->in_use->mem_root,
2326 addr.ptr, addr.null_ptr, addr.null_bit,
2327 Field::NONE, name, attr.decimals);
2328 }
2329
2330
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2331 Field *Type_handler_datetime2::make_table_field(const LEX_CSTRING *name,
2332 const Record_addr &addr,
2333 const Type_all_attributes &attr,
2334 TABLE *table) const
2335 {
2336 /*
2337 Will be changed to "new Field_datetimef" when we reuse
2338 make_table_field() for make_field() purposes in field.cc.
2339 */
2340 return new_Field_datetime(table->in_use->mem_root,
2341 addr.ptr, addr.null_ptr, addr.null_bit,
2342 Field::NONE, name, attr.decimals);
2343 }
2344
2345
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2346 Field *Type_handler_bit::make_table_field(const LEX_CSTRING *name,
2347 const Record_addr &addr,
2348 const Type_all_attributes &attr,
2349 TABLE *table) const
2350
2351 {
2352 return new (table->in_use->mem_root)
2353 Field_bit_as_char(addr.ptr, attr.max_length,
2354 addr.null_ptr, addr.null_bit,
2355 Field::NONE, name);
2356 }
2357
2358
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2359 Field *Type_handler_string::make_table_field(const LEX_CSTRING *name,
2360 const Record_addr &addr,
2361 const Type_all_attributes &attr,
2362 TABLE *table) const
2363
2364 {
2365 return new (table->in_use->mem_root)
2366 Field_string(addr.ptr, attr.max_length, addr.null_ptr, addr.null_bit,
2367 Field::NONE, name, attr.collation);
2368 }
2369
2370
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2371 Field *Type_handler_varchar::make_table_field(const LEX_CSTRING *name,
2372 const Record_addr &addr,
2373 const Type_all_attributes &attr,
2374 TABLE *table) const
2375
2376 {
2377 DBUG_ASSERT(HA_VARCHAR_PACKLENGTH(attr.max_length) <=
2378 MAX_FIELD_VARCHARLENGTH);
2379 return new (table->in_use->mem_root)
2380 Field_varstring(addr.ptr, attr.max_length,
2381 HA_VARCHAR_PACKLENGTH(attr.max_length),
2382 addr.null_ptr, addr.null_bit,
2383 Field::NONE, name,
2384 table->s, attr.collation);
2385 }
2386
2387
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2388 Field *Type_handler_tiny_blob::make_table_field(const LEX_CSTRING *name,
2389 const Record_addr &addr,
2390 const Type_all_attributes &attr,
2391 TABLE *table) const
2392
2393 {
2394 return new (table->in_use->mem_root)
2395 Field_blob(addr.ptr, addr.null_ptr, addr.null_bit,
2396 Field::NONE, name, table->s,
2397 1, attr.collation);
2398 }
2399
2400
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2401 Field *Type_handler_blob::make_table_field(const LEX_CSTRING *name,
2402 const Record_addr &addr,
2403 const Type_all_attributes &attr,
2404 TABLE *table) const
2405
2406 {
2407 return new (table->in_use->mem_root)
2408 Field_blob(addr.ptr, addr.null_ptr, addr.null_bit,
2409 Field::NONE, name, table->s,
2410 2, attr.collation);
2411 }
2412
2413
2414 Field *
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2415 Type_handler_medium_blob::make_table_field(const LEX_CSTRING *name,
2416 const Record_addr &addr,
2417 const Type_all_attributes &attr,
2418 TABLE *table) const
2419
2420 {
2421 return new (table->in_use->mem_root)
2422 Field_blob(addr.ptr, addr.null_ptr, addr.null_bit,
2423 Field::NONE, name, table->s,
2424 3, attr.collation);
2425 }
2426
2427
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2428 Field *Type_handler_long_blob::make_table_field(const LEX_CSTRING *name,
2429 const Record_addr &addr,
2430 const Type_all_attributes &attr,
2431 TABLE *table) const
2432
2433 {
2434 return new (table->in_use->mem_root)
2435 Field_blob(addr.ptr, addr.null_ptr, addr.null_bit,
2436 Field::NONE, name, table->s,
2437 4, attr.collation);
2438 }
2439
2440
2441
2442 #ifdef HAVE_SPATIAL
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2443 Field *Type_handler_geometry::make_table_field(const LEX_CSTRING *name,
2444 const Record_addr &addr,
2445 const Type_all_attributes &attr,
2446 TABLE *table) const
2447 {
2448 return new (table->in_use->mem_root)
2449 Field_geom(addr.ptr, addr.null_ptr, addr.null_bit,
2450 Field::NONE, name, table->s, 4,
2451 (Field::geometry_type) attr.uint_geometry_type(),
2452 0);
2453 }
2454 #endif
2455
2456
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2457 Field *Type_handler_enum::make_table_field(const LEX_CSTRING *name,
2458 const Record_addr &addr,
2459 const Type_all_attributes &attr,
2460 TABLE *table) const
2461 {
2462 TYPELIB *typelib= attr.get_typelib();
2463 DBUG_ASSERT(typelib);
2464 return new (table->in_use->mem_root)
2465 Field_enum(addr.ptr, attr.max_length, addr.null_ptr, addr.null_bit,
2466 Field::NONE, name,
2467 get_enum_pack_length(typelib->count), typelib,
2468 attr.collation);
2469 }
2470
2471
make_table_field(const LEX_CSTRING * name,const Record_addr & addr,const Type_all_attributes & attr,TABLE * table) const2472 Field *Type_handler_set::make_table_field(const LEX_CSTRING *name,
2473 const Record_addr &addr,
2474 const Type_all_attributes &attr,
2475 TABLE *table) const
2476
2477 {
2478 TYPELIB *typelib= attr.get_typelib();
2479 DBUG_ASSERT(typelib);
2480 return new (table->in_use->mem_root)
2481 Field_set(addr.ptr, attr.max_length, addr.null_ptr, addr.null_bit,
2482 Field::NONE, name,
2483 get_enum_pack_length(typelib->count), typelib,
2484 attr.collation);
2485 }
2486
2487 /*************************************************************************/
2488
2489 /*
2490 If length is not specified for a varchar parameter, set length to the
2491 maximum length of the actual argument. Goals are:
2492 - avoid to allocate too much unused memory for m_var_table
2493 - allow length check inside the callee rather than during copy of
2494 returned values in output variables.
2495 - allow varchar parameter size greater than 4000
2496 Default length has been stored in "decimal" member during parse.
2497 */
adjust_spparam_type(Spvar_definition * def,Item * from) const2498 bool Type_handler_varchar::adjust_spparam_type(Spvar_definition *def,
2499 Item *from) const
2500 {
2501 if (def->decimals)
2502 {
2503 uint def_max_char_length= MAX_FIELD_VARCHARLENGTH / def->charset->mbmaxlen;
2504 uint arg_max_length= from->max_char_length();
2505 set_if_smaller(arg_max_length, def_max_char_length);
2506 def->length= arg_max_length > 0 ? arg_max_length : def->decimals;
2507 def->create_length_to_internal_length_string();
2508 }
2509 return false;
2510 }
2511
2512 /*************************************************************************/
2513
max_display_length(const Item * item) const2514 uint32 Type_handler_decimal_result::max_display_length(const Item *item) const
2515 {
2516 return item->max_length;
2517 }
2518
2519
max_display_length(const Item * item) const2520 uint32 Type_handler_temporal_result::max_display_length(const Item *item) const
2521 {
2522 return item->max_length;
2523 }
2524
2525
max_display_length(const Item * item) const2526 uint32 Type_handler_string_result::max_display_length(const Item *item) const
2527 {
2528 return item->max_length;
2529 }
2530
2531
max_display_length(const Item * item) const2532 uint32 Type_handler_year::max_display_length(const Item *item) const
2533 {
2534 return item->max_length;
2535 }
2536
2537
max_display_length(const Item * item) const2538 uint32 Type_handler_bit::max_display_length(const Item *item) const
2539 {
2540 return item->max_length;
2541 }
2542
2543
max_display_length(const Item * item) const2544 uint32 Type_handler_general_purpose_int::max_display_length(const Item *item)
2545 const
2546 {
2547 return type_limits_int_by_unsigned_flag(item->unsigned_flag)->char_length();
2548 }
2549
2550
2551 /*************************************************************************/
2552
Item_save_in_field(Item * item,Field * field,bool no_conversions) const2553 int Type_handler_time_common::Item_save_in_field(Item *item, Field *field,
2554 bool no_conversions) const
2555 {
2556 return item->save_time_in_field(field, no_conversions);
2557 }
2558
Item_save_in_field(Item * item,Field * field,bool no_conversions) const2559 int Type_handler_temporal_with_date::Item_save_in_field(Item *item,
2560 Field *field,
2561 bool no_conversions)
2562 const
2563 {
2564 return item->save_date_in_field(field, no_conversions);
2565 }
2566
2567
Item_save_in_field(Item * item,Field * field,bool no_conversions) const2568 int Type_handler_string_result::Item_save_in_field(Item *item, Field *field,
2569 bool no_conversions) const
2570 {
2571 return item->save_str_in_field(field, no_conversions);
2572 }
2573
2574
Item_save_in_field(Item * item,Field * field,bool no_conversions) const2575 int Type_handler_real_result::Item_save_in_field(Item *item, Field *field,
2576 bool no_conversions) const
2577 {
2578 return item->save_real_in_field(field, no_conversions);
2579 }
2580
2581
Item_save_in_field(Item * item,Field * field,bool no_conversions) const2582 int Type_handler_decimal_result::Item_save_in_field(Item *item, Field *field,
2583 bool no_conversions) const
2584 {
2585 return item->save_decimal_in_field(field, no_conversions);
2586 }
2587
2588
Item_save_in_field(Item * item,Field * field,bool no_conversions) const2589 int Type_handler_int_result::Item_save_in_field(Item *item, Field *field,
2590 bool no_conversions) const
2591 {
2592 return item->save_int_in_field(field, no_conversions);
2593 }
2594
2595
2596 /***********************************************************************/
2597
set_comparator_func(Arg_comparator * cmp) const2598 bool Type_handler_row::set_comparator_func(Arg_comparator *cmp) const
2599 {
2600 return cmp->set_cmp_func_row();
2601 }
2602
set_comparator_func(Arg_comparator * cmp) const2603 bool Type_handler_int_result::set_comparator_func(Arg_comparator *cmp) const
2604 {
2605 return cmp->set_cmp_func_int();
2606 }
2607
set_comparator_func(Arg_comparator * cmp) const2608 bool Type_handler_real_result::set_comparator_func(Arg_comparator *cmp) const
2609 {
2610 return cmp->set_cmp_func_real();
2611 }
2612
set_comparator_func(Arg_comparator * cmp) const2613 bool Type_handler_decimal_result::set_comparator_func(Arg_comparator *cmp) const
2614 {
2615 return cmp->set_cmp_func_decimal();
2616 }
2617
set_comparator_func(Arg_comparator * cmp) const2618 bool Type_handler_string_result::set_comparator_func(Arg_comparator *cmp) const
2619 {
2620 return cmp->set_cmp_func_string();
2621 }
2622
set_comparator_func(Arg_comparator * cmp) const2623 bool Type_handler_time_common::set_comparator_func(Arg_comparator *cmp) const
2624 {
2625 return cmp->set_cmp_func_time();
2626 }
2627
2628 bool
set_comparator_func(Arg_comparator * cmp) const2629 Type_handler_temporal_with_date::set_comparator_func(Arg_comparator *cmp) const
2630 {
2631 return cmp->set_cmp_func_datetime();
2632 }
2633
2634
2635 /*************************************************************************/
2636
2637 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) const2638 can_change_cond_ref_to_const(Item_bool_func2 *target,
2639 Item *target_expr, Item *target_value,
2640 Item_bool_func2 *source,
2641 Item *source_expr, Item *source_const)
2642 const
2643 {
2644 if (source->compare_type_handler()->cmp_type() != TIME_RESULT)
2645 return false;
2646
2647 /*
2648 Can't rewrite:
2649 WHERE COALESCE(time_column)='00:00:00'
2650 AND COALESCE(time_column)=DATE'2015-09-11'
2651 to
2652 WHERE DATE'2015-09-11'='00:00:00'
2653 AND COALESCE(time_column)=DATE'2015-09-11'
2654 because the left part will erroneously try to parse '00:00:00'
2655 as DATE, not as TIME.
2656
2657 TODO: It could still be rewritten to:
2658 WHERE DATE'2015-09-11'=TIME'00:00:00'
2659 AND COALESCE(time_column)=DATE'2015-09-11'
2660 i.e. we need to replace both target_expr and target_value
2661 at the same time. This is not supported yet.
2662 */
2663 return target_value->cmp_type() == TIME_RESULT;
2664 }
2665
2666
2667 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) const2668 can_change_cond_ref_to_const(Item_bool_func2 *target,
2669 Item *target_expr, Item *target_value,
2670 Item_bool_func2 *source,
2671 Item *source_expr, Item *source_const)
2672 const
2673 {
2674 if (source->compare_type_handler()->cmp_type() != STRING_RESULT)
2675 return false;
2676 /*
2677 In this example:
2678 SET NAMES utf8 COLLATE utf8_german2_ci;
2679 DROP TABLE IF EXISTS t1;
2680 CREATE TABLE t1 (a CHAR(10) CHARACTER SET utf8);
2681 INSERT INTO t1 VALUES ('o-umlaut'),('oe');
2682 SELECT * FROM t1 WHERE a='oe' COLLATE utf8_german2_ci AND a='oe';
2683
2684 the query should return only the row with 'oe'.
2685 It should not return 'o-umlaut', because 'o-umlaut' does not match
2686 the right part of the condition: a='oe'
2687 ('o-umlaut' is not equal to 'oe' in utf8_general_ci,
2688 which is the collation of the field "a").
2689
2690 If we change the right part from:
2691 ... AND a='oe'
2692 to
2693 ... AND 'oe' COLLATE utf8_german2_ci='oe'
2694 it will be evalulated to TRUE and removed from the condition,
2695 so the overall query will be simplified to:
2696
2697 SELECT * FROM t1 WHERE a='oe' COLLATE utf8_german2_ci;
2698
2699 which will erroneously start to return both 'oe' and 'o-umlaut'.
2700 So changing "expr" to "const" is not possible if the effective
2701 collations of "target" and "source" are not exactly the same.
2702
2703 Note, the code before the fix for MDEV-7152 only checked that
2704 collations of "source_const" and "target_value" are the same.
2705 This was not enough, as the bug report demonstrated.
2706 */
2707 return
2708 target->compare_collation() == source->compare_collation() &&
2709 target_value->collation.collation == source_const->collation.collation;
2710 }
2711
2712
2713 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) const2714 can_change_cond_ref_to_const(Item_bool_func2 *target,
2715 Item *target_expr, Item *target_value,
2716 Item_bool_func2 *source,
2717 Item *source_expr, Item *source_const)
2718 const
2719 {
2720 /*
2721 The collations of "target" and "source" do not make sense for numeric
2722 data types.
2723 */
2724 return target->compare_type_handler() == source->compare_type_handler();
2725 }
2726
2727
2728 /*************************************************************************/
2729
2730 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2731 Type_handler_row::Item_get_cache(THD *thd, const Item *item) const
2732 {
2733 return new (thd->mem_root) Item_cache_row(thd);
2734 }
2735
2736 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2737 Type_handler_int_result::Item_get_cache(THD *thd, const Item *item) const
2738 {
2739 return new (thd->mem_root) Item_cache_int(thd, item->type_handler());
2740 }
2741
2742 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2743 Type_handler_year::Item_get_cache(THD *thd, const Item *item) const
2744 {
2745 return new (thd->mem_root) Item_cache_year(thd);
2746 }
2747
2748 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2749 Type_handler_double::Item_get_cache(THD *thd, const Item *item) const
2750 {
2751 return new (thd->mem_root) Item_cache_double(thd);
2752 }
2753
2754 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2755 Type_handler_float::Item_get_cache(THD *thd, const Item *item) const
2756 {
2757 return new (thd->mem_root) Item_cache_float(thd);
2758 }
2759
2760 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2761 Type_handler_decimal_result::Item_get_cache(THD *thd, const Item *item) const
2762 {
2763 return new (thd->mem_root) Item_cache_decimal(thd);
2764 }
2765
2766 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2767 Type_handler_string_result::Item_get_cache(THD *thd, const Item *item) const
2768 {
2769 return new (thd->mem_root) Item_cache_str(thd, item);
2770 }
2771
2772 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2773 Type_handler_timestamp_common::Item_get_cache(THD *thd, const Item *item) const
2774 {
2775 return new (thd->mem_root) Item_cache_datetime(thd);
2776 }
2777
2778 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2779 Type_handler_datetime_common::Item_get_cache(THD *thd, const Item *item) const
2780 {
2781 return new (thd->mem_root) Item_cache_datetime(thd);
2782 }
2783
2784 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2785 Type_handler_time_common::Item_get_cache(THD *thd, const Item *item) const
2786 {
2787 return new (thd->mem_root) Item_cache_time(thd);
2788 }
2789
2790 Item_cache *
Item_get_cache(THD * thd,const Item * item) const2791 Type_handler_date_common::Item_get_cache(THD *thd, const Item *item) const
2792 {
2793 return new (thd->mem_root) Item_cache_date(thd);
2794 }
2795
2796 /*************************************************************************/
2797
2798 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) const2799 Item_hybrid_func_fix_attributes(THD *thd,
2800 const char *func_name,
2801 Type_handler_hybrid_field_type *handler,
2802 Type_all_attributes *func,
2803 Item **items, uint nitems) const
2804 {
2805 bool unsigned_flag= items[0]->unsigned_flag;
2806 for (uint i= 1; i < nitems; i++)
2807 {
2808 if (unsigned_flag != items[i]->unsigned_flag)
2809 {
2810 // Convert a mixture of signed and unsigned int to decimal
2811 handler->set_handler(&type_handler_newdecimal);
2812 func->aggregate_attributes_decimal(items, nitems);
2813 return false;
2814 }
2815 }
2816 func->aggregate_attributes_int(items, nitems);
2817 return false;
2818 }
2819
2820
2821 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) const2822 Item_hybrid_func_fix_attributes(THD *thd,
2823 const char *func_name,
2824 Type_handler_hybrid_field_type *handler,
2825 Type_all_attributes *func,
2826 Item **items, uint nitems) const
2827 {
2828 func->aggregate_attributes_real(items, nitems);
2829 return false;
2830 }
2831
2832
2833 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) const2834 Item_hybrid_func_fix_attributes(THD *thd,
2835 const char *func_name,
2836 Type_handler_hybrid_field_type *handler,
2837 Type_all_attributes *func,
2838 Item **items, uint nitems) const
2839 {
2840 func->aggregate_attributes_decimal(items, nitems);
2841 return false;
2842 }
2843
2844
2845 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) const2846 Item_hybrid_func_fix_attributes(THD *thd,
2847 const char *func_name,
2848 Type_handler_hybrid_field_type *handler,
2849 Type_all_attributes *func,
2850 Item **items, uint nitems) const
2851 {
2852 return func->aggregate_attributes_string(func_name, items, nitems);
2853 }
2854
2855
2856
2857 /*
2858 We can have enum/set type after merging only if we have one enum|set
2859 field (or MIN|MAX(enum|set field)) and number of NULL fields
2860 */
2861 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) const2862 Item_hybrid_func_fix_attributes(THD *thd,
2863 const char *func_name,
2864 Type_handler_hybrid_field_type *handler,
2865 Type_all_attributes *func,
2866 Item **items, uint nitems) const
2867 {
2868 TYPELIB *typelib= NULL;
2869 for (uint i= 0; i < nitems; i++)
2870 {
2871 if ((typelib= items[i]->get_typelib()))
2872 break;
2873 }
2874 DBUG_ASSERT(typelib); // There must be at least one typelib
2875 func->set_typelib(typelib);
2876 return func->aggregate_attributes_string(func_name, items, nitems);
2877 }
2878
2879
2880 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) const2881 Item_hybrid_func_fix_attributes(THD *thd,
2882 const char *func_name,
2883 Type_handler_hybrid_field_type *handler,
2884 Type_all_attributes *func,
2885 Item **items, uint nitems) const
2886 {
2887 if (func->aggregate_attributes_string(func_name, items, nitems))
2888 return true;
2889 handler->set_handler(blob_type_handler(func->max_length));
2890 return false;
2891 }
2892
2893
2894 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) const2895 Item_hybrid_func_fix_attributes(THD *thd,
2896 const char *func_name,
2897 Type_handler_hybrid_field_type *handler,
2898 Type_all_attributes *func,
2899 Item **items, uint nitems) const
2900 {
2901 func->fix_attributes_date();
2902 return false;
2903 }
2904
2905
2906 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) const2907 Item_hybrid_func_fix_attributes(THD *thd,
2908 const char *func_name,
2909 Type_handler_hybrid_field_type *handler,
2910 Type_all_attributes *func,
2911 Item **items, uint nitems) const
2912 {
2913 func->aggregate_attributes_temporal(MIN_TIME_WIDTH, items, nitems);
2914 return false;
2915 }
2916
2917
2918 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) const2919 Item_hybrid_func_fix_attributes(THD *thd,
2920 const char *func_name,
2921 Type_handler_hybrid_field_type *handler,
2922 Type_all_attributes *func,
2923 Item **items, uint nitems) const
2924 {
2925 func->aggregate_attributes_temporal(MAX_DATETIME_WIDTH, items, nitems);
2926 return false;
2927 }
2928
2929
2930 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) const2931 Item_hybrid_func_fix_attributes(THD *thd,
2932 const char *func_name,
2933 Type_handler_hybrid_field_type *handler,
2934 Type_all_attributes *func,
2935 Item **items, uint nitems) const
2936 {
2937 func->aggregate_attributes_temporal(MAX_DATETIME_WIDTH, items, nitems);
2938 return false;
2939 }
2940
2941 #ifdef HAVE_SPATIAL
2942 bool Type_handler_geometry::
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) const2943 Item_hybrid_func_fix_attributes(THD *thd,
2944 const char *func_name,
2945 Type_handler_hybrid_field_type *handler,
2946 Type_all_attributes *func,
2947 Item **items, uint nitems) const
2948 {
2949 DBUG_ASSERT(nitems > 0);
2950 Type_geometry_attributes gattr(items[0]->type_handler(), items[0]);
2951 for (uint i= 1; i < nitems; i++)
2952 gattr.join(items[i]);
2953 func->set_geometry_type(gattr.get_geometry_type());
2954 func->collation.set(&my_charset_bin);
2955 func->unsigned_flag= false;
2956 func->decimals= 0;
2957 func->max_length= (uint32) UINT_MAX32;
2958 func->set_maybe_null(true);
2959 return false;
2960 }
2961 #endif
2962
2963
2964 /*************************************************************************/
2965
2966 bool Type_handler::
Item_func_min_max_fix_attributes(THD * thd,Item_func_min_max * func,Item ** items,uint nitems) const2967 Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
2968 Item **items, uint nitems) const
2969 {
2970 /*
2971 Aggregating attributes for LEAST/GREATES is exactly the same
2972 with aggregating for CASE-alike functions (e.g. COALESCE)
2973 for the majority of data type handlers.
2974 */
2975 return Item_hybrid_func_fix_attributes(thd, func->func_name(),
2976 func, func, items, nitems);
2977 }
2978
2979
2980 bool Type_handler_temporal_result::
Item_func_min_max_fix_attributes(THD * thd,Item_func_min_max * func,Item ** items,uint nitems) const2981 Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
2982 Item **items, uint nitems) const
2983 {
2984 bool rc= Type_handler::Item_func_min_max_fix_attributes(thd, func,
2985 items, nitems);
2986 if (rc || func->maybe_null)
2987 return rc;
2988 /*
2989 LEAST/GREATES(non-temporal, temporal) can return NULL.
2990 CAST functions Item_{time|datetime|date}_typecast always set maybe_full
2991 to true. Here we try to detect nullability more thoroughly.
2992 Perhaps CAST functions should also reuse this idea eventually.
2993 */
2994 const Type_handler *hf= func->type_handler();
2995 for (uint i= 0; i < nitems; i++)
2996 {
2997 /*
2998 If items[i] does not need conversion to the current temporal data
2999 type, then we trust items[i]->maybe_null, which was already ORred
3000 to func->maybe_null in the argument loop in fix_fields().
3001 If items[i] requires conversion to the current temporal data type,
3002 then conversion can fail and return NULL even for NOT NULL items.
3003 */
3004 const Type_handler *ha= items[i]->type_handler();
3005 if (hf == ha)
3006 continue; // No conversion.
3007 if (ha->cmp_type() != TIME_RESULT)
3008 {
3009 func->maybe_null= true; // Conversion from non-temporal is not safe
3010 break;
3011 }
3012 timestamp_type tf= hf->mysql_timestamp_type();
3013 timestamp_type ta= ha->mysql_timestamp_type();
3014 if (tf == ta ||
3015 (tf == MYSQL_TIMESTAMP_DATETIME && ta == MYSQL_TIMESTAMP_DATE))
3016 {
3017 /*
3018 If handlers have the same mysql_timestamp_type(),
3019 then conversion is NULL safe. Conversion from DATE to DATETIME
3020 is also safe. This branch includes data type pairs:
3021 Function return type Argument type Comment
3022 -------------------- ------------- -------------
3023 TIMESTAMP TIMESTAMP no conversion
3024 TIMESTAMP DATETIME not possible
3025 TIMESTAMP DATE not possible
3026 DATETIME DATETIME no conversion
3027 DATETIME TIMESTAMP safe conversion
3028 DATETIME DATE safe conversion
3029 DATE DATE no conversion
3030 TIME TIME no conversion
3031
3032 Note, a function cannot return TIMESTAMP if it has non-TIMESTAMP
3033 arguments (it would return DATETIME in such case).
3034 */
3035 DBUG_ASSERT(hf->field_type() != MYSQL_TYPE_TIMESTAMP || tf == ta);
3036 continue;
3037 }
3038 /*
3039 Here we have the following data type pairs that did not match
3040 the condition above:
3041
3042 Function return type Argument type Comment
3043 -------------------- ------------- -------
3044 TIMESTAMP TIME Not possible
3045 DATETIME TIME depends on OLD_MODE_ZERO_DATE_TIME_CAST
3046 DATE TIMESTAMP Not possible
3047 DATE DATETIME Not possible
3048 DATE TIME Not possible
3049 TIME TIMESTAMP Not possible
3050 TIME DATETIME Not possible
3051 TIME DATE Not possible
3052
3053 Most pairs are not possible, because the function data type
3054 would be DATETIME (according to LEAST/GREATEST aggregation rules).
3055 Conversion to DATETIME from TIME is not safe when
3056 OLD_MODE_ZERO_DATE_TIME_CAST is set:
3057 - negative TIME values cannot be converted to not-NULL DATETIME values
3058 - TIME values can produce DATETIME values that do not pass
3059 NO_ZERO_DATE and NO_ZERO_IN_DATE tests.
3060 */
3061 DBUG_ASSERT(hf->field_type() == MYSQL_TYPE_DATETIME);
3062 if (!(thd->variables.old_behavior & OLD_MODE_ZERO_DATE_TIME_CAST))
3063 continue;
3064 func->maybe_null= true;
3065 break;
3066 }
3067 return rc;
3068 }
3069
3070
3071 bool Type_handler_real_result::
Item_func_min_max_fix_attributes(THD * thd,Item_func_min_max * func,Item ** items,uint nitems) const3072 Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
3073 Item **items, uint nitems) const
3074 {
3075 /*
3076 DOUBLE is an exception and aggregates attributes differently
3077 for LEAST/GREATEST vs CASE-alike functions. See the comment in
3078 Item_func_min_max::aggregate_attributes_real().
3079 */
3080 func->aggregate_attributes_real(items, nitems);
3081 return false;
3082 }
3083
3084 /*************************************************************************/
3085
3086 bool Type_handler_int_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const3087 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
3088 {
3089 return func->fix_length_and_dec_numeric(&type_handler_longlong);
3090 }
3091
3092
3093 bool Type_handler_real_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const3094 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
3095 {
3096 (void) func->fix_length_and_dec_numeric(&type_handler_double);
3097 func->max_length= func->float_length(func->decimals);
3098 return false;
3099 }
3100
3101
3102 bool Type_handler_decimal_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const3103 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
3104 {
3105 return func->fix_length_and_dec_numeric(&type_handler_newdecimal);
3106 }
3107
3108
3109 bool Type_handler_string_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const3110 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
3111 {
3112 return func->fix_length_and_dec_string();
3113 }
3114
3115
3116 /**
3117 Traditional temporal types always preserve the type of the argument.
3118 */
3119 bool Type_handler_temporal_result::
Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid * func) const3120 Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const
3121 {
3122 return func->fix_length_and_dec_generic();
3123 }
3124
3125
3126 /*************************************************************************/
3127
3128 bool Type_handler_int_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const3129 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
3130 {
3131 item->fix_length_and_dec_decimal();
3132 return false;
3133 }
3134
3135
3136 bool Type_handler_decimal_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const3137 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
3138 {
3139 item->fix_length_and_dec_decimal();
3140 return false;
3141 }
3142
3143
3144 bool Type_handler_temporal_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const3145 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
3146 {
3147 item->fix_length_and_dec_decimal();
3148 return false;
3149 }
3150
3151
3152 bool Type_handler_real_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const3153 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
3154 {
3155 item->fix_length_and_dec_double();
3156 return false;
3157 }
3158
3159
3160 bool Type_handler_string_result::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const3161 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
3162 {
3163 item->fix_length_and_dec_double();
3164 return false;
3165 }
3166
3167
3168 #ifdef HAVE_SPATIAL
3169 bool Type_handler_geometry::
Item_sum_sum_fix_length_and_dec(Item_sum_sum * item) const3170 Item_sum_sum_fix_length_and_dec(Item_sum_sum *item) const
3171 {
3172 return Item_func_or_sum_illegal_param("sum");
3173 }
3174 #endif
3175
3176
3177 /*************************************************************************/
3178
3179 bool Type_handler_int_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const3180 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
3181 {
3182 item->fix_length_and_dec_decimal();
3183 return false;
3184 }
3185
3186
3187 bool Type_handler_decimal_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const3188 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
3189 {
3190 item->fix_length_and_dec_decimal();
3191 return false;
3192 }
3193
3194
3195 bool Type_handler_temporal_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const3196 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
3197 {
3198 item->fix_length_and_dec_decimal();
3199 return false;
3200 }
3201
3202
3203 bool Type_handler_real_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const3204 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
3205 {
3206 item->fix_length_and_dec_double();
3207 return false;
3208 }
3209
3210
3211 bool Type_handler_string_result::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const3212 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
3213 {
3214 item->fix_length_and_dec_double();
3215 return false;
3216 }
3217
3218
3219 #ifdef HAVE_SPATIAL
3220 bool Type_handler_geometry::
Item_sum_avg_fix_length_and_dec(Item_sum_avg * item) const3221 Item_sum_avg_fix_length_and_dec(Item_sum_avg *item) const
3222 {
3223 return Item_func_or_sum_illegal_param("avg");
3224 }
3225 #endif
3226
3227
3228 /*************************************************************************/
3229
3230 bool Type_handler_int_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const3231 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
3232 {
3233 item->fix_length_and_dec_decimal();
3234 return false;
3235 }
3236
3237
3238 bool Type_handler_decimal_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const3239 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
3240 {
3241 item->fix_length_and_dec_decimal();
3242 return false;
3243 }
3244
3245
3246 bool Type_handler_temporal_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const3247 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
3248 {
3249 item->fix_length_and_dec_decimal();
3250 return false;
3251 }
3252
3253
3254 bool Type_handler_real_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const3255 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
3256 {
3257 item->fix_length_and_dec_double();
3258 return false;
3259 }
3260
3261
3262 bool Type_handler_string_result::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const3263 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
3264 {
3265 item->fix_length_and_dec_double();
3266 return false;
3267 }
3268
3269
3270 #ifdef HAVE_SPATIAL
3271 bool Type_handler_geometry::
Item_sum_variance_fix_length_and_dec(Item_sum_variance * item) const3272 Item_sum_variance_fix_length_and_dec(Item_sum_variance *item) const
3273 {
3274 return Item_func_or_sum_illegal_param(item);
3275 }
3276 #endif
3277
3278
3279 /*************************************************************************/
3280
Item_val_bool(Item * item) const3281 bool Type_handler_real_result::Item_val_bool(Item *item) const
3282 {
3283 return item->val_real() != 0.0;
3284 }
3285
Item_val_bool(Item * item) const3286 bool Type_handler_int_result::Item_val_bool(Item *item) const
3287 {
3288 return item->val_int() != 0;
3289 }
3290
Item_val_bool(Item * item) const3291 bool Type_handler_decimal_result::Item_val_bool(Item *item) const
3292 {
3293 my_decimal decimal_value;
3294 my_decimal *val= item->val_decimal(&decimal_value);
3295 if (val)
3296 return !my_decimal_is_zero(val);
3297 return false;
3298 }
3299
Item_val_bool(Item * item) const3300 bool Type_handler_temporal_result::Item_val_bool(Item *item) const
3301 {
3302 return item->val_real() != 0.0;
3303 }
3304
Item_val_bool(Item * item) const3305 bool Type_handler_string_result::Item_val_bool(Item *item) const
3306 {
3307 return item->val_real() != 0.0;
3308 }
3309
3310
3311 /*************************************************************************/
3312
Item_get_date(Item * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3313 bool Type_handler_int_result::Item_get_date(Item *item, MYSQL_TIME *ltime,
3314 ulonglong fuzzydate) const
3315 {
3316 return item->get_date_from_int(ltime, fuzzydate);
3317 }
3318
3319
Item_get_date(Item * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3320 bool Type_handler_year::Item_get_date(Item *item, MYSQL_TIME *ltime,
3321 ulonglong fuzzydate) const
3322 {
3323 return item->get_date_from_year(ltime, fuzzydate);
3324 }
3325
3326
Item_get_date(Item * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3327 bool Type_handler_real_result::Item_get_date(Item *item, MYSQL_TIME *ltime,
3328 ulonglong fuzzydate) const
3329 {
3330 return item->get_date_from_real(ltime, fuzzydate);
3331 }
3332
3333
Item_get_date(Item * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3334 bool Type_handler_decimal_result::Item_get_date(Item *item, MYSQL_TIME *ltime,
3335 ulonglong fuzzydate) const
3336 {
3337 return item->get_date_from_decimal(ltime, fuzzydate);
3338 }
3339
3340
Item_get_date(Item * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3341 bool Type_handler_string_result::Item_get_date(Item *item, MYSQL_TIME *ltime,
3342 ulonglong fuzzydate) const
3343 {
3344 return item->get_date_from_string(ltime, fuzzydate);
3345 }
3346
3347
Item_get_date(Item * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3348 bool Type_handler_temporal_result::Item_get_date(Item *item, MYSQL_TIME *ltime,
3349 ulonglong fuzzydate) const
3350 {
3351 DBUG_ASSERT(0); // Temporal type items must implement native get_date()
3352 item->null_value= true;
3353 set_zero_time(ltime, mysql_timestamp_type());
3354 return true;
3355 }
3356
3357
3358 /*************************************************************************/
3359
3360 longlong Type_handler_real_result::
Item_val_int_signed_typecast(Item * item) const3361 Item_val_int_signed_typecast(Item *item) const
3362 {
3363 return item->val_int();
3364 }
3365
3366 longlong Type_handler_int_result::
Item_val_int_signed_typecast(Item * item) const3367 Item_val_int_signed_typecast(Item *item) const
3368 {
3369 return item->val_int_signed_typecast_from_int();
3370 }
3371
3372 longlong Type_handler_decimal_result::
Item_val_int_signed_typecast(Item * item) const3373 Item_val_int_signed_typecast(Item *item) const
3374 {
3375 return item->val_int();
3376 }
3377
3378 longlong Type_handler_temporal_result::
Item_val_int_signed_typecast(Item * item) const3379 Item_val_int_signed_typecast(Item *item) const
3380 {
3381 return item->val_int();
3382 }
3383
3384 longlong Type_handler_string_result::
Item_val_int_signed_typecast(Item * item) const3385 Item_val_int_signed_typecast(Item *item) const
3386 {
3387 return item->val_int_signed_typecast_from_str();
3388 }
3389
3390 /*************************************************************************/
3391
3392 longlong Type_handler_real_result::
Item_val_int_unsigned_typecast(Item * item) const3393 Item_val_int_unsigned_typecast(Item *item) const
3394 {
3395 return item->val_int_unsigned_typecast_from_int();
3396 }
3397
3398 longlong Type_handler_int_result::
Item_val_int_unsigned_typecast(Item * item) const3399 Item_val_int_unsigned_typecast(Item *item) const
3400 {
3401 return item->val_int_unsigned_typecast_from_int();
3402 }
3403
3404 longlong Type_handler_decimal_result::
Item_val_int_unsigned_typecast(Item * item) const3405 Item_val_int_unsigned_typecast(Item *item) const
3406 {
3407 return item->val_int_unsigned_typecast_from_decimal();
3408 }
3409
3410 longlong Type_handler_temporal_result::
Item_val_int_unsigned_typecast(Item * item) const3411 Item_val_int_unsigned_typecast(Item *item) const
3412 {
3413 return item->val_int_unsigned_typecast_from_int();
3414 }
3415
3416 longlong Type_handler_string_result::
Item_val_int_unsigned_typecast(Item * item) const3417 Item_val_int_unsigned_typecast(Item *item) const
3418 {
3419 return item->val_int_unsigned_typecast_from_str();
3420 }
3421
3422 /*************************************************************************/
3423
3424 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const3425 Type_handler_real_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
3426 String *str) const
3427 {
3428 return item->val_str_ascii_from_val_real(str);
3429 }
3430
3431
3432 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const3433 Type_handler_decimal_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
3434 String *str) const
3435 {
3436 return item->val_str_ascii_from_val_real(str);
3437 }
3438
3439
3440 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const3441 Type_handler_int_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
3442 String *str) const
3443 {
3444 return item->val_str_ascii_from_val_int(str);
3445 }
3446
3447
3448 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const3449 Type_handler_temporal_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
3450 String *str) const
3451 {
3452 return item->val_str_ascii_from_val_str(str);
3453 }
3454
3455
3456 String *
Item_func_hex_val_str_ascii(Item_func_hex * item,String * str) const3457 Type_handler_string_result::Item_func_hex_val_str_ascii(Item_func_hex *item,
3458 String *str) const
3459 {
3460 return item->val_str_ascii_from_val_str(str);
3461 }
3462
3463 /***************************************************************************/
3464
3465 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const3466 Type_handler_decimal_result::Item_func_hybrid_field_type_val_str(
3467 Item_func_hybrid_field_type *item,
3468 String *str) const
3469 {
3470 return item->val_str_from_decimal_op(str);
3471 }
3472
3473
3474 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const3475 Type_handler_decimal_result::Item_func_hybrid_field_type_val_real(
3476 Item_func_hybrid_field_type *item)
3477 const
3478 {
3479 return item->val_real_from_decimal_op();
3480 }
3481
3482
3483 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const3484 Type_handler_decimal_result::Item_func_hybrid_field_type_val_int(
3485 Item_func_hybrid_field_type *item)
3486 const
3487 {
3488 return item->val_int_from_decimal_op();
3489 }
3490
3491
3492 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const3493 Type_handler_decimal_result::Item_func_hybrid_field_type_val_decimal(
3494 Item_func_hybrid_field_type *item,
3495 my_decimal *dec) const
3496 {
3497 return item->val_decimal_from_decimal_op(dec);
3498 }
3499
3500
3501 bool
Item_func_hybrid_field_type_get_date(Item_func_hybrid_field_type * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3502 Type_handler_decimal_result::Item_func_hybrid_field_type_get_date(
3503 Item_func_hybrid_field_type *item,
3504 MYSQL_TIME *ltime,
3505 ulonglong fuzzydate) const
3506 {
3507 return item->get_date_from_decimal_op(ltime, fuzzydate);
3508 }
3509
3510
3511 /***************************************************************************/
3512
3513
3514 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const3515 Type_handler_int_result::Item_func_hybrid_field_type_val_str(
3516 Item_func_hybrid_field_type *item,
3517 String *str) const
3518 {
3519 return item->val_str_from_int_op(str);
3520 }
3521
3522
3523 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const3524 Type_handler_int_result::Item_func_hybrid_field_type_val_real(
3525 Item_func_hybrid_field_type *item)
3526 const
3527 {
3528 return item->val_real_from_int_op();
3529 }
3530
3531
3532 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const3533 Type_handler_int_result::Item_func_hybrid_field_type_val_int(
3534 Item_func_hybrid_field_type *item)
3535 const
3536 {
3537 return item->val_int_from_int_op();
3538 }
3539
3540
3541 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const3542 Type_handler_int_result::Item_func_hybrid_field_type_val_decimal(
3543 Item_func_hybrid_field_type *item,
3544 my_decimal *dec) const
3545 {
3546 return item->val_decimal_from_int_op(dec);
3547 }
3548
3549
3550 bool
Item_func_hybrid_field_type_get_date(Item_func_hybrid_field_type * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3551 Type_handler_int_result::Item_func_hybrid_field_type_get_date(
3552 Item_func_hybrid_field_type *item,
3553 MYSQL_TIME *ltime,
3554 ulonglong fuzzydate) const
3555 {
3556 return item->get_date_from_int_op(ltime, fuzzydate);
3557 }
3558
3559
3560
3561 /***************************************************************************/
3562
3563 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const3564 Type_handler_double::Item_func_hybrid_field_type_val_str(
3565 Item_func_hybrid_field_type *item,
3566 String *str) const
3567 {
3568 return item->val_str_from_real_op(str);
3569 }
3570
3571
3572 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const3573 Type_handler_float::Item_func_hybrid_field_type_val_str(
3574 Item_func_hybrid_field_type *item,
3575 String *str) const
3576 {
3577 Float nr(item->real_op());
3578 if (item->null_value)
3579 return 0;
3580 nr.to_string(str, item->decimals);
3581 return str;
3582 }
3583
3584
3585 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const3586 Type_handler_real_result::Item_func_hybrid_field_type_val_real(
3587 Item_func_hybrid_field_type *item)
3588 const
3589 {
3590 return item->val_real_from_real_op();
3591 }
3592
3593
3594 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const3595 Type_handler_real_result::Item_func_hybrid_field_type_val_int(
3596 Item_func_hybrid_field_type *item)
3597 const
3598 {
3599 return item->val_int_from_real_op();
3600 }
3601
3602
3603 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const3604 Type_handler_real_result::Item_func_hybrid_field_type_val_decimal(
3605 Item_func_hybrid_field_type *item,
3606 my_decimal *dec) const
3607 {
3608 return item->val_decimal_from_real_op(dec);
3609 }
3610
3611
3612 bool
Item_func_hybrid_field_type_get_date(Item_func_hybrid_field_type * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3613 Type_handler_real_result::Item_func_hybrid_field_type_get_date(
3614 Item_func_hybrid_field_type *item,
3615 MYSQL_TIME *ltime,
3616 ulonglong fuzzydate) const
3617 {
3618 return item->get_date_from_real_op(ltime, fuzzydate);
3619 }
3620
3621
3622 /***************************************************************************/
3623
3624 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const3625 Type_handler_temporal_result::Item_func_hybrid_field_type_val_str(
3626 Item_func_hybrid_field_type *item,
3627 String *str) const
3628 {
3629 return item->val_str_from_date_op(str);
3630 }
3631
3632
3633 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const3634 Type_handler_temporal_result::Item_func_hybrid_field_type_val_real(
3635 Item_func_hybrid_field_type *item)
3636 const
3637 {
3638 return item->val_real_from_date_op();
3639 }
3640
3641
3642 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const3643 Type_handler_temporal_result::Item_func_hybrid_field_type_val_int(
3644 Item_func_hybrid_field_type *item)
3645 const
3646 {
3647 return item->val_int_from_date_op();
3648 }
3649
3650
3651 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const3652 Type_handler_temporal_result::Item_func_hybrid_field_type_val_decimal(
3653 Item_func_hybrid_field_type *item,
3654 my_decimal *dec) const
3655 {
3656 return item->val_decimal_from_date_op(dec);
3657 }
3658
3659
3660 bool
Item_func_hybrid_field_type_get_date(Item_func_hybrid_field_type * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3661 Type_handler_temporal_result::Item_func_hybrid_field_type_get_date(
3662 Item_func_hybrid_field_type *item,
3663 MYSQL_TIME *ltime,
3664 ulonglong fuzzydate) const
3665 {
3666 return item->date_op(ltime, fuzzydate);
3667 }
3668
3669
3670 /***************************************************************************/
3671
3672 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const3673 Type_handler_time_common::Item_func_hybrid_field_type_val_str(
3674 Item_func_hybrid_field_type *item,
3675 String *str) const
3676 {
3677 return item->val_str_from_time_op(str);
3678 }
3679
3680
3681 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const3682 Type_handler_time_common::Item_func_hybrid_field_type_val_real(
3683 Item_func_hybrid_field_type *item)
3684 const
3685 {
3686 return item->val_real_from_time_op();
3687 }
3688
3689
3690 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const3691 Type_handler_time_common::Item_func_hybrid_field_type_val_int(
3692 Item_func_hybrid_field_type *item)
3693 const
3694 {
3695 return item->val_int_from_time_op();
3696 }
3697
3698
3699 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const3700 Type_handler_time_common::Item_func_hybrid_field_type_val_decimal(
3701 Item_func_hybrid_field_type *item,
3702 my_decimal *dec) const
3703 {
3704 return item->val_decimal_from_time_op(dec);
3705 }
3706
3707
3708 bool
Item_func_hybrid_field_type_get_date(Item_func_hybrid_field_type * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3709 Type_handler_time_common::Item_func_hybrid_field_type_get_date(
3710 Item_func_hybrid_field_type *item,
3711 MYSQL_TIME *ltime,
3712 ulonglong fuzzydate) const
3713 {
3714 return item->time_op(ltime);
3715 }
3716
3717
3718 /***************************************************************************/
3719
3720 String *
Item_func_hybrid_field_type_val_str(Item_func_hybrid_field_type * item,String * str) const3721 Type_handler_string_result::Item_func_hybrid_field_type_val_str(
3722 Item_func_hybrid_field_type *item,
3723 String *str) const
3724 {
3725 return item->val_str_from_str_op(str);
3726 }
3727
3728
3729 double
Item_func_hybrid_field_type_val_real(Item_func_hybrid_field_type * item) const3730 Type_handler_string_result::Item_func_hybrid_field_type_val_real(
3731 Item_func_hybrid_field_type *item)
3732 const
3733 {
3734 return item->val_real_from_str_op();
3735 }
3736
3737
3738 longlong
Item_func_hybrid_field_type_val_int(Item_func_hybrid_field_type * item) const3739 Type_handler_string_result::Item_func_hybrid_field_type_val_int(
3740 Item_func_hybrid_field_type *item)
3741 const
3742 {
3743 return item->val_int_from_str_op();
3744 }
3745
3746
3747 my_decimal *
Item_func_hybrid_field_type_val_decimal(Item_func_hybrid_field_type * item,my_decimal * dec) const3748 Type_handler_string_result::Item_func_hybrid_field_type_val_decimal(
3749 Item_func_hybrid_field_type *item,
3750 my_decimal *dec) const
3751 {
3752 return item->val_decimal_from_str_op(dec);
3753 }
3754
3755
3756 bool
Item_func_hybrid_field_type_get_date(Item_func_hybrid_field_type * item,MYSQL_TIME * ltime,ulonglong fuzzydate) const3757 Type_handler_string_result::Item_func_hybrid_field_type_get_date(
3758 Item_func_hybrid_field_type *item,
3759 MYSQL_TIME *ltime,
3760 ulonglong fuzzydate) const
3761 {
3762 return item->get_date_from_str_op(ltime, fuzzydate);
3763 }
3764
3765 /***************************************************************************/
3766
3767 bool Type_handler_numeric::
Item_func_between_fix_length_and_dec(Item_func_between * func) const3768 Item_func_between_fix_length_and_dec(Item_func_between *func) const
3769 {
3770 return func->fix_length_and_dec_numeric(current_thd);
3771 }
3772
3773 bool Type_handler_temporal_result::
Item_func_between_fix_length_and_dec(Item_func_between * func) const3774 Item_func_between_fix_length_and_dec(Item_func_between *func) const
3775 {
3776 return func->fix_length_and_dec_temporal(current_thd);
3777 }
3778
3779 bool Type_handler_string_result::
Item_func_between_fix_length_and_dec(Item_func_between * func) const3780 Item_func_between_fix_length_and_dec(Item_func_between *func) const
3781 {
3782 return func->fix_length_and_dec_string(current_thd);
3783 }
3784
3785
3786 longlong Type_handler_row::
Item_func_between_val_int(Item_func_between * func) const3787 Item_func_between_val_int(Item_func_between *func) const
3788 {
3789 DBUG_ASSERT(0);
3790 func->null_value= true;
3791 return 0;
3792 }
3793
3794 longlong Type_handler_string_result::
Item_func_between_val_int(Item_func_between * func) const3795 Item_func_between_val_int(Item_func_between *func) const
3796 {
3797 return func->val_int_cmp_string();
3798 }
3799
3800 longlong Type_handler_temporal_result::
Item_func_between_val_int(Item_func_between * func) const3801 Item_func_between_val_int(Item_func_between *func) const
3802 {
3803 return func->val_int_cmp_temporal();
3804 }
3805
3806 longlong Type_handler_int_result::
Item_func_between_val_int(Item_func_between * func) const3807 Item_func_between_val_int(Item_func_between *func) const
3808 {
3809 return func->val_int_cmp_int();
3810 }
3811
3812 longlong Type_handler_real_result::
Item_func_between_val_int(Item_func_between * func) const3813 Item_func_between_val_int(Item_func_between *func) const
3814 {
3815 return func->val_int_cmp_real();
3816 }
3817
3818 longlong Type_handler_decimal_result::
Item_func_between_val_int(Item_func_between * func) const3819 Item_func_between_val_int(Item_func_between *func) const
3820 {
3821 return func->val_int_cmp_decimal();
3822 }
3823
3824 /***************************************************************************/
3825
make_cmp_item(THD * thd,CHARSET_INFO * cs) const3826 cmp_item *Type_handler_int_result::make_cmp_item(THD *thd,
3827 CHARSET_INFO *cs) const
3828 {
3829 return new (thd->mem_root) cmp_item_int;
3830 }
3831
make_cmp_item(THD * thd,CHARSET_INFO * cs) const3832 cmp_item *Type_handler_real_result::make_cmp_item(THD *thd,
3833 CHARSET_INFO *cs) const
3834 {
3835 return new (thd->mem_root) cmp_item_real;
3836 }
3837
make_cmp_item(THD * thd,CHARSET_INFO * cs) const3838 cmp_item *Type_handler_decimal_result::make_cmp_item(THD *thd,
3839 CHARSET_INFO *cs) const
3840 {
3841 return new (thd->mem_root) cmp_item_decimal;
3842 }
3843
3844
make_cmp_item(THD * thd,CHARSET_INFO * cs) const3845 cmp_item *Type_handler_string_result::make_cmp_item(THD *thd,
3846 CHARSET_INFO *cs) const
3847 {
3848 return new (thd->mem_root) cmp_item_sort_string(cs);
3849 }
3850
make_cmp_item(THD * thd,CHARSET_INFO * cs) const3851 cmp_item *Type_handler_row::make_cmp_item(THD *thd,
3852 CHARSET_INFO *cs) const
3853 {
3854 return new (thd->mem_root) cmp_item_row;
3855 }
3856
make_cmp_item(THD * thd,CHARSET_INFO * cs) const3857 cmp_item *Type_handler_time_common::make_cmp_item(THD *thd,
3858 CHARSET_INFO *cs) const
3859 {
3860 return new (thd->mem_root) cmp_item_time;
3861 }
3862
make_cmp_item(THD * thd,CHARSET_INFO * cs) const3863 cmp_item *Type_handler_temporal_with_date::make_cmp_item(THD *thd,
3864 CHARSET_INFO *cs) const
3865 {
3866 return new (thd->mem_root) cmp_item_datetime;
3867 }
3868
3869 /***************************************************************************/
3870
srtcmp_in(const void * cs_,const void * x_,const void * y_)3871 static int srtcmp_in(const void *cs_, const void *x_, const void *y_)
3872 {
3873 const CHARSET_INFO *cs= static_cast<const CHARSET_INFO *>(cs_);
3874 const String *x= static_cast<const String *>(x_);
3875 const String *y= static_cast<const String *>(y_);
3876 return cs->coll->strnncollsp(cs,
3877 (uchar *) x->ptr(),x->length(),
3878 (uchar *) y->ptr(),y->length());
3879 }
3880
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const3881 in_vector *Type_handler_string_result::make_in_vector(THD *thd,
3882 const Item_func_in *func,
3883 uint nargs) const
3884 {
3885 return new (thd->mem_root) in_string(thd, nargs, (qsort2_cmp) srtcmp_in,
3886 func->compare_collation());
3887
3888 }
3889
3890
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const3891 in_vector *Type_handler_int_result::make_in_vector(THD *thd,
3892 const Item_func_in *func,
3893 uint nargs) const
3894 {
3895 return new (thd->mem_root) in_longlong(thd, nargs);
3896 }
3897
3898
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const3899 in_vector *Type_handler_real_result::make_in_vector(THD *thd,
3900 const Item_func_in *func,
3901 uint nargs) const
3902 {
3903 return new (thd->mem_root) in_double(thd, nargs);
3904 }
3905
3906
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const3907 in_vector *Type_handler_decimal_result::make_in_vector(THD *thd,
3908 const Item_func_in *func,
3909 uint nargs) const
3910 {
3911 return new (thd->mem_root) in_decimal(thd, nargs);
3912 }
3913
3914
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const3915 in_vector *Type_handler_time_common::make_in_vector(THD *thd,
3916 const Item_func_in *func,
3917 uint nargs) const
3918 {
3919 return new (thd->mem_root) in_time(thd, nargs);
3920 }
3921
3922
3923 in_vector *
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const3924 Type_handler_temporal_with_date::make_in_vector(THD *thd,
3925 const Item_func_in *func,
3926 uint nargs) const
3927 {
3928 return new (thd->mem_root) in_datetime(thd, nargs);
3929 }
3930
3931
make_in_vector(THD * thd,const Item_func_in * func,uint nargs) const3932 in_vector *Type_handler_row::make_in_vector(THD *thd,
3933 const Item_func_in *func,
3934 uint nargs) const
3935 {
3936 return new (thd->mem_root) in_row(thd, nargs, 0);
3937 }
3938
3939 /***************************************************************************/
3940
3941 bool Type_handler_string_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const3942 Item_func_in_fix_comparator_compatible_types(THD *thd,
3943 Item_func_in *func) const
3944 {
3945 if (func->agg_all_arg_charsets_for_comparison())
3946 return true;
3947 if (func->compatible_types_scalar_bisection_possible())
3948 {
3949 return func->value_list_convert_const_to_int(thd) ||
3950 func->fix_for_scalar_comparison_using_bisection(thd);
3951 }
3952 return
3953 func->fix_for_scalar_comparison_using_cmp_items(thd,
3954 1U << (uint) STRING_RESULT);
3955 }
3956
3957
3958 bool Type_handler_int_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const3959 Item_func_in_fix_comparator_compatible_types(THD *thd,
3960 Item_func_in *func) const
3961 {
3962 /*
3963 Does not need to call value_list_convert_const_to_int()
3964 as already handled by int handler.
3965 */
3966 return func->compatible_types_scalar_bisection_possible() ?
3967 func->fix_for_scalar_comparison_using_bisection(thd) :
3968 func->fix_for_scalar_comparison_using_cmp_items(thd,
3969 1U << (uint) INT_RESULT);
3970 }
3971
3972
3973 bool Type_handler_real_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const3974 Item_func_in_fix_comparator_compatible_types(THD *thd,
3975 Item_func_in *func) const
3976 {
3977 return func->compatible_types_scalar_bisection_possible() ?
3978 (func->value_list_convert_const_to_int(thd) ||
3979 func->fix_for_scalar_comparison_using_bisection(thd)) :
3980 func->fix_for_scalar_comparison_using_cmp_items(thd,
3981 1U << (uint) REAL_RESULT);
3982 }
3983
3984
3985 bool Type_handler_decimal_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const3986 Item_func_in_fix_comparator_compatible_types(THD *thd,
3987 Item_func_in *func) const
3988 {
3989 return func->compatible_types_scalar_bisection_possible() ?
3990 (func->value_list_convert_const_to_int(thd) ||
3991 func->fix_for_scalar_comparison_using_bisection(thd)) :
3992 func->fix_for_scalar_comparison_using_cmp_items(thd,
3993 1U << (uint) DECIMAL_RESULT);
3994 }
3995
3996
3997 bool Type_handler_temporal_result::
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const3998 Item_func_in_fix_comparator_compatible_types(THD *thd,
3999 Item_func_in *func) const
4000 {
4001 return func->compatible_types_scalar_bisection_possible() ?
4002 (func->value_list_convert_const_to_int(thd) ||
4003 func->fix_for_scalar_comparison_using_bisection(thd)) :
4004 func->fix_for_scalar_comparison_using_cmp_items(thd,
4005 1U << (uint) TIME_RESULT);
4006 }
4007
4008
Item_func_in_fix_comparator_compatible_types(THD * thd,Item_func_in * func) const4009 bool Type_handler_row::Item_func_in_fix_comparator_compatible_types(THD *thd,
4010 Item_func_in *func) const
4011 {
4012 return func->compatible_types_row_bisection_possible() ?
4013 func->fix_for_row_comparison_using_bisection(thd) :
4014 func->fix_for_row_comparison_using_cmp_items(thd);
4015 }
4016
4017 /***************************************************************************/
4018
4019 String *Type_handler_string_result::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const4020 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
4021 {
4022 return func->val_str_native(str);
4023 }
4024
4025
4026 String *Type_handler_temporal_result::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const4027 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
4028 {
4029 return func->val_string_from_date(str);
4030 }
4031
4032
4033 String *Type_handler_int_result::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const4034 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
4035 {
4036 return func->val_string_from_int(str);
4037 }
4038
4039
4040 String *Type_handler_decimal_result::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const4041 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
4042 {
4043 return func->val_string_from_decimal(str);
4044 }
4045
4046
4047 String *Type_handler_double::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const4048 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
4049 {
4050 return func->val_string_from_real(str);
4051 }
4052
4053
4054 String *Type_handler_float::
Item_func_min_max_val_str(Item_func_min_max * func,String * str) const4055 Item_func_min_max_val_str(Item_func_min_max *func, String *str) const
4056 {
4057 Float nr(func->val_real());
4058 if (func->null_value)
4059 return 0;
4060 nr.to_string(str, func->decimals);
4061 return str;
4062 }
4063
4064
4065 double Type_handler_string_result::
Item_func_min_max_val_real(Item_func_min_max * func) const4066 Item_func_min_max_val_real(Item_func_min_max *func) const
4067 {
4068 return func->val_real_native();
4069 }
4070
4071
4072 double Type_handler_temporal_result::
Item_func_min_max_val_real(Item_func_min_max * func) const4073 Item_func_min_max_val_real(Item_func_min_max *func) const
4074 {
4075 MYSQL_TIME ltime;
4076 if (func->get_date(<ime, 0))
4077 return 0;
4078 return TIME_to_double(<ime);
4079 }
4080
4081
4082 double Type_handler_numeric::
Item_func_min_max_val_real(Item_func_min_max * func) const4083 Item_func_min_max_val_real(Item_func_min_max *func) const
4084 {
4085 return func->val_real_native();
4086 }
4087
4088
4089 longlong Type_handler_string_result::
Item_func_min_max_val_int(Item_func_min_max * func) const4090 Item_func_min_max_val_int(Item_func_min_max *func) const
4091 {
4092 return func->val_int_native();
4093 }
4094
4095
4096 longlong Type_handler_temporal_result::
Item_func_min_max_val_int(Item_func_min_max * func) const4097 Item_func_min_max_val_int(Item_func_min_max *func) const
4098 {
4099 MYSQL_TIME ltime;
4100 if (func->get_date(<ime, 0))
4101 return 0;
4102 return TIME_to_ulonglong(<ime);
4103 }
4104
4105
4106 longlong Type_handler_numeric::
Item_func_min_max_val_int(Item_func_min_max * func) const4107 Item_func_min_max_val_int(Item_func_min_max *func) const
4108 {
4109 return func->val_int_native();
4110 }
4111
4112
4113 my_decimal *Type_handler_string_result::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const4114 Item_func_min_max_val_decimal(Item_func_min_max *func,
4115 my_decimal *dec) const
4116 {
4117 return func->val_decimal_native(dec);
4118 }
4119
4120
4121 my_decimal *Type_handler_numeric::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const4122 Item_func_min_max_val_decimal(Item_func_min_max *func,
4123 my_decimal *dec) const
4124 {
4125 return func->val_decimal_native(dec);
4126 }
4127
4128
4129 my_decimal *Type_handler_temporal_result::
Item_func_min_max_val_decimal(Item_func_min_max * func,my_decimal * dec) const4130 Item_func_min_max_val_decimal(Item_func_min_max *func,
4131 my_decimal *dec) const
4132 {
4133 MYSQL_TIME ltime;
4134 if (func->get_date(<ime, 0))
4135 return 0;
4136 return date2my_decimal(<ime, dec);
4137 }
4138
4139
4140 bool Type_handler_string_result::
Item_func_min_max_get_date(Item_func_min_max * func,MYSQL_TIME * ltime,ulonglong fuzzydate) const4141 Item_func_min_max_get_date(Item_func_min_max *func,
4142 MYSQL_TIME *ltime, ulonglong fuzzydate) const
4143 {
4144 /*
4145 just like ::val_int() method of a string item can be called,
4146 for example, SELECT CONCAT("10", "12") + 1,
4147 ::get_date() can be called for non-temporal values,
4148 for example, SELECT MONTH(GREATEST("2011-11-21", "2010-10-09"))
4149 */
4150 return func->get_date_from_string(ltime, fuzzydate);
4151 }
4152
4153
4154 bool Type_handler_numeric::
Item_func_min_max_get_date(Item_func_min_max * func,MYSQL_TIME * ltime,ulonglong fuzzydate) const4155 Item_func_min_max_get_date(Item_func_min_max *func,
4156 MYSQL_TIME *ltime, ulonglong fuzzydate) const
4157 {
4158 return Item_get_date(func, ltime, fuzzydate);
4159 }
4160
4161
4162 bool Type_handler_temporal_result::
Item_func_min_max_get_date(Item_func_min_max * func,MYSQL_TIME * ltime,ulonglong fuzzydate) const4163 Item_func_min_max_get_date(Item_func_min_max *func,
4164 MYSQL_TIME *ltime, ulonglong fuzzydate) const
4165 {
4166 return func->get_date_native(ltime, fuzzydate);
4167 }
4168
4169 bool Type_handler_time_common::
Item_func_min_max_get_date(Item_func_min_max * func,MYSQL_TIME * ltime,ulonglong fuzzydate) const4170 Item_func_min_max_get_date(Item_func_min_max *func,
4171 MYSQL_TIME *ltime, ulonglong fuzzydate) const
4172 {
4173 return func->get_time_native(ltime);
4174 }
4175
4176 /***************************************************************************/
4177
4178 /**
4179 Get a string representation of the Item value.
4180 See sql_type.h for details.
4181 */
4182 String *Type_handler_row::
print_item_value(THD * thd,Item * item,String * str) const4183 print_item_value(THD *thd, Item *item, String *str) const
4184 {
4185 CHARSET_INFO *cs= thd->variables.character_set_client;
4186 StringBuffer<STRING_BUFFER_USUAL_SIZE> val(cs);
4187 str->append(STRING_WITH_LEN("ROW("));
4188 for (uint i= 0 ; i < item->cols(); i++)
4189 {
4190 if (i > 0)
4191 str->append(',');
4192 Item *elem= item->element_index(i);
4193 String *tmp= elem->type_handler()->print_item_value(thd, elem, &val);
4194 if (tmp)
4195 str->append(*tmp);
4196 else
4197 str->append(STRING_WITH_LEN("NULL"));
4198 }
4199 str->append(STRING_WITH_LEN(")"));
4200 return str;
4201 }
4202
4203
4204 /**
4205 Get a string representation of the Item value,
4206 using the character string format with its charset and collation, e.g.
4207 latin1 'string' COLLATE latin1_german2_ci
4208 */
4209 String *Type_handler::
print_item_value_csstr(THD * thd,Item * item,String * str) const4210 print_item_value_csstr(THD *thd, Item *item, String *str) const
4211 {
4212 String *result= item->val_str(str);
4213
4214 if (!result)
4215 return NULL;
4216
4217 StringBuffer<STRING_BUFFER_USUAL_SIZE> buf(result->charset());
4218 CHARSET_INFO *cs= thd->variables.character_set_client;
4219
4220 buf.append('_');
4221 buf.append(result->charset()->csname);
4222 if (cs->escape_with_backslash_is_dangerous)
4223 buf.append(' ');
4224 append_query_string(cs, &buf, result->ptr(), result->length(),
4225 thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES);
4226 buf.append(" COLLATE '");
4227 buf.append(item->collation.collation->name);
4228 buf.append('\'');
4229 str->copy(buf);
4230
4231 return str;
4232 }
4233
4234
4235 String *Type_handler_numeric::
print_item_value(THD * thd,Item * item,String * str) const4236 print_item_value(THD *thd, Item *item, String *str) const
4237 {
4238 return item->val_str(str);
4239 }
4240
4241
4242 String *Type_handler::
print_item_value_temporal(THD * thd,Item * item,String * str,const Name & type_name,String * buf) const4243 print_item_value_temporal(THD *thd, Item *item, String *str,
4244 const Name &type_name, String *buf) const
4245 {
4246 String *result= item->val_str(buf);
4247 return !result ||
4248 str->realloc(type_name.length() + result->length() + 2) ||
4249 str->copy(type_name.ptr(), type_name.length(), &my_charset_latin1) ||
4250 str->append('\'') ||
4251 str->append(result->ptr(), result->length()) ||
4252 str->append('\'') ?
4253 NULL :
4254 str;
4255 }
4256
4257
4258 String *Type_handler_time_common::
print_item_value(THD * thd,Item * item,String * str) const4259 print_item_value(THD *thd, Item *item, String *str) const
4260 {
4261 StringBuffer<MAX_TIME_FULL_WIDTH+1> buf;
4262 return print_item_value_temporal(thd, item, str,
4263 Name(STRING_WITH_LEN("TIME")), &buf);
4264 }
4265
4266
4267 String *Type_handler_date_common::
print_item_value(THD * thd,Item * item,String * str) const4268 print_item_value(THD *thd, Item *item, String *str) const
4269 {
4270 StringBuffer<MAX_DATE_WIDTH+1> buf;
4271 return print_item_value_temporal(thd, item, str,
4272 Name(STRING_WITH_LEN("DATE")), &buf);
4273 }
4274
4275
4276 String *Type_handler_datetime_common::
print_item_value(THD * thd,Item * item,String * str) const4277 print_item_value(THD *thd, Item *item, String *str) const
4278 {
4279 StringBuffer<MAX_DATETIME_FULL_WIDTH+1> buf;
4280 return print_item_value_temporal(thd, item, str,
4281 Name(STRING_WITH_LEN("TIMESTAMP")), &buf);
4282 }
4283
4284
4285 String *Type_handler_timestamp_common::
print_item_value(THD * thd,Item * item,String * str) const4286 print_item_value(THD *thd, Item *item, String *str) const
4287 {
4288 StringBuffer<MAX_DATETIME_FULL_WIDTH+1> buf;
4289 return print_item_value_temporal(thd, item, str,
4290 Name(STRING_WITH_LEN("TIMESTAMP")), &buf);
4291 }
4292
4293
4294 /***************************************************************************/
4295
4296 bool Type_handler_row::
Item_func_round_fix_length_and_dec(Item_func_round * item) const4297 Item_func_round_fix_length_and_dec(Item_func_round *item) const
4298 {
4299 DBUG_ASSERT(0);
4300 return false;
4301 }
4302
4303
4304 bool Type_handler_int_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const4305 Item_func_round_fix_length_and_dec(Item_func_round *item) const
4306 {
4307 item->fix_arg_int();
4308 return false;
4309 }
4310
4311
4312 bool Type_handler_real_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const4313 Item_func_round_fix_length_and_dec(Item_func_round *item) const
4314 {
4315 item->fix_arg_double();
4316 return false;
4317 }
4318
4319
4320 bool Type_handler_decimal_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const4321 Item_func_round_fix_length_and_dec(Item_func_round *item) const
4322 {
4323 item->fix_arg_decimal();
4324 return false;
4325 }
4326
4327
4328 bool Type_handler_temporal_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const4329 Item_func_round_fix_length_and_dec(Item_func_round *item) const
4330 {
4331 item->fix_arg_double();
4332 return false;
4333 }
4334
4335
4336 bool Type_handler_string_result::
Item_func_round_fix_length_and_dec(Item_func_round * item) const4337 Item_func_round_fix_length_and_dec(Item_func_round *item) const
4338 {
4339 item->fix_arg_double();
4340 return false;
4341 }
4342
4343
4344 #ifdef HAVE_SPATIAL
4345 bool Type_handler_geometry::
Item_func_round_fix_length_and_dec(Item_func_round * item) const4346 Item_func_round_fix_length_and_dec(Item_func_round *item) const
4347 {
4348 return Item_func_or_sum_illegal_param(item);
4349 }
4350 #endif
4351
4352 /***************************************************************************/
4353
4354 bool Type_handler_row::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const4355 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
4356 {
4357 DBUG_ASSERT(0);
4358 return false;
4359 }
4360
4361
4362 bool Type_handler_int_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const4363 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
4364 {
4365 item->fix_length_and_dec_int_or_decimal();
4366 return false;
4367 }
4368
4369
4370 bool Type_handler_real_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const4371 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
4372 {
4373 item->fix_length_and_dec_double();
4374 return false;
4375 }
4376
4377
4378 bool Type_handler_decimal_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const4379 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
4380 {
4381 item->fix_length_and_dec_int_or_decimal();
4382 return false;
4383 }
4384
4385
4386 bool Type_handler_temporal_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const4387 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
4388 {
4389 item->fix_length_and_dec_int_or_decimal();
4390 return false;
4391 }
4392
4393
4394 bool Type_handler_string_result::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const4395 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
4396 {
4397 item->fix_length_and_dec_double();
4398 return false;
4399 }
4400
4401
4402 #ifdef HAVE_SPATIAL
4403 bool Type_handler_geometry::
Item_func_int_val_fix_length_and_dec(Item_func_int_val * item) const4404 Item_func_int_val_fix_length_and_dec(Item_func_int_val *item) const
4405 {
4406 return Item_func_or_sum_illegal_param(item);
4407 }
4408 #endif
4409
4410 /***************************************************************************/
4411
4412 bool Type_handler_row::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const4413 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
4414 {
4415 DBUG_ASSERT(0);
4416 return false;
4417 }
4418
4419
4420 bool Type_handler_int_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const4421 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
4422 {
4423 item->fix_length_and_dec_int();
4424 return false;
4425 }
4426
4427
4428 bool Type_handler_real_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const4429 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
4430 {
4431 item->fix_length_and_dec_double();
4432 return false;
4433 }
4434
4435
4436 bool Type_handler_decimal_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const4437 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
4438 {
4439 item->fix_length_and_dec_decimal();
4440 return false;
4441 }
4442
4443
4444 bool Type_handler_temporal_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const4445 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
4446 {
4447 item->fix_length_and_dec_decimal();
4448 return false;
4449 }
4450
4451
4452 bool Type_handler_string_result::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const4453 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
4454 {
4455 item->fix_length_and_dec_double();
4456 return false;
4457 }
4458
4459
4460 #ifdef HAVE_SPATIAL
4461 bool Type_handler_geometry::
Item_func_abs_fix_length_and_dec(Item_func_abs * item) const4462 Item_func_abs_fix_length_and_dec(Item_func_abs *item) const
4463 {
4464 return Item_func_or_sum_illegal_param(item);
4465 }
4466 #endif
4467
4468 /***************************************************************************/
4469
4470 bool Type_handler_row::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const4471 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
4472 {
4473 DBUG_ASSERT(0);
4474 return false;
4475 }
4476
4477
4478 bool Type_handler_int_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const4479 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
4480 {
4481 item->fix_length_and_dec_int();
4482 return false;
4483 }
4484
4485
4486 bool Type_handler_real_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const4487 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
4488 {
4489 item->fix_length_and_dec_double();
4490 return false;
4491 }
4492
4493
4494 bool Type_handler_decimal_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const4495 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
4496 {
4497 item->fix_length_and_dec_decimal();
4498 return false;
4499 }
4500
4501
4502 bool Type_handler_temporal_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const4503 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
4504 {
4505 item->fix_length_and_dec_decimal();
4506 return false;
4507 }
4508
4509
4510 bool Type_handler_string_result::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const4511 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
4512 {
4513 item->fix_length_and_dec_double();
4514 return false;
4515 }
4516
4517
4518 #ifdef HAVE_SPATIAL
4519 bool Type_handler_geometry::
Item_func_neg_fix_length_and_dec(Item_func_neg * item) const4520 Item_func_neg_fix_length_and_dec(Item_func_neg *item) const
4521 {
4522 return Item_func_or_sum_illegal_param(item);
4523 }
4524 #endif
4525
4526
4527 /***************************************************************************/
4528
4529 bool Type_handler::
Item_func_signed_fix_length_and_dec(Item_func_signed * item) const4530 Item_func_signed_fix_length_and_dec(Item_func_signed *item) const
4531 {
4532 item->fix_length_and_dec_generic();
4533 return false;
4534 }
4535
4536
4537 bool Type_handler::
Item_func_unsigned_fix_length_and_dec(Item_func_unsigned * item) const4538 Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const
4539 {
4540 const Item *arg= item->arguments()[0];
4541 if (!arg->unsigned_flag && arg->val_int_min() < 0)
4542 {
4543 /*
4544 Negative arguments produce long results:
4545 CAST(1-2 AS UNSIGNED) -> 18446744073709551615
4546 */
4547 item->max_length= MAX_BIGINT_WIDTH;
4548 return false;
4549 }
4550 item->fix_length_and_dec_generic();
4551 return false;
4552 }
4553
4554
4555 bool Type_handler_string_result::
Item_func_signed_fix_length_and_dec(Item_func_signed * item) const4556 Item_func_signed_fix_length_and_dec(Item_func_signed *item) const
4557 {
4558 item->fix_length_and_dec_string();
4559 return false;
4560 }
4561
4562
4563 bool Type_handler_string_result::
Item_func_unsigned_fix_length_and_dec(Item_func_unsigned * item) const4564 Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const
4565 {
4566 const Item *arg= item->arguments()[0];
4567 if (!arg->unsigned_flag && // Not HEX hybrid
4568 arg->max_char_length() > 1) // Can be negative
4569 {
4570 // String arguments can give long results: '-1' -> 18446744073709551614
4571 item->max_length= MAX_BIGINT_WIDTH;
4572 return false;
4573 }
4574 item->fix_length_and_dec_string();
4575 return false;
4576 }
4577
4578 bool Type_handler_real_result::
Item_func_signed_fix_length_and_dec(Item_func_signed * item) const4579 Item_func_signed_fix_length_and_dec(Item_func_signed *item) const
4580 {
4581 item->fix_length_and_dec_double();
4582 return false;
4583 }
4584
4585
4586 bool Type_handler_real_result::
Item_func_unsigned_fix_length_and_dec(Item_func_unsigned * item) const4587 Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const
4588 {
4589 item->fix_length_and_dec_double();
4590 return false;
4591 }
4592
4593
4594 bool Type_handler::
Item_double_typecast_fix_length_and_dec(Item_double_typecast * item) const4595 Item_double_typecast_fix_length_and_dec(Item_double_typecast *item) const
4596 {
4597 item->fix_length_and_dec_generic();
4598 return false;
4599 }
4600
4601
4602 bool Type_handler::
Item_float_typecast_fix_length_and_dec(Item_float_typecast * item) const4603 Item_float_typecast_fix_length_and_dec(Item_float_typecast *item) const
4604 {
4605 item->fix_length_and_dec_generic();
4606 return false;
4607 }
4608
4609
4610 bool Type_handler::
Item_decimal_typecast_fix_length_and_dec(Item_decimal_typecast * item) const4611 Item_decimal_typecast_fix_length_and_dec(Item_decimal_typecast *item) const
4612 {
4613 item->fix_length_and_dec_generic();
4614 return false;
4615 }
4616
4617
4618 bool Type_handler::
Item_char_typecast_fix_length_and_dec(Item_char_typecast * item) const4619 Item_char_typecast_fix_length_and_dec(Item_char_typecast *item) const
4620 {
4621 item->fix_length_and_dec_generic();
4622 return false;
4623 }
4624
4625
4626 bool Type_handler_numeric::
Item_char_typecast_fix_length_and_dec(Item_char_typecast * item) const4627 Item_char_typecast_fix_length_and_dec(Item_char_typecast *item) const
4628 {
4629 item->fix_length_and_dec_numeric();
4630 return false;
4631 }
4632
4633
4634 bool Type_handler_string_result::
Item_char_typecast_fix_length_and_dec(Item_char_typecast * item) const4635 Item_char_typecast_fix_length_and_dec(Item_char_typecast *item) const
4636 {
4637 item->fix_length_and_dec_str();
4638 return false;
4639 }
4640
4641
4642 bool Type_handler::
Item_time_typecast_fix_length_and_dec(Item_time_typecast * item) const4643 Item_time_typecast_fix_length_and_dec(Item_time_typecast *item) const
4644 {
4645 uint dec= item->decimals == NOT_FIXED_DEC ?
4646 item->arguments()[0]->time_precision() :
4647 item->decimals;
4648 item->fix_attributes_temporal(MIN_TIME_WIDTH, dec);
4649 item->maybe_null= true;
4650 return false;
4651 }
4652
4653
4654 bool Type_handler::
Item_date_typecast_fix_length_and_dec(Item_date_typecast * item) const4655 Item_date_typecast_fix_length_and_dec(Item_date_typecast *item) const
4656 {
4657 item->fix_attributes_temporal(MAX_DATE_WIDTH, 0);
4658 item->maybe_null= true;
4659 return false;
4660 }
4661
4662
4663 bool Type_handler::
Item_datetime_typecast_fix_length_and_dec(Item_datetime_typecast * item) const4664 Item_datetime_typecast_fix_length_and_dec(Item_datetime_typecast *item)
4665 const
4666 {
4667 uint dec= item->decimals == NOT_FIXED_DEC ?
4668 item->arguments()[0]->datetime_precision() :
4669 item->decimals;
4670 item->fix_attributes_temporal(MAX_DATETIME_WIDTH, dec);
4671 item->maybe_null= true;
4672 return false;
4673 }
4674
4675
4676 #ifdef HAVE_SPATIAL
4677
4678 bool Type_handler_geometry::
Item_func_signed_fix_length_and_dec(Item_func_signed * item) const4679 Item_func_signed_fix_length_and_dec(Item_func_signed *item) const
4680 {
4681 return Item_func_or_sum_illegal_param(item);
4682 }
4683
4684
4685 bool Type_handler_geometry::
Item_func_unsigned_fix_length_and_dec(Item_func_unsigned * item) const4686 Item_func_unsigned_fix_length_and_dec(Item_func_unsigned *item) const
4687 {
4688 return Item_func_or_sum_illegal_param(item);
4689 }
4690
4691
4692 bool Type_handler_geometry::
Item_double_typecast_fix_length_and_dec(Item_double_typecast * item) const4693 Item_double_typecast_fix_length_and_dec(Item_double_typecast *item) const
4694 {
4695 return Item_func_or_sum_illegal_param(item);
4696 }
4697
4698
4699 bool Type_handler_geometry::
Item_float_typecast_fix_length_and_dec(Item_float_typecast * item) const4700 Item_float_typecast_fix_length_and_dec(Item_float_typecast *item) const
4701 {
4702 return Item_func_or_sum_illegal_param(item);
4703 }
4704
4705
4706 bool Type_handler_geometry::
Item_decimal_typecast_fix_length_and_dec(Item_decimal_typecast * item) const4707 Item_decimal_typecast_fix_length_and_dec(Item_decimal_typecast *item) const
4708 {
4709 return Item_func_or_sum_illegal_param(item);
4710 }
4711
4712
4713 bool Type_handler_geometry::
Item_char_typecast_fix_length_and_dec(Item_char_typecast * item) const4714 Item_char_typecast_fix_length_and_dec(Item_char_typecast *item) const
4715 {
4716 if (item->cast_charset() != &my_charset_bin)
4717 return Item_func_or_sum_illegal_param(item); // CAST(geom AS CHAR)
4718 item->fix_length_and_dec_str();
4719 return false; // CAST(geom AS BINARY)
4720 }
4721
4722
4723 bool Type_handler_geometry::
Item_time_typecast_fix_length_and_dec(Item_time_typecast * item) const4724 Item_time_typecast_fix_length_and_dec(Item_time_typecast *item) const
4725 {
4726 return Item_func_or_sum_illegal_param(item);
4727 }
4728
4729
4730
4731 bool Type_handler_geometry::
Item_date_typecast_fix_length_and_dec(Item_date_typecast * item) const4732 Item_date_typecast_fix_length_and_dec(Item_date_typecast *item) const
4733 {
4734 return Item_func_or_sum_illegal_param(item);
4735 }
4736
4737
4738 bool Type_handler_geometry::
Item_datetime_typecast_fix_length_and_dec(Item_datetime_typecast * item) const4739 Item_datetime_typecast_fix_length_and_dec(Item_datetime_typecast *item)
4740 const
4741 {
4742 return Item_func_or_sum_illegal_param(item);
4743
4744 }
4745
4746 #endif /* HAVE_SPATIAL */
4747
4748 /***************************************************************************/
4749
4750 bool Type_handler_row::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const4751 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
4752 {
4753 DBUG_ASSERT(0);
4754 return true;
4755 }
4756
4757
4758 bool Type_handler_int_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const4759 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
4760 {
4761 item->fix_length_and_dec_int();
4762 return false;
4763 }
4764
4765
4766 bool Type_handler_real_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const4767 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
4768 {
4769 item->fix_length_and_dec_double();
4770 return false;
4771 }
4772
4773
4774 bool Type_handler_decimal_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const4775 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
4776 {
4777 item->fix_length_and_dec_decimal();
4778 return false;
4779 }
4780
4781
4782 bool Type_handler_temporal_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const4783 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
4784 {
4785 item->fix_length_and_dec_temporal(true);
4786 return false;
4787 }
4788
4789
4790 bool Type_handler_string_result::
Item_func_plus_fix_length_and_dec(Item_func_plus * item) const4791 Item_func_plus_fix_length_and_dec(Item_func_plus *item) const
4792 {
4793 item->fix_length_and_dec_double();
4794 return false;
4795 }
4796
4797 /***************************************************************************/
4798
4799 bool Type_handler_row::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const4800 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
4801 {
4802 DBUG_ASSERT(0);
4803 return true;
4804 }
4805
4806
4807 bool Type_handler_int_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const4808 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
4809 {
4810 item->fix_length_and_dec_int();
4811 return false;
4812 }
4813
4814
4815 bool Type_handler_real_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const4816 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
4817 {
4818 item->fix_length_and_dec_double();
4819 return false;
4820 }
4821
4822
4823 bool Type_handler_decimal_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const4824 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
4825 {
4826 item->fix_length_and_dec_decimal();
4827 return false;
4828 }
4829
4830
4831 bool Type_handler_temporal_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const4832 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
4833 {
4834 item->fix_length_and_dec_temporal(true);
4835 return false;
4836 }
4837
4838
4839 bool Type_handler_string_result::
Item_func_minus_fix_length_and_dec(Item_func_minus * item) const4840 Item_func_minus_fix_length_and_dec(Item_func_minus *item) const
4841 {
4842 item->fix_length_and_dec_double();
4843 return false;
4844 }
4845
4846 /***************************************************************************/
4847
4848 bool Type_handler_row::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const4849 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
4850 {
4851 DBUG_ASSERT(0);
4852 return true;
4853 }
4854
4855
4856 bool Type_handler_int_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const4857 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
4858 {
4859 item->fix_length_and_dec_int();
4860 return false;
4861 }
4862
4863
4864 bool Type_handler_real_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const4865 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
4866 {
4867 item->fix_length_and_dec_double();
4868 return false;
4869 }
4870
4871
4872 bool Type_handler_decimal_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const4873 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
4874 {
4875 item->fix_length_and_dec_decimal();
4876 return false;
4877 }
4878
4879
4880 bool Type_handler_temporal_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const4881 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
4882 {
4883 item->fix_length_and_dec_temporal(true);
4884 return false;
4885 }
4886
4887
4888 bool Type_handler_string_result::
Item_func_mul_fix_length_and_dec(Item_func_mul * item) const4889 Item_func_mul_fix_length_and_dec(Item_func_mul *item) const
4890 {
4891 item->fix_length_and_dec_double();
4892 return false;
4893 }
4894
4895 /***************************************************************************/
4896
4897 bool Type_handler_row::
Item_func_div_fix_length_and_dec(Item_func_div * item) const4898 Item_func_div_fix_length_and_dec(Item_func_div *item) const
4899 {
4900 DBUG_ASSERT(0);
4901 return true;
4902 }
4903
4904
4905 bool Type_handler_int_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const4906 Item_func_div_fix_length_and_dec(Item_func_div *item) const
4907 {
4908 item->fix_length_and_dec_int();
4909 return false;
4910 }
4911
4912
4913 bool Type_handler_real_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const4914 Item_func_div_fix_length_and_dec(Item_func_div *item) const
4915 {
4916 item->fix_length_and_dec_double();
4917 return false;
4918 }
4919
4920
4921 bool Type_handler_decimal_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const4922 Item_func_div_fix_length_and_dec(Item_func_div *item) const
4923 {
4924 item->fix_length_and_dec_decimal();
4925 return false;
4926 }
4927
4928
4929 bool Type_handler_temporal_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const4930 Item_func_div_fix_length_and_dec(Item_func_div *item) const
4931 {
4932 // Item_func_div::int_op() is not implemented. Disallow DECIMAL->INT downcast.
4933 item->fix_length_and_dec_temporal(false);
4934 return false;
4935 }
4936
4937
4938 bool Type_handler_string_result::
Item_func_div_fix_length_and_dec(Item_func_div * item) const4939 Item_func_div_fix_length_and_dec(Item_func_div *item) const
4940 {
4941 item->fix_length_and_dec_double();
4942 return false;
4943 }
4944
4945 /***************************************************************************/
4946
4947 bool Type_handler_row::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const4948 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
4949 {
4950 DBUG_ASSERT(0);
4951 return true;
4952 }
4953
4954
4955 bool Type_handler_int_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const4956 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
4957 {
4958 item->fix_length_and_dec_int();
4959 return false;
4960 }
4961
4962
4963 bool Type_handler_real_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const4964 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
4965 {
4966 item->fix_length_and_dec_double();
4967 return false;
4968 }
4969
4970
4971 bool Type_handler_decimal_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const4972 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
4973 {
4974 item->fix_length_and_dec_decimal();
4975 return false;
4976 }
4977
4978
4979 bool Type_handler_temporal_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const4980 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
4981 {
4982 item->fix_length_and_dec_temporal(true);
4983 return false;
4984 }
4985
4986
4987 bool Type_handler_string_result::
Item_func_mod_fix_length_and_dec(Item_func_mod * item) const4988 Item_func_mod_fix_length_and_dec(Item_func_mod *item) const
4989 {
4990 item->fix_length_and_dec_double();
4991 return false;
4992 }
4993
4994 /***************************************************************************/
4995
Item_time_precision(Item * item) const4996 uint Type_handler::Item_time_precision(Item *item) const
4997 {
4998 return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
4999 }
5000
5001
Item_datetime_precision(Item * item) const5002 uint Type_handler::Item_datetime_precision(Item *item) const
5003 {
5004 return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
5005 }
5006
5007
Item_temporal_precision(Item * item,bool is_time) const5008 uint Type_handler_string_result::Item_temporal_precision(Item *item,
5009 bool is_time) const
5010 {
5011 MYSQL_TIME ltime;
5012 StringBuffer<64> buf;
5013 String *tmp;
5014 MYSQL_TIME_STATUS status;
5015 DBUG_ASSERT(item->fixed);
5016 if ((tmp= item->val_str(&buf)) &&
5017 !(is_time ?
5018 str_to_time(tmp->charset(), tmp->ptr(), tmp->length(),
5019 <ime, TIME_TIME_ONLY, &status) :
5020 str_to_datetime(tmp->charset(), tmp->ptr(), tmp->length(),
5021 <ime, TIME_FUZZY_DATES, &status)))
5022 return MY_MIN(status.precision, TIME_SECOND_PART_DIGITS);
5023 return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
5024 }
5025
5026 /***************************************************************************/
5027
Item_decimal_scale(const Item * item) const5028 uint Type_handler::Item_decimal_scale(const Item *item) const
5029 {
5030 return item->decimals < NOT_FIXED_DEC ?
5031 item->decimals :
5032 MY_MIN(item->max_length, DECIMAL_MAX_SCALE);
5033 }
5034
5035 uint Type_handler_temporal_result::
Item_decimal_scale_with_seconds(const Item * item) const5036 Item_decimal_scale_with_seconds(const Item *item) const
5037 {
5038 return item->decimals < NOT_FIXED_DEC ?
5039 item->decimals :
5040 TIME_SECOND_PART_DIGITS;
5041 }
5042
Item_divisor_precision_increment(const Item * item) const5043 uint Type_handler::Item_divisor_precision_increment(const Item *item) const
5044 {
5045 return item->decimals;
5046 }
5047
5048 uint Type_handler_temporal_result::
Item_divisor_precision_increment_with_seconds(const Item * item) const5049 Item_divisor_precision_increment_with_seconds(const Item *item) const
5050 {
5051 return item->decimals < NOT_FIXED_DEC ?
5052 item->decimals :
5053 TIME_SECOND_PART_DIGITS;
5054 }
5055
5056 /***************************************************************************/
5057
Item_decimal_precision(const Item * item) const5058 uint Type_handler_string_result::Item_decimal_precision(const Item *item) const
5059 {
5060 uint res= item->max_char_length();
5061 /*
5062 Return at least one decimal digit, even if Item::max_char_length()
5063 returned 0. This is important to avoid attempts to create fields of types
5064 INT(0) or DECIMAL(0,0) when converting NULL or empty strings to INT/DECIMAL:
5065 CREATE TABLE t1 AS SELECT CONVERT(NULL,SIGNED) AS a;
5066 */
5067 return res ? MY_MIN(res, DECIMAL_MAX_PRECISION) : 1;
5068 }
5069
Item_decimal_precision(const Item * item) const5070 uint Type_handler_real_result::Item_decimal_precision(const Item *item) const
5071 {
5072 uint res= item->max_char_length();
5073 return res ? MY_MIN(res, DECIMAL_MAX_PRECISION) : 1;
5074 }
5075
Item_decimal_precision(const Item * item) const5076 uint Type_handler_decimal_result::Item_decimal_precision(const Item *item) const
5077 {
5078 uint prec= my_decimal_length_to_precision(item->max_char_length(),
5079 item->decimals,
5080 item->unsigned_flag);
5081 return MY_MIN(prec, DECIMAL_MAX_PRECISION);
5082 }
5083
Item_decimal_precision(const Item * item) const5084 uint Type_handler_int_result::Item_decimal_precision(const Item *item) const
5085 {
5086 uint prec= my_decimal_length_to_precision(item->max_char_length(),
5087 item->decimals,
5088 item->unsigned_flag);
5089 return MY_MIN(prec, DECIMAL_MAX_PRECISION);
5090 }
5091
Item_decimal_precision(const Item * item) const5092 uint Type_handler_time_common::Item_decimal_precision(const Item *item) const
5093 {
5094 return 7 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
5095 }
5096
Item_decimal_precision(const Item * item) const5097 uint Type_handler_date_common::Item_decimal_precision(const Item *item) const
5098 {
5099 return 8;
5100 }
5101
Item_decimal_precision(const Item * item) const5102 uint Type_handler_datetime_common::Item_decimal_precision(const Item *item) const
5103 {
5104 return 14 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
5105 }
5106
Item_decimal_precision(const Item * item) const5107 uint Type_handler_timestamp_common::Item_decimal_precision(const Item *item) const
5108 {
5109 return 14 + MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
5110 }
5111
5112 /***************************************************************************/
5113
5114 bool Type_handler_real_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const5115 subquery_type_allows_materialization(const Item *inner,
5116 const Item *outer,
5117 bool is_in_predicate) const
5118 {
5119 DBUG_ASSERT(inner->cmp_type() == REAL_RESULT);
5120 return outer->cmp_type() == REAL_RESULT;
5121 }
5122
5123
5124 bool Type_handler_int_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const5125 subquery_type_allows_materialization(const Item *inner,
5126 const Item *outer,
5127 bool is_in_predicate) const
5128 {
5129 DBUG_ASSERT(inner->cmp_type() == INT_RESULT);
5130 return outer->cmp_type() == INT_RESULT;
5131 }
5132
5133
5134 bool Type_handler_decimal_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const5135 subquery_type_allows_materialization(const Item *inner,
5136 const Item *outer,
5137 bool is_in_predicate) const
5138 {
5139 DBUG_ASSERT(inner->cmp_type() == DECIMAL_RESULT);
5140 return outer->cmp_type() == DECIMAL_RESULT;
5141 }
5142
5143
5144 bool Type_handler_string_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const5145 subquery_type_allows_materialization(const Item *inner,
5146 const Item *outer,
5147 bool is_in_predicate) const
5148 {
5149 DBUG_ASSERT(inner->cmp_type() == STRING_RESULT);
5150 if (outer->cmp_type() == STRING_RESULT &&
5151 /*
5152 Materialization also is unable to work when create_tmp_table() will
5153 create a blob column because item->max_length is too big.
5154 The following test is copied from varstring_type_handler().
5155 */
5156 !inner->too_big_for_varchar())
5157 {
5158 if (outer->collation.collation == inner->collation.collation)
5159 return true;
5160 if (is_in_predicate)
5161 {
5162 Charset inner_col(inner->collation.collation);
5163 if (inner_col.encoding_allows_reinterpret_as(outer->
5164 collation.collation) &&
5165 inner_col.eq_collation_specific_names(outer->collation.collation))
5166 return true;
5167 }
5168 }
5169 return false;
5170 }
5171
5172
5173 bool Type_handler_temporal_result::
subquery_type_allows_materialization(const Item * inner,const Item * outer,bool is_in_predicate) const5174 subquery_type_allows_materialization(const Item *inner,
5175 const Item *outer,
5176 bool is_in_predicate) const
5177 {
5178 DBUG_ASSERT(inner->cmp_type() == TIME_RESULT);
5179 return mysql_timestamp_type() ==
5180 outer->type_handler()->mysql_timestamp_type();
5181 }
5182
5183 /***************************************************************************/
5184
5185
5186 const Type_handler *
type_handler_for_tmp_table(const Item * item) const5187 Type_handler_null::type_handler_for_tmp_table(const Item *item) const
5188 {
5189 return &type_handler_string;
5190 }
5191
5192
5193 const Type_handler *
type_handler_for_union(const Item * item) const5194 Type_handler_null::type_handler_for_union(const Item *item) const
5195 {
5196 return &type_handler_string;
5197 }
5198
5199
5200 const Type_handler *
type_handler_for_tmp_table(const Item * item) const5201 Type_handler_olddecimal::type_handler_for_tmp_table(const Item *item) const
5202 {
5203 return &type_handler_newdecimal;
5204 }
5205
5206 const Type_handler *
type_handler_for_union(const Item * item) const5207 Type_handler_olddecimal::type_handler_for_union(const Item *item) const
5208 {
5209 return &type_handler_newdecimal;
5210 }
5211
5212
5213 /***************************************************************************/
5214
check_null(const Item * item,st_value * value) const5215 bool Type_handler::check_null(const Item *item, st_value *value) const
5216 {
5217 if (item->null_value)
5218 {
5219 value->m_type= DYN_COL_NULL;
5220 return true;
5221 }
5222 return false;
5223 }
5224
5225
5226 bool Type_handler_null::
Item_save_in_value(Item * item,st_value * value) const5227 Item_save_in_value(Item *item, st_value *value) const
5228 {
5229 value->m_type= DYN_COL_NULL;
5230 return true;
5231 }
5232
5233
5234 bool Type_handler_row::
Item_save_in_value(Item * item,st_value * value) const5235 Item_save_in_value(Item *item, st_value *value) const
5236 {
5237 DBUG_ASSERT(0);
5238 value->m_type= DYN_COL_NULL;
5239 return true;
5240 }
5241
5242
5243 bool Type_handler_int_result::
Item_save_in_value(Item * item,st_value * value) const5244 Item_save_in_value(Item *item, st_value *value) const
5245 {
5246 value->m_type= item->unsigned_flag ? DYN_COL_UINT : DYN_COL_INT;
5247 value->value.m_longlong= item->val_int();
5248 return check_null(item, value);
5249 }
5250
5251
5252 bool Type_handler_real_result::
Item_save_in_value(Item * item,st_value * value) const5253 Item_save_in_value(Item *item, st_value *value) const
5254 {
5255 value->m_type= DYN_COL_DOUBLE;
5256 value->value.m_double= item->val_real();
5257 return check_null(item, value);
5258 }
5259
5260
5261 bool Type_handler_decimal_result::
Item_save_in_value(Item * item,st_value * value) const5262 Item_save_in_value(Item *item, st_value *value) const
5263 {
5264 value->m_type= DYN_COL_DECIMAL;
5265 my_decimal *dec= item->val_decimal(&value->m_decimal);
5266 if (dec != &value->m_decimal && !item->null_value)
5267 my_decimal2decimal(dec, &value->m_decimal);
5268 return check_null(item, value);
5269 }
5270
5271
5272 bool Type_handler_string_result::
Item_save_in_value(Item * item,st_value * value) const5273 Item_save_in_value(Item *item, st_value *value) const
5274 {
5275 value->m_type= DYN_COL_STRING;
5276 String *str= item->val_str(&value->m_string);
5277 if (str != &value->m_string && !item->null_value)
5278 value->m_string.set(str->ptr(), str->length(), str->charset());
5279 return check_null(item, value);
5280 }
5281
5282
5283 bool Type_handler_temporal_with_date::
Item_save_in_value(Item * item,st_value * value) const5284 Item_save_in_value(Item *item, st_value *value) const
5285 {
5286 value->m_type= DYN_COL_DATETIME;
5287 item->get_date(&value->value.m_time, sql_mode_for_dates(current_thd));
5288 return check_null(item, value);
5289 }
5290
5291
5292 bool Type_handler_time_common::
Item_save_in_value(Item * item,st_value * value) const5293 Item_save_in_value(Item *item, st_value *value) const
5294 {
5295 value->m_type= DYN_COL_DATETIME;
5296 item->get_time(&value->value.m_time);
5297 return check_null(item, value);
5298 }
5299
5300 /***************************************************************************/
5301
5302 bool Type_handler_row::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const5303 Item_param_set_from_value(THD *thd,
5304 Item_param *param,
5305 const Type_all_attributes *attr,
5306 const st_value *val) const
5307 {
5308 DBUG_ASSERT(0);
5309 param->set_null();
5310 return true;
5311 }
5312
5313
5314 bool Type_handler_real_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const5315 Item_param_set_from_value(THD *thd,
5316 Item_param *param,
5317 const Type_all_attributes *attr,
5318 const st_value *val) const
5319 {
5320 param->unsigned_flag= attr->unsigned_flag;
5321 param->set_double(val->value.m_double);
5322 return false;
5323 }
5324
5325
5326 bool Type_handler_int_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const5327 Item_param_set_from_value(THD *thd,
5328 Item_param *param,
5329 const Type_all_attributes *attr,
5330 const st_value *val) const
5331 {
5332 param->unsigned_flag= attr->unsigned_flag;
5333 param->set_int(val->value.m_longlong, attr->max_length);
5334 return false;
5335 }
5336
5337
5338 bool Type_handler_decimal_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const5339 Item_param_set_from_value(THD *thd,
5340 Item_param *param,
5341 const Type_all_attributes *attr,
5342 const st_value *val) const
5343 {
5344 param->unsigned_flag= attr->unsigned_flag;
5345 param->set_decimal(&val->m_decimal, attr->unsigned_flag);
5346 return false;
5347 }
5348
5349
5350 bool Type_handler_string_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const5351 Item_param_set_from_value(THD *thd,
5352 Item_param *param,
5353 const Type_all_attributes *attr,
5354 const st_value *val) const
5355 {
5356 param->unsigned_flag= false;
5357 param->setup_conversion_string(thd, attr->collation.collation);
5358 /*
5359 Exact value of max_length is not known unless data is converted to
5360 charset of connection, so we have to set it later.
5361 */
5362 return param->set_str(val->m_string.ptr(), val->m_string.length(),
5363 attr->collation.collation,
5364 attr->collation.collation);
5365 }
5366
5367
5368 bool Type_handler_temporal_result::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const5369 Item_param_set_from_value(THD *thd,
5370 Item_param *param,
5371 const Type_all_attributes *attr,
5372 const st_value *val) const
5373 {
5374 param->unsigned_flag= attr->unsigned_flag;
5375 param->set_time(&val->value.m_time, attr->max_length, attr->decimals);
5376 return false;
5377 }
5378
5379
5380 #ifdef HAVE_SPATIAL
5381 bool Type_handler_geometry::
Item_param_set_from_value(THD * thd,Item_param * param,const Type_all_attributes * attr,const st_value * val) const5382 Item_param_set_from_value(THD *thd,
5383 Item_param *param,
5384 const Type_all_attributes *attr,
5385 const st_value *val) const
5386 {
5387 param->unsigned_flag= false;
5388 param->setup_conversion_blob(thd);
5389 param->set_geometry_type(attr->uint_geometry_type());
5390 return param->set_str(val->m_string.ptr(), val->m_string.length(),
5391 &my_charset_bin, &my_charset_bin);
5392 }
5393 #endif
5394
5395 /***************************************************************************/
5396
5397 bool Type_handler_null::
Item_send(Item * item,Protocol * protocol,st_value * buf) const5398 Item_send(Item *item, Protocol *protocol, st_value *buf) const
5399 {
5400 return protocol->store_null();
5401 }
5402
5403
5404 bool Type_handler::
Item_send_str(Item * item,Protocol * protocol,st_value * buf) const5405 Item_send_str(Item *item, Protocol *protocol, st_value *buf) const
5406 {
5407 String *res;
5408 if ((res= item->val_str(&buf->m_string)))
5409 {
5410 DBUG_ASSERT(!item->null_value);
5411 return protocol->store(res->ptr(), res->length(), res->charset());
5412 }
5413 DBUG_ASSERT(item->null_value);
5414 return protocol->store_null();
5415 }
5416
5417
5418 bool Type_handler::
Item_send_tiny(Item * item,Protocol * protocol,st_value * buf) const5419 Item_send_tiny(Item *item, Protocol *protocol, st_value *buf) const
5420 {
5421 longlong nr= item->val_int();
5422 if (!item->null_value)
5423 return protocol->store_tiny(nr);
5424 return protocol->store_null();
5425 }
5426
5427
5428 bool Type_handler::
Item_send_short(Item * item,Protocol * protocol,st_value * buf) const5429 Item_send_short(Item *item, Protocol *protocol, st_value *buf) const
5430 {
5431 longlong nr= item->val_int();
5432 if (!item->null_value)
5433 return protocol->store_short(nr);
5434 return protocol->store_null();
5435 }
5436
5437
5438 bool Type_handler::
Item_send_long(Item * item,Protocol * protocol,st_value * buf) const5439 Item_send_long(Item *item, Protocol *protocol, st_value *buf) const
5440 {
5441 longlong nr= item->val_int();
5442 if (!item->null_value)
5443 return protocol->store_long(nr);
5444 return protocol->store_null();
5445 }
5446
5447 bool Type_handler::
Item_send_longlong(Item * item,Protocol * protocol,st_value * buf) const5448 Item_send_longlong(Item *item, Protocol *protocol, st_value *buf) const
5449 {
5450 longlong nr= item->val_int();
5451 if (!item->null_value)
5452 return protocol->store_longlong(nr, item->unsigned_flag);
5453 return protocol->store_null();
5454 }
5455
5456
5457 bool Type_handler::
Item_send_float(Item * item,Protocol * protocol,st_value * buf) const5458 Item_send_float(Item *item, Protocol *protocol, st_value *buf) const
5459 {
5460 float nr= (float) item->val_real();
5461 if (!item->null_value)
5462 return protocol->store(nr, item->decimals, &buf->m_string);
5463 return protocol->store_null();
5464 }
5465
5466
5467 bool Type_handler::
Item_send_double(Item * item,Protocol * protocol,st_value * buf) const5468 Item_send_double(Item *item, Protocol *protocol, st_value *buf) const
5469 {
5470 double nr= item->val_real();
5471 if (!item->null_value)
5472 return protocol->store(nr, item->decimals, &buf->m_string);
5473 return protocol->store_null();
5474 }
5475
5476
5477 bool Type_handler::
Item_send_datetime(Item * item,Protocol * protocol,st_value * buf) const5478 Item_send_datetime(Item *item, Protocol *protocol, st_value *buf) const
5479 {
5480 item->get_date(&buf->value.m_time, sql_mode_for_dates(current_thd));
5481 if (!item->null_value)
5482 return protocol->store(&buf->value.m_time, item->decimals);
5483 return protocol->store_null();
5484 }
5485
5486
5487 bool Type_handler::
Item_send_date(Item * item,Protocol * protocol,st_value * buf) const5488 Item_send_date(Item *item, Protocol *protocol, st_value *buf) const
5489 {
5490 item->get_date(&buf->value.m_time, sql_mode_for_dates(current_thd));
5491 if (!item->null_value)
5492 return protocol->store_date(&buf->value.m_time);
5493 return protocol->store_null();
5494 }
5495
5496
5497 bool Type_handler::
Item_send_time(Item * item,Protocol * protocol,st_value * buf) const5498 Item_send_time(Item *item, Protocol *protocol, st_value *buf) const
5499 {
5500 item->get_time(&buf->value.m_time);
5501 if (!item->null_value)
5502 return protocol->store_time(&buf->value.m_time, item->decimals);
5503 return protocol->store_null();
5504 }
5505
5506 /***************************************************************************/
5507
5508 Item *Type_handler_int_result::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const5509 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
5510 {
5511 longlong result= item->val_int();
5512 if (item->null_value)
5513 return new (thd->mem_root) Item_null(thd, item->name.str);
5514 return new (thd->mem_root) Item_int(thd, item->name.str, result,
5515 item->max_length);
5516 }
5517
5518
5519 Item *Type_handler_real_result::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const5520 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
5521 {
5522 double result= item->val_real();
5523 if (item->null_value)
5524 return new (thd->mem_root) Item_null(thd, item->name.str);
5525 return new (thd->mem_root) Item_float(thd, item->name.str, result,
5526 item->decimals, item->max_length);
5527 }
5528
5529
5530 Item *Type_handler_decimal_result::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const5531 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
5532 {
5533 my_decimal decimal_value;
5534 my_decimal *result= item->val_decimal(&decimal_value);
5535 if (item->null_value)
5536 return new (thd->mem_root) Item_null(thd, item->name.str);
5537 return new (thd->mem_root) Item_decimal(thd, item->name.str, result,
5538 item->max_length, item->decimals);
5539 }
5540
5541
5542 Item *Type_handler_string_result::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const5543 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
5544 {
5545 StringBuffer<MAX_FIELD_WIDTH> tmp;
5546 String *result= item->val_str(&tmp);
5547 if (item->null_value)
5548 return new (thd->mem_root) Item_null(thd, item->name.str);
5549 uint length= result->length();
5550 char *tmp_str= thd->strmake(result->ptr(), length);
5551 return new (thd->mem_root) Item_string(thd, item->name.str,
5552 tmp_str, length, result->charset());
5553 }
5554
5555
5556 Item *Type_handler_time_common::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const5557 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
5558 {
5559 Item_cache_temporal *cache;
5560 longlong value= item->val_time_packed();
5561 if (item->null_value)
5562 return new (thd->mem_root) Item_null(thd, item->name.str);
5563 cache= new (thd->mem_root) Item_cache_time(thd);
5564 if (cache)
5565 cache->store_packed(value, item);
5566 return cache;
5567 }
5568
5569
5570 Item *Type_handler_temporal_with_date::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const5571 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
5572 {
5573 Item_cache_temporal *cache;
5574 longlong value= item->val_datetime_packed();
5575 if (item->null_value)
5576 return new (thd->mem_root) Item_null(thd, item->name.str);
5577 cache= new (thd->mem_root) Item_cache_datetime(thd);
5578 if (cache)
5579 cache->store_packed(value, item);
5580 return cache;
5581 }
5582
5583
5584 Item *Type_handler_row::
make_const_item_for_comparison(THD * thd,Item * item,const Item * cmp) const5585 make_const_item_for_comparison(THD *thd, Item *item, const Item *cmp) const
5586 {
5587 if (item->type() == Item::ROW_ITEM && cmp->type() == Item::ROW_ITEM)
5588 {
5589 /*
5590 Substitute constants only in Item_row's. Don't affect other Items
5591 with ROW_RESULT (eg Item_singlerow_subselect).
5592
5593 For such Items more optimal is to detect if it is constant and replace
5594 it with Item_row. This would optimize queries like this:
5595 SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
5596 */
5597 Item_row *item_row= (Item_row*) item;
5598 Item_row *comp_item_row= (Item_row*) cmp;
5599 uint col;
5600 /*
5601 If item and comp_item are both Item_row's and have same number of cols
5602 then process items in Item_row one by one.
5603 We can't ignore NULL values here as this item may be used with <=>, in
5604 which case NULL's are significant.
5605 */
5606 DBUG_ASSERT(item->result_type() == cmp->result_type());
5607 DBUG_ASSERT(item_row->cols() == comp_item_row->cols());
5608 col= item_row->cols();
5609 while (col-- > 0)
5610 resolve_const_item(thd, item_row->addr(col),
5611 comp_item_row->element_index(col));
5612 }
5613 return NULL;
5614 }
5615
5616 /***************************************************************************/
5617
item_name(Item * a,String * str)5618 static const char* item_name(Item *a, String *str)
5619 {
5620 if (a->name.str)
5621 return a->name.str;
5622 str->length(0);
5623 a->print(str, QT_ORDINARY);
5624 return str->c_ptr_safe();
5625 }
5626
5627
wrong_precision_error(uint errcode,Item * a,ulonglong number,uint maximum)5628 static void wrong_precision_error(uint errcode, Item *a,
5629 ulonglong number, uint maximum)
5630 {
5631 StringBuffer<1024> buf(system_charset_info);
5632 my_error(errcode, MYF(0), number, item_name(a, &buf), maximum);
5633 }
5634
5635
5636 /**
5637 Get precision and scale for a declaration
5638
5639 return
5640 0 ok
5641 1 error
5642 */
5643
get_length_and_scale(ulonglong length,ulonglong decimals,uint * out_length,uint * out_decimals,uint max_precision,uint max_scale,Item * a)5644 bool get_length_and_scale(ulonglong length, ulonglong decimals,
5645 uint *out_length, uint *out_decimals,
5646 uint max_precision, uint max_scale,
5647 Item *a)
5648 {
5649 if (length > (ulonglong) max_precision)
5650 {
5651 wrong_precision_error(ER_TOO_BIG_PRECISION, a, length, max_precision);
5652 return 1;
5653 }
5654 if (decimals > (ulonglong) max_scale)
5655 {
5656 wrong_precision_error(ER_TOO_BIG_SCALE, a, decimals, max_scale);
5657 return 1;
5658 }
5659
5660 *out_decimals= (uint) decimals;
5661 my_decimal_trim(&length, out_decimals);
5662 *out_length= (uint) length;
5663
5664 if (*out_length < *out_decimals)
5665 {
5666 my_error(ER_M_BIGGER_THAN_D, MYF(0), "");
5667 return 1;
5668 }
5669 return 0;
5670 }
5671
5672
5673 Item *Type_handler_longlong::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const5674 create_typecast_item(THD *thd, Item *item,
5675 const Type_cast_attributes &attr) const
5676 {
5677 if (this != &type_handler_ulonglong)
5678 return new (thd->mem_root) Item_func_signed(thd, item);
5679 return new (thd->mem_root) Item_func_unsigned(thd, item);
5680
5681 }
5682
5683
5684 Item *Type_handler_date_common::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const5685 create_typecast_item(THD *thd, Item *item,
5686 const Type_cast_attributes &attr) const
5687 {
5688 return new (thd->mem_root) Item_date_typecast(thd, item);
5689 }
5690
5691
5692
5693 Item *Type_handler_time_common::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const5694 create_typecast_item(THD *thd, Item *item,
5695 const Type_cast_attributes &attr) const
5696 {
5697 if (attr.decimals() > MAX_DATETIME_PRECISION)
5698 {
5699 wrong_precision_error(ER_TOO_BIG_PRECISION, item, attr.decimals(),
5700 MAX_DATETIME_PRECISION);
5701 return 0;
5702 }
5703 return new (thd->mem_root)
5704 Item_time_typecast(thd, item, (uint) attr.decimals());
5705 }
5706
5707
5708 Item *Type_handler_datetime_common::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const5709 create_typecast_item(THD *thd, Item *item,
5710 const Type_cast_attributes &attr) const
5711 {
5712 if (attr.decimals() > MAX_DATETIME_PRECISION)
5713 {
5714 wrong_precision_error(ER_TOO_BIG_PRECISION, item, attr.decimals(),
5715 MAX_DATETIME_PRECISION);
5716 return 0;
5717 }
5718 return new (thd->mem_root)
5719 Item_datetime_typecast(thd, item, (uint) attr.decimals());
5720
5721 }
5722
5723
5724 Item *Type_handler_decimal_result::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const5725 create_typecast_item(THD *thd, Item *item,
5726 const Type_cast_attributes &attr) const
5727 {
5728 uint len, dec;
5729 if (get_length_and_scale(attr.length(), attr.decimals(), &len, &dec,
5730 DECIMAL_MAX_PRECISION, DECIMAL_MAX_SCALE, item))
5731 return NULL;
5732 return new (thd->mem_root) Item_decimal_typecast(thd, item, len, dec);
5733 }
5734
5735
5736 Item *Type_handler_double::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const5737 create_typecast_item(THD *thd, Item *item,
5738 const Type_cast_attributes &attr) const
5739 {
5740 uint len, dec;
5741 if (!attr.length_specified())
5742 return new (thd->mem_root) Item_double_typecast(thd, item,
5743 DBL_DIG + 7,
5744 NOT_FIXED_DEC);
5745
5746 if (get_length_and_scale(attr.length(), attr.decimals(), &len, &dec,
5747 DECIMAL_MAX_PRECISION, NOT_FIXED_DEC - 1, item))
5748 return NULL;
5749 return new (thd->mem_root) Item_double_typecast(thd, item, len, dec);
5750 }
5751
5752
5753 Item *Type_handler_float::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const5754 create_typecast_item(THD *thd, Item *item,
5755 const Type_cast_attributes &attr) const
5756 {
5757 DBUG_ASSERT(!attr.length_specified());
5758 return new (thd->mem_root) Item_float_typecast(thd, item);
5759 }
5760
5761
5762 Item *Type_handler_long_blob::
create_typecast_item(THD * thd,Item * item,const Type_cast_attributes & attr) const5763 create_typecast_item(THD *thd, Item *item,
5764 const Type_cast_attributes &attr) const
5765 {
5766 int len= -1;
5767 CHARSET_INFO *real_cs= attr.charset() ?
5768 attr.charset() :
5769 thd->variables.collation_connection;
5770 if (attr.length_specified())
5771 {
5772 if (attr.length() > MAX_FIELD_BLOBLENGTH)
5773 {
5774 char buff[1024];
5775 String buf(buff, sizeof(buff), system_charset_info);
5776 my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), item_name(item, &buf),
5777 MAX_FIELD_BLOBLENGTH);
5778 return NULL;
5779 }
5780 len= (int) attr.length();
5781 }
5782 return new (thd->mem_root) Item_char_typecast(thd, item, len, real_cs);
5783 }
5784
5785 /***************************************************************************/
5786
Item_param_setup_conversion(THD * thd,Item_param * param) const5787 void Type_handler_string_result::Item_param_setup_conversion(THD *thd,
5788 Item_param *param)
5789 const
5790 {
5791 param->setup_conversion_string(thd, thd->variables.character_set_client);
5792 }
5793
5794
Item_param_setup_conversion(THD * thd,Item_param * param) const5795 void Type_handler_blob_common::Item_param_setup_conversion(THD *thd,
5796 Item_param *param)
5797 const
5798 {
5799 param->setup_conversion_blob(thd);
5800 }
5801
5802
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5803 void Type_handler_tiny::Item_param_set_param_func(Item_param *param,
5804 uchar **pos, ulong len) const
5805 {
5806 param->set_param_tiny(pos, len);
5807 }
5808
5809
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5810 void Type_handler_short::Item_param_set_param_func(Item_param *param,
5811 uchar **pos, ulong len) const
5812 {
5813 param->set_param_short(pos, len);
5814 }
5815
5816
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5817 void Type_handler_long::Item_param_set_param_func(Item_param *param,
5818 uchar **pos, ulong len) const
5819 {
5820 param->set_param_int32(pos, len);
5821 }
5822
5823
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5824 void Type_handler_longlong::Item_param_set_param_func(Item_param *param,
5825 uchar **pos,
5826 ulong len) const
5827 {
5828 param->set_param_int64(pos, len);
5829 }
5830
5831
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5832 void Type_handler_float::Item_param_set_param_func(Item_param *param,
5833 uchar **pos,
5834 ulong len) const
5835 {
5836 param->set_param_float(pos, len);
5837 }
5838
5839
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5840 void Type_handler_double::Item_param_set_param_func(Item_param *param,
5841 uchar **pos,
5842 ulong len) const
5843 {
5844 param->set_param_double(pos, len);
5845 }
5846
5847
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5848 void Type_handler_decimal_result::Item_param_set_param_func(Item_param *param,
5849 uchar **pos,
5850 ulong len) const
5851 {
5852 param->set_param_decimal(pos, len);
5853 }
5854
5855
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5856 void Type_handler_string_result::Item_param_set_param_func(Item_param *param,
5857 uchar **pos,
5858 ulong len) const
5859 {
5860 param->set_param_str(pos, len);
5861 }
5862
5863
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5864 void Type_handler_time_common::Item_param_set_param_func(Item_param *param,
5865 uchar **pos,
5866 ulong len) const
5867 {
5868 param->set_param_time(pos, len);
5869 }
5870
5871
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5872 void Type_handler_date_common::Item_param_set_param_func(Item_param *param,
5873 uchar **pos,
5874 ulong len) const
5875 {
5876 param->set_param_date(pos, len);
5877 }
5878
5879
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5880 void Type_handler_datetime_common::Item_param_set_param_func(Item_param *param,
5881 uchar **pos,
5882 ulong len) const
5883 {
5884 param->set_param_datetime(pos, len);
5885 }
5886
make_conversion_table_field(TABLE * table,uint metadata,const Field * target) const5887 Field *Type_handler_blob_common::make_conversion_table_field(TABLE *table,
5888 uint metadata,
5889 const Field *target)
5890 const
5891 {
5892 uint pack_length= metadata & 0x00ff;
5893 if (pack_length < 1 || pack_length > 4)
5894 return NULL; // Broken binary log?
5895 return new(table->in_use->mem_root)
5896 Field_blob(NULL, (uchar *) "", 1, Field::NONE, &empty_clex_str,
5897 table->s, pack_length, target->charset());
5898 }
5899
5900
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5901 void Type_handler_timestamp_common::Item_param_set_param_func(Item_param *param,
5902 uchar **pos,
5903 ulong len) const
5904 {
5905 param->set_param_datetime(pos, len);
5906 }
5907
5908
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5909 void Type_handler::Item_param_set_param_func(Item_param *param,
5910 uchar **pos,
5911 ulong len) const
5912 {
5913 param->set_null(); // Not possible type code in the client-server protocol
5914 }
5915
5916
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5917 void Type_handler_typelib::Item_param_set_param_func(Item_param *param,
5918 uchar **pos,
5919 ulong len) const
5920 {
5921 param->set_null(); // Not possible type code in the client-server protocol
5922 }
5923
5924
5925 #ifdef HAVE_SPATIAL
Item_param_set_param_func(Item_param * param,uchar ** pos,ulong len) const5926 void Type_handler_geometry::Item_param_set_param_func(Item_param *param,
5927 uchar **pos,
5928 ulong len) const
5929 {
5930 param->set_null(); // Not possible type code in the client-server protocol
5931 }
5932 #endif
5933
5934 /***************************************************************************/
5935
union_element_finalize(Item_type_holder * item) const5936 bool Type_handler_string_result::union_element_finalize(Item_type_holder *item) const
5937 {
5938 if (item->collation.derivation == DERIVATION_NONE)
5939 {
5940 my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION");
5941 return true;
5942 }
5943 return false;
5944 }
5945
union_element_finalize(Item_type_holder * item) const5946 bool Type_handler_null::union_element_finalize(Item_type_holder *item) const
5947 {
5948 item->set_handler(&type_handler_string);
5949 return false;
5950 }
5951
5952 /***************************************************************************/
5953
Vers_history_point_resolve_unit(THD * thd,Vers_history_point * point) const5954 bool Type_handler::Vers_history_point_resolve_unit(THD *thd,
5955 Vers_history_point *point)
5956 const
5957 {
5958 /*
5959 Disallow using non-relevant data types in history points.
5960 Even expressions with explicit TRANSACTION or TIMESTAMP units.
5961 */
5962 point->bad_expression_data_type_error(name().ptr());
5963 return true;
5964 }
5965
5966
5967 bool Type_handler_typelib::
Vers_history_point_resolve_unit(THD * thd,Vers_history_point * point) const5968 Vers_history_point_resolve_unit(THD *thd,
5969 Vers_history_point *point) const
5970 {
5971 /*
5972 ENUM/SET have dual type properties (string and numeric).
5973 Require explicit CAST to avoid ambiguity.
5974 */
5975 point->bad_expression_data_type_error(name().ptr());
5976 return true;
5977 }
5978
5979
5980 bool Type_handler_general_purpose_int::
Vers_history_point_resolve_unit(THD * thd,Vers_history_point * point) const5981 Vers_history_point_resolve_unit(THD *thd,
5982 Vers_history_point *point) const
5983 {
5984 return point->resolve_unit_trx_id(thd);
5985 }
5986
5987
5988 bool Type_handler_bit::
Vers_history_point_resolve_unit(THD * thd,Vers_history_point * point) const5989 Vers_history_point_resolve_unit(THD *thd,
5990 Vers_history_point *point) const
5991 {
5992 return point->resolve_unit_trx_id(thd);
5993 }
5994
5995
5996 bool Type_handler_temporal_result::
Vers_history_point_resolve_unit(THD * thd,Vers_history_point * point) const5997 Vers_history_point_resolve_unit(THD *thd,
5998 Vers_history_point *point) const
5999 {
6000 return point->resolve_unit_timestamp(thd);
6001 }
6002
6003
6004 bool Type_handler_general_purpose_string::
Vers_history_point_resolve_unit(THD * thd,Vers_history_point * point) const6005 Vers_history_point_resolve_unit(THD *thd,
6006 Vers_history_point *point) const
6007 {
6008 return point->resolve_unit_timestamp(thd);
6009 }
6010
6011 /***************************************************************************/
6012
collation_specific_name() const6013 LEX_CSTRING Charset::collation_specific_name() const
6014 {
6015 /*
6016 User defined collations can provide arbitrary names
6017 for character sets and collations, so a collation
6018 name not necessarily starts with the character set name.
6019 */
6020 LEX_CSTRING retval;
6021 size_t csname_length= strlen(m_charset->csname);
6022 if (strncmp(m_charset->name, m_charset->csname, csname_length))
6023 {
6024 retval.str= NULL;
6025 retval.length= 0;
6026 return retval;
6027 }
6028 const char *ptr= m_charset->name + csname_length;
6029 retval.str= ptr;
6030 retval.length= strlen(ptr);
6031 return retval;
6032 }
6033
6034
6035 bool
encoding_allows_reinterpret_as(const CHARSET_INFO * cs) const6036 Charset::encoding_allows_reinterpret_as(const CHARSET_INFO *cs) const
6037 {
6038 if (!strcmp(m_charset->csname, cs->csname))
6039 return true;
6040
6041 if (!strcmp(m_charset->csname, MY_UTF8MB3) &&
6042 !strcmp(cs->csname, MY_UTF8MB4))
6043 return true;
6044
6045 /*
6046 Originally we allowed here instat ALTER for ASCII-to-LATIN1
6047 and UCS2-to-UTF16, but this was wrong:
6048 - MariaDB's ascii is not a subset for 8-bit character sets
6049 like latin1, because it allows storing bytes 0x80..0xFF as
6050 "unassigned" characters (see MDEV-19285).
6051 - MariaDB's ucs2 (as in Unicode-1.1) is not a subset for UTF16,
6052 because they treat surrogate codes differently (MDEV-19284).
6053 */
6054 return false;
6055 }
6056
6057
6058 bool
eq_collation_specific_names(CHARSET_INFO * cs) const6059 Charset::eq_collation_specific_names(CHARSET_INFO *cs) const
6060 {
6061 LEX_CSTRING name0= collation_specific_name();
6062 LEX_CSTRING name1= Charset(cs).collation_specific_name();
6063 return name0.length && !cmp(&name0, &name1);
6064 }
6065