1 #ifndef FIELD_INCLUDED
2 #define FIELD_INCLUDED
3 /* Copyright (c) 2000, 2015, Oracle and/or its affiliates.
4 Copyright (c) 2008, 2020, MariaDB Corporation.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; version 2 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
18
19 /*
20 Because of the function make_new_field() all field classes that have static
21 variables must declare the size_of() member function.
22 */
23
24 #ifdef USE_PRAGMA_INTERFACE
25 #pragma interface /* gcc class implementation */
26 #endif
27
28 #include "mysqld.h" /* system_charset_info */
29 #include "table.h" /* TABLE */
30 #include "sql_string.h" /* String */
31 #include "my_decimal.h" /* my_decimal */
32 #include "sql_error.h" /* Sql_condition */
33 #include "compat56.h"
34 #include "sql_type.h" /* Type_std_attributes */
35 #include "field_comp.h"
36
37 class Send_field;
38 class Copy_field;
39 class Protocol;
40 class Create_field;
41 class Relay_log_info;
42 class Field;
43 class Column_statistics;
44 class Column_statistics_collected;
45 class Item_func;
46 class Item_bool_func;
47 class Item_equal;
48 class Virtual_tmp_table;
49 class Qualified_column_ident;
50 class Table_ident;
51
52 enum enum_check_fields
53 {
54 CHECK_FIELD_IGNORE,
55 CHECK_FIELD_EXPRESSION,
56 CHECK_FIELD_WARN,
57 CHECK_FIELD_ERROR_FOR_NULL,
58 };
59
60 /*
61 Common declarations for Field and Item
62 */
63 class Value_source
64 {
65 protected:
66
67 // Parameters for warning and note generation
68 class Warn_filter
69 {
70 bool m_want_warning_edom;
71 bool m_want_note_truncated_spaces;
72 public:
Warn_filter(bool want_warning_edom,bool want_note_truncated_spaces)73 Warn_filter(bool want_warning_edom, bool want_note_truncated_spaces) :
74 m_want_warning_edom(want_warning_edom),
75 m_want_note_truncated_spaces(want_note_truncated_spaces)
76 { }
77 Warn_filter(const THD *thd);
want_warning_edom()78 bool want_warning_edom() const
79 { return m_want_warning_edom; }
want_note_truncated_spaces()80 bool want_note_truncated_spaces() const
81 { return m_want_note_truncated_spaces; }
82 };
83 class Warn_filter_all: public Warn_filter
84 {
85 public:
Warn_filter_all()86 Warn_filter_all() :Warn_filter(true, true) { }
87 };
88
89 class Converter_double_to_longlong
90 {
91 protected:
92 bool m_error;
93 longlong m_result;
94 public:
95 Converter_double_to_longlong(double nr, bool unsigned_flag);
result()96 longlong result() const { return m_result; }
error()97 bool error() const { return m_error; }
98 void push_warning(THD *thd, double nr, bool unsigned_flag);
99 };
100 class Converter_double_to_longlong_with_warn:
101 public Converter_double_to_longlong
102 {
103 public:
Converter_double_to_longlong_with_warn(THD * thd,double nr,bool unsigned_flag)104 Converter_double_to_longlong_with_warn(THD *thd, double nr,
105 bool unsigned_flag)
106 :Converter_double_to_longlong(nr, unsigned_flag)
107 {
108 if (m_error)
109 push_warning(thd, nr, unsigned_flag);
110 }
Converter_double_to_longlong_with_warn(double nr,bool unsigned_flag)111 Converter_double_to_longlong_with_warn(double nr, bool unsigned_flag)
112 :Converter_double_to_longlong(nr, unsigned_flag)
113 {
114 if (m_error)
115 push_warning(current_thd, nr, unsigned_flag);
116 }
117 };
118
119 // String-to-number converters
120 class Converter_string_to_number
121 {
122 protected:
123 char *m_end_of_num; // Where the low-level conversion routine stopped
124 int m_error; // The error code returned by the low-level routine
125 bool m_edom; // If EDOM-alike error happened during conversion
126 /**
127 Check string-to-number conversion and produce a warning if
128 - could not convert any digits (EDOM-alike error)
129 - found garbage at the end of the string
130 - found extra spaces at the end (a note)
131 See also Field_num::check_edom_and_truncation() for a similar function.
132
133 @param thd - the thread that will be used to generate warnings.
134 Can be NULL (which means current_thd will be used
135 if a warning is really necessary).
136 @param type - name of the data type
137 (e.g. "INTEGER", "DECIMAL", "DOUBLE")
138 @param cs - character set of the original string
139 @param str - the original string
140 @param end - the end of the string
141 @param allow_notes - tells if trailing space notes should be displayed
142 or suppressed.
143
144 Unlike Field_num::check_edom_and_truncation(), this function does not
145 distinguish between EDOM and truncation and reports the same warning for
146 both cases. Perhaps we should eventually print different warnings,
147 to make the explicit CAST work closer to the implicit cast in
148 Field_xxx::store().
149 */
150 void check_edom_and_truncation(THD *thd, Warn_filter filter,
151 const char *type,
152 CHARSET_INFO *cs,
153 const char *str,
154 size_t length) const;
155 public:
error()156 int error() const { return m_error; }
157 };
158
159 class Converter_strntod: public Converter_string_to_number
160 {
161 double m_result;
162 public:
Converter_strntod(CHARSET_INFO * cs,const char * str,size_t length)163 Converter_strntod(CHARSET_INFO *cs, const char *str, size_t length)
164 {
165 m_result= my_strntod(cs, (char *) str, length, &m_end_of_num, &m_error);
166 // strntod() does not set an error if the input string was empty
167 m_edom= m_error !=0 || str == m_end_of_num;
168 }
result()169 double result() const { return m_result; }
170 };
171
172 class Converter_string_to_longlong: public Converter_string_to_number
173 {
174 protected:
175 longlong m_result;
176 public:
result()177 longlong result() const { return m_result; }
178 };
179
180 class Converter_strntoll: public Converter_string_to_longlong
181 {
182 public:
Converter_strntoll(CHARSET_INFO * cs,const char * str,size_t length)183 Converter_strntoll(CHARSET_INFO *cs, const char *str, size_t length)
184 {
185 m_result= my_strntoll(cs, str, length, 10, &m_end_of_num, &m_error);
186 /*
187 All non-zero errors means EDOM error.
188 strntoll() does not set an error if the input string was empty.
189 Check it here.
190 Notice the different with the same condition in Converter_strntoll10.
191 */
192 m_edom= m_error != 0 || str == m_end_of_num;
193 }
194 };
195
196 class Converter_strtoll10: public Converter_string_to_longlong
197 {
198 public:
Converter_strtoll10(CHARSET_INFO * cs,const char * str,size_t length)199 Converter_strtoll10(CHARSET_INFO *cs, const char *str, size_t length)
200 {
201 m_end_of_num= (char *) str + length;
202 m_result= (*(cs->cset->strtoll10))(cs, str, &m_end_of_num, &m_error);
203 /*
204 Negative error means "good negative number".
205 Only a positive m_error value means a real error.
206 strtoll10() sets error to MY_ERRNO_EDOM in case of an empty string,
207 so we don't have to additionally catch empty strings here.
208 */
209 m_edom= m_error > 0;
210 }
211 };
212
213 class Converter_str2my_decimal: public Converter_string_to_number
214 {
215 public:
Converter_str2my_decimal(uint mask,CHARSET_INFO * cs,const char * str,size_t length,my_decimal * buf)216 Converter_str2my_decimal(uint mask,
217 CHARSET_INFO *cs, const char *str, size_t length,
218 my_decimal *buf)
219 {
220 DBUG_ASSERT(length < UINT_MAX32);
221 m_error= str2my_decimal(mask, str, length, cs,
222 buf, (const char **) &m_end_of_num);
223 // E_DEC_TRUNCATED means a very minor truncation: '1e-100' -> 0
224 m_edom= m_error && m_error != E_DEC_TRUNCATED;
225 }
226 };
227
228
229 // String-to-number converters with automatic warning generation
230 class Converter_strntod_with_warn: public Converter_strntod
231 {
232 public:
Converter_strntod_with_warn(THD * thd,Warn_filter filter,CHARSET_INFO * cs,const char * str,size_t length)233 Converter_strntod_with_warn(THD *thd, Warn_filter filter,
234 CHARSET_INFO *cs,
235 const char *str, size_t length)
236 :Converter_strntod(cs, str, length)
237 {
238 check_edom_and_truncation(thd, filter, "DOUBLE", cs, str, length);
239 }
240 };
241
242 class Converter_strntoll_with_warn: public Converter_strntoll
243 {
244 public:
Converter_strntoll_with_warn(THD * thd,Warn_filter filter,CHARSET_INFO * cs,const char * str,size_t length)245 Converter_strntoll_with_warn(THD *thd, Warn_filter filter,
246 CHARSET_INFO *cs,
247 const char *str, size_t length)
248 :Converter_strntoll(cs, str, length)
249 {
250 check_edom_and_truncation(thd, filter, "INTEGER", cs, str, length);
251 }
252 };
253
254 class Converter_strtoll10_with_warn: public Converter_strtoll10
255 {
256 public:
Converter_strtoll10_with_warn(THD * thd,Warn_filter filter,CHARSET_INFO * cs,const char * str,size_t length)257 Converter_strtoll10_with_warn(THD *thd, Warn_filter filter,
258 CHARSET_INFO *cs,
259 const char *str, size_t length)
260 :Converter_strtoll10(cs, str, length)
261 {
262 check_edom_and_truncation(thd, filter, "INTEGER", cs, str, length);
263 }
264 };
265
266 class Converter_str2my_decimal_with_warn: public Converter_str2my_decimal
267 {
268 public:
Converter_str2my_decimal_with_warn(THD * thd,Warn_filter filter,uint mask,CHARSET_INFO * cs,const char * str,size_t length,my_decimal * buf)269 Converter_str2my_decimal_with_warn(THD *thd, Warn_filter filter,
270 uint mask, CHARSET_INFO *cs,
271 const char *str, size_t length,
272 my_decimal *buf)
273 :Converter_str2my_decimal(mask, cs, str, length, buf)
274 {
275 check_edom_and_truncation(thd, filter, "DECIMAL", cs, str, length);
276 }
277 };
278
279
280 // String-to-number conversion methods for the old code compatibility
longlong_from_string_with_check(CHARSET_INFO * cs,const char * cptr,const char * end)281 longlong longlong_from_string_with_check(CHARSET_INFO *cs, const char *cptr,
282 const char *end) const
283 {
284 /*
285 TODO: Give error if we wanted a signed integer and we got an unsigned
286 one
287
288 Notice, longlong_from_string_with_check() honors thd->no_error, because
289 it's used to handle queries like this:
290 SELECT COUNT(@@basedir);
291 and is called when Item_func_get_system_var::update_null_value()
292 suppresses warnings and then calls val_int().
293 The other methods {double|decimal}_from_string_with_check() ignore
294 thd->no_errors, because they are not used for update_null_value()
295 and they always allow all kind of warnings.
296 */
297 THD *thd= current_thd;
298 return Converter_strtoll10_with_warn(thd, Warn_filter(thd),
299 cs, cptr, end - cptr).result();
300 }
301
double_from_string_with_check(CHARSET_INFO * cs,const char * cptr,const char * end)302 double double_from_string_with_check(CHARSET_INFO *cs, const char *cptr,
303 const char *end) const
304 {
305 return Converter_strntod_with_warn(NULL, Warn_filter_all(),
306 cs, cptr, end - cptr).result();
307 }
decimal_from_string_with_check(my_decimal * decimal_value,CHARSET_INFO * cs,const char * cptr,const char * end)308 my_decimal *decimal_from_string_with_check(my_decimal *decimal_value,
309 CHARSET_INFO *cs,
310 const char *cptr,
311 const char *end)
312 {
313 Converter_str2my_decimal_with_warn(NULL, Warn_filter_all(),
314 E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
315 cs, cptr, end - cptr, decimal_value);
316 return decimal_value;
317 }
318
longlong_from_hex_hybrid(const char * str,size_t length)319 longlong longlong_from_hex_hybrid(const char *str, size_t length)
320 {
321 const char *end= str + length;
322 const char *ptr= end - MY_MIN(length, sizeof(longlong));
323 ulonglong value= 0;
324 for ( ; ptr != end ; ptr++)
325 value= (value << 8) + (ulonglong) (uchar) *ptr;
326 return (longlong) value;
327 }
328
longlong_from_string_with_check(const String * str)329 longlong longlong_from_string_with_check(const String *str) const
330 {
331 return longlong_from_string_with_check(str->charset(),
332 str->ptr(), str->end());
333 }
double_from_string_with_check(const String * str)334 double double_from_string_with_check(const String *str) const
335 {
336 return double_from_string_with_check(str->charset(),
337 str->ptr(), str->end());
338 }
decimal_from_string_with_check(my_decimal * decimal_value,const String * str)339 my_decimal *decimal_from_string_with_check(my_decimal *decimal_value,
340 const String *str)
341 {
342 return decimal_from_string_with_check(decimal_value, str->charset(),
343 str->ptr(), str->end());
344 }
345 // End of String-to-number conversion methods
346
347 public:
348 /*
349 The enumeration Subst_constraint is currently used only in implementations
350 of the virtual function subst_argument_checker.
351 */
352 enum Subst_constraint
353 {
354 ANY_SUBST, /* Any substitution for a field is allowed */
355 IDENTITY_SUBST /* Substitution for a field is allowed if any two
356 different values of the field type are not equal */
357 };
358 /*
359 Item context attributes.
360 Comparison functions pass their attributes to propagate_equal_fields().
361 For example, for string comparison, the collation of the comparison
362 operation is important inside propagate_equal_fields().
363 */
364 class Context
365 {
366 /*
367 Which type of propagation is allowed:
368 - ANY_SUBST (loose equality, according to the collation), or
369 - IDENTITY_SUBST (strict binary equality).
370 */
371 Subst_constraint m_subst_constraint;
372 /*
373 Comparison type.
374 Important only when ANY_SUBSTS.
375 */
376 const Type_handler *m_compare_handler;
377 /*
378 Collation of the comparison operation.
379 Important only when ANY_SUBST.
380 */
381 CHARSET_INFO *m_compare_collation;
382 public:
Context(Subst_constraint subst,const Type_handler * h,CHARSET_INFO * cs)383 Context(Subst_constraint subst, const Type_handler *h, CHARSET_INFO *cs)
384 :m_subst_constraint(subst),
385 m_compare_handler(h),
386 m_compare_collation(cs)
387 { DBUG_ASSERT(h == h->type_handler_for_comparison()); }
subst_constraint()388 Subst_constraint subst_constraint() const { return m_subst_constraint; }
compare_type_handler()389 const Type_handler *compare_type_handler() const
390 {
391 DBUG_ASSERT(m_subst_constraint == ANY_SUBST);
392 return m_compare_handler;
393 }
compare_collation()394 CHARSET_INFO *compare_collation() const
395 {
396 DBUG_ASSERT(m_subst_constraint == ANY_SUBST);
397 return m_compare_collation;
398 }
399 };
400 class Context_identity: public Context
401 { // Use this to request only exact value, no invariants.
402 public:
Context_identity()403 Context_identity()
404 :Context(IDENTITY_SUBST, &type_handler_long_blob, &my_charset_bin) { }
405 };
406 class Context_boolean: public Context
407 { // Use this when an item is [a part of] a boolean expression
408 public:
Context_boolean()409 Context_boolean()
410 :Context(ANY_SUBST, &type_handler_longlong, &my_charset_bin) { }
411 };
412 };
413
414
415 #define STORAGE_TYPE_MASK 7
416 #define COLUMN_FORMAT_MASK 7
417 #define COLUMN_FORMAT_SHIFT 3
418
419 /* The length of the header part for each virtual column in the .frm file */
420 #define FRM_VCOL_OLD_HEADER_SIZE(b) (3 + MY_TEST(b))
421 #define FRM_VCOL_NEW_BASE_SIZE 16
422 #define FRM_VCOL_NEW_HEADER_SIZE 6
423
424 class Count_distinct_field;
425
426 struct ha_field_option_struct;
427
428 struct st_cache_field;
429 int field_conv(Field *to,Field *from);
430 int truncate_double(double *nr, uint field_length, uint dec,
431 bool unsigned_flag, double max_value);
432
get_enum_pack_length(int elements)433 inline uint get_enum_pack_length(int elements)
434 {
435 return elements < 256 ? 1 : 2;
436 }
437
get_set_pack_length(int elements)438 inline uint get_set_pack_length(int elements)
439 {
440 uint len= (elements + 7) / 8;
441 return len > 4 ? 8 : len;
442 }
443
444
445 /**
446 Tests if field type is temporal and has date part,
447 i.e. represents DATE, DATETIME or TIMESTAMP types in SQL.
448
449 @param type Field type, as returned by field->type().
450 @retval true If field type is temporal type with date part.
451 @retval false If field type is not temporal type with date part.
452 */
is_temporal_type_with_date(enum_field_types type)453 inline bool is_temporal_type_with_date(enum_field_types type)
454 {
455 switch (type)
456 {
457 case MYSQL_TYPE_DATE:
458 case MYSQL_TYPE_DATETIME:
459 case MYSQL_TYPE_TIMESTAMP:
460 return true;
461 case MYSQL_TYPE_DATETIME2:
462 case MYSQL_TYPE_TIMESTAMP2:
463 DBUG_ASSERT(0); // field->real_type() should not get to here.
464 /* fall through */
465 default:
466 return false;
467 }
468 }
469
470
471 /**
472 Convert temporal real types as returned by field->real_type()
473 to field type as returned by field->type().
474
475 @param real_type Real type.
476 @retval Field type.
477 */
real_type_to_type(enum_field_types real_type)478 inline enum_field_types real_type_to_type(enum_field_types real_type)
479 {
480 switch (real_type)
481 {
482 case MYSQL_TYPE_TIME2:
483 return MYSQL_TYPE_TIME;
484 case MYSQL_TYPE_DATETIME2:
485 return MYSQL_TYPE_DATETIME;
486 case MYSQL_TYPE_TIMESTAMP2:
487 return MYSQL_TYPE_TIMESTAMP;
488 case MYSQL_TYPE_NEWDATE:
489 return MYSQL_TYPE_DATE;
490 /* Note: NEWDECIMAL is a type, not only a real_type */
491 default: return real_type;
492 }
493 }
494
495
496 enum enum_vcol_info_type
497 {
498 VCOL_GENERATED_VIRTUAL, VCOL_GENERATED_STORED,
499 VCOL_DEFAULT, VCOL_CHECK_FIELD, VCOL_CHECK_TABLE,
500 /* Additional types should be added here */
501 /* Following is the highest value last */
502 VCOL_TYPE_NONE = 127 // Since the 0 value is already in use
503 };
504
vcol_type_name(enum_vcol_info_type type)505 static inline const char *vcol_type_name(enum_vcol_info_type type)
506 {
507 switch (type)
508 {
509 case VCOL_GENERATED_VIRTUAL:
510 case VCOL_GENERATED_STORED:
511 return "GENERATED ALWAYS AS";
512 case VCOL_DEFAULT:
513 return "DEFAULT";
514 case VCOL_CHECK_FIELD:
515 case VCOL_CHECK_TABLE:
516 return "CHECK";
517 case VCOL_TYPE_NONE:
518 return "UNTYPED";
519 }
520 return 0;
521 }
522
523 /*
524 Flags for Virtual_column_info. If none is set, the expression must be
525 a constant with no side-effects, so it's calculated at CREATE TABLE time,
526 stored in table->record[2], and not recalculated for every statement.
527 */
528 #define VCOL_FIELD_REF 1
529 #define VCOL_NON_DETERMINISTIC 2
530 #define VCOL_SESSION_FUNC 4 /* uses session data, e.g. USER or DAYNAME */
531 #define VCOL_TIME_FUNC 8
532 #define VCOL_AUTO_INC 16
533 #define VCOL_IMPOSSIBLE 32
534 #define VCOL_NOT_VIRTUAL 64 /* Function can't be virtual */
535
536 #define VCOL_NOT_STRICTLY_DETERMINISTIC \
537 (VCOL_NON_DETERMINISTIC | VCOL_TIME_FUNC | VCOL_SESSION_FUNC)
538
539 /*
540 Virtual_column_info is the class to contain additional
541 characteristics that is specific for a virtual/computed
542 field such as:
543 - the defining expression that is evaluated to compute the value
544 of the field
545 - whether the field is to be stored in the database
546 - whether the field is used in a partitioning expression
547 */
548
549 class Virtual_column_info: public Sql_alloc
550 {
551 private:
552 enum_vcol_info_type vcol_type; /* Virtual column expression type */
553 /*
554 The following data is only updated by the parser and read
555 when a Create_field object is created/initialized.
556 */
557 enum_field_types field_type; /* Real field type*/
558 /* Flag indicating that the field used in a partitioning expression */
559 bool in_partitioning_expr;
560
561 public:
562 /* Flag indicating that the field is physically stored in the database */
563 bool stored_in_db;
564 bool utf8; /* Already in utf8 */
565 bool automatic_name;
566 Item *expr;
567 LEX_CSTRING name; /* Name of constraint */
568 /* see VCOL_* (VCOL_FIELD_REF, ...) */
569 uint flags;
570
Virtual_column_info()571 Virtual_column_info()
572 : vcol_type((enum_vcol_info_type)VCOL_TYPE_NONE),
573 field_type((enum enum_field_types)MYSQL_TYPE_VIRTUAL),
574 in_partitioning_expr(FALSE), stored_in_db(FALSE),
575 utf8(TRUE), automatic_name(FALSE), expr(NULL), flags(0)
576 {
577 name.str= NULL;
578 name.length= 0;
579 };
~Virtual_column_info()580 ~Virtual_column_info() {}
get_vcol_type()581 enum_vcol_info_type get_vcol_type() const
582 {
583 return vcol_type;
584 }
set_vcol_type(enum_vcol_info_type v_type)585 void set_vcol_type(enum_vcol_info_type v_type)
586 {
587 vcol_type= v_type;
588 }
get_vcol_type_name()589 const char *get_vcol_type_name() const
590 {
591 DBUG_ASSERT(vcol_type != VCOL_TYPE_NONE);
592 return vcol_type_name(vcol_type);
593 }
get_real_type()594 enum_field_types get_real_type() const
595 {
596 return field_type;
597 }
set_field_type(enum_field_types fld_type)598 void set_field_type(enum_field_types fld_type)
599 {
600 /* Calling this function can only be done once. */
601 field_type= fld_type;
602 }
is_stored()603 bool is_stored() const
604 {
605 return stored_in_db;
606 }
set_stored_in_db_flag(bool stored)607 void set_stored_in_db_flag(bool stored)
608 {
609 stored_in_db= stored;
610 }
is_in_partitioning_expr()611 bool is_in_partitioning_expr() const
612 {
613 return in_partitioning_expr;
614 }
mark_as_in_partitioning_expr()615 void mark_as_in_partitioning_expr()
616 {
617 in_partitioning_expr= TRUE;
618 }
619 inline bool is_equal(const Virtual_column_info* vcol) const;
620 inline void print(String*);
621 };
622
623 class Field: public Value_source
624 {
625 Field(const Item &); /* Prevent use of these */
626 void operator=(Field &);
627 protected:
save_in_field_str(Field * to)628 int save_in_field_str(Field *to)
629 {
630 StringBuffer<MAX_FIELD_WIDTH> result(charset());
631 val_str(&result);
632 return to->store(result.ptr(), result.length(), charset());
633 }
634 void error_generated_column_function_is_not_allowed(THD *thd, bool error)
635 const;
636 static void do_field_int(Copy_field *copy);
637 static void do_field_real(Copy_field *copy);
638 static void do_field_string(Copy_field *copy);
639 static void do_field_temporal(Copy_field *copy);
640 static void do_field_timestamp(Copy_field *copy);
641 static void do_field_decimal(Copy_field *copy);
642 public:
new(size_t size,MEM_ROOT * mem_root)643 static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
644 { return alloc_root(mem_root, size); }
new(size_t size)645 static void *operator new(size_t size) throw ()
646 {
647 DBUG_ASSERT(size < UINT_MAX32);
648 return thd_alloc(current_thd, (uint) size);
649 }
delete(void * ptr_arg,size_t size)650 static void operator delete(void *ptr_arg, size_t size) { TRASH_FREE(ptr_arg, size); }
delete(void * ptr,MEM_ROOT * mem_root)651 static void operator delete(void *ptr, MEM_ROOT *mem_root)
652 { DBUG_ASSERT(0); }
653
654 /**
655 Used by System Versioning.
656 */
set_max()657 virtual void set_max()
658 { DBUG_ASSERT(0); }
is_max()659 virtual bool is_max()
660 { DBUG_ASSERT(0); return false; }
661
662 uchar *ptr; // Position to field in record
663
664 field_visibility_t invisible;
665 /**
666 Byte where the @c NULL bit is stored inside a record. If this Field is a
667 @c NOT @c NULL field, this member is @c NULL.
668 */
669 uchar *null_ptr;
670 /*
671 Note that you can use table->in_use as replacement for current_thd member
672 only inside of val_*() and store() members (e.g. you can't use it in cons)
673 */
674 TABLE *table; // Pointer for table
675 TABLE *orig_table; // Pointer to original table
676 const char * const *table_name; // Pointer to alias in TABLE
677 LEX_CSTRING field_name;
678 LEX_CSTRING comment;
679 /** reference to the list of options or NULL */
680 engine_option_value *option_list;
681 ha_field_option_struct *option_struct; /* structure with parsed options */
682 /* Field is part of the following keys */
683 key_map key_start, part_of_key, part_of_key_not_clustered;
684
685 /*
686 Bitmap of indexes that have records ordered by col1, ... this_field, ...
687
688 For example, INDEX (col(prefix_n)) is not present in col.part_of_sortkey.
689 */
690 key_map part_of_sortkey;
691 /*
692 We use three additional unireg types for TIMESTAMP to overcome limitation
693 of current binary format of .frm file. We'd like to be able to support
694 NOW() as default and on update value for such fields but unable to hold
695 this info anywhere except unireg_check field. This issue will be resolved
696 in more clean way with transition to new text based .frm format.
697 See also comment for Field_timestamp::Field_timestamp().
698 */
699 enum utype {
700 NONE=0,
701 NEXT_NUMBER=15, // AUTO_INCREMENT
702 TIMESTAMP_OLD_FIELD=18, // TIMESTAMP created before 4.1.3
703 TIMESTAMP_DN_FIELD=21, // TIMESTAMP DEFAULT NOW()
704 TIMESTAMP_UN_FIELD=22, // TIMESTAMP ON UPDATE NOW()
705 TIMESTAMP_DNUN_FIELD=23, // TIMESTAMP DEFAULT NOW() ON UPDATE NOW()
706 TMYSQL_COMPRESSED= 24, // Compatibility with TMySQL
707 };
708 enum geometry_type
709 {
710 GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
711 GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
712 GEOM_GEOMETRYCOLLECTION = 7
713 };
714 enum imagetype { itRAW, itMBR};
715
716 utype unireg_check;
717 uint32 field_length; // Length of field
718 uint32 flags;
719 uint16 field_index; // field number in fields array
720 uchar null_bit; // Bit used to test null bit
721 /**
722 If true, this field was created in create_tmp_field_from_item from a NULL
723 value. This means that the type of the field is just a guess, and the type
724 may be freely coerced to another type.
725
726 @see create_tmp_field_from_item
727 @see Item_type_holder::get_real_type
728
729 */
730 bool is_created_from_null_item;
731
732 /*
733 Selectivity of the range condition over this field.
734 When calculating this selectivity a range predicate
735 is taken into account only if:
736 - it is extracted from the WHERE clause
737 - it depends only on the table the field belongs to
738 */
739 double cond_selectivity;
740
741 /*
742 The next field in the class of equal fields at the top AND level
743 of the WHERE clause
744 */
745 Field *next_equal_field;
746
747 /*
748 This structure is used for statistical data on the column
749 that has been read from the statistical table column_stat
750 */
751 Column_statistics *read_stats;
752 /*
753 This structure is used for statistical data on the column that
754 is collected by the function collect_statistics_for_table
755 */
756 Column_statistics_collected *collected_stats;
757
758 /*
759 This is additional data provided for any computed(virtual) field,
760 default function or check constraint.
761 In particular it includes a pointer to the item by which this field
762 can be computed from other fields.
763 */
764 Virtual_column_info *vcol_info, *check_constraint, *default_value;
765
766 Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
767 uchar null_bit_arg, utype unireg_check_arg,
768 const LEX_CSTRING *field_name_arg);
~Field()769 virtual ~Field() {}
770
dtcollation()771 DTCollation dtcollation() const
772 {
773 return DTCollation(charset(), derivation(), repertoire());
774 }
type_std_attributes()775 virtual Type_std_attributes type_std_attributes() const
776 {
777 return Type_std_attributes(field_length, decimals(),
778 MY_TEST(flags & UNSIGNED_FLAG),
779 dtcollation());
780 }
781
is_unsigned()782 bool is_unsigned() const { return flags & UNSIGNED_FLAG; }
783
784 /**
785 Convenience definition of a copy function returned by
786 Field::get_copy_func()
787 */
788 typedef void Copy_func(Copy_field*);
789 virtual Copy_func *get_copy_func(const Field *from) const= 0;
790 /* Store functions returns 1 on overflow and -1 on fatal error */
store_field(Field * from)791 virtual int store_field(Field *from) { return from->save_in_field(this); }
792 virtual int save_in_field(Field *to)= 0;
793 /**
794 Check if it is possible just copy the value
795 of the field 'from' to the field 'this', e.g. for
796 INSERT INTO t1 (field1) SELECT field2 FROM t2;
797 @param from - The field to copy from
798 @retval true - it is possible to just copy value of 'from' to 'this'
799 @retval false - conversion is needed
800 */
801 virtual bool memcpy_field_possible(const Field *from) const= 0;
802 virtual int store(const char *to, size_t length,CHARSET_INFO *cs)=0;
803 virtual int store_hex_hybrid(const char *str, size_t length);
804 virtual int store(double nr)=0;
805 virtual int store(longlong nr, bool unsigned_val)=0;
806 virtual int store_decimal(const my_decimal *d)=0;
807 virtual int store_time_dec(const MYSQL_TIME *ltime, uint dec);
808 virtual int store_timestamp(my_time_t timestamp, ulong sec_part);
store_time(const MYSQL_TIME * ltime)809 int store_time(const MYSQL_TIME *ltime)
810 { return store_time_dec(ltime, TIME_SECOND_PART_DIGITS); }
811 int store(const char *to, size_t length, CHARSET_INFO *cs,
812 enum_check_fields check_level);
store(const LEX_STRING * ls,CHARSET_INFO * cs)813 int store(const LEX_STRING *ls, CHARSET_INFO *cs)
814 {
815 DBUG_ASSERT(ls->length < UINT_MAX32);
816 return store(ls->str, (uint) ls->length, cs);
817 }
store(const LEX_CSTRING * ls,CHARSET_INFO * cs)818 int store(const LEX_CSTRING *ls, CHARSET_INFO *cs)
819 {
820 DBUG_ASSERT(ls->length < UINT_MAX32);
821 return store(ls->str, (uint) ls->length, cs);
822 }
store(const LEX_CSTRING & ls,CHARSET_INFO * cs)823 int store(const LEX_CSTRING &ls, CHARSET_INFO *cs)
824 {
825 DBUG_ASSERT(ls.length < UINT_MAX32);
826 return store(ls.str, (uint) ls.length, cs);
827 }
828
829 #ifdef HAVE_valgrind
830 /**
831 Mark unused memory in the field as defined. Mainly used to ensure
832 that if we write full field to disk (for example in
833 Count_distinct_field::add(), we don't write unitalized data to
834 disk which would confuse valgrind or MSAN.
835 */
mark_unused_memory_as_defined()836 virtual void mark_unused_memory_as_defined() {}
837 #else
mark_unused_memory_as_defined()838 void mark_unused_memory_as_defined() {}
839 #endif
840
841 virtual double val_real(void)=0;
842 virtual longlong val_int(void)=0;
843 /*
844 Get ulonglong representation.
845 Negative values are truncated to 0.
846 */
val_uint(void)847 virtual ulonglong val_uint(void)
848 {
849 longlong nr= val_int();
850 return nr < 0 ? 0 : (ulonglong) nr;
851 }
852 virtual bool val_bool(void)= 0;
853 virtual my_decimal *val_decimal(my_decimal *);
val_str(String * str)854 inline String *val_str(String *str) { return val_str(str, str); }
855 /*
856 val_str(buf1, buf2) gets two buffers and should use them as follows:
857 if it needs a temp buffer to convert result to string - use buf1
858 example Field_tiny::val_str()
859 if the value exists as a string already - use buf2
860 example Field_string::val_str()
861 consequently, buf2 may be created as 'String buf;' - no memory
862 will be allocated for it. buf1 will be allocated to hold a
863 value if it's too small. Using allocated buffer for buf2 may result in
864 an unnecessary free (and later, may be an alloc).
865 This trickery is used to decrease a number of malloc calls.
866 */
867 virtual String *val_str(String*,String *)=0;
868 String *val_int_as_str(String *val_buffer, bool unsigned_flag);
869 /*
870 Return the field value as a LEX_CSTRING, without padding to full length
871 (MODE_PAD_CHAR_TO_FULL_LENGTH is temporarily suppressed during the call).
872
873 In case of an empty value, to[0] is assigned to empty_clex_string,
874 memory is not allocated.
875 In case of a non-empty value, the memory is allocated on mem_root.
876 In case of a memory allocation failure, to[0] is assigned to {NULL,0}.
877
878 @param [IN] mem_root store non-empty values here
879 @param [OUT to return the string here
880 @retval false (success)
881 @retval true (EOM)
882 */
883 bool val_str_nopad(MEM_ROOT *mem_root, LEX_CSTRING *to);
884 fast_field_copier get_fast_field_copier(const Field *from);
885 /*
886 str_needs_quotes() returns TRUE if the value returned by val_str() needs
887 to be quoted when used in constructing an SQL query.
888 */
str_needs_quotes()889 virtual bool str_needs_quotes() { return FALSE; }
result_type()890 Item_result result_type () const
891 {
892 return type_handler()->result_type();
893 }
cmp_type()894 Item_result cmp_type () const
895 {
896 return type_handler()->cmp_type();
897 }
898 static enum_field_types field_type_merge(enum_field_types, enum_field_types);
eq(Field * field)899 virtual bool eq(Field *field)
900 {
901 return (ptr == field->ptr && null_ptr == field->null_ptr &&
902 null_bit == field->null_bit && field->type() == type());
903 }
904 virtual bool eq_def(const Field *field) const;
905
906 /*
907 pack_length() returns size (in bytes) used to store field data in memory
908 (i.e. it returns the maximum size of the field in a row of the table,
909 which is located in RAM).
910 */
pack_length()911 virtual uint32 pack_length() const { return (uint32) field_length; }
912
913 /*
914 pack_length_in_rec() returns size (in bytes) used to store field data on
915 storage (i.e. it returns the maximal size of the field in a row of the
916 table, which is located on disk).
917 */
pack_length_in_rec()918 virtual uint32 pack_length_in_rec() const { return pack_length(); }
919 virtual bool compatible_field_size(uint metadata, Relay_log_info *rli,
920 uint16 mflags, int *order);
pack_length_from_metadata(uint field_metadata)921 virtual uint pack_length_from_metadata(uint field_metadata)
922 {
923 DBUG_ENTER("Field::pack_length_from_metadata");
924 DBUG_RETURN(field_metadata);
925 }
row_pack_length()926 virtual uint row_pack_length() const { return 0; }
927
928
929 /**
930 Retrieve the field metadata for fields.
931
932 This default implementation returns 0 and saves 0 in the first_byte value.
933
934 @param first_byte First byte of field metadata
935
936 @returns 0 no bytes written.
937 */
938
save_field_metadata(uchar * first_byte)939 virtual int save_field_metadata(uchar *first_byte)
940 { return 0; }
941
942
943 /*
944 data_length() return the "real size" of the data in memory.
945 */
data_length()946 virtual uint32 data_length() { return pack_length(); }
sort_length()947 virtual uint32 sort_length() const { return pack_length(); }
948
949 /*
950 Get the number bytes occupied by the value in the field.
951 CHAR values are stripped of trailing spaces.
952 Flexible values are stripped of their length.
953 */
value_length()954 virtual uint32 value_length()
955 {
956 uint len;
957 if (!zero_pack() &&
958 (type() == MYSQL_TYPE_STRING &&
959 (len= pack_length()) >= 4 && len < 256))
960 {
961 uchar *str, *end;
962 for (str= ptr, end= str+len; end > str && end[-1] == ' '; end--) {}
963 len=(uint) (end-str);
964 return len;
965 }
966 return data_length();
967 }
968
969 /**
970 Get the maximum size of the data in packed format.
971
972 @return Maximum data length of the field when packed using the
973 Field::pack() function.
974 */
max_data_length()975 virtual uint32 max_data_length() const {
976 return pack_length();
977 };
978
reset(void)979 virtual int reset(void) { bzero(ptr,pack_length()); return 0; }
reset_fields()980 virtual void reset_fields() {}
ptr_in_record(const uchar * record)981 const uchar *ptr_in_record(const uchar *record) const
982 {
983 my_ptrdiff_t l_offset= (my_ptrdiff_t) (ptr - table->record[0]);
984 DBUG_ASSERT(l_offset >= 0 && table->s->rec_buff_length - l_offset > 0);
985 return record + l_offset;
986 }
987 virtual int set_default();
988
has_update_default_function()989 bool has_update_default_function() const
990 {
991 return flags & ON_UPDATE_NOW_FLAG;
992 }
has_default_now_unireg_check()993 bool has_default_now_unireg_check() const
994 {
995 return unireg_check == TIMESTAMP_DN_FIELD
996 || unireg_check == TIMESTAMP_DNUN_FIELD;
997 }
998
999 /*
1000 Mark the field as having a value supplied by the client, thus it should
1001 not be auto-updated.
1002 */
set_has_explicit_value()1003 void set_has_explicit_value()
1004 {
1005 bitmap_set_bit(&table->has_value_set, field_index);
1006 }
has_explicit_value()1007 bool has_explicit_value()
1008 {
1009 return bitmap_is_set(&table->has_value_set, field_index);
1010 }
clear_has_explicit_value()1011 void clear_has_explicit_value()
1012 {
1013 bitmap_clear_bit(&table->has_value_set, field_index);
1014 }
1015
get_timestamp(const uchar * pos,ulong * sec_part)1016 virtual my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const
1017 { DBUG_ASSERT(0); return 0; }
get_timestamp(ulong * sec_part)1018 my_time_t get_timestamp(ulong *sec_part) const
1019 {
1020 return get_timestamp(ptr, sec_part);
1021 }
1022
binary()1023 virtual bool binary() const { return 1; }
zero_pack()1024 virtual bool zero_pack() const { return 1; }
key_type()1025 virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
key_length()1026 virtual uint32 key_length() const { return pack_length(); }
1027 virtual const Type_handler *type_handler() const= 0;
type()1028 virtual enum_field_types type() const
1029 {
1030 return type_handler()->field_type();
1031 }
real_type()1032 virtual enum_field_types real_type() const
1033 {
1034 return type_handler()->real_field_type();
1035 }
binlog_type()1036 virtual enum_field_types binlog_type() const
1037 {
1038 /*
1039 Binlog stores field->type() as type code by default. For example,
1040 it puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
1041 with extra data type details put into metadata.
1042
1043 Binlog behaviour slightly differs between various MySQL and MariaDB
1044 versions for the temporal data types TIME, DATETIME and TIMESTAMP.
1045
1046 MySQL prior to 5.6 uses MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
1047 and MYSQL_TYPE_TIMESTAMP type codes in binlog and stores no
1048 additional metadata.
1049
1050 MariaDB-5.3 implements new versions for TIME, DATATIME, TIMESTAMP
1051 with fractional second precision, but uses the old format for the
1052 types TIME(0), DATETIME(0), TIMESTAMP(0), and it still stores
1053 MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME and MYSQL_TYPE_TIMESTAMP in binlog,
1054 with no additional metadata.
1055 So row-based replication between temporal data types of
1056 different precision is not possible in MariaDB.
1057
1058 MySQL-5.6 also implements a new version of TIME, DATETIME, TIMESTAMP
1059 which support fractional second precision 0..6, and use the new
1060 format even for the types TIME(0), DATETIME(0), TIMESTAMP(0).
1061 For these new data types, MySQL-5.6 stores new type codes
1062 MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2 in binlog,
1063 with fractional precision 0..6 put into metadata.
1064 This makes it in theory possible to do row-based replication between
1065 columns of different fractional precision (e.g. from TIME(1) on master
1066 to TIME(6) on slave). However, it's not currently fully implemented yet.
1067 MySQL-5.6 can only do row-based replication from the old types
1068 TIME, DATETIME, TIMESTAMP (represented by MYSQL_TYPE_TIME,
1069 MYSQL_TYPE_DATETIME and MYSQL_TYPE_TIMESTAMP type codes in binlog)
1070 to the new corresponding types TIME(0), DATETIME(0), TIMESTAMP(0).
1071
1072 Note: MariaDB starting from the version 10.0 understands the new
1073 MySQL-5.6 type codes MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2,
1074 MYSQL_TYPE_TIMESTAMP2. When started over MySQL-5.6 tables both on
1075 master and on slave, MariaDB-10.0 can also do row-based replication
1076 from the old types TIME, DATETIME, TIMESTAMP to the new MySQL-5.6
1077 types TIME(0), DATETIME(0), TIMESTAMP(0).
1078
1079 Note: perhaps binlog should eventually be modified to store
1080 real_type() instead of type() for all column types.
1081 */
1082 return type();
1083 }
cmp(const uchar * str)1084 inline int cmp(const uchar *str) { return cmp(ptr,str); }
1085 virtual int cmp(const uchar *,const uchar *)=0;
1086 /*
1087 The following method is used for comparing prefix keys.
1088 Currently it's only used in partitioning.
1089 */
cmp_prefix(const uchar * a,const uchar * b,size_t prefix_len)1090 virtual int cmp_prefix(const uchar *a, const uchar *b, size_t prefix_len)
1091 { return cmp(a, b); }
1092 virtual int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0U)
1093 { return memcmp(a,b,pack_length()); }
cmp_offset(uint row_offset)1094 virtual int cmp_offset(uint row_offset)
1095 { return cmp(ptr,ptr+row_offset); }
cmp_binary_offset(uint row_offset)1096 virtual int cmp_binary_offset(uint row_offset)
1097 { return cmp_binary(ptr, ptr+row_offset); };
key_cmp(const uchar * a,const uchar * b)1098 virtual int key_cmp(const uchar *a,const uchar *b)
1099 { return cmp(a, b); }
key_cmp(const uchar * str,uint length)1100 virtual int key_cmp(const uchar *str, uint length)
1101 { return cmp(ptr,str); }
1102 /*
1103 Update the value m of the 'min_val' field with the current value v
1104 of this field if force_update is set to TRUE or if v < m.
1105 Return TRUE if the value has been updated.
1106 */
update_min(Field * min_val,bool force_update)1107 virtual bool update_min(Field *min_val, bool force_update)
1108 {
1109 bool update_fl= force_update || cmp(ptr, min_val->ptr) < 0;
1110 if (update_fl)
1111 {
1112 min_val->set_notnull();
1113 memcpy(min_val->ptr, ptr, pack_length());
1114 }
1115 return update_fl;
1116 }
1117 /*
1118 Update the value m of the 'max_val' field with the current value v
1119 of this field if force_update is set to TRUE or if v > m.
1120 Return TRUE if the value has been updated.
1121 */
update_max(Field * max_val,bool force_update)1122 virtual bool update_max(Field *max_val, bool force_update)
1123 {
1124 bool update_fl= force_update || cmp(ptr, max_val->ptr) > 0;
1125 if (update_fl)
1126 {
1127 max_val->set_notnull();
1128 memcpy(max_val->ptr, ptr, pack_length());
1129 }
1130 return update_fl;
1131 }
store_field_value(uchar * val,uint len)1132 virtual void store_field_value(uchar *val, uint len)
1133 {
1134 memcpy(ptr, val, len);
1135 }
decimals()1136 virtual uint decimals() const { return 0; }
1137 virtual Information_schema_numeric_attributes
information_schema_numeric_attributes()1138 information_schema_numeric_attributes() const
1139 {
1140 return Information_schema_numeric_attributes();
1141 }
1142 virtual Information_schema_character_attributes
information_schema_character_attributes()1143 information_schema_character_attributes() const
1144 {
1145 return Information_schema_character_attributes();
1146 }
1147 /*
1148 Caller beware: sql_type can change str.Ptr, so check
1149 ptr() to see if it changed if you are using your own buffer
1150 in str and restore it with set() if needed
1151 */
1152 virtual void sql_type(String &str) const =0;
sql_rpl_type(String * str)1153 virtual void sql_rpl_type(String *str) const { sql_type(*str); }
1154 virtual uint size_of() const =0; // For new field
1155 inline bool is_null(my_ptrdiff_t row_offset= 0) const
1156 {
1157 /*
1158 The table may have been marked as containing only NULL values
1159 for all fields if it is a NULL-complemented row of an OUTER JOIN
1160 or if the query is an implicitly grouped query (has aggregate
1161 functions but no GROUP BY clause) with no qualifying rows. If
1162 this is the case (in which TABLE::null_row is true), the field
1163 is considered to be NULL.
1164
1165 Note that if a table->null_row is set then also all null_bits are
1166 set for the row.
1167
1168 In the case of the 'result_field' for GROUP BY, table->null_row might
1169 refer to the *next* row in the table (when the algorithm is: read the
1170 next row, see if any of group column values have changed, send the
1171 result - grouped - row to the client if yes). So, table->null_row might
1172 be wrong, but such a result_field is always nullable (that's defined by
1173 original_field->maybe_null()) and we trust its null bit.
1174 */
1175 return null_ptr ? null_ptr[row_offset] & null_bit : table->null_row;
1176 }
1177 inline bool is_real_null(my_ptrdiff_t row_offset= 0) const
1178 { return null_ptr && (null_ptr[row_offset] & null_bit); }
is_null_in_record(const uchar * record)1179 inline bool is_null_in_record(const uchar *record) const
1180 {
1181 if (maybe_null_in_table())
1182 return record[(uint) (null_ptr - table->record[0])] & null_bit;
1183 return 0;
1184 }
1185 inline void set_null(my_ptrdiff_t row_offset= 0)
1186 { if (null_ptr) null_ptr[row_offset]|= null_bit; }
1187 inline void set_notnull(my_ptrdiff_t row_offset= 0)
1188 { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
maybe_null(void)1189 inline bool maybe_null(void) const
1190 { return null_ptr != 0 || table->maybe_null; }
1191 // Set to NULL on LOAD DATA or LOAD XML
1192 virtual bool load_data_set_null(THD *thd);
1193 // Reset when a LOAD DATA file ended unexpectedly
1194 virtual bool load_data_set_no_data(THD *thd, bool fixed_format);
1195 void load_data_set_value(const char *pos, uint length, CHARSET_INFO *cs);
1196
1197 /* @return true if this field is NULL-able (even if temporarily) */
real_maybe_null(void)1198 inline bool real_maybe_null(void) const { return null_ptr != 0; }
null_offset(const uchar * record)1199 uint null_offset(const uchar *record) const
1200 { return (uint) (null_ptr - record); }
1201 /*
1202 For a NULL-able field (that can actually store a NULL value in a table)
1203 null_ptr points to the "null bitmap" in the table->record[0] header. For
1204 NOT NULL fields it is either 0 or points outside table->record[0] into the
1205 table->triggers->extra_null_bitmap (so that the field can store a NULL
1206 value temporarily, only in memory)
1207 */
maybe_null_in_table()1208 bool maybe_null_in_table() const
1209 { return null_ptr >= table->record[0] && null_ptr <= ptr; }
1210
null_offset()1211 uint null_offset() const
1212 { return null_offset(table->record[0]); }
set_null_ptr(uchar * p_null_ptr,uint p_null_bit)1213 void set_null_ptr(uchar *p_null_ptr, uint p_null_bit)
1214 {
1215 null_ptr= p_null_ptr;
1216 null_bit= p_null_bit;
1217 }
1218
stored_in_db()1219 bool stored_in_db() const { return !vcol_info || vcol_info->stored_in_db; }
1220 bool check_vcol_sql_mode_dependency(THD *, vcol_init_mode mode) const;
1221
value_depends_on_sql_mode()1222 virtual sql_mode_t value_depends_on_sql_mode() const
1223 {
1224 return 0;
1225 }
can_handle_sql_mode_dependency_on_store()1226 virtual sql_mode_t can_handle_sql_mode_dependency_on_store() const
1227 {
1228 return 0;
1229 }
1230
get_thd()1231 inline THD *get_thd() const
1232 { return likely(table) ? table->in_use : current_thd; }
1233
1234 enum {
1235 LAST_NULL_BYTE_UNDEF= 0
1236 };
1237
1238 /*
1239 Find the position of the last null byte for the field.
1240
1241 SYNOPSIS
1242 last_null_byte()
1243
1244 DESCRIPTION
1245 Return a pointer to the last byte of the null bytes where the
1246 field conceptually is placed.
1247
1248 RETURN VALUE
1249 The position of the last null byte relative to the beginning of
1250 the record. If the field does not use any bits of the null
1251 bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
1252 */
last_null_byte()1253 size_t last_null_byte() const {
1254 size_t bytes= do_last_null_byte();
1255 DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
1256 DBUG_ASSERT(bytes <= table->s->null_bytes);
1257 return bytes;
1258 }
1259
1260 void make_sort_key(uchar *buff, uint length);
1261 virtual void make_send_field(Send_field *);
1262
1263 /*
1264 Some implementations actually may write up to 8 bytes regardless of what
1265 size was requested. This is due to the minimum value of the system variable
1266 max_sort_length.
1267 */
1268
1269 virtual void sort_string(uchar *buff,uint length)=0;
1270 virtual bool optimize_range(uint idx, uint part) const;
free()1271 virtual void free() {}
1272 virtual Field *make_new_field(MEM_ROOT *root, TABLE *new_table,
1273 bool keep_type);
1274 virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
1275 uchar *new_ptr, uint32 length,
1276 uchar *new_null_ptr, uint new_null_bit);
1277 Field *clone(MEM_ROOT *mem_root, TABLE *new_table);
1278 Field *clone(MEM_ROOT *mem_root, TABLE *new_table, my_ptrdiff_t diff);
move_field(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg)1279 inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
1280 {
1281 ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
1282 }
move_field(uchar * ptr_arg)1283 inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
record_ptr()1284 inline uchar *record_ptr() // record[0] or wherever the field was moved to
1285 {
1286 my_ptrdiff_t offset= table->s->field[field_index]->ptr - table->s->default_values;
1287 return ptr - offset;
1288 }
move_field_offset(my_ptrdiff_t ptr_diff)1289 virtual void move_field_offset(my_ptrdiff_t ptr_diff)
1290 {
1291 ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
1292 if (null_ptr)
1293 null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
1294 }
get_image(uchar * buff,uint length,CHARSET_INFO * cs)1295 virtual void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
1296 { memcpy(buff,ptr,length); }
set_image(const uchar * buff,uint length,CHARSET_INFO * cs)1297 virtual void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
1298 { memcpy(ptr,buff,length); }
1299
1300
1301 /*
1302 Copy a field part into an output buffer.
1303
1304 SYNOPSIS
1305 Field::get_key_image()
1306 buff [out] output buffer
1307 length output buffer size
1308 type itMBR for geometry blobs, otherwise itRAW
1309
1310 DESCRIPTION
1311 This function makes a copy of field part of size equal to or
1312 less than "length" parameter value.
1313 For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
1314 is padded by zero byte.
1315
1316 NOTES
1317 For variable length character fields (i.e. UTF-8) the "length"
1318 parameter means a number of output buffer bytes as if all field
1319 characters have maximal possible size (mbmaxlen). In the other words,
1320 "length" parameter is a number of characters multiplied by
1321 field_charset->mbmaxlen.
1322
1323 RETURN
1324 Number of copied bytes (excluding padded zero bytes -- see above).
1325 */
1326
get_key_image(uchar * buff,uint length,imagetype type_arg)1327 virtual uint get_key_image(uchar *buff, uint length, imagetype type_arg)
1328 {
1329 get_image(buff, length, &my_charset_bin);
1330 return length;
1331 }
set_key_image(const uchar * buff,uint length)1332 virtual void set_key_image(const uchar *buff,uint length)
1333 { set_image(buff,length, &my_charset_bin); }
val_int_offset(uint row_offset)1334 inline longlong val_int_offset(uint row_offset)
1335 {
1336 ptr+=row_offset;
1337 longlong tmp=val_int();
1338 ptr-=row_offset;
1339 return tmp;
1340 }
val_int(const uchar * new_ptr)1341 inline longlong val_int(const uchar *new_ptr)
1342 {
1343 uchar *old_ptr= ptr;
1344 longlong return_value;
1345 ptr= (uchar*) new_ptr;
1346 return_value= val_int();
1347 ptr= old_ptr;
1348 return return_value;
1349 }
val_str(String * str,const uchar * new_ptr)1350 inline String *val_str(String *str, const uchar *new_ptr)
1351 {
1352 uchar *old_ptr= ptr;
1353 ptr= (uchar*) new_ptr;
1354 val_str(str);
1355 ptr= old_ptr;
1356 return str;
1357 }
1358 virtual bool send_binary(Protocol *protocol);
1359
1360 virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
1361 /**
1362 @overload Field::pack(uchar*, const uchar*, uint, bool)
1363 */
pack(uchar * to,const uchar * from)1364 uchar *pack(uchar *to, const uchar *from)
1365 {
1366 DBUG_ENTER("Field::pack");
1367 uchar *result= this->pack(to, from, UINT_MAX);
1368 DBUG_RETURN(result);
1369 }
1370
1371 virtual const uchar *unpack(uchar* to, const uchar *from,
1372 const uchar *from_end, uint param_data=0);
1373
packed_col_length(const uchar * to,uint length)1374 virtual uint packed_col_length(const uchar *to, uint length)
1375 { return length;}
max_packed_col_length(uint max_length)1376 virtual uint max_packed_col_length(uint max_length)
1377 { return max_length;}
1378
offset(uchar * record)1379 uint offset(uchar *record) const
1380 {
1381 return (uint) (ptr - record);
1382 }
1383 void copy_from_tmp(int offset);
1384 uint fill_cache_field(struct st_cache_field *copy);
1385 virtual bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
get_time(MYSQL_TIME * ltime)1386 bool get_time(MYSQL_TIME *ltime) { return get_date(ltime, TIME_TIME_ONLY); }
get_typelib()1387 virtual TYPELIB *get_typelib() const { return NULL; }
charset(void)1388 virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
charset_for_protocol(void)1389 virtual CHARSET_INFO *charset_for_protocol(void) const
1390 { return binary() ? &my_charset_bin : charset(); }
sort_charset(void)1391 virtual CHARSET_INFO *sort_charset(void) const { return charset(); }
has_charset(void)1392 virtual bool has_charset(void) const { return FALSE; }
derivation(void)1393 virtual enum Derivation derivation(void) const
1394 { return DERIVATION_IMPLICIT; }
repertoire(void)1395 virtual uint repertoire(void) const { return MY_REPERTOIRE_UNICODE30; }
set_time()1396 virtual int set_time() { return 1; }
1397 bool set_warning(Sql_condition::enum_warning_level, unsigned int code,
1398 int cuted_increment, ulong current_row=0) const;
1399 protected:
set_warning(unsigned int code,int cuted_increment)1400 bool set_warning(unsigned int code, int cuted_increment) const
1401 {
1402 return set_warning(Sql_condition::WARN_LEVEL_WARN, code, cuted_increment);
1403 }
set_note(unsigned int code,int cuted_increment)1404 bool set_note(unsigned int code, int cuted_increment) const
1405 {
1406 return set_warning(Sql_condition::WARN_LEVEL_NOTE, code, cuted_increment);
1407 }
1408 void set_datetime_warning(Sql_condition::enum_warning_level, uint code,
1409 const ErrConv *str, timestamp_type ts_type,
1410 int cuted_increment) const;
set_datetime_warning(uint code,const ErrConv * str,timestamp_type ts_type,int cuted_increment)1411 void set_datetime_warning(uint code,
1412 const ErrConv *str, timestamp_type ts_type,
1413 int cuted_increment) const
1414 {
1415 set_datetime_warning(Sql_condition::WARN_LEVEL_WARN, code, str, ts_type,
1416 cuted_increment);
1417 }
1418 void set_warning_truncated_wrong_value(const char *type, const char *value);
check_overflow(int op_result)1419 inline bool check_overflow(int op_result)
1420 {
1421 return (op_result == E_DEC_OVERFLOW);
1422 }
1423 int warn_if_overflow(int op_result);
1424 Copy_func *get_identical_copy_func() const;
1425 public:
set_table_name(String * alias)1426 void set_table_name(String *alias)
1427 {
1428 table_name= &alias->Ptr;
1429 }
init(TABLE * table_arg)1430 void init(TABLE *table_arg)
1431 {
1432 orig_table= table= table_arg;
1433 set_table_name(&table_arg->alias);
1434 }
init_for_make_new_field(TABLE * new_table_arg,TABLE * orig_table_arg)1435 void init_for_make_new_field(TABLE *new_table_arg, TABLE *orig_table_arg)
1436 {
1437 init(new_table_arg);
1438 /*
1439 Normally orig_table is different from table only if field was
1440 created via ::make_new_field. Here we alter the type of field,
1441 so ::make_new_field is not applicable. But we still need to
1442 preserve the original field metadata for the client-server
1443 protocol.
1444 */
1445 orig_table= orig_table_arg;
1446 }
1447
1448 /* maximum possible display length */
1449 virtual uint32 max_display_length() const= 0;
1450 /**
1451 Whether a field being created is compatible with a existing one.
1452
1453 Used by the ALTER TABLE code to evaluate whether the new definition
1454 of a table is compatible with the old definition so that it can
1455 determine if data needs to be copied over (table data change).
1456 */
1457 virtual uint is_equal(Create_field *new_field);
1458 /* convert decimal to longlong with overflow check */
1459 longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
1460 int *err);
1461 /* The max. number of characters */
char_length()1462 virtual uint32 char_length() const
1463 {
1464 return field_length / charset()->mbmaxlen;
1465 }
1466
get_geometry_type()1467 virtual geometry_type get_geometry_type()
1468 {
1469 /* shouldn't get here. */
1470 DBUG_ASSERT(0);
1471 return GEOM_GEOMETRY;
1472 }
1473
field_storage_type()1474 ha_storage_media field_storage_type() const
1475 {
1476 return (ha_storage_media)
1477 ((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
1478 }
1479
set_storage_type(ha_storage_media storage_type_arg)1480 void set_storage_type(ha_storage_media storage_type_arg)
1481 {
1482 DBUG_ASSERT(field_storage_type() == HA_SM_DEFAULT);
1483 flags |= static_cast<uint32>(storage_type_arg) <<
1484 FIELD_FLAGS_STORAGE_MEDIA;
1485 }
1486
column_format()1487 column_format_type column_format() const
1488 {
1489 return (column_format_type)
1490 ((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
1491 }
1492
set_column_format(column_format_type column_format_arg)1493 void set_column_format(column_format_type column_format_arg)
1494 {
1495 DBUG_ASSERT(column_format() == COLUMN_FORMAT_TYPE_DEFAULT);
1496 flags |= static_cast<uint32>(column_format_arg) <<
1497 FIELD_FLAGS_COLUMN_FORMAT;
1498 }
1499
vers_sys_field()1500 bool vers_sys_field() const
1501 {
1502 return flags & (VERS_ROW_START | VERS_ROW_END);
1503 }
1504
vers_update_unversioned()1505 bool vers_update_unversioned() const
1506 {
1507 return flags & VERS_UPDATE_UNVERSIONED_FLAG;
1508 }
1509
1510 /*
1511 Validate a non-null field value stored in the given record
1512 according to the current thread settings, e.g. sql_mode.
1513 @param thd - the thread
1514 @param record - the record to check in
1515 */
validate_value_in_record(THD * thd,const uchar * record)1516 virtual bool validate_value_in_record(THD *thd, const uchar *record) const
1517 { return false; }
1518 bool validate_value_in_record_with_warn(THD *thd, const uchar *record);
1519 key_map get_possible_keys();
1520
1521 /* Hash value */
1522 virtual void hash(ulong *nr, ulong *nr2);
1523
1524 /**
1525 Get the upper limit of the MySQL integral and floating-point type.
1526
1527 @return maximum allowed value for the field
1528 */
get_max_int_value()1529 virtual ulonglong get_max_int_value() const
1530 {
1531 DBUG_ASSERT(false);
1532 return 0ULL;
1533 }
1534
1535 /**
1536 Checks whether a string field is part of write_set.
1537
1538 @return
1539 FALSE - If field is not char/varchar/....
1540 - If field is char/varchar/.. and is not part of write set.
1541 TRUE - If field is char/varchar/.. and is part of write set.
1542 */
is_varchar_and_in_write_set()1543 virtual bool is_varchar_and_in_write_set() const { return FALSE; }
1544
1545 /* Check whether the field can be used as a join attribute in hash join */
hash_join_is_possible()1546 virtual bool hash_join_is_possible() { return TRUE; }
eq_cmp_as_binary()1547 virtual bool eq_cmp_as_binary() { return TRUE; }
1548
1549 /* Position of the field value within the interval of [min, max] */
pos_in_interval(Field * min,Field * max)1550 virtual double pos_in_interval(Field *min, Field *max)
1551 {
1552 return (double) 0.5;
1553 }
1554
1555 /*
1556 Check if comparison between the field and an item unambiguously
1557 identifies a distinct field value.
1558
1559 Example1: SELECT * FROM t1 WHERE int_column=10;
1560 This example returns distinct integer value of 10.
1561
1562 Example2: SELECT * FROM t1 WHERE varchar_column=DATE'2001-01-01'
1563 This example returns non-distinct values.
1564 Comparison as DATE will return '2001-01-01' and '2001-01-01x',
1565 but these two values are not equal to each other as VARCHARs.
1566 See also the function with the same name in sql_select.cc.
1567 */
1568 virtual bool test_if_equality_guarantees_uniqueness(const Item *const_item)
1569 const;
1570 virtual bool can_be_substituted_to_equal_item(const Context &ctx,
1571 const Item_equal *item);
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)1572 virtual Item *get_equal_const_item(THD *thd, const Context &ctx,
1573 Item *const_item)
1574 {
1575 return const_item;
1576 }
1577 virtual bool can_optimize_keypart_ref(const Item_bool_func *cond,
1578 const Item *item) const;
can_optimize_hash_join(const Item_bool_func * cond,const Item * item)1579 virtual bool can_optimize_hash_join(const Item_bool_func *cond,
1580 const Item *item) const
1581 {
1582 return can_optimize_keypart_ref(cond, item);
1583 }
1584 virtual bool can_optimize_group_min_max(const Item_bool_func *cond,
1585 const Item *const_item) const;
1586 /**
1587 Test if Field can use range optimizer for a standard comparison operation:
1588 <=, <, =, <=>, >, >=
1589 Note, this method does not cover spatial operations.
1590 */
1591 virtual bool can_optimize_range(const Item_bool_func *cond,
1592 const Item *item,
1593 bool is_eq_func) const;
1594
can_optimize_outer_join_table_elimination(const Item_bool_func * cond,const Item * item)1595 bool can_optimize_outer_join_table_elimination(const Item_bool_func *cond,
1596 const Item *item) const
1597 {
1598 // Exactly the same rules with REF access
1599 return can_optimize_keypart_ref(cond, item);
1600 }
1601
1602 bool save_in_field_default_value(bool view_eror_processing);
1603 bool save_in_field_ignore_value(bool view_error_processing);
1604
1605 /* Mark field in read map. Updates also virtual fields */
1606 void register_field_in_read_map();
1607
compression_method()1608 virtual Compression_method *compression_method() const { return 0; }
1609
virtual_tmp_table_addr()1610 virtual Virtual_tmp_table **virtual_tmp_table_addr()
1611 {
1612 return NULL;
1613 }
1614 virtual bool sp_prepare_and_store_item(THD *thd, Item **value);
1615
1616 friend int cre_myisam(char * name, TABLE *form, uint options,
1617 ulonglong auto_increment_value);
1618 friend class Copy_field;
1619 friend class Item_avg_field;
1620 friend class Item_std_field;
1621 friend class Item_sum_num;
1622 friend class Item_sum_sum;
1623 friend class Item_sum_count;
1624 friend class Item_sum_avg;
1625 friend class Item_sum_std;
1626 friend class Item_sum_min;
1627 friend class Item_sum_max;
1628 friend class Item_func_group_concat;
1629
1630 private:
1631 /*
1632 Primitive for implementing last_null_byte().
1633
1634 SYNOPSIS
1635 do_last_null_byte()
1636
1637 DESCRIPTION
1638 Primitive for the implementation of the last_null_byte()
1639 function. This represents the inheritance interface and can be
1640 overridden by subclasses.
1641 */
1642 virtual size_t do_last_null_byte() const;
1643
1644 protected:
pack_int(uchar * to,const uchar * from,size_t size)1645 uchar *pack_int(uchar *to, const uchar *from, size_t size)
1646 {
1647 memcpy(to, from, size);
1648 return to + size;
1649 }
1650
unpack_int(uchar * to,const uchar * from,const uchar * from_end,size_t size)1651 const uchar *unpack_int(uchar* to, const uchar *from,
1652 const uchar *from_end, size_t size)
1653 {
1654 if (from + size > from_end)
1655 return 0;
1656 memcpy(to, from, size);
1657 return from + size;
1658 }
1659
pack_int16(uchar * to,const uchar * from)1660 uchar *pack_int16(uchar *to, const uchar *from)
1661 { return pack_int(to, from, 2); }
unpack_int16(uchar * to,const uchar * from,const uchar * from_end)1662 const uchar *unpack_int16(uchar* to, const uchar *from, const uchar *from_end)
1663 { return unpack_int(to, from, from_end, 2); }
pack_int24(uchar * to,const uchar * from)1664 uchar *pack_int24(uchar *to, const uchar *from)
1665 { return pack_int(to, from, 3); }
unpack_int24(uchar * to,const uchar * from,const uchar * from_end)1666 const uchar *unpack_int24(uchar* to, const uchar *from, const uchar *from_end)
1667 { return unpack_int(to, from, from_end, 3); }
pack_int32(uchar * to,const uchar * from)1668 uchar *pack_int32(uchar *to, const uchar *from)
1669 { return pack_int(to, from, 4); }
unpack_int32(uchar * to,const uchar * from,const uchar * from_end)1670 const uchar *unpack_int32(uchar* to, const uchar *from, const uchar *from_end)
1671 { return unpack_int(to, from, from_end, 4); }
pack_int64(uchar * to,const uchar * from)1672 uchar *pack_int64(uchar* to, const uchar *from)
1673 { return pack_int(to, from, 8); }
unpack_int64(uchar * to,const uchar * from,const uchar * from_end)1674 const uchar *unpack_int64(uchar* to, const uchar *from, const uchar *from_end)
1675 { return unpack_int(to, from, from_end, 8); }
1676
1677 double pos_in_interval_val_real(Field *min, Field *max);
1678 double pos_in_interval_val_str(Field *min, Field *max, uint data_offset);
1679 };
1680
1681
1682 class Field_num :public Field {
1683 protected:
1684 int check_edom_and_important_data_truncation(const char *type, bool edom,
1685 CHARSET_INFO *cs,
1686 const char *str, size_t length,
1687 const char *end_of_num);
1688 int check_edom_and_truncation(const char *type, bool edom,
1689 CHARSET_INFO *cs,
1690 const char *str, size_t length,
1691 const char *end_of_num);
check_int(CHARSET_INFO * cs,const char * str,size_t length,const char * int_end,int error)1692 int check_int(CHARSET_INFO *cs, const char *str, size_t length,
1693 const char *int_end, int error)
1694 {
1695 return check_edom_and_truncation("integer",
1696 error == MY_ERRNO_EDOM || str == int_end,
1697 cs, str, length, int_end);
1698 }
1699 bool get_int(CHARSET_INFO *cs, const char *from, size_t len,
1700 longlong *rnd, ulonglong unsigned_max,
1701 longlong signed_min, longlong signed_max);
1702 void prepend_zeros(String *value) const;
1703 Item *get_equal_zerofill_const_item(THD *thd, const Context &ctx,
1704 Item *const_item);
1705 public:
1706 const uint8 dec;
1707 bool zerofill,unsigned_flag; // Purify cannot handle bit fields
1708 Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1709 uchar null_bit_arg, utype unireg_check_arg,
1710 const LEX_CSTRING *field_name_arg,
1711 uint8 dec_arg, bool zero_arg, bool unsigned_arg);
derivation(void)1712 enum Derivation derivation(void) const { return DERIVATION_NUMERIC; }
repertoire(void)1713 uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; }
charset(void)1714 CHARSET_INFO *charset(void) const { return &my_charset_numeric; }
1715 sql_mode_t can_handle_sql_mode_dependency_on_store() const;
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)1716 Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item)
1717 {
1718 return (flags & ZEROFILL_FLAG) ?
1719 get_equal_zerofill_const_item(thd, ctx, const_item) :
1720 const_item;
1721 }
1722 void add_zerofill_and_unsigned(String &res) const;
1723 friend class Create_field;
1724 void make_send_field(Send_field *);
decimals()1725 uint decimals() const { return (uint) dec; }
size_of()1726 uint size_of() const { return sizeof(*this); }
1727 bool eq_def(const Field *field) const;
get_copy_func(const Field * from)1728 Copy_func *get_copy_func(const Field *from) const
1729 {
1730 if (unsigned_flag && from->cmp_type() == DECIMAL_RESULT)
1731 return do_field_decimal;
1732 return do_field_int;
1733 }
save_in_field(Field * to)1734 int save_in_field(Field *to)
1735 {
1736 return to->store(val_int(), MY_TEST(flags & UNSIGNED_FLAG));
1737 }
1738 uint is_equal(Create_field *new_field);
row_pack_length()1739 uint row_pack_length() const { return pack_length(); }
pack_length_from_metadata(uint field_metadata)1740 uint32 pack_length_from_metadata(uint field_metadata) {
1741 uint32 length= pack_length();
1742 DBUG_PRINT("result", ("pack_length_from_metadata(%d): %u",
1743 field_metadata, length));
1744 return length;
1745 }
pos_in_interval(Field * min,Field * max)1746 double pos_in_interval(Field *min, Field *max)
1747 {
1748 return pos_in_interval_val_real(min, max);
1749 }
1750 };
1751
1752
1753 class Field_str :public Field {
1754 protected:
1755 // TODO-10.2: Reuse DTCollation instead of these three members
1756 CHARSET_INFO *field_charset;
1757 enum Derivation field_derivation;
1758 uint field_repertoire;
1759 public:
1760 bool can_be_substituted_to_equal_item(const Context &ctx,
1761 const Item_equal *item_equal);
1762 Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1763 uchar null_bit_arg, utype unireg_check_arg,
1764 const LEX_CSTRING *field_name_arg,
1765 const DTCollation &collation);
decimals()1766 uint decimals() const { return is_created_from_null_item ? 0 : NOT_FIXED_DEC; }
save_in_field(Field * to)1767 int save_in_field(Field *to) { return save_in_field_str(to); }
memcpy_field_possible(const Field * from)1768 bool memcpy_field_possible(const Field *from) const
1769 {
1770 return real_type() == from->real_type() &&
1771 pack_length() == from->pack_length() &&
1772 charset() == from->charset();
1773 }
1774 int store(double nr);
1775 int store(longlong nr, bool unsigned_val);
1776 int store_decimal(const my_decimal *);
1777 int store(const char *to,size_t length,CHARSET_INFO *cs)=0;
store_hex_hybrid(const char * str,size_t length)1778 int store_hex_hybrid(const char *str, size_t length)
1779 {
1780 return store(str, length, &my_charset_bin);
1781 }
repertoire(void)1782 uint repertoire(void) const { return field_repertoire; }
charset(void)1783 CHARSET_INFO *charset(void) const { return field_charset; }
derivation(void)1784 enum Derivation derivation(void) const { return field_derivation; }
binary()1785 bool binary() const { return field_charset == &my_charset_bin; }
max_display_length()1786 uint32 max_display_length() const { return field_length; }
char_length()1787 uint32 char_length() const { return field_length / field_charset->mbmaxlen; }
1788 Information_schema_character_attributes
information_schema_character_attributes()1789 information_schema_character_attributes() const
1790 {
1791 return Information_schema_character_attributes(max_display_length(),
1792 char_length());
1793 }
1794 friend class Create_field;
1795 my_decimal *val_decimal(my_decimal *);
val_bool()1796 bool val_bool() { return val_real() != 0e0; }
str_needs_quotes()1797 virtual bool str_needs_quotes() { return TRUE; }
1798 uint is_equal(Create_field *new_field);
eq_cmp_as_binary()1799 bool eq_cmp_as_binary() { return MY_TEST(flags & BINARY_FLAG); }
length_size()1800 virtual uint length_size() { return 0; }
pos_in_interval(Field * min,Field * max)1801 double pos_in_interval(Field *min, Field *max)
1802 {
1803 return pos_in_interval_val_str(min, max, length_size());
1804 }
1805 bool test_if_equality_guarantees_uniqueness(const Item *const_item) const;
1806 };
1807
1808 /* base class for Field_string, Field_varstring and Field_blob */
1809
1810 class Field_longstr :public Field_str
1811 {
1812 protected:
1813 int report_if_important_data(const char *ptr, const char *end,
1814 bool count_spaces);
1815 bool check_string_copy_error(const String_copier *copier,
1816 const char *end, CHARSET_INFO *cs);
check_conversion_status(const String_copier * copier,const char * end,CHARSET_INFO * cs,bool count_spaces)1817 int check_conversion_status(const String_copier *copier,
1818 const char *end, CHARSET_INFO *cs,
1819 bool count_spaces)
1820 {
1821 if (check_string_copy_error(copier, end, cs))
1822 return 2;
1823 return report_if_important_data(copier->source_end_pos(),
1824 end, count_spaces);
1825 }
well_formed_copy_with_check(char * to,size_t to_length,CHARSET_INFO * from_cs,const char * from,size_t from_length,size_t nchars,bool count_spaces,uint * copy_length)1826 int well_formed_copy_with_check(char *to, size_t to_length,
1827 CHARSET_INFO *from_cs,
1828 const char *from, size_t from_length,
1829 size_t nchars, bool count_spaces,
1830 uint *copy_length)
1831 {
1832 String_copier copier;
1833
1834 *copy_length= copier.well_formed_copy(field_charset, to, to_length,
1835 from_cs, from, from_length,
1836 nchars);
1837
1838 return check_conversion_status(&copier, from + from_length, from_cs, count_spaces);
1839 }
1840 bool cmp_to_string_with_same_collation(const Item_bool_func *cond,
1841 const Item *item) const;
1842 bool cmp_to_string_with_stricter_collation(const Item_bool_func *cond,
1843 const Item *item) const;
1844 int compress(char *to, uint to_length,
1845 const char *from, uint length,
1846 uint max_length,
1847 uint *out_length,
1848 CHARSET_INFO *cs, size_t nchars);
1849 String *uncompress(String *val_buffer, String *val_ptr,
1850 const uchar *from, uint from_length);
1851 public:
Field_longstr(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)1852 Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1853 uchar null_bit_arg, utype unireg_check_arg,
1854 const LEX_CSTRING *field_name_arg,
1855 const DTCollation &collation)
1856 :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
1857 field_name_arg, collation)
1858 {}
1859
1860 int store_decimal(const my_decimal *d);
1861 uint32 max_data_length() const;
1862
is_varchar_and_in_write_set()1863 bool is_varchar_and_in_write_set() const
1864 {
1865 DBUG_ASSERT(table && table->write_set);
1866 return bitmap_is_set(table->write_set, field_index);
1867 }
match_collation_to_optimize_range()1868 bool match_collation_to_optimize_range() const { return true; }
1869
1870 bool can_optimize_keypart_ref(const Item_bool_func *cond,
1871 const Item *item) const;
1872 bool can_optimize_hash_join(const Item_bool_func *cond,
1873 const Item *item) const;
1874 bool can_optimize_group_min_max(const Item_bool_func *cond,
1875 const Item *const_item) const;
1876 bool can_optimize_range(const Item_bool_func *cond,
1877 const Item *item,
1878 bool is_eq_func) const;
1879 };
1880
1881 /* base class for float and double and decimal (old one) */
1882 class Field_real :public Field_num {
1883 protected:
1884 double get_double(const char *str, size_t length, CHARSET_INFO *cs, int *err);
1885 public:
1886 bool not_fixed;
1887
Field_real(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)1888 Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1889 uchar null_bit_arg, utype unireg_check_arg,
1890 const LEX_CSTRING *field_name_arg,
1891 uint8 dec_arg, bool zero_arg, bool unsigned_arg)
1892 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
1893 field_name_arg, dec_arg, zero_arg, unsigned_arg),
1894 not_fixed(dec_arg >= FLOATING_POINT_DECIMALS)
1895 {}
get_copy_func(const Field * from)1896 Copy_func *get_copy_func(const Field *from) const
1897 {
1898 return do_field_real;
1899 }
1900 Information_schema_numeric_attributes
information_schema_numeric_attributes()1901 information_schema_numeric_attributes() const
1902 {
1903 return dec == NOT_FIXED_DEC ?
1904 Information_schema_numeric_attributes(field_length) :
1905 Information_schema_numeric_attributes(field_length, dec);
1906 }
save_in_field(Field * to)1907 int save_in_field(Field *to) { return to->store(val_real()); }
memcpy_field_possible(const Field * from)1908 bool memcpy_field_possible(const Field *from) const
1909 {
1910 /*
1911 Cannot do memcpy from a longer field to a shorter field,
1912 e.g. a DOUBLE(53,10) into a DOUBLE(10,10).
1913 But it should be OK the other way around.
1914 */
1915 return real_type() == from->real_type() &&
1916 pack_length() == from->pack_length() &&
1917 is_unsigned() <= from->is_unsigned() &&
1918 decimals() == from->decimals() &&
1919 field_length >= from->field_length;
1920 }
1921 int store_decimal(const my_decimal *);
1922 int store_time_dec(const MYSQL_TIME *ltime, uint dec);
1923 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
1924 my_decimal *val_decimal(my_decimal *);
val_bool()1925 bool val_bool() { return val_real() != 0e0; }
max_display_length()1926 uint32 max_display_length() const { return field_length; }
size_of()1927 uint size_of() const { return sizeof(*this); }
1928 Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item);
1929 };
1930
1931
1932 class Field_decimal :public Field_real {
1933 public:
Field_decimal(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)1934 Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1935 uchar null_bit_arg,
1936 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
1937 uint8 dec_arg,bool zero_arg,bool unsigned_arg)
1938 :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1939 unireg_check_arg, field_name_arg,
1940 dec_arg, zero_arg, unsigned_arg)
1941 {}
1942 Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
type_handler()1943 const Type_handler *type_handler() const { return &type_handler_olddecimal; }
key_type()1944 enum ha_base_keytype key_type() const
1945 { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
1946 Information_schema_numeric_attributes
information_schema_numeric_attributes()1947 information_schema_numeric_attributes() const
1948 {
1949 uint tmp= dec ? 2 : 1; // The sign and the decimal point
1950 return Information_schema_numeric_attributes(field_length - tmp, dec);
1951 }
get_copy_func(const Field * from)1952 Copy_func *get_copy_func(const Field *from) const
1953 {
1954 return eq_def(from) ? get_identical_copy_func() : do_field_string;
1955 }
1956 int reset(void);
1957 int store(const char *to,size_t length,CHARSET_INFO *charset);
1958 int store(double nr);
1959 int store(longlong nr, bool unsigned_val);
1960 double val_real(void);
1961 longlong val_int(void);
1962 String *val_str(String*,String *);
1963 int cmp(const uchar *,const uchar *);
1964 void sort_string(uchar *buff,uint length);
1965 void overflow(bool negative);
zero_pack()1966 bool zero_pack() const { return 0; }
1967 void sql_type(String &str) const;
pack(uchar * to,const uchar * from,uint max_length)1968 virtual uchar *pack(uchar* to, const uchar *from, uint max_length)
1969 {
1970 return Field::pack(to, from, max_length);
1971 }
1972 };
1973
1974
1975 /* New decimal/numeric field which use fixed point arithmetic */
1976 class Field_new_decimal :public Field_num {
1977 private:
1978 int save_field_metadata(uchar *first_byte);
1979 public:
1980 /* The maximum number of decimal digits can be stored */
1981 uint precision;
1982 uint bin_size;
1983 /*
1984 Constructors take max_length of the field as a parameter - not the
1985 precision as the number of decimal digits allowed.
1986 So for example we need to count length from precision handling
1987 CREATE TABLE ( DECIMAL(x,y))
1988 */
1989 Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1990 uchar null_bit_arg,
1991 enum utype unireg_check_arg,
1992 const LEX_CSTRING *field_name_arg,
1993 uint8 dec_arg, bool zero_arg, bool unsigned_arg);
type_handler()1994 const Type_handler *type_handler() const { return &type_handler_newdecimal; }
key_type()1995 enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
get_copy_func(const Field * from)1996 Copy_func *get_copy_func(const Field *from) const
1997 {
1998 // if (from->real_type() == MYSQL_TYPE_BIT) // QQ: why?
1999 // return do_field_int;
2000 return do_field_decimal;
2001 }
save_in_field(Field * to)2002 int save_in_field(Field *to)
2003 {
2004 my_decimal buff;
2005 return to->store_decimal(val_decimal(&buff));
2006 }
memcpy_field_possible(const Field * from)2007 bool memcpy_field_possible(const Field *from) const
2008 {
2009 return real_type() == from->real_type() &&
2010 pack_length() == from->pack_length() &&
2011 is_unsigned() <= from->is_unsigned() &&
2012 decimals() == from->decimals() &&
2013 field_length == from->field_length;
2014 }
2015 int reset(void);
2016 bool store_value(const my_decimal *decimal_value);
2017 bool store_value(const my_decimal *decimal_value, int *native_error);
2018 void set_value_on_overflow(my_decimal *decimal_value, bool sign);
2019 int store(const char *to, size_t length, CHARSET_INFO *charset);
2020 int store(double nr);
2021 int store(longlong nr, bool unsigned_val);
2022 int store_time_dec(const MYSQL_TIME *ltime, uint dec);
2023 int store_decimal(const my_decimal *);
2024 double val_real(void);
2025 longlong val_int(void);
2026 ulonglong val_uint(void);
2027 my_decimal *val_decimal(my_decimal *);
2028 String *val_str(String*, String *);
2029 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
val_bool()2030 bool val_bool()
2031 {
2032 my_decimal decimal_value;
2033 my_decimal *val= val_decimal(&decimal_value);
2034 return val ? !my_decimal_is_zero(val) : 0;
2035 }
2036 int cmp(const uchar *, const uchar *);
2037 void sort_string(uchar *buff, uint length);
zero_pack()2038 bool zero_pack() const { return 0; }
2039 void sql_type(String &str) const;
max_display_length()2040 uint32 max_display_length() const { return field_length; }
2041 Information_schema_numeric_attributes
information_schema_numeric_attributes()2042 information_schema_numeric_attributes() const
2043 {
2044 return Information_schema_numeric_attributes(precision, dec);
2045 }
size_of()2046 uint size_of() const { return sizeof(*this); }
pack_length()2047 uint32 pack_length() const { return (uint32) bin_size; }
2048 uint pack_length_from_metadata(uint field_metadata);
row_pack_length()2049 uint row_pack_length() const { return pack_length(); }
2050 bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
2051 uint16 mflags, int *order_var);
2052 uint is_equal(Create_field *new_field);
2053 virtual const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end, uint param_data);
2054 Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item);
2055 };
2056
2057
2058 class Field_int :public Field_num
2059 {
2060 protected:
2061 String *val_str_from_long(String *val_buffer, uint max_char_length,
2062 int radix, long nr);
2063 public:
Field_int(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2064 Field_int(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2065 uchar null_bit_arg, enum utype unireg_check_arg,
2066 const LEX_CSTRING *field_name_arg, bool zero_arg, bool unsigned_arg)
2067 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2068 unireg_check_arg, field_name_arg, 0, zero_arg, unsigned_arg)
2069 {}
memcpy_field_possible(const Field * from)2070 bool memcpy_field_possible(const Field *from) const
2071 {
2072 return real_type() == from->real_type() &&
2073 pack_length() == from->pack_length() &&
2074 is_unsigned() == from->is_unsigned();
2075 }
2076 int store_decimal(const my_decimal *);
2077 my_decimal *val_decimal(my_decimal *);
val_bool()2078 bool val_bool() { return val_int() != 0; }
val_uint()2079 ulonglong val_uint()
2080 {
2081 longlong nr= val_int();
2082 return nr < 0 && !unsigned_flag ? 0 : (ulonglong) nr;
2083 }
2084 int store_time_dec(const MYSQL_TIME *ltime, uint dec);
2085 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
2086 virtual const Type_limits_int *type_limits_int() const= 0;
max_display_length()2087 uint32 max_display_length() const
2088 {
2089 return type_limits_int()->char_length();
2090 }
type_std_attributes()2091 Type_std_attributes type_std_attributes() const
2092 {
2093 /*
2094 For integer data types, the user-specified length does not constrain the
2095 supported range, so e.g. a column of the INT(1) data type supports the
2096 full integer range anyway.
2097 Choose the maximum from the user-specified length and the maximum
2098 possible length determined by the data type capacity:
2099 INT(1) -> 11
2100 INT(10) -> 11
2101 INT(40) -> 40
2102 */
2103 uint32 length1= max_display_length();
2104 uint32 length2= field_length;
2105 return Type_std_attributes(MY_MAX(length1, length2), decimals(),
2106 MY_TEST(flags & UNSIGNED_FLAG),
2107 dtcollation());
2108 }
2109 Information_schema_numeric_attributes
information_schema_numeric_attributes()2110 information_schema_numeric_attributes() const
2111 {
2112 uint32 prec= type_limits_int()->precision();
2113 return Information_schema_numeric_attributes(prec, 0);
2114 }
2115 };
2116
2117
2118 class Field_tiny :public Field_int
2119 {
2120 public:
Field_tiny(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2121 Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2122 uchar null_bit_arg,
2123 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2124 bool zero_arg, bool unsigned_arg)
2125 :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2126 unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2127 {}
type_handler()2128 const Type_handler *type_handler() const { return &type_handler_tiny; }
key_type()2129 enum ha_base_keytype key_type() const
2130 { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
2131 int store(const char *to,size_t length,CHARSET_INFO *charset);
2132 int store(double nr);
2133 int store(longlong nr, bool unsigned_val);
reset(void)2134 int reset(void) { ptr[0]=0; return 0; }
2135 double val_real(void);
2136 longlong val_int(void);
2137 String *val_str(String*,String *);
2138 bool send_binary(Protocol *protocol);
2139 int cmp(const uchar *,const uchar *);
2140 void sort_string(uchar *buff,uint length);
pack_length()2141 uint32 pack_length() const { return 1; }
2142 void sql_type(String &str) const;
type_limits_int()2143 const Type_limits_int *type_limits_int() const
2144 {
2145 return type_handler_tiny.type_limits_int_by_unsigned_flag(is_unsigned());
2146 }
2147
pack(uchar * to,const uchar * from,uint max_length)2148 virtual uchar *pack(uchar* to, const uchar *from, uint max_length)
2149 {
2150 *to= *from;
2151 return to + 1;
2152 }
2153
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2154 virtual const uchar *unpack(uchar* to, const uchar *from,
2155 const uchar *from_end, uint param_data)
2156 {
2157 if (from == from_end)
2158 return 0;
2159 *to= *from;
2160 return from + 1;
2161 }
get_max_int_value()2162 virtual ulonglong get_max_int_value() const
2163 {
2164 return unsigned_flag ? 0xFFULL : 0x7FULL;
2165 }
2166 };
2167
2168
2169 class Field_short :public Field_int
2170 {
2171 public:
Field_short(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2172 Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2173 uchar null_bit_arg,
2174 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2175 bool zero_arg, bool unsigned_arg)
2176 :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2177 unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2178 {}
Field_short(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,bool unsigned_arg)2179 Field_short(uint32 len_arg,bool maybe_null_arg,
2180 const LEX_CSTRING *field_name_arg,
2181 bool unsigned_arg)
2182 :Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2183 NONE, field_name_arg, 0, unsigned_arg)
2184 {}
type_handler()2185 const Type_handler *type_handler() const { return &type_handler_short; }
key_type()2186 enum ha_base_keytype key_type() const
2187 { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
2188 int store(const char *to,size_t length,CHARSET_INFO *charset);
2189 int store(double nr);
2190 int store(longlong nr, bool unsigned_val);
reset(void)2191 int reset(void) { ptr[0]=ptr[1]=0; return 0; }
2192 double val_real(void);
2193 longlong val_int(void);
2194 String *val_str(String*,String *);
2195 bool send_binary(Protocol *protocol);
2196 int cmp(const uchar *,const uchar *);
2197 void sort_string(uchar *buff,uint length);
pack_length()2198 uint32 pack_length() const { return 2; }
2199 void sql_type(String &str) const;
type_limits_int()2200 const Type_limits_int *type_limits_int() const
2201 {
2202 return type_handler_short.type_limits_int_by_unsigned_flag(is_unsigned());
2203 }
pack(uchar * to,const uchar * from,uint max_length)2204 virtual uchar *pack(uchar* to, const uchar *from, uint max_length)
2205 { return pack_int16(to, from); }
2206
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2207 virtual const uchar *unpack(uchar* to, const uchar *from,
2208 const uchar *from_end, uint param_data)
2209 { return unpack_int16(to, from, from_end); }
get_max_int_value()2210 virtual ulonglong get_max_int_value() const
2211 {
2212 return unsigned_flag ? 0xFFFFULL : 0x7FFFULL;
2213 }
2214 };
2215
2216 class Field_medium :public Field_int
2217 {
2218 public:
Field_medium(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2219 Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2220 uchar null_bit_arg,
2221 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2222 bool zero_arg, bool unsigned_arg)
2223 :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2224 unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2225 {}
type_handler()2226 const Type_handler *type_handler() const { return &type_handler_int24; }
key_type()2227 enum ha_base_keytype key_type() const
2228 { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
2229 int store(const char *to,size_t length,CHARSET_INFO *charset);
2230 int store(double nr);
2231 int store(longlong nr, bool unsigned_val);
reset(void)2232 int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
2233 double val_real(void);
2234 longlong val_int(void);
2235 String *val_str(String*,String *);
2236 bool send_binary(Protocol *protocol);
2237 int cmp(const uchar *,const uchar *);
2238 void sort_string(uchar *buff,uint length);
pack_length()2239 uint32 pack_length() const { return 3; }
2240 void sql_type(String &str) const;
type_limits_int()2241 const Type_limits_int *type_limits_int() const
2242 {
2243 return type_handler_int24.type_limits_int_by_unsigned_flag(is_unsigned());
2244 }
pack(uchar * to,const uchar * from,uint max_length)2245 virtual uchar *pack(uchar* to, const uchar *from, uint max_length)
2246 {
2247 return Field::pack(to, from, max_length);
2248 }
get_max_int_value()2249 virtual ulonglong get_max_int_value() const
2250 {
2251 return unsigned_flag ? 0xFFFFFFULL : 0x7FFFFFULL;
2252 }
2253 };
2254
2255
2256 class Field_long :public Field_int
2257 {
2258 public:
Field_long(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2259 Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2260 uchar null_bit_arg,
2261 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2262 bool zero_arg, bool unsigned_arg)
2263 :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2264 unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2265 {}
Field_long(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,bool unsigned_arg)2266 Field_long(uint32 len_arg,bool maybe_null_arg,
2267 const LEX_CSTRING *field_name_arg,
2268 bool unsigned_arg)
2269 :Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2270 NONE, field_name_arg, 0, unsigned_arg)
2271 {}
type_handler()2272 const Type_handler *type_handler() const { return &type_handler_long; }
key_type()2273 enum ha_base_keytype key_type() const
2274 { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
2275 int store(const char *to,size_t length,CHARSET_INFO *charset);
2276 int store(double nr);
2277 int store(longlong nr, bool unsigned_val);
reset(void)2278 int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
2279 double val_real(void);
2280 longlong val_int(void);
2281 bool send_binary(Protocol *protocol);
2282 String *val_str(String*,String *);
2283 int cmp(const uchar *,const uchar *);
2284 void sort_string(uchar *buff,uint length);
pack_length()2285 uint32 pack_length() const { return 4; }
2286 void sql_type(String &str) const;
type_limits_int()2287 const Type_limits_int *type_limits_int() const
2288 {
2289 return type_handler_long.type_limits_int_by_unsigned_flag(is_unsigned());
2290 }
pack(uchar * to,const uchar * from,uint max_length)2291 virtual uchar *pack(uchar* to, const uchar *from,
2292 uint max_length __attribute__((unused)))
2293 {
2294 return pack_int32(to, from);
2295 }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2296 virtual const uchar *unpack(uchar* to, const uchar *from,
2297 const uchar *from_end,
2298 uint param_data __attribute__((unused)))
2299 {
2300 return unpack_int32(to, from, from_end);
2301 }
get_max_int_value()2302 virtual ulonglong get_max_int_value() const
2303 {
2304 return unsigned_flag ? 0xFFFFFFFFULL : 0x7FFFFFFFULL;
2305 }
2306 };
2307
2308
2309 class Field_longlong :public Field_int
2310 {
2311 public:
Field_longlong(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2312 Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2313 uchar null_bit_arg,
2314 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2315 bool zero_arg, bool unsigned_arg)
2316 :Field_int(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2317 unireg_check_arg, field_name_arg, zero_arg, unsigned_arg)
2318 {}
Field_longlong(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,bool unsigned_arg)2319 Field_longlong(uint32 len_arg,bool maybe_null_arg,
2320 const LEX_CSTRING *field_name_arg,
2321 bool unsigned_arg)
2322 :Field_int((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
2323 NONE, field_name_arg, 0, unsigned_arg)
2324 {}
type_handler()2325 const Type_handler *type_handler() const { return &type_handler_longlong; }
key_type()2326 enum ha_base_keytype key_type() const
2327 { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
2328 int store(const char *to,size_t length,CHARSET_INFO *charset);
2329 int store(double nr);
2330 int store(longlong nr, bool unsigned_val);
reset(void)2331 int reset(void)
2332 {
2333 ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
2334 return 0;
2335 }
2336 double val_real(void);
2337 longlong val_int(void);
2338 String *val_str(String*,String *);
2339 bool send_binary(Protocol *protocol);
2340 int cmp(const uchar *,const uchar *);
2341 void sort_string(uchar *buff,uint length);
pack_length()2342 uint32 pack_length() const { return 8; }
2343 void sql_type(String &str) const;
type_limits_int()2344 const Type_limits_int *type_limits_int() const
2345 {
2346 return type_handler_longlong.type_limits_int_by_unsigned_flag(is_unsigned());
2347 }
pack(uchar * to,const uchar * from,uint max_length)2348 virtual uchar *pack(uchar* to, const uchar *from,
2349 uint max_length __attribute__((unused)))
2350 {
2351 return pack_int64(to, from);
2352 }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2353 const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
2354 uint param_data __attribute__((unused)))
2355 {
2356 return unpack_int64(to, from, from_end);
2357 }
2358 void set_max();
2359 bool is_max();
get_max_int_value()2360 virtual ulonglong get_max_int_value() const
2361 {
2362 return unsigned_flag ? 0xFFFFFFFFFFFFFFFFULL : 0x7FFFFFFFFFFFFFFFULL;
2363 }
2364 };
2365
2366
2367 class Field_vers_trx_id :public Field_longlong {
2368 MYSQL_TIME cache;
2369 ulonglong cached;
2370 public:
Field_vers_trx_id(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,bool zero_arg,bool unsigned_arg)2371 Field_vers_trx_id(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2372 uchar null_bit_arg, enum utype unireg_check_arg,
2373 const LEX_CSTRING *field_name_arg, bool zero_arg,
2374 bool unsigned_arg)
2375 : Field_longlong(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2376 unireg_check_arg, field_name_arg, zero_arg,
2377 unsigned_arg),
2378 cached(0)
2379 {}
type_handler()2380 const Type_handler *type_handler() const { return &type_handler_vers_trx_id; }
size_of()2381 uint size_of() const { return sizeof(*this); }
2382 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate, ulonglong trx_id);
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2383 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2384 {
2385 return get_date(ltime, fuzzydate, (ulonglong) val_int());
2386 }
2387 bool test_if_equality_guarantees_uniqueness(const Item *item) const;
can_optimize_keypart_ref(const Item_bool_func * cond,const Item * item)2388 bool can_optimize_keypart_ref(const Item_bool_func *cond,
2389 const Item *item) const
2390 {
2391 return true;
2392 }
2393
can_optimize_group_min_max(const Item_bool_func * cond,const Item * const_item)2394 bool can_optimize_group_min_max(const Item_bool_func *cond,
2395 const Item *const_item) const
2396 {
2397 return true;
2398 }
can_optimize_range(const Item_bool_func * cond,const Item * item,bool is_eq_func)2399 bool can_optimize_range(const Item_bool_func *cond,
2400 const Item *item,
2401 bool is_eq_func) const
2402 {
2403 return true;
2404 }
2405 /* cmp_type() cannot be TIME_RESULT, because we want to compare this field against
2406 integers. But in all other cases we treat it as TIME_RESULT! */
2407 };
2408
2409
2410 class Field_float :public Field_real {
2411 public:
Field_float(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2412 Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2413 uchar null_bit_arg,
2414 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2415 uint8 dec_arg,bool zero_arg,bool unsigned_arg)
2416 :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2417 unireg_check_arg, field_name_arg,
2418 dec_arg, zero_arg, unsigned_arg)
2419 {
2420 if (dec_arg >= FLOATING_POINT_DECIMALS)
2421 dec_arg= NOT_FIXED_DEC;
2422 }
Field_float(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg)2423 Field_float(uint32 len_arg, bool maybe_null_arg,
2424 const LEX_CSTRING *field_name_arg, uint8 dec_arg)
2425 :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
2426 NONE, field_name_arg, dec_arg, 0, 0)
2427 {
2428 if (dec_arg >= FLOATING_POINT_DECIMALS)
2429 dec_arg= NOT_FIXED_DEC;
2430 }
type_handler()2431 const Type_handler *type_handler() const { return &type_handler_float; }
key_type()2432 enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
2433 int store(const char *to,size_t length,CHARSET_INFO *charset);
2434 int store(double nr);
2435 int store(longlong nr, bool unsigned_val);
reset(void)2436 int reset(void) { bzero(ptr,sizeof(float)); return 0; }
2437 double val_real(void);
2438 longlong val_int(void);
2439 String *val_str(String*,String *);
2440 bool send_binary(Protocol *protocol);
2441 int cmp(const uchar *,const uchar *);
2442 void sort_string(uchar *buff,uint length);
pack_length()2443 uint32 pack_length() const { return sizeof(float); }
row_pack_length()2444 uint row_pack_length() const { return pack_length(); }
2445 void sql_type(String &str) const;
get_max_int_value()2446 virtual ulonglong get_max_int_value() const
2447 {
2448 /*
2449 We use the maximum as per IEEE754-2008 standard, 2^24
2450 */
2451 return 0x1000000ULL;
2452 }
2453 private:
2454 int save_field_metadata(uchar *first_byte);
2455 };
2456
2457
2458 class Field_double :public Field_real {
2459 longlong val_int_from_real(bool want_unsigned_result);
2460 public:
Field_double(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2461 Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2462 uchar null_bit_arg,
2463 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2464 uint8 dec_arg,bool zero_arg,bool unsigned_arg)
2465 :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2466 unireg_check_arg, field_name_arg,
2467 dec_arg, zero_arg, unsigned_arg)
2468 {
2469 if (dec_arg >= FLOATING_POINT_DECIMALS)
2470 dec_arg= NOT_FIXED_DEC;
2471 }
Field_double(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg)2472 Field_double(uint32 len_arg, bool maybe_null_arg,
2473 const LEX_CSTRING *field_name_arg, uint8 dec_arg)
2474 :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
2475 NONE, field_name_arg, dec_arg, 0, 0)
2476 {
2477 if (dec_arg >= FLOATING_POINT_DECIMALS)
2478 dec_arg= NOT_FIXED_DEC;
2479 }
Field_double(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,uint8 dec_arg,bool not_fixed_arg)2480 Field_double(uint32 len_arg, bool maybe_null_arg,
2481 const LEX_CSTRING *field_name_arg,
2482 uint8 dec_arg, bool not_fixed_arg)
2483 :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
2484 NONE, field_name_arg, dec_arg, 0, 0)
2485 {
2486 not_fixed= not_fixed_arg;
2487 if (dec_arg >= FLOATING_POINT_DECIMALS)
2488 dec_arg= NOT_FIXED_DEC;
2489 }
type_handler()2490 const Type_handler *type_handler() const { return &type_handler_double; }
key_type()2491 enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
2492 int store(const char *to,size_t length,CHARSET_INFO *charset);
2493 int store(double nr);
2494 int store(longlong nr, bool unsigned_val);
reset(void)2495 int reset(void) { bzero(ptr,sizeof(double)); return 0; }
2496 double val_real(void);
val_int(void)2497 longlong val_int(void) { return val_int_from_real(false); }
val_uint(void)2498 ulonglong val_uint(void) { return (ulonglong) val_int_from_real(true); }
2499 String *val_str(String*,String *);
2500 bool send_binary(Protocol *protocol);
2501 int cmp(const uchar *,const uchar *);
2502 void sort_string(uchar *buff,uint length);
pack_length()2503 uint32 pack_length() const { return sizeof(double); }
row_pack_length()2504 uint row_pack_length() const { return pack_length(); }
2505 void sql_type(String &str) const;
get_max_int_value()2506 virtual ulonglong get_max_int_value() const
2507 {
2508 /*
2509 We use the maximum as per IEEE754-2008 standard, 2^53
2510 */
2511 return 0x20000000000000ULL;
2512 }
2513 private:
2514 int save_field_metadata(uchar *first_byte);
2515 };
2516
2517
2518 /* Everything saved in this will disappear. It will always return NULL */
2519
2520 class Field_null :public Field_str {
2521 static uchar null[1];
2522 public:
Field_null(uchar * ptr_arg,uint32 len_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)2523 Field_null(uchar *ptr_arg, uint32 len_arg,
2524 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
2525 const DTCollation &collation)
2526 :Field_str(ptr_arg, len_arg, null, 1,
2527 unireg_check_arg, field_name_arg, collation)
2528 {}
type_handler()2529 const Type_handler *type_handler() const { return &type_handler_null; }
2530 Information_schema_character_attributes
information_schema_character_attributes()2531 information_schema_character_attributes() const
2532 {
2533 return Information_schema_character_attributes();
2534 }
get_copy_func(const Field * from)2535 Copy_func *get_copy_func(const Field *from) const
2536 {
2537 return do_field_string;
2538 }
store(const char * to,size_t length,CHARSET_INFO * cs)2539 int store(const char *to, size_t length, CHARSET_INFO *cs)
2540 { null[0]=1; return 0; }
store(double nr)2541 int store(double nr) { null[0]=1; return 0; }
store(longlong nr,bool unsigned_val)2542 int store(longlong nr, bool unsigned_val) { null[0]=1; return 0; }
store_decimal(const my_decimal * d)2543 int store_decimal(const my_decimal *d) { null[0]=1; return 0; }
reset(void)2544 int reset(void) { return 0; }
val_real(void)2545 double val_real(void) { return 0.0;}
val_int(void)2546 longlong val_int(void) { return 0;}
val_bool(void)2547 bool val_bool(void) { return false; }
val_decimal(my_decimal *)2548 my_decimal *val_decimal(my_decimal *) { return 0; }
val_str(String * value,String * value2)2549 String *val_str(String *value,String *value2)
2550 { value2->length(0); return value2;}
cmp(const uchar * a,const uchar * b)2551 int cmp(const uchar *a, const uchar *b) { return 0;}
sort_string(uchar * buff,uint length)2552 void sort_string(uchar *buff, uint length) {}
pack_length()2553 uint32 pack_length() const { return 0; }
2554 void sql_type(String &str) const;
size_of()2555 uint size_of() const { return sizeof(*this); }
max_display_length()2556 uint32 max_display_length() const { return 4; }
move_field_offset(my_ptrdiff_t ptr_diff)2557 void move_field_offset(my_ptrdiff_t ptr_diff) {}
can_optimize_keypart_ref(const Item_bool_func * cond,const Item * item)2558 bool can_optimize_keypart_ref(const Item_bool_func *cond,
2559 const Item *item) const
2560 {
2561 return false;
2562 }
can_optimize_group_min_max(const Item_bool_func * cond,const Item * const_item)2563 bool can_optimize_group_min_max(const Item_bool_func *cond,
2564 const Item *const_item) const
2565 {
2566 return false;
2567 }
2568 };
2569
2570
2571 class Field_temporal: public Field {
2572 protected:
2573 Item *get_equal_const_item_datetime(THD *thd, const Context &ctx,
2574 Item *const_item);
2575 public:
Field_temporal(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2576 Field_temporal(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
2577 uchar null_bit_arg, utype unireg_check_arg,
2578 const LEX_CSTRING *field_name_arg)
2579 :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
2580 field_name_arg)
2581 { flags|= BINARY_FLAG; }
store_hex_hybrid(const char * str,size_t length)2582 int store_hex_hybrid(const char *str, size_t length)
2583 {
2584 return store(str, length, &my_charset_bin);
2585 }
2586 sql_mode_t can_handle_sql_mode_dependency_on_store() const;
2587 Copy_func *get_copy_func(const Field *from) const;
save_in_field(Field * to)2588 int save_in_field(Field *to)
2589 {
2590 MYSQL_TIME ltime;
2591 if (get_date(<ime, 0))
2592 return to->reset();
2593 return to->store_time_dec(<ime, decimals());
2594 }
2595 bool memcpy_field_possible(const Field *from) const;
max_display_length()2596 uint32 max_display_length() const { return field_length; }
str_needs_quotes()2597 bool str_needs_quotes() { return TRUE; }
derivation(void)2598 enum Derivation derivation(void) const { return DERIVATION_NUMERIC; }
repertoire(void)2599 uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; }
charset(void)2600 CHARSET_INFO *charset(void) const { return &my_charset_numeric; }
sort_charset(void)2601 CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
binary()2602 bool binary() const { return true; }
val_bool()2603 bool val_bool() { return val_real() != 0e0; }
2604 uint is_equal(Create_field *new_field);
eq_def(const Field * field)2605 bool eq_def(const Field *field) const
2606 {
2607 return (Field::eq_def(field) && decimals() == field->decimals());
2608 }
2609 my_decimal *val_decimal(my_decimal*);
2610 void set_warnings(Sql_condition::enum_warning_level trunc_level,
2611 const ErrConv *str, int was_cut, timestamp_type ts_type);
pos_in_interval(Field * min,Field * max)2612 double pos_in_interval(Field *min, Field *max)
2613 {
2614 return pos_in_interval_val_real(min, max);
2615 }
2616 bool can_optimize_keypart_ref(const Item_bool_func *cond,
2617 const Item *item) const;
2618 bool can_optimize_group_min_max(const Item_bool_func *cond,
2619 const Item *const_item) const;
can_optimize_range(const Item_bool_func * cond,const Item * item,bool is_eq_func)2620 bool can_optimize_range(const Item_bool_func *cond,
2621 const Item *item,
2622 bool is_eq_func) const
2623 {
2624 return true;
2625 }
2626 };
2627
2628
2629 /**
2630 Abstract class for:
2631 - DATE
2632 - DATETIME
2633 - DATETIME(1..6)
2634 - DATETIME(0..6) - MySQL56 version
2635 */
2636 class Field_temporal_with_date: public Field_temporal {
2637 protected:
2638 int store_TIME_with_warning(MYSQL_TIME *ltime, const ErrConv *str,
2639 int was_cut, int have_smth_to_conv);
2640 virtual void store_TIME(MYSQL_TIME *ltime) = 0;
2641 virtual bool get_TIME(MYSQL_TIME *ltime, const uchar *pos,
2642 ulonglong fuzzydate) const = 0;
validate_MMDD(bool not_zero_date,uint month,uint day,ulonglong fuzzydate)2643 bool validate_MMDD(bool not_zero_date, uint month, uint day,
2644 ulonglong fuzzydate) const
2645 {
2646 if (!not_zero_date)
2647 return fuzzydate & TIME_NO_ZERO_DATE;
2648 if (!month || !day)
2649 return fuzzydate & TIME_NO_ZERO_IN_DATE;
2650 return false;
2651 }
2652 public:
Field_temporal_with_date(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2653 Field_temporal_with_date(uchar *ptr_arg, uint32 len_arg,
2654 uchar *null_ptr_arg, uchar null_bit_arg,
2655 utype unireg_check_arg,
2656 const LEX_CSTRING *field_name_arg)
2657 :Field_temporal(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2658 unireg_check_arg, field_name_arg)
2659 {}
2660 int store(const char *to, size_t length, CHARSET_INFO *charset);
2661 int store(double nr);
2662 int store(longlong nr, bool unsigned_val);
2663 int store_time_dec(const MYSQL_TIME *ltime, uint dec);
2664 int store_decimal(const my_decimal *);
2665 bool validate_value_in_record(THD *thd, const uchar *record) const;
2666 };
2667
2668
2669 class Field_timestamp :public Field_temporal {
2670 protected:
2671 sql_mode_t sql_mode_for_timestamp(THD *thd) const;
2672 int store_TIME_with_warning(THD *, MYSQL_TIME *, const ErrConv *,
2673 int warnings, bool have_smth_to_conv);
2674 public:
2675 Field_timestamp(uchar *ptr_arg, uint32 len_arg,
2676 uchar *null_ptr_arg, uchar null_bit_arg,
2677 enum utype unireg_check_arg,
2678 const LEX_CSTRING *field_name_arg,
2679 TABLE_SHARE *share);
type_handler()2680 const Type_handler *type_handler() const { return &type_handler_timestamp; }
key_type()2681 enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
2682 Copy_func *get_copy_func(const Field *from) const;
2683 int store(const char *to,size_t length,CHARSET_INFO *charset);
2684 int store(double nr);
2685 int store(longlong nr, bool unsigned_val);
2686 int store_time_dec(const MYSQL_TIME *ltime, uint dec);
2687 int store_decimal(const my_decimal *);
2688 int store_timestamp(my_time_t timestamp, ulong sec_part);
2689 int save_in_field(Field *to);
2690 double val_real(void);
2691 longlong val_int(void);
2692 String *val_str(String*,String *);
2693 bool send_binary(Protocol *protocol);
2694 int cmp(const uchar *,const uchar *);
2695 void sort_string(uchar *buff,uint length);
pack_length()2696 uint32 pack_length() const { return 4; }
2697 void sql_type(String &str) const;
zero_pack()2698 bool zero_pack() const { return 0; }
2699 int set_time();
2700 /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
2701 my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const;
get_timestamp(ulong * sec_part)2702 my_time_t get_timestamp(ulong *sec_part) const
2703 {
2704 return get_timestamp(ptr, sec_part);
2705 }
store_TIME(my_time_t timestamp,ulong sec_part)2706 virtual void store_TIME(my_time_t timestamp, ulong sec_part)
2707 {
2708 int4store(ptr,timestamp);
2709 }
2710 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
pack(uchar * to,const uchar * from,uint max_length)2711 uchar *pack(uchar *to, const uchar *from,
2712 uint max_length __attribute__((unused)))
2713 {
2714 return pack_int32(to, from);
2715 }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2716 const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
2717 uint param_data __attribute__((unused)))
2718 {
2719 return unpack_int32(to, from, from_end);
2720 }
2721 bool validate_value_in_record(THD *thd, const uchar *record) const;
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)2722 Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item)
2723 {
2724 return get_equal_const_item_datetime(thd, ctx, const_item);
2725 }
2726 bool load_data_set_null(THD *thd);
2727 bool load_data_set_no_data(THD *thd, bool fixed_format);
size_of()2728 uint size_of() const { return sizeof(*this); }
2729 };
2730
2731
2732 /**
2733 Abstract class for:
2734 - TIMESTAMP(1..6)
2735 - TIMESTAMP(0..6) - MySQL56 version
2736 */
2737 class Field_timestamp_with_dec :public Field_timestamp {
2738 protected:
2739 uint dec;
2740 public:
Field_timestamp_with_dec(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint dec_arg)2741 Field_timestamp_with_dec(uchar *ptr_arg,
2742 uchar *null_ptr_arg, uchar null_bit_arg,
2743 enum utype unireg_check_arg,
2744 const LEX_CSTRING *field_name_arg,
2745 TABLE_SHARE *share, uint dec_arg) :
2746 Field_timestamp(ptr_arg,
2747 MAX_DATETIME_WIDTH + dec_arg + MY_TEST(dec_arg), null_ptr_arg,
2748 null_bit_arg, unireg_check_arg, field_name_arg, share),
2749 dec(dec_arg)
2750 {
2751 DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
2752 }
decimals()2753 uint decimals() const { return dec; }
key_type()2754 enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
pack(uchar * to,const uchar * from,uint max_length)2755 uchar *pack(uchar *to, const uchar *from, uint max_length)
2756 { return Field::pack(to, from, max_length); }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2757 const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
2758 uint param_data)
2759 { return Field::unpack(to, from, from_end, param_data); }
2760 void make_send_field(Send_field *field);
sort_string(uchar * to,uint length)2761 void sort_string(uchar *to, uint length)
2762 {
2763 DBUG_ASSERT(length == pack_length());
2764 memcpy(to, ptr, length);
2765 }
2766 bool send_binary(Protocol *protocol);
2767 double val_real(void);
2768 my_decimal* val_decimal(my_decimal*);
2769 int set_time();
2770 };
2771
2772
2773 class Field_timestamp_hires :public Field_timestamp_with_dec {
sec_part_bytes(uint dec)2774 uint sec_part_bytes(uint dec) const
2775 {
2776 return Type_handler_timestamp::sec_part_bytes(dec);
2777 }
2778 public:
Field_timestamp_hires(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint dec_arg)2779 Field_timestamp_hires(uchar *ptr_arg,
2780 uchar *null_ptr_arg, uchar null_bit_arg,
2781 enum utype unireg_check_arg,
2782 const LEX_CSTRING *field_name_arg,
2783 TABLE_SHARE *share, uint dec_arg) :
2784 Field_timestamp_with_dec(ptr_arg, null_ptr_arg, null_bit_arg,
2785 unireg_check_arg, field_name_arg, share, dec_arg)
2786 {
2787 DBUG_ASSERT(dec);
2788 }
2789 my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const;
2790 void store_TIME(my_time_t timestamp, ulong sec_part);
2791 int cmp(const uchar *,const uchar *);
pack_length()2792 uint32 pack_length() const { return 4 + sec_part_bytes(dec); }
size_of()2793 uint size_of() const { return sizeof(*this); }
2794 };
2795
2796
2797 /**
2798 TIMESTAMP(0..6) - MySQL56 version
2799 */
2800 class Field_timestampf :public Field_timestamp_with_dec {
save_field_metadata(uchar * metadata_ptr)2801 int save_field_metadata(uchar *metadata_ptr)
2802 {
2803 *metadata_ptr= (uchar) decimals();
2804 return 1;
2805 }
2806 public:
Field_timestampf(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,uint dec_arg)2807 Field_timestampf(uchar *ptr_arg,
2808 uchar *null_ptr_arg, uchar null_bit_arg,
2809 enum utype unireg_check_arg,
2810 const LEX_CSTRING *field_name_arg,
2811 TABLE_SHARE *share, uint dec_arg) :
2812 Field_timestamp_with_dec(ptr_arg, null_ptr_arg, null_bit_arg,
2813 unireg_check_arg, field_name_arg, share, dec_arg)
2814 {}
type_handler()2815 const Type_handler *type_handler() const { return &type_handler_timestamp2; }
binlog_type()2816 enum_field_types binlog_type() const { return MYSQL_TYPE_TIMESTAMP2; }
pack_length()2817 uint32 pack_length() const
2818 {
2819 return my_timestamp_binary_length(dec);
2820 }
row_pack_length()2821 uint row_pack_length() const { return pack_length(); }
pack_length_from_metadata(uint field_metadata)2822 uint pack_length_from_metadata(uint field_metadata)
2823 {
2824 DBUG_ENTER("Field_timestampf::pack_length_from_metadata");
2825 uint tmp= my_timestamp_binary_length(field_metadata);
2826 DBUG_RETURN(tmp);
2827 }
cmp(const uchar * a_ptr,const uchar * b_ptr)2828 int cmp(const uchar *a_ptr,const uchar *b_ptr)
2829 {
2830 return memcmp(a_ptr, b_ptr, pack_length());
2831 }
2832 void set_max();
2833 bool is_max();
2834 void store_TIME(my_time_t timestamp, ulong sec_part);
2835 my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const;
get_timestamp(ulong * sec_part)2836 my_time_t get_timestamp(ulong *sec_part) const
2837 {
2838 return get_timestamp(ptr, sec_part);
2839 }
size_of()2840 uint size_of() const { return sizeof(*this); }
2841 };
2842
2843
2844 class Field_year :public Field_tiny {
2845 public:
Field_year(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2846 Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2847 uchar null_bit_arg,
2848 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg)
2849 :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2850 unireg_check_arg, field_name_arg, 1, 1)
2851 {}
type_handler()2852 const Type_handler *type_handler() const { return &type_handler_year; }
get_copy_func(const Field * from)2853 Copy_func *get_copy_func(const Field *from) const
2854 {
2855 if (eq_def(from))
2856 return get_identical_copy_func();
2857 switch (from->cmp_type()) {
2858 case STRING_RESULT:
2859 {
2860 const Type_handler *handler= from->type_handler();
2861 if (handler == &type_handler_enum || handler == &type_handler_set)
2862 return do_field_int;
2863 return do_field_string;
2864 }
2865 case TIME_RESULT:
2866 return do_field_temporal;
2867 case DECIMAL_RESULT:
2868 return do_field_decimal;
2869 case REAL_RESULT:
2870 return do_field_real;
2871 case INT_RESULT:
2872 break;
2873 case ROW_RESULT:
2874 default:
2875 DBUG_ASSERT(0);
2876 break;
2877 }
2878 return do_field_int;
2879 }
2880 int store(const char *to,size_t length,CHARSET_INFO *charset);
2881 int store(double nr);
2882 int store(longlong nr, bool unsigned_val);
2883 int store_time_dec(const MYSQL_TIME *ltime, uint dec);
2884 double val_real(void);
2885 longlong val_int(void);
2886 String *val_str(String*,String *);
2887 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
2888 bool send_binary(Protocol *protocol);
2889 Information_schema_numeric_attributes
information_schema_numeric_attributes()2890 information_schema_numeric_attributes() const
2891 {
2892 return Information_schema_numeric_attributes();
2893 }
max_display_length()2894 uint32 max_display_length() const { return field_length; }
2895 void sql_type(String &str) const;
2896 };
2897
2898
2899 class Field_date :public Field_temporal_with_date {
2900 void store_TIME(MYSQL_TIME *ltime);
2901 bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
2902 public:
Field_date(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2903 Field_date(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2904 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg)
2905 :Field_temporal_with_date(ptr_arg, MAX_DATE_WIDTH, null_ptr_arg, null_bit_arg,
2906 unireg_check_arg, field_name_arg) {}
type_handler()2907 const Type_handler *type_handler() const { return &type_handler_date; }
key_type()2908 enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
reset(void)2909 int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2910 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2911 { return Field_date::get_TIME(ltime, ptr, fuzzydate); }
2912 double val_real(void);
2913 longlong val_int(void);
2914 String *val_str(String*,String *);
2915 bool send_binary(Protocol *protocol);
2916 int cmp(const uchar *,const uchar *);
2917 void sort_string(uchar *buff,uint length);
pack_length()2918 uint32 pack_length() const { return 4; }
2919 void sql_type(String &str) const;
pack(uchar * to,const uchar * from,uint max_length)2920 uchar *pack(uchar* to, const uchar *from,
2921 uint max_length __attribute__((unused)))
2922 {
2923 return pack_int32(to, from);
2924 }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)2925 const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
2926 uint param_data __attribute__((unused)))
2927 {
2928 return unpack_int32(to, from, from_end);
2929 }
size_of()2930 uint size_of() const { return sizeof(*this); }
2931 };
2932
2933
2934 class Field_newdate :public Field_temporal_with_date {
2935 void store_TIME(MYSQL_TIME *ltime);
2936 bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
2937 public:
Field_newdate(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2938 Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2939 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg)
2940 :Field_temporal_with_date(ptr_arg, MAX_DATE_WIDTH, null_ptr_arg, null_bit_arg,
2941 unireg_check_arg, field_name_arg)
2942 {}
type_handler()2943 const Type_handler *type_handler() const { return &type_handler_newdate; }
key_type()2944 enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
reset(void)2945 int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
2946 double val_real(void);
2947 longlong val_int(void);
2948 String *val_str(String*,String *);
2949 bool send_binary(Protocol *protocol);
2950 int cmp(const uchar *,const uchar *);
2951 void sort_string(uchar *buff,uint length);
pack_length()2952 uint32 pack_length() const { return 3; }
2953 void sql_type(String &str) const;
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2954 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2955 { return Field_newdate::get_TIME(ltime, ptr, fuzzydate); }
size_of()2956 uint size_of() const { return sizeof(*this); }
2957 Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item);
2958 };
2959
2960
2961 class Field_time :public Field_temporal {
2962 /*
2963 when this Field_time instance is used for storing values for index lookups
2964 (see class store_key, Field::new_key_field(), etc), the following
2965 might be set to TO_DAYS(CURDATE()). See also Field_time::store_time_dec()
2966 */
2967 long curdays;
2968 protected:
2969 virtual void store_TIME(const MYSQL_TIME *ltime);
2970 int store_TIME_with_warning(MYSQL_TIME *ltime, const ErrConv *str,
2971 int was_cut, int have_smth_to_conv);
set_warnings(Sql_condition::enum_warning_level level,const ErrConv * str,int was_cut)2972 void set_warnings(Sql_condition::enum_warning_level level,
2973 const ErrConv *str, int was_cut)
2974 {
2975 Field_temporal::set_warnings(level, str, was_cut, MYSQL_TIMESTAMP_TIME);
2976 }
2977 bool check_zero_in_date_with_warn(ulonglong fuzzydate);
2978 static void do_field_time(Copy_field *copy);
2979 public:
Field_time(uchar * ptr_arg,uint length_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)2980 Field_time(uchar *ptr_arg, uint length_arg, uchar *null_ptr_arg,
2981 uchar null_bit_arg, enum utype unireg_check_arg,
2982 const LEX_CSTRING *field_name_arg)
2983 :Field_temporal(ptr_arg, length_arg, null_ptr_arg, null_bit_arg,
2984 unireg_check_arg, field_name_arg), curdays(0)
2985 {}
2986 bool can_be_substituted_to_equal_item(const Context &ctx,
2987 const Item_equal *item_equal);
type_handler()2988 const Type_handler *type_handler() const { return &type_handler_time; }
key_type()2989 enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
get_copy_func(const Field * from)2990 Copy_func *get_copy_func(const Field *from) const
2991 {
2992 return from->cmp_type() == REAL_RESULT ? do_field_string : // MDEV-9344
2993 from->type() == MYSQL_TYPE_YEAR ? do_field_int :
2994 from->type() == MYSQL_TYPE_BIT ? do_field_int :
2995 eq_def(from) ? get_identical_copy_func() :
2996 do_field_time;
2997 }
memcpy_field_possible(const Field * from)2998 bool memcpy_field_possible(const Field *from) const
2999 {
3000 return real_type() == from->real_type() &&
3001 decimals() == from->decimals();
3002 }
3003 int store_time_dec(const MYSQL_TIME *ltime, uint dec);
3004 int store(const char *to,size_t length,CHARSET_INFO *charset);
3005 int store(double nr);
3006 int store(longlong nr, bool unsigned_val);
3007 int store_decimal(const my_decimal *);
3008 double val_real(void);
3009 longlong val_int(void);
3010 String *val_str(String*,String *);
3011 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
3012 bool send_binary(Protocol *protocol);
3013 int cmp(const uchar *,const uchar *);
3014 void sort_string(uchar *buff,uint length);
pack_length()3015 uint32 pack_length() const { return 3; }
3016 void sql_type(String &str) const;
size_of()3017 uint size_of() const { return sizeof(*this); }
3018 void set_curdays(THD *thd);
3019 Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
3020 uchar *new_ptr, uint32 length,
3021 uchar *new_null_ptr, uint new_null_bit);
3022 Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item);
3023 };
3024
3025
3026 /**
3027 Abstract class for:
3028 - TIME(1..6)
3029 - TIME(0..6) - MySQL56 version
3030 */
3031 class Field_time_with_dec :public Field_time {
3032 protected:
3033 uint dec;
3034 public:
Field_time_with_dec(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3035 Field_time_with_dec(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3036 enum utype unireg_check_arg,
3037 const LEX_CSTRING *field_name_arg,
3038 uint dec_arg)
3039 :Field_time(ptr_arg, MIN_TIME_WIDTH + dec_arg + MY_TEST(dec_arg),
3040 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg),
3041 dec(dec_arg)
3042 {
3043 DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
3044 }
decimals()3045 uint decimals() const { return dec; }
key_type()3046 enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
3047 longlong val_int(void);
3048 double val_real(void);
3049 void make_send_field(Send_field *);
3050 };
3051
3052
3053 /**
3054 TIME(1..6)
3055 */
3056 class Field_time_hires :public Field_time_with_dec {
3057 longlong zero_point;
3058 void store_TIME(const MYSQL_TIME *);
3059 public:
Field_time_hires(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3060 Field_time_hires(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3061 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3062 uint dec_arg)
3063 :Field_time_with_dec(ptr_arg, null_ptr_arg,
3064 null_bit_arg, unireg_check_arg, field_name_arg,
3065 dec_arg)
3066 {
3067 DBUG_ASSERT(dec);
3068 zero_point= sec_part_shift(
3069 ((TIME_MAX_VALUE_SECONDS+1LL)*TIME_SECOND_PART_FACTOR), dec);
3070 }
3071 int reset(void);
3072 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
3073 int cmp(const uchar *,const uchar *);
3074 void sort_string(uchar *buff,uint length);
pack_length()3075 uint32 pack_length() const { return Type_handler_time::hires_bytes(dec); }
size_of()3076 uint size_of() const { return sizeof(*this); }
3077 };
3078
3079
3080 /**
3081 TIME(0..6) - MySQL56 version
3082 */
3083 class Field_timef :public Field_time_with_dec {
3084 void store_TIME(const MYSQL_TIME *ltime);
save_field_metadata(uchar * metadata_ptr)3085 int save_field_metadata(uchar *metadata_ptr)
3086 {
3087 *metadata_ptr= (uchar) decimals();
3088 return 1;
3089 }
3090 public:
Field_timef(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3091 Field_timef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3092 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3093 uint dec_arg)
3094 :Field_time_with_dec(ptr_arg, null_ptr_arg,
3095 null_bit_arg, unireg_check_arg, field_name_arg,
3096 dec_arg)
3097 {
3098 DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
3099 }
type_handler()3100 const Type_handler *type_handler() const { return &type_handler_time2; }
binlog_type()3101 enum_field_types binlog_type() const { return MYSQL_TYPE_TIME2; }
pack_length()3102 uint32 pack_length() const
3103 {
3104 return my_time_binary_length(dec);
3105 }
row_pack_length()3106 uint row_pack_length() const { return pack_length(); }
pack_length_from_metadata(uint field_metadata)3107 uint pack_length_from_metadata(uint field_metadata)
3108 {
3109 DBUG_ENTER("Field_timef::pack_length_from_metadata");
3110 uint tmp= my_time_binary_length(field_metadata);
3111 DBUG_RETURN(tmp);
3112 }
sort_string(uchar * to,uint length)3113 void sort_string(uchar *to, uint length)
3114 {
3115 DBUG_ASSERT(length == Field_timef::pack_length());
3116 memcpy(to, ptr, length);
3117 }
cmp(const uchar * a_ptr,const uchar * b_ptr)3118 int cmp(const uchar *a_ptr, const uchar *b_ptr)
3119 {
3120 return memcmp(a_ptr, b_ptr, pack_length());
3121 }
3122 int reset();
3123 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
size_of()3124 uint size_of() const { return sizeof(*this); }
3125 };
3126
3127
3128 class Field_datetime :public Field_temporal_with_date {
3129 void store_TIME(MYSQL_TIME *ltime);
3130 bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
3131 public:
Field_datetime(uchar * ptr_arg,uint length_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg)3132 Field_datetime(uchar *ptr_arg, uint length_arg, uchar *null_ptr_arg,
3133 uchar null_bit_arg, enum utype unireg_check_arg,
3134 const LEX_CSTRING *field_name_arg)
3135 :Field_temporal_with_date(ptr_arg, length_arg, null_ptr_arg, null_bit_arg,
3136 unireg_check_arg, field_name_arg)
3137 {
3138 if (unireg_check == TIMESTAMP_UN_FIELD ||
3139 unireg_check == TIMESTAMP_DNUN_FIELD)
3140 flags|= ON_UPDATE_NOW_FLAG;
3141 }
type_handler()3142 const Type_handler *type_handler() const { return &type_handler_datetime; }
key_type()3143 enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; }
3144 double val_real(void);
3145 longlong val_int(void);
3146 String *val_str(String*,String *);
3147 bool send_binary(Protocol *protocol);
3148 int cmp(const uchar *,const uchar *);
3149 void sort_string(uchar *buff,uint length);
pack_length()3150 uint32 pack_length() const { return 8; }
3151 void sql_type(String &str) const;
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)3152 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
3153 { return Field_datetime::get_TIME(ltime, ptr, fuzzydate); }
3154 int set_time();
pack(uchar * to,const uchar * from,uint max_length)3155 uchar *pack(uchar* to, const uchar *from,
3156 uint max_length __attribute__((unused)))
3157 {
3158 return pack_int64(to, from);
3159 }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)3160 const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
3161 uint param_data __attribute__((unused)))
3162 {
3163 return unpack_int64(to, from, from_end);
3164 }
get_equal_const_item(THD * thd,const Context & ctx,Item * const_item)3165 Item *get_equal_const_item(THD *thd, const Context &ctx, Item *const_item)
3166 {
3167 return get_equal_const_item_datetime(thd, ctx, const_item);
3168 }
size_of()3169 uint size_of() const { return sizeof(*this); }
3170 };
3171
3172
3173 /**
3174 Abstract class for:
3175 - DATETIME(1..6)
3176 - DATETIME(0..6) - MySQL56 version
3177 */
3178 class Field_datetime_with_dec :public Field_datetime {
3179 protected:
3180 uint dec;
3181 public:
Field_datetime_with_dec(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3182 Field_datetime_with_dec(uchar *ptr_arg, uchar *null_ptr_arg,
3183 uchar null_bit_arg, enum utype unireg_check_arg,
3184 const LEX_CSTRING *field_name_arg, uint dec_arg)
3185 :Field_datetime(ptr_arg, MAX_DATETIME_WIDTH + dec_arg + MY_TEST(dec_arg),
3186 null_ptr_arg, null_bit_arg, unireg_check_arg,
3187 field_name_arg), dec(dec_arg)
3188 {
3189 DBUG_ASSERT(dec <= TIME_SECOND_PART_DIGITS);
3190 }
decimals()3191 uint decimals() const { return dec; }
key_type()3192 enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
3193 void make_send_field(Send_field *field);
3194 bool send_binary(Protocol *protocol);
pack(uchar * to,const uchar * from,uint max_length)3195 uchar *pack(uchar *to, const uchar *from, uint max_length)
3196 { return Field::pack(to, from, max_length); }
unpack(uchar * to,const uchar * from,const uchar * from_end,uint param_data)3197 const uchar *unpack(uchar* to, const uchar *from, const uchar *from_end,
3198 uint param_data)
3199 { return Field::unpack(to, from, from_end, param_data); }
sort_string(uchar * to,uint length)3200 void sort_string(uchar *to, uint length)
3201 {
3202 DBUG_ASSERT(length == pack_length());
3203 memcpy(to, ptr, length);
3204 }
3205 double val_real(void);
3206 longlong val_int(void);
3207 String *val_str(String*,String *);
3208 };
3209
3210
3211 /**
3212 DATETIME(1..6)
3213 */
3214 class Field_datetime_hires :public Field_datetime_with_dec {
3215 void store_TIME(MYSQL_TIME *ltime);
3216 bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
3217 public:
Field_datetime_hires(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3218 Field_datetime_hires(uchar *ptr_arg, uchar *null_ptr_arg,
3219 uchar null_bit_arg, enum utype unireg_check_arg,
3220 const LEX_CSTRING *field_name_arg, uint dec_arg)
3221 :Field_datetime_with_dec(ptr_arg, null_ptr_arg, null_bit_arg,
3222 unireg_check_arg, field_name_arg, dec_arg)
3223 {
3224 DBUG_ASSERT(dec);
3225 }
3226 int cmp(const uchar *,const uchar *);
pack_length()3227 uint32 pack_length() const { return Type_handler_datetime::hires_bytes(dec); }
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)3228 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
3229 { return Field_datetime_hires::get_TIME(ltime, ptr, fuzzydate); }
size_of()3230 uint size_of() const { return sizeof(*this); }
3231 };
3232
3233
3234 /**
3235 DATETIME(0..6) - MySQL56 version
3236 */
3237 class Field_datetimef :public Field_datetime_with_dec {
3238 void store_TIME(MYSQL_TIME *ltime);
3239 bool get_TIME(MYSQL_TIME *ltime, const uchar *pos, ulonglong fuzzydate) const;
save_field_metadata(uchar * metadata_ptr)3240 int save_field_metadata(uchar *metadata_ptr)
3241 {
3242 *metadata_ptr= (uchar) decimals();
3243 return 1;
3244 }
3245 public:
Field_datetimef(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,uint dec_arg)3246 Field_datetimef(uchar *ptr_arg, uchar *null_ptr_arg,
3247 uchar null_bit_arg, enum utype unireg_check_arg,
3248 const LEX_CSTRING *field_name_arg, uint dec_arg)
3249 :Field_datetime_with_dec(ptr_arg, null_ptr_arg, null_bit_arg,
3250 unireg_check_arg, field_name_arg, dec_arg)
3251 {}
type_handler()3252 const Type_handler *type_handler() const { return &type_handler_datetime2; }
binlog_type()3253 enum_field_types binlog_type() const { return MYSQL_TYPE_DATETIME2; }
pack_length()3254 uint32 pack_length() const
3255 {
3256 return my_datetime_binary_length(dec);
3257 }
row_pack_length()3258 uint row_pack_length() const { return pack_length(); }
pack_length_from_metadata(uint field_metadata)3259 uint pack_length_from_metadata(uint field_metadata)
3260 {
3261 DBUG_ENTER("Field_datetimef::pack_length_from_metadata");
3262 uint tmp= my_datetime_binary_length(field_metadata);
3263 DBUG_RETURN(tmp);
3264 }
cmp(const uchar * a_ptr,const uchar * b_ptr)3265 int cmp(const uchar *a_ptr, const uchar *b_ptr)
3266 {
3267 return memcmp(a_ptr, b_ptr, pack_length());
3268 }
3269 int reset();
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)3270 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
3271 { return Field_datetimef::get_TIME(ltime, ptr, fuzzydate); }
size_of()3272 uint size_of() const { return sizeof(*this); }
3273 };
3274
3275
3276 static inline Field_timestamp *
new_Field_timestamp(MEM_ROOT * root,uchar * ptr,uchar * null_ptr,uchar null_bit,enum Field::utype unireg_check,const LEX_CSTRING * field_name,TABLE_SHARE * share,uint dec)3277 new_Field_timestamp(MEM_ROOT *root,uchar *ptr, uchar *null_ptr, uchar null_bit,
3278 enum Field::utype unireg_check,
3279 const LEX_CSTRING *field_name,
3280 TABLE_SHARE *share, uint dec)
3281 {
3282 if (dec==0)
3283 return new (root)
3284 Field_timestamp(ptr, MAX_DATETIME_WIDTH, null_ptr,
3285 null_bit, unireg_check, field_name, share);
3286 if (dec >= FLOATING_POINT_DECIMALS)
3287 dec= MAX_DATETIME_PRECISION;
3288 return new (root)
3289 Field_timestamp_hires(ptr, null_ptr, null_bit, unireg_check,
3290 field_name, share, dec);
3291 }
3292
3293 static inline Field_time *
new_Field_time(MEM_ROOT * root,uchar * ptr,uchar * null_ptr,uchar null_bit,enum Field::utype unireg_check,const LEX_CSTRING * field_name,uint dec)3294 new_Field_time(MEM_ROOT *root, uchar *ptr, uchar *null_ptr, uchar null_bit,
3295 enum Field::utype unireg_check, const LEX_CSTRING *field_name,
3296 uint dec)
3297 {
3298 if (dec == 0)
3299 return new (root)
3300 Field_time(ptr, MIN_TIME_WIDTH, null_ptr, null_bit, unireg_check,
3301 field_name);
3302 if (dec >= FLOATING_POINT_DECIMALS)
3303 dec= MAX_DATETIME_PRECISION;
3304 return new (root)
3305 Field_time_hires(ptr, null_ptr, null_bit, unireg_check, field_name, dec);
3306 }
3307
3308 static inline Field_datetime *
new_Field_datetime(MEM_ROOT * root,uchar * ptr,uchar * null_ptr,uchar null_bit,enum Field::utype unireg_check,const LEX_CSTRING * field_name,uint dec)3309 new_Field_datetime(MEM_ROOT *root, uchar *ptr, uchar *null_ptr, uchar null_bit,
3310 enum Field::utype unireg_check,
3311 const LEX_CSTRING *field_name, uint dec)
3312 {
3313 if (dec == 0)
3314 return new (root)
3315 Field_datetime(ptr, MAX_DATETIME_WIDTH, null_ptr, null_bit,
3316 unireg_check, field_name);
3317 if (dec >= FLOATING_POINT_DECIMALS)
3318 dec= MAX_DATETIME_PRECISION;
3319 return new (root)
3320 Field_datetime_hires(ptr, null_ptr, null_bit,
3321 unireg_check, field_name, dec);
3322 }
3323
3324 class Field_string :public Field_longstr {
3325 class Warn_filter_string: public Warn_filter
3326 {
3327 public:
3328 Warn_filter_string(const THD *thd, const Field_string *field);
3329 };
is_var_string()3330 bool is_var_string() const
3331 {
3332 return can_alter_field_type &&
3333 orig_table &&
3334 (orig_table->s->db_create_options & HA_OPTION_PACK_RECORD) &&
3335 field_length >= 4 &&
3336 orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR;
3337 }
3338 public:
3339 bool can_alter_field_type;
Field_string(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)3340 Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
3341 uchar null_bit_arg,
3342 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3343 const DTCollation &collation)
3344 :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3345 unireg_check_arg, field_name_arg, collation),
3346 can_alter_field_type(1) {};
Field_string(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,const DTCollation & collation)3347 Field_string(uint32 len_arg,bool maybe_null_arg,
3348 const LEX_CSTRING *field_name_arg,
3349 const DTCollation &collation)
3350 :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3351 NONE, field_name_arg, collation),
3352 can_alter_field_type(1) {};
3353
type_handler()3354 const Type_handler *type_handler() const
3355 {
3356 if (is_var_string())
3357 return &type_handler_var_string;
3358 return &type_handler_string;
3359 }
key_type()3360 enum ha_base_keytype key_type() const
3361 { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
zero_pack()3362 bool zero_pack() const { return 0; }
3363 Copy_func *get_copy_func(const Field *from) const;
reset(void)3364 int reset(void)
3365 {
3366 charset()->cset->fill(charset(),(char*) ptr, field_length,
3367 (has_charset() ? ' ' : 0));
3368 return 0;
3369 }
3370 int store(const char *to,size_t length,CHARSET_INFO *charset);
3371 using Field_str::store;
3372 double val_real(void);
3373 longlong val_int(void);
3374 String *val_str(String*,String *);
3375 my_decimal *val_decimal(my_decimal *);
3376 int cmp(const uchar *,const uchar *);
3377 void sort_string(uchar *buff,uint length);
3378 void sql_type(String &str) const;
3379 void sql_rpl_type(String*) const;
3380 virtual uchar *pack(uchar *to, const uchar *from,
3381 uint max_length);
3382 virtual const uchar *unpack(uchar* to, const uchar *from,
3383 const uchar *from_end,uint param_data);
pack_length_from_metadata(uint field_metadata)3384 uint pack_length_from_metadata(uint field_metadata)
3385 {
3386 DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
3387 if (field_metadata == 0)
3388 return row_pack_length();
3389 return (((field_metadata >> 4) & 0x300) ^ 0x300) + (field_metadata & 0x00ff);
3390 }
3391 bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
3392 uint16 mflags, int *order_var);
row_pack_length()3393 uint row_pack_length() const { return field_length; }
3394 int pack_cmp(const uchar *a,const uchar *b,uint key_length,
3395 bool insert_or_update);
3396 int pack_cmp(const uchar *b,uint key_length,bool insert_or_update);
3397 uint packed_col_length(const uchar *to, uint length);
3398 uint max_packed_col_length(uint max_length);
size_of()3399 uint size_of() const { return sizeof(*this); }
has_charset(void)3400 bool has_charset(void) const
3401 { return charset() == &my_charset_bin ? FALSE : TRUE; }
3402 Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
3403 virtual uint get_key_image(uchar *buff,uint length, imagetype type);
3404 sql_mode_t value_depends_on_sql_mode() const;
3405 sql_mode_t can_handle_sql_mode_dependency_on_store() const;
3406 private:
3407 int save_field_metadata(uchar *first_byte);
3408 };
3409
3410
3411 class Field_varstring :public Field_longstr {
3412 public:
get_data()3413 uchar *get_data() const
3414 {
3415 return ptr + length_bytes;
3416 }
get_length()3417 uint get_length() const
3418 {
3419 return length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
3420 }
3421 protected:
store_length(uint32 number)3422 void store_length(uint32 number)
3423 {
3424 if (length_bytes == 1)
3425 *ptr= (uchar) number;
3426 else
3427 int2store(ptr, number);
3428 }
3429 public:
3430 /*
3431 The maximum space available in a Field_varstring, in bytes. See
3432 length_bytes.
3433 */
3434 static const uint MAX_SIZE;
3435 /* Store number of bytes used to store length (1 or 2) */
3436 uint32 length_bytes;
Field_varstring(uchar * ptr_arg,uint32 len_arg,uint length_bytes_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,const DTCollation & collation)3437 Field_varstring(uchar *ptr_arg,
3438 uint32 len_arg, uint length_bytes_arg,
3439 uchar *null_ptr_arg, uchar null_bit_arg,
3440 enum utype unireg_check_arg, const LEX_CSTRING *field_name_arg,
3441 TABLE_SHARE *share, const DTCollation &collation)
3442 :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3443 unireg_check_arg, field_name_arg, collation),
3444 length_bytes(length_bytes_arg)
3445 {
3446 share->varchar_fields++;
3447 }
Field_varstring(uint32 len_arg,bool maybe_null_arg,const LEX_CSTRING * field_name_arg,TABLE_SHARE * share,const DTCollation & collation)3448 Field_varstring(uint32 len_arg,bool maybe_null_arg,
3449 const LEX_CSTRING *field_name_arg,
3450 TABLE_SHARE *share, const DTCollation &collation)
3451 :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3452 NONE, field_name_arg, collation),
3453 length_bytes(len_arg < 256 ? 1 :2)
3454 {
3455 share->varchar_fields++;
3456 }
3457
type_handler()3458 const Type_handler *type_handler() const { return &type_handler_varchar; }
3459 enum ha_base_keytype key_type() const;
row_pack_length()3460 uint row_pack_length() const { return field_length; }
zero_pack()3461 bool zero_pack() const { return 0; }
reset(void)3462 int reset(void) { bzero(ptr,field_length+length_bytes); return 0; }
pack_length()3463 uint32 pack_length() const { return (uint32) field_length+length_bytes; }
key_length()3464 uint32 key_length() const { return (uint32) field_length; }
sort_length()3465 uint32 sort_length() const
3466 {
3467 return (uint32) field_length + (field_charset == &my_charset_bin ?
3468 length_bytes : 0);
3469 }
3470 Copy_func *get_copy_func(const Field *from) const;
3471 bool memcpy_field_possible(const Field *from) const;
3472 int store(const char *to,size_t length,CHARSET_INFO *charset);
3473 using Field_str::store;
3474 #ifdef HAVE_valgrind
3475 void mark_unused_memory_as_defined();
3476 #endif
3477 double val_real(void);
3478 longlong val_int(void);
3479 String *val_str(String*,String *);
3480 my_decimal *val_decimal(my_decimal *);
3481 int cmp(const uchar *a,const uchar *b);
3482 int cmp_prefix(const uchar *a, const uchar *b, size_t prefix_len);
3483 void sort_string(uchar *buff,uint length);
3484 uint get_key_image(uchar *buff,uint length, imagetype type);
3485 void set_key_image(const uchar *buff,uint length);
3486 void sql_type(String &str) const;
3487 void sql_rpl_type(String*) const;
3488 virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
3489 virtual const uchar *unpack(uchar* to, const uchar *from,
3490 const uchar *from_end, uint param_data);
3491 int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0U);
3492 int key_cmp(const uchar *,const uchar*);
3493 int key_cmp(const uchar *str, uint length);
3494 uint packed_col_length(const uchar *to, uint length);
3495 uint max_packed_col_length(uint max_length);
3496 uint32 data_length();
size_of()3497 uint size_of() const { return sizeof(*this); }
has_charset(void)3498