-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathibdNinja.cc
4095 lines (3738 loc) · 130 KB
/
ibdNinja.cc
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
/*
* Copyright (c) [2025] [Zhao Song]
*/
#include "ibdNinja.h"
#include "ibdCollations.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <zlib.h>
#include <rapidjson/error/en.h>
#include <algorithm>
namespace ibd_ninja {
#define ninja_warn(format, ...) \
fprintf(stderr, "[WARNING] %s:%d - " format "\n", \
__FILE__, __LINE__, ##__VA_ARGS__)
#define ninja_error(format, ...) \
fprintf(stderr, "[ERROR] %s:%d - " format "\n", \
__FILE__, __LINE__, ##__VA_ARGS__)
#define ninja_pt(enable, format, ...) \
do { \
if (enable) { \
fprintf(stdout, format, ##__VA_ARGS__); \
} \
} while (0)
/* ------Properties------ */
template <typename GV>
bool ReadValue(bool* ap, const GV& gv) {
if (!gv.IsBool()) {
return false;
}
*ap = gv.GetBool();
return true;
}
template <typename GV>
bool ReadValue(int32_t* ap, const GV& gv) {
if (!gv.IsInt()) {
return false;
}
*ap = gv.GetInt();
return true;
}
template <typename GV>
bool ReadValue(uint32_t* ap, const GV& gv) {
if (!gv.IsUint()) {
return false;
}
*ap = gv.GetUint();
return true;
}
template <typename GV>
bool ReadValue(int64_t* ap, const GV& gv) {
if (!gv.IsInt64()) {
return false;
}
*ap = gv.GetInt64();
return true;
}
template <typename GV>
bool ReadValue(uint64_t* ap, const GV& gv) {
if (!gv.IsUint64()) {
return false;
}
*ap = gv.GetUint64();
return true;
}
template <typename GV>
bool ReadValue(std::string* ap, const GV& gv) {
if (!gv.IsString()) {
return false;
}
*ap = gv.GetString();
return true;
}
template <typename T, typename GV>
bool Read(T* ap, const GV& gv, const char* key) {
if (!gv.HasMember(key)) {
return false;
}
return ReadValue(ap, gv[key]);
}
template <typename ENUM_T, typename GV>
bool ReadEnum(ENUM_T* ap, const GV& gv, const char* key) {
uint64_t v = 0;
if (!Read(&v, gv, key)) {
return false;
}
*ap = static_cast<ENUM_T>(v);
return true;
}
bool Properties::ValidKey(const std::string& key) const {
bool ret = (keys_.empty() || keys_.find(key) != keys_.end());
return ret;
}
bool GetValue(const std::string& value_str, std::string* value) {
*value = value_str;
return true;
}
bool GetValue(const std::string& value_str, bool* value) {
if (value_str == "true") {
*value = true;
return true;
}
if (value_str == "false" || value_str == "0") {
*value = false;
return true;
}
size_t pos = 0;
if (value_str[pos] == '+' || value_str[pos] == '-') {
pos += 1;
}
bool is_digit = std::all_of(value_str.begin() + pos, value_str.end(),
::isdigit);
if (is_digit) {
*value = true;
} else {
return false;
}
return true;
}
template <typename ENUM_T>
bool GetValue(const std::string& value_str, ENUM_T* value) {
uint64_t v = 0;
GetValue(value_str, &v);
*value = static_cast<ENUM_T>(v);
return true;
}
bool GetValue(const std::string& value_str, int32_t* value) {
*value = strtol(value_str.c_str(), nullptr, 10);
return true;
}
bool GetValue(const std::string& value_str, uint32_t* value) {
*value = strtoul(value_str.c_str(), nullptr, 10);
return true;
}
bool GetValue(const std::string& value_str, int64_t* value) {
*value = strtoll(value_str.c_str(), nullptr, 10);
return true;
}
bool GetValue(const std::string& value_str, uint64_t* value) {
*value = strtoull(value_str.c_str(), nullptr, 10);
return true;
}
template <typename T>
bool Properties::Get(const std::string& key, T* value) const {
std::string value_str;
if (!ValidKey(key)) {
assert(false);
return false;
}
const auto& iter = kvs_.find(key);
if (iter == kvs_.end()) {
return false;
} else {
value_str = iter->second;
}
GetValue(value_str, value);
return true;
}
bool Properties::Exists(const std::string& key) const {
if (!ValidKey(key)) {
return false;
}
const auto& iter = kvs_.find(key);
if (iter != kvs_.end()) {
return true;
} else {
return false;
}
}
bool Properties::InsertValues(const std::string& opt_string) {
assert(kvs_.empty());
bool found_key = false;
std::string key = "";
bool found_value = false;
std::string value = "";
size_t last_pos = 0;
for (size_t pos = 0; pos < opt_string.length(); pos++) {
if (!found_key && opt_string[pos] != '=') {
continue;
} else if (opt_string[pos] == '=') {
found_key = true;
if (pos - last_pos > 1) {
if (last_pos == 0) {
key = std::string(&opt_string[last_pos], pos - last_pos);
} else {
key = std::string(&opt_string[last_pos + 1], pos - last_pos - 1);
}
}
last_pos = pos;
continue;
}
if (!found_value && opt_string[pos] != ';') {
continue;
} else if (opt_string[pos] == ';') {
found_value = true;
if (pos - last_pos > 1) {
if (last_pos == 0) {
value = std::string(&opt_string[last_pos], pos - last_pos);
} else {
value = std::string(&opt_string[last_pos + 1], pos - last_pos - 1);
}
}
last_pos = pos;
}
assert(found_key && found_value);
if (key.empty()) {
std::cerr << "[SDI]Found empty Properties::key" << std::endl;
return false;
}
if (ValidKey(key)) {
kvs_[key] = value;
key.clear();
found_key = false;
value.clear();
found_value = false;
} else {
std::cerr << "[SDI]Found invalid Properties::key, "
<< key << std::endl;
return false;
}
}
return true;
}
template <typename PP, typename GV>
bool ReadProperties(PP* pp, const GV& gv, const char* key) {
std::string opt_string;
if (Read(&opt_string, gv, key)) {
return pp->InsertValues(opt_string);
} else {
return false;
}
}
/* ------ Column ------ */
const std::set<std::string> Column::default_valid_option_keys = {
"column_format",
"geom_type",
"interval_count",
"not_secondary",
"storage",
"treat_bit_as_char",
"is_array",
"gipk" /* generated implicit primary key column */
};
bool Column::Init(const rapidjson::Value& dd_col_obj) {
Read(&dd_name_, dd_col_obj, "name");
ReadEnum(&dd_type_, dd_col_obj, "type");
Read(&dd_is_nullable_, dd_col_obj, "is_nullable");
Read(&dd_is_zerofill_, dd_col_obj, "is_zerofill");
Read(&dd_is_unsigned_, dd_col_obj, "is_unsigned");
Read(&dd_is_auto_increment_, dd_col_obj, "is_auto_increment");
Read(&dd_is_virtual_, dd_col_obj, "is_virtual");
ReadEnum(&dd_hidden_, dd_col_obj, "hidden");
Read(&dd_ordinal_position_, dd_col_obj, "ordinal_position");
Read(&dd_char_length_, dd_col_obj, "char_length");
Read(&dd_numeric_precision_, dd_col_obj, "numeric_precision");
Read(&dd_numeric_scale_, dd_col_obj, "numeric_scale");
Read(&dd_numeric_scale_null_, dd_col_obj, "numeric_scale_null");
Read(&dd_datetime_precision_, dd_col_obj, "datetime_precision");
Read(&dd_datetime_precision_null_, dd_col_obj, "datetime_precision_null");
Read(&dd_has_no_default_, dd_col_obj, "has_no_default");
Read(&dd_default_value_null_, dd_col_obj, "default_value_null");
Read(&dd_srs_id_null_, dd_col_obj, "srs_id_null");
if (!dd_srs_id_null_) {
uint32_t srs_id = 0;
Read(&srs_id, dd_col_obj, "srs_id");
dd_srs_id_ = srs_id;
}
Read(&dd_default_value_, dd_col_obj, "default_value");
Read(&dd_default_value_utf8_null_, dd_col_obj, "default_value_utf8_null");
Read(&dd_default_value_utf8_, dd_col_obj, "default_value_utf8");
Read(&dd_default_option_, dd_col_obj, "default_option");
Read(&dd_update_option_, dd_col_obj, "update_option");
Read(&dd_comment_, dd_col_obj, "comment");
Read(&dd_generation_expression_, dd_col_obj, "generation_expression");
Read(&dd_generation_expression_utf8_, dd_col_obj,
"generation_expression_utf8");
ReadProperties(&dd_options_, dd_col_obj, "options");
ReadProperties(&dd_se_private_data_, dd_col_obj, "se_private_data");
Read(&dd_engine_attribute_, dd_col_obj, "engine_attribute");
Read(&dd_secondary_engine_attribute_, dd_col_obj,
"secondary_engine_attribute");
ReadEnum(&dd_column_key_, dd_col_obj, "column_key");
Read(&dd_column_type_utf8_, dd_col_obj, "column_type_utf8");
// TODO(Zhao): dd_elements_
if (dd_col_obj.HasMember("elements") && dd_col_obj["elements"].IsArray()) {
dd_elements_size_tmp_ = dd_col_obj["elements"].GetArray().Size();
}
Read(&dd_collation_id_, dd_col_obj, "collation_id");
Read(&dd_is_explicit_collation_, dd_col_obj, "is_explicit_collation");
return true;
}
Column* Column::CreateColumn(const rapidjson::Value& dd_col_obj) {
Column* column = new Column();
bool init_ret = column->Init(dd_col_obj);
if (!init_ret) {
delete column;
column = nullptr;
}
return column;
}
Column::enum_field_types Column::DDType2FieldType(
enum_column_types type) {
switch (type) {
// 31 in total
case enum_column_types::DECIMAL:
return enum_field_types::MYSQL_TYPE_DECIMAL;
case enum_column_types::TINY:
return enum_field_types::MYSQL_TYPE_TINY;
case enum_column_types::SHORT:
return enum_field_types::MYSQL_TYPE_SHORT;
case enum_column_types::LONG:
return enum_field_types::MYSQL_TYPE_LONG;
case enum_column_types::FLOAT:
return enum_field_types::MYSQL_TYPE_FLOAT;
case enum_column_types::DOUBLE:
return enum_field_types::MYSQL_TYPE_DOUBLE;
case enum_column_types::TYPE_NULL:
return enum_field_types::MYSQL_TYPE_NULL;
case enum_column_types::TIMESTAMP:
return enum_field_types::MYSQL_TYPE_TIMESTAMP;
case enum_column_types::LONGLONG:
return enum_field_types::MYSQL_TYPE_LONGLONG;
case enum_column_types::INT24:
return enum_field_types::MYSQL_TYPE_INT24;
case enum_column_types::DATE:
return enum_field_types::MYSQL_TYPE_DATE;
case enum_column_types::TIME:
return enum_field_types::MYSQL_TYPE_TIME;
case enum_column_types::DATETIME:
return enum_field_types::MYSQL_TYPE_DATETIME;
case enum_column_types::YEAR:
return enum_field_types::MYSQL_TYPE_YEAR;
case enum_column_types::NEWDATE:
return enum_field_types::MYSQL_TYPE_NEWDATE;
case enum_column_types::VARCHAR:
return enum_field_types::MYSQL_TYPE_VARCHAR;
case enum_column_types::BIT:
return enum_field_types::MYSQL_TYPE_BIT;
case enum_column_types::TIMESTAMP2:
return enum_field_types::MYSQL_TYPE_TIMESTAMP2;
case enum_column_types::DATETIME2:
return enum_field_types::MYSQL_TYPE_DATETIME2;
case enum_column_types::TIME2:
return enum_field_types::MYSQL_TYPE_TIME2;
case enum_column_types::NEWDECIMAL:
return enum_field_types::MYSQL_TYPE_NEWDECIMAL;
case enum_column_types::ENUM:
return enum_field_types::MYSQL_TYPE_ENUM;
case enum_column_types::SET:
return enum_field_types::MYSQL_TYPE_SET;
case enum_column_types::TINY_BLOB:
return enum_field_types::MYSQL_TYPE_TINY_BLOB;
case enum_column_types::MEDIUM_BLOB:
return enum_field_types::MYSQL_TYPE_MEDIUM_BLOB;
case enum_column_types::LONG_BLOB:
return enum_field_types::MYSQL_TYPE_LONG_BLOB;
case enum_column_types::BLOB:
return enum_field_types::MYSQL_TYPE_BLOB;
case enum_column_types::VAR_STRING:
return enum_field_types::MYSQL_TYPE_VAR_STRING;
case enum_column_types::STRING:
return enum_field_types::MYSQL_TYPE_STRING;
case enum_column_types::GEOMETRY:
return enum_field_types::MYSQL_TYPE_GEOMETRY;
case enum_column_types::JSON:
return enum_field_types::MYSQL_TYPE_JSON;
// TODO(Zhao): support other types
default:
assert(false);
}
}
std::string Column::FieldTypeString() {
enum_field_types field_type = DDType2FieldType(dd_type_);
switch (field_type) {
case MYSQL_TYPE_DECIMAL:
return "DECIMAL";
case MYSQL_TYPE_TINY:
return "TINY";
case MYSQL_TYPE_SHORT:
return "SHORT";
case MYSQL_TYPE_LONG:
return "LONG";
case MYSQL_TYPE_FLOAT:
return "FLOAT";
case MYSQL_TYPE_DOUBLE:
return "DOUBLE";
case MYSQL_TYPE_NULL:
return "NULL";
case MYSQL_TYPE_TIMESTAMP:
return "TIMESTAMP";
case MYSQL_TYPE_LONGLONG:
return "LONGLONG";
case MYSQL_TYPE_INT24:
return "INT24";
case MYSQL_TYPE_DATE:
return "DATE";
case MYSQL_TYPE_TIME:
return "TIME";
case MYSQL_TYPE_DATETIME:
return "DATETIME";
case MYSQL_TYPE_YEAR:
return "YEAR";
case MYSQL_TYPE_NEWDATE:
return "NEWDATE";
case MYSQL_TYPE_VARCHAR:
return "VARCHAR";
case MYSQL_TYPE_BIT:
return "BIT";
case MYSQL_TYPE_TIMESTAMP2:
return "TIMESTAMP2";
case MYSQL_TYPE_DATETIME2:
return "DATETIME2";
case MYSQL_TYPE_TIME2:
return "TIME2";
case MYSQL_TYPE_TYPED_ARRAY:
return "TYPED_ARRAY";
case MYSQL_TYPE_INVALID:
return "INVALID";
case MYSQL_TYPE_BOOL:
return "BOOL";
case MYSQL_TYPE_JSON:
return "JSON";
case MYSQL_TYPE_NEWDECIMAL:
return "NEWDECIMAL";
case MYSQL_TYPE_ENUM:
return "ENUM";
case MYSQL_TYPE_SET:
return "SET";
case MYSQL_TYPE_TINY_BLOB:
return "TINY_BLOB";
case MYSQL_TYPE_MEDIUM_BLOB:
return "MEDIUM_BLOB";
case MYSQL_TYPE_LONG_BLOB:
return "LONG_BLOB";
case MYSQL_TYPE_BLOB:
return "BLOB";
case MYSQL_TYPE_VAR_STRING:
return "VAR_STRING";
case MYSQL_TYPE_STRING:
return "STRING";
case MYSQL_TYPE_GEOMETRY:
return "GEOMETRY";
default:
return "UNKNOWN";
}
}
std::string Column::SeTypeString() {
switch (ib_mtype_) {
case DATA_VARCHAR:
return "DATA_VARCHAR";
case DATA_CHAR:
return "DATA_CHAR";
case DATA_FIXBINARY:
return "DATA_FIXBINARY";
case DATA_BINARY:
return "DATA_BINARY";
case DATA_BLOB:
return "DATA_BLOB";
case DATA_INT:
return "DATA_INT";
case DATA_SYS:
return "DATA_SYS";
case DATA_FLOAT:
return "DATA_FLOAT";
case DATA_DOUBLE:
return "DATA_DOUBLE";
case DATA_DECIMAL:
return "DATA_DECIMAL";
case DATA_VARMYSQL:
return "DATA_VARMYSQL";
case DATA_MYSQL:
return "DATA_MYSQL";
case DATA_GEOMETRY:
return "DATA_GEOMETRY";
case DATA_POINT:
return "DATA_POINT";
case DATA_VAR_POINT:
return "DATA_VAR_POINT";
default:
return "UNKNOWN";
}
}
Column::enum_field_types Column::FieldType() const {
switch (DDType2FieldType(dd_type_)) {
/*
* 30 in total
* Missing MYSQL_TYPE_DATE ?
*/
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
// Field_string -> Field_longstr -> Field_str
return MYSQL_TYPE_STRING;
case MYSQL_TYPE_VARCHAR:
// Field_varstring -> Field_longstr -> Field_str
return MYSQL_TYPE_VARCHAR;
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_LONG_BLOB:
// Field_blob -> Field_longstr -> Field_str
return MYSQL_TYPE_BLOB;
case MYSQL_TYPE_GEOMETRY:
// Field_geom -> Field_blob -> Field_longstr -> Field_str
return MYSQL_TYPE_GEOMETRY;
case MYSQL_TYPE_JSON:
// Field_json -> Field_blob -> Field_longstr -> Field_str
return MYSQL_TYPE_JSON;
case MYSQL_TYPE_ENUM:
// Field_enum -> Field_str
return MYSQL_TYPE_STRING;
case MYSQL_TYPE_SET:
// Field_set -> Field_enum -> Field_str
return MYSQL_TYPE_STRING;
case MYSQL_TYPE_DECIMAL:
// Field_decimal -> Field_real -> Field_num
return MYSQL_TYPE_DECIMAL;
case MYSQL_TYPE_NEWDECIMAL:
// Field_new_decimal -> Field_num
return MYSQL_TYPE_NEWDECIMAL;
case MYSQL_TYPE_FLOAT:
// Field_float -> Field_real -> Field_num
return MYSQL_TYPE_FLOAT;
case MYSQL_TYPE_DOUBLE:
// Field_double -> Field_real -> Field_num
return MYSQL_TYPE_DOUBLE;
case MYSQL_TYPE_TINY:
// Field_tiny -> Field_num
return MYSQL_TYPE_TINY;
case MYSQL_TYPE_SHORT:
// Field_short -> Field_num
return MYSQL_TYPE_SHORT;
case MYSQL_TYPE_INT24:
// Field_medium -> Field_num
return MYSQL_TYPE_INT24;
case MYSQL_TYPE_LONG:
// Field_long -> Field_num
return MYSQL_TYPE_LONG;
case MYSQL_TYPE_LONGLONG:
// Field_longlong -> Field_num
return MYSQL_TYPE_LONGLONG;
case MYSQL_TYPE_TIMESTAMP:
// Field_timestamp -> Field_temporal_with_date_and_time
// -> Field_temporal_with_date
// -> Field_temporal
return MYSQL_TYPE_TIMESTAMP;
case MYSQL_TYPE_TIMESTAMP2:
// Field_timestampf -> Field_temporal_with_date_and_timef
// -> Field_temporal_with_date_and_time
// -> Field_temporal_with_date
// -> Field_temporal
// !!!
return MYSQL_TYPE_TIMESTAMP;
case MYSQL_TYPE_YEAR:
// Field_year -> Field_tiny -> Field_num
return MYSQL_TYPE_YEAR;
case MYSQL_TYPE_NEWDATE:
// Field_newdate -> Field_temporal_with_date
// -> Field_temporal
// !!!
return MYSQL_TYPE_DATE;
case MYSQL_TYPE_TIME:
// Field_time -> Field_time_common
// -> Field_temporal
return MYSQL_TYPE_TIME;
case MYSQL_TYPE_TIME2:
// Field_timef -> Field_time_common
// -> Field_temporal
// !!!
return MYSQL_TYPE_TIME;
case MYSQL_TYPE_DATETIME:
// Field_datetime -> Field_temporal_with_date_and_time
// -> Field_temporal_with_date
// -> Field_temporal
return MYSQL_TYPE_DATETIME;
case MYSQL_TYPE_DATETIME2:
// Field_datetimef -> Field_temporal_with_date_and_timef
// -> Field_temporal_with_date_and_time
// -> Field_temporal_with_date
// -> Field_temporal
// !!!
return MYSQL_TYPE_DATETIME;
case MYSQL_TYPE_NULL:
// Field_null -> Field_str
return MYSQL_TYPE_NULL;
case MYSQL_TYPE_BIT:
// (Field_bit_as_char) -> Field_bit
return MYSQL_TYPE_BIT;
// TODO(Zhao): support other types
// These 2 are not valid user defined in DD
case MYSQL_TYPE_INVALID:
case MYSQL_TYPE_BOOL:
default:
assert(false);
}
}
uint32_t Column::FieldType2SeType() const {
// Check real_type()
if (dd_type_ == enum_column_types::ENUM ||
dd_type_ == enum_column_types::SET) {
return DATA_INT;
}
switch (FieldType()) {
case MYSQL_TYPE_VAR_STRING: /* old <= 4.1 VARCHAR */
case MYSQL_TYPE_VARCHAR: /* new >= 5.0.3 true VARCHAR */
if (IsBinary()) {
return (DATA_BINARY);
} else if (dd_collation_id_ == 8 /* my_charset_latin1 */) {
return (DATA_VARCHAR);
} else {
return (DATA_VARMYSQL);
}
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_STRING:
if (IsBinary()) {
return (DATA_FIXBINARY);
} else if (dd_collation_id_ == 8 /* my_charset_latin1 */) {
return (DATA_CHAR);
} else {
return (DATA_MYSQL);
}
case MYSQL_TYPE_NEWDECIMAL:
return (DATA_FIXBINARY);
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_NEWDATE:
case MYSQL_TYPE_BOOL:
return (DATA_INT);
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_TIME2:
case MYSQL_TYPE_DATETIME2:
case MYSQL_TYPE_TIMESTAMP2:
// Check real_type()
switch (dd_type_) {
case enum_column_types::TIME:
case enum_column_types::DATETIME:
case enum_column_types::TIMESTAMP:
return (DATA_INT);
default:
[[fallthrough]];
case enum_column_types::TIME2:
case enum_column_types::DATETIME2:
case enum_column_types::TIMESTAMP2:
return (DATA_FIXBINARY);
}
case MYSQL_TYPE_FLOAT:
return (DATA_FLOAT);
case MYSQL_TYPE_DOUBLE:
return (DATA_DOUBLE);
case MYSQL_TYPE_DECIMAL:
return (DATA_DECIMAL);
case MYSQL_TYPE_GEOMETRY:
return (DATA_GEOMETRY);
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_JSON: // JSON fields are stored as BLOBs
return (DATA_BLOB);
case MYSQL_TYPE_NULL:
break;
default:
assert(false);
}
return 0;
}
#define DIG_PER_DEC1 9
#define PORTABLE_SIZEOF_CHAR_PTR 8
static const int dig2bytes[DIG_PER_DEC1 + 1] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
uint32_t Column::PackLength() const {
switch (DDType2FieldType(dd_type_)) {
/*
* 30 in total
*/
case MYSQL_TYPE_VAR_STRING:
return dd_char_length_;
case MYSQL_TYPE_STRING:
return dd_char_length_;
case MYSQL_TYPE_VARCHAR:
return VarcharLenBytes() + dd_char_length_;
case MYSQL_TYPE_BLOB:
return 2 + PORTABLE_SIZEOF_CHAR_PTR;
case MYSQL_TYPE_MEDIUM_BLOB:
return 3 + PORTABLE_SIZEOF_CHAR_PTR;
case MYSQL_TYPE_TINY_BLOB:
return 1 + PORTABLE_SIZEOF_CHAR_PTR;
case MYSQL_TYPE_LONG_BLOB:
return 4 + PORTABLE_SIZEOF_CHAR_PTR;
case MYSQL_TYPE_GEOMETRY:
return 4 + PORTABLE_SIZEOF_CHAR_PTR;
case MYSQL_TYPE_JSON:
return 4 + PORTABLE_SIZEOF_CHAR_PTR;
case MYSQL_TYPE_ENUM:
return dd_elements_size_tmp_ < 256 ? 1 : 2;
case MYSQL_TYPE_SET: {
uint64_t len = (dd_elements_size_tmp_ + 7) / 8;
return len > 4 ? 8 : len;
}
[[fallthrough]];
case MYSQL_TYPE_DECIMAL:
return dd_char_length_;
case MYSQL_TYPE_NEWDECIMAL: {
int precision = dd_numeric_precision_;
int scale = dd_numeric_scale_;
int intg = precision - scale;
int intg0 = intg / DIG_PER_DEC1;
int frac0 = scale / DIG_PER_DEC1;
int intg0x = intg - intg0 * DIG_PER_DEC1;
int frac0x = scale - frac0 * DIG_PER_DEC1;
assert(scale >= 0 && precision > 0 &&
scale <= precision);
assert(intg0x >= 0);
assert(intg0x <= DIG_PER_DEC1);
assert(frac0x >= 0);
assert(frac0x <= DIG_PER_DEC1);
return intg0 * sizeof(int32_t) + dig2bytes[intg0x] +
frac0 * sizeof(int32_t) + dig2bytes[frac0x];
}
[[fallthrough]];
case MYSQL_TYPE_FLOAT:
return sizeof(float);
case MYSQL_TYPE_DOUBLE:
return sizeof(double);
case MYSQL_TYPE_TINY:
return 1;
case MYSQL_TYPE_SHORT:
return 2;
case MYSQL_TYPE_INT24:
return 3;
case MYSQL_TYPE_LONG:
return 4;
case MYSQL_TYPE_LONGLONG:
return 8;
case MYSQL_TYPE_TIMESTAMP:
return dd_char_length_;
case MYSQL_TYPE_TIMESTAMP2:
return 4 + (dd_datetime_precision_ + 1) / 2;
case MYSQL_TYPE_YEAR:
return 1;
case MYSQL_TYPE_NEWDATE:
return 3;
case MYSQL_TYPE_TIME:
return 3;
case MYSQL_TYPE_TIME2:
return 3 + (dd_datetime_precision_ + 1) / 2;
case MYSQL_TYPE_DATETIME:
return 8;
case MYSQL_TYPE_DATETIME2:
return 5 + (dd_datetime_precision_ + 1) / 2;
case MYSQL_TYPE_NULL:
return 0;
case MYSQL_TYPE_BIT:
// treat_bit_as_char should be true
return (dd_char_length_ + 7) / 8;
// TODO(Zhao): support other types
// These 2 are not valid user defined in DD
case MYSQL_TYPE_INVALID:
case MYSQL_TYPE_BOOL:
default:
assert(false);
}
}
bool Column::IsBinary() const {
switch (FieldType()) {
// For Field_str
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_GEOMETRY:
case MYSQL_TYPE_JSON:
case MYSQL_TYPE_ENUM:
case MYSQL_TYPE_SET:
case MYSQL_TYPE_NULL:
return (dd_collation_id_ == 63 /* my_charset_bin */);
default:
return true;
}
}
bool Column::IsColumnAdded() const {
if (dd_se_private_data_.Exists("version_added")) {
return true;
} else {
return false;
}
}
uint32_t Column::GetVersionAdded() const {
uint32_t version = UINT8_UNDEFINED;
if (!IsColumnAdded()) {
return version;
}
dd_se_private_data_.Get("version_added", &version);
return version;
}
bool Column::IsInstantAdded() const {
if (ib_version_added_ != UINT8_UNDEFINED &&
ib_version_added_ > 0) {
return true;
}
return false;
}
bool Column::IsColumnDropped() const {
if (dd_se_private_data_.Exists("version_dropped")) {
return true;
} else {
return false;
}
}
uint32_t Column::GetVersionDropped() const {
uint32_t version = UINT8_UNDEFINED;
if (!IsColumnDropped()) {
return version;
}
dd_se_private_data_.Get("version_dropped", &version);
return version;
}
bool Column::IsInstantDropped() const {
if (ib_version_dropped_ != UINT8_UNDEFINED &&
ib_version_dropped_ > 0) {
return true;
} else {
return false;
}
}
uint32_t Column::GetFixedSize() {
switch (ib_mtype_) {
case DATA_SYS:
case DATA_CHAR:
case DATA_FIXBINARY:
case DATA_INT:
case DATA_FLOAT:
case DATA_DOUBLE:
case DATA_POINT:
return ib_col_len_;
case DATA_MYSQL:
if (IsBinary()) {
return ib_col_len_;
// TODO(Zhao): support redundant row format
} else {
auto iter = g_collation_map.find(dd_collation_id_);
assert(iter != g_collation_map.end());
if (iter->second.min == iter->second.max) {
return ib_col_len_;
}
}
[[fallthrough]];
case DATA_VARCHAR:
case DATA_BINARY:
case DATA_DECIMAL:
case DATA_VARMYSQL:
case DATA_VAR_POINT:
case DATA_GEOMETRY:
case DATA_BLOB:
return (0);
default:
assert(0);
}
}
bool Column::IsDroppedInOrBefore(uint8_t version) const {
if (!IsInstantDropped()) {
return false;
}
return (GetVersionDropped() <= version);
}
bool Column::IsAddedAfter(uint8_t version) const {
if (!IsInstantAdded()) {
return false;
}
return (GetVersionAdded() > version);
}
bool Column::IsBigCol() const {
return (ib_col_len_ > 255 ||
(ib_mtype_ == DATA_BLOB || ib_mtype_ == DATA_VAR_POINT ||
ib_mtype_ == DATA_GEOMETRY));
}
/* ------ IndexColumn ------ */
bool IndexColumn::Init(const rapidjson::Value& dd_index_col_obj,
const std::vector<Column*>& columns) {
Read(&dd_ordinal_position_, dd_index_col_obj, "ordinal_position");
Read(&dd_length_, dd_index_col_obj, "length");
ReadEnum(&dd_order_, dd_index_col_obj, "order");
Read(&dd_hidden_, dd_index_col_obj, "hidden");
Read(&dd_column_opx_, dd_index_col_obj, "column_opx");
/*
* Points to the corresponding Column object.
* NOTICE:
* The ordinal_position of a Column starts from 1, while
* the column_opx of an IndexColumn starts from 0.
* However, this is not an issue, as the columns array starts from 0.
*/
column_ = columns[dd_column_opx_];
column_->set_index_column(this);
return true;
}
IndexColumn* IndexColumn::CreateIndexColumn(
const rapidjson::Value& dd_index_col_obj,
const std::vector<Column*>& columns) {
IndexColumn* element = new IndexColumn(false);
bool init_ret = element->Init(dd_index_col_obj, columns);
if (!init_ret) {
delete element;
element = nullptr;
}
return element;
}
// Used only when creating index columns for a dropped column.
IndexColumn* IndexColumn::CreateIndexDroppedColumn(Column* dropped_col) {
IndexColumn* index_column = new IndexColumn(true);
index_column->set_column(dropped_col);
dropped_col->set_index_column(index_column);
return index_column;
}
// Used only when creating a FTS_DOC_ID index column.
IndexColumn* IndexColumn::CreateIndexFTSDocIdColumn(Column* doc_id_col) {
IndexColumn* index_column = new IndexColumn(true);
index_column->set_column(doc_id_col);
doc_id_col->set_index_column(index_column);
return index_column;
}
/* ------ Index ------ */
const std::set<std::string> Index::default_valid_option_keys = {
"block_size",
"flags",
"parser_name",
"gipk" /* generated implicit primary key */
};
bool Index::Init(const rapidjson::Value& dd_index_obj,
const std::vector<Column*>& columns) {