-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathpersist_storage_ddl_handlers.go
More file actions
3004 lines (2850 loc) · 118 KB
/
persist_storage_ddl_handlers.go
File metadata and controls
3004 lines (2850 loc) · 118 KB
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 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package schemastore
import (
"errors"
"fmt"
"strings"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/pkg/common"
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
cerror "github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/filter"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/ast"
"go.uber.org/zap"
)
const (
WithoutTiDBOnly = false
WithTiDBOnly = true
// InvalidTableID used in rename tables ddl to avoid duplicate table id
InvalidTableID = -1
)
type buildPersistedDDLEventFuncArgs struct {
job *model.Job
databaseMap map[int64]*BasicDatabaseInfo
tableMap map[int64]*BasicTableInfo
partitionMap map[int64]BasicPartitionInfo
}
type updateDDLHistoryFuncArgs struct {
ddlEvent *PersistedDDLEvent
databaseMap map[int64]*BasicDatabaseInfo
tableMap map[int64]*BasicTableInfo
partitionMap map[int64]BasicPartitionInfo
tablesDDLHistory map[int64][]uint64
tableTriggerDDLHistory []uint64
}
func (args *updateDDLHistoryFuncArgs) appendTableTriggerDDLHistory(ts uint64) {
args.tableTriggerDDLHistory = append(args.tableTriggerDDLHistory, ts)
}
func (args *updateDDLHistoryFuncArgs) appendTablesDDLHistory(ts uint64, tableIDs ...int64) {
for _, tableID := range tableIDs {
args.tablesDDLHistory[tableID] = append(args.tablesDDLHistory[tableID], ts)
}
}
type updateSchemaMetadataFuncArgs struct {
event *PersistedDDLEvent
databaseMap map[int64]*BasicDatabaseInfo
tableMap map[int64]*BasicTableInfo
partitionMap map[int64]BasicPartitionInfo
}
func (args *updateSchemaMetadataFuncArgs) addTableToDB(tableID int64, schemaID int64) {
databaseInfo, ok := args.databaseMap[schemaID]
if !ok {
log.Panic("database not found.", zap.Int64("schemaID", schemaID), zap.Int64("tableID", tableID))
}
databaseInfo.Tables[tableID] = true
}
func (args *updateSchemaMetadataFuncArgs) removeTableFromDB(tableID int64, schemaID int64) {
databaseInfo, ok := args.databaseMap[schemaID]
if !ok {
log.Panic("database not found.", zap.Int64("schemaID", schemaID), zap.Int64("tableID", tableID))
}
delete(databaseInfo.Tables, tableID)
}
type updateFullTableInfoFuncArgs struct {
event *PersistedDDLEvent
databaseMap map[int64]*BasicDatabaseInfo
// logical table id -> table info
tableInfoMap map[int64]*model.TableInfo
}
type persistStorageDDLHandler struct {
// buildPersistedDDLEventFunc build a PersistedDDLEvent which will be write to disk from a ddl job
buildPersistedDDLEventFunc func(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent
// updateDDLHistoryFunc add the finished ts of ddl event to the history of table trigger and related tables
updateDDLHistoryFunc func(args updateDDLHistoryFuncArgs) []uint64
// updateFullTableInfoFunc update the full table info map according to the ddl event
// Note: it must be called before updateSchemaMetadataFunc,
// because it depends on some info which may be updated by updateSchemaMetadataFunc
// TODO: add unit test
updateFullTableInfoFunc func(args updateFullTableInfoFuncArgs)
// updateSchemaMetadataFunc update database info, table info and partition info according to the ddl event
updateSchemaMetadataFunc func(args updateSchemaMetadataFuncArgs)
// iterateEventTablesFunc iterates through all physical table IDs affected by the DDL event
// and calls the provided `apply` function with those IDs. For partition tables, it includes
// all partition IDs.
iterateEventTablesFunc func(event *PersistedDDLEvent, apply func(tableIDs ...int64))
// extractTableInfoFunc extract (table info, deleted) for the specified `tableID` from ddl event
extractTableInfoFunc func(event *PersistedDDLEvent, tableID int64) (*common.TableInfo, bool)
// buildDDLEvent build a DDLEvent from a PersistedDDLEvent
// NOTE: the tableID is used in exchange table partition and rename tables DDL only,
// see the details in buildDDLEventForExchangeTablePartition and buildDDLEventForRenameTables.
// For other DDLs, tableID is not used and can be set to 0.
buildDDLEventFunc func(rawEvent *PersistedDDLEvent, tableFilter filter.Filter, tableID int64) (commonEvent.DDLEvent, bool, error)
}
var allDDLHandlers = map[model.ActionType]*persistStorageDDLHandler{
model.ActionCreateSchema: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForSchemaDDL,
updateDDLHistoryFunc: updateDDLHistoryForTableTriggerOnlyDDL,
updateFullTableInfoFunc: updateFullTableInfoIgnore,
updateSchemaMetadataFunc: updateSchemaMetadataForCreateSchema,
iterateEventTablesFunc: iterateEventTablesIgnore,
extractTableInfoFunc: extractTableInfoFuncIgnore,
buildDDLEventFunc: buildDDLEventForCreateSchema,
},
model.ActionDropSchema: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForSchemaDDL,
updateDDLHistoryFunc: updateDDLHistoryForSchemaDDL,
updateFullTableInfoFunc: updateFullTableInfoForDropSchema,
updateSchemaMetadataFunc: updateSchemaMetadataForDropSchema,
iterateEventTablesFunc: iterateEventTablesIgnore,
extractTableInfoFunc: extractTableInfoFuncIgnore,
buildDDLEventFunc: buildDDLEventForDropSchema,
},
model.ActionCreateTable: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForCreateTable,
updateDDLHistoryFunc: updateDDLHistoryForAddDropTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForNewTableDDL,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNewTableDDL,
},
model.ActionDropTable: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForDropTable,
updateDDLHistoryFunc: updateDDLHistoryForAddDropTable,
updateFullTableInfoFunc: updateFullTableInfoForDropTable,
updateSchemaMetadataFunc: updateSchemaMetadataForDropTable,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForDropTable,
buildDDLEventFunc: buildDDLEventForDropTable,
},
model.ActionAddColumn: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionDropColumn: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionAddIndex: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionDropIndex: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionAddForeignKey: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionDropForeignKey: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionTruncateTable: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForTruncateTable,
updateDDLHistoryFunc: updateDDLHistoryForTruncateTable,
updateFullTableInfoFunc: updateFullTableInfoForTruncateTable,
updateSchemaMetadataFunc: updateSchemaMetadataForTruncateTable,
iterateEventTablesFunc: iterateEventTablesForTruncateTable,
extractTableInfoFunc: extractTableInfoFuncForTruncateTable,
buildDDLEventFunc: buildDDLEventForTruncateTable,
},
model.ActionModifyColumn: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionRebaseAutoID: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionRenameTable: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForRenameTable,
updateDDLHistoryFunc: updateDDLHistoryForAddDropTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForRenameTable,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForRenameTable,
},
model.ActionSetDefaultValue: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionShardRowID: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionModifyTableComment: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionRenameIndex: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionAddTablePartition: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalPartitionDDL,
updateDDLHistoryFunc: updateDDLHistoryForAddPartition,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForAddPartition,
iterateEventTablesFunc: iterateEventTablesForAddPartition,
extractTableInfoFunc: extractTableInfoFuncForAddPartition,
buildDDLEventFunc: buildDDLEventForAddPartition,
},
model.ActionDropTablePartition: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalPartitionDDL,
updateDDLHistoryFunc: updateDDLHistoryForDropPartition,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForDropPartition,
iterateEventTablesFunc: iterateEventTablesForDropPartition,
extractTableInfoFunc: extractTableInfoFuncForDropPartition,
buildDDLEventFunc: buildDDLEventForDropPartition,
},
model.ActionCreateView: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForCreateView,
updateDDLHistoryFunc: updateDDLHistoryForCreateView,
updateFullTableInfoFunc: updateFullTableInfoIgnore,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesIgnore,
extractTableInfoFunc: extractTableInfoFuncIgnore,
buildDDLEventFunc: buildDDLEventForCreateView,
},
model.ActionModifyTableCharsetAndCollate: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionTruncateTablePartition: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalPartitionDDL,
updateDDLHistoryFunc: updateDDLHistoryForTruncatePartition,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForTruncateTablePartition,
iterateEventTablesFunc: iterateEventTablesForTruncatePartition,
extractTableInfoFunc: extractTableInfoFuncForTruncateAndReorganizePartition,
buildDDLEventFunc: buildDDLEventForTruncateAndReorganizePartition,
},
model.ActionDropView: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForDropView,
updateDDLHistoryFunc: updateDDLHistoryForTableTriggerOnlyDDL,
updateFullTableInfoFunc: updateFullTableInfoIgnore,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesIgnore,
extractTableInfoFunc: extractTableInfoFuncIgnore,
buildDDLEventFunc: buildDDLEventForDropView,
},
model.ActionRecoverTable: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForCreateTable,
updateDDLHistoryFunc: updateDDLHistoryForAddDropTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForNewTableDDL,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNewTableDDL,
},
model.ActionModifySchemaCharsetAndCollate: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForSchemaDDL,
updateDDLHistoryFunc: updateDDLHistoryForSchemaDDL,
updateFullTableInfoFunc: updateFullTableInfoIgnore,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesIgnore,
extractTableInfoFunc: extractTableInfoFuncIgnore,
buildDDLEventFunc: buildDDLEventForModifySchemaCharsetAndCollate,
},
model.ActionAddPrimaryKey: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionDropPrimaryKey: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionAlterIndexVisibility: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionExchangeTablePartition: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForExchangePartition,
updateDDLHistoryFunc: updateDDLHistoryForExchangeTablePartition,
updateFullTableInfoFunc: updateFullTableInfoForExchangeTablePartition,
updateSchemaMetadataFunc: updateSchemaMetadataForExchangeTablePartition,
iterateEventTablesFunc: iterateEventTablesForExchangeTablePartition,
extractTableInfoFunc: extractTableInfoFuncForExchangeTablePartition,
buildDDLEventFunc: buildDDLEventForExchangeTablePartition,
},
model.ActionRenameTables: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForRenameTables,
updateDDLHistoryFunc: updateDDLHistoryForRenameTables,
updateFullTableInfoFunc: updateFullTableInfoForMultiTablesDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForRenameTables,
iterateEventTablesFunc: iterateEventTablesForRenameTables,
extractTableInfoFunc: extractTableInfoFuncForRenameTables,
buildDDLEventFunc: buildDDLEventForRenameTables,
},
model.ActionCreateTables: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForCreateTables,
updateDDLHistoryFunc: updateDDLHistoryForCreateTables,
updateFullTableInfoFunc: updateFullTableInfoForMultiTablesDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForCreateTables,
iterateEventTablesFunc: iterateEventTablesForCreateTables,
extractTableInfoFunc: extractTableInfoFuncForCreateTables,
buildDDLEventFunc: buildDDLEventForCreateTables,
},
model.ActionMultiSchemaChange: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTable,
},
model.ActionReorganizePartition: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalPartitionDDL,
updateDDLHistoryFunc: updateDDLHistoryForReorganizePartition,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForReorganizePartition,
iterateEventTablesFunc: iterateEventTablesForReorganizePartition,
extractTableInfoFunc: extractTableInfoFuncForTruncateAndReorganizePartition,
buildDDLEventFunc: buildDDLEventForTruncateAndReorganizePartition,
},
model.ActionAlterTTLInfo: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForAlterTableTTL,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTableForTiDB,
},
model.ActionAlterTTLRemove: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForAlterTableTTL,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTableForTiDB,
},
model.ActionAlterTablePartitioning: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForAlterTablePartitioning,
updateDDLHistoryFunc: updateDDLHistoryForAlterTablePartitioning,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForAlterTablePartitioning,
iterateEventTablesFunc: iterateEventTablesForAlterTablePartitioning,
extractTableInfoFunc: extractTableInfoFuncForAlterTablePartitioning,
buildDDLEventFunc: buildDDLEventForAlterTablePartitioning,
},
model.ActionRemovePartitioning: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForRemovePartitioning,
updateDDLHistoryFunc: updateDDLHistoryForRemovePartitioning,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataForRemovePartitioning,
iterateEventTablesFunc: iterateEventTablesForRemovePartitioning,
extractTableInfoFunc: extractTableInfoFuncForRemovePartitioning,
buildDDLEventFunc: buildDDLEventForRemovePartitioning,
},
filter.ActionAddFullTextIndex: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTableForTiDB,
},
filter.ActionCreateHybridIndex: {
buildPersistedDDLEventFunc: buildPersistedDDLEventForNormalDDLOnSingleTable,
updateDDLHistoryFunc: updateDDLHistoryForNormalDDLOnSingleTable,
updateFullTableInfoFunc: updateFullTableInfoForSingleTableDDL,
updateSchemaMetadataFunc: updateSchemaMetadataIgnore,
iterateEventTablesFunc: iterateEventTablesForSingleTableDDL,
extractTableInfoFunc: extractTableInfoFuncForSingleTableDDL,
buildDDLEventFunc: buildDDLEventForNormalDDLOnSingleTableForTiDB,
},
}
func isPartitionTable(tableInfo *model.TableInfo) bool {
// tableInfo may only be nil in unit test
return tableInfo != nil && tableInfo.Partition != nil
}
func getAllPartitionIDs(tableInfo *model.TableInfo) []int64 {
physicalIDs := make([]int64, 0, len(tableInfo.Partition.Definitions))
for _, partition := range tableInfo.Partition.Definitions {
physicalIDs = append(physicalIDs, partition.ID)
}
return physicalIDs
}
func getSchemaName(databaseMap map[int64]*BasicDatabaseInfo, schemaID int64) string {
databaseInfo, ok := databaseMap[schemaID]
if !ok {
log.Panic("database not found", zap.Int64("schemaID", schemaID))
}
return databaseInfo.Name
}
func getTableName(tableMap map[int64]*BasicTableInfo, tableID int64) string {
tableInfo, ok := tableMap[tableID]
if !ok {
log.Panic("table not found", zap.Int64("tableID", tableID))
}
return tableInfo.Name
}
func getSchemaID(tableMap map[int64]*BasicTableInfo, tableID int64) int64 {
tableInfo, ok := tableMap[tableID]
if !ok {
log.Panic("table not found", zap.Int64("tableID", tableID))
}
return tableInfo.SchemaID
}
// schemaName should be "Name.O"
func findSchemaIDByName(databaseMap map[int64]*BasicDatabaseInfo, schemaName string) (int64, bool) {
for id, info := range databaseMap {
if strings.EqualFold(info.Name, schemaName) {
return id, true
}
}
return 0, false
}
// tableName should be "Name.O"
func findTableIDByName(tableMap map[int64]*BasicTableInfo, schemaID int64, tableName string) (int64, bool) {
for id, info := range tableMap {
if info.SchemaID == schemaID && strings.EqualFold(info.Name, tableName) {
return id, true
}
}
return 0, false
}
// =======
// buildPersistedDDLEventFunc start
// =======
func buildPersistedDDLEventCommon(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
var query string
job := args.job
// TODO: for ActionAddFullTextIndex and ActionCreateHybridIndex,
// the parser cannot recogonize the query now, so we keep the original query in job.Query.
// only in unit test job.Query may be empty
if job.Type != filter.ActionAddFullTextIndex && job.Type != filter.ActionCreateHybridIndex && job.Query != "" {
var err error
query, err = transformDDLJobQuery(job)
if err != nil {
log.Error("transformDDLJobQuery failed", zap.String("query", job.Query), zap.Error(err))
// send the original query to downstream if transform failed
query = job.Query
}
} else {
query = job.Query
}
// Note: if a ddl involve multiple tables, job.TableID is different with job.BinlogInfo.TableInfo.ID
// and usually job.BinlogInfo.TableInfo.ID will be the newly created IDs.
event := PersistedDDLEvent{
ID: job.ID,
Type: byte(job.Type),
SchemaID: job.SchemaID,
TableID: job.TableID,
Query: query,
SchemaVersion: job.BinlogInfo.SchemaVersion,
DBInfo: job.BinlogInfo.DBInfo,
TableInfo: job.BinlogInfo.TableInfo,
FinishedTs: job.BinlogInfo.FinishedTS,
StartTs: job.StartTS,
BDRRole: job.BDRRole,
CDCWriteSource: job.CDCWriteSource,
}
return event
}
func buildPersistedDDLEventForSchemaDDL(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
log.Info("buildPersistedDDLEvent for create/drop schema",
zap.Any("type", event.Type),
zap.Int64("schemaID", event.SchemaID),
zap.String("schemaName", event.DBInfo.Name.O))
event.SchemaName = event.DBInfo.Name.O
return event
}
func buildPersistedDDLEventForCreateView(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
event.TableName = args.job.TableName
return event
}
func buildPersistedDDLEventForDropView(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
// We don't store the relationship: view_id -> table_name, get table name from args.job
event.TableName = args.job.TableName
// The query in job maybe "DROP VIEW test1.view1, test2.view2", we need rebuild it here.
event.Query = fmt.Sprintf("DROP VIEW %s", common.QuoteSchema(event.SchemaName, event.TableName))
return event
}
func buildPersistedDDLEventForCreateTable(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
event.TableName = event.TableInfo.Name.O
setReferTableForCreateTableLike(&event, args)
return event
}
func setReferTableForCreateTableLike(event *PersistedDDLEvent, args buildPersistedDDLEventFuncArgs) {
if event.Query == "" {
return
}
stmt, err := parser.New().ParseOneStmt(event.Query, "", "")
if err != nil {
log.Error("parse create table ddl failed",
zap.String("query", event.Query),
zap.Error(err))
return
}
createStmt, ok := stmt.(*ast.CreateTableStmt)
if !ok || createStmt.ReferTable == nil {
return
}
refTable := createStmt.ReferTable.Name.O
refSchema := createStmt.ReferTable.Schema.O
if refSchema == "" {
refSchema = event.SchemaName
}
refSchemaID, ok := findSchemaIDByName(args.databaseMap, refSchema)
if !ok {
log.Warn("refer schema not found for create table like",
zap.String("schema", refSchema),
zap.String("table", refTable),
zap.String("query", event.Query))
return
}
refTableID, ok := findTableIDByName(args.tableMap, refSchemaID, refTable)
if !ok {
log.Warn("refer table not found for create table like",
zap.String("schema", refSchema),
zap.String("table", refTable),
zap.String("query", event.Query))
return
}
event.ExtraTableID = refTableID
if partitions, ok := args.partitionMap[refTableID]; ok {
event.ReferTablePartitionIDs = event.ReferTablePartitionIDs[:0]
for id := range partitions {
event.ReferTablePartitionIDs = append(event.ReferTablePartitionIDs, id)
}
}
}
func buildPersistedDDLEventForDropTable(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
event.TableName = getTableName(args.tableMap, event.TableID)
// The query in job maybe "DROP TABLE test1.table1, test2.table2", we need rebuild it here.
event.Query = fmt.Sprintf("DROP TABLE %s", common.QuoteSchema(event.SchemaName, event.TableName))
return event
}
func buildPersistedDDLEventForNormalDDLOnSingleTable(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
event.TableName = getTableName(args.tableMap, event.TableID)
return event
}
func buildPersistedDDLEventForTruncateTable(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
// only table id change after truncate
event.ExtraTableID = event.TableInfo.ID
// schema/table name remains the same, so it is ok to get them using old table/schema id
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
event.TableName = getTableName(args.tableMap, event.TableID)
if isPartitionTable(event.TableInfo) {
for id := range args.partitionMap[event.TableID] {
event.PrevPartitions = append(event.PrevPartitions, id)
}
}
return event
}
func buildPersistedDDLEventForRenameTable(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
// Note: schema id/schema name/table name may be changed or not
// table id does not change, we use it to get the table's prev schema id/name and table name
event.ExtraSchemaID = getSchemaID(args.tableMap, event.TableID)
// TODO: check how ExtraTableName will be used later
event.ExtraTableName = getTableName(args.tableMap, event.TableID)
event.ExtraSchemaName = getSchemaName(args.databaseMap, event.ExtraSchemaID)
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
// get the table's current table name from the ddl job
event.TableName = event.TableInfo.Name.O
// The old schema/table names cannot rely on ExtraSchemaName/ExtraTableName,
// because the snapshot used by schema store may already reflect the post-rename state.
// Example (after https://github.com/pingcap/tidb/pull/43341):
// table `test.t`, DDL `rename table t to test2.t;`, commit ts = 100
// snapshot at ts = 99 already shows `t` under `test2`
// => event.ExtraSchemaName becomes `test2`, which is wrong for the old name
// SchemaStore can still use ExtraSchemaID to update internal state,
// but the emitted event.Query must carry the correct old names.
// Rebuild them with the following precedence:
// 1. InvolvingSchemaInfo provides a fallback old schema/table pair, but names may be normalized.
// 2. RenameTableArgs.OldSchemaName overrides the fallback when available.
// It is reliable in TiDB >= v8.5, but can be missing in older versions.
// 3. The original query (if it specifies old schema) has the highest priority for identifier case.
// 4. If the query omits old schema and ExtraSchemaID differs from SchemaID, use ExtraSchemaID to
// recover the old schema name from the schema store.
oldSchemaName := ""
oldTableName := ""
oldSchemaSource := "unknown"
if len(args.job.InvolvingSchemaInfo) > 0 {
oldSchemaName = args.job.InvolvingSchemaInfo[0].Database
oldTableName = args.job.InvolvingSchemaInfo[0].Table
if oldSchemaName != "" {
oldSchemaSource = "involving_schema_info"
}
}
if args.job.Version == model.JobVersion1 || args.job.Version == model.JobVersion2 {
if renameArgs, err := model.GetRenameTableArgs(args.job); err == nil {
if renameArgs.OldSchemaName.O != "" {
oldSchemaName = renameArgs.OldSchemaName.O
oldSchemaSource = "rename_table_args"
}
} else {
log.Warn("failed to get rename table args from ddl job",
zap.Int64("jobID", args.job.ID),
zap.String("query", event.Query),
zap.Error(err))
}
}
queryProvidedOldSchema := false
if queryInfo, parsed := parseRenameTableQueryInfo(args.job.Query); parsed {
if queryInfo.oldTableName != "" {
oldTableName = queryInfo.oldTableName
}
if queryInfo.oldSchemaName != "" {
oldSchemaName = queryInfo.oldSchemaName
queryProvidedOldSchema = true
oldSchemaSource = "query"
}
}
// ExtraSchemaID can be incorrect due to snapshot timing, so only use it if the query
// does not specify the old schema.
if !queryProvidedOldSchema && event.ExtraSchemaID != 0 && event.ExtraSchemaID != event.SchemaID {
if extraName := getSchemaName(args.databaseMap, event.ExtraSchemaID); extraName != "" {
oldSchemaName = extraName
oldSchemaSource = "extra_schema_id"
}
}
if oldSchemaName != "" && oldTableName != "" {
log.Info("rebuild rename table query",
zap.Int64("jobID", event.ID),
zap.String("query", event.Query),
zap.Int64("schemaID", event.SchemaID),
zap.String("schemaName", event.SchemaName),
zap.String("tableName", event.TableName),
zap.Int64("extraSchemaID", event.ExtraSchemaID),
zap.String("extraSchemaName", event.ExtraSchemaName),
zap.String("extraTableName", event.ExtraTableName),
zap.String("oldSchemaName", oldSchemaName),
zap.String("oldTableName", oldTableName),
zap.String("oldSchemaSource", oldSchemaSource))
event.Query = fmt.Sprintf("RENAME TABLE %s TO %s",
common.QuoteSchema(oldSchemaName, oldTableName),
common.QuoteSchema(event.SchemaName, event.TableName))
}
return event
}
func buildPersistedDDLEventForNormalPartitionDDL(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventForNormalDDLOnSingleTable(args)
for id := range args.partitionMap[event.TableID] {
event.PrevPartitions = append(event.PrevPartitions, id)
}
return event
}
// buildPersistedDDLEventForExchangePartition build a exchange partition ddl event
// the TableID belongs to the new table(nt)
// the TableInfo belongs to the previous table(pt)
func buildPersistedDDLEventForExchangePartition(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
// these are the info of the normal table before exchange
event.TableName = getTableName(args.tableMap, event.TableID)
event.SchemaID = getSchemaID(args.tableMap, event.TableID)
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
// these are the info of the partition table after exchange
event.ExtraTableID = event.TableInfo.ID
event.ExtraTableName = getTableName(args.tableMap, event.ExtraTableID)
event.ExtraSchemaID = getSchemaID(args.tableMap, event.ExtraTableID)
event.ExtraSchemaName = getSchemaName(args.databaseMap, event.ExtraSchemaID)
for id := range args.partitionMap[event.ExtraTableID] {
event.PrevPartitions = append(event.PrevPartitions, id)
}
// Note: event.ExtraTableInfo is set somewhere else,
// because it is hard to get the table info of the normal table in this func.
if event.Query != "" {
upperQuery := strings.ToUpper(event.Query)
idx1 := strings.Index(upperQuery, "EXCHANGE PARTITION") + len("EXCHANGE PARTITION")
idx2 := strings.Index(upperQuery, "WITH TABLE")
// Note that partition name should be parsed from original query, not the upperQuery.
partName := strings.TrimSpace(event.Query[idx1:idx2])
partName = strings.Replace(partName, "`", "", -1)
event.Query = fmt.Sprintf("ALTER TABLE %s EXCHANGE PARTITION %s WITH TABLE %s",
common.QuoteSchema(event.ExtraSchemaName, event.ExtraTableName),
common.QuoteName(partName),
common.QuoteSchema(event.SchemaName, event.TableName))
if strings.HasSuffix(upperQuery, "WITHOUT VALIDATION") {
event.Query += " WITHOUT VALIDATION"
}
} else {
log.Warn("exchange partition query is empty, should only happen in unit tests",
zap.Int64("jobID", event.ID))
}
return event
}
type renameTableQueryInfo struct {
oldSchemaName string
oldTableName string
newSchemaName string
newTableName string
}
func parseRenameTableQueryInfo(query string) (renameTableQueryInfo, bool) {
if query == "" {
return renameTableQueryInfo{}, false
}
stmt, err := parser.New().ParseOneStmt(query, "", "")
if err != nil {
log.Warn("parse rename table query failed",
zap.String("query", query),
zap.Error(err))
return renameTableQueryInfo{}, false
}
switch s := stmt.(type) {
case *ast.AlterTableStmt:
return renameTableQueryInfo{
oldSchemaName: s.Table.Schema.O,
oldTableName: s.Table.Name.O,
}, true
case *ast.RenameTableStmt:
if len(s.TableToTables) == 0 {
return renameTableQueryInfo{}, false
}
return renameTableQueryInfo{
oldSchemaName: s.TableToTables[0].OldTable.Schema.O,
oldTableName: s.TableToTables[0].OldTable.Name.O,
newSchemaName: s.TableToTables[0].NewTable.Schema.O,
newTableName: s.TableToTables[0].NewTable.Name.O,
}, true
default:
return renameTableQueryInfo{}, false
}
}
func parseRenameTablesQueryInfos(query string) ([]renameTableQueryInfo, bool) {
if query == "" {
return nil, false
}
stmt, err := parser.New().ParseOneStmt(query, "", "")
if err != nil {
log.Warn("parse rename tables query failed",
zap.String("query", query),
zap.Error(err))
return nil, false
}
renameStmt, ok := stmt.(*ast.RenameTableStmt)
if !ok {
return nil, false
}
queryInfos := make([]renameTableQueryInfo, 0, len(renameStmt.TableToTables))
for _, tableToTable := range renameStmt.TableToTables {
queryInfos = append(queryInfos, renameTableQueryInfo{
oldSchemaName: tableToTable.OldTable.Schema.O,
oldTableName: tableToTable.OldTable.Name.O,
newSchemaName: tableToTable.NewTable.Schema.O,
newTableName: tableToTable.NewTable.Name.O,
})
}
return queryInfos, true
}
func buildPersistedDDLEventForRenameTables(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
// TODO: does rename tables has the same problem(finished ts is not the real commit ts) with rename table?
event := buildPersistedDDLEventCommon(args)
renameArgs, err := model.GetRenameTablesArgs(args.job)
if err != nil {
log.Panic("GetRenameTablesArgs failed",
zap.String("query", args.job.Query),
zap.Error(err))
}
renameTableInfos := renameArgs.RenameTableInfos
multipleTableInfos := args.job.BinlogInfo.MultipleTableInfos
if len(renameTableInfos) != len(multipleTableInfos) {
log.Panic("should not happen",
zap.Int("renameArgsLen", len(renameTableInfos)),
zap.Int("multipleTableInfosLen", len(multipleTableInfos)),
zap.String("query", args.job.Query))
}
// RenameTableInfos may store normalized lower-case names,
// while the original query can keep user-provided identifier case.
// Prefer names parsed from the original query whenever possible.
// See https://github.com/pingcap/ticdc/pull/2218 for background.
queryInfos, queryParsed := parseRenameTablesQueryInfos(args.job.Query)
if queryParsed && len(queryInfos) != len(renameTableInfos) {
log.Panic("rename tables query info length is inconsistent with args",
zap.Int("queryInfosLen", len(queryInfos)),
zap.Int("renameArgsLen", len(renameTableInfos)),
zap.String("query", args.job.Query))
}
// TiDB <= v8.1 may emit empty old table names for RENAME TABLES args.
// See https://github.com/pingcap/tidb/pull/64421 for the upstream fix.
if !queryParsed && renameTableInfos[0].OldTableName.O == "" {
// TODO: return error instead of falling back to args once builder supports error propagation.
log.Warn("rename tables args miss old table name and query is unavailable, keep args as-is",
zap.Int("tableCount", len(renameTableInfos)),
zap.String("query", args.job.Query))
}
var querys []string
for i, info := range renameTableInfos {
oldSchemaID := info.OldSchemaID
oldSchemaName := info.OldSchemaName.O
oldTableName := info.OldTableName.O
newSchemaName := getSchemaName(args.databaseMap, info.NewSchemaID)
newTableName := info.NewTableName.O
if queryParsed {
queryInfo := queryInfos[i]
if queryInfo.oldSchemaName != "" {
oldSchemaName = queryInfo.oldSchemaName
}
if queryInfo.oldTableName != "" {
oldTableName = queryInfo.oldTableName
}
if queryInfo.newSchemaName != "" {
newSchemaName = queryInfo.newSchemaName
}
if queryInfo.newTableName != "" {
newTableName = queryInfo.newTableName
}
}
event.ExtraSchemaIDs = append(event.ExtraSchemaIDs, oldSchemaID)
event.ExtraSchemaNames = append(event.ExtraSchemaNames, oldSchemaName)
event.ExtraTableNames = append(event.ExtraTableNames, oldTableName)
event.SchemaIDs = append(event.SchemaIDs, info.NewSchemaID)
event.SchemaNames = append(event.SchemaNames, newSchemaName)
querys = append(querys, fmt.Sprintf("RENAME TABLE %s TO %s;",
common.QuoteSchema(oldSchemaName, oldTableName),
common.QuoteSchema(newSchemaName, newTableName)))
}
event.Query = strings.Join(querys, "")
event.MultipleTableInfos = multipleTableInfos
// we have to reverse MultipleTableInfos to get correct schema name
// see https://github.com/pingcap/tidb/issues/63710
//
// If renaming tables creates cycles, some tables may not be created, and their table IDs may coincide with the latest table.
// For example: renaming table 'a' to 'common.c', 'b' to 'a', and 'common.c' to 'b' results in table 'c' not being created,
// with its table ID equal to that of 'b'. This leads to incorrect table info in the function `extractTableInfoFuncForRenameTables`.
// To avoid this situation, we must use the latest table info and disregard previous entries when encountering duplicate table info.
// Therefore, we set the table ID of the previous table info to `InvalidTableID` to ensure that each table ID is unique.
visitTableIDs := make(map[int64]struct{})
for i := len(event.MultipleTableInfos) - 1; i >= 0; i-- {
id := event.MultipleTableInfos[i].ID
if _, ok := visitTableIDs[id]; ok {
event.MultipleTableInfos[i].ID = InvalidTableID
} else {
visitTableIDs[id] = struct{}{}
}
}
return event
}
func buildPersistedDDLEventForCreateTables(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {
event := buildPersistedDDLEventCommon(args)
event.SchemaName = getSchemaName(args.databaseMap, event.SchemaID)
event.MultipleTableInfos = args.job.BinlogInfo.MultipleTableInfos
return event
}
func buildPersistedDDLEventForAlterTablePartitioning(args buildPersistedDDLEventFuncArgs) PersistedDDLEvent {