forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdd_table_share.cc
More file actions
2338 lines (1962 loc) · 79.2 KB
/
dd_table_share.cc
File metadata and controls
2338 lines (1962 loc) · 79.2 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 (c) 2014, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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-1301 USA */
#include "sql/dd_table_share.h"
#include "my_config.h"
#include <string.h>
#include <algorithm>
#include <optional>
#include <string>
#include <type_traits>
#include "lex_string.h"
#include "m_string.h"
#include "map_helpers.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_compare.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/plugin.h"
#include "mysql/udf_registration_types.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/dd/collection.h"
#include "sql/dd/dd_table.h" // dd::FIELD_NAME_SEPARATOR_CHAR
#include "sql/dd/dd_tablespace.h" // dd::get_tablespace_name
// TODO: Avoid exposing dd/impl headers in public files.
#include "sql/dd/impl/utils.h" // dd::eat_str
#include "sql/dd/properties.h" // dd::Properties
#include "sql/dd/string_type.h"
#include "sql/dd/types/check_constraint.h" // dd::Check_constraint
#include "sql/dd/types/column.h" // dd::enum_column_types
#include "sql/dd/types/column_type_element.h" // dd::Column_type_element
#include "sql/dd/types/foreign_key.h"
#include "sql/dd/types/foreign_key_element.h" // dd::Foreign_key_element
#include "sql/dd/types/index.h" // dd::Index
#include "sql/dd/types/index_element.h" // dd::Index_element
#include "sql/dd/types/partition.h" // dd::Partition
#include "sql/dd/types/partition_value.h" // dd::Partition_value
#include "sql/dd/types/table.h" // dd::Table
#include "sql/default_values.h" // prepare_default_value_buffer...
#include "sql/error_handler.h" // Internal_error_handler
#include "sql/field.h"
#include "sql/gis/srid.h"
#include "sql/handler.h"
#include "sql/key.h"
#include "sql/log.h"
#include "sql/partition_element.h" // partition_element
#include "sql/partition_info.h" // partition_info
#include "sql/sql_bitmap.h"
#include "sql/sql_check_constraint.h" // Sql_check_constraint_share_list
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_list.h"
#include "sql/sql_partition.h" // generate_partition_syntax
#include "sql/sql_plugin.h" // plugin_unlock
#include "sql/sql_plugin_ref.h"
#include "sql/sql_table.h" // primary_key_name
#include "sql/strfunc.h" // lex_cstring_handle
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/thd_raii.h"
#include "typelib.h"
namespace histograms {
class Histogram;
} // namespace histograms
enum_field_types dd_get_old_field_type(dd::enum_column_types type) {
switch (type) {
case dd::enum_column_types::DECIMAL:
return MYSQL_TYPE_DECIMAL;
case dd::enum_column_types::TINY:
return MYSQL_TYPE_TINY;
case dd::enum_column_types::SHORT:
return MYSQL_TYPE_SHORT;
case dd::enum_column_types::LONG:
return MYSQL_TYPE_LONG;
case dd::enum_column_types::FLOAT:
return MYSQL_TYPE_FLOAT;
case dd::enum_column_types::DOUBLE:
return MYSQL_TYPE_DOUBLE;
case dd::enum_column_types::TYPE_NULL:
return MYSQL_TYPE_NULL;
case dd::enum_column_types::TIMESTAMP:
return MYSQL_TYPE_TIMESTAMP;
case dd::enum_column_types::LONGLONG:
return MYSQL_TYPE_LONGLONG;
case dd::enum_column_types::INT24:
return MYSQL_TYPE_INT24;
case dd::enum_column_types::DATE:
return MYSQL_TYPE_DATE;
case dd::enum_column_types::TIME:
return MYSQL_TYPE_TIME;
case dd::enum_column_types::DATETIME:
return MYSQL_TYPE_DATETIME;
case dd::enum_column_types::YEAR:
return MYSQL_TYPE_YEAR;
case dd::enum_column_types::NEWDATE:
return MYSQL_TYPE_NEWDATE;
case dd::enum_column_types::VARCHAR:
return MYSQL_TYPE_VARCHAR;
case dd::enum_column_types::BIT:
return MYSQL_TYPE_BIT;
case dd::enum_column_types::TIMESTAMP2:
return MYSQL_TYPE_TIMESTAMP2;
case dd::enum_column_types::DATETIME2:
return MYSQL_TYPE_DATETIME2;
case dd::enum_column_types::TIME2:
return MYSQL_TYPE_TIME2;
case dd::enum_column_types::NEWDECIMAL:
return MYSQL_TYPE_NEWDECIMAL;
case dd::enum_column_types::ENUM:
return MYSQL_TYPE_ENUM;
case dd::enum_column_types::SET:
return MYSQL_TYPE_SET;
case dd::enum_column_types::TINY_BLOB:
return MYSQL_TYPE_TINY_BLOB;
case dd::enum_column_types::MEDIUM_BLOB:
return MYSQL_TYPE_MEDIUM_BLOB;
case dd::enum_column_types::LONG_BLOB:
return MYSQL_TYPE_LONG_BLOB;
case dd::enum_column_types::BLOB:
return MYSQL_TYPE_BLOB;
case dd::enum_column_types::VAR_STRING:
return MYSQL_TYPE_VAR_STRING;
case dd::enum_column_types::STRING:
return MYSQL_TYPE_STRING;
case dd::enum_column_types::GEOMETRY:
return MYSQL_TYPE_GEOMETRY;
case dd::enum_column_types::JSON:
return MYSQL_TYPE_JSON;
default:
assert(!"Should not hit here"); /* purecov: deadcode */
}
return MYSQL_TYPE_LONG;
}
/** For enum in dd::Index */
static enum ha_key_alg dd_get_old_index_algorithm_type(
dd::Index::enum_index_algorithm type) {
switch (type) {
case dd::Index::IA_SE_SPECIFIC:
return HA_KEY_ALG_SE_SPECIFIC;
case dd::Index::IA_BTREE:
return HA_KEY_ALG_BTREE;
case dd::Index::IA_RTREE:
return HA_KEY_ALG_RTREE;
case dd::Index::IA_HASH:
return HA_KEY_ALG_HASH;
case dd::Index::IA_FULLTEXT:
return HA_KEY_ALG_FULLTEXT;
default:
assert(!"Should not hit here"); /* purecov: deadcode */
}
return HA_KEY_ALG_SE_SPECIFIC;
}
/*
Check if the given key_part is suitable to be promoted as part of
primary key.
*/
bool is_suitable_for_primary_key(KEY_PART_INFO *key_part, Field *table_field) {
// Index on virtual generated columns is not allowed to be PK
// even when the conditions below are true, so this case must be
// rejected here.
if (table_field->is_virtual_gcol()) return false;
/*
If the key column is of NOT NULL BLOB type, then it
will definitely have key prefix. And if key part prefix size
is equal to the BLOB column max size, then we can promote
it to primary key.
*/
if (!table_field->is_nullable() && table_field->type() == MYSQL_TYPE_BLOB &&
table_field->field_length == key_part->length)
return true;
if (table_field->is_nullable() ||
table_field->key_length() != key_part->length)
return false;
return true;
}
/**
Finalize preparation of TABLE_SHARE from dd::Table object by filling
in remaining info about columns and keys.
This code similar to code in open_binary_frm(). Can be re-written
independent to other efforts later.
*/
static bool prepare_share(THD *thd, TABLE_SHARE *share,
const dd::Table *table_def) {
my_bitmap_map *bitmaps;
handler *handler_file = nullptr;
// Mark 'system' tables (tables with one row) to help the Optimizer.
share->system =
((share->max_rows == 1) && (share->min_rows == 1) && (share->keys == 0));
bool use_extended_sk = ha_check_storage_engine_flag(
share->db_type(), HTON_SUPPORTS_EXTENDED_KEYS);
share->m_histograms =
new malloc_unordered_map<uint, const histograms::Histogram *>(
PSI_INSTRUMENT_ME);
// Setup other fields =====================================================
/* Allocate handler */
if (!(handler_file = get_new_handler(share, (share->m_part_info != nullptr),
&share->mem_root, share->db_type()))) {
my_error(ER_INVALID_DD_OBJECT, MYF(0), share->path.str,
"Failed to initialize handler.");
return true;
}
if (handler_file->set_ha_share_ref(&share->ha_share)) {
my_error(ER_INVALID_DD_OBJECT, MYF(0), share->path.str, "");
return true;
}
share->db_low_byte_first = handler_file->low_byte_first();
/* Fix key->name and key_part->field */
if (share->keys) {
KEY *keyinfo;
KEY_PART_INFO *key_part;
uint primary_key = (uint)(find_type(primary_key_name, &share->keynames,
FIND_TYPE_NO_PREFIX) -
1);
longlong ha_option = handler_file->ha_table_flags();
keyinfo = share->key_info;
key_part = keyinfo->key_part;
dd::Table::Index_collection::const_iterator idx_it(
table_def->indexes().begin());
for (uint key = 0; key < share->keys; key++, keyinfo++) {
/*
Skip hidden dd::Index objects so idx_it is in sync with key index
and keyinfo pointer.
*/
while ((*idx_it)->is_hidden()) {
++idx_it;
continue;
}
uint usable_parts = 0;
keyinfo->name = share->keynames.type_names[key];
/* Check that fulltext and spatial keys have correct algorithm set. */
assert(!(share->key_info[key].flags & HA_FULLTEXT) ||
share->key_info[key].algorithm == HA_KEY_ALG_FULLTEXT);
assert(!(share->key_info[key].flags & HA_SPATIAL) ||
share->key_info[key].algorithm == HA_KEY_ALG_RTREE);
if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME)) {
/*
If the UNIQUE key doesn't have NULL columns and is not a part key
declare this as a primary key.
*/
primary_key = key;
for (uint i = 0; i < keyinfo->user_defined_key_parts; i++) {
Field *table_field = key_part[i].field;
if (is_suitable_for_primary_key(&key_part[i], table_field) == false) {
primary_key = MAX_KEY;
break;
}
}
/*
Check that dd::Index::is_candidate_key() used by SEs works in
the same way as above call to is_suitable_for_primary_key().
*/
assert((primary_key == key) == (*idx_it)->is_candidate_key());
}
dd::Index::Index_elements::const_iterator idx_el_it(
(*idx_it)->elements().begin());
for (uint i = 0; i < keyinfo->user_defined_key_parts; key_part++, i++) {
/*
Skip hidden Index_element objects so idx_el_it is in sync with
i and key_part pointer.
*/
while ((*idx_el_it)->is_hidden()) {
++idx_el_it;
continue;
}
Field *field = key_part->field;
key_part->type = field->key_type();
if (field->is_nullable()) {
key_part->null_offset = field->null_offset(share->default_values);
key_part->null_bit = field->null_bit;
key_part->store_length += HA_KEY_NULL_LENGTH;
keyinfo->flags |= HA_NULL_PART_KEY;
keyinfo->key_length += HA_KEY_NULL_LENGTH;
}
if (field->type() == MYSQL_TYPE_BLOB ||
field->real_type() == MYSQL_TYPE_VARCHAR ||
field->type() == MYSQL_TYPE_GEOMETRY) {
key_part->store_length += HA_KEY_BLOB_LENGTH;
if (i + 1 <= keyinfo->user_defined_key_parts)
keyinfo->key_length += HA_KEY_BLOB_LENGTH;
}
key_part->init_flags();
if (field->is_virtual_gcol()) keyinfo->flags |= HA_VIRTUAL_GEN_KEY;
setup_key_part_field(share, handler_file, primary_key, keyinfo, key, i,
&usable_parts, true);
field->set_flag(PART_KEY_FLAG);
if (key == primary_key) {
field->set_flag(PRI_KEY_FLAG);
/*
If this field is part of the primary key and all keys contains
the primary key, then we can use any key to find this column
*/
if (ha_option & HA_PRIMARY_KEY_IN_READ_INDEX) {
if (field->key_length() == key_part->length &&
!field->is_flag_set(BLOB_FLAG))
field->part_of_key = share->keys_in_use;
if (field->part_of_sortkey.is_set(key))
field->part_of_sortkey = share->keys_in_use;
}
}
if (field->key_length() != key_part->length) {
#ifndef TO_BE_DELETED_ON_PRODUCTION
if (field->type() == MYSQL_TYPE_NEWDECIMAL) {
/*
Fix a fatal error in decimal key handling that causes crashes
on Innodb. We fix it by reducing the key length so that
InnoDB never gets a too big key when searching.
This allows the end user to do an ALTER TABLE to fix the
error.
*/
keyinfo->key_length -= (key_part->length - field->key_length());
key_part->store_length -=
(uint16)(key_part->length - field->key_length());
key_part->length = (uint16)field->key_length();
LogErr(ERROR_LEVEL, ER_TABLE_WRONG_KEY_DEFINITION,
share->table_name.str, share->table_name.str);
push_warning_printf(thd, Sql_condition::SL_WARNING,
ER_CRASHED_ON_USAGE,
"Found wrong key definition in %s; "
"Please do \"ALTER TABLE `%s` FORCE\" to fix "
"it!",
share->table_name.str, share->table_name.str);
share->crashed = true; // Marker for CHECK TABLE
continue;
}
#endif
key_part->key_part_flag |= HA_PART_KEY_SEG;
}
/*
Check that dd::Index_element::is_prefix() used by SEs works in
the same way as code which sets HA_PART_KEY_SEG flag.
*/
assert((*idx_el_it)->is_prefix() ==
static_cast<bool>(key_part->key_part_flag & HA_PART_KEY_SEG));
++idx_el_it;
}
/*
KEY::flags is fully set-up at this point so we can copy it to
KEY::actual_flags.
*/
keyinfo->actual_flags = keyinfo->flags;
if (primary_key < MAX_KEY && key != primary_key &&
(ha_option & HA_PRIMARY_KEY_IN_READ_INDEX))
key_part += add_pk_parts_to_sk(keyinfo, key, share->key_info,
primary_key, share, handler_file,
&usable_parts, use_extended_sk);
/* Skip unused key parts if they exist */
key_part += keyinfo->unused_key_parts;
keyinfo->usable_key_parts = usable_parts; // Filesort
share->max_key_length =
std::max(share->max_key_length,
keyinfo->key_length + keyinfo->user_defined_key_parts);
share->total_key_length += keyinfo->key_length;
/*
MERGE tables do not have unique indexes. But every key could be
an unique index on the underlying MyISAM table. (Bug #10400)
*/
if ((keyinfo->flags & HA_NOSAME) ||
(ha_option & HA_ANY_INDEX_MAY_BE_UNIQUE))
share->max_unique_length =
std::max(share->max_unique_length, keyinfo->key_length);
++idx_it;
}
if (primary_key < MAX_KEY && (share->keys_in_use.is_set(primary_key))) {
share->primary_key = primary_key;
/*
If we are using an integer as the primary key then allow the user to
refer to it as '_rowid'
*/
if (share->key_info[primary_key].user_defined_key_parts == 1) {
Field *field = share->key_info[primary_key].key_part[0].field;
if (field && field->result_type() == INT_RESULT) {
/* note that fieldnr here (and rowid_field_offset) starts from 1 */
share->rowid_field_offset =
(share->key_info[primary_key].key_part[0].fieldnr);
}
}
} else
share->primary_key = MAX_KEY; // we do not have a primary key
} else
share->primary_key = MAX_KEY;
destroy(handler_file);
if (share->found_next_number_field) {
Field *reg_field = *share->found_next_number_field;
/* Check that the auto-increment column is the first column of some key. */
if ((int)(share->next_number_index = (uint)find_ref_key(
share->key_info, share->keys, share->default_values,
reg_field, &share->next_number_key_offset,
&share->next_number_keypart)) < 0) {
my_error(ER_INVALID_DD_OBJECT, MYF(0), share->path.str,
"Wrong field definition.");
return true;
} else
reg_field->set_flag(AUTO_INCREMENT_FLAG);
}
if (share->blob_fields) {
Field **ptr;
uint k, *save;
/* Store offsets to blob fields to find them fast */
if (!(share->blob_field = save = (uint *)share->mem_root.Alloc(
(uint)(share->blob_fields * sizeof(uint)))))
return true; // OOM error message already reported
for (k = 0, ptr = share->field; *ptr; ptr++, k++) {
if ((*ptr)->is_flag_set(BLOB_FLAG) || (*ptr)->is_array()) (*save++) = k;
}
}
share->column_bitmap_size = bitmap_buffer_size(share->fields);
if (!(bitmaps = (my_bitmap_map *)share->mem_root.Alloc(
share->column_bitmap_size))) {
// OOM error message already reported
return true; /* purecov: inspected */
}
bitmap_init(&share->all_set, bitmaps, share->fields);
bitmap_set_all(&share->all_set);
return false;
}
/** Fill tablespace name from dd::Tablespace. */
static bool fill_tablespace_from_dd(THD *thd, TABLE_SHARE *share,
const dd::Table *tab_obj) {
DBUG_TRACE;
return dd::get_tablespace_name<dd::Table>(thd, tab_obj, &share->tablespace,
&share->mem_root);
}
/**
Convert row format value used in DD to corresponding value in old
row_type enum.
*/
static row_type dd_get_old_row_format(dd::Table::enum_row_format new_format) {
switch (new_format) {
case dd::Table::RF_FIXED:
return ROW_TYPE_FIXED;
case dd::Table::RF_DYNAMIC:
return ROW_TYPE_DYNAMIC;
case dd::Table::RF_COMPRESSED:
return ROW_TYPE_COMPRESSED;
case dd::Table::RF_REDUNDANT:
return ROW_TYPE_REDUNDANT;
case dd::Table::RF_COMPACT:
return ROW_TYPE_COMPACT;
case dd::Table::RF_PAGED:
return ROW_TYPE_PAGED;
default:
assert(0);
break;
}
return ROW_TYPE_FIXED;
}
/** Fill TABLE_SHARE from dd::Table object */
static bool fill_share_from_dd(THD *thd, TABLE_SHARE *share,
const dd::Table *tab_obj) {
const dd::Properties &table_options = tab_obj->options();
// Secondary storage engine.
if (table_options.exists("secondary_engine")) {
table_options.get("secondary_engine", &share->secondary_engine,
&share->mem_root);
} else {
// If no secondary storage engine is set, the share cannot
// represent a table in a secondary engine.
assert(!share->is_secondary_engine());
}
// Read table engine type
LEX_CSTRING engine_name = lex_cstring_handle(tab_obj->engine());
if (share->is_secondary_engine())
engine_name = {share->secondary_engine.str, share->secondary_engine.length};
plugin_ref tmp_plugin = ha_resolve_by_name_raw(thd, engine_name);
if (tmp_plugin) {
#ifndef NDEBUG
handlerton *hton = plugin_data<handlerton *>(tmp_plugin);
#endif
assert(hton && ha_storage_engine_is_enabled(hton));
assert(!ha_check_storage_engine_flag(hton, HTON_NOT_USER_SELECTABLE));
plugin_unlock(nullptr, share->db_plugin);
share->db_plugin = my_plugin_lock(nullptr, &tmp_plugin);
} else {
my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), engine_name.str);
return true;
}
// Set temporarily a good value for db_low_byte_first.
assert(ha_legacy_type(share->db_type()) != DB_TYPE_ISAM);
share->db_low_byte_first = true;
// Read other table options
uint64 option_value = 0;
bool bool_opt = false;
// Max rows
if (table_options.exists("max_rows"))
table_options.get("max_rows", &share->max_rows);
// Min rows
if (table_options.exists("min_rows"))
table_options.get("min_rows", &share->min_rows);
// Options from HA_CREATE_INFO::table_options/TABLE_SHARE::db_create_options.
share->db_create_options = 0;
table_options.get("pack_record", &bool_opt);
if (bool_opt) share->db_create_options |= HA_OPTION_PACK_RECORD;
if (table_options.exists("pack_keys")) {
table_options.get("pack_keys", &bool_opt);
share->db_create_options |=
bool_opt ? HA_OPTION_PACK_KEYS : HA_OPTION_NO_PACK_KEYS;
}
if (table_options.exists("checksum")) {
table_options.get("checksum", &bool_opt);
if (bool_opt) share->db_create_options |= HA_OPTION_CHECKSUM;
}
if (table_options.exists("delay_key_write")) {
table_options.get("delay_key_write", &bool_opt);
if (bool_opt) share->db_create_options |= HA_OPTION_DELAY_KEY_WRITE;
}
if (table_options.exists("stats_persistent")) {
table_options.get("stats_persistent", &bool_opt);
share->db_create_options |=
bool_opt ? HA_OPTION_STATS_PERSISTENT : HA_OPTION_NO_STATS_PERSISTENT;
}
share->db_options_in_use = share->db_create_options;
// Average row length
if (table_options.exists("avg_row_length")) {
table_options.get("avg_row_length", &option_value);
share->avg_row_length = static_cast<ulong>(option_value);
}
// Collation ID
share->table_charset = dd_get_mysql_charset(tab_obj->collation_id());
if (!share->table_charset) {
// Unknown collation
if (use_mb(default_charset_info)) {
/* Warn that we may be changing the size of character columns */
LogErr(WARNING_LEVEL, ER_INVALID_CHARSET_AND_DEFAULT_IS_MB,
share->path.str);
}
share->table_charset = default_charset_info;
}
// Row type. First one really used by the storage engine.
share->real_row_type = dd_get_old_row_format(tab_obj->row_format());
// Then one which was explicitly specified by user for this table.
if (table_options.exists("row_type")) {
table_options.get("row_type", &option_value);
share->row_type =
dd_get_old_row_format((dd::Table::enum_row_format)option_value);
} else
share->row_type = ROW_TYPE_DEFAULT;
// Stats_sample_pages
if (table_options.exists("stats_sample_pages"))
table_options.get("stats_sample_pages", &share->stats_sample_pages);
// Stats_auto_recalc
if (table_options.exists("stats_auto_recalc")) {
table_options.get("stats_auto_recalc", &option_value);
share->stats_auto_recalc = (enum_stats_auto_recalc)option_value;
}
// mysql version
share->mysql_version = tab_obj->mysql_version_id();
// TODO-POST-MERGE-TO-TRUNK: Initialize new field
// share->last_checked_for_upgrade? Or access tab_obj directly where
// needed?
// key block size
table_options.get("key_block_size", &share->key_block_size);
// Prepare the default_value buffer.
if (prepare_default_value_buffer_and_table_share(thd, *tab_obj, share))
return true;
// Storage media flags
if (table_options.exists("storage")) {
uint32 storage_option_value = 0;
table_options.get("storage", &storage_option_value);
share->default_storage_media =
static_cast<ha_storage_media>(storage_option_value);
} else
share->default_storage_media = HA_SM_DEFAULT;
// Read tablespace name
if (fill_tablespace_from_dd(thd, share, tab_obj)) return true;
// Read comment
dd::String_type comment = tab_obj->comment();
if (comment.length()) {
share->comment.str =
strmake_root(&share->mem_root, comment.c_str(), comment.length() + 1);
share->comment.length = comment.length();
}
// Copy SE attributes into share's memroot
share->engine_attribute = LexStringDupRootUnlessEmpty(
&share->mem_root, tab_obj->engine_attribute());
share->secondary_engine_attribute = LexStringDupRootUnlessEmpty(
&share->mem_root, tab_obj->secondary_engine_attribute());
// Read Connection strings
if (table_options.exists("connection_string"))
table_options.get("connection_string", &share->connect_string,
&share->mem_root);
// Read Compress string
if (table_options.exists("compress"))
table_options.get("compress", &share->compress, &share->mem_root);
// Read Encrypt string
if (table_options.exists("encrypt_type"))
table_options.get("encrypt_type", &share->encrypt_type, &share->mem_root);
// Read secondary load option.
if (table_options.exists("secondary_load"))
table_options.get("secondary_load", &share->secondary_load);
return false;
}
/**
Calculate number of bits used for the column in the record preamble
(aka null bits number).
*/
static uint column_preamble_bits(const dd::Column *col_obj) {
uint result = 0;
if (col_obj->is_nullable()) result++;
if (col_obj->type() == dd::enum_column_types::BIT) {
bool treat_bit_as_char = false;
(void)col_obj->options().get("treat_bit_as_char", &treat_bit_as_char);
if (!treat_bit_as_char) result += col_obj->char_length() & 7;
}
return result;
}
inline void get_auto_flags(const dd::Column &col_obj, uint &auto_flags) {
/*
For DEFAULT it is possible to have CURRENT_TIMESTAMP or a
generation expression.
*/
if (!col_obj.default_option().empty()) {
// We're only matching the prefix because there may be parameters
// e.g. CURRENT_TIMESTAMP(6). Regular strings won't match as they
// are preceded by the charset and CURRENT_TIMESTAMP as a default
// expression gets converted to now().
if (col_obj.default_option().compare(0, 17, "CURRENT_TIMESTAMP") == 0) {
// The only allowed patterns are "CURRENT_TIMESTAMP" and
// "CURRENT_TIMESTAP(<integer>)". Stored functions with names
// starting with "CURRENT_TIMESTAMP" should be filtered out before
// we get here.
assert(col_obj.default_option().size() == 17 ||
(col_obj.default_option().size() >= 20 &&
col_obj.default_option()[17] == '(' &&
col_obj.default_option()[col_obj.default_option().size() - 1] ==
')'));
auto_flags |= Field::DEFAULT_NOW;
} else {
auto_flags |= Field::GENERATED_FROM_EXPRESSION;
}
}
/*
For ON UPDATE the only option which is supported
at this point is CURRENT_TIMESTAMP.
*/
if (!col_obj.update_option().empty()) auto_flags |= Field::ON_UPDATE_NOW;
if (col_obj.is_auto_increment()) auto_flags |= Field::NEXT_NUMBER;
/*
Columns can't have AUTO_INCREMENT and DEFAULT/ON UPDATE CURRENT_TIMESTAMP at
the same time.
*/
assert(!((auto_flags & (Field::DEFAULT_NOW | Field::ON_UPDATE_NOW |
Field::GENERATED_FROM_EXPRESSION)) != 0 &&
(auto_flags & Field::NEXT_NUMBER) != 0));
}
static Field *make_field(const dd::Column &col_obj, const CHARSET_INFO *charset,
TABLE_SHARE *share, uchar *ptr, uchar *null_pos,
size_t null_bit) {
auto field_type = dd_get_old_field_type(col_obj.type());
auto field_length = col_obj.char_length();
const dd::Properties &column_options = col_obj.options();
// Reconstruct auto_flags
auto auto_flags = static_cast<uint>(Field::NONE);
get_auto_flags(col_obj, auto_flags);
// Read Interval TYPELIB
TYPELIB *interval = nullptr;
if (field_type == MYSQL_TYPE_ENUM || field_type == MYSQL_TYPE_SET) {
//
// Allocate space for interval (column elements)
//
size_t interval_parts = col_obj.elements_count();
interval = (TYPELIB *)share->mem_root.Alloc(sizeof(TYPELIB));
interval->type_names = (const char **)share->mem_root.Alloc(
sizeof(char *) * (interval_parts + 1));
interval->type_names[interval_parts] = nullptr;
interval->type_lengths =
(uint *)share->mem_root.Alloc(sizeof(uint) * interval_parts);
interval->count = interval_parts;
interval->name = nullptr;
//
// Iterate through all the column elements
//
for (const dd::Column_type_element *ce : col_obj.elements()) {
// Read the enum/set element name
dd::String_type element_name = ce->name();
uint pos = ce->index() - 1;
interval->type_lengths[pos] = static_cast<uint>(element_name.length());
interval->type_names[pos] = strmake_root(
&share->mem_root, element_name.c_str(), element_name.length());
}
}
// Column name
char *name = nullptr;
dd::String_type s = col_obj.name();
assert(!s.empty());
name = strmake_root(&share->mem_root, s.c_str(), s.length());
name[s.length()] = '\0';
uint decimals;
// Decimals
if (field_type == MYSQL_TYPE_DECIMAL || field_type == MYSQL_TYPE_NEWDECIMAL) {
assert(col_obj.is_numeric_scale_null() == false);
decimals = col_obj.numeric_scale();
} else if (field_type == MYSQL_TYPE_FLOAT ||
field_type == MYSQL_TYPE_DOUBLE) {
decimals = col_obj.is_numeric_scale_null() ? DECIMAL_NOT_SPECIFIED
: col_obj.numeric_scale();
} else
decimals = 0;
auto geom_type = Field::GEOM_GEOMETRY;
// Read geometry sub type
if (field_type == MYSQL_TYPE_GEOMETRY) {
uint32 sub_type = 0;
column_options.get("geom_type", &sub_type);
geom_type = static_cast<Field::geometry_type>(sub_type);
}
bool treat_bit_as_char = false;
if (field_type == MYSQL_TYPE_BIT) {
column_options.get("treat_bit_as_char", &treat_bit_as_char);
}
return make_field(*THR_MALLOC, share, ptr, field_length, null_pos, null_bit,
field_type, charset, geom_type, auto_flags, interval, name,
col_obj.is_nullable(), col_obj.is_zerofill(),
col_obj.is_unsigned(), decimals, treat_bit_as_char, 0,
col_obj.srs_id(), col_obj.is_array());
}
/**
Add Field constructed according to column metadata from dd::Column
object to TABLE_SHARE.
*/
static bool fill_column_from_dd(THD *thd, TABLE_SHARE *share,
const dd::Column *col_obj, uchar *null_pos,
uint null_bit_pos, uchar *rec_pos,
uint field_nr) {
char *name = nullptr;
enum_field_types field_type;
const CHARSET_INFO *charset = nullptr;
Field *reg_field;
ha_storage_media field_storage;
column_format_type field_column_format;
//
// Read column details from dd table
//
// Column name
dd::String_type s = col_obj->name();
assert(!s.empty());
name = strmake_root(&share->mem_root, s.c_str(), s.length());
name[s.length()] = '\0';
const dd::Properties *column_options = &col_obj->options();
// Type
field_type = dd_get_old_field_type(col_obj->type());
// Reconstruct auto_flags
auto auto_flags = static_cast<uint>(Field::NONE);
get_auto_flags(*col_obj, auto_flags);
bool treat_bit_as_char = false;
if (field_type == MYSQL_TYPE_BIT)
column_options->get("treat_bit_as_char", &treat_bit_as_char);
// Collation ID
charset = dd_get_mysql_charset(col_obj->collation_id());
if (charset == nullptr) {
my_printf_error(ER_UNKNOWN_COLLATION,
"invalid collation id %llu for table %s, column %s", MYF(0),
col_obj->collation_id(), share->table_name.str, name);
if (thd->is_error()) return true;
charset = default_charset_info;
}
// Decimals
if (field_type == MYSQL_TYPE_DECIMAL || field_type == MYSQL_TYPE_NEWDECIMAL)
assert(col_obj->is_numeric_scale_null() == false);
// Read geometry sub type
if (field_type == MYSQL_TYPE_GEOMETRY) {
uint32 sub_type;
column_options->get("geom_type", &sub_type);
}
// Read values of storage media and column format options
if (column_options->exists("storage")) {
uint32 option_value = 0;
column_options->get("storage", &option_value);
field_storage = static_cast<ha_storage_media>(option_value);
} else
field_storage = HA_SM_DEFAULT;
if (column_options->exists("column_format")) {
uint32 option_value = 0;
column_options->get("column_format", &option_value);
field_column_format = static_cast<column_format_type>(option_value);
} else
field_column_format = COLUMN_FORMAT_TYPE_DEFAULT;
// Read Interval TYPELIB
TYPELIB *interval = nullptr;
if (field_type == MYSQL_TYPE_ENUM || field_type == MYSQL_TYPE_SET) {
//
// Allocate space for interval (column elements)
//
size_t interval_parts = col_obj->elements_count();
interval = (TYPELIB *)share->mem_root.Alloc(sizeof(TYPELIB));
interval->type_names = (const char **)share->mem_root.Alloc(
sizeof(char *) * (interval_parts + 1));
interval->type_names[interval_parts] = nullptr;
interval->type_lengths =
(uint *)share->mem_root.Alloc(sizeof(uint) * interval_parts);
interval->count = interval_parts;
interval->name = nullptr;
//
// Iterate through all the column elements
//
for (const dd::Column_type_element *ce : col_obj->elements()) {
// Read the enum/set element name
dd::String_type element_name = ce->name();
uint pos = ce->index() - 1;
interval->type_lengths[pos] = static_cast<uint>(element_name.length());
interval->type_names[pos] = strmake_root(
&share->mem_root, element_name.c_str(), element_name.length());
}
}
//
// Create FIELD