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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
|
#include "MySQLReplication.h"
#include <DataTypes/DataTypeString.h>
#include <IO/ReadBufferFromString.h>
#include <IO/MySQLBinlogEventReadBuffer.h>
#include <IO/ReadHelpers.h>
#include <IO/Operators.h>
#include <Common/DateLUT.h>
#include <Common/FieldVisitorToString.h>
#include <Core/MySQL/PacketsGeneric.h>
#include <Core/MySQL/PacketsProtocolText.h>
namespace DB
{
namespace ErrorCodes
{
extern const int UNKNOWN_EXCEPTION;
extern const int LOGICAL_ERROR;
extern const int ATTEMPT_TO_READ_AFTER_EOF;
extern const int CANNOT_READ_ALL_DATA;
}
namespace MySQLReplication
{
using namespace MySQLProtocol;
using namespace MySQLProtocol::Generic;
using namespace MySQLProtocol::ProtocolText;
/// https://dev.mysql.com/doc/internals/en/binlog-event-header.html
void EventHeader::parse(ReadBuffer & payload)
{
payload.readStrict(reinterpret_cast<char *>(×tamp), 4);
payload.readStrict(reinterpret_cast<char *>(&type), 1);
payload.readStrict(reinterpret_cast<char *>(&server_id), 4);
payload.readStrict(reinterpret_cast<char *>(&event_size), 4);
payload.readStrict(reinterpret_cast<char *>(&log_pos), 4);
payload.readStrict(reinterpret_cast<char *>(&flags), 2);
}
void EventHeader::dump(WriteBuffer & out) const
{
out << "\n=== " << magic_enum::enum_name(this->type) << " ===" << '\n';
out << "Timestamp: " << this->timestamp << '\n';
out << "Event Type: " << magic_enum::enum_name(this->type) << '\n';
out << "Server ID: " << this->server_id << '\n';
out << "Event Size: " << this->event_size << '\n';
out << "Log Pos: " << this->log_pos << '\n';
out << "Flags: " << this->flags << '\n';
}
/// https://dev.mysql.com/doc/internals/en/format-description-event.html
void FormatDescriptionEvent::parseImpl(ReadBuffer & payload)
{
payload.readStrict(reinterpret_cast<char *>(&binlog_version), 2);
assert(binlog_version == EVENT_VERSION_V4);
server_version.resize(50);
payload.readStrict(reinterpret_cast<char *>(server_version.data()), 50);
payload.readStrict(reinterpret_cast<char *>(&create_timestamp), 4);
payload.readStrict(reinterpret_cast<char *>(&event_header_length), 1);
assert(event_header_length == EVENT_HEADER_LENGTH);
readStringUntilEOF(event_type_header_length, payload);
}
void FormatDescriptionEvent::dump(WriteBuffer & out) const
{
header.dump(out);
out << "Binlog Version: " << this->binlog_version << '\n';
out << "Server Version: " << this->server_version << '\n';
out << "Create Timestamp: " << this->create_timestamp << '\n';
out << "Event Header Len: " << std::to_string(this->event_header_length) << '\n';
}
/// https://dev.mysql.com/doc/internals/en/rotate-event.html
void RotateEvent::parseImpl(ReadBuffer & payload)
{
payload.readStrict(reinterpret_cast<char *>(&position), 8);
readStringUntilEOF(next_binlog, payload);
}
void RotateEvent::dump(WriteBuffer & out) const
{
header.dump(out);
out << "Position: " << this->position << '\n';
out << "Next Binlog: " << this->next_binlog << '\n';
}
/// https://dev.mysql.com/doc/internals/en/query-event.html
void QueryEvent::parseImpl(ReadBuffer & payload)
{
payload.readStrict(reinterpret_cast<char *>(&thread_id), 4);
payload.readStrict(reinterpret_cast<char *>(&exec_time), 4);
payload.readStrict(reinterpret_cast<char *>(&schema_len), 1);
payload.readStrict(reinterpret_cast<char *>(&error_code), 2);
payload.readStrict(reinterpret_cast<char *>(&status_len), 2);
status.resize(status_len);
payload.readStrict(reinterpret_cast<char *>(status.data()), status_len);
schema.resize(schema_len);
payload.readStrict(reinterpret_cast<char *>(schema.data()), schema_len);
payload.ignore(1);
readStringUntilEOF(query, payload);
if (query.starts_with("BEGIN") || query.starts_with("COMMIT"))
{
typ = QUERY_EVENT_MULTI_TXN_FLAG;
if (!query.starts_with("COMMIT"))
transaction_complete = false;
}
else if (query.starts_with("XA"))
{
if (query.starts_with("XA ROLLBACK"))
throw ReplicationError(ErrorCodes::LOGICAL_ERROR, "ParseQueryEvent: Unsupported query event: {}", query);
typ = QUERY_EVENT_XA;
if (!query.starts_with("XA COMMIT"))
transaction_complete = false;
}
else if (query.starts_with("SAVEPOINT") || query.starts_with("ROLLBACK")
|| query.starts_with("RELEASE SAVEPOINT"))
{
typ = QUERY_SAVEPOINT;
}
// https://dev.mysql.com/worklog/task/?id=13355
// When doing query "CREATE TABLE xx AS SELECT", the binlog will be
// "CREATE TABLE ... START TRANSACTION", the DDL will be failed
// so, just ignore the "START TRANSACTION" suffix
if (query.ends_with("START TRANSACTION"))
{
auto pos = query.rfind("START TRANSACTION");
if (pos > 0)
query.resize(pos);
}
}
void QueryEvent::dump(WriteBuffer & out) const
{
header.dump(out);
out << "Thread ID: " << this->thread_id << '\n';
out << "Execution Time: " << this->exec_time << '\n';
out << "Schema Len: " << std::to_string(this->schema_len) << '\n';
out << "Error Code: " << this->error_code << '\n';
out << "Status Len: " << this->status_len << '\n';
out << "Schema: " << this->schema << '\n';
out << "Query: " << this->query << '\n';
}
void XIDEvent::parseImpl(ReadBuffer & payload) { payload.readStrict(reinterpret_cast<char *>(&xid), 8); }
void XIDEvent::dump(WriteBuffer & out) const
{
header.dump(out);
out << "XID: " << this->xid << '\n';
}
void TableMapEventHeader::parse(ReadBuffer & payload)
{
payload.readStrict(reinterpret_cast<char *>(&table_id), 6);
payload.readStrict(reinterpret_cast<char *>(&flags), 2);
payload.readStrict(reinterpret_cast<char *>(&schema_len), 1);
schema.resize(schema_len);
payload.readStrict(reinterpret_cast<char *>(schema.data()), schema_len);
payload.ignore(1);
payload.readStrict(reinterpret_cast<char *>(&table_len), 1);
table.resize(table_len);
payload.readStrict(reinterpret_cast<char *>(table.data()), table_len);
payload.ignore(1);
}
/// https://dev.mysql.com/doc/internals/en/table-map-event.html
void TableMapEvent::parseImpl(ReadBuffer & payload)
{
column_count = static_cast<UInt32>(readLengthEncodedNumber(payload));
for (auto i = 0U; i < column_count; ++i)
{
UInt8 v = 0x00;
payload.readStrict(reinterpret_cast<char *>(&v), 1);
column_type.emplace_back(v);
}
String meta;
readLengthEncodedString(meta, payload);
parseMeta(meta);
size_t null_bitmap_size = (column_count + 7) / 8;
readBitmap(payload, null_bitmap, null_bitmap_size);
/// Parse MySQL 8.0 optional metadata fields.
/// https://mysqlhighavailability.com/more-metadata-is-written-into-binary-log/
parseOptionalMetaField(payload);
}
/// Types that do not used in the binlog event:
/// MYSQL_TYPE_ENUM
/// MYSQL_TYPE_SET
/// MYSQL_TYPE_TINY_BLOB
/// MYSQL_TYPE_MEDIUM_BLOB
/// MYSQL_TYPE_LONG_BLOB
void TableMapEvent::parseMeta(String meta)
{
auto pos = 0;
column_meta.reserve(column_count);
for (auto i = 0U; i < column_count; ++i)
{
UInt16 typ = column_type[i];
switch (typ)
{
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_NEWDATE:
case MYSQL_TYPE_YEAR:
{
/// No data here.
column_meta.emplace_back(0);
break;
}
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
case MYSQL_TYPE_TIMESTAMP2:
case MYSQL_TYPE_DATETIME2:
case MYSQL_TYPE_TIME2:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_GEOMETRY:
{
column_meta.emplace_back(static_cast<UInt16>(meta[pos]));
pos += 1;
break;
}
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_STRING:
{
/// Big-Endian
auto b0 = static_cast<UInt16>(meta[pos] << 8);
auto b1 = static_cast<UInt8>(meta[pos + 1]);
column_meta.emplace_back(static_cast<UInt16>(b0 + b1));
pos += 2;
break;
}
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_VAR_STRING: {
/// Little-Endian
auto b0 = static_cast<UInt8>(meta[pos]);
auto b1 = static_cast<UInt16>(meta[pos + 1] << 8);
column_meta.emplace_back(static_cast<UInt16>(b0 + b1));
pos += 2;
break;
}
default:
throw ReplicationError(ErrorCodes::UNKNOWN_EXCEPTION, "ParseMetaData: Unhandled data type: {}", std::to_string(typ));
}
}
}
void TableMapEvent::parseOptionalMetaField(ReadBuffer & payload)
{
char type = 0;
while (payload.read(type))
{
UInt64 len = readLengthEncodedNumber(payload);
if (len == 0)
{
payload.ignoreAll();
return;
}
switch (type)
{
/// It may be useful, parse later
case SIGNEDNESS:
payload.ignore(len);
break;
case DEFAULT_CHARSET:
{
UInt32 total_read = 0;
UInt16 once_read = 0;
default_charset = static_cast<UInt32>(readLengthEncodedNumber(payload, once_read));
total_read += once_read;
while (total_read < len)
{
UInt32 col_index = static_cast<UInt32>(readLengthEncodedNumber(payload, once_read));
total_read += once_read;
UInt32 col_charset = static_cast<UInt32>(readLengthEncodedNumber(payload, once_read));
total_read += once_read;
default_charset_pairs.emplace(col_index, col_charset);
}
break;
}
case COLUMN_CHARSET:
{
UInt32 total_read = 0;
UInt16 once_read = 0;
while (total_read < len)
{
UInt32 collation_id = static_cast<UInt32>(readLengthEncodedNumber(payload, once_read));
column_charset.emplace_back(collation_id);
total_read += once_read;
}
break;
}
case COLUMN_NAME:
payload.ignore(len);
break;
case SET_STR_VALUE:
case GEOMETRY_TYPE:
case SIMPLE_PRIMARY_KEY:
case PRIMARY_KEY_WITH_PREFIX:
case ENUM_AND_SET_DEFAULT_CHARSET:
case COLUMN_VISIBILITY:
default:
payload.ignore(len);
break;
}
}
}
UInt32 TableMapEvent::getColumnCharsetId(UInt32 column_index)
{
if (!column_charset.empty())
{
UInt32 str_index = 0xFFFFFFFF;
/// Calc the index in the column_charset
for (UInt32 i = 0; i <= column_index; ++i)
{
switch (column_type[i])
{
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_BLOB:
++str_index;
break;
default:
break;
}
}
if (str_index != 0xFFFFFFFF && str_index < column_charset.size())
{
return column_charset[str_index];
}
}
else if (!default_charset_pairs.empty())
{
UInt32 str_index = 0xFFFFFFFF;
for (UInt32 i = 0; i <= column_index; ++i)
{
switch (column_type[i])
{
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_BLOB:
++str_index;
break;
default:
break;
}
}
if (default_charset_pairs.contains(str_index))
{
return default_charset_pairs[str_index];
}
}
return default_charset;
}
void TableMapEvent::dump(WriteBuffer & out) const
{
header.dump(out);
out << "Table ID: " << this->table_id << '\n';
out << "Flags: " << this->flags << '\n';
out << "Schema Len: " << std::to_string(this->schema_len) << '\n';
out << "Schema: " << this->schema << '\n';
out << "Table Len: " << std::to_string(this->table_len) << '\n';
out << "Table: " << this->table << '\n';
out << "Column Count: " << this->column_count << '\n';
for (UInt32 i = 0; i < column_count; ++i)
{
out << "Column Type [" << i << "]: " << std::to_string(column_type[i]) << ", Meta: " << column_meta[i] << '\n';
}
String bitmap_str;
boost::to_string(this->null_bitmap, bitmap_str);
out << "Null Bitmap: " << bitmap_str << '\n';
}
void RowsEventHeader::parse(ReadBuffer & payload)
{
payload.readStrict(reinterpret_cast<char *>(&table_id), 6);
payload.readStrict(reinterpret_cast<char *>(&flags), 2);
UInt16 extra_data_len;
/// This extra_data_len contains the 2 bytes length.
payload.readStrict(reinterpret_cast<char *>(&extra_data_len), 2);
payload.ignore(extra_data_len - 2);
}
void RowsEvent::parseImpl(ReadBuffer & payload)
{
number_columns = static_cast<UInt32>(readLengthEncodedNumber(payload));
size_t columns_bitmap_size = (number_columns + 7) / 8;
switch (header.type)
{
case UPDATE_ROWS_EVENT_V1:
case UPDATE_ROWS_EVENT_V2:
readBitmap(payload, columns_present_bitmap1, columns_bitmap_size);
readBitmap(payload, columns_present_bitmap2, columns_bitmap_size);
break;
default:
readBitmap(payload, columns_present_bitmap1, columns_bitmap_size);
break;
}
while (!payload.eof())
{
parseRow(payload, columns_present_bitmap1);
if (header.type == UPDATE_ROWS_EVENT_V1 || header.type == UPDATE_ROWS_EVENT_V2)
{
parseRow(payload, columns_present_bitmap2);
}
}
}
static inline String convertCharsetIfNeeded(
const std::shared_ptr<TableMapEvent> & table_map,
UInt32 i,
const String & val)
{
const auto collation_id = table_map->getColumnCharsetId(i);
if (table_map->charset_ptr->needConvert(collation_id))
{
String target;
auto err = table_map->charset_ptr->convertFromId(collation_id, target, val);
if (err == 0)
return target;
}
return val;
}
/// Types that do not used in the binlog event:
/// MYSQL_TYPE_SET
/// MYSQL_TYPE_TINY_BLOB
/// MYSQL_TYPE_MEDIUM_BLOB
/// MYSQL_TYPE_LONG_BLOB
void RowsEvent::parseRow(ReadBuffer & payload, Bitmap & bitmap)
{
Tuple row;
UInt32 null_index = 0;
UInt32 re_count = 0;
for (UInt32 i = 0; i < number_columns; ++i)
{
if (bitmap[i])
re_count++;
}
re_count = (re_count + 7) / 8;
boost::dynamic_bitset<> columns_null_set;
readBitmap(payload, columns_null_set, re_count);
for (UInt32 i = 0; i < number_columns; ++i)
{
UInt32 field_len = 0;
/// Column not presents.
if (!bitmap[i])
continue;
if (columns_null_set[null_index])
{
row.push_back(Field{Null{}});
}
else
{
auto meta = table_map->column_meta[i];
auto field_type = table_map->column_type[i];
if (field_type == MYSQL_TYPE_STRING)
{
if (meta >= 256)
{
UInt8 byte0 = meta >> 8;
UInt8 byte1 = meta & 0xff;
if ((byte0 & 0x30) != 0x30)
{
field_len = byte1 | (((byte0 & 0x30) ^ 0x30) << 4);
field_type = byte0 | 0x30;
}
else
{
field_len = byte1;
field_type = byte0;
}
}
else
{
field_len = meta;
}
}
switch (field_type)
{
case MYSQL_TYPE_TINY:
{
UInt8 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 1);
row.push_back(Field{UInt8{val}});
break;
}
case MYSQL_TYPE_SHORT:
{
UInt16 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 2);
row.push_back(Field{UInt16{val}});
break;
}
case MYSQL_TYPE_INT24:
{
Int32 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 3);
row.push_back(Field{Int32{val}});
break;
}
case MYSQL_TYPE_LONG:
{
UInt32 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 4);
row.push_back(Field{UInt32{val}});
break;
}
case MYSQL_TYPE_LONGLONG:
{
UInt64 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 8);
row.push_back(Field{UInt64{val}});
break;
}
case MYSQL_TYPE_FLOAT:
{
Float32 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 4);
row.push_back(Field{Float32{val}});
break;
}
case MYSQL_TYPE_DOUBLE:
{
Float64 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 8);
row.push_back(Field{Float64{val}});
break;
}
case MYSQL_TYPE_TIMESTAMP:
{
UInt32 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 4);
row.push_back(Field{val});
break;
}
case MYSQL_TYPE_DATE:
{
UInt32 i24 = 0;
payload.readStrict(reinterpret_cast<char *>(&i24), 3);
const ExtendedDayNum date_day_number(DateLUT::instance().makeDayNum(
static_cast<int>((i24 >> 9) & 0x7fff), static_cast<int>((i24 >> 5) & 0xf), static_cast<int>(i24 & 0x1f)).toUnderType());
row.push_back(Field(date_day_number.toUnderType()));
break;
}
case MYSQL_TYPE_YEAR: {
Int16 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 1);
row.push_back(Field{UInt16{static_cast<UInt16>(val + 1900)}});
break;
}
case MYSQL_TYPE_TIME2:
{
UInt64 uintpart = 0UL;
Int32 frac = 0U;
Int64 ltime;
Int64 intpart;
switch (meta)
{
case 0:
{
readBigEndianStrict(payload, reinterpret_cast<char *>(&uintpart), 3);
intpart = uintpart - 0x800000L;
ltime = intpart << 24;
break;
}
case 1:
case 2:
{
readBigEndianStrict(payload, reinterpret_cast<char *>(&uintpart), 3);
intpart = uintpart - 0x800000L;
readBigEndianStrict(payload, reinterpret_cast<char *>(&frac), 1);
if (intpart < 0 && frac > 0)
{
intpart ++;
frac -= 0x100;
}
frac = frac * 10000;
ltime = intpart << 24;
break;
}
case 3:
case 4:
{
readBigEndianStrict(payload, reinterpret_cast<char *>(&uintpart), 3);
intpart = uintpart - 0x800000L;
readBigEndianStrict(payload, reinterpret_cast<char *>(&frac), 2);
if (intpart < 0 && frac > 0)
{
intpart ++;
frac -= 0x10000;
}
frac = frac * 100;
ltime = intpart << 24;
break;
}
case 5:
case 6:
{
readBigEndianStrict(payload, reinterpret_cast<char *>(&uintpart), 6);
intpart = uintpart - 0x800000000000L;
ltime = intpart;
frac = static_cast<Int32>(std::abs(intpart % (1L << 24)));
break;
}
default:
{
readBigEndianStrict(payload, reinterpret_cast<char *>(&uintpart), 3);
intpart = uintpart - 0x800000L;
ltime = intpart << 24;
break;
}
}
Int64 hh, mm, ss;
bool negative = false;
if (intpart == 0)
{
hh = 0;
mm = 0;
ss = 0;
}
else
{
if (ltime < 0) negative= true;
UInt64 ultime = std::abs(ltime);
intpart = ultime >> 24;
hh = (intpart >> 12) % (1 << 10);
mm = (intpart >> 6) % (1 << 6);
ss = intpart % (1 << 6);
}
Int64 time_micro = 0;
time_micro = (hh * 3600 + mm * 60 + ss) * 1000000 + std::abs(frac);
if (negative) time_micro = - time_micro;
row.push_back(Field{Int64{time_micro}});
break;
}
case MYSQL_TYPE_DATETIME2:
{
Int64 val = 0;
UInt32 fsp = 0;
readBigEndianStrict(payload, reinterpret_cast<char *>(&val), 5);
readTimeFractionalPart(payload, fsp, meta);
UInt32 year_month = static_cast<UInt32>(readBits(val, 1, 17, 40));
time_t date_time = DateLUT::instance().makeDateTime(
year_month / 13, year_month % 13, readBits(val, 18, 5, 40)
, readBits(val, 23, 5, 40), readBits(val, 28, 6, 40), readBits(val, 34, 6, 40)
);
if (!meta)
row.push_back(Field{static_cast<UInt32>(date_time)});
else
{
DB::DecimalUtils::DecimalComponents<DateTime64> components{
static_cast<DateTime64::NativeType>(date_time), 0};
components.fractional = fsp;
row.push_back(Field(DecimalUtils::decimalFromComponents<DateTime64>(components, meta)));
}
break;
}
case MYSQL_TYPE_TIMESTAMP2:
{
UInt32 sec = 0, fsp = 0;
readBigEndianStrict(payload, reinterpret_cast<char *>(&sec), 4);
readTimeFractionalPart(payload, fsp, meta);
if (!meta)
row.push_back(Field{sec});
else
{
DB::DecimalUtils::DecimalComponents<DateTime64> components{
static_cast<DateTime64::NativeType>(sec), 0};
components.fractional = fsp;
row.push_back(Field(DecimalUtils::decimalFromComponents<DateTime64>(components, meta)));
}
break;
}
case MYSQL_TYPE_NEWDECIMAL:
{
const auto & dispatch = [](size_t precision, size_t scale, const auto & function) -> Field
{
if (precision <= DecimalUtils::max_precision<Decimal32>)
return Field(function(precision, scale, Decimal32()));
else if (precision <= DecimalUtils::max_precision<Decimal64>)
return Field(function(precision, scale, Decimal64()));
else if (precision <= DecimalUtils::max_precision<Decimal128>)
return Field(function(precision, scale, Decimal128()));
return Field(function(precision, scale, Decimal256()));
};
const auto & read_decimal = [&](size_t precision, size_t scale, auto decimal)
{
using DecimalType = decltype(decimal);
static constexpr size_t digits_per_integer = 9;
static const size_t compressed_bytes_map[] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
static const size_t compressed_integer_align_numbers[] = {
0x0, 0xFF, 0xFF, 0xFFFF, 0xFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
UInt32 mask = 0;
DecimalType res(0);
if (payload.eof())
throw Exception(ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF, "Attempt to read after EOF.");
if ((*payload.position() & 0x80) == 0)
mask = static_cast<UInt32>(-1);
*payload.position() ^= 0x80;
{
size_t integral = (precision - scale);
size_t uncompressed_integers = integral / digits_per_integer;
size_t compressed_integers = integral - (uncompressed_integers * digits_per_integer);
/// Compressed part.
if (compressed_integers != 0)
{
UInt32 val = 0;
size_t to_read = compressed_bytes_map[compressed_integers];
readBigEndianStrict(payload, reinterpret_cast<char *>(&val), to_read);
res += (val ^ (mask & compressed_integer_align_numbers[compressed_integers]));
}
for (size_t k = 0; k < uncompressed_integers; ++k)
{
UInt32 val = 0;
readBigEndianStrict(payload, reinterpret_cast<char *>(&val), 4);
res *= intExp10OfSize<typename DecimalType::NativeType>(static_cast<int>(digits_per_integer));
res += (val ^ mask);
}
}
{
size_t uncompressed_decimals = scale / digits_per_integer;
size_t compressed_decimals = scale - (uncompressed_decimals * digits_per_integer);
for (size_t k = 0; k < uncompressed_decimals; ++k)
{
UInt32 val = 0;
readBigEndianStrict(payload, reinterpret_cast<char *>(&val), 4);
res *= intExp10OfSize<typename DecimalType::NativeType>(static_cast<int>(digits_per_integer));
res += (val ^ mask);
}
/// Compressed part.
if (compressed_decimals != 0)
{
UInt32 val = 0;
size_t to_read = compressed_bytes_map[compressed_decimals];
if (to_read)
{
readBigEndianStrict(payload, reinterpret_cast<char *>(&val), to_read);
res *= intExp10OfSize<typename DecimalType::NativeType>(static_cast<int>(compressed_decimals));
res += (val ^ (mask & compressed_integer_align_numbers[compressed_decimals]));
}
}
}
if (mask != 0)
res *= -1;
return res;
};
row.push_back(dispatch((meta >> 8) & 0xFF, meta & 0xFF, read_decimal));
break;
}
case MYSQL_TYPE_ENUM:
{
if ((meta & 0xFF) == 1)
{
UInt8 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 1);
row.push_back(Field{UInt8{val}});
}
else
{
UInt16 val = 0;
payload.readStrict(reinterpret_cast<char *>(&val), 2);
row.push_back(Field{UInt16{val}});
}
break;
}
case MYSQL_TYPE_SET:
{
UInt32 size = (meta & 0xff);
Bitmap bitmap1;
readBitmap(payload, bitmap1, size);
row.push_back(Field{UInt64{bitmap1.to_ulong()}});
break;
}
case MYSQL_TYPE_BIT:
{
UInt32 bits = ((meta >> 8) * 8) + (meta & 0xff);
UInt32 size = (bits + 7) / 8;
UInt64 val = 0UL;
readBigEndianStrict(payload, reinterpret_cast<char *>(&val), size);
row.push_back(val);
break;
}
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_VAR_STRING:
{
uint32_t size = 0;
if (meta < 256)
{
payload.readStrict(reinterpret_cast<char *>(&size), 1);
}
else
{
payload.readStrict(reinterpret_cast<char *>(&size), 2);
}
String val;
val.resize(size);
payload.readStrict(reinterpret_cast<char *>(val.data()), size);
row.emplace_back(Field{convertCharsetIfNeeded(table_map, i, val)});
break;
}
case MYSQL_TYPE_STRING:
{
UInt32 size = 0;
if (field_len < 256)
{
payload.readStrict(reinterpret_cast<char *>(&size), 1);
}
else
{
payload.readStrict(reinterpret_cast<char *>(&size), 2);
}
String val;
val.resize(size);
payload.readStrict(reinterpret_cast<char *>(val.data()), size);
row.emplace_back(Field{convertCharsetIfNeeded(table_map, i, val)});
break;
}
case MYSQL_TYPE_GEOMETRY:
case MYSQL_TYPE_BLOB:
{
UInt32 size = 0;
switch (meta)
{
case 1: {
payload.readStrict(reinterpret_cast<char *>(&size), 1);
break;
}
case 2: {
payload.readStrict(reinterpret_cast<char *>(&size), 2);
break;
}
case 3: {
payload.readStrict(reinterpret_cast<char *>(&size), 3);
break;
}
case 4: {
payload.readStrict(reinterpret_cast<char *>(&size), 4);
break;
}
default:
break;
}
String val;
val.resize(size);
payload.readStrict(reinterpret_cast<char *>(val.data()), size);
row.emplace_back(Field{
field_type == MYSQL_TYPE_BLOB
? convertCharsetIfNeeded(table_map, i, val)
: val});
break;
}
default:
throw ReplicationError(ErrorCodes::UNKNOWN_EXCEPTION,
"ParseRow: Unhandled MySQL field type: {}", std::to_string(field_type));
}
}
null_index++;
}
rows.push_back(row);
}
void RowsEvent::dump(WriteBuffer & out) const
{
FieldVisitorToString to_string;
header.dump(out);
out << "Schema: " << this->schema << '\n';
out << "Table: " << this->table << '\n';
for (size_t i = 0; i < rows.size(); ++i)
{
out << "Row[" << i << "]: " << applyVisitor(to_string, rows[i]) << '\n';
}
}
void GTIDEvent::parseImpl(ReadBuffer & payload)
{
/// We only care uuid:seq_no parts assigned to GTID_NEXT.
payload.readStrict(reinterpret_cast<char *>(&commit_flag), 1);
// MySQL UUID is big-endian.
readBinaryBigEndian(UUIDHelpers::getHighBytes(gtid.uuid), payload);
readBinaryBigEndian(UUIDHelpers::getLowBytes(gtid.uuid), payload);
payload.readStrict(reinterpret_cast<char *>(>id.seq_no), 8);
/// Skip others.
payload.ignoreAll();
}
void GTIDEvent::dump(WriteBuffer & out) const
{
WriteBufferFromOwnString ws;
writeUUIDText(gtid.uuid, ws);
auto gtid_next = ws.str() + ":" + std::to_string(gtid.seq_no);
header.dump(out);
out << "GTID Next: " << gtid_next << '\n';
}
void DryRunEvent::parseImpl(ReadBuffer & payload) { payload.ignoreAll(); }
void DryRunEvent::dump(WriteBuffer & out) const
{
header.dump(out);
out << "[DryRun Event]" << '\n';
}
/// Update binlog name/position/gtid based on the event type.
void Position::update(BinlogEventPtr event)
{
switch (event->header.type)
{
case FORMAT_DESCRIPTION_EVENT: {
binlog_pos = event->header.log_pos;
break;
}
case QUERY_EVENT: {
auto query = std::static_pointer_cast<QueryEvent>(event);
if (query->transaction_complete && pending_gtid)
{
gtid_sets.update(*pending_gtid);
pending_gtid.reset();
}
binlog_pos = event->header.log_pos;
break;
}
case XID_EVENT: {
if (pending_gtid)
{
gtid_sets.update(*pending_gtid);
pending_gtid.reset();
}
binlog_pos = event->header.log_pos;
break;
}
case ROTATE_EVENT: {
auto rotate = std::static_pointer_cast<RotateEvent>(event);
binlog_name = rotate->next_binlog;
binlog_pos = event->header.log_pos;
break;
}
case GTID_EVENT: {
if (pending_gtid)
gtid_sets.update(*pending_gtid);
auto gtid_event = std::static_pointer_cast<GTIDEvent>(event);
binlog_pos = event->header.log_pos;
pending_gtid = gtid_event->gtid;
break;
}
default:
throw ReplicationError(ErrorCodes::LOGICAL_ERROR, "Position update with unsupported event");
}
}
void Position::update(UInt64 binlog_pos_, const String & binlog_name_, const String & gtid_sets_)
{
binlog_pos = binlog_pos_;
binlog_name = binlog_name_;
gtid_sets.parse(gtid_sets_);
}
void Position::dump(WriteBuffer & out) const
{
out << "\n=== Binlog Position ===" << '\n';
out << "Binlog: " << this->binlog_name << '\n';
out << "Position: " << this->binlog_pos << '\n';
out << "GTIDSets: " << this->gtid_sets.toString() << '\n';
}
void MySQLFlavor::readPayloadImpl(ReadBuffer & payload)
{
if (payload.eof())
throw Exception(ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF, "Attempt to read after EOF.");
UInt16 header = static_cast<unsigned char>(*payload.position());
switch (header)
{
case PACKET_EOF:
throw ReplicationError(ErrorCodes::CANNOT_READ_ALL_DATA, "Master maybe lost");
case PACKET_ERR:
ERRPacket err;
err.readPayloadWithUnpacked(payload);
throw ReplicationError::createDeprecated(err.error_message, ErrorCodes::UNKNOWN_EXCEPTION);
}
// skip the generic response packets header flag.
payload.ignore(1);
MySQLBinlogEventReadBuffer event_payload(payload, checksum_signature_length);
EventHeader event_header;
event_header.parse(event_payload);
switch (event_header.type)
{
case FORMAT_DESCRIPTION_EVENT:
{
event = std::make_shared<FormatDescriptionEvent>(std::move(event_header));
event->parseEvent(event_payload);
position.update(event);
break;
}
case ROTATE_EVENT:
{
event = std::make_shared<RotateEvent>(std::move(event_header));
event->parseEvent(event_payload);
position.update(event);
break;
}
case QUERY_EVENT:
{
event = std::make_shared<QueryEvent>(std::move(event_header));
event->parseEvent(event_payload);
position.update(event);
auto query = std::static_pointer_cast<QueryEvent>(event);
switch (query->typ)
{
case QUERY_EVENT_MULTI_TXN_FLAG:
case QUERY_EVENT_XA:
/// Ignore queries that have no impact on the data.
case QUERY_SAVEPOINT:
{
event = std::make_shared<DryRunEvent>(std::move(query->header));
break;
}
default:
break;
}
break;
}
case XID_EVENT:
{
event = std::make_shared<XIDEvent>(std::move(event_header));
event->parseEvent(event_payload);
position.update(event);
break;
}
case TABLE_MAP_EVENT:
{
TableMapEventHeader map_event_header;
map_event_header.parse(event_payload);
if (doReplicate(map_event_header.schema, map_event_header.table))
{
event = std::make_shared<TableMapEvent>(std::move(event_header), map_event_header, flavor_charset);
event->parseEvent(event_payload);
auto table_map = std::static_pointer_cast<TableMapEvent>(event);
table_maps[table_map->table_id] = table_map;
}
else
{
event = std::make_shared<DryRunEvent>(std::move(event_header));
event->parseEvent(event_payload);
}
break;
}
case WRITE_ROWS_EVENT_V1:
case WRITE_ROWS_EVENT_V2: {
RowsEventHeader rows_header(event_header.type);
rows_header.parse(event_payload);
if (doReplicate(rows_header.table_id))
event = std::make_shared<WriteRowsEvent>(table_maps.at(rows_header.table_id), std::move(event_header), rows_header);
else
event = std::make_shared<DryRunEvent>(std::move(event_header));
event->parseEvent(event_payload);
if (rows_header.flags & ROWS_END_OF_STATEMENT)
table_maps.clear();
break;
}
case DELETE_ROWS_EVENT_V1:
case DELETE_ROWS_EVENT_V2: {
RowsEventHeader rows_header(event_header.type);
rows_header.parse(event_payload);
if (doReplicate(rows_header.table_id))
event = std::make_shared<DeleteRowsEvent>(table_maps.at(rows_header.table_id), std::move(event_header), rows_header);
else
event = std::make_shared<DryRunEvent>(std::move(event_header));
event->parseEvent(event_payload);
if (rows_header.flags & ROWS_END_OF_STATEMENT)
table_maps.clear();
break;
}
case UPDATE_ROWS_EVENT_V1:
case UPDATE_ROWS_EVENT_V2: {
RowsEventHeader rows_header(event_header.type);
rows_header.parse(event_payload);
if (doReplicate(rows_header.table_id))
event = std::make_shared<UpdateRowsEvent>(table_maps.at(rows_header.table_id), std::move(event_header), rows_header);
else
event = std::make_shared<DryRunEvent>(std::move(event_header));
event->parseEvent(event_payload);
if (rows_header.flags & ROWS_END_OF_STATEMENT)
table_maps.clear();
break;
}
case GTID_EVENT:
{
event = std::make_shared<GTIDEvent>(std::move(event_header));
event->parseEvent(event_payload);
position.update(event);
break;
}
default:
{
event = std::make_shared<DryRunEvent>(std::move(event_header));
event->parseEvent(event_payload);
break;
}
}
}
bool MySQLFlavor::doReplicate(UInt64 table_id)
{
if (replicate_do_db.empty())
return false;
if (table_id == 0x00ffffff)
{
// Special "dummy event"
return false;
}
if (table_maps.contains(table_id))
{
auto table_map = table_maps.at(table_id);
return (table_map->schema == replicate_do_db) && (replicate_tables.empty() || replicate_tables.contains(table_map->table));
}
return false;
}
bool MySQLFlavor::doReplicate(const String & db, const String & table_name)
{
if (replicate_do_db.empty())
return false;
if (replicate_do_db != db)
return false;
return replicate_tables.empty() || table_name.empty() || replicate_tables.contains(table_name);
}
}
}
|