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