/
handler0alter.cc
11657 lines (10094 loc) · 326 KB
/
handler0alter.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) 2005, 2019, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2013, 2022, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file handler/handler0alter.cc
Smart ALTER TABLE
*******************************************************/
/* Include necessary SQL headers */
#include "univ.i"
#include <debug_sync.h>
#include <log.h>
#include <sql_lex.h>
#include <sql_class.h>
#include <sql_table.h>
#include <mysql/plugin.h>
/* Include necessary InnoDB headers */
#include "btr0sea.h"
#include "dict0crea.h"
#include "dict0dict.h"
#include "dict0load.h"
#include "dict0stats.h"
#include "dict0stats_bg.h"
#include "log0log.h"
#include "rem0types.h"
#include "row0log.h"
#include "row0merge.h"
#include "row0ins.h"
#include "row0row.h"
#include "row0upd.h"
#include "trx0trx.h"
#include "trx0purge.h"
#include "handler0alter.h"
#include "srv0mon.h"
#include "srv0srv.h"
#include "fts0priv.h"
#include "fts0plugin.h"
#include "pars0pars.h"
#include "row0sel.h"
#include "ha_innodb.h"
#include "ut0stage.h"
#include <thread>
#include <sstream>
/** File format constraint for ALTER TABLE */
extern ulong innodb_instant_alter_column_allowed;
static const char *MSG_UNSUPPORTED_ALTER_ONLINE_ON_VIRTUAL_COLUMN=
"INPLACE ADD or DROP of virtual columns cannot be "
"combined with other ALTER TABLE actions";
/** Operations for creating secondary indexes (no rebuild needed) */
static const alter_table_operations INNOBASE_ONLINE_CREATE
= ALTER_ADD_NON_UNIQUE_NON_PRIM_INDEX
| ALTER_ADD_UNIQUE_INDEX;
/** Operations that require filling in default values for columns */
static const alter_table_operations INNOBASE_DEFAULTS
= ALTER_COLUMN_NOT_NULLABLE
| ALTER_ADD_STORED_BASE_COLUMN;
/** Operations that require knowledge about row_start, row_end values */
static const alter_table_operations INNOBASE_ALTER_VERSIONED_REBUILD
= ALTER_ADD_SYSTEM_VERSIONING
| ALTER_DROP_SYSTEM_VERSIONING;
/** Operations for rebuilding a table in place */
static const alter_table_operations INNOBASE_ALTER_REBUILD
= ALTER_ADD_PK_INDEX
| ALTER_DROP_PK_INDEX
| ALTER_OPTIONS
/* ALTER_OPTIONS needs to check alter_options_need_rebuild() */
| ALTER_COLUMN_NULLABLE
| INNOBASE_DEFAULTS
| ALTER_STORED_COLUMN_ORDER
| ALTER_DROP_STORED_COLUMN
| ALTER_RECREATE_TABLE
/*
| ALTER_STORED_COLUMN_TYPE
*/
| INNOBASE_ALTER_VERSIONED_REBUILD
;
/** Operations that require changes to data */
static const alter_table_operations INNOBASE_ALTER_DATA
= INNOBASE_ONLINE_CREATE | INNOBASE_ALTER_REBUILD;
/** Operations for altering a table that InnoDB does not care about */
static const alter_table_operations INNOBASE_INPLACE_IGNORE
= ALTER_COLUMN_DEFAULT
| ALTER_PARTITIONED
| ALTER_COLUMN_COLUMN_FORMAT
| ALTER_COLUMN_STORAGE_TYPE
| ALTER_CONVERT_TO
| ALTER_VIRTUAL_GCOL_EXPR
| ALTER_DROP_CHECK_CONSTRAINT
| ALTER_RENAME
| ALTER_INDEX_ORDER
| ALTER_COLUMN_INDEX_LENGTH
| ALTER_CHANGE_INDEX_COMMENT
| ALTER_INDEX_IGNORABILITY;
/** Operations on foreign key definitions (changing the schema only) */
static const alter_table_operations INNOBASE_FOREIGN_OPERATIONS
= ALTER_DROP_FOREIGN_KEY
| ALTER_ADD_FOREIGN_KEY;
/** Operations that InnoDB cares about and can perform without creating data */
static const alter_table_operations INNOBASE_ALTER_NOCREATE
= ALTER_DROP_NON_UNIQUE_NON_PRIM_INDEX
| ALTER_DROP_UNIQUE_INDEX;
/** Operations that InnoDB cares about and can perform without validation */
static const alter_table_operations INNOBASE_ALTER_NOVALIDATE
= INNOBASE_ALTER_NOCREATE
| ALTER_VIRTUAL_COLUMN_ORDER
| ALTER_COLUMN_NAME
| INNOBASE_FOREIGN_OPERATIONS
| ALTER_COLUMN_UNVERSIONED
| ALTER_DROP_VIRTUAL_COLUMN;
/** Operations that InnoDB cares about and can perform without rebuild */
static const alter_table_operations INNOBASE_ALTER_NOREBUILD
= INNOBASE_ONLINE_CREATE
| INNOBASE_ALTER_NOCREATE;
/** Operations that can be performed instantly, without inplace_alter_table() */
static const alter_table_operations INNOBASE_ALTER_INSTANT
= ALTER_VIRTUAL_COLUMN_ORDER
| ALTER_COLUMN_NAME
| ALTER_ADD_VIRTUAL_COLUMN
| INNOBASE_FOREIGN_OPERATIONS
| ALTER_COLUMN_TYPE_CHANGE_BY_ENGINE
| ALTER_COLUMN_UNVERSIONED
| ALTER_RENAME_INDEX
| ALTER_DROP_VIRTUAL_COLUMN;
/** Initialize instant->field_map.
@param[in] table table definition to copy from */
inline void dict_table_t::init_instant(const dict_table_t& table)
{
const dict_index_t& oindex __attribute__((unused))= *table.indexes.start;
dict_index_t& index = *indexes.start;
const unsigned u = index.first_user_field();
DBUG_ASSERT(u == oindex.first_user_field());
DBUG_ASSERT(index.n_fields >= oindex.n_fields);
field_map_element_t* field_map_it = static_cast<field_map_element_t*>(
mem_heap_zalloc(heap, (index.n_fields - u)
* sizeof *field_map_it));
instant->field_map = field_map_it;
ut_d(unsigned n_drop = 0);
ut_d(unsigned n_nullable = 0);
for (unsigned i = u; i < index.n_fields; i++) {
auto& f = index.fields[i];
ut_d(n_nullable += f.col->is_nullable());
if (!f.col->is_dropped()) {
(*field_map_it++).set_ind(f.col->ind);
continue;
}
auto fixed_len = dict_col_get_fixed_size(
f.col, not_redundant());
field_map_it->set_dropped();
if (!f.col->is_nullable()) {
field_map_it->set_not_null();
}
field_map_it->set_ind(fixed_len
? uint16_t(fixed_len + 1)
: DATA_BIG_COL(f.col));
field_map_it++;
ut_ad(f.col >= table.instant->dropped);
ut_ad(f.col < table.instant->dropped
+ table.instant->n_dropped);
ut_d(n_drop++);
size_t d = f.col - table.instant->dropped;
ut_ad(f.col == &table.instant->dropped[d]);
ut_ad(d <= instant->n_dropped);
f.col = &instant->dropped[d];
}
ut_ad(n_drop == n_dropped());
ut_ad(field_map_it == &instant->field_map[index.n_fields - u]);
ut_ad(index.n_nullable == n_nullable);
}
/** Set is_instant() before instant_column().
@param[in] old previous table definition
@param[in] col_map map from old.cols[] and old.v_cols[] to this
@param[out] first_alter_pos 0, or 1 + first changed column position */
inline void dict_table_t::prepare_instant(const dict_table_t& old,
const ulint* col_map,
unsigned& first_alter_pos)
{
DBUG_ASSERT(!is_instant());
DBUG_ASSERT(n_dropped() == 0);
DBUG_ASSERT(old.n_cols == old.n_def);
DBUG_ASSERT(n_cols == n_def);
DBUG_ASSERT(old.supports_instant());
DBUG_ASSERT(not_redundant() == old.not_redundant());
DBUG_ASSERT(DICT_TF_HAS_ATOMIC_BLOBS(flags)
== DICT_TF_HAS_ATOMIC_BLOBS(old.flags));
DBUG_ASSERT(!persistent_autoinc
|| persistent_autoinc == old.persistent_autoinc);
/* supports_instant() does not necessarily hold here,
in case ROW_FORMAT=COMPRESSED according to the
MariaDB data dictionary, and ALTER_OPTIONS was not set.
If that is the case, the instant ALTER TABLE would keep
the InnoDB table in its current format. */
dict_index_t& oindex = *old.indexes.start;
dict_index_t& index = *indexes.start;
first_alter_pos = 0;
for (unsigned i = 0; i + DATA_N_SYS_COLS < old.n_cols; i++) {
if (col_map[i] != i) {
first_alter_pos = 1 + i;
goto add_metadata;
}
}
if (!old.instant) {
/* Columns were not dropped or reordered.
Therefore columns must have been added at the end,
or modified instantly in place. */
DBUG_ASSERT(index.n_fields >= oindex.n_fields);
DBUG_ASSERT(index.n_fields > oindex.n_fields
|| !not_redundant());
#ifdef UNIV_DEBUG
if (index.n_fields == oindex.n_fields) {
ut_ad(!not_redundant());
for (unsigned i = index.n_fields; i--; ) {
ut_ad(index.fields[i].col->same_format(
*oindex.fields[i].col));
}
}
#endif
set_core_fields:
index.n_core_fields = oindex.n_core_fields;
index.n_core_null_bytes = oindex.n_core_null_bytes;
} else {
add_metadata:
const unsigned n_old_drop = old.n_dropped();
unsigned n_drop = n_old_drop;
for (unsigned i = old.n_cols; i--; ) {
if (col_map[i] == ULINT_UNDEFINED) {
DBUG_ASSERT(i + DATA_N_SYS_COLS
< uint(old.n_cols));
n_drop++;
}
}
instant = new (mem_heap_alloc(heap, sizeof(dict_instant_t)))
dict_instant_t();
instant->n_dropped = n_drop;
if (n_drop) {
instant->dropped
= static_cast<dict_col_t*>(
mem_heap_alloc(heap, n_drop
* sizeof(dict_col_t)));
if (n_old_drop) {
memcpy(instant->dropped, old.instant->dropped,
n_old_drop * sizeof(dict_col_t));
}
} else {
instant->dropped = NULL;
}
for (unsigned i = 0, d = n_old_drop; i < old.n_cols; i++) {
if (col_map[i] == ULINT_UNDEFINED) {
(new (&instant->dropped[d++])
dict_col_t(old.cols[i]))->set_dropped();
}
}
#ifndef DBUG_OFF
for (unsigned i = 0; i < n_drop; i++) {
DBUG_ASSERT(instant->dropped[i].is_dropped());
}
#endif
const unsigned n_fields = index.n_fields + n_dropped();
DBUG_ASSERT(n_fields >= oindex.n_fields);
dict_field_t* fields = static_cast<dict_field_t*>(
mem_heap_zalloc(heap, n_fields * sizeof *fields));
unsigned i = 0, j = 0, n_nullable = 0;
ut_d(uint core_null = 0);
for (; i < oindex.n_fields; i++) {
DBUG_ASSERT(j <= i);
dict_field_t&f = fields[i] = oindex.fields[i];
if (f.col->is_dropped()) {
/* The column has been instantly
dropped earlier. */
DBUG_ASSERT(f.col >= old.instant->dropped);
{
size_t d = f.col
- old.instant->dropped;
DBUG_ASSERT(d < n_old_drop);
DBUG_ASSERT(&old.instant->dropped[d]
== f.col);
DBUG_ASSERT(!f.name);
f.col = instant->dropped + d;
}
if (f.col->is_nullable()) {
found_nullable:
n_nullable++;
ut_d(core_null
+= i < oindex.n_core_fields);
}
continue;
}
const ulint col_ind = col_map[f.col->ind];
if (col_ind != ULINT_UNDEFINED) {
if (index.fields[j].col->ind != col_ind) {
/* The fields for instantly
added columns must be placed
last in the clustered index.
Keep pre-existing fields in
the same position. */
uint k;
for (k = j + 1; k < index.n_fields;
k++) {
if (index.fields[k].col->ind
== col_ind) {
goto found_j;
}
}
DBUG_ASSERT("no such col" == 0);
found_j:
std::swap(index.fields[j],
index.fields[k]);
}
DBUG_ASSERT(index.fields[j].col->ind
== col_ind);
fields[i] = index.fields[j++];
DBUG_ASSERT(!fields[i].col->is_dropped());
DBUG_ASSERT(fields[i].name
== fields[i].col->name(*this));
if (fields[i].col->is_nullable()) {
goto found_nullable;
}
continue;
}
/* This column is being dropped. */
unsigned d = n_old_drop;
for (unsigned c = 0; c < f.col->ind; c++) {
d += col_map[c] == ULINT_UNDEFINED;
}
DBUG_ASSERT(d < n_drop);
f.col = &instant->dropped[d];
f.name = NULL;
if (f.col->is_nullable()) {
goto found_nullable;
}
}
/* In case of discarded tablespace, InnoDB can't
read the root page. So assign the null bytes based
on nullabled fields */
if (!oindex.table->space) {
oindex.n_core_null_bytes = static_cast<uint8_t>(
UT_BITS_IN_BYTES(unsigned(oindex.n_nullable)));
}
/* The n_core_null_bytes only matters for
ROW_FORMAT=COMPACT and ROW_FORMAT=DYNAMIC tables. */
ut_ad(UT_BITS_IN_BYTES(core_null) == oindex.n_core_null_bytes
|| !not_redundant());
DBUG_ASSERT(i >= oindex.n_core_fields);
DBUG_ASSERT(j <= i);
DBUG_ASSERT(n_fields - (i - j) == index.n_fields);
std::sort(index.fields + j, index.fields + index.n_fields,
[](const dict_field_t& a, const dict_field_t& b)
{ return a.col->ind < b.col->ind; });
for (; i < n_fields; i++) {
fields[i] = index.fields[j++];
n_nullable += fields[i].col->is_nullable();
DBUG_ASSERT(!fields[i].col->is_dropped());
DBUG_ASSERT(fields[i].name
== fields[i].col->name(*this));
}
DBUG_ASSERT(j == index.n_fields);
index.n_fields = index.n_def = n_fields
& dict_index_t::MAX_N_FIELDS;
index.fields = fields;
DBUG_ASSERT(n_nullable >= index.n_nullable);
DBUG_ASSERT(n_nullable >= oindex.n_nullable);
index.n_nullable = n_nullable & dict_index_t::MAX_N_FIELDS;
goto set_core_fields;
}
DBUG_ASSERT(n_cols + n_dropped() >= old.n_cols + old.n_dropped());
DBUG_ASSERT(n_dropped() >= old.n_dropped());
DBUG_ASSERT(index.n_core_fields == oindex.n_core_fields);
DBUG_ASSERT(index.n_core_null_bytes == oindex.n_core_null_bytes);
}
/** Adjust index metadata for instant ADD/DROP/reorder COLUMN.
@param[in] clustered index definition after instant ALTER TABLE */
inline void dict_index_t::instant_add_field(const dict_index_t& instant)
{
DBUG_ASSERT(is_primary());
DBUG_ASSERT(instant.is_primary());
DBUG_ASSERT(!has_virtual());
DBUG_ASSERT(!instant.has_virtual());
DBUG_ASSERT(instant.n_core_fields <= instant.n_fields);
DBUG_ASSERT(n_def == n_fields);
DBUG_ASSERT(instant.n_def == instant.n_fields);
DBUG_ASSERT(type == instant.type);
DBUG_ASSERT(trx_id_offset == instant.trx_id_offset);
DBUG_ASSERT(n_user_defined_cols == instant.n_user_defined_cols);
DBUG_ASSERT(n_uniq == instant.n_uniq);
DBUG_ASSERT(instant.n_fields >= n_fields);
DBUG_ASSERT(instant.n_nullable >= n_nullable);
DBUG_ASSERT(instant.n_core_fields == n_core_fields);
DBUG_ASSERT(instant.n_core_null_bytes == n_core_null_bytes);
/* instant will have all fields (including ones for columns
that have been or are being instantly dropped) in the same position
as this index. Fields for any added columns are appended at the end. */
#ifndef DBUG_OFF
for (unsigned i = 0; i < n_fields; i++) {
DBUG_ASSERT(fields[i].same(instant.fields[i]));
DBUG_ASSERT(instant.fields[i].col->same_format(*fields[i]
.col));
/* Instant conversion from NULL to NOT NULL is not allowed. */
DBUG_ASSERT(!fields[i].col->is_nullable()
|| instant.fields[i].col->is_nullable());
DBUG_ASSERT(fields[i].col->is_nullable()
== instant.fields[i].col->is_nullable()
|| !table->not_redundant());
}
#endif
n_fields = instant.n_fields;
n_def = instant.n_def;
n_nullable = instant.n_nullable;
fields = static_cast<dict_field_t*>(
mem_heap_dup(heap, instant.fields, n_fields * sizeof *fields));
ut_d(unsigned n_null = 0);
ut_d(unsigned n_dropped = 0);
for (unsigned i = 0; i < n_fields; i++) {
const dict_col_t* icol = instant.fields[i].col;
dict_field_t& f = fields[i];
ut_d(n_null += icol->is_nullable());
DBUG_ASSERT(!icol->is_virtual());
if (icol->is_dropped()) {
ut_d(n_dropped++);
f.col->set_dropped();
f.name = NULL;
} else {
f.col = &table->cols[icol - instant.table->cols];
f.name = f.col->name(*table);
}
}
ut_ad(n_null == n_nullable);
ut_ad(n_dropped == instant.table->n_dropped());
}
/** Adjust table metadata for instant ADD/DROP/reorder COLUMN.
@param[in] table altered table (with dropped columns)
@param[in] col_map mapping from cols[] and v_cols[] to table
@return whether the metadata record must be updated */
inline bool dict_table_t::instant_column(const dict_table_t& table,
const ulint* col_map)
{
DBUG_ASSERT(!table.cached);
DBUG_ASSERT(table.n_def == table.n_cols);
DBUG_ASSERT(table.n_t_def == table.n_t_cols);
DBUG_ASSERT(n_def == n_cols);
DBUG_ASSERT(n_t_def == n_t_cols);
DBUG_ASSERT(n_v_def == n_v_cols);
DBUG_ASSERT(table.n_v_def == table.n_v_cols);
DBUG_ASSERT(table.n_cols + table.n_dropped() >= n_cols + n_dropped());
DBUG_ASSERT(!table.persistent_autoinc
|| persistent_autoinc == table.persistent_autoinc);
ut_ad(dict_sys.locked());
{
const char* end = table.col_names;
for (unsigned i = table.n_cols; i--; ) end += strlen(end) + 1;
col_names = static_cast<char*>(
mem_heap_dup(heap, table.col_names,
ulint(end - table.col_names)));
}
const dict_col_t* const old_cols = cols;
cols = static_cast<dict_col_t*>(mem_heap_dup(heap, table.cols,
table.n_cols
* sizeof *cols));
/* Preserve the default values of previously instantly added
columns, or copy the new default values to this->heap. */
for (uint16_t i = 0; i < table.n_cols; i++) {
dict_col_t& c = cols[i];
if (const dict_col_t* o = find(old_cols, col_map, n_cols, i)) {
c.def_val = o->def_val;
DBUG_ASSERT(!((c.prtype ^ o->prtype)
& ~(DATA_NOT_NULL | DATA_VERSIONED
| CHAR_COLL_MASK << 16
| DATA_LONG_TRUE_VARCHAR)));
DBUG_ASSERT(c.same_type(*o));
DBUG_ASSERT(c.len >= o->len);
if (o->vers_sys_start()) {
ut_ad(o->ind == vers_start);
vers_start = i & dict_index_t::MAX_N_FIELDS;
} else if (o->vers_sys_end()) {
ut_ad(o->ind == vers_end);
vers_end = i & dict_index_t::MAX_N_FIELDS;
}
continue;
}
DBUG_ASSERT(c.is_added());
if (c.def_val.len <= UNIV_PAGE_SIZE_MAX
&& (!c.def_val.len
|| !memcmp(c.def_val.data, field_ref_zero,
c.def_val.len))) {
c.def_val.data = field_ref_zero;
} else if (const void*& d = c.def_val.data) {
d = mem_heap_dup(heap, d, c.def_val.len);
} else {
DBUG_ASSERT(c.def_val.len == UNIV_SQL_NULL);
}
}
n_t_def = (n_t_def + (table.n_cols - n_cols))
& dict_index_t::MAX_N_FIELDS;
n_t_cols = (n_t_cols + (table.n_cols - n_cols))
& dict_index_t::MAX_N_FIELDS;
n_def = table.n_cols;
const dict_v_col_t* const old_v_cols = v_cols;
if (const char* end = table.v_col_names) {
for (unsigned i = table.n_v_cols; i--; ) {
end += strlen(end) + 1;
}
v_col_names = static_cast<char*>(
mem_heap_dup(heap, table.v_col_names,
ulint(end - table.v_col_names)));
v_cols = static_cast<dict_v_col_t*>(
mem_heap_alloc(heap, table.n_v_cols * sizeof(*v_cols)));
for (ulint i = table.n_v_cols; i--; ) {
new (&v_cols[i]) dict_v_col_t(table.v_cols[i]);
v_cols[i].v_indexes.clear();
}
} else {
ut_ad(table.n_v_cols == 0);
v_col_names = NULL;
v_cols = NULL;
}
n_t_def = (n_t_def + (table.n_v_cols - n_v_cols))
& dict_index_t::MAX_N_FIELDS;
n_t_cols = (n_t_cols + (table.n_v_cols - n_v_cols))
& dict_index_t::MAX_N_FIELDS;
n_v_def = table.n_v_cols;
for (unsigned i = 0; i < n_v_def; i++) {
dict_v_col_t& v = v_cols[i];
DBUG_ASSERT(v.v_indexes.empty());
v.base_col = static_cast<dict_col_t**>(
mem_heap_dup(heap, v.base_col,
v.num_base * sizeof *v.base_col));
for (ulint n = v.num_base; n--; ) {
dict_col_t*& base = v.base_col[n];
if (base->is_virtual()) {
} else if (base >= table.cols
&& base < table.cols + table.n_cols) {
/* The base column was instantly added. */
size_t c = base - table.cols;
DBUG_ASSERT(base == &table.cols[c]);
base = &cols[c];
} else {
DBUG_ASSERT(base >= old_cols);
size_t c = base - old_cols;
DBUG_ASSERT(c + DATA_N_SYS_COLS < n_cols);
DBUG_ASSERT(base == &old_cols[c]);
DBUG_ASSERT(col_map[c] + DATA_N_SYS_COLS
< n_cols);
base = &cols[col_map[c]];
}
}
}
dict_index_t* index = dict_table_get_first_index(this);
bool metadata_changed;
{
const dict_index_t& i = *dict_table_get_first_index(&table);
metadata_changed = i.n_fields > index->n_fields;
ut_ad(i.n_fields >= index->n_fields);
index->instant_add_field(i);
}
if (instant || table.instant) {
const auto old_instant = instant;
/* FIXME: add instant->heap, and transfer ownership here */
if (!instant) {
instant = new (mem_heap_zalloc(heap, sizeof *instant))
dict_instant_t();
goto dup_dropped;
} else if (n_dropped() < table.n_dropped()) {
dup_dropped:
instant->dropped = static_cast<dict_col_t*>(
mem_heap_dup(heap, table.instant->dropped,
table.instant->n_dropped
* sizeof *instant->dropped));
instant->n_dropped = table.instant->n_dropped;
} else if (table.instant->n_dropped) {
memcpy(instant->dropped, table.instant->dropped,
table.instant->n_dropped
* sizeof *instant->dropped);
}
const field_map_element_t* field_map = old_instant
? old_instant->field_map : NULL;
init_instant(table);
if (!metadata_changed) {
metadata_changed = !field_map
|| memcmp(field_map,
instant->field_map,
(index->n_fields
- index->first_user_field())
* sizeof *field_map);
}
}
while ((index = dict_table_get_next_index(index)) != NULL) {
if (index->to_be_dropped) {
continue;
}
for (unsigned i = 0; i < index->n_fields; i++) {
dict_field_t& f = index->fields[i];
if (f.col >= table.cols
&& f.col < table.cols + table.n_cols) {
/* This is an instantly added column
in a newly added index. */
DBUG_ASSERT(!f.col->is_virtual());
size_t c = f.col - table.cols;
DBUG_ASSERT(f.col == &table.cols[c]);
f.col = &cols[c];
} else if (f.col >= &table.v_cols->m_col
&& f.col < &table.v_cols[n_v_cols].m_col) {
/* This is an instantly added virtual column
in a newly added index. */
DBUG_ASSERT(f.col->is_virtual());
size_t c = reinterpret_cast<dict_v_col_t*>(
f.col) - table.v_cols;
DBUG_ASSERT(f.col == &table.v_cols[c].m_col);
f.col = &v_cols[c].m_col;
} else if (f.col < old_cols
|| f.col >= old_cols + n_cols) {
DBUG_ASSERT(f.col->is_virtual());
f.col = &v_cols[col_map[
reinterpret_cast<dict_v_col_t*>(
f.col)
- old_v_cols + n_cols]].m_col;
} else {
f.col = &cols[col_map[f.col - old_cols]];
DBUG_ASSERT(!f.col->is_virtual());
}
f.name = f.col->name(*this);
if (f.col->is_virtual()) {
dict_v_col_t* v_col = reinterpret_cast
<dict_v_col_t*>(f.col);
v_col->v_indexes.push_front(
dict_v_idx_t(index, i));
}
}
}
n_cols = table.n_cols;
n_v_cols = table.n_v_cols;
return metadata_changed;
}
/** Find the old column number for the given new column position.
@param[in] col_map column map from old column to new column
@param[in] pos new column position
@param[in] n number of columns present in the column map
@return old column position for the given new column position. */
static ulint find_old_col_no(const ulint* col_map, ulint pos, ulint n)
{
do {
ut_ad(n);
} while (col_map[--n] != pos);
return n;
}
/** Roll back instant_column().
@param[in] old_n_cols original n_cols
@param[in] old_cols original cols
@param[in] old_col_names original col_names
@param[in] old_instant original instant structure
@param[in] old_fields original fields
@param[in] old_n_fields original number of fields
@param[in] old_n_core_fields original number of core fields
@param[in] old_n_v_cols original n_v_cols
@param[in] old_v_cols original v_cols
@param[in] old_v_col_names original v_col_names
@param[in] col_map column map */
inline void dict_table_t::rollback_instant(
unsigned old_n_cols,
dict_col_t* old_cols,
const char* old_col_names,
dict_instant_t* old_instant,
dict_field_t* old_fields,
unsigned old_n_fields,
unsigned old_n_core_fields,
unsigned old_n_v_cols,
dict_v_col_t* old_v_cols,
const char* old_v_col_names,
const ulint* col_map)
{
ut_ad(dict_sys.locked());
if (cols == old_cols) {
/* Alter fails before instant operation happens.
So there is no need to do rollback instant operation */
return;
}
dict_index_t* index = indexes.start;
/* index->is_instant() does not necessarily hold here, because
the table may have been emptied */
DBUG_ASSERT(old_n_cols >= DATA_N_SYS_COLS);
DBUG_ASSERT(n_cols == n_def);
DBUG_ASSERT(index->n_def == index->n_fields);
DBUG_ASSERT(index->n_core_fields <= index->n_fields);
DBUG_ASSERT(old_n_core_fields <= old_n_fields);
DBUG_ASSERT(instant || !old_instant);
instant = old_instant;
index->n_nullable = 0;
for (unsigned i = old_n_fields; i--; ) {
if (old_fields[i].col->is_nullable()) {
index->n_nullable++;
}
}
for (unsigned i = n_v_cols; i--; ) {
v_cols[i].~dict_v_col_t();
}
index->n_core_fields = ((index->n_fields == index->n_core_fields)
? old_n_fields
: old_n_core_fields)
& dict_index_t::MAX_N_FIELDS;
index->n_def = index->n_fields = old_n_fields
& dict_index_t::MAX_N_FIELDS;
index->n_core_null_bytes = static_cast<uint8_t>(
UT_BITS_IN_BYTES(index->get_n_nullable(index->n_core_fields)));
const dict_col_t* const new_cols = cols;
const dict_col_t* const new_cols_end __attribute__((unused)) = cols + n_cols;
const dict_v_col_t* const new_v_cols = v_cols;
const dict_v_col_t* const new_v_cols_end __attribute__((unused))= v_cols + n_v_cols;
cols = old_cols;
col_names = old_col_names;
v_cols = old_v_cols;
v_col_names = old_v_col_names;
n_def = n_cols = old_n_cols & dict_index_t::MAX_N_FIELDS;
n_v_def = n_v_cols = old_n_v_cols & dict_index_t::MAX_N_FIELDS;
n_t_def = n_t_cols = (n_cols + n_v_cols) & dict_index_t::MAX_N_FIELDS;
if (versioned()) {
for (unsigned i = 0; i < n_cols; ++i) {
if (cols[i].vers_sys_start()) {
vers_start = i & dict_index_t::MAX_N_FIELDS;
} else if (cols[i].vers_sys_end()) {
vers_end = i & dict_index_t::MAX_N_FIELDS;
}
}
}
index->fields = old_fields;
while ((index = dict_table_get_next_index(index)) != NULL) {
if (index->to_be_dropped) {
/* instant_column() did not adjust these indexes. */
continue;
}
for (unsigned i = 0; i < index->n_fields; i++) {
dict_field_t& f = index->fields[i];
if (f.col->is_virtual()) {
DBUG_ASSERT(f.col >= &new_v_cols->m_col);
DBUG_ASSERT(f.col < &new_v_cols_end->m_col);
size_t n = size_t(
reinterpret_cast<dict_v_col_t*>(f.col)
- new_v_cols);
DBUG_ASSERT(n <= n_v_cols);
ulint old_col_no = find_old_col_no(
col_map + n_cols, n, n_v_cols);
DBUG_ASSERT(old_col_no <= n_v_cols);
f.col = &v_cols[old_col_no].m_col;
DBUG_ASSERT(f.col->is_virtual());
} else {
DBUG_ASSERT(f.col >= new_cols);
DBUG_ASSERT(f.col < new_cols_end);
size_t n = size_t(f.col - new_cols);
DBUG_ASSERT(n <= n_cols);
ulint old_col_no = find_old_col_no(col_map,
n, n_cols);
DBUG_ASSERT(old_col_no < n_cols);
f.col = &cols[old_col_no];
DBUG_ASSERT(!f.col->is_virtual());
}
f.name = f.col->name(*this);
}
}
}
/* Report an InnoDB error to the client by invoking my_error(). */
static ATTRIBUTE_COLD __attribute__((nonnull))
void
my_error_innodb(
/*============*/
dberr_t error, /*!< in: InnoDB error code */
const char* table, /*!< in: table name */
ulint flags) /*!< in: table flags */
{
switch (error) {
case DB_MISSING_HISTORY:
my_error(ER_TABLE_DEF_CHANGED, MYF(0));
break;
case DB_RECORD_NOT_FOUND:
my_error(ER_KEY_NOT_FOUND, MYF(0), table);
break;
case DB_DEADLOCK:
my_error(ER_LOCK_DEADLOCK, MYF(0));
break;
case DB_LOCK_WAIT_TIMEOUT:
my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0));
break;
case DB_INTERRUPTED:
my_error(ER_QUERY_INTERRUPTED, MYF(0));
break;
case DB_OUT_OF_MEMORY:
my_error(ER_OUT_OF_RESOURCES, MYF(0));
break;
case DB_OUT_OF_FILE_SPACE:
my_error(ER_RECORD_FILE_FULL, MYF(0), table);
break;
case DB_TEMP_FILE_WRITE_FAIL:
my_error(ER_TEMP_FILE_WRITE_FAILURE, MYF(0));
break;
case DB_TOO_BIG_INDEX_COL:
my_error(ER_INDEX_COLUMN_TOO_LONG, MYF(0),
(ulong) DICT_MAX_FIELD_LEN_BY_FORMAT_FLAG(flags));
break;
case DB_TOO_MANY_CONCURRENT_TRXS:
my_error(ER_TOO_MANY_CONCURRENT_TRXS, MYF(0));
break;
case DB_LOCK_TABLE_FULL:
my_error(ER_LOCK_TABLE_FULL, MYF(0));
break;
case DB_UNDO_RECORD_TOO_BIG:
my_error(ER_UNDO_RECORD_TOO_BIG, MYF(0));
break;
case DB_CORRUPTION:
my_error(ER_NOT_KEYFILE, MYF(0), table);
break;
case DB_TOO_BIG_RECORD: {
/* Note that in page0zip.ic page_zip_rec_needs_ext() rec_size
is limited to COMPRESSED_REC_MAX_DATA_SIZE (16K) or
REDUNDANT_REC_MAX_DATA_SIZE (16K-1). */
bool comp = !!(flags & DICT_TF_COMPACT);
ulint free_space = page_get_free_space_of_empty(comp) / 2;
if (free_space >= ulint(comp ? COMPRESSED_REC_MAX_DATA_SIZE :
REDUNDANT_REC_MAX_DATA_SIZE)) {
free_space = (comp ? COMPRESSED_REC_MAX_DATA_SIZE :
REDUNDANT_REC_MAX_DATA_SIZE) - 1;
}
my_error(ER_TOO_BIG_ROWSIZE, MYF(0), free_space);
break;
}
case DB_INVALID_NULL:
/* TODO: report the row, as we do for DB_DUPLICATE_KEY */
my_error(ER_INVALID_USE_OF_NULL, MYF(0));
break;
case DB_CANT_CREATE_GEOMETRY_OBJECT:
my_error(ER_CANT_CREATE_GEOMETRY_OBJECT, MYF(0));
break;
case DB_TABLESPACE_EXISTS:
my_error(ER_TABLESPACE_EXISTS, MYF(0), table);
break;
#ifdef UNIV_DEBUG
case DB_SUCCESS:
case DB_DUPLICATE_KEY:
case DB_ONLINE_LOG_TOO_BIG:
/* These codes should not be passed here. */
ut_error;
#endif /* UNIV_DEBUG */
default:
my_error(ER_GET_ERRNO, MYF(0), error, "InnoDB");
break;
}
}
/** Get the name of an erroneous key.
@param[in] error_key_num InnoDB number of the erroneus key
@param[in] ha_alter_info changes that were being performed
@param[in] table InnoDB table
@return the name of the erroneous key */
static
const char*
get_error_key_name(
ulint error_key_num,
const Alter_inplace_info* ha_alter_info,
const dict_table_t* table)
{
if (error_key_num == ULINT_UNDEFINED) {
return(FTS_DOC_ID_INDEX_NAME);
} else if (ha_alter_info->key_count == 0) {
return(dict_table_get_first_index(table)->name);
} else {
return(ha_alter_info->key_info_buffer[error_key_num].name.str);
}
}
/** Convert field type and length to InnoDB format */
static void get_type(const Field &f, uint &prtype, uint8_t &mtype,
uint16_t &len)
{
mtype= get_innobase_type_from_mysql_type(&prtype, &f);
len= static_cast<uint16_t>(f.pack_length());
prtype|= f.type();
if (f.type() == MYSQL_TYPE_VARCHAR)
{
auto l= static_cast<const Field_varstring&>(f).length_bytes;
len= static_cast<uint16_t>(len - l);
if (l == 2)
prtype|= DATA_LONG_TRUE_VARCHAR;
}
if (!f.real_maybe_null())
prtype |= DATA_NOT_NULL;
if (f.binary())
prtype |= DATA_BINARY_TYPE;
if (f.table->versioned())
{
if (&f == f.table->field[f.table->s->vers.start_fieldno])
prtype|= DATA_VERS_START;
else if (&f == f.table->field[f.table->s->vers.end_fieldno])
prtype|= DATA_VERS_END;
else if (!(f.flags & VERS_UPDATE_UNVERSIONED_FLAG))
prtype|= DATA_VERSIONED;
}
if (!f.stored_in_db())
prtype|= DATA_VIRTUAL;
if (dtype_is_string_type(mtype))
prtype|= f.charset()->number << 16;
}
struct ha_innobase_inplace_ctx : public inplace_alter_handler_ctx
{
/** Dummy query graph */
que_thr_t*const thr;
/** The prebuilt struct of the creating instance */
row_prebuilt_t*& prebuilt;
/** InnoDB indexes being created */