/
sql_table.cc
12633 lines (11307 loc) · 422 KB
/
sql_table.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) 2000, 2019, Oracle and/or its affiliates.
Copyright (c) 2010, 2022, MariaDB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
*/
/* drop and alter of tables */
#include "mariadb.h"
#include "sql_class.h"
#include "sql_priv.h"
#include "unireg.h"
#include "debug_sync.h"
#include "sql_table.h"
#include "sql_parse.h" // test_if_data_home_dir
#include "sql_cache.h" // query_cache_*
#include "sql_base.h" // lock_table_names
#include "lock.h" // mysql_unlock_tables
#include "strfunc.h" // find_type2, find_set
#include "sql_truncate.h" // regenerate_locked_table
#include "ha_partition.h" // PAR_EXT
// mem_alloc_error,
// partition_info
// NOT_A_PARTITION_ID
#include "sql_db.h" // load_db_opt_by_name
#include "records.h" // init_read_record, end_read_record
#include "filesort.h" // filesort_free_buffers
#include "sql_select.h" // setup_order
#include "sql_handler.h" // mysql_ha_rm_tables
#include "discover.h" // readfrm
#include "my_pthread.h" // pthread_mutex_t
#include "log_event.h" // Query_log_event
#include "sql_statistics.h"
#include <hash.h>
#include <myisam.h>
#include <my_dir.h>
#include "create_options.h"
#include "sp_head.h"
#include "sp.h"
#include "sql_trigger.h"
#include "sql_show.h"
#include "transaction.h"
#include "sql_audit.h"
#include "sql_sequence.h"
#include "tztime.h"
#include "sql_insert.h" // binlog_drop_table
#include "ddl_log.h"
#include "debug.h" // debug_crash_here()
#include <algorithm>
#include "wsrep_mysqld.h"
#include "rpl_mi.h"
#include "rpl_rli.h"
#include "log.h"
#include "sql_debug.h"
#ifdef _WIN32
#include <io.h>
#endif
const LEX_CSTRING primary_key_name= { STRING_WITH_LEN("PRIMARY") };
static const LEX_CSTRING generated_by_server=
{ STRING_WITH_LEN(" /* generated by server */") };
static const LEX_CSTRING SEQUENCE_clex_str= { STRING_WITH_LEN("SEQUENCE") };
static const LEX_CSTRING TABLE_clex_str= { STRING_WITH_LEN("TABLE") };
static int check_if_keyname_exists(const char *name,KEY *start, KEY *end);
static char *make_unique_key_name(THD *, const char *, KEY *, KEY *);
static bool make_unique_constraint_name(THD *, LEX_CSTRING *, const char *,
List<Virtual_column_info> *, uint *);
static const char *make_unique_invisible_field_name(THD *, const char *,
List<Create_field> *);
static int copy_data_between_tables(THD *, TABLE *,TABLE *, bool, uint,
ORDER *, ha_rows *, ha_rows *,
Alter_info *, Alter_table_ctx *);
static int append_system_key_parts(THD *, HA_CREATE_INFO *, Key *);
static int mysql_prepare_create_table(THD *, HA_CREATE_INFO *, Alter_info *,
uint *, handler *, KEY **, uint *, int);
static uint blob_length_by_type(enum_field_types type);
static bool fix_constraints_names(THD *, List<Virtual_column_info> *,
const HA_CREATE_INFO *);
static bool wait_for_master(THD *thd);
static int process_master_state(THD *thd, int alter_result,
uint64 &start_alter_id, bool if_exists);
static bool
write_bin_log_start_alter_rollback(THD *thd, uint64 &start_alter_id,
bool &partial_alter, bool if_exists);
/**
@brief Helper function for explain_filename
@param thd Thread handle
@param to_p Explained name in system_charset_info
@param end_p End of the to_p buffer
@param name Name to be converted
@param name_len Length of the name, in bytes
*/
static char* add_identifier(THD* thd, char *to_p, const char * end_p,
const char* name, size_t name_len)
{
uint res;
uint errors;
const char *conv_name, *conv_name_end;
char tmp_name[FN_REFLEN];
char conv_string[FN_REFLEN];
int quote;
DBUG_ENTER("add_identifier");
if (!name[name_len])
conv_name= name;
else
{
strnmov(tmp_name, name, name_len);
tmp_name[name_len]= 0;
conv_name= tmp_name;
}
res= strconvert(&my_charset_filename, conv_name, name_len,
system_charset_info,
conv_string, FN_REFLEN, &errors);
if (unlikely(!res || errors))
{
DBUG_PRINT("error", ("strconvert of '%s' failed with %u (errors: %u)", conv_name, res, errors));
conv_name= name;
conv_name_end= name + name_len;
}
else
{
DBUG_PRINT("info", ("conv '%s' -> '%s'", conv_name, conv_string));
conv_name= conv_string;
conv_name_end= conv_string + res;
}
quote= (likely(thd) ?
get_quote_char_for_identifier(thd, conv_name, res - 1) :
'`');
if (quote != EOF && (end_p - to_p > 2))
{
*(to_p++)= (char) quote;
while (*conv_name && (end_p - to_p - 1) > 0)
{
int length= system_charset_info->charlen(conv_name, conv_name_end);
if (length <= 0)
length= 1;
if (length == 1 && *conv_name == (char) quote)
{
if ((end_p - to_p) < 3)
break;
*(to_p++)= (char) quote;
*(to_p++)= *(conv_name++);
}
else if (((long) length) < (end_p - to_p))
{
to_p= strnmov(to_p, conv_name, length);
conv_name+= length;
}
else
break; /* string already filled */
}
if (end_p > to_p) {
*(to_p++)= (char) quote;
if (end_p > to_p)
*to_p= 0; /* terminate by NUL, but do not include it in the count */
}
}
else
to_p= strnmov(to_p, conv_name, end_p - to_p);
DBUG_RETURN(to_p);
}
/**
@brief Explain a path name by split it to database, table etc.
@details Break down the path name to its logic parts
(database, table, partition, subpartition).
filename_to_tablename cannot be used on partitions, due to the #P# part.
There can be up to 6 '#', #P# for partition, #SP# for subpartition
and #TMP# or #REN# for temporary or renamed partitions.
This should be used when something should be presented to a user in a
diagnostic, error etc. when it would be useful to know what a particular
file [and directory] means. Such as SHOW ENGINE STATUS, error messages etc.
Examples:
t1#P#p1 table t1 partition p1
t1#P#p1#SP#sp1 table t1 partition p1 subpartition sp1
t1#P#p1#SP#sp1#TMP# table t1 partition p1 subpartition sp1 temporary
t1#P#p1#SP#sp1#REN# table t1 partition p1 subpartition sp1 renamed
@param thd Thread handle
@param from Path name in my_charset_filename
Null terminated in my_charset_filename, normalized
to use '/' as directory separation character.
@param to Explained name in system_charset_info
@param to_length Size of to buffer
@param explain_mode Requested output format.
EXPLAIN_ALL_VERBOSE ->
[Database `db`, ]Table `tbl`[,[ Temporary| Renamed]
Partition `p` [, Subpartition `sp`]]
EXPLAIN_PARTITIONS_VERBOSE -> `db`.`tbl`
[[ Temporary| Renamed] Partition `p`
[, Subpartition `sp`]]
EXPLAIN_PARTITIONS_AS_COMMENT -> `db`.`tbl` |*
[,[ Temporary| Renamed] Partition `p`
[, Subpartition `sp`]] *|
(| is really a /, and it is all in one line)
@retval Length of returned string
*/
uint explain_filename(THD* thd,
const char *from,
char *to,
uint to_length,
enum_explain_filename_mode explain_mode)
{
char *to_p= to;
char *end_p= to_p + to_length;
const char *db_name= NULL;
size_t db_name_len= 0;
const char *table_name;
size_t table_name_len= 0;
const char *part_name= NULL;
size_t part_name_len= 0;
const char *subpart_name= NULL;
size_t subpart_name_len= 0;
uint part_type= NORMAL_PART_NAME;
const char *tmp_p;
DBUG_ENTER("explain_filename");
DBUG_PRINT("enter", ("from '%s'", from));
tmp_p= from;
table_name= from;
/*
If '/' then take last directory part as database.
'/' is the directory separator, not FN_LIB_CHAR
*/
while ((tmp_p= strchr(tmp_p, '/')))
{
db_name= table_name;
/* calculate the length */
db_name_len= (int)(tmp_p - db_name);
tmp_p++;
table_name= tmp_p;
}
tmp_p= table_name;
/* Look if there are partition tokens in the table name. */
while ((tmp_p= strchr(tmp_p, '#')))
{
tmp_p++;
switch (tmp_p[0]) {
case 'P':
case 'p':
if (tmp_p[1] == '#')
{
part_name= tmp_p + 2;
tmp_p+= 2;
}
break;
case 'S':
case 's':
if ((tmp_p[1] == 'P' || tmp_p[1] == 'p') && tmp_p[2] == '#')
{
part_name_len= (int)(tmp_p - part_name - 1);
subpart_name= tmp_p + 3;
tmp_p+= 3;
}
break;
case 'T':
case 't':
if ((tmp_p[1] == 'M' || tmp_p[1] == 'm') &&
(tmp_p[2] == 'P' || tmp_p[2] == 'p') &&
tmp_p[3] == '#' && !tmp_p[4])
{
part_type= TEMP_PART_NAME;
tmp_p+= 4;
}
break;
case 'R':
case 'r':
if ((tmp_p[1] == 'E' || tmp_p[1] == 'e') &&
(tmp_p[2] == 'N' || tmp_p[2] == 'n') &&
tmp_p[3] == '#' && !tmp_p[4])
{
part_type= RENAMED_PART_NAME;
tmp_p+= 4;
}
break;
default:
/* Not partition name part. */
;
}
}
if (part_name)
{
table_name_len= (int)(part_name - table_name - 3);
if (subpart_name)
subpart_name_len= strlen(subpart_name);
else
part_name_len= strlen(part_name);
if (part_type != NORMAL_PART_NAME)
{
if (subpart_name)
subpart_name_len-= 5;
else
part_name_len-= 5;
}
}
else
table_name_len= strlen(table_name);
if (db_name)
{
if (explain_mode == EXPLAIN_ALL_VERBOSE)
{
to_p= strnmov(to_p, ER_THD_OR_DEFAULT(thd, ER_DATABASE_NAME),
end_p - to_p);
*(to_p++)= ' ';
to_p= add_identifier(thd, to_p, end_p, db_name, db_name_len);
to_p= strnmov(to_p, ", ", end_p - to_p);
}
else
{
to_p= add_identifier(thd, to_p, end_p, db_name, db_name_len);
to_p= strnmov(to_p, ".", end_p - to_p);
}
}
if (explain_mode == EXPLAIN_ALL_VERBOSE)
{
to_p= strnmov(to_p, ER_THD_OR_DEFAULT(thd, ER_TABLE_NAME), end_p - to_p);
*(to_p++)= ' ';
to_p= add_identifier(thd, to_p, end_p, table_name, table_name_len);
}
else
to_p= add_identifier(thd, to_p, end_p, table_name, table_name_len);
if (part_name)
{
if (explain_mode == EXPLAIN_PARTITIONS_AS_COMMENT)
to_p= strnmov(to_p, " /* ", end_p - to_p);
else if (explain_mode == EXPLAIN_PARTITIONS_VERBOSE)
to_p= strnmov(to_p, " ", end_p - to_p);
else
to_p= strnmov(to_p, ", ", end_p - to_p);
if (part_type != NORMAL_PART_NAME)
{
if (part_type == TEMP_PART_NAME)
to_p= strnmov(to_p, ER_THD_OR_DEFAULT(thd, ER_TEMPORARY_NAME),
end_p - to_p);
else
to_p= strnmov(to_p, ER_THD_OR_DEFAULT(thd, ER_RENAMED_NAME),
end_p - to_p);
to_p= strnmov(to_p, " ", end_p - to_p);
}
to_p= strnmov(to_p, ER_THD_OR_DEFAULT(thd, ER_PARTITION_NAME),
end_p - to_p);
*(to_p++)= ' ';
to_p= add_identifier(thd, to_p, end_p, part_name, part_name_len);
if (subpart_name)
{
to_p= strnmov(to_p, ", ", end_p - to_p);
to_p= strnmov(to_p, ER_THD_OR_DEFAULT(thd, ER_SUBPARTITION_NAME),
end_p - to_p);
*(to_p++)= ' ';
to_p= add_identifier(thd, to_p, end_p, subpart_name, subpart_name_len);
}
if (explain_mode == EXPLAIN_PARTITIONS_AS_COMMENT)
to_p= strnmov(to_p, " */", end_p - to_p);
}
DBUG_PRINT("exit", ("to '%s'", to));
DBUG_RETURN((uint)(to_p - to));
}
/*
Translate a file name to a table name (WL #1324).
SYNOPSIS
filename_to_tablename()
from The file name in my_charset_filename.
to OUT The table name in system_charset_info.
to_length The size of the table name buffer.
RETURN
Table name length.
*/
uint filename_to_tablename(const char *from, char *to, size_t to_length,
bool stay_quiet)
{
uint errors;
size_t res;
DBUG_ENTER("filename_to_tablename");
DBUG_PRINT("enter", ("from '%s'", from));
res= strconvert(&my_charset_filename, from, FN_REFLEN,
system_charset_info, to, to_length, &errors);
if (unlikely(errors)) // Old 5.0 name
{
res= strxnmov(to, to_length, MYSQL50_TABLE_NAME_PREFIX, from, NullS) - to;
if (!stay_quiet)
sql_print_error("Invalid (old?) table or database name '%s'", from);
}
DBUG_PRINT("exit", ("to '%s'", to));
DBUG_RETURN((uint)res);
}
/**
Check if given string begins with "#mysql50#" prefix
@param name string to check cut
@retval
FALSE no prefix found
@retval
TRUE prefix found
*/
bool check_mysql50_prefix(const char *name)
{
return (name[0] == '#' &&
!strncmp(name, MYSQL50_TABLE_NAME_PREFIX,
MYSQL50_TABLE_NAME_PREFIX_LENGTH));
}
/**
Check if given string begins with "#mysql50#" prefix, cut it if so.
@param from string to check and cut
@param to[out] buffer for result string
@param to_length its size
@retval
0 no prefix found
@retval
non-0 result string length
*/
uint check_n_cut_mysql50_prefix(const char *from, char *to, size_t to_length)
{
if (check_mysql50_prefix(from))
return (uint) (strmake(to, from + MYSQL50_TABLE_NAME_PREFIX_LENGTH,
to_length - 1) - to);
return 0;
}
static bool check_if_frm_exists(char *path, const char *db, const char *table)
{
fn_format(path, table, db, reg_ext, MYF(0));
return !access(path, F_OK);
}
/*
Translate a table name to a file name (WL #1324).
SYNOPSIS
tablename_to_filename()
from The table name in system_charset_info.
to OUT The file name in my_charset_filename.
to_length The size of the file name buffer.
RETURN
File name length.
*/
uint tablename_to_filename(const char *from, char *to, size_t to_length)
{
uint errors, length;
DBUG_ENTER("tablename_to_filename");
DBUG_PRINT("enter", ("from '%s'", from));
if ((length= check_n_cut_mysql50_prefix(from, to, to_length)))
{
/*
Check if the name supplied is a valid mysql 5.0 name and
make the name a zero length string if it's not.
Note that just returning zero length is not enough :
a lot of places don't check the return value and expect
a zero terminated string.
*/
if (check_table_name(to, length, TRUE))
{
to[0]= 0;
length= 0;
}
DBUG_RETURN(length);
}
length= strconvert(system_charset_info, from, FN_REFLEN,
&my_charset_filename, to, to_length, &errors);
if (check_if_legal_tablename(to) &&
length + 4 < to_length)
{
memcpy(to + length, "@@@", 4);
length+= 3;
}
DBUG_PRINT("exit", ("to '%s'", to));
DBUG_RETURN(length);
}
/*
Creates path to a file: mysql_data_dir/db/table.ext
SYNOPSIS
build_table_filename()
buff Where to write result in my_charset_filename.
This may be the same as table_name.
bufflen buff size
db Database name in system_charset_info.
table_name Table name in system_charset_info.
ext File extension.
flags FN_FROM_IS_TMP or FN_TO_IS_TMP or FN_IS_TMP
table_name is temporary, do not change.
NOTES
Uses database and table name, and extension to create
a file name in mysql_data_dir. Database and table
names are converted from system_charset_info into "fscs".
Unless flags indicate a temporary table name.
'db' is always converted.
'ext' is not converted.
The conversion suppression is required for ALTER TABLE. This
statement creates intermediate tables. These are regular
(non-temporary) tables with a temporary name. Their path names must
be derivable from the table name. So we cannot use
build_tmptable_filename() for them.
RETURN
path length
*/
uint build_table_filename(char *buff, size_t bufflen, const char *db,
const char *table_name, const char *ext, uint flags)
{
char dbbuff[FN_REFLEN];
char tbbuff[FN_REFLEN];
DBUG_ENTER("build_table_filename");
DBUG_PRINT("enter", ("db: '%s' table_name: '%s' ext: '%s' flags: %x",
db, table_name, ext, flags));
(void) tablename_to_filename(db, dbbuff, sizeof(dbbuff));
/*
Check if this is a temporary table name. Allow it if a corresponding .frm
file exists.
*/
if (!(flags & FN_IS_TMP) &&
is_prefix(table_name, tmp_file_prefix) &&
strlen(table_name) < NAME_CHAR_LEN &&
check_if_frm_exists(tbbuff, dbbuff, table_name))
flags|= FN_IS_TMP;
if (flags & FN_IS_TMP) // FN_FROM_IS_TMP | FN_TO_IS_TMP
strmake(tbbuff, table_name, sizeof(tbbuff)-1);
else
(void) tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
char *end = buff + bufflen;
char *pos= strnmov(buff, mysql_data_home, bufflen-3);
/*
Add FN_LIBCHAR if mysql_data_home does not include it
In most cases mysql_data_home is just '.'
*/
if (pos[-1] != FN_LIBCHAR)
*pos++= FN_LIBCHAR;
pos= strxnmov(pos, end - 2 - pos, dbbuff,NullS);
*pos++= FN_LIBCHAR;
*pos= 0;
#ifdef USE_SYMDIR
if (!(flags & SKIP_SYMDIR_ACCESS))
{
unpack_dirname(buff, buff);
pos= strend(buff);
}
#endif
pos= strxnmov(pos, end - pos, tbbuff, ext, NullS);
DBUG_PRINT("exit", ("buff: '%s'", buff));
DBUG_RETURN((uint)(pos - buff));
}
/**
Create path to a temporary table mysql_tmpdir/#sql-temptable-1234-12-1
(i.e. to its .FRM file but without an extension).
@param thd The thread handle.
@param buff Where to write result in my_charset_filename.
@param bufflen buff size
@note
Uses current_pid, thread_id, and tmp_table counter to create
a file name in mysql_tmpdir.
@return Path length.
*/
uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen)
{
DBUG_ENTER("build_tmptable_filename");
char *p= strnmov(buff, mysql_tmpdir, bufflen);
my_snprintf(p, bufflen - (p - buff), "/%s-temptable-%lx-%llx-%x",
tmp_file_prefix, current_pid,
thd->thread_id, thd->tmp_table++);
if (lower_case_table_names)
{
/* Convert all except tmpdir to lower case */
my_casedn_str(files_charset_info, p);
}
size_t length= unpack_filename(buff, buff);
DBUG_PRINT("exit", ("buff: '%s'", buff));
DBUG_RETURN((uint)length);
}
/*
Create lower case paths for engines that requires them
*/
void build_lower_case_table_filename(char *buff, size_t bufflen,
const LEX_CSTRING *db,
const LEX_CSTRING *table,
uint flags)
{
char table_name[SAFE_NAME_LEN+1], db_name[SAFE_NAME_LEN+1];
DBUG_ASSERT(db->length <= SAFE_NAME_LEN && table->length <= SAFE_NAME_LEN);
memcpy(db_name, db->str, db->length);
db_name[db->length]= 0;
my_casedn_str(files_charset_info, db_name);
memcpy(table_name, table->str, table->length);
table_name[table->length]= 0;
my_casedn_str(files_charset_info, table_name);
build_table_filename(buff, bufflen, db_name, table_name, "",
flags & FN_IS_TMP);
}
/**
@brief construct a temporary shadow file name.
@details Make a shadow file name used by ALTER TABLE to construct the
modified table (with keeping the original). The modified table is then
moved back as original table. The name must start with the temp file
prefix so it gets filtered out by table files listing routines.
@param[out] buff buffer to receive the constructed name
@param bufflen size of buff
@param lpt alter table data structure
@retval path length
*/
uint build_table_shadow_filename(char *buff, size_t bufflen,
ALTER_PARTITION_PARAM_TYPE *lpt,
bool backup)
{
char tmp_name[FN_REFLEN];
my_snprintf(tmp_name, sizeof (tmp_name), "%s-%s-%lx-%s", tmp_file_prefix,
backup ? "backup" : "shadow",
(ulong) current_thd->thread_id, lpt->alter_info->table_name.str);
return build_table_filename(buff, bufflen, lpt->alter_info->db.str, tmp_name,
"", FN_IS_TMP);
}
/*
SYNOPSIS
mysql_write_frm()
lpt Struct carrying many parameters needed for this
method
flags Flags as defined below
WFRM_INITIAL_WRITE If set we need to prepare table before
creating the frm file
WFRM_INSTALL_SHADOW If set we should install the new frm
WFRM_KEEP_SHARE If set we know that the share is to be
retained and thus we should ensure share
object is correct, if not set we don't
set the new partition syntax string since
we know the share object is destroyed.
WFRM_PACK_FRM If set we should pack the frm file and delete
the frm file
RETURN VALUES
TRUE Error
FALSE Success
DESCRIPTION
A support method that creates a new frm file and in this process it
regenerates the partition data. It works fine also for non-partitioned
tables since it only handles partitioned data if it exists.
*/
/*
TODO: Partitioning atomic DDL refactoring: WFRM_WRITE_SHADOW
should be merged with create_table_impl(frm_only == true).
*/
bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags)
{
/*
Prepare table to prepare for writing a new frm file where the
partitions in add/drop state have temporarily changed their state
We set tmp_table to avoid get errors on naming of primary key index.
*/
int error= 0;
char path[FN_REFLEN+1];
char shadow_path[FN_REFLEN+1];
char shadow_frm_name[FN_REFLEN+1];
char frm_name[FN_REFLEN+1];
#ifdef WITH_PARTITION_STORAGE_ENGINE
char bak_path[FN_REFLEN+1];
char bak_frm_name[FN_REFLEN+1];
char *part_syntax_buf;
uint syntax_len;
partition_info *part_info= lpt->part_info;
#endif
DBUG_ENTER("mysql_write_frm");
/*
Build shadow frm file name
*/
build_table_shadow_filename(shadow_path, sizeof(shadow_path) - 1, lpt);
strxmov(shadow_frm_name, shadow_path, reg_ext, NullS);
if (flags & WFRM_WRITE_SHADOW)
{
if (mysql_prepare_create_table(lpt->thd, lpt->create_info, lpt->alter_info,
&lpt->db_options, lpt->table->file,
&lpt->key_info_buffer, &lpt->key_count,
C_ALTER_TABLE))
{
DBUG_RETURN(TRUE);
}
#ifdef WITH_PARTITION_STORAGE_ENGINE
{
partition_info *part_info= lpt->table->part_info;
if (part_info)
{
part_syntax_buf= generate_partition_syntax_for_frm(lpt->thd, part_info,
&syntax_len, lpt->create_info, lpt->alter_info);
if (!part_syntax_buf)
DBUG_RETURN(TRUE);
part_info->part_info_string= part_syntax_buf;
part_info->part_info_len= syntax_len;
}
}
#endif
/* Write shadow frm file */
lpt->create_info->table_options= lpt->db_options;
LEX_CUSTRING frm= build_frm_image(lpt->thd, lpt->alter_info->table_name,
lpt->create_info,
lpt->alter_info->create_list,
lpt->key_count, lpt->key_info_buffer,
lpt->table->file);
if (!frm.str)
{
error= 1;
goto end;
}
int error= writefile(shadow_frm_name, lpt->alter_info->db.str,
lpt->alter_info->table_name.str,
lpt->create_info->tmp_table(), frm.str, frm.length);
my_free(const_cast<uchar*>(frm.str));
if (unlikely(error) ||
unlikely(lpt->table->file->
ha_create_partitioning_metadata(shadow_path,
NULL, CHF_CREATE_FLAG)))
{
mysql_file_delete(key_file_frm, shadow_frm_name, MYF(0));
error= 1;
goto end;
}
}
#ifdef WITH_PARTITION_STORAGE_ENGINE
if (flags & WFRM_WRITE_CONVERTED_TO)
{
THD *thd= lpt->thd;
Alter_table_ctx *alter_ctx= lpt->alter_ctx;
HA_CREATE_INFO *create_info= lpt->create_info;
LEX_CSTRING new_path= { alter_ctx->get_new_path(), 0 };
partition_info *work_part_info= thd->work_part_info;
handlerton *db_type= create_info->db_type;
DBUG_ASSERT(lpt->table->part_info);
DBUG_ASSERT(lpt->table->part_info == part_info);
handler *file= ((ha_partition *)(lpt->table->file))->get_child_handlers()[0];
DBUG_ASSERT(file);
new_path.length= strlen(new_path.str);
strxnmov(frm_name, sizeof(frm_name) - 1, new_path.str, reg_ext, NullS);
create_info->alias= alter_ctx->table_name;
thd->work_part_info= NULL;
create_info->db_type= work_part_info->default_engine_type;
/* NOTE: partitioned temporary tables are not supported. */
DBUG_ASSERT(!create_info->tmp_table());
if (ddl_log_create_table(thd, part_info, create_info->db_type, &new_path,
&alter_ctx->new_db, &alter_ctx->new_name, true) ||
ERROR_INJECT("create_before_create_frm"))
DBUG_RETURN(TRUE);
if (mysql_prepare_create_table(thd, create_info, lpt->alter_info,
&lpt->db_options, file,
&lpt->key_info_buffer, &lpt->key_count,
C_ALTER_TABLE))
DBUG_RETURN(TRUE);
lpt->create_info->table_options= lpt->db_options;
LEX_CUSTRING frm= build_frm_image(thd, alter_ctx->new_name, create_info,
lpt->alter_info->create_list,
lpt->key_count, lpt->key_info_buffer,
file);
if (unlikely(!frm.str))
DBUG_RETURN(TRUE);
thd->work_part_info= work_part_info;
create_info->db_type= db_type;
ERROR_INJECT("alter_partition_after_create_frm");
error= writefile(frm_name, alter_ctx->new_db.str, alter_ctx->new_name.str,
create_info->tmp_table(), frm.str, frm.length);
my_free((void *) frm.str);
if (unlikely(error) || ERROR_INJECT("alter_partition_after_write_frm"))
{
mysql_file_delete(key_file_frm, frm_name, MYF(0));
DBUG_RETURN(TRUE);
}
DBUG_RETURN(false);
}
if (flags & WFRM_BACKUP_ORIGINAL)
{
build_table_filename(path, sizeof(path) - 1, lpt->alter_info->db.str,
lpt->alter_info->table_name.str, "", 0);
strxnmov(frm_name, sizeof(frm_name), path, reg_ext, NullS);
build_table_shadow_filename(bak_path, sizeof(bak_path) - 1, lpt, true);
strxmov(bak_frm_name, bak_path, reg_ext, NullS);
DDL_LOG_MEMORY_ENTRY *main_entry= part_info->main_entry;
mysql_mutex_lock(&LOCK_gdl);
if (write_log_replace_frm(lpt, part_info->list->entry_pos,
(const char*) bak_path,
(const char*) path) ||
ddl_log_write_execute_entry(part_info->list->entry_pos,
&part_info->execute_entry))
{
mysql_mutex_unlock(&LOCK_gdl);
DBUG_RETURN(TRUE);
}
mysql_mutex_unlock(&LOCK_gdl);
part_info->main_entry= main_entry;
if (mysql_file_rename(key_file_frm, frm_name, bak_frm_name, MYF(MY_WME)))
DBUG_RETURN(TRUE);
if (lpt->table->file->ha_create_partitioning_metadata(bak_path, path,
CHF_RENAME_FLAG))
DBUG_RETURN(TRUE);
}
#else /* !WITH_PARTITION_STORAGE_ENGINE */
DBUG_ASSERT(!(flags & WFRM_BACKUP_ORIGINAL));
#endif /* !WITH_PARTITION_STORAGE_ENGINE */
if (flags & WFRM_INSTALL_SHADOW)
{
#ifdef WITH_PARTITION_STORAGE_ENGINE
partition_info *part_info= lpt->part_info;
#endif
/*
Build frm file name
*/
build_table_filename(path, sizeof(path) - 1, lpt->alter_info->db.str,
lpt->alter_info->table_name.str, "", 0);
strxnmov(frm_name, sizeof(frm_name), path, reg_ext, NullS);
/*
When we are changing to use new frm file we need to ensure that we
don't collide with another thread in process to open the frm file.
We start by deleting the .frm file and possible .par file. Then we
write to the DDL log that we have completed the delete phase by
increasing the phase of the log entry. Next step is to rename the
new .frm file and the new .par file to the real name. After
completing this we write a new phase to the log entry that will
deactivate it.
*/
if (!(flags & WFRM_BACKUP_ORIGINAL) && (
mysql_file_delete(key_file_frm, frm_name, MYF(MY_WME))
#ifdef WITH_PARTITION_STORAGE_ENGINE
|| lpt->table->file->ha_create_partitioning_metadata(path, shadow_path,
CHF_DELETE_FLAG) ||
ddl_log_increment_phase(part_info->main_entry->entry_pos) ||
(ddl_log_sync(), FALSE)
#endif
))
{
error= 1;
goto err;
}
if (mysql_file_rename(key_file_frm, shadow_frm_name, frm_name, MYF(MY_WME))
#ifdef WITH_PARTITION_STORAGE_ENGINE
|| lpt->table->file->ha_create_partitioning_metadata(path, shadow_path,
CHF_RENAME_FLAG)
#endif
)
{
error= 1;
goto err;
}
#ifdef WITH_PARTITION_STORAGE_ENGINE
if (part_info && (flags & WFRM_KEEP_SHARE))
{
TABLE_SHARE *share= lpt->table->s;
char *tmp_part_syntax_str;
part_syntax_buf= generate_partition_syntax_for_frm(lpt->thd,
part_info, &syntax_len, lpt->create_info, lpt->alter_info);
if (!part_syntax_buf)
{
error= 1;
goto err;
}
if (share->partition_info_buffer_size < syntax_len + 1)
{
share->partition_info_buffer_size= syntax_len+1;
if (!(tmp_part_syntax_str= (char*) strmake_root(&share->mem_root,
part_syntax_buf,
syntax_len)))
{
error= 1;
goto err;
}
share->partition_info_str= tmp_part_syntax_str;
}
else
memcpy((char*) share->partition_info_str, part_syntax_buf,
syntax_len + 1);
share->partition_info_str_len= part_info->part_info_len= syntax_len;
part_info->part_info_string= part_syntax_buf;
}
#endif
err:
#ifdef WITH_PARTITION_STORAGE_ENGINE
ddl_log_increment_phase(part_info->main_entry->entry_pos);
part_info->main_entry= NULL;
(void) ddl_log_sync();
#endif
;
}
end:
DBUG_RETURN(error);
}
/*
SYNOPSIS
write_bin_log()
thd Thread object
clear_error is clear_error to be called
query Query to log
query_length Length of query
is_trans if the event changes either
a trans or non-trans engine.
RETURN VALUES
NONE
DESCRIPTION
Write the binlog if open, routine used in multiple places in this
file
*/
int write_bin_log(THD *thd, bool clear_error,
char const *query, ulong query_length, bool is_trans)
{
int error= 0;
if (mysql_bin_log.is_open())
{
int errcode= 0;
thd_proc_info(thd, "Writing to binlog");
if (clear_error)