This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathfield.h
3019 lines (2773 loc) · 107 KB
/
field.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef FIELD_INCLUDED
#define FIELD_INCLUDED
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates.
Copyright (c) 2008, 2014, SkySQL Ab.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/*
Because of the function new_field() all field classes that have static
variables must declare the size_of() member function.
*/
#ifdef USE_PRAGMA_INTERFACE
#pragma interface /* gcc class implementation */
#endif
#include "mysqld.h" /* system_charset_info */
#include "table.h" /* TABLE */
#include "sql_string.h" /* String */
#include "my_decimal.h" /* my_decimal */
#include "sql_error.h" /* Sql_condition */
#include "compat56.h"
class Send_field;
class Protocol;
class Create_field;
class Relay_log_info;
class Field;
class Column_statistics;
class Column_statistics_collected;
enum enum_check_fields
{
CHECK_FIELD_IGNORE,
CHECK_FIELD_WARN,
CHECK_FIELD_ERROR_FOR_NULL
};
enum Derivation
{
DERIVATION_IGNORABLE= 6,
DERIVATION_NUMERIC= 5,
DERIVATION_COERCIBLE= 4,
DERIVATION_SYSCONST= 3,
DERIVATION_IMPLICIT= 2,
DERIVATION_NONE= 1,
DERIVATION_EXPLICIT= 0
};
#define STORAGE_TYPE_MASK 7
#define COLUMN_FORMAT_MASK 7
#define COLUMN_FORMAT_SHIFT 3
#define my_charset_numeric my_charset_latin1
#define MY_REPERTOIRE_NUMERIC MY_REPERTOIRE_ASCII
/* The length of the header part for each virtual column in the .frm file */
#define FRM_VCOL_HEADER_SIZE(b) (3 + MY_TEST(b))
class Count_distinct_field;
struct ha_field_option_struct;
struct st_cache_field;
int field_conv(Field *to,Field *from);
int field_conv_incompatible(Field *to,Field *from);
bool memcpy_field_possible(Field *to, Field *from);
int truncate_double(double *nr, uint field_length, uint dec,
bool unsigned_flag, double max_value);
longlong double_to_longlong(double nr, bool unsigned_flag, bool *error);
inline uint get_enum_pack_length(int elements)
{
return elements < 256 ? 1 : 2;
}
inline uint get_set_pack_length(int elements)
{
uint len= (elements + 7) / 8;
return len > 4 ? 8 : len;
}
/**
Tests if field type is temporal and has date part,
i.e. represents DATE, DATETIME or TIMESTAMP types in SQL.
@param type Field type, as returned by field->type().
@retval true If field type is temporal type with date part.
@retval false If field type is not temporal type with date part.
*/
inline bool is_temporal_type_with_date(enum_field_types type)
{
switch (type)
{
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
return true;
case MYSQL_TYPE_DATETIME2:
case MYSQL_TYPE_TIMESTAMP2:
DBUG_ASSERT(0); // field->real_type() should not get to here.
default:
return false;
}
}
/**
Recognizer for concrete data type (called real_type for some reason),
returning true if it is one of the TIMESTAMP types.
*/
inline bool is_timestamp_type(enum_field_types type)
{
return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2;
}
/**
Convert temporal real types as retuned by field->real_type()
to field type as returned by field->type().
@param real_type Real type.
@retval Field type.
*/
inline enum_field_types real_type_to_type(enum_field_types real_type)
{
switch (real_type)
{
case MYSQL_TYPE_TIME2:
return MYSQL_TYPE_TIME;
case MYSQL_TYPE_DATETIME2:
return MYSQL_TYPE_DATETIME;
case MYSQL_TYPE_TIMESTAMP2:
return MYSQL_TYPE_TIMESTAMP;
case MYSQL_TYPE_NEWDATE:
return MYSQL_TYPE_DATE;
/* Note: NEWDECIMAL is a type, not only a real_type */
default: return real_type;
}
}
static inline enum enum_mysql_timestamp_type
mysql_type_to_time_type(enum enum_field_types mysql_type)
{
switch(mysql_type) {
case MYSQL_TYPE_TIME2:
case MYSQL_TYPE_TIME: return MYSQL_TIMESTAMP_TIME;
case MYSQL_TYPE_TIMESTAMP2:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATETIME2:
case MYSQL_TYPE_DATETIME: return MYSQL_TIMESTAMP_DATETIME;
case MYSQL_TYPE_NEWDATE:
case MYSQL_TYPE_DATE: return MYSQL_TIMESTAMP_DATE;
default: return MYSQL_TIMESTAMP_ERROR;
}
}
/**
Tests if field type is temporal, i.e. represents
DATE, TIME, DATETIME or TIMESTAMP types in SQL.
@param type Field type, as returned by field->type().
@retval true If field type is temporal
@retval false If field type is not temporal
*/
inline bool is_temporal_type(enum_field_types type)
{
return mysql_type_to_time_type(type) != MYSQL_TIMESTAMP_ERROR;
}
/*
Virtual_column_info is the class to contain additional
characteristics that is specific for a virtual/computed
field such as:
- the defining expression that is evaluated to compute the value
of the field
- whether the field is to be stored in the database
- whether the field is used in a partitioning expression
*/
class Virtual_column_info: public Sql_alloc
{
private:
/*
The following data is only updated by the parser and read
when a Create_field object is created/initialized.
*/
enum_field_types field_type; /* Real field type*/
/* Flag indicating that the field is physically stored in the database */
bool stored_in_db;
/* Flag indicating that the field used in a partitioning expression */
bool in_partitioning_expr;
public:
/* The expression to compute the value of the virtual column */
Item *expr_item;
/* Text representation of the defining expression */
LEX_STRING expr_str;
Virtual_column_info()
: field_type((enum enum_field_types)MYSQL_TYPE_VIRTUAL),
stored_in_db(FALSE), in_partitioning_expr(FALSE),
expr_item(NULL)
{
expr_str.str= NULL;
expr_str.length= 0;
};
~Virtual_column_info() {}
enum_field_types get_real_type()
{
return field_type;
}
void set_field_type(enum_field_types fld_type)
{
/* Calling this function can only be done once. */
field_type= fld_type;
}
bool is_stored()
{
return stored_in_db;
}
void set_stored_in_db_flag(bool stored)
{
stored_in_db= stored;
}
bool is_in_partitioning_expr()
{
return in_partitioning_expr;
}
void mark_as_in_partitioning_expr()
{
in_partitioning_expr= TRUE;
}
};
class Field
{
Field(const Item &); /* Prevent use of these */
void operator=(Field &);
public:
static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
{ return alloc_root(mem_root, size); }
static void *operator new(size_t size) throw ()
{ return sql_alloc(size); }
static void operator delete(void *ptr_arg, size_t size) { TRASH(ptr_arg, size); }
static void operator delete(void *ptr, MEM_ROOT *mem_root)
{ DBUG_ASSERT(0); }
uchar *ptr; // Position to field in record
/**
Byte where the @c NULL bit is stored inside a record. If this Field is a
@c NOT @c NULL field, this member is @c NULL.
*/
uchar *null_ptr;
/*
Note that you can use table->in_use as replacement for current_thd member
only inside of val_*() and store() members (e.g. you can't use it in cons)
*/
TABLE *table; // Pointer for table
TABLE *orig_table; // Pointer to original table
const char * const *table_name;
const char *field_name;
/** reference to the list of options or NULL */
engine_option_value *option_list;
ha_field_option_struct *option_struct; /* structure with parsed options */
LEX_STRING comment;
/* Field is part of the following keys */
key_map key_start, part_of_key, part_of_key_not_clustered;
key_map part_of_sortkey;
/*
We use three additional unireg types for TIMESTAMP to overcome limitation
of current binary format of .frm file. We'd like to be able to support
NOW() as default and on update value for such fields but unable to hold
this info anywhere except unireg_check field. This issue will be resolved
in more clean way with transition to new text based .frm format.
See also comment for Field_timestamp::Field_timestamp().
*/
enum utype { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
enum geometry_type
{
GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
GEOM_GEOMETRYCOLLECTION = 7
};
enum imagetype { itRAW, itMBR};
utype unireg_check;
uint32 field_length; // Length of field
uint32 flags;
uint16 field_index; // field number in fields array
uchar null_bit; // Bit used to test null bit
/**
If true, this field was created in create_tmp_field_from_item from a NULL
value. This means that the type of the field is just a guess, and the type
may be freely coerced to another type.
@see create_tmp_field_from_item
@see Item_type_holder::get_real_type
*/
bool is_created_from_null_item;
/* TRUE in Field objects created for column min/max values */
bool is_stat_field;
/*
Selectivity of the range condition over this field.
When calculating this selectivity a range predicate
is taken into account only if:
- it is extracted from the WHERE clause
- it depends only on the table the field belongs to
*/
double cond_selectivity;
/*
The next field in the class of equal fields at the top AND level
of the WHERE clause
*/
Field *next_equal_field;
/*
This structure is used for statistical data on the column
that has been read from the statistical table column_stat
*/
Column_statistics *read_stats;
/*
This structure is used for statistical data on the column that
is collected by the function collect_statistics_for_table
*/
Column_statistics_collected *collected_stats;
/*
This is additional data provided for any computed(virtual) field.
In particular it includes a pointer to the item by which this field
can be computed from other fields.
*/
Virtual_column_info *vcol_info;
/*
Flag indicating that the field is physically stored in tables
rather than just computed from other fields.
As of now, FALSE can be set only for computed virtual columns.
*/
bool stored_in_db;
Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
uchar null_bit_arg, utype unireg_check_arg,
const char *field_name_arg);
virtual ~Field() {}
/* Store functions returns 1 on overflow and -1 on fatal error */
virtual int store(const char *to, uint length,CHARSET_INFO *cs)=0;
virtual int store(double nr)=0;
virtual int store(longlong nr, bool unsigned_val)=0;
virtual int store_decimal(const my_decimal *d)=0;
virtual int store_time_dec(MYSQL_TIME *ltime, uint dec);
int store_time(MYSQL_TIME *ltime)
{ return store_time_dec(ltime, TIME_SECOND_PART_DIGITS); }
int store(const char *to, uint length, CHARSET_INFO *cs,
enum_check_fields check_level);
virtual double val_real(void)=0;
virtual longlong val_int(void)=0;
virtual my_decimal *val_decimal(my_decimal *);
inline String *val_str(String *str) { return val_str(str, str); }
/*
val_str(buf1, buf2) gets two buffers and should use them as follows:
if it needs a temp buffer to convert result to string - use buf1
example Field_tiny::val_str()
if the value exists as a string already - use buf2
example Field_string::val_str()
consequently, buf2 may be created as 'String buf;' - no memory
will be allocated for it. buf1 will be allocated to hold a
value if it's too small. Using allocated buffer for buf2 may result in
an unnecessary free (and later, may be an alloc).
This trickery is used to decrease a number of malloc calls.
*/
virtual String *val_str(String*,String *)=0;
String *val_int_as_str(String *val_buffer, bool unsigned_flag);
/*
str_needs_quotes() returns TRUE if the value returned by val_str() needs
to be quoted when used in constructing an SQL query.
*/
virtual bool str_needs_quotes() { return FALSE; }
virtual Item_result result_type () const=0;
virtual Item_result cmp_type () const { return result_type(); }
static bool type_can_have_key_part(enum_field_types);
static enum_field_types field_type_merge(enum_field_types, enum_field_types);
static Item_result result_merge_type(enum_field_types);
virtual bool eq(Field *field)
{
return (ptr == field->ptr && null_ptr == field->null_ptr &&
null_bit == field->null_bit && field->type() == type());
}
virtual bool eq_def(Field *field);
/*
pack_length() returns size (in bytes) used to store field data in memory
(i.e. it returns the maximum size of the field in a row of the table,
which is located in RAM).
*/
virtual uint32 pack_length() const { return (uint32) field_length; }
/*
pack_length_in_rec() returns size (in bytes) used to store field data on
storage (i.e. it returns the maximal size of the field in a row of the
table, which is located on disk).
*/
virtual uint32 pack_length_in_rec() const { return pack_length(); }
virtual bool compatible_field_size(uint metadata, Relay_log_info *rli,
uint16 mflags, int *order);
virtual uint pack_length_from_metadata(uint field_metadata)
{
DBUG_ENTER("Field::pack_length_from_metadata");
DBUG_RETURN(field_metadata);
}
virtual uint row_pack_length() const { return 0; }
virtual int save_field_metadata(uchar *first_byte)
{ return do_save_field_metadata(first_byte); }
/*
data_length() return the "real size" of the data in memory.
*/
virtual uint32 data_length() { return pack_length(); }
virtual uint32 sort_length() const { return pack_length(); }
/*
Get the number bytes occupied by the value in the field.
CHAR values are stripped of trailing spaces.
Flexible values are stripped of their length.
*/
virtual uint32 value_length()
{
uint len;
if (!zero_pack() &&
(type() == MYSQL_TYPE_STRING &&
(len= pack_length()) >= 4 && len < 256))
{
uchar *str, *end;
for (str= ptr, end= str+len; end > str && end[-1] == ' '; end--) {}
len=(uint) (end-str);
return len;
}
return data_length();
}
/**
Get the maximum size of the data in packed format.
@return Maximum data length of the field when packed using the
Field::pack() function.
*/
virtual uint32 max_data_length() const {
return pack_length();
};
virtual int reset(void) { bzero(ptr,pack_length()); return 0; }
virtual void reset_fields() {}
virtual void set_default()
{
my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
table->record[0]);
memcpy(ptr, ptr + l_offset, pack_length());
if (null_ptr)
*null_ptr= ((*null_ptr & (uchar) ~null_bit) |
(null_ptr[l_offset] & null_bit));
}
bool has_insert_default_function() const
{
return unireg_check == TIMESTAMP_DN_FIELD ||
unireg_check == TIMESTAMP_DNUN_FIELD;
}
bool has_update_default_function() const
{
return unireg_check == TIMESTAMP_UN_FIELD ||
unireg_check == TIMESTAMP_DNUN_FIELD;
}
/*
Mark the field as having a value supplied by the client, thus it should
not be auto-updated.
*/
void set_has_explicit_value()
{
flags|= HAS_EXPLICIT_VALUE;
}
virtual void set_explicit_default(Item *value);
/**
Evaluates the @c INSERT default function and stores the result in the
field. If no such function exists for the column, or the function is not
valid for the column's data type, invoking this function has no effect.
*/
virtual int evaluate_insert_default_function() { return 0; }
/**
Evaluates the @c UPDATE default function, if one exists, and stores the
result in the record buffer. If no such function exists for the column,
or the function is not valid for the column's data type, invoking this
function has no effect.
*/
virtual int evaluate_update_default_function() { return 0; }
virtual bool binary() const { return 1; }
virtual bool zero_pack() const { return 1; }
virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
virtual uint32 key_length() const { return pack_length(); }
virtual enum_field_types type() const =0;
virtual enum_field_types real_type() const { return type(); }
virtual enum_field_types binlog_type() const
{
/*
Binlog stores field->type() as type code by default. For example,
it puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
with extra data type details put into metadata.
Binlog behaviour slightly differs between various MySQL and MariaDB
versions for the temporal data types TIME, DATETIME and TIMESTAMP.
MySQL prior to 5.6 uses MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
and MYSQL_TYPE_TIMESTAMP type codes in binlog and stores no
additional metadata.
MariaDB-5.3 implements new versions for TIME, DATATIME, TIMESTAMP
with fractional second precision, but uses the old format for the
types TIME(0), DATETIME(0), TIMESTAMP(0), and it still stores
MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME and MYSQL_TYPE_TIMESTAMP in binlog,
with no additional metadata.
So row-based replication between temporal data types of
different precision is not possible in MariaDB.
MySQL-5.6 also implements a new version of TIME, DATETIME, TIMESTAMP
which support fractional second precision 0..6, and use the new
format even for the types TIME(0), DATETIME(0), TIMESTAMP(0).
For these new data types, MySQL-5.6 stores new type codes
MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2 in binlog,
with fractional precision 0..6 put into metadata.
This makes it in theory possible to do row-based replication between
columns of different fractional precision (e.g. from TIME(1) on master
to TIME(6) on slave). However, it's not currently fully implemented yet.
MySQL-5.6 can only do row-based replication from the old types
TIME, DATETIME, TIMESTAMP (represented by MYSQL_TYPE_TIME,
MYSQL_TYPE_DATETIME and MYSQL_TYPE_TIMESTAMP type codes in binlog)
to the new corresponding types TIME(0), DATETIME(0), TIMESTAMP(0).
Note: MariaDB starting from the version 10.0 understands the new
MySQL-5.6 type codes MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2,
MYSQL_TYPE_TIMESTAMP2. When started over MySQL-5.6 tables both on
master and on slave, MariaDB-10.0 can also do row-based replication
from the old types TIME, DATETIME, TIMESTAMP to the new MySQL-5.6
types TIME(0), DATETIME(0), TIMESTAMP(0).
Note: perhaps binlog should eventually be modified to store
real_type() instead of type() for all column types.
*/
return type();
}
inline int cmp(const uchar *str) { return cmp(ptr,str); }
virtual int cmp_max(const uchar *a, const uchar *b, uint max_len)
{ return cmp(a, b); }
virtual int cmp(const uchar *,const uchar *)=0;
virtual int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L)
{ return memcmp(a,b,pack_length()); }
virtual int cmp_offset(uint row_offset)
{ return cmp(ptr,ptr+row_offset); }
virtual int cmp_binary_offset(uint row_offset)
{ return cmp_binary(ptr, ptr+row_offset); };
virtual int key_cmp(const uchar *a,const uchar *b)
{ return cmp(a, b); }
virtual int key_cmp(const uchar *str, uint length)
{ return cmp(ptr,str); }
/*
Update the value m of the 'min_val' field with the current value v
of this field if force_update is set to TRUE or if v < m.
Return TRUE if the value has been updated.
*/
virtual bool update_min(Field *min_val, bool force_update)
{
bool update_fl= force_update || cmp(ptr, min_val->ptr) < 0;
if (update_fl)
{
min_val->set_notnull();
memcpy(min_val->ptr, ptr, pack_length());
}
return update_fl;
}
/*
Update the value m of the 'max_val' field with the current value v
of this field if force_update is set to TRUE or if v > m.
Return TRUE if the value has been updated.
*/
virtual bool update_max(Field *max_val, bool force_update)
{
bool update_fl= force_update || cmp(ptr, max_val->ptr) > 0;
if (update_fl)
{
max_val->set_notnull();
memcpy(max_val->ptr, ptr, pack_length());
}
return update_fl;
}
virtual void store_field_value(uchar *val, uint len)
{
memcpy(ptr, val, len);
}
virtual uint decimals() const { return 0; }
/*
Caller beware: sql_type can change str.Ptr, so check
ptr() to see if it changed if you are using your own buffer
in str and restore it with set() if needed
*/
virtual void sql_type(String &str) const =0;
virtual uint size_of() const =0; // For new field
inline bool is_null(my_ptrdiff_t row_offset= 0) const
{
/*
The table may have been marked as containing only NULL values
for all fields if it is a NULL-complemented row of an OUTER JOIN
or if the query is an implicitly grouped query (has aggregate
functions but no GROUP BY clause) with no qualifying rows. If
this is the case (in which TABLE::null_row is true), the field
is considered to be NULL.
Note that if a table->null_row is set then also all null_bits are
set for the row.
Otherwise, if the field is NULLable, it has a valid null_ptr
pointer, and its NULLity is recorded in the "null_bit" bit of
null_ptr[row_offset].
*/
return (table->null_row ? TRUE :
null_ptr ? MY_TEST(null_ptr[row_offset] & null_bit) : 0);
}
inline bool is_real_null(my_ptrdiff_t row_offset= 0) const
{ return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
inline bool is_null_in_record(const uchar *record) const
{
if (!null_ptr)
return 0;
return MY_TEST(record[(uint) (null_ptr - table->record[0])] & null_bit);
}
inline void set_null(my_ptrdiff_t row_offset= 0)
{ if (null_ptr) null_ptr[row_offset]|= null_bit; }
inline void set_notnull(my_ptrdiff_t row_offset= 0)
{ if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
inline bool maybe_null(void) const
{ return null_ptr != 0 || table->maybe_null; }
/* @return true if this field is NULL-able, false otherwise. */
inline bool real_maybe_null(void) const { return null_ptr != 0; }
uint null_offset(const uchar *record) const
{ return (uint) (null_ptr - record); }
uint null_offset() const
{ return null_offset(table->record[0]); }
void set_null_ptr(uchar *p_null_ptr, uint p_null_bit)
{
null_ptr= p_null_ptr;
null_bit= p_null_bit;
}
inline THD *get_thd() { return table ? table->in_use : current_thd; }
enum {
LAST_NULL_BYTE_UNDEF= 0
};
/*
Find the position of the last null byte for the field.
SYNOPSIS
last_null_byte()
DESCRIPTION
Return a pointer to the last byte of the null bytes where the
field conceptually is placed.
RETURN VALUE
The position of the last null byte relative to the beginning of
the record. If the field does not use any bits of the null
bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
*/
size_t last_null_byte() const {
size_t bytes= do_last_null_byte();
DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
DBUG_ASSERT(bytes <= table->s->null_bytes);
return bytes;
}
void make_sort_key(uchar *buff, uint length);
virtual void make_field(Send_field *);
virtual void sort_string(uchar *buff,uint length)=0;
virtual bool optimize_range(uint idx, uint part);
virtual void free() {}
virtual Field *new_field(MEM_ROOT *root, TABLE *new_table,
bool keep_type);
virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
uchar *new_ptr, uint32 length,
uchar *new_null_ptr, uint new_null_bit);
Field *clone(MEM_ROOT *mem_root, TABLE *new_table);
Field *clone(MEM_ROOT *mem_root, TABLE *new_table, my_ptrdiff_t diff,
bool stat_flag= FALSE);
Field *clone(MEM_ROOT *mem_root, my_ptrdiff_t diff);
inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
{
ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
}
inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
virtual void move_field_offset(my_ptrdiff_t ptr_diff)
{
ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
if (null_ptr)
null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
}
virtual void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
{ memcpy(buff,ptr,length); }
virtual void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
{ memcpy(ptr,buff,length); }
/*
Copy a field part into an output buffer.
SYNOPSIS
Field::get_key_image()
buff [out] output buffer
length output buffer size
type itMBR for geometry blobs, otherwise itRAW
DESCRIPTION
This function makes a copy of field part of size equal to or
less than "length" parameter value.
For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
is padded by zero byte.
NOTES
For variable length character fields (i.e. UTF-8) the "length"
parameter means a number of output buffer bytes as if all field
characters have maximal possible size (mbmaxlen). In the other words,
"length" parameter is a number of characters multiplied by
field_charset->mbmaxlen.
RETURN
Number of copied bytes (excluding padded zero bytes -- see above).
*/
virtual uint get_key_image(uchar *buff, uint length, imagetype type_arg)
{
get_image(buff, length, &my_charset_bin);
return length;
}
virtual void set_key_image(const uchar *buff,uint length)
{ set_image(buff,length, &my_charset_bin); }
inline longlong val_int_offset(uint row_offset)
{
ptr+=row_offset;
longlong tmp=val_int();
ptr-=row_offset;
return tmp;
}
inline longlong val_int(const uchar *new_ptr)
{
uchar *old_ptr= ptr;
longlong return_value;
ptr= (uchar*) new_ptr;
return_value= val_int();
ptr= old_ptr;
return return_value;
}
inline String *val_str(String *str, const uchar *new_ptr)
{
uchar *old_ptr= ptr;
ptr= (uchar*) new_ptr;
val_str(str);
ptr= old_ptr;
return str;
}
virtual bool send_binary(Protocol *protocol);
virtual uchar *pack(uchar *to, const uchar *from, uint max_length);
/**
@overload Field::pack(uchar*, const uchar*, uint, bool)
*/
uchar *pack(uchar *to, const uchar *from)
{
DBUG_ENTER("Field::pack");
uchar *result= this->pack(to, from, UINT_MAX);
DBUG_RETURN(result);
}
virtual const uchar *unpack(uchar* to, const uchar *from,
const uchar *from_end, uint param_data=0);
virtual uint packed_col_length(const uchar *to, uint length)
{ return length;}
virtual uint max_packed_col_length(uint max_length)
{ return max_length;}
uint offset(uchar *record)
{
return (uint) (ptr - record);
}
void copy_from_tmp(int offset);
uint fill_cache_field(struct st_cache_field *copy);
virtual bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
bool get_time(MYSQL_TIME *ltime) { return get_date(ltime, TIME_TIME_ONLY); }
virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
virtual CHARSET_INFO *charset_for_protocol(void) const
{ return binary() ? &my_charset_bin : charset(); }
virtual CHARSET_INFO *sort_charset(void) const { return charset(); }
virtual bool has_charset(void) const { return FALSE; }
/*
match_collation_to_optimize_range() is to distinguish in
range optimizer (see opt_range.cc) between real string types:
CHAR, VARCHAR, TEXT
and the other string-alike types with result_type() == STRING_RESULT:
DATE, TIME, DATETIME, TIMESTAMP
We need it to decide whether to test if collation of the operation
matches collation of the field (needed only for real string types).
*/
virtual bool match_collation_to_optimize_range() const { return false; }
virtual void set_charset(CHARSET_INFO *charset_arg) { }
virtual enum Derivation derivation(void) const
{ return DERIVATION_IMPLICIT; }
virtual uint repertoire(void) const { return MY_REPERTOIRE_UNICODE30; }
virtual void set_derivation(enum Derivation derivation_arg) { }
virtual int set_time() { return 1; }
bool set_warning(Sql_condition::enum_warning_level, unsigned int code,
int cuted_increment) const;
void set_datetime_warning(Sql_condition::enum_warning_level, uint code,
const ErrConv *str, timestamp_type ts_type,
int cuted_increment);
inline bool check_overflow(int op_result)
{
return (op_result == E_DEC_OVERFLOW);
}
int warn_if_overflow(int op_result);
void set_table_name(String *alias)
{
table_name= &alias->Ptr;
}
void init(TABLE *table_arg)
{
orig_table= table= table_arg;
set_table_name(&table_arg->alias);
}
/* maximum possible display length */
virtual uint32 max_display_length()= 0;
/**
Whether a field being created is compatible with a existing one.
Used by the ALTER TABLE code to evaluate whether the new definition
of a table is compatible with the old definition so that it can
determine if data needs to be copied over (table data change).
*/
virtual uint is_equal(Create_field *new_field);
/* convert decimal to longlong with overflow check */
longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
int *err);
/* The max. number of characters */
virtual uint32 char_length()
{
return field_length / charset()->mbmaxlen;
}
virtual geometry_type get_geometry_type()
{
/* shouldn't get here. */
DBUG_ASSERT(0);
return GEOM_GEOMETRY;
}
ha_storage_media field_storage_type() const
{
return (ha_storage_media)
((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
}
void set_storage_type(ha_storage_media storage_type_arg)
{
DBUG_ASSERT(field_storage_type() == HA_SM_DEFAULT);
flags |= (storage_type_arg << FIELD_FLAGS_STORAGE_MEDIA);
}
column_format_type column_format() const
{
return (column_format_type)
((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
}
void set_column_format(column_format_type column_format_arg)
{
DBUG_ASSERT(column_format() == COLUMN_FORMAT_TYPE_DEFAULT);
flags |= (column_format_arg << FIELD_FLAGS_COLUMN_FORMAT);
}
key_map get_possible_keys();
/* Hash value */
virtual void hash(ulong *nr, ulong *nr2);
/* Check whether the field can be used as a join attribute in hash join */
virtual bool hash_join_is_possible() { return TRUE; }
virtual bool eq_cmp_as_binary() { return TRUE; }
/* Position of the field value within the interval of [min, max] */
virtual double pos_in_interval(Field *min, Field *max)
{
return (double) 0.5;
}
friend int cre_myisam(char * name, register TABLE *form, uint options,
ulonglong auto_increment_value);
friend class Copy_field;
friend class Item_avg_field;
friend class Item_std_field;
friend class Item_sum_num;
friend class Item_sum_sum;
friend class Item_sum_str;
friend class Item_sum_count;
friend class Item_sum_avg;
friend class Item_sum_std;
friend class Item_sum_min;
friend class Item_sum_max;
friend class Item_func_group_concat;
private:
/*
Primitive for implementing last_null_byte().
SYNOPSIS
do_last_null_byte()
DESCRIPTION
Primitive for the implementation of the last_null_byte()
function. This represents the inheritance interface and can be
overridden by subclasses.
*/
virtual size_t do_last_null_byte() const;
/**
Retrieve the field metadata for fields.
This default implementation returns 0 and saves 0 in the metadata_ptr
value.
@param metadata_ptr First byte of field metadata
@returns 0 no bytes written.
*/
virtual int do_save_field_metadata(uchar *metadata_ptr)
{ return 0; }
protected:
uchar *pack_int(uchar *to, const uchar *from, size_t size)
{
memcpy(to, from, size);
return to + size;
}
const uchar *unpack_int(uchar* to, const uchar *from,
const uchar *from_end, size_t size)
{
if (from + size > from_end)
return 0;
memcpy(to, from, size);
return from + size;
}
uchar *pack_int16(uchar *to, const uchar *from)
{ return pack_int(to, from, 2); }
const uchar *unpack_int16(uchar* to, const uchar *from, const uchar *from_end)
{ return unpack_int(to, from, from_end, 2); }
uchar *pack_int24(uchar *to, const uchar *from)
{ return pack_int(to, from, 3); }
const uchar *unpack_int24(uchar* to, const uchar *from, const uchar *from_end)
{ return unpack_int(to, from, from_end, 3); }
uchar *pack_int32(uchar *to, const uchar *from)
{ return pack_int(to, from, 4); }
const uchar *unpack_int32(uchar* to, const uchar *from, const uchar *from_end)